]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_ch12.adb
c++: Add most missing C++20 and C++23 names to cxxapi-data.csv
[gcc.git] / gcc / ada / sem_ch12.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2024, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Expander; use Expander;
36 with Fname; use Fname;
37 with Fname.UF; use Fname.UF;
38 with Freeze; use Freeze;
39 with Ghost; use Ghost;
40 with Itypes; use Itypes;
41 with Lib; use Lib;
42 with Lib.Load; use Lib.Load;
43 with Lib.Xref; use Lib.Xref;
44 with Mutably_Tagged; use Mutably_Tagged;
45 with Nlists; use Nlists;
46 with Namet; use Namet;
47 with Nmake; use Nmake;
48 with Opt; use Opt;
49 with Rident; use Rident;
50 with Restrict; use Restrict;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch7; use Sem_Ch7;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch10; use Sem_Ch10;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Elab; use Sem_Elab;
64 with Sem_Elim; use Sem_Elim;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Prag; use Sem_Prag;
67 with Sem_Res; use Sem_Res;
68 with Sem_Type; use Sem_Type;
69 with Sem_Util; use Sem_Util;
70 with Sem_Warn; use Sem_Warn;
71 with Stand; use Stand;
72 with Sinfo; use Sinfo;
73 with Sinfo.Nodes; use Sinfo.Nodes;
74 with Sinfo.Utils; use Sinfo.Utils;
75 with Sinfo.CN; use Sinfo.CN;
76 with Sinput; use Sinput;
77 with Sinput.L; use Sinput.L;
78 with Snames; use Snames;
79 with Stringt; use Stringt;
80 with Uname; use Uname;
81 with Table;
82 with Tbuild; use Tbuild;
83 with Uintp; use Uintp;
84 with Urealp; use Urealp;
85 with Warnsw; use Warnsw;
86
87 with GNAT.HTable;
88
89 package body Sem_Ch12 is
90
91 ----------------------------------------------------------
92 -- Implementation of Generic Analysis and Instantiation --
93 ----------------------------------------------------------
94
95 -- GNAT implements generics by macro expansion. No attempt is made to share
96 -- generic instantiations (for now). Analysis of a generic definition does
97 -- not perform any expansion action, but the expander must be called on the
98 -- tree for each instantiation, because the expansion may of course depend
99 -- on the generic actuals. All of this is best achieved as follows:
100 --
101 -- a) Semantic analysis of a generic unit is performed on a copy of the
102 -- tree for the generic unit. All tree modifications that follow analysis
103 -- do not affect the original tree. Links are kept between the original
104 -- tree and the copy, in order to recognize non-local references within
105 -- the generic, and propagate them to each instance (recall that name
106 -- resolution is done on the generic declaration: generics are not really
107 -- macros). This is summarized in the following diagram:
108
109 -- .-----------. .----------.
110 -- | semantic |<--------------| generic |
111 -- | copy | | unit |
112 -- | |==============>| |
113 -- |___________| global |__________|
114 -- references | | |
115 -- | | |
116 -- .-----|--|.
117 -- | .-----|---.
118 -- | | .----------.
119 -- | | | generic |
120 -- |__| | |
121 -- |__| instance |
122 -- |__________|
123
124 -- b) Each instantiation copies the original tree, and inserts into it a
125 -- series of declarations that describe the mapping between generic formals
126 -- and actuals. For example, a generic In OUT parameter is an object
127 -- renaming of the corresponding actual, etc. Generic IN parameters are
128 -- constant declarations.
129
130 -- c) In order to give the right visibility for these renamings, we use
131 -- a different scheme for package and subprogram instantiations. For
132 -- packages, the list of renamings is inserted into the package
133 -- specification, before the visible declarations of the package. The
134 -- renamings are analyzed before any of the text of the instance, and are
135 -- thus visible at the right place. Furthermore, outside of the instance,
136 -- the generic parameters are visible and denote their corresponding
137 -- actuals.
138
139 -- For subprograms, we create a container package to hold the renamings
140 -- and the subprogram instance itself. Analysis of the package makes the
141 -- renaming declarations visible to the subprogram. After analyzing the
142 -- package, the defining entity for the subprogram is touched-up so that
143 -- it appears declared in the current scope, and not inside the container
144 -- package.
145
146 -- If the instantiation is a compilation unit, the container package is
147 -- given the same name as the subprogram instance. This ensures that
148 -- the elaboration procedure called by the binder, using the compilation
149 -- unit name, calls in fact the elaboration procedure for the package.
150
151 -- Not surprisingly, private types complicate this approach. By saving in
152 -- the original generic object the non-local references, we guarantee that
153 -- the proper entities are referenced at the point of instantiation.
154 -- However, for private types, this by itself does not insure that the
155 -- proper VIEW of the entity is used (the full type may be visible at the
156 -- point of generic definition, but not at instantiation, or vice-versa).
157 -- In order to reference the proper view, we special-case any reference
158 -- to private types in the generic object, by saving both views, one in
159 -- the generic and one in the semantic copy. At time of instantiation, we
160 -- check whether the two views are consistent, and exchange declarations if
161 -- necessary, in order to restore the correct visibility. Similarly, if
162 -- the instance view is private when the generic view was not, we perform
163 -- the exchange. After completing the instantiation, we restore the
164 -- current visibility. The flag Has_Private_View marks identifiers in the
165 -- the generic unit that require checking.
166
167 -- Visibility within nested generic units requires special handling.
168 -- Consider the following scheme:
169
170 -- type Global is ... -- outside of generic unit.
171 -- generic ...
172 -- package Outer is
173 -- ...
174 -- type Semi_Global is ... -- global to inner.
175
176 -- generic ... -- 1
177 -- procedure inner (X1 : Global; X2 : Semi_Global);
178
179 -- procedure in2 is new inner (...); -- 4
180 -- end Outer;
181
182 -- package New_Outer is new Outer (...); -- 2
183 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
184
185 -- The semantic analysis of Outer captures all occurrences of Global.
186 -- The semantic analysis of Inner (at 1) captures both occurrences of
187 -- Global and Semi_Global.
188
189 -- At point 2 (instantiation of Outer), we also produce a generic copy
190 -- of Inner, even though Inner is, at that point, not being instantiated.
191 -- (This is just part of the semantic analysis of New_Outer).
192
193 -- Critically, references to Global within Inner must be preserved, while
194 -- references to Semi_Global should not be preserved, because they must now
195 -- resolve to an entity within New_Outer. To distinguish between these, we
196 -- use a global variable, Current_Instantiated_Parent, which is set when
197 -- performing a generic copy during instantiation (at 2). This variable is
198 -- used when performing a generic copy that is not an instantiation, but
199 -- that is nested within one, as the occurrence of 1 within 2. The analysis
200 -- of a nested generic only preserves references that are global to the
201 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
202 -- determine whether a reference is external to the given parent.
203
204 -- The instantiation at point 3 requires no special treatment. The method
205 -- works as well for further nestings of generic units, but of course the
206 -- variable Current_Instantiated_Parent must be stacked because nested
207 -- instantiations can occur, e.g. the occurrence of 4 within 2.
208
209 -- The instantiation of package and subprogram bodies is handled in a
210 -- similar manner, except that it is delayed until after semantic
211 -- analysis is complete. In this fashion complex cross-dependencies
212 -- between several package declarations and bodies containing generics
213 -- can be compiled which otherwise would diagnose spurious circularities.
214
215 -- For example, it is possible to compile two packages A and B that
216 -- have the following structure:
217
218 -- package A is package B is
219 -- generic ... generic ...
220 -- package G_A is package G_B is
221
222 -- with B; with A;
223 -- package body A is package body B is
224 -- package N_B is new G_B (..) package N_A is new G_A (..)
225
226 -- The table Pending_Instantiations in package Inline is used to keep
227 -- track of body instantiations that are delayed in this manner. Inline
228 -- handles the actual calls to do the body instantiations. This activity
229 -- is part of Inline, since the processing occurs at the same point, and
230 -- for essentially the same reason, as the handling of inlined routines.
231
232 ----------------------------------------------
233 -- Detection of Instantiation Circularities --
234 ----------------------------------------------
235
236 -- If we have a chain of instantiations that is circular, this is static
237 -- error which must be detected at compile time. The detection of these
238 -- circularities is carried out at the point that we insert a generic
239 -- instance spec or body. If there is a circularity, then the analysis of
240 -- the offending spec or body will eventually result in trying to load the
241 -- same unit again, and we detect this problem as we analyze the package
242 -- instantiation for the second time.
243
244 -- At least in some cases after we have detected the circularity, we get
245 -- into trouble if we try to keep going. The following flag is set if a
246 -- circularity is detected, and used to abandon compilation after the
247 -- messages have been posted.
248
249 Circularity_Detected : Boolean := False;
250 -- It should really be reset upon encountering a new main unit, but in
251 -- practice we do not use multiple main units so this is not critical.
252
253 -----------------------------------------
254 -- Implementation of Generic Contracts --
255 -----------------------------------------
256
257 -- A "contract" is a collection of aspects and pragmas that either verify a
258 -- property of a construct at runtime or classify the data flow to and from
259 -- the construct in some fashion.
260
261 -- Generic packages, subprograms and their respective bodies may be subject
262 -- to the following contract-related aspects or pragmas collectively known
263 -- as annotations:
264
265 -- package subprogram [body]
266 -- Abstract_State Always_Terminates
267 -- Initial_Condition Contract_Cases
268 -- Initializes Depends
269 -- Exceptional_Cases
270 -- Extensions_Visible
271 -- Global
272 -- package body Post
273 -- Refined_State Post_Class
274 -- Postcondition
275 -- Pre
276 -- Pre_Class
277 -- Precondition
278 -- Refined_Depends
279 -- Refined_Global
280 -- Refined_Post
281 -- Subprogram_Variant
282 -- Test_Case
283
284 -- Most package contract annotations utilize forward references to classify
285 -- data declared within the package [body]. Subprogram annotations then use
286 -- the classifications to further refine them. These inter dependencies are
287 -- problematic with respect to the implementation of generics because their
288 -- analysis, capture of global references and instantiation does not mesh
289 -- well with the existing mechanism.
290
291 -- 1) Analysis of generic contracts is carried out the same way non-generic
292 -- contracts are analyzed:
293
294 -- 1.1) General rule - a contract is analyzed after all related aspects
295 -- and pragmas are analyzed. This is done by routines
296
297 -- Analyze_Package_Body_Contract
298 -- Analyze_Package_Contract
299 -- Analyze_Subprogram_Body_Contract
300 -- Analyze_Subprogram_Contract
301
302 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
303 -- are processed.
304
305 -- 1.3) Compilation unit body - the contract is analyzed at the end of
306 -- the body declaration list.
307
308 -- 1.4) Package - the contract is analyzed at the end of the private or
309 -- visible declarations, prior to analyzing the contracts of any nested
310 -- packages or subprograms.
311
312 -- 1.5) Package body - the contract is analyzed at the end of the body
313 -- declaration list, prior to analyzing the contracts of any nested
314 -- packages or subprograms.
315
316 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
317 -- package or a subprogram, then its contract is analyzed at the end of
318 -- the enclosing declarations, otherwise the subprogram is a compilation
319 -- unit 1.2).
320
321 -- 1.7) Subprogram body - if the subprogram body is declared inside a
322 -- block, a package body or a subprogram body, then its contract is
323 -- analyzed at the end of the enclosing declarations, otherwise the
324 -- subprogram is a compilation unit 1.3).
325
326 -- 2) Capture of global references within contracts is done after capturing
327 -- global references within the generic template. There are two reasons for
328 -- this delay - pragma annotations are not part of the generic template in
329 -- the case of a generic subprogram declaration, and analysis of contracts
330 -- is delayed.
331
332 -- Contract-related source pragmas within generic templates are prepared
333 -- for delayed capture of global references by routine
334
335 -- Create_Generic_Contract
336
337 -- The routine associates these pragmas with the contract of the template.
338 -- In the case of a generic subprogram declaration, the routine creates
339 -- generic templates for the pragmas declared after the subprogram because
340 -- they are not part of the template.
341
342 -- generic -- template starts
343 -- procedure Gen_Proc (Input : Integer); -- template ends
344 -- pragma Precondition (Input > 0); -- requires own template
345
346 -- 2.1) The capture of global references with aspect specifications and
347 -- source pragmas that apply to a generic unit must be suppressed when
348 -- the generic template is being processed because the contracts have not
349 -- been analyzed yet. Any attempts to capture global references at that
350 -- point will destroy the Associated_Node linkages and leave the template
351 -- undecorated. This delay is controlled by routine
352
353 -- Requires_Delayed_Save
354
355 -- 2.2) The real capture of global references within a contract is done
356 -- after the contract has been analyzed, by routine
357
358 -- Save_Global_References_In_Contract
359
360 -- 3) The instantiation of a generic contract occurs as part of the
361 -- instantiation of the contract owner. Generic subprogram declarations
362 -- require additional processing when the contract is specified by pragmas
363 -- because the pragmas are not part of the generic template. This is done
364 -- by routine
365
366 -- Instantiate_Subprogram_Contract
367
368 --------------------------------------------------
369 -- Formal packages and partial parameterization --
370 --------------------------------------------------
371
372 -- When compiling a generic, a formal package is a local instantiation. If
373 -- declared with a box, its generic formals are visible in the enclosing
374 -- generic. If declared with a partial list of actuals, those actuals that
375 -- are defaulted (covered by an Others clause, or given an explicit box
376 -- initialization) are also visible in the enclosing generic, while those
377 -- that have a corresponding actual are not.
378
379 -- In our source model of instantiation, the same visibility must be
380 -- present in the spec and body of an instance: the names of the formals
381 -- that are defaulted must be made visible within the instance, and made
382 -- invisible (hidden) after the instantiation is complete, so that they
383 -- are not accessible outside of the instance.
384
385 -- In a generic, a formal package is treated like a special instantiation.
386 -- Our Ada 95 compiler handled formals with and without box in different
387 -- ways. With partial parameterization, we use a single model for both.
388 -- We create a package declaration that consists of the specification of
389 -- the generic package, and a set of declarations that map the actuals
390 -- into local renamings, just as we do for bona fide instantiations. For
391 -- defaulted parameters and formals with a box, we copy directly the
392 -- declarations of the formals into this local package. The result is a
393 -- package whose visible declarations may include generic formals. This
394 -- package is only used for type checking and visibility analysis, and
395 -- never reaches the back end, so it can freely violate the placement
396 -- rules for generic formal declarations.
397
398 -- The list of declarations (renamings and copies of formals) is built
399 -- by Analyze_Associations, just as for regular instantiations.
400
401 -- At the point of instantiation, conformance checking must be applied only
402 -- to those parameters that were specified in the formals. We perform this
403 -- checking by creating another internal instantiation, this one including
404 -- only the renamings and the formals (the rest of the package spec is not
405 -- relevant to conformance checking). We can then traverse two lists: the
406 -- list of actuals in the instance that corresponds to the formal package,
407 -- and the list of actuals produced for this bogus instantiation. We apply
408 -- the conformance rules to those actuals that are not defaulted, i.e.
409 -- which still appear as generic formals.
410
411 -- When we compile an instance body we must make the right parameters
412 -- visible again. The predicate Is_Generic_Formal indicates which of the
413 -- formals should have its Is_Hidden flag reset.
414
415 -----------------------
416 -- Local subprograms --
417 -----------------------
418
419 procedure Abandon_Instantiation (N : Node_Id);
420 pragma No_Return (Abandon_Instantiation);
421 -- Posts an error message "instantiation abandoned" at the indicated node
422 -- and then raises the exception Instantiation_Error to do it.
423
424 procedure Analyze_Formal_Array_Type
425 (T : in out Entity_Id;
426 Def : Node_Id);
427 -- A formal array type is treated like an array type declaration, and
428 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
429 -- in-out, because in the case of an anonymous type the entity is
430 -- actually created in the procedure.
431
432 -- The following procedures treat other kinds of formal parameters
433
434 procedure Analyze_Formal_Derived_Interface_Type
435 (N : Node_Id;
436 T : Entity_Id;
437 Def : Node_Id);
438
439 procedure Analyze_Formal_Derived_Type
440 (N : Node_Id;
441 T : Entity_Id;
442 Def : Node_Id);
443
444 procedure Analyze_Formal_Interface_Type
445 (N : Node_Id;
446 T : Entity_Id;
447 Def : Node_Id);
448
449 -- The following subprograms create abbreviated declarations for formal
450 -- scalar types. We introduce an anonymous base of the proper class for
451 -- each of them, and define the formals as constrained first subtypes of
452 -- their bases. The bounds are expressions that are non-static in the
453 -- generic.
454
455 procedure Analyze_Formal_Decimal_Fixed_Point_Type
456 (T : Entity_Id; Def : Node_Id);
457 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
458 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
459 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
460 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
461 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
462 (T : Entity_Id; Def : Node_Id);
463
464 procedure Analyze_Formal_Private_Type
465 (N : Node_Id;
466 T : Entity_Id;
467 Def : Node_Id);
468 -- Creates a new private type, which does not require completion
469
470 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
471 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
472
473 procedure Analyze_Generic_Formal_Part (N : Node_Id);
474 -- Analyze generic formal part
475
476 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
477 -- Create a new access type with the given designated type
478
479 function Analyze_Associations
480 (I_Node : Node_Id;
481 Formals : List_Id;
482 F_Copy : List_Id) return List_Id;
483 -- At instantiation time, build the list of associations between formals
484 -- and actuals. Each association becomes a renaming declaration for the
485 -- formal entity. F_Copy is the analyzed list of formals in the generic
486 -- copy. It is used to apply legality checks to the actuals. I_Node is the
487 -- instantiation node.
488
489 procedure Analyze_Subprogram_Instantiation
490 (N : Node_Id;
491 K : Entity_Kind);
492
493 procedure Build_Instance_Compilation_Unit_Nodes
494 (N : Node_Id;
495 Act_Body : Node_Id;
496 Act_Decl : Node_Id);
497 -- This procedure is used in the case where the generic instance of a
498 -- subprogram body or package body is a library unit. In this case, the
499 -- original library unit node for the generic instantiation must be
500 -- replaced by the resulting generic body, and a link made to a new
501 -- compilation unit node for the generic declaration. The argument N is
502 -- the original generic instantiation. Act_Body and Act_Decl are the body
503 -- and declaration of the instance (either package body and declaration
504 -- nodes or subprogram body and declaration nodes depending on the case).
505 -- On return, the node N has been rewritten with the actual body.
506
507 function Build_Subprogram_Decl_Wrapper
508 (Formal_Subp : Entity_Id) return Node_Id;
509 -- Ada 2022 allows formal subprograms to carry pre/postconditions.
510 -- At the point of instantiation these contracts apply to uses of
511 -- the actual subprogram. This is implemented by creating wrapper
512 -- subprograms instead of the renamings previously used to link
513 -- formal subprograms and the corresponding actuals. If the actual
514 -- is not an entity (e.g. an attribute reference) a renaming is
515 -- created to handle the expansion of the attribute.
516
517 function Build_Subprogram_Body_Wrapper
518 (Formal_Subp : Entity_Id;
519 Actual_Name : Node_Id) return Node_Id;
520 -- The body of the wrapper is a call to the actual, with the generated
521 -- pre/postconditon checks added.
522
523 procedure Build_Subprogram_Wrappers
524 (Match, Analyzed_Formal : Node_Id; Renamings : List_Id);
525 -- Ada 2022: AI12-0272 introduces pre/postconditions for formal
526 -- subprograms. The implementation of making the formal into a renaming
527 -- of the actual does not work, given that subprogram renaming cannot
528 -- carry aspect specifications. Instead we must create subprogram
529 -- wrappers whose body is a call to the actual, and whose declaration
530 -- carries the aspects of the formal.
531 -- The wrapper declaration and body are appended to Renamings.
532 -- ???But renaming declarations CAN have aspects specs,
533 -- and that was true from the start (see AI05-0183-1).
534
535 procedure Check_Abbreviated_Instance
536 (N : Node_Id;
537 Parent_Installed : in out Boolean);
538 -- If the name of the generic unit in an abbreviated instantiation is an
539 -- expanded name, then the prefix may be an instance and the selector may
540 -- designate a child unit. If the parent is installed as a result of this
541 -- call, then Parent_Installed is set True, otherwise Parent_Installed is
542 -- unchanged by the call.
543
544 -- This routine needs to be called for declaration nodes of formal objects,
545 -- types and subprograms to check whether they are the copy, present in the
546 -- visible part of the abbreviated instantiation of formal packages, of the
547 -- declaration node of their corresponding formal parameter in the template
548 -- of the formal package, as specified by RM 12.7(10/2), so as to establish
549 -- the proper context for their analysis.
550
551 procedure Check_Access_Definition (N : Node_Id);
552 -- Subsidiary routine to null exclusion processing. Perform an assertion
553 -- check on Ada version and the presence of an access definition in N.
554
555 procedure Check_Formal_Packages (P_Id : Entity_Id);
556 -- Apply the following to all formal packages in generic associations.
557 -- Restore the visibility of the formals of the instance that are not
558 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
559 -- created for formal instances that are not defaulted.
560
561 procedure Check_Formal_Package_Instance
562 (Formal_Pack : Entity_Id;
563 Actual_Pack : Entity_Id);
564 -- Verify that the actuals of the actual instance match the actuals of
565 -- the template for a formal package that is not declared with a box.
566
567 procedure Check_Forward_Instantiation (Decl : Node_Id);
568 -- If the generic is a local entity and the corresponding body has not
569 -- been seen yet, flag enclosing packages to indicate that it will be
570 -- elaborated after the generic body. Subprograms declared in the same
571 -- package cannot be inlined by the front end because front-end inlining
572 -- requires a strict linear order of elaboration.
573
574 function Check_Hidden_Primitives (Renamings : List_Id) return Elist_Id;
575 -- Check if some association between formals and actuals requires to make
576 -- visible primitives of a tagged type, and make those primitives visible.
577 -- Return the list of primitives whose visibility is modified (to restore
578 -- their visibility later through Restore_Hidden_Primitives). If no
579 -- candidate is found then return No_Elist.
580
581 procedure Check_Hidden_Child_Unit
582 (N : Node_Id;
583 Gen_Unit : Entity_Id;
584 Act_Decl_Id : Entity_Id);
585 -- If the generic unit is an implicit child instance within a parent
586 -- instance, we need to make an explicit test that it is not hidden by
587 -- a child instance of the same name and parent.
588
589 procedure Check_Generic_Actuals
590 (Instance : Entity_Id;
591 Is_Formal_Box : Boolean);
592 -- Similar to previous one. Check the actuals in the instantiation,
593 -- whose views can change between the point of instantiation and the point
594 -- of instantiation of the body. In addition, mark the generic renamings
595 -- as generic actuals, so that they are not compatible with other actuals.
596 -- Recurse on an actual that is a formal package whose declaration has
597 -- a box.
598
599 function Component_Type_For_Private_View (T : Entity_Id) return Entity_Id;
600 -- Return the component type of array type T, with the following addition:
601 -- if this component type itself is an array type which has not been first
602 -- declared as private, then recurse on it. This makes it possible to deal
603 -- with arrays of arrays the same way as multi-dimensional arrays in the
604 -- mechanism handling private views.
605
606 function Contains_Instance_Of
607 (Inner : Entity_Id;
608 Outer : Entity_Id;
609 N : Node_Id) return Boolean;
610 -- Inner is instantiated within the generic Outer. Check whether Inner
611 -- directly or indirectly contains an instance of Outer or of one of its
612 -- parents, in the case of a subunit. Each generic unit holds a list of
613 -- the entities instantiated within (at any depth). This procedure
614 -- determines whether the set of such lists contains a cycle, i.e. an
615 -- illegal circular instantiation.
616
617 function Denotes_Formal_Package
618 (Pack : Entity_Id;
619 On_Exit : Boolean := False;
620 Instance : Entity_Id := Empty) return Boolean;
621 -- Returns True if E is a formal package of an enclosing generic, or
622 -- the actual for such a formal in an enclosing instantiation. If such
623 -- a package is used as a formal in an nested generic, or as an actual
624 -- in a nested instantiation, the visibility of ITS formals should not
625 -- be modified. When called from within Restore_Private_Views, the flag
626 -- On_Exit is true, to indicate that the search for a possible enclosing
627 -- instance should ignore the current one. In that case Instance denotes
628 -- the declaration for which this is an actual. This declaration may be
629 -- an instantiation in the source, or the internal instantiation that
630 -- corresponds to the actual for a formal package.
631
632 function Earlier (N1, N2 : Node_Id) return Boolean;
633 -- Yields True if N1 and N2 appear in the same compilation unit,
634 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
635 -- traversal of the tree for the unit. Used to determine the placement
636 -- of freeze nodes for instance bodies that may depend on other instances.
637
638 function Find_Actual_Type
639 (Typ : Entity_Id;
640 Gen_Type : Entity_Id) return Entity_Id;
641 -- When validating the actual types of a child instance, check whether
642 -- the formal is a formal type of the parent unit, and retrieve the current
643 -- actual for it. Typ is the entity in the analyzed formal type declaration
644 -- (component or index type of an array type, or designated type of an
645 -- access formal) and Gen_Type is the enclosing analyzed formal array
646 -- or access type. The desired actual may be a formal of a parent, or may
647 -- be declared in a formal package of a parent. In both cases it is a
648 -- generic actual type because it appears within a visible instance.
649 -- Finally, it may be declared in a parent unit without being a formal
650 -- of that unit, in which case it must be retrieved by visibility.
651 -- Ambiguities may still arise if two homonyms are declared in two formal
652 -- packages, and the prefix of the formal type may be needed to resolve
653 -- the ambiguity in the instance ???
654
655 procedure Freeze_Package_Instance
656 (N : Node_Id;
657 Gen_Body : Node_Id;
658 Gen_Decl : Node_Id;
659 Act_Id : Entity_Id);
660 -- If the instantiation happens textually before the body of the generic,
661 -- the instantiation of the body must be analyzed after the generic body,
662 -- and not at the point of instantiation. Such early instantiations can
663 -- happen if the generic and the instance appear in a package declaration
664 -- because the generic body can only appear in the corresponding package
665 -- body. Early instantiations can also appear if generic, instance and
666 -- body are all in the declarative part of a subprogram or entry. Entities
667 -- of packages that are early instantiations are delayed, and their freeze
668 -- node appears after the generic body. This rather complex machinery is
669 -- needed when nested instantiations are present, because the source does
670 -- not carry any indication of where the corresponding instance bodies must
671 -- be installed and frozen.
672
673 procedure Freeze_Subprogram_Instance
674 (N : Node_Id;
675 Gen_Body : Node_Id;
676 Pack_Id : Entity_Id);
677 -- The generic body may appear textually after the instance, including
678 -- in the proper body of a stub, or within a different package instance.
679 -- Given that the instance can only be elaborated after the generic, we
680 -- place freeze nodes for the instance and/or for packages that may enclose
681 -- the instance and the generic, so that the back-end can establish the
682 -- proper order of elaboration.
683
684 function Get_Associated_Entity (Id : Entity_Id) return Entity_Id;
685 -- Similar to Get_Associated_Node below, but for entities
686
687 function Get_Associated_Node (N : Node_Id) return Node_Id;
688 -- In order to propagate semantic information back from the analyzed copy
689 -- to the original generic, we maintain links between selected nodes in the
690 -- generic and their corresponding copies. At the end of generic analysis,
691 -- the routine Save_Global_References traverses the generic tree, examines
692 -- the semantic information, and preserves the links to those nodes that
693 -- contain global information. At instantiation, the information from the
694 -- associated node is placed on the new copy, so that name resolution is
695 -- not repeated.
696 --
697 -- Three kinds of source nodes have associated nodes:
698 --
699 -- a) those that can reference (denote) entities, that is identifiers,
700 -- character literals, expanded_names, operator symbols, operators,
701 -- and attribute reference nodes. These nodes have an Entity field
702 -- and are the set of nodes that are in N_Has_Entity.
703 --
704 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
705 --
706 -- c) selected components (N_Selected_Component)
707 --
708 -- For the first class, the associated node preserves the entity if it is
709 -- global. If the generic contains nested instantiations, the associated
710 -- node itself has been recopied, and a chain of them must be followed.
711 --
712 -- For aggregates, the associated node allows retrieval of the type, which
713 -- may otherwise not appear in the generic. The view of this type may be
714 -- different between generic and instantiation, and the full view can be
715 -- installed before the instantiation is analyzed. For aggregates of type
716 -- extensions, the same view exchange may have to be performed for some of
717 -- the ancestor types, if their view is private at the point of
718 -- instantiation.
719 --
720 -- Nodes that are selected components in the parse tree may be rewritten
721 -- as expanded names after resolution, and must be treated as potential
722 -- entity holders, which is why they also have an Associated_Node.
723 --
724 -- Nodes that do not come from source, such as freeze nodes, do not appear
725 -- in the generic tree, and need not have an associated node.
726 --
727 -- The associated node is stored in the Associated_Node field. Note that
728 -- this field overlaps Entity, which is fine, because the whole point is
729 -- that we don't need or want the normal Entity field in this situation.
730
731 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
732 -- Traverse the Exchanged_Views list to see if a type was private
733 -- and has already been flipped during this phase of instantiation.
734
735 function Has_Contracts (Decl : Node_Id) return Boolean;
736 -- Determine whether a formal subprogram has a Pre- or Postcondition,
737 -- in which case a subprogram wrapper has to be built for the actual.
738
739 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
740 -- Determine whether the parameter types and the return type of Subp
741 -- are fully defined at the point of instantiation.
742
743 function Has_Null_Default (N : Node_Id) return Boolean is
744 (Nkind (N) in N_Formal_Subprogram_Declaration
745 and then Nkind (Specification (N)) = N_Procedure_Specification
746 and then Null_Present (Specification (N)));
747 -- True if N is the declaration of a formal procedure with "is null"
748 -- as the default.
749
750 procedure Hide_Current_Scope;
751 -- When instantiating a generic child unit, the parent context must be
752 -- present, but the instance and all entities that may be generated
753 -- must be inserted in the current scope. We leave the current scope
754 -- on the stack, but make its entities invisible to avoid visibility
755 -- problems. This is reversed at the end of the instantiation. This is
756 -- not done for the instantiation of the bodies, which only require the
757 -- instances of the generic parents to be in scope.
758
759 function In_Main_Context (E : Entity_Id) return Boolean;
760 -- Check whether an instantiation is in the context of the main unit.
761 -- Used to determine whether its body should be elaborated to allow
762 -- front-end inlining.
763
764 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
765 -- Add the context clause of the unit containing a generic unit to a
766 -- compilation unit that is, or contains, an instantiation.
767
768 procedure Init_Env;
769 -- Establish environment for subsequent instantiation. Separated from
770 -- Save_Env because data-structures for visibility handling must be
771 -- initialized before call to Check_Generic_Child_Unit.
772
773 procedure Inline_Instance_Body
774 (N : Node_Id;
775 Gen_Unit : Entity_Id;
776 Act_Decl : Node_Id);
777 -- If front-end inlining is requested, instantiate the package body,
778 -- and preserve the visibility of its compilation unit, to insure
779 -- that successive instantiations succeed.
780
781 procedure Insert_Freeze_Node_For_Instance
782 (N : Node_Id;
783 F_Node : Node_Id);
784 -- N denotes a package or a subprogram instantiation and F_Node is the
785 -- associated freeze node. Insert the freeze node before the first source
786 -- body which follows immediately after N. If no such body is found, the
787 -- freeze node is inserted at the end of the declarative region which
788 -- contains N, unless the instantiation is done in a package spec that is
789 -- not at library level, in which case it is inserted at the outer level.
790 -- This can also be invoked to insert the freeze node of a package that
791 -- encloses an instantiation, in which case N may denote an arbitrary node.
792
793 procedure Install_Formal_Packages (Par : Entity_Id);
794 -- Install the visible part of any formal of the parent that is a formal
795 -- package. Note that for the case of a formal package with a box, this
796 -- includes the formal part of the formal package (12.7(10/2)).
797
798 procedure Install_Hidden_Primitives
799 (Prims_List : in out Elist_Id;
800 Gen_T : Entity_Id;
801 Act_T : Entity_Id);
802 -- Remove suffix 'P' from hidden primitives of Act_T to match the
803 -- visibility of primitives of Gen_T. The list of primitives to which
804 -- the suffix is removed is added to Prims_List to restore them later.
805
806 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
807 -- When compiling an instance of a child unit the parent (which is
808 -- itself an instance) is an enclosing scope that must be made
809 -- immediately visible. This procedure is also used to install the non-
810 -- generic parent of a generic child unit when compiling its body, so
811 -- that full views of types in the parent are made visible.
812
813 -- The functions Instantiate_... perform various legality checks and build
814 -- the declarations for instantiated generic parameters. In all of these
815 -- Formal is the entity in the generic unit, Actual is the entity or
816 -- expression in the generic associations, and Analyzed_Formal is the
817 -- formal in the generic copy, which contains the semantic information to
818 -- be used to validate the actual.
819
820 function Instantiate_Object
821 (Formal : Node_Id;
822 Actual : Node_Id;
823 Analyzed_Formal : Node_Id) return List_Id;
824
825 function Instantiate_Type
826 (Formal : Node_Id;
827 Actual : Node_Id;
828 Analyzed_Formal : Node_Id;
829 Actual_Decls : List_Id) return List_Id;
830 -- Actual_Decls is the list of renamings being built; this is used for
831 -- formal derived types, to determine whether the parent type is another
832 -- formal derived type in the same generic unit.
833 -- Note that the call site appends the result of this function onto
834 -- the same list it is passing to Actual_Decls.
835
836 function Instantiate_Formal_Subprogram
837 (Formal : Node_Id;
838 Actual : Node_Id;
839 Analyzed_Formal : Node_Id) return Node_Id;
840
841 function Instantiate_Formal_Package
842 (Formal : Node_Id;
843 Actual : Node_Id;
844 Analyzed_Formal : Node_Id) return List_Id;
845 -- If the formal package is declared with a box, special visibility rules
846 -- apply to its formals: they are in the visible part of the package. This
847 -- is true in the declarative region of the formal package, that is to say
848 -- in the enclosing generic or instantiation. For an instantiation, the
849 -- parameters of the formal package are made visible in an explicit step.
850 -- Furthermore, if the actual has a visible USE clause, these formals must
851 -- be made potentially use-visible as well. On exit from the enclosing
852 -- instantiation, the reverse must be done.
853
854 -- For a formal package declared without a box, there are conformance rules
855 -- that apply to the actuals in the generic declaration and the actuals of
856 -- the actual package in the enclosing instantiation. The simplest way to
857 -- apply these rules is to repeat the instantiation of the formal package
858 -- in the context of the enclosing instance, and compare the generic
859 -- associations of this instantiation with those of the actual package.
860 -- This internal instantiation only needs to contain the renamings of the
861 -- formals: the visible and private declarations themselves need not be
862 -- created.
863
864 -- In Ada 2005, the formal package may be only partially parameterized.
865 -- In that case the visibility step must make visible those actuals whose
866 -- corresponding formals were given with a box. A final complication
867 -- involves inherited operations from formal derived types, which must
868 -- be visible if the type is.
869
870 function Is_In_Main_Unit (N : Node_Id) return Boolean;
871 -- Test if given node is in the main unit
872
873 procedure Load_Parent_Of_Generic
874 (N : Node_Id;
875 Spec : Node_Id;
876 Body_Optional : Boolean := False);
877 -- If the generic appears in a separate non-generic library unit, load the
878 -- corresponding body to retrieve the body of the generic. N is the node
879 -- for the generic instantiation, Spec is the generic package declaration.
880 --
881 -- Body_Optional is a flag that indicates that the body is being loaded to
882 -- ensure that temporaries are generated consistently when there are other
883 -- instances in the current declarative part that precede the one being
884 -- loaded. In that case a missing body is acceptable.
885
886 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
887 -- Within the generic part, entities in the formal package are
888 -- visible. To validate subsequent type declarations, indicate
889 -- the correspondence between the entities in the analyzed formal,
890 -- and the entities in the actual package. There are three packages
891 -- involved in the instantiation of a formal package: the parent
892 -- generic P1 which appears in the generic declaration, the fake
893 -- instantiation P2 which appears in the analyzed generic, and whose
894 -- visible entities may be used in subsequent formals, and the actual
895 -- P3 in the instance. To validate subsequent formals, me indicate
896 -- that the entities in P2 are mapped into those of P3. The mapping of
897 -- entities has to be done recursively for nested packages.
898
899 procedure Move_Freeze_Nodes
900 (Out_Of : Entity_Id;
901 After : Node_Id;
902 L : List_Id);
903 -- Freeze nodes can be generated in the analysis of a generic unit, but
904 -- will not be seen by the back-end. It is necessary to move those nodes
905 -- to the enclosing scope if they freeze an outer entity. We place them
906 -- at the end of the enclosing generic package, which is semantically
907 -- neutral.
908
909 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
910 -- Analyze actuals to perform name resolution. Full resolution is done
911 -- later, when the expected types are known, but names have to be captured
912 -- before installing parents of generics, that are not visible for the
913 -- actuals themselves.
914 --
915 -- If Inst is present, it is the entity of the package instance. This
916 -- entity is marked as having a limited_view actual when some actual is
917 -- a limited view. This is used to place the instance body properly.
918
919 procedure Provide_Completing_Bodies (N : Node_Id);
920 -- Generate completing bodies for all subprograms found within package or
921 -- subprogram declaration N.
922
923 procedure Remove_Parent (In_Body : Boolean := False);
924 -- Reverse effect after instantiation of child is complete
925
926 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
927 -- Determine whether Subp renames one of the subprograms defined in the
928 -- generated package Standard.
929
930 function Requires_Conformance_Checking (N : Node_Id) return Boolean;
931 -- Determine whether the formal package declaration N requires conformance
932 -- checking with actuals in instantiations.
933
934 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
935 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
936 -- set to No_Elist.
937
938 procedure Set_Instance_Env
939 (Gen_Unit : Entity_Id;
940 Act_Unit : Entity_Id);
941 -- Save current instance on saved environment, to be used to determine
942 -- the global status of entities in nested instances. Part of Save_Env.
943 -- called after verifying that the generic unit is legal for the instance,
944 -- The procedure also examines whether the generic unit is a predefined
945 -- unit, in order to set configuration switches accordingly. As a result
946 -- the procedure must be called after analyzing and freezing the actuals.
947
948 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
949 -- Associate analyzed generic parameter with corresponding instance. Used
950 -- for semantic checks at instantiation time.
951
952 function True_Parent (N : Node_Id) return Node_Id;
953 -- For a subunit, return parent of corresponding stub, else return
954 -- parent of node.
955
956 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
957 -- Verify that an attribute that appears as the default for a formal
958 -- subprogram is a function or procedure with the correct profile.
959
960 procedure Validate_Formal_Type_Default (Decl : Node_Id);
961 -- Ada_2022 AI12-205: if a default subtype_mark is present, verify
962 -- that it is the name of a type in the same class as the formal.
963 -- The treatment parallels what is done in Instantiate_Type but differs
964 -- in a few ways so that this machinery cannot be reused as is: on one
965 -- hand there are no visibility issues for a default, because it is
966 -- analyzed in the same context as the formal type definition; on the
967 -- other hand the check needs to take into acount the use of a previous
968 -- formal type in the current formal type definition (see details in
969 -- AI12-0205).
970
971 -------------------------------------------
972 -- Data Structures for Generic Renamings --
973 -------------------------------------------
974
975 -- The map Generic_Renamings associates generic entities with their
976 -- corresponding actuals. Currently used to validate type instances. It
977 -- will eventually be used for all generic parameters to eliminate the
978 -- need for overload resolution in the instance.
979
980 type Assoc_Ptr is new Int;
981
982 Assoc_Null : constant Assoc_Ptr := -1;
983
984 type Assoc is record
985 Gen_Id : Entity_Id;
986 Act_Id : Entity_Id;
987 Next_In_HTable : Assoc_Ptr;
988 end record;
989
990 package Generic_Renamings is new Table.Table
991 (Table_Component_Type => Assoc,
992 Table_Index_Type => Assoc_Ptr,
993 Table_Low_Bound => 0,
994 Table_Initial => 10,
995 Table_Increment => 100,
996 Table_Name => "Generic_Renamings");
997
998 -- Variable to hold enclosing instantiation. When the environment is
999 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
1000
1001 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
1002
1003 -- Hash table for associations
1004
1005 HTable_Size : constant := 37;
1006 type HTable_Range is range 0 .. HTable_Size - 1;
1007
1008 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
1009 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
1010 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
1011 function Hash (F : Entity_Id) return HTable_Range;
1012
1013 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
1014 Header_Num => HTable_Range,
1015 Element => Assoc,
1016 Elmt_Ptr => Assoc_Ptr,
1017 Null_Ptr => Assoc_Null,
1018 Set_Next => Set_Next_Assoc,
1019 Next => Next_Assoc,
1020 Key => Entity_Id,
1021 Get_Key => Get_Gen_Id,
1022 Hash => Hash,
1023 Equal => "=");
1024
1025 Exchanged_Views : Elist_Id;
1026 -- This list holds the private views that have been exchanged during
1027 -- instantiation to restore the visibility of the generic declaration.
1028 -- (see comments above). After instantiation, the current visibility is
1029 -- reestablished by means of a traversal of this list.
1030
1031 Hidden_Entities : Elist_Id;
1032 -- This list holds the entities of the current scope that are removed
1033 -- from immediate visibility when instantiating a child unit. Their
1034 -- visibility is restored in Remove_Parent.
1035
1036 -- Because instantiations can be recursive, the following must be saved
1037 -- on entry and restored on exit from an instantiation (spec or body).
1038 -- This is done by the two procedures Save_Env and Restore_Env. For
1039 -- package and subprogram instantiations (but not for the body instances)
1040 -- the action of Save_Env is done in two steps: Init_Env is called before
1041 -- Check_Generic_Child_Unit, because setting the parent instances requires
1042 -- that the visibility data structures be properly initialized. Once the
1043 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
1044
1045 Parent_Unit_Visible : Boolean := False;
1046 -- Parent_Unit_Visible is used when the generic is a child unit, and
1047 -- indicates whether the ultimate parent of the generic is visible in the
1048 -- instantiation environment. It is used to reset the visibility of the
1049 -- parent at the end of the instantiation (see Remove_Parent).
1050
1051 Instance_Parent_Unit : Entity_Id := Empty;
1052 -- This records the ultimate parent unit of an instance of a generic
1053 -- child unit and is used in conjunction with Parent_Unit_Visible to
1054 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
1055
1056 type Instance_Env is record
1057 Instantiated_Parent : Assoc;
1058 Exchanged_Views : Elist_Id;
1059 Hidden_Entities : Elist_Id;
1060 Current_Sem_Unit : Unit_Number_Type;
1061 Parent_Unit_Visible : Boolean := False;
1062 Instance_Parent_Unit : Entity_Id := Empty;
1063 Switches : Config_Switches_Type;
1064 end record;
1065
1066 package Instance_Envs is new Table.Table (
1067 Table_Component_Type => Instance_Env,
1068 Table_Index_Type => Int,
1069 Table_Low_Bound => 0,
1070 Table_Initial => 32,
1071 Table_Increment => 100,
1072 Table_Name => "Instance_Envs");
1073
1074 procedure Restore_Private_Views
1075 (Pack_Id : Entity_Id;
1076 Is_Package : Boolean := True);
1077 -- Restore the private views of external types, and unmark the generic
1078 -- renamings of actuals, so that they become compatible subtypes again.
1079 -- For subprograms, Pack_Id is the package constructed to hold the
1080 -- renamings.
1081
1082 procedure Switch_View (T : Entity_Id);
1083 -- Switch the partial and full views of a type and its private
1084 -- dependents (i.e. its subtypes and derived types).
1085
1086 ------------------------------------
1087 -- Structures for Error Reporting --
1088 ------------------------------------
1089
1090 Instantiation_Node : Node_Id;
1091 -- Used by subprograms that validate instantiation of formal parameters
1092 -- where there might be no actual on which to place the error message.
1093 -- Also used to locate the instantiation node for generic subunits.
1094
1095 Instantiation_Error : exception;
1096 -- When there is a semantic error in the generic parameter matching,
1097 -- there is no point in continuing the instantiation, because the
1098 -- number of cascaded errors is unpredictable. This exception aborts
1099 -- the instantiation process altogether.
1100
1101 S_Adjustment : Sloc_Adjustment;
1102 -- Offset created for each node in an instantiation, in order to keep
1103 -- track of the source position of the instantiation in each of its nodes.
1104 -- A subsequent semantic error or warning on a construct of the instance
1105 -- points to both places: the original generic node, and the point of
1106 -- instantiation. See Sinput and Sinput.L for additional details.
1107
1108 ------------------------------------------------------------
1109 -- Data structure for keeping track when inside a Generic --
1110 ------------------------------------------------------------
1111
1112 -- The following table is used to save values of the Inside_A_Generic
1113 -- flag (see spec of Sem) when they are saved by Start_Generic.
1114
1115 package Generic_Flags is new Table.Table (
1116 Table_Component_Type => Boolean,
1117 Table_Index_Type => Int,
1118 Table_Low_Bound => 0,
1119 Table_Initial => 32,
1120 Table_Increment => 200,
1121 Table_Name => "Generic_Flags");
1122
1123 ------------------
1124 -- Associations --
1125 ------------------
1126
1127 package Associations is
1128
1129 type Actual_Kind is
1130 (None,
1131 None_Use_Clause,
1132 -- Used when the "formal" is a use clause; there is no corresponding
1133 -- actual.
1134 Box_Subp_Default,
1135 -- Used for "is <>" as a subprogram default
1136 Box_Actual,
1137 -- Used for explicit "name => <>" and "others => <>" in formal
1138 -- packages.
1139 Name_Exp,
1140 -- Name or expression or ....
1141 -- Used for an explicit_generic_actual_parameter, and also for the
1142 -- default_expression of an in-mode formal, the default_subtype_mark
1143 -- of a formal type, and the default_name of a formal subprogram.
1144 Null_Default,
1145 -- Used for "is null" as a subprogram default.
1146 Exp_Func_Default,
1147 -- Used for "is (expression)" as a subprogram default,
1148 -- which is a language extension (and is different from "is name"
1149 -- without parentheses).
1150 Dummy_Assoc
1151 -- Used for the dummy associations that are created in
1152 -- Save_Global_Defaults. These have Explicit_Generic_Actual_Parameter
1153 -- = Empty and Box_Present = False
1154 );
1155 -- ???We wouldn't need this enumeration type if we created new node
1156 -- kinds for N_Box_Subp_Default, N_Box_Actual, N_Null_Default, and
1157 -- N_Exp_Func_Default.
1158
1159 type Generic_Actual_Rec (Kind : Actual_Kind := None) is record
1160 -- Representation of one generic actual parameter
1161 case Kind is
1162 when None | None_Use_Clause | Box_Subp_Default | Box_Actual |
1163 Null_Default | Dummy_Assoc =>
1164 null;
1165 when Name_Exp | Exp_Func_Default =>
1166 Name_Exp : Node_Id;
1167 end case;
1168 end record;
1169
1170 type Actual_Origin_Enum is
1171 (None, From_Explicit_Actual, From_Default, From_Inference,
1172 From_Others_Box);
1173 -- Indication of where the Actual came from -- explicitly in the
1174 -- instantiation, inferred from some other type, or defaulted.
1175
1176 type Inference_Reason is
1177 -- Reason an actual type corresponding to a formal type was (or could
1178 -- be) inferred from the actual type corresponding to another formal
1179 -- type.
1180 (Designated_Type, -- designated type from formal access
1181 Index_Type, -- index type from formal array
1182 Component_Type, -- component type from formal array
1183 Discriminant_Type); -- discriminant type from formal discriminated
1184
1185 function Image (Reason : Inference_Reason) return String is
1186 (case Reason is
1187 when Designated_Type => "designated type",
1188 when Index_Type => "index type",
1189 when Component_Type => "component type",
1190 when Discriminant_Type => "discriminant type");
1191
1192 type Assoc_Index is new Pos;
1193 subtype Assoc_Count is Assoc_Index'Base range 0 .. Assoc_Index'Last;
1194
1195 type Assoc_Rec is record
1196 -- Association between a single formal/actual pair. But we store both
1197 -- the unanalyzed and analyzed formal.
1198
1199 Un_Formal, An_Formal : Node_Id; -- unanalyzed and analyzed formals
1200 -- An_Formal is the node in the generic copy that corresponds to
1201 -- Un_Formal. The semantic information on this node is used to
1202 -- perform legality checks on the actuals. Because semantic analysis
1203 -- can introduce some anonymous entities or modify the declaration
1204 -- node itself, the correspondence between the two lists is not
1205 -- one-one. In addition to anonymous types, a formal "=" will
1206 -- introduce an implicit equal and opposite "/=".
1207
1208 Explicit_Assoc : Opt_N_Generic_Association_Id;
1209 -- Explicit association, if any, from the source or generated.
1210
1211 Actual : Generic_Actual_Rec;
1212 -- Generic actual parameter corresponding to Un_Formal/An_Formal,
1213 -- possibly from defaults or others/boxes.
1214
1215 Actual_Origin : Actual_Origin_Enum;
1216 -- Reason why Actual was set; where it came from
1217
1218 Info_Inferred_Actual : Opt_Type_Kind_Id;
1219 -- An inferred actual is always a type entity, not a box, and not
1220 -- something like T'Base. This is used only for messages and
1221 -- assertions. It contains the type that was, or could have been,
1222 -- inferred.
1223
1224 Inferred_From : Assoc_Index;
1225 -- Index of a later Assoc_Rec in the same Gen_Assocs_Rec from which
1226 -- this one was inferred, or could be inferred.
1227 -- Valid only if Info_Inferred_Actual is present.
1228
1229 Reason : Inference_Reason;
1230 -- Reason the type was inferred, or could have been inferred.
1231 -- Valid only if Info_Inferred_Actual is present.
1232 end record; -- Assoc_Rec
1233
1234 type Assoc_Array is array (Assoc_Index range <>) of Assoc_Rec;
1235 -- One element for each formal and (if legal) for each corresponding
1236 -- actual.
1237
1238 type Gen_Assocs_Rec (Num_Assocs : Assoc_Count) is record
1239 -- Representation of formal/actual matching. Num_Assocs
1240 -- is the number of formals and (if legal) the number
1241 -- of actuals.
1242 Gen_Unit : Entity_Id;
1243 -- the generic unit being instantiated
1244 Others_Present : Boolean;
1245 -- True if "others => <>" (only for formal packages)
1246 Assocs : Assoc_Array (1 .. Num_Assocs);
1247 -- One for each formal/actual pair; defaulted and inferred actuals
1248 -- are included.
1249 end record;
1250
1251 function Match_Assocs
1252 (I_Node : Node_Id; Formals : List_Id; F_Copy : List_Id)
1253 return Gen_Assocs_Rec;
1254 -- I_Node is the instantiation node. Formals is the list of unanalyzed
1255 -- formals. F_Copy is the analyzed list of formals in the generic copy.
1256 -- Return a Gen_Assocs_Rec with formals, explicit actuals, and default
1257 -- actuals filled in. Check legality rules related to formal/actual
1258 -- matching.
1259
1260 procedure Note_Potential_Inference
1261 (I_Node : Node_Id; Gen_Assocs : Gen_Assocs_Rec);
1262 -- If -gnatd_I, print "info:" messages about type inference that could
1263 -- have been done.
1264
1265 end Associations;
1266
1267 procedure Analyze_One_Association
1268 (I_Node : Node_Id; -- instantiation node
1269 Assoc : Associations.Assoc_Rec;
1270 -- Logical 'in out' parameters:
1271 Result_Renamings : List_Id;
1272 Default_Actuals : List_Id;
1273 Actuals_To_Freeze : Elist_Id);
1274 -- Called by Analyze_Associations for each association. The renamings
1275 -- are appended onto Result_Renamings. Defaulted actuals are appended
1276 -- onto Default_Actuals, and actuals that require freezing are
1277 -- appended onto Actuals_To_Freeze.
1278
1279 procedure Check_Fixed_Point_Warning
1280 (Gen_Assocs : Associations.Gen_Assocs_Rec;
1281 Renamings : List_Id);
1282 -- Warn if any actual is a fixed-point type that has user-defined
1283 -- arithmetic operators, but there is no corresponding formal in the
1284 -- generic, in which case the predefined operators will be used. This
1285 -- merits a warning because of the special semantics of fixed point
1286 -- operators. However, do not warn if the formal is private, because there
1287 -- can be no arithmetic operators in the generic so there no danger of
1288 -- confusion.
1289
1290 ------------------
1291 -- Associations --
1292 ------------------
1293
1294 package body Associations is
1295
1296 generic
1297 with procedure Action (F : Node_Id; Index : Assoc_Index);
1298 procedure Formal_Iter (Formals : List_Id);
1299 -- Iterate through the unanalyzed formals, calling Action for each one.
1300 -- Skip pragmas, but do not skip use clauses.
1301
1302 function Num_Formals (Formals : List_Id) return Assoc_Count;
1303 -- Note: does not include pragmas that occur in the Formals list;
1304 -- it does include use clauses.
1305
1306 generic
1307 with procedure Action (F : Node_Id; Index : Assoc_Index);
1308 procedure An_Formal_Iter (An_Formals : List_Id);
1309 -- Iterate through the analyzed formals, calling Action for each one
1310 -- that corresponds to an unanalyzed formal. This should call Action
1311 -- exactly the same number of times that Formal_Iter calls its Action.
1312 -- Skip pragmas, but do not skip use clauses. Skip extraneous
1313 -- analyzed formals in cases where there are multiple ones
1314 -- corresponding to a particular unanalyzed one.
1315
1316 function Num_An_Formals (F_Copy : List_Id) return Assoc_Count;
1317 -- Number of analyzed formals that correspond directly to unanalyzed
1318 -- formals. There are all sorts of other things in F_Copy, which
1319 -- are not counted.
1320
1321 procedure Check_Box (I_Node, Actual : Node_Id);
1322 -- Check for errors in "others => <>" and "Name => <>"
1323
1324 function Default (Un_Formal : Node_Id) return Generic_Actual_Rec;
1325 -- Return the default for a given formal, which can be a name,
1326 -- expression, box, etc.
1327
1328 procedure Match_Positional
1329 (Src_Assoc : in out Node_Id; Assoc : in out Assoc_Rec);
1330 -- Called by Match_Assocs to match one positional parameter association.
1331 -- If the current formal (in Assoc) is not a use clause, then there is a
1332 -- match, and we set Assoc.Actual and move Src_Assoc to the next one.
1333
1334 procedure Match_Named
1335 (Src_Assoc : Node_Id; Assoc : in out Assoc_Rec;
1336 Found : in out Boolean);
1337 -- Called by Match_Assocs to match one named parameter association.
1338 -- If the current formal (in Assoc) is not a use clause, and the
1339 -- selector name matches the formal name, then there is a match,
1340 -- and we set Assoc.Actual. We also set the Selector_Name to denote
1341 -- the matched formal, and set Found to True.
1342
1343 procedure Inference_Msg
1344 (Gen_Unit : Entity_Id;
1345 Inferred_To, Inferred_From : Assoc_Rec;
1346 Was_Inferred : Boolean);
1347 -- If Was_Inferred is True, this prints out an "info:" message
1348 -- showing the inference.
1349 -- If Was_Inferred is False, the message says that it could have
1350 -- been inferred.
1351
1352 function Find_Assoc
1353 (Gen_Assocs : Gen_Assocs_Rec; F : Entity_Id) return Assoc_Index;
1354 -- Return the index of F in Gen_Assocs.Assocs, which must be
1355 -- present.
1356
1357 procedure Maybe_Infer_One
1358 (Gen_Assocs : in out Gen_Assocs_Rec;
1359 FF, AA : N_Entity_Id; Inferred_From : Assoc_Index;
1360 Reason : Inference_Reason);
1361 -- If it makes sense to infer that formal FF is associated with
1362 -- actual AA, then do so.
1363
1364 procedure Infer_From_Access
1365 (Gen_Assocs : in out Gen_Assocs_Rec;
1366 Index : Assoc_Index;
1367 F : Node_Id;
1368 A_Full : Entity_Id);
1369 -- Try to infer the designated type
1370
1371 procedure Infer_From_Array
1372 (Gen_Assocs : in out Gen_Assocs_Rec;
1373 Index : Assoc_Index;
1374 F : Node_Id;
1375 A_Full : Entity_Id);
1376 -- Try to infer the index and component types
1377
1378 procedure Infer_From_Discriminated
1379 (Gen_Assocs : in out Gen_Assocs_Rec;
1380 Index : Assoc_Index;
1381 F : Node_Id;
1382 A_Full : Entity_Id);
1383 -- Try to infer the types of discriminants
1384
1385 procedure Infer_Actuals (Gen_Assocs : in out Gen_Assocs_Rec);
1386 -- Called by Match_Assocs after processing explicit and defaulted
1387 -- parameters to infer any that are still missing.
1388
1389 -----------------
1390 -- Formal_Iter --
1391 -----------------
1392
1393 -- Formal_Iter is straightforward; An_Formal_Iter is not.
1394
1395 procedure Formal_Iter (Formals : List_Id) is
1396 F : Node_Id := First (Formals);
1397 Index : Assoc_Index := 1;
1398 begin
1399 while Present (F) loop
1400 case Nkind (F) is
1401 when N_Formal_Object_Declaration
1402 | N_Formal_Type_Declaration
1403 | N_Formal_Subprogram_Declaration
1404 | N_Formal_Package_Declaration
1405 | N_Use_Package_Clause
1406 | N_Use_Type_Clause
1407 =>
1408 Action (F, Index);
1409 Index := Index + 1;
1410 when N_Pragma =>
1411 null;
1412 when others =>
1413 raise Program_Error;
1414 end case;
1415
1416 Next (F);
1417 end loop;
1418 end Formal_Iter;
1419
1420 -----------------
1421 -- Num_Formals --
1422 -----------------
1423
1424 function Num_Formals (Formals : List_Id) return Assoc_Count is
1425 Result : Assoc_Count := 0;
1426 procedure Action (Ignore_F : Node_Id; Ignore : Assoc_Index);
1427 procedure Action (Ignore_F : Node_Id; Ignore : Assoc_Index) is
1428 begin
1429 Result := Result + 1;
1430 end Action;
1431 procedure Iter is new Formal_Iter (Action);
1432 begin
1433 Iter (Formals);
1434 return Result;
1435 end Num_Formals;
1436
1437 --------------------
1438 -- An_Formal_Iter --
1439 --------------------
1440
1441 procedure An_Formal_Iter (An_Formals : List_Id) is
1442 F : Node_Id := First (An_Formals);
1443 Index : Assoc_Index := 1;
1444 begin
1445 -- The correspondence between unanalyzed and analyzed formals is not
1446 -- one-one; hence this needs to do some fancy footwork to skip some
1447 -- items in the analyzed formals list. In each case where multiple
1448 -- items in An_Formals correspond to a particular unanalyzed formal,
1449 -- we must pick the "main" one.
1450
1451 while Present (F) loop
1452 case Nkind (F) is
1453 when N_Use_Package_Clause | N_Use_Type_Clause =>
1454 Action (F, Index);
1455 Index := Index + 1;
1456
1457 when N_Formal_Object_Declaration
1458 | N_Formal_Type_Declaration
1459 | N_Formal_Subprogram_Declaration
1460 | N_Package_Declaration
1461 | N_Full_Type_Declaration
1462 | N_Private_Type_Declaration
1463 | N_Private_Extension_Declaration
1464 =>
1465 if Is_Internal_Name (Chars (Defining_Entity (F))) then
1466 null;
1467 else
1468 Action (F, Index);
1469 Index := Index + 1;
1470
1471 -- Skip full type of derived type
1472
1473 if Nkind (F) = N_Full_Type_Declaration
1474 and then Nkind (Type_Definition (F)) =
1475 N_Derived_Type_Definition
1476 and then Present (Next (F))
1477 and then Nkind (Next (F)) = N_Full_Type_Declaration
1478 and then Chars (Defining_Identifier (F)) =
1479 Chars (Defining_Identifier (Next (F)))
1480 then
1481 Next (F);
1482 end if;
1483 end if;
1484
1485 when N_Subtype_Declaration =>
1486 if Nkind (Original_Node (F)) in N_Formal_Type_Declaration
1487 then
1488 pragma Assert
1489 (not Is_Internal_Name (Chars (Defining_Entity (F))));
1490 Action (F, Index);
1491 Index := Index + 1;
1492
1493 elsif Nkind (Original_Node (F)) in N_Full_Type_Declaration
1494 then
1495 null;
1496 else
1497 -- subtype of a formal object
1498
1499 pragma Assert
1500 (Nkind (Next (F)) = N_Formal_Object_Declaration);
1501 end if;
1502
1503 when N_Pragma =>
1504 null;
1505
1506 when N_Formal_Package_Declaration =>
1507 -- If there were no errors, this would have been transformed
1508 -- into an N_Package_Declaration.
1509
1510 Check_Error_Detected;
1511 pragma Assert (Error_Posted (F));
1512 Abandon_Instantiation (Instantiation_Node);
1513
1514 when others =>
1515 raise Program_Error;
1516 end case;
1517
1518 Next (F);
1519 end loop;
1520 end An_Formal_Iter;
1521
1522 --------------------
1523 -- Num_An_Formals --
1524 --------------------
1525
1526 function Num_An_Formals (F_Copy : List_Id) return Assoc_Count is
1527 Result : Assoc_Count := 0;
1528 procedure Action (Ignore_F : Node_Id; Ignore : Assoc_Index);
1529 procedure Action (Ignore_F : Node_Id; Ignore : Assoc_Index) is
1530 begin
1531 Result := Result + 1;
1532 end Action;
1533 procedure Iter is new An_Formal_Iter (Action);
1534 begin
1535 Iter (F_Copy);
1536 return Result;
1537 end Num_An_Formals;
1538
1539 ---------------
1540 -- Check_Box --
1541 ---------------
1542
1543 procedure Check_Box (I_Node, Actual : Node_Id) is
1544 begin
1545 -- "... => <>" is allowed only in formal packages, not old-fashioned
1546 -- instantiations.
1547
1548 if Nkind (I_Node) /= N_Formal_Package_Declaration
1549 and then Comes_From_Source (I_Node)
1550 then
1551 if Actual in N_Others_Choice_Id then
1552 Error_Msg_N
1553 ("OTHERS association not allowed in an instance", Actual);
1554 elsif Box_Present (Actual) then
1555 Error_Msg_N
1556 ("box association not allowed in an instance", Actual);
1557 end if;
1558 end if;
1559
1560 -- "others => <>" must come last
1561
1562 if Actual in N_Others_Choice_Id
1563 and then Present (Next (Actual))
1564 then
1565 Error_Msg_N
1566 ("OTHERS must be last association", Actual);
1567 end if;
1568 end Check_Box;
1569
1570 -------------
1571 -- Default --
1572 -------------
1573
1574 function Default (Un_Formal : Node_Id) return Generic_Actual_Rec is
1575 begin
1576 return Result : Generic_Actual_Rec do
1577 case Nkind (Un_Formal) is
1578 when N_Formal_Object_Declaration =>
1579 if Present (Default_Expression (Un_Formal)) then
1580 Result := (Name_Exp, Default_Expression (Un_Formal));
1581 end if;
1582 when N_Formal_Type_Declaration =>
1583 if Present (Default_Subtype_Mark (Un_Formal)) then
1584 Result := (Name_Exp, Default_Subtype_Mark (Un_Formal));
1585 end if;
1586 when N_Formal_Subprogram_Declaration =>
1587 if Present (Default_Name (Un_Formal)) then
1588 pragma Assert (Result.Kind = None);
1589 Result := (Name_Exp, Default_Name (Un_Formal));
1590 end if;
1591
1592 if Box_Present (Un_Formal) then
1593 pragma Assert (Result.Kind = None);
1594 Result := (Kind => Box_Subp_Default);
1595 end if;
1596
1597 if Present (Expression (Un_Formal)) then
1598 pragma Assert (Result.Kind = None);
1599 Result := (Exp_Func_Default, Expression (Un_Formal));
1600 end if;
1601
1602 if Has_Null_Default (Un_Formal) then
1603 pragma Assert (Result.Kind = None);
1604 Result := (Kind => Null_Default);
1605 end if;
1606
1607 when N_Formal_Package_Declaration => null;
1608
1609 when others => raise Program_Error;
1610 end case;
1611 pragma Assert
1612 (if Result.Kind in Name_Exp | Exp_Func_Default then
1613 Present (Result.Name_Exp));
1614 end return;
1615 end Default;
1616
1617 ----------------------
1618 -- Match_Positional --
1619 ----------------------
1620
1621 procedure Match_Positional
1622 (Src_Assoc : in out Node_Id; Assoc : in out Assoc_Rec) is
1623 begin
1624 if Nkind (Assoc.Un_Formal) not in
1625 N_Use_Package_Clause | N_Use_Type_Clause
1626 then
1627 pragma Assert (No (Assoc.Explicit_Assoc));
1628 pragma Assert (Assoc.Actual.Kind = None);
1629 Assoc.Explicit_Assoc := Src_Assoc;
1630
1631 -- A "<>" without "name =>" is illegal syntax
1632
1633 if Box_Present (Src_Assoc) then
1634 Assoc.Actual := (Kind => Box_Actual);
1635 if False then -- ???
1636 -- Disable this for now, because we have various
1637 -- code that needs to be updated.
1638 Error_Msg_N
1639 ("box requires named notation", Src_Assoc);
1640 end if;
1641 else
1642 Assoc.Actual :=
1643 (Name_Exp,
1644 Explicit_Generic_Actual_Parameter (Src_Assoc));
1645 pragma Assert (Present (Assoc.Actual.Name_Exp));
1646 end if;
1647 Assoc.Actual_Origin := From_Explicit_Actual;
1648
1649 Next (Src_Assoc);
1650 end if;
1651 end Match_Positional;
1652
1653 -----------------
1654 -- Match_Named --
1655 -----------------
1656
1657 procedure Match_Named
1658 (Src_Assoc : Node_Id; Assoc : in out Assoc_Rec;
1659 Found : in out Boolean) is
1660 begin
1661 if Nkind (Assoc.Un_Formal) not in
1662 N_Use_Package_Clause | N_Use_Type_Clause
1663 and then Chars (Selector_Name (Src_Assoc)) =
1664 Chars (Defining_Entity (Assoc.Un_Formal))
1665 then
1666 if Found then -- second formal with the same name
1667 pragma Assert (Comes_From_Source (Src_Assoc));
1668 Error_Msg_N
1669 ("named association not allowed for " &
1670 "overloaded formal", Src_Assoc);
1671 Abandon_Instantiation (Instantiation_Node);
1672 end if;
1673
1674 if Assoc.Actual.Kind /= None then
1675 if Comes_From_Source (Src_Assoc) then
1676 Error_Msg_NE
1677 ("duplicate actual for &",
1678 Src_Assoc, Selector_Name (Src_Assoc));
1679 end if;
1680 else
1681 Assoc.Explicit_Assoc := Src_Assoc;
1682 if Box_Present (Src_Assoc) then
1683 Assoc.Actual := (Kind => Box_Actual);
1684
1685 else
1686 if No (Explicit_Generic_Actual_Parameter (Src_Assoc)) then
1687 Assoc.Actual := (Kind => Dummy_Assoc);
1688 else
1689 Assoc.Actual :=
1690 (Name_Exp,
1691 Explicit_Generic_Actual_Parameter (Src_Assoc));
1692 end if;
1693
1694 -- Set Entity (etc.) of the selector name:
1695
1696 declare
1697 A_F : constant Entity_Id :=
1698 Defining_Entity (Assoc.An_Formal);
1699 Orig_F : constant Node_Id :=
1700 Original_Node (Assoc.An_Formal);
1701 Sel : constant Node_Id :=
1702 Selector_Name (Assoc.Explicit_Assoc);
1703 begin
1704 Set_Entity (Sel, A_F);
1705 Set_Etype (Sel, Etype (A_F));
1706
1707 if Nkind (Orig_F) = N_Formal_Package_Declaration then
1708 Generate_Reference (Defining_Identifier (Orig_F), Sel);
1709 -- ???Original_Node makes no sense, but we're
1710 -- preserving the old behavior.
1711 else
1712 Generate_Reference (A_F, Sel);
1713 end if;
1714 end;
1715 end if;
1716
1717 Assoc.Actual_Origin := From_Explicit_Actual;
1718 Found := True;
1719 end if;
1720 end if;
1721 end Match_Named;
1722
1723 ------------------
1724 -- Match_Assocs --
1725 ------------------
1726
1727 function Match_Assocs
1728 (I_Node : Node_Id; Formals : List_Id; F_Copy : List_Id)
1729 return Gen_Assocs_Rec
1730 is
1731 Src_Assocs : constant List_Id := Generic_Associations (I_Node);
1732 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
1733 begin
1734 pragma Assert
1735 (Num_An_Formals (F_Copy) = Num_Formals (Formals)
1736 or else Serious_Errors_Detected > 0);
1737
1738 return Result : Gen_Assocs_Rec (Num_Assocs => Num_Formals (Formals))
1739 do
1740 Result.Gen_Unit := Gen_Unit;
1741 Result.Others_Present := False;
1742
1743 -- Loop through the unanalyzed formals:
1744
1745 declare
1746 procedure Set_Formal (F : Node_Id; Index : Assoc_Index);
1747 -- Initialize one Assoc_Rec so the formal is set.
1748 -- Use a dummy assoc for use clauses.
1749
1750 procedure Set_Formal (F : Node_Id; Index : Assoc_Index) is
1751 Assoc : Assoc_Rec renames Result.Assocs (Index);
1752 begin
1753 if Nkind (F) in N_Use_Package_Clause | N_Use_Type_Clause then
1754 Assoc :=
1755 (Un_Formal => F,
1756 An_Formal => Empty,
1757 Explicit_Assoc => Empty,
1758 Actual => (Kind => None_Use_Clause),
1759 Actual_Origin => None,
1760 Info_Inferred_Actual => Empty,
1761 Inferred_From => <>,
1762 Reason => <>);
1763 else
1764 Assoc :=
1765 (Un_Formal => F,
1766 An_Formal => Empty,
1767 Explicit_Assoc => Empty,
1768 Actual => <>,
1769 Actual_Origin => None,
1770 Info_Inferred_Actual => Empty,
1771 Inferred_From => <>,
1772 Reason => <>);
1773 end if;
1774 end Set_Formal;
1775 procedure Iter is new Formal_Iter (Set_Formal);
1776 begin
1777 Iter (Formals);
1778 end;
1779
1780 -- Loop through the analyzed copy of the formals:
1781
1782 declare
1783 procedure Set_An_Formal (F : Node_Id; Index : Assoc_Index);
1784 procedure Set_An_Formal (F : Node_Id; Index : Assoc_Index) is
1785 Assoc : Assoc_Rec renames Result.Assocs (Index);
1786 begin
1787 Assoc.An_Formal := F;
1788 if Nkind (F) in N_Use_Package_Clause | N_Use_Type_Clause then
1789 pragma Assert
1790 (Nkind (Assoc.Un_Formal) = Nkind (Assoc.An_Formal));
1791
1792 else
1793 case Nkind (Assoc.Un_Formal) is
1794 when N_Formal_Object_Declaration
1795 | N_Formal_Subprogram_Declaration
1796 =>
1797 pragma Assert
1798 (Nkind (Assoc.Un_Formal) =
1799 Nkind (Assoc.An_Formal));
1800
1801 when N_Formal_Type_Declaration =>
1802 pragma Assert
1803 (Nkind (Original_Node (Assoc.An_Formal)) =
1804 N_Formal_Type_Declaration);
1805 pragma Assert
1806 (Nkind (Assoc.An_Formal) in
1807 N_Formal_Type_Declaration
1808 | N_Full_Type_Declaration
1809 | N_Private_Type_Declaration
1810 | N_Private_Extension_Declaration
1811 | N_Subtype_Declaration);
1812
1813 when N_Formal_Package_Declaration =>
1814 pragma Assert
1815 (Nkind (Original_Node (Assoc.An_Formal)) =
1816 N_Formal_Package_Declaration);
1817 pragma Assert
1818 (Nkind (Assoc.An_Formal) = N_Package_Declaration);
1819
1820 when others => pragma Assert (False);
1821 end case;
1822
1823 pragma Assert
1824 (Chars (Defining_Entity (Assoc.Un_Formal)) =
1825 Chars (Defining_Entity (Assoc.An_Formal)));
1826 end if;
1827 end Set_An_Formal;
1828
1829 procedure Iter is new An_Formal_Iter (Set_An_Formal);
1830 begin
1831 pragma Assert
1832 (Num_An_Formals (F_Copy) = Result.Assocs'Last
1833 or else Serious_Errors_Detected > 0);
1834 Iter (F_Copy);
1835 end;
1836
1837 -- Loop through actual source associations:
1838
1839 declare
1840 Src_Assoc : Node_Id := First (Src_Assocs);
1841 -- Generic association from the source
1842
1843 function Positional return Boolean is
1844 (Present (Src_Assoc)
1845 and then Src_Assoc not in N_Others_Choice_Id
1846 and then No (Selector_Name (Src_Assoc)));
1847 -- True if Src_Assoc is position; i.e. not named and not others
1848 begin
1849 -- Loop through positional actuals:
1850
1851 for Index in Result.Assocs'Range loop
1852 exit when not Positional;
1853 Match_Positional (Src_Assoc, Result.Assocs (Index));
1854 end loop;
1855
1856 if Positional then
1857 Error_Msg_Sloc := Sloc (Gen_Unit);
1858 Error_Msg_NE
1859 ("unmatched actual in instantiation of & declared#",
1860 Src_Assoc, Gen_Unit);
1861 else
1862 -- Loop through named actuals and "others => <>":
1863
1864 while Present (Src_Assoc) loop
1865 Check_Box (I_Node, Src_Assoc);
1866 if Src_Assoc in N_Others_Choice_Id then
1867 Result.Others_Present := True;
1868 exit;
1869 end if;
1870
1871 if Positional then
1872 Error_Msg_N
1873 ("invalid positional actual after named one",
1874 Src_Assoc);
1875 else
1876 -- For actual "X => ...", find formal whose name is X.
1877 -- Complain if X has already been specified (could be
1878 -- by a positional association, or by a previous named
1879 -- one). Also complain if there's more than one X.
1880 -- See RM-12.3(9/3) and 12.7(4.1/3).
1881 -- However, this rule does not apply to generated
1882 -- code,because for nested instances, we routinely
1883 -- generate things like:
1884 -- X => ..., X => ...
1885 -- where the first one refers to the first formal X,
1886 -- and the second one refers to the second formal X,
1887 -- and so on. (The X's are formal subprograms in this
1888 -- case.)
1889
1890 declare
1891 Found : Boolean := False;
1892 begin
1893 for Index in Result.Assocs'Range loop
1894 Match_Named
1895 (Src_Assoc, Result.Assocs (Index), Found);
1896 exit when Found
1897 and then not Comes_From_Source (Src_Assoc);
1898 end loop;
1899
1900 if not Found and then Comes_From_Source (Src_Assoc)
1901 then
1902 Error_Msg_Sloc := Sloc (Gen_Unit);
1903 Error_Msg_NE
1904 ("unmatched actual &",
1905 Src_Assoc, Selector_Name (Src_Assoc));
1906 Error_Msg_NE
1907 ("\in instantiation of & declared#",
1908 Src_Assoc, Gen_Unit);
1909 end if;
1910 end;
1911 end if;
1912
1913 Next (Src_Assoc);
1914 end loop;
1915 end if;
1916 end;
1917
1918 -- Fill in defaults. For each formal F with no associated actual,
1919 -- if there is "others => <>", set the actual to "F => <>".
1920 -- Otherwise, if the formal has a default, set the actual to
1921 -- "F => default". Otherwise leave it Empty.
1922 -- (If Empty, it could be inferred, or it could be an error).
1923
1924 for Index in Result.Assocs'Range loop
1925 declare
1926 Assoc : Assoc_Rec renames Result.Assocs (Index);
1927 begin
1928 if Assoc.Actual.Kind = None then
1929 pragma Assert (No (Assoc.Explicit_Assoc));
1930 if Result.Others_Present then
1931 Assoc.Actual := (Kind => Box_Actual);
1932 Assoc.Actual_Origin := From_Others_Box;
1933 else
1934 Assoc.Actual := Default (Assoc.Un_Formal);
1935 if Assoc.Actual.Kind /= None then
1936 Assoc.Actual_Origin := From_Default;
1937 end if;
1938 end if;
1939 end if;
1940 end;
1941 end loop;
1942
1943 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1944 Infer_Actuals (Gen_Assocs => Result);
1945 end if;
1946
1947 -- Check for missing actuals
1948
1949 for Index in Result.Assocs'Range loop
1950 if Result.Assocs (Index).Actual.Kind = None then
1951 Error_Msg_Sloc := Sloc (Gen_Unit);
1952 Error_Msg_NE
1953 ("missing actual &",
1954 Instantiation_Node,
1955 Defining_Entity (Result.Assocs (Index).Un_Formal));
1956 Error_Msg_NE
1957 ("\in instantiation of & declared#",
1958 Instantiation_Node, Gen_Unit);
1959 Abandon_Instantiation (Instantiation_Node);
1960 end if;
1961 end loop;
1962 end return;
1963 end Match_Assocs;
1964
1965 -------------------
1966 -- Inference_Msg --
1967 -------------------
1968
1969 procedure Inference_Msg
1970 (Gen_Unit : Entity_Id;
1971 Inferred_To, Inferred_From : Assoc_Rec;
1972 Was_Inferred : Boolean)
1973 is
1974 pragma Assert (Debug_Flag_Underscore_II); -- This is only for -gnatd_I
1975
1976 Was : constant String := (if Was_Inferred then "" else "could have ");
1977
1978 -- "if True" below to leave out some verbosity for now:
1979 Inst : constant String :=
1980 (if True then ""
1981 else " gen: " & Get_Name_String (Chars (Gen_Unit)));
1982 Decl : constant String := (if True then "" else " declared # ");
1983
1984 R : constant String := " (" & Image (Inferred_To.Reason) & ")";
1985
1986 Mess : constant String :=
1987 "info: " & Was & "inferred `% ='> &`" & Decl & Inst & R;
1988 Mess_2 : constant String :=
1989 "info: `% ='> ...`";
1990 begin
1991 Error_Msg_Name_1 := Chars (Defining_Entity (Inferred_To.An_Formal));
1992 Error_Msg_Sloc := Sloc (Inferred_To.Info_Inferred_Actual);
1993 if not In_Instance then
1994 if Debug_Flag_Underscore_II then
1995 Error_Msg_NE
1996 (Mess, Inferred_From.Actual.Name_Exp,
1997 Inferred_To.Info_Inferred_Actual);
1998 Error_Msg_Name_1 :=
1999 Chars (Defining_Identifier (Inferred_From.An_Formal));
2000 Error_Msg_N (Mess_2, Inferred_From.Actual.Name_Exp);
2001 end if;
2002 end if;
2003 end Inference_Msg;
2004
2005 ------------------------------
2006 -- Note_Potential_Inference --
2007 ------------------------------
2008
2009 procedure Note_Potential_Inference
2010 (I_Node : Node_Id; Gen_Assocs : Gen_Assocs_Rec)
2011 is
2012 begin
2013 if not Debug_Flag_Underscore_II or else Serious_Errors_Detected > 0
2014 then
2015 return;
2016 end if;
2017
2018 for Index in Gen_Assocs.Assocs'Range loop
2019 declare
2020 Assoc : Assoc_Rec renames Gen_Assocs.Assocs (Index);
2021 begin
2022 if Assoc.Actual_Origin = From_Explicit_Actual
2023 and then Present (Assoc.Info_Inferred_Actual)
2024 and then In_Extended_Main_Source_Unit (I_Node)
2025 and then not In_Internal_Unit (I_Node)
2026 then
2027 Inference_Msg
2028 (Gen_Assocs.Gen_Unit,
2029 Inferred_To => Assoc,
2030 Inferred_From => Gen_Assocs.Assocs (Assoc.Inferred_From),
2031 Was_Inferred => False);
2032 end if;
2033 end;
2034 end loop;
2035 end Note_Potential_Inference;
2036
2037 --------------
2038 -- Find_Assoc --
2039 --------------
2040
2041 function Find_Assoc
2042 (Gen_Assocs : Gen_Assocs_Rec; F : Entity_Id) return Assoc_Index
2043 is
2044 begin
2045 for Index in Gen_Assocs.Assocs'Range loop
2046 if Defining_Entity (Gen_Assocs.Assocs (Index).An_Formal) = F then
2047 return Index;
2048 end if;
2049 end loop;
2050
2051 raise Program_Error; -- it must be present
2052 end Find_Assoc;
2053
2054 ---------------------
2055 -- Maybe_Infer_One --
2056 ---------------------
2057
2058 procedure Maybe_Infer_One
2059 (Gen_Assocs : in out Gen_Assocs_Rec;
2060 FF, AA : N_Entity_Id; Inferred_From : Assoc_Index;
2061 Reason : Inference_Reason)
2062 is
2063 begin
2064 if not (Is_Generic_Type (FF)
2065 and then Scope (FF) = Gen_Assocs.Gen_Unit)
2066 then
2067 return; -- no inference if not a formal type of this generic
2068 end if;
2069
2070 if Is_Internal_Name (Chars (FF)) or else Is_Itype (AA) then
2071 return; -- no inference if internally generated
2072 end if;
2073
2074 declare
2075 Index : constant Assoc_Index := Find_Assoc (Gen_Assocs, FF);
2076 Assoc : Assoc_Rec renames Gen_Assocs.Assocs (Index);
2077 pragma Assert (Defining_Entity (Assoc.An_Formal) = FF);
2078
2079 From_Actual : constant Node_Id :=
2080 Gen_Assocs.Assocs (Inferred_From).Actual.Name_Exp;
2081
2082 begin
2083 Assoc.Info_Inferred_Actual := AA;
2084 Assoc.Inferred_From := Inferred_From;
2085 Assoc.Reason := Reason;
2086
2087 if Assoc.Actual.Kind = None then
2088 Assoc.Actual :=
2089 (Name_Exp, New_Occurrence_Of (AA, Sloc (From_Actual)));
2090 Assoc.Actual_Origin := From_Inference;
2091
2092 Error_Msg_GNAT_Extension
2093 ("type inference of generic parameters",
2094 Sloc (From_Actual));
2095
2096 if Debug_Flag_Underscore_II then
2097 Inference_Msg
2098 (Gen_Assocs.Gen_Unit,
2099 Inferred_To => Assoc,
2100 Inferred_From => Gen_Assocs.Assocs (Assoc.Inferred_From),
2101 Was_Inferred => True);
2102 end if;
2103 end if;
2104 end;
2105 end Maybe_Infer_One;
2106
2107 -------------------
2108 -- Infer_Actuals --
2109 -------------------
2110
2111 procedure Infer_From_Access
2112 (Gen_Assocs : in out Gen_Assocs_Rec;
2113 Index : Assoc_Index;
2114 F : Node_Id;
2115 A_Full : Entity_Id)
2116 is
2117 begin
2118 if Ekind (A_Full) in Access_Kind then
2119 declare
2120 FF : constant Entity_Id :=
2121 Designated_Type (Defining_Entity (F));
2122 AA : constant Entity_Id := Designated_Type (A_Full);
2123 begin
2124 Maybe_Infer_One
2125 (Gen_Assocs,
2126 FF,
2127 AA,
2128 Inferred_From => Index,
2129 Reason => Designated_Type);
2130 end;
2131 end if;
2132 end Infer_From_Access;
2133
2134 procedure Infer_From_Array
2135 (Gen_Assocs : in out Gen_Assocs_Rec;
2136 Index : Assoc_Index;
2137 F : Node_Id;
2138 A_Full : Entity_Id)
2139 is
2140 begin
2141 if Ekind (A_Full) in Array_Kind then
2142 declare
2143 F_Index_Type : Opt_N_Is_Index_Id :=
2144 First_Index (Defining_Entity (F));
2145 A_Index_Type : Opt_N_Is_Index_Id :=
2146 First_Index (A_Full);
2147 begin
2148 while Present (F_Index_Type) and then Present (A_Index_Type)
2149 loop
2150 Maybe_Infer_One
2151 (Gen_Assocs,
2152 Etype (F_Index_Type),
2153 Etype (A_Index_Type),
2154 Inferred_From => Index,
2155 Reason => Index_Type);
2156
2157 Next_Index (F_Index_Type);
2158 Next_Index (A_Index_Type);
2159 end loop;
2160 end;
2161
2162 declare
2163 F_Comp_Type : constant Type_Kind_Id :=
2164 Component_Type (Defining_Entity (F));
2165 A_Comp_Type : constant Type_Kind_Id :=
2166 Component_Type (A_Full);
2167 begin
2168 Maybe_Infer_One
2169 (Gen_Assocs,
2170 F_Comp_Type,
2171 A_Comp_Type,
2172 Inferred_From => Index,
2173 Reason => Component_Type);
2174 end;
2175 end if;
2176 end Infer_From_Array;
2177
2178 procedure Infer_From_Discriminated
2179 (Gen_Assocs : in out Gen_Assocs_Rec;
2180 Index : Assoc_Index;
2181 F : Node_Id;
2182 A_Full : Entity_Id)
2183 is
2184 begin
2185 if Has_Discriminants (Defining_Entity (F))
2186 and then Present (A_Full)
2187 and then Has_Discriminants (A_Full)
2188 and then Number_Discriminants (A_Full) =
2189 Number_Discriminants (Defining_Entity (F))
2190 then
2191 declare
2192 F_Discrim : Node_Id := First_Discriminant (Defining_Entity (F));
2193 A_Discrim : Node_Id := First_Discriminant (A_Full);
2194 begin
2195 while Present (F_Discrim) loop
2196 Maybe_Infer_One
2197 (Gen_Assocs,
2198 Etype (F_Discrim),
2199 Etype (A_Discrim),
2200 Inferred_From => Index,
2201 Reason => Discriminant_Type);
2202
2203 Next_Discriminant (F_Discrim);
2204 Next_Discriminant (A_Discrim);
2205 end loop;
2206 pragma Assert (No (A_Discrim)); -- same number as F_Discrim
2207 end;
2208 end if;
2209 end Infer_From_Discriminated;
2210
2211 procedure Infer_Actuals (Gen_Assocs : in out Gen_Assocs_Rec) is
2212 -- Note that we can infer FROM defaults, but we cannot infer TO a
2213 -- parameter that has a default. We can also infer from inferred
2214 -- types.
2215
2216 -- We don't need to check that multiple inferences get the same
2217 -- answer; the second one will get a type mismatch or nonstatically
2218 -- matching error.
2219
2220 -- This code needs to be robust, in the sense of tolerating illegal
2221 -- code, because we have not yet checked all legality rules. For
2222 -- example, if a formal type F has a discriminant whose type is
2223 -- another formal type, then we want to infer the type of the
2224 -- discriminant from the actual for F. That actual must have
2225 -- discriminants, but we have not checked that rule yet, so we
2226 -- need to tolerate an actual for F that has no discriminants.
2227
2228 begin
2229 -- For each parameter, check whether we can infer FROM that one TO
2230 -- other ones.
2231
2232 -- Process the parameters in reverse order, because the inferred type
2233 -- always comes before the parameter it is inferred from. This
2234 -- ensures that we can do the inference in one pass, including in
2235 -- cases where an inferred type leads to another inferred type.
2236 -- For example, an array type that allows us to infer the component
2237 -- type, which is an access type that allows us to infer the
2238 -- designated type. The reverse loop implies that we will see the
2239 -- array type, then the access type, then the designated type.
2240
2241 for Index in reverse Gen_Assocs.Assocs'Range loop -- NB: "reverse"
2242 if Gen_Assocs.Assocs (Index).Actual.Kind = Name_Exp then
2243 declare
2244 F : constant Node_Id := Gen_Assocs.Assocs (Index).An_Formal;
2245 A_E : constant Node_Id :=
2246 Gen_Assocs.Assocs (Index).Actual.Name_Exp;
2247 A_Full : Entity_Id := Empty;
2248 begin
2249 if Nkind (A_E) in N_Has_Entity then
2250 A_Full := Entity (A_E);
2251
2252 if Present (A_Full)
2253 and then Ekind (A_Full) in Incomplete_Kind
2254 and then Present (Full_View (A_Full))
2255 then
2256 A_Full := Full_View (A_Full);
2257 end if;
2258 end if;
2259
2260 if Nkind (F) = N_Formal_Type_Declaration
2261 and then Present (A_Full)
2262 then
2263 case Ekind (Defining_Entity (F)) is
2264 when E_Access_Type | E_General_Access_Type =>
2265 Infer_From_Access (Gen_Assocs, Index, F, A_Full);
2266
2267 when E_Access_Subtype
2268 | E_Access_Attribute_Type
2269 | E_Allocator_Type
2270 | E_Anonymous_Access_Type =>
2271 raise Program_Error;
2272
2273 when E_Array_Type | E_Array_Subtype =>
2274 Infer_From_Array (Gen_Assocs, Index, F, A_Full);
2275
2276 when E_String_Literal_Subtype =>
2277 raise Program_Error;
2278
2279 when others =>
2280 null;
2281 end case;
2282
2283 Infer_From_Discriminated (Gen_Assocs, Index, F, A_Full);
2284 end if;
2285 end;
2286 end if;
2287 end loop;
2288 end Infer_Actuals;
2289
2290 end Associations;
2291
2292 ---------------------------
2293 -- Abandon_Instantiation --
2294 ---------------------------
2295
2296 procedure Abandon_Instantiation (N : Node_Id) is
2297 begin
2298 Error_Msg_N ("\instantiation abandoned!", N);
2299 raise Instantiation_Error;
2300 end Abandon_Instantiation;
2301
2302 ----------------------------------
2303 -- Adjust_Inherited_Pragma_Sloc --
2304 ----------------------------------
2305
2306 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
2307 begin
2308 Adjust_Instantiation_Sloc (N, S_Adjustment);
2309 end Adjust_Inherited_Pragma_Sloc;
2310
2311 --------------------------
2312 -- Analyze_Associations --
2313 --------------------------
2314
2315 function Analyze_Associations
2316 (I_Node : Node_Id;
2317 Formals : List_Id;
2318 F_Copy : List_Id) return List_Id
2319 is
2320 use Associations;
2321
2322 Result_Renamings : constant List_Id := New_List;
2323 -- To be returned. Includes "renamings" broadly interpreted
2324 -- (e.g. subtypes are used for types).
2325
2326 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
2327 Default_Actuals : constant List_Id := New_List;
2328
2329 Gen_Assocs : constant Gen_Assocs_Rec :=
2330 Match_Assocs (I_Node, Formals, F_Copy);
2331
2332 begin
2333 for Matching_Actual_Index in Gen_Assocs.Assocs'Range loop
2334 declare
2335 Assoc : Assoc_Rec renames
2336 Gen_Assocs.Assocs (Matching_Actual_Index);
2337 begin
2338 if Nkind (Assoc.Un_Formal) = N_Formal_Package_Declaration
2339 and then Error_Posted (Assoc.An_Formal)
2340 then
2341 -- Restrict this to N_Formal_Package_Declaration,
2342 -- because otherwise we miss errors.
2343 Abandon_Instantiation (Instantiation_Node);
2344 end if;
2345
2346 if Nkind (Assoc.Un_Formal) in
2347 N_Use_Package_Clause | N_Use_Type_Clause
2348 then
2349 -- Copy the use clause to where it belongs:
2350 Append (New_Copy_Tree (Assoc.Un_Formal), Result_Renamings);
2351
2352 else
2353 Analyze_One_Association
2354 (I_Node, Assoc,
2355 Result_Renamings, Default_Actuals, Actuals_To_Freeze);
2356 end if;
2357 end;
2358 end loop;
2359
2360 -- An instantiation freezes all generic actuals, except for incomplete
2361 -- types and subprograms that are not fully defined at the point of
2362 -- instantiation.
2363
2364 declare
2365 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2366 begin
2367 while Present (Elmt) loop
2368 Freeze_Before (I_Node, Node (Elmt));
2369 Next_Elmt (Elmt);
2370 end loop;
2371 end;
2372
2373 -- If there are defaults, normalize the tree by adding explicit
2374 -- associations for them. This is required if the instance appears
2375 -- within a generic.
2376
2377 if not Is_Empty_List (Default_Actuals) then
2378 declare
2379 Default : Node_Id;
2380
2381 begin
2382 Default := First (Default_Actuals);
2383 while Present (Default) loop
2384 Mark_Rewrite_Insertion (Default);
2385 Next (Default);
2386 end loop;
2387
2388 if No (Generic_Associations (I_Node)) then
2389 Set_Generic_Associations (I_Node, Default_Actuals);
2390 else
2391 Append_List_To (Generic_Associations (I_Node), Default_Actuals);
2392 end if;
2393 end;
2394 end if;
2395
2396 Note_Potential_Inference (I_Node, Gen_Assocs);
2397
2398 Check_Fixed_Point_Warning (Gen_Assocs, Result_Renamings);
2399
2400 return Result_Renamings;
2401 end Analyze_Associations;
2402
2403 -----------------------------
2404 -- Analyze_One_Association --
2405 -----------------------------
2406
2407 procedure Analyze_One_Association
2408 (I_Node : Node_Id;
2409 Assoc : Associations.Assoc_Rec;
2410 -- Logical 'in out' parameters:
2411 Result_Renamings : List_Id;
2412 Default_Actuals : List_Id;
2413 Actuals_To_Freeze : Elist_Id)
2414 is
2415 use Associations;
2416
2417 procedure Process_Box_Actual (Formal : Node_Id);
2418 -- Called for "Formal => <>", and also if "Formal => ..." is missing,
2419 -- but there is "others => <>". Add a copy of the declaration of the
2420 -- generic formal to the Result_Renamings.
2421
2422 ------------------------
2423 -- Process_Box_Actual --
2424 ------------------------
2425
2426 procedure Process_Box_Actual (Formal : Node_Id) is
2427 pragma Assert (Assoc.Actual.Kind = Box_Actual);
2428 F_Id : constant Entity_Id := Defining_Entity (Formal);
2429 Decl : constant Node_Id := New_Copy_Tree (Formal);
2430 Id : constant Entity_Id :=
2431 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
2432 begin
2433 if Nkind (Formal) in N_Formal_Subprogram_Declaration then
2434 Set_Defining_Unit_Name (Specification (Decl), Id);
2435
2436 else
2437 Set_Defining_Identifier (Decl, Id);
2438 end if;
2439
2440 Append (Decl, Result_Renamings);
2441 end Process_Box_Actual;
2442
2443 Match : Node_Id;
2444
2445 -- Start of processing for Analyze_One_Association
2446
2447 begin
2448 pragma Assert (Assoc.Actual_Origin /= None);
2449
2450 if Assoc.Actual_Origin = From_Explicit_Actual
2451 and then Assoc.Actual.Kind = Name_Exp
2452 then
2453 Match := Assoc.Actual.Name_Exp;
2454
2455 if Is_Entity_Name (Match) and then Present (Entity (Match)) then
2456 Set_Used_As_Generic_Actual (Entity (Match));
2457 end if;
2458 else
2459 Match := Empty;
2460 end if;
2461
2462 case Nkind (Assoc.Un_Formal) is
2463 when N_Formal_Object_Declaration =>
2464 if Assoc.Actual.Kind = Box_Actual then
2465 Process_Box_Actual (Assoc.Un_Formal);
2466
2467 else
2468 Append_List
2469 (Instantiate_Object (Assoc.Un_Formal, Match, Assoc.An_Formal),
2470 Result_Renamings);
2471
2472 -- GNATprove: For a defaulted in-mode parameter, create
2473 -- an entry in the list of defaulted actuals, for
2474 -- GNATprove use. Do not include these defaults for an
2475 -- instance nested within a generic, because the defaults
2476 -- are also used in the analysis of the enclosing
2477 -- generic, and only defaulted subprograms are relevant
2478 -- there.
2479
2480 if No (Match) and then not Inside_A_Generic then
2481 Append_To (Default_Actuals,
2482 Make_Generic_Association (Sloc (I_Node),
2483 Selector_Name =>
2484 New_Occurrence_Of
2485 (Defining_Identifier
2486 (Assoc.Un_Formal), Sloc (I_Node)),
2487 Explicit_Generic_Actual_Parameter =>
2488 New_Copy_Tree (Default_Expression (Assoc.Un_Formal))));
2489 end if;
2490 end if;
2491
2492 -- If the object is a call to an expression function, this
2493 -- is a freezing point for it.
2494
2495 if Is_Entity_Name (Match)
2496 and then Present (Entity (Match))
2497 and then Nkind
2498 (Original_Node (Unit_Declaration_Node (Entity (Match))))
2499 = N_Expression_Function
2500 then
2501 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2502 end if;
2503
2504 when N_Formal_Type_Declaration =>
2505 if Assoc.Actual.Kind = Box_Actual then
2506 Process_Box_Actual (Assoc.Un_Formal);
2507
2508 elsif No (Match) then
2509 -- No explicit actual; try default
2510
2511 if Present (Default_Subtype_Mark (Assoc.Un_Formal)) then
2512 Match := New_Copy (Default_Subtype_Mark (Assoc.Un_Formal));
2513 Append_List
2514 (Instantiate_Type
2515 (Assoc.Un_Formal, Match, Assoc.An_Formal,
2516 Result_Renamings),
2517 Result_Renamings);
2518 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2519
2520 -- No explicit actual and no default; must be inference
2521
2522 else
2523 pragma Assert (Assoc.Actual_Origin = From_Inference);
2524
2525 Match := Assoc.Actual.Name_Exp;
2526 Append_List
2527 (Instantiate_Type
2528 (Assoc.Un_Formal,
2529 Match,
2530 Assoc.An_Formal,
2531 Result_Renamings),
2532 Result_Renamings);
2533 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2534 end if;
2535
2536 else
2537 Analyze (Match);
2538
2539 -- Rewrite mutably tagged types to be their class-wide
2540 -- equivalent type.
2541
2542 if Ekind (Etype (Match)) /= E_Void
2543 and then Is_Mutably_Tagged_Type (Etype (Match))
2544 then
2545 Rewrite (Match, New_Occurrence_Of
2546 (Class_Wide_Equivalent_Type
2547 (Etype (Match)), Sloc (Match)));
2548 Analyze (Match);
2549 end if;
2550
2551 Append_List
2552 (Instantiate_Type
2553 (Assoc.Un_Formal, Match, Assoc.An_Formal,
2554 Result_Renamings),
2555 Result_Renamings);
2556
2557 -- An instantiation is a freeze point for the actuals,
2558 -- unless this is a rewritten formal package, or the
2559 -- formal is an Ada 2012 formal incomplete type.
2560
2561 if Nkind (I_Node) = N_Formal_Package_Declaration
2562 or else
2563 (Ada_Version >= Ada_2012
2564 and then
2565 Ekind (Defining_Identifier (Assoc.An_Formal)) =
2566 E_Incomplete_Type)
2567 then
2568 null;
2569
2570 else
2571 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2572 end if;
2573 end if;
2574
2575 -- A remote access-to-class-wide type is not a legal actual
2576 -- for a generic formal of an access type (E.2.2(17/2)).
2577 -- In GNAT an exception to this rule is introduced when
2578 -- the formal is marked as remote using implementation
2579 -- defined aspect/pragma Remote_Access_Type. In that case
2580 -- the actual must be remote as well.
2581
2582 -- If the current instantiation is the construction of a
2583 -- local copy for a formal package the actuals may be
2584 -- defaulted, and there is no matching actual to check.
2585
2586 if Nkind (Assoc.An_Formal) = N_Formal_Type_Declaration
2587 and then
2588 Nkind (Formal_Type_Definition (Assoc.An_Formal)) =
2589 N_Access_To_Object_Definition
2590 and then Present (Match)
2591 then
2592 declare
2593 Formal_Ent : constant Entity_Id :=
2594 Defining_Identifier (Assoc.An_Formal);
2595 begin
2596 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
2597 = Is_Remote_Types (Formal_Ent)
2598 then
2599 -- Remoteness of formal and actual match
2600
2601 null;
2602
2603 elsif Is_Remote_Types (Formal_Ent) then
2604
2605 -- Remote formal, non-remote actual
2606
2607 Error_Msg_NE
2608 ("actual for& must be remote", Match, Formal_Ent);
2609
2610 else
2611 -- Non-remote formal, remote actual
2612
2613 Error_Msg_NE
2614 ("actual for& may not be remote",
2615 Match, Formal_Ent);
2616 end if;
2617 end;
2618 end if;
2619
2620 when N_Formal_Subprogram_Declaration =>
2621 -- If there is no corresponding actual, this may be case
2622 -- of partial parameterization, or else the formal has a
2623 -- default or a box.
2624
2625 if Assoc.Actual.Kind = Box_Actual then
2626 Process_Box_Actual (Assoc.Un_Formal);
2627
2628 else
2629 Append_To (Result_Renamings,
2630 Instantiate_Formal_Subprogram
2631 (Assoc.Un_Formal, Match, Assoc.An_Formal));
2632
2633 -- If formal subprogram has contracts, create wrappers
2634 -- for it. This is an expansion activity that cannot
2635 -- take place e.g. within an enclosing generic unit.
2636
2637 if Has_Contracts (Assoc.An_Formal)
2638 and then (Expander_Active or GNATprove_Mode)
2639 then
2640 Build_Subprogram_Wrappers
2641 (Match, Assoc.An_Formal, Result_Renamings);
2642 end if;
2643
2644 -- An instantiation is a freeze point for the actuals,
2645 -- unless this is a rewritten formal package.
2646
2647 if Nkind (I_Node) /= N_Formal_Package_Declaration
2648 and then Nkind (Match) = N_Identifier
2649 and then Is_Subprogram (Entity (Match))
2650
2651 -- The actual subprogram may rename a routine defined
2652 -- in Standard. Avoid freezing such renamings because
2653 -- subprograms coming from Standard cannot be frozen.
2654
2655 and then
2656 not Renames_Standard_Subprogram (Entity (Match))
2657
2658 -- If the actual subprogram comes from a different
2659 -- unit, it is already frozen, either by a body in
2660 -- that unit or by the end of the declarative part
2661 -- of the unit. This check avoids the freezing of
2662 -- subprograms defined in Standard which are used
2663 -- as generic actuals.
2664
2665 and then In_Same_Code_Unit (Entity (Match), I_Node)
2666 and then Has_Fully_Defined_Profile (Entity (Match))
2667 then
2668 -- Mark the subprogram as having a delayed freeze
2669 -- since this may be an out-of-order action.
2670
2671 Set_Has_Delayed_Freeze (Entity (Match));
2672 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2673 end if;
2674 end if;
2675
2676 -- If this is a nested generic, preserve default for later
2677 -- instantiations. We do this as well for GNATprove use,
2678 -- so that the list of generic associations is complete.
2679
2680 if No (Match) and then Box_Present (Assoc.Un_Formal) then
2681 declare
2682 Subp : constant Entity_Id :=
2683 Defining_Unit_Name
2684 (Specification (Last (Result_Renamings)));
2685
2686 begin
2687 Append_To (Default_Actuals,
2688 Make_Generic_Association (Sloc (I_Node),
2689 Selector_Name =>
2690 New_Occurrence_Of (Subp, Sloc (I_Node)),
2691 Explicit_Generic_Actual_Parameter =>
2692 New_Occurrence_Of (Subp, Sloc (I_Node))));
2693 end;
2694 end if;
2695
2696 when N_Formal_Package_Declaration =>
2697 if Assoc.Actual.Kind = Box_Actual then
2698 Process_Box_Actual (Assoc.Un_Formal);
2699
2700 else
2701 Analyze (Match);
2702 Append_List
2703 (Instantiate_Formal_Package
2704 (Assoc.Un_Formal, Match, Assoc.An_Formal),
2705 Result_Renamings);
2706
2707 -- Determine whether the actual package needs an explicit
2708 -- freeze node. This is only the case if the actual is
2709 -- declared in the same unit and has a body. Normally
2710 -- packages do not have explicit freeze nodes, and gigi
2711 -- only uses them to elaborate entities in a package
2712 -- body.
2713
2714 Explicit_Freeze_Check : declare
2715 Actual : constant Entity_Id := Entity (Match);
2716 Gen_Par : Entity_Id;
2717
2718 Needs_Freezing : Boolean;
2719 P : Node_Id;
2720
2721 procedure Check_Generic_Parent;
2722 -- The actual may be an instantiation of a unit
2723 -- declared in a previous instantiation. If that
2724 -- one is also in the current compilation, it must
2725 -- itself be frozen before the actual. The actual
2726 -- may be an instantiation of a generic child unit,
2727 -- in which case the same applies to the instance
2728 -- of the parent which must be frozen before the
2729 -- actual.
2730 -- Should this itself be recursive ???
2731
2732 --------------------------
2733 -- Check_Generic_Parent --
2734 --------------------------
2735
2736 procedure Check_Generic_Parent is
2737 Inst : constant Node_Id :=
2738 Get_Unit_Instantiation_Node (Actual);
2739 Par : Entity_Id;
2740
2741 begin
2742 Par := Empty;
2743
2744 if Nkind (Parent (Actual)) = N_Package_Specification
2745 then
2746 Par := Scope (Generic_Parent (Parent (Actual)));
2747
2748 if Is_Generic_Instance (Par) then
2749 null;
2750
2751 -- If the actual is a child generic unit, check
2752 -- whether the instantiation of the parent is
2753 -- also local and must also be frozen now. We
2754 -- must retrieve the instance node to locate the
2755 -- parent instance if any.
2756
2757 elsif Ekind (Par) = E_Generic_Package
2758 and then Is_Child_Unit (Gen_Par)
2759 and then Ekind (Scope (Gen_Par)) =
2760 E_Generic_Package
2761 then
2762 if Nkind (Inst) = N_Package_Instantiation
2763 and then Nkind (Name (Inst)) =
2764 N_Expanded_Name
2765 then
2766 -- Retrieve entity of parent instance
2767
2768 Par := Entity (Prefix (Name (Inst)));
2769 end if;
2770
2771 else
2772 Par := Empty;
2773 end if;
2774 end if;
2775
2776 if Present (Par)
2777 and then Is_Generic_Instance (Par)
2778 and then Scope (Par) = Current_Scope
2779 and then
2780 (No (Freeze_Node (Par))
2781 or else
2782 not Is_List_Member (Freeze_Node (Par)))
2783 then
2784 Set_Has_Delayed_Freeze (Par);
2785 Append_Elmt (Par, Actuals_To_Freeze);
2786 end if;
2787 end Check_Generic_Parent;
2788
2789 -- Start of processing for Explicit_Freeze_Check
2790
2791 begin
2792 if Present (Renamed_Entity (Actual)) then
2793 Gen_Par :=
2794 Generic_Parent (Specification
2795 (Unit_Declaration_Node
2796 (Renamed_Entity (Actual))));
2797 else
2798 Gen_Par :=
2799 Generic_Parent (Specification
2800 (Unit_Declaration_Node (Actual)));
2801 end if;
2802
2803 if not Expander_Active
2804 or else not Has_Completion (Actual)
2805 or else not In_Same_Source_Unit (I_Node, Actual)
2806 or else Is_Frozen (Actual)
2807 or else
2808 (Present (Renamed_Entity (Actual))
2809 and then
2810 not In_Same_Source_Unit
2811 (I_Node, (Renamed_Entity (Actual))))
2812 then
2813 null;
2814
2815 else
2816 -- Finally we want to exclude such freeze nodes
2817 -- from statement sequences, which freeze
2818 -- everything before them.
2819 -- Is this strictly necessary ???
2820
2821 Needs_Freezing := True;
2822
2823 P := Parent (I_Node);
2824 while Nkind (P) /= N_Compilation_Unit loop
2825 if Nkind (P) = N_Handled_Sequence_Of_Statements
2826 then
2827 Needs_Freezing := False;
2828 exit;
2829 end if;
2830
2831 P := Parent (P);
2832 end loop;
2833
2834 if Needs_Freezing then
2835 Check_Generic_Parent;
2836
2837 -- If the actual is a renaming of a proper
2838 -- instance of the formal package, indicate
2839 -- that it is the instance that must be frozen.
2840
2841 if Nkind (Parent (Actual)) =
2842 N_Package_Renaming_Declaration
2843 then
2844 Set_Has_Delayed_Freeze
2845 (Renamed_Entity (Actual));
2846 Append_Elmt
2847 (Renamed_Entity (Actual),
2848 Actuals_To_Freeze);
2849 else
2850 Set_Has_Delayed_Freeze (Actual);
2851 Append_Elmt (Actual, Actuals_To_Freeze);
2852 end if;
2853 end if;
2854 end if;
2855 end Explicit_Freeze_Check;
2856 end if;
2857
2858 when others =>
2859 raise Program_Error;
2860 end case;
2861
2862 -- Check for correct use of Ghost entities in generic
2863 -- instantiations (SPARK RM 6.9(10)).
2864
2865 Check_Ghost_Context_In_Generic_Association
2866 (Actual => Match,
2867 Formal => Defining_Entity (Assoc.An_Formal));
2868 end Analyze_One_Association;
2869
2870 -------------------------------
2871 -- Analyze_Formal_Array_Type --
2872 -------------------------------
2873
2874 procedure Analyze_Formal_Array_Type
2875 (T : in out Entity_Id;
2876 Def : Node_Id)
2877 is
2878 DSS : Node_Id;
2879
2880 begin
2881 -- Treated like a non-generic array declaration, with additional
2882 -- semantic checks.
2883
2884 Enter_Name (T);
2885
2886 if Nkind (Def) = N_Constrained_Array_Definition then
2887 DSS := First (Discrete_Subtype_Definitions (Def));
2888 while Present (DSS) loop
2889 if Nkind (DSS) in N_Subtype_Indication
2890 | N_Range
2891 | N_Attribute_Reference
2892 then
2893 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2894 end if;
2895
2896 Next (DSS);
2897 end loop;
2898 end if;
2899
2900 Array_Type_Declaration (T, Def);
2901 Set_Is_Generic_Type (Base_Type (T));
2902
2903 if Ekind (Component_Type (T)) = E_Incomplete_Type
2904 and then No (Full_View (Component_Type (T)))
2905 then
2906 Error_Msg_N ("premature usage of incomplete type", Def);
2907
2908 -- Check that range constraint is not allowed on the component type
2909 -- of a generic formal array type (AARM 12.5.3(3))
2910
2911 elsif Is_Internal (Component_Type (T))
2912 and then Present (Subtype_Indication (Component_Definition (Def)))
2913 and then Nkind (Original_Node
2914 (Subtype_Indication (Component_Definition (Def)))) =
2915 N_Subtype_Indication
2916 then
2917 Error_Msg_N
2918 ("in a formal, a subtype indication can only be "
2919 & "a subtype mark (RM 12.5.3(3))",
2920 Subtype_Indication (Component_Definition (Def)));
2921 end if;
2922
2923 end Analyze_Formal_Array_Type;
2924
2925 ---------------------------------------------
2926 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2927 ---------------------------------------------
2928
2929 -- As for other generic types, we create a valid type representation with
2930 -- legal but arbitrary attributes, whose values are never considered
2931 -- static. For all scalar types we introduce an anonymous base type, with
2932 -- the same attributes. We choose the corresponding integer type to be
2933 -- Standard_Integer.
2934 -- Here and in other similar routines, the Sloc of the generated internal
2935 -- type must be the same as the sloc of the defining identifier of the
2936 -- formal type declaration, to provide proper source navigation.
2937
2938 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2939 (T : Entity_Id;
2940 Def : Node_Id)
2941 is
2942 Loc : constant Source_Ptr := Sloc (Def);
2943
2944 Base : constant Entity_Id :=
2945 New_Internal_Entity
2946 (E_Decimal_Fixed_Point_Type,
2947 Current_Scope,
2948 Sloc (Defining_Identifier (Parent (Def))), 'G');
2949
2950 Int_Base : constant Entity_Id := Standard_Integer;
2951 Delta_Val : constant Ureal := Ureal_1;
2952 Digs_Val : constant Uint := Uint_6;
2953
2954 function Make_Dummy_Bound return Node_Id;
2955 -- Return a properly typed universal real literal to use as a bound
2956
2957 ----------------------
2958 -- Make_Dummy_Bound --
2959 ----------------------
2960
2961 function Make_Dummy_Bound return Node_Id is
2962 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2963 begin
2964 Set_Etype (Bound, Universal_Real);
2965 return Bound;
2966 end Make_Dummy_Bound;
2967
2968 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2969
2970 begin
2971 Enter_Name (T);
2972
2973 Set_Etype (Base, Base);
2974 Set_Size_Info (Base, Int_Base);
2975 Set_RM_Size (Base, RM_Size (Int_Base));
2976 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2977 Set_Digits_Value (Base, Digs_Val);
2978 Set_Delta_Value (Base, Delta_Val);
2979 Set_Small_Value (Base, Delta_Val);
2980 Set_Scalar_Range (Base,
2981 Make_Range (Loc,
2982 Low_Bound => Make_Dummy_Bound,
2983 High_Bound => Make_Dummy_Bound));
2984
2985 Set_Is_Generic_Type (Base);
2986 Set_Parent (Base, Parent (Def));
2987
2988 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2989 Set_Etype (T, Base);
2990 Set_Size_Info (T, Int_Base);
2991 Set_RM_Size (T, RM_Size (Int_Base));
2992 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2993 Set_Digits_Value (T, Digs_Val);
2994 Set_Delta_Value (T, Delta_Val);
2995 Set_Small_Value (T, Delta_Val);
2996 Set_Scalar_Range (T, Scalar_Range (Base));
2997 Set_Is_Constrained (T);
2998
2999 Check_Restriction (No_Fixed_Point, Def);
3000 end Analyze_Formal_Decimal_Fixed_Point_Type;
3001
3002 -------------------------------------------
3003 -- Analyze_Formal_Derived_Interface_Type --
3004 -------------------------------------------
3005
3006 procedure Analyze_Formal_Derived_Interface_Type
3007 (N : Node_Id;
3008 T : Entity_Id;
3009 Def : Node_Id)
3010 is
3011 Loc : constant Source_Ptr := Sloc (Def);
3012
3013 begin
3014 -- Rewrite as a type declaration of a derived type. This ensures that
3015 -- the interface list and primitive operations are properly captured.
3016
3017 Rewrite (N,
3018 Make_Full_Type_Declaration (Loc,
3019 Defining_Identifier => T,
3020 Type_Definition => Def));
3021
3022 -- Keep the aspects from the original node
3023
3024 Move_Aspects (Original_Node (N), N);
3025
3026 Analyze (N);
3027 Set_Is_Generic_Type (T);
3028 end Analyze_Formal_Derived_Interface_Type;
3029
3030 ---------------------------------
3031 -- Analyze_Formal_Derived_Type --
3032 ---------------------------------
3033
3034 procedure Analyze_Formal_Derived_Type
3035 (N : Node_Id;
3036 T : Entity_Id;
3037 Def : Node_Id)
3038 is
3039 Loc : constant Source_Ptr := Sloc (Def);
3040 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
3041 New_N : Node_Id;
3042
3043 begin
3044 Set_Is_Generic_Type (T);
3045
3046 if Private_Present (Def) then
3047 New_N :=
3048 Make_Private_Extension_Declaration (Loc,
3049 Defining_Identifier => T,
3050 Discriminant_Specifications => Discriminant_Specifications (N),
3051 Unknown_Discriminants_Present => Unk_Disc,
3052 Subtype_Indication => Subtype_Mark (Def),
3053 Interface_List => Interface_List (Def));
3054
3055 Set_Abstract_Present (New_N, Abstract_Present (Def));
3056 Set_Limited_Present (New_N, Limited_Present (Def));
3057 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
3058
3059 else
3060 New_N :=
3061 Make_Full_Type_Declaration (Loc,
3062 Defining_Identifier => T,
3063 Discriminant_Specifications =>
3064 Discriminant_Specifications (Parent (T)),
3065 Type_Definition =>
3066 Make_Derived_Type_Definition (Loc,
3067 Subtype_Indication => Subtype_Mark (Def)));
3068
3069 Set_Abstract_Present
3070 (Type_Definition (New_N), Abstract_Present (Def));
3071 Set_Limited_Present
3072 (Type_Definition (New_N), Limited_Present (Def));
3073 end if;
3074
3075 Rewrite (N, New_N);
3076
3077 -- Keep the aspects from the original node
3078
3079 Move_Aspects (Original_Node (N), N);
3080
3081 Analyze (N);
3082
3083 if Unk_Disc then
3084 if not Is_Composite_Type (T) then
3085 Error_Msg_N
3086 ("unknown discriminants not allowed for elementary types", N);
3087 else
3088 Set_Has_Unknown_Discriminants (T);
3089 Set_Is_Constrained (T, False);
3090 end if;
3091 end if;
3092
3093 if Subtype_Mark (Def) <= Empty_Or_Error then
3094 pragma Assert (Serious_Errors_Detected > 0);
3095 -- avoid passing bad argument to Entity
3096 return;
3097 end if;
3098
3099 -- If the parent type has a known size, so does the formal, which makes
3100 -- legal representation clauses that involve the formal.
3101
3102 Set_Size_Known_At_Compile_Time
3103 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
3104 end Analyze_Formal_Derived_Type;
3105
3106 ----------------------------------
3107 -- Analyze_Formal_Discrete_Type --
3108 ----------------------------------
3109
3110 -- The operations defined for a discrete types are those of an enumeration
3111 -- type. The size is set to an arbitrary value, for use in analyzing the
3112 -- generic unit.
3113
3114 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
3115 Loc : constant Source_Ptr := Sloc (Def);
3116 Lo : Node_Id;
3117 Hi : Node_Id;
3118
3119 Base : constant Entity_Id :=
3120 New_Internal_Entity
3121 (E_Floating_Point_Type, Current_Scope,
3122 Sloc (Defining_Identifier (Parent (Def))), 'G');
3123
3124 begin
3125 Enter_Name (T);
3126 Mutate_Ekind (T, E_Enumeration_Subtype);
3127 Set_Etype (T, Base);
3128 Init_Size (T, 8);
3129 Reinit_Alignment (T);
3130 Set_Is_Generic_Type (T);
3131 Set_Is_Constrained (T);
3132
3133 -- For semantic analysis, the bounds of the type must be set to some
3134 -- non-static value. The simplest is to create attribute nodes for those
3135 -- bounds, that refer to the type itself. These bounds are never
3136 -- analyzed but serve as place-holders.
3137
3138 Lo :=
3139 Make_Attribute_Reference (Loc,
3140 Attribute_Name => Name_First,
3141 Prefix => New_Occurrence_Of (T, Loc));
3142 Set_Etype (Lo, T);
3143
3144 Hi :=
3145 Make_Attribute_Reference (Loc,
3146 Attribute_Name => Name_Last,
3147 Prefix => New_Occurrence_Of (T, Loc));
3148 Set_Etype (Hi, T);
3149
3150 Set_Scalar_Range (T,
3151 Make_Range (Loc,
3152 Low_Bound => Lo,
3153 High_Bound => Hi));
3154
3155 Mutate_Ekind (Base, E_Enumeration_Type);
3156 Set_Etype (Base, Base);
3157 Init_Size (Base, 8);
3158 Reinit_Alignment (Base);
3159 Set_Is_Generic_Type (Base);
3160 Set_Scalar_Range (Base, Scalar_Range (T));
3161 Set_Parent (Base, Parent (Def));
3162 end Analyze_Formal_Discrete_Type;
3163
3164 ----------------------------------
3165 -- Analyze_Formal_Floating_Type --
3166 ---------------------------------
3167
3168 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
3169 Base : constant Entity_Id :=
3170 New_Internal_Entity
3171 (E_Floating_Point_Type, Current_Scope,
3172 Sloc (Defining_Identifier (Parent (Def))), 'G');
3173
3174 begin
3175 -- The various semantic attributes are taken from the predefined type
3176 -- Float, just so that all of them are initialized. Their values are
3177 -- never used because no constant folding or expansion takes place in
3178 -- the generic itself.
3179
3180 Enter_Name (T);
3181 Mutate_Ekind (T, E_Floating_Point_Subtype);
3182 Set_Etype (T, Base);
3183 Set_Size_Info (T, (Standard_Float));
3184 Set_RM_Size (T, RM_Size (Standard_Float));
3185 Set_Digits_Value (T, Digits_Value (Standard_Float));
3186 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
3187 Set_Is_Constrained (T);
3188
3189 Set_Is_Generic_Type (Base);
3190 Set_Etype (Base, Base);
3191 Set_Size_Info (Base, (Standard_Float));
3192 Set_RM_Size (Base, RM_Size (Standard_Float));
3193 Set_Digits_Value (Base, Digits_Value (Standard_Float));
3194 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
3195 Set_Parent (Base, Parent (Def));
3196
3197 Check_Restriction (No_Floating_Point, Def);
3198 end Analyze_Formal_Floating_Type;
3199
3200 -----------------------------------
3201 -- Analyze_Formal_Interface_Type;--
3202 -----------------------------------
3203
3204 procedure Analyze_Formal_Interface_Type
3205 (N : Node_Id;
3206 T : Entity_Id;
3207 Def : Node_Id)
3208 is
3209 Loc : constant Source_Ptr := Sloc (N);
3210 New_N : Node_Id;
3211
3212 begin
3213 New_N :=
3214 Make_Full_Type_Declaration (Loc,
3215 Defining_Identifier => T,
3216 Type_Definition => Def);
3217
3218 Rewrite (N, New_N);
3219
3220 -- Keep the aspects from the original node
3221
3222 Move_Aspects (Original_Node (N), N);
3223
3224 Analyze (N);
3225 Set_Is_Generic_Type (T);
3226 end Analyze_Formal_Interface_Type;
3227
3228 ---------------------------------
3229 -- Analyze_Formal_Modular_Type --
3230 ---------------------------------
3231
3232 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
3233 begin
3234 -- Apart from their entity kind, generic modular types are treated like
3235 -- signed integer types, and have the same attributes.
3236
3237 Analyze_Formal_Signed_Integer_Type (T, Def);
3238 Mutate_Ekind (T, E_Modular_Integer_Subtype);
3239 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
3240
3241 end Analyze_Formal_Modular_Type;
3242
3243 ---------------------------------------
3244 -- Analyze_Formal_Object_Declaration --
3245 ---------------------------------------
3246
3247 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
3248 E : constant Node_Id := Default_Expression (N);
3249 Id : constant Node_Id := Defining_Identifier (N);
3250
3251 K : Entity_Kind;
3252 Parent_Installed : Boolean := False;
3253 T : Node_Id;
3254
3255 begin
3256 Enter_Name (Id);
3257
3258 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3259
3260 -- Determine the mode of the formal object
3261
3262 if Out_Present (N) then
3263 K := E_Generic_In_Out_Parameter;
3264
3265 if not In_Present (N) then
3266 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
3267 end if;
3268
3269 else
3270 K := E_Generic_In_Parameter;
3271 end if;
3272
3273 if Present (Subtype_Mark (N)) then
3274 Find_Type (Subtype_Mark (N));
3275 T := Entity (Subtype_Mark (N));
3276
3277 -- Verify that there is no redundant null exclusion
3278
3279 if Null_Exclusion_Present (N) then
3280 if not Is_Access_Type (T) then
3281 Error_Msg_N
3282 ("null exclusion can only apply to an access type", N);
3283
3284 elsif Can_Never_Be_Null (T) then
3285 Error_Msg_NE
3286 ("`NOT NULL` not allowed (& already excludes null)", N, T);
3287 end if;
3288 end if;
3289
3290 -- Ada 2005 (AI-423): Formal object with an access definition
3291
3292 else
3293 Check_Access_Definition (N);
3294 T := Access_Definition
3295 (Related_Nod => N,
3296 N => Access_Definition (N));
3297 end if;
3298
3299 if Ekind (T) = E_Incomplete_Type then
3300 declare
3301 Error_Node : Node_Id;
3302
3303 begin
3304 if Present (Subtype_Mark (N)) then
3305 Error_Node := Subtype_Mark (N);
3306 else
3307 Check_Access_Definition (N);
3308 Error_Node := Access_Definition (N);
3309 end if;
3310
3311 Error_Msg_N ("premature usage of incomplete type", Error_Node);
3312 end;
3313 end if;
3314
3315 if K = E_Generic_In_Parameter then
3316
3317 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
3318
3319 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
3320 Error_Msg_N
3321 ("generic formal of mode IN must not be of limited type", N);
3322 Explain_Limited_Type (T, N);
3323 end if;
3324
3325 if Is_Abstract_Type (T) then
3326 Error_Msg_N
3327 ("generic formal of mode IN must not be of abstract type", N);
3328 end if;
3329
3330 if Present (E) then
3331 Preanalyze_Spec_Expression (E, T);
3332
3333 -- The default for a ghost generic formal IN parameter of
3334 -- access-to-variable type should be a ghost object (SPARK
3335 -- RM 6.9(13)).
3336
3337 if Is_Access_Variable (T) then
3338 Check_Ghost_Formal_Variable
3339 (Actual => E,
3340 Formal => Id,
3341 Is_Default => True);
3342 end if;
3343
3344 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
3345 Error_Msg_N
3346 ("initialization not allowed for limited types", E);
3347 Explain_Limited_Type (T, E);
3348 end if;
3349 end if;
3350
3351 Mutate_Ekind (Id, K);
3352 Set_Etype (Id, T);
3353
3354 -- Case of generic IN OUT parameter
3355
3356 else
3357 -- If the formal has an unconstrained type, construct its actual
3358 -- subtype, as is done for subprogram formals. In this fashion, all
3359 -- its uses can refer to specific bounds.
3360
3361 Mutate_Ekind (Id, K);
3362 Set_Etype (Id, T);
3363
3364 if (Is_Array_Type (T) and then not Is_Constrained (T))
3365 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
3366 then
3367 declare
3368 Non_Freezing_Ref : constant Node_Id :=
3369 New_Occurrence_Of (Id, Sloc (Id));
3370 Decl : Node_Id;
3371
3372 begin
3373 -- Make sure the actual subtype doesn't generate bogus freezing
3374
3375 Set_Must_Not_Freeze (Non_Freezing_Ref);
3376 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
3377 Insert_Before_And_Analyze (N, Decl);
3378 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
3379 end;
3380 else
3381 Set_Actual_Subtype (Id, T);
3382 end if;
3383
3384 if Present (E) then
3385 Error_Msg_N
3386 ("initialization not allowed for `IN OUT` formals", N);
3387 end if;
3388 end if;
3389
3390 Analyze_Aspect_Specifications (N, Id);
3391
3392 if Parent_Installed then
3393 Remove_Parent;
3394 end if;
3395 end Analyze_Formal_Object_Declaration;
3396
3397 ----------------------------------------------
3398 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
3399 ----------------------------------------------
3400
3401 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
3402 (T : Entity_Id;
3403 Def : Node_Id)
3404 is
3405 Loc : constant Source_Ptr := Sloc (Def);
3406 Base : constant Entity_Id :=
3407 New_Internal_Entity
3408 (E_Ordinary_Fixed_Point_Type, Current_Scope,
3409 Sloc (Defining_Identifier (Parent (Def))), 'G');
3410
3411 begin
3412 -- The semantic attributes are set for completeness only, their values
3413 -- will never be used, since all properties of the type are non-static.
3414
3415 Enter_Name (T);
3416 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
3417 Set_Etype (T, Base);
3418 Set_Size_Info (T, Standard_Integer);
3419 Set_RM_Size (T, RM_Size (Standard_Integer));
3420 Set_Small_Value (T, Ureal_1);
3421 Set_Delta_Value (T, Ureal_1);
3422 Set_Scalar_Range (T,
3423 Make_Range (Loc,
3424 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
3425 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
3426 Set_Is_Constrained (T);
3427
3428 Set_Is_Generic_Type (Base);
3429 Set_Etype (Base, Base);
3430 Set_Size_Info (Base, Standard_Integer);
3431 Set_RM_Size (Base, RM_Size (Standard_Integer));
3432 Set_Small_Value (Base, Ureal_1);
3433 Set_Delta_Value (Base, Ureal_1);
3434 Set_Scalar_Range (Base, Scalar_Range (T));
3435 Set_Parent (Base, Parent (Def));
3436
3437 Check_Restriction (No_Fixed_Point, Def);
3438 end Analyze_Formal_Ordinary_Fixed_Point_Type;
3439
3440 ----------------------------------------
3441 -- Analyze_Formal_Package_Declaration --
3442 ----------------------------------------
3443
3444 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
3445 Gen_Id : constant Node_Id := Name (N);
3446 Loc : constant Source_Ptr := Sloc (N);
3447 Pack_Id : constant Entity_Id := Defining_Identifier (N);
3448 Formal : Entity_Id;
3449 Gen_Decl : Node_Id;
3450 Gen_Unit : Entity_Id;
3451 Renaming : Node_Id;
3452
3453 Vis_Prims_List : Elist_Id := No_Elist;
3454 -- List of primitives made temporarily visible in the instantiation
3455 -- to match the visibility of the formal type.
3456
3457 function Build_Local_Package return Node_Id;
3458 -- The formal package is rewritten so that its parameters are replaced
3459 -- with corresponding declarations. For parameters with bona fide
3460 -- associations these declarations are created by Analyze_Associations
3461 -- as for a regular instantiation. For boxed parameters, we preserve
3462 -- the formal declarations and analyze them, in order to introduce
3463 -- entities of the right kind in the environment of the formal.
3464
3465 -------------------------
3466 -- Build_Local_Package --
3467 -------------------------
3468
3469 function Build_Local_Package return Node_Id is
3470 Decls : List_Id;
3471 Pack_Decl : Node_Id;
3472
3473 begin
3474 -- Within the formal, the name of the generic package is a renaming
3475 -- of the formal (as for a regular instantiation).
3476
3477 Pack_Decl :=
3478 Make_Package_Declaration (Loc,
3479 Specification =>
3480 Copy_Generic_Node
3481 (Specification (Original_Node (Gen_Decl)),
3482 Empty, Instantiating => True));
3483
3484 Renaming :=
3485 Make_Package_Renaming_Declaration (Loc,
3486 Defining_Unit_Name =>
3487 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3488 Name => New_Occurrence_Of (Formal, Loc));
3489
3490 if Nkind (Gen_Id) = N_Identifier
3491 and then Chars (Gen_Id) = Chars (Pack_Id)
3492 then
3493 Error_Msg_NE
3494 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3495 end if;
3496
3497 -- If the formal is declared with a box, or with an others choice,
3498 -- create corresponding declarations for all entities in the formal
3499 -- part, so that names with the proper types are available in the
3500 -- specification of the formal package.
3501
3502 -- On the other hand, if there are no associations (as in "new G;"),
3503 -- then all the formals must have defaults, and this will be checked
3504 -- by the call to Analyze_Associations.
3505
3506 if Box_Present (N)
3507 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3508 then
3509 declare
3510 Formal_Decl : Node_Id;
3511
3512 begin
3513 -- TBA : for a formal package, need to recurse ???
3514
3515 Decls := New_List;
3516 Formal_Decl :=
3517 First
3518 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
3519 while Present (Formal_Decl) loop
3520 Append_To
3521 (Decls,
3522 Copy_Generic_Node
3523 (Formal_Decl, Empty, Instantiating => True));
3524 Next (Formal_Decl);
3525 end loop;
3526 end;
3527
3528 -- If generic associations are present, use Analyze_Associations to
3529 -- create the proper renaming declarations.
3530
3531 else
3532 declare
3533 Act_Tree : constant Node_Id :=
3534 Copy_Generic_Node
3535 (Original_Node (Gen_Decl), Empty,
3536 Instantiating => True);
3537
3538 begin
3539 Generic_Renamings.Set_Last (0);
3540 Generic_Renamings_HTable.Reset;
3541 Instantiation_Node := N;
3542
3543 Decls :=
3544 Analyze_Associations
3545 (I_Node => Original_Node (N),
3546 Formals => Generic_Formal_Declarations (Act_Tree),
3547 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3548
3549 Vis_Prims_List := Check_Hidden_Primitives (Decls);
3550 end;
3551 end if;
3552
3553 Append (Renaming, To => Decls);
3554
3555 -- Add generated declarations ahead of local declarations in
3556 -- the package.
3557
3558 if No (Visible_Declarations (Specification (Pack_Decl))) then
3559 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
3560 else
3561 Insert_List_Before
3562 (First (Visible_Declarations (Specification (Pack_Decl))),
3563 Decls);
3564 end if;
3565
3566 return Pack_Decl;
3567 end Build_Local_Package;
3568
3569 -- Local variables
3570
3571 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
3572 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
3573
3574 Associations : Boolean := True;
3575 New_N : Node_Id;
3576 Parent_Installed : Boolean := False;
3577 Parent_Instance : Entity_Id;
3578 Renaming_In_Par : Entity_Id;
3579
3580 -- Start of processing for Analyze_Formal_Package_Declaration
3581
3582 begin
3583 Check_Text_IO_Special_Unit (Gen_Id);
3584
3585 Init_Env;
3586 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3587 Gen_Unit := Entity (Gen_Id);
3588
3589 -- Check for a formal package that is a package renaming
3590
3591 if Present (Renamed_Entity (Gen_Unit)) then
3592
3593 -- Indicate that unit is used, before replacing it with renamed
3594 -- entity for use below.
3595
3596 if In_Extended_Main_Source_Unit (N) then
3597 Set_Is_Instantiated (Gen_Unit);
3598 Generate_Reference (Gen_Unit, N);
3599 end if;
3600
3601 Gen_Unit := Renamed_Entity (Gen_Unit);
3602 end if;
3603
3604 if Ekind (Gen_Unit) /= E_Generic_Package then
3605 Error_Msg_N ("expect generic package name", Gen_Id);
3606 Restore_Env;
3607 goto Leave;
3608
3609 elsif Gen_Unit = Current_Scope then
3610 Error_Msg_N
3611 ("generic package cannot be used as a formal package of itself",
3612 Gen_Id);
3613 Restore_Env;
3614 goto Leave;
3615
3616 elsif In_Open_Scopes (Gen_Unit) then
3617 if Is_Compilation_Unit (Gen_Unit)
3618 and then Is_Child_Unit (Current_Scope)
3619 then
3620 -- Special-case the error when the formal is a parent, and
3621 -- continue analysis to minimize cascaded errors.
3622
3623 Error_Msg_N
3624 ("generic parent cannot be used as formal package of a child "
3625 & "unit", Gen_Id);
3626
3627 else
3628 Error_Msg_N
3629 ("generic package cannot be used as a formal package within "
3630 & "itself", Gen_Id);
3631 Restore_Env;
3632 goto Leave;
3633 end if;
3634 end if;
3635
3636 -- Check that name of formal package does not hide name of generic,
3637 -- or its leading prefix. This check must be done separately because
3638 -- the name of the generic has already been analyzed.
3639
3640 declare
3641 Gen_Name : Entity_Id;
3642
3643 begin
3644 Gen_Name := Gen_Id;
3645 while Nkind (Gen_Name) = N_Expanded_Name loop
3646 Gen_Name := Prefix (Gen_Name);
3647 end loop;
3648
3649 if Chars (Gen_Name) = Chars (Pack_Id) then
3650 Error_Msg_NE
3651 ("& is hidden within declaration of formal package",
3652 Gen_Id, Gen_Name);
3653 end if;
3654 end;
3655
3656 if Box_Present (N)
3657 or else No (Generic_Associations (N))
3658 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3659 then
3660 Associations := False;
3661 end if;
3662
3663 -- If there are no generic associations, the generic parameters appear
3664 -- as local entities and are instantiated like them. We copy the generic
3665 -- package declaration as if it were an instantiation, and analyze it
3666 -- like a regular package, except that we treat the formals as
3667 -- additional visible components.
3668
3669 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3670
3671 if In_Extended_Main_Source_Unit (N) then
3672 Set_Is_Instantiated (Gen_Unit);
3673 Generate_Reference (Gen_Unit, N);
3674 end if;
3675
3676 Formal := New_Copy (Pack_Id);
3677 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3678
3679 -- Make local generic without formals. The formals will be replaced with
3680 -- internal declarations.
3681
3682 begin
3683 New_N := Build_Local_Package;
3684
3685 -- If there are errors in the parameter list, Analyze_Associations
3686 -- raises Instantiation_Error. Patch the declaration to prevent further
3687 -- exception propagation.
3688
3689 exception
3690 when Instantiation_Error =>
3691 Enter_Name (Formal);
3692 Mutate_Ekind (Formal, E_Variable);
3693 Set_Etype (Formal, Any_Type);
3694 Restore_Hidden_Primitives (Vis_Prims_List);
3695
3696 if Parent_Installed then
3697 Remove_Parent;
3698 end if;
3699
3700 goto Leave;
3701 end;
3702
3703 Rewrite (N, New_N);
3704 Set_Defining_Unit_Name (Specification (New_N), Formal);
3705 Set_Generic_Parent (Specification (N), Gen_Unit);
3706 Set_Instance_Env (Gen_Unit, Formal);
3707 Set_Is_Generic_Instance (Formal);
3708
3709 Enter_Name (Formal);
3710 Mutate_Ekind (Formal, E_Package);
3711 Set_Etype (Formal, Standard_Void_Type);
3712 Set_Inner_Instances (Formal, New_Elmt_List);
3713
3714 -- It is unclear that any aspects can apply to a formal package
3715 -- declaration, given that they look like a hidden conformance
3716 -- requirement on the corresponding actual. However, Abstract_State
3717 -- must be treated specially because it generates declarations that
3718 -- must appear before other declarations in the specification and
3719 -- must be analyzed at once.
3720
3721 if Present (Aspect_Specifications (Gen_Decl)) then
3722 if No (Aspect_Specifications (N)) then
3723 Set_Aspect_Specifications (N, New_List);
3724 end if;
3725
3726 declare
3727 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3728 New_A : Node_Id;
3729
3730 begin
3731 while Present (ASN) loop
3732 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3733 New_A :=
3734 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3735 Set_Entity (New_A, Formal);
3736 Set_Analyzed (New_A, False);
3737 Append (New_A, Aspect_Specifications (N));
3738 Analyze_Aspect_Specifications (N, Formal);
3739 exit;
3740 end if;
3741
3742 Next (ASN);
3743 end loop;
3744 end;
3745 end if;
3746
3747 Push_Scope (Formal);
3748
3749 -- Manually set the SPARK_Mode from the context because the package
3750 -- declaration is never analyzed.
3751
3752 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3753 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3754 Set_SPARK_Pragma_Inherited (Formal);
3755 Set_SPARK_Aux_Pragma_Inherited (Formal);
3756
3757 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3758
3759 -- Similarly, we have to make the name of the formal visible in the
3760 -- parent instance, to resolve properly fully qualified names that
3761 -- may appear in the generic unit. The parent instance has been
3762 -- placed on the scope stack ahead of the current scope.
3763
3764 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3765
3766 Renaming_In_Par :=
3767 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3768 Mutate_Ekind (Renaming_In_Par, E_Package);
3769 Set_Is_Not_Self_Hidden (Renaming_In_Par);
3770 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3771 Set_Scope (Renaming_In_Par, Parent_Instance);
3772 Set_Parent (Renaming_In_Par, Parent (Formal));
3773 Set_Renamed_Entity (Renaming_In_Par, Formal);
3774 Append_Entity (Renaming_In_Par, Parent_Instance);
3775 end if;
3776
3777 -- A formal package declaration behaves as a package instantiation with
3778 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3779 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3780 -- all SPARK_Mode pragmas within the generic_package_name.
3781
3782 if SPARK_Mode /= On then
3783 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3784
3785 -- Mark the formal spec in case the body is instantiated at a later
3786 -- pass. This preserves the original context in effect for the body.
3787
3788 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3789 end if;
3790
3791 Analyze (Specification (N));
3792
3793 -- The formals for which associations are provided are not visible
3794 -- outside of the formal package. The others are still declared by a
3795 -- formal parameter declaration.
3796
3797 -- If there are no associations, the only local entity to hide is the
3798 -- generated package renaming itself.
3799
3800 declare
3801 E : Entity_Id;
3802
3803 begin
3804 E := First_Entity (Formal);
3805 while Present (E) loop
3806 if Associations and then not Is_Generic_Formal (E) then
3807 Set_Is_Hidden (E);
3808 end if;
3809
3810 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3811 Set_Is_Hidden (E);
3812 exit;
3813 end if;
3814
3815 Next_Entity (E);
3816 end loop;
3817 end;
3818
3819 End_Package_Scope (Formal);
3820 Restore_Hidden_Primitives (Vis_Prims_List);
3821
3822 if Parent_Installed then
3823 Remove_Parent;
3824 end if;
3825
3826 Restore_Env;
3827
3828 -- Inside the generic unit, the formal package is a regular package, but
3829 -- no body is needed for it. Note that after instantiation, the defining
3830 -- unit name we need is in the new tree and not in the original (see
3831 -- Package_Instantiation). A generic formal package is an instance, and
3832 -- can be used as an actual for an inner instance.
3833
3834 Set_Has_Completion (Formal, True);
3835
3836 -- Add semantic information to the original defining identifier.
3837
3838 Mutate_Ekind (Pack_Id, E_Package);
3839 Set_Etype (Pack_Id, Standard_Void_Type);
3840 Set_Scope (Pack_Id, Scope (Formal));
3841 Set_Has_Completion (Pack_Id, True);
3842
3843 <<Leave>>
3844 -- Unclear that any other aspects may appear here, analyze them
3845 -- for completion, given that the grammar allows their appearance.
3846
3847 Analyze_Aspect_Specifications (N, Pack_Id);
3848
3849 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3850 end Analyze_Formal_Package_Declaration;
3851
3852 ---------------------------------
3853 -- Analyze_Formal_Private_Type --
3854 ---------------------------------
3855
3856 procedure Analyze_Formal_Private_Type
3857 (N : Node_Id;
3858 T : Entity_Id;
3859 Def : Node_Id)
3860 is
3861 begin
3862 New_Private_Type (N, T, Def);
3863
3864 -- Set the size to an arbitrary but legal value
3865
3866 Set_Size_Info (T, Standard_Integer);
3867 Set_RM_Size (T, RM_Size (Standard_Integer));
3868 end Analyze_Formal_Private_Type;
3869
3870 ------------------------------------
3871 -- Analyze_Formal_Incomplete_Type --
3872 ------------------------------------
3873
3874 procedure Analyze_Formal_Incomplete_Type
3875 (T : Entity_Id;
3876 Def : Node_Id)
3877 is
3878 begin
3879 Enter_Name (T);
3880 Mutate_Ekind (T, E_Incomplete_Type);
3881 Set_Etype (T, T);
3882 Set_Private_Dependents (T, New_Elmt_List);
3883
3884 if Tagged_Present (Def) then
3885 Set_Is_Tagged_Type (T);
3886 Make_Class_Wide_Type (T);
3887 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3888 end if;
3889 end Analyze_Formal_Incomplete_Type;
3890
3891 ----------------------------------------
3892 -- Analyze_Formal_Signed_Integer_Type --
3893 ----------------------------------------
3894
3895 procedure Analyze_Formal_Signed_Integer_Type
3896 (T : Entity_Id;
3897 Def : Node_Id)
3898 is
3899 Base : constant Entity_Id :=
3900 New_Internal_Entity
3901 (E_Signed_Integer_Type,
3902 Current_Scope,
3903 Sloc (Defining_Identifier (Parent (Def))), 'G');
3904
3905 begin
3906 Enter_Name (T);
3907
3908 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3909 Set_Etype (T, Base);
3910 Set_Size_Info (T, Standard_Integer);
3911 Set_RM_Size (T, RM_Size (Standard_Integer));
3912 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3913 Set_Is_Constrained (T);
3914
3915 Set_Is_Generic_Type (Base);
3916 Set_Size_Info (Base, Standard_Integer);
3917 Set_RM_Size (Base, RM_Size (Standard_Integer));
3918 Set_Etype (Base, Base);
3919 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3920 Set_Parent (Base, Parent (Def));
3921 end Analyze_Formal_Signed_Integer_Type;
3922
3923 -------------------------------------------
3924 -- Analyze_Formal_Subprogram_Declaration --
3925 -------------------------------------------
3926
3927 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3928 Spec : constant Node_Id := Specification (N);
3929 Def : constant Node_Id := Default_Name (N);
3930 Expr : constant Node_Id := Expression (N);
3931 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3932
3933 Parent_Installed : Boolean := False;
3934 Subp : Entity_Id;
3935
3936 begin
3937 if Nam = Error then
3938 return;
3939 end if;
3940
3941 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3942 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3943 goto Leave;
3944 end if;
3945
3946 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3947
3948 Analyze_Subprogram_Declaration (N);
3949 Set_Is_Formal_Subprogram (Nam);
3950 Set_Has_Completion (Nam);
3951
3952 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3953 Set_Is_Abstract_Subprogram (Nam);
3954
3955 Set_Is_Dispatching_Operation (Nam);
3956
3957 -- A formal abstract procedure cannot have a null default
3958 -- (RM 12.6(4.1/2)).
3959
3960 if Has_Null_Default (N) then
3961 Error_Msg_N
3962 ("a formal abstract subprogram cannot default to null", Spec);
3963 end if;
3964
3965 -- A formal abstract function cannot have an expression default
3966 -- (expression defaults are allowed for nonabstract formal functions
3967 -- when extensions are enabled).
3968
3969 if Nkind (Spec) = N_Function_Specification
3970 and then Present (Expr)
3971 then
3972 Error_Msg_N
3973 ("a formal abstract subprogram cannot default to an expression",
3974 Spec);
3975 end if;
3976
3977 declare
3978 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3979 begin
3980 if No (Ctrl_Type) then
3981 Error_Msg_N
3982 ("abstract formal subprogram must have a controlling type",
3983 N);
3984
3985 elsif Ada_Version >= Ada_2012
3986 and then Is_Incomplete_Type (Ctrl_Type)
3987 then
3988 Error_Msg_NE
3989 ("controlling type of abstract formal subprogram cannot "
3990 & "be incomplete type", N, Ctrl_Type);
3991
3992 else
3993 Check_Controlling_Formals (Ctrl_Type, Nam);
3994 end if;
3995 end;
3996 end if;
3997
3998 -- Default name is resolved at the point of instantiation
3999
4000 if Box_Present (N) then
4001 null;
4002
4003 -- Default name is bound at the point of generic declaration
4004
4005 elsif Present (Def) then
4006 if Nkind (Def) = N_Operator_Symbol then
4007 Find_Direct_Name (Def);
4008
4009 elsif Nkind (Def) /= N_Attribute_Reference then
4010 Analyze (Def);
4011
4012 else
4013 -- For an attribute reference, analyze the prefix and verify
4014 -- that it has the proper profile for the subprogram.
4015
4016 Analyze (Prefix (Def));
4017 Valid_Default_Attribute (Nam, Def);
4018 goto Leave;
4019 end if;
4020
4021 -- The default for a ghost generic formal procedure should be a ghost
4022 -- procedure (SPARK RM 6.9(13)).
4023
4024 if Ekind (Nam) = E_Procedure then
4025 declare
4026 Def_E : Entity_Id := Empty;
4027 begin
4028 if Nkind (Def) in N_Has_Entity then
4029 Def_E := Entity (Def);
4030 end if;
4031
4032 Check_Ghost_Formal_Procedure_Or_Package
4033 (N => Def,
4034 Actual => Def_E,
4035 Formal => Nam,
4036 Is_Default => True);
4037 end;
4038 end if;
4039
4040 -- Default name may be overloaded, in which case the interpretation
4041 -- with the correct profile must be selected, as for a renaming.
4042 -- If the definition is an indexed component, it must denote a
4043 -- member of an entry family. If it is a selected component, it
4044 -- can be a protected operation.
4045
4046 if Etype (Def) = Any_Type then
4047 goto Leave;
4048
4049 elsif Nkind (Def) = N_Selected_Component then
4050 if not Is_Overloadable (Entity (Selector_Name (Def))) then
4051 Error_Msg_N ("expect valid subprogram name as default", Def);
4052 end if;
4053
4054 elsif Nkind (Def) = N_Indexed_Component then
4055 if Is_Entity_Name (Prefix (Def)) then
4056 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
4057 Error_Msg_N ("expect valid subprogram name as default", Def);
4058 end if;
4059
4060 elsif Nkind (Prefix (Def)) = N_Selected_Component then
4061 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
4062 E_Entry_Family
4063 then
4064 Error_Msg_N ("expect valid subprogram name as default", Def);
4065 end if;
4066
4067 else
4068 Error_Msg_N ("expect valid subprogram name as default", Def);
4069 goto Leave;
4070 end if;
4071
4072 elsif Nkind (Def) = N_Character_Literal then
4073
4074 -- Needs some type checks: subprogram should be parameterless???
4075
4076 Resolve (Def, (Etype (Nam)));
4077
4078 elsif not Is_Entity_Name (Def)
4079 or else not Is_Overloadable (Entity (Def))
4080 then
4081 Error_Msg_N ("expect valid subprogram name as default", Def);
4082 goto Leave;
4083
4084 elsif not Is_Overloaded (Def) then
4085 Subp := Entity (Def);
4086
4087 if Subp = Nam then
4088 Error_Msg_N ("premature usage of formal subprogram", Def);
4089
4090 elsif not Entity_Matches_Spec (Subp, Nam) then
4091 Error_Msg_N ("no visible entity matches specification", Def);
4092 end if;
4093
4094 -- More than one interpretation, so disambiguate as for a renaming
4095
4096 else
4097 declare
4098 I : Interp_Index;
4099 I1 : Interp_Index := 0;
4100 It : Interp;
4101 It1 : Interp;
4102
4103 begin
4104 Subp := Any_Id;
4105 Get_First_Interp (Def, I, It);
4106 while Present (It.Nam) loop
4107 if Entity_Matches_Spec (It.Nam, Nam) then
4108 if Subp /= Any_Id then
4109 It1 := Disambiguate (Def, I1, I, Etype (Subp));
4110
4111 if It1 = No_Interp then
4112 Error_Msg_N ("ambiguous default subprogram", Def);
4113 else
4114 Subp := It1.Nam;
4115 end if;
4116
4117 exit;
4118
4119 else
4120 I1 := I;
4121 Subp := It.Nam;
4122 end if;
4123 end if;
4124
4125 Get_Next_Interp (I, It);
4126 end loop;
4127 end;
4128
4129 if Subp /= Any_Id then
4130
4131 -- Subprogram found, generate reference to it
4132
4133 Set_Entity (Def, Subp);
4134 Generate_Reference (Subp, Def);
4135
4136 if Subp = Nam then
4137 Error_Msg_N ("premature usage of formal subprogram", Def);
4138
4139 elsif Ekind (Subp) /= E_Operator then
4140 Check_Mode_Conformant (Subp, Nam);
4141 end if;
4142
4143 else
4144 Error_Msg_N ("no visible subprogram matches specification", N);
4145 end if;
4146 end if;
4147
4148 -- When extensions are enabled, an expression can be given as default
4149 -- for a formal function. The expression must be of the function result
4150 -- type and can reference formal parameters of the function.
4151
4152 elsif Present (Expr) then
4153 Push_Scope (Nam);
4154 Install_Formals (Nam);
4155 Preanalyze_Spec_Expression (Expr, Etype (Nam));
4156 End_Scope;
4157 end if;
4158
4159 <<Leave>>
4160 Analyze_Aspect_Specifications (N, Nam);
4161
4162 if Parent_Installed then
4163 Remove_Parent;
4164 end if;
4165 end Analyze_Formal_Subprogram_Declaration;
4166
4167 -------------------------------------
4168 -- Analyze_Formal_Type_Declaration --
4169 -------------------------------------
4170
4171 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
4172 Def : constant Node_Id := Formal_Type_Definition (N);
4173
4174 Parent_Installed : Boolean := False;
4175 T : Entity_Id;
4176
4177 begin
4178 T := Defining_Identifier (N);
4179
4180 if Present (Discriminant_Specifications (N))
4181 and then Nkind (Def) /= N_Formal_Private_Type_Definition
4182 then
4183 Error_Msg_N
4184 ("discriminants not allowed for this formal type", T);
4185 end if;
4186
4187 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
4188
4189 -- Enter the new name, and branch to specific routine
4190
4191 case Nkind (Def) is
4192 when N_Formal_Private_Type_Definition =>
4193 Analyze_Formal_Private_Type (N, T, Def);
4194
4195 when N_Formal_Derived_Type_Definition =>
4196 Analyze_Formal_Derived_Type (N, T, Def);
4197
4198 when N_Formal_Incomplete_Type_Definition =>
4199 Analyze_Formal_Incomplete_Type (T, Def);
4200
4201 when N_Formal_Discrete_Type_Definition =>
4202 Analyze_Formal_Discrete_Type (T, Def);
4203
4204 when N_Formal_Signed_Integer_Type_Definition =>
4205 Analyze_Formal_Signed_Integer_Type (T, Def);
4206
4207 when N_Formal_Modular_Type_Definition =>
4208 Analyze_Formal_Modular_Type (T, Def);
4209
4210 when N_Formal_Floating_Point_Definition =>
4211 Analyze_Formal_Floating_Type (T, Def);
4212
4213 when N_Formal_Ordinary_Fixed_Point_Definition =>
4214 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
4215
4216 when N_Formal_Decimal_Fixed_Point_Definition =>
4217 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
4218
4219 when N_Array_Type_Definition =>
4220 Analyze_Formal_Array_Type (T, Def);
4221
4222 when N_Access_Function_Definition
4223 | N_Access_Procedure_Definition
4224 | N_Access_To_Object_Definition
4225 =>
4226 Analyze_Generic_Access_Type (T, Def);
4227
4228 -- Ada 2005: a interface declaration is encoded as an abstract
4229 -- record declaration or a abstract type derivation.
4230
4231 when N_Record_Definition =>
4232 Analyze_Formal_Interface_Type (N, T, Def);
4233
4234 when N_Derived_Type_Definition =>
4235 Analyze_Formal_Derived_Interface_Type (N, T, Def);
4236
4237 when N_Error =>
4238 null;
4239
4240 when others =>
4241 raise Program_Error;
4242 end case;
4243
4244 -- A formal type declaration declares a type and its first
4245 -- subtype.
4246
4247 Set_Is_Generic_Type (T);
4248 Set_Is_First_Subtype (T);
4249
4250 if Present (Default_Subtype_Mark (Original_Node (N))) then
4251 Validate_Formal_Type_Default (N);
4252 end if;
4253
4254 Analyze_Aspect_Specifications (N, T);
4255
4256 if Parent_Installed then
4257 Remove_Parent;
4258 end if;
4259 end Analyze_Formal_Type_Declaration;
4260
4261 ------------------------------------
4262 -- Analyze_Function_Instantiation --
4263 ------------------------------------
4264
4265 procedure Analyze_Function_Instantiation (N : Node_Id) is
4266 begin
4267 Analyze_Subprogram_Instantiation (N, E_Function);
4268 end Analyze_Function_Instantiation;
4269
4270 ---------------------------------
4271 -- Analyze_Generic_Access_Type --
4272 ---------------------------------
4273
4274 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
4275 begin
4276 Enter_Name (T);
4277
4278 if Nkind (Def) = N_Access_To_Object_Definition then
4279 Access_Type_Declaration (T, Def);
4280
4281 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
4282 and then No (Full_View (Designated_Type (T)))
4283 and then not Is_Generic_Type (Designated_Type (T))
4284 then
4285 Error_Msg_N ("premature usage of incomplete type", Def);
4286
4287 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
4288 Error_Msg_N
4289 ("only a subtype mark is allowed in a formal", Def);
4290 end if;
4291
4292 else
4293 Access_Subprogram_Declaration (T, Def);
4294 end if;
4295 end Analyze_Generic_Access_Type;
4296
4297 ---------------------------------
4298 -- Analyze_Generic_Formal_Part --
4299 ---------------------------------
4300
4301 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
4302 Gen_Parm_Decl : Node_Id;
4303
4304 begin
4305 -- The generic formals are processed in the scope of the generic unit,
4306 -- where they are immediately visible. The scope is installed by the
4307 -- caller.
4308
4309 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
4310 while Present (Gen_Parm_Decl) loop
4311 Analyze (Gen_Parm_Decl);
4312 Next (Gen_Parm_Decl);
4313 end loop;
4314
4315 Generate_Reference_To_Generic_Formals (Current_Scope);
4316
4317 -- For Ada 2022, some formal parameters can carry aspects, which must
4318 -- be name-resolved at the end of the list of formal parameters (which
4319 -- has the semantics of a declaration list).
4320
4321 Analyze_Contracts (Generic_Formal_Declarations (N));
4322 end Analyze_Generic_Formal_Part;
4323
4324 ------------------------------------------
4325 -- Analyze_Generic_Package_Declaration --
4326 ------------------------------------------
4327
4328 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
4329 Decls : constant List_Id := Visible_Declarations (Specification (N));
4330 Loc : constant Source_Ptr := Sloc (N);
4331
4332 Decl : Node_Id;
4333 Id : Entity_Id;
4334 New_N : Node_Id;
4335 Renaming : Node_Id;
4336 Save_Parent : Node_Id;
4337
4338 begin
4339 -- A generic may grant access to its private enclosing context depending
4340 -- on the placement of its corresponding body. From elaboration point of
4341 -- view, the flow of execution may enter this private context, and then
4342 -- reach an external unit, thus producing a dependency on that external
4343 -- unit. For such a path to be properly discovered and encoded in the
4344 -- ALI file of the main unit, let the ABE mechanism process the body of
4345 -- the main unit, and encode all relevant invocation constructs and the
4346 -- relations between them.
4347
4348 Mark_Save_Invocation_Graph_Of_Body;
4349
4350 -- We introduce a renaming of the enclosing package, to have a usable
4351 -- entity as the prefix of an expanded name for a local entity of the
4352 -- form Par.P.Q, where P is the generic package. This is because a local
4353 -- entity named P may hide it, so that the usual visibility rules in
4354 -- the instance will not resolve properly.
4355
4356 Renaming :=
4357 Make_Package_Renaming_Declaration (Loc,
4358 Defining_Unit_Name =>
4359 Make_Defining_Identifier (Loc,
4360 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
4361 Name =>
4362 Make_Identifier (Loc, Chars (Defining_Entity (N))));
4363
4364 -- The declaration is inserted before other declarations, but before
4365 -- pragmas that may be library-unit pragmas and must appear before other
4366 -- declarations. The pragma Compile_Time_Error is not in this class, and
4367 -- may contain an expression that includes such a qualified name, so the
4368 -- renaming declaration must appear before it.
4369
4370 -- Are there other pragmas that require this special handling ???
4371
4372 if Present (Decls) then
4373 Decl := First (Decls);
4374 while Present (Decl)
4375 and then Nkind (Decl) = N_Pragma
4376 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
4377 loop
4378 Next (Decl);
4379 end loop;
4380
4381 if Present (Decl) then
4382 Insert_Before (Decl, Renaming);
4383 else
4384 Append (Renaming, Visible_Declarations (Specification (N)));
4385 end if;
4386
4387 else
4388 Set_Visible_Declarations (Specification (N), New_List (Renaming));
4389 end if;
4390
4391 -- Create copy of generic unit, and save for instantiation. If the unit
4392 -- is a child unit, do not copy the specifications for the parent, which
4393 -- are not part of the generic tree.
4394
4395 Save_Parent := Parent_Spec (N);
4396 Set_Parent_Spec (N, Empty);
4397
4398 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
4399 Set_Parent_Spec (New_N, Save_Parent);
4400 Rewrite (N, New_N);
4401
4402 -- Collect all contract-related source pragmas found within the template
4403 -- and attach them to the contract of the package spec. This contract is
4404 -- used in the capture of global references within annotations.
4405
4406 Create_Generic_Contract (N);
4407
4408 Id := Defining_Entity (N);
4409 Generate_Definition (Id);
4410
4411 -- Expansion is not applied to generic units
4412
4413 Start_Generic;
4414
4415 Enter_Name (Id);
4416 Mutate_Ekind (Id, E_Generic_Package);
4417 Set_Is_Not_Self_Hidden (Id);
4418 Set_Etype (Id, Standard_Void_Type);
4419
4420 -- Set SPARK_Mode from context
4421
4422 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4423 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
4424 Set_SPARK_Pragma_Inherited (Id);
4425 Set_SPARK_Aux_Pragma_Inherited (Id);
4426
4427 -- Preserve relevant elaboration-related attributes of the context which
4428 -- are no longer available or very expensive to recompute once analysis,
4429 -- resolution, and expansion are over.
4430
4431 Mark_Elaboration_Attributes
4432 (N_Id => Id,
4433 Checks => True,
4434 Warnings => True);
4435
4436 -- Analyze aspects now, so that generated pragmas appear in the
4437 -- declarations before building and analyzing the generic copy.
4438
4439 Analyze_Aspect_Specifications (N, Id);
4440
4441 Push_Scope (Id);
4442 Enter_Generic_Scope (Id);
4443 Set_Inner_Instances (Id, New_Elmt_List);
4444
4445 Set_Categorization_From_Pragmas (N);
4446 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4447
4448 -- Link the declaration of the generic homonym in the generic copy to
4449 -- the package it renames, so that it is always resolved properly.
4450
4451 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
4452 Set_Entity (Associated_Node (Name (Renaming)), Id);
4453
4454 -- For a library unit, we have reconstructed the entity for the unit,
4455 -- and must reset it in the library tables.
4456
4457 if Nkind (Parent (N)) = N_Compilation_Unit then
4458 Set_Cunit_Entity (Current_Sem_Unit, Id);
4459 end if;
4460
4461 Analyze_Generic_Formal_Part (N);
4462
4463 -- After processing the generic formals, analysis proceeds as for a
4464 -- non-generic package.
4465
4466 Analyze (Specification (N));
4467
4468 Validate_Categorization_Dependency (N, Id);
4469
4470 End_Generic;
4471
4472 End_Package_Scope (Id);
4473 Exit_Generic_Scope (Id);
4474
4475 -- If the generic appears within a package unit, the body of that unit
4476 -- has to be present for instantiation and inlining.
4477
4478 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
4479 Set_Body_Needed_For_Inlining
4480 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4481 end if;
4482
4483 if Nkind (Parent (N)) /= N_Compilation_Unit then
4484 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
4485 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
4486 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
4487
4488 else
4489 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4490 Validate_RT_RAT_Component (N);
4491
4492 -- If this is a spec without a body, check that generic parameters
4493 -- are referenced.
4494
4495 if not Body_Required (Parent (N)) then
4496 Check_References (Id);
4497 end if;
4498 end if;
4499
4500 -- If there is a specified storage pool in the context, create an
4501 -- aspect on the package declaration, so that it is used in any
4502 -- instance that does not override it.
4503
4504 if Present (Default_Pool) then
4505 declare
4506 ASN : Node_Id;
4507
4508 begin
4509 ASN :=
4510 Make_Aspect_Specification (Loc,
4511 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
4512 Expression => New_Copy (Default_Pool));
4513
4514 if No (Aspect_Specifications (Specification (N))) then
4515 Set_Aspect_Specifications (Specification (N), New_List (ASN));
4516 else
4517 Append (ASN, Aspect_Specifications (Specification (N)));
4518 end if;
4519 end;
4520 end if;
4521 end Analyze_Generic_Package_Declaration;
4522
4523 --------------------------------------------
4524 -- Analyze_Generic_Subprogram_Declaration --
4525 --------------------------------------------
4526
4527 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
4528 Formals : List_Id;
4529 Id : Entity_Id;
4530 New_N : Node_Id;
4531 Result_Type : Entity_Id;
4532 Save_Parent : Node_Id;
4533 Spec : Node_Id;
4534 Typ : Entity_Id;
4535
4536 begin
4537 -- A generic may grant access to its private enclosing context depending
4538 -- on the placement of its corresponding body. From elaboration point of
4539 -- view, the flow of execution may enter this private context, and then
4540 -- reach an external unit, thus producing a dependency on that external
4541 -- unit. For such a path to be properly discovered and encoded in the
4542 -- ALI file of the main unit, let the ABE mechanism process the body of
4543 -- the main unit, and encode all relevant invocation constructs and the
4544 -- relations between them.
4545
4546 Mark_Save_Invocation_Graph_Of_Body;
4547
4548 -- Create copy of generic unit, and save for instantiation. If the unit
4549 -- is a child unit, do not copy the specifications for the parent, which
4550 -- are not part of the generic tree.
4551
4552 Save_Parent := Parent_Spec (N);
4553 Set_Parent_Spec (N, Empty);
4554
4555 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
4556 Set_Parent_Spec (New_N, Save_Parent);
4557 Rewrite (N, New_N);
4558
4559 -- Collect all contract-related source pragmas found within the template
4560 -- and attach them to the contract of the subprogram spec. This contract
4561 -- is used in the capture of global references within annotations.
4562
4563 Create_Generic_Contract (N);
4564
4565 Spec := Specification (N);
4566 Id := Defining_Entity (Spec);
4567 Generate_Definition (Id);
4568
4569 if Nkind (Id) = N_Defining_Operator_Symbol then
4570 Error_Msg_N
4571 ("operator symbol not allowed for generic subprogram", Id);
4572 end if;
4573
4574 Start_Generic;
4575
4576 Enter_Name (Id);
4577 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
4578
4579 Push_Scope (Id);
4580 Enter_Generic_Scope (Id);
4581 Set_Inner_Instances (Id, New_Elmt_List);
4582 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4583
4584 Analyze_Generic_Formal_Part (N);
4585
4586 if Nkind (Spec) = N_Function_Specification then
4587 Mutate_Ekind (Id, E_Generic_Function);
4588 else
4589 Mutate_Ekind (Id, E_Generic_Procedure);
4590 end if;
4591
4592 -- Set SPARK_Mode from context
4593
4594 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4595 Set_SPARK_Pragma_Inherited (Id);
4596
4597 -- Preserve relevant elaboration-related attributes of the context which
4598 -- are no longer available or very expensive to recompute once analysis,
4599 -- resolution, and expansion are over.
4600
4601 Mark_Elaboration_Attributes
4602 (N_Id => Id,
4603 Checks => True,
4604 Warnings => True);
4605
4606 Formals := Parameter_Specifications (Spec);
4607
4608 if Present (Formals) then
4609 Process_Formals (Formals, Spec);
4610 end if;
4611
4612 if Nkind (Spec) = N_Function_Specification then
4613 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
4614 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
4615 Set_Etype (Id, Result_Type);
4616
4617 -- Check restriction imposed by AI05-073: a generic function
4618 -- cannot return an abstract type or an access to such.
4619
4620 if Is_Abstract_Type (Designated_Type (Result_Type)) then
4621 Error_Msg_N
4622 ("generic function cannot have an access result "
4623 & "that designates an abstract type", Spec);
4624 end if;
4625
4626 else
4627 Find_Type (Result_Definition (Spec));
4628 Typ := Entity (Result_Definition (Spec));
4629
4630 if Is_Abstract_Type (Typ)
4631 and then Ada_Version >= Ada_2012
4632 then
4633 Error_Msg_N
4634 ("generic function cannot have abstract result type", Spec);
4635 end if;
4636
4637 -- If a null exclusion is imposed on the result type, then create
4638 -- a null-excluding itype (an access subtype) and use it as the
4639 -- function's Etype.
4640
4641 if Is_Access_Type (Typ)
4642 and then Null_Exclusion_Present (Spec)
4643 then
4644 Set_Etype (Id,
4645 Create_Null_Excluding_Itype
4646 (T => Typ,
4647 Related_Nod => Spec,
4648 Scope_Id => Defining_Unit_Name (Spec)));
4649 else
4650 Set_Etype (Id, Typ);
4651 end if;
4652 end if;
4653
4654 else
4655 Set_Etype (Id, Standard_Void_Type);
4656 end if;
4657
4658 Set_Is_Not_Self_Hidden (Id);
4659
4660 -- Analyze the aspects of the generic copy to ensure that all generated
4661 -- pragmas (if any) perform their semantic effects.
4662
4663 Analyze_Aspect_Specifications (N, Id);
4664
4665 -- For a library unit, we have reconstructed the entity for the unit,
4666 -- and must reset it in the library tables. We also make sure that
4667 -- Body_Required is set properly in the original compilation unit node.
4668
4669 if Nkind (Parent (N)) = N_Compilation_Unit then
4670 Set_Cunit_Entity (Current_Sem_Unit, Id);
4671 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4672 end if;
4673
4674 -- If the generic appears within a package unit, the body of that unit
4675 -- has to be present for instantiation and inlining.
4676
4677 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
4678 and then Unit_Requires_Body (Id)
4679 then
4680 Set_Body_Needed_For_Inlining
4681 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4682 end if;
4683
4684 Set_Categorization_From_Pragmas (N);
4685 Validate_Categorization_Dependency (N, Id);
4686
4687 -- Capture all global references that occur within the profile of the
4688 -- generic subprogram. Aspects are not part of this processing because
4689 -- they must be delayed. If processed now, Save_Global_References will
4690 -- destroy the Associated_Node links and prevent the capture of global
4691 -- references when the contract of the generic subprogram is analyzed.
4692
4693 Save_Global_References (Original_Node (N));
4694
4695 End_Generic;
4696 End_Scope;
4697 Exit_Generic_Scope (Id);
4698 Generate_Reference_To_Formals (Id);
4699
4700 List_Inherited_Pre_Post_Aspects (Id);
4701 end Analyze_Generic_Subprogram_Declaration;
4702
4703 -----------------------------------
4704 -- Analyze_Package_Instantiation --
4705 -----------------------------------
4706
4707 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4708 -- must be replaced by gotos which jump to the end of the routine in order
4709 -- to restore the Ghost and SPARK modes.
4710
4711 procedure Analyze_Package_Instantiation (N : Node_Id) is
4712 Has_Inline_Always : Boolean := False;
4713 -- Set if the generic unit contains any subprograms with Inline_Always.
4714 -- Only relevant when back-end inlining is not enabled.
4715
4716 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4717 -- Return True if inlining is active and Gen_Unit contains inlined
4718 -- subprograms. In this case, we may either instantiate the body when
4719 -- front-end inlining is enabled, or add a pending instantiation when
4720 -- back-end inlining is enabled. In the former case, this may cause
4721 -- superfluous instantiations, but in either case we need to perform
4722 -- the instantiation of the body in the context of the instance and
4723 -- not in that of the point of inlining.
4724
4725 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4726 -- Return True if Gen_Unit needs to have its body instantiated in the
4727 -- context of N. This in particular excludes generic contexts.
4728
4729 -----------------------
4730 -- Might_Inline_Subp --
4731 -----------------------
4732
4733 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4734 E : Entity_Id;
4735
4736 begin
4737 if Inline_Processing_Required then
4738 -- No need to recompute the answer if we know it is positive
4739 -- and back-end inlining is enabled.
4740
4741 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4742 return True;
4743 end if;
4744
4745 E := First_Entity (Gen_Unit);
4746 while Present (E) loop
4747 if Is_Subprogram (E) and then Is_Inlined (E) then
4748 -- Remember if there are any subprograms with Inline_Always
4749
4750 if Has_Pragma_Inline_Always (E) then
4751 Has_Inline_Always := True;
4752 end if;
4753
4754 Set_Is_Inlined (Gen_Unit);
4755 return True;
4756 end if;
4757
4758 Next_Entity (E);
4759 end loop;
4760 end if;
4761
4762 return False;
4763 end Might_Inline_Subp;
4764
4765 -------------------------------
4766 -- Needs_Body_Instantiated --
4767 -------------------------------
4768
4769 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4770 begin
4771 -- No need to instantiate bodies in generic units
4772
4773 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4774 return False;
4775 end if;
4776
4777 -- If the instantiation is in the main unit, then the body is needed
4778
4779 if Is_In_Main_Unit (N) then
4780 return True;
4781 end if;
4782
4783 -- In GNATprove mode, never instantiate bodies outside of the main
4784 -- unit, as it does not use frontend/backend inlining in the way that
4785 -- GNAT does, so does not benefit from such instantiations. On the
4786 -- contrary, such instantiations may bring artificial constraints,
4787 -- as for example such bodies may require preprocessing.
4788
4789 if GNATprove_Mode then
4790 return False;
4791 end if;
4792
4793 -- If not, then again no need to instantiate bodies in generic units
4794
4795 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4796 return False;
4797 end if;
4798
4799 -- Here we have a special handling for back-end inlining: if inline
4800 -- processing is required, then we unconditionally want to have the
4801 -- body instantiated. The reason is that Might_Inline_Subp does not
4802 -- catch all the cases (as it does not recurse into nested packages)
4803 -- so this avoids the need to patch things up afterwards. Moreover,
4804 -- these instantiations are only performed on demand when back-end
4805 -- inlining is enabled, so this causes very little extra work.
4806
4807 if Inline_Processing_Required and then Back_End_Inlining then
4808 return True;
4809 end if;
4810
4811 -- We want to have the bodies instantiated in non-main units if
4812 -- they might contribute inlined subprograms.
4813
4814 return Might_Inline_Subp (Gen_Unit);
4815 end Needs_Body_Instantiated;
4816
4817 -- Local declarations
4818
4819 Gen_Id : constant Node_Id := Name (N);
4820 Inst_Id : constant Entity_Id := Defining_Entity (N);
4821 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4822 Loc : constant Source_Ptr := Sloc (N);
4823
4824 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4825 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4826 Saved_ISMP : constant Boolean :=
4827 Ignore_SPARK_Mode_Pragmas_In_Instance;
4828 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4829 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4830 -- Save the Ghost and SPARK mode-related data to restore on exit
4831
4832 Saved_Style_Check : constant Boolean := Style_Check;
4833 -- Save style check mode for restore on exit
4834
4835 Act_Decl : Node_Id;
4836 Act_Decl_Name : Node_Id;
4837 Act_Decl_Id : Entity_Id;
4838 Act_Spec : Node_Id;
4839 Act_Tree : Node_Id;
4840 Env_Installed : Boolean := False;
4841 Gen_Decl : Node_Id;
4842 Gen_Spec : Node_Id;
4843 Gen_Unit : Entity_Id;
4844 Inline_Now : Boolean := False;
4845 Needs_Body : Boolean;
4846 Parent_Installed : Boolean := False;
4847 Renamings : List_Id;
4848 Unit_Renaming : Node_Id;
4849
4850 Vis_Prims_List : Elist_Id := No_Elist;
4851 -- List of primitives made temporarily visible in the instantiation
4852 -- to match the visibility of the formal type
4853
4854 -- Start of processing for Analyze_Package_Instantiation
4855
4856 begin
4857 -- Preserve relevant elaboration-related attributes of the context which
4858 -- are no longer available or very expensive to recompute once analysis,
4859 -- resolution, and expansion are over.
4860
4861 Mark_Elaboration_Attributes
4862 (N_Id => N,
4863 Checks => True,
4864 Level => True,
4865 Modes => True,
4866 Warnings => True);
4867
4868 -- Very first thing: check for Text_IO special unit in case we are
4869 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4870
4871 Check_Text_IO_Special_Unit (Name (N));
4872
4873 -- Make node global for error reporting
4874
4875 Instantiation_Node := N;
4876
4877 -- Case of instantiation of a generic package
4878
4879 if Nkind (N) = N_Package_Instantiation then
4880 Act_Decl_Id := New_Copy (Defining_Entity (N));
4881
4882 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4883 Act_Decl_Name :=
4884 Make_Defining_Program_Unit_Name (Loc,
4885 Name =>
4886 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4887 Defining_Identifier => Act_Decl_Id);
4888 else
4889 Act_Decl_Name := Act_Decl_Id;
4890 end if;
4891
4892 -- Case of instantiation of a formal package
4893
4894 else
4895 Act_Decl_Id := Defining_Identifier (N);
4896 Act_Decl_Name := Act_Decl_Id;
4897 end if;
4898
4899 Generate_Definition (Act_Decl_Id);
4900 Mutate_Ekind (Act_Decl_Id, E_Package);
4901 Set_Is_Not_Self_Hidden (Act_Decl_Id);
4902
4903 -- Initialize list of incomplete actuals before analysis
4904
4905 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4906
4907 Preanalyze_Actuals (N, Act_Decl_Id);
4908
4909 -- Turn off style checking in instances. If the check is enabled on the
4910 -- generic unit, a warning in an instance would just be noise. If not
4911 -- enabled on the generic, then a warning in an instance is just wrong.
4912 -- This must be done after analyzing the actuals, which do come from
4913 -- source and are subject to style checking.
4914
4915 Style_Check := False;
4916
4917 Init_Env;
4918 Env_Installed := True;
4919
4920 -- Reset renaming map for formal types. The mapping is established
4921 -- when analyzing the generic associations, but some mappings are
4922 -- inherited from formal packages of parent units, and these are
4923 -- constructed when the parents are installed.
4924
4925 Generic_Renamings.Set_Last (0);
4926 Generic_Renamings_HTable.Reset;
4927
4928 -- Except for an abbreviated instance created to check a formal package,
4929 -- install the parent if this is a generic child unit.
4930
4931 if not Is_Abbreviated_Instance (Inst_Id) then
4932 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4933 end if;
4934
4935 Gen_Unit := Entity (Gen_Id);
4936
4937 -- A package instantiation is Ghost when it is subject to pragma Ghost
4938 -- or the generic template is Ghost. Set the mode now to ensure that
4939 -- any nodes generated during analysis and expansion are marked as
4940 -- Ghost.
4941
4942 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4943
4944 -- Verify that it is the name of a generic package
4945
4946 -- A visibility glitch: if the instance is a child unit and the generic
4947 -- is the generic unit of a parent instance (i.e. both the parent and
4948 -- the child units are instances of the same package) the name now
4949 -- denotes the renaming within the parent, not the intended generic
4950 -- unit. See if there is a homonym that is the desired generic. The
4951 -- renaming declaration must be visible inside the instance of the
4952 -- child, but not when analyzing the name in the instantiation itself.
4953
4954 if Ekind (Gen_Unit) = E_Package
4955 and then Present (Renamed_Entity (Gen_Unit))
4956 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4957 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4958 and then Present (Homonym (Gen_Unit))
4959 then
4960 Gen_Unit := Homonym (Gen_Unit);
4961 end if;
4962
4963 if Etype (Gen_Unit) = Any_Type then
4964 Restore_Env;
4965 goto Leave;
4966
4967 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4968
4969 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4970
4971 if From_Limited_With (Gen_Unit) then
4972 Error_Msg_N
4973 ("cannot instantiate a limited withed package", Gen_Id);
4974 else
4975 Error_Msg_NE
4976 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4977 end if;
4978
4979 Restore_Env;
4980 goto Leave;
4981 end if;
4982
4983 if In_Extended_Main_Source_Unit (N) then
4984 Set_Is_Instantiated (Gen_Unit);
4985 Generate_Reference (Gen_Unit, N);
4986
4987 if Present (Renamed_Entity (Gen_Unit)) then
4988 Set_Is_Instantiated (Renamed_Entity (Gen_Unit));
4989 Generate_Reference (Renamed_Entity (Gen_Unit), N);
4990 end if;
4991 end if;
4992
4993 if Nkind (Gen_Id) = N_Identifier
4994 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4995 then
4996 Error_Msg_NE
4997 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4998
4999 elsif Nkind (Gen_Id) = N_Expanded_Name
5000 and then Is_Child_Unit (Gen_Unit)
5001 and then Nkind (Prefix (Gen_Id)) = N_Identifier
5002 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
5003 then
5004 Error_Msg_N
5005 ("& is hidden within declaration of instance", Prefix (Gen_Id));
5006 end if;
5007
5008 Set_Entity (Gen_Id, Gen_Unit);
5009
5010 -- If generic is a renaming, get original generic unit
5011
5012 if Present (Renamed_Entity (Gen_Unit))
5013 and then Ekind (Renamed_Entity (Gen_Unit)) = E_Generic_Package
5014 then
5015 Gen_Unit := Renamed_Entity (Gen_Unit);
5016 end if;
5017
5018 -- Verify that there are no circular instantiations
5019
5020 if In_Open_Scopes (Gen_Unit) then
5021 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5022 Restore_Env;
5023 goto Leave;
5024
5025 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5026 Error_Msg_Node_2 := Current_Scope;
5027 Error_Msg_NE
5028 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5029 Circularity_Detected := True;
5030 Restore_Env;
5031 goto Leave;
5032
5033 else
5034 Mutate_Ekind (Inst_Id, E_Package);
5035 Set_Scope (Inst_Id, Current_Scope);
5036
5037 -- If the context of the instance is subject to SPARK_Mode "off" or
5038 -- the annotation is altogether missing, set the global flag which
5039 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5040 -- the instance.
5041
5042 if SPARK_Mode /= On then
5043 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5044
5045 -- Mark the instance spec in case the body is instantiated at a
5046 -- later pass. This preserves the original context in effect for
5047 -- the body.
5048
5049 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5050 end if;
5051
5052 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5053 Gen_Spec := Specification (Gen_Decl);
5054
5055 -- Initialize renamings map, for error checking, and the list that
5056 -- holds private entities whose views have changed between generic
5057 -- definition and instantiation. If this is the instance created to
5058 -- validate an actual package, the instantiation environment is that
5059 -- of the enclosing instance.
5060
5061 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5062
5063 -- Copy original generic tree, to produce text for instantiation
5064
5065 Act_Tree :=
5066 Copy_Generic_Node
5067 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5068
5069 Act_Spec := Specification (Act_Tree);
5070
5071 -- If this is the instance created to validate an actual package,
5072 -- only the formals matter, do not examine the package spec itself.
5073
5074 if Is_Actual_Pack then
5075 Set_Visible_Declarations (Act_Spec, New_List);
5076 Set_Private_Declarations (Act_Spec, New_List);
5077 end if;
5078
5079 Renamings :=
5080 Analyze_Associations
5081 (I_Node => N,
5082 Formals => Generic_Formal_Declarations (Act_Tree),
5083 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5084
5085 Vis_Prims_List := Check_Hidden_Primitives (Renamings);
5086
5087 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
5088 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
5089 Set_Is_Generic_Instance (Act_Decl_Id);
5090 Set_Generic_Parent (Act_Spec, Gen_Unit);
5091
5092 -- References to the generic in its own declaration or its body are
5093 -- references to the instance. Add a renaming declaration for the
5094 -- generic unit itself. This declaration, as well as the renaming
5095 -- declarations for the generic formals, must remain private to the
5096 -- unit: the formals, because this is the language semantics, and
5097 -- the unit because its use is an artifact of the implementation.
5098
5099 Unit_Renaming :=
5100 Make_Package_Renaming_Declaration (Loc,
5101 Defining_Unit_Name =>
5102 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
5103 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
5104
5105 Append (Unit_Renaming, Renamings);
5106
5107 -- The renaming declarations are the first local declarations of the
5108 -- new unit.
5109
5110 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
5111 Insert_List_Before
5112 (First (Visible_Declarations (Act_Spec)), Renamings);
5113 else
5114 Set_Visible_Declarations (Act_Spec, Renamings);
5115 end if;
5116
5117 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
5118
5119 -- Propagate the aspect specifications from the package declaration
5120 -- template to the instantiated version of the package declaration.
5121
5122 if Has_Aspects (Act_Tree) then
5123 Set_Aspect_Specifications (Act_Decl,
5124 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
5125 end if;
5126
5127 -- The generic may have a generated Default_Storage_Pool aspect,
5128 -- set at the point of generic declaration. If the instance has
5129 -- that aspect, it overrides the one inherited from the generic.
5130
5131 if Has_Aspects (Gen_Spec) then
5132 if No (Aspect_Specifications (N)) then
5133 Set_Aspect_Specifications (N,
5134 (New_Copy_List_Tree
5135 (Aspect_Specifications (Gen_Spec))));
5136
5137 else
5138 declare
5139 Inherited_Aspects : constant List_Id :=
5140 New_Copy_List_Tree
5141 (Aspect_Specifications (Gen_Spec));
5142
5143 ASN1 : Node_Id;
5144 ASN2 : Node_Id;
5145 Pool_Present : Boolean := False;
5146
5147 begin
5148 ASN1 := First (Aspect_Specifications (N));
5149 while Present (ASN1) loop
5150 if Chars (Identifier (ASN1)) =
5151 Name_Default_Storage_Pool
5152 then
5153 Pool_Present := True;
5154 exit;
5155 end if;
5156
5157 Next (ASN1);
5158 end loop;
5159
5160 if Pool_Present then
5161
5162 -- If generic carries a default storage pool, remove it
5163 -- in favor of the instance one.
5164
5165 ASN2 := First (Inherited_Aspects);
5166 while Present (ASN2) loop
5167 if Chars (Identifier (ASN2)) =
5168 Name_Default_Storage_Pool
5169 then
5170 Remove (ASN2);
5171 exit;
5172 end if;
5173
5174 Next (ASN2);
5175 end loop;
5176 end if;
5177
5178 Prepend_List_To
5179 (Aspect_Specifications (N), Inherited_Aspects);
5180 end;
5181 end if;
5182 end if;
5183
5184 -- Save the instantiation node for a subsequent instantiation of the
5185 -- body if there is one and it needs to be instantiated here.
5186
5187 -- We instantiate the body only if we are generating code, or if we
5188 -- are generating cross-reference information, or for GNATprove use.
5189
5190 declare
5191 Enclosing_Body_Present : Boolean := False;
5192 -- If the generic unit is not a compilation unit, then a body may
5193 -- be present in its parent even if none is required. We create a
5194 -- tentative pending instantiation for the body, which will be
5195 -- discarded if none is actually present.
5196
5197 Scop : Entity_Id;
5198
5199 begin
5200 if Scope (Gen_Unit) /= Standard_Standard
5201 and then not Is_Child_Unit (Gen_Unit)
5202 then
5203 Scop := Scope (Gen_Unit);
5204 while Present (Scop) and then Scop /= Standard_Standard loop
5205 if Unit_Requires_Body (Scop) then
5206 Enclosing_Body_Present := True;
5207 exit;
5208
5209 elsif In_Open_Scopes (Scop)
5210 and then In_Package_Body (Scop)
5211 then
5212 Enclosing_Body_Present := True;
5213 exit;
5214 end if;
5215
5216 exit when Is_Compilation_Unit (Scop);
5217 Scop := Scope (Scop);
5218 end loop;
5219 end if;
5220
5221 -- If front-end inlining is enabled or there are any subprograms
5222 -- marked with Inline_Always, and this is a unit for which code
5223 -- will be generated, we instantiate the body at once.
5224
5225 -- This is done if the instance is not the main unit, and if the
5226 -- generic is not a child unit of another generic, to avoid scope
5227 -- problems and the reinstallation of parent instances.
5228
5229 if Expander_Active
5230 and then (not Is_Child_Unit (Gen_Unit)
5231 or else not Is_Generic_Unit (Scope (Gen_Unit)))
5232 and then Might_Inline_Subp (Gen_Unit)
5233 and then not Is_Actual_Pack
5234 then
5235 if not Back_End_Inlining
5236 and then (Front_End_Inlining or else Has_Inline_Always)
5237 and then (Is_In_Main_Unit (N)
5238 or else In_Main_Context (Current_Scope))
5239 and then Nkind (Parent (N)) /= N_Compilation_Unit
5240 then
5241 Inline_Now := True;
5242
5243 -- In configurable_run_time mode we force the inlining of
5244 -- predefined subprograms marked Inline_Always, to minimize
5245 -- the use of the run-time library.
5246
5247 elsif In_Predefined_Unit (Gen_Decl)
5248 and then Configurable_Run_Time_Mode
5249 and then Nkind (Parent (N)) /= N_Compilation_Unit
5250 then
5251 Inline_Now := True;
5252 end if;
5253
5254 -- If the current scope is itself an instance within a child
5255 -- unit, there will be duplications in the scope stack, and the
5256 -- unstacking mechanism in Inline_Instance_Body will fail.
5257 -- This loses some rare cases of optimization.
5258
5259 if Is_Generic_Instance (Current_Scope) then
5260 declare
5261 Curr_Unit : constant Entity_Id :=
5262 Cunit_Entity (Current_Sem_Unit);
5263 begin
5264 if Curr_Unit /= Current_Scope
5265 and then Is_Child_Unit (Curr_Unit)
5266 then
5267 Inline_Now := False;
5268 end if;
5269 end;
5270 end if;
5271 end if;
5272
5273 Needs_Body :=
5274 (Unit_Requires_Body (Gen_Unit)
5275 or else Enclosing_Body_Present
5276 or else Present (Corresponding_Body (Gen_Decl)))
5277 and then Needs_Body_Instantiated (Gen_Unit)
5278 and then not Is_Actual_Pack
5279 and then not Inline_Now
5280 and then (Operating_Mode = Generate_Code
5281 or else (Operating_Mode = Check_Semantics
5282 and then GNATprove_Mode));
5283
5284 -- If front-end inlining is enabled or there are any subprograms
5285 -- marked with Inline_Always, do not instantiate body when within
5286 -- a generic context.
5287
5288 if not Back_End_Inlining
5289 and then (Front_End_Inlining or else Has_Inline_Always)
5290 and then not Expander_Active
5291 then
5292 Needs_Body := False;
5293 end if;
5294
5295 -- If the current context is generic, and the package being
5296 -- instantiated is declared within a formal package, there is no
5297 -- body to instantiate until the enclosing generic is instantiated
5298 -- and there is an actual for the formal package. If the formal
5299 -- package has parameters, we build a regular package instance for
5300 -- it, that precedes the original formal package declaration.
5301
5302 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
5303 declare
5304 Decl : constant Node_Id :=
5305 Original_Node
5306 (Unit_Declaration_Node (Scope (Gen_Unit)));
5307 begin
5308 if Nkind (Decl) = N_Formal_Package_Declaration
5309 or else (Nkind (Decl) = N_Package_Declaration
5310 and then Is_List_Member (Decl)
5311 and then Present (Next (Decl))
5312 and then
5313 Nkind (Next (Decl)) =
5314 N_Formal_Package_Declaration)
5315 then
5316 Needs_Body := False;
5317 end if;
5318 end;
5319 end if;
5320 end;
5321
5322 -- For RCI unit calling stubs, we omit the instance body if the
5323 -- instance is the RCI library unit itself.
5324
5325 -- However there is a special case for nested instances: in this case
5326 -- we do generate the instance body, as it might be required, e.g.
5327 -- because it provides stream attributes for some type used in the
5328 -- profile of a remote subprogram. This is consistent with 12.3(12),
5329 -- which indicates that the instance body occurs at the place of the
5330 -- instantiation, and thus is part of the RCI declaration, which is
5331 -- present on all client partitions (this is E.2.3(18)).
5332
5333 -- Note that AI12-0002 may make it illegal at some point to have
5334 -- stream attributes defined in an RCI unit, in which case this
5335 -- special case will become unnecessary. In the meantime, there
5336 -- is known application code in production that depends on this
5337 -- being possible, so we definitely cannot eliminate the body in
5338 -- the case of nested instances for the time being.
5339
5340 -- When we generate a nested instance body, calling stubs for any
5341 -- relevant subprogram will be inserted immediately after the
5342 -- subprogram declarations, and will take precedence over the
5343 -- subsequent (original) body. (The stub and original body will be
5344 -- complete homographs, but this is permitted in an instance).
5345 -- (Could we do better and remove the original body???)
5346
5347 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
5348 and then Comes_From_Source (N)
5349 and then Nkind (Parent (N)) = N_Compilation_Unit
5350 then
5351 Needs_Body := False;
5352 end if;
5353
5354 -- If the context requires a full instantiation, set things up for
5355 -- subsequent construction of the body.
5356
5357 if Needs_Body then
5358 declare
5359 Fin_Scop, S : Entity_Id;
5360
5361 begin
5362 Check_Forward_Instantiation (Gen_Decl);
5363
5364 Fin_Scop := Empty;
5365
5366 -- For a package instantiation that is not a compilation unit,
5367 -- indicate that cleanup actions of the innermost enclosing
5368 -- scope for which they are generated should be delayed until
5369 -- after the package body is instantiated.
5370
5371 if Nkind (N) = N_Package_Instantiation
5372 and then not Is_Compilation_Unit (Act_Decl_Id)
5373 then
5374 S := Current_Scope;
5375
5376 while S /= Standard_Standard loop
5377 -- Cleanup actions are not generated within generic units
5378 -- or in the formal part of generic units.
5379
5380 if not Expander_Active then
5381 exit;
5382
5383 -- For package scopes, cleanup actions are generated only
5384 -- for compilation units, for spec and body separately.
5385
5386 elsif Ekind (S) = E_Package then
5387 if Is_Compilation_Unit (S) then
5388 if In_Package_Body (S) then
5389 Fin_Scop := Body_Entity (S);
5390 else
5391 Fin_Scop := S;
5392 end if;
5393
5394 Set_Delay_Cleanups (Fin_Scop);
5395 exit;
5396
5397 else
5398 S := Scope (S);
5399 end if;
5400
5401 -- Cleanup actions are generated for all dynamic scopes
5402
5403 else
5404 Fin_Scop := S;
5405 Set_Delay_Cleanups (Fin_Scop);
5406 exit;
5407 end if;
5408 end loop;
5409 end if;
5410
5411 Add_Pending_Instantiation (N, Act_Decl, Fin_Scop);
5412 end;
5413 end if;
5414
5415 Set_Categorization_From_Pragmas (Act_Decl);
5416
5417 if Parent_Installed then
5418 Hide_Current_Scope;
5419 end if;
5420
5421 Set_Instance_Spec (N, Act_Decl);
5422
5423 -- If not a compilation unit, insert the package declaration before
5424 -- the original instantiation node.
5425
5426 if Nkind (Parent (N)) /= N_Compilation_Unit then
5427 Mark_Rewrite_Insertion (Act_Decl);
5428 Insert_Before (N, Act_Decl);
5429
5430 if Has_Aspects (N) then
5431 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5432
5433 -- The pragma created for a Default_Storage_Pool aspect must
5434 -- appear ahead of the declarations in the instance spec.
5435 -- Analysis has placed it after the instance node, so remove
5436 -- it and reinsert it properly now.
5437
5438 declare
5439 ASN : constant Node_Id := First (Aspect_Specifications (N));
5440 A_Name : constant Name_Id := Chars (Identifier (ASN));
5441 Decl : Node_Id;
5442
5443 begin
5444 if A_Name = Name_Default_Storage_Pool then
5445 if No (Visible_Declarations (Act_Spec)) then
5446 Set_Visible_Declarations (Act_Spec, New_List);
5447 end if;
5448
5449 Decl := Next (N);
5450 while Present (Decl) loop
5451 if Nkind (Decl) = N_Pragma then
5452 Remove (Decl);
5453 Prepend (Decl, Visible_Declarations (Act_Spec));
5454 exit;
5455 end if;
5456
5457 Next (Decl);
5458 end loop;
5459 end if;
5460 end;
5461 end if;
5462
5463 Analyze (Act_Decl);
5464
5465 -- For an instantiation that is a compilation unit, place
5466 -- declaration on current node so context is complete for analysis
5467 -- (including nested instantiations). If this is the main unit,
5468 -- the declaration eventually replaces the instantiation node.
5469 -- If the instance body is created later, it replaces the
5470 -- instance node, and the declaration is attached to it
5471 -- (see Build_Instance_Compilation_Unit_Nodes).
5472
5473 else
5474 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
5475
5476 -- The entity for the current unit is the newly created one,
5477 -- and all semantic information is attached to it.
5478
5479 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
5480
5481 -- If this is the main unit, replace the main entity as well
5482
5483 if Current_Sem_Unit = Main_Unit then
5484 Main_Unit_Entity := Act_Decl_Id;
5485 end if;
5486 end if;
5487
5488 Set_Unit (Parent (N), Act_Decl);
5489 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5490 Set_Package_Instantiation (Act_Decl_Id, N);
5491
5492 -- Process aspect specifications of the instance node, if any, to
5493 -- take into account categorization pragmas before analyzing the
5494 -- instance.
5495
5496 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5497
5498 Analyze (Act_Decl);
5499 Set_Unit (Parent (N), N);
5500 Set_Body_Required (Parent (N), False);
5501
5502 -- We never need elaboration checks on instantiations, since by
5503 -- definition, the body instantiation is elaborated at the same
5504 -- time as the spec instantiation.
5505
5506 if Legacy_Elaboration_Checks then
5507 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5508 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5509 end if;
5510 end if;
5511
5512 if Legacy_Elaboration_Checks then
5513 Check_Elab_Instantiation (N);
5514 end if;
5515
5516 -- Save the scenario for later examination by the ABE Processing
5517 -- phase.
5518
5519 Record_Elaboration_Scenario (N);
5520
5521 -- The instantiation results in a guaranteed ABE
5522
5523 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
5524 -- Do not instantiate the corresponding body because gigi cannot
5525 -- handle certain types of premature instantiations.
5526
5527 Remove_Dead_Instance (N);
5528
5529 -- Create completing bodies for all subprogram declarations since
5530 -- their real bodies will not be instantiated.
5531
5532 Provide_Completing_Bodies (Instance_Spec (N));
5533 end if;
5534
5535 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5536
5537 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
5538 First_Private_Entity (Act_Decl_Id));
5539
5540 -- If the instantiation needs a body, the unit will be turned into
5541 -- a package body and receive its own elaboration entity. Otherwise,
5542 -- the nature of the unit is now a package declaration.
5543
5544 -- Note that the below rewriting means that Act_Decl, which has been
5545 -- analyzed and expanded, will be re-expanded as the rewritten N.
5546
5547 if Nkind (Parent (N)) = N_Compilation_Unit
5548 and then not Needs_Body
5549 then
5550 Rewrite (N, Act_Decl);
5551 end if;
5552
5553 if Present (Corresponding_Body (Gen_Decl))
5554 or else Unit_Requires_Body (Gen_Unit)
5555 then
5556 Set_Has_Completion (Act_Decl_Id);
5557 end if;
5558
5559 Check_Formal_Packages (Act_Decl_Id);
5560
5561 Restore_Hidden_Primitives (Vis_Prims_List);
5562 Restore_Private_Views (Act_Decl_Id);
5563
5564 Inherit_Context (Gen_Decl, N);
5565
5566 if Parent_Installed then
5567 Remove_Parent;
5568 end if;
5569
5570 Restore_Env;
5571 Env_Installed := False;
5572 end if;
5573
5574 Validate_Categorization_Dependency (N, Act_Decl_Id);
5575
5576 -- There used to be a check here to prevent instantiations in local
5577 -- contexts if the No_Local_Allocators restriction was active. This
5578 -- check was removed by a binding interpretation in AI-95-00130/07,
5579 -- but we retain the code for documentation purposes.
5580
5581 -- if Ekind (Act_Decl_Id) /= E_Void
5582 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
5583 -- then
5584 -- Check_Restriction (No_Local_Allocators, N);
5585 -- end if;
5586
5587 if Inline_Now then
5588 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
5589 end if;
5590
5591 -- Check that if N is an instantiation of System.Dim_Float_IO or
5592 -- System.Dim_Integer_IO, the formal type has a dimension system.
5593
5594 if Nkind (N) = N_Package_Instantiation
5595 and then Is_Dim_IO_Package_Instantiation (N)
5596 then
5597 declare
5598 Assoc : constant Node_Id := First (Generic_Associations (N));
5599 begin
5600 if not Has_Dimension_System
5601 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
5602 then
5603 Error_Msg_N ("type with a dimension system expected", Assoc);
5604 end if;
5605 end;
5606 end if;
5607
5608 <<Leave>>
5609 if Nkind (Parent (N)) /= N_Compilation_Unit then
5610 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5611 end if;
5612
5613 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5614 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5615 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5616 Style_Check := Saved_Style_Check;
5617
5618 exception
5619 when Instantiation_Error =>
5620 if Parent_Installed then
5621 Remove_Parent;
5622 end if;
5623
5624 if Env_Installed then
5625 Restore_Env;
5626 end if;
5627
5628 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5629 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5630 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5631 Style_Check := Saved_Style_Check;
5632 end Analyze_Package_Instantiation;
5633
5634 --------------------------
5635 -- Inline_Instance_Body --
5636 --------------------------
5637
5638 -- WARNING: This routine manages SPARK regions. Return statements must be
5639 -- replaced by gotos which jump to the end of the routine and restore the
5640 -- SPARK mode.
5641
5642 procedure Inline_Instance_Body
5643 (N : Node_Id;
5644 Gen_Unit : Entity_Id;
5645 Act_Decl : Node_Id)
5646 is
5647 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
5648
5649 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
5650 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5651 Gen_Comp : constant Entity_Id :=
5652 Cunit_Entity (Get_Source_Unit (Gen_Unit));
5653
5654 Scope_Stack_Depth : constant Pos :=
5655 Scope_Stack.Last - Scope_Stack.First + 1;
5656
5657 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
5658 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
5659 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
5660
5661 Curr_Scope : Entity_Id := Empty;
5662 List : Elist_Id := No_Elist; -- init to avoid warning
5663 N_Instances : Nat := 0;
5664 Num_Inner : Nat := 0;
5665 Num_Scopes : Nat := 0;
5666 Removed : Boolean := False;
5667 S : Entity_Id;
5668 Vis : Boolean;
5669
5670 begin
5671 -- Case of generic unit defined in another unit. We must remove the
5672 -- complete context of the current unit to install that of the generic.
5673
5674 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
5675
5676 -- Loop through enclosing scopes until we reach a generic instance,
5677 -- package body, or subprogram.
5678
5679 S := Current_Scope;
5680 while Present (S) and then S /= Standard_Standard loop
5681
5682 -- Save use clauses from enclosing scopes into Use_Clauses
5683
5684 loop
5685 Num_Scopes := Num_Scopes + 1;
5686
5687 Use_Clauses (Num_Scopes) :=
5688 (Scope_Stack.Table
5689 (Scope_Stack.Last - Num_Scopes + 1).First_Use_Clause);
5690 End_Use_Clauses (Use_Clauses (Num_Scopes));
5691
5692 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5693 or else Scope_Stack.Table
5694 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5695 end loop;
5696
5697 exit when Is_Generic_Instance (S)
5698 and then (In_Package_Body (S)
5699 or else Ekind (S) = E_Procedure
5700 or else Ekind (S) = E_Function);
5701 S := Scope (S);
5702 end loop;
5703
5704 Vis := Is_Immediately_Visible (Gen_Comp);
5705
5706 -- Find and save all enclosing instances
5707
5708 S := Current_Scope;
5709
5710 while Present (S)
5711 and then S /= Standard_Standard
5712 loop
5713 if Is_Generic_Instance (S) then
5714 N_Instances := N_Instances + 1;
5715 Instances (N_Instances) := S;
5716
5717 exit when In_Package_Body (S);
5718 end if;
5719
5720 S := Scope (S);
5721 end loop;
5722
5723 -- Remove context of current compilation unit, unless we are within a
5724 -- nested package instantiation, in which case the context has been
5725 -- removed previously.
5726
5727 -- If current scope is the body of a child unit, remove context of
5728 -- spec as well. If an enclosing scope is an instance body, the
5729 -- context has already been removed, but the entities in the body
5730 -- must be made invisible as well.
5731
5732 S := Current_Scope;
5733 while Present (S) and then S /= Standard_Standard loop
5734 if Is_Generic_Instance (S)
5735 and then (In_Package_Body (S)
5736 or else Ekind (S) in E_Procedure | E_Function)
5737 then
5738 -- We still have to remove the entities of the enclosing
5739 -- instance from direct visibility.
5740
5741 declare
5742 E : Entity_Id;
5743 begin
5744 E := First_Entity (S);
5745 while Present (E) loop
5746 Set_Is_Immediately_Visible (E, False);
5747 Next_Entity (E);
5748 end loop;
5749 end;
5750
5751 exit;
5752 end if;
5753
5754 if S = Curr_Unit
5755 or else (Ekind (Curr_Unit) = E_Package_Body
5756 and then S = Spec_Entity (Curr_Unit))
5757 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5758 and then S = Corresponding_Spec
5759 (Unit_Declaration_Node (Curr_Unit)))
5760 then
5761 Removed := True;
5762
5763 -- Remove entities in current scopes from visibility, so that
5764 -- instance body is compiled in a clean environment.
5765
5766 List := Save_Scope_Stack (Handle_Use => False);
5767
5768 if Is_Child_Unit (S) then
5769
5770 -- Remove child unit from stack, as well as inner scopes.
5771 -- Removing the context of a child unit removes parent units
5772 -- as well.
5773
5774 while Current_Scope /= S loop
5775 Num_Inner := Num_Inner + 1;
5776 Inner_Scopes (Num_Inner) := Current_Scope;
5777 Pop_Scope;
5778 end loop;
5779
5780 Pop_Scope;
5781 Remove_Context (Curr_Comp);
5782 Curr_Scope := S;
5783
5784 else
5785 Remove_Context (Curr_Comp);
5786 end if;
5787
5788 if Ekind (Curr_Unit) = E_Package_Body then
5789 Remove_Context (Library_Unit (Curr_Comp));
5790 end if;
5791 end if;
5792
5793 S := Scope (S);
5794 end loop;
5795
5796 pragma Assert (Num_Inner < Num_Scopes);
5797
5798 Push_Scope (Standard_Standard);
5799 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5800
5801 -- The inlined package body is analyzed with the configuration state
5802 -- of the context prior to the scope manipulations performed above.
5803
5804 -- ??? shouldn't this also use the warning state of the context prior
5805 -- to the scope manipulations?
5806
5807 Instantiate_Package_Body
5808 (Body_Info =>
5809 ((Inst_Node => N,
5810 Act_Decl => Act_Decl,
5811 Fin_Scop => Empty,
5812 Config_Switches => Config_Attrs,
5813 Current_Sem_Unit => Current_Sem_Unit,
5814 Expander_Status => Expander_Active,
5815 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5816 Scope_Suppress => Scope_Suppress,
5817 Warnings => Save_Warnings)),
5818 Inlined_Body => True);
5819
5820 Pop_Scope;
5821
5822 -- Restore context
5823
5824 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5825
5826 -- Reset Generic_Instance flag so that use clauses can be installed
5827 -- in the proper order. (See Use_One_Package for effect of enclosing
5828 -- instances on processing of use clauses).
5829
5830 for J in 1 .. N_Instances loop
5831 Set_Is_Generic_Instance (Instances (J), False);
5832 end loop;
5833
5834 if Removed then
5835 Install_Context (Curr_Comp, Chain => False);
5836
5837 if Present (Curr_Scope)
5838 and then Is_Child_Unit (Curr_Scope)
5839 then
5840 Push_Scope (Curr_Scope);
5841 Set_Is_Immediately_Visible (Curr_Scope);
5842
5843 -- Finally, restore inner scopes as well
5844
5845 for J in reverse 1 .. Num_Inner loop
5846 Push_Scope (Inner_Scopes (J));
5847 end loop;
5848 end if;
5849
5850 Restore_Scope_Stack (List, Handle_Use => False);
5851
5852 if Present (Curr_Scope)
5853 and then
5854 (In_Private_Part (Curr_Scope)
5855 or else In_Package_Body (Curr_Scope))
5856 then
5857 -- Install private declaration of ancestor units, which are
5858 -- currently available. Restore_Scope_Stack and Install_Context
5859 -- only install the visible part of parents.
5860
5861 declare
5862 Par : Entity_Id;
5863 begin
5864 Par := Scope (Curr_Scope);
5865 while Present (Par) and then Par /= Standard_Standard loop
5866 Install_Private_Declarations (Par);
5867 Par := Scope (Par);
5868 end loop;
5869 end;
5870 end if;
5871 end if;
5872
5873 -- Restore use clauses. For a child unit, use clauses in the parents
5874 -- are restored when installing the context, so only those in inner
5875 -- scopes (and those local to the child unit itself) need to be
5876 -- installed explicitly.
5877
5878 if Is_Child_Unit (Curr_Unit) and then Removed then
5879 for J in reverse 1 .. Num_Inner + 1 loop
5880 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5881 Use_Clauses (J);
5882 Install_Use_Clauses (Use_Clauses (J));
5883 end loop;
5884
5885 else
5886 for J in reverse 1 .. Num_Scopes loop
5887 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5888 Use_Clauses (J);
5889 Install_Use_Clauses (Use_Clauses (J));
5890 end loop;
5891 end if;
5892
5893 -- Restore status of instances. If one of them is a body, make its
5894 -- local entities visible again.
5895
5896 declare
5897 E : Entity_Id;
5898 Inst : Entity_Id;
5899
5900 begin
5901 for J in 1 .. N_Instances loop
5902 Inst := Instances (J);
5903 Set_Is_Generic_Instance (Inst, True);
5904
5905 if In_Package_Body (Inst)
5906 or else Ekind (S) in E_Procedure | E_Function
5907 then
5908 E := First_Entity (Instances (J));
5909 while Present (E) loop
5910 Set_Is_Immediately_Visible (E);
5911 Next_Entity (E);
5912 end loop;
5913 end if;
5914 end loop;
5915 end;
5916
5917 -- If generic unit is in current unit, current context is correct. Note
5918 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5919 -- enclosing scopes were removed.
5920
5921 else
5922 Instantiate_Package_Body
5923 (Body_Info =>
5924 ((Inst_Node => N,
5925 Act_Decl => Act_Decl,
5926 Fin_Scop => Empty,
5927 Config_Switches => Save_Config_Switches,
5928 Current_Sem_Unit => Current_Sem_Unit,
5929 Expander_Status => Expander_Active,
5930 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5931 Scope_Suppress => Scope_Suppress,
5932 Warnings => Save_Warnings)),
5933 Inlined_Body => True);
5934 end if;
5935 end Inline_Instance_Body;
5936
5937 -------------------------------------
5938 -- Analyze_Procedure_Instantiation --
5939 -------------------------------------
5940
5941 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5942 begin
5943 Analyze_Subprogram_Instantiation (N, E_Procedure);
5944 end Analyze_Procedure_Instantiation;
5945
5946 -----------------------------------
5947 -- Need_Subprogram_Instance_Body --
5948 -----------------------------------
5949
5950 function Need_Subprogram_Instance_Body
5951 (N : Node_Id;
5952 Subp : Entity_Id) return Boolean
5953 is
5954 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5955 -- Return True if E is an inlined subprogram, an inlined renaming or a
5956 -- subprogram nested in an inlined subprogram. The inlining machinery
5957 -- totally disregards nested subprograms since it considers that they
5958 -- will always be compiled if the parent is (see Inline.Is_Nested).
5959
5960 ------------------------------------
5961 -- Is_Inlined_Or_Child_Of_Inlined --
5962 ------------------------------------
5963
5964 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5965 Scop : Entity_Id;
5966
5967 begin
5968 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5969 return True;
5970 end if;
5971
5972 Scop := Scope (E);
5973 while Scop /= Standard_Standard loop
5974 if Is_Subprogram (Scop) and then Is_Inlined (Scop) then
5975 return True;
5976 end if;
5977
5978 Scop := Scope (Scop);
5979 end loop;
5980
5981 return False;
5982 end Is_Inlined_Or_Child_Of_Inlined;
5983
5984 -- Start of processing for Need_Subprogram_Instance_Body
5985
5986 begin
5987 -- Must be in the main unit or inlined (or child of inlined)
5988
5989 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5990
5991 -- Must be generating code or analyzing code in GNATprove mode
5992
5993 and then (Operating_Mode = Generate_Code
5994 or else (Operating_Mode = Check_Semantics
5995 and then GNATprove_Mode))
5996
5997 -- The body is needed when generating code (full expansion) and in
5998 -- in GNATprove mode (special expansion) for formal verification of
5999 -- the body itself.
6000
6001 and then (Expander_Active or GNATprove_Mode)
6002
6003 -- No point in inlining if ABE is inevitable
6004
6005 and then not Is_Known_Guaranteed_ABE (N)
6006
6007 -- Or if subprogram is eliminated
6008
6009 and then not Is_Eliminated (Subp)
6010 then
6011 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
6012 return True;
6013
6014 -- Here if not inlined, or we ignore the inlining
6015
6016 else
6017 return False;
6018 end if;
6019 end Need_Subprogram_Instance_Body;
6020
6021 --------------------------------------
6022 -- Analyze_Subprogram_Instantiation --
6023 --------------------------------------
6024
6025 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
6026 -- must be replaced by gotos which jump to the end of the routine in order
6027 -- to restore the Ghost and SPARK modes.
6028
6029 procedure Analyze_Subprogram_Instantiation
6030 (N : Node_Id;
6031 K : Entity_Kind)
6032 is
6033 Errs : constant Nat := Serious_Errors_Detected;
6034 Gen_Id : constant Node_Id := Name (N);
6035 Inst_Id : constant Entity_Id := Defining_Entity (N);
6036 Anon_Id : constant Entity_Id :=
6037 Make_Defining_Identifier (Sloc (Inst_Id),
6038 Chars => New_External_Name (Chars (Inst_Id), 'R'));
6039 Loc : constant Source_Ptr := Sloc (N);
6040
6041 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
6042 Act_Decl : Node_Id;
6043 Act_Spec : Node_Id;
6044 Act_Tree : Node_Id;
6045
6046 Env_Installed : Boolean := False;
6047 Gen_Unit : Entity_Id;
6048 Gen_Decl : Node_Id;
6049 Pack_Id : Entity_Id;
6050 Parent_Installed : Boolean := False;
6051
6052 Renamings : List_Id;
6053 -- The list of declarations that link formals and actuals of the
6054 -- instance. These are subtype declarations for formal types, and
6055 -- renaming declarations for other formals. The subprogram declaration
6056 -- for the instance is then appended to the list, and the last item on
6057 -- the list is the renaming declaration for the instance.
6058
6059 procedure Analyze_Instance_And_Renamings;
6060 -- The instance must be analyzed in a context that includes the mappings
6061 -- of generic parameters into actuals. We create a package declaration
6062 -- for this purpose, and a subprogram with an internal name within the
6063 -- package. The subprogram instance is simply an alias for the internal
6064 -- subprogram, declared in the current scope.
6065
6066 procedure Build_Subprogram_Renaming;
6067 -- If the subprogram is recursive, there are occurrences of the name of
6068 -- the generic within the body, which must resolve to the current
6069 -- instance. We add a renaming declaration after the declaration, which
6070 -- is available in the instance body, as well as in the analysis of
6071 -- aspects that appear in the generic. This renaming declaration is
6072 -- inserted after the instance declaration which it renames.
6073
6074 ------------------------------------
6075 -- Analyze_Instance_And_Renamings --
6076 ------------------------------------
6077
6078 procedure Analyze_Instance_And_Renamings is
6079 Def_Ent : constant Entity_Id := Defining_Entity (N);
6080 Pack_Decl : Node_Id;
6081
6082 begin
6083 if Nkind (Parent (N)) = N_Compilation_Unit then
6084
6085 -- For the case of a compilation unit, the container package has
6086 -- the same name as the instantiation, to insure that the binder
6087 -- calls the elaboration procedure with the right name. Copy the
6088 -- entity of the instance, which may have compilation level flags
6089 -- (e.g. Is_Child_Unit) set.
6090
6091 Pack_Id := New_Copy (Def_Ent);
6092
6093 else
6094 -- Otherwise we use the name of the instantiation concatenated
6095 -- with its source position to ensure uniqueness if there are
6096 -- several instantiations with the same name.
6097
6098 Pack_Id :=
6099 Make_Defining_Identifier (Loc,
6100 Chars => New_External_Name
6101 (Related_Id => Chars (Def_Ent),
6102 Suffix => "GP",
6103 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
6104 end if;
6105
6106 Pack_Decl :=
6107 Make_Package_Declaration (Loc,
6108 Specification => Make_Package_Specification (Loc,
6109 Defining_Unit_Name => Pack_Id,
6110 Visible_Declarations => Renamings,
6111 End_Label => Empty));
6112
6113 Set_Instance_Spec (N, Pack_Decl);
6114 Set_Is_Generic_Instance (Pack_Id);
6115 Set_Debug_Info_Needed (Pack_Id);
6116
6117 -- Case of not a compilation unit
6118
6119 if Nkind (Parent (N)) /= N_Compilation_Unit then
6120 Mark_Rewrite_Insertion (Pack_Decl);
6121 Insert_Before (N, Pack_Decl);
6122 Set_Has_Completion (Pack_Id);
6123
6124 -- Case of an instantiation that is a compilation unit
6125
6126 -- Place declaration on current node so context is complete for
6127 -- analysis (including nested instantiations), and for use in a
6128 -- context_clause (see Analyze_With_Clause).
6129
6130 else
6131 Set_Unit (Parent (N), Pack_Decl);
6132 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
6133 end if;
6134
6135 Analyze (Pack_Decl);
6136 Check_Formal_Packages (Pack_Id);
6137
6138 -- Body of the enclosing package is supplied when instantiating the
6139 -- subprogram body, after semantic analysis is completed.
6140
6141 if Nkind (Parent (N)) = N_Compilation_Unit then
6142
6143 -- Remove package itself from visibility, so it does not
6144 -- conflict with subprogram.
6145
6146 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
6147
6148 -- Set name and scope of internal subprogram so that the proper
6149 -- external name will be generated. The proper scope is the scope
6150 -- of the wrapper package. We need to generate debugging info for
6151 -- the internal subprogram, so set flag accordingly.
6152
6153 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
6154 Set_Scope (Anon_Id, Scope (Pack_Id));
6155
6156 -- Mark wrapper package as referenced, to avoid spurious warnings
6157 -- if the instantiation appears in various with_ clauses of
6158 -- subunits of the main unit.
6159
6160 Set_Referenced (Pack_Id);
6161 end if;
6162
6163 Set_Is_Generic_Instance (Anon_Id);
6164 Set_Debug_Info_Needed (Anon_Id);
6165 Act_Decl_Id := New_Copy (Anon_Id);
6166
6167 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
6168 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
6169 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
6170
6171 -- Subprogram instance comes from source only if generic does
6172
6173 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
6174
6175 -- If the instance is a child unit, mark the Id accordingly. Mark
6176 -- the anonymous entity as well, which is the real subprogram and
6177 -- which is used when the instance appears in a context clause.
6178 -- Similarly, propagate the Is_Eliminated flag to handle properly
6179 -- nested eliminated subprograms.
6180
6181 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
6182 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
6183 New_Overloaded_Entity (Act_Decl_Id);
6184 Check_Eliminated (Act_Decl_Id);
6185 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
6186
6187 if Nkind (Parent (N)) = N_Compilation_Unit then
6188
6189 -- In compilation unit case, kill elaboration checks on the
6190 -- instantiation, since they are never needed - the body is
6191 -- instantiated at the same point as the spec.
6192
6193 if Legacy_Elaboration_Checks then
6194 Set_Kill_Elaboration_Checks (Act_Decl_Id);
6195 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
6196 end if;
6197
6198 Set_Is_Compilation_Unit (Anon_Id);
6199 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
6200 end if;
6201
6202 -- The instance is not a freezing point for the new subprogram.
6203 -- The anonymous subprogram may have a freeze node, created for
6204 -- some delayed aspects. This freeze node must not be inherited
6205 -- by the visible subprogram entity.
6206
6207 Set_Is_Frozen (Act_Decl_Id, False);
6208 Set_Freeze_Node (Act_Decl_Id, Empty);
6209
6210 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
6211 Valid_Operator_Definition (Act_Decl_Id);
6212 end if;
6213
6214 Set_Alias (Act_Decl_Id, Anon_Id);
6215 Set_Has_Completion (Act_Decl_Id);
6216 Set_Related_Instance (Pack_Id, Act_Decl_Id);
6217
6218 if Nkind (Parent (N)) = N_Compilation_Unit then
6219 Set_Body_Required (Parent (N), False);
6220 end if;
6221 end Analyze_Instance_And_Renamings;
6222
6223 -------------------------------
6224 -- Build_Subprogram_Renaming --
6225 -------------------------------
6226
6227 procedure Build_Subprogram_Renaming is
6228 Renaming_Decl : Node_Id;
6229 Unit_Renaming : Node_Id;
6230
6231 begin
6232 Unit_Renaming :=
6233 Make_Subprogram_Renaming_Declaration (Loc,
6234 Specification =>
6235 Copy_Generic_Node
6236 (Specification (Original_Node (Gen_Decl)),
6237 Empty,
6238 Instantiating => True),
6239 Name => New_Occurrence_Of (Anon_Id, Loc));
6240
6241 -- The generic may be a child unit. The renaming needs an identifier
6242 -- with the proper name.
6243
6244 Set_Defining_Unit_Name (Specification (Unit_Renaming),
6245 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
6246
6247 -- If there is a formal subprogram with the same name as the unit
6248 -- itself, do not add this renaming declaration, to prevent
6249 -- ambiguities when there is a call with that name in the body.
6250
6251 Renaming_Decl := First (Renamings);
6252 while Present (Renaming_Decl) loop
6253 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
6254 and then
6255 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
6256 then
6257 exit;
6258 end if;
6259
6260 Next (Renaming_Decl);
6261 end loop;
6262
6263 if No (Renaming_Decl) then
6264 Append (Unit_Renaming, Renamings);
6265 end if;
6266 end Build_Subprogram_Renaming;
6267
6268 -- Local variables
6269
6270 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
6271 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
6272 Saved_ISMP : constant Boolean :=
6273 Ignore_SPARK_Mode_Pragmas_In_Instance;
6274 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
6275 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
6276 -- Save the Ghost and SPARK mode-related data to restore on exit
6277
6278 Vis_Prims_List : Elist_Id := No_Elist;
6279 -- List of primitives made temporarily visible in the instantiation
6280 -- to match the visibility of the formal type
6281
6282 -- Start of processing for Analyze_Subprogram_Instantiation
6283
6284 begin
6285 -- Preserve relevant elaboration-related attributes of the context which
6286 -- are no longer available or very expensive to recompute once analysis,
6287 -- resolution, and expansion are over.
6288
6289 Mark_Elaboration_Attributes
6290 (N_Id => N,
6291 Checks => True,
6292 Level => True,
6293 Modes => True,
6294 Warnings => True);
6295
6296 -- Very first thing: check for special Text_IO unit in case we are
6297 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
6298 -- such an instantiation is bogus (these are packages, not subprograms),
6299 -- but we get a better error message if we do this.
6300
6301 Check_Text_IO_Special_Unit (Gen_Id);
6302
6303 -- Make node global for error reporting
6304
6305 Instantiation_Node := N;
6306
6307 -- For package instantiations we turn off style checks, because they
6308 -- will have been emitted in the generic. For subprogram instantiations
6309 -- we want to apply at least the check on overriding indicators so we
6310 -- do not modify the style check status.
6311
6312 -- The renaming declarations for the actuals do not come from source and
6313 -- will not generate spurious warnings.
6314
6315 Preanalyze_Actuals (N);
6316
6317 Init_Env;
6318 Env_Installed := True;
6319 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
6320 Gen_Unit := Entity (Gen_Id);
6321
6322 -- A subprogram instantiation is Ghost when it is subject to pragma
6323 -- Ghost or the generic template is Ghost. Set the mode now to ensure
6324 -- that any nodes generated during analysis and expansion are marked as
6325 -- Ghost.
6326
6327 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
6328
6329 Generate_Reference (Gen_Unit, Gen_Id);
6330
6331 if Nkind (Gen_Id) = N_Identifier
6332 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
6333 then
6334 Error_Msg_NE
6335 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
6336 end if;
6337
6338 if Etype (Gen_Unit) = Any_Type then
6339 Restore_Env;
6340 goto Leave;
6341 end if;
6342
6343 -- Verify that it is a generic subprogram of the right kind, and that
6344 -- it does not lead to a circular instantiation.
6345
6346 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
6347 Error_Msg_NE
6348 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
6349
6350 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
6351 Error_Msg_NE
6352 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
6353
6354 elsif In_Open_Scopes (Gen_Unit) then
6355 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
6356
6357 else
6358 Mutate_Ekind (Inst_Id, K);
6359 Set_Scope (Inst_Id, Current_Scope);
6360
6361 Set_Entity (Gen_Id, Gen_Unit);
6362
6363 if In_Extended_Main_Source_Unit (N) then
6364 Set_Is_Instantiated (Gen_Unit);
6365 Generate_Reference (Gen_Unit, N);
6366 end if;
6367
6368 -- If renaming, get original unit
6369
6370 if Present (Renamed_Entity (Gen_Unit))
6371 and then Is_Generic_Subprogram (Renamed_Entity (Gen_Unit))
6372 then
6373 Gen_Unit := Renamed_Entity (Gen_Unit);
6374 Set_Is_Instantiated (Gen_Unit);
6375 Generate_Reference (Gen_Unit, N);
6376 end if;
6377
6378 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
6379 Error_Msg_Node_2 := Current_Scope;
6380 Error_Msg_NE
6381 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
6382 Circularity_Detected := True;
6383 Restore_Hidden_Primitives (Vis_Prims_List);
6384 goto Leave;
6385 end if;
6386
6387 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
6388
6389 -- Initialize renamings map, for error checking
6390
6391 Generic_Renamings.Set_Last (0);
6392 Generic_Renamings_HTable.Reset;
6393
6394 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
6395
6396 -- Copy original generic tree, to produce text for instantiation
6397
6398 Act_Tree :=
6399 Copy_Generic_Node
6400 (Original_Node (Gen_Decl), Empty, Instantiating => True);
6401
6402 -- Inherit overriding indicator from instance node
6403
6404 Act_Spec := Specification (Act_Tree);
6405 Set_Must_Override (Act_Spec, Must_Override (N));
6406 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
6407
6408 Renamings :=
6409 Analyze_Associations
6410 (I_Node => N,
6411 Formals => Generic_Formal_Declarations (Act_Tree),
6412 F_Copy => Generic_Formal_Declarations (Gen_Decl));
6413
6414 Vis_Prims_List := Check_Hidden_Primitives (Renamings);
6415
6416 -- The subprogram itself cannot contain a nested instance, so the
6417 -- current parent is left empty.
6418
6419 Set_Instance_Env (Gen_Unit, Empty);
6420
6421 -- Build the subprogram declaration, which does not appear in the
6422 -- generic template, and give it a sloc consistent with that of the
6423 -- template.
6424
6425 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
6426 Set_Generic_Parent (Act_Spec, Gen_Unit);
6427 Act_Decl :=
6428 Make_Subprogram_Declaration (Sloc (Act_Spec),
6429 Specification => Act_Spec);
6430
6431 -- The aspects have been copied previously, but they have to be
6432 -- linked explicitly to the new subprogram declaration. Explicit
6433 -- pre/postconditions on the instance are analyzed below, in a
6434 -- separate step.
6435
6436 Move_Aspects (Act_Tree, To => Act_Decl);
6437 Set_Categorization_From_Pragmas (Act_Decl);
6438
6439 if Parent_Installed then
6440 Hide_Current_Scope;
6441 end if;
6442
6443 Append (Act_Decl, Renamings);
6444
6445 -- Contract-related source pragmas that follow a generic subprogram
6446 -- must be instantiated explicitly because they are not part of the
6447 -- subprogram template.
6448
6449 Instantiate_Subprogram_Contract
6450 (Original_Node (Gen_Decl), Renamings);
6451
6452 Build_Subprogram_Renaming;
6453
6454 -- If the context of the instance is subject to SPARK_Mode "off" or
6455 -- the annotation is altogether missing, set the global flag which
6456 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
6457 -- the instance. This should be done prior to analyzing the instance.
6458
6459 if SPARK_Mode /= On then
6460 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
6461 end if;
6462
6463 -- If the context of an instance is not subject to SPARK_Mode "off",
6464 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
6465 -- the latter should be the one applicable to the instance.
6466
6467 if not Ignore_SPARK_Mode_Pragmas_In_Instance
6468 and then Saved_SM /= Off
6469 and then Present (SPARK_Pragma (Gen_Unit))
6470 then
6471 Set_SPARK_Mode (Gen_Unit);
6472 end if;
6473
6474 -- Need to mark Anon_Id intrinsic before calling
6475 -- Analyze_Instance_And_Renamings because this flag may be propagated
6476 -- to other nodes.
6477
6478 if Is_Intrinsic_Subprogram (Gen_Unit) then
6479 Set_Is_Intrinsic_Subprogram (Anon_Id);
6480 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
6481 end if;
6482
6483 Analyze_Instance_And_Renamings;
6484
6485 -- Restore SPARK_Mode from the context after analysis of the package
6486 -- declaration, so that the SPARK_Mode on the generic spec does not
6487 -- apply to the pending instance for the instance body.
6488
6489 if not Ignore_SPARK_Mode_Pragmas_In_Instance
6490 and then Saved_SM /= Off
6491 and then Present (SPARK_Pragma (Gen_Unit))
6492 then
6493 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6494 end if;
6495
6496 -- If the generic is marked Import (Intrinsic), then so is the
6497 -- instance; this indicates that there is no body to instantiate.
6498 -- We also copy the interface name in case this is handled by the
6499 -- back-end and deal with an instance of unchecked conversion.
6500
6501 if Is_Intrinsic_Subprogram (Gen_Unit) then
6502 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
6503 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
6504
6505 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
6506 Validate_Unchecked_Conversion (N, Act_Decl_Id);
6507 end if;
6508 end if;
6509
6510 -- Inherit convention from generic unit. Intrinsic convention, as for
6511 -- an instance of unchecked conversion, is not inherited because an
6512 -- explicit Ada instance has been created.
6513
6514 if Has_Convention_Pragma (Gen_Unit)
6515 and then Convention (Gen_Unit) /= Convention_Intrinsic
6516 then
6517 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
6518 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
6519 end if;
6520
6521 Generate_Definition (Act_Decl_Id);
6522
6523 -- Inherit all inlining-related flags which apply to the generic in
6524 -- the subprogram and its declaration.
6525
6526 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
6527 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
6528
6529 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
6530 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
6531
6532 Set_Has_Pragma_Inline_Always
6533 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
6534 Set_Has_Pragma_Inline_Always
6535 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
6536
6537 Set_Has_Pragma_No_Inline
6538 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
6539 Set_Has_Pragma_No_Inline
6540 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
6541
6542 -- Propagate No_Raise if pragma applied to generic unit. This must
6543 -- be done explicitly because the pragma does not appear in generic
6544 -- declarations (unlike the aspect).
6545
6546 if No_Raise (Gen_Unit) then
6547 Set_No_Raise (Act_Decl_Id);
6548 Set_No_Raise (Anon_Id);
6549 end if;
6550
6551 -- Likewise for No_Return
6552
6553 if No_Return (Gen_Unit) then
6554 Set_No_Return (Act_Decl_Id);
6555 Set_No_Return (Anon_Id);
6556 end if;
6557
6558 -- Mark both the instance spec and the anonymous package in case the
6559 -- body is instantiated at a later pass. This preserves the original
6560 -- context in effect for the body.
6561
6562 if SPARK_Mode /= On then
6563 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
6564 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
6565 end if;
6566
6567 if Legacy_Elaboration_Checks
6568 and then not Is_Intrinsic_Subprogram (Gen_Unit)
6569 then
6570 Check_Elab_Instantiation (N);
6571 end if;
6572
6573 -- Save the scenario for later examination by the ABE Processing
6574 -- phase.
6575
6576 Record_Elaboration_Scenario (N);
6577
6578 -- The instantiation results in a guaranteed ABE. Create a completing
6579 -- body for the subprogram declaration because the real body will not
6580 -- be instantiated.
6581
6582 if Is_Known_Guaranteed_ABE (N) then
6583 Provide_Completing_Bodies (Instance_Spec (N));
6584 end if;
6585
6586 if Is_Dispatching_Operation (Act_Decl_Id)
6587 and then Ada_Version >= Ada_2005
6588 then
6589 declare
6590 Formal : Entity_Id;
6591
6592 begin
6593 Formal := First_Formal (Act_Decl_Id);
6594 while Present (Formal) loop
6595 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
6596 and then Is_Controlling_Formal (Formal)
6597 and then not Can_Never_Be_Null (Formal)
6598 then
6599 Error_Msg_NE
6600 ("access parameter& is controlling,", N, Formal);
6601 Error_Msg_NE
6602 ("\corresponding parameter of & must be explicitly "
6603 & "null-excluding", N, Gen_Id);
6604 end if;
6605
6606 Next_Formal (Formal);
6607 end loop;
6608 end;
6609 end if;
6610
6611 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
6612
6613 Validate_Categorization_Dependency (N, Act_Decl_Id);
6614
6615 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
6616 Inherit_Context (Gen_Decl, N);
6617
6618 Restore_Private_Views (Pack_Id, False);
6619
6620 -- If the context requires a full instantiation, mark node for
6621 -- subsequent construction of the body.
6622
6623 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
6624 Check_Forward_Instantiation (Gen_Decl);
6625
6626 -- The wrapper package is always delayed, because it does not
6627 -- constitute a freeze point, but to insure that the freeze node
6628 -- is placed properly, it is created directly when instantiating
6629 -- the body (otherwise the freeze node might appear to early for
6630 -- nested instantiations).
6631
6632 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6633 Rewrite (N, Unit (Parent (N)));
6634 Set_Unit (Parent (N), N);
6635 end if;
6636
6637 -- Replace instance node for library-level instantiations of
6638 -- intrinsic subprograms.
6639
6640 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6641 Rewrite (N, Unit (Parent (N)));
6642 Set_Unit (Parent (N), N);
6643 end if;
6644
6645 if Parent_Installed then
6646 Remove_Parent;
6647 end if;
6648
6649 Restore_Hidden_Primitives (Vis_Prims_List);
6650 Restore_Env;
6651 Env_Installed := False;
6652 Generic_Renamings.Set_Last (0);
6653 Generic_Renamings_HTable.Reset;
6654 end if;
6655
6656 <<Leave>>
6657 -- Analyze aspects in declaration if no errors appear in the instance.
6658
6659 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
6660 Analyze_Aspect_Specifications (N, Act_Decl_Id);
6661 end if;
6662
6663 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6664 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6665 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6666
6667 exception
6668 when Instantiation_Error =>
6669 if Parent_Installed then
6670 Remove_Parent;
6671 end if;
6672
6673 if Env_Installed then
6674 Restore_Env;
6675 end if;
6676
6677 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6678 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6679 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6680 end Analyze_Subprogram_Instantiation;
6681
6682 ---------------------------
6683 -- Get_Associated_Entity --
6684 ---------------------------
6685
6686 function Get_Associated_Entity (Id : Entity_Id) return Entity_Id is
6687 Assoc : Entity_Id;
6688
6689 begin
6690 Assoc := Associated_Entity (Id);
6691
6692 if Present (Assoc) then
6693 while Present (Associated_Entity (Assoc)) loop
6694 Assoc := Associated_Entity (Assoc);
6695 end loop;
6696 end if;
6697
6698 return Assoc;
6699 end Get_Associated_Entity;
6700
6701 -------------------------
6702 -- Get_Associated_Node --
6703 -------------------------
6704
6705 function Get_Associated_Node (N : Node_Id) return Node_Id is
6706 Assoc : Node_Id;
6707
6708 begin
6709 Assoc := Associated_Node (N);
6710
6711 if Nkind (Assoc) /= Nkind (N) then
6712 return Assoc;
6713
6714 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
6715 return Assoc;
6716
6717 else
6718 -- If the node is part of an inner generic, it may itself have been
6719 -- remapped into a further generic copy. Associated_Node is otherwise
6720 -- used for the entity of the node, and will be of a different node
6721 -- kind, or else N has been rewritten as a literal or function call.
6722
6723 while Present (Associated_Node (Assoc))
6724 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6725 loop
6726 Assoc := Associated_Node (Assoc);
6727 end loop;
6728
6729 -- Follow an additional link in case the final node was rewritten.
6730 -- This can only happen with nested generic units.
6731
6732 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6733 and then Present (Associated_Node (Assoc))
6734 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6735 | N_Explicit_Dereference
6736 | N_Integer_Literal
6737 | N_Real_Literal
6738 | N_String_Literal
6739 then
6740 Assoc := Associated_Node (Assoc);
6741 end if;
6742
6743 -- An additional special case: an unconstrained type in an object
6744 -- declaration may have been rewritten as a local subtype constrained
6745 -- by the expression in the declaration. We need to recover the
6746 -- original entity, which may be global.
6747
6748 if Present (Original_Node (Assoc))
6749 and then Nkind (Parent (N)) = N_Object_Declaration
6750 then
6751 Assoc := Original_Node (Assoc);
6752 end if;
6753
6754 return Assoc;
6755 end if;
6756 end Get_Associated_Node;
6757
6758 -----------------------------------
6759 -- Build_Subprogram_Decl_Wrapper --
6760 -----------------------------------
6761
6762 function Build_Subprogram_Decl_Wrapper
6763 (Formal_Subp : Entity_Id) return Node_Id
6764 is
6765 Loc : constant Source_Ptr := Sloc (Current_Scope);
6766 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6767 Decl : Node_Id;
6768 Subp : Entity_Id;
6769 Parm_Spec : Node_Id;
6770 Profile : List_Id := New_List;
6771 Spec : Node_Id;
6772 Form_F : Entity_Id;
6773 New_F : Entity_Id;
6774
6775 begin
6776 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6777 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6778 Set_Is_Generic_Actual_Subprogram (Subp);
6779
6780 Profile := Parameter_Specifications (
6781 New_Copy_Tree
6782 (Specification (Unit_Declaration_Node (Formal_Subp))));
6783
6784 Form_F := First_Formal (Formal_Subp);
6785 Parm_Spec := First (Profile);
6786
6787 -- Create new entities for the formals. Reset entities so that
6788 -- parameter types are properly resolved when wrapper declaration
6789 -- is analyzed.
6790
6791 while Present (Parm_Spec) loop
6792 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6793 Set_Defining_Identifier (Parm_Spec, New_F);
6794 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6795 Next (Parm_Spec);
6796 Next_Formal (Form_F);
6797 end loop;
6798
6799 if Ret_Type = Standard_Void_Type then
6800 Spec :=
6801 Make_Procedure_Specification (Loc,
6802 Defining_Unit_Name => Subp,
6803 Parameter_Specifications => Profile);
6804 else
6805 Spec :=
6806 Make_Function_Specification (Loc,
6807 Defining_Unit_Name => Subp,
6808 Parameter_Specifications => Profile,
6809 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6810 end if;
6811
6812 Decl :=
6813 Make_Subprogram_Declaration (Loc, Specification => Spec);
6814
6815 return Decl;
6816 end Build_Subprogram_Decl_Wrapper;
6817
6818 -----------------------------------
6819 -- Build_Subprogram_Body_Wrapper --
6820 -----------------------------------
6821
6822 function Build_Subprogram_Body_Wrapper
6823 (Formal_Subp : Entity_Id;
6824 Actual_Name : Node_Id) return Node_Id
6825 is
6826 Loc : constant Source_Ptr := Sloc (Current_Scope);
6827 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6828 Spec_Node : constant Node_Id :=
6829 Specification
6830 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6831 Act : Node_Id;
6832 Actuals : List_Id;
6833 Body_Node : Node_Id;
6834 Stmt : Node_Id;
6835 begin
6836 Actuals := New_List;
6837 Act := First (Parameter_Specifications (Spec_Node));
6838
6839 while Present (Act) loop
6840 Append_To (Actuals,
6841 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6842 Next (Act);
6843 end loop;
6844
6845 if Ret_Type = Standard_Void_Type then
6846 Stmt := Make_Procedure_Call_Statement (Loc,
6847 Name => Actual_Name,
6848 Parameter_Associations => Actuals);
6849
6850 else
6851 Stmt := Make_Simple_Return_Statement (Loc,
6852 Expression =>
6853 Make_Function_Call (Loc,
6854 Name => Actual_Name,
6855 Parameter_Associations => Actuals));
6856 end if;
6857
6858 Body_Node := Make_Subprogram_Body (Loc,
6859 Specification => Spec_Node,
6860 Declarations => New_List,
6861 Handled_Statement_Sequence =>
6862 Make_Handled_Sequence_Of_Statements (Loc,
6863 Statements => New_List (Stmt)));
6864
6865 return Body_Node;
6866 end Build_Subprogram_Body_Wrapper;
6867
6868 -------------------------------
6869 -- Build_Subprogram_Wrappers --
6870 -------------------------------
6871
6872 procedure Build_Subprogram_Wrappers
6873 (Match, Analyzed_Formal : Node_Id; Renamings : List_Id)
6874 is
6875 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result;
6876 -- Adjust Sloc so that errors will be reported on the instance rather
6877 -- than the generic.
6878
6879 ------------------------
6880 -- Adjust_Aspect_Sloc --
6881 ------------------------
6882
6883 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result is
6884 begin
6885 Adjust_Instantiation_Sloc (N, S_Adjustment);
6886 return OK;
6887 end Adjust_Aspect_Sloc;
6888
6889 procedure Adjust_Aspect_Slocs is new
6890 Traverse_Proc (Adjust_Aspect_Sloc);
6891
6892 Formal : constant Entity_Id :=
6893 Defining_Unit_Name (Specification (Analyzed_Formal));
6894 Aspect_Spec : Node_Id;
6895 Decl_Node : Node_Id;
6896 Actual_Name : Node_Id;
6897
6898 -- Start of processing for Build_Subprogram_Wrappers
6899
6900 begin
6901 -- Create declaration for wrapper subprogram.
6902 -- The actual can be overloaded, in which case it will be
6903 -- resolved when the call in the wrapper body is analyzed.
6904 -- We attach the possible interpretations of the actual to
6905 -- the name to be used in the call in the wrapper body.
6906
6907 if Is_Entity_Name (Match) then
6908 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
6909
6910 if Is_Overloaded (Match) then
6911 Save_Interps (Match, Actual_Name);
6912 end if;
6913
6914 else
6915 -- Use renaming declaration created when analyzing actual.
6916 -- This may be incomplete if there are several formal
6917 -- subprograms whose actual is an attribute ???
6918
6919 declare
6920 Renaming_Decl : constant Node_Id := Last (Renamings);
6921
6922 begin
6923 Actual_Name := New_Occurrence_Of
6924 (Defining_Entity (Renaming_Decl), Sloc (Match));
6925 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
6926 end;
6927 end if;
6928
6929 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
6930
6931 -- Transfer aspect specifications from formal subprogram to wrapper
6932
6933 Set_Aspect_Specifications (Decl_Node,
6934 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
6935
6936 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
6937 while Present (Aspect_Spec) loop
6938 Adjust_Aspect_Slocs (Aspect_Spec);
6939 Set_Analyzed (Aspect_Spec, False);
6940 Next (Aspect_Spec);
6941 end loop;
6942
6943 Append_To (Renamings, Decl_Node);
6944
6945 -- Create corresponding body, and append it to association list
6946 -- that appears at the head of the declarations in the instance.
6947 -- The subprogram may be called in the analysis of subsequent
6948 -- actuals.
6949
6950 Append_To (Renamings,
6951 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
6952 end Build_Subprogram_Wrappers;
6953
6954 -------------------------------------------
6955 -- Build_Instance_Compilation_Unit_Nodes --
6956 -------------------------------------------
6957
6958 procedure Build_Instance_Compilation_Unit_Nodes
6959 (N : Node_Id;
6960 Act_Body : Node_Id;
6961 Act_Decl : Node_Id)
6962 is
6963 Decl_Cunit : Node_Id;
6964 Body_Cunit : Node_Id;
6965 Citem : Node_Id;
6966 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6967 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6968
6969 begin
6970 -- A new compilation unit node is built for the instance declaration.
6971 -- It relocates the auxiliary declaration node from the compilation unit
6972 -- where the instance appeared, so that declarations that originally
6973 -- followed the instance will be attached to the spec compilation unit.
6974
6975 Decl_Cunit :=
6976 Make_Compilation_Unit (Sloc (N),
6977 Context_Items => Empty_List,
6978 Unit => Act_Decl,
6979 Aux_Decls_Node => Relocate_Node (Aux_Decls_Node (Parent (N))));
6980
6981 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6982
6983 -- The new compilation unit is linked to its body, but both share the
6984 -- same file, so we do not set Body_Required on the new unit so as not
6985 -- to create a spurious dependency on a non-existent body in the ali.
6986 -- This simplifies CodePeer unit traversal.
6987
6988 -- We use the original instantiation compilation unit as the resulting
6989 -- compilation unit of the instance, since this is the main unit.
6990
6991 Rewrite (N, Act_Body);
6992
6993 Body_Cunit := Parent (N);
6994
6995 -- The two compilation unit nodes are linked by the Library_Unit field
6996
6997 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6998 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6999
7000 -- Preserve the private nature of the package if needed
7001
7002 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
7003
7004 -- If the instance is not the main unit, its context, categorization
7005 -- and elaboration entity are not relevant to the compilation.
7006
7007 if Body_Cunit /= Cunit (Main_Unit) then
7008 Make_Instance_Unit (Body_Cunit, In_Main => False);
7009 return;
7010 end if;
7011
7012 -- The context clause items on the instantiation, which are now attached
7013 -- to the body compilation unit (since the body overwrote the original
7014 -- instantiation node), semantically belong on the spec, so copy them
7015 -- there. It's harmless to leave them on the body as well. In fact one
7016 -- could argue that they belong in both places.
7017
7018 Citem := First (Context_Items (Body_Cunit));
7019 while Present (Citem) loop
7020 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
7021 Next (Citem);
7022 end loop;
7023
7024 -- Propagate categorization flags on packages, so that they appear in
7025 -- the ali file for the spec of the unit.
7026
7027 if Ekind (New_Main) = E_Package then
7028 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
7029 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
7030 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
7031 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
7032 Set_Is_Remote_Call_Interface
7033 (Old_Main, Is_Remote_Call_Interface (New_Main));
7034 end if;
7035
7036 -- Make entry in Units table, so that binder can generate call to
7037 -- elaboration procedure for body, if any.
7038
7039 Make_Instance_Unit (Body_Cunit, In_Main => True);
7040 Main_Unit_Entity := New_Main;
7041 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
7042
7043 -- Build elaboration entity, since the instance may certainly generate
7044 -- elaboration code requiring a flag for protection.
7045
7046 Build_Elaboration_Entity (Decl_Cunit, New_Main);
7047 end Build_Instance_Compilation_Unit_Nodes;
7048
7049 --------------------------------
7050 -- Check_Abbreviated_Instance --
7051 --------------------------------
7052
7053 procedure Check_Abbreviated_Instance
7054 (N : Node_Id;
7055 Parent_Installed : in out Boolean)
7056 is
7057 Inst_Node : Node_Id;
7058
7059 begin
7060 if Nkind (N) = N_Package_Specification
7061 and then Is_Abbreviated_Instance (Defining_Entity (N))
7062 then
7063 Inst_Node := Get_Unit_Instantiation_Node (Defining_Entity (N));
7064 Check_Generic_Child_Unit (Name (Inst_Node), Parent_Installed);
7065 end if;
7066 end Check_Abbreviated_Instance;
7067
7068 -----------------------------
7069 -- Check_Access_Definition --
7070 -----------------------------
7071
7072 procedure Check_Access_Definition (N : Node_Id) is
7073 begin
7074 pragma Assert
7075 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
7076 null;
7077 end Check_Access_Definition;
7078
7079 -----------------------------------
7080 -- Check_Formal_Package_Instance --
7081 -----------------------------------
7082
7083 -- If the formal has specific parameters, they must match those of the
7084 -- actual. Both of them are instances, and the renaming declarations for
7085 -- their formal parameters appear in the same order in both. The analyzed
7086 -- formal has been analyzed in the context of the current instance.
7087
7088 procedure Check_Formal_Package_Instance
7089 (Formal_Pack : Entity_Id;
7090 Actual_Pack : Entity_Id)
7091 is
7092 E1 : Entity_Id := First_Entity (Actual_Pack);
7093 E2 : Entity_Id := First_Entity (Formal_Pack);
7094 Prev_E1 : Entity_Id;
7095
7096 Expr1 : Node_Id;
7097 Expr2 : Node_Id;
7098
7099 procedure Check_Mismatch (B : Boolean);
7100 -- Common error routine for mismatch between the parameters of the
7101 -- actual instance and those of the formal package.
7102
7103 function Is_Defaulted (Param : Entity_Id) return Boolean;
7104 -- If the formal package has partly box-initialized formals, skip
7105 -- conformance check for these formals. Previously the code assumed
7106 -- that box initialization for a formal package applied to all its
7107 -- formal parameters.
7108
7109 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
7110 -- The formal may come from a nested formal package, and the actual may
7111 -- have been constant-folded. To determine whether the two denote the
7112 -- same entity we may have to traverse several definitions to recover
7113 -- the ultimate entity that they refer to.
7114
7115 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
7116 -- The formal and the actual must be identical, but if both are
7117 -- given by attributes they end up renaming different generated bodies,
7118 -- and we must verify that the attributes themselves match.
7119
7120 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
7121 -- Similarly, if the formal comes from a nested formal package, the
7122 -- actual may designate the formal through multiple renamings, which
7123 -- have to be followed to determine the original variable in question.
7124
7125 --------------------
7126 -- Check_Mismatch --
7127 --------------------
7128
7129 procedure Check_Mismatch (B : Boolean) is
7130 -- A Formal_Type_Declaration for a derived private type is rewritten
7131 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
7132 -- which is why we examine the original node.
7133
7134 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
7135
7136 begin
7137 if Kind = N_Formal_Type_Declaration then
7138 return;
7139
7140 elsif Kind in N_Formal_Object_Declaration
7141 | N_Formal_Package_Declaration
7142 | N_Formal_Subprogram_Declaration
7143 then
7144 null;
7145
7146 -- Ada 2012: If both formal and actual are incomplete types they
7147 -- are conformant.
7148
7149 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
7150 null;
7151
7152 elsif B then
7153 Error_Msg_NE
7154 ("actual for & in actual instance does not match formal",
7155 Parent (Actual_Pack), E1);
7156 end if;
7157 end Check_Mismatch;
7158
7159 ------------------
7160 -- Is_Defaulted --
7161 ------------------
7162
7163 function Is_Defaulted (Param : Entity_Id) return Boolean is
7164 Assoc : Node_Id;
7165
7166 begin
7167 Assoc :=
7168 First (Generic_Associations (Parent
7169 (Associated_Formal_Package (Actual_Pack))));
7170
7171 while Present (Assoc) loop
7172 if Nkind (Assoc) = N_Others_Choice then
7173 return True;
7174
7175 elsif Nkind (Assoc) = N_Generic_Association
7176 and then Chars (Selector_Name (Assoc)) = Chars (Param)
7177 then
7178 return Box_Present (Assoc);
7179 end if;
7180
7181 Next (Assoc);
7182 end loop;
7183
7184 return False;
7185 end Is_Defaulted;
7186
7187 --------------------------------
7188 -- Same_Instantiated_Constant --
7189 --------------------------------
7190
7191 function Same_Instantiated_Constant
7192 (E1, E2 : Entity_Id) return Boolean
7193 is
7194 Ent : Entity_Id;
7195
7196 begin
7197 Ent := E2;
7198 while Present (Ent) loop
7199 if E1 = Ent then
7200 return True;
7201
7202 elsif Ekind (Ent) /= E_Constant then
7203 return False;
7204
7205 elsif Is_Entity_Name (Constant_Value (Ent)) then
7206 if Entity (Constant_Value (Ent)) = E1 then
7207 return True;
7208 else
7209 Ent := Entity (Constant_Value (Ent));
7210 end if;
7211
7212 -- The actual may be a constant that has been folded. Recover
7213 -- original name.
7214
7215 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
7216 Ent := Entity (Original_Node (Constant_Value (Ent)));
7217
7218 else
7219 return False;
7220 end if;
7221 end loop;
7222
7223 return False;
7224 end Same_Instantiated_Constant;
7225
7226 --------------------------------
7227 -- Same_Instantiated_Function --
7228 --------------------------------
7229
7230 function Same_Instantiated_Function
7231 (E1, E2 : Entity_Id) return Boolean
7232 is
7233 U1, U2 : Node_Id;
7234 begin
7235 if Alias (E1) = Alias (E2) then
7236 return True;
7237
7238 elsif Present (Alias (E2)) then
7239 U1 := Original_Node (Unit_Declaration_Node (E1));
7240 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
7241
7242 return Nkind (U1) = N_Subprogram_Renaming_Declaration
7243 and then Nkind (Name (U1)) = N_Attribute_Reference
7244
7245 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
7246 and then Nkind (Name (U2)) = N_Attribute_Reference
7247
7248 and then
7249 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
7250 else
7251 return False;
7252 end if;
7253 end Same_Instantiated_Function;
7254
7255 --------------------------------
7256 -- Same_Instantiated_Variable --
7257 --------------------------------
7258
7259 function Same_Instantiated_Variable
7260 (E1, E2 : Entity_Id) return Boolean
7261 is
7262 function Original_Entity (E : Entity_Id) return Entity_Id;
7263 -- Follow chain of renamings to the ultimate ancestor
7264
7265 ---------------------
7266 -- Original_Entity --
7267 ---------------------
7268
7269 function Original_Entity (E : Entity_Id) return Entity_Id is
7270 Orig : Entity_Id;
7271
7272 begin
7273 Orig := E;
7274 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
7275 and then Present (Renamed_Object (Orig))
7276 and then Is_Entity_Name (Renamed_Object (Orig))
7277 loop
7278 Orig := Entity (Renamed_Object (Orig));
7279 end loop;
7280
7281 return Orig;
7282 end Original_Entity;
7283
7284 -- Start of processing for Same_Instantiated_Variable
7285
7286 begin
7287 return Ekind (E1) = Ekind (E2)
7288 and then Original_Entity (E1) = Original_Entity (E2);
7289 end Same_Instantiated_Variable;
7290
7291 -- Start of processing for Check_Formal_Package_Instance
7292
7293 begin
7294 Prev_E1 := E1;
7295 while Present (E1) and then Present (E2) loop
7296 exit when Ekind (E1) = E_Package
7297 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
7298
7299 -- If the formal is the renaming of the formal package, this
7300 -- is the end of its formal part, which may occur before the
7301 -- end of the formal part in the actual in the presence of
7302 -- defaulted parameters in the formal package.
7303
7304 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
7305 and then Renamed_Entity (E2) = Scope (E2);
7306
7307 -- The analysis of the actual may generate additional internal
7308 -- entities. If the formal is defaulted, there is no corresponding
7309 -- analysis and the internal entities must be skipped, until we
7310 -- find corresponding entities again.
7311
7312 if Comes_From_Source (E2)
7313 and then not Comes_From_Source (E1)
7314 and then Chars (E1) /= Chars (E2)
7315 then
7316 while Present (E1) and then Chars (E1) /= Chars (E2) loop
7317 Next_Entity (E1);
7318 end loop;
7319 end if;
7320
7321 if No (E1) then
7322 return;
7323
7324 -- Entities may be declared without full declaration, such as
7325 -- itypes and predefined operators (concatenation for arrays, eg).
7326 -- Skip it and keep the formal entity to find a later match for it.
7327
7328 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
7329 E1 := Prev_E1;
7330 goto Next_E;
7331
7332 -- If the formal entity comes from a formal declaration, it was
7333 -- defaulted in the formal package, and no check is needed on it.
7334
7335 elsif Nkind (Original_Node (Parent (E2))) in
7336 N_Formal_Object_Declaration | N_Formal_Type_Declaration
7337 then
7338 -- If the formal is a tagged type the corresponding class-wide
7339 -- type has been generated as well, and it must be skipped.
7340
7341 if Is_Type (E2) and then Is_Tagged_Type (E2) then
7342 Next_Entity (E2);
7343 end if;
7344
7345 goto Next_E;
7346
7347 -- Ditto for defaulted formal subprograms.
7348
7349 elsif Is_Overloadable (E1)
7350 and then Nkind (Unit_Declaration_Node (E2)) in
7351 N_Formal_Subprogram_Declaration
7352 then
7353 goto Next_E;
7354
7355 elsif Is_Defaulted (E1) then
7356 goto Next_E;
7357
7358 elsif Is_Type (E1) then
7359
7360 -- Subtypes must statically match. E1, E2 are the local entities
7361 -- that are subtypes of the actuals. Itypes generated for other
7362 -- parameters need not be checked, the check will be performed
7363 -- on the parameters themselves.
7364
7365 -- If E2 is a formal type declaration, it is a defaulted parameter
7366 -- and needs no checking.
7367
7368 if not Is_Itype (E1) and then not Is_Itype (E2) then
7369 Check_Mismatch
7370 (not Is_Type (E2)
7371 or else Etype (E1) /= Etype (E2)
7372 or else not Subtypes_Statically_Match (E1, E2));
7373 end if;
7374
7375 elsif Ekind (E1) = E_Constant then
7376
7377 -- IN parameters must denote the same static value, or the same
7378 -- constant, or the literal null.
7379
7380 Expr1 := Expression (Parent (E1));
7381
7382 if Ekind (E2) /= E_Constant then
7383 Check_Mismatch (True);
7384 goto Next_E;
7385 else
7386 Expr2 := Expression (Parent (E2));
7387 end if;
7388
7389 if Is_OK_Static_Expression (Expr1) then
7390 if not Is_OK_Static_Expression (Expr2) then
7391 Check_Mismatch (True);
7392
7393 elsif Is_Discrete_Type (Etype (E1)) then
7394 declare
7395 V1 : constant Uint := Expr_Value (Expr1);
7396 V2 : constant Uint := Expr_Value (Expr2);
7397 begin
7398 Check_Mismatch (V1 /= V2);
7399 end;
7400
7401 elsif Is_Real_Type (Etype (E1)) then
7402 declare
7403 V1 : constant Ureal := Expr_Value_R (Expr1);
7404 V2 : constant Ureal := Expr_Value_R (Expr2);
7405 begin
7406 Check_Mismatch (V1 /= V2);
7407 end;
7408
7409 elsif Is_String_Type (Etype (E1))
7410 and then Nkind (Expr1) = N_String_Literal
7411 then
7412 if Nkind (Expr2) /= N_String_Literal then
7413 Check_Mismatch (True);
7414 else
7415 Check_Mismatch
7416 (not String_Equal (Strval (Expr1), Strval (Expr2)));
7417 end if;
7418 end if;
7419
7420 elsif Is_Entity_Name (Expr1) then
7421 if Is_Entity_Name (Expr2) then
7422 if Entity (Expr1) = Entity (Expr2) then
7423 null;
7424 else
7425 Check_Mismatch
7426 (not Same_Instantiated_Constant
7427 (Entity (Expr1), Entity (Expr2)));
7428 end if;
7429
7430 else
7431 Check_Mismatch (True);
7432 end if;
7433
7434 elsif Is_Entity_Name (Original_Node (Expr1))
7435 and then Is_Entity_Name (Expr2)
7436 and then Same_Instantiated_Constant
7437 (Entity (Original_Node (Expr1)), Entity (Expr2))
7438 then
7439 null;
7440
7441 elsif Nkind (Expr1) = N_Null then
7442 Check_Mismatch (Nkind (Expr1) /= N_Null);
7443
7444 else
7445 Check_Mismatch (True);
7446 end if;
7447
7448 elsif Ekind (E1) = E_Variable then
7449 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
7450
7451 elsif Ekind (E1) = E_Package then
7452 Check_Mismatch
7453 (Ekind (E1) /= Ekind (E2)
7454 or else (Present (Renamed_Entity (E2))
7455 and then Renamed_Entity (E1) /=
7456 Renamed_Entity (E2)));
7457
7458 elsif Is_Overloadable (E1) then
7459 -- Verify that the actual subprograms match. Note that actuals
7460 -- that are attributes are rewritten as subprograms. If the
7461 -- subprogram in the formal package is defaulted, no check is
7462 -- needed. Note that this can only happen in Ada 2005 when the
7463 -- formal package can be partially parameterized.
7464
7465 if Nkind (Unit_Declaration_Node (E1)) =
7466 N_Subprogram_Renaming_Declaration
7467 and then From_Default (Unit_Declaration_Node (E1))
7468 then
7469 null;
7470
7471 -- If the formal package has an "others" box association that
7472 -- covers this formal, there is no need for a check either.
7473
7474 elsif Nkind (Unit_Declaration_Node (E2)) in
7475 N_Formal_Subprogram_Declaration
7476 and then Box_Present (Unit_Declaration_Node (E2))
7477 then
7478 null;
7479
7480 -- No check needed if subprogram is a defaulted null procedure
7481
7482 elsif No (Alias (E2))
7483 and then Ekind (E2) = E_Procedure
7484 and then
7485 Null_Present (Specification (Unit_Declaration_Node (E2)))
7486 then
7487 null;
7488
7489 -- Otherwise the actual in the formal and the actual in the
7490 -- instantiation of the formal must match, up to renamings.
7491
7492 else
7493 Check_Mismatch
7494 (Ekind (E2) /= Ekind (E1)
7495 or else not Same_Instantiated_Function (E1, E2));
7496 end if;
7497
7498 else
7499 raise Program_Error;
7500 end if;
7501
7502 <<Next_E>>
7503 Prev_E1 := E1;
7504 Next_Entity (E1);
7505 Next_Entity (E2);
7506 end loop;
7507 end Check_Formal_Package_Instance;
7508
7509 -------------------------------
7510 -- Check_Fixed_Point_Warning --
7511 -------------------------------
7512
7513 procedure Check_Fixed_Point_Warning
7514 (Gen_Assocs : Associations.Gen_Assocs_Rec;
7515 Renamings : List_Id)
7516 is
7517 use Associations;
7518 begin
7519 for Type_Index in Gen_Assocs.Assocs'Range loop
7520 declare
7521 Assoc : Assoc_Rec renames Gen_Assocs.Assocs (Type_Index);
7522 begin
7523 if Nkind (Assoc.An_Formal) = N_Formal_Type_Declaration
7524 and then Is_Fixed_Point_Type (Defining_Entity (Assoc.An_Formal))
7525 and then Assoc.Actual.Kind = Name_Exp
7526 then
7527 declare
7528 Typ : constant Entity_Id := Entity (Assoc.Actual.Name_Exp);
7529 pragma Assert (Is_Fixed_Point_Type (Typ));
7530
7531 Prims : constant Elist_Id :=
7532 Collect_Primitive_Operations (Typ);
7533 Elem : Elmt_Id := First_Elmt (Prims);
7534 Formal : Node_Id;
7535 Op : Entity_Id;
7536 begin
7537 -- Locate primitive operations of the type that are
7538 -- arithmetic operations.
7539
7540 while Present (Elem) loop
7541 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
7542
7543 -- Check whether the generic unit has a formal
7544 -- subprogram of the same name. This does not check
7545 -- types but is good enough to justify a warning.
7546
7547 Op := Alias (Node (Elem));
7548
7549 for Op_Index in Type_Index + 1 ..
7550 Gen_Assocs.Assocs'Last
7551 loop
7552 Formal := Gen_Assocs.Assocs (Op_Index).Un_Formal;
7553
7554 if Nkind (Formal) =
7555 N_Formal_Concrete_Subprogram_Declaration
7556 and then Chars (Defining_Entity (Formal)) =
7557 Chars (Node (Elem))
7558 then
7559 goto OK;
7560
7561 elsif Nkind (Formal) = N_Formal_Package_Declaration
7562 then
7563 declare
7564 Assoc : Node_Id;
7565 Ent : Entity_Id;
7566
7567 begin
7568 -- Locate corresponding actual, and check
7569 -- whether it includes a fixed-point type.
7570
7571 Assoc := First (Renamings);
7572 while Present (Assoc) loop
7573 exit when
7574 Nkind (Assoc) =
7575 N_Package_Renaming_Declaration
7576 and then
7577 Chars (Defining_Unit_Name (Assoc)) =
7578 Chars (Defining_Identifier (Formal));
7579
7580 Next (Assoc);
7581 end loop;
7582
7583 if Present (Assoc) then
7584 -- If the formal package declares a
7585 -- fixed-point type, and the user-defined
7586 -- operator is derived from a generic
7587 -- instance package, the fixed-point type
7588 -- does not use the corresponding
7589 -- predefined op.
7590
7591 Ent :=
7592 First_Entity (Entity (Name (Assoc)));
7593 while Present (Ent) loop
7594 if Is_Fixed_Point_Type (Ent)
7595 and then Present (Op)
7596 and then
7597 Is_Generic_Instance (Scope (Op))
7598 then
7599 goto OK;
7600 end if;
7601
7602 Next_Entity (Ent);
7603 end loop;
7604 end if;
7605 end;
7606 end if;
7607 end loop;
7608
7609 Error_Msg_Sloc := Sloc (Node (Elem));
7610 Error_Msg_NE
7611 ("?instance uses predefined, not primitive, " &
7612 "operator&#",
7613 Assoc.Actual.Name_Exp, Node (Elem));
7614 <<OK>> null;
7615 end if;
7616
7617 Next_Elmt (Elem);
7618 end loop;
7619 end;
7620 end if;
7621 end;
7622 end loop;
7623 end Check_Fixed_Point_Warning;
7624
7625 ---------------------------
7626 -- Check_Formal_Packages --
7627 ---------------------------
7628
7629 procedure Check_Formal_Packages (P_Id : Entity_Id) is
7630 E : Entity_Id;
7631 Formal_P : Entity_Id;
7632 Formal_Decl : Node_Id;
7633
7634 begin
7635 -- Iterate through the declarations in the instance, looking for package
7636 -- renaming declarations that denote instances of formal packages, until
7637 -- we find the renaming of the current package itself. The declaration
7638 -- of a formal package that requires conformance checking is followed by
7639 -- an internal entity that is the abbreviated instance.
7640
7641 E := First_Entity (P_Id);
7642 while Present (E) loop
7643 if Ekind (E) = E_Package then
7644 exit when Renamed_Entity (E) = P_Id;
7645
7646 if Nkind (Parent (E)) = N_Package_Renaming_Declaration then
7647 Formal_Decl := Parent (Associated_Formal_Package (E));
7648
7649 if Requires_Conformance_Checking (Formal_Decl) then
7650 Formal_P := Next_Entity (E);
7651
7652 -- If the instance is within an enclosing instance body
7653 -- there is no need to verify the legality of current formal
7654 -- packages because they were legal in the generic body.
7655 -- This optimization may be applicable elsewhere, and it
7656 -- also removes spurious errors that may arise with
7657 -- on-the-fly inlining and confusion between private and
7658 -- full views.
7659
7660 if not In_Instance_Body then
7661 Check_Formal_Package_Instance (Formal_P, E);
7662 end if;
7663
7664 -- Restore the visibility of formals of the formal instance
7665 -- that are not defaulted, and are hidden within the current
7666 -- generic. These formals may be visible within an enclosing
7667 -- generic.
7668
7669 declare
7670 Elmt : Elmt_Id;
7671 begin
7672 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
7673 while Present (Elmt) loop
7674 Set_Is_Hidden (Node (Elmt), False);
7675 Next_Elmt (Elmt);
7676 end loop;
7677 end;
7678
7679 -- After checking, remove the internal validating package.
7680 -- It is only needed for semantic checks, and as it may
7681 -- contain generic formal declarations it should not reach
7682 -- gigi.
7683
7684 Remove (Unit_Declaration_Node (Formal_P));
7685 end if;
7686 end if;
7687 end if;
7688
7689 Next_Entity (E);
7690 end loop;
7691 end Check_Formal_Packages;
7692
7693 ---------------------------------
7694 -- Check_Forward_Instantiation --
7695 ---------------------------------
7696
7697 procedure Check_Forward_Instantiation (Decl : Node_Id) is
7698 S : Entity_Id;
7699 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
7700
7701 begin
7702 -- The instantiation appears before the generic body if we are in the
7703 -- scope of the unit containing the generic, either in its spec or in
7704 -- the package body, and before the generic body.
7705
7706 if Ekind (Gen_Comp) = E_Package_Body then
7707 Gen_Comp := Spec_Entity (Gen_Comp);
7708 end if;
7709
7710 if In_Open_Scopes (Gen_Comp)
7711 and then No (Corresponding_Body (Decl))
7712 then
7713 S := Current_Scope;
7714
7715 while Present (S)
7716 and then not Is_Compilation_Unit (S)
7717 and then not Is_Child_Unit (S)
7718 loop
7719 if Ekind (S) = E_Package then
7720 Set_Has_Forward_Instantiation (S);
7721 end if;
7722
7723 S := Scope (S);
7724 end loop;
7725 end if;
7726 end Check_Forward_Instantiation;
7727
7728 ---------------------------
7729 -- Check_Generic_Actuals --
7730 ---------------------------
7731
7732 -- The visibility of the actuals may be different between the point of
7733 -- generic instantiation and the instantiation of the body.
7734
7735 procedure Check_Generic_Actuals
7736 (Instance : Entity_Id;
7737 Is_Formal_Box : Boolean)
7738 is
7739 Gen_Id : constant Entity_Id :=
7740 (if Is_Generic_Unit (Instance) then
7741 Instance
7742 elsif Is_Wrapper_Package (Instance) then
7743 Generic_Parent
7744 (Specification
7745 (Unit_Declaration_Node (Related_Instance (Instance))))
7746 else
7747 Generic_Parent (Package_Specification (Instance)));
7748 -- The generic unit
7749
7750 Parent_Scope : constant Entity_Id := Scope (Gen_Id);
7751 -- The enclosing scope of the generic unit
7752
7753 procedure Check_Actual_Type (Typ : Entity_Id);
7754 -- If the type of the actual is a private type declared in the enclosing
7755 -- scope of the generic, either directly or through packages nested in
7756 -- bodies, but not a derived type of a private type declared elsewhere,
7757 -- then the body of the generic sees the full view of the type because
7758 -- it has to appear in the package body. If the type is private now then
7759 -- exchange views to restore the proper visibility in the instance.
7760
7761 -----------------------
7762 -- Check_Actual_Type --
7763 -----------------------
7764
7765 procedure Check_Actual_Type (Typ : Entity_Id) is
7766 Btyp : constant Entity_Id := Base_Type (Typ);
7767
7768 function Scope_Within_Body_Or_Same
7769 (Inner : Entity_Id;
7770 Outer : Entity_Id) return Boolean;
7771 -- Determine whether scope Inner is within the body of scope Outer
7772 -- or is Outer itself.
7773
7774 -------------------------------
7775 -- Scope_Within_Body_Or_Same --
7776 -------------------------------
7777
7778 function Scope_Within_Body_Or_Same
7779 (Inner : Entity_Id;
7780 Outer : Entity_Id) return Boolean
7781 is
7782 Curr : Entity_Id := Inner;
7783
7784 begin
7785 while Curr /= Standard_Standard loop
7786 if Curr = Outer then
7787 return True;
7788
7789 elsif Is_Package_Body_Entity (Curr) then
7790 Curr := Scope (Curr);
7791
7792 else
7793 exit;
7794 end if;
7795 end loop;
7796
7797 return False;
7798 end Scope_Within_Body_Or_Same;
7799
7800 -- Start of processing for Check_Actual_Type
7801
7802 begin
7803 -- The exchange is only needed if the generic is defined
7804 -- within a package which is not a common ancestor of the
7805 -- scope of the instance, and is not already in scope.
7806
7807 if Is_Private_Type (Btyp)
7808 and then not Has_Private_Ancestor (Btyp)
7809 and then Ekind (Parent_Scope) in E_Package | E_Generic_Package
7810 and then Scope_Within_Body_Or_Same (Parent_Scope, Scope (Btyp))
7811 and then Parent_Scope /= Scope (Instance)
7812 and then not Is_Child_Unit (Gen_Id)
7813 then
7814 Switch_View (Btyp);
7815
7816 -- If the type of the entity is a subtype, it may also have
7817 -- to be made visible, together with the base type of its
7818 -- full view, after exchange.
7819
7820 if Is_Private_Type (Typ) then
7821 Switch_View (Typ);
7822 Switch_View (Base_Type (Typ));
7823 end if;
7824 end if;
7825 end Check_Actual_Type;
7826
7827 -- Local variables
7828
7829 Astype : Entity_Id;
7830 E : Entity_Id;
7831 Formal : Node_Id;
7832
7833 -- Start of processing for Check_Generic_Actuals
7834
7835 begin
7836 E := First_Entity (Instance);
7837 while Present (E) loop
7838 if Is_Type (E)
7839 and then Nkind (Parent (E)) = N_Subtype_Declaration
7840 and then Scope (Etype (E)) /= Instance
7841 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7842 then
7843 declare
7844 Indic : constant Node_Id := Subtype_Indication (Parent (E));
7845
7846 begin
7847 -- Restore the proper view of the actual from the information
7848 -- saved earlier by Instantiate_Type.
7849
7850 Check_Private_View (Indic);
7851
7852 -- If this view is an array type, check its component type.
7853 -- This handles the case of an array type whose component
7854 -- type is private, used as the actual in an instantiation
7855 -- of a generic construct declared in the same package as
7856 -- the component type and taking an array type with this
7857 -- component type as formal type parameter.
7858
7859 if Is_Array_Type (Etype (Indic)) then
7860 Check_Actual_Type
7861 (Component_Type_For_Private_View (Etype (Indic)));
7862 end if;
7863 end;
7864
7865 -- If the actual is itself the formal of a parent instance,
7866 -- then also restore the proper view of its actual and so on.
7867 -- That's necessary for nested instantiations of the form
7868
7869 -- generic
7870 -- type Component is private;
7871 -- type Array_Type is array (Positive range <>) of Component;
7872 -- procedure Proc;
7873
7874 -- when the outermost actuals have inconsistent views, because
7875 -- the Component_Type of Array_Type of the inner instantiations
7876 -- is the actual of Component of the outermost one and not that
7877 -- of the corresponding inner instantiations.
7878
7879 Astype := Ancestor_Subtype (E);
7880 while Present (Astype)
7881 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7882 and then Present (Generic_Parent_Type (Parent (Astype)))
7883 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7884 loop
7885 Check_Private_View (Subtype_Indication (Parent (Astype)));
7886 Astype := Ancestor_Subtype (Astype);
7887 end loop;
7888
7889 Set_Is_Generic_Actual_Type (E);
7890
7891 if Is_Private_Type (E) and then Present (Full_View (E)) then
7892 Set_Is_Generic_Actual_Type (Full_View (E));
7893 end if;
7894
7895 Set_Is_Hidden (E, False);
7896 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7897
7898 -- We constructed the generic actual type as a subtype of the
7899 -- supplied type. This means that it normally would not inherit
7900 -- subtype specific attributes of the actual, which is wrong for
7901 -- the generic case.
7902
7903 Astype := Ancestor_Subtype (E);
7904
7905 if No (Astype) then
7906
7907 -- This can happen when E is an itype that is the full view of
7908 -- a private type completed, e.g. with a constrained array. In
7909 -- that case, use the first subtype, which will carry size
7910 -- information. The base type itself is unconstrained and will
7911 -- not carry it.
7912
7913 Astype := First_Subtype (E);
7914 end if;
7915
7916 Set_Size_Info (E, Astype);
7917 Copy_RM_Size (To => E, From => Astype);
7918 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7919
7920 if Is_Discrete_Or_Fixed_Point_Type (E) then
7921 Set_RM_Size (E, RM_Size (Astype));
7922 end if;
7923
7924 elsif Ekind (E) = E_Package then
7925
7926 -- If this is the renaming for the current instance, we're done.
7927 -- Otherwise it is a formal package. If the corresponding formal
7928 -- was declared with a box, the (instantiations of the) generic
7929 -- formal part are also visible. Otherwise, ignore the entity
7930 -- created to validate the actuals.
7931
7932 if Renamed_Entity (E) = Instance then
7933 exit;
7934
7935 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7936 null;
7937
7938 -- The visibility of a formal of an enclosing generic is already
7939 -- correct.
7940
7941 elsif Denotes_Formal_Package (E) then
7942 null;
7943
7944 elsif Present (Associated_Formal_Package (E))
7945 and then not Is_Generic_Formal (E)
7946 then
7947 Check_Generic_Actuals
7948 (Renamed_Entity (E),
7949 Is_Formal_Box =>
7950 Box_Present (Parent (Associated_Formal_Package (E))));
7951
7952 Set_Is_Hidden (E, False);
7953 end if;
7954
7955 -- If this is a subprogram instance (in a wrapper package) the
7956 -- actual is fully visible.
7957
7958 elsif Is_Wrapper_Package (Instance) then
7959 Set_Is_Hidden (E, False);
7960
7961 -- If the formal package is declared with a box, or if the formal
7962 -- parameter is defaulted, it is visible in the body.
7963
7964 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7965 Set_Is_Hidden (E, False);
7966 end if;
7967
7968 -- Check directly the type of the actual objects, including the
7969 -- component type for array types.
7970
7971 if Ekind (E) in E_Constant | E_Variable then
7972 Check_Actual_Type (Etype (E));
7973
7974 if Is_Array_Type (Etype (E)) then
7975 Check_Actual_Type (Component_Type (Etype (E)));
7976 end if;
7977
7978 -- As well as the type of formal parameters of actual subprograms
7979
7980 elsif Ekind (E) in E_Function | E_Procedure
7981 and then Is_Generic_Actual_Subprogram (E)
7982 and then Present (Alias (E))
7983 then
7984 Formal := First_Formal (Alias (E));
7985 while Present (Formal) loop
7986 Check_Actual_Type (Etype (Formal));
7987 Next_Formal (Formal);
7988 end loop;
7989 end if;
7990
7991 Next_Entity (E);
7992 end loop;
7993 end Check_Generic_Actuals;
7994
7995 ------------------------------
7996 -- Check_Generic_Child_Unit --
7997 ------------------------------
7998
7999 procedure Check_Generic_Child_Unit
8000 (Gen_Id : Node_Id;
8001 Parent_Installed : in out Boolean)
8002 is
8003 Loc : constant Source_Ptr := Sloc (Gen_Id);
8004 Gen_Par : Entity_Id := Empty;
8005 E : Entity_Id;
8006 Inst_Par : Entity_Id := Empty;
8007 S : Node_Id;
8008
8009 function Find_Generic_Child
8010 (Scop : Entity_Id;
8011 Id : Node_Id) return Entity_Id;
8012 -- Search generic parent for possible child unit with the given name
8013
8014 function In_Enclosing_Instance return Boolean;
8015 -- Within an instance of the parent, the child unit may be denoted by
8016 -- a simple name, or an abbreviated expanded name. Examine enclosing
8017 -- scopes to locate a possible parent instantiation.
8018
8019 ------------------------
8020 -- Find_Generic_Child --
8021 ------------------------
8022
8023 function Find_Generic_Child
8024 (Scop : Entity_Id;
8025 Id : Node_Id) return Entity_Id
8026 is
8027 E : Entity_Id;
8028
8029 begin
8030 -- If entity of name is already set, instance has already been
8031 -- resolved, e.g. in an enclosing instantiation.
8032
8033 if Present (Entity (Id)) then
8034 if Scope (Entity (Id)) = Scop then
8035 return Entity (Id);
8036 else
8037 return Empty;
8038 end if;
8039
8040 else
8041 E := First_Entity (Scop);
8042 while Present (E) loop
8043 if Chars (E) = Chars (Id)
8044 and then Is_Child_Unit (E)
8045 then
8046 if Is_Child_Unit (E)
8047 and then not Is_Visible_Lib_Unit (E)
8048 then
8049 Error_Msg_NE
8050 ("generic child unit& is not visible", Gen_Id, E);
8051 end if;
8052
8053 Set_Entity (Id, E);
8054 return E;
8055 end if;
8056
8057 Next_Entity (E);
8058 end loop;
8059
8060 return Empty;
8061 end if;
8062 end Find_Generic_Child;
8063
8064 ---------------------------
8065 -- In_Enclosing_Instance --
8066 ---------------------------
8067
8068 function In_Enclosing_Instance return Boolean is
8069 Enclosing_Instance : Node_Id;
8070 Instance_Decl : Node_Id;
8071
8072 begin
8073 -- We do not inline any call that contains instantiations, except
8074 -- for instantiations of Unchecked_Conversion, so if we are within
8075 -- an inlined body the current instance does not require parents.
8076
8077 if In_Inlined_Body then
8078 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
8079 return False;
8080 end if;
8081
8082 -- Loop to check enclosing scopes
8083
8084 Enclosing_Instance := Current_Scope;
8085 while Present (Enclosing_Instance) loop
8086 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
8087
8088 if Ekind (Enclosing_Instance) = E_Package
8089 and then Is_Generic_Instance (Enclosing_Instance)
8090 and then Present
8091 (Generic_Parent (Specification (Instance_Decl)))
8092 then
8093 -- Check whether the generic we are looking for is a child of
8094 -- this instance.
8095
8096 E := Find_Generic_Child
8097 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
8098 exit when Present (E);
8099
8100 else
8101 E := Empty;
8102 end if;
8103
8104 Enclosing_Instance := Scope (Enclosing_Instance);
8105 end loop;
8106
8107 if No (E) then
8108
8109 -- Not a child unit
8110
8111 Analyze (Gen_Id);
8112 return False;
8113
8114 else
8115 Rewrite (Gen_Id,
8116 Make_Expanded_Name (Loc,
8117 Chars => Chars (E),
8118 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
8119 Selector_Name => New_Occurrence_Of (E, Loc)));
8120
8121 Set_Entity (Gen_Id, E);
8122 Set_Etype (Gen_Id, Etype (E));
8123 Parent_Installed := False; -- Already in scope.
8124 return True;
8125 end if;
8126 end In_Enclosing_Instance;
8127
8128 -- Start of processing for Check_Generic_Child_Unit
8129
8130 begin
8131 -- If the name of the generic is given by a selected component, it may
8132 -- be the name of a generic child unit, and the prefix is the name of an
8133 -- instance of the parent, in which case the child unit must be visible.
8134 -- If this instance is not in scope, it must be placed there and removed
8135 -- after instantiation, because what is being instantiated is not the
8136 -- original child, but the corresponding child present in the instance
8137 -- of the parent.
8138
8139 -- If the child is instantiated within the parent, it can be given by
8140 -- a simple name. In this case the instance is already in scope, but
8141 -- the child generic must be recovered from the generic parent as well.
8142
8143 if Nkind (Gen_Id) = N_Selected_Component then
8144 S := Selector_Name (Gen_Id);
8145 Analyze (Prefix (Gen_Id));
8146 Inst_Par := Entity (Prefix (Gen_Id));
8147
8148 if Ekind (Inst_Par) = E_Package
8149 and then Present (Renamed_Entity (Inst_Par))
8150 then
8151 Inst_Par := Renamed_Entity (Inst_Par);
8152 end if;
8153
8154 if Ekind (Inst_Par) = E_Package then
8155 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
8156 Gen_Par := Generic_Parent (Parent (Inst_Par));
8157
8158 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
8159 and then
8160 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
8161 then
8162 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
8163 end if;
8164
8165 elsif Ekind (Inst_Par) = E_Generic_Package
8166 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
8167 then
8168 -- A formal package may be a real child package, and not the
8169 -- implicit instance within a parent. In this case the child is
8170 -- not visible and has to be retrieved explicitly as well.
8171
8172 Gen_Par := Inst_Par;
8173 end if;
8174
8175 if Present (Gen_Par) then
8176
8177 -- The prefix denotes an instantiation. The entity itself may be a
8178 -- nested generic, or a child unit.
8179
8180 E := Find_Generic_Child (Gen_Par, S);
8181
8182 if Present (E) then
8183 Change_Selected_Component_To_Expanded_Name (Gen_Id);
8184 Set_Entity (Gen_Id, E);
8185 Set_Etype (Gen_Id, Etype (E));
8186 Set_Entity (S, E);
8187 Set_Etype (S, Etype (E));
8188
8189 -- Indicate that this is a reference to the parent
8190
8191 if In_Extended_Main_Source_Unit (Gen_Id) then
8192 Set_Is_Instantiated (Inst_Par);
8193 end if;
8194
8195 -- A common mistake is to replicate the naming scheme of a
8196 -- hierarchy by instantiating a generic child directly, rather
8197 -- than the implicit child in a parent instance:
8198
8199 -- generic .. package Gpar is ..
8200 -- generic .. package Gpar.Child is ..
8201 -- package Par is new Gpar ();
8202
8203 -- with Gpar.Child;
8204 -- package Par.Child is new Gpar.Child ();
8205 -- rather than Par.Child
8206
8207 -- In this case the instantiation is within Par, which is an
8208 -- instance, but Gpar does not denote Par because we are not IN
8209 -- the instance of Gpar, so this is illegal. The test below
8210 -- recognizes this particular case.
8211
8212 declare
8213 -- We want to reject the final instantiation in
8214 -- generic package G1 is end G1;
8215 -- generic package G1.G2 is end G1.G2;
8216 -- with G1; package I1 is new G1;
8217 -- with G1.G2; package I1.I2 is new G1.G2;
8218 -- because the use of G1.G2 should instead be either
8219 -- I1.G2 or simply G2. However, the tree that is built
8220 -- in this case is wrong. In the expanded copy
8221 -- of G2, we need (and therefore generate) a renaming
8222 -- package G1 renames I1;
8223 -- but this renaming should not participate in resolving
8224 -- this occurrence of the name "G1.G2"; unfortunately,
8225 -- it does. Rather than correct this error, we compensate
8226 -- for it in this function.
8227 --
8228 -- We also perform another adjustment here. If we are
8229 -- currently inside a generic package, then that
8230 -- generic package needs to be treated as a package.
8231 -- For example, if a generic Aaa declares a nested generic
8232 -- Bbb (perhaps as a child unit) then Aaa can also legally
8233 -- declare an instance of Aaa.Bbb.
8234
8235 function Adjusted_Inst_Par_Ekind return Entity_Kind;
8236
8237 -----------------------------
8238 -- Adjusted_Inst_Par_Ekind --
8239 -----------------------------
8240
8241 function Adjusted_Inst_Par_Ekind return Entity_Kind is
8242 Prefix_Entity : Entity_Id;
8243 Inst_Par_GP : Node_Id;
8244 Inst_Par_Parent : Node_Id := Parent (Inst_Par);
8245 begin
8246 if Nkind (Inst_Par_Parent) = N_Defining_Program_Unit_Name
8247 then
8248 Inst_Par_Parent := Parent (Inst_Par_Parent);
8249 end if;
8250
8251 Inst_Par_GP := Generic_Parent (Inst_Par_Parent);
8252
8253 if Nkind (Gen_Id) = N_Expanded_Name
8254 and then Present (Inst_Par_GP)
8255 and then Ekind (Inst_Par_GP) = E_Generic_Package
8256 then
8257 Prefix_Entity := Entity (Prefix (Gen_Id));
8258
8259 if Present (Prefix_Entity)
8260 and then not Comes_From_Source (Prefix_Entity)
8261 and then Nkind (Parent (Prefix_Entity)) =
8262 N_Package_Renaming_Declaration
8263 and then Chars (Prefix_Entity) = Chars (Inst_Par_GP)
8264 then
8265 return E_Generic_Package;
8266 end if;
8267 end if;
8268
8269 if Ekind (Inst_Par) = E_Generic_Package
8270 and then In_Open_Scopes (Inst_Par)
8271 then
8272 -- If we are inside a generic package then
8273 -- treat it as a package.
8274 return E_Package;
8275 end if;
8276
8277 -- The usual path
8278 return Ekind (Inst_Par);
8279 end Adjusted_Inst_Par_Ekind;
8280
8281 begin
8282 if Is_Child_Unit (E)
8283 and then (No (Inst_Par)
8284 or else Adjusted_Inst_Par_Ekind =
8285 E_Generic_Package)
8286 and then (not In_Instance
8287 or else Nkind (Parent (Parent (Gen_Id))) =
8288 N_Compilation_Unit)
8289 then
8290 Error_Msg_N
8291 ("prefix of generic child unit must be " &
8292 "instance of parent",
8293 Gen_Id);
8294 end if;
8295 end;
8296
8297 if not In_Open_Scopes (Inst_Par)
8298 and then Nkind (Parent (Gen_Id)) not in
8299 N_Generic_Renaming_Declaration
8300 then
8301 Install_Parent (Inst_Par);
8302 Parent_Installed := True;
8303
8304 elsif In_Open_Scopes (Inst_Par) then
8305
8306 -- If the parent is already installed, install the actuals
8307 -- for its formal packages. This is necessary when the child
8308 -- instance is a child of the parent instance: in this case,
8309 -- the parent is placed on the scope stack but the formal
8310 -- packages are not made visible.
8311
8312 Install_Formal_Packages (Inst_Par);
8313 end if;
8314
8315 else
8316 -- If the generic parent does not contain an entity that
8317 -- corresponds to the selector, the instance doesn't either.
8318 -- Analyzing the node will yield the appropriate error message.
8319 -- If the entity is not a child unit, then it is an inner
8320 -- generic in the parent.
8321
8322 Analyze (Gen_Id);
8323 end if;
8324
8325 else
8326 Analyze (Gen_Id);
8327
8328 if Is_Child_Unit (Entity (Gen_Id))
8329 and then
8330 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
8331 and then not In_Open_Scopes (Inst_Par)
8332 then
8333 Install_Parent (Inst_Par);
8334 Parent_Installed := True;
8335
8336 -- The generic unit may be the renaming of the implicit child
8337 -- present in an instance. In that case the parent instance is
8338 -- obtained from the name of the renamed entity.
8339
8340 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
8341 and then Present (Renamed_Entity (Entity (Gen_Id)))
8342 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
8343 then
8344 declare
8345 Renamed_Package : constant Node_Id :=
8346 Name (Parent (Entity (Gen_Id)));
8347 begin
8348 if Nkind (Renamed_Package) = N_Expanded_Name then
8349 Inst_Par := Entity (Prefix (Renamed_Package));
8350 Install_Parent (Inst_Par);
8351 Parent_Installed := True;
8352 end if;
8353 end;
8354 end if;
8355 end if;
8356
8357 elsif Nkind (Gen_Id) = N_Expanded_Name then
8358
8359 -- Entity already present, analyze prefix, whose meaning may be an
8360 -- instance in the current context. If it is an instance of a
8361 -- relative within another, the proper parent may still have to be
8362 -- installed, if they are not of the same generation.
8363
8364 Analyze (Prefix (Gen_Id));
8365
8366 -- Prevent cascaded errors
8367
8368 if Etype (Prefix (Gen_Id)) = Any_Type then
8369 return;
8370 end if;
8371
8372 -- In the unlikely case that a local declaration hides the name of
8373 -- the parent package, locate it on the homonym chain. If the context
8374 -- is an instance of the parent, the renaming entity is flagged as
8375 -- such.
8376
8377 Inst_Par := Entity (Prefix (Gen_Id));
8378 while Present (Inst_Par)
8379 and then not Is_Package_Or_Generic_Package (Inst_Par)
8380 loop
8381 Inst_Par := Homonym (Inst_Par);
8382 end loop;
8383
8384 pragma Assert (Present (Inst_Par));
8385 Set_Entity (Prefix (Gen_Id), Inst_Par);
8386
8387 if In_Enclosing_Instance then
8388 null;
8389
8390 elsif Present (Entity (Gen_Id))
8391 and then No (Renamed_Entity (Entity (Gen_Id)))
8392 and then Is_Child_Unit (Entity (Gen_Id))
8393 and then not In_Open_Scopes (Inst_Par)
8394 then
8395 Install_Parent (Inst_Par);
8396 Parent_Installed := True;
8397
8398 -- Handle renaming of generic child unit
8399
8400 elsif Present (Entity (Gen_Id))
8401 and then Present (Renamed_Entity (Entity (Gen_Id)))
8402 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
8403 then
8404 declare
8405 E : Entity_Id;
8406 Ren_Decl : Node_Id;
8407
8408 begin
8409 -- The entity of the renamed generic child unit does not
8410 -- have any reference to the instantiated parent. In order to
8411 -- locate it we traverse the scope containing the renaming
8412 -- declaration; the instance of the parent is available in
8413 -- the prefix of the renaming declaration. For example:
8414
8415 -- package A is
8416 -- package Inst_Par is new ...
8417 -- generic package Ren_Child renames Ins_Par.Child;
8418 -- end;
8419
8420 -- with A;
8421 -- package B is
8422 -- package Inst_Child is new A.Ren_Child;
8423 -- end;
8424
8425 E := First_Entity (Entity (Prefix (Gen_Id)));
8426 while Present (E) loop
8427 if not Is_Object (E)
8428 and then Present (Renamed_Entity (E))
8429 and then
8430 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
8431 then
8432 Ren_Decl := Parent (E);
8433 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
8434
8435 if not In_Open_Scopes (Inst_Par) then
8436 Install_Parent (Inst_Par);
8437 Parent_Installed := True;
8438 end if;
8439
8440 exit;
8441 end if;
8442
8443 E := Next_Entity (E);
8444 end loop;
8445 end;
8446 end if;
8447
8448 elsif In_Enclosing_Instance then
8449
8450 -- The child unit is found in some enclosing scope
8451
8452 null;
8453
8454 else
8455 Analyze (Gen_Id);
8456
8457 -- If this is the renaming of the implicit child in a parent
8458 -- instance, recover the parent name and install it.
8459
8460 if Is_Entity_Name (Gen_Id) then
8461 E := Entity (Gen_Id);
8462
8463 if Is_Generic_Unit (E)
8464 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
8465 and then Is_Child_Unit (Renamed_Entity (E))
8466 and then Is_Generic_Unit (Scope (Renamed_Entity (E)))
8467 and then Nkind (Name (Parent (E))) = N_Expanded_Name
8468 then
8469 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
8470 Inst_Par := Entity (Prefix (Gen_Id));
8471
8472 if not In_Open_Scopes (Inst_Par) then
8473 Install_Parent (Inst_Par);
8474 Parent_Installed := True;
8475 end if;
8476
8477 -- If it is a child unit of a non-generic parent, it may be
8478 -- use-visible and given by a direct name. Install parent as
8479 -- for other cases.
8480
8481 elsif Is_Generic_Unit (E)
8482 and then Is_Child_Unit (E)
8483 and then
8484 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
8485 and then not Is_Generic_Unit (Scope (E))
8486 then
8487 if not In_Open_Scopes (Scope (E)) then
8488 Install_Parent (Scope (E));
8489 Parent_Installed := True;
8490 end if;
8491 end if;
8492 end if;
8493 end if;
8494 end Check_Generic_Child_Unit;
8495
8496 -----------------------------
8497 -- Check_Hidden_Child_Unit --
8498 -----------------------------
8499
8500 procedure Check_Hidden_Child_Unit
8501 (N : Node_Id;
8502 Gen_Unit : Entity_Id;
8503 Act_Decl_Id : Entity_Id)
8504 is
8505 Gen_Id : constant Node_Id := Name (N);
8506
8507 begin
8508 if Is_Child_Unit (Gen_Unit)
8509 and then Is_Child_Unit (Act_Decl_Id)
8510 and then Nkind (Gen_Id) = N_Expanded_Name
8511 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
8512 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
8513 then
8514 Error_Msg_Node_2 := Scope (Act_Decl_Id);
8515 Error_Msg_NE
8516 ("generic unit & is implicitly declared in &",
8517 Defining_Unit_Name (N), Gen_Unit);
8518 Error_Msg_N ("\instance must have different name",
8519 Defining_Unit_Name (N));
8520 end if;
8521 end Check_Hidden_Child_Unit;
8522
8523 ------------------------
8524 -- Check_Private_View --
8525 ------------------------
8526
8527 procedure Check_Private_View (N : Node_Id) is
8528 Comparison : constant Boolean := Nkind (N) in N_Op_Compare;
8529 Typ : constant Entity_Id :=
8530 (if Comparison then Compare_Type (N) else Etype (N));
8531
8532 procedure Check_Private_Type (T : Entity_Id; Private_View : Boolean);
8533 -- Check that the available view of T matches Private_View and, if not,
8534 -- switch the view of T or of its base type.
8535
8536 procedure Check_Private_Type (T : Entity_Id; Private_View : Boolean) is
8537 BT : constant Entity_Id := Base_Type (T);
8538
8539 begin
8540 -- If the full declaration was not visible in the generic, stop here
8541
8542 if Private_View then
8543 return;
8544 end if;
8545
8546 -- Exchange views if the type was not private in the generic but is
8547 -- private at the point of instantiation. Do not exchange views if
8548 -- the scope of the type is in scope. This can happen if both generic
8549 -- and instance are sibling units, or if type is defined in a parent.
8550 -- In this case the visibility of the type will be correct for all
8551 -- semantic checks.
8552
8553 if Is_Private_Type (T)
8554 and then Present (Full_View (T))
8555 and then not In_Open_Scopes (Scope (T))
8556 then
8557 Switch_View (T);
8558
8559 -- Finally, a nonprivate subtype may have a private base type, which
8560 -- must be exchanged for consistency. This can happen when a package
8561 -- body is instantiated, when the scope stack is empty but in fact
8562 -- the subtype and the base type are declared in an enclosing scope.
8563
8564 -- Note that in this case we introduce an inconsistency in the view
8565 -- set, because we switch the base type BT, but there could be some
8566 -- private dependent subtypes of BT which remain unswitched. Such
8567 -- subtypes might need to be switched at a later point (see specific
8568 -- provision for that case in Switch_View).
8569
8570 elsif not Is_Private_Type (T)
8571 and then Is_Private_Type (BT)
8572 and then Present (Full_View (BT))
8573 and then not In_Open_Scopes (BT)
8574 then
8575 Prepend_Elmt (Full_View (BT), Exchanged_Views);
8576 Exchange_Declarations (BT);
8577 end if;
8578 end Check_Private_Type;
8579
8580 -- Start of processing for Check_Private_View
8581
8582 begin
8583 if Present (Typ) then
8584 -- If the type appears in a subtype declaration, the subtype in
8585 -- instance must have a view compatible with that of its parent,
8586 -- which must be exchanged (see corresponding code in Restore_
8587 -- Private_Views) so we make an exception to the open scope rule
8588 -- implemented by Check_Private_Type above.
8589
8590 if Has_Private_View (N)
8591 and then not Is_Private_Type (Typ)
8592 and then not Has_Been_Exchanged (Typ)
8593 and then (not In_Open_Scopes (Scope (Typ))
8594 or else Nkind (Parent (N)) = N_Subtype_Declaration)
8595 then
8596 declare
8597 Assoc : constant Node_Id := Get_Associated_Node (N);
8598
8599 begin
8600 -- In the generic, only the private declaration was visible
8601
8602 Prepend_Elmt (Typ, Exchanged_Views);
8603 Exchange_Declarations
8604 (if Comparison then Compare_Type (Assoc) else Etype (Assoc));
8605 end;
8606
8607 -- Check that the available views of Typ match their respective flag.
8608 -- Note that the type of a visible discriminant is never private.
8609
8610 else
8611 Check_Private_Type (Typ, Has_Private_View (N));
8612
8613 if Is_Access_Type (Typ) then
8614 Check_Private_Type
8615 (Designated_Type (Typ), Has_Secondary_Private_View (N));
8616
8617 elsif Is_Array_Type (Typ) then
8618 Check_Private_Type
8619 (Component_Type_For_Private_View (Typ),
8620 Has_Secondary_Private_View (N));
8621
8622 elsif (Is_Record_Type (Typ) or else Is_Concurrent_Type (Typ))
8623 and then Has_Discriminants (Typ)
8624 then
8625 declare
8626 Disc : Entity_Id;
8627
8628 begin
8629 Disc := First_Discriminant (Typ);
8630 while Present (Disc) loop
8631 Check_Private_Type (Etype (Disc), False);
8632 Next_Discriminant (Disc);
8633 end loop;
8634 end;
8635 end if;
8636 end if;
8637 end if;
8638 end Check_Private_View;
8639
8640 -----------------------------
8641 -- Check_Hidden_Primitives --
8642 -----------------------------
8643
8644 function Check_Hidden_Primitives (Renamings : List_Id) return Elist_Id is
8645 Actual : Node_Id;
8646 Gen_T : Entity_Id;
8647 Result : Elist_Id := No_Elist;
8648
8649 begin
8650 if No (Renamings) then
8651 return No_Elist;
8652 end if;
8653
8654 -- Traverse the list of associations between formals and actuals
8655 -- searching for renamings of tagged types
8656
8657 Actual := First (Renamings);
8658 while Present (Actual) loop
8659 if Nkind (Actual) = N_Subtype_Declaration then
8660 Gen_T := Generic_Parent_Type (Actual);
8661
8662 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
8663
8664 -- Traverse the list of primitives of the actual types
8665 -- searching for hidden primitives that are visible in the
8666 -- corresponding generic formal; leave them visible and
8667 -- append them to Result to restore their decoration later.
8668
8669 Install_Hidden_Primitives
8670 (Prims_List => Result,
8671 Gen_T => Gen_T,
8672 Act_T => Entity (Subtype_Indication (Actual)));
8673 end if;
8674 end if;
8675
8676 Next (Actual);
8677 end loop;
8678
8679 return Result;
8680 end Check_Hidden_Primitives;
8681
8682 -------------------------------------
8683 -- Component_Type_For_Private_View --
8684 -------------------------------------
8685
8686 function Component_Type_For_Private_View (T : Entity_Id) return Entity_Id is
8687 Typ : constant Entity_Id := Component_Type (T);
8688
8689 begin
8690 if Is_Array_Type (Typ) and then not Has_Private_Declaration (Typ) then
8691 return Component_Type_For_Private_View (Typ);
8692 else
8693 return Typ;
8694 end if;
8695 end Component_Type_For_Private_View;
8696
8697 --------------------------
8698 -- Contains_Instance_Of --
8699 --------------------------
8700
8701 function Contains_Instance_Of
8702 (Inner : Entity_Id;
8703 Outer : Entity_Id;
8704 N : Node_Id) return Boolean
8705 is
8706 Elmt : Elmt_Id;
8707 Scop : Entity_Id;
8708
8709 begin
8710 Scop := Outer;
8711
8712 -- Verify that there are no circular instantiations. We check whether
8713 -- the unit contains an instance of the current scope or some enclosing
8714 -- scope (in case one of the instances appears in a subunit). Longer
8715 -- circularities involving subunits might seem too pathological to
8716 -- consider, but they were not too pathological for the authors of
8717 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
8718 -- enclosing generic scopes as containing an instance.
8719
8720 loop
8721 -- Within a generic subprogram body, the scope is not generic, to
8722 -- allow for recursive subprograms. Use the declaration to determine
8723 -- whether this is a generic unit.
8724
8725 if Ekind (Scop) = E_Generic_Package
8726 or else (Is_Subprogram (Scop)
8727 and then Nkind (Unit_Declaration_Node (Scop)) =
8728 N_Generic_Subprogram_Declaration)
8729 then
8730 Elmt := First_Elmt (Inner_Instances (Inner));
8731
8732 while Present (Elmt) loop
8733 if Node (Elmt) = Scop then
8734 Error_Msg_Node_2 := Inner;
8735 Error_Msg_NE
8736 ("circular instantiation: & instantiated within &!",
8737 N, Scop);
8738 return True;
8739
8740 elsif Node (Elmt) = Inner then
8741 return True;
8742
8743 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
8744 Error_Msg_Node_2 := Inner;
8745 Error_Msg_NE
8746 ("circular instantiation: & instantiated within &!",
8747 N, Node (Elmt));
8748 return True;
8749 end if;
8750
8751 Next_Elmt (Elmt);
8752 end loop;
8753
8754 -- Indicate that Inner is being instantiated within Scop
8755
8756 Append_Elmt (Inner, Inner_Instances (Scop));
8757 end if;
8758
8759 if Scop = Standard_Standard then
8760 exit;
8761 else
8762 Scop := Scope (Scop);
8763 end if;
8764 end loop;
8765
8766 return False;
8767 end Contains_Instance_Of;
8768
8769 -----------------------
8770 -- Copy_Generic_Node --
8771 -----------------------
8772
8773 function Copy_Generic_Node
8774 (N : Node_Id;
8775 Parent_Id : Node_Id;
8776 Instantiating : Boolean) return Node_Id
8777 is
8778 Ent : Entity_Id;
8779 New_N : Node_Id;
8780
8781 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
8782 -- Check the given value of one of the Fields referenced by the current
8783 -- node to determine whether to copy it recursively. The field may hold
8784 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
8785 -- Char) in which case it need not be copied.
8786
8787 procedure Copy_Descendants;
8788 -- Common utility for various nodes
8789
8790 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
8791 -- Make copy of element list
8792
8793 function Copy_Generic_List
8794 (L : List_Id;
8795 Parent_Id : Node_Id) return List_Id;
8796 -- Apply Copy_Generic_Node recursively to the members of a node list
8797
8798 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
8799 -- True if an identifier is part of the defining program unit name of
8800 -- a child unit.
8801 -- Consider removing this subprogram now that ASIS no longer uses it.
8802
8803 ----------------------
8804 -- Copy_Descendants --
8805 ----------------------
8806
8807 procedure Copy_Descendants is
8808 procedure Walk is new
8809 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
8810 begin
8811 Walk (New_N, N);
8812 end Copy_Descendants;
8813
8814 -----------------------------
8815 -- Copy_Generic_Descendant --
8816 -----------------------------
8817
8818 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
8819 begin
8820 if D = Union_Id (Empty) then
8821 return D;
8822
8823 elsif D in Node_Range then
8824 return Union_Id
8825 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
8826
8827 elsif D in List_Range then
8828 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
8829
8830 elsif D in Elist_Range then
8831 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
8832
8833 -- Nothing else is copyable (e.g. Uint values), return as is
8834
8835 else
8836 return D;
8837 end if;
8838 end Copy_Generic_Descendant;
8839
8840 ------------------------
8841 -- Copy_Generic_Elist --
8842 ------------------------
8843
8844 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
8845 M : Elmt_Id;
8846 L : Elist_Id;
8847
8848 begin
8849 if Present (E) then
8850 L := New_Elmt_List;
8851 M := First_Elmt (E);
8852 while Present (M) loop
8853 Append_Elmt
8854 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
8855 Next_Elmt (M);
8856 end loop;
8857
8858 return L;
8859
8860 else
8861 return No_Elist;
8862 end if;
8863 end Copy_Generic_Elist;
8864
8865 -----------------------
8866 -- Copy_Generic_List --
8867 -----------------------
8868
8869 function Copy_Generic_List
8870 (L : List_Id;
8871 Parent_Id : Node_Id) return List_Id
8872 is
8873 N : Node_Id;
8874 New_L : List_Id;
8875
8876 begin
8877 if Present (L) then
8878 New_L := New_List;
8879 Set_Parent (New_L, Parent_Id);
8880
8881 N := First (L);
8882 while Present (N) loop
8883 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
8884 Next (N);
8885 end loop;
8886
8887 return New_L;
8888
8889 else
8890 return No_List;
8891 end if;
8892 end Copy_Generic_List;
8893
8894 ---------------------------
8895 -- In_Defining_Unit_Name --
8896 ---------------------------
8897
8898 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
8899 begin
8900 return
8901 Present (Parent (Nam))
8902 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
8903 or else
8904 (Nkind (Parent (Nam)) = N_Expanded_Name
8905 and then In_Defining_Unit_Name (Parent (Nam))));
8906 end In_Defining_Unit_Name;
8907
8908 -- Start of processing for Copy_Generic_Node
8909
8910 begin
8911 if N = Empty then
8912 return N;
8913 end if;
8914
8915 New_N := New_Copy (N);
8916
8917 -- If we are instantiating, we want to adjust the sloc based on the
8918 -- current S_Adjustment. However, if this is the root node of a subunit,
8919 -- we need to defer that adjustment to below (see "elsif Instantiating
8920 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8921 -- computed the adjustment.
8922
8923 if Instantiating
8924 and then not (Nkind (N) in N_Proper_Body
8925 and then Was_Originally_Stub (N))
8926 then
8927 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8928 end if;
8929
8930 if not Is_List_Member (N) then
8931 Set_Parent (New_N, Parent_Id);
8932 end if;
8933
8934 -- Special casing for identifiers and other entity names and operators
8935
8936 if Nkind (N) in N_Character_Literal
8937 | N_Expanded_Name
8938 | N_Identifier
8939 | N_Operator_Symbol
8940 | N_Op
8941 then
8942 if not Instantiating then
8943
8944 -- Link both nodes in order to assign subsequently the entity of
8945 -- the copy to the original node, in case this is a global
8946 -- reference.
8947
8948 Set_Associated_Node (N, New_N);
8949
8950 -- If we are within an instantiation, this is a nested generic
8951 -- that has already been analyzed at the point of definition.
8952 -- We must preserve references that were global to the enclosing
8953 -- parent at that point. Other occurrences, whether global or
8954 -- local to the current generic, must be resolved anew, so we
8955 -- reset the entity in the generic copy. A global reference has a
8956 -- smaller depth than the parent, or else the same depth in case
8957 -- both are distinct compilation units.
8958
8959 -- A child unit is implicitly declared within the enclosing parent
8960 -- but is in fact global to it, and must be preserved.
8961
8962 -- It is also possible for Current_Instantiated_Parent to be
8963 -- defined, and for this not to be a nested generic, namely if
8964 -- the unit is loaded through Rtsfind. In that case, the entity of
8965 -- New_N is only a link to the associated node, and not a defining
8966 -- occurrence.
8967
8968 -- The entities for parent units in the defining_program_unit of a
8969 -- generic child unit are established when the context of the unit
8970 -- is first analyzed, before the generic copy is made. They are
8971 -- preserved in the copy for use in e.g. ASIS queries.
8972
8973 Ent := Entity (New_N);
8974
8975 if No (Current_Instantiated_Parent.Gen_Id) then
8976 if No (Ent)
8977 or else Nkind (Ent) /= N_Defining_Identifier
8978 or else not In_Defining_Unit_Name (N)
8979 then
8980 Set_Associated_Node (New_N, Empty);
8981 end if;
8982
8983 elsif No (Ent)
8984 or else Nkind (Ent) not in N_Entity
8985 or else No (Scope (Ent))
8986 or else
8987 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8988 and then not Is_Child_Unit (Ent))
8989 or else
8990 (Scope_Depth_Set (Scope (Ent))
8991 and then
8992 Scope_Depth (Scope (Ent)) >
8993 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8994 and then
8995 Get_Source_Unit (Ent) =
8996 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8997 then
8998 Set_Associated_Node (New_N, Empty);
8999 end if;
9000
9001 -- Case of instantiating identifier or some other name or operator
9002
9003 else
9004 -- If the associated node is still defined, the entity in it
9005 -- is global, and must be copied to the instance. If this copy
9006 -- is being made for a body to inline, it is applied to an
9007 -- instantiated tree, and the entity is already present and
9008 -- must be also preserved.
9009
9010 declare
9011 Assoc : constant Node_Id := Get_Associated_Node (N);
9012
9013 begin
9014 if Present (Assoc) then
9015 if Nkind (Assoc) = Nkind (N) then
9016 Set_Entity (New_N, Entity (Assoc));
9017 Check_Private_View (N);
9018
9019 -- The node is a reference to a global type and acts as the
9020 -- subtype mark of a qualified expression created in order
9021 -- to aid resolution of accidental overloading in instances.
9022 -- Since N is a reference to a type, the Associated_Node of
9023 -- N denotes an entity rather than another identifier. See
9024 -- Qualify_Universal_Operands for details.
9025
9026 elsif Nkind (N) = N_Identifier
9027 and then Nkind (Parent (N)) = N_Qualified_Expression
9028 and then Subtype_Mark (Parent (N)) = N
9029 and then Is_Qualified_Universal_Literal (Parent (N))
9030 then
9031 Set_Entity (New_N, Assoc);
9032
9033 -- Cope with the rewriting into expanded name that may have
9034 -- occurred in between, e.g. in Check_Generic_Child_Unit for
9035 -- generic renaming declarations.
9036
9037 elsif Nkind (Assoc) = N_Expanded_Name then
9038 Rewrite (N, New_Copy_Tree (Assoc));
9039 Set_Associated_Node (N, Assoc);
9040 return Copy_Generic_Node (N, Parent_Id, Instantiating);
9041
9042 -- The name in the call may be a selected component if the
9043 -- call has not been analyzed yet, as may be the case for
9044 -- pre/post conditions in a generic unit.
9045
9046 elsif Nkind (Assoc) = N_Function_Call
9047 and then Is_Entity_Name (Name (Assoc))
9048 then
9049 Set_Entity (New_N, Entity (Name (Assoc)));
9050 Check_Private_View (N);
9051
9052 elsif Nkind (Assoc) in N_Entity
9053 and then (Expander_Active
9054 or else (GNATprove_Mode
9055 and then not In_Spec_Expression
9056 and then not Inside_A_Generic))
9057 then
9058 -- Inlining case: we are copying a tree that contains
9059 -- global entities, which are preserved in the copy to be
9060 -- used for subsequent inlining.
9061
9062 null;
9063
9064 else
9065 Set_Entity (New_N, Empty);
9066 end if;
9067 end if;
9068 end;
9069 end if;
9070
9071 -- For expanded name, we must copy the Prefix and Selector_Name
9072
9073 if Nkind (N) = N_Expanded_Name then
9074 Set_Prefix
9075 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
9076
9077 Set_Selector_Name (New_N,
9078 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
9079
9080 -- For operators, copy the operands
9081
9082 elsif Nkind (N) in N_Op then
9083 if Nkind (N) in N_Binary_Op then
9084 Set_Left_Opnd (New_N,
9085 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
9086 end if;
9087
9088 Set_Right_Opnd (New_N,
9089 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
9090 end if;
9091
9092 -- Establish a link between an entity from the generic template and the
9093 -- corresponding entity in the generic copy to be analyzed.
9094
9095 elsif Nkind (N) in N_Entity then
9096 if not Instantiating then
9097 Set_Associated_Entity (N, New_N);
9098 end if;
9099
9100 -- Clear any existing link the copy may inherit from the replicated
9101 -- generic template entity.
9102
9103 Set_Associated_Entity (New_N, Empty);
9104
9105 -- Special casing for stubs
9106
9107 elsif Nkind (N) in N_Body_Stub then
9108
9109 -- In any case, we must copy the specification or defining
9110 -- identifier as appropriate.
9111
9112 if Nkind (N) = N_Subprogram_Body_Stub then
9113 Set_Specification (New_N,
9114 Copy_Generic_Node (Specification (N), New_N, Instantiating));
9115
9116 else
9117 Set_Defining_Identifier (New_N,
9118 Copy_Generic_Node
9119 (Defining_Identifier (N), New_N, Instantiating));
9120 end if;
9121
9122 -- If we are not instantiating, then this is where we load and
9123 -- analyze subunits, i.e. at the point where the stub occurs. A
9124 -- more permissive system might defer this analysis to the point
9125 -- of instantiation, but this seems too complicated for now.
9126
9127 if not Instantiating then
9128 declare
9129 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
9130 Subunit : Node_Id;
9131 Unum : Unit_Number_Type;
9132 New_Body : Node_Id;
9133
9134 begin
9135 -- Make sure that, if it is a subunit of the main unit that is
9136 -- preprocessed and if -gnateG is specified, the preprocessed
9137 -- file will be written.
9138
9139 Lib.Analysing_Subunit_Of_Main :=
9140 Lib.In_Extended_Main_Source_Unit (N);
9141 Unum :=
9142 Load_Unit
9143 (Load_Name => Subunit_Name,
9144 Required => False,
9145 Subunit => True,
9146 Error_Node => N);
9147 Lib.Analysing_Subunit_Of_Main := False;
9148
9149 -- If the proper body is not found, a warning message will be
9150 -- emitted when analyzing the stub, or later at the point of
9151 -- instantiation. Here we just leave the stub as is.
9152
9153 if Unum = No_Unit then
9154 Subunits_Missing := True;
9155 goto Subunit_Not_Found;
9156 end if;
9157
9158 Subunit := Cunit (Unum);
9159
9160 if Nkind (Unit (Subunit)) /= N_Subunit then
9161 Error_Msg_N
9162 ("found child unit instead of expected SEPARATE subunit",
9163 Subunit);
9164 Error_Msg_Sloc := Sloc (N);
9165 Error_Msg_N ("\to complete stub #", Subunit);
9166 goto Subunit_Not_Found;
9167 end if;
9168
9169 -- We must create a generic copy of the subunit, in order to
9170 -- perform semantic analysis on it, and we must replace the
9171 -- stub in the original generic unit with the subunit, in order
9172 -- to preserve non-local references within.
9173
9174 -- Only the proper body needs to be copied. Library_Unit and
9175 -- context clause are simply inherited by the generic copy.
9176 -- Note that the copy (which may be recursive if there are
9177 -- nested subunits) must be done first, before attaching it to
9178 -- the enclosing generic.
9179
9180 New_Body :=
9181 Copy_Generic_Node
9182 (Proper_Body (Unit (Subunit)),
9183 Empty, Instantiating => False);
9184
9185 -- Now place the original proper body in the original generic
9186 -- unit. This is a body, not a compilation unit.
9187
9188 Rewrite (N, Proper_Body (Unit (Subunit)));
9189 Set_Is_Compilation_Unit (Defining_Entity (N), False);
9190 Set_Was_Originally_Stub (N);
9191
9192 -- Finally replace the body of the subunit with its copy, and
9193 -- make this new subunit into the library unit of the generic
9194 -- copy, which does not have stubs any longer.
9195
9196 Set_Proper_Body (Unit (Subunit), New_Body);
9197 Set_Library_Unit (New_N, Subunit);
9198 Inherit_Context (Unit (Subunit), N);
9199 end;
9200
9201 -- If we are instantiating, this must be an error case, since
9202 -- otherwise we would have replaced the stub node by the proper body
9203 -- that corresponds. So just ignore it in the copy (i.e. we have
9204 -- copied it, and that is good enough).
9205
9206 else
9207 null;
9208 end if;
9209
9210 <<Subunit_Not_Found>> null;
9211
9212 -- If the node is a compilation unit, it is the subunit of a stub, which
9213 -- has been loaded already (see code below). In this case, the library
9214 -- unit field of N points to the parent unit (which is a compilation
9215 -- unit) and need not (and cannot) be copied.
9216
9217 -- When the proper body of the stub is analyzed, the library_unit link
9218 -- is used to establish the proper context (see sem_ch10).
9219
9220 -- The other fields of a compilation unit are copied as usual
9221
9222 elsif Nkind (N) = N_Compilation_Unit then
9223
9224 -- This code can only be executed when not instantiating, because in
9225 -- the copy made for an instantiation, the compilation unit node has
9226 -- disappeared at the point that a stub is replaced by its proper
9227 -- body.
9228
9229 pragma Assert (not Instantiating);
9230
9231 Set_Context_Items (New_N,
9232 Copy_Generic_List (Context_Items (N), New_N));
9233
9234 Set_Unit (New_N,
9235 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
9236
9237 Set_First_Inlined_Subprogram (New_N,
9238 Copy_Generic_Node
9239 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
9240
9241 Set_Aux_Decls_Node
9242 (New_N,
9243 Copy_Generic_Node
9244 (Aux_Decls_Node (N), New_N, Instantiating => False));
9245
9246 -- For an assignment node, the assignment is known to be semantically
9247 -- legal if we are instantiating the template. This avoids incorrect
9248 -- diagnostics in generated code.
9249
9250 elsif Nkind (N) = N_Assignment_Statement then
9251
9252 -- Copy name and expression fields in usual manner
9253
9254 Set_Name (New_N,
9255 Copy_Generic_Node (Name (N), New_N, Instantiating));
9256
9257 Set_Expression (New_N,
9258 Copy_Generic_Node (Expression (N), New_N, Instantiating));
9259
9260 if Instantiating then
9261 Set_Assignment_OK (Name (New_N), True);
9262 end if;
9263
9264 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
9265 if not Instantiating then
9266 Set_Associated_Node (N, New_N);
9267
9268 else
9269 -- If, in the generic, the aggregate has a global composite type
9270 -- and, at the point of instantiation, the type has a private view
9271 -- then install the full view.
9272
9273 declare
9274 Assoc : constant Node_Id := Get_Associated_Node (N);
9275
9276 begin
9277 if Present (Assoc)
9278 and then Nkind (Assoc) = Nkind (N)
9279 and then Present (Etype (Assoc))
9280 and then Is_Private_Type (Etype (Assoc))
9281 then
9282 Switch_View (Etype (Assoc));
9283 end if;
9284 end;
9285
9286 -- Moreover, for a full aggregate, if the type is a derived tagged
9287 -- type and has a global ancestor, then also restore the full view
9288 -- of this ancestor and do so up to the root type. Beware that the
9289 -- Ancestor_Type field is overloaded, so test that it's an entity.
9290
9291 if Nkind (N) = N_Aggregate
9292 and then Present (Ancestor_Type (N))
9293 and then Nkind (Ancestor_Type (N)) in N_Entity
9294 then
9295 declare
9296 Root_Typ : constant Entity_Id :=
9297 Root_Type (Ancestor_Type (N));
9298
9299 Typ : Entity_Id := Ancestor_Type (N);
9300
9301 begin
9302 loop
9303 if Is_Private_Type (Typ) then
9304 Switch_View (Typ);
9305 end if;
9306
9307 exit when Typ = Root_Typ;
9308
9309 Typ := Etype (Typ);
9310 end loop;
9311 end;
9312 end if;
9313 end if;
9314
9315 -- Do not copy the associated node, which points to the generic copy
9316 -- of the aggregate.
9317
9318 if Nkind (N) = N_Aggregate then
9319 Set_Aggregate_Bounds
9320 (New_N,
9321 Node_Id (Copy_Generic_Descendant
9322 (Union_Id (Aggregate_Bounds (N)))));
9323
9324 elsif Nkind (N) = N_Extension_Aggregate then
9325 Set_Ancestor_Part
9326 (New_N,
9327 Node_Id (Copy_Generic_Descendant
9328 (Union_Id (Ancestor_Part (N)))));
9329
9330 else
9331 pragma Assert (False);
9332 end if;
9333
9334 Set_Expressions
9335 (New_N,
9336 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
9337 Set_Component_Associations
9338 (New_N,
9339 List_Id (Copy_Generic_Descendant
9340 (Union_Id (Component_Associations (N)))));
9341 Set_Etype
9342 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
9343
9344 -- Allocators do not have an identifier denoting the access type, so we
9345 -- must locate it through the expression to check whether the views are
9346 -- consistent.
9347
9348 elsif Nkind (N) = N_Allocator
9349 and then Nkind (Expression (N)) = N_Qualified_Expression
9350 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
9351 and then Instantiating
9352 then
9353 declare
9354 T : constant Node_Id :=
9355 Get_Associated_Node (Subtype_Mark (Expression (N)));
9356 Acc_T : Entity_Id;
9357
9358 begin
9359 if Present (T) then
9360
9361 -- Retrieve the allocator node in the generic copy
9362
9363 Acc_T := Etype (Parent (Parent (T)));
9364
9365 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
9366 Switch_View (Acc_T);
9367 end if;
9368 end if;
9369
9370 Copy_Descendants;
9371 end;
9372
9373 -- Loop parameter specifications do not have an identifier denoting the
9374 -- index type, so we must locate it through the defining identifier to
9375 -- check whether the views are consistent.
9376
9377 elsif Nkind (N) = N_Loop_Parameter_Specification
9378 and then Instantiating
9379 then
9380 declare
9381 Id : constant Entity_Id :=
9382 Get_Associated_Entity (Defining_Identifier (N));
9383
9384 Index_T : Entity_Id;
9385
9386 begin
9387 if Present (Id) and then Present (Etype (Id)) then
9388 Index_T := First_Subtype (Etype (Id));
9389
9390 if Present (Index_T) and then Is_Private_Type (Index_T) then
9391 Switch_View (Index_T);
9392 end if;
9393 end if;
9394
9395 Copy_Descendants;
9396 end;
9397
9398 -- For a proper body, we must catch the case of a proper body that
9399 -- replaces a stub. This represents the point at which a separate
9400 -- compilation unit, and hence template file, may be referenced, so we
9401 -- must make a new source instantiation entry for the template of the
9402 -- subunit, and ensure that all nodes in the subunit are adjusted using
9403 -- this new source instantiation entry.
9404
9405 elsif Nkind (N) in N_Proper_Body then
9406 declare
9407 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
9408 begin
9409 if Instantiating and then Was_Originally_Stub (N) then
9410 Create_Instantiation_Source
9411 (Instantiation_Node,
9412 Defining_Entity (N),
9413 S_Adjustment);
9414
9415 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
9416 end if;
9417
9418 -- Now copy the fields of the proper body, using the new
9419 -- adjustment factor if one was needed as per test above.
9420
9421 Copy_Descendants;
9422
9423 -- Restore the original adjustment factor
9424
9425 S_Adjustment := Save_Adjustment;
9426 end;
9427
9428 elsif Nkind (N) = N_Pragma and then Instantiating then
9429
9430 -- Do not copy Comment or Ident pragmas their content is relevant to
9431 -- the generic unit, not to the instantiating unit.
9432
9433 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
9434 New_N := Make_Null_Statement (Sloc (N));
9435
9436 -- Do not copy pragmas generated from aspects because the pragmas do
9437 -- not carry any semantic information, plus they will be regenerated
9438 -- in the instance.
9439
9440 -- However, generating C we need to copy them since postconditions
9441 -- are inlined by the front end, and the front-end inlining machinery
9442 -- relies on this routine to perform inlining.
9443
9444 elsif From_Aspect_Specification (N) then
9445 New_N := Make_Null_Statement (Sloc (N));
9446
9447 else
9448 Copy_Descendants;
9449 end if;
9450
9451 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
9452
9453 -- No descendant fields need traversing
9454
9455 null;
9456
9457 elsif Nkind (N) = N_String_Literal
9458 and then Present (Etype (N))
9459 and then Instantiating
9460 then
9461 -- If the string is declared in an outer scope, the string_literal
9462 -- subtype created for it may have the wrong scope. Force reanalysis
9463 -- of the constant to generate a new itype in the proper context.
9464
9465 Set_Etype (New_N, Empty);
9466 Set_Analyzed (New_N, False);
9467
9468 -- For the remaining nodes, copy their descendants recursively
9469
9470 else
9471 Copy_Descendants;
9472
9473 if Instantiating and then Nkind (N) = N_Subprogram_Body then
9474 Set_Generic_Parent (Specification (New_N), N);
9475
9476 -- Should preserve Corresponding_Spec??? (12.3(14))
9477 end if;
9478 end if;
9479
9480 -- Propagate dimensions if present, so that they are reflected in the
9481 -- instance.
9482
9483 if Nkind (N) in N_Has_Etype
9484 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
9485 and then Present (Etype (N))
9486 and then Is_Floating_Point_Type (Etype (N))
9487 and then Has_Dimension_System (Etype (N))
9488 then
9489 Copy_Dimensions (N, New_N);
9490 end if;
9491
9492 return New_N;
9493 end Copy_Generic_Node;
9494
9495 ----------------------------
9496 -- Denotes_Formal_Package --
9497 ----------------------------
9498
9499 function Denotes_Formal_Package
9500 (Pack : Entity_Id;
9501 On_Exit : Boolean := False;
9502 Instance : Entity_Id := Empty) return Boolean
9503 is
9504 Par : Entity_Id;
9505 Scop : constant Entity_Id := Scope (Pack);
9506 E : Entity_Id;
9507
9508 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
9509 -- The package in question may be an actual for a previous formal
9510 -- package P of the current instance, so examine its actuals as well.
9511 -- This must be recursive over other formal packages.
9512
9513 ----------------------------------
9514 -- Is_Actual_Of_Previous_Formal --
9515 ----------------------------------
9516
9517 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
9518 E1 : Entity_Id;
9519
9520 begin
9521 E1 := First_Entity (P);
9522 while Present (E1) and then E1 /= Instance loop
9523 if Ekind (E1) = E_Package
9524 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
9525 then
9526 if Renamed_Entity (E1) = Pack then
9527 return True;
9528
9529 elsif E1 = P or else Renamed_Entity (E1) = P then
9530 return False;
9531
9532 elsif Is_Actual_Of_Previous_Formal (E1) then
9533 return True;
9534 end if;
9535 end if;
9536
9537 Next_Entity (E1);
9538 end loop;
9539
9540 return False;
9541 end Is_Actual_Of_Previous_Formal;
9542
9543 -- Start of processing for Denotes_Formal_Package
9544
9545 begin
9546 if On_Exit then
9547 Par :=
9548 Instance_Envs.Table
9549 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
9550 else
9551 Par := Current_Instantiated_Parent.Act_Id;
9552 end if;
9553
9554 if Ekind (Scop) = E_Generic_Package
9555 or else Nkind (Unit_Declaration_Node (Scop)) =
9556 N_Generic_Subprogram_Declaration
9557 then
9558 return True;
9559
9560 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
9561 N_Formal_Package_Declaration
9562 then
9563 return True;
9564
9565 elsif No (Par) then
9566 return False;
9567
9568 else
9569 -- Check whether this package is associated with a formal package of
9570 -- the enclosing instantiation. Iterate over the list of renamings.
9571
9572 E := First_Entity (Par);
9573 while Present (E) loop
9574 if Ekind (E) /= E_Package
9575 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
9576 then
9577 null;
9578
9579 elsif Renamed_Entity (E) = Par then
9580 return False;
9581
9582 elsif Renamed_Entity (E) = Pack then
9583 return True;
9584
9585 elsif Is_Actual_Of_Previous_Formal (E) then
9586 return True;
9587
9588 end if;
9589
9590 Next_Entity (E);
9591 end loop;
9592
9593 return False;
9594 end if;
9595 end Denotes_Formal_Package;
9596
9597 -----------------
9598 -- End_Generic --
9599 -----------------
9600
9601 procedure End_Generic is
9602 begin
9603 -- ??? More things could be factored out in this routine. Should
9604 -- probably be done at a later stage.
9605
9606 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
9607 Generic_Flags.Decrement_Last;
9608
9609 Expander_Mode_Restore;
9610 end End_Generic;
9611
9612 -------------
9613 -- Earlier --
9614 -------------
9615
9616 function Earlier (N1, N2 : Node_Id) return Boolean is
9617 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
9618 -- Find distance from given node to enclosing compilation unit
9619
9620 ----------------
9621 -- Find_Depth --
9622 ----------------
9623
9624 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
9625 begin
9626 while Present (P)
9627 and then Nkind (P) /= N_Compilation_Unit
9628 loop
9629 P := True_Parent (P);
9630 D := D + 1;
9631 end loop;
9632 end Find_Depth;
9633
9634 -- Local declarations
9635
9636 D1 : Integer := 0;
9637 D2 : Integer := 0;
9638 P1 : Node_Id := N1;
9639 P2 : Node_Id := N2;
9640 T1 : Source_Ptr;
9641 T2 : Source_Ptr;
9642
9643 -- Start of processing for Earlier
9644
9645 begin
9646 Find_Depth (P1, D1);
9647 Find_Depth (P2, D2);
9648
9649 if P1 /= P2 then
9650 return False;
9651 else
9652 P1 := N1;
9653 P2 := N2;
9654 end if;
9655
9656 while D1 > D2 loop
9657 P1 := True_Parent (P1);
9658 D1 := D1 - 1;
9659 end loop;
9660
9661 while D2 > D1 loop
9662 P2 := True_Parent (P2);
9663 D2 := D2 - 1;
9664 end loop;
9665
9666 -- At this point P1 and P2 are at the same distance from the root.
9667 -- We examine their parents until we find a common declarative list.
9668 -- If we reach the root, N1 and N2 do not descend from the same
9669 -- declarative list (e.g. one is nested in the declarative part and
9670 -- the other is in a block in the statement part) and the earlier
9671 -- one is already frozen.
9672
9673 while not Is_List_Member (P1)
9674 or else not Is_List_Member (P2)
9675 or else not In_Same_List (P1, P2)
9676 loop
9677 P1 := True_Parent (P1);
9678 P2 := True_Parent (P2);
9679
9680 if Nkind (Parent (P1)) = N_Subunit then
9681 P1 := Corresponding_Stub (Parent (P1));
9682 end if;
9683
9684 if Nkind (Parent (P2)) = N_Subunit then
9685 P2 := Corresponding_Stub (Parent (P2));
9686 end if;
9687
9688 if P1 = P2 then
9689 return False;
9690 end if;
9691 end loop;
9692
9693 -- Expanded code usually shares the source location of the original
9694 -- construct it was generated for. This however may not necessarily
9695 -- reflect the true location of the code within the tree.
9696
9697 -- Before comparing the slocs of the two nodes, make sure that we are
9698 -- working with correct source locations. Assume that P1 is to the left
9699 -- of P2. If either one does not come from source, traverse the common
9700 -- list heading towards the other node and locate the first source
9701 -- statement.
9702
9703 -- P1 P2
9704 -- ----+===+===+--------------+===+===+----
9705 -- expanded code expanded code
9706
9707 if not Comes_From_Source (P1) then
9708 while Present (P1) loop
9709
9710 -- Neither P2 nor a source statement were located during the
9711 -- search. If we reach the end of the list, then P1 does not
9712 -- occur earlier than P2.
9713
9714 -- ---->
9715 -- start --- P2 ----- P1 --- end
9716
9717 if No (Next (P1)) then
9718 return False;
9719
9720 -- We encounter P2 while going to the right of the list. This
9721 -- means that P1 does indeed appear earlier.
9722
9723 -- ---->
9724 -- start --- P1 ===== P2 --- end
9725 -- expanded code in between
9726
9727 elsif P1 = P2 then
9728 return True;
9729
9730 -- No need to look any further since we have located a source
9731 -- statement.
9732
9733 elsif Comes_From_Source (P1) then
9734 exit;
9735 end if;
9736
9737 -- Keep going right
9738
9739 Next (P1);
9740 end loop;
9741 end if;
9742
9743 if not Comes_From_Source (P2) then
9744 while Present (P2) loop
9745
9746 -- Neither P1 nor a source statement were located during the
9747 -- search. If we reach the start of the list, then P1 does not
9748 -- occur earlier than P2.
9749
9750 -- <----
9751 -- start --- P2 --- P1 --- end
9752
9753 if No (Prev (P2)) then
9754 return False;
9755
9756 -- We encounter P1 while going to the left of the list. This
9757 -- means that P1 does indeed appear earlier.
9758
9759 -- <----
9760 -- start --- P1 ===== P2 --- end
9761 -- expanded code in between
9762
9763 elsif P2 = P1 then
9764 return True;
9765
9766 -- No need to look any further since we have located a source
9767 -- statement.
9768
9769 elsif Comes_From_Source (P2) then
9770 exit;
9771 end if;
9772
9773 -- Keep going left
9774
9775 Prev (P2);
9776 end loop;
9777 end if;
9778
9779 -- At this point either both nodes came from source or we approximated
9780 -- their source locations through neighboring source statements.
9781
9782 T1 := Top_Level_Location (Sloc (P1));
9783 T2 := Top_Level_Location (Sloc (P2));
9784
9785 -- When two nodes come from the same instance, they have identical top
9786 -- level locations. To determine proper relation within the tree, check
9787 -- their locations within the template.
9788
9789 if T1 = T2 then
9790 return Sloc (P1) < Sloc (P2);
9791
9792 -- The two nodes either come from unrelated instances or do not come
9793 -- from instantiated code at all.
9794
9795 else
9796 return T1 < T2;
9797 end if;
9798 end Earlier;
9799
9800 ----------------------
9801 -- Find_Actual_Type --
9802 ----------------------
9803
9804 function Find_Actual_Type
9805 (Typ : Entity_Id;
9806 Gen_Type : Entity_Id) return Entity_Id
9807 is
9808 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
9809 T : Entity_Id;
9810
9811 begin
9812 -- Special processing only applies to child units
9813
9814 if not Is_Child_Unit (Gen_Scope) then
9815 return Get_Instance_Of (Typ);
9816
9817 -- If designated or component type is itself a formal of the child unit,
9818 -- its instance is available.
9819
9820 elsif Scope (Typ) = Gen_Scope then
9821 return Get_Instance_Of (Typ);
9822
9823 -- If the array or access type is not declared in the parent unit,
9824 -- no special processing needed.
9825
9826 elsif not Is_Generic_Type (Typ)
9827 and then Scope (Gen_Scope) /= Scope (Typ)
9828 then
9829 return Get_Instance_Of (Typ);
9830
9831 -- Otherwise, retrieve designated or component type by visibility
9832
9833 else
9834 T := Current_Entity (Typ);
9835 while Present (T) loop
9836 if In_Open_Scopes (Scope (T)) then
9837 return T;
9838 elsif Is_Generic_Actual_Type (T) then
9839 return T;
9840 end if;
9841
9842 T := Homonym (T);
9843 end loop;
9844
9845 return Typ;
9846 end if;
9847 end Find_Actual_Type;
9848
9849 -----------------------------
9850 -- Freeze_Package_Instance --
9851 -----------------------------
9852
9853 procedure Freeze_Package_Instance
9854 (N : Node_Id;
9855 Gen_Body : Node_Id;
9856 Gen_Decl : Node_Id;
9857 Act_Id : Entity_Id)
9858 is
9859 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9860 -- Check if the generic definition and the instantiation come from
9861 -- a common scope, in which case the instance must be frozen after
9862 -- the generic body.
9863
9864 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9865 -- If the instance is nested inside a generic unit, the Sloc of the
9866 -- instance indicates the place of the original definition, not the
9867 -- point of the current enclosing instance. Pending a better usage of
9868 -- Slocs to indicate instantiation places, we determine the place of
9869 -- origin of a node by finding the maximum sloc of any ancestor node.
9870
9871 -- Why is this not equivalent to Top_Level_Location ???
9872
9873 -------------------
9874 -- In_Same_Scope --
9875 -------------------
9876
9877 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9878 Act_Scop : Entity_Id := Scope (Act_Id);
9879 Gen_Scop : Entity_Id := Scope (Gen_Id);
9880
9881 begin
9882 while Act_Scop /= Standard_Standard
9883 and then Gen_Scop /= Standard_Standard
9884 loop
9885 if Act_Scop = Gen_Scop then
9886 return True;
9887 end if;
9888
9889 Act_Scop := Scope (Act_Scop);
9890 Gen_Scop := Scope (Gen_Scop);
9891 end loop;
9892
9893 return False;
9894 end In_Same_Scope;
9895
9896 ---------------
9897 -- True_Sloc --
9898 ---------------
9899
9900 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9901 N1 : Node_Id;
9902 Res : Source_Ptr;
9903
9904 begin
9905 Res := Sloc (N);
9906 N1 := N;
9907 while Present (N1) and then N1 /= Act_Unit loop
9908 if Sloc (N1) > Res then
9909 Res := Sloc (N1);
9910 end if;
9911
9912 N1 := Parent (N1);
9913 end loop;
9914
9915 return Res;
9916 end True_Sloc;
9917
9918 -- Local variables
9919
9920 Gen_Id : constant Entity_Id := Get_Generic_Entity (N);
9921 Par_Id : constant Entity_Id := Scope (Gen_Id);
9922 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9923 Gen_Unit : constant Node_Id :=
9924 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9925
9926 Body_Unit : Node_Id;
9927 F_Node : Node_Id;
9928 Must_Delay : Boolean;
9929 Orig_Body : Node_Id;
9930
9931 -- Start of processing for Freeze_Package_Instance
9932
9933 begin
9934 -- If the body is a subunit, the freeze point is the corresponding stub
9935 -- in the current compilation, not the subunit itself.
9936
9937 if Nkind (Parent (Gen_Body)) = N_Subunit then
9938 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9939 else
9940 Orig_Body := Gen_Body;
9941 end if;
9942
9943 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9944
9945 -- If the instantiation and the generic definition appear in the same
9946 -- package declaration, this is an early instantiation. If they appear
9947 -- in the same declarative part, it is an early instantiation only if
9948 -- the generic body appears textually later, and the generic body is
9949 -- also in the main unit.
9950
9951 -- If instance is nested within a subprogram, and the generic body
9952 -- is not, the instance is delayed because the enclosing body is. If
9953 -- instance and body are within the same scope, or the same subprogram
9954 -- body, indicate explicitly that the instance is delayed.
9955
9956 Must_Delay :=
9957 (Gen_Unit = Act_Unit
9958 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9959 | N_Package_Declaration
9960 or else (Gen_Unit = Body_Unit
9961 and then
9962 True_Sloc (N, Act_Unit) < Sloc (Orig_Body)))
9963 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9964 and then In_Same_Scope (Gen_Id, Act_Id));
9965
9966 -- If this is an early instantiation, the freeze node is placed after
9967 -- the generic body. Otherwise, if the generic appears in an instance,
9968 -- we cannot freeze the current instance until the outer one is frozen.
9969 -- This is only relevant if the current instance is nested within some
9970 -- inner scope not itself within the outer instance. If this scope is
9971 -- a package body in the same declarative part as the outer instance,
9972 -- then that body needs to be frozen after the outer instance. Finally,
9973 -- if no delay is needed, we place the freeze node at the end of the
9974 -- current declarative part.
9975
9976 if No (Freeze_Node (Act_Id))
9977 or else not Is_List_Member (Freeze_Node (Act_Id))
9978 then
9979 Ensure_Freeze_Node (Act_Id);
9980 F_Node := Freeze_Node (Act_Id);
9981
9982 if Must_Delay then
9983 Insert_After (Orig_Body, F_Node);
9984
9985 elsif Is_Generic_Instance (Par_Id)
9986 and then Present (Freeze_Node (Par_Id))
9987 and then Scope (Act_Id) /= Par_Id
9988 then
9989 -- Freeze instance of inner generic after instance of enclosing
9990 -- generic.
9991
9992 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N) then
9993
9994 -- Handle the following case:
9995
9996 -- package Parent_Inst is new ...
9997 -- freeze Parent_Inst []
9998
9999 -- procedure P ... -- this body freezes Parent_Inst
10000
10001 -- package Inst is new ...
10002
10003 -- In this particular scenario, the freeze node for Inst must
10004 -- be inserted in the same manner as that of Parent_Inst,
10005 -- before the next source body or at the end of the declarative
10006 -- list (body not available). If body P did not exist and
10007 -- Parent_Inst was frozen after Inst, either by a body
10008 -- following Inst or at the end of the declarative region,
10009 -- the freeze node for Inst must be inserted after that of
10010 -- Parent_Inst. This relation is established by comparing
10011 -- the Slocs of Parent_Inst freeze node and Inst.
10012 -- We examine the parents of the enclosing lists to handle
10013 -- the case where the parent instance is in the visible part
10014 -- of a package declaration, and the inner instance is in
10015 -- the corresponding private part.
10016
10017 if Parent (List_Containing (Freeze_Node (Par_Id)))
10018 = Parent (List_Containing (N))
10019 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
10020 then
10021 Insert_Freeze_Node_For_Instance (N, F_Node);
10022 else
10023 Insert_After (Freeze_Node (Par_Id), F_Node);
10024 end if;
10025
10026 -- Freeze package enclosing instance of inner generic after
10027 -- instance of enclosing generic.
10028
10029 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
10030 and then In_Same_Declarative_Part
10031 (Parent (Freeze_Node (Par_Id)), Parent (N))
10032 then
10033 declare
10034 Enclosing : Entity_Id;
10035
10036 begin
10037 Enclosing := Corresponding_Spec (Parent (N));
10038
10039 if No (Enclosing) then
10040 Enclosing := Defining_Entity (Parent (N));
10041 end if;
10042
10043 Insert_Freeze_Node_For_Instance (N, F_Node);
10044 Ensure_Freeze_Node (Enclosing);
10045
10046 if not Is_List_Member (Freeze_Node (Enclosing)) then
10047
10048 -- The enclosing context is a subunit, insert the freeze
10049 -- node after the stub.
10050
10051 if Nkind (Parent (Parent (N))) = N_Subunit then
10052 Insert_Freeze_Node_For_Instance
10053 (Corresponding_Stub (Parent (Parent (N))),
10054 Freeze_Node (Enclosing));
10055
10056 -- The enclosing context is a package with a stub body
10057 -- which has already been replaced by the real body.
10058 -- Insert the freeze node after the actual body.
10059
10060 elsif Ekind (Enclosing) = E_Package
10061 and then Present (Body_Entity (Enclosing))
10062 and then Was_Originally_Stub
10063 (Parent (Body_Entity (Enclosing)))
10064 then
10065 Insert_Freeze_Node_For_Instance
10066 (Parent (Body_Entity (Enclosing)),
10067 Freeze_Node (Enclosing));
10068
10069 -- The parent instance has been frozen before the body of
10070 -- the enclosing package, insert the freeze node after
10071 -- the body.
10072
10073 elsif In_Same_List (Freeze_Node (Par_Id), Parent (N))
10074 and then
10075 Sloc (Freeze_Node (Par_Id)) <= Sloc (Parent (N))
10076 then
10077 Insert_Freeze_Node_For_Instance
10078 (Parent (N), Freeze_Node (Enclosing));
10079
10080 else
10081 Insert_After
10082 (Freeze_Node (Par_Id), Freeze_Node (Enclosing));
10083 end if;
10084 end if;
10085 end;
10086
10087 else
10088 Insert_Freeze_Node_For_Instance (N, F_Node);
10089 end if;
10090
10091 else
10092 Insert_Freeze_Node_For_Instance (N, F_Node);
10093 end if;
10094 end if;
10095 end Freeze_Package_Instance;
10096
10097 --------------------------------
10098 -- Freeze_Subprogram_Instance --
10099 --------------------------------
10100
10101 procedure Freeze_Subprogram_Instance
10102 (N : Node_Id;
10103 Gen_Body : Node_Id;
10104 Pack_Id : Entity_Id)
10105 is
10106 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
10107 -- Find innermost package body that encloses the given node, and which
10108 -- is not a compilation unit. Freeze nodes for the instance, or for its
10109 -- enclosing body, may be inserted after the enclosing_body of the
10110 -- generic unit. Used to determine proper placement of freeze node for
10111 -- both package and subprogram instances.
10112
10113 function Package_Freeze_Node (B : Node_Id) return Node_Id;
10114 -- Find entity for given package body, and locate or create a freeze
10115 -- node for it.
10116
10117 ----------------------------
10118 -- Enclosing_Package_Body --
10119 ----------------------------
10120
10121 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
10122 P : Node_Id;
10123
10124 begin
10125 P := Parent (N);
10126 while Present (P)
10127 and then Nkind (Parent (P)) /= N_Compilation_Unit
10128 loop
10129 if Nkind (P) = N_Package_Body then
10130 if Nkind (Parent (P)) = N_Subunit then
10131 return Corresponding_Stub (Parent (P));
10132 else
10133 return P;
10134 end if;
10135 end if;
10136
10137 P := True_Parent (P);
10138 end loop;
10139
10140 return Empty;
10141 end Enclosing_Package_Body;
10142
10143 -------------------------
10144 -- Package_Freeze_Node --
10145 -------------------------
10146
10147 function Package_Freeze_Node (B : Node_Id) return Node_Id is
10148 Id : Entity_Id;
10149
10150 begin
10151 if Nkind (B) = N_Package_Body then
10152 Id := Corresponding_Spec (B);
10153 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
10154 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
10155 end if;
10156
10157 Ensure_Freeze_Node (Id);
10158 return Freeze_Node (Id);
10159 end Package_Freeze_Node;
10160
10161 -- Local variables
10162
10163 Enc_G : constant Node_Id := Enclosing_Package_Body (Gen_Body);
10164 Enc_N : constant Node_Id := Enclosing_Package_Body (N);
10165 Par_Id : constant Entity_Id := Scope (Get_Generic_Entity (N));
10166
10167 Enc_G_F : Node_Id;
10168 F_Node : Node_Id;
10169
10170 -- Start of processing for Freeze_Subprogram_Instance
10171
10172 begin
10173 -- If the instance and the generic body appear within the same unit, and
10174 -- the instance precedes the generic, the freeze node for the instance
10175 -- must appear after that of the generic. If the generic is nested
10176 -- within another instance I2, then current instance must be frozen
10177 -- after I2. In both cases, the freeze nodes are those of enclosing
10178 -- packages. Otherwise, the freeze node is placed at the end of the
10179 -- current declarative part.
10180
10181 Ensure_Freeze_Node (Pack_Id);
10182 F_Node := Freeze_Node (Pack_Id);
10183
10184 if Is_Generic_Instance (Par_Id)
10185 and then Present (Freeze_Node (Par_Id))
10186 and then In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N)
10187 then
10188 -- The parent was a premature instantiation. Insert freeze node at
10189 -- the end the current declarative part.
10190
10191 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par_Id)) then
10192 Insert_Freeze_Node_For_Instance (N, F_Node);
10193
10194 -- Handle the following case:
10195 --
10196 -- package Parent_Inst is new ...
10197 -- freeze Parent_Inst []
10198 --
10199 -- procedure P ... -- this body freezes Parent_Inst
10200 --
10201 -- procedure Inst is new ...
10202 --
10203 -- In this particular scenario, the freeze node for Inst must be
10204 -- inserted in the same manner as that of Parent_Inst - before the
10205 -- next source body or at the end of the declarative list (body not
10206 -- available). If body P did not exist and Parent_Inst was frozen
10207 -- after Inst, either by a body following Inst or at the end of the
10208 -- declarative region, the freeze node for Inst must be inserted
10209 -- after that of Parent_Inst. This relation is established by
10210 -- comparing the Slocs of Parent_Inst freeze node and Inst.
10211
10212 elsif In_Same_List (Freeze_Node (Par_Id), N)
10213 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
10214 then
10215 Insert_Freeze_Node_For_Instance (N, F_Node);
10216
10217 else
10218 Insert_After (Freeze_Node (Par_Id), F_Node);
10219 end if;
10220
10221 -- The body enclosing the instance should be frozen after the body that
10222 -- includes the generic, because the body of the instance may make
10223 -- references to entities therein. If the two are not in the same
10224 -- declarative part, or if the one enclosing the instance is frozen
10225 -- already, freeze the instance at the end of the current declarative
10226 -- part.
10227
10228 elsif Is_Generic_Instance (Par_Id)
10229 and then Present (Freeze_Node (Par_Id))
10230 and then Present (Enc_N)
10231 then
10232 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), Enc_N)
10233 then
10234 -- The enclosing package may contain several instances. Rather
10235 -- than computing the earliest point at which to insert its freeze
10236 -- node, we place it at the end of the declarative part of the
10237 -- parent of the generic.
10238
10239 Insert_Freeze_Node_For_Instance
10240 (Freeze_Node (Par_Id), Package_Freeze_Node (Enc_N));
10241 end if;
10242
10243 Insert_Freeze_Node_For_Instance (N, F_Node);
10244
10245 elsif Present (Enc_G)
10246 and then Present (Enc_N)
10247 and then Enc_G /= Enc_N
10248 and then Earlier (N, Gen_Body)
10249 then
10250 -- Freeze package that encloses instance, and place node after the
10251 -- package that encloses generic. If enclosing package is already
10252 -- frozen we have to assume it is at the proper place. This may be a
10253 -- potential ABE that requires dynamic checking. Do not add a freeze
10254 -- node if the package that encloses the generic is inside the body
10255 -- that encloses the instance, because the freeze node would be in
10256 -- the wrong scope. Additional contortions needed if the bodies are
10257 -- within a subunit.
10258
10259 declare
10260 Enclosing_Body : Node_Id;
10261
10262 begin
10263 if Nkind (Enc_N) = N_Package_Body_Stub then
10264 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_N)));
10265 else
10266 Enclosing_Body := Enc_N;
10267 end if;
10268
10269 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
10270 Insert_Freeze_Node_For_Instance
10271 (Enc_G, Package_Freeze_Node (Enc_N));
10272 end if;
10273 end;
10274
10275 -- Freeze enclosing subunit before instance
10276
10277 Enc_G_F := Package_Freeze_Node (Enc_G);
10278
10279 if not Is_List_Member (Enc_G_F) then
10280 Insert_After (Enc_G, Enc_G_F);
10281 end if;
10282
10283 Insert_Freeze_Node_For_Instance (N, F_Node);
10284
10285 else
10286 -- If none of the above, insert freeze node at the end of the current
10287 -- declarative part.
10288
10289 Insert_Freeze_Node_For_Instance (N, F_Node);
10290 end if;
10291 end Freeze_Subprogram_Instance;
10292
10293 ----------------
10294 -- Get_Gen_Id --
10295 ----------------
10296
10297 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
10298 begin
10299 return Generic_Renamings.Table (E).Gen_Id;
10300 end Get_Gen_Id;
10301
10302 ---------------------
10303 -- Get_Instance_Of --
10304 ---------------------
10305
10306 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
10307 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
10308
10309 begin
10310 if Res /= Assoc_Null then
10311 return Generic_Renamings.Table (Res).Act_Id;
10312
10313 else
10314 -- On exit, entity is not instantiated: not a generic parameter, or
10315 -- else parameter of an inner generic unit.
10316
10317 return A;
10318 end if;
10319 end Get_Instance_Of;
10320
10321 ---------------------------------
10322 -- Get_Unit_Instantiation_Node --
10323 ---------------------------------
10324
10325 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
10326 Decl : Node_Id := Unit_Declaration_Node (A);
10327 Inst : Node_Id;
10328
10329 begin
10330 -- If the Package_Instantiation attribute has been set on the package
10331 -- entity, then use it directly when it (or its Original_Node) refers
10332 -- to an N_Package_Instantiation node. In principle it should be
10333 -- possible to have this field set in all cases, which should be
10334 -- investigated, and would allow this function to be significantly
10335 -- simplified. ???
10336
10337 Inst := Package_Instantiation (A);
10338
10339 if Present (Inst) then
10340 if Nkind (Inst) = N_Package_Instantiation then
10341 return Inst;
10342
10343 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
10344 return Original_Node (Inst);
10345 end if;
10346 end if;
10347
10348 -- If the instantiation is a compilation unit that does not need body
10349 -- then the instantiation node has been rewritten as a package
10350 -- declaration for the instance, and we return the original node.
10351
10352 -- If it is a compilation unit and the instance node has not been
10353 -- rewritten, then it is still the unit of the compilation. Finally, if
10354 -- a body is present, this is a parent of the main unit whose body has
10355 -- been compiled for inlining purposes, and the instantiation node has
10356 -- been rewritten with the instance body.
10357
10358 -- Otherwise the instantiation node appears after the declaration. If
10359 -- the entity is a formal package, the declaration may have been
10360 -- rewritten as a generic declaration (in the case of a formal with box)
10361 -- or left as a formal package declaration if it has actuals, and is
10362 -- found with a forward search.
10363
10364 if Nkind (Parent (Decl)) = N_Compilation_Unit then
10365 if Nkind (Decl) = N_Package_Declaration
10366 and then Present (Corresponding_Body (Decl))
10367 then
10368 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
10369 end if;
10370
10371 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
10372 return Original_Node (Decl);
10373 else
10374 return Unit (Parent (Decl));
10375 end if;
10376
10377 elsif Nkind (Decl) = N_Package_Declaration
10378 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
10379 then
10380 return Original_Node (Decl);
10381
10382 else
10383 Inst := Next (Decl);
10384 while Nkind (Inst) not in N_Formal_Package_Declaration
10385 | N_Function_Instantiation
10386 | N_Package_Instantiation
10387 | N_Procedure_Instantiation
10388 loop
10389 Next (Inst);
10390 end loop;
10391
10392 return Inst;
10393 end if;
10394 end Get_Unit_Instantiation_Node;
10395
10396 ------------------------
10397 -- Has_Been_Exchanged --
10398 ------------------------
10399
10400 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
10401 Next : Elmt_Id;
10402
10403 begin
10404 Next := First_Elmt (Exchanged_Views);
10405 while Present (Next) loop
10406 if Full_View (Node (Next)) = E then
10407 return True;
10408 end if;
10409
10410 Next_Elmt (Next);
10411 end loop;
10412
10413 return False;
10414 end Has_Been_Exchanged;
10415
10416 -------------------
10417 -- Has_Contracts --
10418 -------------------
10419
10420 function Has_Contracts (Decl : Node_Id) return Boolean is
10421 A_List : constant List_Id := Aspect_Specifications (Decl);
10422 A_Spec : Node_Id;
10423 A_Id : Aspect_Id;
10424 begin
10425 A_Spec := First (A_List);
10426 while Present (A_Spec) loop
10427 A_Id := Get_Aspect_Id (A_Spec);
10428 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
10429 return True;
10430 end if;
10431
10432 Next (A_Spec);
10433 end loop;
10434
10435 return False;
10436 end Has_Contracts;
10437
10438 -------------------------------
10439 -- Has_Fully_Defined_Profile --
10440 -------------------------------
10441
10442 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
10443 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
10444 -- Determine whethet type Typ is fully defined
10445
10446 ---------------------------
10447 -- Is_Fully_Defined_Type --
10448 ---------------------------
10449
10450 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
10451 begin
10452 -- A private type without a full view is not fully defined
10453
10454 if Is_Private_Type (Typ)
10455 and then No (Full_View (Typ))
10456 then
10457 return False;
10458
10459 -- An incomplete type is never fully defined
10460
10461 elsif Is_Incomplete_Type (Typ) then
10462 return False;
10463
10464 -- All other types are fully defined
10465
10466 else
10467 return True;
10468 end if;
10469 end Is_Fully_Defined_Type;
10470
10471 -- Local declarations
10472
10473 Param : Entity_Id;
10474
10475 -- Start of processing for Has_Fully_Defined_Profile
10476
10477 begin
10478 -- Check the parameters
10479
10480 Param := First_Formal (Subp);
10481 while Present (Param) loop
10482 if not Is_Fully_Defined_Type (Etype (Param)) then
10483 return False;
10484 end if;
10485
10486 Next_Formal (Param);
10487 end loop;
10488
10489 -- Check the return type
10490
10491 return Is_Fully_Defined_Type (Etype (Subp));
10492 end Has_Fully_Defined_Profile;
10493
10494 ----------
10495 -- Hash --
10496 ----------
10497
10498 function Hash (F : Entity_Id) return HTable_Range is
10499 begin
10500 return HTable_Range (F mod HTable_Size);
10501 end Hash;
10502
10503 ------------------------
10504 -- Hide_Current_Scope --
10505 ------------------------
10506
10507 procedure Hide_Current_Scope is
10508 C : constant Entity_Id := Current_Scope;
10509 E : Entity_Id;
10510
10511 begin
10512 Set_Is_Hidden_Open_Scope (C);
10513
10514 E := First_Entity (C);
10515 while Present (E) loop
10516 if Is_Immediately_Visible (E) then
10517 Set_Is_Immediately_Visible (E, False);
10518 Append_Elmt (E, Hidden_Entities);
10519 end if;
10520
10521 Next_Entity (E);
10522 end loop;
10523
10524 -- Make the scope name invisible as well. This is necessary, but might
10525 -- conflict with calls to Rtsfind later on, in case the scope is a
10526 -- predefined one. There is no clean solution to this problem, so for
10527 -- now we depend on the user not redefining Standard itself in one of
10528 -- the parent units.
10529
10530 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
10531 Set_Is_Immediately_Visible (C, False);
10532 Append_Elmt (C, Hidden_Entities);
10533 end if;
10534
10535 end Hide_Current_Scope;
10536
10537 --------------
10538 -- Init_Env --
10539 --------------
10540
10541 procedure Init_Env is
10542 Saved : Instance_Env;
10543
10544 begin
10545 Saved.Instantiated_Parent := Current_Instantiated_Parent;
10546 Saved.Exchanged_Views := Exchanged_Views;
10547 Saved.Hidden_Entities := Hidden_Entities;
10548 Saved.Current_Sem_Unit := Current_Sem_Unit;
10549 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
10550 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
10551
10552 -- Save configuration switches. These may be reset if the unit is a
10553 -- predefined unit, and the current mode is not Ada 2005.
10554
10555 Saved.Switches := Save_Config_Switches;
10556
10557 Instance_Envs.Append (Saved);
10558
10559 Exchanged_Views := New_Elmt_List;
10560 Hidden_Entities := New_Elmt_List;
10561
10562 -- Make dummy entry for Instantiated parent. If generic unit is legal,
10563 -- this is set properly in Set_Instance_Env.
10564
10565 Current_Instantiated_Parent :=
10566 (Current_Scope, Current_Scope, Assoc_Null);
10567 end Init_Env;
10568
10569 ---------------------
10570 -- In_Main_Context --
10571 ---------------------
10572
10573 function In_Main_Context (E : Entity_Id) return Boolean is
10574 Context : List_Id;
10575 Clause : Node_Id;
10576 Nam : Node_Id;
10577
10578 begin
10579 if not Is_Compilation_Unit (E)
10580 or else Ekind (E) /= E_Package
10581 or else In_Private_Part (E)
10582 then
10583 return False;
10584 end if;
10585
10586 Context := Context_Items (Cunit (Main_Unit));
10587
10588 Clause := First (Context);
10589 while Present (Clause) loop
10590 if Nkind (Clause) = N_With_Clause then
10591 Nam := Name (Clause);
10592
10593 -- If the current scope is part of the context of the main unit,
10594 -- analysis of the corresponding with_clause is not complete, and
10595 -- the entity is not set. We use the Chars field directly, which
10596 -- might produce false positives in rare cases, but guarantees
10597 -- that we produce all the instance bodies we will need.
10598
10599 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
10600 or else (Nkind (Nam) = N_Selected_Component
10601 and then Chars (Selector_Name (Nam)) = Chars (E))
10602 then
10603 return True;
10604 end if;
10605 end if;
10606
10607 Next (Clause);
10608 end loop;
10609
10610 return False;
10611 end In_Main_Context;
10612
10613 ---------------------
10614 -- Inherit_Context --
10615 ---------------------
10616
10617 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
10618 Current_Context : List_Id;
10619 Current_Unit : Node_Id;
10620 Item : Node_Id;
10621 New_I : Node_Id;
10622
10623 Clause : Node_Id;
10624 OK : Boolean;
10625 Lib_Unit : Node_Id;
10626
10627 begin
10628 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
10629
10630 -- The inherited context is attached to the enclosing compilation
10631 -- unit. This is either the main unit, or the declaration for the
10632 -- main unit (in case the instantiation appears within the package
10633 -- declaration and the main unit is its body).
10634
10635 Current_Unit := Parent (Inst);
10636 while Present (Current_Unit)
10637 and then Nkind (Current_Unit) /= N_Compilation_Unit
10638 loop
10639 Current_Unit := Parent (Current_Unit);
10640 end loop;
10641
10642 Current_Context := Context_Items (Current_Unit);
10643
10644 Item := First (Context_Items (Parent (Gen_Decl)));
10645 while Present (Item) loop
10646 if Nkind (Item) = N_With_Clause then
10647 Lib_Unit := Library_Unit (Item);
10648
10649 -- Take care to prevent direct cyclic with's
10650
10651 if Lib_Unit /= Current_Unit then
10652
10653 -- Do not add a unit if it is already in the context
10654
10655 Clause := First (Current_Context);
10656 OK := True;
10657 while Present (Clause) loop
10658 if Nkind (Clause) = N_With_Clause
10659 and then Library_Unit (Clause) = Lib_Unit
10660 then
10661 OK := False;
10662 exit;
10663 end if;
10664
10665 Next (Clause);
10666 end loop;
10667
10668 if OK then
10669 New_I := New_Copy (Item);
10670 Set_Implicit_With (New_I);
10671
10672 Append (New_I, Current_Context);
10673 end if;
10674 end if;
10675 end if;
10676
10677 Next (Item);
10678 end loop;
10679 end if;
10680 end Inherit_Context;
10681
10682 ----------------
10683 -- Initialize --
10684 ----------------
10685
10686 procedure Initialize is
10687 begin
10688 Generic_Renamings.Init;
10689 Instance_Envs.Init;
10690 Generic_Flags.Init;
10691 Generic_Renamings_HTable.Reset;
10692 Circularity_Detected := False;
10693 Exchanged_Views := No_Elist;
10694 Hidden_Entities := No_Elist;
10695 end Initialize;
10696
10697 -------------------------------------
10698 -- Insert_Freeze_Node_For_Instance --
10699 -------------------------------------
10700
10701 procedure Insert_Freeze_Node_For_Instance
10702 (N : Node_Id;
10703 F_Node : Node_Id)
10704 is
10705 function Enclosing_Body (N : Node_Id) return Node_Id;
10706 -- Find enclosing package or subprogram body, if any. Freeze node may
10707 -- be placed at end of current declarative list if previous instance
10708 -- and current one have different enclosing bodies.
10709
10710 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
10711 -- Find the local instance, if any, that declares the generic that is
10712 -- being instantiated. If present, the freeze node for this instance
10713 -- must follow the freeze node for the previous instance.
10714
10715 --------------------
10716 -- Enclosing_Body --
10717 --------------------
10718
10719 function Enclosing_Body (N : Node_Id) return Node_Id is
10720 P : Node_Id;
10721
10722 begin
10723 P := Parent (N);
10724 while Present (P)
10725 and then Nkind (Parent (P)) /= N_Compilation_Unit
10726 loop
10727 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
10728 if Nkind (Parent (P)) = N_Subunit then
10729 return Corresponding_Stub (Parent (P));
10730 else
10731 return P;
10732 end if;
10733 end if;
10734
10735 P := True_Parent (P);
10736 end loop;
10737
10738 return Empty;
10739 end Enclosing_Body;
10740
10741 -----------------------
10742 -- Previous_Instance --
10743 -----------------------
10744
10745 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
10746 S : Entity_Id;
10747
10748 begin
10749 S := Scope (Gen);
10750 while Present (S) and then S /= Standard_Standard loop
10751 if Is_Generic_Instance (S)
10752 and then In_Same_Source_Unit (S, N)
10753 then
10754 return S;
10755 end if;
10756
10757 S := Scope (S);
10758 end loop;
10759
10760 return Empty;
10761 end Previous_Instance;
10762
10763 -- Local variables
10764
10765 Decl : Node_Id;
10766 Decls : List_Id;
10767 Inst : Entity_Id;
10768 Origin : Entity_Id;
10769 Par_Inst : Node_Id;
10770 Par_N : Node_Id;
10771
10772 -- Start of processing for Insert_Freeze_Node_For_Instance
10773
10774 begin
10775 -- Nothing to do if the freeze node has already been inserted
10776
10777 if Is_List_Member (F_Node) then
10778 return;
10779 end if;
10780
10781 Inst := Entity (F_Node);
10782
10783 -- When processing a subprogram instantiation, utilize the actual
10784 -- subprogram instantiation rather than its package wrapper as it
10785 -- carries all the context information.
10786
10787 if Is_Wrapper_Package (Inst) then
10788 Inst := Related_Instance (Inst);
10789 end if;
10790
10791 Par_Inst := Parent (Inst);
10792
10793 -- If this is a package instance, check whether the generic is declared
10794 -- in a previous instance and the current instance is not within the
10795 -- previous one.
10796
10797 if Present (Generic_Parent (Par_Inst)) and then Is_In_Main_Unit (N) then
10798 declare
10799 Enclosing_N : constant Node_Id := Enclosing_Body (N);
10800 Par_I : constant Entity_Id :=
10801 Previous_Instance (Generic_Parent (Par_Inst));
10802 Scop : Entity_Id;
10803
10804 begin
10805 if Present (Par_I) and then Earlier (N, Freeze_Node (Par_I)) then
10806 Scop := Scope (Inst);
10807
10808 -- If the current instance is within the one that contains
10809 -- the generic, the freeze node for the current one must
10810 -- appear in the current declarative part. Ditto, if the
10811 -- current instance is within another package instance or
10812 -- within a body that does not enclose the current instance.
10813 -- In these three cases the freeze node of the previous
10814 -- instance is not relevant.
10815
10816 while Present (Scop) and then Scop /= Standard_Standard loop
10817 exit when Scop = Par_I
10818 or else
10819 (Is_Generic_Instance (Scop)
10820 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
10821 Scop := Scope (Scop);
10822 end loop;
10823
10824 -- Previous instance encloses current instance
10825
10826 if Scop = Par_I then
10827 null;
10828
10829 -- If the next node is a source body we must freeze in the
10830 -- current scope as well.
10831
10832 elsif Present (Next (N))
10833 and then Nkind (Next (N)) in N_Subprogram_Body
10834 | N_Package_Body
10835 and then Comes_From_Source (Next (N))
10836 then
10837 null;
10838
10839 -- Current instance is within an unrelated instance
10840
10841 elsif Is_Generic_Instance (Scop) then
10842 null;
10843
10844 -- Current instance is within an unrelated body
10845
10846 elsif Present (Enclosing_N)
10847 and then Enclosing_N /= Enclosing_Body (Par_I)
10848 then
10849 null;
10850
10851 else
10852 Insert_After (Freeze_Node (Par_I), F_Node);
10853 return;
10854 end if;
10855 end if;
10856 end;
10857 end if;
10858
10859 Decl := N;
10860 Decls := List_Containing (N);
10861 Par_N := Parent (Decls);
10862 Origin := Empty;
10863
10864 -- Determine the proper freeze point of an instantiation
10865
10866 if Is_Generic_Instance (Inst) then
10867 loop
10868 -- When the instantiation occurs in a package spec, append the
10869 -- freeze node to the private declarations (if any).
10870
10871 if Nkind (Par_N) = N_Package_Specification
10872 and then Decls = Visible_Declarations (Par_N)
10873 and then not Is_Empty_List (Private_Declarations (Par_N))
10874 then
10875 Decls := Private_Declarations (Par_N);
10876 Decl := First (Decls);
10877 end if;
10878
10879 -- We adhere to the general rule of a package or subprogram body
10880 -- causing freezing of anything before it in the same declarative
10881 -- region. In this respect, the proper freeze point of a package
10882 -- instantiation is before the first source body which follows, or
10883 -- before a stub. This ensures that entities from the instance are
10884 -- already frozen and therefore usable in source bodies.
10885
10886 if Nkind (Par_N) /= N_Package_Declaration
10887 and then
10888 not In_Same_Source_Unit (Generic_Parent (Par_Inst), Inst)
10889 then
10890 while Present (Decl) loop
10891 if ((Nkind (Decl) in N_Unit_Body
10892 or else
10893 Nkind (Decl) in N_Body_Stub)
10894 and then Comes_From_Source (Decl))
10895 or else (Present (Origin)
10896 and then Nkind (Decl) in N_Generic_Instantiation
10897 and then Instance_Spec (Decl) /= Origin)
10898 then
10899 Set_Sloc (F_Node, Sloc (Decl));
10900 Insert_Before (Decl, F_Node);
10901 return;
10902 end if;
10903
10904 Next (Decl);
10905 end loop;
10906 end if;
10907
10908 -- When the instantiation occurs in a package spec and there is
10909 -- no source body which follows, and the package has a body but
10910 -- is delayed, then insert immediately before its freeze node.
10911
10912 if Nkind (Par_N) = N_Package_Specification
10913 and then Present (Corresponding_Body (Parent (Par_N)))
10914 and then Present (Freeze_Node (Defining_Entity (Par_N)))
10915 then
10916 Set_Sloc (F_Node, Sloc (Freeze_Node (Defining_Entity (Par_N))));
10917 Insert_Before (Freeze_Node (Defining_Entity (Par_N)), F_Node);
10918 return;
10919
10920 -- When the instantiation occurs in a package spec and there is
10921 -- no source body which follows, not even of the package itself,
10922 -- then insert into the declaration list of the outer level, but
10923 -- do not jump over following instantiations in this list because
10924 -- they may have a body that has not materialized yet, see above.
10925
10926 elsif Nkind (Par_N) = N_Package_Specification
10927 and then No (Corresponding_Body (Parent (Par_N)))
10928 and then Is_List_Member (Parent (Par_N))
10929 then
10930 Decl := Parent (Par_N);
10931 Decls := List_Containing (Decl);
10932 Par_N := Parent (Decls);
10933 Origin := Decl;
10934
10935 -- In a package declaration, or if no source body which follows
10936 -- and at library level, then insert at end of list.
10937
10938 else
10939 exit;
10940 end if;
10941 end loop;
10942 end if;
10943
10944 -- Insert and adjust the Sloc of the freeze node
10945
10946 Set_Sloc (F_Node, Sloc (Last (Decls)));
10947 Insert_After (Last (Decls), F_Node);
10948 end Insert_Freeze_Node_For_Instance;
10949
10950 -----------------------------
10951 -- Install_Formal_Packages --
10952 -----------------------------
10953
10954 procedure Install_Formal_Packages (Par : Entity_Id) is
10955 E : Entity_Id;
10956 Gen : Entity_Id;
10957 Gen_E : Entity_Id := Empty;
10958
10959 begin
10960 E := First_Entity (Par);
10961
10962 -- If we are installing an instance parent, locate the formal packages
10963 -- of its generic parent.
10964
10965 if Is_Generic_Instance (Par) then
10966 Gen := Generic_Parent (Package_Specification (Par));
10967 Gen_E := First_Entity (Gen);
10968 end if;
10969
10970 while Present (E) loop
10971 if Ekind (E) = E_Package
10972 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10973 then
10974 -- If this is the renaming for the parent instance, done
10975
10976 if Renamed_Entity (E) = Par then
10977 exit;
10978
10979 -- The visibility of a formal of an enclosing generic is already
10980 -- correct.
10981
10982 elsif Denotes_Formal_Package (E) then
10983 null;
10984
10985 elsif Present (Associated_Formal_Package (E)) then
10986 Check_Generic_Actuals (Renamed_Entity (E), True);
10987 Set_Is_Hidden (E, False);
10988
10989 -- Find formal package in generic unit that corresponds to
10990 -- (instance of) formal package in instance.
10991
10992 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10993 Next_Entity (Gen_E);
10994 end loop;
10995
10996 if Present (Gen_E) then
10997 Map_Formal_Package_Entities (Gen_E, E);
10998 end if;
10999 end if;
11000 end if;
11001
11002 Next_Entity (E);
11003
11004 if Present (Gen_E) then
11005 Next_Entity (Gen_E);
11006 end if;
11007 end loop;
11008 end Install_Formal_Packages;
11009
11010 --------------------
11011 -- Install_Parent --
11012 --------------------
11013
11014 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
11015 Ancestors : constant Elist_Id := New_Elmt_List;
11016 S : constant Entity_Id := Current_Scope;
11017 Inst_Par : Entity_Id;
11018 First_Par : Entity_Id;
11019 Inst_Node : Node_Id;
11020 Gen_Par : Entity_Id;
11021 First_Gen : Entity_Id;
11022 Elmt : Elmt_Id;
11023
11024 procedure Install_Noninstance_Specs (Par : Entity_Id);
11025 -- Install the scopes of noninstance parent units ending with Par
11026
11027 procedure Install_Spec (Par : Entity_Id);
11028 -- The child unit is within the declarative part of the parent, so the
11029 -- declarations within the parent are immediately visible.
11030
11031 -------------------------------
11032 -- Install_Noninstance_Specs --
11033 -------------------------------
11034
11035 procedure Install_Noninstance_Specs (Par : Entity_Id) is
11036 begin
11037 if Present (Par)
11038 and then Par /= Standard_Standard
11039 and then not In_Open_Scopes (Par)
11040 then
11041 Install_Noninstance_Specs (Scope (Par));
11042 Install_Spec (Par);
11043 end if;
11044 end Install_Noninstance_Specs;
11045
11046 ------------------
11047 -- Install_Spec --
11048 ------------------
11049
11050 procedure Install_Spec (Par : Entity_Id) is
11051 Spec : constant Node_Id := Package_Specification (Par);
11052
11053 begin
11054 -- If this parent of the child instance is a top-level unit,
11055 -- then record the unit and its visibility for later resetting in
11056 -- Remove_Parent. We exclude units that are generic instances, as we
11057 -- only want to record this information for the ultimate top-level
11058 -- noninstance parent (is that always correct???).
11059
11060 if Scope (Par) = Standard_Standard
11061 and then not Is_Generic_Instance (Par)
11062 then
11063 Parent_Unit_Visible := Is_Immediately_Visible (Par);
11064 Instance_Parent_Unit := Par;
11065 end if;
11066
11067 -- Open the parent scope and make it and its declarations visible.
11068 -- If this point is not within a body, then only the visible
11069 -- declarations should be made visible, and installation of the
11070 -- private declarations is deferred until the appropriate point
11071 -- within analysis of the spec being instantiated (see the handling
11072 -- of parent visibility in Analyze_Package_Specification). This is
11073 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
11074 -- private view problems that occur when compiling instantiations of
11075 -- a generic child of that package (Generic_Dispatching_Constructor).
11076 -- If the instance freezes a tagged type, inlinings of operations
11077 -- from Ada.Tags may need the full view of type Tag. If inlining took
11078 -- proper account of establishing visibility of inlined subprograms'
11079 -- parents then it should be possible to remove this
11080 -- special check. ???
11081
11082 Push_Scope (Par);
11083 Set_Is_Immediately_Visible (Par);
11084 Install_Visible_Declarations (Par);
11085 Set_Use (Visible_Declarations (Spec));
11086
11087 if In_Body or else Is_RTU (Par, Ada_Tags) then
11088 Install_Private_Declarations (Par);
11089 Set_Use (Private_Declarations (Spec));
11090 end if;
11091 end Install_Spec;
11092
11093 -- Start of processing for Install_Parent
11094
11095 begin
11096 -- We need to install the parent instance to compile the instantiation
11097 -- of the child, but the child instance must appear in the current
11098 -- scope. Given that we cannot place the parent above the current scope
11099 -- in the scope stack, we duplicate the current scope and unstack both
11100 -- after the instantiation is complete.
11101
11102 -- If the parent is itself the instantiation of a child unit, we must
11103 -- also stack the instantiation of its parent, and so on. Each such
11104 -- ancestor is the prefix of the name in a prior instantiation.
11105
11106 -- If this is a nested instance, the parent unit itself resolves to
11107 -- a renaming of the parent instance, whose declaration we need.
11108
11109 -- Finally, the parent may be a generic (not an instance) when the
11110 -- child unit appears as a formal package.
11111
11112 Inst_Par := P;
11113
11114 if Present (Renamed_Entity (Inst_Par)) then
11115 Inst_Par := Renamed_Entity (Inst_Par);
11116 end if;
11117
11118 First_Par := Inst_Par;
11119
11120 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
11121
11122 First_Gen := Gen_Par;
11123
11124 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
11125
11126 -- Load grandparent instance as well
11127
11128 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
11129
11130 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
11131 Inst_Par := Entity (Prefix (Name (Inst_Node)));
11132
11133 if Present (Renamed_Entity (Inst_Par)) then
11134 Inst_Par := Renamed_Entity (Inst_Par);
11135 end if;
11136
11137 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
11138
11139 if Present (Gen_Par) then
11140 Prepend_Elmt (Inst_Par, Ancestors);
11141
11142 else
11143 -- Parent is not the name of an instantiation
11144
11145 Install_Noninstance_Specs (Inst_Par);
11146 exit;
11147 end if;
11148
11149 else
11150 -- Previous error
11151
11152 exit;
11153 end if;
11154 end loop;
11155
11156 if Present (First_Gen) then
11157 Append_Elmt (First_Par, Ancestors);
11158 else
11159 Install_Noninstance_Specs (First_Par);
11160 end if;
11161
11162 if not Is_Empty_Elmt_List (Ancestors) then
11163 Elmt := First_Elmt (Ancestors);
11164 while Present (Elmt) loop
11165 Install_Spec (Node (Elmt));
11166 Install_Formal_Packages (Node (Elmt));
11167 Next_Elmt (Elmt);
11168 end loop;
11169 end if;
11170
11171 if not In_Body then
11172 Push_Scope (S);
11173 end if;
11174 end Install_Parent;
11175
11176 -------------------------------
11177 -- Install_Hidden_Primitives --
11178 -------------------------------
11179
11180 procedure Install_Hidden_Primitives
11181 (Prims_List : in out Elist_Id;
11182 Gen_T : Entity_Id;
11183 Act_T : Entity_Id)
11184 is
11185 Elmt : Elmt_Id;
11186 List : Elist_Id := No_Elist;
11187 Prim_G_Elmt : Elmt_Id;
11188 Prim_A_Elmt : Elmt_Id;
11189 Prim_G : Node_Id;
11190 Prim_A : Node_Id;
11191
11192 begin
11193 -- No action needed in case of serious errors because we cannot trust
11194 -- in the order of primitives
11195
11196 if Serious_Errors_Detected > 0 then
11197 return;
11198
11199 -- No action possible if we don't have available the list of primitive
11200 -- operations
11201
11202 elsif No (Gen_T)
11203 or else not Is_Record_Type (Gen_T)
11204 or else not Is_Tagged_Type (Gen_T)
11205 or else not Is_Record_Type (Act_T)
11206 or else not Is_Tagged_Type (Act_T)
11207 then
11208 return;
11209
11210 -- There is no need to handle interface types since their primitives
11211 -- cannot be hidden
11212
11213 elsif Is_Interface (Gen_T) then
11214 return;
11215 end if;
11216
11217 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
11218
11219 if not Is_Class_Wide_Type (Act_T) then
11220 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
11221 else
11222 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
11223 end if;
11224
11225 loop
11226 -- Skip predefined primitives in the generic formal
11227
11228 while Present (Prim_G_Elmt)
11229 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
11230 loop
11231 Next_Elmt (Prim_G_Elmt);
11232 end loop;
11233
11234 -- Skip predefined primitives in the generic actual
11235
11236 while Present (Prim_A_Elmt)
11237 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
11238 loop
11239 Next_Elmt (Prim_A_Elmt);
11240 end loop;
11241
11242 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
11243
11244 Prim_G := Node (Prim_G_Elmt);
11245 Prim_A := Node (Prim_A_Elmt);
11246
11247 -- There is no need to handle interface primitives because their
11248 -- primitives are not hidden
11249
11250 exit when Present (Interface_Alias (Prim_G));
11251
11252 -- Here we install one hidden primitive
11253
11254 if Chars (Prim_G) /= Chars (Prim_A)
11255 and then Has_Suffix (Prim_A, 'P')
11256 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
11257 then
11258 Set_Chars (Prim_A, Chars (Prim_G));
11259 Append_New_Elmt (Prim_A, To => List);
11260 end if;
11261
11262 Next_Elmt (Prim_A_Elmt);
11263 Next_Elmt (Prim_G_Elmt);
11264 end loop;
11265
11266 -- Append the elements to the list of temporarily visible primitives
11267 -- avoiding duplicates.
11268
11269 if Present (List) then
11270 if No (Prims_List) then
11271 Prims_List := New_Elmt_List;
11272 end if;
11273
11274 Elmt := First_Elmt (List);
11275 while Present (Elmt) loop
11276 Append_Unique_Elmt (Node (Elmt), Prims_List);
11277 Next_Elmt (Elmt);
11278 end loop;
11279 end if;
11280 end Install_Hidden_Primitives;
11281
11282 ---------------------------------
11283 -- Renames_Standard_Subprogram --
11284 ---------------------------------
11285
11286 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
11287 Id : Entity_Id;
11288
11289 begin
11290 Id := Alias (Subp);
11291 while Present (Id) loop
11292 if Scope (Id) = Standard_Standard then
11293 return True;
11294 end if;
11295
11296 Id := Alias (Id);
11297 end loop;
11298
11299 return False;
11300 end Renames_Standard_Subprogram;
11301
11302 -------------------------------
11303 -- Restore_Hidden_Primitives --
11304 -------------------------------
11305
11306 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
11307 Prim_Elmt : Elmt_Id;
11308 Prim : Node_Id;
11309
11310 begin
11311 if Present (Prims_List) then
11312 Prim_Elmt := First_Elmt (Prims_List);
11313 while Present (Prim_Elmt) loop
11314 Prim := Node (Prim_Elmt);
11315 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
11316 Next_Elmt (Prim_Elmt);
11317 end loop;
11318
11319 Prims_List := No_Elist;
11320 end if;
11321 end Restore_Hidden_Primitives;
11322
11323 --------------------------------
11324 -- Instantiate_Formal_Package --
11325 --------------------------------
11326
11327 function Instantiate_Formal_Package
11328 (Formal : Node_Id;
11329 Actual : Node_Id;
11330 Analyzed_Formal : Node_Id) return List_Id
11331 is
11332 Loc : constant Source_Ptr := Sloc (Actual);
11333 Hidden_Formals : constant Elist_Id := New_Elmt_List;
11334
11335 Actual_Pack : Entity_Id;
11336 Formal_Pack : Entity_Id;
11337 Gen_Parent : Entity_Id;
11338 Decls : List_Id;
11339 Nod : Node_Id;
11340 Parent_Spec : Node_Id;
11341
11342 procedure Find_Matching_Actual
11343 (F : Node_Id;
11344 Act : in out Entity_Id);
11345 -- We need to associate each formal entity in the formal package with
11346 -- the corresponding entity in the actual package. The actual package
11347 -- has been analyzed and possibly expanded, and as a result there is
11348 -- no one-to-one correspondence between the two lists (for example,
11349 -- the actual may include subtypes, itypes, and inherited primitive
11350 -- operations, interspersed among the renaming declarations for the
11351 -- actuals). We retrieve the corresponding actual by name because each
11352 -- actual has the same name as the formal, and they do appear in the
11353 -- same order.
11354
11355 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
11356 -- Retrieve entity of defining entity of generic formal parameter.
11357 -- Only the declarations of formals need to be considered when
11358 -- linking them to actuals, but the declarative list may include
11359 -- internal entities generated during analysis, and those are ignored.
11360
11361 procedure Match_Formal_Entity
11362 (Formal_Node : Node_Id;
11363 Formal_Ent : Entity_Id;
11364 Actual_Ent : Entity_Id);
11365 -- Associates the formal entity with the actual. In the case where
11366 -- Formal_Ent is a formal package, this procedure iterates through all
11367 -- of its formals and enters associations between the actuals occurring
11368 -- in the formal package's corresponding actual package (given by
11369 -- Actual_Ent) and the formal package's formal parameters. This
11370 -- procedure recurses if any of the parameters is itself a package.
11371
11372 function Is_Instance_Of
11373 (Act_Spec : Entity_Id;
11374 Gen_Anc : Entity_Id) return Boolean;
11375 -- The actual can be an instantiation of a generic within another
11376 -- instance, in which case there is no direct link from it to the
11377 -- original generic ancestor. In that case, we recognize that the
11378 -- ultimate ancestor is the same by examining names and scopes.
11379
11380 procedure Process_Nested_Formal (Formal : Entity_Id);
11381 -- If the current formal is declared with a box, its own formals are
11382 -- visible in the instance, as they were in the generic, and their
11383 -- Hidden flag must be reset. If some of these formals are themselves
11384 -- packages declared with a box, the processing must be recursive.
11385
11386 --------------------------
11387 -- Find_Matching_Actual --
11388 --------------------------
11389
11390 procedure Find_Matching_Actual
11391 (F : Node_Id;
11392 Act : in out Entity_Id)
11393 is
11394 Formal_Ent : Entity_Id;
11395
11396 begin
11397 case Nkind (Original_Node (F)) is
11398 when N_Formal_Object_Declaration
11399 | N_Formal_Type_Declaration
11400 =>
11401 Formal_Ent := Defining_Identifier (F);
11402
11403 while Present (Act)
11404 and then Chars (Act) /= Chars (Formal_Ent)
11405 loop
11406 Next_Entity (Act);
11407 end loop;
11408
11409 when N_Formal_Package_Declaration
11410 | N_Formal_Subprogram_Declaration
11411 | N_Generic_Package_Declaration
11412 | N_Package_Declaration
11413 =>
11414 Formal_Ent := Defining_Entity (F);
11415
11416 while Present (Act)
11417 and then Chars (Act) /= Chars (Formal_Ent)
11418 loop
11419 Next_Entity (Act);
11420 end loop;
11421
11422 when others =>
11423 raise Program_Error;
11424 end case;
11425 end Find_Matching_Actual;
11426
11427 -------------------------
11428 -- Match_Formal_Entity --
11429 -------------------------
11430
11431 procedure Match_Formal_Entity
11432 (Formal_Node : Node_Id;
11433 Formal_Ent : Entity_Id;
11434 Actual_Ent : Entity_Id)
11435 is
11436 Act_Pkg : Entity_Id;
11437
11438 begin
11439 Set_Instance_Of (Formal_Ent, Actual_Ent);
11440
11441 if Ekind (Actual_Ent) = E_Package then
11442
11443 -- Record associations for each parameter
11444
11445 Act_Pkg := Actual_Ent;
11446
11447 declare
11448 A_Ent : Entity_Id := First_Entity (Act_Pkg);
11449 F_Ent : Entity_Id;
11450 F_Node : Node_Id;
11451
11452 Gen_Decl : Node_Id;
11453 Formals : List_Id;
11454 Actual : Entity_Id;
11455
11456 begin
11457 -- Retrieve the actual given in the formal package declaration
11458
11459 Actual := Entity (Name (Original_Node (Formal_Node)));
11460
11461 -- The actual in the formal package declaration may be a
11462 -- renamed generic package, in which case we want to retrieve
11463 -- the original generic in order to traverse its formal part.
11464
11465 if Present (Renamed_Entity (Actual)) then
11466 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
11467 else
11468 Gen_Decl := Unit_Declaration_Node (Actual);
11469 end if;
11470
11471 Formals := Generic_Formal_Declarations (Gen_Decl);
11472
11473 if Present (Formals) then
11474 F_Node := First_Non_Pragma (Formals);
11475 else
11476 F_Node := Empty;
11477 end if;
11478
11479 while Present (A_Ent)
11480 and then Present (F_Node)
11481 and then A_Ent /= First_Private_Entity (Act_Pkg)
11482 loop
11483 F_Ent := Get_Formal_Entity (F_Node);
11484
11485 if Present (F_Ent) then
11486
11487 -- This is a formal of the original package. Record
11488 -- association and recurse.
11489
11490 Find_Matching_Actual (F_Node, A_Ent);
11491 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
11492 Next_Entity (A_Ent);
11493 end if;
11494
11495 Next_Non_Pragma (F_Node);
11496 end loop;
11497 end;
11498 end if;
11499 end Match_Formal_Entity;
11500
11501 -----------------------
11502 -- Get_Formal_Entity --
11503 -----------------------
11504
11505 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
11506 Kind : constant Node_Kind := Nkind (Original_Node (N));
11507 begin
11508 case Kind is
11509 when N_Formal_Object_Declaration =>
11510 return Defining_Identifier (N);
11511
11512 when N_Formal_Type_Declaration =>
11513 return Defining_Identifier (N);
11514
11515 when N_Formal_Subprogram_Declaration =>
11516 return Defining_Unit_Name (Specification (N));
11517
11518 when N_Formal_Package_Declaration =>
11519 return Defining_Identifier (Original_Node (N));
11520
11521 when N_Generic_Package_Declaration =>
11522 return Defining_Identifier (Original_Node (N));
11523
11524 -- All other declarations are introduced by semantic analysis and
11525 -- have no match in the actual.
11526
11527 when others =>
11528 return Empty;
11529 end case;
11530 end Get_Formal_Entity;
11531
11532 --------------------
11533 -- Is_Instance_Of --
11534 --------------------
11535
11536 function Is_Instance_Of
11537 (Act_Spec : Entity_Id;
11538 Gen_Anc : Entity_Id) return Boolean
11539 is
11540 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
11541
11542 begin
11543 if No (Gen_Par) then
11544 return False;
11545
11546 -- Simplest case: the generic parent of the actual is the formal
11547
11548 elsif Gen_Par = Gen_Anc then
11549 return True;
11550
11551 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
11552 return False;
11553
11554 -- The actual may be obtained through several instantiations. Its
11555 -- scope must itself be an instance of a generic declared in the
11556 -- same scope as the formal. Any other case is detected above.
11557
11558 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
11559 return False;
11560
11561 else
11562 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
11563 end if;
11564 end Is_Instance_Of;
11565
11566 ---------------------------
11567 -- Process_Nested_Formal --
11568 ---------------------------
11569
11570 procedure Process_Nested_Formal (Formal : Entity_Id) is
11571 Ent : Entity_Id;
11572
11573 begin
11574 if Present (Associated_Formal_Package (Formal))
11575 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
11576 then
11577 Ent := First_Entity (Formal);
11578 while Present (Ent) loop
11579 Set_Is_Hidden (Ent, False);
11580 Set_Is_Visible_Formal (Ent);
11581 Set_Is_Potentially_Use_Visible
11582 (Ent, Is_Potentially_Use_Visible (Formal));
11583
11584 if Ekind (Ent) = E_Package then
11585 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
11586 Process_Nested_Formal (Ent);
11587 end if;
11588
11589 Next_Entity (Ent);
11590 end loop;
11591 end if;
11592 end Process_Nested_Formal;
11593
11594 -- Start of processing for Instantiate_Formal_Package
11595
11596 begin
11597 Analyze (Actual);
11598
11599 -- The actual must be a package instance, or else a current instance
11600 -- such as a parent generic within the body of a generic child.
11601
11602 if not Is_Entity_Name (Actual)
11603 or else not Is_Package_Or_Generic_Package (Entity (Actual))
11604 then
11605 Error_Msg_N
11606 ("expect package instance to instantiate formal", Actual);
11607 Abandon_Instantiation (Actual);
11608
11609 else
11610 Actual_Pack := Entity (Actual);
11611 Set_Is_Instantiated (Actual_Pack);
11612
11613 -- The actual may be a renamed package, or an outer generic formal
11614 -- package whose instantiation is converted into a renaming.
11615
11616 if Present (Renamed_Entity (Actual_Pack)) then
11617 Actual_Pack := Renamed_Entity (Actual_Pack);
11618 end if;
11619
11620 -- The analyzed formal is expected to be the result of the rewriting
11621 -- of the formal package into a regular package by analysis.
11622
11623 pragma Assert (Nkind (Analyzed_Formal) = N_Package_Declaration
11624 and then Nkind (Original_Node (Analyzed_Formal)) =
11625 N_Formal_Package_Declaration);
11626
11627 Gen_Parent := Generic_Parent (Specification (Analyzed_Formal));
11628 Formal_Pack := Defining_Unit_Name (Specification (Analyzed_Formal));
11629
11630 -- The actual for a ghost generic formal package should be a ghost
11631 -- package (SPARK RM 6.9(14)).
11632
11633 Check_Ghost_Formal_Procedure_Or_Package
11634 (N => Actual,
11635 Actual => Actual_Pack,
11636 Formal => Formal_Pack);
11637
11638 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
11639 Parent_Spec := Package_Specification (Actual_Pack);
11640 else
11641 Parent_Spec := Parent (Actual_Pack);
11642 end if;
11643
11644 if Gen_Parent = Any_Id then
11645 Error_Msg_N
11646 ("previous error in declaration of formal package", Actual);
11647 Abandon_Instantiation (Actual);
11648
11649 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
11650 null;
11651
11652 -- If this is the current instance of an enclosing generic, that unit
11653 -- is the generic package we need.
11654
11655 elsif In_Open_Scopes (Actual_Pack)
11656 and then Ekind (Actual_Pack) = E_Generic_Package
11657 then
11658 null;
11659
11660 else
11661 Error_Msg_NE
11662 ("actual parameter must be instance of&", Actual, Gen_Parent);
11663 Abandon_Instantiation (Actual);
11664 end if;
11665
11666 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
11667 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
11668
11669 Nod :=
11670 Make_Package_Renaming_Declaration (Loc,
11671 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
11672 Name => New_Occurrence_Of (Actual_Pack, Loc));
11673
11674 Set_Associated_Formal_Package
11675 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
11676 Decls := New_List (Nod);
11677
11678 -- If the formal F has a box, then the generic declarations are
11679 -- visible in the generic G. In an instance of G, the corresponding
11680 -- entities in the actual for F (which are the actuals for the
11681 -- instantiation of the generic that F denotes) must also be made
11682 -- visible for analysis of the current instance. On exit from the
11683 -- current instance, those entities are made private again. If the
11684 -- actual is currently in use, these entities are also use-visible.
11685
11686 -- The loop through the actual entities also steps through the formal
11687 -- entities and enters associations from formals to actuals into the
11688 -- renaming map. This is necessary to properly handle checking of
11689 -- actual parameter associations for later formals that depend on
11690 -- actuals declared in the formal package.
11691
11692 -- In Ada 2005, partial parameterization requires that we make
11693 -- visible the actuals corresponding to formals that were defaulted
11694 -- in the formal package. There formals are identified because they
11695 -- remain formal generics within the formal package, rather than
11696 -- being renamings of the actuals supplied.
11697
11698 declare
11699 Gen_Decl : constant Node_Id :=
11700 Unit_Declaration_Node (Gen_Parent);
11701 Formals : constant List_Id :=
11702 Generic_Formal_Declarations (Gen_Decl);
11703
11704 Actual_Ent : Entity_Id;
11705 Actual_Of_Formal : Node_Id;
11706 Formal_Node : Node_Id;
11707 Formal_Ent : Entity_Id;
11708
11709 begin
11710 if Present (Formals) then
11711 Formal_Node := First_Non_Pragma (Formals);
11712 else
11713 Formal_Node := Empty;
11714 end if;
11715
11716 Actual_Ent := First_Entity (Actual_Pack);
11717 Actual_Of_Formal :=
11718 First (Visible_Declarations (Specification (Analyzed_Formal)));
11719 while Present (Actual_Ent)
11720 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
11721 loop
11722 if Present (Formal_Node) then
11723 Formal_Ent := Get_Formal_Entity (Formal_Node);
11724
11725 if Present (Formal_Ent) then
11726 Find_Matching_Actual (Formal_Node, Actual_Ent);
11727 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
11728
11729 -- We iterate at the same time over the actuals of the
11730 -- local package created for the formal, to determine
11731 -- which one of the formals of the original generic were
11732 -- defaulted in the formal. The corresponding actual
11733 -- entities are visible in the enclosing instance.
11734
11735 if Box_Present (Formal)
11736 or else
11737 (Present (Actual_Of_Formal)
11738 and then
11739 Is_Generic_Formal
11740 (Get_Formal_Entity (Actual_Of_Formal)))
11741 then
11742 Set_Is_Hidden (Actual_Ent, False);
11743 Set_Is_Visible_Formal (Actual_Ent);
11744 Set_Is_Potentially_Use_Visible
11745 (Actual_Ent, In_Use (Actual_Pack));
11746
11747 if Ekind (Actual_Ent) = E_Package then
11748 Process_Nested_Formal (Actual_Ent);
11749 end if;
11750
11751 else
11752 if not Is_Hidden (Actual_Ent) then
11753 Append_Elmt (Actual_Ent, Hidden_Formals);
11754 end if;
11755
11756 Set_Is_Hidden (Actual_Ent);
11757 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
11758 end if;
11759 end if;
11760
11761 Next_Non_Pragma (Formal_Node);
11762 Next (Actual_Of_Formal);
11763
11764 -- A formal subprogram may be overloaded, so advance in
11765 -- the list of actuals to make sure we do not match two
11766 -- successive formals to the same actual. This is only
11767 -- relevant for overloadable entities, others have
11768 -- distinct names.
11769
11770 if Is_Overloadable (Actual_Ent) then
11771 Next_Entity (Actual_Ent);
11772 end if;
11773
11774 else
11775 -- No further formals to match, but the generic part may
11776 -- contain inherited operation that are not hidden in the
11777 -- enclosing instance.
11778
11779 Next_Entity (Actual_Ent);
11780 end if;
11781 end loop;
11782
11783 -- Inherited subprograms generated by formal derived types are
11784 -- also visible if the types are.
11785
11786 Actual_Ent := First_Entity (Actual_Pack);
11787 while Present (Actual_Ent)
11788 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
11789 loop
11790 if Is_Overloadable (Actual_Ent)
11791 and then
11792 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
11793 and then
11794 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
11795 then
11796 Set_Is_Hidden (Actual_Ent, False);
11797 Set_Is_Potentially_Use_Visible
11798 (Actual_Ent, In_Use (Actual_Pack));
11799 end if;
11800
11801 Next_Entity (Actual_Ent);
11802 end loop;
11803 end;
11804
11805 -- If the formal requires conformance checking, reanalyze it as an
11806 -- abbreviated instantiation, to verify the matching rules of 12.7.
11807 -- The actual checks are performed after the generic associations
11808 -- have been analyzed, to guarantee the same visibility for this
11809 -- instantiation and for the actuals.
11810
11811 -- In Ada 2005, the generic associations for the formal can include
11812 -- defaulted parameters. These are ignored during check. This
11813 -- internal instantiation is removed from the tree after conformance
11814 -- checking, because it contains formal declarations for those
11815 -- defaulted parameters, and those should not reach the back-end.
11816
11817 if Requires_Conformance_Checking (Formal) then
11818 declare
11819 I_Pack : constant Entity_Id := Make_Temporary (Loc, 'P');
11820 I_Nam : Node_Id;
11821 begin
11822 Set_Is_Internal (I_Pack);
11823 Mutate_Ekind (I_Pack, E_Package);
11824
11825 -- Insert the package into the list of its hidden entities so
11826 -- that the list is not empty for Is_Abbreviated_Instance.
11827
11828 Append_Elmt (I_Pack, Hidden_Formals);
11829
11830 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
11831
11832 -- If the generic is a child unit, Check_Generic_Child_Unit
11833 -- needs its original name in case it is qualified.
11834
11835 if Is_Child_Unit (Gen_Parent) then
11836 I_Nam :=
11837 New_Copy_Tree (Name (Original_Node (Analyzed_Formal)));
11838 pragma Assert (Entity (I_Nam) = Gen_Parent
11839 or else Renamed_Entity (Entity (I_Nam)) = Gen_Parent);
11840
11841 else
11842 I_Nam :=
11843 New_Occurrence_Of (Get_Instance_Of (Gen_Parent), Loc);
11844 end if;
11845
11846 Append_To (Decls,
11847 Make_Package_Instantiation (Loc,
11848 Defining_Unit_Name => I_Pack,
11849 Name => I_Nam,
11850 Generic_Associations => Generic_Associations (Formal)));
11851 end;
11852 end if;
11853
11854 return Decls;
11855 end if;
11856 end Instantiate_Formal_Package;
11857
11858 -----------------------------------
11859 -- Instantiate_Formal_Subprogram --
11860 -----------------------------------
11861
11862 function Instantiate_Formal_Subprogram
11863 (Formal : Node_Id;
11864 Actual : Node_Id;
11865 Analyzed_Formal : Node_Id) return Node_Id
11866 is
11867 Analyzed_S : constant Entity_Id :=
11868 Defining_Unit_Name (Specification (Analyzed_Formal));
11869 Formal_Sub : constant Entity_Id :=
11870 Defining_Unit_Name (Specification (Formal));
11871
11872 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
11873 -- If the generic is a child unit, the parent has been installed on the
11874 -- scope stack, but a default subprogram cannot resolve to something
11875 -- on the parent because that parent is not really part of the visible
11876 -- context (it is there to resolve explicit local entities). If the
11877 -- default has resolved in this way, we remove the entity from immediate
11878 -- visibility and analyze the node again to emit an error message or
11879 -- find another visible candidate.
11880
11881 procedure Valid_Actual_Subprogram (Act : Node_Id);
11882 -- Perform legality check and raise exception on failure
11883
11884 -----------------------
11885 -- From_Parent_Scope --
11886 -----------------------
11887
11888 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
11889 Gen_Scope : Node_Id;
11890
11891 begin
11892 Gen_Scope := Scope (Analyzed_S);
11893 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
11894 if Scope (Subp) = Scope (Gen_Scope) then
11895 return True;
11896 end if;
11897
11898 Gen_Scope := Scope (Gen_Scope);
11899 end loop;
11900
11901 return False;
11902 end From_Parent_Scope;
11903
11904 -----------------------------
11905 -- Valid_Actual_Subprogram --
11906 -----------------------------
11907
11908 procedure Valid_Actual_Subprogram (Act : Node_Id) is
11909 Act_E : Entity_Id;
11910
11911 begin
11912 if Is_Entity_Name (Act) then
11913 Act_E := Entity (Act);
11914
11915 elsif Nkind (Act) = N_Selected_Component
11916 and then Is_Entity_Name (Selector_Name (Act))
11917 then
11918 Act_E := Entity (Selector_Name (Act));
11919
11920 else
11921 Act_E := Empty;
11922 end if;
11923
11924 -- The actual for a ghost generic formal procedure should be a ghost
11925 -- procedure (SPARK RM 6.9(14)).
11926
11927 if Present (Act_E)
11928 and then Ekind (Act_E) = E_Procedure
11929 then
11930 Check_Ghost_Formal_Procedure_Or_Package
11931 (N => Act,
11932 Actual => Act_E,
11933 Formal => Analyzed_S);
11934 end if;
11935
11936 if (Present (Act_E) and then Is_Overloadable (Act_E))
11937 or else Nkind (Act) in N_Attribute_Reference
11938 | N_Indexed_Component
11939 | N_Character_Literal
11940 | N_Explicit_Dereference
11941 then
11942 return;
11943 end if;
11944
11945 Error_Msg_NE
11946 ("expect subprogram or entry name in instantiation of &",
11947 Instantiation_Node, Formal_Sub);
11948 Abandon_Instantiation (Instantiation_Node);
11949 end Valid_Actual_Subprogram;
11950
11951 -- Local variables
11952
11953 Decl_Node : Node_Id;
11954 Loc : Source_Ptr;
11955 Nam : Node_Id;
11956 New_Spec : Node_Id;
11957 New_Subp : Entity_Id;
11958
11959 -- Start of processing for Instantiate_Formal_Subprogram
11960
11961 begin
11962 New_Spec := New_Copy_Tree (Specification (Formal));
11963
11964 -- The tree copy has created the proper instantiation sloc for the
11965 -- new specification. Use this location for all other constructed
11966 -- declarations.
11967
11968 Loc := Sloc (Defining_Unit_Name (New_Spec));
11969
11970 -- Create new entity for the actual (New_Copy_Tree does not), and
11971 -- indicate that it is an actual.
11972
11973 -- If the actual is not an entity (i.e. an attribute reference)
11974 -- and the formal includes aspect specifications for contracts,
11975 -- we create an internal name for the renaming declaration. The
11976 -- constructed wrapper contains a call to the entity in the renaming.
11977 -- This is an expansion activity, as is the wrapper creation.
11978
11979 if Ada_Version >= Ada_2022
11980 and then Has_Contracts (Analyzed_Formal)
11981 and then not Is_Entity_Name (Actual)
11982 and then Expander_Active
11983 then
11984 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11985 else
11986 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11987 end if;
11988
11989 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
11990 Set_Is_Generic_Actual_Subprogram (New_Subp);
11991 Set_Defining_Unit_Name (New_Spec, New_Subp);
11992
11993 -- Create new entities for the each of the formals in the specification
11994 -- of the renaming declaration built for the actual.
11995
11996 if Present (Parameter_Specifications (New_Spec)) then
11997 declare
11998 F : Node_Id;
11999 F_Id : Entity_Id;
12000
12001 begin
12002 F := First (Parameter_Specifications (New_Spec));
12003 while Present (F) loop
12004 F_Id := Defining_Identifier (F);
12005
12006 Set_Defining_Identifier (F,
12007 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
12008 Next (F);
12009 end loop;
12010 end;
12011 end if;
12012
12013 -- Find entity of actual. If the actual is an attribute reference, it
12014 -- cannot be resolved here (its formal is missing) but is handled
12015 -- instead in Attribute_Renaming. If the actual is overloaded, it is
12016 -- fully resolved subsequently, when the renaming declaration for the
12017 -- formal is analyzed. If it is an explicit dereference, resolve the
12018 -- prefix but not the actual itself, to prevent interpretation as call.
12019
12020 if Present (Actual) then
12021 Loc := Sloc (Actual);
12022 Set_Sloc (New_Spec, Loc);
12023
12024 if Nkind (Actual) = N_Operator_Symbol then
12025 Find_Direct_Name (Actual);
12026
12027 elsif Nkind (Actual) = N_Explicit_Dereference then
12028 Analyze (Prefix (Actual));
12029
12030 elsif Nkind (Actual) /= N_Attribute_Reference then
12031 Analyze (Actual);
12032 end if;
12033
12034 Valid_Actual_Subprogram (Actual);
12035 Nam := Actual;
12036
12037 elsif Present (Default_Name (Formal)) then
12038 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
12039 | N_Selected_Component
12040 | N_Indexed_Component
12041 | N_Character_Literal
12042 and then Present (Entity (Default_Name (Formal)))
12043 then
12044 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
12045 else
12046 Nam := New_Copy (Default_Name (Formal));
12047 Set_Sloc (Nam, Loc);
12048 end if;
12049
12050 elsif Box_Present (Formal) then
12051
12052 -- Actual is resolved at the point of instantiation. Create an
12053 -- identifier or operator with the same name as the formal.
12054
12055 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
12056 Nam :=
12057 Make_Operator_Symbol (Loc,
12058 Chars => Chars (Formal_Sub),
12059 Strval => No_String);
12060 else
12061 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
12062 end if;
12063
12064 elsif Has_Null_Default (Formal) then
12065 -- Generate null body for procedure, for use in the instance
12066
12067 Decl_Node :=
12068 Make_Subprogram_Body (Loc,
12069 Specification => New_Spec,
12070 Declarations => New_List,
12071 Handled_Statement_Sequence =>
12072 Make_Handled_Sequence_Of_Statements (Loc,
12073 Statements => New_List (Make_Null_Statement (Loc))));
12074
12075 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
12076
12077 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
12078
12079 Copy_Ghost_Aspect (Formal, To => Decl_Node);
12080
12081 -- Eliminate the calls to it when optimization is enabled
12082
12083 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
12084 return Decl_Node;
12085
12086 -- Handle case of a formal function with an expression default (allowed
12087 -- when extensions are enabled).
12088
12089 elsif Nkind (Specification (Formal)) = N_Function_Specification
12090 and then Present (Expression (Formal))
12091 then
12092 -- Generate body for function, for use in the instance
12093
12094 declare
12095 Expr : constant Node_Id := New_Copy (Expression (Formal));
12096 Stmt : constant Node_Id := Make_Simple_Return_Statement (Loc);
12097 begin
12098 Set_Sloc (Expr, Loc);
12099 Set_Expression (Stmt, Expr);
12100
12101 Decl_Node :=
12102 Make_Subprogram_Body (Loc,
12103 Specification => New_Spec,
12104 Declarations => New_List,
12105 Handled_Statement_Sequence =>
12106 Make_Handled_Sequence_Of_Statements (Loc,
12107 Statements => New_List (Stmt)));
12108 end;
12109
12110 -- RM 12.6 (16.2/2): Like a null procedure default, the function
12111 -- has convention Intrinsic.
12112
12113 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
12114
12115 -- Inline calls to it when optimization is enabled
12116
12117 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
12118 return Decl_Node;
12119
12120 else
12121 pragma Assert (False);
12122 end if;
12123
12124 Decl_Node :=
12125 Make_Subprogram_Renaming_Declaration (Loc,
12126 Specification => New_Spec,
12127 Name => Nam);
12128
12129 -- If we do not have an actual and the formal specified <> then set to
12130 -- get proper default.
12131
12132 if No (Actual) and then Box_Present (Formal) then
12133 Set_From_Default (Decl_Node);
12134 end if;
12135
12136 -- Gather possible interpretations for the actual before analyzing the
12137 -- instance. If overloaded, it will be resolved when analyzing the
12138 -- renaming declaration.
12139
12140 if Box_Present (Formal) and then No (Actual) then
12141 Analyze (Nam);
12142
12143 if Is_Child_Unit (Scope (Analyzed_S))
12144 and then Present (Entity (Nam))
12145 then
12146 if not Is_Overloaded (Nam) then
12147 if From_Parent_Scope (Entity (Nam)) then
12148 Set_Is_Immediately_Visible (Entity (Nam), False);
12149 Set_Entity (Nam, Empty);
12150 Set_Etype (Nam, Empty);
12151
12152 Analyze (Nam);
12153 Set_Is_Immediately_Visible (Entity (Nam));
12154 end if;
12155
12156 else
12157 declare
12158 I : Interp_Index;
12159 It : Interp;
12160
12161 begin
12162 Get_First_Interp (Nam, I, It);
12163 while Present (It.Nam) loop
12164 if From_Parent_Scope (It.Nam) then
12165 Remove_Interp (I);
12166 end if;
12167
12168 Get_Next_Interp (I, It);
12169 end loop;
12170 end;
12171 end if;
12172 end if;
12173 end if;
12174
12175 -- The generic instantiation freezes the actual. This can only be done
12176 -- once the actual is resolved, in the analysis of the renaming
12177 -- declaration. To make the formal subprogram entity available, we set
12178 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
12179 -- This is also needed in Analyze_Subprogram_Renaming for the processing
12180 -- of formal abstract subprograms.
12181
12182 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
12183
12184 -- We cannot analyze the renaming declaration, and thus find the actual,
12185 -- until all the actuals are assembled in the instance. For subsequent
12186 -- checks of other actuals, indicate the node that will hold the
12187 -- instance of this formal.
12188
12189 Set_Instance_Of (Analyzed_S, Nam);
12190
12191 if Nkind (Actual) = N_Selected_Component
12192 and then Is_Task_Type (Etype (Prefix (Actual)))
12193 and then not Is_Frozen (Etype (Prefix (Actual)))
12194 then
12195 -- The renaming declaration will create a body, which must appear
12196 -- outside of the instantiation, We move the renaming declaration
12197 -- out of the instance, and create an additional renaming inside,
12198 -- to prevent freezing anomalies.
12199
12200 declare
12201 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
12202
12203 begin
12204 Set_Defining_Unit_Name (New_Spec, Anon_Id);
12205 Insert_Before (Instantiation_Node, Decl_Node);
12206 Analyze (Decl_Node);
12207
12208 -- Now create renaming within the instance
12209
12210 Decl_Node :=
12211 Make_Subprogram_Renaming_Declaration (Loc,
12212 Specification => New_Copy_Tree (New_Spec),
12213 Name => New_Occurrence_Of (Anon_Id, Loc));
12214
12215 Set_Defining_Unit_Name (Specification (Decl_Node),
12216 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
12217 end;
12218 end if;
12219
12220 return Decl_Node;
12221 end Instantiate_Formal_Subprogram;
12222
12223 ------------------------
12224 -- Instantiate_Object --
12225 ------------------------
12226
12227 function Instantiate_Object
12228 (Formal : Node_Id;
12229 Actual : Node_Id;
12230 Analyzed_Formal : Node_Id) return List_Id
12231 is
12232 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
12233 A_Gen_Obj : constant Entity_Id :=
12234 Defining_Identifier (Analyzed_Formal);
12235 Acc_Def : Node_Id := Empty;
12236 Act_Assoc : constant Node_Id :=
12237 (if No (Actual) then Empty else Parent (Actual));
12238 Actual_Decl : Node_Id := Empty;
12239 Decl_Node : Node_Id;
12240 Def : Node_Id;
12241 Ftyp : Entity_Id;
12242 List : constant List_Id := New_List;
12243 Loc : constant Source_Ptr := Sloc (Actual);
12244 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
12245 Subt_Decl : Node_Id := Empty;
12246 Subt_Mark : Node_Id := Empty;
12247
12248 -- Start of processing for Instantiate_Object
12249
12250 begin
12251 -- Formal may be an anonymous access
12252
12253 if Present (Subtype_Mark (Formal)) then
12254 Subt_Mark := Subtype_Mark (Formal);
12255 else
12256 Check_Access_Definition (Formal);
12257 Acc_Def := Access_Definition (Formal);
12258 end if;
12259
12260 Set_Parent (List, Act_Assoc);
12261
12262 -- OUT present
12263
12264 if Out_Present (Formal) then
12265
12266 -- An IN OUT generic actual must be a name. The instantiation is a
12267 -- renaming declaration. The actual is the name being renamed. We
12268 -- use the actual directly, rather than a copy, because it is not
12269 -- used further in the list of actuals, and because a copy or a use
12270 -- of Relocate_Node is incorrect if the instance is nested within a
12271 -- generic. In order to simplify e.g. ASIS queries, the
12272 -- Generic_Parent field links the declaration to the generic
12273 -- association.
12274
12275 if Present (Subt_Mark) then
12276 Decl_Node :=
12277 Make_Object_Renaming_Declaration (Loc,
12278 Defining_Identifier => New_Copy (Gen_Obj),
12279 Subtype_Mark => New_Copy_Tree (Subt_Mark),
12280 Name => Actual);
12281
12282 else pragma Assert (Present (Acc_Def));
12283 Decl_Node :=
12284 Make_Object_Renaming_Declaration (Loc,
12285 Defining_Identifier => New_Copy (Gen_Obj),
12286 Access_Definition => New_Copy_Tree (Acc_Def),
12287 Name => Actual);
12288 end if;
12289
12290 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
12291
12292 -- The analysis of the actual may produce Insert_Action nodes, so
12293 -- the declaration must have a context in which to attach them.
12294
12295 Append (Decl_Node, List);
12296 Analyze (Actual);
12297
12298 -- Return if the analysis of the actual reported some error
12299
12300 if Etype (Actual) = Any_Type then
12301 return List;
12302 end if;
12303
12304 -- This check is performed here because Analyze_Object_Renaming will
12305 -- not check it when Comes_From_Source is False. Note though that the
12306 -- check for the actual being the name of an object will be performed
12307 -- in Analyze_Object_Renaming.
12308
12309 if Is_Object_Reference (Actual)
12310 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
12311 then
12312 Error_Msg_N
12313 ("illegal discriminant-dependent component for in out parameter",
12314 Actual);
12315 elsif Depends_On_Mutably_Tagged_Ext_Comp (Actual) then
12316 Error_Msg_N
12317 ("illegal mutably tagged component for in out parameter",
12318 Actual);
12319 end if;
12320
12321 -- The actual has to be resolved in order to check that it is a
12322 -- variable (due to cases such as F (1), where F returns access to
12323 -- an array, and for overloaded prefixes).
12324
12325 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
12326
12327 -- If the type of the formal is not itself a formal, and the current
12328 -- unit is a child unit, the formal type must be declared in a
12329 -- parent, and must be retrieved by visibility.
12330
12331 if Ftyp = Orig_Ftyp
12332 and then Is_Generic_Unit (Scope (Ftyp))
12333 and then Is_Child_Unit (Scope (A_Gen_Obj))
12334 then
12335 declare
12336 Temp : constant Node_Id :=
12337 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
12338 begin
12339 Set_Entity (Temp, Empty);
12340 Find_Type (Temp);
12341 Ftyp := Entity (Temp);
12342 end;
12343 end if;
12344
12345 if Is_Private_Type (Ftyp)
12346 and then not Is_Private_Type (Etype (Actual))
12347 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
12348 or else Base_Type (Etype (Actual)) = Ftyp)
12349 then
12350 -- If the actual has the type of the full view of the formal, or
12351 -- else a non-private subtype of the formal, then the visibility
12352 -- of the formal type has changed. Add to the actuals a subtype
12353 -- declaration that will force the exchange of views in the body
12354 -- of the instance as well.
12355
12356 Subt_Decl :=
12357 Make_Subtype_Declaration (Loc,
12358 Defining_Identifier => Make_Temporary (Loc, 'P'),
12359 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
12360
12361 Prepend (Subt_Decl, List);
12362
12363 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
12364 Exchange_Declarations (Ftyp);
12365 end if;
12366
12367 Resolve (Actual, Ftyp);
12368
12369 if not Denotes_Variable (Actual) then
12370 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
12371
12372 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
12373
12374 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
12375 -- the type of the actual shall resolve to a specific anonymous
12376 -- access type.
12377
12378 if Ada_Version < Ada_2005
12379 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
12380 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
12381 then
12382 Error_Msg_NE
12383 ("type of actual does not match type of&", Actual, Gen_Obj);
12384 end if;
12385 end if;
12386
12387 Note_Possible_Modification (Actual, Sure => True);
12388
12389 -- Check for instantiation with atomic/volatile/VFA object actual for
12390 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
12391
12392 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
12393 Error_Msg_NE
12394 ("cannot instantiate nonatomic formal & of mode in out",
12395 Actual, Gen_Obj);
12396 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
12397
12398 elsif Is_Volatile_Object_Ref (Actual)
12399 and then not Is_Volatile (Orig_Ftyp)
12400 then
12401 Error_Msg_NE
12402 ("cannot instantiate nonvolatile formal & of mode in out",
12403 Actual, Gen_Obj);
12404 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
12405
12406 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
12407 and then not Is_Volatile_Full_Access (Orig_Ftyp)
12408 then
12409 Error_Msg_NE
12410 ("cannot instantiate nonfull access formal & of mode in out",
12411 Actual, Gen_Obj);
12412 Error_Msg_N
12413 ("\with full access object actual (RM C.6(12))", Actual);
12414 end if;
12415
12416 -- Check for instantiation on nonatomic subcomponent of a full access
12417 -- object in Ada 2022 (RM C.6 (12)).
12418
12419 if Ada_Version >= Ada_2022
12420 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
12421 and then not Is_Atomic_Object (Actual)
12422 then
12423 Error_Msg_NE
12424 ("cannot instantiate formal & of mode in out with actual",
12425 Actual, Gen_Obj);
12426 Error_Msg_N
12427 ("\nonatomic subcomponent of full access object (RM C.6(12))",
12428 Actual);
12429 end if;
12430
12431 -- The actual for a ghost generic formal IN OUT parameter should be a
12432 -- ghost object (SPARK RM 6.9(14)).
12433
12434 Check_Ghost_Formal_Variable
12435 (Actual => Actual,
12436 Formal => A_Gen_Obj);
12437
12438 -- Formal in-mode parameter
12439
12440 else
12441 -- The instantiation of a generic formal in-mode parameter is a
12442 -- constant declaration. The actual is the expression for that
12443 -- declaration. Its type is a full copy of the type of the
12444 -- formal. This may be an access to subprogram, for which we need
12445 -- to generate entities for the formals in the new signature.
12446
12447 if Present (Actual) then
12448 if Present (Subt_Mark) then
12449 Def := New_Copy_Tree (Subt_Mark);
12450 else
12451 pragma Assert (Present (Acc_Def));
12452 Def := New_Copy_Tree (Acc_Def);
12453 end if;
12454
12455 Decl_Node :=
12456 Make_Object_Declaration (Loc,
12457 Defining_Identifier => New_Copy (Gen_Obj),
12458 Constant_Present => True,
12459 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
12460 Object_Definition => Def,
12461 Expression => Actual);
12462
12463 Copy_Ghost_Aspect (Formal, To => Decl_Node);
12464 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
12465
12466 -- A generic formal object of a tagged type is defined to be
12467 -- aliased so the new constant must also be treated as aliased.
12468
12469 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
12470 Set_Aliased_Present (Decl_Node);
12471 end if;
12472
12473 Append (Decl_Node, List);
12474
12475 -- The actual for a ghost generic formal IN parameter of
12476 -- access-to-variable type should be a ghost object (SPARK
12477 -- RM 6.9(14)).
12478
12479 if Is_Access_Variable (Etype (A_Gen_Obj)) then
12480 Check_Ghost_Formal_Variable
12481 (Actual => Actual,
12482 Formal => A_Gen_Obj);
12483 end if;
12484
12485 -- No need to repeat (pre-)analysis of some expression nodes
12486 -- already handled in Preanalyze_Actuals.
12487
12488 if Nkind (Actual) /= N_Allocator then
12489 Analyze (Actual);
12490
12491 -- Return if the analysis of the actual reported some error
12492
12493 if Etype (Actual) = Any_Type then
12494 return List;
12495 end if;
12496 end if;
12497
12498 declare
12499 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
12500 Typ : Entity_Id;
12501
12502 begin
12503 Typ := Get_Instance_Of (Formal_Type);
12504
12505 -- If the actual appears in the current or an enclosing scope,
12506 -- use its type directly. This is relevant if it has an actual
12507 -- subtype that is distinct from its nominal one. This cannot
12508 -- be done in general because the type of the actual may
12509 -- depend on other actuals, and only be fully determined when
12510 -- the enclosing instance is analyzed.
12511
12512 if Present (Etype (Actual))
12513 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
12514 then
12515 Freeze_Before (Instantiation_Node, Etype (Actual));
12516 else
12517 Freeze_Before (Instantiation_Node, Typ);
12518 end if;
12519
12520 -- If the actual is an aggregate, perform name resolution on
12521 -- its components (the analysis of an aggregate does not do it)
12522 -- to capture local names that may be hidden if the generic is
12523 -- a child unit.
12524
12525 if Nkind (Actual) = N_Aggregate then
12526 Preanalyze_And_Resolve (Actual, Typ);
12527 end if;
12528
12529 if Is_Limited_Type (Typ)
12530 and then not OK_For_Limited_Init (Typ, Actual)
12531 then
12532 Error_Msg_N
12533 ("initialization not allowed for limited types", Actual);
12534 Explain_Limited_Type (Typ, Actual);
12535 end if;
12536 end;
12537
12538 elsif Present (Default_Expression (Formal)) then
12539
12540 -- Use default to construct declaration
12541
12542 if Present (Subt_Mark) then
12543 Def := New_Copy_Tree (Subt_Mark);
12544 else
12545 pragma Assert (Present (Acc_Def));
12546 Def := New_Copy_Tree (Acc_Def);
12547 end if;
12548
12549 Decl_Node :=
12550 Make_Object_Declaration (Sloc (Formal),
12551 Defining_Identifier => New_Copy (Gen_Obj),
12552 Constant_Present => True,
12553 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
12554 Object_Definition => Def,
12555 Expression => New_Copy_Tree
12556 (Default_Expression (Formal)));
12557
12558 Copy_Ghost_Aspect (Formal, To => Decl_Node);
12559 Set_Corresponding_Generic_Association
12560 (Decl_Node, Expression (Decl_Node));
12561
12562 Append (Decl_Node, List);
12563 Set_Analyzed (Expression (Decl_Node), False);
12564
12565 else
12566 pragma Assert (False);
12567 end if;
12568 end if;
12569
12570 if Nkind (Actual) in N_Has_Entity
12571 and then Present (Entity (Actual))
12572 then
12573 Actual_Decl := Parent (Entity (Actual));
12574 end if;
12575
12576 -- Ada 2005 (AI-423) refined by AI12-0287:
12577 -- For an object_renaming_declaration with a null_exclusion or an
12578 -- access_definition that has a null_exclusion, the subtype of the
12579 -- object_name shall exclude null. In addition, if the
12580 -- object_renaming_declaration occurs within the body of a generic unit
12581 -- G or within the body of a generic unit declared within the
12582 -- declarative region of generic unit G, then:
12583 -- * if the object_name statically denotes a generic formal object of
12584 -- mode in out of G, then the declaration of that object shall have a
12585 -- null_exclusion;
12586 -- * if the object_name statically denotes a call of a generic formal
12587 -- function of G, then the declaration of the result of that function
12588 -- shall have a null_exclusion.
12589
12590 if Ada_Version >= Ada_2005
12591 and then Present (Actual_Decl)
12592 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
12593 | N_Object_Declaration
12594 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
12595 and then not Has_Null_Exclusion (Actual_Decl)
12596 and then Has_Null_Exclusion (Analyzed_Formal)
12597 and then Ekind (Defining_Identifier (Analyzed_Formal))
12598 = E_Generic_In_Out_Parameter
12599 and then ((In_Generic_Scope (Entity (Actual))
12600 and then In_Package_Body (Scope (Entity (Actual))))
12601 or else not Can_Never_Be_Null (Etype (Actual)))
12602 then
12603 Error_Msg_Sloc := Sloc (Analyzed_Formal);
12604 Error_Msg_N
12605 ("actual must exclude null to match generic formal#", Actual);
12606 end if;
12607
12608 return List;
12609 end Instantiate_Object;
12610
12611 ------------------------------
12612 -- Instantiate_Package_Body --
12613 ------------------------------
12614
12615 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12616 -- must be replaced by gotos which jump to the end of the routine in order
12617 -- to restore the Ghost and SPARK modes.
12618
12619 procedure Instantiate_Package_Body
12620 (Body_Info : Pending_Body_Info;
12621 Inlined_Body : Boolean := False;
12622 Body_Optional : Boolean := False)
12623 is
12624 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12625 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12626 Act_Spec : constant Node_Id := Specification (Act_Decl);
12627 Ctx_Parents : Elist_Id := No_Elist;
12628 Ctx_Top : Int := 0;
12629 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12630 Gen_Id : constant Node_Id := Name (Inst_Node);
12631 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12632 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12633 Loc : constant Source_Ptr := Sloc (Inst_Node);
12634
12635 procedure Check_Initialized_Types;
12636 -- In a generic package body, an entity of a generic private type may
12637 -- appear uninitialized. This is suspicious, unless the actual is a
12638 -- fully initialized type.
12639
12640 procedure Install_Parents_Of_Generic_Context
12641 (Inst_Scope : Entity_Id;
12642 Ctx_Parents : out Elist_Id);
12643 -- Inst_Scope is the scope where the instance appears within; when it
12644 -- appears within a generic child package G, this routine collects and
12645 -- installs the enclosing packages of G in the scopes stack; installed
12646 -- packages are returned in Ctx_Parents.
12647
12648 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
12649 -- Reverse effect after instantiation is complete
12650
12651 -----------------------------
12652 -- Check_Initialized_Types --
12653 -----------------------------
12654
12655 procedure Check_Initialized_Types is
12656 Decl : Node_Id;
12657 Formal : Entity_Id;
12658 Actual : Entity_Id;
12659 Uninit_Var : Entity_Id;
12660
12661 begin
12662 Decl := First (Generic_Formal_Declarations (Gen_Decl));
12663 while Present (Decl) loop
12664 Uninit_Var := Empty;
12665
12666 if Nkind (Decl) = N_Private_Extension_Declaration then
12667 Uninit_Var := Uninitialized_Variable (Decl);
12668
12669 elsif Nkind (Decl) = N_Formal_Type_Declaration
12670 and then Nkind (Formal_Type_Definition (Decl)) =
12671 N_Formal_Private_Type_Definition
12672 then
12673 Uninit_Var :=
12674 Uninitialized_Variable (Formal_Type_Definition (Decl));
12675 end if;
12676
12677 if Present (Uninit_Var) then
12678 Formal := Defining_Identifier (Decl);
12679 Actual := First_Entity (Act_Decl_Id);
12680
12681 -- For each formal there is a subtype declaration that renames
12682 -- the actual and has the same name as the formal. Locate the
12683 -- formal for warning message about uninitialized variables
12684 -- in the generic, for which the actual type should be a fully
12685 -- initialized type.
12686
12687 while Present (Actual) loop
12688 exit when Ekind (Actual) = E_Package
12689 and then Present (Renamed_Entity (Actual));
12690
12691 if Chars (Actual) = Chars (Formal)
12692 and then not Is_Scalar_Type (Actual)
12693 and then not Is_Fully_Initialized_Type (Actual)
12694 and then Warn_On_No_Value_Assigned
12695 then
12696 Error_Msg_Node_2 := Formal;
12697 Error_Msg_NE
12698 ("generic unit has uninitialized variable& of "
12699 & "formal private type &?v?", Actual, Uninit_Var);
12700 Error_Msg_NE
12701 ("actual type for& should be fully initialized type?v?",
12702 Actual, Formal);
12703 exit;
12704 end if;
12705
12706 Next_Entity (Actual);
12707 end loop;
12708 end if;
12709
12710 Next (Decl);
12711 end loop;
12712 end Check_Initialized_Types;
12713
12714 ----------------------------------------
12715 -- Install_Parents_Of_Generic_Context --
12716 ----------------------------------------
12717
12718 procedure Install_Parents_Of_Generic_Context
12719 (Inst_Scope : Entity_Id;
12720 Ctx_Parents : out Elist_Id)
12721 is
12722 Elmt : Elmt_Id;
12723 S : Entity_Id;
12724
12725 begin
12726 Ctx_Parents := New_Elmt_List;
12727
12728 -- Collect context parents (ie. parents where the instantiation
12729 -- appears within).
12730
12731 S := Inst_Scope;
12732 while S /= Standard_Standard loop
12733 Prepend_Elmt (S, Ctx_Parents);
12734 S := Scope (S);
12735 end loop;
12736
12737 -- Install enclosing parents
12738
12739 Elmt := First_Elmt (Ctx_Parents);
12740 while Present (Elmt) loop
12741 Push_Scope (Node (Elmt));
12742 Set_Is_Immediately_Visible (Node (Elmt));
12743 Next_Elmt (Elmt);
12744 end loop;
12745 end Install_Parents_Of_Generic_Context;
12746
12747 ---------------------------------------
12748 -- Remove_Parents_Of_Generic_Context --
12749 ---------------------------------------
12750
12751 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
12752 Elmt : Elmt_Id;
12753
12754 begin
12755 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
12756
12757 Elmt := Last_Elmt (Ctx_Parents);
12758 while Present (Elmt) loop
12759 pragma Assert (Current_Scope = Node (Elmt));
12760 Set_Is_Immediately_Visible (Current_Scope, False);
12761 Pop_Scope;
12762
12763 Remove_Last_Elmt (Ctx_Parents);
12764 Elmt := Last_Elmt (Ctx_Parents);
12765 end loop;
12766 end Remove_Parents_Of_Generic_Context;
12767
12768 -- Local variables
12769
12770 -- The following constants capture the context prior to instantiating
12771 -- the package body.
12772
12773 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12774 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12775 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12776 Saved_ISMP : constant Boolean :=
12777 Ignore_SPARK_Mode_Pragmas_In_Instance;
12778 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12779 Local_Suppress_Stack_Top;
12780 Saved_SC : constant Boolean := Style_Check;
12781 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12782 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12783 Saved_SS : constant Suppress_Record := Scope_Suppress;
12784 Saved_Warn : constant Warnings_State := Save_Warnings;
12785
12786 Act_Body : Node_Id;
12787 Act_Body_Id : Entity_Id;
12788 Act_Body_Name : Node_Id;
12789 Gen_Body : Node_Id;
12790 Gen_Body_Id : Node_Id;
12791 Par_Ent : Entity_Id := Empty;
12792 Par_Installed : Boolean := False;
12793 Par_Vis : Boolean := False;
12794
12795 Scope_Check_Id : Entity_Id;
12796 Scope_Check_Last : Nat;
12797 -- Value of Current_Scope before calls to Install_Parents; used to check
12798 -- that scopes are correctly removed after instantiation.
12799
12800 Vis_Prims_List : Elist_Id := No_Elist;
12801 -- List of primitives made temporarily visible in the instantiation
12802 -- to match the visibility of the formal type.
12803
12804 -- Start of processing for Instantiate_Package_Body
12805
12806 begin
12807 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12808
12809 -- The instance body may already have been processed, as the parent of
12810 -- another instance that is inlined (Load_Parent_Of_Generic).
12811
12812 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12813 return;
12814 end if;
12815
12816 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12817
12818 -- Re-establish the state of information on which checks are suppressed.
12819 -- This information was set in Body_Info at the point of instantiation,
12820 -- and now we restore it so that the instance is compiled using the
12821 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12822
12823 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12824 Scope_Suppress := Body_Info.Scope_Suppress;
12825
12826 Restore_Config_Switches (Body_Info.Config_Switches);
12827 Restore_Warnings (Body_Info.Warnings);
12828
12829 if No (Gen_Body_Id) then
12830
12831 -- Do not look for parent of generic body if none is required.
12832 -- This may happen when the routine is called as part of the
12833 -- Pending_Instantiations processing, when nested instances
12834 -- may precede the one generated from the main unit.
12835
12836 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
12837 and then Body_Optional
12838 then
12839 goto Leave;
12840 else
12841 Load_Parent_Of_Generic
12842 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12843
12844 -- Surprisingly enough, loading the body of the parent can cause
12845 -- the body to be instantiated and the double instantiation needs
12846 -- to be prevented in order to avoid giving bogus semantic errors.
12847
12848 -- This case can occur because of the Collect_Previous_Instances
12849 -- machinery of Load_Parent_Of_Generic, which will instantiate
12850 -- bodies that are deemed to be ahead of the body of the parent
12851 -- in the compilation unit. But the relative position of these
12852 -- bodies is computed using the mere comparison of their Sloc.
12853
12854 -- Now suppose that you have two generic packages G and H, with
12855 -- G containing a mere instantiation of H:
12856
12857 -- generic
12858 -- package H is
12859
12860 -- generic
12861 -- package Nested_G is
12862 -- ...
12863 -- end Nested_G;
12864
12865 -- end H;
12866
12867 -- with H;
12868
12869 -- generic
12870 -- package G is
12871
12872 -- package My_H is new H;
12873
12874 -- end G;
12875
12876 -- and a third package Q instantiating G and Nested_G:
12877
12878 -- with G;
12879
12880 -- package Q is
12881
12882 -- package My_G is new G;
12883
12884 -- package My_Nested_G is new My_G.My_H.Nested_G;
12885
12886 -- end Q;
12887
12888 -- The body to be instantiated is that of My_Nested_G and its
12889 -- parent is the instance My_G.My_H. This latter instantiation
12890 -- is done when My_G is analyzed, i.e. after the declarations
12891 -- of My_G and My_Nested_G have been parsed; as a result, the
12892 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
12893
12894 -- Therefore loading the body of My_G.My_H will cause the body
12895 -- of My_Nested_G to be instantiated because it is deemed to be
12896 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
12897 -- will again be invoked on My_G.My_H, but this time with the
12898 -- Collect_Previous_Instances machinery disabled, so there is
12899 -- no endless mutual recursion and things are done in order.
12900
12901 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12902 goto Leave;
12903 end if;
12904
12905 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12906 end if;
12907 end if;
12908
12909 -- Establish global variable for sloc adjustment and for error recovery
12910 -- In the case of an instance body for an instantiation with actuals
12911 -- from a limited view, the instance body is placed at the beginning
12912 -- of the enclosing package body: use the body entity as the source
12913 -- location for nodes of the instance body.
12914
12915 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
12916 declare
12917 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12918 Body_Id : constant Node_Id :=
12919 Corresponding_Body (Unit_Declaration_Node (Scop));
12920
12921 begin
12922 Instantiation_Node := Body_Id;
12923 end;
12924 else
12925 Instantiation_Node := Inst_Node;
12926 end if;
12927
12928 -- The package being instantiated may be subject to pragma Ghost. Set
12929 -- the mode now to ensure that any nodes generated during instantiation
12930 -- are properly marked as Ghost.
12931
12932 Set_Ghost_Mode (Act_Decl_Id);
12933
12934 if Present (Gen_Body_Id) then
12935 Save_Env (Gen_Unit, Act_Decl_Id);
12936 Style_Check := False;
12937
12938 -- If the context of the instance is subject to SPARK_Mode "off", the
12939 -- annotation is missing, or the body is instantiated at a later pass
12940 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12941 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12942 -- instance.
12943
12944 if SPARK_Mode /= On
12945 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12946 then
12947 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12948 end if;
12949
12950 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12951 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12952
12953 Create_Instantiation_Source
12954 (Inst_Node, Gen_Body_Id, S_Adjustment);
12955
12956 Act_Body :=
12957 Copy_Generic_Node
12958 (Original_Node (Gen_Body), Empty, Instantiating => True);
12959
12960 -- Create proper (possibly qualified) defining name for the body, to
12961 -- correspond to the one in the spec.
12962
12963 Act_Body_Id :=
12964 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12965 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12966
12967 -- Some attributes of spec entity are not inherited by body entity
12968
12969 Set_Handler_Records (Act_Body_Id, No_List);
12970
12971 if Nkind (Defining_Unit_Name (Act_Spec)) =
12972 N_Defining_Program_Unit_Name
12973 then
12974 Act_Body_Name :=
12975 Make_Defining_Program_Unit_Name (Loc,
12976 Name =>
12977 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12978 Defining_Identifier => Act_Body_Id);
12979 else
12980 Act_Body_Name := Act_Body_Id;
12981 end if;
12982
12983 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12984
12985 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12986 Check_Generic_Actuals (Act_Decl_Id, False);
12987 Check_Initialized_Types;
12988
12989 -- Install primitives hidden at the point of the instantiation but
12990 -- visible when processing the generic formals
12991
12992 declare
12993 E : Entity_Id;
12994
12995 begin
12996 E := First_Entity (Act_Decl_Id);
12997 while Present (E) loop
12998 if Is_Type (E)
12999 and then not Is_Itype (E)
13000 and then Is_Generic_Actual_Type (E)
13001 and then Is_Tagged_Type (E)
13002 then
13003 Install_Hidden_Primitives
13004 (Prims_List => Vis_Prims_List,
13005 Gen_T => Generic_Parent_Type (Parent (E)),
13006 Act_T => E);
13007 end if;
13008
13009 Next_Entity (E);
13010 end loop;
13011 end;
13012
13013 Scope_Check_Id := Current_Scope;
13014 Scope_Check_Last := Scope_Stack.Last;
13015
13016 -- If the instantiation appears within a generic child some actual
13017 -- parameter may be the current instance of the enclosing generic
13018 -- parent.
13019
13020 declare
13021 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
13022
13023 begin
13024 if Is_Child_Unit (Inst_Scope)
13025 and then Ekind (Inst_Scope) = E_Generic_Package
13026 and then Present (Generic_Associations (Inst_Node))
13027 then
13028 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
13029
13030 -- Hide them from visibility; required to avoid conflicts
13031 -- installing the parent instance.
13032
13033 if Present (Ctx_Parents) then
13034 Push_Scope (Standard_Standard);
13035 Ctx_Top := Scope_Stack.Last;
13036 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
13037 end if;
13038 end if;
13039 end;
13040
13041 -- If it is a child unit, make the parent instance (which is an
13042 -- instance of the parent of the generic) visible.
13043
13044 -- 1) The child unit's parent is an explicit parent instance (the
13045 -- prefix of the name of the generic unit):
13046
13047 -- package Child_Package is new Parent_Instance.Child_Unit;
13048
13049 -- 2) The child unit's parent is an implicit parent instance (e.g.
13050 -- when instantiating a sibling package):
13051
13052 -- generic
13053 -- package Parent.Second_Child is
13054 -- ...
13055
13056 -- generic
13057 -- package Parent.First_Child is
13058 -- package Sibling_Package is new Second_Child;
13059
13060 -- 3) The child unit's parent is not an instance, so the scope is
13061 -- simply the one of the unit.
13062
13063 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
13064 and then Nkind (Gen_Id) = N_Expanded_Name
13065 then
13066 Par_Ent := Entity (Prefix (Gen_Id));
13067
13068 elsif Ekind (Scope (Gen_Unit)) = E_Generic_Package
13069 and then Ekind (Scope (Act_Decl_Id)) = E_Package
13070 and then Is_Generic_Instance (Scope (Act_Decl_Id))
13071 and then Nkind
13072 (Name (Get_Unit_Instantiation_Node
13073 (Scope (Act_Decl_Id)))) = N_Expanded_Name
13074 then
13075 Par_Ent := Entity
13076 (Prefix (Name (Get_Unit_Instantiation_Node
13077 (Scope (Act_Decl_Id)))));
13078
13079 elsif Is_Child_Unit (Gen_Unit) then
13080 Par_Ent := Scope (Gen_Unit);
13081 end if;
13082
13083 if Present (Par_Ent) then
13084 Par_Vis := Is_Immediately_Visible (Par_Ent);
13085 Install_Parent (Par_Ent, In_Body => True);
13086 Par_Installed := True;
13087 end if;
13088
13089 -- If the instantiation is a library unit, and this is the main unit,
13090 -- then build the resulting compilation unit nodes for the instance.
13091 -- If this is a compilation unit but it is not the main unit, then it
13092 -- is the body of a unit in the context, that is being compiled
13093 -- because it is encloses some inlined unit or another generic unit
13094 -- being instantiated. In that case, this body is not part of the
13095 -- current compilation, and is not attached to the tree, but its
13096 -- parent must be set for analysis.
13097
13098 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
13099
13100 -- Replace instance node with body of instance, and create new
13101 -- node for corresponding instance declaration.
13102
13103 Build_Instance_Compilation_Unit_Nodes
13104 (Inst_Node, Act_Body, Act_Decl);
13105
13106 -- If the instantiation appears within a generic child package
13107 -- enable visibility of current instance of enclosing generic
13108 -- parents.
13109
13110 if Present (Ctx_Parents) then
13111 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
13112 Analyze (Inst_Node);
13113 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
13114 else
13115 Analyze (Inst_Node);
13116 end if;
13117
13118 if Parent (Inst_Node) = Cunit (Main_Unit) then
13119
13120 -- If the instance is a child unit itself, then set the scope
13121 -- of the expanded body to be the parent of the instantiation
13122 -- (ensuring that the fully qualified name will be generated
13123 -- for the elaboration subprogram).
13124
13125 if Nkind (Defining_Unit_Name (Act_Spec)) =
13126 N_Defining_Program_Unit_Name
13127 then
13128 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
13129 end if;
13130 end if;
13131
13132 -- Case where instantiation is not a library unit
13133
13134 else
13135 -- Handle the case of an instance with incomplete actual types.
13136 -- The instance body cannot be placed just after the declaration
13137 -- because full views have not been seen yet. Any use of the non-
13138 -- limited views in the instance body requires the presence of a
13139 -- regular with_clause in the enclosing unit. Therefore we place
13140 -- the instance body at the beginning of the enclosing body, and
13141 -- the freeze node for the instance is then placed after the body.
13142
13143 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id))
13144 and then Ekind (Scope (Act_Decl_Id)) = E_Package
13145 then
13146 declare
13147 Scop : constant Entity_Id := Scope (Act_Decl_Id);
13148 Body_Id : constant Node_Id :=
13149 Corresponding_Body (Unit_Declaration_Node (Scop));
13150
13151 F_Node : Node_Id;
13152
13153 begin
13154 pragma Assert (Present (Body_Id));
13155
13156 Prepend (Act_Body, Declarations (Parent (Body_Id)));
13157
13158 if Expander_Active then
13159 Ensure_Freeze_Node (Act_Decl_Id);
13160 F_Node := Freeze_Node (Act_Decl_Id);
13161 Set_Is_Frozen (Act_Decl_Id, False);
13162 if Is_List_Member (F_Node) then
13163 Remove (F_Node);
13164 end if;
13165
13166 Insert_After (Act_Body, F_Node);
13167 end if;
13168 end;
13169
13170 else
13171 Insert_Before (Inst_Node, Act_Body);
13172 Mark_Rewrite_Insertion (Act_Body);
13173
13174 -- Insert the freeze node for the instance if need be
13175
13176 if Expander_Active then
13177 Freeze_Package_Instance
13178 (Inst_Node, Gen_Body, Gen_Decl, Act_Decl_Id);
13179 Set_Is_Frozen (Act_Decl_Id);
13180 end if;
13181 end if;
13182
13183 -- If the instantiation appears within a generic child package
13184 -- enable visibility of current instance of enclosing generic
13185 -- parents.
13186
13187 if Present (Ctx_Parents) then
13188 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
13189 Analyze (Act_Body);
13190 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
13191 else
13192 Analyze (Act_Body);
13193 end if;
13194 end if;
13195
13196 Inherit_Context (Gen_Body, Inst_Node);
13197
13198 if Par_Installed then
13199 Remove_Parent (In_Body => True);
13200
13201 -- Restore the previous visibility of the parent
13202
13203 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
13204 end if;
13205
13206 -- Remove the parent instances if they have been placed on the scope
13207 -- stack to compile the body.
13208
13209 if Present (Ctx_Parents) then
13210 pragma Assert (Scope_Stack.Last = Ctx_Top
13211 and then Current_Scope = Standard_Standard);
13212 Pop_Scope;
13213
13214 Remove_Parents_Of_Generic_Context (Ctx_Parents);
13215 end if;
13216
13217 pragma Assert (Current_Scope = Scope_Check_Id);
13218 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
13219
13220 Restore_Hidden_Primitives (Vis_Prims_List);
13221
13222 -- Restore the private views that were made visible when the body of
13223 -- the instantiation was created. Note that, in the case where one of
13224 -- these private views is declared in the parent, there is a nesting
13225 -- issue with the calls to Install_Parent and Remove_Parent made in
13226 -- between above with In_Body set to True, because these calls also
13227 -- want to swap and restore this private view respectively. In this
13228 -- case, the call to Install_Parent does nothing, but the call to
13229 -- Remove_Parent does restore the private view, thus undercutting the
13230 -- call to Restore_Private_Views. That's OK under the condition that
13231 -- the two mechanisms swap exactly the same entities, in particular
13232 -- the private entities dependent on the primary private entities.
13233
13234 Restore_Private_Views (Act_Decl_Id);
13235
13236 -- Remove the current unit from visibility if this is an instance
13237 -- that is not elaborated on the fly for inlining purposes.
13238
13239 if not Inlined_Body then
13240 Set_Is_Immediately_Visible (Act_Decl_Id, False);
13241 end if;
13242
13243 Restore_Env;
13244
13245 -- If we have no body, and the unit requires a body, then complain. This
13246 -- complaint is suppressed if we have detected other errors (since a
13247 -- common reason for missing the body is that it had errors).
13248 -- In CodePeer mode, a warning has been emitted already, no need for
13249 -- further messages.
13250
13251 elsif Unit_Requires_Body (Gen_Unit)
13252 and then not Body_Optional
13253 then
13254 if CodePeer_Mode then
13255 null;
13256
13257 elsif Serious_Errors_Detected = 0 then
13258 Error_Msg_NE
13259 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
13260
13261 -- Don't attempt to perform any cleanup actions if some other error
13262 -- was already detected, since this can cause blowups.
13263
13264 else
13265 goto Leave;
13266 end if;
13267
13268 -- Case of package that does not need a body
13269
13270 else
13271 -- If the instantiation of the declaration is a library unit, rewrite
13272 -- the original package instantiation as a package declaration in the
13273 -- compilation unit node.
13274
13275 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
13276 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
13277 Rewrite (Inst_Node, Act_Decl);
13278
13279 -- Generate elaboration entity, in case spec has elaboration code.
13280 -- This cannot be done when the instance is analyzed, because it
13281 -- is not known yet whether the body exists.
13282
13283 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
13284 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
13285
13286 -- If the instantiation is not a library unit, then append the
13287 -- declaration to the list of implicitly generated entities, unless
13288 -- it is already a list member which means that it was already
13289 -- processed
13290
13291 elsif not Is_List_Member (Act_Decl) then
13292 Mark_Rewrite_Insertion (Act_Decl);
13293 Insert_Before (Inst_Node, Act_Decl);
13294 end if;
13295 end if;
13296
13297 <<Leave>>
13298
13299 -- Restore the context that was in effect prior to instantiating the
13300 -- package body.
13301
13302 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
13303 Local_Suppress_Stack_Top := Saved_LSST;
13304 Scope_Suppress := Saved_SS;
13305 Style_Check := Saved_SC;
13306
13307 Expander_Mode_Restore;
13308 Restore_Config_Switches (Saved_CS);
13309 Restore_Ghost_Region (Saved_GM, Saved_IGR);
13310 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
13311 Restore_Warnings (Saved_Warn);
13312 end Instantiate_Package_Body;
13313
13314 ---------------------------------
13315 -- Instantiate_Subprogram_Body --
13316 ---------------------------------
13317
13318 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
13319 -- must be replaced by gotos which jump to the end of the routine in order
13320 -- to restore the Ghost and SPARK modes.
13321
13322 procedure Instantiate_Subprogram_Body
13323 (Body_Info : Pending_Body_Info;
13324 Body_Optional : Boolean := False)
13325 is
13326 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
13327 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
13328 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
13329 Gen_Id : constant Node_Id := Name (Inst_Node);
13330 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
13331 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
13332 Loc : constant Source_Ptr := Sloc (Inst_Node);
13333 Pack_Id : constant Entity_Id :=
13334 Defining_Unit_Name (Parent (Act_Decl));
13335
13336 -- The following constants capture the context prior to instantiating
13337 -- the subprogram body.
13338
13339 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
13340 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
13341 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
13342 Saved_ISMP : constant Boolean :=
13343 Ignore_SPARK_Mode_Pragmas_In_Instance;
13344 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
13345 Local_Suppress_Stack_Top;
13346 Saved_SC : constant Boolean := Style_Check;
13347 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
13348 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
13349 Saved_SS : constant Suppress_Record := Scope_Suppress;
13350 Saved_Warn : constant Warnings_State := Save_Warnings;
13351
13352 Act_Body : Node_Id;
13353 Act_Body_Id : Entity_Id;
13354 Gen_Body : Node_Id;
13355 Gen_Body_Id : Node_Id;
13356 Pack_Body : Node_Id;
13357 Par_Ent : Entity_Id := Empty;
13358 Par_Installed : Boolean := False;
13359 Par_Vis : Boolean := False;
13360 Ret_Expr : Node_Id;
13361
13362 begin
13363 Gen_Body_Id := Corresponding_Body (Gen_Decl);
13364
13365 -- Subprogram body may have been created already because of an inline
13366 -- pragma, or because of multiple elaborations of the enclosing package
13367 -- when several instances of the subprogram appear in the main unit.
13368
13369 if Present (Corresponding_Body (Act_Decl)) then
13370 return;
13371 end if;
13372
13373 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
13374
13375 -- Re-establish the state of information on which checks are suppressed.
13376 -- This information was set in Body_Info at the point of instantiation,
13377 -- and now we restore it so that the instance is compiled using the
13378 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
13379
13380 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
13381 Scope_Suppress := Body_Info.Scope_Suppress;
13382
13383 Restore_Config_Switches (Body_Info.Config_Switches);
13384 Restore_Warnings (Body_Info.Warnings);
13385
13386 if No (Gen_Body_Id) then
13387
13388 -- For imported generic subprogram, no body to compile, complete
13389 -- the spec entity appropriately.
13390
13391 if Is_Imported (Gen_Unit) then
13392 Set_Is_Imported (Act_Decl_Id);
13393 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
13394 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
13395 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
13396 Set_Has_Completion (Act_Decl_Id);
13397 goto Leave;
13398
13399 -- For other cases, compile the body
13400
13401 else
13402 Load_Parent_Of_Generic
13403 (Inst_Node, Specification (Gen_Decl), Body_Optional);
13404 Gen_Body_Id := Corresponding_Body (Gen_Decl);
13405 end if;
13406 end if;
13407
13408 Instantiation_Node := Inst_Node;
13409
13410 -- The subprogram being instantiated may be subject to pragma Ghost. Set
13411 -- the mode now to ensure that any nodes generated during instantiation
13412 -- are properly marked as Ghost.
13413
13414 Set_Ghost_Mode (Act_Decl_Id);
13415
13416 if Present (Gen_Body_Id) then
13417 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
13418
13419 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
13420
13421 -- Either body is not present, or context is non-expanding, as
13422 -- when compiling a subunit. Mark the instance as completed, and
13423 -- diagnose a missing body when needed.
13424
13425 if Expander_Active
13426 and then Operating_Mode = Generate_Code
13427 then
13428 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
13429 end if;
13430
13431 Set_Has_Completion (Act_Decl_Id);
13432 goto Leave;
13433 end if;
13434
13435 Save_Env (Gen_Unit, Act_Decl_Id);
13436 Style_Check := False;
13437
13438 -- If the context of the instance is subject to SPARK_Mode "off", the
13439 -- annotation is missing, or the body is instantiated at a later pass
13440 -- and its spec ignored SPARK_Mode pragma, set the global flag which
13441 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
13442 -- instance.
13443
13444 if SPARK_Mode /= On
13445 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
13446 then
13447 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
13448 end if;
13449
13450 -- If the context of an instance is not subject to SPARK_Mode "off",
13451 -- and the generic body is subject to an explicit SPARK_Mode pragma,
13452 -- the latter should be the one applicable to the instance.
13453
13454 if not Ignore_SPARK_Mode_Pragmas_In_Instance
13455 and then SPARK_Mode /= Off
13456 and then Present (SPARK_Pragma (Gen_Body_Id))
13457 then
13458 Set_SPARK_Mode (Gen_Body_Id);
13459 end if;
13460
13461 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
13462 Create_Instantiation_Source
13463 (Inst_Node,
13464 Gen_Body_Id,
13465 S_Adjustment);
13466
13467 Act_Body :=
13468 Copy_Generic_Node
13469 (Original_Node (Gen_Body), Empty, Instantiating => True);
13470
13471 -- Create proper defining name for the body, to correspond to the one
13472 -- in the spec.
13473
13474 Act_Body_Id :=
13475 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
13476
13477 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
13478 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
13479
13480 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
13481 Set_Has_Completion (Act_Decl_Id);
13482 Check_Generic_Actuals (Pack_Id, False);
13483
13484 -- Generate a reference to link the visible subprogram instance to
13485 -- the generic body, which for navigation purposes is the only
13486 -- available source for the instance.
13487
13488 Generate_Reference
13489 (Related_Instance (Pack_Id),
13490 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
13491
13492 -- If it is a child unit, make the parent instance (which is an
13493 -- instance of the parent of the generic) visible. The parent
13494 -- instance is the prefix of the name of the generic unit.
13495
13496 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
13497 and then Nkind (Gen_Id) = N_Expanded_Name
13498 then
13499 Par_Ent := Entity (Prefix (Gen_Id));
13500 elsif Is_Child_Unit (Gen_Unit) then
13501 Par_Ent := Scope (Gen_Unit);
13502 end if;
13503
13504 if Present (Par_Ent) then
13505 Par_Vis := Is_Immediately_Visible (Par_Ent);
13506 Install_Parent (Par_Ent, In_Body => True);
13507 Par_Installed := True;
13508 end if;
13509
13510 -- Subprogram body is placed in the body of wrapper package,
13511 -- whose spec contains the subprogram declaration as well as
13512 -- the renaming declarations for the generic parameters.
13513
13514 Pack_Body :=
13515 Make_Package_Body (Loc,
13516 Defining_Unit_Name => New_Copy (Pack_Id),
13517 Declarations => New_List (Act_Body));
13518
13519 Set_Corresponding_Spec (Pack_Body, Pack_Id);
13520
13521 -- If the instantiation is a library unit, then build resulting
13522 -- compilation unit nodes for the instance. The declaration of
13523 -- the enclosing package is the grandparent of the subprogram
13524 -- declaration. First replace the instantiation node as the unit
13525 -- of the corresponding compilation.
13526
13527 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
13528 if Parent (Inst_Node) = Cunit (Main_Unit) then
13529 Set_Unit (Parent (Inst_Node), Inst_Node);
13530 Build_Instance_Compilation_Unit_Nodes
13531 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
13532 Analyze (Inst_Node);
13533 else
13534 Set_Parent (Pack_Body, Parent (Inst_Node));
13535 Analyze (Pack_Body);
13536 end if;
13537
13538 else
13539 Insert_Before (Inst_Node, Pack_Body);
13540 Mark_Rewrite_Insertion (Pack_Body);
13541
13542 -- Insert the freeze node for the instance if need be
13543
13544 if Expander_Active then
13545 Freeze_Subprogram_Instance (Inst_Node, Gen_Body, Pack_Id);
13546 end if;
13547
13548 Analyze (Pack_Body);
13549 end if;
13550
13551 Inherit_Context (Gen_Body, Inst_Node);
13552
13553 Restore_Private_Views (Pack_Id, False);
13554
13555 if Par_Installed then
13556 Remove_Parent (In_Body => True);
13557
13558 -- Restore the previous visibility of the parent
13559
13560 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
13561 end if;
13562
13563 Restore_Env;
13564
13565 -- Body not found. Error was emitted already. If there were no previous
13566 -- errors, this may be an instance whose scope is a premature instance.
13567 -- In that case we must insure that the (legal) program does raise
13568 -- program error if executed. We generate a subprogram body for this
13569 -- purpose.
13570
13571 elsif Serious_Errors_Detected = 0
13572 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
13573 then
13574 if Body_Optional then
13575 goto Leave;
13576
13577 elsif Ekind (Act_Decl_Id) = E_Procedure then
13578 Act_Body :=
13579 Make_Subprogram_Body (Loc,
13580 Specification =>
13581 Make_Procedure_Specification (Loc,
13582 Defining_Unit_Name =>
13583 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
13584 Parameter_Specifications =>
13585 New_Copy_List
13586 (Parameter_Specifications (Parent (Act_Decl_Id)))),
13587
13588 Declarations => Empty_List,
13589 Handled_Statement_Sequence =>
13590 Make_Handled_Sequence_Of_Statements (Loc,
13591 Statements => New_List (
13592 Make_Raise_Program_Error (Loc,
13593 Reason => PE_Access_Before_Elaboration))));
13594
13595 else
13596 Ret_Expr :=
13597 Make_Raise_Program_Error (Loc,
13598 Reason => PE_Access_Before_Elaboration);
13599
13600 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
13601 Set_Analyzed (Ret_Expr);
13602
13603 Act_Body :=
13604 Make_Subprogram_Body (Loc,
13605 Specification =>
13606 Make_Function_Specification (Loc,
13607 Defining_Unit_Name =>
13608 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
13609 Parameter_Specifications =>
13610 New_Copy_List
13611 (Parameter_Specifications (Parent (Act_Decl_Id))),
13612 Result_Definition =>
13613 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
13614
13615 Declarations => Empty_List,
13616 Handled_Statement_Sequence =>
13617 Make_Handled_Sequence_Of_Statements (Loc,
13618 Statements => New_List (
13619 Make_Simple_Return_Statement (Loc, Ret_Expr))));
13620 end if;
13621
13622 Pack_Body :=
13623 Make_Package_Body (Loc,
13624 Defining_Unit_Name => New_Copy (Pack_Id),
13625 Declarations => New_List (Act_Body));
13626
13627 Insert_After (Inst_Node, Pack_Body);
13628 Set_Corresponding_Spec (Pack_Body, Pack_Id);
13629 Analyze (Pack_Body);
13630 end if;
13631
13632 <<Leave>>
13633
13634 -- Restore the context that was in effect prior to instantiating the
13635 -- subprogram body.
13636
13637 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
13638 Local_Suppress_Stack_Top := Saved_LSST;
13639 Scope_Suppress := Saved_SS;
13640 Style_Check := Saved_SC;
13641
13642 Expander_Mode_Restore;
13643 Restore_Config_Switches (Saved_CS);
13644 Restore_Ghost_Region (Saved_GM, Saved_IGR);
13645 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
13646 Restore_Warnings (Saved_Warn);
13647 end Instantiate_Subprogram_Body;
13648
13649 ----------------------
13650 -- Instantiate_Type --
13651 ----------------------
13652
13653 function Instantiate_Type
13654 (Formal : Node_Id;
13655 Actual : Node_Id;
13656 Analyzed_Formal : Node_Id;
13657 Actual_Decls : List_Id) return List_Id
13658 is
13659 A_Gen_T : constant Entity_Id :=
13660 Defining_Identifier (Analyzed_Formal);
13661 Def : constant Node_Id := Formal_Type_Definition (Formal);
13662 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
13663 Act_T : Entity_Id;
13664 Ancestor : Entity_Id := Empty;
13665 Decl_Node : Node_Id;
13666 Decl_Nodes : List_Id; -- result
13667 Loc : Source_Ptr;
13668 Subt : Entity_Id;
13669
13670 procedure Check_Shared_Variable_Control_Aspects;
13671 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
13672 -- that may be specified for a formal type are obeyed by the actual.
13673
13674 procedure Diagnose_Predicated_Actual;
13675 -- There are a number of constructs in which a discrete type with
13676 -- predicates is illegal, e.g. as an index in an array type declaration.
13677 -- If a generic type is used is such a construct in a generic package
13678 -- declaration, it carries the flag No_Predicate_On_Actual. It is part
13679 -- of the generic contract that the actual cannot have predicates.
13680
13681 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
13682 -- Check that base types are the same and that the subtypes match
13683 -- statically. Used in several of the validation subprograms for
13684 -- actuals in instantiations.
13685
13686 procedure Validate_Array_Type_Instance;
13687 procedure Validate_Access_Subprogram_Instance;
13688 procedure Validate_Access_Type_Instance;
13689 procedure Validate_Derived_Type_Instance;
13690 procedure Validate_Derived_Interface_Type_Instance;
13691 procedure Validate_Discriminated_Formal_Type;
13692 procedure Validate_Interface_Type_Instance;
13693 procedure Validate_Private_Type_Instance;
13694 procedure Validate_Incomplete_Type_Instance;
13695 -- These procedures perform validation tests for the named case.
13696 -- Validate_Discriminated_Formal_Type is shared by formal private
13697 -- types and Ada 2012 formal incomplete types.
13698
13699 --------------------------------------------
13700 -- Check_Shared_Variable_Control_Aspects --
13701 --------------------------------------------
13702
13703 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
13704 -- that may be specified for the formal are obeyed by the actual.
13705 -- If the formal is a derived type the aspect specifications must match.
13706 -- NOTE: AI12-0282 implies that matching of aspects is required between
13707 -- formal and actual in all cases, but this is too restrictive.
13708 -- In particular it violates a language design rule: a limited private
13709 -- indefinite formal can be matched by any actual. The current code
13710 -- reflects an older and more permissive version of RM C.6 (12/5).
13711
13712 procedure Check_Shared_Variable_Control_Aspects is
13713 begin
13714 if Ada_Version >= Ada_2022 then
13715 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
13716 Error_Msg_NE
13717 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
13718
13719 elsif Is_Derived_Type (A_Gen_T)
13720 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
13721 then
13722 Error_Msg_NE
13723 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
13724 end if;
13725
13726 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
13727 Error_Msg_NE
13728 ("actual for& must have Volatile aspect",
13729 Actual, A_Gen_T);
13730
13731 elsif Is_Derived_Type (A_Gen_T)
13732 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
13733 then
13734 Error_Msg_NE
13735 ("actual for& has different Volatile aspect",
13736 Actual, A_Gen_T);
13737 end if;
13738
13739 -- We assume that an array type whose atomic component type
13740 -- is Atomic is equivalent to an array type with the explicit
13741 -- aspect Has_Atomic_Components. This is a reasonable inference
13742 -- from the intent of AI12-0282, and makes it legal to use an
13743 -- actual that does not have the identical aspect as the formal.
13744 -- Ditto for volatile components.
13745
13746 declare
13747 Actual_Atomic_Comp : constant Boolean :=
13748 Has_Atomic_Components (Act_T)
13749 or else (Is_Array_Type (Act_T)
13750 and then Is_Atomic (Component_Type (Act_T)));
13751 begin
13752 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
13753 Error_Msg_NE
13754 ("formal and actual for& must agree on atomic components",
13755 Actual, A_Gen_T);
13756 end if;
13757 end;
13758
13759 declare
13760 Actual_Volatile_Comp : constant Boolean :=
13761 Has_Volatile_Components (Act_T)
13762 or else (Is_Array_Type (Act_T)
13763 and then Is_Volatile (Component_Type (Act_T)));
13764 begin
13765 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
13766 then
13767 Error_Msg_NE
13768 ("actual for& must have volatile components",
13769 Actual, A_Gen_T);
13770 end if;
13771 end;
13772
13773 -- The following two aspects do not require exact matching,
13774 -- but only one-way agreement. See RM C.6.
13775
13776 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
13777 then
13778 Error_Msg_NE
13779 ("actual for& must have Independent aspect specified",
13780 Actual, A_Gen_T);
13781 end if;
13782
13783 if Has_Independent_Components (A_Gen_T)
13784 and then not Has_Independent_Components (Act_T)
13785 then
13786 Error_Msg_NE
13787 ("actual for& must have Independent_Components specified",
13788 Actual, A_Gen_T);
13789 end if;
13790 end if;
13791 end Check_Shared_Variable_Control_Aspects;
13792
13793 ---------------------------------
13794 -- Diagnose_Predicated_Actual --
13795 ---------------------------------
13796
13797 procedure Diagnose_Predicated_Actual is
13798 begin
13799 if No_Predicate_On_Actual (A_Gen_T)
13800 and then Has_Predicates (Act_T)
13801 then
13802 Error_Msg_NE
13803 ("actual for& cannot be a type with predicate",
13804 Instantiation_Node, A_Gen_T);
13805
13806 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
13807 and then Has_Predicates (Act_T)
13808 and then not Has_Static_Predicate_Aspect (Act_T)
13809 then
13810 Error_Msg_NE
13811 ("actual for& cannot be a type with a dynamic predicate",
13812 Instantiation_Node, A_Gen_T);
13813 end if;
13814 end Diagnose_Predicated_Actual;
13815
13816 --------------------
13817 -- Subtypes_Match --
13818 --------------------
13819
13820 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
13821 T : constant Entity_Id := Get_Instance_Of (Gen_T);
13822
13823 begin
13824 -- Check that the base types, root types (when dealing with class
13825 -- wide types), or designated types (when dealing with anonymous
13826 -- access types) of Gen_T and Act_T are statically matching subtypes.
13827
13828 return (Base_Type (Base_Type (T)) = Base_Type (Act_T)
13829 and then Subtypes_Statically_Match (T, Act_T))
13830
13831 or else (Is_Class_Wide_Type (Gen_T)
13832 and then Is_Class_Wide_Type (Act_T)
13833 and then Subtypes_Match
13834 (Get_Instance_Of (Root_Type (Gen_T)),
13835 Root_Type (Act_T)))
13836
13837 or else (Is_Anonymous_Access_Type (Gen_T)
13838 and then Ekind (Act_T) = Ekind (Gen_T)
13839 and then Subtypes_Statically_Match
13840 (Designated_Type (Gen_T), Designated_Type (Act_T)));
13841 end Subtypes_Match;
13842
13843 -----------------------------------------
13844 -- Validate_Access_Subprogram_Instance --
13845 -----------------------------------------
13846
13847 procedure Validate_Access_Subprogram_Instance is
13848 begin
13849 if not Is_Access_Type (Act_T)
13850 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
13851 then
13852 Error_Msg_NE
13853 ("expect access type in instantiation of &", Actual, Gen_T);
13854 Abandon_Instantiation (Actual);
13855 end if;
13856
13857 -- According to AI05-288, actuals for access_to_subprograms must be
13858 -- subtype conformant with the generic formal. Previous to AI05-288
13859 -- only mode conformance was required.
13860
13861 -- This is a binding interpretation that applies to previous versions
13862 -- of the language, no need to maintain previous weaker checks.
13863
13864 Check_Subtype_Conformant
13865 (Designated_Type (Act_T),
13866 Designated_Type (A_Gen_T),
13867 Actual,
13868 Get_Inst => True);
13869
13870 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
13871 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
13872 Error_Msg_NE
13873 ("protected access type not allowed for formal &",
13874 Actual, Gen_T);
13875 end if;
13876
13877 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
13878 Error_Msg_NE
13879 ("expect protected access type for formal &",
13880 Actual, Gen_T);
13881 end if;
13882
13883 -- If the formal has a specified convention (which in most cases
13884 -- will be StdCall) verify that the actual has the same convention.
13885
13886 if Has_Convention_Pragma (A_Gen_T)
13887 and then Convention (A_Gen_T) /= Convention (Act_T)
13888 then
13889 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
13890 Error_Msg_NE
13891 ("actual for formal & must have convention %", Actual, Gen_T);
13892 end if;
13893
13894 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13895 Error_Msg_NE
13896 ("non null exclusion of actual and formal & do not match",
13897 Actual, Gen_T);
13898 end if;
13899 end Validate_Access_Subprogram_Instance;
13900
13901 -----------------------------------
13902 -- Validate_Access_Type_Instance --
13903 -----------------------------------
13904
13905 procedure Validate_Access_Type_Instance is
13906 Desig_Type : constant Entity_Id :=
13907 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
13908 Desig_Act : Entity_Id;
13909
13910 begin
13911 if not Is_Access_Type (Act_T) then
13912 Error_Msg_NE
13913 ("expect access type in instantiation of &", Actual, Gen_T);
13914 Abandon_Instantiation (Actual);
13915 end if;
13916
13917 if Is_Access_Constant (A_Gen_T) then
13918 if not Is_Access_Constant (Act_T) then
13919 Error_Msg_N
13920 ("actual type must be access-to-constant type", Actual);
13921 Abandon_Instantiation (Actual);
13922 end if;
13923 else
13924 if Is_Access_Constant (Act_T) then
13925 Error_Msg_N
13926 ("actual type must be access-to-variable type", Actual);
13927 Abandon_Instantiation (Actual);
13928
13929 elsif Ekind (A_Gen_T) = E_General_Access_Type
13930 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
13931 then
13932 Error_Msg_N
13933 ("actual must be general access type!", Actual);
13934 Error_Msg_NE -- CODEFIX
13935 ("\add ALL to }!", Actual, Act_T);
13936 Abandon_Instantiation (Actual);
13937 end if;
13938 end if;
13939
13940 -- The designated subtypes, that is to say the subtypes introduced
13941 -- by an access type declaration (and not by a subtype declaration)
13942 -- must match.
13943
13944 Desig_Act := Designated_Type (Base_Type (Act_T));
13945
13946 -- The designated type may have been introduced through a limited_
13947 -- with clause, in which case retrieve the non-limited view. This
13948 -- applies to incomplete types as well as to class-wide types.
13949
13950 if From_Limited_With (Desig_Act) then
13951 Desig_Act := Available_View (Desig_Act);
13952 end if;
13953
13954 if not Subtypes_Match (Desig_Type, Desig_Act) then
13955 Error_Msg_NE
13956 ("designated type of actual does not match that of formal &",
13957 Actual, Gen_T);
13958
13959 if not Predicates_Match (Desig_Type, Desig_Act) then
13960 Error_Msg_N ("\predicates do not match", Actual);
13961 end if;
13962
13963 Abandon_Instantiation (Actual);
13964 end if;
13965
13966 -- Ada 2005: null-exclusion indicators of the two types must agree
13967
13968 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13969 Error_Msg_NE
13970 ("non null exclusion of actual and formal & do not match",
13971 Actual, Gen_T);
13972 end if;
13973 end Validate_Access_Type_Instance;
13974
13975 ----------------------------------
13976 -- Validate_Array_Type_Instance --
13977 ----------------------------------
13978
13979 procedure Validate_Array_Type_Instance is
13980 I1 : Node_Id;
13981 I2 : Node_Id;
13982 T2 : Entity_Id;
13983
13984 function Formal_Dimensions return Nat;
13985 -- Count number of dimensions in array type formal
13986
13987 -----------------------
13988 -- Formal_Dimensions --
13989 -----------------------
13990
13991 function Formal_Dimensions return Nat is
13992 Num : Nat := 0;
13993 Index : Node_Id;
13994
13995 begin
13996 if Nkind (Def) = N_Constrained_Array_Definition then
13997 Index := First (Discrete_Subtype_Definitions (Def));
13998 else
13999 Index := First (Subtype_Marks (Def));
14000 end if;
14001
14002 while Present (Index) loop
14003 Num := Num + 1;
14004 Next (Index);
14005 end loop;
14006
14007 return Num;
14008 end Formal_Dimensions;
14009
14010 -- Start of processing for Validate_Array_Type_Instance
14011
14012 begin
14013 if not Is_Array_Type (Act_T) then
14014 Error_Msg_NE
14015 ("expect array type in instantiation of &", Actual, Gen_T);
14016 Abandon_Instantiation (Actual);
14017
14018 elsif Nkind (Def) = N_Constrained_Array_Definition then
14019 if not Is_Constrained (Act_T) then
14020 Error_Msg_NE
14021 ("expect constrained array in instantiation of &",
14022 Actual, Gen_T);
14023 Abandon_Instantiation (Actual);
14024 end if;
14025
14026 else
14027 if Is_Constrained (Act_T) then
14028 Error_Msg_NE
14029 ("expect unconstrained array in instantiation of &",
14030 Actual, Gen_T);
14031 Abandon_Instantiation (Actual);
14032 end if;
14033 end if;
14034
14035 if Formal_Dimensions /= Number_Dimensions (Act_T) then
14036 Error_Msg_NE
14037 ("dimensions of actual do not match formal &", Actual, Gen_T);
14038 Abandon_Instantiation (Actual);
14039 end if;
14040
14041 I1 := First_Index (A_Gen_T);
14042 I2 := First_Index (Act_T);
14043 for J in 1 .. Formal_Dimensions loop
14044
14045 -- If the indexes of the actual were given by a subtype_mark,
14046 -- the index was transformed into a range attribute. Retrieve
14047 -- the original type mark for checking.
14048
14049 if Is_Entity_Name (Original_Node (I2)) then
14050 T2 := Entity (Original_Node (I2));
14051 else
14052 T2 := Etype (I2);
14053 end if;
14054
14055 if not Subtypes_Match
14056 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
14057 then
14058 Error_Msg_NE
14059 ("index types of actual do not match those of formal &",
14060 Actual, Gen_T);
14061 Abandon_Instantiation (Actual);
14062 end if;
14063
14064 Next_Index (I1);
14065 Next_Index (I2);
14066 end loop;
14067
14068 -- Check matching subtypes. Note that there are complex visibility
14069 -- issues when the generic is a child unit and some aspect of the
14070 -- generic type is declared in a parent unit of the generic. We do
14071 -- the test to handle this special case only after a direct check
14072 -- for static matching has failed. The case where both the component
14073 -- type and the array type are separate formals, and the component
14074 -- type is a private view may also require special checking in
14075 -- Subtypes_Match. Finally, we assume that a child instance where
14076 -- the component type comes from a formal of a parent instance is
14077 -- correct because the generic was correct. A more precise check
14078 -- seems too complex to install???
14079
14080 if Subtypes_Match
14081 (Component_Type (A_Gen_T), Component_Type (Act_T))
14082 or else
14083 Subtypes_Match
14084 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
14085 Component_Type (Act_T))
14086 or else
14087 (not Inside_A_Generic
14088 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
14089 then
14090 null;
14091 else
14092 Error_Msg_NE
14093 ("component subtype of actual does not match that of formal &",
14094 Actual, Gen_T);
14095 Abandon_Instantiation (Actual);
14096 end if;
14097
14098 if Has_Aliased_Components (A_Gen_T)
14099 and then not Has_Aliased_Components (Act_T)
14100 then
14101 Error_Msg_NE
14102 ("actual must have aliased components to match formal type &",
14103 Actual, Gen_T);
14104 end if;
14105 end Validate_Array_Type_Instance;
14106
14107 -----------------------------------------------
14108 -- Validate_Derived_Interface_Type_Instance --
14109 -----------------------------------------------
14110
14111 procedure Validate_Derived_Interface_Type_Instance is
14112 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
14113 Elmt : Elmt_Id;
14114
14115 begin
14116 -- First apply interface instance checks
14117
14118 Validate_Interface_Type_Instance;
14119
14120 -- Verify that immediate parent interface is an ancestor of
14121 -- the actual.
14122
14123 if Present (Par)
14124 and then not Interface_Present_In_Ancestor (Act_T, Par)
14125 then
14126 Error_Msg_NE
14127 ("interface actual must include progenitor&", Actual, Par);
14128 end if;
14129
14130 -- Now verify that the actual includes all other ancestors of
14131 -- the formal.
14132
14133 Elmt := First_Elmt (Interfaces (A_Gen_T));
14134 while Present (Elmt) loop
14135 if not Interface_Present_In_Ancestor
14136 (Act_T, Get_Instance_Of (Node (Elmt)))
14137 then
14138 Error_Msg_NE
14139 ("interface actual must include progenitor&",
14140 Actual, Node (Elmt));
14141 end if;
14142
14143 Next_Elmt (Elmt);
14144 end loop;
14145 end Validate_Derived_Interface_Type_Instance;
14146
14147 ------------------------------------
14148 -- Validate_Derived_Type_Instance --
14149 ------------------------------------
14150
14151 procedure Validate_Derived_Type_Instance is
14152 Actual_Discr : Entity_Id;
14153 Ancestor_Discr : Entity_Id;
14154
14155 begin
14156 -- Verify that the actual includes the progenitors of the formal,
14157 -- if any. The formal may depend on previous formals and their
14158 -- instance, so we must examine instance of interfaces if present.
14159 -- The actual may be an extension of an interface, in which case
14160 -- it does not appear in the interface list, so this must be
14161 -- checked separately.
14162
14163 if Present (Interface_List (Def)) then
14164 if not Has_Interfaces (Act_T) then
14165 Error_Msg_NE
14166 ("actual must implement all interfaces of formal&",
14167 Actual, A_Gen_T);
14168
14169 else
14170 declare
14171 Act_Iface_List : Elist_Id;
14172 Iface : Node_Id;
14173 Iface_Ent : Entity_Id;
14174
14175 function Instance_Exists (I : Entity_Id) return Boolean;
14176 -- If the interface entity is declared in a generic unit,
14177 -- this can only be legal if we are within an instantiation
14178 -- of a child of that generic. There is currently no
14179 -- mechanism to relate an interface declared within a
14180 -- generic to the corresponding interface in an instance,
14181 -- so we traverse the list of interfaces of the actual,
14182 -- looking for a name match.
14183
14184 ---------------------
14185 -- Instance_Exists --
14186 ---------------------
14187
14188 function Instance_Exists (I : Entity_Id) return Boolean is
14189 Iface_Elmt : Elmt_Id;
14190
14191 begin
14192 Iface_Elmt := First_Elmt (Act_Iface_List);
14193 while Present (Iface_Elmt) loop
14194 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
14195 and then Chars (Node (Iface_Elmt)) = Chars (I)
14196 then
14197 return True;
14198 end if;
14199
14200 Next_Elmt (Iface_Elmt);
14201 end loop;
14202
14203 return False;
14204 end Instance_Exists;
14205
14206 begin
14207 Iface := First (Abstract_Interface_List (A_Gen_T));
14208 Collect_Interfaces (Act_T, Act_Iface_List);
14209
14210 while Present (Iface) loop
14211 Iface_Ent := Get_Instance_Of (Entity (Iface));
14212
14213 if Is_Ancestor (Iface_Ent, Act_T)
14214 or else Is_Progenitor (Iface_Ent, Act_T)
14215 then
14216 null;
14217
14218 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
14219 and then Instance_Exists (Iface_Ent)
14220 then
14221 null;
14222
14223 else
14224 Error_Msg_Name_1 := Chars (Act_T);
14225 Error_Msg_NE
14226 ("actual% must implement interface&",
14227 Actual, Etype (Iface));
14228 end if;
14229
14230 Next (Iface);
14231 end loop;
14232 end;
14233 end if;
14234 end if;
14235
14236 -- If the parent type in the generic declaration is itself a previous
14237 -- formal type, then it is local to the generic and absent from the
14238 -- analyzed generic definition. In that case the ancestor is the
14239 -- instance of the formal (which must have been instantiated
14240 -- previously), unless the ancestor is itself a formal derived type.
14241 -- In this latter case (which is the subject of Corrigendum 8652/0038
14242 -- (AI-202) the ancestor of the formals is the ancestor of its
14243 -- parent. Otherwise, the analyzed generic carries the parent type.
14244 -- If the parent type is defined in a previous formal package, then
14245 -- the scope of that formal package is that of the generic type
14246 -- itself, and it has already been mapped into the corresponding type
14247 -- in the actual package.
14248
14249 -- Common case: parent type defined outside of the generic
14250
14251 if Is_Entity_Name (Subtype_Mark (Def))
14252 and then Present (Entity (Subtype_Mark (Def)))
14253 then
14254 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
14255
14256 -- Check whether parent is defined in a previous formal package
14257
14258 elsif
14259 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
14260 then
14261 Ancestor :=
14262 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
14263
14264 -- The type may be a local derivation, or a type extension of a
14265 -- previous formal, or of a formal of a parent package.
14266
14267 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
14268 or else
14269 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
14270 then
14271 -- Check whether the parent is another formal derived type in the
14272 -- same generic unit.
14273
14274 if Etype (A_Gen_T) /= A_Gen_T
14275 and then Is_Generic_Type (Etype (A_Gen_T))
14276 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
14277 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
14278 then
14279 -- Locate ancestor of parent from the subtype declaration
14280 -- created for the actual.
14281
14282 declare
14283 Decl : Node_Id;
14284
14285 begin
14286 Decl := First (Actual_Decls);
14287 while Present (Decl) loop
14288 if Nkind (Decl) = N_Subtype_Declaration
14289 and then Chars (Defining_Identifier (Decl)) =
14290 Chars (Etype (A_Gen_T))
14291 then
14292 Ancestor := Generic_Parent_Type (Decl);
14293 exit;
14294 else
14295 Next (Decl);
14296 end if;
14297 end loop;
14298 end;
14299
14300 pragma Assert (Present (Ancestor));
14301
14302 -- The ancestor itself may be a previous formal that has been
14303 -- instantiated.
14304
14305 Ancestor := Get_Instance_Of (Ancestor);
14306
14307 else
14308 Ancestor := Get_Instance_Of (Etype (Get_Instance_Of (A_Gen_T)));
14309 end if;
14310
14311 -- Check whether parent is a previous formal of the current generic
14312
14313 elsif Is_Derived_Type (A_Gen_T)
14314 and then Is_Generic_Type (Etype (A_Gen_T))
14315 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
14316 then
14317 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
14318
14319 -- An unusual case: the actual is a type declared in a parent unit,
14320 -- but is not a formal type so there is no instance_of for it.
14321 -- Retrieve it by analyzing the record extension.
14322
14323 elsif Is_Child_Unit (Scope (A_Gen_T))
14324 and then In_Open_Scopes (Scope (Act_T))
14325 and then Is_Generic_Instance (Scope (Act_T))
14326 then
14327 Analyze (Subtype_Mark (Def));
14328 Ancestor := Entity (Subtype_Mark (Def));
14329
14330 else
14331 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
14332 end if;
14333
14334 -- If the formal derived type has pragma Preelaborable_Initialization
14335 -- then the actual type must have preelaborable initialization.
14336
14337 if Known_To_Have_Preelab_Init (A_Gen_T)
14338 and then not Has_Preelaborable_Initialization (Act_T)
14339 then
14340 Error_Msg_NE
14341 ("actual for & must have preelaborable initialization",
14342 Actual, Gen_T);
14343 end if;
14344
14345 -- Ada 2005 (AI-251)
14346
14347 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
14348 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
14349 Error_Msg_NE
14350 ("(Ada 2005) expected type implementing & in instantiation",
14351 Actual, Ancestor);
14352 end if;
14353
14354 -- Finally verify that the (instance of) the ancestor is an ancestor
14355 -- of the actual.
14356
14357 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
14358 Error_Msg_NE
14359 ("expect type derived from & in instantiation",
14360 Actual, First_Subtype (Ancestor));
14361 Abandon_Instantiation (Actual);
14362 end if;
14363
14364 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
14365 -- that the formal type declaration has been rewritten as a private
14366 -- extension.
14367
14368 if Ada_Version >= Ada_2005
14369 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
14370 and then Synchronized_Present (Parent (A_Gen_T))
14371 then
14372 -- The actual must be a synchronized tagged type
14373
14374 if not Is_Tagged_Type (Act_T) then
14375 Error_Msg_N
14376 ("actual of synchronized type must be tagged", Actual);
14377 Abandon_Instantiation (Actual);
14378
14379 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
14380 and then Nkind (Type_Definition (Parent (Act_T))) =
14381 N_Derived_Type_Definition
14382 and then not Synchronized_Present
14383 (Type_Definition (Parent (Act_T)))
14384 then
14385 Error_Msg_N
14386 ("actual of synchronized type must be synchronized", Actual);
14387 Abandon_Instantiation (Actual);
14388 end if;
14389 end if;
14390
14391 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
14392 -- removes the second instance of the phrase "or allow pass by copy".
14393
14394 -- For Ada 2022, the aspect may be specified explicitly for the
14395 -- formal regardless of whether an ancestor obeys it.
14396
14397 if Is_Atomic (Act_T)
14398 and then not Is_Atomic (Ancestor)
14399 and then not Is_Atomic (A_Gen_T)
14400 then
14401 Error_Msg_N
14402 ("cannot have atomic actual type for non-atomic formal type",
14403 Actual);
14404
14405 elsif Is_Volatile (Act_T)
14406 and then not Is_Volatile (Ancestor)
14407 and then not Is_Volatile (A_Gen_T)
14408 then
14409 Error_Msg_N
14410 ("cannot have volatile actual type for non-volatile formal type",
14411 Actual);
14412 end if;
14413
14414 -- It should not be necessary to check for unknown discriminants on
14415 -- Formal, but for some reason Has_Unknown_Discriminants is false for
14416 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
14417 -- needs fixing. ???
14418
14419 if Is_Definite_Subtype (A_Gen_T)
14420 and then not Unknown_Discriminants_Present (Formal)
14421 and then not Is_Definite_Subtype (Act_T)
14422 then
14423 Error_Msg_N ("actual subtype must be constrained", Actual);
14424 Abandon_Instantiation (Actual);
14425 end if;
14426
14427 if not Unknown_Discriminants_Present (Formal) then
14428 if Is_Constrained (Ancestor) then
14429 if not Is_Constrained (Act_T) then
14430 Error_Msg_N ("actual subtype must be constrained", Actual);
14431 Abandon_Instantiation (Actual);
14432 end if;
14433
14434 -- Ancestor is unconstrained, Check if generic formal and actual
14435 -- agree on constrainedness. The check only applies to array types
14436 -- and discriminated types.
14437
14438 elsif Is_Constrained (Act_T) then
14439 if Ekind (Ancestor) = E_Access_Type
14440 or else (not Is_Constrained (A_Gen_T)
14441 and then Is_Composite_Type (A_Gen_T))
14442 then
14443 Error_Msg_N ("actual subtype must be unconstrained", Actual);
14444 Abandon_Instantiation (Actual);
14445 end if;
14446
14447 -- A class-wide type is only allowed if the formal has unknown
14448 -- discriminants.
14449
14450 elsif Is_Class_Wide_Type (Act_T)
14451 and then not Has_Unknown_Discriminants (Ancestor)
14452 then
14453 Error_Msg_NE
14454 ("actual for & cannot be a class-wide type", Actual, Gen_T);
14455 Abandon_Instantiation (Actual);
14456
14457 -- Otherwise, the formal and actual must have the same number
14458 -- of discriminants and each discriminant of the actual must
14459 -- correspond to a discriminant of the formal.
14460
14461 elsif Has_Discriminants (Act_T)
14462 and then not Has_Unknown_Discriminants (Act_T)
14463 and then Has_Discriminants (Ancestor)
14464 then
14465 Actual_Discr := First_Discriminant (Act_T);
14466 Ancestor_Discr := First_Discriminant (Ancestor);
14467 while Present (Actual_Discr)
14468 and then Present (Ancestor_Discr)
14469 loop
14470 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
14471 No (Corresponding_Discriminant (Actual_Discr))
14472 then
14473 Error_Msg_NE
14474 ("discriminant & does not correspond "
14475 & "to ancestor discriminant", Actual, Actual_Discr);
14476 Abandon_Instantiation (Actual);
14477 end if;
14478
14479 Next_Discriminant (Actual_Discr);
14480 Next_Discriminant (Ancestor_Discr);
14481 end loop;
14482
14483 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
14484 Error_Msg_NE
14485 ("actual for & must have same number of discriminants",
14486 Actual, Gen_T);
14487 Abandon_Instantiation (Actual);
14488 end if;
14489
14490 -- This case should be caught by the earlier check for
14491 -- constrainedness, but the check here is added for completeness.
14492
14493 elsif Has_Discriminants (Act_T)
14494 and then not Has_Unknown_Discriminants (Act_T)
14495 then
14496 Error_Msg_NE
14497 ("actual for & must not have discriminants", Actual, Gen_T);
14498 Abandon_Instantiation (Actual);
14499
14500 elsif Has_Discriminants (Ancestor) then
14501 Error_Msg_NE
14502 ("actual for & must have known discriminants", Actual, Gen_T);
14503 Abandon_Instantiation (Actual);
14504 end if;
14505
14506 if not Subtypes_Statically_Compatible
14507 (Act_T, Ancestor, Formal_Derived_Matching => True)
14508 then
14509 Error_Msg_NE
14510 ("actual for & must be statically compatible with ancestor",
14511 Actual, Gen_T);
14512
14513 if not Predicates_Compatible (Act_T, Ancestor) then
14514 Error_Msg_N
14515 ("\predicate on actual is not compatible with ancestor",
14516 Actual);
14517 end if;
14518
14519 Abandon_Instantiation (Actual);
14520 end if;
14521 end if;
14522
14523 -- If the formal and actual types are abstract, check that there
14524 -- are no abstract primitives of the actual type that correspond to
14525 -- nonabstract primitives of the formal type (second sentence of
14526 -- RM95 3.9.3(9)).
14527
14528 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
14529 Check_Abstract_Primitives : declare
14530 Gen_Prims : constant Elist_Id :=
14531 Primitive_Operations (A_Gen_T);
14532 Gen_Elmt : Elmt_Id;
14533 Gen_Subp : Entity_Id;
14534 Anc_Subp : Entity_Id;
14535 Anc_Formal : Entity_Id;
14536 Anc_F_Type : Entity_Id;
14537
14538 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
14539 Act_Elmt : Elmt_Id;
14540 Act_Subp : Entity_Id;
14541 Act_Formal : Entity_Id;
14542 Act_F_Type : Entity_Id;
14543
14544 Subprograms_Correspond : Boolean;
14545
14546 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
14547 -- Returns true if T2 is derived directly or indirectly from
14548 -- T1, including derivations from interfaces. T1 and T2 are
14549 -- required to be specific tagged base types.
14550
14551 ------------------------
14552 -- Is_Tagged_Ancestor --
14553 ------------------------
14554
14555 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
14556 is
14557 Intfc_Elmt : Elmt_Id;
14558
14559 begin
14560 -- The predicate is satisfied if the types are the same
14561
14562 if T1 = T2 then
14563 return True;
14564
14565 -- If we've reached the top of the derivation chain then
14566 -- we know that T1 is not an ancestor of T2.
14567
14568 elsif Etype (T2) = T2 then
14569 return False;
14570
14571 -- Proceed to check T2's immediate parent
14572
14573 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
14574 return True;
14575
14576 -- Finally, check to see if T1 is an ancestor of any of T2's
14577 -- progenitors.
14578
14579 else
14580 Intfc_Elmt := First_Elmt (Interfaces (T2));
14581 while Present (Intfc_Elmt) loop
14582 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
14583 return True;
14584 end if;
14585
14586 Next_Elmt (Intfc_Elmt);
14587 end loop;
14588 end if;
14589
14590 return False;
14591 end Is_Tagged_Ancestor;
14592
14593 -- Start of processing for Check_Abstract_Primitives
14594
14595 begin
14596 -- Loop over all of the formal derived type's primitives
14597
14598 Gen_Elmt := First_Elmt (Gen_Prims);
14599 while Present (Gen_Elmt) loop
14600 Gen_Subp := Node (Gen_Elmt);
14601
14602 -- If the primitive of the formal is not abstract, then
14603 -- determine whether there is a corresponding primitive of
14604 -- the actual type that's abstract.
14605
14606 if not Is_Abstract_Subprogram (Gen_Subp) then
14607 Act_Elmt := First_Elmt (Act_Prims);
14608 while Present (Act_Elmt) loop
14609 Act_Subp := Node (Act_Elmt);
14610
14611 -- If we find an abstract primitive of the actual,
14612 -- then we need to test whether it corresponds to the
14613 -- subprogram from which the generic formal primitive
14614 -- is inherited.
14615
14616 if Is_Abstract_Subprogram (Act_Subp) then
14617 Anc_Subp := Alias (Gen_Subp);
14618
14619 -- Test whether we have a corresponding primitive
14620 -- by comparing names, kinds, formal types, and
14621 -- result types.
14622
14623 if Chars (Anc_Subp) = Chars (Act_Subp)
14624 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
14625 then
14626 Anc_Formal := First_Formal (Anc_Subp);
14627 Act_Formal := First_Formal (Act_Subp);
14628 while Present (Anc_Formal)
14629 and then Present (Act_Formal)
14630 loop
14631 Anc_F_Type := Etype (Anc_Formal);
14632 Act_F_Type := Etype (Act_Formal);
14633
14634 if Ekind (Anc_F_Type) =
14635 E_Anonymous_Access_Type
14636 then
14637 Anc_F_Type := Designated_Type (Anc_F_Type);
14638
14639 if Ekind (Act_F_Type) =
14640 E_Anonymous_Access_Type
14641 then
14642 Act_F_Type :=
14643 Designated_Type (Act_F_Type);
14644 else
14645 exit;
14646 end if;
14647
14648 elsif
14649 Ekind (Act_F_Type) = E_Anonymous_Access_Type
14650 then
14651 exit;
14652 end if;
14653
14654 Anc_F_Type := Base_Type (Anc_F_Type);
14655 Act_F_Type := Base_Type (Act_F_Type);
14656
14657 -- If the formal is controlling, then the
14658 -- the type of the actual primitive's formal
14659 -- must be derived directly or indirectly
14660 -- from the type of the ancestor primitive's
14661 -- formal.
14662
14663 if Is_Controlling_Formal (Anc_Formal) then
14664 if not Is_Tagged_Ancestor
14665 (Anc_F_Type, Act_F_Type)
14666 then
14667 exit;
14668 end if;
14669
14670 -- Otherwise the types of the formals must
14671 -- be the same.
14672
14673 elsif Anc_F_Type /= Act_F_Type then
14674 exit;
14675 end if;
14676
14677 Next_Formal (Anc_Formal);
14678 Next_Formal (Act_Formal);
14679 end loop;
14680
14681 -- If we traversed through all of the formals
14682 -- then so far the subprograms correspond, so
14683 -- now check that any result types correspond.
14684
14685 if No (Anc_Formal) and then No (Act_Formal) then
14686 Subprograms_Correspond := True;
14687
14688 if Ekind (Act_Subp) = E_Function then
14689 Anc_F_Type := Etype (Anc_Subp);
14690 Act_F_Type := Etype (Act_Subp);
14691
14692 if Ekind (Anc_F_Type) =
14693 E_Anonymous_Access_Type
14694 then
14695 Anc_F_Type :=
14696 Designated_Type (Anc_F_Type);
14697
14698 if Ekind (Act_F_Type) =
14699 E_Anonymous_Access_Type
14700 then
14701 Act_F_Type :=
14702 Designated_Type (Act_F_Type);
14703 else
14704 Subprograms_Correspond := False;
14705 end if;
14706
14707 elsif
14708 Ekind (Act_F_Type)
14709 = E_Anonymous_Access_Type
14710 then
14711 Subprograms_Correspond := False;
14712 end if;
14713
14714 Anc_F_Type := Base_Type (Anc_F_Type);
14715 Act_F_Type := Base_Type (Act_F_Type);
14716
14717 -- Now either the result types must be
14718 -- the same or, if the result type is
14719 -- controlling, the result type of the
14720 -- actual primitive must descend from the
14721 -- result type of the ancestor primitive.
14722
14723 if Subprograms_Correspond
14724 and then Anc_F_Type /= Act_F_Type
14725 and then
14726 Has_Controlling_Result (Anc_Subp)
14727 and then not Is_Tagged_Ancestor
14728 (Anc_F_Type, Act_F_Type)
14729 then
14730 Subprograms_Correspond := False;
14731 end if;
14732 end if;
14733
14734 -- Found a matching subprogram belonging to
14735 -- formal ancestor type, so actual subprogram
14736 -- corresponds and this violates 3.9.3(9).
14737
14738 if Subprograms_Correspond then
14739 Error_Msg_NE
14740 ("abstract subprogram & overrides "
14741 & "nonabstract subprogram of ancestor",
14742 Actual, Act_Subp);
14743 end if;
14744 end if;
14745 end if;
14746 end if;
14747
14748 Next_Elmt (Act_Elmt);
14749 end loop;
14750 end if;
14751
14752 Next_Elmt (Gen_Elmt);
14753 end loop;
14754 end Check_Abstract_Primitives;
14755 end if;
14756
14757 -- Verify that limitedness matches. If parent is a limited
14758 -- interface then the generic formal is not unless declared
14759 -- explicitly so. If not declared limited, the actual cannot be
14760 -- limited (see AI05-0087).
14761
14762 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
14763 if not In_Instance then
14764 Error_Msg_NE
14765 ("actual for non-limited & cannot be a limited type",
14766 Actual, Gen_T);
14767 Explain_Limited_Type (Act_T, Actual);
14768 Abandon_Instantiation (Actual);
14769 end if;
14770 end if;
14771
14772 -- Check for AI12-0036
14773
14774 declare
14775 Formal_Is_Private_Extension : constant Boolean :=
14776 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
14777
14778 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
14779
14780 begin
14781 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
14782 if not In_Instance then
14783 if Actual_Is_Tagged then
14784 Error_Msg_NE
14785 ("actual for & cannot be a tagged type", Actual, Gen_T);
14786 else
14787 Error_Msg_NE
14788 ("actual for & must be a tagged type", Actual, Gen_T);
14789 end if;
14790
14791 Abandon_Instantiation (Actual);
14792 end if;
14793 end if;
14794 end;
14795 end Validate_Derived_Type_Instance;
14796
14797 ----------------------------------------
14798 -- Validate_Discriminated_Formal_Type --
14799 ----------------------------------------
14800
14801 procedure Validate_Discriminated_Formal_Type is
14802 Formal_Discr : Entity_Id;
14803 Actual_Discr : Entity_Id;
14804 Formal_Subt : Entity_Id;
14805
14806 begin
14807 if Has_Discriminants (A_Gen_T) then
14808 if not Has_Discriminants (Act_T) then
14809 Error_Msg_NE
14810 ("actual for & must have discriminants", Actual, Gen_T);
14811 Abandon_Instantiation (Actual);
14812
14813 elsif Is_Constrained (Act_T) then
14814 Error_Msg_NE
14815 ("actual for & must be unconstrained", Actual, Gen_T);
14816 Abandon_Instantiation (Actual);
14817
14818 else
14819 Formal_Discr := First_Discriminant (A_Gen_T);
14820 Actual_Discr := First_Discriminant (Act_T);
14821 while Formal_Discr /= Empty loop
14822 if Actual_Discr = Empty then
14823 Error_Msg_N
14824 ("discriminants on actual do not match formal",
14825 Actual);
14826 Abandon_Instantiation (Actual);
14827 end if;
14828
14829 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
14830
14831 -- Access discriminants match if designated types do
14832
14833 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
14834 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
14835 E_Anonymous_Access_Type
14836 and then
14837 Subtypes_Match
14838 (Get_Instance_Of
14839 (Designated_Type (Base_Type (Formal_Subt))),
14840 Designated_Type (Base_Type (Etype (Actual_Discr))))
14841 then
14842 null;
14843
14844 elsif Base_Type (Formal_Subt) /=
14845 Base_Type (Etype (Actual_Discr))
14846 then
14847 Error_Msg_N
14848 ("types of actual discriminants must match formal",
14849 Actual);
14850 Abandon_Instantiation (Actual);
14851
14852 elsif not Subtypes_Statically_Match
14853 (Formal_Subt, Etype (Actual_Discr))
14854 and then Ada_Version >= Ada_95
14855 then
14856 Error_Msg_N
14857 ("subtypes of actual discriminants must match formal",
14858 Actual);
14859 Abandon_Instantiation (Actual);
14860 end if;
14861
14862 Next_Discriminant (Formal_Discr);
14863 Next_Discriminant (Actual_Discr);
14864 end loop;
14865
14866 if Actual_Discr /= Empty then
14867 Error_Msg_NE
14868 ("discriminants on actual do not match formal",
14869 Actual, Gen_T);
14870 Abandon_Instantiation (Actual);
14871 end if;
14872 end if;
14873 end if;
14874 end Validate_Discriminated_Formal_Type;
14875
14876 ---------------------------------------
14877 -- Validate_Incomplete_Type_Instance --
14878 ---------------------------------------
14879
14880 procedure Validate_Incomplete_Type_Instance is
14881 begin
14882 if not Is_Tagged_Type (Act_T)
14883 and then Is_Tagged_Type (A_Gen_T)
14884 then
14885 Error_Msg_NE
14886 ("actual for & must be a tagged type", Actual, Gen_T);
14887 end if;
14888
14889 Validate_Discriminated_Formal_Type;
14890 end Validate_Incomplete_Type_Instance;
14891
14892 --------------------------------------
14893 -- Validate_Interface_Type_Instance --
14894 --------------------------------------
14895
14896 procedure Validate_Interface_Type_Instance is
14897 begin
14898 if not Is_Interface (Act_T) then
14899 Error_Msg_NE
14900 ("actual for formal interface type must be an interface",
14901 Actual, Gen_T);
14902
14903 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
14904 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
14905 or else Is_Protected_Interface (A_Gen_T) /=
14906 Is_Protected_Interface (Act_T)
14907 or else Is_Synchronized_Interface (A_Gen_T) /=
14908 Is_Synchronized_Interface (Act_T)
14909 then
14910 Error_Msg_NE
14911 ("actual for interface& does not match (RM 12.5.5(4))",
14912 Actual, Gen_T);
14913 end if;
14914 end Validate_Interface_Type_Instance;
14915
14916 ------------------------------------
14917 -- Validate_Private_Type_Instance --
14918 ------------------------------------
14919
14920 procedure Validate_Private_Type_Instance is
14921 begin
14922 if Is_Limited_Type (Act_T)
14923 and then not Is_Limited_Type (A_Gen_T)
14924 then
14925 if In_Instance then
14926 null;
14927 else
14928 Error_Msg_NE
14929 ("actual for non-limited & cannot be a limited type", Actual,
14930 Gen_T);
14931 Explain_Limited_Type (Act_T, Actual);
14932 Abandon_Instantiation (Actual);
14933 end if;
14934
14935 elsif Known_To_Have_Preelab_Init (A_Gen_T)
14936 and then not Has_Preelaborable_Initialization (Act_T)
14937 then
14938 Error_Msg_NE
14939 ("actual for & must have preelaborable initialization", Actual,
14940 Gen_T);
14941
14942 elsif not Is_Definite_Subtype (Act_T)
14943 and then Is_Definite_Subtype (A_Gen_T)
14944 and then No (Class_Wide_Equivalent_Type (Act_T))
14945 and then Ada_Version >= Ada_95
14946 then
14947 Error_Msg_NE
14948 ("actual for & must be a definite subtype", Actual, Gen_T);
14949
14950 elsif not Is_Tagged_Type (Act_T)
14951 and then Is_Tagged_Type (A_Gen_T)
14952 then
14953 Error_Msg_NE
14954 ("actual for & must be a tagged type", Actual, Gen_T);
14955
14956 -- For generic formal tagged types with the First_Controlling_Param
14957 -- aspect, ensure that the actual type also has this aspect.
14958
14959 elsif Is_Tagged_Type (Act_T)
14960 and then Is_Tagged_Type (A_Gen_T)
14961 and then not Has_First_Controlling_Parameter_Aspect (Act_T)
14962 and then Has_First_Controlling_Parameter_Aspect (A_Gen_T)
14963 then
14964 Error_Msg_NE
14965 ("actual for & must be a 'First_'Controlling_'Parameter tagged "
14966 & "type", Actual, Gen_T);
14967 end if;
14968
14969 Validate_Discriminated_Formal_Type;
14970 Ancestor := Gen_T;
14971 end Validate_Private_Type_Instance;
14972
14973 -- Start of processing for Instantiate_Type
14974
14975 begin
14976 if not Is_Entity_Name (Actual)
14977 or else not Is_Type (Entity (Actual))
14978 then
14979 Error_Msg_NE
14980 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
14981 Abandon_Instantiation (Actual);
14982 end if;
14983
14984 Act_T := Entity (Actual);
14985
14986 -- Obtain the class-wide equivalent type and use it for the
14987 -- instantiation instead of a mutably tagged type.
14988
14989 if Present (Class_Wide_Equivalent_Type (Act_T)) then
14990 Act_T := Class_Wide_Equivalent_Type (Act_T);
14991 end if;
14992
14993 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
14994 -- as a generic actual parameter if the corresponding formal type
14995 -- does not have a known_discriminant_part, or is a formal derived
14996 -- type that is an Unchecked_Union type.
14997
14998 if Is_Unchecked_Union (Base_Type (Act_T)) then
14999 if not Has_Discriminants (A_Gen_T)
15000 or else (Is_Derived_Type (A_Gen_T)
15001 and then Is_Unchecked_Union (A_Gen_T))
15002 then
15003 null;
15004 else
15005 Error_Msg_N ("unchecked union cannot be the actual for a "
15006 & "discriminated formal type", Act_T);
15007
15008 end if;
15009 end if;
15010
15011 -- Deal with fixed/floating restrictions
15012
15013 if Is_Floating_Point_Type (Act_T) then
15014 Check_Restriction (No_Floating_Point, Actual);
15015 elsif Is_Fixed_Point_Type (Act_T) then
15016 Check_Restriction (No_Fixed_Point, Actual);
15017 end if;
15018
15019 -- Deal with error of using incomplete type as generic actual.
15020 -- This includes limited views of a type, even if the non-limited
15021 -- view may be available.
15022
15023 if Ekind (Act_T) = E_Incomplete_Type
15024 or else (Is_Class_Wide_Type (Act_T)
15025 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
15026 then
15027 -- If the formal is an incomplete type, the actual can be
15028 -- incomplete as well, but if an actual incomplete type has
15029 -- a full view, then we'll retrieve that.
15030
15031 if Ekind (A_Gen_T) = E_Incomplete_Type
15032 and then No (Full_View (Act_T))
15033 then
15034 null;
15035
15036 elsif Is_Class_Wide_Type (Act_T)
15037 or else No (Full_View (Act_T))
15038 then
15039 Error_Msg_N ("premature use of incomplete type", Actual);
15040 Abandon_Instantiation (Actual);
15041
15042 else
15043 Act_T := Full_View (Act_T);
15044 Set_Entity (Actual, Act_T);
15045
15046 if Has_Private_Component (Act_T) then
15047 Error_Msg_N
15048 ("premature use of type with private component", Actual);
15049 end if;
15050 end if;
15051
15052 -- Deal with error of premature use of private type as generic actual,
15053 -- which is allowed for incomplete formals.
15054
15055 elsif Ekind (A_Gen_T) /= E_Incomplete_Type then
15056 if Is_Private_Type (Act_T)
15057 and then Is_Private_Type (Base_Type (Act_T))
15058 and then not Is_Generic_Type (Act_T)
15059 and then not Is_Derived_Type (Act_T)
15060 and then No (Full_View (Root_Type (Act_T)))
15061 then
15062 Error_Msg_N ("premature use of private type", Actual);
15063
15064 elsif Has_Private_Component (Act_T) then
15065 Error_Msg_N
15066 ("premature use of type with private component", Actual);
15067 end if;
15068 end if;
15069
15070 Set_Instance_Of (A_Gen_T, Act_T);
15071
15072 -- If the type is generic, the class-wide type may also be used
15073
15074 if Is_Tagged_Type (A_Gen_T)
15075 and then Is_Tagged_Type (Act_T)
15076 and then not Is_Class_Wide_Type (A_Gen_T)
15077 then
15078 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
15079 Class_Wide_Type (Act_T));
15080 end if;
15081
15082 if not Is_Abstract_Type (A_Gen_T)
15083 and then Is_Abstract_Type (Act_T)
15084 then
15085 Error_Msg_N
15086 ("actual of non-abstract formal cannot be abstract", Actual);
15087 end if;
15088
15089 -- A generic scalar type is a first subtype for which we generate
15090 -- an anonymous base type. Indicate that the instance of this base
15091 -- is the base type of the actual.
15092
15093 if Is_Scalar_Type (A_Gen_T) then
15094 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
15095 end if;
15096
15097 Check_Shared_Variable_Control_Aspects;
15098
15099 if not Error_Posted (Act_T) then
15100 case Nkind (Def) is
15101 when N_Formal_Private_Type_Definition =>
15102 Validate_Private_Type_Instance;
15103
15104 when N_Formal_Incomplete_Type_Definition =>
15105 Validate_Incomplete_Type_Instance;
15106
15107 when N_Formal_Derived_Type_Definition =>
15108 Validate_Derived_Type_Instance;
15109
15110 when N_Formal_Discrete_Type_Definition =>
15111 if not Is_Discrete_Type (Act_T) then
15112 Error_Msg_NE
15113 ("expect discrete type in instantiation of&",
15114 Actual, Gen_T);
15115 Abandon_Instantiation (Actual);
15116 end if;
15117
15118 Diagnose_Predicated_Actual;
15119
15120 when N_Formal_Signed_Integer_Type_Definition =>
15121 if not Is_Signed_Integer_Type (Act_T) then
15122 Error_Msg_NE
15123 ("expect signed integer type in instantiation of&",
15124 Actual, Gen_T);
15125 Abandon_Instantiation (Actual);
15126 end if;
15127
15128 Diagnose_Predicated_Actual;
15129
15130 when N_Formal_Modular_Type_Definition =>
15131 if not Is_Modular_Integer_Type (Act_T) then
15132 Error_Msg_NE
15133 ("expect modular type in instantiation of &",
15134 Actual, Gen_T);
15135 Abandon_Instantiation (Actual);
15136 end if;
15137
15138 Diagnose_Predicated_Actual;
15139
15140 when N_Formal_Floating_Point_Definition =>
15141 if not Is_Floating_Point_Type (Act_T) then
15142 Error_Msg_NE
15143 ("expect float type in instantiation of &", Actual, Gen_T);
15144 Abandon_Instantiation (Actual);
15145 end if;
15146
15147 when N_Formal_Ordinary_Fixed_Point_Definition =>
15148 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
15149 Error_Msg_NE
15150 ("expect ordinary fixed point type in instantiation of &",
15151 Actual, Gen_T);
15152 Abandon_Instantiation (Actual);
15153 end if;
15154
15155 when N_Formal_Decimal_Fixed_Point_Definition =>
15156 if not Is_Decimal_Fixed_Point_Type (Act_T) then
15157 Error_Msg_NE
15158 ("expect decimal type in instantiation of &",
15159 Actual, Gen_T);
15160 Abandon_Instantiation (Actual);
15161 end if;
15162
15163 when N_Array_Type_Definition =>
15164 Validate_Array_Type_Instance;
15165
15166 when N_Access_To_Object_Definition =>
15167 Validate_Access_Type_Instance;
15168
15169 when N_Access_Function_Definition
15170 | N_Access_Procedure_Definition
15171 =>
15172 Validate_Access_Subprogram_Instance;
15173
15174 when N_Record_Definition =>
15175 Validate_Interface_Type_Instance;
15176
15177 when N_Derived_Type_Definition =>
15178 Validate_Derived_Interface_Type_Instance;
15179
15180 when others =>
15181 raise Program_Error;
15182 end case;
15183 end if;
15184
15185 Subt := New_Copy (Gen_T);
15186
15187 -- Use adjusted sloc of subtype name as the location for other nodes in
15188 -- the subtype declaration.
15189
15190 Loc := Sloc (Subt);
15191
15192 Decl_Node :=
15193 Make_Subtype_Declaration (Loc,
15194 Defining_Identifier => Subt,
15195 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
15196
15197 Copy_Ghost_Aspect (Formal, To => Decl_Node);
15198
15199 -- Record whether the actual is private at this point, so that
15200 -- Check_Generic_Actuals can restore its proper view before the
15201 -- semantic analysis of the instance.
15202
15203 if Is_Private_Type (Act_T) then
15204 Set_Has_Private_View (Subtype_Indication (Decl_Node));
15205
15206 elsif (Is_Access_Type (Act_T)
15207 and then Is_Private_Type (Designated_Type (Act_T)))
15208 or else (Is_Array_Type (Act_T)
15209 and then
15210 Is_Private_Type (Component_Type_For_Private_View (Act_T)))
15211 then
15212 Set_Has_Secondary_Private_View (Subtype_Indication (Decl_Node));
15213 end if;
15214
15215 -- In Ada 2012 the actual may be a limited view. Indicate that
15216 -- the local subtype must be treated as such.
15217
15218 if From_Limited_With (Act_T) then
15219 Mutate_Ekind (Subt, E_Incomplete_Subtype);
15220 Set_From_Limited_With (Subt);
15221 end if;
15222
15223 Decl_Nodes := New_List (Decl_Node);
15224
15225 -- Flag actual derived types so their elaboration produces the
15226 -- appropriate renamings for the primitive operations of the ancestor.
15227 -- Flag actual for formal private types as well, to determine whether
15228 -- operations in the private part may override inherited operations.
15229 -- If the formal has an interface list, the ancestor is not the
15230 -- parent, but the analyzed formal that includes the interface
15231 -- operations of all its progenitors.
15232
15233 -- Same treatment for formal private types, so we can check whether the
15234 -- type is tagged limited when validating derivations in the private
15235 -- part. (See AI05-096).
15236
15237 if Nkind (Def) = N_Formal_Derived_Type_Definition then
15238 if Present (Interface_List (Def)) then
15239 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
15240 else
15241 Set_Generic_Parent_Type (Decl_Node, Ancestor);
15242 end if;
15243
15244 elsif Nkind (Def) in N_Formal_Private_Type_Definition
15245 | N_Formal_Incomplete_Type_Definition
15246 then
15247 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
15248 end if;
15249
15250 -- If the actual is a synchronized type that implements an interface,
15251 -- the primitive operations are attached to the corresponding record,
15252 -- and we have to treat it as an additional generic actual, so that its
15253 -- primitive operations become visible in the instance. The task or
15254 -- protected type itself does not carry primitive operations.
15255
15256 if Is_Concurrent_Type (Act_T)
15257 and then Is_Tagged_Type (Act_T)
15258 and then Present (Corresponding_Record_Type (Act_T))
15259 and then Present (Ancestor)
15260 and then Is_Interface (Ancestor)
15261 then
15262 declare
15263 Corr_Rec : constant Entity_Id :=
15264 Corresponding_Record_Type (Act_T);
15265 New_Corr : Entity_Id;
15266 Corr_Decl : Node_Id;
15267
15268 begin
15269 New_Corr := Make_Temporary (Loc, 'S');
15270 Corr_Decl :=
15271 Make_Subtype_Declaration (Loc,
15272 Defining_Identifier => New_Corr,
15273 Subtype_Indication =>
15274 New_Occurrence_Of (Corr_Rec, Loc));
15275 Append_To (Decl_Nodes, Corr_Decl);
15276
15277 if Ekind (Act_T) = E_Task_Type then
15278 Mutate_Ekind (Subt, E_Task_Subtype);
15279 else
15280 Mutate_Ekind (Subt, E_Protected_Subtype);
15281 end if;
15282
15283 Set_Corresponding_Record_Type (Subt, Corr_Rec);
15284 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
15285 Set_Generic_Parent_Type (Decl_Node, Empty);
15286 end;
15287 end if;
15288
15289 -- For a floating-point type, capture dimension info if any, because
15290 -- the generated subtype declaration does not come from source and
15291 -- will not process dimensions.
15292
15293 if Is_Floating_Point_Type (Act_T) then
15294 Copy_Dimensions (Act_T, Subt);
15295 end if;
15296
15297 return Decl_Nodes;
15298 end Instantiate_Type;
15299
15300 -----------------------------
15301 -- Is_Abbreviated_Instance --
15302 -----------------------------
15303
15304 function Is_Abbreviated_Instance (E : Entity_Id) return Boolean is
15305 begin
15306 return Ekind (E) = E_Package
15307 and then Present (Hidden_In_Formal_Instance (E));
15308 end Is_Abbreviated_Instance;
15309
15310 ---------------------
15311 -- Is_In_Main_Unit --
15312 ---------------------
15313
15314 function Is_In_Main_Unit (N : Node_Id) return Boolean is
15315 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
15316 Current_Unit : Node_Id;
15317
15318 begin
15319 if Unum = Main_Unit then
15320 return True;
15321
15322 -- If the current unit is a subunit then it is either the main unit or
15323 -- is being compiled as part of the main unit.
15324
15325 elsif Nkind (N) = N_Compilation_Unit then
15326 return Nkind (Unit (N)) = N_Subunit;
15327 end if;
15328
15329 Current_Unit := Parent (N);
15330 while Present (Current_Unit)
15331 and then Nkind (Current_Unit) /= N_Compilation_Unit
15332 loop
15333 Current_Unit := Parent (Current_Unit);
15334 end loop;
15335
15336 -- The instantiation node is in the main unit, or else the current node
15337 -- (perhaps as the result of nested instantiations) is in the main unit,
15338 -- or in the declaration of the main unit, which in this last case must
15339 -- be a body.
15340
15341 return
15342 Current_Unit = Cunit (Main_Unit)
15343 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
15344 or else (Present (Current_Unit)
15345 and then Present (Library_Unit (Current_Unit))
15346 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
15347 end Is_In_Main_Unit;
15348
15349 ----------------------------
15350 -- Load_Parent_Of_Generic --
15351 ----------------------------
15352
15353 procedure Load_Parent_Of_Generic
15354 (N : Node_Id;
15355 Spec : Node_Id;
15356 Body_Optional : Boolean := False)
15357 is
15358 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
15359 Saved_Style_Check : constant Boolean := Style_Check;
15360 Saved_Warn : constant Warnings_State := Save_Warnings;
15361 True_Parent : Node_Id;
15362 Inst_Node : Node_Id;
15363 OK : Boolean;
15364 Previous_Instances : constant Elist_Id := New_Elmt_List;
15365
15366 procedure Collect_Previous_Instances (Decls : List_Id);
15367 -- Collect all instantiations in the given list of declarations, that
15368 -- precede the generic that we need to load. If the bodies of these
15369 -- instantiations are available, we must analyze them, to ensure that
15370 -- the public symbols generated are the same when the unit is compiled
15371 -- to generate code, and when it is compiled in the context of a unit
15372 -- that needs a particular nested instance. This process is applied to
15373 -- both package and subprogram instances.
15374
15375 --------------------------------
15376 -- Collect_Previous_Instances --
15377 --------------------------------
15378
15379 procedure Collect_Previous_Instances (Decls : List_Id) is
15380 Decl : Node_Id;
15381
15382 begin
15383 Decl := First (Decls);
15384 while Present (Decl) loop
15385 if Sloc (Decl) >= Sloc (Inst_Node) then
15386 return;
15387
15388 -- If Decl is an instantiation, then record it as requiring
15389 -- instantiation of the corresponding body, except if it is an
15390 -- abbreviated instantiation generated internally for conformance
15391 -- checking purposes only for the case of a formal package
15392 -- declared without a box (see Instantiate_Formal_Package). Such
15393 -- an instantiation does not generate any code (the actual code
15394 -- comes from actual) and thus does not need to be analyzed here.
15395 -- If the instantiation appears with a generic package body it is
15396 -- not analyzed here either.
15397
15398 elsif Nkind (Decl) = N_Package_Instantiation
15399 and then not Is_Abbreviated_Instance (Defining_Entity (Decl))
15400 then
15401 Append_Elmt (Decl, Previous_Instances);
15402
15403 -- For a subprogram instantiation, omit instantiations intrinsic
15404 -- operations (Unchecked_Conversions, etc.) that have no bodies.
15405
15406 elsif Nkind (Decl) in N_Function_Instantiation
15407 | N_Procedure_Instantiation
15408 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
15409 then
15410 Append_Elmt (Decl, Previous_Instances);
15411
15412 elsif Nkind (Decl) = N_Package_Declaration then
15413 Collect_Previous_Instances
15414 (Visible_Declarations (Specification (Decl)));
15415 Collect_Previous_Instances
15416 (Private_Declarations (Specification (Decl)));
15417
15418 -- Previous non-generic bodies may contain instances as well
15419
15420 elsif Nkind (Decl) = N_Package_Body
15421 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
15422 then
15423 Collect_Previous_Instances (Declarations (Decl));
15424
15425 elsif Nkind (Decl) = N_Subprogram_Body
15426 and then not Acts_As_Spec (Decl)
15427 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
15428 then
15429 Collect_Previous_Instances (Declarations (Decl));
15430 end if;
15431
15432 Next (Decl);
15433 end loop;
15434 end Collect_Previous_Instances;
15435
15436 -- Start of processing for Load_Parent_Of_Generic
15437
15438 begin
15439 if not In_Same_Source_Unit (N, Spec)
15440 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
15441 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
15442 and then not Is_In_Main_Unit (Spec))
15443 then
15444 -- Find body of parent of spec, and analyze it. A special case arises
15445 -- when the parent is an instantiation, that is to say when we are
15446 -- currently instantiating a nested generic. In that case, there is
15447 -- no separate file for the body of the enclosing instance. Instead,
15448 -- the enclosing body must be instantiated as if it were a pending
15449 -- instantiation, in order to produce the body for the nested generic
15450 -- we require now. Note that in that case the generic may be defined
15451 -- in a package body, the instance defined in the same package body,
15452 -- and the original enclosing body may not be in the main unit.
15453
15454 Inst_Node := Empty;
15455
15456 True_Parent := Parent (Spec);
15457 while Present (True_Parent)
15458 and then Nkind (True_Parent) /= N_Compilation_Unit
15459 loop
15460 if Nkind (True_Parent) = N_Package_Declaration
15461 and then
15462 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
15463 then
15464 -- Parent is a compilation unit that is an instantiation, and
15465 -- instantiation node has been replaced with package decl.
15466
15467 Inst_Node := Original_Node (True_Parent);
15468 exit;
15469
15470 elsif Nkind (True_Parent) = N_Package_Declaration
15471 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
15472 and then
15473 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
15474 then
15475 -- Parent is a compilation unit that is an instantiation, but
15476 -- instantiation node has not been replaced with package decl.
15477
15478 Inst_Node := Unit (Parent (True_Parent));
15479 exit;
15480
15481 elsif Nkind (True_Parent) = N_Package_Declaration
15482 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
15483 and then Present (Generic_Parent (Specification (True_Parent)))
15484 then
15485 -- Parent is an instantiation within another specification.
15486 -- Declaration for instance has been inserted before original
15487 -- instantiation node. A direct link would be preferable?
15488
15489 Inst_Node := Next (True_Parent);
15490 while Present (Inst_Node)
15491 and then Nkind (Inst_Node) /= N_Package_Instantiation
15492 loop
15493 Next (Inst_Node);
15494 end loop;
15495
15496 -- If the instance appears within a generic, and the generic
15497 -- unit is defined within a formal package of the enclosing
15498 -- generic, there is no generic body available, and none
15499 -- needed. A more precise test should be used ???
15500
15501 if No (Inst_Node) then
15502 return;
15503 end if;
15504
15505 exit;
15506
15507 -- If an ancestor of the generic comes from a formal package
15508 -- there is no source for the ancestor body. This is detected
15509 -- by examining the scope of the ancestor and its declaration.
15510 -- The body, if any is needed, will be available when the
15511 -- current unit (containing a formal package) is instantiated.
15512
15513 elsif Nkind (True_Parent) = N_Package_Specification
15514 and then Present (Generic_Parent (True_Parent))
15515 and then Nkind
15516 (Original_Node (Unit_Declaration_Node
15517 (Scope (Generic_Parent (True_Parent)))))
15518 = N_Formal_Package_Declaration
15519 then
15520 return;
15521
15522 else
15523 True_Parent := Parent (True_Parent);
15524 end if;
15525 end loop;
15526
15527 -- Case where we are currently instantiating a nested generic
15528
15529 if Present (Inst_Node) then
15530 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
15531
15532 -- Instantiation node and declaration of instantiated package
15533 -- were exchanged when only the declaration was needed.
15534 -- Restore instantiation node before proceeding with body.
15535
15536 Set_Unit (Parent (True_Parent), Inst_Node);
15537 end if;
15538
15539 -- Now complete instantiation of enclosing body, if it appears in
15540 -- some other unit. If it appears in the current unit, the body
15541 -- will have been instantiated already.
15542
15543 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
15544
15545 -- We need to determine the expander mode to instantiate the
15546 -- enclosing body. Because the generic body we need may use
15547 -- global entities declared in the enclosing package (including
15548 -- aggregates) it is in general necessary to compile this body
15549 -- with expansion enabled, except if we are within a generic
15550 -- package, in which case the usual generic rule applies.
15551
15552 declare
15553 Exp_Status : Boolean := True;
15554 Scop : Entity_Id;
15555
15556 begin
15557 -- Loop through scopes looking for generic package
15558
15559 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
15560 while Present (Scop)
15561 and then Scop /= Standard_Standard
15562 loop
15563 if Ekind (Scop) = E_Generic_Package then
15564 Exp_Status := False;
15565 exit;
15566 end if;
15567
15568 Scop := Scope (Scop);
15569 end loop;
15570
15571 -- Collect previous instantiations in the unit that contains
15572 -- the desired generic.
15573
15574 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
15575 and then not Body_Optional
15576 then
15577 declare
15578 Decl : Elmt_Id;
15579 Info : Pending_Body_Info;
15580 Par : Node_Id;
15581
15582 begin
15583 Par := Parent (Inst_Node);
15584 while Present (Par) loop
15585 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
15586 Par := Parent (Par);
15587 end loop;
15588
15589 pragma Assert (Present (Par));
15590
15591 if Nkind (Par) = N_Package_Body then
15592 Collect_Previous_Instances (Declarations (Par));
15593
15594 elsif Nkind (Par) = N_Package_Declaration then
15595 Collect_Previous_Instances
15596 (Visible_Declarations (Specification (Par)));
15597 Collect_Previous_Instances
15598 (Private_Declarations (Specification (Par)));
15599
15600 else
15601 -- Enclosing unit is a subprogram body. In this
15602 -- case all instance bodies are processed in order
15603 -- and there is no need to collect them separately.
15604
15605 null;
15606 end if;
15607
15608 Decl := First_Elmt (Previous_Instances);
15609 while Present (Decl) loop
15610 Info :=
15611 (Inst_Node => Node (Decl),
15612 Act_Decl =>
15613 Instance_Spec (Node (Decl)),
15614 Fin_Scop => Empty,
15615 Config_Switches => Save_Config_Switches,
15616 Current_Sem_Unit =>
15617 Get_Code_Unit (Sloc (Node (Decl))),
15618 Expander_Status => Exp_Status,
15619 Local_Suppress_Stack_Top =>
15620 Local_Suppress_Stack_Top,
15621 Scope_Suppress => Scope_Suppress,
15622 Warnings => Save_Warnings);
15623
15624 -- Package instance
15625
15626 if Nkind (Node (Decl)) = N_Package_Instantiation
15627 then
15628 Instantiate_Package_Body
15629 (Info, Body_Optional => True);
15630
15631 -- Subprogram instance
15632
15633 else
15634 -- The instance_spec is in the wrapper package,
15635 -- usually followed by its local renaming
15636 -- declaration. See Build_Subprogram_Renaming
15637 -- for details. If the instance carries aspects,
15638 -- these result in the corresponding pragmas,
15639 -- inserted after the subprogram declaration.
15640 -- They must be skipped as well when retrieving
15641 -- the desired spec. Some of them may have been
15642 -- rewritten as null statements.
15643 -- A direct link would be more robust ???
15644
15645 declare
15646 Decl : Node_Id :=
15647 (Last (Visible_Declarations
15648 (Specification (Info.Act_Decl))));
15649 begin
15650 while Nkind (Decl) in
15651 N_Null_Statement |
15652 N_Pragma |
15653 N_Subprogram_Renaming_Declaration
15654 loop
15655 Decl := Prev (Decl);
15656 end loop;
15657
15658 Info.Act_Decl := Decl;
15659 end;
15660
15661 Instantiate_Subprogram_Body
15662 (Info, Body_Optional => True);
15663 end if;
15664
15665 Next_Elmt (Decl);
15666 end loop;
15667 end;
15668 end if;
15669
15670 Instantiate_Package_Body
15671 (Body_Info =>
15672 ((Inst_Node => Inst_Node,
15673 Act_Decl => True_Parent,
15674 Fin_Scop => Empty,
15675 Config_Switches => Save_Config_Switches,
15676 Current_Sem_Unit =>
15677 Get_Code_Unit (Sloc (Inst_Node)),
15678 Expander_Status => Exp_Status,
15679 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
15680 Scope_Suppress => Scope_Suppress,
15681 Warnings => Save_Warnings)),
15682 Body_Optional => Body_Optional);
15683 end;
15684 end if;
15685
15686 -- Case where we are not instantiating a nested generic
15687
15688 else
15689 Opt.Style_Check := False;
15690 Expander_Mode_Save_And_Set (True);
15691 Load_Needed_Body (Comp_Unit, OK);
15692 Opt.Style_Check := Saved_Style_Check;
15693 Restore_Warnings (Saved_Warn);
15694 Expander_Mode_Restore;
15695
15696 if not OK
15697 and then Unit_Requires_Body (Defining_Entity (Spec))
15698 and then not Body_Optional
15699 then
15700 declare
15701 Bname : constant Unit_Name_Type :=
15702 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
15703
15704 begin
15705 -- In CodePeer mode, the missing body may make the analysis
15706 -- incomplete, but we do not treat it as fatal.
15707
15708 if CodePeer_Mode then
15709 return;
15710
15711 else
15712 Error_Msg_Unit_1 := Bname;
15713 Error_Msg_N ("this instantiation requires$!", N);
15714 Error_Msg_File_1 :=
15715 Get_File_Name (Bname, Subunit => False);
15716 Error_Msg_N ("\but file{ was not found!", N);
15717 raise Unrecoverable_Error;
15718 end if;
15719 end;
15720 end if;
15721 end if;
15722 end if;
15723
15724 -- If loading parent of the generic caused an instantiation circularity,
15725 -- we abandon compilation at this point, because otherwise in some cases
15726 -- we get into trouble with infinite recursions after this point.
15727
15728 if Circularity_Detected then
15729 raise Unrecoverable_Error;
15730 end if;
15731 end Load_Parent_Of_Generic;
15732
15733 ---------------------------------
15734 -- Map_Formal_Package_Entities --
15735 ---------------------------------
15736
15737 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
15738 E1 : Entity_Id;
15739 E2 : Entity_Id;
15740
15741 begin
15742 Set_Instance_Of (Form, Act);
15743
15744 -- Traverse formal and actual package to map the corresponding entities.
15745 -- We skip over internal entities that may be generated during semantic
15746 -- analysis, and find the matching entities by name, given that they
15747 -- must appear in the same order.
15748
15749 E1 := First_Entity (Form);
15750 E2 := First_Entity (Act);
15751 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
15752 -- Could this test be a single condition??? Seems like it could, and
15753 -- isn't FPE (Form) a constant anyway???
15754
15755 if not Is_Internal (E1)
15756 and then Present (Parent (E1))
15757 and then not Is_Class_Wide_Type (E1)
15758 and then not Is_Internal_Name (Chars (E1))
15759 then
15760 while Present (E2) and then Chars (E2) /= Chars (E1) loop
15761 Next_Entity (E2);
15762 end loop;
15763
15764 if No (E2) then
15765 exit;
15766 else
15767 Set_Instance_Of (E1, E2);
15768
15769 if Is_Type (E1) and then Is_Tagged_Type (E2) then
15770 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
15771 end if;
15772
15773 if Is_Constrained (E1) then
15774 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
15775 end if;
15776
15777 if Ekind (E1) = E_Package and then No (Renamed_Entity (E1)) then
15778 Map_Formal_Package_Entities (E1, E2);
15779 end if;
15780 end if;
15781 end if;
15782
15783 Next_Entity (E1);
15784 end loop;
15785 end Map_Formal_Package_Entities;
15786
15787 -----------------------
15788 -- Move_Freeze_Nodes --
15789 -----------------------
15790
15791 procedure Move_Freeze_Nodes
15792 (Out_Of : Entity_Id;
15793 After : Node_Id;
15794 L : List_Id)
15795 is
15796 Decl : Node_Id;
15797 Next_Decl : Node_Id;
15798 Next_Node : Node_Id := After;
15799 Spec : Node_Id;
15800
15801 function Is_Outer_Type (T : Entity_Id) return Boolean;
15802 -- Check whether entity is declared in a scope external to that of the
15803 -- generic unit.
15804
15805 -------------------
15806 -- Is_Outer_Type --
15807 -------------------
15808
15809 function Is_Outer_Type (T : Entity_Id) return Boolean is
15810 Scop : Entity_Id := Scope (T);
15811
15812 begin
15813 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
15814 return True;
15815
15816 else
15817 while Scop /= Standard_Standard loop
15818 if Scop = Out_Of then
15819 return False;
15820 else
15821 Scop := Scope (Scop);
15822 end if;
15823 end loop;
15824
15825 return True;
15826 end if;
15827 end Is_Outer_Type;
15828
15829 -- Start of processing for Move_Freeze_Nodes
15830
15831 begin
15832 if No (L) then
15833 return;
15834 end if;
15835
15836 -- First remove the freeze nodes that may appear before all other
15837 -- declarations.
15838
15839 Decl := First (L);
15840 while Present (Decl)
15841 and then Nkind (Decl) = N_Freeze_Entity
15842 and then Is_Outer_Type (Entity (Decl))
15843 loop
15844 Decl := Remove_Head (L);
15845 Insert_After (Next_Node, Decl);
15846 Set_Analyzed (Decl, False);
15847 Next_Node := Decl;
15848 Decl := First (L);
15849 end loop;
15850
15851 -- Next scan the list of declarations and remove each freeze node that
15852 -- appears ahead of the current node.
15853
15854 while Present (Decl) loop
15855 while Present (Next (Decl))
15856 and then Nkind (Next (Decl)) = N_Freeze_Entity
15857 and then Is_Outer_Type (Entity (Next (Decl)))
15858 loop
15859 Next_Decl := Remove_Next (Decl);
15860 Insert_After (Next_Node, Next_Decl);
15861 Set_Analyzed (Next_Decl, False);
15862 Next_Node := Next_Decl;
15863 end loop;
15864
15865 -- If the declaration is a nested package or concurrent type, then
15866 -- recurse. Nested generic packages will have been processed from the
15867 -- inside out.
15868
15869 case Nkind (Decl) is
15870 when N_Package_Declaration =>
15871 Spec := Specification (Decl);
15872
15873 when N_Task_Type_Declaration =>
15874 Spec := Task_Definition (Decl);
15875
15876 when N_Protected_Type_Declaration =>
15877 Spec := Protected_Definition (Decl);
15878
15879 when others =>
15880 Spec := Empty;
15881 end case;
15882
15883 if Present (Spec) then
15884 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
15885 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
15886 end if;
15887
15888 Next (Decl);
15889 end loop;
15890 end Move_Freeze_Nodes;
15891
15892 ----------------
15893 -- Next_Assoc --
15894 ----------------
15895
15896 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
15897 begin
15898 return Generic_Renamings.Table (E).Next_In_HTable;
15899 end Next_Assoc;
15900
15901 ------------------------
15902 -- Preanalyze_Actuals --
15903 ------------------------
15904
15905 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
15906 procedure Perform_Appropriate_Analysis (N : Node_Id);
15907 -- Determine if the actuals we are analyzing come from a generic
15908 -- instantiation that is a library unit and dispatch accordingly.
15909
15910 ----------------------------------
15911 -- Perform_Appropriate_Analysis --
15912 ----------------------------------
15913
15914 procedure Perform_Appropriate_Analysis (N : Node_Id) is
15915 begin
15916 -- When we have a library instantiation we cannot allow any expansion
15917 -- to occur, since there may be no place to put it. Instead, in that
15918 -- case we perform a preanalysis of the actual.
15919
15920 if Present (Inst) and then Is_Compilation_Unit (Inst) then
15921 Preanalyze (N);
15922 else
15923 Analyze (N);
15924 end if;
15925 end Perform_Appropriate_Analysis;
15926
15927 -- Local variables
15928
15929 Errs : constant Nat := Serious_Errors_Detected;
15930
15931 Assoc : Node_Id;
15932 Act : Node_Id;
15933
15934 Cur : Entity_Id := Empty;
15935 -- Current homograph of the instance name
15936
15937 Vis : Boolean := False;
15938 -- Saved visibility status of the current homograph
15939
15940 -- Start of processing for Preanalyze_Actuals
15941
15942 begin
15943 Assoc := First (Generic_Associations (N));
15944
15945 -- If the instance is a child unit, its name may hide an outer homonym,
15946 -- so make it invisible to perform name resolution on the actuals.
15947
15948 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
15949 and then Present
15950 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
15951 then
15952 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
15953
15954 if Is_Compilation_Unit (Cur) then
15955 Vis := Is_Immediately_Visible (Cur);
15956 Set_Is_Immediately_Visible (Cur, False);
15957 else
15958 Cur := Empty;
15959 end if;
15960 end if;
15961
15962 while Present (Assoc) loop
15963 if Nkind (Assoc) /= N_Others_Choice then
15964 Act := Explicit_Generic_Actual_Parameter (Assoc);
15965
15966 -- Within a nested instantiation, a defaulted actual is an empty
15967 -- association, so nothing to analyze. If the subprogram actual
15968 -- is an attribute, analyze prefix only, because actual is not a
15969 -- complete attribute reference.
15970
15971 -- If actual is an allocator, analyze expression only. The full
15972 -- analysis can generate code, and if instance is a compilation
15973 -- unit we have to wait until the package instance is installed
15974 -- to have a proper place to insert this code.
15975
15976 -- String literals may be operators, but at this point we do not
15977 -- know whether the actual is a formal subprogram or a string.
15978
15979 if No (Act) then
15980 null;
15981
15982 elsif Nkind (Act) = N_Attribute_Reference then
15983 Perform_Appropriate_Analysis (Prefix (Act));
15984
15985 elsif Nkind (Act) = N_Explicit_Dereference then
15986 Perform_Appropriate_Analysis (Prefix (Act));
15987
15988 elsif Nkind (Act) = N_Allocator then
15989 declare
15990 Expr : constant Node_Id := Expression (Act);
15991
15992 begin
15993 if Nkind (Expr) = N_Subtype_Indication then
15994 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
15995
15996 -- Analyze separately each discriminant constraint, when
15997 -- given with a named association.
15998
15999 declare
16000 Constr : Node_Id;
16001
16002 begin
16003 Constr := First (Constraints (Constraint (Expr)));
16004 while Present (Constr) loop
16005 if Nkind (Constr) = N_Discriminant_Association then
16006 Perform_Appropriate_Analysis
16007 (Expression (Constr));
16008 else
16009 Perform_Appropriate_Analysis (Constr);
16010 end if;
16011
16012 Next (Constr);
16013 end loop;
16014 end;
16015
16016 else
16017 Perform_Appropriate_Analysis (Expr);
16018 end if;
16019 end;
16020
16021 elsif Nkind (Act) /= N_Operator_Symbol then
16022 Perform_Appropriate_Analysis (Act);
16023
16024 -- Within a package instance, mark actuals that are limited
16025 -- views, so their use can be moved to the body of the
16026 -- enclosing unit.
16027
16028 if Is_Entity_Name (Act)
16029 and then Is_Type (Entity (Act))
16030 and then From_Limited_With (Entity (Act))
16031 and then Present (Inst)
16032 then
16033 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
16034 end if;
16035 end if;
16036
16037 if Errs /= Serious_Errors_Detected then
16038
16039 -- Do a minimal analysis of the generic, to prevent spurious
16040 -- warnings complaining about the generic being unreferenced,
16041 -- before abandoning the instantiation.
16042
16043 Perform_Appropriate_Analysis (Name (N));
16044
16045 if Is_Entity_Name (Name (N))
16046 and then Etype (Name (N)) /= Any_Type
16047 then
16048 Generate_Reference (Entity (Name (N)), Name (N));
16049 Set_Is_Instantiated (Entity (Name (N)));
16050 end if;
16051
16052 if Present (Cur) then
16053
16054 -- For the case of a child instance hiding an outer homonym,
16055 -- provide additional warning which might explain the error.
16056
16057 Set_Is_Immediately_Visible (Cur, Vis);
16058 Error_Msg_NE
16059 ("& hides outer unit with the same name??",
16060 N, Defining_Unit_Name (N));
16061 end if;
16062
16063 Abandon_Instantiation (Act);
16064 end if;
16065 end if;
16066
16067 Next (Assoc);
16068 end loop;
16069
16070 if Present (Cur) then
16071 Set_Is_Immediately_Visible (Cur, Vis);
16072 end if;
16073 end Preanalyze_Actuals;
16074
16075 -------------------------------
16076 -- Provide_Completing_Bodies --
16077 -------------------------------
16078
16079 procedure Provide_Completing_Bodies (N : Node_Id) is
16080 procedure Build_Completing_Body (Subp_Decl : Node_Id);
16081 -- Generate the completing body for subprogram declaration Subp_Decl
16082
16083 procedure Provide_Completing_Bodies_In (Decls : List_Id);
16084 -- Generating completing bodies for all subprograms found in declarative
16085 -- list Decls.
16086
16087 ---------------------------
16088 -- Build_Completing_Body --
16089 ---------------------------
16090
16091 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
16092 Loc : constant Source_Ptr := Sloc (Subp_Decl);
16093 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
16094 Spec : Node_Id;
16095
16096 begin
16097 -- Nothing to do if the subprogram already has a completing body
16098
16099 if Present (Corresponding_Body (Subp_Decl)) then
16100 return;
16101
16102 -- Mark the function as having a valid return statement even though
16103 -- the body contains a single raise statement.
16104
16105 elsif Ekind (Subp_Id) = E_Function then
16106 Set_Return_Present (Subp_Id);
16107 end if;
16108
16109 -- Clone the specification to obtain new entities and reset the only
16110 -- semantic field.
16111
16112 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
16113 Set_Generic_Parent (Spec, Empty);
16114
16115 -- Generate:
16116 -- function Func ... return ... is
16117 -- <or>
16118 -- procedure Proc ... is
16119 -- begin
16120 -- raise Program_Error with "access before elaboration";
16121 -- edn Proc;
16122
16123 Insert_After_And_Analyze (Subp_Decl,
16124 Make_Subprogram_Body (Loc,
16125 Specification => Spec,
16126 Declarations => New_List,
16127 Handled_Statement_Sequence =>
16128 Make_Handled_Sequence_Of_Statements (Loc,
16129 Statements => New_List (
16130 Make_Raise_Program_Error (Loc,
16131 Reason => PE_Access_Before_Elaboration)))));
16132 end Build_Completing_Body;
16133
16134 ----------------------------------
16135 -- Provide_Completing_Bodies_In --
16136 ----------------------------------
16137
16138 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
16139 Decl : Node_Id;
16140
16141 begin
16142 if Present (Decls) then
16143 Decl := First (Decls);
16144 while Present (Decl) loop
16145 Provide_Completing_Bodies (Decl);
16146 Next (Decl);
16147 end loop;
16148 end if;
16149 end Provide_Completing_Bodies_In;
16150
16151 -- Local variables
16152
16153 Spec : Node_Id;
16154
16155 -- Start of processing for Provide_Completing_Bodies
16156
16157 begin
16158 if Nkind (N) = N_Package_Declaration then
16159 Spec := Specification (N);
16160
16161 Push_Scope (Defining_Entity (N));
16162 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
16163 Provide_Completing_Bodies_In (Private_Declarations (Spec));
16164 Pop_Scope;
16165
16166 elsif Nkind (N) = N_Subprogram_Declaration then
16167 Build_Completing_Body (N);
16168 end if;
16169 end Provide_Completing_Bodies;
16170
16171 -------------------
16172 -- Remove_Parent --
16173 -------------------
16174
16175 procedure Remove_Parent (In_Body : Boolean := False) is
16176 S : Entity_Id := Current_Scope;
16177 -- S is the scope containing the instantiation just completed. The scope
16178 -- stack contains the parent instances of the instantiation, followed by
16179 -- the original S.
16180
16181 Cur_P : Entity_Id;
16182 E : Entity_Id;
16183 P : Entity_Id;
16184 Hidden : Elmt_Id;
16185
16186 begin
16187 -- After child instantiation is complete, remove from scope stack the
16188 -- extra copy of the current scope, and then remove parent instances.
16189
16190 if not In_Body then
16191 Pop_Scope;
16192
16193 while Current_Scope /= S loop
16194 P := Current_Scope;
16195 End_Package_Scope (Current_Scope);
16196
16197 if In_Open_Scopes (P) then
16198 E := First_Entity (P);
16199 while Present (E) loop
16200 Set_Is_Immediately_Visible (E, True);
16201 Next_Entity (E);
16202 end loop;
16203
16204 -- If instantiation is declared in a block, it is the enclosing
16205 -- scope that might be a parent instance. Note that only one
16206 -- block can be involved, because the parent instances have
16207 -- been installed within it.
16208
16209 if Ekind (P) = E_Block then
16210 Cur_P := Scope (P);
16211 else
16212 Cur_P := P;
16213 end if;
16214
16215 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
16216 -- We are within an instance of some sibling. Retain
16217 -- visibility of parent, for proper subsequent cleanup, and
16218 -- reinstall private declarations as well.
16219
16220 Set_In_Private_Part (P);
16221 Install_Private_Declarations (P);
16222 end if;
16223
16224 -- If the ultimate parent is a top-level unit recorded in
16225 -- Instance_Parent_Unit, then reset its visibility to what it was
16226 -- before instantiation. (It's not clear what the purpose is of
16227 -- testing whether Scope (P) is In_Open_Scopes, but that test was
16228 -- present before the ultimate parent test was added.???)
16229
16230 elsif not In_Open_Scopes (Scope (P))
16231 or else (P = Instance_Parent_Unit
16232 and then not Parent_Unit_Visible)
16233 then
16234 Set_Is_Immediately_Visible (P, False);
16235
16236 -- If the current scope is itself an instantiation of a generic
16237 -- nested within P, and we are in the private part of body of this
16238 -- instantiation, restore the full views of P, that were removed
16239 -- in End_Package_Scope above. This obscure case can occur when a
16240 -- subunit of a generic contains an instance of a child unit of
16241 -- its generic parent unit.
16242
16243 elsif S = Current_Scope and then Is_Generic_Instance (S)
16244 and then (In_Package_Body (S) or else In_Private_Part (S))
16245 then
16246 declare
16247 Par : constant Entity_Id :=
16248 Generic_Parent (Package_Specification (S));
16249 begin
16250 if Present (Par)
16251 and then P = Scope (Par)
16252 then
16253 Set_In_Private_Part (P);
16254 Install_Private_Declarations (P);
16255 end if;
16256 end;
16257 end if;
16258 end loop;
16259
16260 -- Reset visibility of entities in the enclosing scope
16261
16262 Set_Is_Hidden_Open_Scope (Current_Scope, False);
16263
16264 Hidden := First_Elmt (Hidden_Entities);
16265 while Present (Hidden) loop
16266 Set_Is_Immediately_Visible (Node (Hidden), True);
16267 Next_Elmt (Hidden);
16268 end loop;
16269
16270 else
16271 -- Each body is analyzed separately, and there is no context that
16272 -- needs preserving from one body instance to the next, so remove all
16273 -- parent scopes that have been installed.
16274
16275 while Present (S) loop
16276 End_Package_Scope (S);
16277 Set_Is_Immediately_Visible (S, False);
16278 S := Current_Scope;
16279 exit when S = Standard_Standard;
16280 end loop;
16281 end if;
16282 end Remove_Parent;
16283
16284 -----------------------------------
16285 -- Requires_Conformance_Checking --
16286 -----------------------------------
16287
16288 function Requires_Conformance_Checking (N : Node_Id) return Boolean is
16289 begin
16290 -- No conformance checking required if the generic actual part is empty,
16291 -- or is a box or an others_clause (necessarily with a box).
16292
16293 return Present (Generic_Associations (N))
16294 and then not Box_Present (N)
16295 and then Nkind (First (Generic_Associations (N))) /= N_Others_Choice;
16296 end Requires_Conformance_Checking;
16297
16298 -----------------
16299 -- Restore_Env --
16300 -----------------
16301
16302 procedure Restore_Env is
16303 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
16304
16305 begin
16306 if No (Current_Instantiated_Parent.Act_Id) then
16307 -- Restore environment after subprogram inlining
16308
16309 Restore_Private_Views (Empty);
16310 end if;
16311
16312 Current_Instantiated_Parent := Saved.Instantiated_Parent;
16313 Exchanged_Views := Saved.Exchanged_Views;
16314 Hidden_Entities := Saved.Hidden_Entities;
16315 Current_Sem_Unit := Saved.Current_Sem_Unit;
16316 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
16317 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
16318
16319 Restore_Config_Switches (Saved.Switches);
16320
16321 Instance_Envs.Decrement_Last;
16322 end Restore_Env;
16323
16324 ---------------------------
16325 -- Restore_Private_Views --
16326 ---------------------------
16327
16328 procedure Restore_Private_Views
16329 (Pack_Id : Entity_Id;
16330 Is_Package : Boolean := True)
16331 is
16332 M : Elmt_Id;
16333 E : Entity_Id;
16334 Typ : Entity_Id;
16335 Dep_Elmt : Elmt_Id;
16336 Dep_Typ : Node_Id;
16337
16338 procedure Restore_Nested_Formal (Formal : Entity_Id);
16339 -- Hide the generic formals of formal packages declared with box which
16340 -- were reachable in the current instantiation.
16341
16342 ---------------------------
16343 -- Restore_Nested_Formal --
16344 ---------------------------
16345
16346 procedure Restore_Nested_Formal (Formal : Entity_Id) is
16347 pragma Assert (Ekind (Formal) = E_Package);
16348 Ent : Entity_Id;
16349 begin
16350 if Present (Renamed_Entity (Formal))
16351 and then Denotes_Formal_Package (Renamed_Entity (Formal), True)
16352 then
16353 return;
16354
16355 elsif Present (Associated_Formal_Package (Formal)) then
16356 Ent := First_Entity (Formal);
16357 while Present (Ent) loop
16358 exit when Ekind (Ent) = E_Package
16359 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
16360
16361 Set_Is_Hidden (Ent);
16362 Set_Is_Potentially_Use_Visible (Ent, False);
16363
16364 -- If package, then recurse
16365
16366 if Ekind (Ent) = E_Package then
16367 Restore_Nested_Formal (Ent);
16368 end if;
16369
16370 Next_Entity (Ent);
16371 end loop;
16372 end if;
16373 end Restore_Nested_Formal;
16374
16375 -- Start of processing for Restore_Private_Views
16376
16377 begin
16378 M := First_Elmt (Exchanged_Views);
16379 while Present (M) loop
16380 Typ := Node (M);
16381
16382 -- Subtypes of types whose views have been exchanged, and that are
16383 -- defined within the instance, were not on the Private_Dependents
16384 -- list on entry to the instance, so they have to be exchanged
16385 -- explicitly now, in order to remain consistent with the view of the
16386 -- parent type.
16387
16388 if Ekind (Typ) in E_Private_Type
16389 | E_Limited_Private_Type
16390 | E_Record_Type_With_Private
16391 then
16392 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
16393 while Present (Dep_Elmt) loop
16394 Dep_Typ := Node (Dep_Elmt);
16395
16396 if Scope (Dep_Typ) = Pack_Id
16397 and then Present (Full_View (Dep_Typ))
16398 then
16399 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
16400 Exchange_Declarations (Dep_Typ);
16401 end if;
16402
16403 Next_Elmt (Dep_Elmt);
16404 end loop;
16405 end if;
16406
16407 Exchange_Declarations (Typ);
16408 Next_Elmt (M);
16409 end loop;
16410
16411 if No (Pack_Id) then
16412 return;
16413 end if;
16414
16415 -- Make the generic formal parameters private, and make the formal types
16416 -- into subtypes of the actuals again.
16417
16418 E := First_Entity (Pack_Id);
16419 while Present (E) loop
16420 Set_Is_Hidden (E, True);
16421
16422 if Is_Type (E)
16423 and then Nkind (Parent (E)) = N_Subtype_Declaration
16424 then
16425 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
16426 -- as it is needed to identify the subtype with the type it
16427 -- renames, when there are conversions between access types
16428 -- to these.
16429
16430 if GNATprove_Mode then
16431 null;
16432
16433 -- If the actual for E is itself a generic actual type from
16434 -- an enclosing instance, E is still a generic actual type
16435 -- outside of the current instance. This matter when resolving
16436 -- an overloaded call that may be ambiguous in the enclosing
16437 -- instance, when two of its actuals coincide.
16438
16439 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
16440 and then Is_Generic_Actual_Type
16441 (Entity (Subtype_Indication (Parent (E))))
16442 then
16443 null;
16444 else
16445 Set_Is_Generic_Actual_Type (E, False);
16446
16447 -- It might seem reasonable to clear the Is_Generic_Actual_Type
16448 -- flag also on the Full_View if the type is private, since it
16449 -- was set also on this Full_View. However, this flag is relied
16450 -- upon by Covers to spot "types exported from instantiations"
16451 -- which are implicit Full_Views built for instantiations made
16452 -- on private types and we get type mismatches if we do it when
16453 -- the block exchanging the declarations below triggers ???
16454
16455 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
16456 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
16457 -- end if;
16458 end if;
16459
16460 -- An unusual case of aliasing: the actual may also be directly
16461 -- visible in the generic, and be private there, while it is fully
16462 -- visible in the context of the instance. The internal subtype
16463 -- is private in the instance but has full visibility like its
16464 -- parent in the enclosing scope. This enforces the invariant that
16465 -- the privacy status of all private dependents of a type coincide
16466 -- with that of the parent type. This can only happen when a
16467 -- generic child unit is instantiated within a sibling.
16468
16469 if Is_Private_Type (E)
16470 and then not Is_Private_Type (Etype (E))
16471 then
16472 Exchange_Declarations (E);
16473 end if;
16474
16475 elsif Ekind (E) = E_Package then
16476
16477 -- The end of the renaming list is the renaming of the generic
16478 -- package itself. If the instance is a subprogram, all entities
16479 -- in the corresponding package are renamings. If this entity is
16480 -- a formal package, make its own formals private as well. The
16481 -- actual in this case is itself the renaming of an instantiation.
16482 -- If the entity is not a package renaming, it is the entity
16483 -- created to validate formal package actuals: ignore it.
16484
16485 -- If the actual is itself a formal package for the enclosing
16486 -- generic, or the actual for such a formal package, it remains
16487 -- visible on exit from the instance, and therefore nothing needs
16488 -- to be done either, except to keep it accessible.
16489
16490 if Is_Package and then Renamed_Entity (E) = Pack_Id then
16491 exit;
16492
16493 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
16494 null;
16495
16496 elsif
16497 Denotes_Formal_Package (Renamed_Entity (E), True, Pack_Id)
16498 then
16499 Set_Is_Hidden (E, False);
16500
16501 else
16502 declare
16503 Act_P : constant Entity_Id := Renamed_Entity (E);
16504 Id : Entity_Id;
16505
16506 begin
16507 Id := First_Entity (Act_P);
16508 while Present (Id)
16509 and then Id /= First_Private_Entity (Act_P)
16510 loop
16511 exit when Ekind (Id) = E_Package
16512 and then Renamed_Entity (Id) = Act_P;
16513
16514 Set_Is_Hidden (Id, True);
16515 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
16516
16517 if Ekind (Id) = E_Package then
16518 Restore_Nested_Formal (Id);
16519 end if;
16520
16521 Next_Entity (Id);
16522 end loop;
16523 end;
16524 end if;
16525 end if;
16526
16527 Next_Entity (E);
16528 end loop;
16529 end Restore_Private_Views;
16530
16531 --------------
16532 -- Save_Env --
16533 --------------
16534
16535 procedure Save_Env
16536 (Gen_Unit : Entity_Id;
16537 Act_Unit : Entity_Id)
16538 is
16539 begin
16540 Init_Env;
16541 Set_Instance_Env (Gen_Unit, Act_Unit);
16542 end Save_Env;
16543
16544 ----------------------------
16545 -- Save_Global_References --
16546 ----------------------------
16547
16548 procedure Save_Global_References (Templ : Node_Id) is
16549
16550 -- ??? it is horrible to use global variables in highly recursive code
16551
16552 E : Entity_Id;
16553 -- The entity of the current associated node
16554
16555 Gen_Scope : Entity_Id;
16556 -- The scope of the generic for which references are being saved
16557
16558 N2 : Node_Id;
16559 -- The current associated node
16560
16561 function Is_Global (E : Entity_Id) return Boolean;
16562 -- Check whether entity is defined outside of generic unit. Examine the
16563 -- scope of an entity, and the scope of the scope, etc, until we find
16564 -- either Standard, in which case the entity is global, or the generic
16565 -- unit itself, which indicates that the entity is local. If the entity
16566 -- is the generic unit itself, as in the case of a recursive call, or
16567 -- the enclosing generic unit, if different from the current scope, then
16568 -- it is local as well, because it will be replaced at the point of
16569 -- instantiation. On the other hand, if it is a reference to a child
16570 -- unit of a common ancestor, which appears in an instantiation, it is
16571 -- global because it is used to denote a specific compilation unit at
16572 -- the time the instantiations will be analyzed.
16573
16574 procedure Qualify_Universal_Operands
16575 (Op : Node_Id;
16576 Func_Call : Node_Id);
16577 -- Op denotes a binary or unary operator in generic template Templ. Node
16578 -- Func_Call is the function call alternative of the operator within the
16579 -- the analyzed copy of the template. Change each operand which yields a
16580 -- universal type by wrapping it into a qualified expression
16581 --
16582 -- Actual_Typ'(Operand)
16583 --
16584 -- where Actual_Typ is the type of corresponding actual parameter of
16585 -- Operand in Func_Call.
16586
16587 procedure Reset_Entity (N : Node_Id);
16588 -- Save semantic information on global entity so that it is not resolved
16589 -- again at instantiation time.
16590
16591 procedure Save_Entity_Descendants (N : Node_Id);
16592 -- Apply Save_Global_References to the two syntactic descendants of
16593 -- non-terminal nodes that carry an Associated_Node and are processed
16594 -- through Reset_Entity. Once the global entity (if any) has been
16595 -- captured together with its type, only two syntactic descendants need
16596 -- to be traversed to complete the processing of the tree rooted at N.
16597 -- This applies to Selected_Components, Expanded_Names, and to Operator
16598 -- nodes. N can also be a character literal, identifier, or operator
16599 -- symbol node, but the call has no effect in these cases.
16600
16601 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
16602 -- Default actuals in nested instances must be handled specially
16603 -- because there is no link to them from the original tree. When an
16604 -- actual subprogram is given by a default, we add an explicit generic
16605 -- association for it in the instantiation node. When we save the
16606 -- global references on the name of the instance, we recover the list
16607 -- of generic associations, and add an explicit one to the original
16608 -- generic tree, through which a global actual can be preserved.
16609 -- Similarly, if a child unit is instantiated within a sibling, in the
16610 -- context of the parent, we must preserve the identifier of the parent
16611 -- so that it can be properly resolved in a subsequent instantiation.
16612
16613 procedure Save_Global_Descendant (D : Union_Id);
16614 -- Apply Save_References recursively to the descendants of node D
16615
16616 procedure Save_References (N : Node_Id);
16617 -- This is the recursive procedure that does the work, once the
16618 -- enclosing generic scope has been established.
16619
16620 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
16621 -- If the type of N2 is global to the generic unit, save the type in
16622 -- the generic node. Just as we perform name capture for explicit
16623 -- references within the generic, we must capture the global types
16624 -- of local entities because they may participate in resolution in
16625 -- the instance.
16626
16627 ---------------
16628 -- Is_Global --
16629 ---------------
16630
16631 function Is_Global (E : Entity_Id) return Boolean is
16632 Se : Entity_Id;
16633
16634 function Is_Instance_Node (Decl : Node_Id) return Boolean;
16635 -- Determine whether the parent node of a reference to a child unit
16636 -- denotes an instantiation or a formal package, in which case the
16637 -- reference to the child unit is global, even if it appears within
16638 -- the current scope (e.g. when the instance appears within the body
16639 -- of an ancestor).
16640
16641 ----------------------
16642 -- Is_Instance_Node --
16643 ----------------------
16644
16645 function Is_Instance_Node (Decl : Node_Id) return Boolean is
16646 begin
16647 return Nkind (Decl) in N_Generic_Instantiation
16648 or else
16649 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
16650 end Is_Instance_Node;
16651
16652 -- Start of processing for Is_Global
16653
16654 begin
16655 if E = Gen_Scope then
16656 return False;
16657
16658 elsif E = Standard_Standard then
16659 return True;
16660
16661 -- E should be an entity, but it is not always
16662
16663 elsif Nkind (E) not in N_Entity then
16664 return False;
16665
16666 elsif Nkind (E) /= N_Expanded_Name
16667 and then Is_Child_Unit (E)
16668 and then (Is_Instance_Node (Parent (N2))
16669 or else (Nkind (Parent (N2)) = N_Expanded_Name
16670 and then N2 = Selector_Name (Parent (N2))
16671 and then
16672 Is_Instance_Node (Parent (Parent (N2)))))
16673 then
16674 return True;
16675
16676 else
16677 -- E may be an expanded name - typically an operator - in which
16678 -- case we must find its enclosing scope since expanded names
16679 -- don't have corresponding scopes.
16680
16681 if Nkind (E) = N_Expanded_Name then
16682 Se := Find_Enclosing_Scope (E);
16683
16684 -- Otherwise, E is an entity and will have Scope set
16685
16686 else
16687 Se := Scope (E);
16688 end if;
16689
16690 while Se /= Gen_Scope loop
16691 if Se = Standard_Standard then
16692 return True;
16693 else
16694 Se := Scope (Se);
16695 end if;
16696 end loop;
16697
16698 return False;
16699 end if;
16700 end Is_Global;
16701
16702 --------------------------------
16703 -- Qualify_Universal_Operands --
16704 --------------------------------
16705
16706 procedure Qualify_Universal_Operands
16707 (Op : Node_Id;
16708 Func_Call : Node_Id)
16709 is
16710 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
16711 -- Rewrite operand Opnd as a qualified expression of the form
16712 --
16713 -- Actual_Typ'(Opnd)
16714 --
16715 -- where Actual is the corresponding actual parameter of Opnd in
16716 -- function call Func_Call.
16717
16718 function Qualify_Type
16719 (Loc : Source_Ptr;
16720 Typ : Entity_Id) return Node_Id;
16721 -- Qualify type Typ by creating a selected component of the form
16722 --
16723 -- Scope_Of_Typ.Typ
16724
16725 ---------------------
16726 -- Qualify_Operand --
16727 ---------------------
16728
16729 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
16730 Loc : constant Source_Ptr := Sloc (Opnd);
16731 Typ : constant Entity_Id := Etype (Actual);
16732 Mark : Node_Id;
16733 Qual : Node_Id;
16734
16735 begin
16736 -- Qualify the operand when it is of a universal type. Note that
16737 -- the template is unanalyzed and it is not possible to directly
16738 -- query the type. This transformation is not done when the type
16739 -- of the actual is internally generated because the type will be
16740 -- regenerated in the instance.
16741
16742 if Yields_Universal_Type (Opnd)
16743 and then Comes_From_Source (Typ)
16744 and then not Is_Hidden (Typ)
16745 then
16746 -- The type of the actual may be a global reference. Save this
16747 -- information by creating a reference to it.
16748
16749 if Is_Global (Typ) then
16750 Mark := New_Occurrence_Of (Typ, Loc);
16751
16752 -- Otherwise rely on resolution to find the proper type within
16753 -- the instance.
16754
16755 else
16756 Mark := Qualify_Type (Loc, Typ);
16757 end if;
16758
16759 Qual :=
16760 Make_Qualified_Expression (Loc,
16761 Subtype_Mark => Mark,
16762 Expression => Relocate_Node (Opnd));
16763
16764 -- Mark the qualification to distinguish it from other source
16765 -- constructs and signal the instantiation mechanism that this
16766 -- node requires special processing. See Copy_Generic_Node for
16767 -- details.
16768
16769 Set_Is_Qualified_Universal_Literal (Qual);
16770
16771 Rewrite (Opnd, Qual);
16772 end if;
16773 end Qualify_Operand;
16774
16775 ------------------
16776 -- Qualify_Type --
16777 ------------------
16778
16779 function Qualify_Type
16780 (Loc : Source_Ptr;
16781 Typ : Entity_Id) return Node_Id
16782 is
16783 Scop : constant Entity_Id := Scope (Typ);
16784 Result : Node_Id;
16785
16786 begin
16787 Result := Make_Identifier (Loc, Chars (Typ));
16788
16789 if Present (Scop) and then not Is_Generic_Unit (Scop) then
16790 Result :=
16791 Make_Selected_Component (Loc,
16792 Prefix => Make_Identifier (Loc, Chars (Scop)),
16793 Selector_Name => Result);
16794 end if;
16795
16796 return Result;
16797 end Qualify_Type;
16798
16799 -- Local variables
16800
16801 Actuals : constant List_Id := Parameter_Associations (Func_Call);
16802
16803 -- Start of processing for Qualify_Universal_Operands
16804
16805 begin
16806 if Nkind (Op) in N_Binary_Op then
16807 Qualify_Operand (Left_Opnd (Op), First (Actuals));
16808 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
16809
16810 elsif Nkind (Op) in N_Unary_Op then
16811 Qualify_Operand (Right_Opnd (Op), First (Actuals));
16812 end if;
16813 end Qualify_Universal_Operands;
16814
16815 ------------------
16816 -- Reset_Entity --
16817 ------------------
16818
16819 procedure Reset_Entity (N : Node_Id) is
16820 function Top_Ancestor (E : Entity_Id) return Entity_Id;
16821 -- Find the ultimate ancestor of the current unit. If it is not a
16822 -- generic unit, then the name of the current unit in the prefix of
16823 -- an expanded name must be replaced with its generic homonym to
16824 -- ensure that it will be properly resolved in an instance.
16825
16826 ------------------
16827 -- Top_Ancestor --
16828 ------------------
16829
16830 function Top_Ancestor (E : Entity_Id) return Entity_Id is
16831 Par : Entity_Id;
16832
16833 begin
16834 Par := E;
16835 while Is_Child_Unit (Par) loop
16836 Par := Scope (Par);
16837 end loop;
16838
16839 return Par;
16840 end Top_Ancestor;
16841
16842 -- Start of processing for Reset_Entity
16843
16844 begin
16845 N2 := Get_Associated_Node (N);
16846 E := Entity (N2);
16847
16848 if Present (E) then
16849
16850 -- If the node is an entry call to an entry in an enclosing task,
16851 -- it is rewritten as a selected component. No global entity to
16852 -- preserve in this case, since the expansion will be redone in
16853 -- the instance.
16854
16855 if Nkind (E) not in N_Entity then
16856 Set_Associated_Node (N, Empty);
16857 Set_Etype (N, Empty);
16858 return;
16859 end if;
16860
16861 -- If the entity is an itype created as a subtype of an access
16862 -- type with a null exclusion restore source entity for proper
16863 -- visibility. The itype will be created anew in the instance.
16864
16865 if Is_Itype (E)
16866 and then Ekind (E) = E_Access_Subtype
16867 and then Is_Entity_Name (N)
16868 and then Chars (Etype (E)) = Chars (N)
16869 then
16870 E := Etype (E);
16871 Set_Entity (N2, E);
16872 Set_Etype (N2, E);
16873 end if;
16874
16875 if Is_Global (E) then
16876 Set_Global_Type (N, N2);
16877
16878 elsif Nkind (N) = N_Op_Concat
16879 and then Is_Generic_Type (Etype (N2))
16880 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
16881 or else
16882 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
16883 and then Is_Intrinsic_Subprogram (E)
16884 then
16885 null;
16886
16887 -- Entity is local. Mark generic node as unresolved. Note that now
16888 -- it does not have an entity.
16889
16890 else
16891 Set_Associated_Node (N, Empty);
16892 Set_Etype (N, Empty);
16893 end if;
16894
16895 if Nkind (Parent (N)) in N_Generic_Instantiation
16896 and then N = Name (Parent (N))
16897 then
16898 Save_Global_Defaults (Parent (N), Parent (N2));
16899 end if;
16900
16901 elsif Nkind (Parent (N)) = N_Selected_Component
16902 and then Nkind (Parent (N2)) = N_Expanded_Name
16903 then
16904 -- In case of previous errors, the tree might be malformed
16905
16906 if No (Entity (Parent (N2))) then
16907 null;
16908
16909 elsif Is_Global (Entity (Parent (N2))) then
16910 Change_Selected_Component_To_Expanded_Name (Parent (N));
16911 Set_Associated_Node (Parent (N), Parent (N2));
16912 Set_Global_Type (Parent (N), Parent (N2));
16913 Save_Entity_Descendants (N);
16914
16915 -- If this is a reference to the current generic entity, replace
16916 -- by the name of the generic homonym of the current package. This
16917 -- is because in an instantiation Par.P.Q will not resolve to the
16918 -- name of the instance, whose enclosing scope is not necessarily
16919 -- Par. We use the generic homonym rather that the name of the
16920 -- generic itself because it may be hidden by a local declaration.
16921
16922 elsif In_Open_Scopes (Entity (Parent (N2)))
16923 and then not
16924 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
16925 then
16926 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
16927 Rewrite (Parent (N),
16928 Make_Identifier (Sloc (N),
16929 Chars =>
16930 Chars (Generic_Homonym (Entity (Parent (N2))))));
16931 else
16932 Rewrite (Parent (N),
16933 Make_Identifier (Sloc (N),
16934 Chars => Chars (Selector_Name (Parent (N2)))));
16935 end if;
16936 end if;
16937
16938 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
16939 and then Parent (N) = Name (Parent (Parent (N)))
16940 then
16941 Save_Global_Defaults
16942 (Parent (Parent (N)), Parent (Parent (N2)));
16943 end if;
16944
16945 -- A selected component may denote a static constant that has been
16946 -- folded. If the static constant is global to the generic, capture
16947 -- its value. Otherwise the folding will happen in any instantiation.
16948
16949 elsif Nkind (Parent (N)) = N_Selected_Component
16950 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
16951 then
16952 if Present (Entity (Original_Node (Parent (N2))))
16953 and then Is_Global (Entity (Original_Node (Parent (N2))))
16954 then
16955 Rewrite (Parent (N), New_Copy (Parent (N2)));
16956 Set_Analyzed (Parent (N), False);
16957 end if;
16958
16959 -- A selected component may be transformed into a parameterless
16960 -- function call. If the called entity is global, rewrite the node
16961 -- appropriately, i.e. as an extended name for the global entity.
16962
16963 elsif Nkind (Parent (N)) = N_Selected_Component
16964 and then Nkind (Parent (N2)) = N_Function_Call
16965 and then N = Selector_Name (Parent (N))
16966 then
16967 if No (Parameter_Associations (Parent (N2))) then
16968 if Is_Global (Entity (Name (Parent (N2)))) then
16969 Change_Selected_Component_To_Expanded_Name (Parent (N));
16970 Set_Associated_Node (Parent (N), Name (Parent (N2)));
16971 Set_Global_Type (Parent (N), Name (Parent (N2)));
16972 Save_Entity_Descendants (N);
16973
16974 else
16975 Set_Is_Prefixed_Call (Parent (N));
16976 Set_Associated_Node (N, Empty);
16977 Set_Etype (N, Empty);
16978 end if;
16979
16980 -- In Ada 2005, X.F may be a call to a primitive operation,
16981 -- rewritten as F (X). This rewriting will be done again in an
16982 -- instance, so keep the original node. Global entities will be
16983 -- captured as for other constructs. Indicate that this must
16984 -- resolve as a call, to prevent accidental overloading in the
16985 -- instance, if both a component and a primitive operation appear
16986 -- as candidates.
16987
16988 else
16989 Set_Is_Prefixed_Call (Parent (N));
16990 end if;
16991
16992 -- Entity is local. Reset in generic unit, so that node is resolved
16993 -- anew at the point of instantiation.
16994
16995 else
16996 Set_Associated_Node (N, Empty);
16997 Set_Etype (N, Empty);
16998 end if;
16999 end Reset_Entity;
17000
17001 -----------------------------
17002 -- Save_Entity_Descendants --
17003 -----------------------------
17004
17005 procedure Save_Entity_Descendants (N : Node_Id) is
17006 begin
17007 case Nkind (N) is
17008 when N_Binary_Op =>
17009 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
17010 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
17011
17012 when N_Unary_Op =>
17013 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
17014
17015 when N_Expanded_Name
17016 | N_Selected_Component
17017 =>
17018 Save_Global_Descendant (Union_Id (Prefix (N)));
17019 Save_Global_Descendant (Union_Id (Selector_Name (N)));
17020
17021 when N_Character_Literal
17022 | N_Identifier
17023 | N_Operator_Symbol
17024 =>
17025 null;
17026
17027 when others =>
17028 raise Program_Error;
17029 end case;
17030 end Save_Entity_Descendants;
17031
17032 --------------------------
17033 -- Save_Global_Defaults --
17034 --------------------------
17035
17036 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
17037 Loc : constant Source_Ptr := Sloc (N1);
17038 Assoc2 : constant List_Id := Generic_Associations (N2);
17039 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
17040 Assoc1 : List_Id;
17041 Act1 : Node_Id;
17042 Act2 : Node_Id;
17043 Def : Node_Id;
17044 Ndec : Node_Id;
17045 Subp : Entity_Id;
17046 Actual : Entity_Id;
17047
17048 begin
17049 Assoc1 := Generic_Associations (N1);
17050
17051 if Present (Assoc1) then
17052 Act1 := First (Assoc1);
17053 else
17054 Act1 := Empty;
17055 Set_Generic_Associations (N1, New_List);
17056 Assoc1 := Generic_Associations (N1);
17057 end if;
17058
17059 if Present (Assoc2) then
17060 Act2 := First (Assoc2);
17061 else
17062 return;
17063 end if;
17064
17065 while Present (Act1) and then Present (Act2) loop
17066 Next (Act1);
17067 Next (Act2);
17068 end loop;
17069
17070 -- Find the associations added for default subprograms
17071
17072 if Present (Act2) then
17073 while Nkind (Act2) /= N_Generic_Association
17074 or else No (Entity (Selector_Name (Act2)))
17075 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
17076 loop
17077 Next (Act2);
17078 end loop;
17079
17080 -- Add a similar association if the default is global. The
17081 -- renaming declaration for the actual has been analyzed, and
17082 -- its alias is the program it renames. Link the actual in the
17083 -- original generic tree with the node in the analyzed tree.
17084
17085 while Present (Act2) loop
17086 Subp := Entity (Selector_Name (Act2));
17087 Def := Explicit_Generic_Actual_Parameter (Act2);
17088
17089 -- Following test is defence against rubbish errors
17090
17091 if No (Alias (Subp)) then
17092 return;
17093 end if;
17094
17095 -- Retrieve the resolved actual from the renaming declaration
17096 -- created for the instantiated formal.
17097
17098 Actual := Entity (Name (Parent (Parent (Subp))));
17099 Set_Entity (Def, Actual);
17100 Set_Etype (Def, Etype (Actual));
17101
17102 if Is_Global (Actual) then
17103 Ndec :=
17104 Make_Generic_Association (Loc,
17105 Selector_Name =>
17106 New_Occurrence_Of (Subp, Loc),
17107 Explicit_Generic_Actual_Parameter =>
17108 New_Occurrence_Of (Actual, Loc));
17109
17110 Set_Associated_Node
17111 (Explicit_Generic_Actual_Parameter (Ndec), Def);
17112
17113 Append (Ndec, Assoc1);
17114
17115 -- If there are other defaults, add a dummy association in case
17116 -- there are other defaulted formals with the same name.
17117 -- Note that we are creating an N_Generic_Association with
17118 -- neither Explicit_Generic_Actual_Parameter nor Box_Present.
17119
17120 elsif Present (Next (Act2)) then
17121 Ndec :=
17122 Make_Generic_Association (Loc,
17123 Selector_Name => New_Occurrence_Of (Subp, Loc),
17124 Explicit_Generic_Actual_Parameter => Empty);
17125
17126 Append (Ndec, Assoc1);
17127 end if;
17128
17129 Next (Act2);
17130 end loop;
17131 end if;
17132
17133 if Nkind (Name (N1)) = N_Identifier
17134 and then Is_Child_Unit (Gen_Id)
17135 and then Is_Global (Gen_Id)
17136 and then Is_Generic_Unit (Scope (Gen_Id))
17137 and then In_Open_Scopes (Scope (Gen_Id))
17138 then
17139 -- This is an instantiation of a child unit within a sibling, so
17140 -- that the generic parent is in scope. An eventual instance must
17141 -- occur within the scope of an instance of the parent. Make name
17142 -- in instance into an expanded name, to preserve the identifier
17143 -- of the parent, so it can be resolved subsequently.
17144
17145 Rewrite (Name (N2),
17146 Make_Expanded_Name (Loc,
17147 Chars => Chars (Gen_Id),
17148 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
17149 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
17150 Set_Entity (Name (N2), Gen_Id);
17151
17152 Rewrite (Name (N1),
17153 Make_Expanded_Name (Loc,
17154 Chars => Chars (Gen_Id),
17155 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
17156 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
17157
17158 Set_Associated_Node (Name (N1), Name (N2));
17159 Set_Associated_Node (Prefix (Name (N1)), Empty);
17160 Set_Associated_Node
17161 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
17162 Set_Etype (Name (N1), Etype (Gen_Id));
17163 end if;
17164 end Save_Global_Defaults;
17165
17166 ----------------------------
17167 -- Save_Global_Descendant --
17168 ----------------------------
17169
17170 procedure Save_Global_Descendant (D : Union_Id) is
17171 N1 : Node_Id;
17172
17173 begin
17174 if D in Node_Range then
17175 if D = Union_Id (Empty) then
17176 null;
17177
17178 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
17179 Save_References (Node_Id (D));
17180 end if;
17181
17182 elsif D in List_Range then
17183 pragma Assert (D /= Union_Id (No_List));
17184 -- Because No_List = Empty, which is in Node_Range above
17185
17186 N1 := First (List_Id (D));
17187 while Present (N1) loop
17188 Save_References (N1);
17189 Next (N1);
17190 end loop;
17191
17192 -- Element list or other non-node field, nothing to do
17193
17194 else
17195 null;
17196 end if;
17197 end Save_Global_Descendant;
17198
17199 ---------------------
17200 -- Save_References --
17201 ---------------------
17202
17203 -- This is the recursive procedure that does the work once the enclosing
17204 -- generic scope has been established. We have to treat specially a
17205 -- number of node rewritings that are required by semantic processing
17206 -- and which change the kind of nodes in the generic copy: typically
17207 -- constant-folding, replacing an operator node by a string literal, or
17208 -- a selected component by an expanded name. In each of those cases, the
17209 -- transformation is propagated to the generic unit.
17210
17211 procedure Save_References (N : Node_Id) is
17212 Loc : constant Source_Ptr := Sloc (N);
17213
17214 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
17215 -- Determine whether arbitrary node Nod requires delayed capture of
17216 -- global references within its aspect specifications.
17217
17218 procedure Save_References_In_Aggregate (N : Node_Id);
17219 -- Save all global references in [extension] aggregate node N
17220
17221 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
17222 -- Save all global references in a character literal or operator
17223 -- symbol denoted by N.
17224
17225 procedure Save_References_In_Descendants (N : Node_Id);
17226 -- Save all global references in all descendants of node N
17227
17228 procedure Save_References_In_Identifier (N : Node_Id);
17229 -- Save all global references in identifier node N
17230
17231 procedure Save_References_In_Operator (N : Node_Id);
17232 -- Save all global references in operator node N
17233
17234 procedure Save_References_In_Pragma (Prag : Node_Id);
17235 -- Save all global references found within the expression of pragma
17236 -- Prag.
17237
17238 ---------------------------
17239 -- Requires_Delayed_Save --
17240 ---------------------------
17241
17242 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
17243 begin
17244 -- Generic packages and subprograms require delayed capture of
17245 -- global references within their aspects due to the timing of
17246 -- annotation analysis.
17247
17248 if Nkind (Nod) in N_Generic_Package_Declaration
17249 | N_Generic_Subprogram_Declaration
17250 | N_Package_Body
17251 | N_Package_Body_Stub
17252 | N_Subprogram_Body
17253 | N_Subprogram_Body_Stub
17254 then
17255 -- Since the capture of global references is done on the
17256 -- unanalyzed generic template, there is no information around
17257 -- to infer the context. Use the Associated_Entity linkages to
17258 -- peek into the analyzed generic copy and determine what the
17259 -- template corresponds to.
17260
17261 if Nod = Templ then
17262 return
17263 Is_Generic_Declaration_Or_Body
17264 (Unit_Declaration_Node
17265 (Get_Associated_Entity (Defining_Entity (Nod))));
17266
17267 -- Otherwise the generic unit being processed is not the top
17268 -- level template. It is safe to capture of global references
17269 -- within the generic unit because at this point the top level
17270 -- copy is fully analyzed.
17271
17272 else
17273 return False;
17274 end if;
17275
17276 -- Otherwise capture the global references without interference
17277
17278 else
17279 return False;
17280 end if;
17281 end Requires_Delayed_Save;
17282
17283 ----------------------------------
17284 -- Save_References_In_Aggregate --
17285 ----------------------------------
17286
17287 procedure Save_References_In_Aggregate (N : Node_Id) is
17288 Nam : Node_Id;
17289 Qual : Node_Id := Empty;
17290 Typ : Entity_Id := Empty;
17291
17292 begin
17293 N2 := Get_Associated_Node (N);
17294
17295 if Present (N2) then
17296 Typ := Etype (N2);
17297
17298 -- In an instance within a generic, use the name of the actual
17299 -- and not the original generic parameter. If the actual is
17300 -- global in the current generic it must be preserved for its
17301 -- instantiation.
17302
17303 if Parent_Kind (Typ) = N_Subtype_Declaration
17304 and then Present (Generic_Parent_Type (Parent (Typ)))
17305 then
17306 Typ := Base_Type (Typ);
17307 Set_Etype (N2, Typ);
17308 end if;
17309 end if;
17310
17311 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
17312 Set_Associated_Node (N, Empty);
17313
17314 -- For a full aggregate, if the type is local but is a derived
17315 -- tagged type of a global ancestor, we will need to have the
17316 -- full view of this global ancestor available in the instance
17317 -- in order to analyze the full aggregate.
17318
17319 if Present (N2)
17320 and then Nkind (N2) = N_Aggregate
17321 and then Present (Typ)
17322 and then Is_Tagged_Type (Typ)
17323 and then Is_Derived_Type (Typ)
17324 then
17325 declare
17326 Root_Typ : constant Entity_Id := Root_Type (Typ);
17327
17328 Parent_Typ : Entity_Id := Typ;
17329
17330 begin
17331 loop
17332 Parent_Typ := Etype (Parent_Typ);
17333
17334 if Is_Global (Parent_Typ) then
17335 Set_Ancestor_Type (N, Parent_Typ);
17336 exit;
17337 end if;
17338
17339 exit when Parent_Typ = Root_Typ;
17340 end loop;
17341 end;
17342 end if;
17343
17344 -- If the aggregate is an actual in a call, it has been
17345 -- resolved in the current context, to some local type. The
17346 -- enclosing call may have been disambiguated by the aggregate,
17347 -- and this disambiguation might fail at instantiation time
17348 -- because the type to which the aggregate did resolve is not
17349 -- preserved. In order to preserve some of this information,
17350 -- wrap the aggregate in a qualified expression, using the id
17351 -- of its type. For further disambiguation we qualify the type
17352 -- name with its scope (if visible and not hidden by a local
17353 -- homograph) because both id's will have corresponding
17354 -- entities in an instance. This resolves most of the problems
17355 -- with missing type information on aggregates in instances.
17356
17357 if Present (N2)
17358 and then Nkind (N2) = Nkind (N)
17359 and then Nkind (Parent (N2)) in N_Subprogram_Call
17360 and then Present (Typ)
17361 and then Comes_From_Source (Typ)
17362 then
17363 Nam := Make_Identifier (Loc, Chars (Typ));
17364
17365 if Is_Immediately_Visible (Scope (Typ))
17366 and then
17367 (not In_Open_Scopes (Scope (Typ))
17368 or else Current_Entity (Scope (Typ)) = Scope (Typ))
17369 then
17370 Nam :=
17371 Make_Selected_Component (Loc,
17372 Prefix =>
17373 Make_Identifier (Loc, Chars (Scope (Typ))),
17374 Selector_Name => Nam);
17375 end if;
17376
17377 Qual :=
17378 Make_Qualified_Expression (Loc,
17379 Subtype_Mark => Nam,
17380 Expression => Relocate_Node (N));
17381 end if;
17382
17383 -- For a full aggregate, if the type is global and a derived
17384 -- tagged type, we will also need to have the full view of its
17385 -- ancestor available in the instance in order to analyze the
17386 -- full aggregate.
17387
17388 elsif Present (N2)
17389 and then Nkind (N2) = N_Aggregate
17390 and then Present (Typ)
17391 and then Is_Tagged_Type (Typ)
17392 and then Is_Derived_Type (Typ)
17393 then
17394 Set_Ancestor_Type (N, Etype (Typ));
17395 end if;
17396
17397 if Nkind (N) = N_Aggregate then
17398 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
17399
17400 elsif Nkind (N) = N_Extension_Aggregate then
17401 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
17402
17403 else
17404 pragma Assert (False);
17405 end if;
17406
17407 Save_Global_Descendant (Union_Id (Expressions (N)));
17408 Save_Global_Descendant (Union_Id (Component_Associations (N)));
17409 Save_Global_Descendant (Union_Id (Etype (N)));
17410
17411 if Present (Qual) then
17412 Rewrite (N, Qual);
17413 end if;
17414 end Save_References_In_Aggregate;
17415
17416 ----------------------------------------------
17417 -- Save_References_In_Char_Lit_Or_Op_Symbol --
17418 ----------------------------------------------
17419
17420 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
17421 begin
17422 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
17423 Reset_Entity (N);
17424
17425 elsif Nkind (N) = N_Operator_Symbol
17426 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
17427 then
17428 Change_Operator_Symbol_To_String_Literal (N);
17429 end if;
17430 end Save_References_In_Char_Lit_Or_Op_Symbol;
17431
17432 ------------------------------------
17433 -- Save_References_In_Descendants --
17434 ------------------------------------
17435
17436 procedure Save_References_In_Descendants (N : Node_Id) is
17437 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
17438 begin
17439 Walk (N);
17440 end Save_References_In_Descendants;
17441
17442 -----------------------------------
17443 -- Save_References_In_Identifier --
17444 -----------------------------------
17445
17446 procedure Save_References_In_Identifier (N : Node_Id) is
17447 begin
17448 -- The node did not undergo a transformation
17449
17450 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
17451 -- If this is a discriminant reference, always save it.
17452 -- It is used in the instance to find the corresponding
17453 -- discriminant positionally rather than by name.
17454
17455 Set_Original_Discriminant
17456 (N, Original_Discriminant (Get_Associated_Node (N)));
17457
17458 Reset_Entity (N);
17459
17460 -- The analysis of the generic copy transformed the identifier
17461 -- into another construct. Propagate the changes to the template.
17462
17463 else
17464 N2 := Get_Associated_Node (N);
17465
17466 -- The identifier denotes a call to a parameterless function.
17467 -- Mark the node as resolved when the function is external.
17468
17469 if Nkind (N2) = N_Function_Call then
17470 E := Entity (Name (N2));
17471
17472 if Present (E) and then Is_Global (E) then
17473 Set_Global_Type (N, N2);
17474 else
17475 Set_Associated_Node (N, Empty);
17476 Set_Etype (N, Empty);
17477 end if;
17478
17479 -- The identifier denotes a named number that was constant
17480 -- folded. Preserve the original name for ASIS and undo the
17481 -- constant folding which will be repeated in the instance.
17482 -- Is this still needed???
17483
17484 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
17485 and then Is_Entity_Name (Original_Node (N2))
17486 then
17487 Set_Associated_Node (N, Original_Node (N2));
17488 Reset_Entity (N);
17489
17490 -- The identifier resolved to a string literal. Propagate this
17491 -- information to the generic template.
17492
17493 elsif Nkind (N2) = N_String_Literal then
17494 Rewrite (N, New_Copy (N2));
17495
17496 -- The identifier is rewritten as a dereference if it is the
17497 -- prefix of an implicit dereference. Preserve the original
17498 -- tree as the analysis of the instance will expand the node
17499 -- again, but preserve the resolved entity if it is global.
17500
17501 elsif Nkind (N2) = N_Explicit_Dereference then
17502 if Is_Entity_Name (Prefix (N2))
17503 and then Present (Entity (Prefix (N2)))
17504 and then Is_Global (Entity (Prefix (N2)))
17505 then
17506 Set_Associated_Node (N, Prefix (N2));
17507 Set_Global_Type (N, Prefix (N2));
17508
17509 elsif Nkind (Prefix (N2)) = N_Function_Call
17510 and then Is_Entity_Name (Name (Prefix (N2)))
17511 and then Present (Entity (Name (Prefix (N2))))
17512 and then Is_Global (Entity (Name (Prefix (N2))))
17513 then
17514 Rewrite (N,
17515 Make_Explicit_Dereference (Loc,
17516 Prefix =>
17517 Make_Function_Call (Loc,
17518 Name =>
17519 New_Occurrence_Of
17520 (Entity (Name (Prefix (N2))), Loc))));
17521 Set_Associated_Node
17522 (Name (Prefix (N)), Name (Prefix (N2)));
17523 Set_Global_Type (Name (Prefix (N)), Name (Prefix (N2)));
17524
17525 else
17526 Set_Associated_Node (N, Empty);
17527 Set_Etype (N, Empty);
17528 end if;
17529
17530 -- The subtype mark of a nominally unconstrained object is
17531 -- rewritten as a subtype indication using the bounds of the
17532 -- expression. Recover the original subtype mark.
17533
17534 elsif Nkind (N2) = N_Subtype_Indication
17535 and then Is_Entity_Name (Original_Node (N2))
17536 then
17537 Set_Associated_Node (N, Original_Node (N2));
17538 Reset_Entity (N);
17539 end if;
17540 end if;
17541 end Save_References_In_Identifier;
17542
17543 ---------------------------------
17544 -- Save_References_In_Operator --
17545 ---------------------------------
17546
17547 procedure Save_References_In_Operator (N : Node_Id) is
17548 begin
17549 N2 := Get_Associated_Node (N);
17550
17551 -- The node did not undergo a transformation
17552
17553 if Nkind (N) = Nkind (N2) then
17554 if Nkind (N) = N_Op_Concat then
17555 Set_Is_Component_Left_Opnd
17556 (N, Is_Component_Left_Opnd (N2));
17557 Set_Is_Component_Right_Opnd
17558 (N, Is_Component_Right_Opnd (N2));
17559 end if;
17560
17561 Reset_Entity (N);
17562
17563 -- The analysis of the generic copy transformed the operator into
17564 -- some other construct. Propagate the changes to the template if
17565 -- applicable.
17566
17567 else
17568 -- The operator resoved to a function call
17569
17570 if Nkind (N2) = N_Function_Call then
17571
17572 -- Add explicit qualifications in the generic template for
17573 -- all operands of universal type. This aids resolution by
17574 -- preserving the actual type of a literal or an attribute
17575 -- that yields a universal result.
17576
17577 Qualify_Universal_Operands (N, N2);
17578
17579 E := Entity (Name (N2));
17580
17581 if Present (E) and then Is_Global (E) then
17582 Set_Global_Type (N, N2);
17583 else
17584 Set_Associated_Node (N, Empty);
17585 Set_Etype (N, Empty);
17586 end if;
17587
17588 -- The operator was folded into a literal
17589
17590 elsif Nkind (N2) in N_Integer_Literal
17591 | N_Real_Literal
17592 | N_String_Literal
17593 then
17594 if Present (Original_Node (N2))
17595 and then Nkind (Original_Node (N2)) = Nkind (N)
17596 then
17597 -- Operation was constant-folded. Whenever possible,
17598 -- recover semantic information from unfolded node.
17599 -- This was initially done for ASIS but is apparently
17600 -- needed also for e.g. compiling a-nbnbin.adb.
17601
17602 Set_Associated_Node (N, Original_Node (N2));
17603
17604 if Nkind (N) = N_Op_Concat then
17605 Set_Is_Component_Left_Opnd (N,
17606 Is_Component_Left_Opnd (Get_Associated_Node (N)));
17607 Set_Is_Component_Right_Opnd (N,
17608 Is_Component_Right_Opnd (Get_Associated_Node (N)));
17609 end if;
17610
17611 Reset_Entity (N);
17612
17613 -- Propagate the constant folding back to the template
17614
17615 else
17616 Rewrite (N, New_Copy (N2));
17617 Set_Analyzed (N, False);
17618 end if;
17619
17620 -- The operator was folded into an enumeration literal. Retain
17621 -- the entity to avoid spurious ambiguities if it is overloaded
17622 -- at the point of instantiation or inlining.
17623
17624 elsif Nkind (N2) = N_Identifier
17625 and then Ekind (Entity (N2)) = E_Enumeration_Literal
17626 then
17627 Rewrite (N, New_Copy (N2));
17628 Set_Analyzed (N, False);
17629 end if;
17630 end if;
17631
17632 -- Complete the operands check if node has not been constant
17633 -- folded.
17634
17635 if Nkind (N) in N_Op then
17636 Save_Entity_Descendants (N);
17637 end if;
17638 end Save_References_In_Operator;
17639
17640 -------------------------------
17641 -- Save_References_In_Pragma --
17642 -------------------------------
17643
17644 procedure Save_References_In_Pragma (Prag : Node_Id) is
17645 Context : Node_Id;
17646 Do_Save : Boolean := True;
17647
17648 begin
17649 -- Do not save global references in pragmas generated from aspects
17650 -- because the pragmas will be regenerated at instantiation time.
17651
17652 if From_Aspect_Specification (Prag) then
17653 Do_Save := False;
17654
17655 -- The capture of global references within contract-related source
17656 -- pragmas associated with generic packages, subprograms or their
17657 -- respective bodies must be delayed due to timing of annotation
17658 -- analysis. Global references are still captured in routine
17659 -- Save_Global_References_In_Contract.
17660
17661 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
17662 if Is_Package_Contract_Annotation (Prag) then
17663 Context := Find_Related_Package_Or_Body (Prag);
17664 else
17665 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
17666 Context := Find_Related_Declaration_Or_Body (Prag);
17667 end if;
17668
17669 -- The use of Original_Node accounts for the case when the
17670 -- related context is generic template.
17671
17672 if Requires_Delayed_Save (Original_Node (Context)) then
17673 Do_Save := False;
17674 end if;
17675 end if;
17676
17677 -- For all other cases, save all global references within the
17678 -- descendants, but skip the following semantic fields:
17679 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
17680
17681 if Do_Save then
17682 Save_Global_Descendant
17683 (Union_Id (Pragma_Argument_Associations (N)));
17684 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
17685 end if;
17686 end Save_References_In_Pragma;
17687
17688 -- Start of processing for Save_References
17689
17690 begin
17691 if N = Empty then
17692 null;
17693
17694 -- Aggregates
17695
17696 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
17697 Save_References_In_Aggregate (N);
17698
17699 -- Character literals, operator symbols
17700
17701 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
17702 Save_References_In_Char_Lit_Or_Op_Symbol (N);
17703
17704 -- Defining identifiers
17705
17706 elsif Nkind (N) in N_Entity then
17707 null;
17708
17709 -- Identifiers
17710
17711 elsif Nkind (N) = N_Identifier then
17712 Save_References_In_Identifier (N);
17713
17714 -- Operators
17715
17716 elsif Nkind (N) in N_Op then
17717 Save_References_In_Operator (N);
17718
17719 -- Pragmas
17720
17721 elsif Nkind (N) = N_Pragma then
17722 Save_References_In_Pragma (N);
17723
17724 elsif Nkind (N) = N_Aspect_Specification then
17725 declare
17726 P : constant Node_Id := Parent (N);
17727 Expr : Node_Id;
17728 begin
17729
17730 if Permits_Aspect_Specifications (P) then
17731
17732 -- The capture of global references within aspects
17733 -- associated with generic packages, subprograms or
17734 -- their bodies must be delayed due to timing of
17735 -- annotation analysis. Global references are still
17736 -- captured in routine Save_Global_References_In_Contract.
17737
17738 if Requires_Delayed_Save (Original_Node (P)) then
17739 null;
17740
17741 -- Otherwise save all global references within the
17742 -- aspects
17743
17744 else
17745 Expr := Expression (N);
17746
17747 if Present (Expr) then
17748 Save_Global_References (Expr);
17749 end if;
17750 end if;
17751 end if;
17752 end;
17753
17754 -- Do not walk the node pointed to by Label_Construct twice
17755
17756 elsif Nkind (N) = N_Implicit_Label_Declaration then
17757 null;
17758
17759 else
17760 Save_References_In_Descendants (N);
17761 end if;
17762
17763 end Save_References;
17764
17765 ---------------------
17766 -- Set_Global_Type --
17767 ---------------------
17768
17769 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
17770 Comparison : constant Boolean := Nkind (N2) in N_Op_Compare;
17771 Typ : constant Entity_Id :=
17772 (if Comparison then Compare_Type (N2) else Etype (N2));
17773
17774 begin
17775 -- For a comparison (or equality) operator, the Etype is Boolean, so
17776 -- it is always global. But the type subject to the Has_Private_View
17777 -- processing is the Compare_Type, so we must specifically check it.
17778
17779 if Comparison then
17780 Set_Etype (N, Etype (N2));
17781
17782 if not Is_Global (Typ) then
17783 return;
17784 end if;
17785
17786 Set_Compare_Type (N, Typ);
17787
17788 else
17789 Set_Etype (N, Typ);
17790 end if;
17791
17792 -- If the entity of N is not the associated node, this is a
17793 -- nested generic and it has an associated node as well, whose
17794 -- type is already the full view (see below). Indicate that the
17795 -- original node has a private view.
17796
17797 if Entity (N) /= N2 then
17798 if Has_Private_View (Entity (N)) then
17799 Set_Has_Private_View (N);
17800 end if;
17801
17802 if Has_Secondary_Private_View (Entity (N)) then
17803 Set_Has_Secondary_Private_View (N);
17804 end if;
17805 end if;
17806
17807 -- If not a private type, deal with a secondary private view
17808
17809 if not Is_Private_Type (Typ) then
17810 if (Is_Access_Type (Typ)
17811 and then Is_Private_Type (Designated_Type (Typ)))
17812 or else (Is_Array_Type (Typ)
17813 and then
17814 Is_Private_Type (Component_Type_For_Private_View (Typ)))
17815 then
17816 Set_Has_Secondary_Private_View (N);
17817 end if;
17818
17819 -- If it is a derivation of a private type in a context where no
17820 -- full view is needed, nothing to do either.
17821
17822 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
17823 null;
17824
17825 -- Otherwise mark the type for flipping and set the full view on N2
17826 -- when available, which is necessary for Check_Private_View to swap
17827 -- back the views in case the full declaration of Typ is visible in
17828 -- the instantiation context. Note that this will be problematic if
17829 -- N2 is re-analyzed later, e.g. if it's a default value in a call.
17830
17831 else
17832 Set_Has_Private_View (N);
17833
17834 if Present (Full_View (Typ)) then
17835 if Comparison then
17836 Set_Compare_Type (N2, Full_View (Typ));
17837 else
17838 Set_Etype (N2, Full_View (Typ));
17839 end if;
17840 end if;
17841 end if;
17842
17843 if Is_Floating_Point_Type (Typ)
17844 and then Has_Dimension_System (Typ)
17845 then
17846 Copy_Dimensions (N2, N);
17847 end if;
17848 end Set_Global_Type;
17849
17850 -- Start of processing for Save_Global_References
17851
17852 begin
17853 Gen_Scope := Current_Scope;
17854
17855 -- If the generic unit is a child unit, references to entities in the
17856 -- parent are treated as local, because they will be resolved anew in
17857 -- the context of the instance of the parent.
17858
17859 while Is_Child_Unit (Gen_Scope)
17860 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
17861 loop
17862 Gen_Scope := Scope (Gen_Scope);
17863 end loop;
17864
17865 Save_References (Templ);
17866 end Save_Global_References;
17867
17868 ---------------------------------------
17869 -- Save_Global_References_In_Aspects --
17870 ---------------------------------------
17871
17872 procedure Save_Global_References_In_Aspects (N : Node_Id) is
17873 Asp : Node_Id;
17874 Expr : Node_Id;
17875
17876 begin
17877 Asp := First (Aspect_Specifications (N));
17878 while Present (Asp) loop
17879 Expr := Expression (Asp);
17880
17881 if Present (Expr) then
17882 Save_Global_References (Expr);
17883 end if;
17884
17885 Next (Asp);
17886 end loop;
17887 end Save_Global_References_In_Aspects;
17888
17889 ------------------------------------------
17890 -- Set_Copied_Sloc_For_Inherited_Pragma --
17891 ------------------------------------------
17892
17893 procedure Set_Copied_Sloc_For_Inherited_Pragma
17894 (N : Node_Id;
17895 E : Entity_Id)
17896 is
17897 begin
17898 Create_Instantiation_Source (N, E,
17899 Inlined_Body => False,
17900 Inherited_Pragma => True,
17901 Factor => S_Adjustment);
17902 end Set_Copied_Sloc_For_Inherited_Pragma;
17903
17904 --------------------------------------
17905 -- Set_Copied_Sloc_For_Inlined_Body --
17906 --------------------------------------
17907
17908 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
17909 begin
17910 Create_Instantiation_Source (N, E,
17911 Inlined_Body => True,
17912 Inherited_Pragma => False,
17913 Factor => S_Adjustment);
17914 end Set_Copied_Sloc_For_Inlined_Body;
17915
17916 ---------------------
17917 -- Set_Instance_Of --
17918 ---------------------
17919
17920 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
17921 begin
17922 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
17923 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
17924 Generic_Renamings.Increment_Last;
17925 end Set_Instance_Of;
17926
17927 --------------------
17928 -- Set_Next_Assoc --
17929 --------------------
17930
17931 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
17932 begin
17933 Generic_Renamings.Table (E).Next_In_HTable := Next;
17934 end Set_Next_Assoc;
17935
17936 -------------------
17937 -- Start_Generic --
17938 -------------------
17939
17940 procedure Start_Generic is
17941 begin
17942 -- ??? More things could be factored out in this routine.
17943 -- Should probably be done at a later stage.
17944
17945 Generic_Flags.Append (Inside_A_Generic);
17946 Inside_A_Generic := True;
17947
17948 Expander_Mode_Save_And_Set (False);
17949 end Start_Generic;
17950
17951 ----------------------
17952 -- Set_Instance_Env --
17953 ----------------------
17954
17955 -- WARNING: This routine manages SPARK regions
17956
17957 procedure Set_Instance_Env
17958 (Gen_Unit : Entity_Id;
17959 Act_Unit : Entity_Id)
17960 is
17961 Saved_AE : constant Boolean := Assertions_Enabled;
17962 Saved_CPL : constant Node_Id := Check_Policy_List;
17963 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
17964 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
17965 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
17966
17967 begin
17968 -- Regardless of the current mode, predefined units are analyzed in the
17969 -- most current Ada mode, and earlier version Ada checks do not apply
17970 -- to predefined units. Nothing needs to be done for non-internal units.
17971 -- These are always analyzed in the current mode.
17972
17973 if In_Internal_Unit (Gen_Unit) then
17974
17975 -- The following call resets all configuration attributes to default
17976 -- or the xxx_Config versions of the attributes when the current sem
17977 -- unit is the main unit. At the same time, internal units must also
17978 -- inherit certain configuration attributes from their context. It
17979 -- is unclear what these two sets are.
17980
17981 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
17982
17983 -- Reinstall relevant configuration attributes of the context
17984
17985 Assertions_Enabled := Saved_AE;
17986 Check_Policy_List := Saved_CPL;
17987 Dynamic_Elaboration_Checks := Saved_DEC;
17988
17989 Install_SPARK_Mode (Saved_SM, Saved_SMP);
17990 end if;
17991
17992 Current_Instantiated_Parent :=
17993 (Gen_Id => Gen_Unit,
17994 Act_Id => Act_Unit,
17995 Next_In_HTable => Assoc_Null);
17996 end Set_Instance_Env;
17997
17998 -----------------
17999 -- Switch_View --
18000 -----------------
18001
18002 procedure Switch_View (T : Entity_Id) is
18003 BT : constant Entity_Id := Base_Type (T);
18004 Priv_Elmt : Elmt_Id := No_Elmt;
18005 Priv_Sub : Entity_Id;
18006
18007 begin
18008 -- T may be private but its base type may have been exchanged through
18009 -- some other occurrence, in which case there is nothing to switch
18010 -- besides T itself. Note that a private dependent subtype of a private
18011 -- type might not have been switched even if the base type has been,
18012 -- because of the last branch of Check_Private_View (see comment there).
18013
18014 if not Is_Private_Type (BT) then
18015 Prepend_Elmt (Full_View (T), Exchanged_Views);
18016 Exchange_Declarations (T);
18017 return;
18018 end if;
18019
18020 Priv_Elmt := First_Elmt (Private_Dependents (BT));
18021
18022 if Present (Full_View (BT)) then
18023 Prepend_Elmt (Full_View (BT), Exchanged_Views);
18024 Exchange_Declarations (BT);
18025 end if;
18026
18027 while Present (Priv_Elmt) loop
18028 Priv_Sub := Node (Priv_Elmt);
18029
18030 if Present (Full_View (Priv_Sub)) then
18031 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
18032 Exchange_Declarations (Priv_Sub);
18033 end if;
18034
18035 Next_Elmt (Priv_Elmt);
18036 end loop;
18037 end Switch_View;
18038
18039 -----------------
18040 -- True_Parent --
18041 -----------------
18042
18043 function True_Parent (N : Node_Id) return Node_Id is
18044 begin
18045 if Nkind (Parent (N)) = N_Subunit then
18046 return Parent (Corresponding_Stub (Parent (N)));
18047 else
18048 return Parent (N);
18049 end if;
18050 end True_Parent;
18051
18052 -----------------------------
18053 -- Valid_Default_Attribute --
18054 -----------------------------
18055
18056 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
18057 Attr_Id : constant Attribute_Id :=
18058 Get_Attribute_Id (Attribute_Name (Def));
18059 T : constant Entity_Id := Entity (Prefix (Def));
18060 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
18061 F : Entity_Id;
18062 Num_F : Nat;
18063 OK : Boolean;
18064
18065 begin
18066 if No (T) or else T = Any_Id then
18067 return;
18068 end if;
18069
18070 Num_F := 0;
18071 F := First_Formal (Nam);
18072 while Present (F) loop
18073 Num_F := Num_F + 1;
18074 Next_Formal (F);
18075 end loop;
18076
18077 case Attr_Id is
18078 when Attribute_Adjacent
18079 | Attribute_Ceiling
18080 | Attribute_Copy_Sign
18081 | Attribute_Floor
18082 | Attribute_Fraction
18083 | Attribute_Machine
18084 | Attribute_Model
18085 | Attribute_Remainder
18086 | Attribute_Rounding
18087 | Attribute_Unbiased_Rounding
18088 =>
18089 OK := Is_Fun
18090 and then Num_F = 1
18091 and then Is_Floating_Point_Type (T);
18092
18093 when Attribute_Image
18094 | Attribute_Pred
18095 | Attribute_Succ
18096 | Attribute_Value
18097 | Attribute_Wide_Image
18098 | Attribute_Wide_Value
18099 =>
18100 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
18101
18102 when Attribute_Max
18103 | Attribute_Min
18104 =>
18105 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
18106
18107 when Attribute_Input =>
18108 OK := (Is_Fun and then Num_F = 1);
18109
18110 when Attribute_Output
18111 | Attribute_Put_Image
18112 | Attribute_Read
18113 | Attribute_Write
18114 =>
18115 OK := not Is_Fun and then Num_F = 2;
18116
18117 when others =>
18118 OK := False;
18119 end case;
18120
18121 if not OK then
18122 Error_Msg_N
18123 ("attribute reference has wrong profile for subprogram", Def);
18124 end if;
18125 end Valid_Default_Attribute;
18126
18127 ----------------------------------
18128 -- Validate_Formal_Type_Default --
18129 ----------------------------------
18130
18131 procedure Validate_Formal_Type_Default (Decl : Node_Id) is
18132 Default : constant Node_Id :=
18133 Default_Subtype_Mark (Original_Node (Decl));
18134 Formal : constant Entity_Id := Defining_Identifier (Decl);
18135
18136 Def_Sub : Entity_Id; -- Default subtype mark
18137 Type_Def : Node_Id;
18138
18139 procedure Check_Discriminated_Formal;
18140 -- Check that discriminants of default for private or incomplete
18141 -- type match those of formal type.
18142
18143 function Reference_Formal (N : Node_Id) return Traverse_Result;
18144 -- Check whether formal type definition mentions a previous formal
18145 -- type of the same generic.
18146
18147 ----------------------
18148 -- Reference_Formal --
18149 ----------------------
18150
18151 function Reference_Formal (N : Node_Id) return Traverse_Result is
18152 begin
18153 if Is_Entity_Name (N)
18154 and then Scope (Entity (N)) = Current_Scope
18155 then
18156 return Abandon;
18157 else
18158 return OK;
18159 end if;
18160 end Reference_Formal;
18161
18162 function Depends_On_Other_Formals is
18163 new Traverse_Func (Reference_Formal);
18164
18165 function Default_Subtype_Matches
18166 (Gen_T, Def_T : Entity_Id) return Boolean;
18167
18168 procedure Validate_Array_Type_Default;
18169 -- Verify that dimension, indices, and component types of default
18170 -- are compatible with formal array type definition.
18171
18172 procedure Validate_Derived_Type_Default;
18173 -- Verify that ancestor and progenitor types match.
18174
18175 ---------------------------------
18176 -- Check_Discriminated_Formal --
18177 ---------------------------------
18178
18179 procedure Check_Discriminated_Formal is
18180 Formal_Discr : Entity_Id;
18181 Actual_Discr : Entity_Id;
18182 Formal_Subt : Entity_Id;
18183
18184 begin
18185 if Has_Discriminants (Formal) then
18186 if not Has_Discriminants (Def_Sub) then
18187 Error_Msg_NE
18188 ("default for & must have discriminants", Default, Formal);
18189
18190 elsif Is_Constrained (Def_Sub) then
18191 Error_Msg_NE
18192 ("default for & must be unconstrained", Default, Formal);
18193
18194 else
18195 Formal_Discr := First_Discriminant (Formal);
18196 Actual_Discr := First_Discriminant (Def_Sub);
18197 while Formal_Discr /= Empty loop
18198 if Actual_Discr = Empty then
18199 Error_Msg_N
18200 ("discriminants on Formal do not match formal",
18201 Default);
18202 end if;
18203
18204 Formal_Subt := Etype (Formal_Discr);
18205
18206 -- Access discriminants match if designated types do
18207
18208 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
18209 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
18210 E_Anonymous_Access_Type
18211 and then
18212 Base_Type
18213 (Designated_Type (Base_Type (Formal_Subt))) =
18214 Base_Type
18215 (Designated_Type (Base_Type (Etype (Actual_Discr))))
18216 and then
18217 Subtypes_Statically_Match
18218 (Designated_Type (Base_Type (Formal_Subt)),
18219 Designated_Type (Base_Type (Etype (Actual_Discr))))
18220 then
18221 null;
18222
18223 elsif Base_Type (Formal_Subt) /=
18224 Base_Type (Etype (Actual_Discr))
18225 then
18226 Error_Msg_N
18227 ("types of discriminants of default must match formal",
18228 Default);
18229
18230 elsif not Subtypes_Statically_Match
18231 (Formal_Subt, Etype (Actual_Discr))
18232 and then Ada_Version >= Ada_95
18233 then
18234 Error_Msg_N
18235 ("subtypes of discriminants of default "
18236 & "must match formal",
18237 Default);
18238 end if;
18239
18240 Next_Discriminant (Formal_Discr);
18241 Next_Discriminant (Actual_Discr);
18242 end loop;
18243
18244 if Actual_Discr /= Empty then
18245 Error_Msg_NE
18246 ("discriminants on default do not match formal",
18247 Default, Formal);
18248 end if;
18249 end if;
18250 end if;
18251 end Check_Discriminated_Formal;
18252
18253 ---------------------------
18254 -- Default_Subtype_Matches --
18255 ---------------------------
18256
18257 function Default_Subtype_Matches
18258 (Gen_T, Def_T : Entity_Id) return Boolean
18259 is
18260 begin
18261 -- Check that the base types, root types (when dealing with class
18262 -- wide types), or designated types (when dealing with anonymous
18263 -- access types) of Gen_T and Def_T are statically matching subtypes.
18264
18265 return (Base_Type (Gen_T) = Base_Type (Def_T)
18266 and then Subtypes_Statically_Match (Gen_T, Def_T))
18267
18268 or else (Is_Class_Wide_Type (Gen_T)
18269 and then Is_Class_Wide_Type (Def_T)
18270 and then Default_Subtype_Matches
18271 (Root_Type (Gen_T), Root_Type (Def_T)))
18272
18273 or else (Is_Anonymous_Access_Type (Gen_T)
18274 and then Ekind (Def_T) = Ekind (Gen_T)
18275 and then Subtypes_Statically_Match
18276 (Designated_Type (Gen_T), Designated_Type (Def_T)));
18277
18278 end Default_Subtype_Matches;
18279
18280 ----------------------------------
18281 -- Validate_Array_Type_Default --
18282 ----------------------------------
18283
18284 procedure Validate_Array_Type_Default is
18285 I1, I2 : Node_Id;
18286 T2 : Entity_Id;
18287 begin
18288 if not Is_Array_Type (Def_Sub) then
18289 Error_Msg_NE ("default for& must be an array type ",
18290 Default, Formal);
18291 return;
18292
18293 elsif Number_Dimensions (Def_Sub) /= Number_Dimensions (Formal)
18294 or else Is_Constrained (Def_Sub) /=
18295 Is_Constrained (Formal)
18296 then
18297 Error_Msg_NE ("default array type does not match&",
18298 Default, Formal);
18299 return;
18300 end if;
18301
18302 I1 := First_Index (Formal);
18303 I2 := First_Index (Def_Sub);
18304 for J in 1 .. Number_Dimensions (Formal) loop
18305
18306 -- If the indexes of the actual were given by a subtype_mark,
18307 -- the index was transformed into a range attribute. Retrieve
18308 -- the original type mark for checking.
18309
18310 if Is_Entity_Name (Original_Node (I2)) then
18311 T2 := Entity (Original_Node (I2));
18312 else
18313 T2 := Etype (I2);
18314 end if;
18315
18316 if not Subtypes_Statically_Match (Etype (I1), T2) then
18317 Error_Msg_NE
18318 ("index types of default do not match those of formal &",
18319 Default, Formal);
18320 end if;
18321
18322 Next_Index (I1);
18323 Next_Index (I2);
18324 end loop;
18325
18326 if not Default_Subtype_Matches
18327 (Component_Type (Formal), Component_Type (Def_Sub))
18328 then
18329 Error_Msg_NE
18330 ("component subtype of default does not match that of formal &",
18331 Default, Formal);
18332 end if;
18333
18334 if Has_Aliased_Components (Formal)
18335 and then not Has_Aliased_Components (Default)
18336 then
18337 Error_Msg_NE
18338 ("default must have aliased components to match formal type &",
18339 Default, Formal);
18340 end if;
18341 end Validate_Array_Type_Default;
18342
18343 -----------------------------------
18344 -- Validate_Derived_Type_Default --
18345 -----------------------------------
18346
18347 procedure Validate_Derived_Type_Default is
18348 begin
18349 if not Is_Ancestor (Etype (Formal), Def_Sub) then
18350 Error_Msg_NE ("default must be a descendent of&",
18351 Default, Etype (Formal));
18352 end if;
18353
18354 if Has_Interfaces (Formal) then
18355 if not Has_Interfaces (Def_Sub) then
18356 Error_Msg_NE
18357 ("default must implement all interfaces of formal&",
18358 Default, Formal);
18359
18360 else
18361 declare
18362 Iface : Node_Id;
18363 Iface_Ent : Entity_Id;
18364
18365 begin
18366 Iface := First (Abstract_Interface_List (Formal));
18367
18368 while Present (Iface) loop
18369 Iface_Ent := Entity (Iface);
18370
18371 if Is_Ancestor (Iface_Ent, Def_Sub)
18372 or else Is_Progenitor (Iface_Ent, Def_Sub)
18373 then
18374 null;
18375
18376 else
18377 Error_Msg_NE
18378 ("Default must implement interface&",
18379 Default, Etype (Iface));
18380 end if;
18381
18382 Next (Iface);
18383 end loop;
18384 end;
18385 end if;
18386 end if;
18387 end Validate_Derived_Type_Default;
18388
18389 -- Start of processing for Validate_Formal_Type_Default
18390
18391 begin
18392 Analyze (Default);
18393 if not Is_Entity_Name (Default)
18394 or else not Is_Type (Entity (Default))
18395 then
18396 Error_Msg_N
18397 ("Expect type name for default of formal type", Default);
18398 return;
18399 else
18400 Def_Sub := Entity (Default);
18401 end if;
18402
18403 -- Formal derived_type declarations are transformed into full
18404 -- type declarations or Private_Type_Extensions for ease of processing.
18405
18406 if Nkind (Decl) = N_Full_Type_Declaration then
18407 Type_Def := Type_Definition (Decl);
18408
18409 elsif Nkind (Decl) = N_Private_Extension_Declaration then
18410 Type_Def := Subtype_Indication (Decl);
18411
18412 else
18413 Type_Def := Formal_Type_Definition (Decl);
18414 end if;
18415
18416 if Depends_On_Other_Formals (Type_Def) = Abandon
18417 and then Scope (Def_Sub) /= Current_Scope
18418 then
18419 Error_Msg_N ("default of formal type that depends on "
18420 & "other formals must be a previous formal type", Default);
18421 return;
18422
18423 elsif Def_Sub = Formal then
18424 Error_Msg_N
18425 ("default for formal type cannot be formal itsef", Default);
18426 return;
18427 end if;
18428
18429 case Nkind (Type_Def) is
18430
18431 when N_Formal_Private_Type_Definition =>
18432 if (Is_Abstract_Type (Formal)
18433 and then not Is_Abstract_Type (Def_Sub))
18434 or else (Is_Limited_Type (Formal)
18435 and then not Is_Limited_Type (Def_Sub))
18436 then
18437 Error_Msg_NE
18438 ("default for private type$ does not match",
18439 Default, Formal);
18440 end if;
18441
18442 Check_Discriminated_Formal;
18443
18444 when N_Formal_Derived_Type_Definition =>
18445 Check_Discriminated_Formal;
18446 Validate_Derived_Type_Default;
18447
18448 when N_Formal_Incomplete_Type_Definition =>
18449 if Is_Tagged_Type (Formal)
18450 and then not Is_Tagged_Type (Def_Sub)
18451 then
18452 Error_Msg_NE
18453 ("default for & must be a tagged type", Default, Formal);
18454 end if;
18455
18456 Check_Discriminated_Formal;
18457
18458 when N_Formal_Discrete_Type_Definition =>
18459 if not Is_Discrete_Type (Def_Sub) then
18460 Error_Msg_NE ("default for& must be a discrete type",
18461 Default, Formal);
18462 end if;
18463
18464 when N_Formal_Signed_Integer_Type_Definition =>
18465 if not Is_Integer_Type (Def_Sub) then
18466 Error_Msg_NE ("default for& must be a discrete type",
18467 Default, Formal);
18468 end if;
18469
18470 when N_Formal_Modular_Type_Definition =>
18471 if not Is_Modular_Integer_Type (Def_Sub) then
18472 Error_Msg_NE ("default for& must be a modular_integer Type",
18473 Default, Formal);
18474 end if;
18475
18476 when N_Formal_Floating_Point_Definition =>
18477 if not Is_Floating_Point_Type (Def_Sub) then
18478 Error_Msg_NE ("default for& must be a floating_point type",
18479 Default, Formal);
18480 end if;
18481
18482 when N_Formal_Ordinary_Fixed_Point_Definition =>
18483 if not Is_Ordinary_Fixed_Point_Type (Def_Sub) then
18484 Error_Msg_NE ("default for& must be "
18485 & "an ordinary_fixed_point type ",
18486 Default, Formal);
18487 end if;
18488
18489 when N_Formal_Decimal_Fixed_Point_Definition =>
18490 if not Is_Decimal_Fixed_Point_Type (Def_Sub) then
18491 Error_Msg_NE ("default for& must be "
18492 & "an Decimal_fixed_point type ",
18493 Default, Formal);
18494 end if;
18495
18496 when N_Array_Type_Definition =>
18497 Validate_Array_Type_Default;
18498
18499 when N_Access_Function_Definition |
18500 N_Access_Procedure_Definition =>
18501 if Ekind (Def_Sub) /= E_Access_Subprogram_Type then
18502 Error_Msg_NE ("default for& must be an Access_To_Subprogram",
18503 Default, Formal);
18504 end if;
18505 Check_Subtype_Conformant
18506 (Designated_Type (Formal), Designated_Type (Def_Sub));
18507
18508 when N_Access_To_Object_Definition =>
18509 if not Is_Access_Object_Type (Def_Sub) then
18510 Error_Msg_NE ("default for& must be an Access_To_Object",
18511 Default, Formal);
18512
18513 elsif not Default_Subtype_Matches
18514 (Designated_Type (Formal), Designated_Type (Def_Sub))
18515 then
18516 Error_Msg_NE ("designated type of defaul does not match "
18517 & "designated type of formal type",
18518 Default, Formal);
18519 end if;
18520
18521 when N_Record_Definition => -- Formal interface type
18522 if not Is_Interface (Def_Sub) then
18523 Error_Msg_NE
18524 ("default for formal interface type must be an interface",
18525 Default, Formal);
18526
18527 elsif Is_Limited_Type (Def_Sub) /= Is_Limited_Type (Formal)
18528 or else Is_Task_Interface (Formal) /= Is_Task_Interface (Def_Sub)
18529 or else Is_Protected_Interface (Formal) /=
18530 Is_Protected_Interface (Def_Sub)
18531 or else Is_Synchronized_Interface (Formal) /=
18532 Is_Synchronized_Interface (Def_Sub)
18533 then
18534 Error_Msg_NE
18535 ("default for interface& does not match", Def_Sub, Formal);
18536 end if;
18537
18538 when N_Derived_Type_Definition =>
18539 Validate_Derived_Type_Default;
18540
18541 when N_Identifier => -- case of a private extension
18542 Validate_Derived_Type_Default;
18543
18544 when N_Error =>
18545 null;
18546
18547 when others =>
18548 raise Program_Error;
18549 end case;
18550 end Validate_Formal_Type_Default;
18551
18552 package body Instance_Context is
18553
18554 --------------------
18555 -- Save_And_Reset --
18556 --------------------
18557
18558 function Save_And_Reset return Context is
18559 begin
18560 return Result : Context (0 .. Integer (Generic_Renamings.Last)) do
18561 for Index in Result'Range loop
18562 declare
18563 Indexed_Assoc : Assoc renames Generic_Renamings.Table
18564 (Assoc_Ptr (Index));
18565 Result_Pair : Binding_Pair renames Result (Index);
18566 begin
18567 -- If we have called Increment_Last but have not yet
18568 -- initialized the new last element of the table, then
18569 -- that last element might be invalid. Saving and
18570 -- restoring (especially restoring, it turns out) invalid
18571 -- values can result in exceptions if predicate checking
18572 -- is enabled, so replace invalid values with Empty.
18573
18574 if Indexed_Assoc.Gen_Id'Valid then
18575 Result_Pair.Formal_Id := Indexed_Assoc.Gen_Id;
18576 else
18577 pragma Assert (Index = Result'Last);
18578 Result_Pair.Formal_Id := Empty;
18579 end if;
18580
18581 if Indexed_Assoc.Act_Id'Valid then
18582 Result_Pair.Actual_Id := Indexed_Assoc.Act_Id;
18583 else
18584 pragma Assert (Index = Result'Last);
18585 Result_Pair.Actual_Id := Empty;
18586 end if;
18587 end;
18588 end loop;
18589
18590 Generic_Renamings.Init;
18591 Generic_Renamings.Set_Last (-1);
18592 Generic_Renamings_HTable.Reset;
18593 end return;
18594 end Save_And_Reset;
18595
18596 -------------
18597 -- Restore --
18598 -------------
18599
18600 procedure Restore (Saved : Context) is
18601 begin
18602 Generic_Renamings.Init;
18603 Generic_Renamings.Set_Last (0);
18604 Generic_Renamings_HTable.Reset;
18605 Generic_Renamings.Increment_Last;
18606 for Pair of Saved loop
18607 Set_Instance_Of (Pair.Formal_Id, Pair.Actual_Id);
18608 end loop;
18609 Generic_Renamings.Decrement_Last;
18610 end Restore;
18611
18612 end Instance_Context;
18613 end Sem_Ch12;
This page took 0.787302 seconds and 6 git commands to generate.