ada: Small fixes to handling of private views in instances
[official-gcc.git] / gcc / ada / sem_ch12.adb
bloba65bd0fdfb53a545bd8fff68d2370fd5fb264a64
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Einfo; use Einfo;
30 with Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Fname; use Fname;
36 with Fname.UF; use Fname.UF;
37 with Freeze; use Freeze;
38 with Ghost; use Ghost;
39 with Itypes; use Itypes;
40 with Lib; use Lib;
41 with Lib.Load; use Lib.Load;
42 with Lib.Xref; use Lib.Xref;
43 with Nlists; use Nlists;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Rident; use Rident;
48 with Restrict; use Restrict;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch7; use Sem_Ch7;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Dim; use Sem_Dim;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Elab; use Sem_Elab;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Type; use Sem_Type;
67 with Sem_Util; use Sem_Util;
68 with Sem_Warn; use Sem_Warn;
69 with Stand; use Stand;
70 with Sinfo; use Sinfo;
71 with Sinfo.Nodes; use Sinfo.Nodes;
72 with Sinfo.Utils; use Sinfo.Utils;
73 with Sinfo.CN; use Sinfo.CN;
74 with Sinput; use Sinput;
75 with Sinput.L; use Sinput.L;
76 with Snames; use Snames;
77 with Stringt; use Stringt;
78 with Uname; use Uname;
79 with Table;
80 with Tbuild; use Tbuild;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
83 with Warnsw; use Warnsw;
85 with GNAT.HTable;
87 package body Sem_Ch12 is
89 ----------------------------------------------------------
90 -- Implementation of Generic Analysis and Instantiation --
91 ----------------------------------------------------------
93 -- GNAT implements generics by macro expansion. No attempt is made to share
94 -- generic instantiations (for now). Analysis of a generic definition does
95 -- not perform any expansion action, but the expander must be called on the
96 -- tree for each instantiation, because the expansion may of course depend
97 -- on the generic actuals. All of this is best achieved as follows:
99 -- a) Semantic analysis of a generic unit is performed on a copy of the
100 -- tree for the generic unit. All tree modifications that follow analysis
101 -- do not affect the original tree. Links are kept between the original
102 -- tree and the copy, in order to recognize non-local references within
103 -- the generic, and propagate them to each instance (recall that name
104 -- resolution is done on the generic declaration: generics are not really
105 -- macros). This is summarized in the following diagram:
107 -- .-----------. .----------.
108 -- | semantic |<--------------| generic |
109 -- | copy | | unit |
110 -- | |==============>| |
111 -- |___________| global |__________|
112 -- references | | |
113 -- | | |
114 -- .-----|--|.
115 -- | .-----|---.
116 -- | | .----------.
117 -- | | | generic |
118 -- |__| | |
119 -- |__| instance |
120 -- |__________|
122 -- b) Each instantiation copies the original tree, and inserts into it a
123 -- series of declarations that describe the mapping between generic formals
124 -- and actuals. For example, a generic In OUT parameter is an object
125 -- renaming of the corresponding actual, etc. Generic IN parameters are
126 -- constant declarations.
128 -- c) In order to give the right visibility for these renamings, we use
129 -- a different scheme for package and subprogram instantiations. For
130 -- packages, the list of renamings is inserted into the package
131 -- specification, before the visible declarations of the package. The
132 -- renamings are analyzed before any of the text of the instance, and are
133 -- thus visible at the right place. Furthermore, outside of the instance,
134 -- the generic parameters are visible and denote their corresponding
135 -- actuals.
137 -- For subprograms, we create a container package to hold the renamings
138 -- and the subprogram instance itself. Analysis of the package makes the
139 -- renaming declarations visible to the subprogram. After analyzing the
140 -- package, the defining entity for the subprogram is touched-up so that
141 -- it appears declared in the current scope, and not inside the container
142 -- package.
144 -- If the instantiation is a compilation unit, the container package is
145 -- given the same name as the subprogram instance. This ensures that
146 -- the elaboration procedure called by the binder, using the compilation
147 -- unit name, calls in fact the elaboration procedure for the package.
149 -- Not surprisingly, private types complicate this approach. By saving in
150 -- the original generic object the non-local references, we guarantee that
151 -- the proper entities are referenced at the point of instantiation.
152 -- However, for private types, this by itself does not insure that the
153 -- proper VIEW of the entity is used (the full type may be visible at the
154 -- point of generic definition, but not at instantiation, or vice-versa).
155 -- In order to reference the proper view, we special-case any reference
156 -- to private types in the generic object, by saving both views, one in
157 -- the generic and one in the semantic copy. At time of instantiation, we
158 -- check whether the two views are consistent, and exchange declarations if
159 -- necessary, in order to restore the correct visibility. Similarly, if
160 -- the instance view is private when the generic view was not, we perform
161 -- the exchange. After completing the instantiation, we restore the
162 -- current visibility. The flag Has_Private_View marks identifiers in the
163 -- the generic unit that require checking.
165 -- Visibility within nested generic units requires special handling.
166 -- Consider the following scheme:
168 -- type Global is ... -- outside of generic unit.
169 -- generic ...
170 -- package Outer is
171 -- ...
172 -- type Semi_Global is ... -- global to inner.
174 -- generic ... -- 1
175 -- procedure inner (X1 : Global; X2 : Semi_Global);
177 -- procedure in2 is new inner (...); -- 4
178 -- end Outer;
180 -- package New_Outer is new Outer (...); -- 2
181 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
183 -- The semantic analysis of Outer captures all occurrences of Global.
184 -- The semantic analysis of Inner (at 1) captures both occurrences of
185 -- Global and Semi_Global.
187 -- At point 2 (instantiation of Outer), we also produce a generic copy
188 -- of Inner, even though Inner is, at that point, not being instantiated.
189 -- (This is just part of the semantic analysis of New_Outer).
191 -- Critically, references to Global within Inner must be preserved, while
192 -- references to Semi_Global should not preserved, because they must now
193 -- resolve to an entity within New_Outer. To distinguish between these, we
194 -- use a global variable, Current_Instantiated_Parent, which is set when
195 -- performing a generic copy during instantiation (at 2). This variable is
196 -- used when performing a generic copy that is not an instantiation, but
197 -- that is nested within one, as the occurrence of 1 within 2. The analysis
198 -- of a nested generic only preserves references that are global to the
199 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
200 -- determine whether a reference is external to the given parent.
202 -- The instantiation at point 3 requires no special treatment. The method
203 -- works as well for further nestings of generic units, but of course the
204 -- variable Current_Instantiated_Parent must be stacked because nested
205 -- instantiations can occur, e.g. the occurrence of 4 within 2.
207 -- The instantiation of package and subprogram bodies is handled in a
208 -- similar manner, except that it is delayed until after semantic
209 -- analysis is complete. In this fashion complex cross-dependencies
210 -- between several package declarations and bodies containing generics
211 -- can be compiled which otherwise would diagnose spurious circularities.
213 -- For example, it is possible to compile two packages A and B that
214 -- have the following structure:
216 -- package A is package B is
217 -- generic ... generic ...
218 -- package G_A is package G_B is
220 -- with B; with A;
221 -- package body A is package body B is
222 -- package N_B is new G_B (..) package N_A is new G_A (..)
224 -- The table Pending_Instantiations in package Inline is used to keep
225 -- track of body instantiations that are delayed in this manner. Inline
226 -- handles the actual calls to do the body instantiations. This activity
227 -- is part of Inline, since the processing occurs at the same point, and
228 -- for essentially the same reason, as the handling of inlined routines.
230 ----------------------------------------------
231 -- Detection of Instantiation Circularities --
232 ----------------------------------------------
234 -- If we have a chain of instantiations that is circular, this is static
235 -- error which must be detected at compile time. The detection of these
236 -- circularities is carried out at the point that we insert a generic
237 -- instance spec or body. If there is a circularity, then the analysis of
238 -- the offending spec or body will eventually result in trying to load the
239 -- same unit again, and we detect this problem as we analyze the package
240 -- instantiation for the second time.
242 -- At least in some cases after we have detected the circularity, we get
243 -- into trouble if we try to keep going. The following flag is set if a
244 -- circularity is detected, and used to abandon compilation after the
245 -- messages have been posted.
247 Circularity_Detected : Boolean := False;
248 -- It should really be reset upon encountering a new main unit, but in
249 -- practice we do not use multiple main units so this is not critical.
251 -----------------------------------------
252 -- Implementation of Generic Contracts --
253 -----------------------------------------
255 -- A "contract" is a collection of aspects and pragmas that either verify a
256 -- property of a construct at runtime or classify the data flow to and from
257 -- the construct in some fashion.
259 -- Generic packages, subprograms and their respective bodies may be subject
260 -- to the following contract-related aspects or pragmas collectively known
261 -- as annotations:
263 -- package subprogram [body]
264 -- Abstract_State Always_Terminates
265 -- Initial_Condition Contract_Cases
266 -- Initializes Depends
267 -- Exceptional_Cases
268 -- Extensions_Visible
269 -- Global
270 -- package body Post
271 -- Refined_State Post_Class
272 -- Postcondition
273 -- Pre
274 -- Pre_Class
275 -- Precondition
276 -- Refined_Depends
277 -- Refined_Global
278 -- Refined_Post
279 -- Subprogram_Variant
280 -- Test_Case
282 -- Most package contract annotations utilize forward references to classify
283 -- data declared within the package [body]. Subprogram annotations then use
284 -- the classifications to further refine them. These inter dependencies are
285 -- problematic with respect to the implementation of generics because their
286 -- analysis, capture of global references and instantiation does not mesh
287 -- well with the existing mechanism.
289 -- 1) Analysis of generic contracts is carried out the same way non-generic
290 -- contracts are analyzed:
292 -- 1.1) General rule - a contract is analyzed after all related aspects
293 -- and pragmas are analyzed. This is done by routines
295 -- Analyze_Package_Body_Contract
296 -- Analyze_Package_Contract
297 -- Analyze_Subprogram_Body_Contract
298 -- Analyze_Subprogram_Contract
300 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
301 -- are processed.
303 -- 1.3) Compilation unit body - the contract is analyzed at the end of
304 -- the body declaration list.
306 -- 1.4) Package - the contract is analyzed at the end of the private or
307 -- visible declarations, prior to analyzing the contracts of any nested
308 -- packages or subprograms.
310 -- 1.5) Package body - the contract is analyzed at the end of the body
311 -- declaration list, prior to analyzing the contracts of any nested
312 -- packages or subprograms.
314 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
315 -- package or a subprogram, then its contract is analyzed at the end of
316 -- the enclosing declarations, otherwise the subprogram is a compilation
317 -- unit 1.2).
319 -- 1.7) Subprogram body - if the subprogram body is declared inside a
320 -- block, a package body or a subprogram body, then its contract is
321 -- analyzed at the end of the enclosing declarations, otherwise the
322 -- subprogram is a compilation unit 1.3).
324 -- 2) Capture of global references within contracts is done after capturing
325 -- global references within the generic template. There are two reasons for
326 -- this delay - pragma annotations are not part of the generic template in
327 -- the case of a generic subprogram declaration, and analysis of contracts
328 -- is delayed.
330 -- Contract-related source pragmas within generic templates are prepared
331 -- for delayed capture of global references by routine
333 -- Create_Generic_Contract
335 -- The routine associates these pragmas with the contract of the template.
336 -- In the case of a generic subprogram declaration, the routine creates
337 -- generic templates for the pragmas declared after the subprogram because
338 -- they are not part of the template.
340 -- generic -- template starts
341 -- procedure Gen_Proc (Input : Integer); -- template ends
342 -- pragma Precondition (Input > 0); -- requires own template
344 -- 2.1) The capture of global references with aspect specifications and
345 -- source pragmas that apply to a generic unit must be suppressed when
346 -- the generic template is being processed because the contracts have not
347 -- been analyzed yet. Any attempts to capture global references at that
348 -- point will destroy the Associated_Node linkages and leave the template
349 -- undecorated. This delay is controlled by routine
351 -- Requires_Delayed_Save
353 -- 2.2) The real capture of global references within a contract is done
354 -- after the contract has been analyzed, by routine
356 -- Save_Global_References_In_Contract
358 -- 3) The instantiation of a generic contract occurs as part of the
359 -- instantiation of the contract owner. Generic subprogram declarations
360 -- require additional processing when the contract is specified by pragmas
361 -- because the pragmas are not part of the generic template. This is done
362 -- by routine
364 -- Instantiate_Subprogram_Contract
366 --------------------------------------------------
367 -- Formal packages and partial parameterization --
368 --------------------------------------------------
370 -- When compiling a generic, a formal package is a local instantiation. If
371 -- declared with a box, its generic formals are visible in the enclosing
372 -- generic. If declared with a partial list of actuals, those actuals that
373 -- are defaulted (covered by an Others clause, or given an explicit box
374 -- initialization) are also visible in the enclosing generic, while those
375 -- that have a corresponding actual are not.
377 -- In our source model of instantiation, the same visibility must be
378 -- present in the spec and body of an instance: the names of the formals
379 -- that are defaulted must be made visible within the instance, and made
380 -- invisible (hidden) after the instantiation is complete, so that they
381 -- are not accessible outside of the instance.
383 -- In a generic, a formal package is treated like a special instantiation.
384 -- Our Ada 95 compiler handled formals with and without box in different
385 -- ways. With partial parameterization, we use a single model for both.
386 -- We create a package declaration that consists of the specification of
387 -- the generic package, and a set of declarations that map the actuals
388 -- into local renamings, just as we do for bona fide instantiations. For
389 -- defaulted parameters and formals with a box, we copy directly the
390 -- declarations of the formals into this local package. The result is a
391 -- package whose visible declarations may include generic formals. This
392 -- package is only used for type checking and visibility analysis, and
393 -- never reaches the back end, so it can freely violate the placement
394 -- rules for generic formal declarations.
396 -- The list of declarations (renamings and copies of formals) is built
397 -- by Analyze_Associations, just as for regular instantiations.
399 -- At the point of instantiation, conformance checking must be applied only
400 -- to those parameters that were specified in the formals. We perform this
401 -- checking by creating another internal instantiation, this one including
402 -- only the renamings and the formals (the rest of the package spec is not
403 -- relevant to conformance checking). We can then traverse two lists: the
404 -- list of actuals in the instance that corresponds to the formal package,
405 -- and the list of actuals produced for this bogus instantiation. We apply
406 -- the conformance rules to those actuals that are not defaulted, i.e.
407 -- which still appear as generic formals.
409 -- When we compile an instance body we must make the right parameters
410 -- visible again. The predicate Is_Generic_Formal indicates which of the
411 -- formals should have its Is_Hidden flag reset.
413 -----------------------
414 -- Local subprograms --
415 -----------------------
417 procedure Abandon_Instantiation (N : Node_Id);
418 pragma No_Return (Abandon_Instantiation);
419 -- Posts an error message "instantiation abandoned" at the indicated node
420 -- and then raises the exception Instantiation_Error to do it.
422 procedure Analyze_Formal_Array_Type
423 (T : in out Entity_Id;
424 Def : Node_Id);
425 -- A formal array type is treated like an array type declaration, and
426 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
427 -- in-out, because in the case of an anonymous type the entity is
428 -- actually created in the procedure.
430 -- The following procedures treat other kinds of formal parameters
432 procedure Analyze_Formal_Derived_Interface_Type
433 (N : Node_Id;
434 T : Entity_Id;
435 Def : Node_Id);
437 procedure Analyze_Formal_Derived_Type
438 (N : Node_Id;
439 T : Entity_Id;
440 Def : Node_Id);
442 procedure Analyze_Formal_Interface_Type
443 (N : Node_Id;
444 T : Entity_Id;
445 Def : Node_Id);
447 -- The following subprograms create abbreviated declarations for formal
448 -- scalar types. We introduce an anonymous base of the proper class for
449 -- each of them, and define the formals as constrained first subtypes of
450 -- their bases. The bounds are expressions that are non-static in the
451 -- generic.
453 procedure Analyze_Formal_Decimal_Fixed_Point_Type
454 (T : Entity_Id; Def : Node_Id);
455 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
456 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
457 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
458 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
459 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
460 (T : Entity_Id; Def : Node_Id);
462 procedure Analyze_Formal_Private_Type
463 (N : Node_Id;
464 T : Entity_Id;
465 Def : Node_Id);
466 -- Creates a new private type, which does not require completion
468 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
469 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
471 procedure Analyze_Generic_Formal_Part (N : Node_Id);
472 -- Analyze generic formal part
474 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
475 -- Create a new access type with the given designated type
477 function Analyze_Associations
478 (I_Node : Node_Id;
479 Formals : List_Id;
480 F_Copy : List_Id) return List_Id;
481 -- At instantiation time, build the list of associations between formals
482 -- and actuals. Each association becomes a renaming declaration for the
483 -- formal entity. F_Copy is the analyzed list of formals in the generic
484 -- copy. It is used to apply legality checks to the actuals. I_Node is the
485 -- instantiation node itself.
487 procedure Analyze_Subprogram_Instantiation
488 (N : Node_Id;
489 K : Entity_Kind);
491 procedure Build_Instance_Compilation_Unit_Nodes
492 (N : Node_Id;
493 Act_Body : Node_Id;
494 Act_Decl : Node_Id);
495 -- This procedure is used in the case where the generic instance of a
496 -- subprogram body or package body is a library unit. In this case, the
497 -- original library unit node for the generic instantiation must be
498 -- replaced by the resulting generic body, and a link made to a new
499 -- compilation unit node for the generic declaration. The argument N is
500 -- the original generic instantiation. Act_Body and Act_Decl are the body
501 -- and declaration of the instance (either package body and declaration
502 -- nodes or subprogram body and declaration nodes depending on the case).
503 -- On return, the node N has been rewritten with the actual body.
505 function Build_Subprogram_Decl_Wrapper
506 (Formal_Subp : Entity_Id) return Node_Id;
507 -- Ada 2022 allows formal subprograms to carry pre/postconditions.
508 -- At the point of instantiation these contracts apply to uses of
509 -- the actual subprogram. This is implemented by creating wrapper
510 -- subprograms instead of the renamings previously used to link
511 -- formal subprograms and the corresponding actuals. If the actual
512 -- is not an entity (e.g. an attribute reference) a renaming is
513 -- created to handle the expansion of the attribute.
515 function Build_Subprogram_Body_Wrapper
516 (Formal_Subp : Entity_Id;
517 Actual_Name : Node_Id) return Node_Id;
518 -- The body of the wrapper is a call to the actual, with the generated
519 -- pre/postconditon checks added.
521 procedure Check_Abbreviated_Instance
522 (N : Node_Id;
523 Parent_Installed : in out Boolean);
524 -- If the name of the generic unit in an abbreviated instantiation is an
525 -- expanded name, then the prefix may be an instance and the selector may
526 -- designate a child unit. If the parent is installed as a result of this
527 -- call, then Parent_Installed is set True, otherwise Parent_Installed is
528 -- unchanged by the call.
530 -- This routine needs to be called for declaration nodes of formal objects,
531 -- types and subprograms to check whether they are the copy, present in the
532 -- visible part of the abbreviated instantiation of formal packages, of the
533 -- declaration node of their corresponding formal parameter in the template
534 -- of the formal package, as specified by RM 12.7(10/2), so as to establish
535 -- the proper context for their analysis.
537 procedure Check_Access_Definition (N : Node_Id);
538 -- Subsidiary routine to null exclusion processing. Perform an assertion
539 -- check on Ada version and the presence of an access definition in N.
541 procedure Check_Formal_Packages (P_Id : Entity_Id);
542 -- Apply the following to all formal packages in generic associations.
543 -- Restore the visibility of the formals of the instance that are not
544 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
545 -- created for formal instances that are not defaulted.
547 procedure Check_Formal_Package_Instance
548 (Formal_Pack : Entity_Id;
549 Actual_Pack : Entity_Id);
550 -- Verify that the actuals of the actual instance match the actuals of
551 -- the template for a formal package that is not declared with a box.
553 procedure Check_Forward_Instantiation (Decl : Node_Id);
554 -- If the generic is a local entity and the corresponding body has not
555 -- been seen yet, flag enclosing packages to indicate that it will be
556 -- elaborated after the generic body. Subprograms declared in the same
557 -- package cannot be inlined by the front end because front-end inlining
558 -- requires a strict linear order of elaboration.
560 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
561 -- Check if some association between formals and actuals requires to make
562 -- visible primitives of a tagged type, and make those primitives visible.
563 -- Return the list of primitives whose visibility is modified (to restore
564 -- their visibility later through Restore_Hidden_Primitives). If no
565 -- candidate is found then return No_Elist.
567 procedure Check_Hidden_Child_Unit
568 (N : Node_Id;
569 Gen_Unit : Entity_Id;
570 Act_Decl_Id : Entity_Id);
571 -- If the generic unit is an implicit child instance within a parent
572 -- instance, we need to make an explicit test that it is not hidden by
573 -- a child instance of the same name and parent.
575 procedure Check_Generic_Actuals
576 (Instance : Entity_Id;
577 Is_Formal_Box : Boolean);
578 -- Similar to previous one. Check the actuals in the instantiation,
579 -- whose views can change between the point of instantiation and the point
580 -- of instantiation of the body. In addition, mark the generic renamings
581 -- as generic actuals, so that they are not compatible with other actuals.
582 -- Recurse on an actual that is a formal package whose declaration has
583 -- a box.
585 function Contains_Instance_Of
586 (Inner : Entity_Id;
587 Outer : Entity_Id;
588 N : Node_Id) return Boolean;
589 -- Inner is instantiated within the generic Outer. Check whether Inner
590 -- directly or indirectly contains an instance of Outer or of one of its
591 -- parents, in the case of a subunit. Each generic unit holds a list of
592 -- the entities instantiated within (at any depth). This procedure
593 -- determines whether the set of such lists contains a cycle, i.e. an
594 -- illegal circular instantiation.
596 function Denotes_Formal_Package
597 (Pack : Entity_Id;
598 On_Exit : Boolean := False;
599 Instance : Entity_Id := Empty) return Boolean;
600 -- Returns True if E is a formal package of an enclosing generic, or
601 -- the actual for such a formal in an enclosing instantiation. If such
602 -- a package is used as a formal in an nested generic, or as an actual
603 -- in a nested instantiation, the visibility of ITS formals should not
604 -- be modified. When called from within Restore_Private_Views, the flag
605 -- On_Exit is true, to indicate that the search for a possible enclosing
606 -- instance should ignore the current one. In that case Instance denotes
607 -- the declaration for which this is an actual. This declaration may be
608 -- an instantiation in the source, or the internal instantiation that
609 -- corresponds to the actual for a formal package.
611 function Earlier (N1, N2 : Node_Id) return Boolean;
612 -- Yields True if N1 and N2 appear in the same compilation unit,
613 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
614 -- traversal of the tree for the unit. Used to determine the placement
615 -- of freeze nodes for instance bodies that may depend on other instances.
617 function Find_Actual_Type
618 (Typ : Entity_Id;
619 Gen_Type : Entity_Id) return Entity_Id;
620 -- When validating the actual types of a child instance, check whether
621 -- the formal is a formal type of the parent unit, and retrieve the current
622 -- actual for it. Typ is the entity in the analyzed formal type declaration
623 -- (component or index type of an array type, or designated type of an
624 -- access formal) and Gen_Type is the enclosing analyzed formal array
625 -- or access type. The desired actual may be a formal of a parent, or may
626 -- be declared in a formal package of a parent. In both cases it is a
627 -- generic actual type because it appears within a visible instance.
628 -- Finally, it may be declared in a parent unit without being a formal
629 -- of that unit, in which case it must be retrieved by visibility.
630 -- Ambiguities may still arise if two homonyms are declared in two formal
631 -- packages, and the prefix of the formal type may be needed to resolve
632 -- the ambiguity in the instance ???
634 procedure Freeze_Package_Instance
635 (N : Node_Id;
636 Gen_Body : Node_Id;
637 Gen_Decl : Node_Id;
638 Act_Id : Entity_Id);
639 -- If the instantiation happens textually before the body of the generic,
640 -- the instantiation of the body must be analyzed after the generic body,
641 -- and not at the point of instantiation. Such early instantiations can
642 -- happen if the generic and the instance appear in a package declaration
643 -- because the generic body can only appear in the corresponding package
644 -- body. Early instantiations can also appear if generic, instance and
645 -- body are all in the declarative part of a subprogram or entry. Entities
646 -- of packages that are early instantiations are delayed, and their freeze
647 -- node appears after the generic body. This rather complex machinery is
648 -- needed when nested instantiations are present, because the source does
649 -- not carry any indication of where the corresponding instance bodies must
650 -- be installed and frozen.
652 procedure Freeze_Subprogram_Instance
653 (N : Node_Id;
654 Gen_Body : Node_Id;
655 Pack_Id : Entity_Id);
656 -- The generic body may appear textually after the instance, including
657 -- in the proper body of a stub, or within a different package instance.
658 -- Given that the instance can only be elaborated after the generic, we
659 -- place freeze nodes for the instance and/or for packages that may enclose
660 -- the instance and the generic, so that the back-end can establish the
661 -- proper order of elaboration.
663 function Get_Associated_Node (N : Node_Id) return Node_Id;
664 -- In order to propagate semantic information back from the analyzed copy
665 -- to the original generic, we maintain links between selected nodes in the
666 -- generic and their corresponding copies. At the end of generic analysis,
667 -- the routine Save_Global_References traverses the generic tree, examines
668 -- the semantic information, and preserves the links to those nodes that
669 -- contain global information. At instantiation, the information from the
670 -- associated node is placed on the new copy, so that name resolution is
671 -- not repeated.
673 -- Three kinds of source nodes have associated nodes:
675 -- a) those that can reference (denote) entities, that is identifiers,
676 -- character literals, expanded_names, operator symbols, operators,
677 -- and attribute reference nodes. These nodes have an Entity field
678 -- and are the set of nodes that are in N_Has_Entity.
680 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
682 -- c) selected components (N_Selected_Component)
684 -- For the first class, the associated node preserves the entity if it is
685 -- global. If the generic contains nested instantiations, the associated
686 -- node itself has been recopied, and a chain of them must be followed.
688 -- For aggregates, the associated node allows retrieval of the type, which
689 -- may otherwise not appear in the generic. The view of this type may be
690 -- different between generic and instantiation, and the full view can be
691 -- installed before the instantiation is analyzed. For aggregates of type
692 -- extensions, the same view exchange may have to be performed for some of
693 -- the ancestor types, if their view is private at the point of
694 -- instantiation.
696 -- Nodes that are selected components in the parse tree may be rewritten
697 -- as expanded names after resolution, and must be treated as potential
698 -- entity holders, which is why they also have an Associated_Node.
700 -- Nodes that do not come from source, such as freeze nodes, do not appear
701 -- in the generic tree, and need not have an associated node.
703 -- The associated node is stored in the Associated_Node field. Note that
704 -- this field overlaps Entity, which is fine, because the whole point is
705 -- that we don't need or want the normal Entity field in this situation.
707 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
708 -- Traverse the Exchanged_Views list to see if a type was private
709 -- and has already been flipped during this phase of instantiation.
711 function Has_Contracts (Decl : Node_Id) return Boolean;
712 -- Determine whether a formal subprogram has a Pre- or Postcondition,
713 -- in which case a subprogram wrapper has to be built for the actual.
715 procedure Hide_Current_Scope;
716 -- When instantiating a generic child unit, the parent context must be
717 -- present, but the instance and all entities that may be generated
718 -- must be inserted in the current scope. We leave the current scope
719 -- on the stack, but make its entities invisible to avoid visibility
720 -- problems. This is reversed at the end of the instantiation. This is
721 -- not done for the instantiation of the bodies, which only require the
722 -- instances of the generic parents to be in scope.
724 function In_Main_Context (E : Entity_Id) return Boolean;
725 -- Check whether an instantiation is in the context of the main unit.
726 -- Used to determine whether its body should be elaborated to allow
727 -- front-end inlining.
729 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
730 -- Add the context clause of the unit containing a generic unit to a
731 -- compilation unit that is, or contains, an instantiation.
733 procedure Init_Env;
734 -- Establish environment for subsequent instantiation. Separated from
735 -- Save_Env because data-structures for visibility handling must be
736 -- initialized before call to Check_Generic_Child_Unit.
738 procedure Inline_Instance_Body
739 (N : Node_Id;
740 Gen_Unit : Entity_Id;
741 Act_Decl : Node_Id);
742 -- If front-end inlining is requested, instantiate the package body,
743 -- and preserve the visibility of its compilation unit, to insure
744 -- that successive instantiations succeed.
746 procedure Insert_Freeze_Node_For_Instance
747 (N : Node_Id;
748 F_Node : Node_Id);
749 -- N denotes a package or a subprogram instantiation and F_Node is the
750 -- associated freeze node. Insert the freeze node before the first source
751 -- body which follows immediately after N. If no such body is found, the
752 -- freeze node is inserted at the end of the declarative region which
753 -- contains N, unless the instantiation is done in a package spec that is
754 -- not at library level, in which case it is inserted at the outer level.
755 -- This can also be invoked to insert the freeze node of a package that
756 -- encloses an instantiation, in which case N may denote an arbitrary node.
758 procedure Install_Formal_Packages (Par : Entity_Id);
759 -- Install the visible part of any formal of the parent that is a formal
760 -- package. Note that for the case of a formal package with a box, this
761 -- includes the formal part of the formal package (12.7(10/2)).
763 procedure Install_Hidden_Primitives
764 (Prims_List : in out Elist_Id;
765 Gen_T : Entity_Id;
766 Act_T : Entity_Id);
767 -- Remove suffix 'P' from hidden primitives of Act_T to match the
768 -- visibility of primitives of Gen_T. The list of primitives to which
769 -- the suffix is removed is added to Prims_List to restore them later.
771 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
772 -- When compiling an instance of a child unit the parent (which is
773 -- itself an instance) is an enclosing scope that must be made
774 -- immediately visible. This procedure is also used to install the non-
775 -- generic parent of a generic child unit when compiling its body, so
776 -- that full views of types in the parent are made visible.
778 -- The functions Instantiate_XXX perform various legality checks and build
779 -- the declarations for instantiated generic parameters. In all of these
780 -- Formal is the entity in the generic unit, Actual is the entity of
781 -- expression in the generic associations, and Analyzed_Formal is the
782 -- formal in the generic copy, which contains the semantic information to
783 -- be used to validate the actual.
785 function Instantiate_Object
786 (Formal : Node_Id;
787 Actual : Node_Id;
788 Analyzed_Formal : Node_Id) return List_Id;
790 function Instantiate_Type
791 (Formal : Node_Id;
792 Actual : Node_Id;
793 Analyzed_Formal : Node_Id;
794 Actual_Decls : List_Id) return List_Id;
796 function Instantiate_Formal_Subprogram
797 (Formal : Node_Id;
798 Actual : Node_Id;
799 Analyzed_Formal : Node_Id) return Node_Id;
801 function Instantiate_Formal_Package
802 (Formal : Node_Id;
803 Actual : Node_Id;
804 Analyzed_Formal : Node_Id) return List_Id;
805 -- If the formal package is declared with a box, special visibility rules
806 -- apply to its formals: they are in the visible part of the package. This
807 -- is true in the declarative region of the formal package, that is to say
808 -- in the enclosing generic or instantiation. For an instantiation, the
809 -- parameters of the formal package are made visible in an explicit step.
810 -- Furthermore, if the actual has a visible USE clause, these formals must
811 -- be made potentially use-visible as well. On exit from the enclosing
812 -- instantiation, the reverse must be done.
814 -- For a formal package declared without a box, there are conformance rules
815 -- that apply to the actuals in the generic declaration and the actuals of
816 -- the actual package in the enclosing instantiation. The simplest way to
817 -- apply these rules is to repeat the instantiation of the formal package
818 -- in the context of the enclosing instance, and compare the generic
819 -- associations of this instantiation with those of the actual package.
820 -- This internal instantiation only needs to contain the renamings of the
821 -- formals: the visible and private declarations themselves need not be
822 -- created.
824 -- In Ada 2005, the formal package may be only partially parameterized.
825 -- In that case the visibility step must make visible those actuals whose
826 -- corresponding formals were given with a box. A final complication
827 -- involves inherited operations from formal derived types, which must
828 -- be visible if the type is.
830 function Is_In_Main_Unit (N : Node_Id) return Boolean;
831 -- Test if given node is in the main unit
833 procedure Load_Parent_Of_Generic
834 (N : Node_Id;
835 Spec : Node_Id;
836 Body_Optional : Boolean := False);
837 -- If the generic appears in a separate non-generic library unit, load the
838 -- corresponding body to retrieve the body of the generic. N is the node
839 -- for the generic instantiation, Spec is the generic package declaration.
841 -- Body_Optional is a flag that indicates that the body is being loaded to
842 -- ensure that temporaries are generated consistently when there are other
843 -- instances in the current declarative part that precede the one being
844 -- loaded. In that case a missing body is acceptable.
846 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
847 -- Within the generic part, entities in the formal package are
848 -- visible. To validate subsequent type declarations, indicate
849 -- the correspondence between the entities in the analyzed formal,
850 -- and the entities in the actual package. There are three packages
851 -- involved in the instantiation of a formal package: the parent
852 -- generic P1 which appears in the generic declaration, the fake
853 -- instantiation P2 which appears in the analyzed generic, and whose
854 -- visible entities may be used in subsequent formals, and the actual
855 -- P3 in the instance. To validate subsequent formals, me indicate
856 -- that the entities in P2 are mapped into those of P3. The mapping of
857 -- entities has to be done recursively for nested packages.
859 procedure Move_Freeze_Nodes
860 (Out_Of : Entity_Id;
861 After : Node_Id;
862 L : List_Id);
863 -- Freeze nodes can be generated in the analysis of a generic unit, but
864 -- will not be seen by the back-end. It is necessary to move those nodes
865 -- to the enclosing scope if they freeze an outer entity. We place them
866 -- at the end of the enclosing generic package, which is semantically
867 -- neutral.
869 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
870 -- Analyze actuals to perform name resolution. Full resolution is done
871 -- later, when the expected types are known, but names have to be captured
872 -- before installing parents of generics, that are not visible for the
873 -- actuals themselves.
875 -- If Inst is present, it is the entity of the package instance. This
876 -- entity is marked as having a limited_view actual when some actual is
877 -- a limited view. This is used to place the instance body properly.
879 procedure Provide_Completing_Bodies (N : Node_Id);
880 -- Generate completing bodies for all subprograms found within package or
881 -- subprogram declaration N.
883 procedure Remove_Parent (In_Body : Boolean := False);
884 -- Reverse effect after instantiation of child is complete
886 function Requires_Conformance_Checking (N : Node_Id) return Boolean;
887 -- Determine whether the formal package declaration N requires conformance
888 -- checking with actuals in instantiations.
890 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
891 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
892 -- set to No_Elist.
894 procedure Set_Instance_Env
895 (Gen_Unit : Entity_Id;
896 Act_Unit : Entity_Id);
897 -- Save current instance on saved environment, to be used to determine
898 -- the global status of entities in nested instances. Part of Save_Env.
899 -- called after verifying that the generic unit is legal for the instance,
900 -- The procedure also examines whether the generic unit is a predefined
901 -- unit, in order to set configuration switches accordingly. As a result
902 -- the procedure must be called after analyzing and freezing the actuals.
904 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
905 -- Associate analyzed generic parameter with corresponding instance. Used
906 -- for semantic checks at instantiation time.
908 function True_Parent (N : Node_Id) return Node_Id;
909 -- For a subunit, return parent of corresponding stub, else return
910 -- parent of node.
912 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
913 -- Verify that an attribute that appears as the default for a formal
914 -- subprogram is a function or procedure with the correct profile.
916 procedure Validate_Formal_Type_Default (Decl : Node_Id);
917 -- Ada_2022 AI12-205: if a default subtype_mark is present, verify
918 -- that it is the name of a type in the same class as the formal.
919 -- The treatment parallels what is done in Instantiate_Type but differs
920 -- in a few ways so that this machinery cannot be reused as is: on one
921 -- hand there are no visibility issues for a default, because it is
922 -- analyzed in the same context as the formal type definition; on the
923 -- other hand the check needs to take into acount the use of a previous
924 -- formal type in the current formal type definition (see details in
925 -- AI12-0205).
927 -------------------------------------------
928 -- Data Structures for Generic Renamings --
929 -------------------------------------------
931 -- The map Generic_Renamings associates generic entities with their
932 -- corresponding actuals. Currently used to validate type instances. It
933 -- will eventually be used for all generic parameters to eliminate the
934 -- need for overload resolution in the instance.
936 type Assoc_Ptr is new Int;
938 Assoc_Null : constant Assoc_Ptr := -1;
940 type Assoc is record
941 Gen_Id : Entity_Id;
942 Act_Id : Entity_Id;
943 Next_In_HTable : Assoc_Ptr;
944 end record;
946 package Generic_Renamings is new Table.Table
947 (Table_Component_Type => Assoc,
948 Table_Index_Type => Assoc_Ptr,
949 Table_Low_Bound => 0,
950 Table_Initial => 10,
951 Table_Increment => 100,
952 Table_Name => "Generic_Renamings");
954 -- Variable to hold enclosing instantiation. When the environment is
955 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
957 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
959 -- Hash table for associations
961 HTable_Size : constant := 37;
962 type HTable_Range is range 0 .. HTable_Size - 1;
964 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
965 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
966 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
967 function Hash (F : Entity_Id) return HTable_Range;
969 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
970 Header_Num => HTable_Range,
971 Element => Assoc,
972 Elmt_Ptr => Assoc_Ptr,
973 Null_Ptr => Assoc_Null,
974 Set_Next => Set_Next_Assoc,
975 Next => Next_Assoc,
976 Key => Entity_Id,
977 Get_Key => Get_Gen_Id,
978 Hash => Hash,
979 Equal => "=");
981 Exchanged_Views : Elist_Id;
982 -- This list holds the private views that have been exchanged during
983 -- instantiation to restore the visibility of the generic declaration.
984 -- (see comments above). After instantiation, the current visibility is
985 -- reestablished by means of a traversal of this list.
987 Hidden_Entities : Elist_Id;
988 -- This list holds the entities of the current scope that are removed
989 -- from immediate visibility when instantiating a child unit. Their
990 -- visibility is restored in Remove_Parent.
992 -- Because instantiations can be recursive, the following must be saved
993 -- on entry and restored on exit from an instantiation (spec or body).
994 -- This is done by the two procedures Save_Env and Restore_Env. For
995 -- package and subprogram instantiations (but not for the body instances)
996 -- the action of Save_Env is done in two steps: Init_Env is called before
997 -- Check_Generic_Child_Unit, because setting the parent instances requires
998 -- that the visibility data structures be properly initialized. Once the
999 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
1001 Parent_Unit_Visible : Boolean := False;
1002 -- Parent_Unit_Visible is used when the generic is a child unit, and
1003 -- indicates whether the ultimate parent of the generic is visible in the
1004 -- instantiation environment. It is used to reset the visibility of the
1005 -- parent at the end of the instantiation (see Remove_Parent).
1007 Instance_Parent_Unit : Entity_Id := Empty;
1008 -- This records the ultimate parent unit of an instance of a generic
1009 -- child unit and is used in conjunction with Parent_Unit_Visible to
1010 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
1012 type Instance_Env is record
1013 Instantiated_Parent : Assoc;
1014 Exchanged_Views : Elist_Id;
1015 Hidden_Entities : Elist_Id;
1016 Current_Sem_Unit : Unit_Number_Type;
1017 Parent_Unit_Visible : Boolean := False;
1018 Instance_Parent_Unit : Entity_Id := Empty;
1019 Switches : Config_Switches_Type;
1020 end record;
1022 package Instance_Envs is new Table.Table (
1023 Table_Component_Type => Instance_Env,
1024 Table_Index_Type => Int,
1025 Table_Low_Bound => 0,
1026 Table_Initial => 32,
1027 Table_Increment => 100,
1028 Table_Name => "Instance_Envs");
1030 procedure Restore_Private_Views
1031 (Pack_Id : Entity_Id;
1032 Is_Package : Boolean := True);
1033 -- Restore the private views of external types, and unmark the generic
1034 -- renamings of actuals, so that they become compatible subtypes again.
1035 -- For subprograms, Pack_Id is the package constructed to hold the
1036 -- renamings.
1038 procedure Switch_View (T : Entity_Id);
1039 -- Switch the partial and full views of a type and its private
1040 -- dependents (i.e. its subtypes and derived types).
1042 ------------------------------------
1043 -- Structures for Error Reporting --
1044 ------------------------------------
1046 Instantiation_Node : Node_Id;
1047 -- Used by subprograms that validate instantiation of formal parameters
1048 -- where there might be no actual on which to place the error message.
1049 -- Also used to locate the instantiation node for generic subunits.
1051 Instantiation_Error : exception;
1052 -- When there is a semantic error in the generic parameter matching,
1053 -- there is no point in continuing the instantiation, because the
1054 -- number of cascaded errors is unpredictable. This exception aborts
1055 -- the instantiation process altogether.
1057 S_Adjustment : Sloc_Adjustment;
1058 -- Offset created for each node in an instantiation, in order to keep
1059 -- track of the source position of the instantiation in each of its nodes.
1060 -- A subsequent semantic error or warning on a construct of the instance
1061 -- points to both places: the original generic node, and the point of
1062 -- instantiation. See Sinput and Sinput.L for additional details.
1064 ------------------------------------------------------------
1065 -- Data structure for keeping track when inside a Generic --
1066 ------------------------------------------------------------
1068 -- The following table is used to save values of the Inside_A_Generic
1069 -- flag (see spec of Sem) when they are saved by Start_Generic.
1071 package Generic_Flags is new Table.Table (
1072 Table_Component_Type => Boolean,
1073 Table_Index_Type => Int,
1074 Table_Low_Bound => 0,
1075 Table_Initial => 32,
1076 Table_Increment => 200,
1077 Table_Name => "Generic_Flags");
1079 ---------------------------
1080 -- Abandon_Instantiation --
1081 ---------------------------
1083 procedure Abandon_Instantiation (N : Node_Id) is
1084 begin
1085 Error_Msg_N ("\instantiation abandoned!", N);
1086 raise Instantiation_Error;
1087 end Abandon_Instantiation;
1089 ----------------------------------
1090 -- Adjust_Inherited_Pragma_Sloc --
1091 ----------------------------------
1093 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1094 begin
1095 Adjust_Instantiation_Sloc (N, S_Adjustment);
1096 end Adjust_Inherited_Pragma_Sloc;
1098 --------------------------
1099 -- Analyze_Associations --
1100 --------------------------
1102 function Analyze_Associations
1103 (I_Node : Node_Id;
1104 Formals : List_Id;
1105 F_Copy : List_Id) return List_Id
1107 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1108 Assoc_List : constant List_Id := New_List;
1109 Default_Actuals : constant List_Id := New_List;
1110 Gen_Unit : constant Entity_Id :=
1111 Defining_Entity (Parent (F_Copy));
1113 Actuals : List_Id;
1114 Actual : Node_Id;
1115 Analyzed_Formal : Node_Id;
1116 First_Named : Node_Id := Empty;
1117 Formal : Node_Id;
1118 Match : Node_Id := Empty;
1119 Named : Node_Id;
1120 Saved_Formal : Node_Id;
1122 Default_Formals : constant List_Id := New_List;
1123 -- If an Others_Choice is present, some of the formals may be defaulted.
1124 -- To simplify the treatment of visibility in an instance, we introduce
1125 -- individual defaults for each such formal. These defaults are
1126 -- appended to the list of associations and replace the Others_Choice.
1128 Found_Assoc : Node_Id;
1129 -- Association for the current formal being match. Empty if there are
1130 -- no remaining actuals, or if there is no named association with the
1131 -- name of the formal.
1133 Is_Named_Assoc : Boolean;
1134 Num_Matched : Nat := 0;
1135 Num_Actuals : Nat := 0;
1137 Others_Present : Boolean := False;
1138 Others_Choice : Node_Id := Empty;
1139 -- In Ada 2005, indicates partial parameterization of a formal
1140 -- package. As usual an other association must be last in the list.
1142 procedure Build_Subprogram_Wrappers;
1143 -- Ada 2022: AI12-0272 introduces pre/postconditions for formal
1144 -- subprograms. The implementation of making the formal into a renaming
1145 -- of the actual does not work, given that subprogram renaming cannot
1146 -- carry aspect specifications. Instead we must create subprogram
1147 -- wrappers whose body is a call to the actual, and whose declaration
1148 -- carries the aspects of the formal.
1150 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1151 -- Warn if an actual fixed-point type has user-defined arithmetic
1152 -- operations, but there is no corresponding formal in the generic,
1153 -- in which case the predefined operations will be used. This merits
1154 -- a warning because of the special semantics of fixed point ops.
1156 procedure Check_Overloaded_Formal_Subprogram (Formal : Node_Id);
1157 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1158 -- cannot have a named association for it. AI05-0025 extends this rule
1159 -- to formals of formal packages by AI05-0025, and it also applies to
1160 -- box-initialized formals.
1162 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1163 -- Determine whether the parameter types and the return type of Subp
1164 -- are fully defined at the point of instantiation.
1166 function Matching_Actual
1167 (F : Entity_Id;
1168 A_F : Entity_Id) return Node_Id;
1169 -- Find actual that corresponds to a given formal parameter. If the
1170 -- actuals are positional, return the next one, if any. If the actuals
1171 -- are named, scan the parameter associations to find the right one.
1172 -- A_F is the corresponding entity in the analyzed generic, which is
1173 -- placed on the selector name.
1175 -- In Ada 2005, a named association may be given with a box, in which
1176 -- case Matching_Actual sets Found_Assoc to the generic association,
1177 -- but return Empty for the actual itself. In this case the code below
1178 -- creates a corresponding declaration for the formal.
1180 function Partial_Parameterization return Boolean;
1181 -- Ada 2005: if no match is found for a given formal, check if the
1182 -- association for it includes a box, or whether the associations
1183 -- include an Others clause.
1185 procedure Process_Default (Formal : Node_Id);
1186 -- Add a copy of the declaration of a generic formal to the list of
1187 -- associations, and add an explicit box association for its entity
1188 -- if there is none yet, and the default comes from an Others_Choice.
1190 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1191 -- Determine whether Subp renames one of the subprograms defined in the
1192 -- generated package Standard.
1194 procedure Set_Analyzed_Formal;
1195 -- Find the node in the generic copy that corresponds to a given formal.
1196 -- The semantic information on this node is used to perform legality
1197 -- checks on the actuals. Because semantic analysis can introduce some
1198 -- anonymous entities or modify the declaration node itself, the
1199 -- correspondence between the two lists is not one-one. In addition to
1200 -- anonymous types, the presence a formal equality will introduce an
1201 -- implicit declaration for the corresponding inequality.
1203 -------------------------------
1204 -- Build_Subprogram_Wrappers --
1205 -------------------------------
1207 procedure Build_Subprogram_Wrappers is
1208 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result;
1209 -- Adjust sloc so that errors located at N will be reported with
1210 -- information about the instance and not just about the generic.
1212 ------------------------
1213 -- Adjust_Aspect_Sloc --
1214 ------------------------
1216 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result is
1217 begin
1218 Adjust_Instantiation_Sloc (N, S_Adjustment);
1219 return OK;
1220 end Adjust_Aspect_Sloc;
1222 procedure Adjust_Aspect_Slocs is new
1223 Traverse_Proc (Adjust_Aspect_Sloc);
1225 Formal : constant Entity_Id :=
1226 Defining_Unit_Name (Specification (Analyzed_Formal));
1227 Aspect_Spec : Node_Id;
1228 Decl_Node : Node_Id;
1229 Actual_Name : Node_Id;
1231 -- Start of processing for Build_Subprogram_Wrappers
1233 begin
1234 -- Create declaration for wrapper subprogram
1235 -- The actual can be overloaded, in which case it will be
1236 -- resolved when the call in the wrapper body is analyzed.
1237 -- We attach the possible interpretations of the actual to
1238 -- the name to be used in the call in the wrapper body.
1240 if Is_Entity_Name (Match) then
1241 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
1243 if Is_Overloaded (Match) then
1244 Save_Interps (Match, Actual_Name);
1245 end if;
1247 else
1248 -- Use renaming declaration created when analyzing actual.
1249 -- This may be incomplete if there are several formal
1250 -- subprograms whose actual is an attribute ???
1252 declare
1253 Renaming_Decl : constant Node_Id := Last (Assoc_List);
1255 begin
1256 Actual_Name := New_Occurrence_Of
1257 (Defining_Entity (Renaming_Decl), Sloc (Match));
1258 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
1259 end;
1260 end if;
1262 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
1264 -- Transfer aspect specifications from formal subprogram to wrapper
1266 Set_Aspect_Specifications (Decl_Node,
1267 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
1269 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
1270 while Present (Aspect_Spec) loop
1271 Adjust_Aspect_Slocs (Aspect_Spec);
1272 Set_Analyzed (Aspect_Spec, False);
1273 Next (Aspect_Spec);
1274 end loop;
1276 Append_To (Assoc_List, Decl_Node);
1278 -- Create corresponding body, and append it to association list
1279 -- that appears at the head of the declarations in the instance.
1280 -- The subprogram may be called in the analysis of subsequent
1281 -- actuals.
1283 Append_To (Assoc_List,
1284 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
1285 end Build_Subprogram_Wrappers;
1287 ----------------------------------------
1288 -- Check_Overloaded_Formal_Subprogram --
1289 ----------------------------------------
1291 procedure Check_Overloaded_Formal_Subprogram (Formal : Node_Id) is
1292 Temp_Formal : Node_Id;
1294 begin
1295 Temp_Formal := First (Formals);
1296 while Present (Temp_Formal) loop
1297 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1298 and then Temp_Formal /= Formal
1299 and then
1300 Chars (Defining_Unit_Name (Specification (Formal))) =
1301 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1302 then
1303 if Present (Found_Assoc) then
1304 Error_Msg_N
1305 ("named association not allowed for overloaded formal",
1306 Found_Assoc);
1308 else
1309 Error_Msg_N
1310 ("named association not allowed for overloaded formal",
1311 Others_Choice);
1312 end if;
1314 Abandon_Instantiation (Instantiation_Node);
1315 end if;
1317 Next (Temp_Formal);
1318 end loop;
1319 end Check_Overloaded_Formal_Subprogram;
1321 -------------------------------
1322 -- Check_Fixed_Point_Actual --
1323 -------------------------------
1325 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1326 Typ : constant Entity_Id := Entity (Actual);
1327 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1328 Elem : Elmt_Id;
1329 Formal : Node_Id;
1330 Op : Entity_Id;
1332 begin
1333 -- Locate primitive operations of the type that are arithmetic
1334 -- operations.
1336 Elem := First_Elmt (Prims);
1337 while Present (Elem) loop
1338 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1340 -- Check whether the generic unit has a formal subprogram of
1341 -- the same name. This does not check types but is good enough
1342 -- to justify a warning.
1344 Formal := First_Non_Pragma (Formals);
1345 Op := Alias (Node (Elem));
1347 while Present (Formal) loop
1348 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1349 and then Chars (Defining_Entity (Formal)) =
1350 Chars (Node (Elem))
1351 then
1352 exit;
1354 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1355 declare
1356 Assoc : Node_Id;
1357 Ent : Entity_Id;
1359 begin
1360 -- Locate corresponding actual, and check whether it
1361 -- includes a fixed-point type.
1363 Assoc := First (Assoc_List);
1364 while Present (Assoc) loop
1365 exit when
1366 Nkind (Assoc) = N_Package_Renaming_Declaration
1367 and then Chars (Defining_Unit_Name (Assoc)) =
1368 Chars (Defining_Identifier (Formal));
1370 Next (Assoc);
1371 end loop;
1373 if Present (Assoc) then
1375 -- If formal package declares a fixed-point type,
1376 -- and the user-defined operator is derived from
1377 -- a generic instance package, the fixed-point type
1378 -- does not use the corresponding predefined op.
1380 Ent := First_Entity (Entity (Name (Assoc)));
1381 while Present (Ent) loop
1382 if Is_Fixed_Point_Type (Ent)
1383 and then Present (Op)
1384 and then Is_Generic_Instance (Scope (Op))
1385 then
1386 return;
1387 end if;
1389 Next_Entity (Ent);
1390 end loop;
1391 end if;
1392 end;
1393 end if;
1395 Next (Formal);
1396 end loop;
1398 if No (Formal) then
1399 Error_Msg_Sloc := Sloc (Node (Elem));
1400 Error_Msg_NE
1401 ("?instance uses predefined operation, not primitive "
1402 & "operation&#", Actual, Node (Elem));
1403 end if;
1404 end if;
1406 Next_Elmt (Elem);
1407 end loop;
1408 end Check_Fixed_Point_Actual;
1410 -------------------------------
1411 -- Has_Fully_Defined_Profile --
1412 -------------------------------
1414 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1415 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1416 -- Determine whethet type Typ is fully defined
1418 ---------------------------
1419 -- Is_Fully_Defined_Type --
1420 ---------------------------
1422 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1423 begin
1424 -- A private type without a full view is not fully defined
1426 if Is_Private_Type (Typ)
1427 and then No (Full_View (Typ))
1428 then
1429 return False;
1431 -- An incomplete type is never fully defined
1433 elsif Is_Incomplete_Type (Typ) then
1434 return False;
1436 -- All other types are fully defined
1438 else
1439 return True;
1440 end if;
1441 end Is_Fully_Defined_Type;
1443 -- Local declarations
1445 Param : Entity_Id;
1447 -- Start of processing for Has_Fully_Defined_Profile
1449 begin
1450 -- Check the parameters
1452 Param := First_Formal (Subp);
1453 while Present (Param) loop
1454 if not Is_Fully_Defined_Type (Etype (Param)) then
1455 return False;
1456 end if;
1458 Next_Formal (Param);
1459 end loop;
1461 -- Check the return type
1463 return Is_Fully_Defined_Type (Etype (Subp));
1464 end Has_Fully_Defined_Profile;
1466 ---------------------
1467 -- Matching_Actual --
1468 ---------------------
1470 function Matching_Actual
1471 (F : Entity_Id;
1472 A_F : Entity_Id) return Node_Id
1474 Prev : Node_Id;
1475 Act : Node_Id;
1477 begin
1478 Is_Named_Assoc := False;
1480 -- End of list of purely positional parameters
1482 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1483 Found_Assoc := Empty;
1484 Act := Empty;
1486 -- Case of positional parameter corresponding to current formal
1488 elsif No (Selector_Name (Actual)) then
1489 Found_Assoc := Actual;
1490 Act := Explicit_Generic_Actual_Parameter (Actual);
1491 Num_Matched := Num_Matched + 1;
1492 Next (Actual);
1494 -- Otherwise scan list of named actuals to find the one with the
1495 -- desired name. All remaining actuals have explicit names.
1497 else
1498 Is_Named_Assoc := True;
1499 Found_Assoc := Empty;
1500 Act := Empty;
1501 Prev := Empty;
1503 while Present (Actual) loop
1504 if Nkind (Actual) = N_Others_Choice then
1505 Found_Assoc := Empty;
1506 Act := Empty;
1508 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1509 Set_Entity (Selector_Name (Actual), A_F);
1510 Set_Etype (Selector_Name (Actual), Etype (A_F));
1511 Generate_Reference (A_F, Selector_Name (Actual));
1513 Found_Assoc := Actual;
1514 Act := Explicit_Generic_Actual_Parameter (Actual);
1515 Num_Matched := Num_Matched + 1;
1516 exit;
1517 end if;
1519 Prev := Actual;
1520 Next (Actual);
1521 end loop;
1523 -- Reset for subsequent searches. In most cases the named
1524 -- associations are in order. If they are not, we reorder them
1525 -- to avoid scanning twice the same actual. This is not just a
1526 -- question of efficiency: there may be multiple defaults with
1527 -- boxes that have the same name. In a nested instantiation we
1528 -- insert actuals for those defaults, and cannot rely on their
1529 -- names to disambiguate them.
1531 if Actual = First_Named then
1532 Next (First_Named);
1534 elsif Present (Actual) then
1535 Insert_Before (First_Named, Remove_Next (Prev));
1536 end if;
1538 Actual := First_Named;
1539 end if;
1541 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1542 Set_Used_As_Generic_Actual (Entity (Act));
1543 end if;
1545 return Act;
1546 end Matching_Actual;
1548 ------------------------------
1549 -- Partial_Parameterization --
1550 ------------------------------
1552 function Partial_Parameterization return Boolean is
1553 begin
1554 return Others_Present
1555 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1556 end Partial_Parameterization;
1558 ---------------------
1559 -- Process_Default --
1560 ---------------------
1562 procedure Process_Default (Formal : Node_Id) is
1563 Loc : constant Source_Ptr := Sloc (I_Node);
1564 F_Id : constant Entity_Id := Defining_Entity (Formal);
1565 Decl : Node_Id;
1566 Default : Node_Id;
1567 Id : Entity_Id;
1569 begin
1570 -- Append copy of formal declaration to associations, and create new
1571 -- defining identifier for it.
1573 Decl := New_Copy_Tree (Formal);
1574 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1576 if Nkind (Formal) in N_Formal_Subprogram_Declaration then
1577 Set_Defining_Unit_Name (Specification (Decl), Id);
1579 else
1580 Set_Defining_Identifier (Decl, Id);
1581 end if;
1583 Append (Decl, Assoc_List);
1585 if No (Found_Assoc) then
1586 Default :=
1587 Make_Generic_Association (Loc,
1588 Selector_Name =>
1589 New_Occurrence_Of (Id, Loc),
1590 Explicit_Generic_Actual_Parameter => Empty);
1591 Set_Box_Present (Default);
1592 Append (Default, Default_Formals);
1593 end if;
1594 end Process_Default;
1596 ---------------------------------
1597 -- Renames_Standard_Subprogram --
1598 ---------------------------------
1600 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1601 Id : Entity_Id;
1603 begin
1604 Id := Alias (Subp);
1605 while Present (Id) loop
1606 if Scope (Id) = Standard_Standard then
1607 return True;
1608 end if;
1610 Id := Alias (Id);
1611 end loop;
1613 return False;
1614 end Renames_Standard_Subprogram;
1616 -------------------------
1617 -- Set_Analyzed_Formal --
1618 -------------------------
1620 procedure Set_Analyzed_Formal is
1621 Kind : Node_Kind;
1623 begin
1624 while Present (Analyzed_Formal) loop
1625 Kind := Nkind (Analyzed_Formal);
1627 case Nkind (Formal) is
1628 when N_Formal_Subprogram_Declaration =>
1629 exit when Kind in N_Formal_Subprogram_Declaration
1630 and then
1631 Chars
1632 (Defining_Unit_Name (Specification (Formal))) =
1633 Chars
1634 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1636 when N_Formal_Package_Declaration =>
1637 exit when Kind in N_Formal_Package_Declaration
1638 | N_Generic_Package_Declaration
1639 | N_Package_Declaration;
1641 when N_Use_Package_Clause
1642 | N_Use_Type_Clause
1644 exit;
1646 when others =>
1648 -- Skip freeze nodes, and nodes inserted to replace
1649 -- unrecognized pragmas.
1651 exit when
1652 Kind not in N_Formal_Subprogram_Declaration
1653 and then Kind not in N_Subprogram_Declaration
1654 | N_Freeze_Entity
1655 | N_Null_Statement
1656 | N_Itype_Reference
1657 and then Chars (Defining_Identifier (Formal)) =
1658 Chars (Defining_Identifier (Analyzed_Formal));
1659 end case;
1661 Next (Analyzed_Formal);
1662 end loop;
1663 end Set_Analyzed_Formal;
1665 -- Start of processing for Analyze_Associations
1667 begin
1668 Actuals := Generic_Associations (I_Node);
1670 if Present (Actuals) then
1672 -- Check for an Others choice, indicating a partial parameterization
1673 -- for a formal package.
1675 Actual := First (Actuals);
1676 while Present (Actual) loop
1677 if Nkind (Actual) = N_Others_Choice then
1678 Others_Present := True;
1679 Others_Choice := Actual;
1681 if Present (Next (Actual)) then
1682 Error_Msg_N ("OTHERS must be last association", Actual);
1683 end if;
1685 -- This subprogram is used both for formal packages and for
1686 -- instantiations. For the latter, associations must all be
1687 -- explicit.
1689 if Nkind (I_Node) /= N_Formal_Package_Declaration
1690 and then Comes_From_Source (I_Node)
1691 then
1692 Error_Msg_N
1693 ("OTHERS association not allowed in an instance",
1694 Actual);
1695 end if;
1697 -- In any case, nothing to do after the others association
1699 exit;
1701 elsif Box_Present (Actual)
1702 and then Comes_From_Source (I_Node)
1703 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1704 then
1705 Error_Msg_N
1706 ("box association not allowed in an instance", Actual);
1707 end if;
1709 Next (Actual);
1710 end loop;
1712 -- If named associations are present, save first named association
1713 -- (it may of course be Empty) to facilitate subsequent name search.
1715 First_Named := First (Actuals);
1716 while Present (First_Named)
1717 and then Nkind (First_Named) /= N_Others_Choice
1718 and then No (Selector_Name (First_Named))
1719 loop
1720 Num_Actuals := Num_Actuals + 1;
1721 Next (First_Named);
1722 end loop;
1723 end if;
1725 Named := First_Named;
1726 while Present (Named) loop
1727 if Nkind (Named) /= N_Others_Choice
1728 and then No (Selector_Name (Named))
1729 then
1730 Error_Msg_N ("invalid positional actual after named one", Named);
1731 Abandon_Instantiation (Named);
1732 end if;
1734 -- A named association may lack an actual parameter, if it was
1735 -- introduced for a default subprogram that turns out to be local
1736 -- to the outer instantiation. If it has a box association it must
1737 -- correspond to some formal in the generic.
1739 if Nkind (Named) /= N_Others_Choice
1740 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1741 or else Box_Present (Named))
1742 then
1743 Num_Actuals := Num_Actuals + 1;
1744 end if;
1746 Next (Named);
1747 end loop;
1749 if Present (Formals) then
1750 Formal := First_Non_Pragma (Formals);
1751 Analyzed_Formal := First_Non_Pragma (F_Copy);
1753 if Present (Actuals) then
1754 Actual := First (Actuals);
1756 -- All formals should have default values
1758 else
1759 Actual := Empty;
1760 end if;
1762 while Present (Formal) loop
1763 Set_Analyzed_Formal;
1764 Saved_Formal := Next_Non_Pragma (Formal);
1766 case Nkind (Formal) is
1767 when N_Formal_Object_Declaration =>
1768 Match :=
1769 Matching_Actual
1770 (Defining_Identifier (Formal),
1771 Defining_Identifier (Analyzed_Formal));
1773 if No (Match) and then Partial_Parameterization then
1774 Process_Default (Formal);
1776 else
1777 Append_List
1778 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1779 Assoc_List);
1781 -- For a defaulted in_parameter, create an entry in the
1782 -- the list of defaulted actuals, for GNATprove use. Do
1783 -- not included these defaults for an instance nested
1784 -- within a generic, because the defaults are also used
1785 -- in the analysis of the enclosing generic, and only
1786 -- defaulted subprograms are relevant there.
1788 if No (Match) and then not Inside_A_Generic then
1789 Append_To (Default_Actuals,
1790 Make_Generic_Association (Sloc (I_Node),
1791 Selector_Name =>
1792 New_Occurrence_Of
1793 (Defining_Identifier (Formal), Sloc (I_Node)),
1794 Explicit_Generic_Actual_Parameter =>
1795 New_Copy_Tree (Default_Expression (Formal))));
1796 end if;
1797 end if;
1799 -- If the object is a call to an expression function, this
1800 -- is a freezing point for it.
1802 if Is_Entity_Name (Match)
1803 and then Present (Entity (Match))
1804 and then Nkind
1805 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1806 = N_Expression_Function
1807 then
1808 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1809 end if;
1811 when N_Formal_Type_Declaration =>
1812 Match :=
1813 Matching_Actual
1814 (Defining_Identifier (Formal),
1815 Defining_Identifier (Analyzed_Formal));
1817 if No (Match) then
1818 if Partial_Parameterization then
1819 Process_Default (Formal);
1821 elsif Present (Default_Subtype_Mark (Formal)) then
1822 Match := New_Copy (Default_Subtype_Mark (Formal));
1823 Append_List
1824 (Instantiate_Type
1825 (Formal, Match, Analyzed_Formal, Assoc_List),
1826 Assoc_List);
1827 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1829 else
1830 Error_Msg_Sloc := Sloc (Gen_Unit);
1831 Error_Msg_NE
1832 ("missing actual&",
1833 Instantiation_Node, Defining_Identifier (Formal));
1834 Error_Msg_NE
1835 ("\in instantiation of & declared#",
1836 Instantiation_Node, Gen_Unit);
1837 Abandon_Instantiation (Instantiation_Node);
1838 end if;
1840 else
1841 Analyze (Match);
1842 Append_List
1843 (Instantiate_Type
1844 (Formal, Match, Analyzed_Formal, Assoc_List),
1845 Assoc_List);
1847 -- Warn when an actual is a fixed-point with user-
1848 -- defined promitives. The warning is superfluous
1849 -- if the formal is private, because there can be
1850 -- no arithmetic operations in the generic so there
1851 -- no danger of confusion.
1853 if Is_Fixed_Point_Type (Entity (Match))
1854 and then not Is_Private_Type
1855 (Defining_Identifier (Analyzed_Formal))
1856 then
1857 Check_Fixed_Point_Actual (Match);
1858 end if;
1860 -- An instantiation is a freeze point for the actuals,
1861 -- unless this is a rewritten formal package, or the
1862 -- formal is an Ada 2012 formal incomplete type.
1864 if Nkind (I_Node) = N_Formal_Package_Declaration
1865 or else
1866 (Ada_Version >= Ada_2012
1867 and then
1868 Ekind (Defining_Identifier (Analyzed_Formal)) =
1869 E_Incomplete_Type)
1870 then
1871 null;
1873 else
1874 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1875 end if;
1876 end if;
1878 -- A remote access-to-class-wide type is not a legal actual
1879 -- for a generic formal of an access type (E.2.2(17/2)).
1880 -- In GNAT an exception to this rule is introduced when
1881 -- the formal is marked as remote using implementation
1882 -- defined aspect/pragma Remote_Access_Type. In that case
1883 -- the actual must be remote as well.
1885 -- If the current instantiation is the construction of a
1886 -- local copy for a formal package the actuals may be
1887 -- defaulted, and there is no matching actual to check.
1889 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1890 and then
1891 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1892 N_Access_To_Object_Definition
1893 and then Present (Match)
1894 then
1895 declare
1896 Formal_Ent : constant Entity_Id :=
1897 Defining_Identifier (Analyzed_Formal);
1898 begin
1899 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1900 = Is_Remote_Types (Formal_Ent)
1901 then
1902 -- Remoteness of formal and actual match
1904 null;
1906 elsif Is_Remote_Types (Formal_Ent) then
1908 -- Remote formal, non-remote actual
1910 Error_Msg_NE
1911 ("actual for& must be remote", Match, Formal_Ent);
1913 else
1914 -- Non-remote formal, remote actual
1916 Error_Msg_NE
1917 ("actual for& may not be remote",
1918 Match, Formal_Ent);
1919 end if;
1920 end;
1921 end if;
1923 when N_Formal_Subprogram_Declaration =>
1924 Match :=
1925 Matching_Actual
1926 (Defining_Unit_Name (Specification (Formal)),
1927 Defining_Unit_Name (Specification (Analyzed_Formal)));
1929 -- If the formal subprogram has the same name as another
1930 -- formal subprogram of the generic, then a named
1931 -- association is illegal (12.3(9)). Exclude named
1932 -- associations that are generated for a nested instance.
1934 if Present (Match)
1935 and then Is_Named_Assoc
1936 and then Comes_From_Source (Found_Assoc)
1937 then
1938 Check_Overloaded_Formal_Subprogram (Formal);
1939 end if;
1941 -- If there is no corresponding actual, this may be case
1942 -- of partial parameterization, or else the formal has a
1943 -- default or a box.
1945 if No (Match) and then Partial_Parameterization then
1946 Process_Default (Formal);
1948 if Nkind (I_Node) = N_Formal_Package_Declaration then
1949 Check_Overloaded_Formal_Subprogram (Formal);
1950 end if;
1952 else
1953 Append_To (Assoc_List,
1954 Instantiate_Formal_Subprogram
1955 (Formal, Match, Analyzed_Formal));
1957 -- If formal subprogram has contracts, create wrappers
1958 -- for it. This is an expansion activity that cannot
1959 -- take place e.g. within an enclosing generic unit.
1961 if Has_Contracts (Analyzed_Formal)
1962 and then (Expander_Active or GNATprove_Mode)
1963 then
1964 Build_Subprogram_Wrappers;
1965 end if;
1967 -- An instantiation is a freeze point for the actuals,
1968 -- unless this is a rewritten formal package.
1970 if Nkind (I_Node) /= N_Formal_Package_Declaration
1971 and then Nkind (Match) = N_Identifier
1972 and then Is_Subprogram (Entity (Match))
1974 -- The actual subprogram may rename a routine defined
1975 -- in Standard. Avoid freezing such renamings because
1976 -- subprograms coming from Standard cannot be frozen.
1978 and then
1979 not Renames_Standard_Subprogram (Entity (Match))
1981 -- If the actual subprogram comes from a different
1982 -- unit, it is already frozen, either by a body in
1983 -- that unit or by the end of the declarative part
1984 -- of the unit. This check avoids the freezing of
1985 -- subprograms defined in Standard which are used
1986 -- as generic actuals.
1988 and then In_Same_Code_Unit (Entity (Match), I_Node)
1989 and then Has_Fully_Defined_Profile (Entity (Match))
1990 then
1991 -- Mark the subprogram as having a delayed freeze
1992 -- since this may be an out-of-order action.
1994 Set_Has_Delayed_Freeze (Entity (Match));
1995 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1996 end if;
1997 end if;
1999 -- If this is a nested generic, preserve default for later
2000 -- instantiations. We do this as well for GNATprove use,
2001 -- so that the list of generic associations is complete.
2003 if No (Match) and then Box_Present (Formal) then
2004 declare
2005 Subp : constant Entity_Id :=
2006 Defining_Unit_Name
2007 (Specification (Last (Assoc_List)));
2009 begin
2010 Append_To (Default_Actuals,
2011 Make_Generic_Association (Sloc (I_Node),
2012 Selector_Name =>
2013 New_Occurrence_Of (Subp, Sloc (I_Node)),
2014 Explicit_Generic_Actual_Parameter =>
2015 New_Occurrence_Of (Subp, Sloc (I_Node))));
2016 end;
2017 end if;
2019 when N_Formal_Package_Declaration =>
2020 -- The name of the formal package may be hidden by the
2021 -- formal parameter itself.
2023 if Error_Posted (Analyzed_Formal) then
2024 Abandon_Instantiation (Instantiation_Node);
2026 else
2027 Match :=
2028 Matching_Actual
2029 (Defining_Identifier (Formal),
2030 Defining_Identifier
2031 (Original_Node (Analyzed_Formal)));
2032 end if;
2034 if No (Match) then
2035 if Partial_Parameterization then
2036 Process_Default (Formal);
2038 else
2039 Error_Msg_Sloc := Sloc (Gen_Unit);
2040 Error_Msg_NE
2041 ("missing actual&",
2042 Instantiation_Node, Defining_Identifier (Formal));
2043 Error_Msg_NE
2044 ("\in instantiation of & declared#",
2045 Instantiation_Node, Gen_Unit);
2047 Abandon_Instantiation (Instantiation_Node);
2048 end if;
2050 else
2051 Analyze (Match);
2052 Append_List
2053 (Instantiate_Formal_Package
2054 (Formal, Match, Analyzed_Formal),
2055 Assoc_List);
2057 -- Determine whether the actual package needs an explicit
2058 -- freeze node. This is only the case if the actual is
2059 -- declared in the same unit and has a body. Normally
2060 -- packages do not have explicit freeze nodes, and gigi
2061 -- only uses them to elaborate entities in a package
2062 -- body.
2064 Explicit_Freeze_Check : declare
2065 Actual : constant Entity_Id := Entity (Match);
2066 Gen_Par : Entity_Id;
2068 Needs_Freezing : Boolean;
2069 P : Node_Id;
2071 procedure Check_Generic_Parent;
2072 -- The actual may be an instantiation of a unit
2073 -- declared in a previous instantiation. If that
2074 -- one is also in the current compilation, it must
2075 -- itself be frozen before the actual. The actual
2076 -- may be an instantiation of a generic child unit,
2077 -- in which case the same applies to the instance
2078 -- of the parent which must be frozen before the
2079 -- actual.
2080 -- Should this itself be recursive ???
2082 --------------------------
2083 -- Check_Generic_Parent --
2084 --------------------------
2086 procedure Check_Generic_Parent is
2087 Inst : constant Node_Id :=
2088 Get_Unit_Instantiation_Node (Actual);
2089 Par : Entity_Id;
2091 begin
2092 Par := Empty;
2094 if Nkind (Parent (Actual)) = N_Package_Specification
2095 then
2096 Par := Scope (Generic_Parent (Parent (Actual)));
2098 if Is_Generic_Instance (Par) then
2099 null;
2101 -- If the actual is a child generic unit, check
2102 -- whether the instantiation of the parent is
2103 -- also local and must also be frozen now. We
2104 -- must retrieve the instance node to locate the
2105 -- parent instance if any.
2107 elsif Ekind (Par) = E_Generic_Package
2108 and then Is_Child_Unit (Gen_Par)
2109 and then Ekind (Scope (Gen_Par)) =
2110 E_Generic_Package
2111 then
2112 if Nkind (Inst) = N_Package_Instantiation
2113 and then Nkind (Name (Inst)) =
2114 N_Expanded_Name
2115 then
2116 -- Retrieve entity of parent instance
2118 Par := Entity (Prefix (Name (Inst)));
2119 end if;
2121 else
2122 Par := Empty;
2123 end if;
2124 end if;
2126 if Present (Par)
2127 and then Is_Generic_Instance (Par)
2128 and then Scope (Par) = Current_Scope
2129 and then
2130 (No (Freeze_Node (Par))
2131 or else
2132 not Is_List_Member (Freeze_Node (Par)))
2133 then
2134 Set_Has_Delayed_Freeze (Par);
2135 Append_Elmt (Par, Actuals_To_Freeze);
2136 end if;
2137 end Check_Generic_Parent;
2139 -- Start of processing for Explicit_Freeze_Check
2141 begin
2142 if Present (Renamed_Entity (Actual)) then
2143 Gen_Par :=
2144 Generic_Parent (Specification
2145 (Unit_Declaration_Node
2146 (Renamed_Entity (Actual))));
2147 else
2148 Gen_Par :=
2149 Generic_Parent (Specification
2150 (Unit_Declaration_Node (Actual)));
2151 end if;
2153 if not Expander_Active
2154 or else not Has_Completion (Actual)
2155 or else not In_Same_Source_Unit (I_Node, Actual)
2156 or else Is_Frozen (Actual)
2157 or else
2158 (Present (Renamed_Entity (Actual))
2159 and then
2160 not In_Same_Source_Unit
2161 (I_Node, (Renamed_Entity (Actual))))
2162 then
2163 null;
2165 else
2166 -- Finally we want to exclude such freeze nodes
2167 -- from statement sequences, which freeze
2168 -- everything before them.
2169 -- Is this strictly necessary ???
2171 Needs_Freezing := True;
2173 P := Parent (I_Node);
2174 while Nkind (P) /= N_Compilation_Unit loop
2175 if Nkind (P) = N_Handled_Sequence_Of_Statements
2176 then
2177 Needs_Freezing := False;
2178 exit;
2179 end if;
2181 P := Parent (P);
2182 end loop;
2184 if Needs_Freezing then
2185 Check_Generic_Parent;
2187 -- If the actual is a renaming of a proper
2188 -- instance of the formal package, indicate
2189 -- that it is the instance that must be frozen.
2191 if Nkind (Parent (Actual)) =
2192 N_Package_Renaming_Declaration
2193 then
2194 Set_Has_Delayed_Freeze
2195 (Renamed_Entity (Actual));
2196 Append_Elmt
2197 (Renamed_Entity (Actual),
2198 Actuals_To_Freeze);
2199 else
2200 Set_Has_Delayed_Freeze (Actual);
2201 Append_Elmt (Actual, Actuals_To_Freeze);
2202 end if;
2203 end if;
2204 end if;
2205 end Explicit_Freeze_Check;
2206 end if;
2208 -- For use type and use package appearing in the generic part,
2209 -- we have already copied them, so we can just move them where
2210 -- they belong (we mustn't recopy them since this would mess up
2211 -- the Sloc values).
2213 when N_Use_Package_Clause
2214 | N_Use_Type_Clause
2216 if Nkind (Original_Node (I_Node)) =
2217 N_Formal_Package_Declaration
2218 then
2219 Append (New_Copy_Tree (Formal), Assoc_List);
2220 else
2221 Remove (Formal);
2222 Append (Formal, Assoc_List);
2223 end if;
2225 when others =>
2226 raise Program_Error;
2227 end case;
2229 -- Check here the correct use of Ghost entities in generic
2230 -- instantiations, as now the generic has been resolved and
2231 -- we know which formal generic parameters are ghost (SPARK
2232 -- RM 6.9(10)).
2234 if Nkind (Formal) not in N_Use_Package_Clause
2235 | N_Use_Type_Clause
2236 then
2237 Check_Ghost_Context_In_Generic_Association
2238 (Actual => Match,
2239 Formal => Defining_Entity (Analyzed_Formal));
2240 end if;
2242 Formal := Saved_Formal;
2243 Next_Non_Pragma (Analyzed_Formal);
2244 end loop;
2246 if Num_Actuals > Num_Matched then
2247 Error_Msg_Sloc := Sloc (Gen_Unit);
2249 if Present (Selector_Name (Actual)) then
2250 Error_Msg_NE
2251 ("unmatched actual &", Actual, Selector_Name (Actual));
2252 Error_Msg_NE
2253 ("\in instantiation of & declared#", Actual, Gen_Unit);
2254 else
2255 Error_Msg_NE
2256 ("unmatched actual in instantiation of & declared#",
2257 Actual, Gen_Unit);
2258 end if;
2259 end if;
2261 elsif Present (Actuals) then
2262 Error_Msg_N
2263 ("too many actuals in generic instantiation", Instantiation_Node);
2264 end if;
2266 -- An instantiation freezes all generic actuals. The only exceptions
2267 -- to this are incomplete types and subprograms which are not fully
2268 -- defined at the point of instantiation.
2270 declare
2271 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2272 begin
2273 while Present (Elmt) loop
2274 Freeze_Before (I_Node, Node (Elmt));
2275 Next_Elmt (Elmt);
2276 end loop;
2277 end;
2279 -- If there are default subprograms, normalize the tree by adding
2280 -- explicit associations for them. This is required if the instance
2281 -- appears within a generic.
2283 if not Is_Empty_List (Default_Actuals) then
2284 declare
2285 Default : Node_Id;
2287 begin
2288 Default := First (Default_Actuals);
2289 while Present (Default) loop
2290 Mark_Rewrite_Insertion (Default);
2291 Next (Default);
2292 end loop;
2294 if No (Actuals) then
2295 Set_Generic_Associations (I_Node, Default_Actuals);
2296 else
2297 Append_List_To (Actuals, Default_Actuals);
2298 end if;
2299 end;
2300 end if;
2302 -- If this is a formal package, normalize the parameter list by adding
2303 -- explicit box associations for the formals that are covered by an
2304 -- Others_Choice.
2306 Append_List (Default_Formals, Formals);
2308 return Assoc_List;
2309 end Analyze_Associations;
2311 -------------------------------
2312 -- Analyze_Formal_Array_Type --
2313 -------------------------------
2315 procedure Analyze_Formal_Array_Type
2316 (T : in out Entity_Id;
2317 Def : Node_Id)
2319 DSS : Node_Id;
2321 begin
2322 -- Treated like a non-generic array declaration, with additional
2323 -- semantic checks.
2325 Enter_Name (T);
2327 if Nkind (Def) = N_Constrained_Array_Definition then
2328 DSS := First (Discrete_Subtype_Definitions (Def));
2329 while Present (DSS) loop
2330 if Nkind (DSS) in N_Subtype_Indication
2331 | N_Range
2332 | N_Attribute_Reference
2333 then
2334 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2335 end if;
2337 Next (DSS);
2338 end loop;
2339 end if;
2341 Array_Type_Declaration (T, Def);
2342 Set_Is_Generic_Type (Base_Type (T));
2344 if Ekind (Component_Type (T)) = E_Incomplete_Type
2345 and then No (Full_View (Component_Type (T)))
2346 then
2347 Error_Msg_N ("premature usage of incomplete type", Def);
2349 -- Check that range constraint is not allowed on the component type
2350 -- of a generic formal array type (AARM 12.5.3(3))
2352 elsif Is_Internal (Component_Type (T))
2353 and then Present (Subtype_Indication (Component_Definition (Def)))
2354 and then Nkind (Original_Node
2355 (Subtype_Indication (Component_Definition (Def)))) =
2356 N_Subtype_Indication
2357 then
2358 Error_Msg_N
2359 ("in a formal, a subtype indication can only be "
2360 & "a subtype mark (RM 12.5.3(3))",
2361 Subtype_Indication (Component_Definition (Def)));
2362 end if;
2364 end Analyze_Formal_Array_Type;
2366 ---------------------------------------------
2367 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2368 ---------------------------------------------
2370 -- As for other generic types, we create a valid type representation with
2371 -- legal but arbitrary attributes, whose values are never considered
2372 -- static. For all scalar types we introduce an anonymous base type, with
2373 -- the same attributes. We choose the corresponding integer type to be
2374 -- Standard_Integer.
2375 -- Here and in other similar routines, the Sloc of the generated internal
2376 -- type must be the same as the sloc of the defining identifier of the
2377 -- formal type declaration, to provide proper source navigation.
2379 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2380 (T : Entity_Id;
2381 Def : Node_Id)
2383 Loc : constant Source_Ptr := Sloc (Def);
2385 Base : constant Entity_Id :=
2386 New_Internal_Entity
2387 (E_Decimal_Fixed_Point_Type,
2388 Current_Scope,
2389 Sloc (Defining_Identifier (Parent (Def))), 'G');
2391 Int_Base : constant Entity_Id := Standard_Integer;
2392 Delta_Val : constant Ureal := Ureal_1;
2393 Digs_Val : constant Uint := Uint_6;
2395 function Make_Dummy_Bound return Node_Id;
2396 -- Return a properly typed universal real literal to use as a bound
2398 ----------------------
2399 -- Make_Dummy_Bound --
2400 ----------------------
2402 function Make_Dummy_Bound return Node_Id is
2403 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2404 begin
2405 Set_Etype (Bound, Universal_Real);
2406 return Bound;
2407 end Make_Dummy_Bound;
2409 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2411 begin
2412 Enter_Name (T);
2414 Set_Etype (Base, Base);
2415 Set_Size_Info (Base, Int_Base);
2416 Set_RM_Size (Base, RM_Size (Int_Base));
2417 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2418 Set_Digits_Value (Base, Digs_Val);
2419 Set_Delta_Value (Base, Delta_Val);
2420 Set_Small_Value (Base, Delta_Val);
2421 Set_Scalar_Range (Base,
2422 Make_Range (Loc,
2423 Low_Bound => Make_Dummy_Bound,
2424 High_Bound => Make_Dummy_Bound));
2426 Set_Is_Generic_Type (Base);
2427 Set_Parent (Base, Parent (Def));
2429 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2430 Set_Etype (T, Base);
2431 Set_Size_Info (T, Int_Base);
2432 Set_RM_Size (T, RM_Size (Int_Base));
2433 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2434 Set_Digits_Value (T, Digs_Val);
2435 Set_Delta_Value (T, Delta_Val);
2436 Set_Small_Value (T, Delta_Val);
2437 Set_Scalar_Range (T, Scalar_Range (Base));
2438 Set_Is_Constrained (T);
2440 Check_Restriction (No_Fixed_Point, Def);
2441 end Analyze_Formal_Decimal_Fixed_Point_Type;
2443 -------------------------------------------
2444 -- Analyze_Formal_Derived_Interface_Type --
2445 -------------------------------------------
2447 procedure Analyze_Formal_Derived_Interface_Type
2448 (N : Node_Id;
2449 T : Entity_Id;
2450 Def : Node_Id)
2452 Loc : constant Source_Ptr := Sloc (Def);
2454 begin
2455 -- Rewrite as a type declaration of a derived type. This ensures that
2456 -- the interface list and primitive operations are properly captured.
2458 Rewrite (N,
2459 Make_Full_Type_Declaration (Loc,
2460 Defining_Identifier => T,
2461 Type_Definition => Def));
2462 Analyze (N);
2463 Set_Is_Generic_Type (T);
2464 end Analyze_Formal_Derived_Interface_Type;
2466 ---------------------------------
2467 -- Analyze_Formal_Derived_Type --
2468 ---------------------------------
2470 procedure Analyze_Formal_Derived_Type
2471 (N : Node_Id;
2472 T : Entity_Id;
2473 Def : Node_Id)
2475 Loc : constant Source_Ptr := Sloc (Def);
2476 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2477 New_N : Node_Id;
2479 begin
2480 Set_Is_Generic_Type (T);
2482 if Private_Present (Def) then
2483 New_N :=
2484 Make_Private_Extension_Declaration (Loc,
2485 Defining_Identifier => T,
2486 Discriminant_Specifications => Discriminant_Specifications (N),
2487 Unknown_Discriminants_Present => Unk_Disc,
2488 Subtype_Indication => Subtype_Mark (Def),
2489 Interface_List => Interface_List (Def));
2491 Set_Abstract_Present (New_N, Abstract_Present (Def));
2492 Set_Limited_Present (New_N, Limited_Present (Def));
2493 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2495 else
2496 New_N :=
2497 Make_Full_Type_Declaration (Loc,
2498 Defining_Identifier => T,
2499 Discriminant_Specifications =>
2500 Discriminant_Specifications (Parent (T)),
2501 Type_Definition =>
2502 Make_Derived_Type_Definition (Loc,
2503 Subtype_Indication => Subtype_Mark (Def)));
2505 Set_Abstract_Present
2506 (Type_Definition (New_N), Abstract_Present (Def));
2507 Set_Limited_Present
2508 (Type_Definition (New_N), Limited_Present (Def));
2509 end if;
2511 Rewrite (N, New_N);
2512 Analyze (N);
2514 if Unk_Disc then
2515 if not Is_Composite_Type (T) then
2516 Error_Msg_N
2517 ("unknown discriminants not allowed for elementary types", N);
2518 else
2519 Set_Has_Unknown_Discriminants (T);
2520 Set_Is_Constrained (T, False);
2521 end if;
2522 end if;
2524 -- If the parent type has a known size, so does the formal, which makes
2525 -- legal representation clauses that involve the formal.
2527 Set_Size_Known_At_Compile_Time
2528 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2529 end Analyze_Formal_Derived_Type;
2531 ----------------------------------
2532 -- Analyze_Formal_Discrete_Type --
2533 ----------------------------------
2535 -- The operations defined for a discrete types are those of an enumeration
2536 -- type. The size is set to an arbitrary value, for use in analyzing the
2537 -- generic unit.
2539 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2540 Loc : constant Source_Ptr := Sloc (Def);
2541 Lo : Node_Id;
2542 Hi : Node_Id;
2544 Base : constant Entity_Id :=
2545 New_Internal_Entity
2546 (E_Floating_Point_Type, Current_Scope,
2547 Sloc (Defining_Identifier (Parent (Def))), 'G');
2549 begin
2550 Enter_Name (T);
2551 Mutate_Ekind (T, E_Enumeration_Subtype);
2552 Set_Etype (T, Base);
2553 Init_Size (T, 8);
2554 Reinit_Alignment (T);
2555 Set_Is_Generic_Type (T);
2556 Set_Is_Constrained (T);
2558 -- For semantic analysis, the bounds of the type must be set to some
2559 -- non-static value. The simplest is to create attribute nodes for those
2560 -- bounds, that refer to the type itself. These bounds are never
2561 -- analyzed but serve as place-holders.
2563 Lo :=
2564 Make_Attribute_Reference (Loc,
2565 Attribute_Name => Name_First,
2566 Prefix => New_Occurrence_Of (T, Loc));
2567 Set_Etype (Lo, T);
2569 Hi :=
2570 Make_Attribute_Reference (Loc,
2571 Attribute_Name => Name_Last,
2572 Prefix => New_Occurrence_Of (T, Loc));
2573 Set_Etype (Hi, T);
2575 Set_Scalar_Range (T,
2576 Make_Range (Loc,
2577 Low_Bound => Lo,
2578 High_Bound => Hi));
2580 Mutate_Ekind (Base, E_Enumeration_Type);
2581 Set_Etype (Base, Base);
2582 Init_Size (Base, 8);
2583 Reinit_Alignment (Base);
2584 Set_Is_Generic_Type (Base);
2585 Set_Scalar_Range (Base, Scalar_Range (T));
2586 Set_Parent (Base, Parent (Def));
2587 end Analyze_Formal_Discrete_Type;
2589 ----------------------------------
2590 -- Analyze_Formal_Floating_Type --
2591 ---------------------------------
2593 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2594 Base : constant Entity_Id :=
2595 New_Internal_Entity
2596 (E_Floating_Point_Type, Current_Scope,
2597 Sloc (Defining_Identifier (Parent (Def))), 'G');
2599 begin
2600 -- The various semantic attributes are taken from the predefined type
2601 -- Float, just so that all of them are initialized. Their values are
2602 -- never used because no constant folding or expansion takes place in
2603 -- the generic itself.
2605 Enter_Name (T);
2606 Mutate_Ekind (T, E_Floating_Point_Subtype);
2607 Set_Etype (T, Base);
2608 Set_Size_Info (T, (Standard_Float));
2609 Set_RM_Size (T, RM_Size (Standard_Float));
2610 Set_Digits_Value (T, Digits_Value (Standard_Float));
2611 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2612 Set_Is_Constrained (T);
2614 Set_Is_Generic_Type (Base);
2615 Set_Etype (Base, Base);
2616 Set_Size_Info (Base, (Standard_Float));
2617 Set_RM_Size (Base, RM_Size (Standard_Float));
2618 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2619 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2620 Set_Parent (Base, Parent (Def));
2622 Check_Restriction (No_Floating_Point, Def);
2623 end Analyze_Formal_Floating_Type;
2625 -----------------------------------
2626 -- Analyze_Formal_Interface_Type;--
2627 -----------------------------------
2629 procedure Analyze_Formal_Interface_Type
2630 (N : Node_Id;
2631 T : Entity_Id;
2632 Def : Node_Id)
2634 Loc : constant Source_Ptr := Sloc (N);
2635 New_N : Node_Id;
2637 begin
2638 New_N :=
2639 Make_Full_Type_Declaration (Loc,
2640 Defining_Identifier => T,
2641 Type_Definition => Def);
2643 Rewrite (N, New_N);
2644 Analyze (N);
2645 Set_Is_Generic_Type (T);
2646 end Analyze_Formal_Interface_Type;
2648 ---------------------------------
2649 -- Analyze_Formal_Modular_Type --
2650 ---------------------------------
2652 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2653 begin
2654 -- Apart from their entity kind, generic modular types are treated like
2655 -- signed integer types, and have the same attributes.
2657 Analyze_Formal_Signed_Integer_Type (T, Def);
2658 Mutate_Ekind (T, E_Modular_Integer_Subtype);
2659 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
2661 end Analyze_Formal_Modular_Type;
2663 ---------------------------------------
2664 -- Analyze_Formal_Object_Declaration --
2665 ---------------------------------------
2667 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2668 E : constant Node_Id := Default_Expression (N);
2669 Id : constant Node_Id := Defining_Identifier (N);
2671 K : Entity_Kind;
2672 Parent_Installed : Boolean := False;
2673 T : Node_Id;
2675 begin
2676 Enter_Name (Id);
2678 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
2680 -- Determine the mode of the formal object
2682 if Out_Present (N) then
2683 K := E_Generic_In_Out_Parameter;
2685 if not In_Present (N) then
2686 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2687 end if;
2689 else
2690 K := E_Generic_In_Parameter;
2691 end if;
2693 if Present (Subtype_Mark (N)) then
2694 Find_Type (Subtype_Mark (N));
2695 T := Entity (Subtype_Mark (N));
2697 -- Verify that there is no redundant null exclusion
2699 if Null_Exclusion_Present (N) then
2700 if not Is_Access_Type (T) then
2701 Error_Msg_N
2702 ("null exclusion can only apply to an access type", N);
2704 elsif Can_Never_Be_Null (T) then
2705 Error_Msg_NE
2706 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2707 end if;
2708 end if;
2710 -- Ada 2005 (AI-423): Formal object with an access definition
2712 else
2713 Check_Access_Definition (N);
2714 T := Access_Definition
2715 (Related_Nod => N,
2716 N => Access_Definition (N));
2717 end if;
2719 if Ekind (T) = E_Incomplete_Type then
2720 declare
2721 Error_Node : Node_Id;
2723 begin
2724 if Present (Subtype_Mark (N)) then
2725 Error_Node := Subtype_Mark (N);
2726 else
2727 Check_Access_Definition (N);
2728 Error_Node := Access_Definition (N);
2729 end if;
2731 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2732 end;
2733 end if;
2735 if K = E_Generic_In_Parameter then
2737 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2739 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2740 Error_Msg_N
2741 ("generic formal of mode IN must not be of limited type", N);
2742 Explain_Limited_Type (T, N);
2743 end if;
2745 if Is_Abstract_Type (T) then
2746 Error_Msg_N
2747 ("generic formal of mode IN must not be of abstract type", N);
2748 end if;
2750 if Present (E) then
2751 Preanalyze_Spec_Expression (E, T);
2753 -- The default for a ghost generic formal IN parameter of
2754 -- access-to-variable type should be a ghost object (SPARK
2755 -- RM 6.9(13)).
2757 if Is_Access_Variable (T) then
2758 Check_Ghost_Formal_Variable
2759 (Actual => E,
2760 Formal => Id,
2761 Is_Default => True);
2762 end if;
2764 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2765 Error_Msg_N
2766 ("initialization not allowed for limited types", E);
2767 Explain_Limited_Type (T, E);
2768 end if;
2769 end if;
2771 Mutate_Ekind (Id, K);
2772 Set_Etype (Id, T);
2774 -- Case of generic IN OUT parameter
2776 else
2777 -- If the formal has an unconstrained type, construct its actual
2778 -- subtype, as is done for subprogram formals. In this fashion, all
2779 -- its uses can refer to specific bounds.
2781 Mutate_Ekind (Id, K);
2782 Set_Etype (Id, T);
2784 if (Is_Array_Type (T) and then not Is_Constrained (T))
2785 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2786 then
2787 declare
2788 Non_Freezing_Ref : constant Node_Id :=
2789 New_Occurrence_Of (Id, Sloc (Id));
2790 Decl : Node_Id;
2792 begin
2793 -- Make sure the actual subtype doesn't generate bogus freezing
2795 Set_Must_Not_Freeze (Non_Freezing_Ref);
2796 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2797 Insert_Before_And_Analyze (N, Decl);
2798 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2799 end;
2800 else
2801 Set_Actual_Subtype (Id, T);
2802 end if;
2804 if Present (E) then
2805 Error_Msg_N
2806 ("initialization not allowed for `IN OUT` formals", N);
2807 end if;
2808 end if;
2810 if Has_Aspects (N) then
2811 Analyze_Aspect_Specifications (N, Id);
2812 end if;
2814 if Parent_Installed then
2815 Remove_Parent;
2816 end if;
2817 end Analyze_Formal_Object_Declaration;
2819 ----------------------------------------------
2820 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2821 ----------------------------------------------
2823 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2824 (T : Entity_Id;
2825 Def : Node_Id)
2827 Loc : constant Source_Ptr := Sloc (Def);
2828 Base : constant Entity_Id :=
2829 New_Internal_Entity
2830 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2831 Sloc (Defining_Identifier (Parent (Def))), 'G');
2833 begin
2834 -- The semantic attributes are set for completeness only, their values
2835 -- will never be used, since all properties of the type are non-static.
2837 Enter_Name (T);
2838 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2839 Set_Etype (T, Base);
2840 Set_Size_Info (T, Standard_Integer);
2841 Set_RM_Size (T, RM_Size (Standard_Integer));
2842 Set_Small_Value (T, Ureal_1);
2843 Set_Delta_Value (T, Ureal_1);
2844 Set_Scalar_Range (T,
2845 Make_Range (Loc,
2846 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2847 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2848 Set_Is_Constrained (T);
2850 Set_Is_Generic_Type (Base);
2851 Set_Etype (Base, Base);
2852 Set_Size_Info (Base, Standard_Integer);
2853 Set_RM_Size (Base, RM_Size (Standard_Integer));
2854 Set_Small_Value (Base, Ureal_1);
2855 Set_Delta_Value (Base, Ureal_1);
2856 Set_Scalar_Range (Base, Scalar_Range (T));
2857 Set_Parent (Base, Parent (Def));
2859 Check_Restriction (No_Fixed_Point, Def);
2860 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2862 ----------------------------------------
2863 -- Analyze_Formal_Package_Declaration --
2864 ----------------------------------------
2866 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2867 Gen_Id : constant Node_Id := Name (N);
2868 Loc : constant Source_Ptr := Sloc (N);
2869 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2870 Formal : Entity_Id;
2871 Gen_Decl : Node_Id;
2872 Gen_Unit : Entity_Id;
2873 Renaming : Node_Id;
2875 Vis_Prims_List : Elist_Id := No_Elist;
2876 -- List of primitives made temporarily visible in the instantiation
2877 -- to match the visibility of the formal type.
2879 function Build_Local_Package return Node_Id;
2880 -- The formal package is rewritten so that its parameters are replaced
2881 -- with corresponding declarations. For parameters with bona fide
2882 -- associations these declarations are created by Analyze_Associations
2883 -- as for a regular instantiation. For boxed parameters, we preserve
2884 -- the formal declarations and analyze them, in order to introduce
2885 -- entities of the right kind in the environment of the formal.
2887 -------------------------
2888 -- Build_Local_Package --
2889 -------------------------
2891 function Build_Local_Package return Node_Id is
2892 Decls : List_Id;
2893 Pack_Decl : Node_Id;
2895 begin
2896 -- Within the formal, the name of the generic package is a renaming
2897 -- of the formal (as for a regular instantiation).
2899 Pack_Decl :=
2900 Make_Package_Declaration (Loc,
2901 Specification =>
2902 Copy_Generic_Node
2903 (Specification (Original_Node (Gen_Decl)),
2904 Empty, Instantiating => True));
2906 Renaming :=
2907 Make_Package_Renaming_Declaration (Loc,
2908 Defining_Unit_Name =>
2909 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2910 Name => New_Occurrence_Of (Formal, Loc));
2912 if Nkind (Gen_Id) = N_Identifier
2913 and then Chars (Gen_Id) = Chars (Pack_Id)
2914 then
2915 Error_Msg_NE
2916 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2917 end if;
2919 -- If the formal is declared with a box, or with an others choice,
2920 -- create corresponding declarations for all entities in the formal
2921 -- part, so that names with the proper types are available in the
2922 -- specification of the formal package.
2924 -- On the other hand, if there are no associations, then all the
2925 -- formals must have defaults, and this will be checked by the
2926 -- call to Analyze_Associations.
2928 if Box_Present (N)
2929 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2930 then
2931 declare
2932 Formal_Decl : Node_Id;
2934 begin
2935 -- TBA : for a formal package, need to recurse ???
2937 Decls := New_List;
2938 Formal_Decl :=
2939 First
2940 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2941 while Present (Formal_Decl) loop
2942 Append_To
2943 (Decls,
2944 Copy_Generic_Node
2945 (Formal_Decl, Empty, Instantiating => True));
2946 Next (Formal_Decl);
2947 end loop;
2948 end;
2950 -- If generic associations are present, use Analyze_Associations to
2951 -- create the proper renaming declarations.
2953 else
2954 declare
2955 Act_Tree : constant Node_Id :=
2956 Copy_Generic_Node
2957 (Original_Node (Gen_Decl), Empty,
2958 Instantiating => True);
2960 begin
2961 Generic_Renamings.Set_Last (0);
2962 Generic_Renamings_HTable.Reset;
2963 Instantiation_Node := N;
2965 Decls :=
2966 Analyze_Associations
2967 (I_Node => Original_Node (N),
2968 Formals => Generic_Formal_Declarations (Act_Tree),
2969 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2971 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2972 end;
2973 end if;
2975 Append (Renaming, To => Decls);
2977 -- Add generated declarations ahead of local declarations in
2978 -- the package.
2980 if No (Visible_Declarations (Specification (Pack_Decl))) then
2981 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2982 else
2983 Insert_List_Before
2984 (First (Visible_Declarations (Specification (Pack_Decl))),
2985 Decls);
2986 end if;
2988 return Pack_Decl;
2989 end Build_Local_Package;
2991 -- Local variables
2993 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2994 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2996 Associations : Boolean := True;
2997 New_N : Node_Id;
2998 Parent_Installed : Boolean := False;
2999 Parent_Instance : Entity_Id;
3000 Renaming_In_Par : Entity_Id;
3002 -- Start of processing for Analyze_Formal_Package_Declaration
3004 begin
3005 Check_Text_IO_Special_Unit (Gen_Id);
3007 Init_Env;
3008 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3009 Gen_Unit := Entity (Gen_Id);
3011 -- Check for a formal package that is a package renaming
3013 if Present (Renamed_Entity (Gen_Unit)) then
3015 -- Indicate that unit is used, before replacing it with renamed
3016 -- entity for use below.
3018 if In_Extended_Main_Source_Unit (N) then
3019 Set_Is_Instantiated (Gen_Unit);
3020 Generate_Reference (Gen_Unit, N);
3021 end if;
3023 Gen_Unit := Renamed_Entity (Gen_Unit);
3024 end if;
3026 if Ekind (Gen_Unit) /= E_Generic_Package then
3027 Error_Msg_N ("expect generic package name", Gen_Id);
3028 Restore_Env;
3029 goto Leave;
3031 elsif Gen_Unit = Current_Scope then
3032 Error_Msg_N
3033 ("generic package cannot be used as a formal package of itself",
3034 Gen_Id);
3035 Restore_Env;
3036 goto Leave;
3038 elsif In_Open_Scopes (Gen_Unit) then
3039 if Is_Compilation_Unit (Gen_Unit)
3040 and then Is_Child_Unit (Current_Scope)
3041 then
3042 -- Special-case the error when the formal is a parent, and
3043 -- continue analysis to minimize cascaded errors.
3045 Error_Msg_N
3046 ("generic parent cannot be used as formal package of a child "
3047 & "unit", Gen_Id);
3049 else
3050 Error_Msg_N
3051 ("generic package cannot be used as a formal package within "
3052 & "itself", Gen_Id);
3053 Restore_Env;
3054 goto Leave;
3055 end if;
3056 end if;
3058 -- Check that name of formal package does not hide name of generic,
3059 -- or its leading prefix. This check must be done separately because
3060 -- the name of the generic has already been analyzed.
3062 declare
3063 Gen_Name : Entity_Id;
3065 begin
3066 Gen_Name := Gen_Id;
3067 while Nkind (Gen_Name) = N_Expanded_Name loop
3068 Gen_Name := Prefix (Gen_Name);
3069 end loop;
3071 if Chars (Gen_Name) = Chars (Pack_Id) then
3072 Error_Msg_NE
3073 ("& is hidden within declaration of formal package",
3074 Gen_Id, Gen_Name);
3075 end if;
3076 end;
3078 if Box_Present (N)
3079 or else No (Generic_Associations (N))
3080 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3081 then
3082 Associations := False;
3083 end if;
3085 -- If there are no generic associations, the generic parameters appear
3086 -- as local entities and are instantiated like them. We copy the generic
3087 -- package declaration as if it were an instantiation, and analyze it
3088 -- like a regular package, except that we treat the formals as
3089 -- additional visible components.
3091 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3093 if In_Extended_Main_Source_Unit (N) then
3094 Set_Is_Instantiated (Gen_Unit);
3095 Generate_Reference (Gen_Unit, N);
3096 end if;
3098 Formal := New_Copy (Pack_Id);
3099 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3101 -- Make local generic without formals. The formals will be replaced with
3102 -- internal declarations.
3104 begin
3105 New_N := Build_Local_Package;
3107 -- If there are errors in the parameter list, Analyze_Associations
3108 -- raises Instantiation_Error. Patch the declaration to prevent further
3109 -- exception propagation.
3111 exception
3112 when Instantiation_Error =>
3113 Enter_Name (Formal);
3114 Mutate_Ekind (Formal, E_Variable);
3115 Set_Etype (Formal, Any_Type);
3116 Restore_Hidden_Primitives (Vis_Prims_List);
3118 if Parent_Installed then
3119 Remove_Parent;
3120 end if;
3122 goto Leave;
3123 end;
3125 Rewrite (N, New_N);
3126 Set_Defining_Unit_Name (Specification (New_N), Formal);
3127 Set_Generic_Parent (Specification (N), Gen_Unit);
3128 Set_Instance_Env (Gen_Unit, Formal);
3129 Set_Is_Generic_Instance (Formal);
3131 Enter_Name (Formal);
3132 Mutate_Ekind (Formal, E_Package);
3133 Set_Etype (Formal, Standard_Void_Type);
3134 Set_Inner_Instances (Formal, New_Elmt_List);
3136 -- It is unclear that any aspects can apply to a formal package
3137 -- declaration, given that they look like a hidden conformance
3138 -- requirement on the corresponding actual. However, Abstract_State
3139 -- must be treated specially because it generates declarations that
3140 -- must appear before other declarations in the specification and
3141 -- must be analyzed at once.
3143 if Present (Aspect_Specifications (Gen_Decl)) then
3144 if No (Aspect_Specifications (N)) then
3145 Set_Aspect_Specifications (N, New_List);
3146 end if;
3148 declare
3149 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3150 New_A : Node_Id;
3152 begin
3153 while Present (ASN) loop
3154 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3155 New_A :=
3156 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3157 Set_Entity (New_A, Formal);
3158 Set_Analyzed (New_A, False);
3159 Append (New_A, Aspect_Specifications (N));
3160 Analyze_Aspect_Specifications (N, Formal);
3161 exit;
3162 end if;
3164 Next (ASN);
3165 end loop;
3166 end;
3167 end if;
3169 Push_Scope (Formal);
3171 -- Manually set the SPARK_Mode from the context because the package
3172 -- declaration is never analyzed.
3174 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3175 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3176 Set_SPARK_Pragma_Inherited (Formal);
3177 Set_SPARK_Aux_Pragma_Inherited (Formal);
3179 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3181 -- Similarly, we have to make the name of the formal visible in the
3182 -- parent instance, to resolve properly fully qualified names that
3183 -- may appear in the generic unit. The parent instance has been
3184 -- placed on the scope stack ahead of the current scope.
3186 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3188 Renaming_In_Par :=
3189 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3190 Mutate_Ekind (Renaming_In_Par, E_Package);
3191 Set_Is_Not_Self_Hidden (Renaming_In_Par);
3192 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3193 Set_Scope (Renaming_In_Par, Parent_Instance);
3194 Set_Parent (Renaming_In_Par, Parent (Formal));
3195 Set_Renamed_Entity (Renaming_In_Par, Formal);
3196 Append_Entity (Renaming_In_Par, Parent_Instance);
3197 end if;
3199 -- A formal package declaration behaves as a package instantiation with
3200 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3201 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3202 -- all SPARK_Mode pragmas within the generic_package_name.
3204 if SPARK_Mode /= On then
3205 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3207 -- Mark the formal spec in case the body is instantiated at a later
3208 -- pass. This preserves the original context in effect for the body.
3210 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3211 end if;
3213 Analyze (Specification (N));
3215 -- The formals for which associations are provided are not visible
3216 -- outside of the formal package. The others are still declared by a
3217 -- formal parameter declaration.
3219 -- If there are no associations, the only local entity to hide is the
3220 -- generated package renaming itself.
3222 declare
3223 E : Entity_Id;
3225 begin
3226 E := First_Entity (Formal);
3227 while Present (E) loop
3228 if Associations and then not Is_Generic_Formal (E) then
3229 Set_Is_Hidden (E);
3230 end if;
3232 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3233 Set_Is_Hidden (E);
3234 exit;
3235 end if;
3237 Next_Entity (E);
3238 end loop;
3239 end;
3241 End_Package_Scope (Formal);
3242 Restore_Hidden_Primitives (Vis_Prims_List);
3244 if Parent_Installed then
3245 Remove_Parent;
3246 end if;
3248 Restore_Env;
3250 -- Inside the generic unit, the formal package is a regular package, but
3251 -- no body is needed for it. Note that after instantiation, the defining
3252 -- unit name we need is in the new tree and not in the original (see
3253 -- Package_Instantiation). A generic formal package is an instance, and
3254 -- can be used as an actual for an inner instance.
3256 Set_Has_Completion (Formal, True);
3258 -- Add semantic information to the original defining identifier.
3260 Mutate_Ekind (Pack_Id, E_Package);
3261 Set_Etype (Pack_Id, Standard_Void_Type);
3262 Set_Scope (Pack_Id, Scope (Formal));
3263 Set_Has_Completion (Pack_Id, True);
3265 <<Leave>>
3266 if Has_Aspects (N) then
3267 -- Unclear that any other aspects may appear here, analyze them
3268 -- for completion, given that the grammar allows their appearance.
3270 Analyze_Aspect_Specifications (N, Pack_Id);
3271 end if;
3273 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3274 end Analyze_Formal_Package_Declaration;
3276 ---------------------------------
3277 -- Analyze_Formal_Private_Type --
3278 ---------------------------------
3280 procedure Analyze_Formal_Private_Type
3281 (N : Node_Id;
3282 T : Entity_Id;
3283 Def : Node_Id)
3285 begin
3286 New_Private_Type (N, T, Def);
3288 -- Set the size to an arbitrary but legal value
3290 Set_Size_Info (T, Standard_Integer);
3291 Set_RM_Size (T, RM_Size (Standard_Integer));
3292 end Analyze_Formal_Private_Type;
3294 ------------------------------------
3295 -- Analyze_Formal_Incomplete_Type --
3296 ------------------------------------
3298 procedure Analyze_Formal_Incomplete_Type
3299 (T : Entity_Id;
3300 Def : Node_Id)
3302 begin
3303 Enter_Name (T);
3304 Mutate_Ekind (T, E_Incomplete_Type);
3305 Set_Etype (T, T);
3306 Set_Private_Dependents (T, New_Elmt_List);
3308 if Tagged_Present (Def) then
3309 Set_Is_Tagged_Type (T);
3310 Make_Class_Wide_Type (T);
3311 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3312 end if;
3313 end Analyze_Formal_Incomplete_Type;
3315 ----------------------------------------
3316 -- Analyze_Formal_Signed_Integer_Type --
3317 ----------------------------------------
3319 procedure Analyze_Formal_Signed_Integer_Type
3320 (T : Entity_Id;
3321 Def : Node_Id)
3323 Base : constant Entity_Id :=
3324 New_Internal_Entity
3325 (E_Signed_Integer_Type,
3326 Current_Scope,
3327 Sloc (Defining_Identifier (Parent (Def))), 'G');
3329 begin
3330 Enter_Name (T);
3332 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3333 Set_Etype (T, Base);
3334 Set_Size_Info (T, Standard_Integer);
3335 Set_RM_Size (T, RM_Size (Standard_Integer));
3336 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3337 Set_Is_Constrained (T);
3339 Set_Is_Generic_Type (Base);
3340 Set_Size_Info (Base, Standard_Integer);
3341 Set_RM_Size (Base, RM_Size (Standard_Integer));
3342 Set_Etype (Base, Base);
3343 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3344 Set_Parent (Base, Parent (Def));
3345 end Analyze_Formal_Signed_Integer_Type;
3347 -------------------------------------------
3348 -- Analyze_Formal_Subprogram_Declaration --
3349 -------------------------------------------
3351 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3352 Spec : constant Node_Id := Specification (N);
3353 Def : constant Node_Id := Default_Name (N);
3354 Expr : constant Node_Id := Expression (N);
3355 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3357 Parent_Installed : Boolean := False;
3358 Subp : Entity_Id;
3360 begin
3361 if Nam = Error then
3362 return;
3363 end if;
3365 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3366 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3367 goto Leave;
3368 end if;
3370 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3372 Analyze_Subprogram_Declaration (N);
3373 Set_Is_Formal_Subprogram (Nam);
3374 Set_Has_Completion (Nam);
3376 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3377 Set_Is_Abstract_Subprogram (Nam);
3379 Set_Is_Dispatching_Operation (Nam);
3381 -- A formal abstract procedure cannot have a null default
3382 -- (RM 12.6(4.1/2)).
3384 if Nkind (Spec) = N_Procedure_Specification
3385 and then Null_Present (Spec)
3386 then
3387 Error_Msg_N
3388 ("a formal abstract subprogram cannot default to null", Spec);
3389 end if;
3391 -- A formal abstract function cannot have an expression default
3392 -- (expression defaults are allowed for nonabstract formal functions
3393 -- when extensions are enabled).
3395 if Nkind (Spec) = N_Function_Specification
3396 and then Present (Expr)
3397 then
3398 Error_Msg_N
3399 ("a formal abstract subprogram cannot default to an expression",
3400 Spec);
3401 end if;
3403 declare
3404 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3405 begin
3406 if No (Ctrl_Type) then
3407 Error_Msg_N
3408 ("abstract formal subprogram must have a controlling type",
3411 elsif Ada_Version >= Ada_2012
3412 and then Is_Incomplete_Type (Ctrl_Type)
3413 then
3414 Error_Msg_NE
3415 ("controlling type of abstract formal subprogram cannot "
3416 & "be incomplete type", N, Ctrl_Type);
3418 else
3419 Check_Controlling_Formals (Ctrl_Type, Nam);
3420 end if;
3421 end;
3422 end if;
3424 -- Default name is resolved at the point of instantiation
3426 if Box_Present (N) then
3427 null;
3429 -- Default name is bound at the point of generic declaration
3431 elsif Present (Def) then
3432 if Nkind (Def) = N_Operator_Symbol then
3433 Find_Direct_Name (Def);
3435 elsif Nkind (Def) /= N_Attribute_Reference then
3436 Analyze (Def);
3438 else
3439 -- For an attribute reference, analyze the prefix and verify
3440 -- that it has the proper profile for the subprogram.
3442 Analyze (Prefix (Def));
3443 Valid_Default_Attribute (Nam, Def);
3444 goto Leave;
3445 end if;
3447 -- The default for a ghost generic formal procedure should be a ghost
3448 -- procedure (SPARK RM 6.9(13)).
3450 if Ekind (Nam) = E_Procedure then
3451 declare
3452 Def_E : Entity_Id := Empty;
3453 begin
3454 if Nkind (Def) in N_Has_Entity then
3455 Def_E := Entity (Def);
3456 end if;
3458 Check_Ghost_Formal_Procedure_Or_Package
3459 (N => Def,
3460 Actual => Def_E,
3461 Formal => Nam,
3462 Is_Default => True);
3463 end;
3464 end if;
3466 -- Default name may be overloaded, in which case the interpretation
3467 -- with the correct profile must be selected, as for a renaming.
3468 -- If the definition is an indexed component, it must denote a
3469 -- member of an entry family. If it is a selected component, it
3470 -- can be a protected operation.
3472 if Etype (Def) = Any_Type then
3473 goto Leave;
3475 elsif Nkind (Def) = N_Selected_Component then
3476 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3477 Error_Msg_N ("expect valid subprogram name as default", Def);
3478 end if;
3480 elsif Nkind (Def) = N_Indexed_Component then
3481 if Is_Entity_Name (Prefix (Def)) then
3482 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3483 Error_Msg_N ("expect valid subprogram name as default", Def);
3484 end if;
3486 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3487 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3488 E_Entry_Family
3489 then
3490 Error_Msg_N ("expect valid subprogram name as default", Def);
3491 end if;
3493 else
3494 Error_Msg_N ("expect valid subprogram name as default", Def);
3495 goto Leave;
3496 end if;
3498 elsif Nkind (Def) = N_Character_Literal then
3500 -- Needs some type checks: subprogram should be parameterless???
3502 Resolve (Def, (Etype (Nam)));
3504 elsif not Is_Entity_Name (Def)
3505 or else not Is_Overloadable (Entity (Def))
3506 then
3507 Error_Msg_N ("expect valid subprogram name as default", Def);
3508 goto Leave;
3510 elsif not Is_Overloaded (Def) then
3511 Subp := Entity (Def);
3513 if Subp = Nam then
3514 Error_Msg_N ("premature usage of formal subprogram", Def);
3516 elsif not Entity_Matches_Spec (Subp, Nam) then
3517 Error_Msg_N ("no visible entity matches specification", Def);
3518 end if;
3520 -- More than one interpretation, so disambiguate as for a renaming
3522 else
3523 declare
3524 I : Interp_Index;
3525 I1 : Interp_Index := 0;
3526 It : Interp;
3527 It1 : Interp;
3529 begin
3530 Subp := Any_Id;
3531 Get_First_Interp (Def, I, It);
3532 while Present (It.Nam) loop
3533 if Entity_Matches_Spec (It.Nam, Nam) then
3534 if Subp /= Any_Id then
3535 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3537 if It1 = No_Interp then
3538 Error_Msg_N ("ambiguous default subprogram", Def);
3539 else
3540 Subp := It1.Nam;
3541 end if;
3543 exit;
3545 else
3546 I1 := I;
3547 Subp := It.Nam;
3548 end if;
3549 end if;
3551 Get_Next_Interp (I, It);
3552 end loop;
3553 end;
3555 if Subp /= Any_Id then
3557 -- Subprogram found, generate reference to it
3559 Set_Entity (Def, Subp);
3560 Generate_Reference (Subp, Def);
3562 if Subp = Nam then
3563 Error_Msg_N ("premature usage of formal subprogram", Def);
3565 elsif Ekind (Subp) /= E_Operator then
3566 Check_Mode_Conformant (Subp, Nam);
3567 end if;
3569 else
3570 Error_Msg_N ("no visible subprogram matches specification", N);
3571 end if;
3572 end if;
3574 -- When extensions are enabled, an expression can be given as default
3575 -- for a formal function. The expression must be of the function result
3576 -- type and can reference formal parameters of the function.
3578 elsif Present (Expr) then
3579 Push_Scope (Nam);
3580 Install_Formals (Nam);
3581 Preanalyze_Spec_Expression (Expr, Etype (Nam));
3582 End_Scope;
3583 end if;
3585 <<Leave>>
3586 if Has_Aspects (N) then
3587 Analyze_Aspect_Specifications (N, Nam);
3588 end if;
3590 if Parent_Installed then
3591 Remove_Parent;
3592 end if;
3593 end Analyze_Formal_Subprogram_Declaration;
3595 -------------------------------------
3596 -- Analyze_Formal_Type_Declaration --
3597 -------------------------------------
3599 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3600 Def : constant Node_Id := Formal_Type_Definition (N);
3602 Parent_Installed : Boolean := False;
3603 T : Entity_Id;
3605 begin
3606 T := Defining_Identifier (N);
3608 if Present (Discriminant_Specifications (N))
3609 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3610 then
3611 Error_Msg_N
3612 ("discriminants not allowed for this formal type", T);
3613 end if;
3615 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3617 -- Enter the new name, and branch to specific routine
3619 case Nkind (Def) is
3620 when N_Formal_Private_Type_Definition =>
3621 Analyze_Formal_Private_Type (N, T, Def);
3623 when N_Formal_Derived_Type_Definition =>
3624 Analyze_Formal_Derived_Type (N, T, Def);
3626 when N_Formal_Incomplete_Type_Definition =>
3627 Analyze_Formal_Incomplete_Type (T, Def);
3629 when N_Formal_Discrete_Type_Definition =>
3630 Analyze_Formal_Discrete_Type (T, Def);
3632 when N_Formal_Signed_Integer_Type_Definition =>
3633 Analyze_Formal_Signed_Integer_Type (T, Def);
3635 when N_Formal_Modular_Type_Definition =>
3636 Analyze_Formal_Modular_Type (T, Def);
3638 when N_Formal_Floating_Point_Definition =>
3639 Analyze_Formal_Floating_Type (T, Def);
3641 when N_Formal_Ordinary_Fixed_Point_Definition =>
3642 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3644 when N_Formal_Decimal_Fixed_Point_Definition =>
3645 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3647 when N_Array_Type_Definition =>
3648 Analyze_Formal_Array_Type (T, Def);
3650 when N_Access_Function_Definition
3651 | N_Access_Procedure_Definition
3652 | N_Access_To_Object_Definition
3654 Analyze_Generic_Access_Type (T, Def);
3656 -- Ada 2005: a interface declaration is encoded as an abstract
3657 -- record declaration or a abstract type derivation.
3659 when N_Record_Definition =>
3660 Analyze_Formal_Interface_Type (N, T, Def);
3662 when N_Derived_Type_Definition =>
3663 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3665 when N_Error =>
3666 null;
3668 when others =>
3669 raise Program_Error;
3670 end case;
3672 -- A formal type declaration declares a type and its first
3673 -- subtype.
3675 Set_Is_Generic_Type (T);
3676 Set_Is_First_Subtype (T);
3678 if Present (Default_Subtype_Mark (Original_Node (N))) then
3679 Validate_Formal_Type_Default (N);
3680 end if;
3682 if Has_Aspects (N) then
3683 Analyze_Aspect_Specifications (N, T);
3684 end if;
3686 if Parent_Installed then
3687 Remove_Parent;
3688 end if;
3689 end Analyze_Formal_Type_Declaration;
3691 ------------------------------------
3692 -- Analyze_Function_Instantiation --
3693 ------------------------------------
3695 procedure Analyze_Function_Instantiation (N : Node_Id) is
3696 begin
3697 Analyze_Subprogram_Instantiation (N, E_Function);
3698 end Analyze_Function_Instantiation;
3700 ---------------------------------
3701 -- Analyze_Generic_Access_Type --
3702 ---------------------------------
3704 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3705 begin
3706 Enter_Name (T);
3708 if Nkind (Def) = N_Access_To_Object_Definition then
3709 Access_Type_Declaration (T, Def);
3711 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3712 and then No (Full_View (Designated_Type (T)))
3713 and then not Is_Generic_Type (Designated_Type (T))
3714 then
3715 Error_Msg_N ("premature usage of incomplete type", Def);
3717 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3718 Error_Msg_N
3719 ("only a subtype mark is allowed in a formal", Def);
3720 end if;
3722 else
3723 Access_Subprogram_Declaration (T, Def);
3724 end if;
3725 end Analyze_Generic_Access_Type;
3727 ---------------------------------
3728 -- Analyze_Generic_Formal_Part --
3729 ---------------------------------
3731 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3732 Gen_Parm_Decl : Node_Id;
3734 begin
3735 -- The generic formals are processed in the scope of the generic unit,
3736 -- where they are immediately visible. The scope is installed by the
3737 -- caller.
3739 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3740 while Present (Gen_Parm_Decl) loop
3741 Analyze (Gen_Parm_Decl);
3742 Next (Gen_Parm_Decl);
3743 end loop;
3745 Generate_Reference_To_Generic_Formals (Current_Scope);
3747 -- For Ada 2022, some formal parameters can carry aspects, which must
3748 -- be name-resolved at the end of the list of formal parameters (which
3749 -- has the semantics of a declaration list).
3751 Analyze_Contracts (Generic_Formal_Declarations (N));
3752 end Analyze_Generic_Formal_Part;
3754 ------------------------------------------
3755 -- Analyze_Generic_Package_Declaration --
3756 ------------------------------------------
3758 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3759 Decls : constant List_Id := Visible_Declarations (Specification (N));
3760 Loc : constant Source_Ptr := Sloc (N);
3762 Decl : Node_Id;
3763 Id : Entity_Id;
3764 New_N : Node_Id;
3765 Renaming : Node_Id;
3766 Save_Parent : Node_Id;
3768 begin
3769 -- A generic may grant access to its private enclosing context depending
3770 -- on the placement of its corresponding body. From elaboration point of
3771 -- view, the flow of execution may enter this private context, and then
3772 -- reach an external unit, thus producing a dependency on that external
3773 -- unit. For such a path to be properly discovered and encoded in the
3774 -- ALI file of the main unit, let the ABE mechanism process the body of
3775 -- the main unit, and encode all relevant invocation constructs and the
3776 -- relations between them.
3778 Mark_Save_Invocation_Graph_Of_Body;
3780 -- We introduce a renaming of the enclosing package, to have a usable
3781 -- entity as the prefix of an expanded name for a local entity of the
3782 -- form Par.P.Q, where P is the generic package. This is because a local
3783 -- entity named P may hide it, so that the usual visibility rules in
3784 -- the instance will not resolve properly.
3786 Renaming :=
3787 Make_Package_Renaming_Declaration (Loc,
3788 Defining_Unit_Name =>
3789 Make_Defining_Identifier (Loc,
3790 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3791 Name =>
3792 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3794 -- The declaration is inserted before other declarations, but before
3795 -- pragmas that may be library-unit pragmas and must appear before other
3796 -- declarations. The pragma Compile_Time_Error is not in this class, and
3797 -- may contain an expression that includes such a qualified name, so the
3798 -- renaming declaration must appear before it.
3800 -- Are there other pragmas that require this special handling ???
3802 if Present (Decls) then
3803 Decl := First (Decls);
3804 while Present (Decl)
3805 and then Nkind (Decl) = N_Pragma
3806 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3807 loop
3808 Next (Decl);
3809 end loop;
3811 if Present (Decl) then
3812 Insert_Before (Decl, Renaming);
3813 else
3814 Append (Renaming, Visible_Declarations (Specification (N)));
3815 end if;
3817 else
3818 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3819 end if;
3821 -- Create copy of generic unit, and save for instantiation. If the unit
3822 -- is a child unit, do not copy the specifications for the parent, which
3823 -- are not part of the generic tree.
3825 Save_Parent := Parent_Spec (N);
3826 Set_Parent_Spec (N, Empty);
3828 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3829 Set_Parent_Spec (New_N, Save_Parent);
3830 Rewrite (N, New_N);
3832 -- Once the contents of the generic copy and the template are swapped,
3833 -- do the same for their respective aspect specifications.
3835 Exchange_Aspects (N, New_N);
3837 -- Collect all contract-related source pragmas found within the template
3838 -- and attach them to the contract of the package spec. This contract is
3839 -- used in the capture of global references within annotations.
3841 Create_Generic_Contract (N);
3843 Id := Defining_Entity (N);
3844 Generate_Definition (Id);
3846 -- Expansion is not applied to generic units
3848 Start_Generic;
3850 Enter_Name (Id);
3851 Mutate_Ekind (Id, E_Generic_Package);
3852 Set_Is_Not_Self_Hidden (Id);
3853 Set_Etype (Id, Standard_Void_Type);
3855 -- Set SPARK_Mode from context
3857 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3858 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3859 Set_SPARK_Pragma_Inherited (Id);
3860 Set_SPARK_Aux_Pragma_Inherited (Id);
3862 -- Preserve relevant elaboration-related attributes of the context which
3863 -- are no longer available or very expensive to recompute once analysis,
3864 -- resolution, and expansion are over.
3866 Mark_Elaboration_Attributes
3867 (N_Id => Id,
3868 Checks => True,
3869 Warnings => True);
3871 -- Analyze aspects now, so that generated pragmas appear in the
3872 -- declarations before building and analyzing the generic copy.
3874 if Has_Aspects (N) then
3875 Analyze_Aspect_Specifications (N, Id);
3876 end if;
3878 Push_Scope (Id);
3879 Enter_Generic_Scope (Id);
3880 Set_Inner_Instances (Id, New_Elmt_List);
3882 Set_Categorization_From_Pragmas (N);
3883 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3885 -- Link the declaration of the generic homonym in the generic copy to
3886 -- the package it renames, so that it is always resolved properly.
3888 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3889 Set_Entity (Associated_Node (Name (Renaming)), Id);
3891 -- For a library unit, we have reconstructed the entity for the unit,
3892 -- and must reset it in the library tables.
3894 if Nkind (Parent (N)) = N_Compilation_Unit then
3895 Set_Cunit_Entity (Current_Sem_Unit, Id);
3896 end if;
3898 Analyze_Generic_Formal_Part (N);
3900 -- After processing the generic formals, analysis proceeds as for a
3901 -- non-generic package.
3903 Analyze (Specification (N));
3905 Validate_Categorization_Dependency (N, Id);
3907 End_Generic;
3909 End_Package_Scope (Id);
3910 Exit_Generic_Scope (Id);
3912 -- If the generic appears within a package unit, the body of that unit
3913 -- has to be present for instantiation and inlining.
3915 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3916 Set_Body_Needed_For_Inlining
3917 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3918 end if;
3920 if Nkind (Parent (N)) /= N_Compilation_Unit then
3921 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3922 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3923 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3925 else
3926 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3927 Validate_RT_RAT_Component (N);
3929 -- If this is a spec without a body, check that generic parameters
3930 -- are referenced.
3932 if not Body_Required (Parent (N)) then
3933 Check_References (Id);
3934 end if;
3935 end if;
3937 -- If there is a specified storage pool in the context, create an
3938 -- aspect on the package declaration, so that it is used in any
3939 -- instance that does not override it.
3941 if Present (Default_Pool) then
3942 declare
3943 ASN : Node_Id;
3945 begin
3946 ASN :=
3947 Make_Aspect_Specification (Loc,
3948 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3949 Expression => New_Copy (Default_Pool));
3951 if No (Aspect_Specifications (Specification (N))) then
3952 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3953 else
3954 Append (ASN, Aspect_Specifications (Specification (N)));
3955 end if;
3956 end;
3957 end if;
3958 end Analyze_Generic_Package_Declaration;
3960 --------------------------------------------
3961 -- Analyze_Generic_Subprogram_Declaration --
3962 --------------------------------------------
3964 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3965 Formals : List_Id;
3966 Id : Entity_Id;
3967 New_N : Node_Id;
3968 Result_Type : Entity_Id;
3969 Save_Parent : Node_Id;
3970 Spec : Node_Id;
3971 Typ : Entity_Id;
3973 begin
3974 -- A generic may grant access to its private enclosing context depending
3975 -- on the placement of its corresponding body. From elaboration point of
3976 -- view, the flow of execution may enter this private context, and then
3977 -- reach an external unit, thus producing a dependency on that external
3978 -- unit. For such a path to be properly discovered and encoded in the
3979 -- ALI file of the main unit, let the ABE mechanism process the body of
3980 -- the main unit, and encode all relevant invocation constructs and the
3981 -- relations between them.
3983 Mark_Save_Invocation_Graph_Of_Body;
3985 -- Create copy of generic unit, and save for instantiation. If the unit
3986 -- is a child unit, do not copy the specifications for the parent, which
3987 -- are not part of the generic tree.
3989 Save_Parent := Parent_Spec (N);
3990 Set_Parent_Spec (N, Empty);
3992 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3993 Set_Parent_Spec (New_N, Save_Parent);
3994 Rewrite (N, New_N);
3996 -- Once the contents of the generic copy and the template are swapped,
3997 -- do the same for their respective aspect specifications.
3999 Exchange_Aspects (N, New_N);
4001 -- Collect all contract-related source pragmas found within the template
4002 -- and attach them to the contract of the subprogram spec. This contract
4003 -- is used in the capture of global references within annotations.
4005 Create_Generic_Contract (N);
4007 Spec := Specification (N);
4008 Id := Defining_Entity (Spec);
4009 Generate_Definition (Id);
4011 if Nkind (Id) = N_Defining_Operator_Symbol then
4012 Error_Msg_N
4013 ("operator symbol not allowed for generic subprogram", Id);
4014 end if;
4016 Start_Generic;
4018 Enter_Name (Id);
4019 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
4021 Push_Scope (Id);
4022 Enter_Generic_Scope (Id);
4023 Set_Inner_Instances (Id, New_Elmt_List);
4024 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4026 Analyze_Generic_Formal_Part (N);
4028 if Nkind (Spec) = N_Function_Specification then
4029 Mutate_Ekind (Id, E_Generic_Function);
4030 else
4031 Mutate_Ekind (Id, E_Generic_Procedure);
4032 end if;
4034 -- Set SPARK_Mode from context
4036 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4037 Set_SPARK_Pragma_Inherited (Id);
4039 -- Preserve relevant elaboration-related attributes of the context which
4040 -- are no longer available or very expensive to recompute once analysis,
4041 -- resolution, and expansion are over.
4043 Mark_Elaboration_Attributes
4044 (N_Id => Id,
4045 Checks => True,
4046 Warnings => True);
4048 Formals := Parameter_Specifications (Spec);
4050 if Present (Formals) then
4051 Process_Formals (Formals, Spec);
4052 end if;
4054 if Nkind (Spec) = N_Function_Specification then
4055 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
4056 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
4057 Set_Etype (Id, Result_Type);
4059 -- Check restriction imposed by AI05-073: a generic function
4060 -- cannot return an abstract type or an access to such.
4062 if Is_Abstract_Type (Designated_Type (Result_Type)) then
4063 Error_Msg_N
4064 ("generic function cannot have an access result "
4065 & "that designates an abstract type", Spec);
4066 end if;
4068 else
4069 Find_Type (Result_Definition (Spec));
4070 Typ := Entity (Result_Definition (Spec));
4072 if Is_Abstract_Type (Typ)
4073 and then Ada_Version >= Ada_2012
4074 then
4075 Error_Msg_N
4076 ("generic function cannot have abstract result type", Spec);
4077 end if;
4079 -- If a null exclusion is imposed on the result type, then create
4080 -- a null-excluding itype (an access subtype) and use it as the
4081 -- function's Etype.
4083 if Is_Access_Type (Typ)
4084 and then Null_Exclusion_Present (Spec)
4085 then
4086 Set_Etype (Id,
4087 Create_Null_Excluding_Itype
4088 (T => Typ,
4089 Related_Nod => Spec,
4090 Scope_Id => Defining_Unit_Name (Spec)));
4091 else
4092 Set_Etype (Id, Typ);
4093 end if;
4094 end if;
4096 else
4097 Set_Etype (Id, Standard_Void_Type);
4098 end if;
4100 Set_Is_Not_Self_Hidden (Id);
4102 -- Analyze the aspects of the generic copy to ensure that all generated
4103 -- pragmas (if any) perform their semantic effects.
4105 if Has_Aspects (N) then
4106 Analyze_Aspect_Specifications (N, Id);
4107 end if;
4109 -- For a library unit, we have reconstructed the entity for the unit,
4110 -- and must reset it in the library tables. We also make sure that
4111 -- Body_Required is set properly in the original compilation unit node.
4113 if Nkind (Parent (N)) = N_Compilation_Unit then
4114 Set_Cunit_Entity (Current_Sem_Unit, Id);
4115 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4116 end if;
4118 -- If the generic appears within a package unit, the body of that unit
4119 -- has to be present for instantiation and inlining.
4121 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
4122 and then Unit_Requires_Body (Id)
4123 then
4124 Set_Body_Needed_For_Inlining
4125 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4126 end if;
4128 Set_Categorization_From_Pragmas (N);
4129 Validate_Categorization_Dependency (N, Id);
4131 -- Capture all global references that occur within the profile of the
4132 -- generic subprogram. Aspects are not part of this processing because
4133 -- they must be delayed. If processed now, Save_Global_References will
4134 -- destroy the Associated_Node links and prevent the capture of global
4135 -- references when the contract of the generic subprogram is analyzed.
4137 Save_Global_References (Original_Node (N));
4139 End_Generic;
4140 End_Scope;
4141 Exit_Generic_Scope (Id);
4142 Generate_Reference_To_Formals (Id);
4144 List_Inherited_Pre_Post_Aspects (Id);
4145 end Analyze_Generic_Subprogram_Declaration;
4147 -----------------------------------
4148 -- Analyze_Package_Instantiation --
4149 -----------------------------------
4151 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4152 -- must be replaced by gotos which jump to the end of the routine in order
4153 -- to restore the Ghost and SPARK modes.
4155 procedure Analyze_Package_Instantiation (N : Node_Id) is
4156 Has_Inline_Always : Boolean := False;
4157 -- Set if the generic unit contains any subprograms with Inline_Always.
4158 -- Only relevant when back-end inlining is not enabled.
4160 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4161 -- Return True if inlining is active and Gen_Unit contains inlined
4162 -- subprograms. In this case, we may either instantiate the body when
4163 -- front-end inlining is enabled, or add a pending instantiation when
4164 -- back-end inlining is enabled. In the former case, this may cause
4165 -- superfluous instantiations, but in either case we need to perform
4166 -- the instantiation of the body in the context of the instance and
4167 -- not in that of the point of inlining.
4169 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4170 -- Return True if Gen_Unit needs to have its body instantiated in the
4171 -- context of N. This in particular excludes generic contexts.
4173 -----------------------
4174 -- Might_Inline_Subp --
4175 -----------------------
4177 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4178 E : Entity_Id;
4180 begin
4181 if Inline_Processing_Required then
4182 -- No need to recompute the answer if we know it is positive
4183 -- and back-end inlining is enabled.
4185 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4186 return True;
4187 end if;
4189 E := First_Entity (Gen_Unit);
4190 while Present (E) loop
4191 if Is_Subprogram (E) and then Is_Inlined (E) then
4192 -- Remember if there are any subprograms with Inline_Always
4194 if Has_Pragma_Inline_Always (E) then
4195 Has_Inline_Always := True;
4196 end if;
4198 Set_Is_Inlined (Gen_Unit);
4199 return True;
4200 end if;
4202 Next_Entity (E);
4203 end loop;
4204 end if;
4206 return False;
4207 end Might_Inline_Subp;
4209 -------------------------------
4210 -- Needs_Body_Instantiated --
4211 -------------------------------
4213 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4214 begin
4215 -- No need to instantiate bodies in generic units
4217 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4218 return False;
4219 end if;
4221 -- If the instantiation is in the main unit, then the body is needed
4223 if Is_In_Main_Unit (N) then
4224 return True;
4225 end if;
4227 -- In GNATprove mode, never instantiate bodies outside of the main
4228 -- unit, as it does not use frontend/backend inlining in the way that
4229 -- GNAT does, so does not benefit from such instantiations. On the
4230 -- contrary, such instantiations may bring artificial constraints,
4231 -- as for example such bodies may require preprocessing.
4233 if GNATprove_Mode then
4234 return False;
4235 end if;
4237 -- If not, then again no need to instantiate bodies in generic units
4239 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4240 return False;
4241 end if;
4243 -- Here we have a special handling for back-end inlining: if inline
4244 -- processing is required, then we unconditionally want to have the
4245 -- body instantiated. The reason is that Might_Inline_Subp does not
4246 -- catch all the cases (as it does not recurse into nested packages)
4247 -- so this avoids the need to patch things up afterwards. Moreover,
4248 -- these instantiations are only performed on demand when back-end
4249 -- inlining is enabled, so this causes very little extra work.
4251 if Inline_Processing_Required and then Back_End_Inlining then
4252 return True;
4253 end if;
4255 -- We want to have the bodies instantiated in non-main units if
4256 -- they might contribute inlined subprograms.
4258 return Might_Inline_Subp (Gen_Unit);
4259 end Needs_Body_Instantiated;
4261 -- Local declarations
4263 Gen_Id : constant Node_Id := Name (N);
4264 Inst_Id : constant Entity_Id := Defining_Entity (N);
4265 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4266 Loc : constant Source_Ptr := Sloc (N);
4268 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4269 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4270 Saved_ISMP : constant Boolean :=
4271 Ignore_SPARK_Mode_Pragmas_In_Instance;
4272 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4273 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4274 -- Save the Ghost and SPARK mode-related data to restore on exit
4276 Saved_Style_Check : constant Boolean := Style_Check;
4277 -- Save style check mode for restore on exit
4279 Act_Decl : Node_Id;
4280 Act_Decl_Name : Node_Id;
4281 Act_Decl_Id : Entity_Id;
4282 Act_Spec : Node_Id;
4283 Act_Tree : Node_Id;
4284 Env_Installed : Boolean := False;
4285 Gen_Decl : Node_Id;
4286 Gen_Spec : Node_Id;
4287 Gen_Unit : Entity_Id;
4288 Inline_Now : Boolean := False;
4289 Needs_Body : Boolean;
4290 Parent_Installed : Boolean := False;
4291 Renaming_List : List_Id;
4292 Unit_Renaming : Node_Id;
4294 Vis_Prims_List : Elist_Id := No_Elist;
4295 -- List of primitives made temporarily visible in the instantiation
4296 -- to match the visibility of the formal type
4298 -- Start of processing for Analyze_Package_Instantiation
4300 begin
4301 -- Preserve relevant elaboration-related attributes of the context which
4302 -- are no longer available or very expensive to recompute once analysis,
4303 -- resolution, and expansion are over.
4305 Mark_Elaboration_Attributes
4306 (N_Id => N,
4307 Checks => True,
4308 Level => True,
4309 Modes => True,
4310 Warnings => True);
4312 -- Very first thing: check for Text_IO special unit in case we are
4313 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4315 Check_Text_IO_Special_Unit (Name (N));
4317 -- Make node global for error reporting
4319 Instantiation_Node := N;
4321 -- Case of instantiation of a generic package
4323 if Nkind (N) = N_Package_Instantiation then
4324 Act_Decl_Id := New_Copy (Defining_Entity (N));
4326 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4327 Act_Decl_Name :=
4328 Make_Defining_Program_Unit_Name (Loc,
4329 Name =>
4330 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4331 Defining_Identifier => Act_Decl_Id);
4332 else
4333 Act_Decl_Name := Act_Decl_Id;
4334 end if;
4336 -- Case of instantiation of a formal package
4338 else
4339 Act_Decl_Id := Defining_Identifier (N);
4340 Act_Decl_Name := Act_Decl_Id;
4341 end if;
4343 Generate_Definition (Act_Decl_Id);
4344 Mutate_Ekind (Act_Decl_Id, E_Package);
4345 Set_Is_Not_Self_Hidden (Act_Decl_Id);
4347 -- Initialize list of incomplete actuals before analysis
4349 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4351 Preanalyze_Actuals (N, Act_Decl_Id);
4353 -- Turn off style checking in instances. If the check is enabled on the
4354 -- generic unit, a warning in an instance would just be noise. If not
4355 -- enabled on the generic, then a warning in an instance is just wrong.
4356 -- This must be done after analyzing the actuals, which do come from
4357 -- source and are subject to style checking.
4359 Style_Check := False;
4361 Init_Env;
4362 Env_Installed := True;
4364 -- Reset renaming map for formal types. The mapping is established
4365 -- when analyzing the generic associations, but some mappings are
4366 -- inherited from formal packages of parent units, and these are
4367 -- constructed when the parents are installed.
4369 Generic_Renamings.Set_Last (0);
4370 Generic_Renamings_HTable.Reset;
4372 -- Except for an abbreviated instance created to check a formal package,
4373 -- install the parent if this is a generic child unit.
4375 if not Is_Abbreviated_Instance (Inst_Id) then
4376 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4377 end if;
4379 Gen_Unit := Entity (Gen_Id);
4381 -- A package instantiation is Ghost when it is subject to pragma Ghost
4382 -- or the generic template is Ghost. Set the mode now to ensure that
4383 -- any nodes generated during analysis and expansion are marked as
4384 -- Ghost.
4386 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4388 -- Verify that it is the name of a generic package
4390 -- A visibility glitch: if the instance is a child unit and the generic
4391 -- is the generic unit of a parent instance (i.e. both the parent and
4392 -- the child units are instances of the same package) the name now
4393 -- denotes the renaming within the parent, not the intended generic
4394 -- unit. See if there is a homonym that is the desired generic. The
4395 -- renaming declaration must be visible inside the instance of the
4396 -- child, but not when analyzing the name in the instantiation itself.
4398 if Ekind (Gen_Unit) = E_Package
4399 and then Present (Renamed_Entity (Gen_Unit))
4400 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4401 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4402 and then Present (Homonym (Gen_Unit))
4403 then
4404 Gen_Unit := Homonym (Gen_Unit);
4405 end if;
4407 if Etype (Gen_Unit) = Any_Type then
4408 Restore_Env;
4409 goto Leave;
4411 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4413 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4415 if From_Limited_With (Gen_Unit) then
4416 Error_Msg_N
4417 ("cannot instantiate a limited withed package", Gen_Id);
4418 else
4419 Error_Msg_NE
4420 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4421 end if;
4423 Restore_Env;
4424 goto Leave;
4425 end if;
4427 if In_Extended_Main_Source_Unit (N) then
4428 Set_Is_Instantiated (Gen_Unit);
4429 Generate_Reference (Gen_Unit, N);
4431 if Present (Renamed_Entity (Gen_Unit)) then
4432 Set_Is_Instantiated (Renamed_Entity (Gen_Unit));
4433 Generate_Reference (Renamed_Entity (Gen_Unit), N);
4434 end if;
4435 end if;
4437 if Nkind (Gen_Id) = N_Identifier
4438 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4439 then
4440 Error_Msg_NE
4441 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4443 elsif Nkind (Gen_Id) = N_Expanded_Name
4444 and then Is_Child_Unit (Gen_Unit)
4445 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4446 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4447 then
4448 Error_Msg_N
4449 ("& is hidden within declaration of instance", Prefix (Gen_Id));
4450 end if;
4452 Set_Entity (Gen_Id, Gen_Unit);
4454 -- If generic is a renaming, get original generic unit
4456 if Present (Renamed_Entity (Gen_Unit))
4457 and then Ekind (Renamed_Entity (Gen_Unit)) = E_Generic_Package
4458 then
4459 Gen_Unit := Renamed_Entity (Gen_Unit);
4460 end if;
4462 -- Verify that there are no circular instantiations
4464 if In_Open_Scopes (Gen_Unit) then
4465 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4466 Restore_Env;
4467 goto Leave;
4469 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4470 Error_Msg_Node_2 := Current_Scope;
4471 Error_Msg_NE
4472 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
4473 Circularity_Detected := True;
4474 Restore_Env;
4475 goto Leave;
4477 else
4478 Mutate_Ekind (Inst_Id, E_Package);
4479 Set_Scope (Inst_Id, Current_Scope);
4481 -- If the context of the instance is subject to SPARK_Mode "off" or
4482 -- the annotation is altogether missing, set the global flag which
4483 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4484 -- the instance.
4486 if SPARK_Mode /= On then
4487 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4489 -- Mark the instance spec in case the body is instantiated at a
4490 -- later pass. This preserves the original context in effect for
4491 -- the body.
4493 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4494 end if;
4496 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4497 Gen_Spec := Specification (Gen_Decl);
4499 -- Initialize renamings map, for error checking, and the list that
4500 -- holds private entities whose views have changed between generic
4501 -- definition and instantiation. If this is the instance created to
4502 -- validate an actual package, the instantiation environment is that
4503 -- of the enclosing instance.
4505 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4507 -- Copy original generic tree, to produce text for instantiation
4509 Act_Tree :=
4510 Copy_Generic_Node
4511 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4513 Act_Spec := Specification (Act_Tree);
4515 -- If this is the instance created to validate an actual package,
4516 -- only the formals matter, do not examine the package spec itself.
4518 if Is_Actual_Pack then
4519 Set_Visible_Declarations (Act_Spec, New_List);
4520 Set_Private_Declarations (Act_Spec, New_List);
4521 end if;
4523 Renaming_List :=
4524 Analyze_Associations
4525 (I_Node => N,
4526 Formals => Generic_Formal_Declarations (Act_Tree),
4527 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4529 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4531 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4532 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4533 Set_Is_Generic_Instance (Act_Decl_Id);
4534 Set_Generic_Parent (Act_Spec, Gen_Unit);
4536 -- References to the generic in its own declaration or its body are
4537 -- references to the instance. Add a renaming declaration for the
4538 -- generic unit itself. This declaration, as well as the renaming
4539 -- declarations for the generic formals, must remain private to the
4540 -- unit: the formals, because this is the language semantics, and
4541 -- the unit because its use is an artifact of the implementation.
4543 Unit_Renaming :=
4544 Make_Package_Renaming_Declaration (Loc,
4545 Defining_Unit_Name =>
4546 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4547 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4549 Append (Unit_Renaming, Renaming_List);
4551 -- The renaming declarations are the first local declarations of the
4552 -- new unit.
4554 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4555 Insert_List_Before
4556 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4557 else
4558 Set_Visible_Declarations (Act_Spec, Renaming_List);
4559 end if;
4561 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4563 -- Propagate the aspect specifications from the package declaration
4564 -- template to the instantiated version of the package declaration.
4566 if Has_Aspects (Act_Tree) then
4567 Set_Aspect_Specifications (Act_Decl,
4568 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4569 end if;
4571 -- The generic may have a generated Default_Storage_Pool aspect,
4572 -- set at the point of generic declaration. If the instance has
4573 -- that aspect, it overrides the one inherited from the generic.
4575 if Has_Aspects (Gen_Spec) then
4576 if No (Aspect_Specifications (N)) then
4577 Set_Aspect_Specifications (N,
4578 (New_Copy_List_Tree
4579 (Aspect_Specifications (Gen_Spec))));
4581 else
4582 declare
4583 Inherited_Aspects : constant List_Id :=
4584 New_Copy_List_Tree
4585 (Aspect_Specifications (Gen_Spec));
4587 ASN1 : Node_Id;
4588 ASN2 : Node_Id;
4589 Pool_Present : Boolean := False;
4591 begin
4592 ASN1 := First (Aspect_Specifications (N));
4593 while Present (ASN1) loop
4594 if Chars (Identifier (ASN1)) =
4595 Name_Default_Storage_Pool
4596 then
4597 Pool_Present := True;
4598 exit;
4599 end if;
4601 Next (ASN1);
4602 end loop;
4604 if Pool_Present then
4606 -- If generic carries a default storage pool, remove it
4607 -- in favor of the instance one.
4609 ASN2 := First (Inherited_Aspects);
4610 while Present (ASN2) loop
4611 if Chars (Identifier (ASN2)) =
4612 Name_Default_Storage_Pool
4613 then
4614 Remove (ASN2);
4615 exit;
4616 end if;
4618 Next (ASN2);
4619 end loop;
4620 end if;
4622 Prepend_List_To
4623 (Aspect_Specifications (N), Inherited_Aspects);
4624 end;
4625 end if;
4626 end if;
4628 -- Save the instantiation node for a subsequent instantiation of the
4629 -- body if there is one and it needs to be instantiated here.
4631 -- We instantiate the body only if we are generating code, or if we
4632 -- are generating cross-reference information, or for GNATprove use.
4634 declare
4635 Enclosing_Body_Present : Boolean := False;
4636 -- If the generic unit is not a compilation unit, then a body may
4637 -- be present in its parent even if none is required. We create a
4638 -- tentative pending instantiation for the body, which will be
4639 -- discarded if none is actually present.
4641 Scop : Entity_Id;
4643 begin
4644 if Scope (Gen_Unit) /= Standard_Standard
4645 and then not Is_Child_Unit (Gen_Unit)
4646 then
4647 Scop := Scope (Gen_Unit);
4648 while Present (Scop) and then Scop /= Standard_Standard loop
4649 if Unit_Requires_Body (Scop) then
4650 Enclosing_Body_Present := True;
4651 exit;
4653 elsif In_Open_Scopes (Scop)
4654 and then In_Package_Body (Scop)
4655 then
4656 Enclosing_Body_Present := True;
4657 exit;
4658 end if;
4660 exit when Is_Compilation_Unit (Scop);
4661 Scop := Scope (Scop);
4662 end loop;
4663 end if;
4665 -- If front-end inlining is enabled or there are any subprograms
4666 -- marked with Inline_Always, and this is a unit for which code
4667 -- will be generated, we instantiate the body at once.
4669 -- This is done if the instance is not the main unit, and if the
4670 -- generic is not a child unit of another generic, to avoid scope
4671 -- problems and the reinstallation of parent instances.
4673 if Expander_Active
4674 and then (not Is_Child_Unit (Gen_Unit)
4675 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4676 and then Might_Inline_Subp (Gen_Unit)
4677 and then not Is_Actual_Pack
4678 then
4679 if not Back_End_Inlining
4680 and then (Front_End_Inlining or else Has_Inline_Always)
4681 and then (Is_In_Main_Unit (N)
4682 or else In_Main_Context (Current_Scope))
4683 and then Nkind (Parent (N)) /= N_Compilation_Unit
4684 then
4685 Inline_Now := True;
4687 -- In configurable_run_time mode we force the inlining of
4688 -- predefined subprograms marked Inline_Always, to minimize
4689 -- the use of the run-time library.
4691 elsif In_Predefined_Unit (Gen_Decl)
4692 and then Configurable_Run_Time_Mode
4693 and then Nkind (Parent (N)) /= N_Compilation_Unit
4694 then
4695 Inline_Now := True;
4696 end if;
4698 -- If the current scope is itself an instance within a child
4699 -- unit, there will be duplications in the scope stack, and the
4700 -- unstacking mechanism in Inline_Instance_Body will fail.
4701 -- This loses some rare cases of optimization.
4703 if Is_Generic_Instance (Current_Scope) then
4704 declare
4705 Curr_Unit : constant Entity_Id :=
4706 Cunit_Entity (Current_Sem_Unit);
4707 begin
4708 if Curr_Unit /= Current_Scope
4709 and then Is_Child_Unit (Curr_Unit)
4710 then
4711 Inline_Now := False;
4712 end if;
4713 end;
4714 end if;
4715 end if;
4717 Needs_Body :=
4718 (Unit_Requires_Body (Gen_Unit)
4719 or else Enclosing_Body_Present
4720 or else Present (Corresponding_Body (Gen_Decl)))
4721 and then Needs_Body_Instantiated (Gen_Unit)
4722 and then not Is_Actual_Pack
4723 and then not Inline_Now
4724 and then (Operating_Mode = Generate_Code
4725 or else (Operating_Mode = Check_Semantics
4726 and then GNATprove_Mode));
4728 -- If front-end inlining is enabled or there are any subprograms
4729 -- marked with Inline_Always, do not instantiate body when within
4730 -- a generic context.
4732 if not Back_End_Inlining
4733 and then (Front_End_Inlining or else Has_Inline_Always)
4734 and then not Expander_Active
4735 then
4736 Needs_Body := False;
4737 end if;
4739 -- If the current context is generic, and the package being
4740 -- instantiated is declared within a formal package, there is no
4741 -- body to instantiate until the enclosing generic is instantiated
4742 -- and there is an actual for the formal package. If the formal
4743 -- package has parameters, we build a regular package instance for
4744 -- it, that precedes the original formal package declaration.
4746 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4747 declare
4748 Decl : constant Node_Id :=
4749 Original_Node
4750 (Unit_Declaration_Node (Scope (Gen_Unit)));
4751 begin
4752 if Nkind (Decl) = N_Formal_Package_Declaration
4753 or else (Nkind (Decl) = N_Package_Declaration
4754 and then Is_List_Member (Decl)
4755 and then Present (Next (Decl))
4756 and then
4757 Nkind (Next (Decl)) =
4758 N_Formal_Package_Declaration)
4759 then
4760 Needs_Body := False;
4761 end if;
4762 end;
4763 end if;
4764 end;
4766 -- For RCI unit calling stubs, we omit the instance body if the
4767 -- instance is the RCI library unit itself.
4769 -- However there is a special case for nested instances: in this case
4770 -- we do generate the instance body, as it might be required, e.g.
4771 -- because it provides stream attributes for some type used in the
4772 -- profile of a remote subprogram. This is consistent with 12.3(12),
4773 -- which indicates that the instance body occurs at the place of the
4774 -- instantiation, and thus is part of the RCI declaration, which is
4775 -- present on all client partitions (this is E.2.3(18)).
4777 -- Note that AI12-0002 may make it illegal at some point to have
4778 -- stream attributes defined in an RCI unit, in which case this
4779 -- special case will become unnecessary. In the meantime, there
4780 -- is known application code in production that depends on this
4781 -- being possible, so we definitely cannot eliminate the body in
4782 -- the case of nested instances for the time being.
4784 -- When we generate a nested instance body, calling stubs for any
4785 -- relevant subprogram will be inserted immediately after the
4786 -- subprogram declarations, and will take precedence over the
4787 -- subsequent (original) body. (The stub and original body will be
4788 -- complete homographs, but this is permitted in an instance).
4789 -- (Could we do better and remove the original body???)
4791 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4792 and then Comes_From_Source (N)
4793 and then Nkind (Parent (N)) = N_Compilation_Unit
4794 then
4795 Needs_Body := False;
4796 end if;
4798 -- If the context requires a full instantiation, set things up for
4799 -- subsequent construction of the body.
4801 if Needs_Body then
4802 declare
4803 Fin_Scop, S : Entity_Id;
4805 begin
4806 Check_Forward_Instantiation (Gen_Decl);
4808 Fin_Scop := Empty;
4810 -- For a package instantiation that is not a compilation unit,
4811 -- indicate that cleanup actions of the innermost enclosing
4812 -- scope for which they are generated should be delayed until
4813 -- after the package body is instantiated.
4815 if Nkind (N) = N_Package_Instantiation
4816 and then not Is_Compilation_Unit (Act_Decl_Id)
4817 then
4818 S := Current_Scope;
4820 while S /= Standard_Standard loop
4821 -- Cleanup actions are not generated within generic units
4822 -- or in the formal part of generic units.
4824 if Inside_A_Generic
4825 or else Is_Generic_Unit (S)
4826 or else Ekind (S) = E_Void
4827 then
4828 exit;
4830 -- For package scopes, cleanup actions are generated only
4831 -- for compilation units, for spec and body separately.
4833 elsif Ekind (S) = E_Package then
4834 if Is_Compilation_Unit (S) then
4835 if In_Package_Body (S) then
4836 Fin_Scop := Body_Entity (S);
4837 else
4838 Fin_Scop := S;
4839 end if;
4841 Set_Delay_Cleanups (Fin_Scop);
4842 exit;
4844 else
4845 S := Scope (S);
4846 end if;
4848 -- Cleanup actions are generated for all dynamic scopes
4850 else
4851 Fin_Scop := S;
4852 Set_Delay_Cleanups (Fin_Scop);
4853 exit;
4854 end if;
4855 end loop;
4856 end if;
4858 Add_Pending_Instantiation (N, Act_Decl, Fin_Scop);
4859 end;
4860 end if;
4862 Set_Categorization_From_Pragmas (Act_Decl);
4864 if Parent_Installed then
4865 Hide_Current_Scope;
4866 end if;
4868 Set_Instance_Spec (N, Act_Decl);
4870 -- If not a compilation unit, insert the package declaration before
4871 -- the original instantiation node.
4873 if Nkind (Parent (N)) /= N_Compilation_Unit then
4874 Mark_Rewrite_Insertion (Act_Decl);
4875 Insert_Before (N, Act_Decl);
4877 if Has_Aspects (N) then
4878 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4880 -- The pragma created for a Default_Storage_Pool aspect must
4881 -- appear ahead of the declarations in the instance spec.
4882 -- Analysis has placed it after the instance node, so remove
4883 -- it and reinsert it properly now.
4885 declare
4886 ASN : constant Node_Id := First (Aspect_Specifications (N));
4887 A_Name : constant Name_Id := Chars (Identifier (ASN));
4888 Decl : Node_Id;
4890 begin
4891 if A_Name = Name_Default_Storage_Pool then
4892 if No (Visible_Declarations (Act_Spec)) then
4893 Set_Visible_Declarations (Act_Spec, New_List);
4894 end if;
4896 Decl := Next (N);
4897 while Present (Decl) loop
4898 if Nkind (Decl) = N_Pragma then
4899 Remove (Decl);
4900 Prepend (Decl, Visible_Declarations (Act_Spec));
4901 exit;
4902 end if;
4904 Next (Decl);
4905 end loop;
4906 end if;
4907 end;
4908 end if;
4910 Analyze (Act_Decl);
4912 -- For an instantiation that is a compilation unit, place
4913 -- declaration on current node so context is complete for analysis
4914 -- (including nested instantiations). If this is the main unit,
4915 -- the declaration eventually replaces the instantiation node.
4916 -- If the instance body is created later, it replaces the
4917 -- instance node, and the declaration is attached to it
4918 -- (see Build_Instance_Compilation_Unit_Nodes).
4920 else
4921 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4923 -- The entity for the current unit is the newly created one,
4924 -- and all semantic information is attached to it.
4926 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4928 -- If this is the main unit, replace the main entity as well
4930 if Current_Sem_Unit = Main_Unit then
4931 Main_Unit_Entity := Act_Decl_Id;
4932 end if;
4933 end if;
4935 Set_Unit (Parent (N), Act_Decl);
4936 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4937 Set_Package_Instantiation (Act_Decl_Id, N);
4939 -- Process aspect specifications of the instance node, if any, to
4940 -- take into account categorization pragmas before analyzing the
4941 -- instance.
4943 if Has_Aspects (N) then
4944 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4945 end if;
4947 Analyze (Act_Decl);
4948 Set_Unit (Parent (N), N);
4949 Set_Body_Required (Parent (N), False);
4951 -- We never need elaboration checks on instantiations, since by
4952 -- definition, the body instantiation is elaborated at the same
4953 -- time as the spec instantiation.
4955 if Legacy_Elaboration_Checks then
4956 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4957 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4958 end if;
4959 end if;
4961 if Legacy_Elaboration_Checks then
4962 Check_Elab_Instantiation (N);
4963 end if;
4965 -- Save the scenario for later examination by the ABE Processing
4966 -- phase.
4968 Record_Elaboration_Scenario (N);
4970 -- The instantiation results in a guaranteed ABE
4972 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4973 -- Do not instantiate the corresponding body because gigi cannot
4974 -- handle certain types of premature instantiations.
4976 Remove_Dead_Instance (N);
4978 -- Create completing bodies for all subprogram declarations since
4979 -- their real bodies will not be instantiated.
4981 Provide_Completing_Bodies (Instance_Spec (N));
4982 end if;
4984 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4986 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4987 First_Private_Entity (Act_Decl_Id));
4989 -- If the instantiation needs a body, the unit will be turned into
4990 -- a package body and receive its own elaboration entity. Otherwise,
4991 -- the nature of the unit is now a package declaration.
4993 -- Note that the below rewriting means that Act_Decl, which has been
4994 -- analyzed and expanded, will be re-expanded as the rewritten N.
4996 if Nkind (Parent (N)) = N_Compilation_Unit
4997 and then not Needs_Body
4998 then
4999 Rewrite (N, Act_Decl);
5000 end if;
5002 if Present (Corresponding_Body (Gen_Decl))
5003 or else Unit_Requires_Body (Gen_Unit)
5004 then
5005 Set_Has_Completion (Act_Decl_Id);
5006 end if;
5008 Check_Formal_Packages (Act_Decl_Id);
5010 Restore_Hidden_Primitives (Vis_Prims_List);
5011 Restore_Private_Views (Act_Decl_Id);
5013 Inherit_Context (Gen_Decl, N);
5015 if Parent_Installed then
5016 Remove_Parent;
5017 end if;
5019 Restore_Env;
5020 Env_Installed := False;
5021 end if;
5023 Validate_Categorization_Dependency (N, Act_Decl_Id);
5025 -- There used to be a check here to prevent instantiations in local
5026 -- contexts if the No_Local_Allocators restriction was active. This
5027 -- check was removed by a binding interpretation in AI-95-00130/07,
5028 -- but we retain the code for documentation purposes.
5030 -- if Ekind (Act_Decl_Id) /= E_Void
5031 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
5032 -- then
5033 -- Check_Restriction (No_Local_Allocators, N);
5034 -- end if;
5036 if Inline_Now then
5037 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
5038 end if;
5040 -- Check that if N is an instantiation of System.Dim_Float_IO or
5041 -- System.Dim_Integer_IO, the formal type has a dimension system.
5043 if Nkind (N) = N_Package_Instantiation
5044 and then Is_Dim_IO_Package_Instantiation (N)
5045 then
5046 declare
5047 Assoc : constant Node_Id := First (Generic_Associations (N));
5048 begin
5049 if not Has_Dimension_System
5050 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
5051 then
5052 Error_Msg_N ("type with a dimension system expected", Assoc);
5053 end if;
5054 end;
5055 end if;
5057 <<Leave>>
5058 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
5059 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5060 end if;
5062 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5063 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5064 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5065 Style_Check := Saved_Style_Check;
5067 exception
5068 when Instantiation_Error =>
5069 if Parent_Installed then
5070 Remove_Parent;
5071 end if;
5073 if Env_Installed then
5074 Restore_Env;
5075 end if;
5077 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5078 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5079 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5080 Style_Check := Saved_Style_Check;
5081 end Analyze_Package_Instantiation;
5083 --------------------------
5084 -- Inline_Instance_Body --
5085 --------------------------
5087 -- WARNING: This routine manages SPARK regions. Return statements must be
5088 -- replaced by gotos which jump to the end of the routine and restore the
5089 -- SPARK mode.
5091 procedure Inline_Instance_Body
5092 (N : Node_Id;
5093 Gen_Unit : Entity_Id;
5094 Act_Decl : Node_Id)
5096 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
5098 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
5099 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5100 Gen_Comp : constant Entity_Id :=
5101 Cunit_Entity (Get_Source_Unit (Gen_Unit));
5103 Scope_Stack_Depth : constant Pos :=
5104 Scope_Stack.Last - Scope_Stack.First + 1;
5106 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
5107 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
5108 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
5110 Curr_Scope : Entity_Id := Empty;
5111 List : Elist_Id := No_Elist; -- init to avoid warning
5112 N_Instances : Nat := 0;
5113 Num_Inner : Nat := 0;
5114 Num_Scopes : Nat := 0;
5115 Removed : Boolean := False;
5116 S : Entity_Id;
5117 Vis : Boolean;
5119 begin
5120 -- Case of generic unit defined in another unit. We must remove the
5121 -- complete context of the current unit to install that of the generic.
5123 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
5125 -- Loop through enclosing scopes until we reach a generic instance,
5126 -- package body, or subprogram.
5128 S := Current_Scope;
5129 while Present (S) and then S /= Standard_Standard loop
5131 -- Save use clauses from enclosing scopes into Use_Clauses
5133 loop
5134 Num_Scopes := Num_Scopes + 1;
5136 Use_Clauses (Num_Scopes) :=
5137 (Scope_Stack.Table
5138 (Scope_Stack.Last - Num_Scopes + 1).First_Use_Clause);
5139 End_Use_Clauses (Use_Clauses (Num_Scopes));
5141 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5142 or else Scope_Stack.Table
5143 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5144 end loop;
5146 exit when Is_Generic_Instance (S)
5147 and then (In_Package_Body (S)
5148 or else Ekind (S) = E_Procedure
5149 or else Ekind (S) = E_Function);
5150 S := Scope (S);
5151 end loop;
5153 Vis := Is_Immediately_Visible (Gen_Comp);
5155 -- Find and save all enclosing instances
5157 S := Current_Scope;
5159 while Present (S)
5160 and then S /= Standard_Standard
5161 loop
5162 if Is_Generic_Instance (S) then
5163 N_Instances := N_Instances + 1;
5164 Instances (N_Instances) := S;
5166 exit when In_Package_Body (S);
5167 end if;
5169 S := Scope (S);
5170 end loop;
5172 -- Remove context of current compilation unit, unless we are within a
5173 -- nested package instantiation, in which case the context has been
5174 -- removed previously.
5176 -- If current scope is the body of a child unit, remove context of
5177 -- spec as well. If an enclosing scope is an instance body, the
5178 -- context has already been removed, but the entities in the body
5179 -- must be made invisible as well.
5181 S := Current_Scope;
5182 while Present (S) and then S /= Standard_Standard loop
5183 if Is_Generic_Instance (S)
5184 and then (In_Package_Body (S)
5185 or else Ekind (S) in E_Procedure | E_Function)
5186 then
5187 -- We still have to remove the entities of the enclosing
5188 -- instance from direct visibility.
5190 declare
5191 E : Entity_Id;
5192 begin
5193 E := First_Entity (S);
5194 while Present (E) loop
5195 Set_Is_Immediately_Visible (E, False);
5196 Next_Entity (E);
5197 end loop;
5198 end;
5200 exit;
5201 end if;
5203 if S = Curr_Unit
5204 or else (Ekind (Curr_Unit) = E_Package_Body
5205 and then S = Spec_Entity (Curr_Unit))
5206 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5207 and then S = Corresponding_Spec
5208 (Unit_Declaration_Node (Curr_Unit)))
5209 then
5210 Removed := True;
5212 -- Remove entities in current scopes from visibility, so that
5213 -- instance body is compiled in a clean environment.
5215 List := Save_Scope_Stack (Handle_Use => False);
5217 if Is_Child_Unit (S) then
5219 -- Remove child unit from stack, as well as inner scopes.
5220 -- Removing the context of a child unit removes parent units
5221 -- as well.
5223 while Current_Scope /= S loop
5224 Num_Inner := Num_Inner + 1;
5225 Inner_Scopes (Num_Inner) := Current_Scope;
5226 Pop_Scope;
5227 end loop;
5229 Pop_Scope;
5230 Remove_Context (Curr_Comp);
5231 Curr_Scope := S;
5233 else
5234 Remove_Context (Curr_Comp);
5235 end if;
5237 if Ekind (Curr_Unit) = E_Package_Body then
5238 Remove_Context (Library_Unit (Curr_Comp));
5239 end if;
5240 end if;
5242 S := Scope (S);
5243 end loop;
5245 pragma Assert (Num_Inner < Num_Scopes);
5247 Push_Scope (Standard_Standard);
5248 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5250 -- The inlined package body is analyzed with the configuration state
5251 -- of the context prior to the scope manipulations performed above.
5253 -- ??? shouldn't this also use the warning state of the context prior
5254 -- to the scope manipulations?
5256 Instantiate_Package_Body
5257 (Body_Info =>
5258 ((Inst_Node => N,
5259 Act_Decl => Act_Decl,
5260 Fin_Scop => Empty,
5261 Config_Switches => Config_Attrs,
5262 Current_Sem_Unit => Current_Sem_Unit,
5263 Expander_Status => Expander_Active,
5264 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5265 Scope_Suppress => Scope_Suppress,
5266 Warnings => Save_Warnings)),
5267 Inlined_Body => True);
5269 Pop_Scope;
5271 -- Restore context
5273 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5275 -- Reset Generic_Instance flag so that use clauses can be installed
5276 -- in the proper order. (See Use_One_Package for effect of enclosing
5277 -- instances on processing of use clauses).
5279 for J in 1 .. N_Instances loop
5280 Set_Is_Generic_Instance (Instances (J), False);
5281 end loop;
5283 if Removed then
5284 Install_Context (Curr_Comp, Chain => False);
5286 if Present (Curr_Scope)
5287 and then Is_Child_Unit (Curr_Scope)
5288 then
5289 Push_Scope (Curr_Scope);
5290 Set_Is_Immediately_Visible (Curr_Scope);
5292 -- Finally, restore inner scopes as well
5294 for J in reverse 1 .. Num_Inner loop
5295 Push_Scope (Inner_Scopes (J));
5296 end loop;
5297 end if;
5299 Restore_Scope_Stack (List, Handle_Use => False);
5301 if Present (Curr_Scope)
5302 and then
5303 (In_Private_Part (Curr_Scope)
5304 or else In_Package_Body (Curr_Scope))
5305 then
5306 -- Install private declaration of ancestor units, which are
5307 -- currently available. Restore_Scope_Stack and Install_Context
5308 -- only install the visible part of parents.
5310 declare
5311 Par : Entity_Id;
5312 begin
5313 Par := Scope (Curr_Scope);
5314 while Present (Par) and then Par /= Standard_Standard loop
5315 Install_Private_Declarations (Par);
5316 Par := Scope (Par);
5317 end loop;
5318 end;
5319 end if;
5320 end if;
5322 -- Restore use clauses. For a child unit, use clauses in the parents
5323 -- are restored when installing the context, so only those in inner
5324 -- scopes (and those local to the child unit itself) need to be
5325 -- installed explicitly.
5327 if Is_Child_Unit (Curr_Unit) and then Removed then
5328 for J in reverse 1 .. Num_Inner + 1 loop
5329 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5330 Use_Clauses (J);
5331 Install_Use_Clauses (Use_Clauses (J));
5332 end loop;
5334 else
5335 for J in reverse 1 .. Num_Scopes loop
5336 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5337 Use_Clauses (J);
5338 Install_Use_Clauses (Use_Clauses (J));
5339 end loop;
5340 end if;
5342 -- Restore status of instances. If one of them is a body, make its
5343 -- local entities visible again.
5345 declare
5346 E : Entity_Id;
5347 Inst : Entity_Id;
5349 begin
5350 for J in 1 .. N_Instances loop
5351 Inst := Instances (J);
5352 Set_Is_Generic_Instance (Inst, True);
5354 if In_Package_Body (Inst)
5355 or else Ekind (S) in E_Procedure | E_Function
5356 then
5357 E := First_Entity (Instances (J));
5358 while Present (E) loop
5359 Set_Is_Immediately_Visible (E);
5360 Next_Entity (E);
5361 end loop;
5362 end if;
5363 end loop;
5364 end;
5366 -- If generic unit is in current unit, current context is correct. Note
5367 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5368 -- enclosing scopes were removed.
5370 else
5371 Instantiate_Package_Body
5372 (Body_Info =>
5373 ((Inst_Node => N,
5374 Act_Decl => Act_Decl,
5375 Fin_Scop => Empty,
5376 Config_Switches => Save_Config_Switches,
5377 Current_Sem_Unit => Current_Sem_Unit,
5378 Expander_Status => Expander_Active,
5379 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5380 Scope_Suppress => Scope_Suppress,
5381 Warnings => Save_Warnings)),
5382 Inlined_Body => True);
5383 end if;
5384 end Inline_Instance_Body;
5386 -------------------------------------
5387 -- Analyze_Procedure_Instantiation --
5388 -------------------------------------
5390 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5391 begin
5392 Analyze_Subprogram_Instantiation (N, E_Procedure);
5393 end Analyze_Procedure_Instantiation;
5395 -----------------------------------
5396 -- Need_Subprogram_Instance_Body --
5397 -----------------------------------
5399 function Need_Subprogram_Instance_Body
5400 (N : Node_Id;
5401 Subp : Entity_Id) return Boolean
5403 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5404 -- Return True if E is an inlined subprogram, an inlined renaming or a
5405 -- subprogram nested in an inlined subprogram. The inlining machinery
5406 -- totally disregards nested subprograms since it considers that they
5407 -- will always be compiled if the parent is (see Inline.Is_Nested).
5409 ------------------------------------
5410 -- Is_Inlined_Or_Child_Of_Inlined --
5411 ------------------------------------
5413 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5414 Scop : Entity_Id;
5416 begin
5417 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5418 return True;
5419 end if;
5421 Scop := Scope (E);
5422 while Scop /= Standard_Standard loop
5423 if Is_Subprogram (Scop) and then Is_Inlined (Scop) then
5424 return True;
5425 end if;
5427 Scop := Scope (Scop);
5428 end loop;
5430 return False;
5431 end Is_Inlined_Or_Child_Of_Inlined;
5433 begin
5434 -- Must be in the main unit or inlined (or child of inlined)
5436 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5438 -- Must be generating code or analyzing code in GNATprove mode
5440 and then (Operating_Mode = Generate_Code
5441 or else (Operating_Mode = Check_Semantics
5442 and then GNATprove_Mode))
5444 -- The body is needed when generating code (full expansion) and in
5445 -- in GNATprove mode (special expansion) for formal verification of
5446 -- the body itself.
5448 and then (Expander_Active or GNATprove_Mode)
5450 -- No point in inlining if ABE is inevitable
5452 and then not Is_Known_Guaranteed_ABE (N)
5454 -- Or if subprogram is eliminated
5456 and then not Is_Eliminated (Subp)
5457 then
5458 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5459 return True;
5461 -- Here if not inlined, or we ignore the inlining
5463 else
5464 return False;
5465 end if;
5466 end Need_Subprogram_Instance_Body;
5468 --------------------------------------
5469 -- Analyze_Subprogram_Instantiation --
5470 --------------------------------------
5472 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5473 -- must be replaced by gotos which jump to the end of the routine in order
5474 -- to restore the Ghost and SPARK modes.
5476 procedure Analyze_Subprogram_Instantiation
5477 (N : Node_Id;
5478 K : Entity_Kind)
5480 Errs : constant Nat := Serious_Errors_Detected;
5481 Gen_Id : constant Node_Id := Name (N);
5482 Inst_Id : constant Entity_Id := Defining_Entity (N);
5483 Anon_Id : constant Entity_Id :=
5484 Make_Defining_Identifier (Sloc (Inst_Id),
5485 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5486 Loc : constant Source_Ptr := Sloc (N);
5488 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5489 Act_Decl : Node_Id;
5490 Act_Spec : Node_Id;
5491 Act_Tree : Node_Id;
5493 Env_Installed : Boolean := False;
5494 Gen_Unit : Entity_Id;
5495 Gen_Decl : Node_Id;
5496 Pack_Id : Entity_Id;
5497 Parent_Installed : Boolean := False;
5499 Renaming_List : List_Id;
5500 -- The list of declarations that link formals and actuals of the
5501 -- instance. These are subtype declarations for formal types, and
5502 -- renaming declarations for other formals. The subprogram declaration
5503 -- for the instance is then appended to the list, and the last item on
5504 -- the list is the renaming declaration for the instance.
5506 procedure Analyze_Instance_And_Renamings;
5507 -- The instance must be analyzed in a context that includes the mappings
5508 -- of generic parameters into actuals. We create a package declaration
5509 -- for this purpose, and a subprogram with an internal name within the
5510 -- package. The subprogram instance is simply an alias for the internal
5511 -- subprogram, declared in the current scope.
5513 procedure Build_Subprogram_Renaming;
5514 -- If the subprogram is recursive, there are occurrences of the name of
5515 -- the generic within the body, which must resolve to the current
5516 -- instance. We add a renaming declaration after the declaration, which
5517 -- is available in the instance body, as well as in the analysis of
5518 -- aspects that appear in the generic. This renaming declaration is
5519 -- inserted after the instance declaration which it renames.
5521 ------------------------------------
5522 -- Analyze_Instance_And_Renamings --
5523 ------------------------------------
5525 procedure Analyze_Instance_And_Renamings is
5526 Def_Ent : constant Entity_Id := Defining_Entity (N);
5527 Pack_Decl : Node_Id;
5529 begin
5530 if Nkind (Parent (N)) = N_Compilation_Unit then
5532 -- For the case of a compilation unit, the container package has
5533 -- the same name as the instantiation, to insure that the binder
5534 -- calls the elaboration procedure with the right name. Copy the
5535 -- entity of the instance, which may have compilation level flags
5536 -- (e.g. Is_Child_Unit) set.
5538 Pack_Id := New_Copy (Def_Ent);
5540 else
5541 -- Otherwise we use the name of the instantiation concatenated
5542 -- with its source position to ensure uniqueness if there are
5543 -- several instantiations with the same name.
5545 Pack_Id :=
5546 Make_Defining_Identifier (Loc,
5547 Chars => New_External_Name
5548 (Related_Id => Chars (Def_Ent),
5549 Suffix => "GP",
5550 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5551 end if;
5553 Pack_Decl :=
5554 Make_Package_Declaration (Loc,
5555 Specification => Make_Package_Specification (Loc,
5556 Defining_Unit_Name => Pack_Id,
5557 Visible_Declarations => Renaming_List,
5558 End_Label => Empty));
5560 Set_Instance_Spec (N, Pack_Decl);
5561 Set_Is_Generic_Instance (Pack_Id);
5562 Set_Debug_Info_Needed (Pack_Id);
5564 -- Case of not a compilation unit
5566 if Nkind (Parent (N)) /= N_Compilation_Unit then
5567 Mark_Rewrite_Insertion (Pack_Decl);
5568 Insert_Before (N, Pack_Decl);
5569 Set_Has_Completion (Pack_Id);
5571 -- Case of an instantiation that is a compilation unit
5573 -- Place declaration on current node so context is complete for
5574 -- analysis (including nested instantiations), and for use in a
5575 -- context_clause (see Analyze_With_Clause).
5577 else
5578 Set_Unit (Parent (N), Pack_Decl);
5579 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5580 end if;
5582 Analyze (Pack_Decl);
5583 Check_Formal_Packages (Pack_Id);
5585 -- Body of the enclosing package is supplied when instantiating the
5586 -- subprogram body, after semantic analysis is completed.
5588 if Nkind (Parent (N)) = N_Compilation_Unit then
5590 -- Remove package itself from visibility, so it does not
5591 -- conflict with subprogram.
5593 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5595 -- Set name and scope of internal subprogram so that the proper
5596 -- external name will be generated. The proper scope is the scope
5597 -- of the wrapper package. We need to generate debugging info for
5598 -- the internal subprogram, so set flag accordingly.
5600 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5601 Set_Scope (Anon_Id, Scope (Pack_Id));
5603 -- Mark wrapper package as referenced, to avoid spurious warnings
5604 -- if the instantiation appears in various with_ clauses of
5605 -- subunits of the main unit.
5607 Set_Referenced (Pack_Id);
5608 end if;
5610 Set_Is_Generic_Instance (Anon_Id);
5611 Set_Debug_Info_Needed (Anon_Id);
5612 Act_Decl_Id := New_Copy (Anon_Id);
5614 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5615 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5616 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5618 -- Subprogram instance comes from source only if generic does
5620 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5622 -- If the instance is a child unit, mark the Id accordingly. Mark
5623 -- the anonymous entity as well, which is the real subprogram and
5624 -- which is used when the instance appears in a context clause.
5625 -- Similarly, propagate the Is_Eliminated flag to handle properly
5626 -- nested eliminated subprograms.
5628 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5629 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5630 New_Overloaded_Entity (Act_Decl_Id);
5631 Check_Eliminated (Act_Decl_Id);
5632 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5634 if Nkind (Parent (N)) = N_Compilation_Unit then
5636 -- In compilation unit case, kill elaboration checks on the
5637 -- instantiation, since they are never needed - the body is
5638 -- instantiated at the same point as the spec.
5640 if Legacy_Elaboration_Checks then
5641 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5642 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5643 end if;
5645 Set_Is_Compilation_Unit (Anon_Id);
5646 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5647 end if;
5649 -- The instance is not a freezing point for the new subprogram.
5650 -- The anonymous subprogram may have a freeze node, created for
5651 -- some delayed aspects. This freeze node must not be inherited
5652 -- by the visible subprogram entity.
5654 Set_Is_Frozen (Act_Decl_Id, False);
5655 Set_Freeze_Node (Act_Decl_Id, Empty);
5657 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5658 Valid_Operator_Definition (Act_Decl_Id);
5659 end if;
5661 Set_Alias (Act_Decl_Id, Anon_Id);
5662 Set_Has_Completion (Act_Decl_Id);
5663 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5665 if Nkind (Parent (N)) = N_Compilation_Unit then
5666 Set_Body_Required (Parent (N), False);
5667 end if;
5668 end Analyze_Instance_And_Renamings;
5670 -------------------------------
5671 -- Build_Subprogram_Renaming --
5672 -------------------------------
5674 procedure Build_Subprogram_Renaming is
5675 Renaming_Decl : Node_Id;
5676 Unit_Renaming : Node_Id;
5678 begin
5679 Unit_Renaming :=
5680 Make_Subprogram_Renaming_Declaration (Loc,
5681 Specification =>
5682 Copy_Generic_Node
5683 (Specification (Original_Node (Gen_Decl)),
5684 Empty,
5685 Instantiating => True),
5686 Name => New_Occurrence_Of (Anon_Id, Loc));
5688 -- The generic may be a child unit. The renaming needs an identifier
5689 -- with the proper name.
5691 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5692 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5694 -- If there is a formal subprogram with the same name as the unit
5695 -- itself, do not add this renaming declaration, to prevent
5696 -- ambiguities when there is a call with that name in the body.
5698 Renaming_Decl := First (Renaming_List);
5699 while Present (Renaming_Decl) loop
5700 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5701 and then
5702 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5703 then
5704 exit;
5705 end if;
5707 Next (Renaming_Decl);
5708 end loop;
5710 if No (Renaming_Decl) then
5711 Append (Unit_Renaming, Renaming_List);
5712 end if;
5713 end Build_Subprogram_Renaming;
5715 -- Local variables
5717 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5718 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5719 Saved_ISMP : constant Boolean :=
5720 Ignore_SPARK_Mode_Pragmas_In_Instance;
5721 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5722 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5723 -- Save the Ghost and SPARK mode-related data to restore on exit
5725 Vis_Prims_List : Elist_Id := No_Elist;
5726 -- List of primitives made temporarily visible in the instantiation
5727 -- to match the visibility of the formal type
5729 -- Start of processing for Analyze_Subprogram_Instantiation
5731 begin
5732 -- Preserve relevant elaboration-related attributes of the context which
5733 -- are no longer available or very expensive to recompute once analysis,
5734 -- resolution, and expansion are over.
5736 Mark_Elaboration_Attributes
5737 (N_Id => N,
5738 Checks => True,
5739 Level => True,
5740 Modes => True,
5741 Warnings => True);
5743 -- Very first thing: check for special Text_IO unit in case we are
5744 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5745 -- such an instantiation is bogus (these are packages, not subprograms),
5746 -- but we get a better error message if we do this.
5748 Check_Text_IO_Special_Unit (Gen_Id);
5750 -- Make node global for error reporting
5752 Instantiation_Node := N;
5754 -- For package instantiations we turn off style checks, because they
5755 -- will have been emitted in the generic. For subprogram instantiations
5756 -- we want to apply at least the check on overriding indicators so we
5757 -- do not modify the style check status.
5759 -- The renaming declarations for the actuals do not come from source and
5760 -- will not generate spurious warnings.
5762 Preanalyze_Actuals (N);
5764 Init_Env;
5765 Env_Installed := True;
5766 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5767 Gen_Unit := Entity (Gen_Id);
5769 -- A subprogram instantiation is Ghost when it is subject to pragma
5770 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5771 -- that any nodes generated during analysis and expansion are marked as
5772 -- Ghost.
5774 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5776 Generate_Reference (Gen_Unit, Gen_Id);
5778 if Nkind (Gen_Id) = N_Identifier
5779 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5780 then
5781 Error_Msg_NE
5782 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5783 end if;
5785 if Etype (Gen_Unit) = Any_Type then
5786 Restore_Env;
5787 goto Leave;
5788 end if;
5790 -- Verify that it is a generic subprogram of the right kind, and that
5791 -- it does not lead to a circular instantiation.
5793 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5794 Error_Msg_NE
5795 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5797 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5798 Error_Msg_NE
5799 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5801 elsif In_Open_Scopes (Gen_Unit) then
5802 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5804 else
5805 Mutate_Ekind (Inst_Id, K);
5806 Set_Scope (Inst_Id, Current_Scope);
5808 Set_Entity (Gen_Id, Gen_Unit);
5810 if In_Extended_Main_Source_Unit (N) then
5811 Set_Is_Instantiated (Gen_Unit);
5812 Generate_Reference (Gen_Unit, N);
5813 end if;
5815 -- If renaming, get original unit
5817 if Present (Renamed_Entity (Gen_Unit))
5818 and then Is_Generic_Subprogram (Renamed_Entity (Gen_Unit))
5819 then
5820 Gen_Unit := Renamed_Entity (Gen_Unit);
5821 Set_Is_Instantiated (Gen_Unit);
5822 Generate_Reference (Gen_Unit, N);
5823 end if;
5825 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5826 Error_Msg_Node_2 := Current_Scope;
5827 Error_Msg_NE
5828 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5829 Circularity_Detected := True;
5830 Restore_Hidden_Primitives (Vis_Prims_List);
5831 goto Leave;
5832 end if;
5834 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5836 -- Initialize renamings map, for error checking
5838 Generic_Renamings.Set_Last (0);
5839 Generic_Renamings_HTable.Reset;
5841 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5843 -- Copy original generic tree, to produce text for instantiation
5845 Act_Tree :=
5846 Copy_Generic_Node
5847 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5849 -- Inherit overriding indicator from instance node
5851 Act_Spec := Specification (Act_Tree);
5852 Set_Must_Override (Act_Spec, Must_Override (N));
5853 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5855 Renaming_List :=
5856 Analyze_Associations
5857 (I_Node => N,
5858 Formals => Generic_Formal_Declarations (Act_Tree),
5859 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5861 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5863 -- The subprogram itself cannot contain a nested instance, so the
5864 -- current parent is left empty.
5866 Set_Instance_Env (Gen_Unit, Empty);
5868 -- Build the subprogram declaration, which does not appear in the
5869 -- generic template, and give it a sloc consistent with that of the
5870 -- template.
5872 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5873 Set_Generic_Parent (Act_Spec, Gen_Unit);
5874 Act_Decl :=
5875 Make_Subprogram_Declaration (Sloc (Act_Spec),
5876 Specification => Act_Spec);
5878 -- The aspects have been copied previously, but they have to be
5879 -- linked explicitly to the new subprogram declaration. Explicit
5880 -- pre/postconditions on the instance are analyzed below, in a
5881 -- separate step.
5883 Move_Aspects (Act_Tree, To => Act_Decl);
5884 Set_Categorization_From_Pragmas (Act_Decl);
5886 if Parent_Installed then
5887 Hide_Current_Scope;
5888 end if;
5890 Append (Act_Decl, Renaming_List);
5892 -- Contract-related source pragmas that follow a generic subprogram
5893 -- must be instantiated explicitly because they are not part of the
5894 -- subprogram template.
5896 Instantiate_Subprogram_Contract
5897 (Original_Node (Gen_Decl), Renaming_List);
5899 Build_Subprogram_Renaming;
5901 -- If the context of the instance is subject to SPARK_Mode "off" or
5902 -- the annotation is altogether missing, set the global flag which
5903 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5904 -- the instance. This should be done prior to analyzing the instance.
5906 if SPARK_Mode /= On then
5907 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5908 end if;
5910 -- If the context of an instance is not subject to SPARK_Mode "off",
5911 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5912 -- the latter should be the one applicable to the instance.
5914 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5915 and then Saved_SM /= Off
5916 and then Present (SPARK_Pragma (Gen_Unit))
5917 then
5918 Set_SPARK_Mode (Gen_Unit);
5919 end if;
5921 -- Need to mark Anon_Id intrinsic before calling
5922 -- Analyze_Instance_And_Renamings because this flag may be propagated
5923 -- to other nodes.
5925 if Is_Intrinsic_Subprogram (Gen_Unit) then
5926 Set_Is_Intrinsic_Subprogram (Anon_Id);
5927 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
5928 end if;
5930 Analyze_Instance_And_Renamings;
5932 -- Restore SPARK_Mode from the context after analysis of the package
5933 -- declaration, so that the SPARK_Mode on the generic spec does not
5934 -- apply to the pending instance for the instance body.
5936 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5937 and then Saved_SM /= Off
5938 and then Present (SPARK_Pragma (Gen_Unit))
5939 then
5940 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5941 end if;
5943 -- If the generic is marked Import (Intrinsic), then so is the
5944 -- instance; this indicates that there is no body to instantiate.
5945 -- We also copy the interface name in case this is handled by the
5946 -- back-end and deal with an instance of unchecked conversion.
5948 if Is_Intrinsic_Subprogram (Gen_Unit) then
5949 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5950 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
5952 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5953 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5954 end if;
5955 end if;
5957 -- Inherit convention from generic unit. Intrinsic convention, as for
5958 -- an instance of unchecked conversion, is not inherited because an
5959 -- explicit Ada instance has been created.
5961 if Has_Convention_Pragma (Gen_Unit)
5962 and then Convention (Gen_Unit) /= Convention_Intrinsic
5963 then
5964 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5965 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5966 end if;
5968 Generate_Definition (Act_Decl_Id);
5970 -- Inherit all inlining-related flags which apply to the generic in
5971 -- the subprogram and its declaration.
5973 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5974 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5976 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5977 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5979 Set_Has_Pragma_Inline_Always
5980 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5981 Set_Has_Pragma_Inline_Always
5982 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5984 Set_Has_Pragma_No_Inline
5985 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5986 Set_Has_Pragma_No_Inline
5987 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5989 -- Propagate No_Return if pragma applied to generic unit. This must
5990 -- be done explicitly because pragma does not appear in generic
5991 -- declaration (unlike the aspect case).
5993 if No_Return (Gen_Unit) then
5994 Set_No_Return (Act_Decl_Id);
5995 Set_No_Return (Anon_Id);
5996 end if;
5998 -- Mark both the instance spec and the anonymous package in case the
5999 -- body is instantiated at a later pass. This preserves the original
6000 -- context in effect for the body.
6002 if SPARK_Mode /= On then
6003 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
6004 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
6005 end if;
6007 if Legacy_Elaboration_Checks
6008 and then not Is_Intrinsic_Subprogram (Gen_Unit)
6009 then
6010 Check_Elab_Instantiation (N);
6011 end if;
6013 -- Save the scenario for later examination by the ABE Processing
6014 -- phase.
6016 Record_Elaboration_Scenario (N);
6018 -- The instantiation results in a guaranteed ABE. Create a completing
6019 -- body for the subprogram declaration because the real body will not
6020 -- be instantiated.
6022 if Is_Known_Guaranteed_ABE (N) then
6023 Provide_Completing_Bodies (Instance_Spec (N));
6024 end if;
6026 if Is_Dispatching_Operation (Act_Decl_Id)
6027 and then Ada_Version >= Ada_2005
6028 then
6029 declare
6030 Formal : Entity_Id;
6032 begin
6033 Formal := First_Formal (Act_Decl_Id);
6034 while Present (Formal) loop
6035 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
6036 and then Is_Controlling_Formal (Formal)
6037 and then not Can_Never_Be_Null (Formal)
6038 then
6039 Error_Msg_NE
6040 ("access parameter& is controlling,", N, Formal);
6041 Error_Msg_NE
6042 ("\corresponding parameter of & must be explicitly "
6043 & "null-excluding", N, Gen_Id);
6044 end if;
6046 Next_Formal (Formal);
6047 end loop;
6048 end;
6049 end if;
6051 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
6053 Validate_Categorization_Dependency (N, Act_Decl_Id);
6055 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
6056 Inherit_Context (Gen_Decl, N);
6058 Restore_Private_Views (Pack_Id, False);
6060 -- If the context requires a full instantiation, mark node for
6061 -- subsequent construction of the body.
6063 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
6064 Check_Forward_Instantiation (Gen_Decl);
6066 -- The wrapper package is always delayed, because it does not
6067 -- constitute a freeze point, but to insure that the freeze node
6068 -- is placed properly, it is created directly when instantiating
6069 -- the body (otherwise the freeze node might appear to early for
6070 -- nested instantiations).
6072 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6073 Rewrite (N, Unit (Parent (N)));
6074 Set_Unit (Parent (N), N);
6075 end if;
6077 -- Replace instance node for library-level instantiations of
6078 -- intrinsic subprograms.
6080 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6081 Rewrite (N, Unit (Parent (N)));
6082 Set_Unit (Parent (N), N);
6083 end if;
6085 if Parent_Installed then
6086 Remove_Parent;
6087 end if;
6089 Restore_Hidden_Primitives (Vis_Prims_List);
6090 Restore_Env;
6091 Env_Installed := False;
6092 Generic_Renamings.Set_Last (0);
6093 Generic_Renamings_HTable.Reset;
6094 end if;
6096 <<Leave>>
6097 -- Analyze aspects in declaration if no errors appear in the instance.
6099 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
6100 Analyze_Aspect_Specifications (N, Act_Decl_Id);
6101 end if;
6103 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6104 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6105 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6107 exception
6108 when Instantiation_Error =>
6109 if Parent_Installed then
6110 Remove_Parent;
6111 end if;
6113 if Env_Installed then
6114 Restore_Env;
6115 end if;
6117 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6118 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6119 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6120 end Analyze_Subprogram_Instantiation;
6122 -------------------------
6123 -- Get_Associated_Node --
6124 -------------------------
6126 function Get_Associated_Node (N : Node_Id) return Node_Id is
6127 Assoc : Node_Id;
6129 begin
6130 Assoc := Associated_Node (N);
6132 if Nkind (Assoc) /= Nkind (N) then
6133 return Assoc;
6135 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
6136 return Assoc;
6138 else
6139 -- If the node is part of an inner generic, it may itself have been
6140 -- remapped into a further generic copy. Associated_Node is otherwise
6141 -- used for the entity of the node, and will be of a different node
6142 -- kind, or else N has been rewritten as a literal or function call.
6144 while Present (Associated_Node (Assoc))
6145 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6146 loop
6147 Assoc := Associated_Node (Assoc);
6148 end loop;
6150 -- Follow an additional link in case the final node was rewritten.
6151 -- This can only happen with nested generic units.
6153 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6154 and then Present (Associated_Node (Assoc))
6155 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6156 | N_Explicit_Dereference
6157 | N_Integer_Literal
6158 | N_Real_Literal
6159 | N_String_Literal
6160 then
6161 Assoc := Associated_Node (Assoc);
6162 end if;
6164 -- An additional special case: an unconstrained type in an object
6165 -- declaration may have been rewritten as a local subtype constrained
6166 -- by the expression in the declaration. We need to recover the
6167 -- original entity, which may be global.
6169 if Present (Original_Node (Assoc))
6170 and then Nkind (Parent (N)) = N_Object_Declaration
6171 then
6172 Assoc := Original_Node (Assoc);
6173 end if;
6175 return Assoc;
6176 end if;
6177 end Get_Associated_Node;
6179 -----------------------------------
6180 -- Build_Subprogram_Decl_Wrapper --
6181 -----------------------------------
6183 function Build_Subprogram_Decl_Wrapper
6184 (Formal_Subp : Entity_Id) return Node_Id
6186 Loc : constant Source_Ptr := Sloc (Current_Scope);
6187 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6188 Decl : Node_Id;
6189 Subp : Entity_Id;
6190 Parm_Spec : Node_Id;
6191 Profile : List_Id := New_List;
6192 Spec : Node_Id;
6193 Form_F : Entity_Id;
6194 New_F : Entity_Id;
6196 begin
6198 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6199 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6200 Set_Is_Generic_Actual_Subprogram (Subp);
6202 Profile := Parameter_Specifications (
6203 New_Copy_Tree
6204 (Specification (Unit_Declaration_Node (Formal_Subp))));
6206 Form_F := First_Formal (Formal_Subp);
6207 Parm_Spec := First (Profile);
6209 -- Create new entities for the formals. Reset entities so that
6210 -- parameter types are properly resolved when wrapper declaration
6211 -- is analyzed.
6213 while Present (Parm_Spec) loop
6214 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6215 Set_Defining_Identifier (Parm_Spec, New_F);
6216 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6217 Next (Parm_Spec);
6218 Next_Formal (Form_F);
6219 end loop;
6221 if Ret_Type = Standard_Void_Type then
6222 Spec :=
6223 Make_Procedure_Specification (Loc,
6224 Defining_Unit_Name => Subp,
6225 Parameter_Specifications => Profile);
6226 else
6227 Spec :=
6228 Make_Function_Specification (Loc,
6229 Defining_Unit_Name => Subp,
6230 Parameter_Specifications => Profile,
6231 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6232 end if;
6234 Decl :=
6235 Make_Subprogram_Declaration (Loc, Specification => Spec);
6237 return Decl;
6238 end Build_Subprogram_Decl_Wrapper;
6240 -----------------------------------
6241 -- Build_Subprogram_Body_Wrapper --
6242 -----------------------------------
6244 function Build_Subprogram_Body_Wrapper
6245 (Formal_Subp : Entity_Id;
6246 Actual_Name : Node_Id) return Node_Id
6248 Loc : constant Source_Ptr := Sloc (Current_Scope);
6249 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6250 Spec_Node : constant Node_Id :=
6251 Specification
6252 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6253 Act : Node_Id;
6254 Actuals : List_Id;
6255 Body_Node : Node_Id;
6256 Stmt : Node_Id;
6257 begin
6258 Actuals := New_List;
6259 Act := First (Parameter_Specifications (Spec_Node));
6261 while Present (Act) loop
6262 Append_To (Actuals,
6263 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6264 Next (Act);
6265 end loop;
6267 if Ret_Type = Standard_Void_Type then
6268 Stmt := Make_Procedure_Call_Statement (Loc,
6269 Name => Actual_Name,
6270 Parameter_Associations => Actuals);
6272 else
6273 Stmt := Make_Simple_Return_Statement (Loc,
6274 Expression =>
6275 Make_Function_Call (Loc,
6276 Name => Actual_Name,
6277 Parameter_Associations => Actuals));
6278 end if;
6280 Body_Node := Make_Subprogram_Body (Loc,
6281 Specification => Spec_Node,
6282 Declarations => New_List,
6283 Handled_Statement_Sequence =>
6284 Make_Handled_Sequence_Of_Statements (Loc,
6285 Statements => New_List (Stmt)));
6287 return Body_Node;
6288 end Build_Subprogram_Body_Wrapper;
6290 -------------------------------------------
6291 -- Build_Instance_Compilation_Unit_Nodes --
6292 -------------------------------------------
6294 procedure Build_Instance_Compilation_Unit_Nodes
6295 (N : Node_Id;
6296 Act_Body : Node_Id;
6297 Act_Decl : Node_Id)
6299 Decl_Cunit : Node_Id;
6300 Body_Cunit : Node_Id;
6301 Citem : Node_Id;
6302 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6303 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6305 begin
6306 -- A new compilation unit node is built for the instance declaration.
6307 -- It relocates the auxiliary declaration node from the compilation unit
6308 -- where the instance appeared, so that declarations that originally
6309 -- followed the instance will be attached to the spec compilation unit.
6311 Decl_Cunit :=
6312 Make_Compilation_Unit (Sloc (N),
6313 Context_Items => Empty_List,
6314 Unit => Act_Decl,
6315 Aux_Decls_Node => Relocate_Node (Aux_Decls_Node (Parent (N))));
6317 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6319 -- The new compilation unit is linked to its body, but both share the
6320 -- same file, so we do not set Body_Required on the new unit so as not
6321 -- to create a spurious dependency on a non-existent body in the ali.
6322 -- This simplifies CodePeer unit traversal.
6324 -- We use the original instantiation compilation unit as the resulting
6325 -- compilation unit of the instance, since this is the main unit.
6327 Rewrite (N, Act_Body);
6329 -- Propagate the aspect specifications from the package body template to
6330 -- the instantiated version of the package body.
6332 if Has_Aspects (Act_Body) then
6333 Set_Aspect_Specifications
6334 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6335 end if;
6337 Body_Cunit := Parent (N);
6339 -- The two compilation unit nodes are linked by the Library_Unit field
6341 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6342 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6344 -- Preserve the private nature of the package if needed
6346 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6348 -- If the instance is not the main unit, its context, categorization
6349 -- and elaboration entity are not relevant to the compilation.
6351 if Body_Cunit /= Cunit (Main_Unit) then
6352 Make_Instance_Unit (Body_Cunit, In_Main => False);
6353 return;
6354 end if;
6356 -- The context clause items on the instantiation, which are now attached
6357 -- to the body compilation unit (since the body overwrote the original
6358 -- instantiation node), semantically belong on the spec, so copy them
6359 -- there. It's harmless to leave them on the body as well. In fact one
6360 -- could argue that they belong in both places.
6362 Citem := First (Context_Items (Body_Cunit));
6363 while Present (Citem) loop
6364 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6365 Next (Citem);
6366 end loop;
6368 -- Propagate categorization flags on packages, so that they appear in
6369 -- the ali file for the spec of the unit.
6371 if Ekind (New_Main) = E_Package then
6372 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6373 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6374 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6375 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6376 Set_Is_Remote_Call_Interface
6377 (Old_Main, Is_Remote_Call_Interface (New_Main));
6378 end if;
6380 -- Make entry in Units table, so that binder can generate call to
6381 -- elaboration procedure for body, if any.
6383 Make_Instance_Unit (Body_Cunit, In_Main => True);
6384 Main_Unit_Entity := New_Main;
6385 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6387 -- Build elaboration entity, since the instance may certainly generate
6388 -- elaboration code requiring a flag for protection.
6390 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6391 end Build_Instance_Compilation_Unit_Nodes;
6393 --------------------------------
6394 -- Check_Abbreviated_Instance --
6395 --------------------------------
6397 procedure Check_Abbreviated_Instance
6398 (N : Node_Id;
6399 Parent_Installed : in out Boolean)
6401 Inst_Node : Node_Id;
6403 begin
6404 if Nkind (N) = N_Package_Specification
6405 and then Is_Abbreviated_Instance (Defining_Entity (N))
6406 then
6407 Inst_Node := Get_Unit_Instantiation_Node (Defining_Entity (N));
6408 Check_Generic_Child_Unit (Name (Inst_Node), Parent_Installed);
6409 end if;
6410 end Check_Abbreviated_Instance;
6412 -----------------------------
6413 -- Check_Access_Definition --
6414 -----------------------------
6416 procedure Check_Access_Definition (N : Node_Id) is
6417 begin
6418 pragma Assert
6419 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6420 null;
6421 end Check_Access_Definition;
6423 -----------------------------------
6424 -- Check_Formal_Package_Instance --
6425 -----------------------------------
6427 -- If the formal has specific parameters, they must match those of the
6428 -- actual. Both of them are instances, and the renaming declarations for
6429 -- their formal parameters appear in the same order in both. The analyzed
6430 -- formal has been analyzed in the context of the current instance.
6432 procedure Check_Formal_Package_Instance
6433 (Formal_Pack : Entity_Id;
6434 Actual_Pack : Entity_Id)
6436 E1 : Entity_Id := First_Entity (Actual_Pack);
6437 E2 : Entity_Id := First_Entity (Formal_Pack);
6438 Prev_E1 : Entity_Id;
6440 Expr1 : Node_Id;
6441 Expr2 : Node_Id;
6443 procedure Check_Mismatch (B : Boolean);
6444 -- Common error routine for mismatch between the parameters of the
6445 -- actual instance and those of the formal package.
6447 function Is_Defaulted (Param : Entity_Id) return Boolean;
6448 -- If the formal package has partly box-initialized formals, skip
6449 -- conformance check for these formals. Previously the code assumed
6450 -- that box initialization for a formal package applied to all its
6451 -- formal parameters.
6453 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6454 -- The formal may come from a nested formal package, and the actual may
6455 -- have been constant-folded. To determine whether the two denote the
6456 -- same entity we may have to traverse several definitions to recover
6457 -- the ultimate entity that they refer to.
6459 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6460 -- The formal and the actual must be identical, but if both are
6461 -- given by attributes they end up renaming different generated bodies,
6462 -- and we must verify that the attributes themselves match.
6464 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6465 -- Similarly, if the formal comes from a nested formal package, the
6466 -- actual may designate the formal through multiple renamings, which
6467 -- have to be followed to determine the original variable in question.
6469 --------------------
6470 -- Check_Mismatch --
6471 --------------------
6473 procedure Check_Mismatch (B : Boolean) is
6474 -- A Formal_Type_Declaration for a derived private type is rewritten
6475 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6476 -- which is why we examine the original node.
6478 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6480 begin
6481 if Kind = N_Formal_Type_Declaration then
6482 return;
6484 elsif Kind in N_Formal_Object_Declaration
6485 | N_Formal_Package_Declaration
6486 | N_Formal_Subprogram_Declaration
6487 then
6488 null;
6490 -- Ada 2012: If both formal and actual are incomplete types they
6491 -- are conformant.
6493 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6494 null;
6496 elsif B then
6497 Error_Msg_NE
6498 ("actual for & in actual instance does not match formal",
6499 Parent (Actual_Pack), E1);
6500 end if;
6501 end Check_Mismatch;
6503 ------------------
6504 -- Is_Defaulted --
6505 ------------------
6507 function Is_Defaulted (Param : Entity_Id) return Boolean is
6508 Assoc : Node_Id;
6510 begin
6511 Assoc :=
6512 First (Generic_Associations (Parent
6513 (Associated_Formal_Package (Actual_Pack))));
6515 while Present (Assoc) loop
6516 if Nkind (Assoc) = N_Others_Choice then
6517 return True;
6519 elsif Nkind (Assoc) = N_Generic_Association
6520 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6521 then
6522 return Box_Present (Assoc);
6523 end if;
6525 Next (Assoc);
6526 end loop;
6528 return False;
6529 end Is_Defaulted;
6531 --------------------------------
6532 -- Same_Instantiated_Constant --
6533 --------------------------------
6535 function Same_Instantiated_Constant
6536 (E1, E2 : Entity_Id) return Boolean
6538 Ent : Entity_Id;
6540 begin
6541 Ent := E2;
6542 while Present (Ent) loop
6543 if E1 = Ent then
6544 return True;
6546 elsif Ekind (Ent) /= E_Constant then
6547 return False;
6549 elsif Is_Entity_Name (Constant_Value (Ent)) then
6550 if Entity (Constant_Value (Ent)) = E1 then
6551 return True;
6552 else
6553 Ent := Entity (Constant_Value (Ent));
6554 end if;
6556 -- The actual may be a constant that has been folded. Recover
6557 -- original name.
6559 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6560 Ent := Entity (Original_Node (Constant_Value (Ent)));
6562 else
6563 return False;
6564 end if;
6565 end loop;
6567 return False;
6568 end Same_Instantiated_Constant;
6570 --------------------------------
6571 -- Same_Instantiated_Function --
6572 --------------------------------
6574 function Same_Instantiated_Function
6575 (E1, E2 : Entity_Id) return Boolean
6577 U1, U2 : Node_Id;
6578 begin
6579 if Alias (E1) = Alias (E2) then
6580 return True;
6582 elsif Present (Alias (E2)) then
6583 U1 := Original_Node (Unit_Declaration_Node (E1));
6584 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6586 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6587 and then Nkind (Name (U1)) = N_Attribute_Reference
6589 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6590 and then Nkind (Name (U2)) = N_Attribute_Reference
6592 and then
6593 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6594 else
6595 return False;
6596 end if;
6597 end Same_Instantiated_Function;
6599 --------------------------------
6600 -- Same_Instantiated_Variable --
6601 --------------------------------
6603 function Same_Instantiated_Variable
6604 (E1, E2 : Entity_Id) return Boolean
6606 function Original_Entity (E : Entity_Id) return Entity_Id;
6607 -- Follow chain of renamings to the ultimate ancestor
6609 ---------------------
6610 -- Original_Entity --
6611 ---------------------
6613 function Original_Entity (E : Entity_Id) return Entity_Id is
6614 Orig : Entity_Id;
6616 begin
6617 Orig := E;
6618 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6619 and then Present (Renamed_Object (Orig))
6620 and then Is_Entity_Name (Renamed_Object (Orig))
6621 loop
6622 Orig := Entity (Renamed_Object (Orig));
6623 end loop;
6625 return Orig;
6626 end Original_Entity;
6628 -- Start of processing for Same_Instantiated_Variable
6630 begin
6631 return Ekind (E1) = Ekind (E2)
6632 and then Original_Entity (E1) = Original_Entity (E2);
6633 end Same_Instantiated_Variable;
6635 -- Start of processing for Check_Formal_Package_Instance
6637 begin
6638 Prev_E1 := E1;
6639 while Present (E1) and then Present (E2) loop
6640 exit when Ekind (E1) = E_Package
6641 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6643 -- If the formal is the renaming of the formal package, this
6644 -- is the end of its formal part, which may occur before the
6645 -- end of the formal part in the actual in the presence of
6646 -- defaulted parameters in the formal package.
6648 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6649 and then Renamed_Entity (E2) = Scope (E2);
6651 -- The analysis of the actual may generate additional internal
6652 -- entities. If the formal is defaulted, there is no corresponding
6653 -- analysis and the internal entities must be skipped, until we
6654 -- find corresponding entities again.
6656 if Comes_From_Source (E2)
6657 and then not Comes_From_Source (E1)
6658 and then Chars (E1) /= Chars (E2)
6659 then
6660 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6661 Next_Entity (E1);
6662 end loop;
6663 end if;
6665 if No (E1) then
6666 return;
6668 -- Entities may be declared without full declaration, such as
6669 -- itypes and predefined operators (concatenation for arrays, eg).
6670 -- Skip it and keep the formal entity to find a later match for it.
6672 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6673 E1 := Prev_E1;
6674 goto Next_E;
6676 -- If the formal entity comes from a formal declaration, it was
6677 -- defaulted in the formal package, and no check is needed on it.
6679 elsif Nkind (Original_Node (Parent (E2))) in
6680 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6681 then
6682 -- If the formal is a tagged type the corresponding class-wide
6683 -- type has been generated as well, and it must be skipped.
6685 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6686 Next_Entity (E2);
6687 end if;
6689 goto Next_E;
6691 -- Ditto for defaulted formal subprograms.
6693 elsif Is_Overloadable (E1)
6694 and then Nkind (Unit_Declaration_Node (E2)) in
6695 N_Formal_Subprogram_Declaration
6696 then
6697 goto Next_E;
6699 elsif Is_Defaulted (E1) then
6700 goto Next_E;
6702 elsif Is_Type (E1) then
6704 -- Subtypes must statically match. E1, E2 are the local entities
6705 -- that are subtypes of the actuals. Itypes generated for other
6706 -- parameters need not be checked, the check will be performed
6707 -- on the parameters themselves.
6709 -- If E2 is a formal type declaration, it is a defaulted parameter
6710 -- and needs no checking.
6712 if not Is_Itype (E1) and then not Is_Itype (E2) then
6713 Check_Mismatch
6714 (not Is_Type (E2)
6715 or else Etype (E1) /= Etype (E2)
6716 or else not Subtypes_Statically_Match (E1, E2));
6717 end if;
6719 elsif Ekind (E1) = E_Constant then
6721 -- IN parameters must denote the same static value, or the same
6722 -- constant, or the literal null.
6724 Expr1 := Expression (Parent (E1));
6726 if Ekind (E2) /= E_Constant then
6727 Check_Mismatch (True);
6728 goto Next_E;
6729 else
6730 Expr2 := Expression (Parent (E2));
6731 end if;
6733 if Is_OK_Static_Expression (Expr1) then
6734 if not Is_OK_Static_Expression (Expr2) then
6735 Check_Mismatch (True);
6737 elsif Is_Discrete_Type (Etype (E1)) then
6738 declare
6739 V1 : constant Uint := Expr_Value (Expr1);
6740 V2 : constant Uint := Expr_Value (Expr2);
6741 begin
6742 Check_Mismatch (V1 /= V2);
6743 end;
6745 elsif Is_Real_Type (Etype (E1)) then
6746 declare
6747 V1 : constant Ureal := Expr_Value_R (Expr1);
6748 V2 : constant Ureal := Expr_Value_R (Expr2);
6749 begin
6750 Check_Mismatch (V1 /= V2);
6751 end;
6753 elsif Is_String_Type (Etype (E1))
6754 and then Nkind (Expr1) = N_String_Literal
6755 then
6756 if Nkind (Expr2) /= N_String_Literal then
6757 Check_Mismatch (True);
6758 else
6759 Check_Mismatch
6760 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6761 end if;
6762 end if;
6764 elsif Is_Entity_Name (Expr1) then
6765 if Is_Entity_Name (Expr2) then
6766 if Entity (Expr1) = Entity (Expr2) then
6767 null;
6768 else
6769 Check_Mismatch
6770 (not Same_Instantiated_Constant
6771 (Entity (Expr1), Entity (Expr2)));
6772 end if;
6774 else
6775 Check_Mismatch (True);
6776 end if;
6778 elsif Is_Entity_Name (Original_Node (Expr1))
6779 and then Is_Entity_Name (Expr2)
6780 and then Same_Instantiated_Constant
6781 (Entity (Original_Node (Expr1)), Entity (Expr2))
6782 then
6783 null;
6785 elsif Nkind (Expr1) = N_Null then
6786 Check_Mismatch (Nkind (Expr1) /= N_Null);
6788 else
6789 Check_Mismatch (True);
6790 end if;
6792 elsif Ekind (E1) = E_Variable then
6793 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6795 elsif Ekind (E1) = E_Package then
6796 Check_Mismatch
6797 (Ekind (E1) /= Ekind (E2)
6798 or else (Present (Renamed_Entity (E2))
6799 and then Renamed_Entity (E1) /=
6800 Renamed_Entity (E2)));
6802 elsif Is_Overloadable (E1) then
6803 -- Verify that the actual subprograms match. Note that actuals
6804 -- that are attributes are rewritten as subprograms. If the
6805 -- subprogram in the formal package is defaulted, no check is
6806 -- needed. Note that this can only happen in Ada 2005 when the
6807 -- formal package can be partially parameterized.
6809 if Nkind (Unit_Declaration_Node (E1)) =
6810 N_Subprogram_Renaming_Declaration
6811 and then From_Default (Unit_Declaration_Node (E1))
6812 then
6813 null;
6815 -- If the formal package has an "others" box association that
6816 -- covers this formal, there is no need for a check either.
6818 elsif Nkind (Unit_Declaration_Node (E2)) in
6819 N_Formal_Subprogram_Declaration
6820 and then Box_Present (Unit_Declaration_Node (E2))
6821 then
6822 null;
6824 -- No check needed if subprogram is a defaulted null procedure
6826 elsif No (Alias (E2))
6827 and then Ekind (E2) = E_Procedure
6828 and then
6829 Null_Present (Specification (Unit_Declaration_Node (E2)))
6830 then
6831 null;
6833 -- Otherwise the actual in the formal and the actual in the
6834 -- instantiation of the formal must match, up to renamings.
6836 else
6837 Check_Mismatch
6838 (Ekind (E2) /= Ekind (E1)
6839 or else not Same_Instantiated_Function (E1, E2));
6840 end if;
6842 else
6843 raise Program_Error;
6844 end if;
6846 <<Next_E>>
6847 Prev_E1 := E1;
6848 Next_Entity (E1);
6849 Next_Entity (E2);
6850 end loop;
6851 end Check_Formal_Package_Instance;
6853 ---------------------------
6854 -- Check_Formal_Packages --
6855 ---------------------------
6857 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6858 E : Entity_Id;
6859 Formal_P : Entity_Id;
6860 Formal_Decl : Node_Id;
6862 begin
6863 -- Iterate through the declarations in the instance, looking for package
6864 -- renaming declarations that denote instances of formal packages, until
6865 -- we find the renaming of the current package itself. The declaration
6866 -- of a formal package that requires conformance checking is followed by
6867 -- an internal entity that is the abbreviated instance.
6869 E := First_Entity (P_Id);
6870 while Present (E) loop
6871 if Ekind (E) = E_Package then
6872 exit when Renamed_Entity (E) = P_Id;
6874 if Nkind (Parent (E)) = N_Package_Renaming_Declaration then
6875 Formal_Decl := Parent (Associated_Formal_Package (E));
6877 if Requires_Conformance_Checking (Formal_Decl) then
6878 Formal_P := Next_Entity (E);
6880 -- If the instance is within an enclosing instance body
6881 -- there is no need to verify the legality of current formal
6882 -- packages because they were legal in the generic body.
6883 -- This optimization may be applicable elsewhere, and it
6884 -- also removes spurious errors that may arise with
6885 -- on-the-fly inlining and confusion between private and
6886 -- full views.
6888 if not In_Instance_Body then
6889 Check_Formal_Package_Instance (Formal_P, E);
6890 end if;
6892 -- Restore the visibility of formals of the formal instance
6893 -- that are not defaulted, and are hidden within the current
6894 -- generic. These formals may be visible within an enclosing
6895 -- generic.
6897 declare
6898 Elmt : Elmt_Id;
6899 begin
6900 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6901 while Present (Elmt) loop
6902 Set_Is_Hidden (Node (Elmt), False);
6903 Next_Elmt (Elmt);
6904 end loop;
6905 end;
6907 -- After checking, remove the internal validating package.
6908 -- It is only needed for semantic checks, and as it may
6909 -- contain generic formal declarations it should not reach
6910 -- gigi.
6912 Remove (Unit_Declaration_Node (Formal_P));
6913 end if;
6914 end if;
6915 end if;
6917 Next_Entity (E);
6918 end loop;
6919 end Check_Formal_Packages;
6921 ---------------------------------
6922 -- Check_Forward_Instantiation --
6923 ---------------------------------
6925 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6926 S : Entity_Id;
6927 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6929 begin
6930 -- The instantiation appears before the generic body if we are in the
6931 -- scope of the unit containing the generic, either in its spec or in
6932 -- the package body, and before the generic body.
6934 if Ekind (Gen_Comp) = E_Package_Body then
6935 Gen_Comp := Spec_Entity (Gen_Comp);
6936 end if;
6938 if In_Open_Scopes (Gen_Comp)
6939 and then No (Corresponding_Body (Decl))
6940 then
6941 S := Current_Scope;
6943 while Present (S)
6944 and then not Is_Compilation_Unit (S)
6945 and then not Is_Child_Unit (S)
6946 loop
6947 if Ekind (S) = E_Package then
6948 Set_Has_Forward_Instantiation (S);
6949 end if;
6951 S := Scope (S);
6952 end loop;
6953 end if;
6954 end Check_Forward_Instantiation;
6956 ---------------------------
6957 -- Check_Generic_Actuals --
6958 ---------------------------
6960 -- The visibility of the actuals may be different between the point of
6961 -- generic instantiation and the instantiation of the body.
6963 procedure Check_Generic_Actuals
6964 (Instance : Entity_Id;
6965 Is_Formal_Box : Boolean)
6967 Gen_Id : constant Entity_Id
6968 := (if Is_Generic_Unit (Instance) then
6969 Instance
6970 elsif Is_Wrapper_Package (Instance) then
6971 Generic_Parent
6972 (Specification
6973 (Unit_Declaration_Node (Related_Instance (Instance))))
6974 else
6975 Generic_Parent (Package_Specification (Instance)));
6976 -- The generic unit
6978 Parent_Scope : constant Entity_Id := Scope (Gen_Id);
6979 -- The enclosing scope of the generic unit
6981 procedure Check_Actual_Type (Typ : Entity_Id);
6982 -- If the type of the actual is a private type declared in the
6983 -- enclosing scope of the generic unit, the body of the generic
6984 -- sees the full view of the type (because it has to appear in
6985 -- the corresponding package body). If the type is private now,
6986 -- exchange views to restore the proper visibility in the instance.
6988 -----------------------
6989 -- Check_Actual_Type --
6990 -----------------------
6992 procedure Check_Actual_Type (Typ : Entity_Id) is
6993 Btyp : constant Entity_Id := Base_Type (Typ);
6995 begin
6996 -- The exchange is only needed if the generic is defined
6997 -- within a package which is not a common ancestor of the
6998 -- scope of the instance, and is not already in scope.
7000 if Is_Private_Type (Btyp)
7001 and then Scope (Btyp) = Parent_Scope
7002 and then Ekind (Parent_Scope) in E_Package | E_Generic_Package
7003 and then Scope (Instance) /= Parent_Scope
7004 and then not Is_Child_Unit (Gen_Id)
7005 then
7006 Switch_View (Btyp);
7008 -- If the type of the entity is a subtype, it may also have
7009 -- to be made visible, together with the base type of its
7010 -- full view, after exchange.
7012 if Is_Private_Type (Typ) then
7013 Switch_View (Typ);
7014 Switch_View (Base_Type (Typ));
7015 end if;
7016 end if;
7017 end Check_Actual_Type;
7019 Astype : Entity_Id;
7020 E : Entity_Id;
7021 Formal : Node_Id;
7023 begin
7024 E := First_Entity (Instance);
7025 while Present (E) loop
7026 if Is_Type (E)
7027 and then Nkind (Parent (E)) = N_Subtype_Declaration
7028 and then Scope (Etype (E)) /= Instance
7029 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7030 then
7031 -- Restore the proper view of the actual from the information
7032 -- saved earlier by Instantiate_Type.
7034 Check_Private_View (Subtype_Indication (Parent (E)));
7036 -- If the actual is itself the formal of a parent instance,
7037 -- then also restore the proper view of its actual and so on.
7038 -- That's necessary for nested instantiations of the form
7040 -- generic
7041 -- type Component is private;
7042 -- type Array_Type is array (Positive range <>) of Component;
7043 -- procedure Proc;
7045 -- when the outermost actuals have inconsistent views, because
7046 -- the Component_Type of Array_Type of the inner instantiations
7047 -- is the actual of Component of the outermost one and not that
7048 -- of the corresponding inner instantiations.
7050 Astype := Ancestor_Subtype (E);
7051 while Present (Astype)
7052 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7053 and then Present (Generic_Parent_Type (Parent (Astype)))
7054 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7055 loop
7056 Check_Private_View (Subtype_Indication (Parent (Astype)));
7057 Astype := Ancestor_Subtype (Astype);
7058 end loop;
7060 Set_Is_Generic_Actual_Type (E);
7062 if Is_Private_Type (E) and then Present (Full_View (E)) then
7063 Set_Is_Generic_Actual_Type (Full_View (E));
7064 end if;
7066 Set_Is_Hidden (E, False);
7067 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7069 -- We constructed the generic actual type as a subtype of the
7070 -- supplied type. This means that it normally would not inherit
7071 -- subtype specific attributes of the actual, which is wrong for
7072 -- the generic case.
7074 Astype := Ancestor_Subtype (E);
7076 if No (Astype) then
7078 -- This can happen when E is an itype that is the full view of
7079 -- a private type completed, e.g. with a constrained array. In
7080 -- that case, use the first subtype, which will carry size
7081 -- information. The base type itself is unconstrained and will
7082 -- not carry it.
7084 Astype := First_Subtype (E);
7085 end if;
7087 Set_Size_Info (E, Astype);
7088 Copy_RM_Size (To => E, From => Astype);
7089 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7091 if Is_Discrete_Or_Fixed_Point_Type (E) then
7092 Set_RM_Size (E, RM_Size (Astype));
7093 end if;
7095 elsif Ekind (E) = E_Package then
7097 -- If this is the renaming for the current instance, we're done.
7098 -- Otherwise it is a formal package. If the corresponding formal
7099 -- was declared with a box, the (instantiations of the) generic
7100 -- formal part are also visible. Otherwise, ignore the entity
7101 -- created to validate the actuals.
7103 if Renamed_Entity (E) = Instance then
7104 exit;
7106 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7107 null;
7109 -- The visibility of a formal of an enclosing generic is already
7110 -- correct.
7112 elsif Denotes_Formal_Package (E) then
7113 null;
7115 elsif Present (Associated_Formal_Package (E))
7116 and then not Is_Generic_Formal (E)
7117 then
7118 Check_Generic_Actuals
7119 (Renamed_Entity (E),
7120 Is_Formal_Box =>
7121 Box_Present (Parent (Associated_Formal_Package (E))));
7123 Set_Is_Hidden (E, False);
7124 end if;
7126 -- If this is a subprogram instance (in a wrapper package) the
7127 -- actual is fully visible.
7129 elsif Is_Wrapper_Package (Instance) then
7130 Set_Is_Hidden (E, False);
7132 -- If the formal package is declared with a box, or if the formal
7133 -- parameter is defaulted, it is visible in the body.
7135 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7136 Set_Is_Hidden (E, False);
7137 end if;
7139 -- Check directly the type of the actual objects
7141 if Ekind (E) in E_Constant | E_Variable then
7142 Check_Actual_Type (Etype (E));
7144 -- As well as the type of formal parameters of actual subprograms
7146 elsif Ekind (E) in E_Function | E_Procedure
7147 and then Is_Generic_Actual_Subprogram (E)
7148 and then Present (Alias (E))
7149 then
7150 Formal := First_Formal (Alias (E));
7151 while Present (Formal) loop
7152 Check_Actual_Type (Etype (Formal));
7153 Next_Formal (Formal);
7154 end loop;
7155 end if;
7157 Next_Entity (E);
7158 end loop;
7159 end Check_Generic_Actuals;
7161 ------------------------------
7162 -- Check_Generic_Child_Unit --
7163 ------------------------------
7165 procedure Check_Generic_Child_Unit
7166 (Gen_Id : Node_Id;
7167 Parent_Installed : in out Boolean)
7169 Loc : constant Source_Ptr := Sloc (Gen_Id);
7170 Gen_Par : Entity_Id := Empty;
7171 E : Entity_Id;
7172 Inst_Par : Entity_Id;
7173 S : Node_Id;
7175 function Find_Generic_Child
7176 (Scop : Entity_Id;
7177 Id : Node_Id) return Entity_Id;
7178 -- Search generic parent for possible child unit with the given name
7180 function In_Enclosing_Instance return Boolean;
7181 -- Within an instance of the parent, the child unit may be denoted by
7182 -- a simple name, or an abbreviated expanded name. Examine enclosing
7183 -- scopes to locate a possible parent instantiation.
7185 ------------------------
7186 -- Find_Generic_Child --
7187 ------------------------
7189 function Find_Generic_Child
7190 (Scop : Entity_Id;
7191 Id : Node_Id) return Entity_Id
7193 E : Entity_Id;
7195 begin
7196 -- If entity of name is already set, instance has already been
7197 -- resolved, e.g. in an enclosing instantiation.
7199 if Present (Entity (Id)) then
7200 if Scope (Entity (Id)) = Scop then
7201 return Entity (Id);
7202 else
7203 return Empty;
7204 end if;
7206 else
7207 E := First_Entity (Scop);
7208 while Present (E) loop
7209 if Chars (E) = Chars (Id)
7210 and then Is_Child_Unit (E)
7211 then
7212 if Is_Child_Unit (E)
7213 and then not Is_Visible_Lib_Unit (E)
7214 then
7215 Error_Msg_NE
7216 ("generic child unit& is not visible", Gen_Id, E);
7217 end if;
7219 Set_Entity (Id, E);
7220 return E;
7221 end if;
7223 Next_Entity (E);
7224 end loop;
7226 return Empty;
7227 end if;
7228 end Find_Generic_Child;
7230 ---------------------------
7231 -- In_Enclosing_Instance --
7232 ---------------------------
7234 function In_Enclosing_Instance return Boolean is
7235 Enclosing_Instance : Node_Id;
7236 Instance_Decl : Node_Id;
7238 begin
7239 -- We do not inline any call that contains instantiations, except
7240 -- for instantiations of Unchecked_Conversion, so if we are within
7241 -- an inlined body the current instance does not require parents.
7243 if In_Inlined_Body then
7244 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7245 return False;
7246 end if;
7248 -- Loop to check enclosing scopes
7250 Enclosing_Instance := Current_Scope;
7251 while Present (Enclosing_Instance) loop
7252 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7254 if Ekind (Enclosing_Instance) = E_Package
7255 and then Is_Generic_Instance (Enclosing_Instance)
7256 and then Present
7257 (Generic_Parent (Specification (Instance_Decl)))
7258 then
7259 -- Check whether the generic we are looking for is a child of
7260 -- this instance.
7262 E := Find_Generic_Child
7263 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7264 exit when Present (E);
7266 else
7267 E := Empty;
7268 end if;
7270 Enclosing_Instance := Scope (Enclosing_Instance);
7271 end loop;
7273 if No (E) then
7275 -- Not a child unit
7277 Analyze (Gen_Id);
7278 return False;
7280 else
7281 Rewrite (Gen_Id,
7282 Make_Expanded_Name (Loc,
7283 Chars => Chars (E),
7284 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7285 Selector_Name => New_Occurrence_Of (E, Loc)));
7287 Set_Entity (Gen_Id, E);
7288 Set_Etype (Gen_Id, Etype (E));
7289 Parent_Installed := False; -- Already in scope.
7290 return True;
7291 end if;
7292 end In_Enclosing_Instance;
7294 -- Start of processing for Check_Generic_Child_Unit
7296 begin
7297 -- If the name of the generic is given by a selected component, it may
7298 -- be the name of a generic child unit, and the prefix is the name of an
7299 -- instance of the parent, in which case the child unit must be visible.
7300 -- If this instance is not in scope, it must be placed there and removed
7301 -- after instantiation, because what is being instantiated is not the
7302 -- original child, but the corresponding child present in the instance
7303 -- of the parent.
7305 -- If the child is instantiated within the parent, it can be given by
7306 -- a simple name. In this case the instance is already in scope, but
7307 -- the child generic must be recovered from the generic parent as well.
7309 if Nkind (Gen_Id) = N_Selected_Component then
7310 S := Selector_Name (Gen_Id);
7311 Analyze (Prefix (Gen_Id));
7312 Inst_Par := Entity (Prefix (Gen_Id));
7314 if Ekind (Inst_Par) = E_Package
7315 and then Present (Renamed_Entity (Inst_Par))
7316 then
7317 Inst_Par := Renamed_Entity (Inst_Par);
7318 end if;
7320 if Ekind (Inst_Par) = E_Package then
7321 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7322 Gen_Par := Generic_Parent (Parent (Inst_Par));
7324 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7325 and then
7326 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7327 then
7328 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7329 end if;
7331 elsif Ekind (Inst_Par) = E_Generic_Package
7332 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7333 then
7334 -- A formal package may be a real child package, and not the
7335 -- implicit instance within a parent. In this case the child is
7336 -- not visible and has to be retrieved explicitly as well.
7338 Gen_Par := Inst_Par;
7339 end if;
7341 if Present (Gen_Par) then
7343 -- The prefix denotes an instantiation. The entity itself may be a
7344 -- nested generic, or a child unit.
7346 E := Find_Generic_Child (Gen_Par, S);
7348 if Present (E) then
7349 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7350 Set_Entity (Gen_Id, E);
7351 Set_Etype (Gen_Id, Etype (E));
7352 Set_Entity (S, E);
7353 Set_Etype (S, Etype (E));
7355 -- Indicate that this is a reference to the parent
7357 if In_Extended_Main_Source_Unit (Gen_Id) then
7358 Set_Is_Instantiated (Inst_Par);
7359 end if;
7361 -- A common mistake is to replicate the naming scheme of a
7362 -- hierarchy by instantiating a generic child directly, rather
7363 -- than the implicit child in a parent instance:
7365 -- generic .. package Gpar is ..
7366 -- generic .. package Gpar.Child is ..
7367 -- package Par is new Gpar ();
7369 -- with Gpar.Child;
7370 -- package Par.Child is new Gpar.Child ();
7371 -- rather than Par.Child
7373 -- In this case the instantiation is within Par, which is an
7374 -- instance, but Gpar does not denote Par because we are not IN
7375 -- the instance of Gpar, so this is illegal. The test below
7376 -- recognizes this particular case.
7378 if Is_Child_Unit (E)
7379 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7380 and then (not In_Instance
7381 or else Nkind (Parent (Parent (Gen_Id))) =
7382 N_Compilation_Unit)
7383 then
7384 Error_Msg_N
7385 ("prefix of generic child unit must be instance of parent",
7386 Gen_Id);
7387 end if;
7389 if not In_Open_Scopes (Inst_Par)
7390 and then Nkind (Parent (Gen_Id)) not in
7391 N_Generic_Renaming_Declaration
7392 then
7393 Install_Parent (Inst_Par);
7394 Parent_Installed := True;
7396 elsif In_Open_Scopes (Inst_Par) then
7398 -- If the parent is already installed, install the actuals
7399 -- for its formal packages. This is necessary when the child
7400 -- instance is a child of the parent instance: in this case,
7401 -- the parent is placed on the scope stack but the formal
7402 -- packages are not made visible.
7404 Install_Formal_Packages (Inst_Par);
7405 end if;
7407 else
7408 -- If the generic parent does not contain an entity that
7409 -- corresponds to the selector, the instance doesn't either.
7410 -- Analyzing the node will yield the appropriate error message.
7411 -- If the entity is not a child unit, then it is an inner
7412 -- generic in the parent.
7414 Analyze (Gen_Id);
7415 end if;
7417 else
7418 Analyze (Gen_Id);
7420 if Is_Child_Unit (Entity (Gen_Id))
7421 and then
7422 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7423 and then not In_Open_Scopes (Inst_Par)
7424 then
7425 Install_Parent (Inst_Par);
7426 Parent_Installed := True;
7428 -- The generic unit may be the renaming of the implicit child
7429 -- present in an instance. In that case the parent instance is
7430 -- obtained from the name of the renamed entity.
7432 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7433 and then Present (Renamed_Entity (Entity (Gen_Id)))
7434 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7435 then
7436 declare
7437 Renamed_Package : constant Node_Id :=
7438 Name (Parent (Entity (Gen_Id)));
7439 begin
7440 if Nkind (Renamed_Package) = N_Expanded_Name then
7441 Inst_Par := Entity (Prefix (Renamed_Package));
7442 Install_Parent (Inst_Par);
7443 Parent_Installed := True;
7444 end if;
7445 end;
7446 end if;
7447 end if;
7449 elsif Nkind (Gen_Id) = N_Expanded_Name then
7451 -- Entity already present, analyze prefix, whose meaning may be an
7452 -- instance in the current context. If it is an instance of a
7453 -- relative within another, the proper parent may still have to be
7454 -- installed, if they are not of the same generation.
7456 Analyze (Prefix (Gen_Id));
7458 -- Prevent cascaded errors
7460 if Etype (Prefix (Gen_Id)) = Any_Type then
7461 return;
7462 end if;
7464 -- In the unlikely case that a local declaration hides the name of
7465 -- the parent package, locate it on the homonym chain. If the context
7466 -- is an instance of the parent, the renaming entity is flagged as
7467 -- such.
7469 Inst_Par := Entity (Prefix (Gen_Id));
7470 while Present (Inst_Par)
7471 and then not Is_Package_Or_Generic_Package (Inst_Par)
7472 loop
7473 Inst_Par := Homonym (Inst_Par);
7474 end loop;
7476 pragma Assert (Present (Inst_Par));
7477 Set_Entity (Prefix (Gen_Id), Inst_Par);
7479 if In_Enclosing_Instance then
7480 null;
7482 elsif Present (Entity (Gen_Id))
7483 and then No (Renamed_Entity (Entity (Gen_Id)))
7484 and then Is_Child_Unit (Entity (Gen_Id))
7485 and then not In_Open_Scopes (Inst_Par)
7486 then
7487 Install_Parent (Inst_Par);
7488 Parent_Installed := True;
7490 -- Handle renaming of generic child unit
7492 elsif Present (Entity (Gen_Id))
7493 and then Present (Renamed_Entity (Entity (Gen_Id)))
7494 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7495 then
7496 declare
7497 E : Entity_Id;
7498 Ren_Decl : Node_Id;
7500 begin
7501 -- The entity of the renamed generic child unit does not
7502 -- have any reference to the instantiated parent. In order to
7503 -- locate it we traverse the scope containing the renaming
7504 -- declaration; the instance of the parent is available in
7505 -- the prefix of the renaming declaration. For example:
7507 -- package A is
7508 -- package Inst_Par is new ...
7509 -- generic package Ren_Child renames Ins_Par.Child;
7510 -- end;
7512 -- with A;
7513 -- package B is
7514 -- package Inst_Child is new A.Ren_Child;
7515 -- end;
7517 E := First_Entity (Entity (Prefix (Gen_Id)));
7518 while Present (E) loop
7519 if not Is_Object (E)
7520 and then Present (Renamed_Entity (E))
7521 and then
7522 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7523 then
7524 Ren_Decl := Parent (E);
7525 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7527 if not In_Open_Scopes (Inst_Par) then
7528 Install_Parent (Inst_Par);
7529 Parent_Installed := True;
7530 end if;
7532 exit;
7533 end if;
7535 E := Next_Entity (E);
7536 end loop;
7537 end;
7538 end if;
7540 elsif In_Enclosing_Instance then
7542 -- The child unit is found in some enclosing scope
7544 null;
7546 else
7547 Analyze (Gen_Id);
7549 -- If this is the renaming of the implicit child in a parent
7550 -- instance, recover the parent name and install it.
7552 if Is_Entity_Name (Gen_Id) then
7553 E := Entity (Gen_Id);
7555 if Is_Generic_Unit (E)
7556 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7557 and then Is_Child_Unit (Renamed_Entity (E))
7558 and then Is_Generic_Unit (Scope (Renamed_Entity (E)))
7559 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7560 then
7561 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7562 Inst_Par := Entity (Prefix (Gen_Id));
7564 if not In_Open_Scopes (Inst_Par) then
7565 Install_Parent (Inst_Par);
7566 Parent_Installed := True;
7567 end if;
7569 -- If it is a child unit of a non-generic parent, it may be
7570 -- use-visible and given by a direct name. Install parent as
7571 -- for other cases.
7573 elsif Is_Generic_Unit (E)
7574 and then Is_Child_Unit (E)
7575 and then
7576 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7577 and then not Is_Generic_Unit (Scope (E))
7578 then
7579 if not In_Open_Scopes (Scope (E)) then
7580 Install_Parent (Scope (E));
7581 Parent_Installed := True;
7582 end if;
7583 end if;
7584 end if;
7585 end if;
7586 end Check_Generic_Child_Unit;
7588 -----------------------------
7589 -- Check_Hidden_Child_Unit --
7590 -----------------------------
7592 procedure Check_Hidden_Child_Unit
7593 (N : Node_Id;
7594 Gen_Unit : Entity_Id;
7595 Act_Decl_Id : Entity_Id)
7597 Gen_Id : constant Node_Id := Name (N);
7599 begin
7600 if Is_Child_Unit (Gen_Unit)
7601 and then Is_Child_Unit (Act_Decl_Id)
7602 and then Nkind (Gen_Id) = N_Expanded_Name
7603 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7604 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7605 then
7606 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7607 Error_Msg_NE
7608 ("generic unit & is implicitly declared in &",
7609 Defining_Unit_Name (N), Gen_Unit);
7610 Error_Msg_N ("\instance must have different name",
7611 Defining_Unit_Name (N));
7612 end if;
7613 end Check_Hidden_Child_Unit;
7615 ------------------------
7616 -- Check_Private_View --
7617 ------------------------
7619 procedure Check_Private_View (N : Node_Id) is
7620 T : constant Entity_Id := Etype (N);
7621 BT : Entity_Id;
7623 begin
7624 -- Exchange views if the type was not private in the generic but is
7625 -- private at the point of instantiation. Do not exchange views if
7626 -- the scope of the type is in scope. This can happen if both generic
7627 -- and instance are sibling units, or if type is defined in a parent.
7628 -- In this case the visibility of the type will be correct for all
7629 -- semantic checks.
7631 if Present (T) then
7632 BT := Base_Type (T);
7634 if Is_Private_Type (T)
7635 and then not Has_Private_View (N)
7636 and then Present (Full_View (T))
7637 and then not In_Open_Scopes (Scope (T))
7638 then
7639 -- In the generic, the full declaration was visible
7641 Switch_View (T);
7643 elsif Has_Private_View (N)
7644 and then not Is_Private_Type (T)
7645 and then not Has_Been_Exchanged (T)
7646 and then (not In_Open_Scopes (Scope (T))
7647 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7648 then
7649 -- In the generic, only the private declaration was visible
7651 -- If the type appears in a subtype declaration, the subtype in
7652 -- instance must have a view compatible with that of its parent,
7653 -- which must be exchanged (see corresponding code in Restore_
7654 -- Private_Views) so we make an exception to the open scope rule.
7656 Prepend_Elmt (T, Exchanged_Views);
7657 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7659 -- Finally, a non-private subtype may have a private base type, which
7660 -- must be exchanged for consistency. This can happen when a package
7661 -- body is instantiated, when the scope stack is empty but in fact
7662 -- the subtype and the base type are declared in an enclosing scope.
7664 -- Note that in this case we introduce an inconsistency in the view
7665 -- set, because we switch the base type BT, but there could be some
7666 -- private dependent subtypes of BT which remain unswitched. Such
7667 -- subtypes might need to be switched at a later point (see specific
7668 -- provision for that case in Switch_View).
7670 elsif not Is_Private_Type (T)
7671 and then not Has_Private_View (N)
7672 and then Is_Private_Type (BT)
7673 and then Present (Full_View (BT))
7674 and then not Is_Generic_Type (BT)
7675 and then not In_Open_Scopes (BT)
7676 then
7677 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7678 Exchange_Declarations (BT);
7679 end if;
7680 end if;
7681 end Check_Private_View;
7683 -----------------------------
7684 -- Check_Hidden_Primitives --
7685 -----------------------------
7687 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7688 Actual : Node_Id;
7689 Gen_T : Entity_Id;
7690 Result : Elist_Id := No_Elist;
7692 begin
7693 if No (Assoc_List) then
7694 return No_Elist;
7695 end if;
7697 -- Traverse the list of associations between formals and actuals
7698 -- searching for renamings of tagged types
7700 Actual := First (Assoc_List);
7701 while Present (Actual) loop
7702 if Nkind (Actual) = N_Subtype_Declaration then
7703 Gen_T := Generic_Parent_Type (Actual);
7705 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7707 -- Traverse the list of primitives of the actual types
7708 -- searching for hidden primitives that are visible in the
7709 -- corresponding generic formal; leave them visible and
7710 -- append them to Result to restore their decoration later.
7712 Install_Hidden_Primitives
7713 (Prims_List => Result,
7714 Gen_T => Gen_T,
7715 Act_T => Entity (Subtype_Indication (Actual)));
7716 end if;
7717 end if;
7719 Next (Actual);
7720 end loop;
7722 return Result;
7723 end Check_Hidden_Primitives;
7725 --------------------------
7726 -- Contains_Instance_Of --
7727 --------------------------
7729 function Contains_Instance_Of
7730 (Inner : Entity_Id;
7731 Outer : Entity_Id;
7732 N : Node_Id) return Boolean
7734 Elmt : Elmt_Id;
7735 Scop : Entity_Id;
7737 begin
7738 Scop := Outer;
7740 -- Verify that there are no circular instantiations. We check whether
7741 -- the unit contains an instance of the current scope or some enclosing
7742 -- scope (in case one of the instances appears in a subunit). Longer
7743 -- circularities involving subunits might seem too pathological to
7744 -- consider, but they were not too pathological for the authors of
7745 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7746 -- enclosing generic scopes as containing an instance.
7748 loop
7749 -- Within a generic subprogram body, the scope is not generic, to
7750 -- allow for recursive subprograms. Use the declaration to determine
7751 -- whether this is a generic unit.
7753 if Ekind (Scop) = E_Generic_Package
7754 or else (Is_Subprogram (Scop)
7755 and then Nkind (Unit_Declaration_Node (Scop)) =
7756 N_Generic_Subprogram_Declaration)
7757 then
7758 Elmt := First_Elmt (Inner_Instances (Inner));
7760 while Present (Elmt) loop
7761 if Node (Elmt) = Scop then
7762 Error_Msg_Node_2 := Inner;
7763 Error_Msg_NE
7764 ("circular instantiation: & instantiated within &!",
7765 N, Scop);
7766 return True;
7768 elsif Node (Elmt) = Inner then
7769 return True;
7771 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7772 Error_Msg_Node_2 := Inner;
7773 Error_Msg_NE
7774 ("circular instantiation: & instantiated within &!",
7775 N, Node (Elmt));
7776 return True;
7777 end if;
7779 Next_Elmt (Elmt);
7780 end loop;
7782 -- Indicate that Inner is being instantiated within Scop
7784 Append_Elmt (Inner, Inner_Instances (Scop));
7785 end if;
7787 if Scop = Standard_Standard then
7788 exit;
7789 else
7790 Scop := Scope (Scop);
7791 end if;
7792 end loop;
7794 return False;
7795 end Contains_Instance_Of;
7797 -----------------------
7798 -- Copy_Generic_Node --
7799 -----------------------
7801 function Copy_Generic_Node
7802 (N : Node_Id;
7803 Parent_Id : Node_Id;
7804 Instantiating : Boolean) return Node_Id
7806 Ent : Entity_Id;
7807 New_N : Node_Id;
7809 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7810 -- Check the given value of one of the Fields referenced by the current
7811 -- node to determine whether to copy it recursively. The field may hold
7812 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7813 -- Char) in which case it need not be copied.
7815 procedure Copy_Descendants;
7816 -- Common utility for various nodes
7818 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7819 -- Make copy of element list
7821 function Copy_Generic_List
7822 (L : List_Id;
7823 Parent_Id : Node_Id) return List_Id;
7824 -- Apply Copy_Generic_Node recursively to the members of a node list
7826 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7827 -- True if an identifier is part of the defining program unit name of
7828 -- a child unit.
7829 -- Consider removing this subprogram now that ASIS no longer uses it.
7831 ----------------------
7832 -- Copy_Descendants --
7833 ----------------------
7835 procedure Copy_Descendants is
7836 procedure Walk is new
7837 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
7838 begin
7839 Walk (New_N, N);
7840 end Copy_Descendants;
7842 -----------------------------
7843 -- Copy_Generic_Descendant --
7844 -----------------------------
7846 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7847 begin
7848 if D = Union_Id (Empty) then
7849 return D;
7851 elsif D in Node_Range then
7852 return Union_Id
7853 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7855 elsif D in List_Range then
7856 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7858 elsif D in Elist_Range then
7859 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7861 -- Nothing else is copyable (e.g. Uint values), return as is
7863 else
7864 return D;
7865 end if;
7866 end Copy_Generic_Descendant;
7868 ------------------------
7869 -- Copy_Generic_Elist --
7870 ------------------------
7872 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7873 M : Elmt_Id;
7874 L : Elist_Id;
7876 begin
7877 if Present (E) then
7878 L := New_Elmt_List;
7879 M := First_Elmt (E);
7880 while Present (M) loop
7881 Append_Elmt
7882 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7883 Next_Elmt (M);
7884 end loop;
7886 return L;
7888 else
7889 return No_Elist;
7890 end if;
7891 end Copy_Generic_Elist;
7893 -----------------------
7894 -- Copy_Generic_List --
7895 -----------------------
7897 function Copy_Generic_List
7898 (L : List_Id;
7899 Parent_Id : Node_Id) return List_Id
7901 N : Node_Id;
7902 New_L : List_Id;
7904 begin
7905 if Present (L) then
7906 New_L := New_List;
7907 Set_Parent (New_L, Parent_Id);
7909 N := First (L);
7910 while Present (N) loop
7911 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7912 Next (N);
7913 end loop;
7915 return New_L;
7917 else
7918 return No_List;
7919 end if;
7920 end Copy_Generic_List;
7922 ---------------------------
7923 -- In_Defining_Unit_Name --
7924 ---------------------------
7926 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7927 begin
7928 return
7929 Present (Parent (Nam))
7930 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7931 or else
7932 (Nkind (Parent (Nam)) = N_Expanded_Name
7933 and then In_Defining_Unit_Name (Parent (Nam))));
7934 end In_Defining_Unit_Name;
7936 -- Start of processing for Copy_Generic_Node
7938 begin
7939 if N = Empty then
7940 return N;
7941 end if;
7943 New_N := New_Copy (N);
7945 -- Copy aspects if present
7947 if Has_Aspects (N) then
7948 Set_Has_Aspects (New_N, False);
7949 Set_Aspect_Specifications
7950 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7951 end if;
7953 -- If we are instantiating, we want to adjust the sloc based on the
7954 -- current S_Adjustment. However, if this is the root node of a subunit,
7955 -- we need to defer that adjustment to below (see "elsif Instantiating
7956 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7957 -- computed the adjustment.
7959 if Instantiating
7960 and then not (Nkind (N) in N_Proper_Body
7961 and then Was_Originally_Stub (N))
7962 then
7963 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7964 end if;
7966 if not Is_List_Member (N) then
7967 Set_Parent (New_N, Parent_Id);
7968 end if;
7970 -- Special casing for identifiers and other entity names and operators
7972 if Nkind (N) in N_Character_Literal
7973 | N_Expanded_Name
7974 | N_Identifier
7975 | N_Operator_Symbol
7976 | N_Op
7977 then
7978 if not Instantiating then
7980 -- Link both nodes in order to assign subsequently the entity of
7981 -- the copy to the original node, in case this is a global
7982 -- reference.
7984 Set_Associated_Node (N, New_N);
7986 -- If we are within an instantiation, this is a nested generic
7987 -- that has already been analyzed at the point of definition.
7988 -- We must preserve references that were global to the enclosing
7989 -- parent at that point. Other occurrences, whether global or
7990 -- local to the current generic, must be resolved anew, so we
7991 -- reset the entity in the generic copy. A global reference has a
7992 -- smaller depth than the parent, or else the same depth in case
7993 -- both are distinct compilation units.
7995 -- A child unit is implicitly declared within the enclosing parent
7996 -- but is in fact global to it, and must be preserved.
7998 -- It is also possible for Current_Instantiated_Parent to be
7999 -- defined, and for this not to be a nested generic, namely if
8000 -- the unit is loaded through Rtsfind. In that case, the entity of
8001 -- New_N is only a link to the associated node, and not a defining
8002 -- occurrence.
8004 -- The entities for parent units in the defining_program_unit of a
8005 -- generic child unit are established when the context of the unit
8006 -- is first analyzed, before the generic copy is made. They are
8007 -- preserved in the copy for use in e.g. ASIS queries.
8009 Ent := Entity (New_N);
8011 if No (Current_Instantiated_Parent.Gen_Id) then
8012 if No (Ent)
8013 or else Nkind (Ent) /= N_Defining_Identifier
8014 or else not In_Defining_Unit_Name (N)
8015 then
8016 Set_Associated_Node (New_N, Empty);
8017 end if;
8019 elsif No (Ent)
8020 or else Nkind (Ent) not in N_Entity
8021 or else No (Scope (Ent))
8022 or else
8023 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8024 and then not Is_Child_Unit (Ent))
8025 or else
8026 (Scope_Depth_Set (Scope (Ent))
8027 and then
8028 Scope_Depth (Scope (Ent)) >
8029 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8030 and then
8031 Get_Source_Unit (Ent) =
8032 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8033 then
8034 Set_Associated_Node (New_N, Empty);
8035 end if;
8037 -- Case of instantiating identifier or some other name or operator
8039 else
8040 -- If the associated node is still defined, the entity in it
8041 -- is global, and must be copied to the instance. If this copy
8042 -- is being made for a body to inline, it is applied to an
8043 -- instantiated tree, and the entity is already present and
8044 -- must be also preserved.
8046 declare
8047 Assoc : constant Node_Id := Get_Associated_Node (N);
8049 begin
8050 if Present (Assoc) then
8051 if Nkind (Assoc) = Nkind (N) then
8052 Set_Entity (New_N, Entity (Assoc));
8053 Check_Private_View (N);
8055 -- Here we deal with a very peculiar case for which the
8056 -- Has_Private_View mechanism is not sufficient, because
8057 -- the reference to the type is implicit in the tree,
8058 -- that is to say, it's not referenced from a node but
8059 -- only from another type, namely through Component_Type.
8061 -- package P is
8063 -- type Pt is private;
8065 -- generic
8066 -- type Ft is array (Positive range <>) of Pt;
8067 -- package G is
8068 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8069 -- end G;
8071 -- private
8072 -- type Pt is new Boolean;
8073 -- end P;
8075 -- package body P is
8076 -- package body G is
8077 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8078 -- begin
8079 -- if (F1 < F2) /= Lt then
8080 -- null;
8081 -- end if;
8082 -- end Check;
8083 -- end G;
8084 -- end P;
8086 -- type Arr is array (Positive range <>) of P.Pt;
8088 -- package Inst is new P.G (Arr);
8090 -- Pt is a global type for the generic package G and it
8091 -- is not referenced in its body, but only as component
8092 -- type of Ft, which is a local type. This means that no
8093 -- references to Pt or Ft are seen during the copy of the
8094 -- body, the only reference to Pt being seen is when the
8095 -- actuals are checked by Check_Generic_Actuals, but Pt
8096 -- is still private at this point. In the end, the views
8097 -- of Pt are not switched in the body and, therefore, the
8098 -- array comparison is rejected because the component is
8099 -- still private.
8101 -- Adding e.g. a dummy variable of type Pt in the body is
8102 -- sufficient to make everything work, so we generate an
8103 -- artificial reference to Pt on the fly and thus force
8104 -- the switching of views on the grounds that, if the
8105 -- comparison was accepted during the semantic analysis
8106 -- of the generic, this means that the component cannot
8107 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8109 if Nkind (Assoc) in N_Op_Compare
8110 and then Present (Etype (Left_Opnd (Assoc)))
8111 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8112 and then Present (Etype (Right_Opnd (Assoc)))
8113 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8114 then
8115 declare
8116 Ltyp : constant Entity_Id :=
8117 Etype (Left_Opnd (Assoc));
8118 Rtyp : constant Entity_Id :=
8119 Etype (Right_Opnd (Assoc));
8120 begin
8121 if Is_Private_Type (Component_Type (Ltyp)) then
8122 Check_Private_View
8123 (New_Occurrence_Of (Component_Type (Ltyp),
8124 Sloc (N)));
8125 end if;
8126 if Is_Private_Type (Component_Type (Rtyp)) then
8127 Check_Private_View
8128 (New_Occurrence_Of (Component_Type (Rtyp),
8129 Sloc (N)));
8130 end if;
8131 end;
8133 -- Here is a similar case, for the Designated_Type of an
8134 -- access type that is present as target type in a type
8135 -- conversion from another access type. In this case, if
8136 -- the base types of the designated types are different
8137 -- and the conversion was accepted during the semantic
8138 -- analysis of the generic, this means that the target
8139 -- type cannot have been private (see Valid_Conversion).
8141 elsif Nkind (Assoc) = N_Identifier
8142 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8143 and then Subtype_Mark (Parent (Assoc)) = Assoc
8144 and then Present (Etype (Assoc))
8145 and then Is_Access_Type (Etype (Assoc))
8146 and then Present (Etype (Expression (Parent (Assoc))))
8147 and then
8148 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8149 then
8150 declare
8151 Targ_Desig : constant Entity_Id :=
8152 Designated_Type (Etype (Assoc));
8153 Expr_Desig : constant Entity_Id :=
8154 Designated_Type
8155 (Etype (Expression (Parent (Assoc))));
8156 begin
8157 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8158 and then Is_Private_Type (Targ_Desig)
8159 then
8160 Check_Private_View
8161 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8162 end if;
8163 end;
8164 end if;
8166 -- The node is a reference to a global type and acts as the
8167 -- subtype mark of a qualified expression created in order
8168 -- to aid resolution of accidental overloading in instances.
8169 -- Since N is a reference to a type, the Associated_Node of
8170 -- N denotes an entity rather than another identifier. See
8171 -- Qualify_Universal_Operands for details.
8173 elsif Nkind (N) = N_Identifier
8174 and then Nkind (Parent (N)) = N_Qualified_Expression
8175 and then Subtype_Mark (Parent (N)) = N
8176 and then Is_Qualified_Universal_Literal (Parent (N))
8177 then
8178 Set_Entity (New_N, Assoc);
8180 -- Cope with the rewriting into expanded name that may have
8181 -- occurred in between, e.g. in Check_Generic_Child_Unit for
8182 -- generic renaming declarations.
8184 elsif Nkind (Assoc) = N_Expanded_Name then
8185 Rewrite (N, New_Copy_Tree (Assoc));
8186 Set_Associated_Node (N, Assoc);
8187 return Copy_Generic_Node (N, Parent_Id, Instantiating);
8189 -- The name in the call may be a selected component if the
8190 -- call has not been analyzed yet, as may be the case for
8191 -- pre/post conditions in a generic unit.
8193 elsif Nkind (Assoc) = N_Function_Call
8194 and then Is_Entity_Name (Name (Assoc))
8195 then
8196 Set_Entity (New_N, Entity (Name (Assoc)));
8197 Check_Private_View (N);
8199 elsif Nkind (Assoc) in N_Entity
8200 and then (Expander_Active
8201 or else (GNATprove_Mode
8202 and then not In_Spec_Expression
8203 and then not Inside_A_Generic))
8204 then
8205 -- Inlining case: we are copying a tree that contains
8206 -- global entities, which are preserved in the copy to be
8207 -- used for subsequent inlining.
8209 null;
8211 else
8212 Set_Entity (New_N, Empty);
8213 end if;
8214 end if;
8215 end;
8216 end if;
8218 -- For expanded name, we must copy the Prefix and Selector_Name
8220 if Nkind (N) = N_Expanded_Name then
8221 Set_Prefix
8222 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8224 Set_Selector_Name (New_N,
8225 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8227 -- For operators, copy the operands
8229 elsif Nkind (N) in N_Op then
8230 if Nkind (N) in N_Binary_Op then
8231 Set_Left_Opnd (New_N,
8232 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8233 end if;
8235 Set_Right_Opnd (New_N,
8236 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8237 end if;
8239 -- Establish a link between an entity from the generic template and the
8240 -- corresponding entity in the generic copy to be analyzed.
8242 elsif Nkind (N) in N_Entity then
8243 if not Instantiating then
8244 Set_Associated_Entity (N, New_N);
8245 end if;
8247 -- Clear any existing link the copy may inherit from the replicated
8248 -- generic template entity.
8250 Set_Associated_Entity (New_N, Empty);
8252 -- Special casing for stubs
8254 elsif Nkind (N) in N_Body_Stub then
8256 -- In any case, we must copy the specification or defining
8257 -- identifier as appropriate.
8259 if Nkind (N) = N_Subprogram_Body_Stub then
8260 Set_Specification (New_N,
8261 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8263 else
8264 Set_Defining_Identifier (New_N,
8265 Copy_Generic_Node
8266 (Defining_Identifier (N), New_N, Instantiating));
8267 end if;
8269 -- If we are not instantiating, then this is where we load and
8270 -- analyze subunits, i.e. at the point where the stub occurs. A
8271 -- more permissive system might defer this analysis to the point
8272 -- of instantiation, but this seems too complicated for now.
8274 if not Instantiating then
8275 declare
8276 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8277 Subunit : Node_Id;
8278 Unum : Unit_Number_Type;
8279 New_Body : Node_Id;
8281 begin
8282 -- Make sure that, if it is a subunit of the main unit that is
8283 -- preprocessed and if -gnateG is specified, the preprocessed
8284 -- file will be written.
8286 Lib.Analysing_Subunit_Of_Main :=
8287 Lib.In_Extended_Main_Source_Unit (N);
8288 Unum :=
8289 Load_Unit
8290 (Load_Name => Subunit_Name,
8291 Required => False,
8292 Subunit => True,
8293 Error_Node => N);
8294 Lib.Analysing_Subunit_Of_Main := False;
8296 -- If the proper body is not found, a warning message will be
8297 -- emitted when analyzing the stub, or later at the point of
8298 -- instantiation. Here we just leave the stub as is.
8300 if Unum = No_Unit then
8301 Subunits_Missing := True;
8302 goto Subunit_Not_Found;
8303 end if;
8305 Subunit := Cunit (Unum);
8307 if Nkind (Unit (Subunit)) /= N_Subunit then
8308 Error_Msg_N
8309 ("found child unit instead of expected SEPARATE subunit",
8310 Subunit);
8311 Error_Msg_Sloc := Sloc (N);
8312 Error_Msg_N ("\to complete stub #", Subunit);
8313 goto Subunit_Not_Found;
8314 end if;
8316 -- We must create a generic copy of the subunit, in order to
8317 -- perform semantic analysis on it, and we must replace the
8318 -- stub in the original generic unit with the subunit, in order
8319 -- to preserve non-local references within.
8321 -- Only the proper body needs to be copied. Library_Unit and
8322 -- context clause are simply inherited by the generic copy.
8323 -- Note that the copy (which may be recursive if there are
8324 -- nested subunits) must be done first, before attaching it to
8325 -- the enclosing generic.
8327 New_Body :=
8328 Copy_Generic_Node
8329 (Proper_Body (Unit (Subunit)),
8330 Empty, Instantiating => False);
8332 -- Now place the original proper body in the original generic
8333 -- unit. This is a body, not a compilation unit.
8335 Rewrite (N, Proper_Body (Unit (Subunit)));
8336 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8337 Set_Was_Originally_Stub (N);
8339 -- Finally replace the body of the subunit with its copy, and
8340 -- make this new subunit into the library unit of the generic
8341 -- copy, which does not have stubs any longer.
8343 Set_Proper_Body (Unit (Subunit), New_Body);
8344 Set_Library_Unit (New_N, Subunit);
8345 Inherit_Context (Unit (Subunit), N);
8346 end;
8348 -- If we are instantiating, this must be an error case, since
8349 -- otherwise we would have replaced the stub node by the proper body
8350 -- that corresponds. So just ignore it in the copy (i.e. we have
8351 -- copied it, and that is good enough).
8353 else
8354 null;
8355 end if;
8357 <<Subunit_Not_Found>> null;
8359 -- If the node is a compilation unit, it is the subunit of a stub, which
8360 -- has been loaded already (see code below). In this case, the library
8361 -- unit field of N points to the parent unit (which is a compilation
8362 -- unit) and need not (and cannot) be copied.
8364 -- When the proper body of the stub is analyzed, the library_unit link
8365 -- is used to establish the proper context (see sem_ch10).
8367 -- The other fields of a compilation unit are copied as usual
8369 elsif Nkind (N) = N_Compilation_Unit then
8371 -- This code can only be executed when not instantiating, because in
8372 -- the copy made for an instantiation, the compilation unit node has
8373 -- disappeared at the point that a stub is replaced by its proper
8374 -- body.
8376 pragma Assert (not Instantiating);
8378 Set_Context_Items (New_N,
8379 Copy_Generic_List (Context_Items (N), New_N));
8381 Set_Unit (New_N,
8382 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8384 Set_First_Inlined_Subprogram (New_N,
8385 Copy_Generic_Node
8386 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8388 Set_Aux_Decls_Node
8389 (New_N,
8390 Copy_Generic_Node
8391 (Aux_Decls_Node (N), New_N, Instantiating => False));
8393 -- For an assignment node, the assignment is known to be semantically
8394 -- legal if we are instantiating the template. This avoids incorrect
8395 -- diagnostics in generated code.
8397 elsif Nkind (N) = N_Assignment_Statement then
8399 -- Copy name and expression fields in usual manner
8401 Set_Name (New_N,
8402 Copy_Generic_Node (Name (N), New_N, Instantiating));
8404 Set_Expression (New_N,
8405 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8407 if Instantiating then
8408 Set_Assignment_OK (Name (New_N), True);
8409 end if;
8411 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8412 if not Instantiating then
8413 Set_Associated_Node (N, New_N);
8415 else
8416 if Present (Get_Associated_Node (N))
8417 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8418 then
8419 -- In the generic the aggregate has some composite type. If at
8420 -- the point of instantiation the type has a private view,
8421 -- install the full view (and that of its ancestors, if any).
8423 declare
8424 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8425 Rt : Entity_Id;
8427 begin
8428 if Present (T) and then Is_Private_Type (T) then
8429 Switch_View (T);
8430 end if;
8432 if Present (T)
8433 and then Is_Tagged_Type (T)
8434 and then Is_Derived_Type (T)
8435 then
8436 Rt := Root_Type (T);
8438 loop
8439 T := Etype (T);
8441 if Is_Private_Type (T) then
8442 Switch_View (T);
8443 end if;
8445 exit when T = Rt;
8446 end loop;
8447 end if;
8448 end;
8449 end if;
8450 end if;
8452 -- Do not copy the associated node, which points to the generic copy
8453 -- of the aggregate.
8455 if Nkind (N) = N_Aggregate then
8456 Set_Aggregate_Bounds
8457 (New_N,
8458 Node_Id (Copy_Generic_Descendant
8459 (Union_Id (Aggregate_Bounds (N)))));
8461 elsif Nkind (N) = N_Extension_Aggregate then
8462 Set_Ancestor_Part
8463 (New_N,
8464 Node_Id (Copy_Generic_Descendant
8465 (Union_Id (Ancestor_Part (N)))));
8467 else
8468 pragma Assert (False);
8469 end if;
8471 Set_Expressions
8472 (New_N,
8473 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
8474 Set_Component_Associations
8475 (New_N,
8476 List_Id (Copy_Generic_Descendant
8477 (Union_Id (Component_Associations (N)))));
8478 Set_Etype
8479 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
8481 -- Allocators do not have an identifier denoting the access type, so we
8482 -- must locate it through the expression to check whether the views are
8483 -- consistent.
8485 elsif Nkind (N) = N_Allocator
8486 and then Nkind (Expression (N)) = N_Qualified_Expression
8487 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8488 and then Instantiating
8489 then
8490 declare
8491 T : constant Node_Id :=
8492 Get_Associated_Node (Subtype_Mark (Expression (N)));
8493 Acc_T : Entity_Id;
8495 begin
8496 if Present (T) then
8498 -- Retrieve the allocator node in the generic copy
8500 Acc_T := Etype (Parent (Parent (T)));
8502 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8503 Switch_View (Acc_T);
8504 end if;
8505 end if;
8507 Copy_Descendants;
8508 end;
8510 -- For a proper body, we must catch the case of a proper body that
8511 -- replaces a stub. This represents the point at which a separate
8512 -- compilation unit, and hence template file, may be referenced, so we
8513 -- must make a new source instantiation entry for the template of the
8514 -- subunit, and ensure that all nodes in the subunit are adjusted using
8515 -- this new source instantiation entry.
8517 elsif Nkind (N) in N_Proper_Body then
8518 declare
8519 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8520 begin
8521 if Instantiating and then Was_Originally_Stub (N) then
8522 Create_Instantiation_Source
8523 (Instantiation_Node,
8524 Defining_Entity (N),
8525 S_Adjustment);
8527 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8528 end if;
8530 -- Now copy the fields of the proper body, using the new
8531 -- adjustment factor if one was needed as per test above.
8533 Copy_Descendants;
8535 -- Restore the original adjustment factor
8537 S_Adjustment := Save_Adjustment;
8538 end;
8540 elsif Nkind (N) = N_Pragma and then Instantiating then
8542 -- Do not copy Comment or Ident pragmas their content is relevant to
8543 -- the generic unit, not to the instantiating unit.
8545 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8546 New_N := Make_Null_Statement (Sloc (N));
8548 -- Do not copy pragmas generated from aspects because the pragmas do
8549 -- not carry any semantic information, plus they will be regenerated
8550 -- in the instance.
8552 -- However, generating C we need to copy them since postconditions
8553 -- are inlined by the front end, and the front-end inlining machinery
8554 -- relies on this routine to perform inlining.
8556 elsif From_Aspect_Specification (N)
8557 and then not Modify_Tree_For_C
8558 then
8559 New_N := Make_Null_Statement (Sloc (N));
8561 else
8562 Copy_Descendants;
8563 end if;
8565 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8567 -- No descendant fields need traversing
8569 null;
8571 elsif Nkind (N) = N_String_Literal
8572 and then Present (Etype (N))
8573 and then Instantiating
8574 then
8575 -- If the string is declared in an outer scope, the string_literal
8576 -- subtype created for it may have the wrong scope. Force reanalysis
8577 -- of the constant to generate a new itype in the proper context.
8579 Set_Etype (New_N, Empty);
8580 Set_Analyzed (New_N, False);
8582 -- For the remaining nodes, copy their descendants recursively
8584 else
8585 Copy_Descendants;
8587 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8588 Set_Generic_Parent (Specification (New_N), N);
8590 -- Should preserve Corresponding_Spec??? (12.3(14))
8591 end if;
8592 end if;
8594 -- Propagate dimensions if present, so that they are reflected in the
8595 -- instance.
8597 if Nkind (N) in N_Has_Etype
8598 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8599 and then Present (Etype (N))
8600 and then Is_Floating_Point_Type (Etype (N))
8601 and then Has_Dimension_System (Etype (N))
8602 then
8603 Copy_Dimensions (N, New_N);
8604 end if;
8606 return New_N;
8607 end Copy_Generic_Node;
8609 ----------------------------
8610 -- Denotes_Formal_Package --
8611 ----------------------------
8613 function Denotes_Formal_Package
8614 (Pack : Entity_Id;
8615 On_Exit : Boolean := False;
8616 Instance : Entity_Id := Empty) return Boolean
8618 Par : Entity_Id;
8619 Scop : constant Entity_Id := Scope (Pack);
8620 E : Entity_Id;
8622 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8623 -- The package in question may be an actual for a previous formal
8624 -- package P of the current instance, so examine its actuals as well.
8625 -- This must be recursive over other formal packages.
8627 ----------------------------------
8628 -- Is_Actual_Of_Previous_Formal --
8629 ----------------------------------
8631 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8632 E1 : Entity_Id;
8634 begin
8635 E1 := First_Entity (P);
8636 while Present (E1) and then E1 /= Instance loop
8637 if Ekind (E1) = E_Package
8638 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8639 then
8640 if Renamed_Entity (E1) = Pack then
8641 return True;
8643 elsif E1 = P or else Renamed_Entity (E1) = P then
8644 return False;
8646 elsif Is_Actual_Of_Previous_Formal (E1) then
8647 return True;
8648 end if;
8649 end if;
8651 Next_Entity (E1);
8652 end loop;
8654 return False;
8655 end Is_Actual_Of_Previous_Formal;
8657 -- Start of processing for Denotes_Formal_Package
8659 begin
8660 if On_Exit then
8661 Par :=
8662 Instance_Envs.Table
8663 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8664 else
8665 Par := Current_Instantiated_Parent.Act_Id;
8666 end if;
8668 if Ekind (Scop) = E_Generic_Package
8669 or else Nkind (Unit_Declaration_Node (Scop)) =
8670 N_Generic_Subprogram_Declaration
8671 then
8672 return True;
8674 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8675 N_Formal_Package_Declaration
8676 then
8677 return True;
8679 elsif No (Par) then
8680 return False;
8682 else
8683 -- Check whether this package is associated with a formal package of
8684 -- the enclosing instantiation. Iterate over the list of renamings.
8686 E := First_Entity (Par);
8687 while Present (E) loop
8688 if Ekind (E) /= E_Package
8689 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8690 then
8691 null;
8693 elsif Renamed_Entity (E) = Par then
8694 return False;
8696 elsif Renamed_Entity (E) = Pack then
8697 return True;
8699 elsif Is_Actual_Of_Previous_Formal (E) then
8700 return True;
8702 end if;
8704 Next_Entity (E);
8705 end loop;
8707 return False;
8708 end if;
8709 end Denotes_Formal_Package;
8711 -----------------
8712 -- End_Generic --
8713 -----------------
8715 procedure End_Generic is
8716 begin
8717 -- ??? More things could be factored out in this routine. Should
8718 -- probably be done at a later stage.
8720 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8721 Generic_Flags.Decrement_Last;
8723 Expander_Mode_Restore;
8724 end End_Generic;
8726 -------------
8727 -- Earlier --
8728 -------------
8730 function Earlier (N1, N2 : Node_Id) return Boolean is
8731 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8732 -- Find distance from given node to enclosing compilation unit
8734 ----------------
8735 -- Find_Depth --
8736 ----------------
8738 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8739 begin
8740 while Present (P)
8741 and then Nkind (P) /= N_Compilation_Unit
8742 loop
8743 P := True_Parent (P);
8744 D := D + 1;
8745 end loop;
8746 end Find_Depth;
8748 -- Local declarations
8750 D1 : Integer := 0;
8751 D2 : Integer := 0;
8752 P1 : Node_Id := N1;
8753 P2 : Node_Id := N2;
8754 T1 : Source_Ptr;
8755 T2 : Source_Ptr;
8757 -- Start of processing for Earlier
8759 begin
8760 Find_Depth (P1, D1);
8761 Find_Depth (P2, D2);
8763 if P1 /= P2 then
8764 return False;
8765 else
8766 P1 := N1;
8767 P2 := N2;
8768 end if;
8770 while D1 > D2 loop
8771 P1 := True_Parent (P1);
8772 D1 := D1 - 1;
8773 end loop;
8775 while D2 > D1 loop
8776 P2 := True_Parent (P2);
8777 D2 := D2 - 1;
8778 end loop;
8780 -- At this point P1 and P2 are at the same distance from the root.
8781 -- We examine their parents until we find a common declarative list.
8782 -- If we reach the root, N1 and N2 do not descend from the same
8783 -- declarative list (e.g. one is nested in the declarative part and
8784 -- the other is in a block in the statement part) and the earlier
8785 -- one is already frozen.
8787 while not Is_List_Member (P1)
8788 or else not Is_List_Member (P2)
8789 or else not In_Same_List (P1, P2)
8790 loop
8791 P1 := True_Parent (P1);
8792 P2 := True_Parent (P2);
8794 if Nkind (Parent (P1)) = N_Subunit then
8795 P1 := Corresponding_Stub (Parent (P1));
8796 end if;
8798 if Nkind (Parent (P2)) = N_Subunit then
8799 P2 := Corresponding_Stub (Parent (P2));
8800 end if;
8802 if P1 = P2 then
8803 return False;
8804 end if;
8805 end loop;
8807 -- Expanded code usually shares the source location of the original
8808 -- construct it was generated for. This however may not necessarily
8809 -- reflect the true location of the code within the tree.
8811 -- Before comparing the slocs of the two nodes, make sure that we are
8812 -- working with correct source locations. Assume that P1 is to the left
8813 -- of P2. If either one does not come from source, traverse the common
8814 -- list heading towards the other node and locate the first source
8815 -- statement.
8817 -- P1 P2
8818 -- ----+===+===+--------------+===+===+----
8819 -- expanded code expanded code
8821 if not Comes_From_Source (P1) then
8822 while Present (P1) loop
8824 -- Neither P2 nor a source statement were located during the
8825 -- search. If we reach the end of the list, then P1 does not
8826 -- occur earlier than P2.
8828 -- ---->
8829 -- start --- P2 ----- P1 --- end
8831 if No (Next (P1)) then
8832 return False;
8834 -- We encounter P2 while going to the right of the list. This
8835 -- means that P1 does indeed appear earlier.
8837 -- ---->
8838 -- start --- P1 ===== P2 --- end
8839 -- expanded code in between
8841 elsif P1 = P2 then
8842 return True;
8844 -- No need to look any further since we have located a source
8845 -- statement.
8847 elsif Comes_From_Source (P1) then
8848 exit;
8849 end if;
8851 -- Keep going right
8853 Next (P1);
8854 end loop;
8855 end if;
8857 if not Comes_From_Source (P2) then
8858 while Present (P2) loop
8860 -- Neither P1 nor a source statement were located during the
8861 -- search. If we reach the start of the list, then P1 does not
8862 -- occur earlier than P2.
8864 -- <----
8865 -- start --- P2 --- P1 --- end
8867 if No (Prev (P2)) then
8868 return False;
8870 -- We encounter P1 while going to the left of the list. This
8871 -- means that P1 does indeed appear earlier.
8873 -- <----
8874 -- start --- P1 ===== P2 --- end
8875 -- expanded code in between
8877 elsif P2 = P1 then
8878 return True;
8880 -- No need to look any further since we have located a source
8881 -- statement.
8883 elsif Comes_From_Source (P2) then
8884 exit;
8885 end if;
8887 -- Keep going left
8889 Prev (P2);
8890 end loop;
8891 end if;
8893 -- At this point either both nodes came from source or we approximated
8894 -- their source locations through neighboring source statements.
8896 T1 := Top_Level_Location (Sloc (P1));
8897 T2 := Top_Level_Location (Sloc (P2));
8899 -- When two nodes come from the same instance, they have identical top
8900 -- level locations. To determine proper relation within the tree, check
8901 -- their locations within the template.
8903 if T1 = T2 then
8904 return Sloc (P1) < Sloc (P2);
8906 -- The two nodes either come from unrelated instances or do not come
8907 -- from instantiated code at all.
8909 else
8910 return T1 < T2;
8911 end if;
8912 end Earlier;
8914 ----------------------
8915 -- Find_Actual_Type --
8916 ----------------------
8918 function Find_Actual_Type
8919 (Typ : Entity_Id;
8920 Gen_Type : Entity_Id) return Entity_Id
8922 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8923 T : Entity_Id;
8925 begin
8926 -- Special processing only applies to child units
8928 if not Is_Child_Unit (Gen_Scope) then
8929 return Get_Instance_Of (Typ);
8931 -- If designated or component type is itself a formal of the child unit,
8932 -- its instance is available.
8934 elsif Scope (Typ) = Gen_Scope then
8935 return Get_Instance_Of (Typ);
8937 -- If the array or access type is not declared in the parent unit,
8938 -- no special processing needed.
8940 elsif not Is_Generic_Type (Typ)
8941 and then Scope (Gen_Scope) /= Scope (Typ)
8942 then
8943 return Get_Instance_Of (Typ);
8945 -- Otherwise, retrieve designated or component type by visibility
8947 else
8948 T := Current_Entity (Typ);
8949 while Present (T) loop
8950 if In_Open_Scopes (Scope (T)) then
8951 return T;
8952 elsif Is_Generic_Actual_Type (T) then
8953 return T;
8954 end if;
8956 T := Homonym (T);
8957 end loop;
8959 return Typ;
8960 end if;
8961 end Find_Actual_Type;
8963 -----------------------------
8964 -- Freeze_Package_Instance --
8965 -----------------------------
8967 procedure Freeze_Package_Instance
8968 (N : Node_Id;
8969 Gen_Body : Node_Id;
8970 Gen_Decl : Node_Id;
8971 Act_Id : Entity_Id)
8973 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
8974 -- Check if the generic definition and the instantiation come from
8975 -- a common scope, in which case the instance must be frozen after
8976 -- the generic body.
8978 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
8979 -- If the instance is nested inside a generic unit, the Sloc of the
8980 -- instance indicates the place of the original definition, not the
8981 -- point of the current enclosing instance. Pending a better usage of
8982 -- Slocs to indicate instantiation places, we determine the place of
8983 -- origin of a node by finding the maximum sloc of any ancestor node.
8985 -- Why is this not equivalent to Top_Level_Location ???
8987 -------------------
8988 -- In_Same_Scope --
8989 -------------------
8991 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
8992 Act_Scop : Entity_Id := Scope (Act_Id);
8993 Gen_Scop : Entity_Id := Scope (Gen_Id);
8995 begin
8996 while Act_Scop /= Standard_Standard
8997 and then Gen_Scop /= Standard_Standard
8998 loop
8999 if Act_Scop = Gen_Scop then
9000 return True;
9001 end if;
9003 Act_Scop := Scope (Act_Scop);
9004 Gen_Scop := Scope (Gen_Scop);
9005 end loop;
9007 return False;
9008 end In_Same_Scope;
9010 ---------------
9011 -- True_Sloc --
9012 ---------------
9014 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9015 N1 : Node_Id;
9016 Res : Source_Ptr;
9018 begin
9019 Res := Sloc (N);
9020 N1 := N;
9021 while Present (N1) and then N1 /= Act_Unit loop
9022 if Sloc (N1) > Res then
9023 Res := Sloc (N1);
9024 end if;
9026 N1 := Parent (N1);
9027 end loop;
9029 return Res;
9030 end True_Sloc;
9032 -- Local variables
9034 Gen_Id : constant Entity_Id := Get_Generic_Entity (N);
9035 Par_Id : constant Entity_Id := Scope (Gen_Id);
9036 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9037 Gen_Unit : constant Node_Id :=
9038 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9040 Body_Unit : Node_Id;
9041 F_Node : Node_Id;
9042 Must_Delay : Boolean;
9043 Orig_Body : Node_Id;
9045 -- Start of processing for Freeze_Package_Instance
9047 begin
9048 -- If the body is a subunit, the freeze point is the corresponding stub
9049 -- in the current compilation, not the subunit itself.
9051 if Nkind (Parent (Gen_Body)) = N_Subunit then
9052 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9053 else
9054 Orig_Body := Gen_Body;
9055 end if;
9057 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9059 -- If the instantiation and the generic definition appear in the same
9060 -- package declaration, this is an early instantiation. If they appear
9061 -- in the same declarative part, it is an early instantiation only if
9062 -- the generic body appears textually later, and the generic body is
9063 -- also in the main unit.
9065 -- If instance is nested within a subprogram, and the generic body
9066 -- is not, the instance is delayed because the enclosing body is. If
9067 -- instance and body are within the same scope, or the same subprogram
9068 -- body, indicate explicitly that the instance is delayed.
9070 Must_Delay :=
9071 (Gen_Unit = Act_Unit
9072 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9073 | N_Package_Declaration
9074 or else (Gen_Unit = Body_Unit
9075 and then
9076 True_Sloc (N, Act_Unit) < Sloc (Orig_Body)))
9077 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9078 and then In_Same_Scope (Gen_Id, Act_Id));
9080 -- If this is an early instantiation, the freeze node is placed after
9081 -- the generic body. Otherwise, if the generic appears in an instance,
9082 -- we cannot freeze the current instance until the outer one is frozen.
9083 -- This is only relevant if the current instance is nested within some
9084 -- inner scope not itself within the outer instance. If this scope is
9085 -- a package body in the same declarative part as the outer instance,
9086 -- then that body needs to be frozen after the outer instance. Finally,
9087 -- if no delay is needed, we place the freeze node at the end of the
9088 -- current declarative part.
9090 if No (Freeze_Node (Act_Id))
9091 or else not Is_List_Member (Freeze_Node (Act_Id))
9092 then
9093 Ensure_Freeze_Node (Act_Id);
9094 F_Node := Freeze_Node (Act_Id);
9096 if Must_Delay then
9097 Insert_After (Orig_Body, F_Node);
9099 elsif Is_Generic_Instance (Par_Id)
9100 and then Present (Freeze_Node (Par_Id))
9101 and then Scope (Act_Id) /= Par_Id
9102 then
9103 -- Freeze instance of inner generic after instance of enclosing
9104 -- generic.
9106 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N) then
9108 -- Handle the following case:
9110 -- package Parent_Inst is new ...
9111 -- freeze Parent_Inst []
9113 -- procedure P ... -- this body freezes Parent_Inst
9115 -- package Inst is new ...
9117 -- In this particular scenario, the freeze node for Inst must
9118 -- be inserted in the same manner as that of Parent_Inst,
9119 -- before the next source body or at the end of the declarative
9120 -- list (body not available). If body P did not exist and
9121 -- Parent_Inst was frozen after Inst, either by a body
9122 -- following Inst or at the end of the declarative region,
9123 -- the freeze node for Inst must be inserted after that of
9124 -- Parent_Inst. This relation is established by comparing
9125 -- the Slocs of Parent_Inst freeze node and Inst.
9126 -- We examine the parents of the enclosing lists to handle
9127 -- the case where the parent instance is in the visible part
9128 -- of a package declaration, and the inner instance is in
9129 -- the corresponding private part.
9131 if Parent (List_Containing (Freeze_Node (Par_Id)))
9132 = Parent (List_Containing (N))
9133 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9134 then
9135 Insert_Freeze_Node_For_Instance (N, F_Node);
9136 else
9137 Insert_After (Freeze_Node (Par_Id), F_Node);
9138 end if;
9140 -- Freeze package enclosing instance of inner generic after
9141 -- instance of enclosing generic.
9143 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9144 and then In_Same_Declarative_Part
9145 (Parent (Freeze_Node (Par_Id)), Parent (N))
9146 then
9147 declare
9148 Enclosing : Entity_Id;
9150 begin
9151 Enclosing := Corresponding_Spec (Parent (N));
9153 if No (Enclosing) then
9154 Enclosing := Defining_Entity (Parent (N));
9155 end if;
9157 Insert_Freeze_Node_For_Instance (N, F_Node);
9158 Ensure_Freeze_Node (Enclosing);
9160 if not Is_List_Member (Freeze_Node (Enclosing)) then
9162 -- The enclosing context is a subunit, insert the freeze
9163 -- node after the stub.
9165 if Nkind (Parent (Parent (N))) = N_Subunit then
9166 Insert_Freeze_Node_For_Instance
9167 (Corresponding_Stub (Parent (Parent (N))),
9168 Freeze_Node (Enclosing));
9170 -- The enclosing context is a package with a stub body
9171 -- which has already been replaced by the real body.
9172 -- Insert the freeze node after the actual body.
9174 elsif Ekind (Enclosing) = E_Package
9175 and then Present (Body_Entity (Enclosing))
9176 and then Was_Originally_Stub
9177 (Parent (Body_Entity (Enclosing)))
9178 then
9179 Insert_Freeze_Node_For_Instance
9180 (Parent (Body_Entity (Enclosing)),
9181 Freeze_Node (Enclosing));
9183 -- The parent instance has been frozen before the body of
9184 -- the enclosing package, insert the freeze node after
9185 -- the body.
9187 elsif In_Same_List (Freeze_Node (Par_Id), Parent (N))
9188 and then
9189 Sloc (Freeze_Node (Par_Id)) <= Sloc (Parent (N))
9190 then
9191 Insert_Freeze_Node_For_Instance
9192 (Parent (N), Freeze_Node (Enclosing));
9194 else
9195 Insert_After
9196 (Freeze_Node (Par_Id), Freeze_Node (Enclosing));
9197 end if;
9198 end if;
9199 end;
9201 else
9202 Insert_Freeze_Node_For_Instance (N, F_Node);
9203 end if;
9205 else
9206 Insert_Freeze_Node_For_Instance (N, F_Node);
9207 end if;
9208 end if;
9209 end Freeze_Package_Instance;
9211 --------------------------------
9212 -- Freeze_Subprogram_Instance --
9213 --------------------------------
9215 procedure Freeze_Subprogram_Instance
9216 (N : Node_Id;
9217 Gen_Body : Node_Id;
9218 Pack_Id : Entity_Id)
9220 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
9221 -- Find innermost package body that encloses the given node, and which
9222 -- is not a compilation unit. Freeze nodes for the instance, or for its
9223 -- enclosing body, may be inserted after the enclosing_body of the
9224 -- generic unit. Used to determine proper placement of freeze node for
9225 -- both package and subprogram instances.
9227 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9228 -- Find entity for given package body, and locate or create a freeze
9229 -- node for it.
9231 ----------------------------
9232 -- Enclosing_Package_Body --
9233 ----------------------------
9235 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9236 P : Node_Id;
9238 begin
9239 P := Parent (N);
9240 while Present (P)
9241 and then Nkind (Parent (P)) /= N_Compilation_Unit
9242 loop
9243 if Nkind (P) = N_Package_Body then
9244 if Nkind (Parent (P)) = N_Subunit then
9245 return Corresponding_Stub (Parent (P));
9246 else
9247 return P;
9248 end if;
9249 end if;
9251 P := True_Parent (P);
9252 end loop;
9254 return Empty;
9255 end Enclosing_Package_Body;
9257 -------------------------
9258 -- Package_Freeze_Node --
9259 -------------------------
9261 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9262 Id : Entity_Id;
9264 begin
9265 if Nkind (B) = N_Package_Body then
9266 Id := Corresponding_Spec (B);
9267 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9268 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9269 end if;
9271 Ensure_Freeze_Node (Id);
9272 return Freeze_Node (Id);
9273 end Package_Freeze_Node;
9275 -- Local variables
9277 Enc_G : constant Node_Id := Enclosing_Package_Body (Gen_Body);
9278 Enc_N : constant Node_Id := Enclosing_Package_Body (N);
9279 Par_Id : constant Entity_Id := Scope (Get_Generic_Entity (N));
9281 Enc_G_F : Node_Id;
9282 F_Node : Node_Id;
9284 -- Start of processing for Freeze_Subprogram_Instance
9286 begin
9287 -- If the instance and the generic body appear within the same unit, and
9288 -- the instance precedes the generic, the freeze node for the instance
9289 -- must appear after that of the generic. If the generic is nested
9290 -- within another instance I2, then current instance must be frozen
9291 -- after I2. In both cases, the freeze nodes are those of enclosing
9292 -- packages. Otherwise, the freeze node is placed at the end of the
9293 -- current declarative part.
9295 Ensure_Freeze_Node (Pack_Id);
9296 F_Node := Freeze_Node (Pack_Id);
9298 if Is_Generic_Instance (Par_Id)
9299 and then Present (Freeze_Node (Par_Id))
9300 and then In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N)
9301 then
9302 -- The parent was a premature instantiation. Insert freeze node at
9303 -- the end the current declarative part.
9305 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par_Id)) then
9306 Insert_Freeze_Node_For_Instance (N, F_Node);
9308 -- Handle the following case:
9310 -- package Parent_Inst is new ...
9311 -- freeze Parent_Inst []
9313 -- procedure P ... -- this body freezes Parent_Inst
9315 -- procedure Inst is new ...
9317 -- In this particular scenario, the freeze node for Inst must be
9318 -- inserted in the same manner as that of Parent_Inst - before the
9319 -- next source body or at the end of the declarative list (body not
9320 -- available). If body P did not exist and Parent_Inst was frozen
9321 -- after Inst, either by a body following Inst or at the end of the
9322 -- declarative region, the freeze node for Inst must be inserted
9323 -- after that of Parent_Inst. This relation is established by
9324 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9326 elsif In_Same_List (Freeze_Node (Par_Id), N)
9327 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9328 then
9329 Insert_Freeze_Node_For_Instance (N, F_Node);
9331 else
9332 Insert_After (Freeze_Node (Par_Id), F_Node);
9333 end if;
9335 -- The body enclosing the instance should be frozen after the body that
9336 -- includes the generic, because the body of the instance may make
9337 -- references to entities therein. If the two are not in the same
9338 -- declarative part, or if the one enclosing the instance is frozen
9339 -- already, freeze the instance at the end of the current declarative
9340 -- part.
9342 elsif Is_Generic_Instance (Par_Id)
9343 and then Present (Freeze_Node (Par_Id))
9344 and then Present (Enc_N)
9345 then
9346 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), Enc_N)
9347 then
9348 -- The enclosing package may contain several instances. Rather
9349 -- than computing the earliest point at which to insert its freeze
9350 -- node, we place it at the end of the declarative part of the
9351 -- parent of the generic.
9353 Insert_Freeze_Node_For_Instance
9354 (Freeze_Node (Par_Id), Package_Freeze_Node (Enc_N));
9355 end if;
9357 Insert_Freeze_Node_For_Instance (N, F_Node);
9359 elsif Present (Enc_G)
9360 and then Present (Enc_N)
9361 and then Enc_G /= Enc_N
9362 and then Earlier (N, Gen_Body)
9363 then
9364 -- Freeze package that encloses instance, and place node after the
9365 -- package that encloses generic. If enclosing package is already
9366 -- frozen we have to assume it is at the proper place. This may be a
9367 -- potential ABE that requires dynamic checking. Do not add a freeze
9368 -- node if the package that encloses the generic is inside the body
9369 -- that encloses the instance, because the freeze node would be in
9370 -- the wrong scope. Additional contortions needed if the bodies are
9371 -- within a subunit.
9373 declare
9374 Enclosing_Body : Node_Id;
9376 begin
9377 if Nkind (Enc_N) = N_Package_Body_Stub then
9378 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_N)));
9379 else
9380 Enclosing_Body := Enc_N;
9381 end if;
9383 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9384 Insert_Freeze_Node_For_Instance
9385 (Enc_G, Package_Freeze_Node (Enc_N));
9386 end if;
9387 end;
9389 -- Freeze enclosing subunit before instance
9391 Enc_G_F := Package_Freeze_Node (Enc_G);
9393 if not Is_List_Member (Enc_G_F) then
9394 Insert_After (Enc_G, Enc_G_F);
9395 end if;
9397 Insert_Freeze_Node_For_Instance (N, F_Node);
9399 else
9400 -- If none of the above, insert freeze node at the end of the current
9401 -- declarative part.
9403 Insert_Freeze_Node_For_Instance (N, F_Node);
9404 end if;
9405 end Freeze_Subprogram_Instance;
9407 ----------------
9408 -- Get_Gen_Id --
9409 ----------------
9411 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9412 begin
9413 return Generic_Renamings.Table (E).Gen_Id;
9414 end Get_Gen_Id;
9416 ---------------------
9417 -- Get_Instance_Of --
9418 ---------------------
9420 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9421 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9423 begin
9424 if Res /= Assoc_Null then
9425 return Generic_Renamings.Table (Res).Act_Id;
9427 else
9428 -- On exit, entity is not instantiated: not a generic parameter, or
9429 -- else parameter of an inner generic unit.
9431 return A;
9432 end if;
9433 end Get_Instance_Of;
9435 ---------------------------------
9436 -- Get_Unit_Instantiation_Node --
9437 ---------------------------------
9439 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9440 Decl : Node_Id := Unit_Declaration_Node (A);
9441 Inst : Node_Id;
9443 begin
9444 -- If the Package_Instantiation attribute has been set on the package
9445 -- entity, then use it directly when it (or its Original_Node) refers
9446 -- to an N_Package_Instantiation node. In principle it should be
9447 -- possible to have this field set in all cases, which should be
9448 -- investigated, and would allow this function to be significantly
9449 -- simplified. ???
9451 Inst := Package_Instantiation (A);
9453 if Present (Inst) then
9454 if Nkind (Inst) = N_Package_Instantiation then
9455 return Inst;
9457 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9458 return Original_Node (Inst);
9459 end if;
9460 end if;
9462 -- If the instantiation is a compilation unit that does not need body
9463 -- then the instantiation node has been rewritten as a package
9464 -- declaration for the instance, and we return the original node.
9466 -- If it is a compilation unit and the instance node has not been
9467 -- rewritten, then it is still the unit of the compilation. Finally, if
9468 -- a body is present, this is a parent of the main unit whose body has
9469 -- been compiled for inlining purposes, and the instantiation node has
9470 -- been rewritten with the instance body.
9472 -- Otherwise the instantiation node appears after the declaration. If
9473 -- the entity is a formal package, the declaration may have been
9474 -- rewritten as a generic declaration (in the case of a formal with box)
9475 -- or left as a formal package declaration if it has actuals, and is
9476 -- found with a forward search.
9478 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9479 if Nkind (Decl) = N_Package_Declaration
9480 and then Present (Corresponding_Body (Decl))
9481 then
9482 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9483 end if;
9485 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9486 return Original_Node (Decl);
9487 else
9488 return Unit (Parent (Decl));
9489 end if;
9491 elsif Nkind (Decl) = N_Package_Declaration
9492 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9493 then
9494 return Original_Node (Decl);
9496 else
9497 Inst := Next (Decl);
9498 while Nkind (Inst) not in N_Formal_Package_Declaration
9499 | N_Function_Instantiation
9500 | N_Package_Instantiation
9501 | N_Procedure_Instantiation
9502 loop
9503 Next (Inst);
9504 end loop;
9506 return Inst;
9507 end if;
9508 end Get_Unit_Instantiation_Node;
9510 ------------------------
9511 -- Has_Been_Exchanged --
9512 ------------------------
9514 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9515 Next : Elmt_Id;
9517 begin
9518 Next := First_Elmt (Exchanged_Views);
9519 while Present (Next) loop
9520 if Full_View (Node (Next)) = E then
9521 return True;
9522 end if;
9524 Next_Elmt (Next);
9525 end loop;
9527 return False;
9528 end Has_Been_Exchanged;
9530 -------------------
9531 -- Has_Contracts --
9532 -------------------
9534 function Has_Contracts (Decl : Node_Id) return Boolean is
9535 A_List : constant List_Id := Aspect_Specifications (Decl);
9536 A_Spec : Node_Id;
9537 A_Id : Aspect_Id;
9538 begin
9539 if No (A_List) then
9540 return False;
9541 else
9542 A_Spec := First (A_List);
9543 while Present (A_Spec) loop
9544 A_Id := Get_Aspect_Id (A_Spec);
9545 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9546 return True;
9547 end if;
9549 Next (A_Spec);
9550 end loop;
9552 return False;
9553 end if;
9554 end Has_Contracts;
9556 ----------
9557 -- Hash --
9558 ----------
9560 function Hash (F : Entity_Id) return HTable_Range is
9561 begin
9562 return HTable_Range (F mod HTable_Size);
9563 end Hash;
9565 ------------------------
9566 -- Hide_Current_Scope --
9567 ------------------------
9569 procedure Hide_Current_Scope is
9570 C : constant Entity_Id := Current_Scope;
9571 E : Entity_Id;
9573 begin
9574 Set_Is_Hidden_Open_Scope (C);
9576 E := First_Entity (C);
9577 while Present (E) loop
9578 if Is_Immediately_Visible (E) then
9579 Set_Is_Immediately_Visible (E, False);
9580 Append_Elmt (E, Hidden_Entities);
9581 end if;
9583 Next_Entity (E);
9584 end loop;
9586 -- Make the scope name invisible as well. This is necessary, but might
9587 -- conflict with calls to Rtsfind later on, in case the scope is a
9588 -- predefined one. There is no clean solution to this problem, so for
9589 -- now we depend on the user not redefining Standard itself in one of
9590 -- the parent units.
9592 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9593 Set_Is_Immediately_Visible (C, False);
9594 Append_Elmt (C, Hidden_Entities);
9595 end if;
9597 end Hide_Current_Scope;
9599 --------------
9600 -- Init_Env --
9601 --------------
9603 procedure Init_Env is
9604 Saved : Instance_Env;
9606 begin
9607 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9608 Saved.Exchanged_Views := Exchanged_Views;
9609 Saved.Hidden_Entities := Hidden_Entities;
9610 Saved.Current_Sem_Unit := Current_Sem_Unit;
9611 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9612 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9614 -- Save configuration switches. These may be reset if the unit is a
9615 -- predefined unit, and the current mode is not Ada 2005.
9617 Saved.Switches := Save_Config_Switches;
9619 Instance_Envs.Append (Saved);
9621 Exchanged_Views := New_Elmt_List;
9622 Hidden_Entities := New_Elmt_List;
9624 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9625 -- this is set properly in Set_Instance_Env.
9627 Current_Instantiated_Parent :=
9628 (Current_Scope, Current_Scope, Assoc_Null);
9629 end Init_Env;
9631 ---------------------
9632 -- In_Main_Context --
9633 ---------------------
9635 function In_Main_Context (E : Entity_Id) return Boolean is
9636 Context : List_Id;
9637 Clause : Node_Id;
9638 Nam : Node_Id;
9640 begin
9641 if not Is_Compilation_Unit (E)
9642 or else Ekind (E) /= E_Package
9643 or else In_Private_Part (E)
9644 then
9645 return False;
9646 end if;
9648 Context := Context_Items (Cunit (Main_Unit));
9650 Clause := First (Context);
9651 while Present (Clause) loop
9652 if Nkind (Clause) = N_With_Clause then
9653 Nam := Name (Clause);
9655 -- If the current scope is part of the context of the main unit,
9656 -- analysis of the corresponding with_clause is not complete, and
9657 -- the entity is not set. We use the Chars field directly, which
9658 -- might produce false positives in rare cases, but guarantees
9659 -- that we produce all the instance bodies we will need.
9661 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9662 or else (Nkind (Nam) = N_Selected_Component
9663 and then Chars (Selector_Name (Nam)) = Chars (E))
9664 then
9665 return True;
9666 end if;
9667 end if;
9669 Next (Clause);
9670 end loop;
9672 return False;
9673 end In_Main_Context;
9675 ---------------------
9676 -- Inherit_Context --
9677 ---------------------
9679 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9680 Current_Context : List_Id;
9681 Current_Unit : Node_Id;
9682 Item : Node_Id;
9683 New_I : Node_Id;
9685 Clause : Node_Id;
9686 OK : Boolean;
9687 Lib_Unit : Node_Id;
9689 begin
9690 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9692 -- The inherited context is attached to the enclosing compilation
9693 -- unit. This is either the main unit, or the declaration for the
9694 -- main unit (in case the instantiation appears within the package
9695 -- declaration and the main unit is its body).
9697 Current_Unit := Parent (Inst);
9698 while Present (Current_Unit)
9699 and then Nkind (Current_Unit) /= N_Compilation_Unit
9700 loop
9701 Current_Unit := Parent (Current_Unit);
9702 end loop;
9704 Current_Context := Context_Items (Current_Unit);
9706 Item := First (Context_Items (Parent (Gen_Decl)));
9707 while Present (Item) loop
9708 if Nkind (Item) = N_With_Clause then
9709 Lib_Unit := Library_Unit (Item);
9711 -- Take care to prevent direct cyclic with's
9713 if Lib_Unit /= Current_Unit then
9715 -- Do not add a unit if it is already in the context
9717 Clause := First (Current_Context);
9718 OK := True;
9719 while Present (Clause) loop
9720 if Nkind (Clause) = N_With_Clause
9721 and then Library_Unit (Clause) = Lib_Unit
9722 then
9723 OK := False;
9724 exit;
9725 end if;
9727 Next (Clause);
9728 end loop;
9730 if OK then
9731 New_I := New_Copy (Item);
9732 Set_Implicit_With (New_I);
9734 Append (New_I, Current_Context);
9735 end if;
9736 end if;
9737 end if;
9739 Next (Item);
9740 end loop;
9741 end if;
9742 end Inherit_Context;
9744 ----------------
9745 -- Initialize --
9746 ----------------
9748 procedure Initialize is
9749 begin
9750 Generic_Renamings.Init;
9751 Instance_Envs.Init;
9752 Generic_Flags.Init;
9753 Generic_Renamings_HTable.Reset;
9754 Circularity_Detected := False;
9755 Exchanged_Views := No_Elist;
9756 Hidden_Entities := No_Elist;
9757 end Initialize;
9759 -------------------------------------
9760 -- Insert_Freeze_Node_For_Instance --
9761 -------------------------------------
9763 procedure Insert_Freeze_Node_For_Instance
9764 (N : Node_Id;
9765 F_Node : Node_Id)
9767 function Enclosing_Body (N : Node_Id) return Node_Id;
9768 -- Find enclosing package or subprogram body, if any. Freeze node may
9769 -- be placed at end of current declarative list if previous instance
9770 -- and current one have different enclosing bodies.
9772 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9773 -- Find the local instance, if any, that declares the generic that is
9774 -- being instantiated. If present, the freeze node for this instance
9775 -- must follow the freeze node for the previous instance.
9777 --------------------
9778 -- Enclosing_Body --
9779 --------------------
9781 function Enclosing_Body (N : Node_Id) return Node_Id is
9782 P : Node_Id;
9784 begin
9785 P := Parent (N);
9786 while Present (P)
9787 and then Nkind (Parent (P)) /= N_Compilation_Unit
9788 loop
9789 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9790 if Nkind (Parent (P)) = N_Subunit then
9791 return Corresponding_Stub (Parent (P));
9792 else
9793 return P;
9794 end if;
9795 end if;
9797 P := True_Parent (P);
9798 end loop;
9800 return Empty;
9801 end Enclosing_Body;
9803 -----------------------
9804 -- Previous_Instance --
9805 -----------------------
9807 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9808 S : Entity_Id;
9810 begin
9811 S := Scope (Gen);
9812 while Present (S) and then S /= Standard_Standard loop
9813 if Is_Generic_Instance (S)
9814 and then In_Same_Source_Unit (S, N)
9815 then
9816 return S;
9817 end if;
9819 S := Scope (S);
9820 end loop;
9822 return Empty;
9823 end Previous_Instance;
9825 -- Local variables
9827 Decl : Node_Id;
9828 Decls : List_Id;
9829 Inst : Entity_Id;
9830 Origin : Entity_Id;
9831 Par_Inst : Node_Id;
9832 Par_N : Node_Id;
9834 -- Start of processing for Insert_Freeze_Node_For_Instance
9836 begin
9837 -- Nothing to do if the freeze node has already been inserted
9839 if Is_List_Member (F_Node) then
9840 return;
9841 end if;
9843 Inst := Entity (F_Node);
9845 -- When processing a subprogram instantiation, utilize the actual
9846 -- subprogram instantiation rather than its package wrapper as it
9847 -- carries all the context information.
9849 if Is_Wrapper_Package (Inst) then
9850 Inst := Related_Instance (Inst);
9851 end if;
9853 Par_Inst := Parent (Inst);
9855 -- If this is a package instance, check whether the generic is declared
9856 -- in a previous instance and the current instance is not within the
9857 -- previous one.
9859 if Present (Generic_Parent (Par_Inst)) and then Is_In_Main_Unit (N) then
9860 declare
9861 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9862 Par_I : constant Entity_Id :=
9863 Previous_Instance (Generic_Parent (Par_Inst));
9864 Scop : Entity_Id;
9866 begin
9867 if Present (Par_I) and then Earlier (N, Freeze_Node (Par_I)) then
9868 Scop := Scope (Inst);
9870 -- If the current instance is within the one that contains
9871 -- the generic, the freeze node for the current one must
9872 -- appear in the current declarative part. Ditto, if the
9873 -- current instance is within another package instance or
9874 -- within a body that does not enclose the current instance.
9875 -- In these three cases the freeze node of the previous
9876 -- instance is not relevant.
9878 while Present (Scop) and then Scop /= Standard_Standard loop
9879 exit when Scop = Par_I
9880 or else
9881 (Is_Generic_Instance (Scop)
9882 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9883 Scop := Scope (Scop);
9884 end loop;
9886 -- Previous instance encloses current instance
9888 if Scop = Par_I then
9889 null;
9891 -- If the next node is a source body we must freeze in the
9892 -- current scope as well.
9894 elsif Present (Next (N))
9895 and then Nkind (Next (N)) in N_Subprogram_Body
9896 | N_Package_Body
9897 and then Comes_From_Source (Next (N))
9898 then
9899 null;
9901 -- Current instance is within an unrelated instance
9903 elsif Is_Generic_Instance (Scop) then
9904 null;
9906 -- Current instance is within an unrelated body
9908 elsif Present (Enclosing_N)
9909 and then Enclosing_N /= Enclosing_Body (Par_I)
9910 then
9911 null;
9913 else
9914 Insert_After (Freeze_Node (Par_I), F_Node);
9915 return;
9916 end if;
9917 end if;
9918 end;
9919 end if;
9921 Decl := N;
9922 Decls := List_Containing (N);
9923 Par_N := Parent (Decls);
9924 Origin := Empty;
9926 -- Determine the proper freeze point of an instantiation
9928 if Is_Generic_Instance (Inst) then
9929 loop
9930 -- When the instantiation occurs in a package spec, append the
9931 -- freeze node to the private declarations (if any).
9933 if Nkind (Par_N) = N_Package_Specification
9934 and then Decls = Visible_Declarations (Par_N)
9935 and then not Is_Empty_List (Private_Declarations (Par_N))
9936 then
9937 Decls := Private_Declarations (Par_N);
9938 Decl := First (Decls);
9939 end if;
9941 -- We adhere to the general rule of a package or subprogram body
9942 -- causing freezing of anything before it in the same declarative
9943 -- region. In this respect, the proper freeze point of a package
9944 -- instantiation is before the first source body which follows, or
9945 -- before a stub. This ensures that entities from the instance are
9946 -- already frozen and therefore usable in source bodies.
9948 if Nkind (Par_N) /= N_Package_Declaration
9949 and then
9950 not In_Same_Source_Unit (Generic_Parent (Par_Inst), Inst)
9951 then
9952 while Present (Decl) loop
9953 if ((Nkind (Decl) in N_Unit_Body
9954 or else
9955 Nkind (Decl) in N_Body_Stub)
9956 and then Comes_From_Source (Decl))
9957 or else (Present (Origin)
9958 and then Nkind (Decl) in N_Generic_Instantiation
9959 and then Instance_Spec (Decl) /= Origin)
9960 then
9961 Set_Sloc (F_Node, Sloc (Decl));
9962 Insert_Before (Decl, F_Node);
9963 return;
9964 end if;
9966 Next (Decl);
9967 end loop;
9968 end if;
9970 -- When the instantiation occurs in a package spec and there is
9971 -- no source body which follows, and the package has a body but
9972 -- is delayed, then insert immediately before its freeze node.
9974 if Nkind (Par_N) = N_Package_Specification
9975 and then Present (Corresponding_Body (Parent (Par_N)))
9976 and then Present (Freeze_Node (Defining_Entity (Par_N)))
9977 then
9978 Set_Sloc (F_Node, Sloc (Freeze_Node (Defining_Entity (Par_N))));
9979 Insert_Before (Freeze_Node (Defining_Entity (Par_N)), F_Node);
9980 return;
9982 -- When the instantiation occurs in a package spec and there is
9983 -- no source body which follows, not even of the package itself,
9984 -- then insert into the declaration list of the outer level, but
9985 -- do not jump over following instantiations in this list because
9986 -- they may have a body that has not materialized yet, see above.
9988 elsif Nkind (Par_N) = N_Package_Specification
9989 and then No (Corresponding_Body (Parent (Par_N)))
9990 and then Is_List_Member (Parent (Par_N))
9991 then
9992 Decl := Parent (Par_N);
9993 Decls := List_Containing (Decl);
9994 Par_N := Parent (Decls);
9995 Origin := Decl;
9997 -- In a package declaration, or if no source body which follows
9998 -- and at library level, then insert at end of list.
10000 else
10001 exit;
10002 end if;
10003 end loop;
10004 end if;
10006 -- Insert and adjust the Sloc of the freeze node
10008 Set_Sloc (F_Node, Sloc (Last (Decls)));
10009 Insert_After (Last (Decls), F_Node);
10010 end Insert_Freeze_Node_For_Instance;
10012 -----------------------------
10013 -- Install_Formal_Packages --
10014 -----------------------------
10016 procedure Install_Formal_Packages (Par : Entity_Id) is
10017 E : Entity_Id;
10018 Gen : Entity_Id;
10019 Gen_E : Entity_Id := Empty;
10021 begin
10022 E := First_Entity (Par);
10024 -- If we are installing an instance parent, locate the formal packages
10025 -- of its generic parent.
10027 if Is_Generic_Instance (Par) then
10028 Gen := Generic_Parent (Package_Specification (Par));
10029 Gen_E := First_Entity (Gen);
10030 end if;
10032 while Present (E) loop
10033 if Ekind (E) = E_Package
10034 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10035 then
10036 -- If this is the renaming for the parent instance, done
10038 if Renamed_Entity (E) = Par then
10039 exit;
10041 -- The visibility of a formal of an enclosing generic is already
10042 -- correct.
10044 elsif Denotes_Formal_Package (E) then
10045 null;
10047 elsif Present (Associated_Formal_Package (E)) then
10048 Check_Generic_Actuals (Renamed_Entity (E), True);
10049 Set_Is_Hidden (E, False);
10051 -- Find formal package in generic unit that corresponds to
10052 -- (instance of) formal package in instance.
10054 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10055 Next_Entity (Gen_E);
10056 end loop;
10058 if Present (Gen_E) then
10059 Map_Formal_Package_Entities (Gen_E, E);
10060 end if;
10061 end if;
10062 end if;
10064 Next_Entity (E);
10066 if Present (Gen_E) then
10067 Next_Entity (Gen_E);
10068 end if;
10069 end loop;
10070 end Install_Formal_Packages;
10072 --------------------
10073 -- Install_Parent --
10074 --------------------
10076 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10077 Ancestors : constant Elist_Id := New_Elmt_List;
10078 S : constant Entity_Id := Current_Scope;
10079 Inst_Par : Entity_Id;
10080 First_Par : Entity_Id;
10081 Inst_Node : Node_Id;
10082 Gen_Par : Entity_Id;
10083 First_Gen : Entity_Id;
10084 Elmt : Elmt_Id;
10086 procedure Install_Noninstance_Specs (Par : Entity_Id);
10087 -- Install the scopes of noninstance parent units ending with Par
10089 procedure Install_Spec (Par : Entity_Id);
10090 -- The child unit is within the declarative part of the parent, so the
10091 -- declarations within the parent are immediately visible.
10093 -------------------------------
10094 -- Install_Noninstance_Specs --
10095 -------------------------------
10097 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10098 begin
10099 if Present (Par)
10100 and then Par /= Standard_Standard
10101 and then not In_Open_Scopes (Par)
10102 then
10103 Install_Noninstance_Specs (Scope (Par));
10104 Install_Spec (Par);
10105 end if;
10106 end Install_Noninstance_Specs;
10108 ------------------
10109 -- Install_Spec --
10110 ------------------
10112 procedure Install_Spec (Par : Entity_Id) is
10113 Spec : constant Node_Id := Package_Specification (Par);
10115 begin
10116 -- If this parent of the child instance is a top-level unit,
10117 -- then record the unit and its visibility for later resetting in
10118 -- Remove_Parent. We exclude units that are generic instances, as we
10119 -- only want to record this information for the ultimate top-level
10120 -- noninstance parent (is that always correct???).
10122 if Scope (Par) = Standard_Standard
10123 and then not Is_Generic_Instance (Par)
10124 then
10125 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10126 Instance_Parent_Unit := Par;
10127 end if;
10129 -- Open the parent scope and make it and its declarations visible.
10130 -- If this point is not within a body, then only the visible
10131 -- declarations should be made visible, and installation of the
10132 -- private declarations is deferred until the appropriate point
10133 -- within analysis of the spec being instantiated (see the handling
10134 -- of parent visibility in Analyze_Package_Specification). This is
10135 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10136 -- private view problems that occur when compiling instantiations of
10137 -- a generic child of that package (Generic_Dispatching_Constructor).
10138 -- If the instance freezes a tagged type, inlinings of operations
10139 -- from Ada.Tags may need the full view of type Tag. If inlining took
10140 -- proper account of establishing visibility of inlined subprograms'
10141 -- parents then it should be possible to remove this
10142 -- special check. ???
10144 Push_Scope (Par);
10145 Set_Is_Immediately_Visible (Par);
10146 Install_Visible_Declarations (Par);
10147 Set_Use (Visible_Declarations (Spec));
10149 if In_Body or else Is_RTU (Par, Ada_Tags) then
10150 Install_Private_Declarations (Par);
10151 Set_Use (Private_Declarations (Spec));
10152 end if;
10153 end Install_Spec;
10155 -- Start of processing for Install_Parent
10157 begin
10158 -- We need to install the parent instance to compile the instantiation
10159 -- of the child, but the child instance must appear in the current
10160 -- scope. Given that we cannot place the parent above the current scope
10161 -- in the scope stack, we duplicate the current scope and unstack both
10162 -- after the instantiation is complete.
10164 -- If the parent is itself the instantiation of a child unit, we must
10165 -- also stack the instantiation of its parent, and so on. Each such
10166 -- ancestor is the prefix of the name in a prior instantiation.
10168 -- If this is a nested instance, the parent unit itself resolves to
10169 -- a renaming of the parent instance, whose declaration we need.
10171 -- Finally, the parent may be a generic (not an instance) when the
10172 -- child unit appears as a formal package.
10174 Inst_Par := P;
10176 if Present (Renamed_Entity (Inst_Par)) then
10177 Inst_Par := Renamed_Entity (Inst_Par);
10178 end if;
10180 First_Par := Inst_Par;
10182 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10184 First_Gen := Gen_Par;
10186 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10188 -- Load grandparent instance as well
10190 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10192 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10193 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10195 if Present (Renamed_Entity (Inst_Par)) then
10196 Inst_Par := Renamed_Entity (Inst_Par);
10197 end if;
10199 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10201 if Present (Gen_Par) then
10202 Prepend_Elmt (Inst_Par, Ancestors);
10204 else
10205 -- Parent is not the name of an instantiation
10207 Install_Noninstance_Specs (Inst_Par);
10208 exit;
10209 end if;
10211 else
10212 -- Previous error
10214 exit;
10215 end if;
10216 end loop;
10218 if Present (First_Gen) then
10219 Append_Elmt (First_Par, Ancestors);
10220 else
10221 Install_Noninstance_Specs (First_Par);
10222 end if;
10224 if not Is_Empty_Elmt_List (Ancestors) then
10225 Elmt := First_Elmt (Ancestors);
10226 while Present (Elmt) loop
10227 Install_Spec (Node (Elmt));
10228 Install_Formal_Packages (Node (Elmt));
10229 Next_Elmt (Elmt);
10230 end loop;
10231 end if;
10233 if not In_Body then
10234 Push_Scope (S);
10235 end if;
10236 end Install_Parent;
10238 -------------------------------
10239 -- Install_Hidden_Primitives --
10240 -------------------------------
10242 procedure Install_Hidden_Primitives
10243 (Prims_List : in out Elist_Id;
10244 Gen_T : Entity_Id;
10245 Act_T : Entity_Id)
10247 Elmt : Elmt_Id;
10248 List : Elist_Id := No_Elist;
10249 Prim_G_Elmt : Elmt_Id;
10250 Prim_A_Elmt : Elmt_Id;
10251 Prim_G : Node_Id;
10252 Prim_A : Node_Id;
10254 begin
10255 -- No action needed in case of serious errors because we cannot trust
10256 -- in the order of primitives
10258 if Serious_Errors_Detected > 0 then
10259 return;
10261 -- No action possible if we don't have available the list of primitive
10262 -- operations
10264 elsif No (Gen_T)
10265 or else not Is_Record_Type (Gen_T)
10266 or else not Is_Tagged_Type (Gen_T)
10267 or else not Is_Record_Type (Act_T)
10268 or else not Is_Tagged_Type (Act_T)
10269 then
10270 return;
10272 -- There is no need to handle interface types since their primitives
10273 -- cannot be hidden
10275 elsif Is_Interface (Gen_T) then
10276 return;
10277 end if;
10279 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10281 if not Is_Class_Wide_Type (Act_T) then
10282 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10283 else
10284 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10285 end if;
10287 loop
10288 -- Skip predefined primitives in the generic formal
10290 while Present (Prim_G_Elmt)
10291 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10292 loop
10293 Next_Elmt (Prim_G_Elmt);
10294 end loop;
10296 -- Skip predefined primitives in the generic actual
10298 while Present (Prim_A_Elmt)
10299 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10300 loop
10301 Next_Elmt (Prim_A_Elmt);
10302 end loop;
10304 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10306 Prim_G := Node (Prim_G_Elmt);
10307 Prim_A := Node (Prim_A_Elmt);
10309 -- There is no need to handle interface primitives because their
10310 -- primitives are not hidden
10312 exit when Present (Interface_Alias (Prim_G));
10314 -- Here we install one hidden primitive
10316 if Chars (Prim_G) /= Chars (Prim_A)
10317 and then Has_Suffix (Prim_A, 'P')
10318 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10319 then
10320 Set_Chars (Prim_A, Chars (Prim_G));
10321 Append_New_Elmt (Prim_A, To => List);
10322 end if;
10324 Next_Elmt (Prim_A_Elmt);
10325 Next_Elmt (Prim_G_Elmt);
10326 end loop;
10328 -- Append the elements to the list of temporarily visible primitives
10329 -- avoiding duplicates.
10331 if Present (List) then
10332 if No (Prims_List) then
10333 Prims_List := New_Elmt_List;
10334 end if;
10336 Elmt := First_Elmt (List);
10337 while Present (Elmt) loop
10338 Append_Unique_Elmt (Node (Elmt), Prims_List);
10339 Next_Elmt (Elmt);
10340 end loop;
10341 end if;
10342 end Install_Hidden_Primitives;
10344 -------------------------------
10345 -- Restore_Hidden_Primitives --
10346 -------------------------------
10348 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10349 Prim_Elmt : Elmt_Id;
10350 Prim : Node_Id;
10352 begin
10353 if Present (Prims_List) then
10354 Prim_Elmt := First_Elmt (Prims_List);
10355 while Present (Prim_Elmt) loop
10356 Prim := Node (Prim_Elmt);
10357 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10358 Next_Elmt (Prim_Elmt);
10359 end loop;
10361 Prims_List := No_Elist;
10362 end if;
10363 end Restore_Hidden_Primitives;
10365 --------------------------------
10366 -- Instantiate_Formal_Package --
10367 --------------------------------
10369 function Instantiate_Formal_Package
10370 (Formal : Node_Id;
10371 Actual : Node_Id;
10372 Analyzed_Formal : Node_Id) return List_Id
10374 Loc : constant Source_Ptr := Sloc (Actual);
10375 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10377 Actual_Pack : Entity_Id;
10378 Formal_Pack : Entity_Id;
10379 Gen_Parent : Entity_Id;
10380 Decls : List_Id;
10381 Nod : Node_Id;
10382 Parent_Spec : Node_Id;
10384 procedure Find_Matching_Actual
10385 (F : Node_Id;
10386 Act : in out Entity_Id);
10387 -- We need to associate each formal entity in the formal package with
10388 -- the corresponding entity in the actual package. The actual package
10389 -- has been analyzed and possibly expanded, and as a result there is
10390 -- no one-to-one correspondence between the two lists (for example,
10391 -- the actual may include subtypes, itypes, and inherited primitive
10392 -- operations, interspersed among the renaming declarations for the
10393 -- actuals). We retrieve the corresponding actual by name because each
10394 -- actual has the same name as the formal, and they do appear in the
10395 -- same order.
10397 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10398 -- Retrieve entity of defining entity of generic formal parameter.
10399 -- Only the declarations of formals need to be considered when
10400 -- linking them to actuals, but the declarative list may include
10401 -- internal entities generated during analysis, and those are ignored.
10403 procedure Match_Formal_Entity
10404 (Formal_Node : Node_Id;
10405 Formal_Ent : Entity_Id;
10406 Actual_Ent : Entity_Id);
10407 -- Associates the formal entity with the actual. In the case where
10408 -- Formal_Ent is a formal package, this procedure iterates through all
10409 -- of its formals and enters associations between the actuals occurring
10410 -- in the formal package's corresponding actual package (given by
10411 -- Actual_Ent) and the formal package's formal parameters. This
10412 -- procedure recurses if any of the parameters is itself a package.
10414 function Is_Instance_Of
10415 (Act_Spec : Entity_Id;
10416 Gen_Anc : Entity_Id) return Boolean;
10417 -- The actual can be an instantiation of a generic within another
10418 -- instance, in which case there is no direct link from it to the
10419 -- original generic ancestor. In that case, we recognize that the
10420 -- ultimate ancestor is the same by examining names and scopes.
10422 procedure Process_Nested_Formal (Formal : Entity_Id);
10423 -- If the current formal is declared with a box, its own formals are
10424 -- visible in the instance, as they were in the generic, and their
10425 -- Hidden flag must be reset. If some of these formals are themselves
10426 -- packages declared with a box, the processing must be recursive.
10428 --------------------------
10429 -- Find_Matching_Actual --
10430 --------------------------
10432 procedure Find_Matching_Actual
10433 (F : Node_Id;
10434 Act : in out Entity_Id)
10436 Formal_Ent : Entity_Id;
10438 begin
10439 case Nkind (Original_Node (F)) is
10440 when N_Formal_Object_Declaration
10441 | N_Formal_Type_Declaration
10443 Formal_Ent := Defining_Identifier (F);
10445 while Present (Act)
10446 and then Chars (Act) /= Chars (Formal_Ent)
10447 loop
10448 Next_Entity (Act);
10449 end loop;
10451 when N_Formal_Package_Declaration
10452 | N_Formal_Subprogram_Declaration
10453 | N_Generic_Package_Declaration
10454 | N_Package_Declaration
10456 Formal_Ent := Defining_Entity (F);
10458 while Present (Act)
10459 and then Chars (Act) /= Chars (Formal_Ent)
10460 loop
10461 Next_Entity (Act);
10462 end loop;
10464 when others =>
10465 raise Program_Error;
10466 end case;
10467 end Find_Matching_Actual;
10469 -------------------------
10470 -- Match_Formal_Entity --
10471 -------------------------
10473 procedure Match_Formal_Entity
10474 (Formal_Node : Node_Id;
10475 Formal_Ent : Entity_Id;
10476 Actual_Ent : Entity_Id)
10478 Act_Pkg : Entity_Id;
10480 begin
10481 Set_Instance_Of (Formal_Ent, Actual_Ent);
10483 if Ekind (Actual_Ent) = E_Package then
10485 -- Record associations for each parameter
10487 Act_Pkg := Actual_Ent;
10489 declare
10490 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10491 F_Ent : Entity_Id;
10492 F_Node : Node_Id;
10494 Gen_Decl : Node_Id;
10495 Formals : List_Id;
10496 Actual : Entity_Id;
10498 begin
10499 -- Retrieve the actual given in the formal package declaration
10501 Actual := Entity (Name (Original_Node (Formal_Node)));
10503 -- The actual in the formal package declaration may be a
10504 -- renamed generic package, in which case we want to retrieve
10505 -- the original generic in order to traverse its formal part.
10507 if Present (Renamed_Entity (Actual)) then
10508 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10509 else
10510 Gen_Decl := Unit_Declaration_Node (Actual);
10511 end if;
10513 Formals := Generic_Formal_Declarations (Gen_Decl);
10515 if Present (Formals) then
10516 F_Node := First_Non_Pragma (Formals);
10517 else
10518 F_Node := Empty;
10519 end if;
10521 while Present (A_Ent)
10522 and then Present (F_Node)
10523 and then A_Ent /= First_Private_Entity (Act_Pkg)
10524 loop
10525 F_Ent := Get_Formal_Entity (F_Node);
10527 if Present (F_Ent) then
10529 -- This is a formal of the original package. Record
10530 -- association and recurse.
10532 Find_Matching_Actual (F_Node, A_Ent);
10533 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10534 Next_Entity (A_Ent);
10535 end if;
10537 Next_Non_Pragma (F_Node);
10538 end loop;
10539 end;
10540 end if;
10541 end Match_Formal_Entity;
10543 -----------------------
10544 -- Get_Formal_Entity --
10545 -----------------------
10547 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10548 Kind : constant Node_Kind := Nkind (Original_Node (N));
10549 begin
10550 case Kind is
10551 when N_Formal_Object_Declaration =>
10552 return Defining_Identifier (N);
10554 when N_Formal_Type_Declaration =>
10555 return Defining_Identifier (N);
10557 when N_Formal_Subprogram_Declaration =>
10558 return Defining_Unit_Name (Specification (N));
10560 when N_Formal_Package_Declaration =>
10561 return Defining_Identifier (Original_Node (N));
10563 when N_Generic_Package_Declaration =>
10564 return Defining_Identifier (Original_Node (N));
10566 -- All other declarations are introduced by semantic analysis and
10567 -- have no match in the actual.
10569 when others =>
10570 return Empty;
10571 end case;
10572 end Get_Formal_Entity;
10574 --------------------
10575 -- Is_Instance_Of --
10576 --------------------
10578 function Is_Instance_Of
10579 (Act_Spec : Entity_Id;
10580 Gen_Anc : Entity_Id) return Boolean
10582 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10584 begin
10585 if No (Gen_Par) then
10586 return False;
10588 -- Simplest case: the generic parent of the actual is the formal
10590 elsif Gen_Par = Gen_Anc then
10591 return True;
10593 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10594 return False;
10596 -- The actual may be obtained through several instantiations. Its
10597 -- scope must itself be an instance of a generic declared in the
10598 -- same scope as the formal. Any other case is detected above.
10600 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10601 return False;
10603 else
10604 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10605 end if;
10606 end Is_Instance_Of;
10608 ---------------------------
10609 -- Process_Nested_Formal --
10610 ---------------------------
10612 procedure Process_Nested_Formal (Formal : Entity_Id) is
10613 Ent : Entity_Id;
10615 begin
10616 if Present (Associated_Formal_Package (Formal))
10617 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10618 then
10619 Ent := First_Entity (Formal);
10620 while Present (Ent) loop
10621 Set_Is_Hidden (Ent, False);
10622 Set_Is_Visible_Formal (Ent);
10623 Set_Is_Potentially_Use_Visible
10624 (Ent, Is_Potentially_Use_Visible (Formal));
10626 if Ekind (Ent) = E_Package then
10627 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10628 Process_Nested_Formal (Ent);
10629 end if;
10631 Next_Entity (Ent);
10632 end loop;
10633 end if;
10634 end Process_Nested_Formal;
10636 -- Start of processing for Instantiate_Formal_Package
10638 begin
10639 Analyze (Actual);
10641 -- The actual must be a package instance, or else a current instance
10642 -- such as a parent generic within the body of a generic child.
10644 if not Is_Entity_Name (Actual)
10645 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10646 then
10647 Error_Msg_N
10648 ("expect package instance to instantiate formal", Actual);
10649 Abandon_Instantiation (Actual);
10651 else
10652 Actual_Pack := Entity (Actual);
10653 Set_Is_Instantiated (Actual_Pack);
10655 -- The actual may be a renamed package, or an outer generic formal
10656 -- package whose instantiation is converted into a renaming.
10658 if Present (Renamed_Entity (Actual_Pack)) then
10659 Actual_Pack := Renamed_Entity (Actual_Pack);
10660 end if;
10662 -- The analyzed formal is expected to be the result of the rewriting
10663 -- of the formal package into a regular package by analysis.
10665 pragma Assert (Nkind (Analyzed_Formal) = N_Package_Declaration
10666 and then Nkind (Original_Node (Analyzed_Formal)) =
10667 N_Formal_Package_Declaration);
10669 Gen_Parent := Generic_Parent (Specification (Analyzed_Formal));
10670 Formal_Pack := Defining_Unit_Name (Specification (Analyzed_Formal));
10672 -- The actual for a ghost generic formal package should be a ghost
10673 -- package (SPARK RM 6.9(14)).
10675 Check_Ghost_Formal_Procedure_Or_Package
10676 (N => Actual,
10677 Actual => Actual_Pack,
10678 Formal => Formal_Pack);
10680 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10681 Parent_Spec := Package_Specification (Actual_Pack);
10682 else
10683 Parent_Spec := Parent (Actual_Pack);
10684 end if;
10686 if Gen_Parent = Any_Id then
10687 Error_Msg_N
10688 ("previous error in declaration of formal package", Actual);
10689 Abandon_Instantiation (Actual);
10691 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10692 null;
10694 -- If this is the current instance of an enclosing generic, that unit
10695 -- is the generic package we need.
10697 elsif In_Open_Scopes (Actual_Pack)
10698 and then Ekind (Actual_Pack) = E_Generic_Package
10699 then
10700 null;
10702 else
10703 Error_Msg_NE
10704 ("actual parameter must be instance of&", Actual, Gen_Parent);
10705 Abandon_Instantiation (Actual);
10706 end if;
10708 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10709 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10711 Nod :=
10712 Make_Package_Renaming_Declaration (Loc,
10713 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10714 Name => New_Occurrence_Of (Actual_Pack, Loc));
10716 Set_Associated_Formal_Package
10717 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10718 Decls := New_List (Nod);
10720 -- If the formal F has a box, then the generic declarations are
10721 -- visible in the generic G. In an instance of G, the corresponding
10722 -- entities in the actual for F (which are the actuals for the
10723 -- instantiation of the generic that F denotes) must also be made
10724 -- visible for analysis of the current instance. On exit from the
10725 -- current instance, those entities are made private again. If the
10726 -- actual is currently in use, these entities are also use-visible.
10728 -- The loop through the actual entities also steps through the formal
10729 -- entities and enters associations from formals to actuals into the
10730 -- renaming map. This is necessary to properly handle checking of
10731 -- actual parameter associations for later formals that depend on
10732 -- actuals declared in the formal package.
10734 -- In Ada 2005, partial parameterization requires that we make
10735 -- visible the actuals corresponding to formals that were defaulted
10736 -- in the formal package. There formals are identified because they
10737 -- remain formal generics within the formal package, rather than
10738 -- being renamings of the actuals supplied.
10740 declare
10741 Gen_Decl : constant Node_Id :=
10742 Unit_Declaration_Node (Gen_Parent);
10743 Formals : constant List_Id :=
10744 Generic_Formal_Declarations (Gen_Decl);
10746 Actual_Ent : Entity_Id;
10747 Actual_Of_Formal : Node_Id;
10748 Formal_Node : Node_Id;
10749 Formal_Ent : Entity_Id;
10751 begin
10752 if Present (Formals) then
10753 Formal_Node := First_Non_Pragma (Formals);
10754 else
10755 Formal_Node := Empty;
10756 end if;
10758 Actual_Ent := First_Entity (Actual_Pack);
10759 Actual_Of_Formal :=
10760 First (Visible_Declarations (Specification (Analyzed_Formal)));
10761 while Present (Actual_Ent)
10762 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10763 loop
10764 if Present (Formal_Node) then
10765 Formal_Ent := Get_Formal_Entity (Formal_Node);
10767 if Present (Formal_Ent) then
10768 Find_Matching_Actual (Formal_Node, Actual_Ent);
10769 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10771 -- We iterate at the same time over the actuals of the
10772 -- local package created for the formal, to determine
10773 -- which one of the formals of the original generic were
10774 -- defaulted in the formal. The corresponding actual
10775 -- entities are visible in the enclosing instance.
10777 if Box_Present (Formal)
10778 or else
10779 (Present (Actual_Of_Formal)
10780 and then
10781 Is_Generic_Formal
10782 (Get_Formal_Entity (Actual_Of_Formal)))
10783 then
10784 Set_Is_Hidden (Actual_Ent, False);
10785 Set_Is_Visible_Formal (Actual_Ent);
10786 Set_Is_Potentially_Use_Visible
10787 (Actual_Ent, In_Use (Actual_Pack));
10789 if Ekind (Actual_Ent) = E_Package then
10790 Process_Nested_Formal (Actual_Ent);
10791 end if;
10793 else
10794 if not Is_Hidden (Actual_Ent) then
10795 Append_Elmt (Actual_Ent, Hidden_Formals);
10796 end if;
10798 Set_Is_Hidden (Actual_Ent);
10799 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10800 end if;
10801 end if;
10803 Next_Non_Pragma (Formal_Node);
10804 Next (Actual_Of_Formal);
10806 -- A formal subprogram may be overloaded, so advance in
10807 -- the list of actuals to make sure we do not match two
10808 -- successive formals to the same actual. This is only
10809 -- relevant for overloadable entities, others have
10810 -- distinct names.
10812 if Is_Overloadable (Actual_Ent) then
10813 Next_Entity (Actual_Ent);
10814 end if;
10816 else
10817 -- No further formals to match, but the generic part may
10818 -- contain inherited operation that are not hidden in the
10819 -- enclosing instance.
10821 Next_Entity (Actual_Ent);
10822 end if;
10823 end loop;
10825 -- Inherited subprograms generated by formal derived types are
10826 -- also visible if the types are.
10828 Actual_Ent := First_Entity (Actual_Pack);
10829 while Present (Actual_Ent)
10830 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10831 loop
10832 if Is_Overloadable (Actual_Ent)
10833 and then
10834 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10835 and then
10836 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10837 then
10838 Set_Is_Hidden (Actual_Ent, False);
10839 Set_Is_Potentially_Use_Visible
10840 (Actual_Ent, In_Use (Actual_Pack));
10841 end if;
10843 Next_Entity (Actual_Ent);
10844 end loop;
10845 end;
10847 -- If the formal requires conformance checking, reanalyze it as an
10848 -- abbreviated instantiation, to verify the matching rules of 12.7.
10849 -- The actual checks are performed after the generic associations
10850 -- have been analyzed, to guarantee the same visibility for this
10851 -- instantiation and for the actuals.
10853 -- In Ada 2005, the generic associations for the formal can include
10854 -- defaulted parameters. These are ignored during check. This
10855 -- internal instantiation is removed from the tree after conformance
10856 -- checking, because it contains formal declarations for those
10857 -- defaulted parameters, and those should not reach the back-end.
10859 if Requires_Conformance_Checking (Formal) then
10860 declare
10861 I_Pack : constant Entity_Id := Make_Temporary (Loc, 'P');
10863 I_Nam : Node_Id;
10865 begin
10866 Set_Is_Internal (I_Pack);
10867 Mutate_Ekind (I_Pack, E_Package);
10869 -- Insert the package into the list of its hidden entities so
10870 -- that the list is not empty for Is_Abbreviated_Instance.
10872 Append_Elmt (I_Pack, Hidden_Formals);
10874 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10876 -- If the generic is a child unit, Check_Generic_Child_Unit
10877 -- needs its original name in case it is qualified.
10879 if Is_Child_Unit (Gen_Parent) then
10880 I_Nam :=
10881 New_Copy_Tree (Name (Original_Node (Analyzed_Formal)));
10882 pragma Assert (Entity (I_Nam) = Gen_Parent);
10884 else
10885 I_Nam :=
10886 New_Occurrence_Of (Get_Instance_Of (Gen_Parent), Loc);
10887 end if;
10889 Append_To (Decls,
10890 Make_Package_Instantiation (Loc,
10891 Defining_Unit_Name => I_Pack,
10892 Name => I_Nam,
10893 Generic_Associations => Generic_Associations (Formal)));
10894 end;
10895 end if;
10897 return Decls;
10898 end if;
10899 end Instantiate_Formal_Package;
10901 -----------------------------------
10902 -- Instantiate_Formal_Subprogram --
10903 -----------------------------------
10905 function Instantiate_Formal_Subprogram
10906 (Formal : Node_Id;
10907 Actual : Node_Id;
10908 Analyzed_Formal : Node_Id) return Node_Id
10910 Analyzed_S : constant Entity_Id :=
10911 Defining_Unit_Name (Specification (Analyzed_Formal));
10912 Formal_Sub : constant Entity_Id :=
10913 Defining_Unit_Name (Specification (Formal));
10915 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10916 -- If the generic is a child unit, the parent has been installed on the
10917 -- scope stack, but a default subprogram cannot resolve to something
10918 -- on the parent because that parent is not really part of the visible
10919 -- context (it is there to resolve explicit local entities). If the
10920 -- default has resolved in this way, we remove the entity from immediate
10921 -- visibility and analyze the node again to emit an error message or
10922 -- find another visible candidate.
10924 procedure Valid_Actual_Subprogram (Act : Node_Id);
10925 -- Perform legality check and raise exception on failure
10927 -----------------------
10928 -- From_Parent_Scope --
10929 -----------------------
10931 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10932 Gen_Scope : Node_Id;
10934 begin
10935 Gen_Scope := Scope (Analyzed_S);
10936 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10937 if Scope (Subp) = Scope (Gen_Scope) then
10938 return True;
10939 end if;
10941 Gen_Scope := Scope (Gen_Scope);
10942 end loop;
10944 return False;
10945 end From_Parent_Scope;
10947 -----------------------------
10948 -- Valid_Actual_Subprogram --
10949 -----------------------------
10951 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10952 Act_E : Entity_Id;
10954 begin
10955 if Is_Entity_Name (Act) then
10956 Act_E := Entity (Act);
10958 elsif Nkind (Act) = N_Selected_Component
10959 and then Is_Entity_Name (Selector_Name (Act))
10960 then
10961 Act_E := Entity (Selector_Name (Act));
10963 else
10964 Act_E := Empty;
10965 end if;
10967 -- The actual for a ghost generic formal procedure should be a ghost
10968 -- procedure (SPARK RM 6.9(14)).
10970 if Present (Act_E)
10971 and then Ekind (Act_E) = E_Procedure
10972 then
10973 Check_Ghost_Formal_Procedure_Or_Package
10974 (N => Act,
10975 Actual => Act_E,
10976 Formal => Analyzed_S);
10977 end if;
10979 if (Present (Act_E) and then Is_Overloadable (Act_E))
10980 or else Nkind (Act) in N_Attribute_Reference
10981 | N_Indexed_Component
10982 | N_Character_Literal
10983 | N_Explicit_Dereference
10984 then
10985 return;
10986 end if;
10988 Error_Msg_NE
10989 ("expect subprogram or entry name in instantiation of &",
10990 Instantiation_Node, Formal_Sub);
10991 Abandon_Instantiation (Instantiation_Node);
10992 end Valid_Actual_Subprogram;
10994 -- Local variables
10996 Decl_Node : Node_Id;
10997 Loc : Source_Ptr;
10998 Nam : Node_Id;
10999 New_Spec : Node_Id;
11000 New_Subp : Entity_Id;
11002 -- Start of processing for Instantiate_Formal_Subprogram
11004 begin
11005 New_Spec := New_Copy_Tree (Specification (Formal));
11007 -- The tree copy has created the proper instantiation sloc for the
11008 -- new specification. Use this location for all other constructed
11009 -- declarations.
11011 Loc := Sloc (Defining_Unit_Name (New_Spec));
11013 -- Create new entity for the actual (New_Copy_Tree does not), and
11014 -- indicate that it is an actual.
11016 -- If the actual is not an entity (i.e. an attribute reference)
11017 -- and the formal includes aspect specifications for contracts,
11018 -- we create an internal name for the renaming declaration. The
11019 -- constructed wrapper contains a call to the entity in the renaming.
11020 -- This is an expansion activity, as is the wrapper creation.
11022 if Ada_Version >= Ada_2022
11023 and then Has_Contracts (Analyzed_Formal)
11024 and then not Is_Entity_Name (Actual)
11025 and then Expander_Active
11026 then
11027 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11028 else
11029 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11030 end if;
11032 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
11033 Set_Is_Generic_Actual_Subprogram (New_Subp);
11034 Set_Defining_Unit_Name (New_Spec, New_Subp);
11036 -- Create new entities for the each of the formals in the specification
11037 -- of the renaming declaration built for the actual.
11039 if Present (Parameter_Specifications (New_Spec)) then
11040 declare
11041 F : Node_Id;
11042 F_Id : Entity_Id;
11044 begin
11045 F := First (Parameter_Specifications (New_Spec));
11046 while Present (F) loop
11047 F_Id := Defining_Identifier (F);
11049 Set_Defining_Identifier (F,
11050 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11051 Next (F);
11052 end loop;
11053 end;
11054 end if;
11056 -- Find entity of actual. If the actual is an attribute reference, it
11057 -- cannot be resolved here (its formal is missing) but is handled
11058 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11059 -- fully resolved subsequently, when the renaming declaration for the
11060 -- formal is analyzed. If it is an explicit dereference, resolve the
11061 -- prefix but not the actual itself, to prevent interpretation as call.
11063 if Present (Actual) then
11064 Loc := Sloc (Actual);
11065 Set_Sloc (New_Spec, Loc);
11067 if Nkind (Actual) = N_Operator_Symbol then
11068 Find_Direct_Name (Actual);
11070 elsif Nkind (Actual) = N_Explicit_Dereference then
11071 Analyze (Prefix (Actual));
11073 elsif Nkind (Actual) /= N_Attribute_Reference then
11074 Analyze (Actual);
11075 end if;
11077 Valid_Actual_Subprogram (Actual);
11078 Nam := Actual;
11080 elsif Present (Default_Name (Formal)) then
11081 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11082 | N_Selected_Component
11083 | N_Indexed_Component
11084 | N_Character_Literal
11085 and then Present (Entity (Default_Name (Formal)))
11086 then
11087 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11088 else
11089 Nam := New_Copy (Default_Name (Formal));
11090 Set_Sloc (Nam, Loc);
11091 end if;
11093 elsif Box_Present (Formal) then
11095 -- Actual is resolved at the point of instantiation. Create an
11096 -- identifier or operator with the same name as the formal.
11098 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11099 Nam :=
11100 Make_Operator_Symbol (Loc,
11101 Chars => Chars (Formal_Sub),
11102 Strval => No_String);
11103 else
11104 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11105 end if;
11107 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11108 and then Null_Present (Specification (Formal))
11109 then
11110 -- Generate null body for procedure, for use in the instance
11112 Decl_Node :=
11113 Make_Subprogram_Body (Loc,
11114 Specification => New_Spec,
11115 Declarations => New_List,
11116 Handled_Statement_Sequence =>
11117 Make_Handled_Sequence_Of_Statements (Loc,
11118 Statements => New_List (Make_Null_Statement (Loc))));
11120 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
11122 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11124 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11126 -- Eliminate the calls to it when optimization is enabled
11128 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11129 return Decl_Node;
11131 -- Handle case of a formal function with an expression default (allowed
11132 -- when extensions are enabled).
11134 elsif Nkind (Specification (Formal)) = N_Function_Specification
11135 and then Present (Expression (Formal))
11136 then
11137 -- Generate body for function, for use in the instance
11139 declare
11140 Expr : constant Node_Id := New_Copy (Expression (Formal));
11141 Stmt : constant Node_Id := Make_Simple_Return_Statement (Loc);
11142 begin
11143 Set_Sloc (Expr, Loc);
11144 Set_Expression (Stmt, Expr);
11146 Decl_Node :=
11147 Make_Subprogram_Body (Loc,
11148 Specification => New_Spec,
11149 Declarations => New_List,
11150 Handled_Statement_Sequence =>
11151 Make_Handled_Sequence_Of_Statements (Loc,
11152 Statements => New_List (Stmt)));
11153 end;
11155 -- RM 12.6 (16.2/2): Like a null procedure default, the function
11156 -- has convention Intrinsic.
11158 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11160 -- Inline calls to it when optimization is enabled
11162 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11163 return Decl_Node;
11165 else
11166 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11167 Error_Msg_NE
11168 ("missing actual&", Instantiation_Node, Formal_Sub);
11169 Error_Msg_NE
11170 ("\in instantiation of & declared#",
11171 Instantiation_Node, Scope (Analyzed_S));
11172 Abandon_Instantiation (Instantiation_Node);
11173 end if;
11175 Decl_Node :=
11176 Make_Subprogram_Renaming_Declaration (Loc,
11177 Specification => New_Spec,
11178 Name => Nam);
11180 -- If we do not have an actual and the formal specified <> then set to
11181 -- get proper default.
11183 if No (Actual) and then Box_Present (Formal) then
11184 Set_From_Default (Decl_Node);
11185 end if;
11187 -- Gather possible interpretations for the actual before analyzing the
11188 -- instance. If overloaded, it will be resolved when analyzing the
11189 -- renaming declaration.
11191 if Box_Present (Formal) and then No (Actual) then
11192 Analyze (Nam);
11194 if Is_Child_Unit (Scope (Analyzed_S))
11195 and then Present (Entity (Nam))
11196 then
11197 if not Is_Overloaded (Nam) then
11198 if From_Parent_Scope (Entity (Nam)) then
11199 Set_Is_Immediately_Visible (Entity (Nam), False);
11200 Set_Entity (Nam, Empty);
11201 Set_Etype (Nam, Empty);
11203 Analyze (Nam);
11204 Set_Is_Immediately_Visible (Entity (Nam));
11205 end if;
11207 else
11208 declare
11209 I : Interp_Index;
11210 It : Interp;
11212 begin
11213 Get_First_Interp (Nam, I, It);
11214 while Present (It.Nam) loop
11215 if From_Parent_Scope (It.Nam) then
11216 Remove_Interp (I);
11217 end if;
11219 Get_Next_Interp (I, It);
11220 end loop;
11221 end;
11222 end if;
11223 end if;
11224 end if;
11226 -- The generic instantiation freezes the actual. This can only be done
11227 -- once the actual is resolved, in the analysis of the renaming
11228 -- declaration. To make the formal subprogram entity available, we set
11229 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11230 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11231 -- of formal abstract subprograms.
11233 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11235 -- We cannot analyze the renaming declaration, and thus find the actual,
11236 -- until all the actuals are assembled in the instance. For subsequent
11237 -- checks of other actuals, indicate the node that will hold the
11238 -- instance of this formal.
11240 Set_Instance_Of (Analyzed_S, Nam);
11242 if Nkind (Actual) = N_Selected_Component
11243 and then Is_Task_Type (Etype (Prefix (Actual)))
11244 and then not Is_Frozen (Etype (Prefix (Actual)))
11245 then
11246 -- The renaming declaration will create a body, which must appear
11247 -- outside of the instantiation, We move the renaming declaration
11248 -- out of the instance, and create an additional renaming inside,
11249 -- to prevent freezing anomalies.
11251 declare
11252 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11254 begin
11255 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11256 Insert_Before (Instantiation_Node, Decl_Node);
11257 Analyze (Decl_Node);
11259 -- Now create renaming within the instance
11261 Decl_Node :=
11262 Make_Subprogram_Renaming_Declaration (Loc,
11263 Specification => New_Copy_Tree (New_Spec),
11264 Name => New_Occurrence_Of (Anon_Id, Loc));
11266 Set_Defining_Unit_Name (Specification (Decl_Node),
11267 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11268 end;
11269 end if;
11271 return Decl_Node;
11272 end Instantiate_Formal_Subprogram;
11274 ------------------------
11275 -- Instantiate_Object --
11276 ------------------------
11278 function Instantiate_Object
11279 (Formal : Node_Id;
11280 Actual : Node_Id;
11281 Analyzed_Formal : Node_Id) return List_Id
11283 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11284 A_Gen_Obj : constant Entity_Id :=
11285 Defining_Identifier (Analyzed_Formal);
11286 Acc_Def : Node_Id := Empty;
11287 Act_Assoc : constant Node_Id :=
11288 (if No (Actual) then Empty else Parent (Actual));
11289 Actual_Decl : Node_Id := Empty;
11290 Decl_Node : Node_Id;
11291 Def : Node_Id;
11292 Ftyp : Entity_Id;
11293 List : constant List_Id := New_List;
11294 Loc : constant Source_Ptr := Sloc (Actual);
11295 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11296 Subt_Decl : Node_Id := Empty;
11297 Subt_Mark : Node_Id := Empty;
11299 -- Start of processing for Instantiate_Object
11301 begin
11302 -- Formal may be an anonymous access
11304 if Present (Subtype_Mark (Formal)) then
11305 Subt_Mark := Subtype_Mark (Formal);
11306 else
11307 Check_Access_Definition (Formal);
11308 Acc_Def := Access_Definition (Formal);
11309 end if;
11311 -- Sloc for error message on missing actual
11313 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11315 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11316 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11317 end if;
11319 Set_Parent (List, Act_Assoc);
11321 -- OUT present
11323 if Out_Present (Formal) then
11325 -- An IN OUT generic actual must be a name. The instantiation is a
11326 -- renaming declaration. The actual is the name being renamed. We
11327 -- use the actual directly, rather than a copy, because it is not
11328 -- used further in the list of actuals, and because a copy or a use
11329 -- of relocate_node is incorrect if the instance is nested within a
11330 -- generic. In order to simplify e.g. ASIS queries, the
11331 -- Generic_Parent field links the declaration to the generic
11332 -- association.
11334 if No (Actual) then
11335 Error_Msg_NE
11336 ("missing actual &",
11337 Instantiation_Node, Gen_Obj);
11338 Error_Msg_NE
11339 ("\in instantiation of & declared#",
11340 Instantiation_Node, Scope (A_Gen_Obj));
11341 Abandon_Instantiation (Instantiation_Node);
11342 end if;
11344 if Present (Subt_Mark) then
11345 Decl_Node :=
11346 Make_Object_Renaming_Declaration (Loc,
11347 Defining_Identifier => New_Copy (Gen_Obj),
11348 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11349 Name => Actual);
11351 else pragma Assert (Present (Acc_Def));
11352 Decl_Node :=
11353 Make_Object_Renaming_Declaration (Loc,
11354 Defining_Identifier => New_Copy (Gen_Obj),
11355 Access_Definition => New_Copy_Tree (Acc_Def),
11356 Name => Actual);
11357 end if;
11359 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11361 -- The analysis of the actual may produce Insert_Action nodes, so
11362 -- the declaration must have a context in which to attach them.
11364 Append (Decl_Node, List);
11365 Analyze (Actual);
11367 -- Return if the analysis of the actual reported some error
11369 if Etype (Actual) = Any_Type then
11370 return List;
11371 end if;
11373 -- This check is performed here because Analyze_Object_Renaming will
11374 -- not check it when Comes_From_Source is False. Note though that the
11375 -- check for the actual being the name of an object will be performed
11376 -- in Analyze_Object_Renaming.
11378 if Is_Object_Reference (Actual)
11379 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11380 then
11381 Error_Msg_N
11382 ("illegal discriminant-dependent component for in out parameter",
11383 Actual);
11384 end if;
11386 -- The actual has to be resolved in order to check that it is a
11387 -- variable (due to cases such as F (1), where F returns access to
11388 -- an array, and for overloaded prefixes).
11390 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11392 -- If the type of the formal is not itself a formal, and the current
11393 -- unit is a child unit, the formal type must be declared in a
11394 -- parent, and must be retrieved by visibility.
11396 if Ftyp = Orig_Ftyp
11397 and then Is_Generic_Unit (Scope (Ftyp))
11398 and then Is_Child_Unit (Scope (A_Gen_Obj))
11399 then
11400 declare
11401 Temp : constant Node_Id :=
11402 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11403 begin
11404 Set_Entity (Temp, Empty);
11405 Find_Type (Temp);
11406 Ftyp := Entity (Temp);
11407 end;
11408 end if;
11410 if Is_Private_Type (Ftyp)
11411 and then not Is_Private_Type (Etype (Actual))
11412 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11413 or else Base_Type (Etype (Actual)) = Ftyp)
11414 then
11415 -- If the actual has the type of the full view of the formal, or
11416 -- else a non-private subtype of the formal, then the visibility
11417 -- of the formal type has changed. Add to the actuals a subtype
11418 -- declaration that will force the exchange of views in the body
11419 -- of the instance as well.
11421 Subt_Decl :=
11422 Make_Subtype_Declaration (Loc,
11423 Defining_Identifier => Make_Temporary (Loc, 'P'),
11424 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11426 Prepend (Subt_Decl, List);
11428 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11429 Exchange_Declarations (Ftyp);
11430 end if;
11432 Resolve (Actual, Ftyp);
11434 if not Denotes_Variable (Actual) then
11435 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11437 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11439 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11440 -- the type of the actual shall resolve to a specific anonymous
11441 -- access type.
11443 if Ada_Version < Ada_2005
11444 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11445 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11446 then
11447 Error_Msg_NE
11448 ("type of actual does not match type of&", Actual, Gen_Obj);
11449 end if;
11450 end if;
11452 Note_Possible_Modification (Actual, Sure => True);
11454 -- Check for instantiation with atomic/volatile/VFA object actual for
11455 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11457 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11458 Error_Msg_NE
11459 ("cannot instantiate nonatomic formal & of mode in out",
11460 Actual, Gen_Obj);
11461 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11463 elsif Is_Volatile_Object_Ref (Actual)
11464 and then not Is_Volatile (Orig_Ftyp)
11465 then
11466 Error_Msg_NE
11467 ("cannot instantiate nonvolatile formal & of mode in out",
11468 Actual, Gen_Obj);
11469 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11471 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
11472 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11473 then
11474 Error_Msg_NE
11475 ("cannot instantiate nonfull access formal & of mode in out",
11476 Actual, Gen_Obj);
11477 Error_Msg_N
11478 ("\with full access object actual (RM C.6(12))", Actual);
11479 end if;
11481 -- Check for instantiation on nonatomic subcomponent of a full access
11482 -- object in Ada 2022 (RM C.6 (12)).
11484 if Ada_Version >= Ada_2022
11485 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11486 and then not Is_Atomic_Object (Actual)
11487 then
11488 Error_Msg_NE
11489 ("cannot instantiate formal & of mode in out with actual",
11490 Actual, Gen_Obj);
11491 Error_Msg_N
11492 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11493 Actual);
11494 end if;
11496 -- Check actual/formal compatibility with respect to the four
11497 -- volatility refinement aspects.
11499 declare
11500 Actual_Obj : constant Entity_Id :=
11501 Get_Enclosing_Deep_Object (Actual);
11502 begin
11503 Check_Volatility_Compatibility
11504 (Actual_Obj, A_Gen_Obj, "actual object",
11505 "its corresponding formal object of mode in out",
11506 Srcpos_Bearer => Actual);
11507 end;
11509 -- The actual for a ghost generic formal IN OUT parameter should be a
11510 -- ghost object (SPARK RM 6.9(14)).
11512 Check_Ghost_Formal_Variable
11513 (Actual => Actual,
11514 Formal => A_Gen_Obj);
11516 -- Formal in-parameter
11518 else
11519 -- The instantiation of a generic formal in-parameter is constant
11520 -- declaration. The actual is the expression for that declaration.
11521 -- Its type is a full copy of the type of the formal. This may be
11522 -- an access to subprogram, for which we need to generate entities
11523 -- for the formals in the new signature.
11525 if Present (Actual) then
11526 if Present (Subt_Mark) then
11527 Def := New_Copy_Tree (Subt_Mark);
11528 else
11529 pragma Assert (Present (Acc_Def));
11530 Def := New_Copy_Tree (Acc_Def);
11531 end if;
11533 Decl_Node :=
11534 Make_Object_Declaration (Loc,
11535 Defining_Identifier => New_Copy (Gen_Obj),
11536 Constant_Present => True,
11537 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11538 Object_Definition => Def,
11539 Expression => Actual);
11541 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11542 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11544 -- A generic formal object of a tagged type is defined to be
11545 -- aliased so the new constant must also be treated as aliased.
11547 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11548 Set_Aliased_Present (Decl_Node);
11549 end if;
11551 Append (Decl_Node, List);
11553 -- The actual for a ghost generic formal IN parameter of
11554 -- access-to-variable type should be a ghost object (SPARK
11555 -- RM 6.9(14)).
11557 if Is_Access_Variable (Etype (A_Gen_Obj)) then
11558 Check_Ghost_Formal_Variable
11559 (Actual => Actual,
11560 Formal => A_Gen_Obj);
11561 end if;
11563 -- No need to repeat (pre-)analysis of some expression nodes
11564 -- already handled in Preanalyze_Actuals.
11566 if Nkind (Actual) /= N_Allocator then
11567 Analyze (Actual);
11569 -- Return if the analysis of the actual reported some error
11571 if Etype (Actual) = Any_Type then
11572 return List;
11573 end if;
11574 end if;
11576 declare
11577 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11578 Typ : Entity_Id;
11580 begin
11581 Typ := Get_Instance_Of (Formal_Type);
11583 -- If the actual appears in the current or an enclosing scope,
11584 -- use its type directly. This is relevant if it has an actual
11585 -- subtype that is distinct from its nominal one. This cannot
11586 -- be done in general because the type of the actual may
11587 -- depend on other actuals, and only be fully determined when
11588 -- the enclosing instance is analyzed.
11590 if Present (Etype (Actual))
11591 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11592 then
11593 Freeze_Before (Instantiation_Node, Etype (Actual));
11594 else
11595 Freeze_Before (Instantiation_Node, Typ);
11596 end if;
11598 -- If the actual is an aggregate, perform name resolution on
11599 -- its components (the analysis of an aggregate does not do it)
11600 -- to capture local names that may be hidden if the generic is
11601 -- a child unit.
11603 if Nkind (Actual) = N_Aggregate then
11604 Preanalyze_And_Resolve (Actual, Typ);
11605 end if;
11607 if Is_Limited_Type (Typ)
11608 and then not OK_For_Limited_Init (Typ, Actual)
11609 then
11610 Error_Msg_N
11611 ("initialization not allowed for limited types", Actual);
11612 Explain_Limited_Type (Typ, Actual);
11613 end if;
11614 end;
11616 elsif Present (Default_Expression (Formal)) then
11618 -- Use default to construct declaration
11620 if Present (Subt_Mark) then
11621 Def := New_Copy_Tree (Subt_Mark);
11622 else
11623 pragma Assert (Present (Acc_Def));
11624 Def := New_Copy_Tree (Acc_Def);
11625 end if;
11627 Decl_Node :=
11628 Make_Object_Declaration (Sloc (Formal),
11629 Defining_Identifier => New_Copy (Gen_Obj),
11630 Constant_Present => True,
11631 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11632 Object_Definition => Def,
11633 Expression => New_Copy_Tree
11634 (Default_Expression (Formal)));
11636 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11637 Set_Corresponding_Generic_Association
11638 (Decl_Node, Expression (Decl_Node));
11640 Append (Decl_Node, List);
11641 Set_Analyzed (Expression (Decl_Node), False);
11643 else
11644 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11645 Error_Msg_NE ("\in instantiation of & declared#",
11646 Instantiation_Node, Scope (A_Gen_Obj));
11648 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11650 -- Create dummy constant declaration so that instance can be
11651 -- analyzed, to minimize cascaded visibility errors.
11653 if Present (Subt_Mark) then
11654 Def := Subt_Mark;
11655 else pragma Assert (Present (Acc_Def));
11656 Def := Acc_Def;
11657 end if;
11659 Decl_Node :=
11660 Make_Object_Declaration (Loc,
11661 Defining_Identifier => New_Copy (Gen_Obj),
11662 Constant_Present => True,
11663 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11664 Object_Definition => New_Copy (Def),
11665 Expression =>
11666 Make_Attribute_Reference (Sloc (Gen_Obj),
11667 Attribute_Name => Name_First,
11668 Prefix => New_Copy (Def)));
11670 Append (Decl_Node, List);
11672 else
11673 Abandon_Instantiation (Instantiation_Node);
11674 end if;
11675 end if;
11676 end if;
11678 if Nkind (Actual) in N_Has_Entity
11679 and then Present (Entity (Actual))
11680 then
11681 Actual_Decl := Parent (Entity (Actual));
11682 end if;
11684 -- Ada 2005 (AI-423) refined by AI12-0287:
11685 -- For an object_renaming_declaration with a null_exclusion or an
11686 -- access_definition that has a null_exclusion, the subtype of the
11687 -- object_name shall exclude null. In addition, if the
11688 -- object_renaming_declaration occurs within the body of a generic unit
11689 -- G or within the body of a generic unit declared within the
11690 -- declarative region of generic unit G, then:
11691 -- * if the object_name statically denotes a generic formal object of
11692 -- mode in out of G, then the declaration of that object shall have a
11693 -- null_exclusion;
11694 -- * if the object_name statically denotes a call of a generic formal
11695 -- function of G, then the declaration of the result of that function
11696 -- shall have a null_exclusion.
11698 if Ada_Version >= Ada_2005
11699 and then Present (Actual_Decl)
11700 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11701 | N_Object_Declaration
11702 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11703 and then not Has_Null_Exclusion (Actual_Decl)
11704 and then Has_Null_Exclusion (Analyzed_Formal)
11705 and then Ekind (Defining_Identifier (Analyzed_Formal))
11706 = E_Generic_In_Out_Parameter
11707 and then ((In_Generic_Scope (Entity (Actual))
11708 and then In_Package_Body (Scope (Entity (Actual))))
11709 or else not Can_Never_Be_Null (Etype (Actual)))
11710 then
11711 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11712 Error_Msg_N
11713 ("actual must exclude null to match generic formal#", Actual);
11714 end if;
11716 -- An effectively volatile object cannot be used as an actual in a
11717 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11718 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11719 -- legality rule, and also verifies that the actual is an object.
11721 if SPARK_Mode = On
11722 and then Present (Actual)
11723 and then Is_Object_Reference (Actual)
11724 and then Is_Effectively_Volatile_Object (Actual)
11725 and then not Is_Effectively_Volatile (A_Gen_Obj)
11726 then
11727 Error_Msg_N
11728 ("volatile object cannot act as actual in generic instantiation",
11729 Actual);
11730 end if;
11732 return List;
11733 end Instantiate_Object;
11735 ------------------------------
11736 -- Instantiate_Package_Body --
11737 ------------------------------
11739 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11740 -- must be replaced by gotos which jump to the end of the routine in order
11741 -- to restore the Ghost and SPARK modes.
11743 procedure Instantiate_Package_Body
11744 (Body_Info : Pending_Body_Info;
11745 Inlined_Body : Boolean := False;
11746 Body_Optional : Boolean := False)
11748 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11749 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11750 Act_Spec : constant Node_Id := Specification (Act_Decl);
11751 Ctx_Parents : Elist_Id := No_Elist;
11752 Ctx_Top : Int := 0;
11753 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11754 Gen_Id : constant Node_Id := Name (Inst_Node);
11755 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11756 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11757 Loc : constant Source_Ptr := Sloc (Inst_Node);
11759 procedure Check_Initialized_Types;
11760 -- In a generic package body, an entity of a generic private type may
11761 -- appear uninitialized. This is suspicious, unless the actual is a
11762 -- fully initialized type.
11764 procedure Install_Parents_Of_Generic_Context
11765 (Inst_Scope : Entity_Id;
11766 Ctx_Parents : out Elist_Id);
11767 -- Inst_Scope is the scope where the instance appears within; when it
11768 -- appears within a generic child package G, this routine collects and
11769 -- installs the enclosing packages of G in the scopes stack; installed
11770 -- packages are returned in Ctx_Parents.
11772 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11773 -- Reverse effect after instantiation is complete
11775 -----------------------------
11776 -- Check_Initialized_Types --
11777 -----------------------------
11779 procedure Check_Initialized_Types is
11780 Decl : Node_Id;
11781 Formal : Entity_Id;
11782 Actual : Entity_Id;
11783 Uninit_Var : Entity_Id;
11785 begin
11786 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11787 while Present (Decl) loop
11788 Uninit_Var := Empty;
11790 if Nkind (Decl) = N_Private_Extension_Declaration then
11791 Uninit_Var := Uninitialized_Variable (Decl);
11793 elsif Nkind (Decl) = N_Formal_Type_Declaration
11794 and then Nkind (Formal_Type_Definition (Decl)) =
11795 N_Formal_Private_Type_Definition
11796 then
11797 Uninit_Var :=
11798 Uninitialized_Variable (Formal_Type_Definition (Decl));
11799 end if;
11801 if Present (Uninit_Var) then
11802 Formal := Defining_Identifier (Decl);
11803 Actual := First_Entity (Act_Decl_Id);
11805 -- For each formal there is a subtype declaration that renames
11806 -- the actual and has the same name as the formal. Locate the
11807 -- formal for warning message about uninitialized variables
11808 -- in the generic, for which the actual type should be a fully
11809 -- initialized type.
11811 while Present (Actual) loop
11812 exit when Ekind (Actual) = E_Package
11813 and then Present (Renamed_Entity (Actual));
11815 if Chars (Actual) = Chars (Formal)
11816 and then not Is_Scalar_Type (Actual)
11817 and then not Is_Fully_Initialized_Type (Actual)
11818 and then Warn_On_No_Value_Assigned
11819 then
11820 Error_Msg_Node_2 := Formal;
11821 Error_Msg_NE
11822 ("generic unit has uninitialized variable& of "
11823 & "formal private type &?v?", Actual, Uninit_Var);
11824 Error_Msg_NE
11825 ("actual type for& should be fully initialized type?v?",
11826 Actual, Formal);
11827 exit;
11828 end if;
11830 Next_Entity (Actual);
11831 end loop;
11832 end if;
11834 Next (Decl);
11835 end loop;
11836 end Check_Initialized_Types;
11838 ----------------------------------------
11839 -- Install_Parents_Of_Generic_Context --
11840 ----------------------------------------
11842 procedure Install_Parents_Of_Generic_Context
11843 (Inst_Scope : Entity_Id;
11844 Ctx_Parents : out Elist_Id)
11846 Elmt : Elmt_Id;
11847 S : Entity_Id;
11849 begin
11850 Ctx_Parents := New_Elmt_List;
11852 -- Collect context parents (ie. parents where the instantiation
11853 -- appears within).
11855 S := Inst_Scope;
11856 while S /= Standard_Standard loop
11857 Prepend_Elmt (S, Ctx_Parents);
11858 S := Scope (S);
11859 end loop;
11861 -- Install enclosing parents
11863 Elmt := First_Elmt (Ctx_Parents);
11864 while Present (Elmt) loop
11865 Push_Scope (Node (Elmt));
11866 Set_Is_Immediately_Visible (Node (Elmt));
11867 Next_Elmt (Elmt);
11868 end loop;
11869 end Install_Parents_Of_Generic_Context;
11871 ---------------------------------------
11872 -- Remove_Parents_Of_Generic_Context --
11873 ---------------------------------------
11875 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11876 Elmt : Elmt_Id;
11878 begin
11879 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11881 Elmt := Last_Elmt (Ctx_Parents);
11882 while Present (Elmt) loop
11883 pragma Assert (Current_Scope = Node (Elmt));
11884 Set_Is_Immediately_Visible (Current_Scope, False);
11885 Pop_Scope;
11887 Remove_Last_Elmt (Ctx_Parents);
11888 Elmt := Last_Elmt (Ctx_Parents);
11889 end loop;
11890 end Remove_Parents_Of_Generic_Context;
11892 -- Local variables
11894 -- The following constants capture the context prior to instantiating
11895 -- the package body.
11897 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11898 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11899 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11900 Saved_ISMP : constant Boolean :=
11901 Ignore_SPARK_Mode_Pragmas_In_Instance;
11902 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11903 Local_Suppress_Stack_Top;
11904 Saved_SC : constant Boolean := Style_Check;
11905 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11906 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11907 Saved_SS : constant Suppress_Record := Scope_Suppress;
11908 Saved_Warn : constant Warnings_State := Save_Warnings;
11910 Act_Body : Node_Id;
11911 Act_Body_Id : Entity_Id;
11912 Act_Body_Name : Node_Id;
11913 Gen_Body : Node_Id;
11914 Gen_Body_Id : Node_Id;
11915 Par_Ent : Entity_Id := Empty;
11916 Par_Installed : Boolean := False;
11917 Par_Vis : Boolean := False;
11919 Scope_Check_Id : Entity_Id;
11920 Scope_Check_Last : Nat;
11921 -- Value of Current_Scope before calls to Install_Parents; used to check
11922 -- that scopes are correctly removed after instantiation.
11924 Vis_Prims_List : Elist_Id := No_Elist;
11925 -- List of primitives made temporarily visible in the instantiation
11926 -- to match the visibility of the formal type.
11928 -- Start of processing for Instantiate_Package_Body
11930 begin
11931 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11933 -- The instance body may already have been processed, as the parent of
11934 -- another instance that is inlined (Load_Parent_Of_Generic).
11936 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11937 return;
11938 end if;
11940 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11942 -- Re-establish the state of information on which checks are suppressed.
11943 -- This information was set in Body_Info at the point of instantiation,
11944 -- and now we restore it so that the instance is compiled using the
11945 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11947 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11948 Scope_Suppress := Body_Info.Scope_Suppress;
11950 Restore_Config_Switches (Body_Info.Config_Switches);
11951 Restore_Warnings (Body_Info.Warnings);
11953 if No (Gen_Body_Id) then
11955 -- Do not look for parent of generic body if none is required.
11956 -- This may happen when the routine is called as part of the
11957 -- Pending_Instantiations processing, when nested instances
11958 -- may precede the one generated from the main unit.
11960 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11961 and then Body_Optional
11962 then
11963 goto Leave;
11964 else
11965 Load_Parent_Of_Generic
11966 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11968 -- Surprisingly enough, loading the body of the parent can cause
11969 -- the body to be instantiated and the double instantiation needs
11970 -- to be prevented in order to avoid giving bogus semantic errors.
11972 -- This case can occur because of the Collect_Previous_Instances
11973 -- machinery of Load_Parent_Of_Generic, which will instantiate
11974 -- bodies that are deemed to be ahead of the body of the parent
11975 -- in the compilation unit. But the relative position of these
11976 -- bodies is computed using the mere comparison of their Sloc.
11978 -- Now suppose that you have two generic packages G and H, with
11979 -- G containing a mere instantiation of H:
11981 -- generic
11982 -- package H is
11984 -- generic
11985 -- package Nested_G is
11986 -- ...
11987 -- end Nested_G;
11989 -- end H;
11991 -- with H;
11993 -- generic
11994 -- package G is
11996 -- package My_H is new H;
11998 -- end G;
12000 -- and a third package Q instantiating G and Nested_G:
12002 -- with G;
12004 -- package Q is
12006 -- package My_G is new G;
12008 -- package My_Nested_G is new My_G.My_H.Nested_G;
12010 -- end Q;
12012 -- The body to be instantiated is that of My_Nested_G and its
12013 -- parent is the instance My_G.My_H. This latter instantiation
12014 -- is done when My_G is analyzed, i.e. after the declarations
12015 -- of My_G and My_Nested_G have been parsed; as a result, the
12016 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
12018 -- Therefore loading the body of My_G.My_H will cause the body
12019 -- of My_Nested_G to be instantiated because it is deemed to be
12020 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
12021 -- will again be invoked on My_G.My_H, but this time with the
12022 -- Collect_Previous_Instances machinery disabled, so there is
12023 -- no endless mutual recursion and things are done in order.
12025 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12026 goto Leave;
12027 end if;
12029 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12030 end if;
12031 end if;
12033 -- Establish global variable for sloc adjustment and for error recovery
12034 -- In the case of an instance body for an instantiation with actuals
12035 -- from a limited view, the instance body is placed at the beginning
12036 -- of the enclosing package body: use the body entity as the source
12037 -- location for nodes of the instance body.
12039 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
12040 declare
12041 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12042 Body_Id : constant Node_Id :=
12043 Corresponding_Body (Unit_Declaration_Node (Scop));
12045 begin
12046 Instantiation_Node := Body_Id;
12047 end;
12048 else
12049 Instantiation_Node := Inst_Node;
12050 end if;
12052 -- The package being instantiated may be subject to pragma Ghost. Set
12053 -- the mode now to ensure that any nodes generated during instantiation
12054 -- are properly marked as Ghost.
12056 Set_Ghost_Mode (Act_Decl_Id);
12058 if Present (Gen_Body_Id) then
12059 Save_Env (Gen_Unit, Act_Decl_Id);
12060 Style_Check := False;
12062 -- If the context of the instance is subject to SPARK_Mode "off", the
12063 -- annotation is missing, or the body is instantiated at a later pass
12064 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12065 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12066 -- instance.
12068 if SPARK_Mode /= On
12069 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12070 then
12071 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12072 end if;
12074 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12075 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12077 Create_Instantiation_Source
12078 (Inst_Node, Gen_Body_Id, S_Adjustment);
12080 Act_Body :=
12081 Copy_Generic_Node
12082 (Original_Node (Gen_Body), Empty, Instantiating => True);
12084 -- Create proper (possibly qualified) defining name for the body, to
12085 -- correspond to the one in the spec.
12087 Act_Body_Id :=
12088 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12089 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12091 -- Some attributes of spec entity are not inherited by body entity
12093 Set_Handler_Records (Act_Body_Id, No_List);
12095 if Nkind (Defining_Unit_Name (Act_Spec)) =
12096 N_Defining_Program_Unit_Name
12097 then
12098 Act_Body_Name :=
12099 Make_Defining_Program_Unit_Name (Loc,
12100 Name =>
12101 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12102 Defining_Identifier => Act_Body_Id);
12103 else
12104 Act_Body_Name := Act_Body_Id;
12105 end if;
12107 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12109 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12110 Check_Generic_Actuals (Act_Decl_Id, False);
12111 Check_Initialized_Types;
12113 -- Install primitives hidden at the point of the instantiation but
12114 -- visible when processing the generic formals
12116 declare
12117 E : Entity_Id;
12119 begin
12120 E := First_Entity (Act_Decl_Id);
12121 while Present (E) loop
12122 if Is_Type (E)
12123 and then not Is_Itype (E)
12124 and then Is_Generic_Actual_Type (E)
12125 and then Is_Tagged_Type (E)
12126 then
12127 Install_Hidden_Primitives
12128 (Prims_List => Vis_Prims_List,
12129 Gen_T => Generic_Parent_Type (Parent (E)),
12130 Act_T => E);
12131 end if;
12133 Next_Entity (E);
12134 end loop;
12135 end;
12137 Scope_Check_Id := Current_Scope;
12138 Scope_Check_Last := Scope_Stack.Last;
12140 -- If the instantiation appears within a generic child some actual
12141 -- parameter may be the current instance of the enclosing generic
12142 -- parent.
12144 declare
12145 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12147 begin
12148 if Is_Child_Unit (Inst_Scope)
12149 and then Ekind (Inst_Scope) = E_Generic_Package
12150 and then Present (Generic_Associations (Inst_Node))
12151 then
12152 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12154 -- Hide them from visibility; required to avoid conflicts
12155 -- installing the parent instance.
12157 if Present (Ctx_Parents) then
12158 Push_Scope (Standard_Standard);
12159 Ctx_Top := Scope_Stack.Last;
12160 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12161 end if;
12162 end if;
12163 end;
12165 -- If it is a child unit, make the parent instance (which is an
12166 -- instance of the parent of the generic) visible.
12168 -- 1) The child unit's parent is an explicit parent instance (the
12169 -- prefix of the name of the generic unit):
12171 -- package Child_Package is new Parent_Instance.Child_Unit;
12173 -- 2) The child unit's parent is an implicit parent instance (e.g.
12174 -- when instantiating a sibling package):
12176 -- generic
12177 -- package Parent.Second_Child is
12178 -- ...
12180 -- generic
12181 -- package Parent.First_Child is
12182 -- package Sibling_Package is new Second_Child;
12184 -- 3) The child unit's parent is not an instance, so the scope is
12185 -- simply the one of the unit.
12187 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12188 and then Nkind (Gen_Id) = N_Expanded_Name
12189 then
12190 Par_Ent := Entity (Prefix (Gen_Id));
12192 elsif Ekind (Scope (Gen_Unit)) = E_Generic_Package
12193 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12194 and then Is_Generic_Instance (Scope (Act_Decl_Id))
12195 and then Nkind
12196 (Name (Get_Unit_Instantiation_Node
12197 (Scope (Act_Decl_Id)))) = N_Expanded_Name
12198 then
12199 Par_Ent := Entity
12200 (Prefix (Name (Get_Unit_Instantiation_Node
12201 (Scope (Act_Decl_Id)))));
12203 elsif Is_Child_Unit (Gen_Unit) then
12204 Par_Ent := Scope (Gen_Unit);
12205 end if;
12207 if Present (Par_Ent) then
12208 Par_Vis := Is_Immediately_Visible (Par_Ent);
12209 Install_Parent (Par_Ent, In_Body => True);
12210 Par_Installed := True;
12211 end if;
12213 -- If the instantiation is a library unit, and this is the main unit,
12214 -- then build the resulting compilation unit nodes for the instance.
12215 -- If this is a compilation unit but it is not the main unit, then it
12216 -- is the body of a unit in the context, that is being compiled
12217 -- because it is encloses some inlined unit or another generic unit
12218 -- being instantiated. In that case, this body is not part of the
12219 -- current compilation, and is not attached to the tree, but its
12220 -- parent must be set for analysis.
12222 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12224 -- Replace instance node with body of instance, and create new
12225 -- node for corresponding instance declaration.
12227 Build_Instance_Compilation_Unit_Nodes
12228 (Inst_Node, Act_Body, Act_Decl);
12230 -- If the instantiation appears within a generic child package
12231 -- enable visibility of current instance of enclosing generic
12232 -- parents.
12234 if Present (Ctx_Parents) then
12235 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12236 Analyze (Inst_Node);
12237 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12238 else
12239 Analyze (Inst_Node);
12240 end if;
12242 if Parent (Inst_Node) = Cunit (Main_Unit) then
12244 -- If the instance is a child unit itself, then set the scope
12245 -- of the expanded body to be the parent of the instantiation
12246 -- (ensuring that the fully qualified name will be generated
12247 -- for the elaboration subprogram).
12249 if Nkind (Defining_Unit_Name (Act_Spec)) =
12250 N_Defining_Program_Unit_Name
12251 then
12252 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12253 end if;
12254 end if;
12256 -- Case where instantiation is not a library unit
12258 else
12259 -- Handle the case of an instance with incomplete actual types.
12260 -- The instance body cannot be placed just after the declaration
12261 -- because full views have not been seen yet. Any use of the non-
12262 -- limited views in the instance body requires the presence of a
12263 -- regular with_clause in the enclosing unit. Therefore we place
12264 -- the instance body at the beginning of the enclosing body, and
12265 -- the freeze node for the instance is then placed after the body.
12267 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id))
12268 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12269 then
12270 declare
12271 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12272 Body_Id : constant Node_Id :=
12273 Corresponding_Body (Unit_Declaration_Node (Scop));
12275 F_Node : Node_Id;
12277 begin
12278 pragma Assert (Present (Body_Id));
12280 Prepend (Act_Body, Declarations (Parent (Body_Id)));
12282 if Expander_Active then
12283 Ensure_Freeze_Node (Act_Decl_Id);
12284 F_Node := Freeze_Node (Act_Decl_Id);
12285 Set_Is_Frozen (Act_Decl_Id, False);
12286 if Is_List_Member (F_Node) then
12287 Remove (F_Node);
12288 end if;
12290 Insert_After (Act_Body, F_Node);
12291 end if;
12292 end;
12294 else
12295 Insert_Before (Inst_Node, Act_Body);
12296 Mark_Rewrite_Insertion (Act_Body);
12298 -- Insert the freeze node for the instance if need be
12300 if Expander_Active then
12301 Freeze_Package_Instance
12302 (Inst_Node, Gen_Body, Gen_Decl, Act_Decl_Id);
12303 Set_Is_Frozen (Act_Decl_Id);
12304 end if;
12305 end if;
12307 -- If the instantiation appears within a generic child package
12308 -- enable visibility of current instance of enclosing generic
12309 -- parents.
12311 if Present (Ctx_Parents) then
12312 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12313 Analyze (Act_Body);
12314 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12315 else
12316 Analyze (Act_Body);
12317 end if;
12318 end if;
12320 Inherit_Context (Gen_Body, Inst_Node);
12322 if Par_Installed then
12323 Remove_Parent (In_Body => True);
12325 -- Restore the previous visibility of the parent
12327 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12328 end if;
12330 -- Remove the parent instances if they have been placed on the scope
12331 -- stack to compile the body.
12333 if Present (Ctx_Parents) then
12334 pragma Assert (Scope_Stack.Last = Ctx_Top
12335 and then Current_Scope = Standard_Standard);
12336 Pop_Scope;
12338 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12339 end if;
12341 pragma Assert (Current_Scope = Scope_Check_Id);
12342 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12344 Restore_Hidden_Primitives (Vis_Prims_List);
12346 -- Restore the private views that were made visible when the body of
12347 -- the instantiation was created. Note that, in the case where one of
12348 -- these private views is declared in the parent, there is a nesting
12349 -- issue with the calls to Install_Parent and Remove_Parent made in
12350 -- between above with In_Body set to True, because these calls also
12351 -- want to swap and restore this private view respectively. In this
12352 -- case, the call to Install_Parent does nothing, but the call to
12353 -- Remove_Parent does restore the private view, thus undercutting the
12354 -- call to Restore_Private_Views. That's OK under the condition that
12355 -- the two mechanisms swap exactly the same entities, in particular
12356 -- the private entities dependent on the primary private entities.
12358 Restore_Private_Views (Act_Decl_Id);
12360 -- Remove the current unit from visibility if this is an instance
12361 -- that is not elaborated on the fly for inlining purposes.
12363 if not Inlined_Body then
12364 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12365 end if;
12367 Restore_Env;
12369 -- If we have no body, and the unit requires a body, then complain. This
12370 -- complaint is suppressed if we have detected other errors (since a
12371 -- common reason for missing the body is that it had errors).
12372 -- In CodePeer mode, a warning has been emitted already, no need for
12373 -- further messages.
12375 elsif Unit_Requires_Body (Gen_Unit)
12376 and then not Body_Optional
12377 then
12378 if CodePeer_Mode then
12379 null;
12381 elsif Serious_Errors_Detected = 0 then
12382 Error_Msg_NE
12383 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12385 -- Don't attempt to perform any cleanup actions if some other error
12386 -- was already detected, since this can cause blowups.
12388 else
12389 goto Leave;
12390 end if;
12392 -- Case of package that does not need a body
12394 else
12395 -- If the instantiation of the declaration is a library unit, rewrite
12396 -- the original package instantiation as a package declaration in the
12397 -- compilation unit node.
12399 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12400 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12401 Rewrite (Inst_Node, Act_Decl);
12403 -- Generate elaboration entity, in case spec has elaboration code.
12404 -- This cannot be done when the instance is analyzed, because it
12405 -- is not known yet whether the body exists.
12407 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12408 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12410 -- If the instantiation is not a library unit, then append the
12411 -- declaration to the list of implicitly generated entities, unless
12412 -- it is already a list member which means that it was already
12413 -- processed
12415 elsif not Is_List_Member (Act_Decl) then
12416 Mark_Rewrite_Insertion (Act_Decl);
12417 Insert_Before (Inst_Node, Act_Decl);
12418 end if;
12419 end if;
12421 <<Leave>>
12423 -- Restore the context that was in effect prior to instantiating the
12424 -- package body.
12426 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12427 Local_Suppress_Stack_Top := Saved_LSST;
12428 Scope_Suppress := Saved_SS;
12429 Style_Check := Saved_SC;
12431 Expander_Mode_Restore;
12432 Restore_Config_Switches (Saved_CS);
12433 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12434 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12435 Restore_Warnings (Saved_Warn);
12436 end Instantiate_Package_Body;
12438 ---------------------------------
12439 -- Instantiate_Subprogram_Body --
12440 ---------------------------------
12442 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12443 -- must be replaced by gotos which jump to the end of the routine in order
12444 -- to restore the Ghost and SPARK modes.
12446 procedure Instantiate_Subprogram_Body
12447 (Body_Info : Pending_Body_Info;
12448 Body_Optional : Boolean := False)
12450 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12451 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12452 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12453 Gen_Id : constant Node_Id := Name (Inst_Node);
12454 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12455 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12456 Loc : constant Source_Ptr := Sloc (Inst_Node);
12457 Pack_Id : constant Entity_Id :=
12458 Defining_Unit_Name (Parent (Act_Decl));
12460 -- The following constants capture the context prior to instantiating
12461 -- the subprogram body.
12463 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12464 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12465 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12466 Saved_ISMP : constant Boolean :=
12467 Ignore_SPARK_Mode_Pragmas_In_Instance;
12468 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12469 Local_Suppress_Stack_Top;
12470 Saved_SC : constant Boolean := Style_Check;
12471 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12472 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12473 Saved_SS : constant Suppress_Record := Scope_Suppress;
12474 Saved_Warn : constant Warnings_State := Save_Warnings;
12476 Act_Body : Node_Id;
12477 Act_Body_Id : Entity_Id;
12478 Gen_Body : Node_Id;
12479 Gen_Body_Id : Node_Id;
12480 Pack_Body : Node_Id;
12481 Par_Ent : Entity_Id := Empty;
12482 Par_Installed : Boolean := False;
12483 Par_Vis : Boolean := False;
12484 Ret_Expr : Node_Id;
12486 begin
12487 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12489 -- Subprogram body may have been created already because of an inline
12490 -- pragma, or because of multiple elaborations of the enclosing package
12491 -- when several instances of the subprogram appear in the main unit.
12493 if Present (Corresponding_Body (Act_Decl)) then
12494 return;
12495 end if;
12497 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12499 -- Re-establish the state of information on which checks are suppressed.
12500 -- This information was set in Body_Info at the point of instantiation,
12501 -- and now we restore it so that the instance is compiled using the
12502 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12504 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12505 Scope_Suppress := Body_Info.Scope_Suppress;
12507 Restore_Config_Switches (Body_Info.Config_Switches);
12508 Restore_Warnings (Body_Info.Warnings);
12510 if No (Gen_Body_Id) then
12512 -- For imported generic subprogram, no body to compile, complete
12513 -- the spec entity appropriately.
12515 if Is_Imported (Gen_Unit) then
12516 Set_Is_Imported (Act_Decl_Id);
12517 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12518 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12519 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12520 Set_Has_Completion (Act_Decl_Id);
12521 goto Leave;
12523 -- For other cases, compile the body
12525 else
12526 Load_Parent_Of_Generic
12527 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12528 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12529 end if;
12530 end if;
12532 Instantiation_Node := Inst_Node;
12534 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12535 -- the mode now to ensure that any nodes generated during instantiation
12536 -- are properly marked as Ghost.
12538 Set_Ghost_Mode (Act_Decl_Id);
12540 if Present (Gen_Body_Id) then
12541 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12543 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12545 -- Either body is not present, or context is non-expanding, as
12546 -- when compiling a subunit. Mark the instance as completed, and
12547 -- diagnose a missing body when needed.
12549 if Expander_Active
12550 and then Operating_Mode = Generate_Code
12551 then
12552 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12553 end if;
12555 Set_Has_Completion (Act_Decl_Id);
12556 goto Leave;
12557 end if;
12559 Save_Env (Gen_Unit, Act_Decl_Id);
12560 Style_Check := False;
12562 -- If the context of the instance is subject to SPARK_Mode "off", the
12563 -- annotation is missing, or the body is instantiated at a later pass
12564 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12565 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12566 -- instance.
12568 if SPARK_Mode /= On
12569 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12570 then
12571 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12572 end if;
12574 -- If the context of an instance is not subject to SPARK_Mode "off",
12575 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12576 -- the latter should be the one applicable to the instance.
12578 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12579 and then SPARK_Mode /= Off
12580 and then Present (SPARK_Pragma (Gen_Body_Id))
12581 then
12582 Set_SPARK_Mode (Gen_Body_Id);
12583 end if;
12585 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12586 Create_Instantiation_Source
12587 (Inst_Node,
12588 Gen_Body_Id,
12589 S_Adjustment);
12591 Act_Body :=
12592 Copy_Generic_Node
12593 (Original_Node (Gen_Body), Empty, Instantiating => True);
12595 -- Create proper defining name for the body, to correspond to the one
12596 -- in the spec.
12598 Act_Body_Id :=
12599 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12601 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12602 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12604 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12605 Set_Has_Completion (Act_Decl_Id);
12606 Check_Generic_Actuals (Pack_Id, False);
12608 -- Generate a reference to link the visible subprogram instance to
12609 -- the generic body, which for navigation purposes is the only
12610 -- available source for the instance.
12612 Generate_Reference
12613 (Related_Instance (Pack_Id),
12614 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12616 -- If it is a child unit, make the parent instance (which is an
12617 -- instance of the parent of the generic) visible. The parent
12618 -- instance is the prefix of the name of the generic unit.
12620 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12621 and then Nkind (Gen_Id) = N_Expanded_Name
12622 then
12623 Par_Ent := Entity (Prefix (Gen_Id));
12624 elsif Is_Child_Unit (Gen_Unit) then
12625 Par_Ent := Scope (Gen_Unit);
12626 end if;
12628 if Present (Par_Ent) then
12629 Par_Vis := Is_Immediately_Visible (Par_Ent);
12630 Install_Parent (Par_Ent, In_Body => True);
12631 Par_Installed := True;
12632 end if;
12634 -- Subprogram body is placed in the body of wrapper package,
12635 -- whose spec contains the subprogram declaration as well as
12636 -- the renaming declarations for the generic parameters.
12638 Pack_Body :=
12639 Make_Package_Body (Loc,
12640 Defining_Unit_Name => New_Copy (Pack_Id),
12641 Declarations => New_List (Act_Body));
12643 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12645 -- If the instantiation is a library unit, then build resulting
12646 -- compilation unit nodes for the instance. The declaration of
12647 -- the enclosing package is the grandparent of the subprogram
12648 -- declaration. First replace the instantiation node as the unit
12649 -- of the corresponding compilation.
12651 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12652 if Parent (Inst_Node) = Cunit (Main_Unit) then
12653 Set_Unit (Parent (Inst_Node), Inst_Node);
12654 Build_Instance_Compilation_Unit_Nodes
12655 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12656 Analyze (Inst_Node);
12657 else
12658 Set_Parent (Pack_Body, Parent (Inst_Node));
12659 Analyze (Pack_Body);
12660 end if;
12662 else
12663 Insert_Before (Inst_Node, Pack_Body);
12664 Mark_Rewrite_Insertion (Pack_Body);
12666 -- Insert the freeze node for the instance if need be
12668 if Expander_Active then
12669 Freeze_Subprogram_Instance (Inst_Node, Gen_Body, Pack_Id);
12670 end if;
12672 Analyze (Pack_Body);
12673 end if;
12675 Inherit_Context (Gen_Body, Inst_Node);
12677 Restore_Private_Views (Pack_Id, False);
12679 if Par_Installed then
12680 Remove_Parent (In_Body => True);
12682 -- Restore the previous visibility of the parent
12684 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12685 end if;
12687 Restore_Env;
12689 -- Body not found. Error was emitted already. If there were no previous
12690 -- errors, this may be an instance whose scope is a premature instance.
12691 -- In that case we must insure that the (legal) program does raise
12692 -- program error if executed. We generate a subprogram body for this
12693 -- purpose.
12695 elsif Serious_Errors_Detected = 0
12696 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12697 then
12698 if Body_Optional then
12699 goto Leave;
12701 elsif Ekind (Act_Decl_Id) = E_Procedure then
12702 Act_Body :=
12703 Make_Subprogram_Body (Loc,
12704 Specification =>
12705 Make_Procedure_Specification (Loc,
12706 Defining_Unit_Name =>
12707 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12708 Parameter_Specifications =>
12709 New_Copy_List
12710 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12712 Declarations => Empty_List,
12713 Handled_Statement_Sequence =>
12714 Make_Handled_Sequence_Of_Statements (Loc,
12715 Statements => New_List (
12716 Make_Raise_Program_Error (Loc,
12717 Reason => PE_Access_Before_Elaboration))));
12719 else
12720 Ret_Expr :=
12721 Make_Raise_Program_Error (Loc,
12722 Reason => PE_Access_Before_Elaboration);
12724 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12725 Set_Analyzed (Ret_Expr);
12727 Act_Body :=
12728 Make_Subprogram_Body (Loc,
12729 Specification =>
12730 Make_Function_Specification (Loc,
12731 Defining_Unit_Name =>
12732 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12733 Parameter_Specifications =>
12734 New_Copy_List
12735 (Parameter_Specifications (Parent (Act_Decl_Id))),
12736 Result_Definition =>
12737 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12739 Declarations => Empty_List,
12740 Handled_Statement_Sequence =>
12741 Make_Handled_Sequence_Of_Statements (Loc,
12742 Statements => New_List (
12743 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12744 end if;
12746 Pack_Body :=
12747 Make_Package_Body (Loc,
12748 Defining_Unit_Name => New_Copy (Pack_Id),
12749 Declarations => New_List (Act_Body));
12751 Insert_After (Inst_Node, Pack_Body);
12752 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12753 Analyze (Pack_Body);
12754 end if;
12756 <<Leave>>
12758 -- Restore the context that was in effect prior to instantiating the
12759 -- subprogram body.
12761 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12762 Local_Suppress_Stack_Top := Saved_LSST;
12763 Scope_Suppress := Saved_SS;
12764 Style_Check := Saved_SC;
12766 Expander_Mode_Restore;
12767 Restore_Config_Switches (Saved_CS);
12768 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12769 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12770 Restore_Warnings (Saved_Warn);
12771 end Instantiate_Subprogram_Body;
12773 ----------------------
12774 -- Instantiate_Type --
12775 ----------------------
12777 function Instantiate_Type
12778 (Formal : Node_Id;
12779 Actual : Node_Id;
12780 Analyzed_Formal : Node_Id;
12781 Actual_Decls : List_Id) return List_Id
12783 A_Gen_T : constant Entity_Id :=
12784 Defining_Identifier (Analyzed_Formal);
12785 Def : constant Node_Id := Formal_Type_Definition (Formal);
12786 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12787 Act_T : Entity_Id;
12788 Ancestor : Entity_Id := Empty;
12789 Decl_Node : Node_Id;
12790 Decl_Nodes : List_Id;
12791 Loc : Source_Ptr;
12792 Subt : Entity_Id;
12794 procedure Check_Shared_Variable_Control_Aspects;
12795 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12796 -- that may be specified for a formal type are obeyed by the actual.
12798 procedure Diagnose_Predicated_Actual;
12799 -- There are a number of constructs in which a discrete type with
12800 -- predicates is illegal, e.g. as an index in an array type declaration.
12801 -- If a generic type is used is such a construct in a generic package
12802 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12803 -- of the generic contract that the actual cannot have predicates.
12805 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12806 -- Check that base types are the same and that the subtypes match
12807 -- statically. Used in several of the validation subprograms for
12808 -- actuals in instantiations.
12810 procedure Validate_Array_Type_Instance;
12811 procedure Validate_Access_Subprogram_Instance;
12812 procedure Validate_Access_Type_Instance;
12813 procedure Validate_Derived_Type_Instance;
12814 procedure Validate_Derived_Interface_Type_Instance;
12815 procedure Validate_Discriminated_Formal_Type;
12816 procedure Validate_Interface_Type_Instance;
12817 procedure Validate_Private_Type_Instance;
12818 procedure Validate_Incomplete_Type_Instance;
12819 -- These procedures perform validation tests for the named case.
12820 -- Validate_Discriminated_Formal_Type is shared by formal private
12821 -- types and Ada 2012 formal incomplete types.
12823 --------------------------------------------
12824 -- Check_Shared_Variable_Control_Aspects --
12825 --------------------------------------------
12827 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12828 -- that may be specified for the formal are obeyed by the actual.
12829 -- If the formal is a derived type the aspect specifications must match.
12830 -- NOTE: AI12-0282 implies that matching of aspects is required between
12831 -- formal and actual in all cases, but this is too restrictive.
12832 -- In particular it violates a language design rule: a limited private
12833 -- indefinite formal can be matched by any actual. The current code
12834 -- reflects an older and more permissive version of RM C.6 (12/5).
12836 procedure Check_Shared_Variable_Control_Aspects is
12837 begin
12838 if Ada_Version >= Ada_2022 then
12839 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12840 Error_Msg_NE
12841 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12843 elsif Is_Derived_Type (A_Gen_T)
12844 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12845 then
12846 Error_Msg_NE
12847 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12848 end if;
12850 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12851 Error_Msg_NE
12852 ("actual for& must have Volatile aspect",
12853 Actual, A_Gen_T);
12855 elsif Is_Derived_Type (A_Gen_T)
12856 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12857 then
12858 Error_Msg_NE
12859 ("actual for& has different Volatile aspect",
12860 Actual, A_Gen_T);
12861 end if;
12863 -- We assume that an array type whose atomic component type
12864 -- is Atomic is equivalent to an array type with the explicit
12865 -- aspect Has_Atomic_Components. This is a reasonable inference
12866 -- from the intent of AI12-0282, and makes it legal to use an
12867 -- actual that does not have the identical aspect as the formal.
12868 -- Ditto for volatile components.
12870 declare
12871 Actual_Atomic_Comp : constant Boolean :=
12872 Has_Atomic_Components (Act_T)
12873 or else (Is_Array_Type (Act_T)
12874 and then Is_Atomic (Component_Type (Act_T)));
12875 begin
12876 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12877 Error_Msg_NE
12878 ("formal and actual for& must agree on atomic components",
12879 Actual, A_Gen_T);
12880 end if;
12881 end;
12883 declare
12884 Actual_Volatile_Comp : constant Boolean :=
12885 Has_Volatile_Components (Act_T)
12886 or else (Is_Array_Type (Act_T)
12887 and then Is_Volatile (Component_Type (Act_T)));
12888 begin
12889 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12890 then
12891 Error_Msg_NE
12892 ("actual for& must have volatile components",
12893 Actual, A_Gen_T);
12894 end if;
12895 end;
12897 -- The following two aspects do not require exact matching,
12898 -- but only one-way agreement. See RM C.6.
12900 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12901 then
12902 Error_Msg_NE
12903 ("actual for& must have Independent aspect specified",
12904 Actual, A_Gen_T);
12905 end if;
12907 if Has_Independent_Components (A_Gen_T)
12908 and then not Has_Independent_Components (Act_T)
12909 then
12910 Error_Msg_NE
12911 ("actual for& must have Independent_Components specified",
12912 Actual, A_Gen_T);
12913 end if;
12915 -- Check actual/formal compatibility with respect to the four
12916 -- volatility refinement aspects.
12918 Check_Volatility_Compatibility
12919 (Act_T, A_Gen_T,
12920 "actual type", "its corresponding formal type",
12921 Srcpos_Bearer => Actual);
12922 end if;
12923 end Check_Shared_Variable_Control_Aspects;
12925 ---------------------------------
12926 -- Diagnose_Predicated_Actual --
12927 ---------------------------------
12929 procedure Diagnose_Predicated_Actual is
12930 begin
12931 if No_Predicate_On_Actual (A_Gen_T)
12932 and then Has_Predicates (Act_T)
12933 then
12934 Error_Msg_NE
12935 ("actual for& cannot be a type with predicate",
12936 Instantiation_Node, A_Gen_T);
12938 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12939 and then Has_Predicates (Act_T)
12940 and then not Has_Static_Predicate_Aspect (Act_T)
12941 then
12942 Error_Msg_NE
12943 ("actual for& cannot be a type with a dynamic predicate",
12944 Instantiation_Node, A_Gen_T);
12945 end if;
12946 end Diagnose_Predicated_Actual;
12948 --------------------
12949 -- Subtypes_Match --
12950 --------------------
12952 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12953 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12955 begin
12956 -- Check that the base types, root types (when dealing with class
12957 -- wide types), or designated types (when dealing with anonymous
12958 -- access types) of Gen_T and Act_T are statically matching subtypes.
12960 return ((Base_Type (T) = Act_T
12961 or else Base_Type (T) = Base_Type (Act_T))
12962 and then Subtypes_Statically_Match (T, Act_T))
12964 or else (Is_Class_Wide_Type (Gen_T)
12965 and then Is_Class_Wide_Type (Act_T)
12966 and then Subtypes_Match
12967 (Get_Instance_Of (Root_Type (Gen_T)),
12968 Root_Type (Act_T)))
12970 or else (Is_Anonymous_Access_Type (Gen_T)
12971 and then Ekind (Act_T) = Ekind (Gen_T)
12972 and then Subtypes_Statically_Match
12973 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12974 end Subtypes_Match;
12976 -----------------------------------------
12977 -- Validate_Access_Subprogram_Instance --
12978 -----------------------------------------
12980 procedure Validate_Access_Subprogram_Instance is
12981 begin
12982 if not Is_Access_Type (Act_T)
12983 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12984 then
12985 Error_Msg_NE
12986 ("expect access type in instantiation of &", Actual, Gen_T);
12987 Abandon_Instantiation (Actual);
12988 end if;
12990 -- According to AI05-288, actuals for access_to_subprograms must be
12991 -- subtype conformant with the generic formal. Previous to AI05-288
12992 -- only mode conformance was required.
12994 -- This is a binding interpretation that applies to previous versions
12995 -- of the language, no need to maintain previous weaker checks.
12997 Check_Subtype_Conformant
12998 (Designated_Type (Act_T),
12999 Designated_Type (A_Gen_T),
13000 Actual,
13001 Get_Inst => True);
13003 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
13004 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
13005 Error_Msg_NE
13006 ("protected access type not allowed for formal &",
13007 Actual, Gen_T);
13008 end if;
13010 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
13011 Error_Msg_NE
13012 ("expect protected access type for formal &",
13013 Actual, Gen_T);
13014 end if;
13016 -- If the formal has a specified convention (which in most cases
13017 -- will be StdCall) verify that the actual has the same convention.
13019 if Has_Convention_Pragma (A_Gen_T)
13020 and then Convention (A_Gen_T) /= Convention (Act_T)
13021 then
13022 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
13023 Error_Msg_NE
13024 ("actual for formal & must have convention %", Actual, Gen_T);
13025 end if;
13027 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13028 Error_Msg_NE
13029 ("non null exclusion of actual and formal & do not match",
13030 Actual, Gen_T);
13031 end if;
13032 end Validate_Access_Subprogram_Instance;
13034 -----------------------------------
13035 -- Validate_Access_Type_Instance --
13036 -----------------------------------
13038 procedure Validate_Access_Type_Instance is
13039 Desig_Type : constant Entity_Id :=
13040 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
13041 Desig_Act : Entity_Id;
13043 begin
13044 if not Is_Access_Type (Act_T) then
13045 Error_Msg_NE
13046 ("expect access type in instantiation of &", Actual, Gen_T);
13047 Abandon_Instantiation (Actual);
13048 end if;
13050 if Is_Access_Constant (A_Gen_T) then
13051 if not Is_Access_Constant (Act_T) then
13052 Error_Msg_N
13053 ("actual type must be access-to-constant type", Actual);
13054 Abandon_Instantiation (Actual);
13055 end if;
13056 else
13057 if Is_Access_Constant (Act_T) then
13058 Error_Msg_N
13059 ("actual type must be access-to-variable type", Actual);
13060 Abandon_Instantiation (Actual);
13062 elsif Ekind (A_Gen_T) = E_General_Access_Type
13063 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
13064 then
13065 Error_Msg_N
13066 ("actual must be general access type!", Actual);
13067 Error_Msg_NE -- CODEFIX
13068 ("\add ALL to }!", Actual, Act_T);
13069 Abandon_Instantiation (Actual);
13070 end if;
13071 end if;
13073 -- The designated subtypes, that is to say the subtypes introduced
13074 -- by an access type declaration (and not by a subtype declaration)
13075 -- must match.
13077 Desig_Act := Designated_Type (Base_Type (Act_T));
13079 -- The designated type may have been introduced through a limited_
13080 -- with clause, in which case retrieve the non-limited view. This
13081 -- applies to incomplete types as well as to class-wide types.
13083 if From_Limited_With (Desig_Act) then
13084 Desig_Act := Available_View (Desig_Act);
13085 end if;
13087 if not Subtypes_Match (Desig_Type, Desig_Act) then
13088 Error_Msg_NE
13089 ("designated type of actual does not match that of formal &",
13090 Actual, Gen_T);
13092 if not Predicates_Match (Desig_Type, Desig_Act) then
13093 Error_Msg_N ("\predicates do not match", Actual);
13094 end if;
13096 Abandon_Instantiation (Actual);
13097 end if;
13099 -- Ada 2005: null-exclusion indicators of the two types must agree
13101 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13102 Error_Msg_NE
13103 ("non null exclusion of actual and formal & do not match",
13104 Actual, Gen_T);
13105 end if;
13106 end Validate_Access_Type_Instance;
13108 ----------------------------------
13109 -- Validate_Array_Type_Instance --
13110 ----------------------------------
13112 procedure Validate_Array_Type_Instance is
13113 I1 : Node_Id;
13114 I2 : Node_Id;
13115 T2 : Entity_Id;
13117 function Formal_Dimensions return Nat;
13118 -- Count number of dimensions in array type formal
13120 -----------------------
13121 -- Formal_Dimensions --
13122 -----------------------
13124 function Formal_Dimensions return Nat is
13125 Num : Nat := 0;
13126 Index : Node_Id;
13128 begin
13129 if Nkind (Def) = N_Constrained_Array_Definition then
13130 Index := First (Discrete_Subtype_Definitions (Def));
13131 else
13132 Index := First (Subtype_Marks (Def));
13133 end if;
13135 while Present (Index) loop
13136 Num := Num + 1;
13137 Next (Index);
13138 end loop;
13140 return Num;
13141 end Formal_Dimensions;
13143 -- Start of processing for Validate_Array_Type_Instance
13145 begin
13146 if not Is_Array_Type (Act_T) then
13147 Error_Msg_NE
13148 ("expect array type in instantiation of &", Actual, Gen_T);
13149 Abandon_Instantiation (Actual);
13151 elsif Nkind (Def) = N_Constrained_Array_Definition then
13152 if not (Is_Constrained (Act_T)) then
13153 Error_Msg_NE
13154 ("expect constrained array in instantiation of &",
13155 Actual, Gen_T);
13156 Abandon_Instantiation (Actual);
13157 end if;
13159 else
13160 if Is_Constrained (Act_T) then
13161 Error_Msg_NE
13162 ("expect unconstrained array in instantiation of &",
13163 Actual, Gen_T);
13164 Abandon_Instantiation (Actual);
13165 end if;
13166 end if;
13168 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13169 Error_Msg_NE
13170 ("dimensions of actual do not match formal &", Actual, Gen_T);
13171 Abandon_Instantiation (Actual);
13172 end if;
13174 I1 := First_Index (A_Gen_T);
13175 I2 := First_Index (Act_T);
13176 for J in 1 .. Formal_Dimensions loop
13178 -- If the indexes of the actual were given by a subtype_mark,
13179 -- the index was transformed into a range attribute. Retrieve
13180 -- the original type mark for checking.
13182 if Is_Entity_Name (Original_Node (I2)) then
13183 T2 := Entity (Original_Node (I2));
13184 else
13185 T2 := Etype (I2);
13186 end if;
13188 if not Subtypes_Match
13189 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13190 then
13191 Error_Msg_NE
13192 ("index types of actual do not match those of formal &",
13193 Actual, Gen_T);
13194 Abandon_Instantiation (Actual);
13195 end if;
13197 Next_Index (I1);
13198 Next_Index (I2);
13199 end loop;
13201 -- Check matching subtypes. Note that there are complex visibility
13202 -- issues when the generic is a child unit and some aspect of the
13203 -- generic type is declared in a parent unit of the generic. We do
13204 -- the test to handle this special case only after a direct check
13205 -- for static matching has failed. The case where both the component
13206 -- type and the array type are separate formals, and the component
13207 -- type is a private view may also require special checking in
13208 -- Subtypes_Match. Finally, we assume that a child instance where
13209 -- the component type comes from a formal of a parent instance is
13210 -- correct because the generic was correct. A more precise check
13211 -- seems too complex to install???
13213 if Subtypes_Match
13214 (Component_Type (A_Gen_T), Component_Type (Act_T))
13215 or else
13216 Subtypes_Match
13217 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13218 Component_Type (Act_T))
13219 or else
13220 (not Inside_A_Generic
13221 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13222 then
13223 null;
13224 else
13225 Error_Msg_NE
13226 ("component subtype of actual does not match that of formal &",
13227 Actual, Gen_T);
13228 Abandon_Instantiation (Actual);
13229 end if;
13231 if Has_Aliased_Components (A_Gen_T)
13232 and then not Has_Aliased_Components (Act_T)
13233 then
13234 Error_Msg_NE
13235 ("actual must have aliased components to match formal type &",
13236 Actual, Gen_T);
13237 end if;
13238 end Validate_Array_Type_Instance;
13240 -----------------------------------------------
13241 -- Validate_Derived_Interface_Type_Instance --
13242 -----------------------------------------------
13244 procedure Validate_Derived_Interface_Type_Instance is
13245 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13246 Elmt : Elmt_Id;
13248 begin
13249 -- First apply interface instance checks
13251 Validate_Interface_Type_Instance;
13253 -- Verify that immediate parent interface is an ancestor of
13254 -- the actual.
13256 if Present (Par)
13257 and then not Interface_Present_In_Ancestor (Act_T, Par)
13258 then
13259 Error_Msg_NE
13260 ("interface actual must include progenitor&", Actual, Par);
13261 end if;
13263 -- Now verify that the actual includes all other ancestors of
13264 -- the formal.
13266 Elmt := First_Elmt (Interfaces (A_Gen_T));
13267 while Present (Elmt) loop
13268 if not Interface_Present_In_Ancestor
13269 (Act_T, Get_Instance_Of (Node (Elmt)))
13270 then
13271 Error_Msg_NE
13272 ("interface actual must include progenitor&",
13273 Actual, Node (Elmt));
13274 end if;
13276 Next_Elmt (Elmt);
13277 end loop;
13278 end Validate_Derived_Interface_Type_Instance;
13280 ------------------------------------
13281 -- Validate_Derived_Type_Instance --
13282 ------------------------------------
13284 procedure Validate_Derived_Type_Instance is
13285 Actual_Discr : Entity_Id;
13286 Ancestor_Discr : Entity_Id;
13288 begin
13289 -- Verify that the actual includes the progenitors of the formal,
13290 -- if any. The formal may depend on previous formals and their
13291 -- instance, so we must examine instance of interfaces if present.
13292 -- The actual may be an extension of an interface, in which case
13293 -- it does not appear in the interface list, so this must be
13294 -- checked separately.
13296 if Present (Interface_List (Def)) then
13297 if not Has_Interfaces (Act_T) then
13298 Error_Msg_NE
13299 ("actual must implement all interfaces of formal&",
13300 Actual, A_Gen_T);
13302 else
13303 declare
13304 Act_Iface_List : Elist_Id;
13305 Iface : Node_Id;
13306 Iface_Ent : Entity_Id;
13308 function Instance_Exists (I : Entity_Id) return Boolean;
13309 -- If the interface entity is declared in a generic unit,
13310 -- this can only be legal if we are within an instantiation
13311 -- of a child of that generic. There is currently no
13312 -- mechanism to relate an interface declared within a
13313 -- generic to the corresponding interface in an instance,
13314 -- so we traverse the list of interfaces of the actual,
13315 -- looking for a name match.
13317 ---------------------
13318 -- Instance_Exists --
13319 ---------------------
13321 function Instance_Exists (I : Entity_Id) return Boolean is
13322 Iface_Elmt : Elmt_Id;
13324 begin
13325 Iface_Elmt := First_Elmt (Act_Iface_List);
13326 while Present (Iface_Elmt) loop
13327 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13328 and then Chars (Node (Iface_Elmt)) = Chars (I)
13329 then
13330 return True;
13331 end if;
13333 Next_Elmt (Iface_Elmt);
13334 end loop;
13336 return False;
13337 end Instance_Exists;
13339 begin
13340 Iface := First (Abstract_Interface_List (A_Gen_T));
13341 Collect_Interfaces (Act_T, Act_Iface_List);
13343 while Present (Iface) loop
13344 Iface_Ent := Get_Instance_Of (Entity (Iface));
13346 if Is_Ancestor (Iface_Ent, Act_T)
13347 or else Is_Progenitor (Iface_Ent, Act_T)
13348 then
13349 null;
13351 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13352 and then Instance_Exists (Iface_Ent)
13353 then
13354 null;
13356 else
13357 Error_Msg_Name_1 := Chars (Act_T);
13358 Error_Msg_NE
13359 ("actual% must implement interface&",
13360 Actual, Etype (Iface));
13361 end if;
13363 Next (Iface);
13364 end loop;
13365 end;
13366 end if;
13367 end if;
13369 -- If the parent type in the generic declaration is itself a previous
13370 -- formal type, then it is local to the generic and absent from the
13371 -- analyzed generic definition. In that case the ancestor is the
13372 -- instance of the formal (which must have been instantiated
13373 -- previously), unless the ancestor is itself a formal derived type.
13374 -- In this latter case (which is the subject of Corrigendum 8652/0038
13375 -- (AI-202) the ancestor of the formals is the ancestor of its
13376 -- parent. Otherwise, the analyzed generic carries the parent type.
13377 -- If the parent type is defined in a previous formal package, then
13378 -- the scope of that formal package is that of the generic type
13379 -- itself, and it has already been mapped into the corresponding type
13380 -- in the actual package.
13382 -- Common case: parent type defined outside of the generic
13384 if Is_Entity_Name (Subtype_Mark (Def))
13385 and then Present (Entity (Subtype_Mark (Def)))
13386 then
13387 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13389 -- Check whether parent is defined in a previous formal package
13391 elsif
13392 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13393 then
13394 Ancestor :=
13395 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13397 -- The type may be a local derivation, or a type extension of a
13398 -- previous formal, or of a formal of a parent package.
13400 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13401 or else
13402 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13403 then
13404 -- Check whether the parent is another derived formal type in the
13405 -- same generic unit.
13407 if Etype (A_Gen_T) /= A_Gen_T
13408 and then Is_Generic_Type (Etype (A_Gen_T))
13409 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13410 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13411 then
13412 -- Locate ancestor of parent from the subtype declaration
13413 -- created for the actual.
13415 declare
13416 Decl : Node_Id;
13418 begin
13419 Decl := First (Actual_Decls);
13420 while Present (Decl) loop
13421 if Nkind (Decl) = N_Subtype_Declaration
13422 and then Chars (Defining_Identifier (Decl)) =
13423 Chars (Etype (A_Gen_T))
13424 then
13425 Ancestor := Generic_Parent_Type (Decl);
13426 exit;
13427 else
13428 Next (Decl);
13429 end if;
13430 end loop;
13431 end;
13433 pragma Assert (Present (Ancestor));
13435 -- The ancestor itself may be a previous formal that has been
13436 -- instantiated.
13438 Ancestor := Get_Instance_Of (Ancestor);
13440 else
13441 Ancestor :=
13442 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13443 end if;
13445 -- Check whether parent is a previous formal of the current generic
13447 elsif Is_Derived_Type (A_Gen_T)
13448 and then Is_Generic_Type (Etype (A_Gen_T))
13449 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13450 then
13451 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13453 -- An unusual case: the actual is a type declared in a parent unit,
13454 -- but is not a formal type so there is no instance_of for it.
13455 -- Retrieve it by analyzing the record extension.
13457 elsif Is_Child_Unit (Scope (A_Gen_T))
13458 and then In_Open_Scopes (Scope (Act_T))
13459 and then Is_Generic_Instance (Scope (Act_T))
13460 then
13461 Analyze (Subtype_Mark (Def));
13462 Ancestor := Entity (Subtype_Mark (Def));
13464 else
13465 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13466 end if;
13468 -- If the formal derived type has pragma Preelaborable_Initialization
13469 -- then the actual type must have preelaborable initialization.
13471 if Known_To_Have_Preelab_Init (A_Gen_T)
13472 and then not Has_Preelaborable_Initialization (Act_T)
13473 then
13474 Error_Msg_NE
13475 ("actual for & must have preelaborable initialization",
13476 Actual, Gen_T);
13477 end if;
13479 -- Ada 2005 (AI-251)
13481 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13482 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13483 Error_Msg_NE
13484 ("(Ada 2005) expected type implementing & in instantiation",
13485 Actual, Ancestor);
13486 end if;
13488 -- Finally verify that the (instance of) the ancestor is an ancestor
13489 -- of the actual.
13491 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13492 Error_Msg_NE
13493 ("expect type derived from & in instantiation",
13494 Actual, First_Subtype (Ancestor));
13495 Abandon_Instantiation (Actual);
13496 end if;
13498 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13499 -- that the formal type declaration has been rewritten as a private
13500 -- extension.
13502 if Ada_Version >= Ada_2005
13503 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13504 and then Synchronized_Present (Parent (A_Gen_T))
13505 then
13506 -- The actual must be a synchronized tagged type
13508 if not Is_Tagged_Type (Act_T) then
13509 Error_Msg_N
13510 ("actual of synchronized type must be tagged", Actual);
13511 Abandon_Instantiation (Actual);
13513 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13514 and then Nkind (Type_Definition (Parent (Act_T))) =
13515 N_Derived_Type_Definition
13516 and then not Synchronized_Present
13517 (Type_Definition (Parent (Act_T)))
13518 then
13519 Error_Msg_N
13520 ("actual of synchronized type must be synchronized", Actual);
13521 Abandon_Instantiation (Actual);
13522 end if;
13523 end if;
13525 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13526 -- removes the second instance of the phrase "or allow pass by copy".
13528 -- For Ada 2022, the aspect may be specified explicitly for the
13529 -- formal regardless of whether an ancestor obeys it.
13531 if Is_Atomic (Act_T)
13532 and then not Is_Atomic (Ancestor)
13533 and then not Is_Atomic (A_Gen_T)
13534 then
13535 Error_Msg_N
13536 ("cannot have atomic actual type for non-atomic formal type",
13537 Actual);
13539 elsif Is_Volatile (Act_T)
13540 and then not Is_Volatile (Ancestor)
13541 and then not Is_Volatile (A_Gen_T)
13542 then
13543 Error_Msg_N
13544 ("cannot have volatile actual type for non-volatile formal type",
13545 Actual);
13546 end if;
13548 -- It should not be necessary to check for unknown discriminants on
13549 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13550 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13551 -- needs fixing. ???
13553 if Is_Definite_Subtype (A_Gen_T)
13554 and then not Unknown_Discriminants_Present (Formal)
13555 and then not Is_Definite_Subtype (Act_T)
13556 then
13557 Error_Msg_N ("actual subtype must be constrained", Actual);
13558 Abandon_Instantiation (Actual);
13559 end if;
13561 if not Unknown_Discriminants_Present (Formal) then
13562 if Is_Constrained (Ancestor) then
13563 if not Is_Constrained (Act_T) then
13564 Error_Msg_N ("actual subtype must be constrained", Actual);
13565 Abandon_Instantiation (Actual);
13566 end if;
13568 -- Ancestor is unconstrained, Check if generic formal and actual
13569 -- agree on constrainedness. The check only applies to array types
13570 -- and discriminated types.
13572 elsif Is_Constrained (Act_T) then
13573 if Ekind (Ancestor) = E_Access_Type
13574 or else (not Is_Constrained (A_Gen_T)
13575 and then Is_Composite_Type (A_Gen_T))
13576 then
13577 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13578 Abandon_Instantiation (Actual);
13579 end if;
13581 -- A class-wide type is only allowed if the formal has unknown
13582 -- discriminants.
13584 elsif Is_Class_Wide_Type (Act_T)
13585 and then not Has_Unknown_Discriminants (Ancestor)
13586 then
13587 Error_Msg_NE
13588 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13589 Abandon_Instantiation (Actual);
13591 -- Otherwise, the formal and actual must have the same number
13592 -- of discriminants and each discriminant of the actual must
13593 -- correspond to a discriminant of the formal.
13595 elsif Has_Discriminants (Act_T)
13596 and then not Has_Unknown_Discriminants (Act_T)
13597 and then Has_Discriminants (Ancestor)
13598 then
13599 Actual_Discr := First_Discriminant (Act_T);
13600 Ancestor_Discr := First_Discriminant (Ancestor);
13601 while Present (Actual_Discr)
13602 and then Present (Ancestor_Discr)
13603 loop
13604 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13605 No (Corresponding_Discriminant (Actual_Discr))
13606 then
13607 Error_Msg_NE
13608 ("discriminant & does not correspond "
13609 & "to ancestor discriminant", Actual, Actual_Discr);
13610 Abandon_Instantiation (Actual);
13611 end if;
13613 Next_Discriminant (Actual_Discr);
13614 Next_Discriminant (Ancestor_Discr);
13615 end loop;
13617 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13618 Error_Msg_NE
13619 ("actual for & must have same number of discriminants",
13620 Actual, Gen_T);
13621 Abandon_Instantiation (Actual);
13622 end if;
13624 -- This case should be caught by the earlier check for
13625 -- constrainedness, but the check here is added for completeness.
13627 elsif Has_Discriminants (Act_T)
13628 and then not Has_Unknown_Discriminants (Act_T)
13629 then
13630 Error_Msg_NE
13631 ("actual for & must not have discriminants", Actual, Gen_T);
13632 Abandon_Instantiation (Actual);
13634 elsif Has_Discriminants (Ancestor) then
13635 Error_Msg_NE
13636 ("actual for & must have known discriminants", Actual, Gen_T);
13637 Abandon_Instantiation (Actual);
13638 end if;
13640 if not Subtypes_Statically_Compatible
13641 (Act_T, Ancestor, Formal_Derived_Matching => True)
13642 then
13643 Error_Msg_NE
13644 ("actual for & must be statically compatible with ancestor",
13645 Actual, Gen_T);
13647 if not Predicates_Compatible (Act_T, Ancestor) then
13648 Error_Msg_N
13649 ("\predicate on actual is not compatible with ancestor",
13650 Actual);
13651 end if;
13653 Abandon_Instantiation (Actual);
13654 end if;
13655 end if;
13657 -- If the formal and actual types are abstract, check that there
13658 -- are no abstract primitives of the actual type that correspond to
13659 -- nonabstract primitives of the formal type (second sentence of
13660 -- RM95 3.9.3(9)).
13662 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13663 Check_Abstract_Primitives : declare
13664 Gen_Prims : constant Elist_Id :=
13665 Primitive_Operations (A_Gen_T);
13666 Gen_Elmt : Elmt_Id;
13667 Gen_Subp : Entity_Id;
13668 Anc_Subp : Entity_Id;
13669 Anc_Formal : Entity_Id;
13670 Anc_F_Type : Entity_Id;
13672 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13673 Act_Elmt : Elmt_Id;
13674 Act_Subp : Entity_Id;
13675 Act_Formal : Entity_Id;
13676 Act_F_Type : Entity_Id;
13678 Subprograms_Correspond : Boolean;
13680 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13681 -- Returns true if T2 is derived directly or indirectly from
13682 -- T1, including derivations from interfaces. T1 and T2 are
13683 -- required to be specific tagged base types.
13685 ------------------------
13686 -- Is_Tagged_Ancestor --
13687 ------------------------
13689 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13691 Intfc_Elmt : Elmt_Id;
13693 begin
13694 -- The predicate is satisfied if the types are the same
13696 if T1 = T2 then
13697 return True;
13699 -- If we've reached the top of the derivation chain then
13700 -- we know that T1 is not an ancestor of T2.
13702 elsif Etype (T2) = T2 then
13703 return False;
13705 -- Proceed to check T2's immediate parent
13707 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13708 return True;
13710 -- Finally, check to see if T1 is an ancestor of any of T2's
13711 -- progenitors.
13713 else
13714 Intfc_Elmt := First_Elmt (Interfaces (T2));
13715 while Present (Intfc_Elmt) loop
13716 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13717 return True;
13718 end if;
13720 Next_Elmt (Intfc_Elmt);
13721 end loop;
13722 end if;
13724 return False;
13725 end Is_Tagged_Ancestor;
13727 -- Start of processing for Check_Abstract_Primitives
13729 begin
13730 -- Loop over all of the formal derived type's primitives
13732 Gen_Elmt := First_Elmt (Gen_Prims);
13733 while Present (Gen_Elmt) loop
13734 Gen_Subp := Node (Gen_Elmt);
13736 -- If the primitive of the formal is not abstract, then
13737 -- determine whether there is a corresponding primitive of
13738 -- the actual type that's abstract.
13740 if not Is_Abstract_Subprogram (Gen_Subp) then
13741 Act_Elmt := First_Elmt (Act_Prims);
13742 while Present (Act_Elmt) loop
13743 Act_Subp := Node (Act_Elmt);
13745 -- If we find an abstract primitive of the actual,
13746 -- then we need to test whether it corresponds to the
13747 -- subprogram from which the generic formal primitive
13748 -- is inherited.
13750 if Is_Abstract_Subprogram (Act_Subp) then
13751 Anc_Subp := Alias (Gen_Subp);
13753 -- Test whether we have a corresponding primitive
13754 -- by comparing names, kinds, formal types, and
13755 -- result types.
13757 if Chars (Anc_Subp) = Chars (Act_Subp)
13758 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13759 then
13760 Anc_Formal := First_Formal (Anc_Subp);
13761 Act_Formal := First_Formal (Act_Subp);
13762 while Present (Anc_Formal)
13763 and then Present (Act_Formal)
13764 loop
13765 Anc_F_Type := Etype (Anc_Formal);
13766 Act_F_Type := Etype (Act_Formal);
13768 if Ekind (Anc_F_Type) =
13769 E_Anonymous_Access_Type
13770 then
13771 Anc_F_Type := Designated_Type (Anc_F_Type);
13773 if Ekind (Act_F_Type) =
13774 E_Anonymous_Access_Type
13775 then
13776 Act_F_Type :=
13777 Designated_Type (Act_F_Type);
13778 else
13779 exit;
13780 end if;
13782 elsif
13783 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13784 then
13785 exit;
13786 end if;
13788 Anc_F_Type := Base_Type (Anc_F_Type);
13789 Act_F_Type := Base_Type (Act_F_Type);
13791 -- If the formal is controlling, then the
13792 -- the type of the actual primitive's formal
13793 -- must be derived directly or indirectly
13794 -- from the type of the ancestor primitive's
13795 -- formal.
13797 if Is_Controlling_Formal (Anc_Formal) then
13798 if not Is_Tagged_Ancestor
13799 (Anc_F_Type, Act_F_Type)
13800 then
13801 exit;
13802 end if;
13804 -- Otherwise the types of the formals must
13805 -- be the same.
13807 elsif Anc_F_Type /= Act_F_Type then
13808 exit;
13809 end if;
13811 Next_Formal (Anc_Formal);
13812 Next_Formal (Act_Formal);
13813 end loop;
13815 -- If we traversed through all of the formals
13816 -- then so far the subprograms correspond, so
13817 -- now check that any result types correspond.
13819 if No (Anc_Formal) and then No (Act_Formal) then
13820 Subprograms_Correspond := True;
13822 if Ekind (Act_Subp) = E_Function then
13823 Anc_F_Type := Etype (Anc_Subp);
13824 Act_F_Type := Etype (Act_Subp);
13826 if Ekind (Anc_F_Type) =
13827 E_Anonymous_Access_Type
13828 then
13829 Anc_F_Type :=
13830 Designated_Type (Anc_F_Type);
13832 if Ekind (Act_F_Type) =
13833 E_Anonymous_Access_Type
13834 then
13835 Act_F_Type :=
13836 Designated_Type (Act_F_Type);
13837 else
13838 Subprograms_Correspond := False;
13839 end if;
13841 elsif
13842 Ekind (Act_F_Type)
13843 = E_Anonymous_Access_Type
13844 then
13845 Subprograms_Correspond := False;
13846 end if;
13848 Anc_F_Type := Base_Type (Anc_F_Type);
13849 Act_F_Type := Base_Type (Act_F_Type);
13851 -- Now either the result types must be
13852 -- the same or, if the result type is
13853 -- controlling, the result type of the
13854 -- actual primitive must descend from the
13855 -- result type of the ancestor primitive.
13857 if Subprograms_Correspond
13858 and then Anc_F_Type /= Act_F_Type
13859 and then
13860 Has_Controlling_Result (Anc_Subp)
13861 and then not Is_Tagged_Ancestor
13862 (Anc_F_Type, Act_F_Type)
13863 then
13864 Subprograms_Correspond := False;
13865 end if;
13866 end if;
13868 -- Found a matching subprogram belonging to
13869 -- formal ancestor type, so actual subprogram
13870 -- corresponds and this violates 3.9.3(9).
13872 if Subprograms_Correspond then
13873 Error_Msg_NE
13874 ("abstract subprogram & overrides "
13875 & "nonabstract subprogram of ancestor",
13876 Actual, Act_Subp);
13877 end if;
13878 end if;
13879 end if;
13880 end if;
13882 Next_Elmt (Act_Elmt);
13883 end loop;
13884 end if;
13886 Next_Elmt (Gen_Elmt);
13887 end loop;
13888 end Check_Abstract_Primitives;
13889 end if;
13891 -- Verify that limitedness matches. If parent is a limited
13892 -- interface then the generic formal is not unless declared
13893 -- explicitly so. If not declared limited, the actual cannot be
13894 -- limited (see AI05-0087).
13896 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13897 if not In_Instance then
13898 Error_Msg_NE
13899 ("actual for non-limited & cannot be a limited type",
13900 Actual, Gen_T);
13901 Explain_Limited_Type (Act_T, Actual);
13902 Abandon_Instantiation (Actual);
13903 end if;
13904 end if;
13906 -- Check for AI12-0036
13908 declare
13909 Formal_Is_Private_Extension : constant Boolean :=
13910 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13912 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13914 begin
13915 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13916 if not In_Instance then
13917 if Actual_Is_Tagged then
13918 Error_Msg_NE
13919 ("actual for & cannot be a tagged type", Actual, Gen_T);
13920 else
13921 Error_Msg_NE
13922 ("actual for & must be a tagged type", Actual, Gen_T);
13923 end if;
13925 Abandon_Instantiation (Actual);
13926 end if;
13927 end if;
13928 end;
13929 end Validate_Derived_Type_Instance;
13931 ----------------------------------------
13932 -- Validate_Discriminated_Formal_Type --
13933 ----------------------------------------
13935 procedure Validate_Discriminated_Formal_Type is
13936 Formal_Discr : Entity_Id;
13937 Actual_Discr : Entity_Id;
13938 Formal_Subt : Entity_Id;
13940 begin
13941 if Has_Discriminants (A_Gen_T) then
13942 if not Has_Discriminants (Act_T) then
13943 Error_Msg_NE
13944 ("actual for & must have discriminants", Actual, Gen_T);
13945 Abandon_Instantiation (Actual);
13947 elsif Is_Constrained (Act_T) then
13948 Error_Msg_NE
13949 ("actual for & must be unconstrained", Actual, Gen_T);
13950 Abandon_Instantiation (Actual);
13952 else
13953 Formal_Discr := First_Discriminant (A_Gen_T);
13954 Actual_Discr := First_Discriminant (Act_T);
13955 while Formal_Discr /= Empty loop
13956 if Actual_Discr = Empty then
13957 Error_Msg_N
13958 ("discriminants on actual do not match formal",
13959 Actual);
13960 Abandon_Instantiation (Actual);
13961 end if;
13963 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13965 -- Access discriminants match if designated types do
13967 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13968 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13969 E_Anonymous_Access_Type
13970 and then
13971 Get_Instance_Of
13972 (Designated_Type (Base_Type (Formal_Subt))) =
13973 Designated_Type (Base_Type (Etype (Actual_Discr)))
13974 then
13975 null;
13977 elsif Base_Type (Formal_Subt) /=
13978 Base_Type (Etype (Actual_Discr))
13979 then
13980 Error_Msg_N
13981 ("types of actual discriminants must match formal",
13982 Actual);
13983 Abandon_Instantiation (Actual);
13985 elsif not Subtypes_Statically_Match
13986 (Formal_Subt, Etype (Actual_Discr))
13987 and then Ada_Version >= Ada_95
13988 then
13989 Error_Msg_N
13990 ("subtypes of actual discriminants must match formal",
13991 Actual);
13992 Abandon_Instantiation (Actual);
13993 end if;
13995 Next_Discriminant (Formal_Discr);
13996 Next_Discriminant (Actual_Discr);
13997 end loop;
13999 if Actual_Discr /= Empty then
14000 Error_Msg_NE
14001 ("discriminants on actual do not match formal",
14002 Actual, Gen_T);
14003 Abandon_Instantiation (Actual);
14004 end if;
14005 end if;
14006 end if;
14007 end Validate_Discriminated_Formal_Type;
14009 ---------------------------------------
14010 -- Validate_Incomplete_Type_Instance --
14011 ---------------------------------------
14013 procedure Validate_Incomplete_Type_Instance is
14014 begin
14015 if not Is_Tagged_Type (Act_T)
14016 and then Is_Tagged_Type (A_Gen_T)
14017 then
14018 Error_Msg_NE
14019 ("actual for & must be a tagged type", Actual, Gen_T);
14020 end if;
14022 Validate_Discriminated_Formal_Type;
14023 end Validate_Incomplete_Type_Instance;
14025 --------------------------------------
14026 -- Validate_Interface_Type_Instance --
14027 --------------------------------------
14029 procedure Validate_Interface_Type_Instance is
14030 begin
14031 if not Is_Interface (Act_T) then
14032 Error_Msg_NE
14033 ("actual for formal interface type must be an interface",
14034 Actual, Gen_T);
14036 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
14037 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
14038 or else Is_Protected_Interface (A_Gen_T) /=
14039 Is_Protected_Interface (Act_T)
14040 or else Is_Synchronized_Interface (A_Gen_T) /=
14041 Is_Synchronized_Interface (Act_T)
14042 then
14043 Error_Msg_NE
14044 ("actual for interface& does not match (RM 12.5.5(4))",
14045 Actual, Gen_T);
14046 end if;
14047 end Validate_Interface_Type_Instance;
14049 ------------------------------------
14050 -- Validate_Private_Type_Instance --
14051 ------------------------------------
14053 procedure Validate_Private_Type_Instance is
14054 begin
14055 if Is_Limited_Type (Act_T)
14056 and then not Is_Limited_Type (A_Gen_T)
14057 then
14058 if In_Instance then
14059 null;
14060 else
14061 Error_Msg_NE
14062 ("actual for non-limited & cannot be a limited type", Actual,
14063 Gen_T);
14064 Explain_Limited_Type (Act_T, Actual);
14065 Abandon_Instantiation (Actual);
14066 end if;
14068 elsif Known_To_Have_Preelab_Init (A_Gen_T)
14069 and then not Has_Preelaborable_Initialization (Act_T)
14070 then
14071 Error_Msg_NE
14072 ("actual for & must have preelaborable initialization", Actual,
14073 Gen_T);
14075 elsif not Is_Definite_Subtype (Act_T)
14076 and then Is_Definite_Subtype (A_Gen_T)
14077 and then Ada_Version >= Ada_95
14078 then
14079 Error_Msg_NE
14080 ("actual for & must be a definite subtype", Actual, Gen_T);
14082 elsif not Is_Tagged_Type (Act_T)
14083 and then Is_Tagged_Type (A_Gen_T)
14084 then
14085 Error_Msg_NE
14086 ("actual for & must be a tagged type", Actual, Gen_T);
14087 end if;
14089 Validate_Discriminated_Formal_Type;
14090 Ancestor := Gen_T;
14091 end Validate_Private_Type_Instance;
14093 -- Start of processing for Instantiate_Type
14095 begin
14096 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
14097 Error_Msg_N ("duplicate instantiation of generic type", Actual);
14098 return New_List (Error);
14100 elsif not Is_Entity_Name (Actual)
14101 or else not Is_Type (Entity (Actual))
14102 then
14103 Error_Msg_NE
14104 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
14105 Abandon_Instantiation (Actual);
14107 else
14108 Act_T := Entity (Actual);
14110 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
14111 -- as a generic actual parameter if the corresponding formal type
14112 -- does not have a known_discriminant_part, or is a formal derived
14113 -- type that is an Unchecked_Union type.
14115 if Is_Unchecked_Union (Base_Type (Act_T)) then
14116 if not Has_Discriminants (A_Gen_T)
14117 or else (Is_Derived_Type (A_Gen_T)
14118 and then Is_Unchecked_Union (A_Gen_T))
14119 then
14120 null;
14121 else
14122 Error_Msg_N ("unchecked union cannot be the actual for a "
14123 & "discriminated formal type", Act_T);
14125 end if;
14126 end if;
14128 -- Deal with fixed/floating restrictions
14130 if Is_Floating_Point_Type (Act_T) then
14131 Check_Restriction (No_Floating_Point, Actual);
14132 elsif Is_Fixed_Point_Type (Act_T) then
14133 Check_Restriction (No_Fixed_Point, Actual);
14134 end if;
14136 -- Deal with error of using incomplete type as generic actual.
14137 -- This includes limited views of a type, even if the non-limited
14138 -- view may be available.
14140 if Ekind (Act_T) = E_Incomplete_Type
14141 or else (Is_Class_Wide_Type (Act_T)
14142 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14143 then
14144 -- If the formal is an incomplete type, the actual can be
14145 -- incomplete as well, but if an actual incomplete type has
14146 -- a full view, then we'll retrieve that.
14148 if Ekind (A_Gen_T) = E_Incomplete_Type
14149 and then No (Full_View (Act_T))
14150 then
14151 null;
14153 elsif Is_Class_Wide_Type (Act_T)
14154 or else No (Full_View (Act_T))
14155 then
14156 Error_Msg_N ("premature use of incomplete type", Actual);
14157 Abandon_Instantiation (Actual);
14159 else
14160 Act_T := Full_View (Act_T);
14161 Set_Entity (Actual, Act_T);
14163 if Has_Private_Component (Act_T) then
14164 Error_Msg_N
14165 ("premature use of type with private component", Actual);
14166 end if;
14167 end if;
14169 -- Deal with error of premature use of private type as generic actual
14171 elsif Is_Private_Type (Act_T)
14172 and then Is_Private_Type (Base_Type (Act_T))
14173 and then not Is_Generic_Type (Act_T)
14174 and then not Is_Derived_Type (Act_T)
14175 and then No (Full_View (Root_Type (Act_T)))
14176 then
14177 -- If the formal is an incomplete type, the actual can be
14178 -- private or incomplete as well.
14180 if Ekind (A_Gen_T) = E_Incomplete_Type then
14181 null;
14182 else
14183 Error_Msg_N ("premature use of private type", Actual);
14184 end if;
14186 elsif Has_Private_Component (Act_T) then
14187 Error_Msg_N
14188 ("premature use of type with private component", Actual);
14189 end if;
14191 Set_Instance_Of (A_Gen_T, Act_T);
14193 -- If the type is generic, the class-wide type may also be used
14195 if Is_Tagged_Type (A_Gen_T)
14196 and then Is_Tagged_Type (Act_T)
14197 and then not Is_Class_Wide_Type (A_Gen_T)
14198 then
14199 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14200 Class_Wide_Type (Act_T));
14201 end if;
14203 if not Is_Abstract_Type (A_Gen_T)
14204 and then Is_Abstract_Type (Act_T)
14205 then
14206 Error_Msg_N
14207 ("actual of non-abstract formal cannot be abstract", Actual);
14208 end if;
14210 -- A generic scalar type is a first subtype for which we generate
14211 -- an anonymous base type. Indicate that the instance of this base
14212 -- is the base type of the actual.
14214 if Is_Scalar_Type (A_Gen_T) then
14215 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14216 end if;
14217 end if;
14219 Check_Shared_Variable_Control_Aspects;
14221 if Error_Posted (Act_T) then
14222 null;
14223 else
14224 case Nkind (Def) is
14225 when N_Formal_Private_Type_Definition =>
14226 Validate_Private_Type_Instance;
14228 when N_Formal_Incomplete_Type_Definition =>
14229 Validate_Incomplete_Type_Instance;
14231 when N_Formal_Derived_Type_Definition =>
14232 Validate_Derived_Type_Instance;
14234 when N_Formal_Discrete_Type_Definition =>
14235 if not Is_Discrete_Type (Act_T) then
14236 Error_Msg_NE
14237 ("expect discrete type in instantiation of&",
14238 Actual, Gen_T);
14239 Abandon_Instantiation (Actual);
14240 end if;
14242 Diagnose_Predicated_Actual;
14244 when N_Formal_Signed_Integer_Type_Definition =>
14245 if not Is_Signed_Integer_Type (Act_T) then
14246 Error_Msg_NE
14247 ("expect signed integer type in instantiation of&",
14248 Actual, Gen_T);
14249 Abandon_Instantiation (Actual);
14250 end if;
14252 Diagnose_Predicated_Actual;
14254 when N_Formal_Modular_Type_Definition =>
14255 if not Is_Modular_Integer_Type (Act_T) then
14256 Error_Msg_NE
14257 ("expect modular type in instantiation of &",
14258 Actual, Gen_T);
14259 Abandon_Instantiation (Actual);
14260 end if;
14262 Diagnose_Predicated_Actual;
14264 when N_Formal_Floating_Point_Definition =>
14265 if not Is_Floating_Point_Type (Act_T) then
14266 Error_Msg_NE
14267 ("expect float type in instantiation of &", Actual, Gen_T);
14268 Abandon_Instantiation (Actual);
14269 end if;
14271 when N_Formal_Ordinary_Fixed_Point_Definition =>
14272 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14273 Error_Msg_NE
14274 ("expect ordinary fixed point type in instantiation of &",
14275 Actual, Gen_T);
14276 Abandon_Instantiation (Actual);
14277 end if;
14279 when N_Formal_Decimal_Fixed_Point_Definition =>
14280 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14281 Error_Msg_NE
14282 ("expect decimal type in instantiation of &",
14283 Actual, Gen_T);
14284 Abandon_Instantiation (Actual);
14285 end if;
14287 when N_Array_Type_Definition =>
14288 Validate_Array_Type_Instance;
14290 when N_Access_To_Object_Definition =>
14291 Validate_Access_Type_Instance;
14293 when N_Access_Function_Definition
14294 | N_Access_Procedure_Definition
14296 Validate_Access_Subprogram_Instance;
14298 when N_Record_Definition =>
14299 Validate_Interface_Type_Instance;
14301 when N_Derived_Type_Definition =>
14302 Validate_Derived_Interface_Type_Instance;
14304 when others =>
14305 raise Program_Error;
14306 end case;
14307 end if;
14309 Subt := New_Copy (Gen_T);
14311 -- Use adjusted sloc of subtype name as the location for other nodes in
14312 -- the subtype declaration.
14314 Loc := Sloc (Subt);
14316 Decl_Node :=
14317 Make_Subtype_Declaration (Loc,
14318 Defining_Identifier => Subt,
14319 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14321 Copy_Ghost_Aspect (Formal, To => Decl_Node);
14323 -- Record whether the actual is private at this point, so that
14324 -- Check_Generic_Actuals can restore its proper view before the
14325 -- semantic analysis of the instance.
14327 if Is_Private_Type (Act_T) then
14328 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14329 end if;
14331 -- In Ada 2012 the actual may be a limited view. Indicate that
14332 -- the local subtype must be treated as such.
14334 if From_Limited_With (Act_T) then
14335 Mutate_Ekind (Subt, E_Incomplete_Subtype);
14336 Set_From_Limited_With (Subt);
14337 end if;
14339 Decl_Nodes := New_List (Decl_Node);
14341 -- Flag actual derived types so their elaboration produces the
14342 -- appropriate renamings for the primitive operations of the ancestor.
14343 -- Flag actual for formal private types as well, to determine whether
14344 -- operations in the private part may override inherited operations.
14345 -- If the formal has an interface list, the ancestor is not the
14346 -- parent, but the analyzed formal that includes the interface
14347 -- operations of all its progenitors.
14349 -- Same treatment for formal private types, so we can check whether the
14350 -- type is tagged limited when validating derivations in the private
14351 -- part. (See AI05-096).
14353 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14354 if Present (Interface_List (Def)) then
14355 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14356 else
14357 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14358 end if;
14360 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14361 | N_Formal_Incomplete_Type_Definition
14362 then
14363 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14364 end if;
14366 -- If the actual is a synchronized type that implements an interface,
14367 -- the primitive operations are attached to the corresponding record,
14368 -- and we have to treat it as an additional generic actual, so that its
14369 -- primitive operations become visible in the instance. The task or
14370 -- protected type itself does not carry primitive operations.
14372 if Is_Concurrent_Type (Act_T)
14373 and then Is_Tagged_Type (Act_T)
14374 and then Present (Corresponding_Record_Type (Act_T))
14375 and then Present (Ancestor)
14376 and then Is_Interface (Ancestor)
14377 then
14378 declare
14379 Corr_Rec : constant Entity_Id :=
14380 Corresponding_Record_Type (Act_T);
14381 New_Corr : Entity_Id;
14382 Corr_Decl : Node_Id;
14384 begin
14385 New_Corr := Make_Temporary (Loc, 'S');
14386 Corr_Decl :=
14387 Make_Subtype_Declaration (Loc,
14388 Defining_Identifier => New_Corr,
14389 Subtype_Indication =>
14390 New_Occurrence_Of (Corr_Rec, Loc));
14391 Append_To (Decl_Nodes, Corr_Decl);
14393 if Ekind (Act_T) = E_Task_Type then
14394 Mutate_Ekind (Subt, E_Task_Subtype);
14395 else
14396 Mutate_Ekind (Subt, E_Protected_Subtype);
14397 end if;
14399 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14400 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14401 Set_Generic_Parent_Type (Decl_Node, Empty);
14402 end;
14403 end if;
14405 -- For a floating-point type, capture dimension info if any, because
14406 -- the generated subtype declaration does not come from source and
14407 -- will not process dimensions.
14409 if Is_Floating_Point_Type (Act_T) then
14410 Copy_Dimensions (Act_T, Subt);
14411 end if;
14413 return Decl_Nodes;
14414 end Instantiate_Type;
14416 -----------------------------
14417 -- Is_Abbreviated_Instance --
14418 -----------------------------
14420 function Is_Abbreviated_Instance (E : Entity_Id) return Boolean is
14421 begin
14422 return Ekind (E) = E_Package
14423 and then Present (Hidden_In_Formal_Instance (E));
14424 end Is_Abbreviated_Instance;
14426 ---------------------
14427 -- Is_In_Main_Unit --
14428 ---------------------
14430 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14431 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14432 Current_Unit : Node_Id;
14434 begin
14435 if Unum = Main_Unit then
14436 return True;
14438 -- If the current unit is a subunit then it is either the main unit or
14439 -- is being compiled as part of the main unit.
14441 elsif Nkind (N) = N_Compilation_Unit then
14442 return Nkind (Unit (N)) = N_Subunit;
14443 end if;
14445 Current_Unit := Parent (N);
14446 while Present (Current_Unit)
14447 and then Nkind (Current_Unit) /= N_Compilation_Unit
14448 loop
14449 Current_Unit := Parent (Current_Unit);
14450 end loop;
14452 -- The instantiation node is in the main unit, or else the current node
14453 -- (perhaps as the result of nested instantiations) is in the main unit,
14454 -- or in the declaration of the main unit, which in this last case must
14455 -- be a body.
14457 return
14458 Current_Unit = Cunit (Main_Unit)
14459 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14460 or else (Present (Current_Unit)
14461 and then Present (Library_Unit (Current_Unit))
14462 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14463 end Is_In_Main_Unit;
14465 ----------------------------
14466 -- Load_Parent_Of_Generic --
14467 ----------------------------
14469 procedure Load_Parent_Of_Generic
14470 (N : Node_Id;
14471 Spec : Node_Id;
14472 Body_Optional : Boolean := False)
14474 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14475 Saved_Style_Check : constant Boolean := Style_Check;
14476 Saved_Warn : constant Warnings_State := Save_Warnings;
14477 True_Parent : Node_Id;
14478 Inst_Node : Node_Id;
14479 OK : Boolean;
14480 Previous_Instances : constant Elist_Id := New_Elmt_List;
14482 procedure Collect_Previous_Instances (Decls : List_Id);
14483 -- Collect all instantiations in the given list of declarations, that
14484 -- precede the generic that we need to load. If the bodies of these
14485 -- instantiations are available, we must analyze them, to ensure that
14486 -- the public symbols generated are the same when the unit is compiled
14487 -- to generate code, and when it is compiled in the context of a unit
14488 -- that needs a particular nested instance. This process is applied to
14489 -- both package and subprogram instances.
14491 --------------------------------
14492 -- Collect_Previous_Instances --
14493 --------------------------------
14495 procedure Collect_Previous_Instances (Decls : List_Id) is
14496 Decl : Node_Id;
14498 begin
14499 Decl := First (Decls);
14500 while Present (Decl) loop
14501 if Sloc (Decl) >= Sloc (Inst_Node) then
14502 return;
14504 -- If Decl is an instantiation, then record it as requiring
14505 -- instantiation of the corresponding body, except if it is an
14506 -- abbreviated instantiation generated internally for conformance
14507 -- checking purposes only for the case of a formal package
14508 -- declared without a box (see Instantiate_Formal_Package). Such
14509 -- an instantiation does not generate any code (the actual code
14510 -- comes from actual) and thus does not need to be analyzed here.
14511 -- If the instantiation appears with a generic package body it is
14512 -- not analyzed here either.
14514 elsif Nkind (Decl) = N_Package_Instantiation
14515 and then not Is_Abbreviated_Instance (Defining_Entity (Decl))
14516 then
14517 Append_Elmt (Decl, Previous_Instances);
14519 -- For a subprogram instantiation, omit instantiations intrinsic
14520 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14522 elsif Nkind (Decl) in N_Function_Instantiation
14523 | N_Procedure_Instantiation
14524 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14525 then
14526 Append_Elmt (Decl, Previous_Instances);
14528 elsif Nkind (Decl) = N_Package_Declaration then
14529 Collect_Previous_Instances
14530 (Visible_Declarations (Specification (Decl)));
14531 Collect_Previous_Instances
14532 (Private_Declarations (Specification (Decl)));
14534 -- Previous non-generic bodies may contain instances as well
14536 elsif Nkind (Decl) = N_Package_Body
14537 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14538 then
14539 Collect_Previous_Instances (Declarations (Decl));
14541 elsif Nkind (Decl) = N_Subprogram_Body
14542 and then not Acts_As_Spec (Decl)
14543 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14544 then
14545 Collect_Previous_Instances (Declarations (Decl));
14546 end if;
14548 Next (Decl);
14549 end loop;
14550 end Collect_Previous_Instances;
14552 -- Start of processing for Load_Parent_Of_Generic
14554 begin
14555 if not In_Same_Source_Unit (N, Spec)
14556 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14557 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14558 and then not Is_In_Main_Unit (Spec))
14559 then
14560 -- Find body of parent of spec, and analyze it. A special case arises
14561 -- when the parent is an instantiation, that is to say when we are
14562 -- currently instantiating a nested generic. In that case, there is
14563 -- no separate file for the body of the enclosing instance. Instead,
14564 -- the enclosing body must be instantiated as if it were a pending
14565 -- instantiation, in order to produce the body for the nested generic
14566 -- we require now. Note that in that case the generic may be defined
14567 -- in a package body, the instance defined in the same package body,
14568 -- and the original enclosing body may not be in the main unit.
14570 Inst_Node := Empty;
14572 True_Parent := Parent (Spec);
14573 while Present (True_Parent)
14574 and then Nkind (True_Parent) /= N_Compilation_Unit
14575 loop
14576 if Nkind (True_Parent) = N_Package_Declaration
14577 and then
14578 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14579 then
14580 -- Parent is a compilation unit that is an instantiation, and
14581 -- instantiation node has been replaced with package decl.
14583 Inst_Node := Original_Node (True_Parent);
14584 exit;
14586 elsif Nkind (True_Parent) = N_Package_Declaration
14587 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14588 and then
14589 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14590 then
14591 -- Parent is a compilation unit that is an instantiation, but
14592 -- instantiation node has not been replaced with package decl.
14594 Inst_Node := Unit (Parent (True_Parent));
14595 exit;
14597 elsif Nkind (True_Parent) = N_Package_Declaration
14598 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14599 and then Present (Generic_Parent (Specification (True_Parent)))
14600 then
14601 -- Parent is an instantiation within another specification.
14602 -- Declaration for instance has been inserted before original
14603 -- instantiation node. A direct link would be preferable?
14605 Inst_Node := Next (True_Parent);
14606 while Present (Inst_Node)
14607 and then Nkind (Inst_Node) /= N_Package_Instantiation
14608 loop
14609 Next (Inst_Node);
14610 end loop;
14612 -- If the instance appears within a generic, and the generic
14613 -- unit is defined within a formal package of the enclosing
14614 -- generic, there is no generic body available, and none
14615 -- needed. A more precise test should be used ???
14617 if No (Inst_Node) then
14618 return;
14619 end if;
14621 exit;
14623 -- If an ancestor of the generic comes from a formal package
14624 -- there is no source for the ancestor body. This is detected
14625 -- by examining the scope of the ancestor and its declaration.
14626 -- The body, if any is needed, will be available when the
14627 -- current unit (containing a formal package) is instantiated.
14629 elsif Nkind (True_Parent) = N_Package_Specification
14630 and then Present (Generic_Parent (True_Parent))
14631 and then Nkind
14632 (Original_Node (Unit_Declaration_Node
14633 (Scope (Generic_Parent (True_Parent)))))
14634 = N_Formal_Package_Declaration
14635 then
14636 return;
14638 else
14639 True_Parent := Parent (True_Parent);
14640 end if;
14641 end loop;
14643 -- Case where we are currently instantiating a nested generic
14645 if Present (Inst_Node) then
14646 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14648 -- Instantiation node and declaration of instantiated package
14649 -- were exchanged when only the declaration was needed.
14650 -- Restore instantiation node before proceeding with body.
14652 Set_Unit (Parent (True_Parent), Inst_Node);
14653 end if;
14655 -- Now complete instantiation of enclosing body, if it appears in
14656 -- some other unit. If it appears in the current unit, the body
14657 -- will have been instantiated already.
14659 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14661 -- We need to determine the expander mode to instantiate the
14662 -- enclosing body. Because the generic body we need may use
14663 -- global entities declared in the enclosing package (including
14664 -- aggregates) it is in general necessary to compile this body
14665 -- with expansion enabled, except if we are within a generic
14666 -- package, in which case the usual generic rule applies.
14668 declare
14669 Exp_Status : Boolean := True;
14670 Scop : Entity_Id;
14672 begin
14673 -- Loop through scopes looking for generic package
14675 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14676 while Present (Scop)
14677 and then Scop /= Standard_Standard
14678 loop
14679 if Ekind (Scop) = E_Generic_Package then
14680 Exp_Status := False;
14681 exit;
14682 end if;
14684 Scop := Scope (Scop);
14685 end loop;
14687 -- Collect previous instantiations in the unit that contains
14688 -- the desired generic.
14690 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14691 and then not Body_Optional
14692 then
14693 declare
14694 Decl : Elmt_Id;
14695 Info : Pending_Body_Info;
14696 Par : Node_Id;
14698 begin
14699 Par := Parent (Inst_Node);
14700 while Present (Par) loop
14701 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14702 Par := Parent (Par);
14703 end loop;
14705 pragma Assert (Present (Par));
14707 if Nkind (Par) = N_Package_Body then
14708 Collect_Previous_Instances (Declarations (Par));
14710 elsif Nkind (Par) = N_Package_Declaration then
14711 Collect_Previous_Instances
14712 (Visible_Declarations (Specification (Par)));
14713 Collect_Previous_Instances
14714 (Private_Declarations (Specification (Par)));
14716 else
14717 -- Enclosing unit is a subprogram body. In this
14718 -- case all instance bodies are processed in order
14719 -- and there is no need to collect them separately.
14721 null;
14722 end if;
14724 Decl := First_Elmt (Previous_Instances);
14725 while Present (Decl) loop
14726 Info :=
14727 (Inst_Node => Node (Decl),
14728 Act_Decl =>
14729 Instance_Spec (Node (Decl)),
14730 Fin_Scop => Empty,
14731 Config_Switches => Save_Config_Switches,
14732 Current_Sem_Unit =>
14733 Get_Code_Unit (Sloc (Node (Decl))),
14734 Expander_Status => Exp_Status,
14735 Local_Suppress_Stack_Top =>
14736 Local_Suppress_Stack_Top,
14737 Scope_Suppress => Scope_Suppress,
14738 Warnings => Save_Warnings);
14740 -- Package instance
14742 if Nkind (Node (Decl)) = N_Package_Instantiation
14743 then
14744 Instantiate_Package_Body
14745 (Info, Body_Optional => True);
14747 -- Subprogram instance
14749 else
14750 -- The instance_spec is in the wrapper package,
14751 -- usually followed by its local renaming
14752 -- declaration. See Build_Subprogram_Renaming
14753 -- for details. If the instance carries aspects,
14754 -- these result in the corresponding pragmas,
14755 -- inserted after the subprogram declaration.
14756 -- They must be skipped as well when retrieving
14757 -- the desired spec. Some of them may have been
14758 -- rewritten as null statements.
14759 -- A direct link would be more robust ???
14761 declare
14762 Decl : Node_Id :=
14763 (Last (Visible_Declarations
14764 (Specification (Info.Act_Decl))));
14765 begin
14766 while Nkind (Decl) in
14767 N_Null_Statement |
14768 N_Pragma |
14769 N_Subprogram_Renaming_Declaration
14770 loop
14771 Decl := Prev (Decl);
14772 end loop;
14774 Info.Act_Decl := Decl;
14775 end;
14777 Instantiate_Subprogram_Body
14778 (Info, Body_Optional => True);
14779 end if;
14781 Next_Elmt (Decl);
14782 end loop;
14783 end;
14784 end if;
14786 Instantiate_Package_Body
14787 (Body_Info =>
14788 ((Inst_Node => Inst_Node,
14789 Act_Decl => True_Parent,
14790 Fin_Scop => Empty,
14791 Config_Switches => Save_Config_Switches,
14792 Current_Sem_Unit =>
14793 Get_Code_Unit (Sloc (Inst_Node)),
14794 Expander_Status => Exp_Status,
14795 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14796 Scope_Suppress => Scope_Suppress,
14797 Warnings => Save_Warnings)),
14798 Body_Optional => Body_Optional);
14799 end;
14800 end if;
14802 -- Case where we are not instantiating a nested generic
14804 else
14805 Opt.Style_Check := False;
14806 Expander_Mode_Save_And_Set (True);
14807 Load_Needed_Body (Comp_Unit, OK);
14808 Opt.Style_Check := Saved_Style_Check;
14809 Restore_Warnings (Saved_Warn);
14810 Expander_Mode_Restore;
14812 if not OK
14813 and then Unit_Requires_Body (Defining_Entity (Spec))
14814 and then not Body_Optional
14815 then
14816 declare
14817 Bname : constant Unit_Name_Type :=
14818 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14820 begin
14821 -- In CodePeer mode, the missing body may make the analysis
14822 -- incomplete, but we do not treat it as fatal.
14824 if CodePeer_Mode then
14825 return;
14827 else
14828 Error_Msg_Unit_1 := Bname;
14829 Error_Msg_N ("this instantiation requires$!", N);
14830 Error_Msg_File_1 :=
14831 Get_File_Name (Bname, Subunit => False);
14832 Error_Msg_N ("\but file{ was not found!", N);
14833 raise Unrecoverable_Error;
14834 end if;
14835 end;
14836 end if;
14837 end if;
14838 end if;
14840 -- If loading parent of the generic caused an instantiation circularity,
14841 -- we abandon compilation at this point, because otherwise in some cases
14842 -- we get into trouble with infinite recursions after this point.
14844 if Circularity_Detected then
14845 raise Unrecoverable_Error;
14846 end if;
14847 end Load_Parent_Of_Generic;
14849 ---------------------------------
14850 -- Map_Formal_Package_Entities --
14851 ---------------------------------
14853 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14854 E1 : Entity_Id;
14855 E2 : Entity_Id;
14857 begin
14858 Set_Instance_Of (Form, Act);
14860 -- Traverse formal and actual package to map the corresponding entities.
14861 -- We skip over internal entities that may be generated during semantic
14862 -- analysis, and find the matching entities by name, given that they
14863 -- must appear in the same order.
14865 E1 := First_Entity (Form);
14866 E2 := First_Entity (Act);
14867 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14868 -- Could this test be a single condition??? Seems like it could, and
14869 -- isn't FPE (Form) a constant anyway???
14871 if not Is_Internal (E1)
14872 and then Present (Parent (E1))
14873 and then not Is_Class_Wide_Type (E1)
14874 and then not Is_Internal_Name (Chars (E1))
14875 then
14876 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14877 Next_Entity (E2);
14878 end loop;
14880 if No (E2) then
14881 exit;
14882 else
14883 Set_Instance_Of (E1, E2);
14885 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14886 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14887 end if;
14889 if Is_Constrained (E1) then
14890 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14891 end if;
14893 if Ekind (E1) = E_Package and then No (Renamed_Entity (E1)) then
14894 Map_Formal_Package_Entities (E1, E2);
14895 end if;
14896 end if;
14897 end if;
14899 Next_Entity (E1);
14900 end loop;
14901 end Map_Formal_Package_Entities;
14903 -----------------------
14904 -- Move_Freeze_Nodes --
14905 -----------------------
14907 procedure Move_Freeze_Nodes
14908 (Out_Of : Entity_Id;
14909 After : Node_Id;
14910 L : List_Id)
14912 Decl : Node_Id;
14913 Next_Decl : Node_Id;
14914 Next_Node : Node_Id := After;
14915 Spec : Node_Id;
14917 function Is_Outer_Type (T : Entity_Id) return Boolean;
14918 -- Check whether entity is declared in a scope external to that of the
14919 -- generic unit.
14921 -------------------
14922 -- Is_Outer_Type --
14923 -------------------
14925 function Is_Outer_Type (T : Entity_Id) return Boolean is
14926 Scop : Entity_Id := Scope (T);
14928 begin
14929 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14930 return True;
14932 else
14933 while Scop /= Standard_Standard loop
14934 if Scop = Out_Of then
14935 return False;
14936 else
14937 Scop := Scope (Scop);
14938 end if;
14939 end loop;
14941 return True;
14942 end if;
14943 end Is_Outer_Type;
14945 -- Start of processing for Move_Freeze_Nodes
14947 begin
14948 if No (L) then
14949 return;
14950 end if;
14952 -- First remove the freeze nodes that may appear before all other
14953 -- declarations.
14955 Decl := First (L);
14956 while Present (Decl)
14957 and then Nkind (Decl) = N_Freeze_Entity
14958 and then Is_Outer_Type (Entity (Decl))
14959 loop
14960 Decl := Remove_Head (L);
14961 Insert_After (Next_Node, Decl);
14962 Set_Analyzed (Decl, False);
14963 Next_Node := Decl;
14964 Decl := First (L);
14965 end loop;
14967 -- Next scan the list of declarations and remove each freeze node that
14968 -- appears ahead of the current node.
14970 while Present (Decl) loop
14971 while Present (Next (Decl))
14972 and then Nkind (Next (Decl)) = N_Freeze_Entity
14973 and then Is_Outer_Type (Entity (Next (Decl)))
14974 loop
14975 Next_Decl := Remove_Next (Decl);
14976 Insert_After (Next_Node, Next_Decl);
14977 Set_Analyzed (Next_Decl, False);
14978 Next_Node := Next_Decl;
14979 end loop;
14981 -- If the declaration is a nested package or concurrent type, then
14982 -- recurse. Nested generic packages will have been processed from the
14983 -- inside out.
14985 case Nkind (Decl) is
14986 when N_Package_Declaration =>
14987 Spec := Specification (Decl);
14989 when N_Task_Type_Declaration =>
14990 Spec := Task_Definition (Decl);
14992 when N_Protected_Type_Declaration =>
14993 Spec := Protected_Definition (Decl);
14995 when others =>
14996 Spec := Empty;
14997 end case;
14999 if Present (Spec) then
15000 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
15001 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
15002 end if;
15004 Next (Decl);
15005 end loop;
15006 end Move_Freeze_Nodes;
15008 ----------------
15009 -- Next_Assoc --
15010 ----------------
15012 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
15013 begin
15014 return Generic_Renamings.Table (E).Next_In_HTable;
15015 end Next_Assoc;
15017 ------------------------
15018 -- Preanalyze_Actuals --
15019 ------------------------
15021 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
15022 procedure Perform_Appropriate_Analysis (N : Node_Id);
15023 -- Determine if the actuals we are analyzing come from a generic
15024 -- instantiation that is a library unit and dispatch accordingly.
15026 ----------------------------------
15027 -- Perform_Appropriate_Analysis --
15028 ----------------------------------
15030 procedure Perform_Appropriate_Analysis (N : Node_Id) is
15031 begin
15032 -- When we have a library instantiation we cannot allow any expansion
15033 -- to occur, since there may be no place to put it. Instead, in that
15034 -- case we perform a preanalysis of the actual.
15036 if Present (Inst) and then Is_Compilation_Unit (Inst) then
15037 Preanalyze (N);
15038 else
15039 Analyze (N);
15040 end if;
15041 end Perform_Appropriate_Analysis;
15043 -- Local variables
15045 Errs : constant Nat := Serious_Errors_Detected;
15047 Assoc : Node_Id;
15048 Act : Node_Id;
15050 Cur : Entity_Id := Empty;
15051 -- Current homograph of the instance name
15053 Vis : Boolean := False;
15054 -- Saved visibility status of the current homograph
15056 -- Start of processing for Preanalyze_Actuals
15058 begin
15059 Assoc := First (Generic_Associations (N));
15061 -- If the instance is a child unit, its name may hide an outer homonym,
15062 -- so make it invisible to perform name resolution on the actuals.
15064 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
15065 and then Present
15066 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
15067 then
15068 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
15070 if Is_Compilation_Unit (Cur) then
15071 Vis := Is_Immediately_Visible (Cur);
15072 Set_Is_Immediately_Visible (Cur, False);
15073 else
15074 Cur := Empty;
15075 end if;
15076 end if;
15078 while Present (Assoc) loop
15079 if Nkind (Assoc) /= N_Others_Choice then
15080 Act := Explicit_Generic_Actual_Parameter (Assoc);
15082 -- Within a nested instantiation, a defaulted actual is an empty
15083 -- association, so nothing to analyze. If the subprogram actual
15084 -- is an attribute, analyze prefix only, because actual is not a
15085 -- complete attribute reference.
15087 -- If actual is an allocator, analyze expression only. The full
15088 -- analysis can generate code, and if instance is a compilation
15089 -- unit we have to wait until the package instance is installed
15090 -- to have a proper place to insert this code.
15092 -- String literals may be operators, but at this point we do not
15093 -- know whether the actual is a formal subprogram or a string.
15095 if No (Act) then
15096 null;
15098 elsif Nkind (Act) = N_Attribute_Reference then
15099 Perform_Appropriate_Analysis (Prefix (Act));
15101 elsif Nkind (Act) = N_Explicit_Dereference then
15102 Perform_Appropriate_Analysis (Prefix (Act));
15104 elsif Nkind (Act) = N_Allocator then
15105 declare
15106 Expr : constant Node_Id := Expression (Act);
15108 begin
15109 if Nkind (Expr) = N_Subtype_Indication then
15110 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
15112 -- Analyze separately each discriminant constraint, when
15113 -- given with a named association.
15115 declare
15116 Constr : Node_Id;
15118 begin
15119 Constr := First (Constraints (Constraint (Expr)));
15120 while Present (Constr) loop
15121 if Nkind (Constr) = N_Discriminant_Association then
15122 Perform_Appropriate_Analysis
15123 (Expression (Constr));
15124 else
15125 Perform_Appropriate_Analysis (Constr);
15126 end if;
15128 Next (Constr);
15129 end loop;
15130 end;
15132 else
15133 Perform_Appropriate_Analysis (Expr);
15134 end if;
15135 end;
15137 elsif Nkind (Act) /= N_Operator_Symbol then
15138 Perform_Appropriate_Analysis (Act);
15140 -- Within a package instance, mark actuals that are limited
15141 -- views, so their use can be moved to the body of the
15142 -- enclosing unit.
15144 if Is_Entity_Name (Act)
15145 and then Is_Type (Entity (Act))
15146 and then From_Limited_With (Entity (Act))
15147 and then Present (Inst)
15148 then
15149 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15150 end if;
15151 end if;
15153 if Errs /= Serious_Errors_Detected then
15155 -- Do a minimal analysis of the generic, to prevent spurious
15156 -- warnings complaining about the generic being unreferenced,
15157 -- before abandoning the instantiation.
15159 Perform_Appropriate_Analysis (Name (N));
15161 if Is_Entity_Name (Name (N))
15162 and then Etype (Name (N)) /= Any_Type
15163 then
15164 Generate_Reference (Entity (Name (N)), Name (N));
15165 Set_Is_Instantiated (Entity (Name (N)));
15166 end if;
15168 if Present (Cur) then
15170 -- For the case of a child instance hiding an outer homonym,
15171 -- provide additional warning which might explain the error.
15173 Set_Is_Immediately_Visible (Cur, Vis);
15174 Error_Msg_NE
15175 ("& hides outer unit with the same name??",
15176 N, Defining_Unit_Name (N));
15177 end if;
15179 Abandon_Instantiation (Act);
15180 end if;
15181 end if;
15183 Next (Assoc);
15184 end loop;
15186 if Present (Cur) then
15187 Set_Is_Immediately_Visible (Cur, Vis);
15188 end if;
15189 end Preanalyze_Actuals;
15191 -------------------------------
15192 -- Provide_Completing_Bodies --
15193 -------------------------------
15195 procedure Provide_Completing_Bodies (N : Node_Id) is
15196 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15197 -- Generate the completing body for subprogram declaration Subp_Decl
15199 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15200 -- Generating completing bodies for all subprograms found in declarative
15201 -- list Decls.
15203 ---------------------------
15204 -- Build_Completing_Body --
15205 ---------------------------
15207 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15208 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15209 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15210 Spec : Node_Id;
15212 begin
15213 -- Nothing to do if the subprogram already has a completing body
15215 if Present (Corresponding_Body (Subp_Decl)) then
15216 return;
15218 -- Mark the function as having a valid return statement even though
15219 -- the body contains a single raise statement.
15221 elsif Ekind (Subp_Id) = E_Function then
15222 Set_Return_Present (Subp_Id);
15223 end if;
15225 -- Clone the specification to obtain new entities and reset the only
15226 -- semantic field.
15228 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15229 Set_Generic_Parent (Spec, Empty);
15231 -- Generate:
15232 -- function Func ... return ... is
15233 -- <or>
15234 -- procedure Proc ... is
15235 -- begin
15236 -- raise Program_Error with "access before elaboration";
15237 -- edn Proc;
15239 Insert_After_And_Analyze (Subp_Decl,
15240 Make_Subprogram_Body (Loc,
15241 Specification => Spec,
15242 Declarations => New_List,
15243 Handled_Statement_Sequence =>
15244 Make_Handled_Sequence_Of_Statements (Loc,
15245 Statements => New_List (
15246 Make_Raise_Program_Error (Loc,
15247 Reason => PE_Access_Before_Elaboration)))));
15248 end Build_Completing_Body;
15250 ----------------------------------
15251 -- Provide_Completing_Bodies_In --
15252 ----------------------------------
15254 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15255 Decl : Node_Id;
15257 begin
15258 if Present (Decls) then
15259 Decl := First (Decls);
15260 while Present (Decl) loop
15261 Provide_Completing_Bodies (Decl);
15262 Next (Decl);
15263 end loop;
15264 end if;
15265 end Provide_Completing_Bodies_In;
15267 -- Local variables
15269 Spec : Node_Id;
15271 -- Start of processing for Provide_Completing_Bodies
15273 begin
15274 if Nkind (N) = N_Package_Declaration then
15275 Spec := Specification (N);
15277 Push_Scope (Defining_Entity (N));
15278 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15279 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15280 Pop_Scope;
15282 elsif Nkind (N) = N_Subprogram_Declaration then
15283 Build_Completing_Body (N);
15284 end if;
15285 end Provide_Completing_Bodies;
15287 -------------------
15288 -- Remove_Parent --
15289 -------------------
15291 procedure Remove_Parent (In_Body : Boolean := False) is
15292 S : Entity_Id := Current_Scope;
15293 -- S is the scope containing the instantiation just completed. The scope
15294 -- stack contains the parent instances of the instantiation, followed by
15295 -- the original S.
15297 Cur_P : Entity_Id;
15298 E : Entity_Id;
15299 P : Entity_Id;
15300 Hidden : Elmt_Id;
15302 begin
15303 -- After child instantiation is complete, remove from scope stack the
15304 -- extra copy of the current scope, and then remove parent instances.
15306 if not In_Body then
15307 Pop_Scope;
15309 while Current_Scope /= S loop
15310 P := Current_Scope;
15311 End_Package_Scope (Current_Scope);
15313 if In_Open_Scopes (P) then
15314 E := First_Entity (P);
15315 while Present (E) loop
15316 Set_Is_Immediately_Visible (E, True);
15317 Next_Entity (E);
15318 end loop;
15320 -- If instantiation is declared in a block, it is the enclosing
15321 -- scope that might be a parent instance. Note that only one
15322 -- block can be involved, because the parent instances have
15323 -- been installed within it.
15325 if Ekind (P) = E_Block then
15326 Cur_P := Scope (P);
15327 else
15328 Cur_P := P;
15329 end if;
15331 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15332 -- We are within an instance of some sibling. Retain
15333 -- visibility of parent, for proper subsequent cleanup, and
15334 -- reinstall private declarations as well.
15336 Set_In_Private_Part (P);
15337 Install_Private_Declarations (P);
15338 end if;
15340 -- If the ultimate parent is a top-level unit recorded in
15341 -- Instance_Parent_Unit, then reset its visibility to what it was
15342 -- before instantiation. (It's not clear what the purpose is of
15343 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15344 -- present before the ultimate parent test was added.???)
15346 elsif not In_Open_Scopes (Scope (P))
15347 or else (P = Instance_Parent_Unit
15348 and then not Parent_Unit_Visible)
15349 then
15350 Set_Is_Immediately_Visible (P, False);
15352 -- If the current scope is itself an instantiation of a generic
15353 -- nested within P, and we are in the private part of body of this
15354 -- instantiation, restore the full views of P, that were removed
15355 -- in End_Package_Scope above. This obscure case can occur when a
15356 -- subunit of a generic contains an instance of a child unit of
15357 -- its generic parent unit.
15359 elsif S = Current_Scope and then Is_Generic_Instance (S)
15360 and then (In_Package_Body (S) or else In_Private_Part (S))
15361 then
15362 declare
15363 Par : constant Entity_Id :=
15364 Generic_Parent (Package_Specification (S));
15365 begin
15366 if Present (Par)
15367 and then P = Scope (Par)
15368 then
15369 Set_In_Private_Part (P);
15370 Install_Private_Declarations (P);
15371 end if;
15372 end;
15373 end if;
15374 end loop;
15376 -- Reset visibility of entities in the enclosing scope
15378 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15380 Hidden := First_Elmt (Hidden_Entities);
15381 while Present (Hidden) loop
15382 Set_Is_Immediately_Visible (Node (Hidden), True);
15383 Next_Elmt (Hidden);
15384 end loop;
15386 else
15387 -- Each body is analyzed separately, and there is no context that
15388 -- needs preserving from one body instance to the next, so remove all
15389 -- parent scopes that have been installed.
15391 while Present (S) loop
15392 End_Package_Scope (S);
15393 Set_Is_Immediately_Visible (S, False);
15394 S := Current_Scope;
15395 exit when S = Standard_Standard;
15396 end loop;
15397 end if;
15398 end Remove_Parent;
15400 -----------------------------------
15401 -- Requires_Conformance_Checking --
15402 -----------------------------------
15404 function Requires_Conformance_Checking (N : Node_Id) return Boolean is
15405 begin
15406 -- No conformance checking required if the generic actual part is empty,
15407 -- or is a box or an others_clause (necessarily with a box).
15409 return Present (Generic_Associations (N))
15410 and then not Box_Present (N)
15411 and then Nkind (First (Generic_Associations (N))) /= N_Others_Choice;
15412 end Requires_Conformance_Checking;
15414 -----------------
15415 -- Restore_Env --
15416 -----------------
15418 procedure Restore_Env is
15419 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15421 begin
15422 if No (Current_Instantiated_Parent.Act_Id) then
15423 -- Restore environment after subprogram inlining
15425 Restore_Private_Views (Empty);
15426 end if;
15428 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15429 Exchanged_Views := Saved.Exchanged_Views;
15430 Hidden_Entities := Saved.Hidden_Entities;
15431 Current_Sem_Unit := Saved.Current_Sem_Unit;
15432 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15433 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15435 Restore_Config_Switches (Saved.Switches);
15437 Instance_Envs.Decrement_Last;
15438 end Restore_Env;
15440 ---------------------------
15441 -- Restore_Private_Views --
15442 ---------------------------
15444 procedure Restore_Private_Views
15445 (Pack_Id : Entity_Id;
15446 Is_Package : Boolean := True)
15448 M : Elmt_Id;
15449 E : Entity_Id;
15450 Typ : Entity_Id;
15451 Dep_Elmt : Elmt_Id;
15452 Dep_Typ : Node_Id;
15454 procedure Restore_Nested_Formal (Formal : Entity_Id);
15455 -- Hide the generic formals of formal packages declared with box which
15456 -- were reachable in the current instantiation.
15458 ---------------------------
15459 -- Restore_Nested_Formal --
15460 ---------------------------
15462 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15463 pragma Assert (Ekind (Formal) = E_Package);
15464 Ent : Entity_Id;
15465 begin
15466 if Present (Renamed_Entity (Formal))
15467 and then Denotes_Formal_Package (Renamed_Entity (Formal), True)
15468 then
15469 return;
15471 elsif Present (Associated_Formal_Package (Formal)) then
15472 Ent := First_Entity (Formal);
15473 while Present (Ent) loop
15474 exit when Ekind (Ent) = E_Package
15475 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15477 Set_Is_Hidden (Ent);
15478 Set_Is_Potentially_Use_Visible (Ent, False);
15480 -- If package, then recurse
15482 if Ekind (Ent) = E_Package then
15483 Restore_Nested_Formal (Ent);
15484 end if;
15486 Next_Entity (Ent);
15487 end loop;
15488 end if;
15489 end Restore_Nested_Formal;
15491 -- Start of processing for Restore_Private_Views
15493 begin
15494 M := First_Elmt (Exchanged_Views);
15495 while Present (M) loop
15496 Typ := Node (M);
15498 -- Subtypes of types whose views have been exchanged, and that are
15499 -- defined within the instance, were not on the Private_Dependents
15500 -- list on entry to the instance, so they have to be exchanged
15501 -- explicitly now, in order to remain consistent with the view of the
15502 -- parent type.
15504 if Ekind (Typ) in E_Private_Type
15505 | E_Limited_Private_Type
15506 | E_Record_Type_With_Private
15507 then
15508 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15509 while Present (Dep_Elmt) loop
15510 Dep_Typ := Node (Dep_Elmt);
15512 if Scope (Dep_Typ) = Pack_Id
15513 and then Present (Full_View (Dep_Typ))
15514 then
15515 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15516 Exchange_Declarations (Dep_Typ);
15517 end if;
15519 Next_Elmt (Dep_Elmt);
15520 end loop;
15521 end if;
15523 Exchange_Declarations (Typ);
15524 Next_Elmt (M);
15525 end loop;
15527 if No (Pack_Id) then
15528 return;
15529 end if;
15531 -- Make the generic formal parameters private, and make the formal types
15532 -- into subtypes of the actuals again.
15534 E := First_Entity (Pack_Id);
15535 while Present (E) loop
15536 Set_Is_Hidden (E, True);
15538 if Is_Type (E)
15539 and then Nkind (Parent (E)) = N_Subtype_Declaration
15540 then
15541 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15542 -- as it is needed to identify the subtype with the type it
15543 -- renames, when there are conversions between access types
15544 -- to these.
15546 if GNATprove_Mode then
15547 null;
15549 -- If the actual for E is itself a generic actual type from
15550 -- an enclosing instance, E is still a generic actual type
15551 -- outside of the current instance. This matter when resolving
15552 -- an overloaded call that may be ambiguous in the enclosing
15553 -- instance, when two of its actuals coincide.
15555 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
15556 and then Is_Generic_Actual_Type
15557 (Entity (Subtype_Indication (Parent (E))))
15558 then
15559 null;
15560 else
15561 Set_Is_Generic_Actual_Type (E, False);
15563 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15564 -- flag also on the Full_View if the type is private, since it
15565 -- was set also on this Full_View. However, this flag is relied
15566 -- upon by Covers to spot "types exported from instantiations"
15567 -- which are implicit Full_Views built for instantiations made
15568 -- on private types and we get type mismatches if we do it when
15569 -- the block exchanging the declarations below triggers ???
15571 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15572 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15573 -- end if;
15574 end if;
15576 -- An unusual case of aliasing: the actual may also be directly
15577 -- visible in the generic, and be private there, while it is fully
15578 -- visible in the context of the instance. The internal subtype
15579 -- is private in the instance but has full visibility like its
15580 -- parent in the enclosing scope. This enforces the invariant that
15581 -- the privacy status of all private dependents of a type coincide
15582 -- with that of the parent type. This can only happen when a
15583 -- generic child unit is instantiated within a sibling.
15585 if Is_Private_Type (E)
15586 and then not Is_Private_Type (Etype (E))
15587 then
15588 Exchange_Declarations (E);
15589 end if;
15591 elsif Ekind (E) = E_Package then
15593 -- The end of the renaming list is the renaming of the generic
15594 -- package itself. If the instance is a subprogram, all entities
15595 -- in the corresponding package are renamings. If this entity is
15596 -- a formal package, make its own formals private as well. The
15597 -- actual in this case is itself the renaming of an instantiation.
15598 -- If the entity is not a package renaming, it is the entity
15599 -- created to validate formal package actuals: ignore it.
15601 -- If the actual is itself a formal package for the enclosing
15602 -- generic, or the actual for such a formal package, it remains
15603 -- visible on exit from the instance, and therefore nothing needs
15604 -- to be done either, except to keep it accessible.
15606 if Is_Package and then Renamed_Entity (E) = Pack_Id then
15607 exit;
15609 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15610 null;
15612 elsif
15613 Denotes_Formal_Package (Renamed_Entity (E), True, Pack_Id)
15614 then
15615 Set_Is_Hidden (E, False);
15617 else
15618 declare
15619 Act_P : constant Entity_Id := Renamed_Entity (E);
15620 Id : Entity_Id;
15622 begin
15623 Id := First_Entity (Act_P);
15624 while Present (Id)
15625 and then Id /= First_Private_Entity (Act_P)
15626 loop
15627 exit when Ekind (Id) = E_Package
15628 and then Renamed_Entity (Id) = Act_P;
15630 Set_Is_Hidden (Id, True);
15631 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15633 if Ekind (Id) = E_Package then
15634 Restore_Nested_Formal (Id);
15635 end if;
15637 Next_Entity (Id);
15638 end loop;
15639 end;
15640 end if;
15641 end if;
15643 Next_Entity (E);
15644 end loop;
15645 end Restore_Private_Views;
15647 --------------
15648 -- Save_Env --
15649 --------------
15651 procedure Save_Env
15652 (Gen_Unit : Entity_Id;
15653 Act_Unit : Entity_Id)
15655 begin
15656 Init_Env;
15657 Set_Instance_Env (Gen_Unit, Act_Unit);
15658 end Save_Env;
15660 ----------------------------
15661 -- Save_Global_References --
15662 ----------------------------
15664 procedure Save_Global_References (Templ : Node_Id) is
15666 -- ??? it is horrible to use global variables in highly recursive code
15668 E : Entity_Id;
15669 -- The entity of the current associated node
15671 Gen_Scope : Entity_Id;
15672 -- The scope of the generic for which references are being saved
15674 N2 : Node_Id;
15675 -- The current associated node
15677 function Is_Global (E : Entity_Id) return Boolean;
15678 -- Check whether entity is defined outside of generic unit. Examine the
15679 -- scope of an entity, and the scope of the scope, etc, until we find
15680 -- either Standard, in which case the entity is global, or the generic
15681 -- unit itself, which indicates that the entity is local. If the entity
15682 -- is the generic unit itself, as in the case of a recursive call, or
15683 -- the enclosing generic unit, if different from the current scope, then
15684 -- it is local as well, because it will be replaced at the point of
15685 -- instantiation. On the other hand, if it is a reference to a child
15686 -- unit of a common ancestor, which appears in an instantiation, it is
15687 -- global because it is used to denote a specific compilation unit at
15688 -- the time the instantiations will be analyzed.
15690 procedure Qualify_Universal_Operands
15691 (Op : Node_Id;
15692 Func_Call : Node_Id);
15693 -- Op denotes a binary or unary operator in generic template Templ. Node
15694 -- Func_Call is the function call alternative of the operator within the
15695 -- the analyzed copy of the template. Change each operand which yields a
15696 -- universal type by wrapping it into a qualified expression
15698 -- Actual_Typ'(Operand)
15700 -- where Actual_Typ is the type of corresponding actual parameter of
15701 -- Operand in Func_Call.
15703 procedure Reset_Entity (N : Node_Id);
15704 -- Save semantic information on global entity so that it is not resolved
15705 -- again at instantiation time.
15707 procedure Save_Entity_Descendants (N : Node_Id);
15708 -- Apply Save_Global_References to the two syntactic descendants of
15709 -- non-terminal nodes that carry an Associated_Node and are processed
15710 -- through Reset_Entity. Once the global entity (if any) has been
15711 -- captured together with its type, only two syntactic descendants need
15712 -- to be traversed to complete the processing of the tree rooted at N.
15713 -- This applies to Selected_Components, Expanded_Names, and to Operator
15714 -- nodes. N can also be a character literal, identifier, or operator
15715 -- symbol node, but the call has no effect in these cases.
15717 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15718 -- Default actuals in nested instances must be handled specially
15719 -- because there is no link to them from the original tree. When an
15720 -- actual subprogram is given by a default, we add an explicit generic
15721 -- association for it in the instantiation node. When we save the
15722 -- global references on the name of the instance, we recover the list
15723 -- of generic associations, and add an explicit one to the original
15724 -- generic tree, through which a global actual can be preserved.
15725 -- Similarly, if a child unit is instantiated within a sibling, in the
15726 -- context of the parent, we must preserve the identifier of the parent
15727 -- so that it can be properly resolved in a subsequent instantiation.
15729 procedure Save_Global_Descendant (D : Union_Id);
15730 -- Apply Save_References recursively to the descendants of node D
15732 procedure Save_References (N : Node_Id);
15733 -- This is the recursive procedure that does the work, once the
15734 -- enclosing generic scope has been established.
15736 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15737 -- If the type of N2 is global to the generic unit, save the type in
15738 -- the generic node. Just as we perform name capture for explicit
15739 -- references within the generic, we must capture the global types
15740 -- of local entities because they may participate in resolution in
15741 -- the instance.
15743 ---------------
15744 -- Is_Global --
15745 ---------------
15747 function Is_Global (E : Entity_Id) return Boolean is
15748 Se : Entity_Id;
15750 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15751 -- Determine whether the parent node of a reference to a child unit
15752 -- denotes an instantiation or a formal package, in which case the
15753 -- reference to the child unit is global, even if it appears within
15754 -- the current scope (e.g. when the instance appears within the body
15755 -- of an ancestor).
15757 ----------------------
15758 -- Is_Instance_Node --
15759 ----------------------
15761 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15762 begin
15763 return Nkind (Decl) in N_Generic_Instantiation
15764 or else
15765 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15766 end Is_Instance_Node;
15768 -- Start of processing for Is_Global
15770 begin
15771 if E = Gen_Scope then
15772 return False;
15774 elsif E = Standard_Standard then
15775 return True;
15777 -- E should be an entity, but it is not always
15779 elsif Nkind (E) not in N_Entity then
15780 return False;
15782 elsif Nkind (E) /= N_Expanded_Name
15783 and then Is_Child_Unit (E)
15784 and then (Is_Instance_Node (Parent (N2))
15785 or else (Nkind (Parent (N2)) = N_Expanded_Name
15786 and then N2 = Selector_Name (Parent (N2))
15787 and then
15788 Is_Instance_Node (Parent (Parent (N2)))))
15789 then
15790 return True;
15792 else
15793 -- E may be an expanded name - typically an operator - in which
15794 -- case we must find its enclosing scope since expanded names
15795 -- don't have corresponding scopes.
15797 if Nkind (E) = N_Expanded_Name then
15798 Se := Find_Enclosing_Scope (E);
15800 -- Otherwise, E is an entity and will have Scope set
15802 else
15803 Se := Scope (E);
15804 end if;
15806 while Se /= Gen_Scope loop
15807 if Se = Standard_Standard then
15808 return True;
15809 else
15810 Se := Scope (Se);
15811 end if;
15812 end loop;
15814 return False;
15815 end if;
15816 end Is_Global;
15818 --------------------------------
15819 -- Qualify_Universal_Operands --
15820 --------------------------------
15822 procedure Qualify_Universal_Operands
15823 (Op : Node_Id;
15824 Func_Call : Node_Id)
15826 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15827 -- Rewrite operand Opnd as a qualified expression of the form
15829 -- Actual_Typ'(Opnd)
15831 -- where Actual is the corresponding actual parameter of Opnd in
15832 -- function call Func_Call.
15834 function Qualify_Type
15835 (Loc : Source_Ptr;
15836 Typ : Entity_Id) return Node_Id;
15837 -- Qualify type Typ by creating a selected component of the form
15839 -- Scope_Of_Typ.Typ
15841 ---------------------
15842 -- Qualify_Operand --
15843 ---------------------
15845 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15846 Loc : constant Source_Ptr := Sloc (Opnd);
15847 Typ : constant Entity_Id := Etype (Actual);
15848 Mark : Node_Id;
15849 Qual : Node_Id;
15851 begin
15852 -- Qualify the operand when it is of a universal type. Note that
15853 -- the template is unanalyzed and it is not possible to directly
15854 -- query the type. This transformation is not done when the type
15855 -- of the actual is internally generated because the type will be
15856 -- regenerated in the instance.
15858 if Yields_Universal_Type (Opnd)
15859 and then Comes_From_Source (Typ)
15860 and then not Is_Hidden (Typ)
15861 then
15862 -- The type of the actual may be a global reference. Save this
15863 -- information by creating a reference to it.
15865 if Is_Global (Typ) then
15866 Mark := New_Occurrence_Of (Typ, Loc);
15868 -- Otherwise rely on resolution to find the proper type within
15869 -- the instance.
15871 else
15872 Mark := Qualify_Type (Loc, Typ);
15873 end if;
15875 Qual :=
15876 Make_Qualified_Expression (Loc,
15877 Subtype_Mark => Mark,
15878 Expression => Relocate_Node (Opnd));
15880 -- Mark the qualification to distinguish it from other source
15881 -- constructs and signal the instantiation mechanism that this
15882 -- node requires special processing. See Copy_Generic_Node for
15883 -- details.
15885 Set_Is_Qualified_Universal_Literal (Qual);
15887 Rewrite (Opnd, Qual);
15888 end if;
15889 end Qualify_Operand;
15891 ------------------
15892 -- Qualify_Type --
15893 ------------------
15895 function Qualify_Type
15896 (Loc : Source_Ptr;
15897 Typ : Entity_Id) return Node_Id
15899 Scop : constant Entity_Id := Scope (Typ);
15900 Result : Node_Id;
15902 begin
15903 Result := Make_Identifier (Loc, Chars (Typ));
15905 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15906 Result :=
15907 Make_Selected_Component (Loc,
15908 Prefix => Make_Identifier (Loc, Chars (Scop)),
15909 Selector_Name => Result);
15910 end if;
15912 return Result;
15913 end Qualify_Type;
15915 -- Local variables
15917 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15919 -- Start of processing for Qualify_Universal_Operands
15921 begin
15922 if Nkind (Op) in N_Binary_Op then
15923 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15924 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15926 elsif Nkind (Op) in N_Unary_Op then
15927 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15928 end if;
15929 end Qualify_Universal_Operands;
15931 ------------------
15932 -- Reset_Entity --
15933 ------------------
15935 procedure Reset_Entity (N : Node_Id) is
15936 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15937 -- Find the ultimate ancestor of the current unit. If it is not a
15938 -- generic unit, then the name of the current unit in the prefix of
15939 -- an expanded name must be replaced with its generic homonym to
15940 -- ensure that it will be properly resolved in an instance.
15942 ------------------
15943 -- Top_Ancestor --
15944 ------------------
15946 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15947 Par : Entity_Id;
15949 begin
15950 Par := E;
15951 while Is_Child_Unit (Par) loop
15952 Par := Scope (Par);
15953 end loop;
15955 return Par;
15956 end Top_Ancestor;
15958 -- Start of processing for Reset_Entity
15960 begin
15961 N2 := Get_Associated_Node (N);
15962 E := Entity (N2);
15964 if Present (E) then
15966 -- If the node is an entry call to an entry in an enclosing task,
15967 -- it is rewritten as a selected component. No global entity to
15968 -- preserve in this case, since the expansion will be redone in
15969 -- the instance.
15971 if Nkind (E) not in N_Entity then
15972 Set_Associated_Node (N, Empty);
15973 Set_Etype (N, Empty);
15974 return;
15975 end if;
15977 -- If the entity is an itype created as a subtype of an access
15978 -- type with a null exclusion restore source entity for proper
15979 -- visibility. The itype will be created anew in the instance.
15981 if Is_Itype (E)
15982 and then Ekind (E) = E_Access_Subtype
15983 and then Is_Entity_Name (N)
15984 and then Chars (Etype (E)) = Chars (N)
15985 then
15986 E := Etype (E);
15987 Set_Entity (N2, E);
15988 Set_Etype (N2, E);
15989 end if;
15991 if Is_Global (E) then
15992 Set_Global_Type (N, N2);
15994 elsif Nkind (N) = N_Op_Concat
15995 and then Is_Generic_Type (Etype (N2))
15996 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15997 or else
15998 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15999 and then Is_Intrinsic_Subprogram (E)
16000 then
16001 null;
16003 -- Entity is local. Mark generic node as unresolved. Note that now
16004 -- it does not have an entity.
16006 else
16007 Set_Associated_Node (N, Empty);
16008 Set_Etype (N, Empty);
16009 end if;
16011 if Nkind (Parent (N)) in N_Generic_Instantiation
16012 and then N = Name (Parent (N))
16013 then
16014 Save_Global_Defaults (Parent (N), Parent (N2));
16015 end if;
16017 elsif Nkind (Parent (N)) = N_Selected_Component
16018 and then Nkind (Parent (N2)) = N_Expanded_Name
16019 then
16020 -- In case of previous errors, the tree might be malformed
16022 if No (Entity (Parent (N2))) then
16023 null;
16025 elsif Is_Global (Entity (Parent (N2))) then
16026 Change_Selected_Component_To_Expanded_Name (Parent (N));
16027 Set_Associated_Node (Parent (N), Parent (N2));
16028 Set_Global_Type (Parent (N), Parent (N2));
16029 Save_Entity_Descendants (N);
16031 -- If this is a reference to the current generic entity, replace
16032 -- by the name of the generic homonym of the current package. This
16033 -- is because in an instantiation Par.P.Q will not resolve to the
16034 -- name of the instance, whose enclosing scope is not necessarily
16035 -- Par. We use the generic homonym rather that the name of the
16036 -- generic itself because it may be hidden by a local declaration.
16038 elsif In_Open_Scopes (Entity (Parent (N2)))
16039 and then not
16040 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
16041 then
16042 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
16043 Rewrite (Parent (N),
16044 Make_Identifier (Sloc (N),
16045 Chars =>
16046 Chars (Generic_Homonym (Entity (Parent (N2))))));
16047 else
16048 Rewrite (Parent (N),
16049 Make_Identifier (Sloc (N),
16050 Chars => Chars (Selector_Name (Parent (N2)))));
16051 end if;
16052 end if;
16054 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
16055 and then Parent (N) = Name (Parent (Parent (N)))
16056 then
16057 Save_Global_Defaults
16058 (Parent (Parent (N)), Parent (Parent (N2)));
16059 end if;
16061 -- A selected component may denote a static constant that has been
16062 -- folded. If the static constant is global to the generic, capture
16063 -- its value. Otherwise the folding will happen in any instantiation.
16065 elsif Nkind (Parent (N)) = N_Selected_Component
16066 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
16067 then
16068 if Present (Entity (Original_Node (Parent (N2))))
16069 and then Is_Global (Entity (Original_Node (Parent (N2))))
16070 then
16071 Rewrite (Parent (N), New_Copy (Parent (N2)));
16072 Set_Analyzed (Parent (N), False);
16073 end if;
16075 -- A selected component may be transformed into a parameterless
16076 -- function call. If the called entity is global, rewrite the node
16077 -- appropriately, i.e. as an extended name for the global entity.
16079 elsif Nkind (Parent (N)) = N_Selected_Component
16080 and then Nkind (Parent (N2)) = N_Function_Call
16081 and then N = Selector_Name (Parent (N))
16082 then
16083 if No (Parameter_Associations (Parent (N2))) then
16084 if Is_Global (Entity (Name (Parent (N2)))) then
16085 Change_Selected_Component_To_Expanded_Name (Parent (N));
16086 Set_Associated_Node (Parent (N), Name (Parent (N2)));
16087 Set_Global_Type (Parent (N), Name (Parent (N2)));
16088 Save_Entity_Descendants (N);
16090 else
16091 Set_Is_Prefixed_Call (Parent (N));
16092 Set_Associated_Node (N, Empty);
16093 Set_Etype (N, Empty);
16094 end if;
16096 -- In Ada 2005, X.F may be a call to a primitive operation,
16097 -- rewritten as F (X). This rewriting will be done again in an
16098 -- instance, so keep the original node. Global entities will be
16099 -- captured as for other constructs. Indicate that this must
16100 -- resolve as a call, to prevent accidental overloading in the
16101 -- instance, if both a component and a primitive operation appear
16102 -- as candidates.
16104 else
16105 Set_Is_Prefixed_Call (Parent (N));
16106 end if;
16108 -- Entity is local. Reset in generic unit, so that node is resolved
16109 -- anew at the point of instantiation.
16111 else
16112 Set_Associated_Node (N, Empty);
16113 Set_Etype (N, Empty);
16114 end if;
16115 end Reset_Entity;
16117 -----------------------------
16118 -- Save_Entity_Descendants --
16119 -----------------------------
16121 procedure Save_Entity_Descendants (N : Node_Id) is
16122 begin
16123 case Nkind (N) is
16124 when N_Binary_Op =>
16125 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
16126 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16128 when N_Unary_Op =>
16129 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16131 when N_Expanded_Name
16132 | N_Selected_Component
16134 Save_Global_Descendant (Union_Id (Prefix (N)));
16135 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16137 when N_Character_Literal
16138 | N_Identifier
16139 | N_Operator_Symbol
16141 null;
16143 when others =>
16144 raise Program_Error;
16145 end case;
16146 end Save_Entity_Descendants;
16148 --------------------------
16149 -- Save_Global_Defaults --
16150 --------------------------
16152 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16153 Loc : constant Source_Ptr := Sloc (N1);
16154 Assoc2 : constant List_Id := Generic_Associations (N2);
16155 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16156 Assoc1 : List_Id;
16157 Act1 : Node_Id;
16158 Act2 : Node_Id;
16159 Def : Node_Id;
16160 Ndec : Node_Id;
16161 Subp : Entity_Id;
16162 Actual : Entity_Id;
16164 begin
16165 Assoc1 := Generic_Associations (N1);
16167 if Present (Assoc1) then
16168 Act1 := First (Assoc1);
16169 else
16170 Act1 := Empty;
16171 Set_Generic_Associations (N1, New_List);
16172 Assoc1 := Generic_Associations (N1);
16173 end if;
16175 if Present (Assoc2) then
16176 Act2 := First (Assoc2);
16177 else
16178 return;
16179 end if;
16181 while Present (Act1) and then Present (Act2) loop
16182 Next (Act1);
16183 Next (Act2);
16184 end loop;
16186 -- Find the associations added for default subprograms
16188 if Present (Act2) then
16189 while Nkind (Act2) /= N_Generic_Association
16190 or else No (Entity (Selector_Name (Act2)))
16191 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16192 loop
16193 Next (Act2);
16194 end loop;
16196 -- Add a similar association if the default is global. The
16197 -- renaming declaration for the actual has been analyzed, and
16198 -- its alias is the program it renames. Link the actual in the
16199 -- original generic tree with the node in the analyzed tree.
16201 while Present (Act2) loop
16202 Subp := Entity (Selector_Name (Act2));
16203 Def := Explicit_Generic_Actual_Parameter (Act2);
16205 -- Following test is defence against rubbish errors
16207 if No (Alias (Subp)) then
16208 return;
16209 end if;
16211 -- Retrieve the resolved actual from the renaming declaration
16212 -- created for the instantiated formal.
16214 Actual := Entity (Name (Parent (Parent (Subp))));
16215 Set_Entity (Def, Actual);
16216 Set_Etype (Def, Etype (Actual));
16218 if Is_Global (Actual) then
16219 Ndec :=
16220 Make_Generic_Association (Loc,
16221 Selector_Name =>
16222 New_Occurrence_Of (Subp, Loc),
16223 Explicit_Generic_Actual_Parameter =>
16224 New_Occurrence_Of (Actual, Loc));
16226 Set_Associated_Node
16227 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16229 Append (Ndec, Assoc1);
16231 -- If there are other defaults, add a dummy association in case
16232 -- there are other defaulted formals with the same name.
16234 elsif Present (Next (Act2)) then
16235 Ndec :=
16236 Make_Generic_Association (Loc,
16237 Selector_Name =>
16238 New_Occurrence_Of (Subp, Loc),
16239 Explicit_Generic_Actual_Parameter => Empty);
16241 Append (Ndec, Assoc1);
16242 end if;
16244 Next (Act2);
16245 end loop;
16246 end if;
16248 if Nkind (Name (N1)) = N_Identifier
16249 and then Is_Child_Unit (Gen_Id)
16250 and then Is_Global (Gen_Id)
16251 and then Is_Generic_Unit (Scope (Gen_Id))
16252 and then In_Open_Scopes (Scope (Gen_Id))
16253 then
16254 -- This is an instantiation of a child unit within a sibling, so
16255 -- that the generic parent is in scope. An eventual instance must
16256 -- occur within the scope of an instance of the parent. Make name
16257 -- in instance into an expanded name, to preserve the identifier
16258 -- of the parent, so it can be resolved subsequently.
16260 Rewrite (Name (N2),
16261 Make_Expanded_Name (Loc,
16262 Chars => Chars (Gen_Id),
16263 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16264 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16265 Set_Entity (Name (N2), Gen_Id);
16267 Rewrite (Name (N1),
16268 Make_Expanded_Name (Loc,
16269 Chars => Chars (Gen_Id),
16270 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16271 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16273 Set_Associated_Node (Name (N1), Name (N2));
16274 Set_Associated_Node (Prefix (Name (N1)), Empty);
16275 Set_Associated_Node
16276 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16277 Set_Etype (Name (N1), Etype (Gen_Id));
16278 end if;
16279 end Save_Global_Defaults;
16281 ----------------------------
16282 -- Save_Global_Descendant --
16283 ----------------------------
16285 procedure Save_Global_Descendant (D : Union_Id) is
16286 N1 : Node_Id;
16288 begin
16289 if D in Node_Range then
16290 if D = Union_Id (Empty) then
16291 null;
16293 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16294 Save_References (Node_Id (D));
16295 end if;
16297 elsif D in List_Range then
16298 pragma Assert (D /= Union_Id (No_List));
16299 -- Because No_List = Empty, which is in Node_Range above
16301 N1 := First (List_Id (D));
16302 while Present (N1) loop
16303 Save_References (N1);
16304 Next (N1);
16305 end loop;
16307 -- Element list or other non-node field, nothing to do
16309 else
16310 null;
16311 end if;
16312 end Save_Global_Descendant;
16314 ---------------------
16315 -- Save_References --
16316 ---------------------
16318 -- This is the recursive procedure that does the work once the enclosing
16319 -- generic scope has been established. We have to treat specially a
16320 -- number of node rewritings that are required by semantic processing
16321 -- and which change the kind of nodes in the generic copy: typically
16322 -- constant-folding, replacing an operator node by a string literal, or
16323 -- a selected component by an expanded name. In each of those cases, the
16324 -- transformation is propagated to the generic unit.
16326 procedure Save_References (N : Node_Id) is
16327 Loc : constant Source_Ptr := Sloc (N);
16329 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16330 -- Determine whether arbitrary node Nod requires delayed capture of
16331 -- global references within its aspect specifications.
16333 procedure Save_References_In_Aggregate (N : Node_Id);
16334 -- Save all global references in [extension] aggregate node N
16336 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16337 -- Save all global references in a character literal or operator
16338 -- symbol denoted by N.
16340 procedure Save_References_In_Descendants (N : Node_Id);
16341 -- Save all global references in all descendants of node N
16343 procedure Save_References_In_Identifier (N : Node_Id);
16344 -- Save all global references in identifier node N
16346 procedure Save_References_In_Operator (N : Node_Id);
16347 -- Save all global references in operator node N
16349 procedure Save_References_In_Pragma (Prag : Node_Id);
16350 -- Save all global references found within the expression of pragma
16351 -- Prag.
16353 ---------------------------
16354 -- Requires_Delayed_Save --
16355 ---------------------------
16357 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16358 begin
16359 -- Generic packages and subprograms require delayed capture of
16360 -- global references within their aspects due to the timing of
16361 -- annotation analysis.
16363 if Nkind (Nod) in N_Generic_Package_Declaration
16364 | N_Generic_Subprogram_Declaration
16365 | N_Package_Body
16366 | N_Package_Body_Stub
16367 | N_Subprogram_Body
16368 | N_Subprogram_Body_Stub
16369 then
16370 -- Since the capture of global references is done on the
16371 -- unanalyzed generic template, there is no information around
16372 -- to infer the context. Use the Associated_Entity linkages to
16373 -- peek into the analyzed generic copy and determine what the
16374 -- template corresponds to.
16376 if Nod = Templ then
16377 return
16378 Is_Generic_Declaration_Or_Body
16379 (Unit_Declaration_Node
16380 (Associated_Entity (Defining_Entity (Nod))));
16382 -- Otherwise the generic unit being processed is not the top
16383 -- level template. It is safe to capture of global references
16384 -- within the generic unit because at this point the top level
16385 -- copy is fully analyzed.
16387 else
16388 return False;
16389 end if;
16391 -- Otherwise capture the global references without interference
16393 else
16394 return False;
16395 end if;
16396 end Requires_Delayed_Save;
16398 ----------------------------------
16399 -- Save_References_In_Aggregate --
16400 ----------------------------------
16402 procedure Save_References_In_Aggregate (N : Node_Id) is
16403 Nam : Node_Id;
16404 Qual : Node_Id := Empty;
16405 Typ : Entity_Id := Empty;
16407 begin
16408 N2 := Get_Associated_Node (N);
16410 if Present (N2) then
16411 Typ := Etype (N2);
16413 -- In an instance within a generic, use the name of the actual
16414 -- and not the original generic parameter. If the actual is
16415 -- global in the current generic it must be preserved for its
16416 -- instantiation.
16418 if Parent_Kind (Typ) = N_Subtype_Declaration
16419 and then Present (Generic_Parent_Type (Parent (Typ)))
16420 then
16421 Typ := Base_Type (Typ);
16422 Set_Etype (N2, Typ);
16423 end if;
16424 end if;
16426 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16427 Set_Associated_Node (N, Empty);
16429 -- If the aggregate is an actual in a call, it has been
16430 -- resolved in the current context, to some local type. The
16431 -- enclosing call may have been disambiguated by the aggregate,
16432 -- and this disambiguation might fail at instantiation time
16433 -- because the type to which the aggregate did resolve is not
16434 -- preserved. In order to preserve some of this information,
16435 -- wrap the aggregate in a qualified expression, using the id
16436 -- of its type. For further disambiguation we qualify the type
16437 -- name with its scope (if visible and not hidden by a local
16438 -- homograph) because both id's will have corresponding
16439 -- entities in an instance. This resolves most of the problems
16440 -- with missing type information on aggregates in instances.
16442 if Present (N2)
16443 and then Nkind (N2) = Nkind (N)
16444 and then Nkind (Parent (N2)) in N_Subprogram_Call
16445 and then Present (Typ)
16446 and then Comes_From_Source (Typ)
16447 then
16448 Nam := Make_Identifier (Loc, Chars (Typ));
16450 if Is_Immediately_Visible (Scope (Typ))
16451 and then
16452 (not In_Open_Scopes (Scope (Typ))
16453 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16454 then
16455 Nam :=
16456 Make_Selected_Component (Loc,
16457 Prefix =>
16458 Make_Identifier (Loc, Chars (Scope (Typ))),
16459 Selector_Name => Nam);
16460 end if;
16462 Qual :=
16463 Make_Qualified_Expression (Loc,
16464 Subtype_Mark => Nam,
16465 Expression => Relocate_Node (N));
16466 end if;
16467 end if;
16469 if Nkind (N) = N_Aggregate then
16470 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
16472 elsif Nkind (N) = N_Extension_Aggregate then
16473 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
16475 else
16476 pragma Assert (False);
16477 end if;
16479 Save_Global_Descendant (Union_Id (Expressions (N)));
16480 Save_Global_Descendant (Union_Id (Component_Associations (N)));
16481 Save_Global_Descendant (Union_Id (Etype (N)));
16483 if Present (Qual) then
16484 Rewrite (N, Qual);
16485 end if;
16486 end Save_References_In_Aggregate;
16488 ----------------------------------------------
16489 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16490 ----------------------------------------------
16492 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16493 begin
16494 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16495 Reset_Entity (N);
16497 elsif Nkind (N) = N_Operator_Symbol
16498 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16499 then
16500 Change_Operator_Symbol_To_String_Literal (N);
16501 end if;
16502 end Save_References_In_Char_Lit_Or_Op_Symbol;
16504 ------------------------------------
16505 -- Save_References_In_Descendants --
16506 ------------------------------------
16508 procedure Save_References_In_Descendants (N : Node_Id) is
16509 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
16510 begin
16511 Walk (N);
16512 end Save_References_In_Descendants;
16514 -----------------------------------
16515 -- Save_References_In_Identifier --
16516 -----------------------------------
16518 procedure Save_References_In_Identifier (N : Node_Id) is
16519 begin
16520 -- The node did not undergo a transformation
16522 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16523 -- If this is a discriminant reference, always save it.
16524 -- It is used in the instance to find the corresponding
16525 -- discriminant positionally rather than by name.
16527 Set_Original_Discriminant
16528 (N, Original_Discriminant (Get_Associated_Node (N)));
16530 Reset_Entity (N);
16532 -- The analysis of the generic copy transformed the identifier
16533 -- into another construct. Propagate the changes to the template.
16535 else
16536 N2 := Get_Associated_Node (N);
16538 -- The identifier denotes a call to a parameterless function.
16539 -- Mark the node as resolved when the function is external.
16541 if Nkind (N2) = N_Function_Call then
16542 E := Entity (Name (N2));
16544 if Present (E) and then Is_Global (E) then
16545 Set_Global_Type (N, N2);
16546 else
16547 Set_Associated_Node (N, Empty);
16548 Set_Etype (N, Empty);
16549 end if;
16551 -- The identifier denotes a named number that was constant
16552 -- folded. Preserve the original name for ASIS and undo the
16553 -- constant folding which will be repeated in the instance.
16554 -- Is this still needed???
16556 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16557 and then Is_Entity_Name (Original_Node (N2))
16558 then
16559 Set_Associated_Node (N, Original_Node (N2));
16560 Reset_Entity (N);
16562 -- The identifier resolved to a string literal. Propagate this
16563 -- information to the generic template.
16565 elsif Nkind (N2) = N_String_Literal then
16566 Rewrite (N, New_Copy (N2));
16568 -- The identifier is rewritten as a dereference if it is the
16569 -- prefix of an implicit dereference. Preserve the original
16570 -- tree as the analysis of the instance will expand the node
16571 -- again, but preserve the resolved entity if it is global.
16573 elsif Nkind (N2) = N_Explicit_Dereference then
16574 if Is_Entity_Name (Prefix (N2))
16575 and then Present (Entity (Prefix (N2)))
16576 and then Is_Global (Entity (Prefix (N2)))
16577 then
16578 Set_Associated_Node (N, Prefix (N2));
16579 Set_Global_Type (N, Prefix (N2));
16581 elsif Nkind (Prefix (N2)) = N_Function_Call
16582 and then Is_Entity_Name (Name (Prefix (N2)))
16583 and then Present (Entity (Name (Prefix (N2))))
16584 and then Is_Global (Entity (Name (Prefix (N2))))
16585 then
16586 Rewrite (N,
16587 Make_Explicit_Dereference (Loc,
16588 Prefix =>
16589 Make_Function_Call (Loc,
16590 Name =>
16591 New_Occurrence_Of
16592 (Entity (Name (Prefix (N2))), Loc))));
16593 Set_Associated_Node
16594 (Name (Prefix (N)), Name (Prefix (N2)));
16595 Set_Global_Type (Name (Prefix (N)), Name (Prefix (N2)));
16597 else
16598 Set_Associated_Node (N, Empty);
16599 Set_Etype (N, Empty);
16600 end if;
16602 -- The subtype mark of a nominally unconstrained object is
16603 -- rewritten as a subtype indication using the bounds of the
16604 -- expression. Recover the original subtype mark.
16606 elsif Nkind (N2) = N_Subtype_Indication
16607 and then Is_Entity_Name (Original_Node (N2))
16608 then
16609 Set_Associated_Node (N, Original_Node (N2));
16610 Reset_Entity (N);
16611 end if;
16612 end if;
16613 end Save_References_In_Identifier;
16615 ---------------------------------
16616 -- Save_References_In_Operator --
16617 ---------------------------------
16619 procedure Save_References_In_Operator (N : Node_Id) is
16620 begin
16621 N2 := Get_Associated_Node (N);
16623 -- The node did not undergo a transformation
16625 if Nkind (N) = Nkind (N2) then
16626 if Nkind (N) = N_Op_Concat then
16627 Set_Is_Component_Left_Opnd
16628 (N, Is_Component_Left_Opnd (N2));
16629 Set_Is_Component_Right_Opnd
16630 (N, Is_Component_Right_Opnd (N2));
16631 end if;
16633 Reset_Entity (N);
16635 -- The analysis of the generic copy transformed the operator into
16636 -- some other construct. Propagate the changes to the template if
16637 -- applicable.
16639 else
16640 -- The operator resoved to a function call
16642 if Nkind (N2) = N_Function_Call then
16644 -- Add explicit qualifications in the generic template for
16645 -- all operands of universal type. This aids resolution by
16646 -- preserving the actual type of a literal or an attribute
16647 -- that yields a universal result.
16649 Qualify_Universal_Operands (N, N2);
16651 E := Entity (Name (N2));
16653 if Present (E) and then Is_Global (E) then
16654 Set_Global_Type (N, N2);
16655 else
16656 Set_Associated_Node (N, Empty);
16657 Set_Etype (N, Empty);
16658 end if;
16660 -- The operator was folded into a literal
16662 elsif Nkind (N2) in N_Integer_Literal
16663 | N_Real_Literal
16664 | N_String_Literal
16665 then
16666 if Present (Original_Node (N2))
16667 and then Nkind (Original_Node (N2)) = Nkind (N)
16668 then
16669 -- Operation was constant-folded. Whenever possible,
16670 -- recover semantic information from unfolded node.
16671 -- This was initially done for ASIS but is apparently
16672 -- needed also for e.g. compiling a-nbnbin.adb.
16674 Set_Associated_Node (N, Original_Node (N2));
16676 if Nkind (N) = N_Op_Concat then
16677 Set_Is_Component_Left_Opnd (N,
16678 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16679 Set_Is_Component_Right_Opnd (N,
16680 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16681 end if;
16683 Reset_Entity (N);
16685 -- Propagate the constant folding back to the template
16687 else
16688 Rewrite (N, New_Copy (N2));
16689 Set_Analyzed (N, False);
16690 end if;
16692 -- The operator was folded into an enumeration literal. Retain
16693 -- the entity to avoid spurious ambiguities if it is overloaded
16694 -- at the point of instantiation or inlining.
16696 elsif Nkind (N2) = N_Identifier
16697 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16698 then
16699 Rewrite (N, New_Copy (N2));
16700 Set_Analyzed (N, False);
16701 end if;
16702 end if;
16704 -- Complete the operands check if node has not been constant
16705 -- folded.
16707 if Nkind (N) in N_Op then
16708 Save_Entity_Descendants (N);
16709 end if;
16710 end Save_References_In_Operator;
16712 -------------------------------
16713 -- Save_References_In_Pragma --
16714 -------------------------------
16716 procedure Save_References_In_Pragma (Prag : Node_Id) is
16717 Context : Node_Id;
16718 Do_Save : Boolean := True;
16720 begin
16721 -- Do not save global references in pragmas generated from aspects
16722 -- because the pragmas will be regenerated at instantiation time.
16724 if From_Aspect_Specification (Prag) then
16725 Do_Save := False;
16727 -- The capture of global references within contract-related source
16728 -- pragmas associated with generic packages, subprograms or their
16729 -- respective bodies must be delayed due to timing of annotation
16730 -- analysis. Global references are still captured in routine
16731 -- Save_Global_References_In_Contract.
16733 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16734 if Is_Package_Contract_Annotation (Prag) then
16735 Context := Find_Related_Package_Or_Body (Prag);
16736 else
16737 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16738 Context := Find_Related_Declaration_Or_Body (Prag);
16739 end if;
16741 -- The use of Original_Node accounts for the case when the
16742 -- related context is generic template.
16744 if Requires_Delayed_Save (Original_Node (Context)) then
16745 Do_Save := False;
16746 end if;
16747 end if;
16749 -- For all other cases, save all global references within the
16750 -- descendants, but skip the following semantic fields:
16751 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16753 if Do_Save then
16754 Save_Global_Descendant
16755 (Union_Id (Pragma_Argument_Associations (N)));
16756 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
16757 end if;
16758 end Save_References_In_Pragma;
16760 -- Start of processing for Save_References
16762 begin
16763 if N = Empty then
16764 null;
16766 -- Aggregates
16768 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16769 Save_References_In_Aggregate (N);
16771 -- Character literals, operator symbols
16773 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16774 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16776 -- Defining identifiers
16778 elsif Nkind (N) in N_Entity then
16779 null;
16781 -- Identifiers
16783 elsif Nkind (N) = N_Identifier then
16784 Save_References_In_Identifier (N);
16786 -- Operators
16788 elsif Nkind (N) in N_Op then
16789 Save_References_In_Operator (N);
16791 -- Pragmas
16793 elsif Nkind (N) = N_Pragma then
16794 Save_References_In_Pragma (N);
16796 else
16797 Save_References_In_Descendants (N);
16798 end if;
16800 -- Save all global references found within the aspect specifications
16801 -- of the related node.
16803 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16805 -- The capture of global references within aspects associated with
16806 -- generic packages, subprograms or their bodies must be delayed
16807 -- due to timing of annotation analysis. Global references are
16808 -- still captured in routine Save_Global_References_In_Contract.
16810 if Requires_Delayed_Save (N) then
16811 null;
16813 -- Otherwise save all global references within the aspects
16815 else
16816 Save_Global_References_In_Aspects (N);
16817 end if;
16818 end if;
16819 end Save_References;
16821 ---------------------
16822 -- Set_Global_Type --
16823 ---------------------
16825 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
16826 Typ : constant Entity_Id := Etype (N2);
16828 begin
16829 Set_Etype (N, Typ);
16831 -- If the entity of N is not the associated node, this is a
16832 -- nested generic and it has an associated node as well, whose
16833 -- type is already the full view (see below). Indicate that the
16834 -- original node has a private view.
16836 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
16837 Set_Has_Private_View (N);
16838 end if;
16840 -- If not a private type, nothing else to do
16842 if not Is_Private_Type (Typ) then
16843 null;
16845 -- If it is a derivation of a private type in a context where no
16846 -- full view is needed, nothing to do either.
16848 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
16849 null;
16851 -- Otherwise mark the type for flipping and use the full view when
16852 -- available.
16854 else
16855 Set_Has_Private_View (N);
16857 if Present (Full_View (Typ)) then
16858 Set_Etype (N2, Full_View (Typ));
16859 end if;
16860 end if;
16862 if Is_Floating_Point_Type (Typ)
16863 and then Has_Dimension_System (Typ)
16864 then
16865 Copy_Dimensions (N2, N);
16866 end if;
16867 end Set_Global_Type;
16869 -- Start of processing for Save_Global_References
16871 begin
16872 Gen_Scope := Current_Scope;
16874 -- If the generic unit is a child unit, references to entities in the
16875 -- parent are treated as local, because they will be resolved anew in
16876 -- the context of the instance of the parent.
16878 while Is_Child_Unit (Gen_Scope)
16879 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16880 loop
16881 Gen_Scope := Scope (Gen_Scope);
16882 end loop;
16884 Save_References (Templ);
16885 end Save_Global_References;
16887 ---------------------------------------
16888 -- Save_Global_References_In_Aspects --
16889 ---------------------------------------
16891 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16892 Asp : Node_Id;
16893 Expr : Node_Id;
16895 begin
16896 Asp := First (Aspect_Specifications (N));
16897 while Present (Asp) loop
16898 Expr := Expression (Asp);
16900 if Present (Expr) then
16901 Save_Global_References (Expr);
16902 end if;
16904 Next (Asp);
16905 end loop;
16906 end Save_Global_References_In_Aspects;
16908 ------------------------------------------
16909 -- Set_Copied_Sloc_For_Inherited_Pragma --
16910 ------------------------------------------
16912 procedure Set_Copied_Sloc_For_Inherited_Pragma
16913 (N : Node_Id;
16914 E : Entity_Id)
16916 begin
16917 Create_Instantiation_Source (N, E,
16918 Inlined_Body => False,
16919 Inherited_Pragma => True,
16920 Factor => S_Adjustment);
16921 end Set_Copied_Sloc_For_Inherited_Pragma;
16923 --------------------------------------
16924 -- Set_Copied_Sloc_For_Inlined_Body --
16925 --------------------------------------
16927 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16928 begin
16929 Create_Instantiation_Source (N, E,
16930 Inlined_Body => True,
16931 Inherited_Pragma => False,
16932 Factor => S_Adjustment);
16933 end Set_Copied_Sloc_For_Inlined_Body;
16935 ---------------------
16936 -- Set_Instance_Of --
16937 ---------------------
16939 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16940 begin
16941 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16942 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16943 Generic_Renamings.Increment_Last;
16944 end Set_Instance_Of;
16946 --------------------
16947 -- Set_Next_Assoc --
16948 --------------------
16950 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16951 begin
16952 Generic_Renamings.Table (E).Next_In_HTable := Next;
16953 end Set_Next_Assoc;
16955 -------------------
16956 -- Start_Generic --
16957 -------------------
16959 procedure Start_Generic is
16960 begin
16961 -- ??? More things could be factored out in this routine.
16962 -- Should probably be done at a later stage.
16964 Generic_Flags.Append (Inside_A_Generic);
16965 Inside_A_Generic := True;
16967 Expander_Mode_Save_And_Set (False);
16968 end Start_Generic;
16970 ----------------------
16971 -- Set_Instance_Env --
16972 ----------------------
16974 -- WARNING: This routine manages SPARK regions
16976 procedure Set_Instance_Env
16977 (Gen_Unit : Entity_Id;
16978 Act_Unit : Entity_Id)
16980 Saved_AE : constant Boolean := Assertions_Enabled;
16981 Saved_CPL : constant Node_Id := Check_Policy_List;
16982 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16983 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16984 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16986 begin
16987 -- Regardless of the current mode, predefined units are analyzed in the
16988 -- most current Ada mode, and earlier version Ada checks do not apply
16989 -- to predefined units. Nothing needs to be done for non-internal units.
16990 -- These are always analyzed in the current mode.
16992 if In_Internal_Unit (Gen_Unit) then
16994 -- The following call resets all configuration attributes to default
16995 -- or the xxx_Config versions of the attributes when the current sem
16996 -- unit is the main unit. At the same time, internal units must also
16997 -- inherit certain configuration attributes from their context. It
16998 -- is unclear what these two sets are.
17000 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
17002 -- Reinstall relevant configuration attributes of the context
17004 Assertions_Enabled := Saved_AE;
17005 Check_Policy_List := Saved_CPL;
17006 Dynamic_Elaboration_Checks := Saved_DEC;
17008 Install_SPARK_Mode (Saved_SM, Saved_SMP);
17009 end if;
17011 Current_Instantiated_Parent :=
17012 (Gen_Id => Gen_Unit,
17013 Act_Id => Act_Unit,
17014 Next_In_HTable => Assoc_Null);
17015 end Set_Instance_Env;
17017 -----------------
17018 -- Switch_View --
17019 -----------------
17021 procedure Switch_View (T : Entity_Id) is
17022 BT : constant Entity_Id := Base_Type (T);
17023 Priv_Elmt : Elmt_Id := No_Elmt;
17024 Priv_Sub : Entity_Id;
17026 begin
17027 -- T may be private but its base type may have been exchanged through
17028 -- some other occurrence, in which case there is nothing to switch
17029 -- besides T itself. Note that a private dependent subtype of a private
17030 -- type might not have been switched even if the base type has been,
17031 -- because of the last branch of Check_Private_View (see comment there).
17033 if not Is_Private_Type (BT) then
17034 Prepend_Elmt (Full_View (T), Exchanged_Views);
17035 Exchange_Declarations (T);
17036 return;
17037 end if;
17039 Priv_Elmt := First_Elmt (Private_Dependents (BT));
17041 if Present (Full_View (BT)) then
17042 Prepend_Elmt (Full_View (BT), Exchanged_Views);
17043 Exchange_Declarations (BT);
17044 end if;
17046 while Present (Priv_Elmt) loop
17047 Priv_Sub := Node (Priv_Elmt);
17049 if Present (Full_View (Priv_Sub)) then
17050 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
17051 Exchange_Declarations (Priv_Sub);
17052 end if;
17054 Next_Elmt (Priv_Elmt);
17055 end loop;
17056 end Switch_View;
17058 -----------------
17059 -- True_Parent --
17060 -----------------
17062 function True_Parent (N : Node_Id) return Node_Id is
17063 begin
17064 if Nkind (Parent (N)) = N_Subunit then
17065 return Parent (Corresponding_Stub (Parent (N)));
17066 else
17067 return Parent (N);
17068 end if;
17069 end True_Parent;
17071 -----------------------------
17072 -- Valid_Default_Attribute --
17073 -----------------------------
17075 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
17076 Attr_Id : constant Attribute_Id :=
17077 Get_Attribute_Id (Attribute_Name (Def));
17078 T : constant Entity_Id := Entity (Prefix (Def));
17079 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
17080 F : Entity_Id;
17081 Num_F : Nat;
17082 OK : Boolean;
17084 begin
17085 if No (T) or else T = Any_Id then
17086 return;
17087 end if;
17089 Num_F := 0;
17090 F := First_Formal (Nam);
17091 while Present (F) loop
17092 Num_F := Num_F + 1;
17093 Next_Formal (F);
17094 end loop;
17096 case Attr_Id is
17097 when Attribute_Adjacent
17098 | Attribute_Ceiling
17099 | Attribute_Copy_Sign
17100 | Attribute_Floor
17101 | Attribute_Fraction
17102 | Attribute_Machine
17103 | Attribute_Model
17104 | Attribute_Remainder
17105 | Attribute_Rounding
17106 | Attribute_Unbiased_Rounding
17108 OK := Is_Fun
17109 and then Num_F = 1
17110 and then Is_Floating_Point_Type (T);
17112 when Attribute_Image
17113 | Attribute_Pred
17114 | Attribute_Succ
17115 | Attribute_Value
17116 | Attribute_Wide_Image
17117 | Attribute_Wide_Value
17119 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
17121 when Attribute_Max
17122 | Attribute_Min
17124 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
17126 when Attribute_Input =>
17127 OK := (Is_Fun and then Num_F = 1);
17129 when Attribute_Output
17130 | Attribute_Put_Image
17131 | Attribute_Read
17132 | Attribute_Write
17134 OK := not Is_Fun and then Num_F = 2;
17136 when others =>
17137 OK := False;
17138 end case;
17140 if not OK then
17141 Error_Msg_N
17142 ("attribute reference has wrong profile for subprogram", Def);
17143 end if;
17144 end Valid_Default_Attribute;
17146 ----------------------------------
17147 -- Validate_Formal_Type_Default --
17148 ----------------------------------
17150 procedure Validate_Formal_Type_Default (Decl : Node_Id) is
17151 Default : constant Node_Id :=
17152 Default_Subtype_Mark (Original_Node (Decl));
17153 Formal : constant Entity_Id := Defining_Identifier (Decl);
17155 Def_Sub : Entity_Id; -- Default subtype mark
17156 Type_Def : Node_Id;
17158 procedure Check_Discriminated_Formal;
17159 -- Check that discriminants of default for private or incomplete
17160 -- type match those of formal type.
17162 function Reference_Formal (N : Node_Id) return Traverse_Result;
17163 -- Check whether formal type definition mentions a previous formal
17164 -- type of the same generic.
17166 ----------------------
17167 -- Reference_Formal --
17168 ----------------------
17170 function Reference_Formal (N : Node_Id) return Traverse_Result is
17171 begin
17172 if Is_Entity_Name (N)
17173 and then Scope (Entity (N)) = Current_Scope
17174 then
17175 return Abandon;
17176 else
17177 return OK;
17178 end if;
17179 end Reference_Formal;
17181 function Depends_On_Other_Formals is
17182 new Traverse_Func (Reference_Formal);
17184 function Default_Subtype_Matches
17185 (Gen_T, Def_T : Entity_Id) return Boolean;
17187 procedure Validate_Array_Type_Default;
17188 -- Verify that dimension, indices, and component types of default
17189 -- are compatible with formal array type definition.
17191 procedure Validate_Derived_Type_Default;
17192 -- Verify that ancestor and progenitor types match.
17194 ---------------------------------
17195 -- Check_Discriminated_Formal --
17196 ---------------------------------
17198 procedure Check_Discriminated_Formal is
17199 Formal_Discr : Entity_Id;
17200 Actual_Discr : Entity_Id;
17201 Formal_Subt : Entity_Id;
17203 begin
17204 if Has_Discriminants (Formal) then
17205 if not Has_Discriminants (Def_Sub) then
17206 Error_Msg_NE
17207 ("default for & must have discriminants", Default, Formal);
17209 elsif Is_Constrained (Def_Sub) then
17210 Error_Msg_NE
17211 ("default for & must be unconstrained", Default, Formal);
17213 else
17214 Formal_Discr := First_Discriminant (Formal);
17215 Actual_Discr := First_Discriminant (Def_Sub);
17216 while Formal_Discr /= Empty loop
17217 if Actual_Discr = Empty then
17218 Error_Msg_N
17219 ("discriminants on Formal do not match formal",
17220 Default);
17221 end if;
17223 Formal_Subt := Etype (Formal_Discr);
17225 -- Access discriminants match if designated types do
17227 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
17228 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
17229 E_Anonymous_Access_Type
17230 and then
17231 Designated_Type (Base_Type (Formal_Subt)) =
17232 Designated_Type (Base_Type (Etype (Actual_Discr)))
17233 then
17234 null;
17236 elsif Base_Type (Formal_Subt) /=
17237 Base_Type (Etype (Actual_Discr))
17238 then
17239 Error_Msg_N
17240 ("types of discriminants of default must match formal",
17241 Default);
17243 elsif not Subtypes_Statically_Match
17244 (Formal_Subt, Etype (Actual_Discr))
17245 and then Ada_Version >= Ada_95
17246 then
17247 Error_Msg_N
17248 ("subtypes of discriminants of default "
17249 & "must match formal",
17250 Default);
17251 end if;
17253 Next_Discriminant (Formal_Discr);
17254 Next_Discriminant (Actual_Discr);
17255 end loop;
17257 if Actual_Discr /= Empty then
17258 Error_Msg_NE
17259 ("discriminants on default do not match formal",
17260 Default, Formal);
17261 end if;
17262 end if;
17263 end if;
17264 end Check_Discriminated_Formal;
17266 ---------------------------
17267 -- Default_Subtype_Matches --
17268 ---------------------------
17270 function Default_Subtype_Matches
17271 (Gen_T, Def_T : Entity_Id) return Boolean
17273 begin
17274 -- Check that the base types, root types (when dealing with class
17275 -- wide types), or designated types (when dealing with anonymous
17276 -- access types) of Gen_T and Def_T are statically matching subtypes.
17278 return (Base_Type (Gen_T) = Base_Type (Def_T)
17279 and then Subtypes_Statically_Match (Gen_T, Def_T))
17281 or else (Is_Class_Wide_Type (Gen_T)
17282 and then Is_Class_Wide_Type (Def_T)
17283 and then Default_Subtype_Matches
17284 (Root_Type (Gen_T), Root_Type (Def_T)))
17286 or else (Is_Anonymous_Access_Type (Gen_T)
17287 and then Ekind (Def_T) = Ekind (Gen_T)
17288 and then Subtypes_Statically_Match
17289 (Designated_Type (Gen_T), Designated_Type (Def_T)));
17291 end Default_Subtype_Matches;
17293 ----------------------------------
17294 -- Validate_Array_Type_Default --
17295 ----------------------------------
17297 procedure Validate_Array_Type_Default is
17298 I1, I2 : Node_Id;
17299 T2 : Entity_Id;
17300 begin
17301 if not Is_Array_Type (Def_Sub) then
17302 Error_Msg_NE ("default for& must be an array type ",
17303 Default, Formal);
17304 return;
17306 elsif Number_Dimensions (Def_Sub) /= Number_Dimensions (Formal)
17307 or else Is_Constrained (Def_Sub) /=
17308 Is_Constrained (Formal)
17309 then
17310 Error_Msg_NE ("default array type does not match&",
17311 Default, Formal);
17312 return;
17313 end if;
17315 I1 := First_Index (Formal);
17316 I2 := First_Index (Def_Sub);
17317 for J in 1 .. Number_Dimensions (Formal) loop
17319 -- If the indexes of the actual were given by a subtype_mark,
17320 -- the index was transformed into a range attribute. Retrieve
17321 -- the original type mark for checking.
17323 if Is_Entity_Name (Original_Node (I2)) then
17324 T2 := Entity (Original_Node (I2));
17325 else
17326 T2 := Etype (I2);
17327 end if;
17329 if not Subtypes_Statically_Match (Etype (I1), T2) then
17330 Error_Msg_NE
17331 ("index types of default do not match those of formal &",
17332 Default, Formal);
17333 end if;
17335 Next_Index (I1);
17336 Next_Index (I2);
17337 end loop;
17339 if not Default_Subtype_Matches
17340 (Component_Type (Formal), Component_Type (Def_Sub))
17341 then
17342 Error_Msg_NE
17343 ("component subtype of default does not match that of formal &",
17344 Default, Formal);
17345 end if;
17347 if Has_Aliased_Components (Formal)
17348 and then not Has_Aliased_Components (Default)
17349 then
17350 Error_Msg_NE
17351 ("default must have aliased components to match formal type &",
17352 Default, Formal);
17353 end if;
17354 end Validate_Array_Type_Default;
17356 -----------------------------------
17357 -- Validate_Derived_Type_Default --
17358 -----------------------------------
17360 procedure Validate_Derived_Type_Default is
17361 begin
17362 if not Is_Ancestor (Etype (Formal), Def_Sub) then
17363 Error_Msg_NE ("default must be a descendent of&",
17364 Default, Etype (Formal));
17365 end if;
17367 if Has_Interfaces (Formal) then
17368 if not Has_Interfaces (Def_Sub) then
17369 Error_Msg_NE
17370 ("default must implement all interfaces of formal&",
17371 Default, Formal);
17373 else
17374 declare
17375 Iface : Node_Id;
17376 Iface_Ent : Entity_Id;
17378 begin
17379 Iface := First (Abstract_Interface_List (Formal));
17381 while Present (Iface) loop
17382 Iface_Ent := Entity (Iface);
17384 if Is_Ancestor (Iface_Ent, Def_Sub)
17385 or else Is_Progenitor (Iface_Ent, Def_Sub)
17386 then
17387 null;
17389 else
17390 Error_Msg_NE
17391 ("Default must implement interface&",
17392 Default, Etype (Iface));
17393 end if;
17395 Next (Iface);
17396 end loop;
17397 end;
17398 end if;
17399 end if;
17400 end Validate_Derived_Type_Default;
17402 -- Start of processing for Validate_Formal_Type_Default
17404 begin
17405 Analyze (Default);
17406 if not Is_Entity_Name (Default)
17407 or else not Is_Type (Entity (Default))
17408 then
17409 Error_Msg_N
17410 ("Expect type name for default of formal type", Default);
17411 return;
17412 else
17413 Def_Sub := Entity (Default);
17414 end if;
17416 -- Formal derived_type declarations are transformed into full
17417 -- type declarations or Private_Type_Extensions for ease of processing.
17419 if Nkind (Decl) = N_Full_Type_Declaration then
17420 Type_Def := Type_Definition (Decl);
17422 elsif Nkind (Decl) = N_Private_Extension_Declaration then
17423 Type_Def := Subtype_Indication (Decl);
17425 else
17426 Type_Def := Formal_Type_Definition (Decl);
17427 end if;
17429 if Depends_On_Other_Formals (Type_Def) = Abandon
17430 and then Scope (Def_Sub) /= Current_Scope
17431 then
17432 Error_Msg_N ("default of formal type that depends on "
17433 & "other formals must be a previous formal type", Default);
17434 return;
17436 elsif Def_Sub = Formal then
17437 Error_Msg_N
17438 ("default for formal type cannot be formal itsef", Default);
17439 return;
17440 end if;
17442 case Nkind (Type_Def) is
17444 when N_Formal_Private_Type_Definition =>
17445 if (Is_Abstract_Type (Formal)
17446 and then not Is_Abstract_Type (Def_Sub))
17447 or else (Is_Limited_Type (Formal)
17448 and then not Is_Limited_Type (Def_Sub))
17449 then
17450 Error_Msg_NE
17451 ("default for private type$ does not match",
17452 Default, Formal);
17453 end if;
17455 Check_Discriminated_Formal;
17457 when N_Formal_Derived_Type_Definition =>
17458 Check_Discriminated_Formal;
17459 Validate_Derived_Type_Default;
17461 when N_Formal_Incomplete_Type_Definition =>
17462 if Is_Tagged_Type (Formal)
17463 and then not Is_Tagged_Type (Def_Sub)
17464 then
17465 Error_Msg_NE
17466 ("default for & must be a tagged type", Default, Formal);
17467 end if;
17469 Check_Discriminated_Formal;
17471 when N_Formal_Discrete_Type_Definition =>
17472 if not Is_Discrete_Type (Def_Sub) then
17473 Error_Msg_NE ("default for& must be a discrete type",
17474 Default, Formal);
17475 end if;
17477 when N_Formal_Signed_Integer_Type_Definition =>
17478 if not Is_Integer_Type (Def_Sub) then
17479 Error_Msg_NE ("default for& must be a discrete type",
17480 Default, Formal);
17481 end if;
17483 when N_Formal_Modular_Type_Definition =>
17484 if not Is_Modular_Integer_Type (Def_Sub) then
17485 Error_Msg_NE ("default for& must be a modular_integer Type",
17486 Default, Formal);
17487 end if;
17489 when N_Formal_Floating_Point_Definition =>
17490 if not Is_Floating_Point_Type (Def_Sub) then
17491 Error_Msg_NE ("default for& must be a floating_point type",
17492 Default, Formal);
17493 end if;
17495 when N_Formal_Ordinary_Fixed_Point_Definition =>
17496 if not Is_Ordinary_Fixed_Point_Type (Def_Sub) then
17497 Error_Msg_NE ("default for& must be "
17498 & "an ordinary_fixed_point type ",
17499 Default, Formal);
17500 end if;
17502 when N_Formal_Decimal_Fixed_Point_Definition =>
17503 if not Is_Decimal_Fixed_Point_Type (Def_Sub) then
17504 Error_Msg_NE ("default for& must be "
17505 & "an Decimal_fixed_point type ",
17506 Default, Formal);
17507 end if;
17509 when N_Array_Type_Definition =>
17510 Validate_Array_Type_Default;
17512 when N_Access_Function_Definition |
17513 N_Access_Procedure_Definition =>
17514 if Ekind (Def_Sub) /= E_Access_Subprogram_Type then
17515 Error_Msg_NE ("default for& must be an Access_To_Subprogram",
17516 Default, Formal);
17517 end if;
17518 Check_Subtype_Conformant
17519 (Designated_Type (Formal), Designated_Type (Def_Sub));
17521 when N_Access_To_Object_Definition =>
17522 if not Is_Access_Object_Type (Def_Sub) then
17523 Error_Msg_NE ("default for& must be an Access_To_Object",
17524 Default, Formal);
17526 elsif not Default_Subtype_Matches
17527 (Designated_Type (Formal), Designated_Type (Def_Sub))
17528 then
17529 Error_Msg_NE ("designated type of defaul does not match "
17530 & "designated type of formal type",
17531 Default, Formal);
17532 end if;
17534 when N_Record_Definition => -- Formal interface type
17535 if not Is_Interface (Def_Sub) then
17536 Error_Msg_NE
17537 ("default for formal interface type must be an interface",
17538 Default, Formal);
17540 elsif Is_Limited_Type (Def_Sub) /= Is_Limited_Type (Formal)
17541 or else Is_Task_Interface (Formal) /= Is_Task_Interface (Def_Sub)
17542 or else Is_Protected_Interface (Formal) /=
17543 Is_Protected_Interface (Def_Sub)
17544 or else Is_Synchronized_Interface (Formal) /=
17545 Is_Synchronized_Interface (Def_Sub)
17546 then
17547 Error_Msg_NE
17548 ("default for interface& does not match", Def_Sub, Formal);
17549 end if;
17551 when N_Derived_Type_Definition =>
17552 Validate_Derived_Type_Default;
17554 when N_Identifier => -- case of a private extension
17555 Validate_Derived_Type_Default;
17557 when N_Error =>
17558 null;
17560 when others =>
17561 raise Program_Error;
17562 end case;
17563 end Validate_Formal_Type_Default;
17564 end Sem_Ch12;