Debian Codegraph @

gcc/ada/sem_ch12.adb

raw
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-2025, 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 -- Exit_Cases
271 -- Extensions_Visible
272 -- Global
273 -- package body Post
274 -- Refined_State Post_Class
275 -- Postcondition
276 -- Pre
277 -- Pre_Class
278 -- Precondition
279 -- Refined_Depends
280 -- Refined_Global
281 -- Refined_Post
282 -- Subprogram_Variant
283 -- Test_Case
284
285 -- Most package contract annotations utilize forward references to classify
286 -- data declared within the package [body]. Subprogram annotations then use
287 -- the classifications to further refine them. These inter dependencies are
288 -- problematic with respect to the implementation of generics because their
289 -- analysis, capture of global references and instantiation does not mesh
290 -- well with the existing mechanism.
291
292 -- 1) Analysis of generic contracts is carried out the same way non-generic
293 -- contracts are analyzed:
294
295 -- 1.1) General rule - a contract is analyzed after all related aspects
296 -- and pragmas are analyzed. This is done by routines
297
298 -- Analyze_Package_Body_Contract
299 -- Analyze_Package_Contract
300 -- Analyze_Subprogram_Body_Contract
301 -- Analyze_Subprogram_Contract
302
303 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
304 -- are processed.
305
306 -- 1.3) Compilation unit body - the contract is analyzed at the end of
307 -- the body declaration list.
308
309 -- 1.4) Package - the contract is analyzed at the end of the private or
310 -- visible declarations, prior to analyzing the contracts of any nested
311 -- packages or subprograms.
312
313 -- 1.5) Package body - the contract is analyzed at the end of the body
314 -- declaration list, prior to analyzing the contracts of any nested
315 -- packages or subprograms.
316
317 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
318 -- package or a subprogram, then its contract is analyzed at the end of
319 -- the enclosing declarations, otherwise the subprogram is a compilation
320 -- unit 1.2).
321
322 -- 1.7) Subprogram body - if the subprogram body is declared inside a
323 -- block, a package body or a subprogram body, then its contract is
324 -- analyzed at the end of the enclosing declarations, otherwise the
325 -- subprogram is a compilation unit 1.3).
326
327 -- 2) Capture of global references within contracts is done after capturing
328 -- global references within the generic template. There are two reasons for
329 -- this delay - pragma annotations are not part of the generic template in
330 -- the case of a generic subprogram declaration, and analysis of contracts
331 -- is delayed.
332
333 -- Contract-related source pragmas within generic templates are prepared
334 -- for delayed capture of global references by routine
335
336 -- Create_Generic_Contract
337
338 -- The routine associates these pragmas with the contract of the template.
339 -- In the case of a generic subprogram declaration, the routine creates
340 -- generic templates for the pragmas declared after the subprogram because
341 -- they are not part of the template.
342
343 -- generic -- template starts
344 -- procedure Gen_Proc (Input : Integer); -- template ends
345 -- pragma Precondition (Input > 0); -- requires own template
346
347 -- 2.1) The capture of global references with aspect specifications and
348 -- source pragmas that apply to a generic unit must be suppressed when
349 -- the generic template is being processed because the contracts have not
350 -- been analyzed yet. Any attempts to capture global references at that
351 -- point will destroy the Associated_Node linkages and leave the template
352 -- undecorated. This delay is controlled by routine
353
354 -- Requires_Delayed_Save
355
356 -- 2.2) The real capture of global references within a contract is done
357 -- after the contract has been analyzed, by routine
358
359 -- Save_Global_References_In_Contract
360
361 -- 3) The instantiation of a generic contract occurs as part of the
362 -- instantiation of the contract owner. Generic subprogram declarations
363 -- require additional processing when the contract is specified by pragmas
364 -- because the pragmas are not part of the generic template. This is done
365 -- by routine
366
367 -- Instantiate_Subprogram_Contract
368
369 --------------------------------------------------
370 -- Formal packages and partial parameterization --
371 --------------------------------------------------
372
373 -- When compiling a generic, a formal package is a local instantiation. If
374 -- declared with a box, its generic formals are visible in the enclosing
375 -- generic. If declared with a partial list of actuals, those actuals that
376 -- are defaulted (covered by an Others clause, or given an explicit box
377 -- initialization) are also visible in the enclosing generic, while those
378 -- that have a corresponding actual are not.
379
380 -- In our source model of instantiation, the same visibility must be
381 -- present in the spec and body of an instance: the names of the formals
382 -- that are defaulted must be made visible within the instance, and made
383 -- invisible (hidden) after the instantiation is complete, so that they
384 -- are not accessible outside of the instance.
385
386 -- In a generic, a formal package is treated like a special instantiation.
387 -- Our Ada 95 compiler handled formals with and without box in different
388 -- ways. With partial parameterization, we use a single model for both.
389 -- We create a package declaration that consists of the specification of
390 -- the generic package, and a set of declarations that map the actuals
391 -- into local renamings, just as we do for bona fide instantiations. For
392 -- defaulted parameters and formals with a box, we copy directly the
393 -- declarations of the formals into this local package. The result is a
394 -- package whose visible declarations may include generic formals. This
395 -- package is only used for type checking and visibility analysis, and
396 -- never reaches the back end, so it can freely violate the placement
397 -- rules for generic formal declarations.
398
399 -- The list of declarations (renamings and copies of formals) is built
400 -- by Analyze_Associations, just as for regular instantiations.
401
402 -- At the point of instantiation, conformance checking must be applied only
403 -- to those parameters that were specified in the formals. We perform this
404 -- checking by creating another internal instantiation, this one including
405 -- only the renamings and the formals (the rest of the package spec is not
406 -- relevant to conformance checking). We can then traverse two lists: the
407 -- list of actuals in the instance that corresponds to the formal package,
408 -- and the list of actuals produced for this bogus instantiation. We apply
409 -- the conformance rules to those actuals that are not defaulted, i.e.
410 -- which still appear as generic formals.
411
412 -- When we compile an instance body we must make the right parameters
413 -- visible again. The predicate Is_Generic_Formal indicates which of the
414 -- formals should have its Is_Hidden flag reset.
415
416 -----------------------
417 -- Local subprograms --
418 -----------------------
419
420 procedure Abandon_Instantiation (N : Node_Id);
421 pragma No_Return (Abandon_Instantiation);
422 -- Posts an error message "instantiation abandoned" at the indicated node
423 -- and then raises the exception Instantiation_Error to do it.
424
425 procedure Analyze_Formal_Array_Type
426 (T : in out Entity_Id;
427 Def : Node_Id);
428 -- A formal array type is treated like an array type declaration, and
429 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
430 -- in-out, because in the case of an anonymous type the entity is
431 -- actually created in the procedure.
432
433 -- The following procedures treat other kinds of formal parameters
434
435 procedure Analyze_Formal_Derived_Interface_Type
436 (N : Node_Id;
437 T : Entity_Id;
438 Def : Node_Id);
439
440 procedure Analyze_Formal_Derived_Type
441 (N : Node_Id;
442 T : Entity_Id;
443 Def : Node_Id);
444
445 procedure Analyze_Formal_Interface_Type
446 (N : Node_Id;
447 T : Entity_Id;
448 Def : Node_Id);
449
450 -- The following subprograms create abbreviated declarations for formal
451 -- scalar types. We introduce an anonymous base of the proper class for
452 -- each of them, and define the formals as constrained first subtypes of
453 -- their bases. The bounds are expressions that are non-static in the
454 -- generic.
455
456 procedure Analyze_Formal_Decimal_Fixed_Point_Type
457 (T : Entity_Id; Def : Node_Id);
458 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
459 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
460 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
461 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
462 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
463 (T : Entity_Id; Def : Node_Id);
464
465 procedure Analyze_Formal_Private_Type
466 (N : Node_Id;
467 T : Entity_Id;
468 Def : Node_Id);
469 -- Creates a new private type, which does not require completion
470
471 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
472 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
473
474 procedure Analyze_Generic_Formal_Part (N : Node_Id);
475 -- Analyze generic formal part
476
477 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
478 -- Create a new access type with the given designated type
479
480 function Analyze_Associations
481 (I_Node : Node_Id;
482 Formals : List_Id;
483 F_Copy : List_Id) return List_Id;
484 -- At instantiation time, build the list of associations between formals
485 -- and actuals. Each association becomes a renaming declaration for the
486 -- formal entity. F_Copy is the analyzed list of formals in the generic
487 -- copy. It is used to apply legality checks to the actuals. I_Node is the
488 -- instantiation node.
489
490 procedure Analyze_Subprogram_Instantiation
491 (N : Node_Id;
492 K : Entity_Kind);
493
494 procedure Build_Instance_Compilation_Unit_Nodes
495 (N : Node_Id;
496 Act_Body : Node_Id;
497 Act_Decl : Node_Id);
498 -- This procedure is used in the case where the generic instance of a
499 -- subprogram body or package body is a library unit. In this case, the
500 -- original library unit node for the generic instantiation must be
501 -- replaced by the resulting generic body, and a link made to a new
502 -- compilation unit node for the generic declaration. The argument N is
503 -- the original generic instantiation. Act_Body and Act_Decl are the body
504 -- and declaration of the instance (either package body and declaration
505 -- nodes or subprogram body and declaration nodes depending on the case).
506 -- On return, the node N has been rewritten with the actual body.
507
508 function Build_Subprogram_Decl_Wrapper
509 (Formal_Subp : Entity_Id) return Node_Id;
510 -- Ada 2022 allows formal subprograms to carry pre/postconditions.
511 -- At the point of instantiation these contracts apply to uses of
512 -- the actual subprogram. This is implemented by creating wrapper
513 -- subprograms instead of the renamings previously used to link
514 -- formal subprograms and the corresponding actuals. If the actual
515 -- is not an entity (e.g. an attribute reference) a renaming is
516 -- created to handle the expansion of the attribute.
517
518 function Build_Subprogram_Body_Wrapper
519 (Formal_Subp : Entity_Id;
520 Actual_Name : Node_Id) return Node_Id;
521 -- The body of the wrapper is a call to the actual, with the generated
522 -- pre/postconditon checks added.
523
524 procedure Build_Subprogram_Wrappers
525 (Match, Analyzed_Formal : Node_Id; Renamings : List_Id);
526 -- Ada 2022: AI12-0272 introduces pre/postconditions for formal
527 -- subprograms. The implementation of making the formal into a renaming
528 -- of the actual does not work, given that subprogram renaming cannot
529 -- carry aspect specifications. Instead we must create subprogram
530 -- wrappers whose body is a call to the actual, and whose declaration
531 -- carries the aspects of the formal.
532 -- The wrapper declaration and body are appended to Renamings.
533 -- ???But renaming declarations CAN have aspects specs,
534 -- and that was true from the start (see AI05-0183-1).
535
536 procedure Check_Abbreviated_Instance
537 (N : Node_Id;
538 Parent_Installed : in out Boolean);
539 -- If the name of the generic unit in an abbreviated instantiation is an
540 -- expanded name, then the prefix may be an instance and the selector may
541 -- designate a child unit. If the parent is installed as a result of this
542 -- call, then Parent_Installed is set True, otherwise Parent_Installed is
543 -- unchanged by the call.
544
545 -- This routine needs to be called for declaration nodes of formal objects,
546 -- types and subprograms to check whether they are the copy, present in the
547 -- visible part of the abbreviated instantiation of formal packages, of the
548 -- declaration node of their corresponding formal parameter in the template
549 -- of the formal package, as specified by RM 12.7(10/2), so as to establish
550 -- the proper context for their analysis.
551
552 procedure Check_Access_Definition (N : Node_Id);
553 -- Subsidiary routine to null exclusion processing. Perform an assertion
554 -- check on Ada version and the presence of an access definition in N.
555
556 procedure Check_Formal_Packages (P_Id : Entity_Id);
557 -- Apply the following to all formal packages in generic associations.
558 -- Restore the visibility of the formals of the instance that are not
559 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
560 -- created for formal instances that are not defaulted.
561
562 procedure Check_Formal_Package_Instance
563 (Formal_Pack : Entity_Id;
564 Actual_Pack : Entity_Id);
565 -- Verify that the actuals of the actual instance match the actuals of
566 -- the template for a formal package that is not declared with a box.
567
568 procedure Check_Forward_Instantiation (Decl : Node_Id);
569 -- If the generic is a local entity and the corresponding body has not
570 -- been seen yet, flag enclosing packages to indicate that it will be
571 -- elaborated after the generic body. Subprograms declared in the same
572 -- package cannot be inlined by the front end because front-end inlining
573 -- requires a strict linear order of elaboration.
574
575 function Check_Hidden_Primitives (Renamings : List_Id) return Elist_Id;
576 -- Check if some association between formals and actuals requires to make
577 -- visible primitives of a tagged type, and make those primitives visible.
578 -- Return the list of primitives whose visibility is modified (to restore
579 -- their visibility later through Restore_Hidden_Primitives). If no
580 -- candidate is found then return No_Elist.
581
582 procedure Check_Hidden_Child_Unit
583 (N : Node_Id;
584 Gen_Unit : Entity_Id;
585 Act_Decl_Id : Entity_Id);
586 -- If the generic unit is an implicit child instance within a parent
587 -- instance, we need to make an explicit test that it is not hidden by
588 -- a child instance of the same name and parent.
589
590 procedure Check_Generic_Actuals
591 (Instance : Entity_Id;
592 Is_Formal_Box : Boolean);
593 -- Similar to previous one. Check the actuals in the instantiation,
594 -- whose views can change between the point of instantiation and the point
595 -- of instantiation of the body. In addition, mark the generic renamings
596 -- as generic actuals, so that they are not compatible with other actuals.
597 -- Recurse on an actual that is a formal package whose declaration has
598 -- a box.
599
600 function Component_Type_For_Private_View (T : Entity_Id) return Entity_Id;
601 -- Return the component type of array type T, with the following addition:
602 -- if this component type itself is an array type which has not been first
603 -- declared as private, then recurse on it. This makes it possible to deal
604 -- with arrays of arrays the same way as multi-dimensional arrays in the
605 -- mechanism handling private views.
606
607 function Contains_Instance_Of
608 (Inner : Entity_Id;
609 Outer : Entity_Id;
610 N : Node_Id) return Boolean;
611 -- Inner is instantiated within the generic Outer. Check whether Inner
612 -- directly or indirectly contains an instance of Outer or of one of its
613 -- parents, in the case of a subunit. Each generic unit holds a list of
614 -- the entities instantiated within (at any depth). This procedure
615 -- determines whether the set of such lists contains a cycle, i.e. an
616 -- illegal circular instantiation.
617
618 function Denotes_Formal_Package
619 (Pack : Entity_Id;
620 On_Exit : Boolean := False;
621 Instance : Entity_Id := Empty) return Boolean;
622 -- Returns True if E is a formal package of an enclosing generic, or
623 -- the actual for such a formal in an enclosing instantiation. If such
624 -- a package is used as a formal in an nested generic, or as an actual
625 -- in a nested instantiation, the visibility of ITS formals should not
626 -- be modified. When called from within Restore_Private_Views, the flag
627 -- On_Exit is true, to indicate that the search for a possible enclosing
628 -- instance should ignore the current one. In that case Instance denotes
629 -- the declaration for which this is an actual. This declaration may be
630 -- an instantiation in the source, or the internal instantiation that
631 -- corresponds to the actual for a formal package.
632
633 function Earlier (N1, N2 : Node_Id) return Boolean;
634 -- Yields True if N1 and N2 appear in the same compilation unit,
635 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
636 -- traversal of the tree for the unit. Used to determine the placement
637 -- of freeze nodes for instance bodies that may depend on other instances.
638
639 function Find_Actual_Type
640 (Typ : Entity_Id;
641 Gen_Typ : Entity_Id) return Entity_Id;
642 -- When validating the actual types of a child instance, check whether
643 -- the formal is a formal type of the parent unit, and retrieve the current
644 -- actual for it. Typ is the entity in the analyzed formal type declaration
645 -- (component or index type of an array type, or designated type of an
646 -- access formal) and Gen_Typ is the enclosing analyzed formal array
647 -- or access type. The desired actual may be a formal of a parent, or may
648 -- be declared in a formal package of a parent. In both cases it is a
649 -- generic actual type because it appears within a visible instance.
650 -- Finally, it may be declared in a parent unit without being a formal
651 -- of that unit, in which case it must be retrieved by visibility.
652 -- Ambiguities may still arise if two homonyms are declared in two formal
653 -- packages, and the prefix of the formal type may be needed to resolve
654 -- the ambiguity in the instance ???
655
656 procedure Freeze_Package_Instance
657 (N : Node_Id;
658 Gen_Body : Node_Id;
659 Gen_Decl : Node_Id;
660 Act_Id : Entity_Id);
661 -- If the instantiation happens textually before the body of the generic,
662 -- the instantiation of the body must be analyzed after the generic body,
663 -- and not at the point of instantiation. Such early instantiations can
664 -- happen if the generic and the instance appear in a package declaration
665 -- because the generic body can only appear in the corresponding package
666 -- body. Early instantiations can also appear if generic, instance and
667 -- body are all in the declarative part of a subprogram or entry. Entities
668 -- of packages that are early instantiations are delayed, and their freeze
669 -- node appears after the generic body. This rather complex machinery is
670 -- needed when nested instantiations are present, because the source does
671 -- not carry any indication of where the corresponding instance bodies must
672 -- be installed and frozen.
673
674 procedure Freeze_Subprogram_Instance
675 (N : Node_Id;
676 Gen_Body : Node_Id;
677 Pack_Id : Entity_Id);
678 -- The generic body may appear textually after the instance, including
679 -- in the proper body of a stub, or within a different package instance.
680 -- Given that the instance can only be elaborated after the generic, we
681 -- place freeze nodes for the instance and/or for packages that may enclose
682 -- the instance and the generic, so that the back-end can establish the
683 -- proper order of elaboration.
684
685 function Get_Associated_Entity (Id : Entity_Id) return Entity_Id;
686 -- Similar to Get_Associated_Node below, but for entities
687
688 function Get_Associated_Node (N : Node_Id) return Node_Id;
689 -- In order to propagate semantic information back from the analyzed copy
690 -- to the original generic, we maintain links between selected nodes in the
691 -- generic and their corresponding copies. At the end of generic analysis,
692 -- the routine Save_Global_References traverses the generic tree, examines
693 -- the semantic information, and preserves the links to those nodes that
694 -- contain global information. At instantiation, the information from the
695 -- associated node is placed on the new copy, so that name resolution is
696 -- not repeated.
697 --
698 -- Three kinds of source nodes have associated nodes:
699 --
700 -- a) those that can reference (denote) entities, that is identifiers,
701 -- character literals, expanded_names, operator symbols, operators,
702 -- and attribute reference nodes. These nodes have an Entity field
703 -- and are the set of nodes that are in N_Has_Entity.
704 --
705 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
706 --
707 -- c) selected components (N_Selected_Component)
708 --
709 -- For the first class, the associated node preserves the entity if it is
710 -- global. If the generic contains nested instantiations, the associated
711 -- node itself has been recopied, and a chain of them must be followed.
712 --
713 -- For aggregates, the associated node allows retrieval of the type, which
714 -- may otherwise not appear in the generic. The view of this type may be
715 -- different between generic and instantiation, and the full view can be
716 -- installed before the instantiation is analyzed. For aggregates of type
717 -- extensions, the same view exchange may have to be performed for some of
718 -- the ancestor types, if their view is private at the point of
719 -- instantiation.
720 --
721 -- Nodes that are selected components in the parse tree may be rewritten
722 -- as expanded names after resolution, and must be treated as potential
723 -- entity holders, which is why they also have an Associated_Node.
724 --
725 -- Nodes that do not come from source, such as freeze nodes, do not appear
726 -- in the generic tree, and need not have an associated node.
727 --
728 -- The associated node is stored in the Associated_Node field. Note that
729 -- this field overlaps Entity, which is fine, because the whole point is
730 -- that we don't need or want the normal Entity field in this situation.
731
732 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
733 -- Traverse the Exchanged_Views list to see if a type was private
734 -- and has already been flipped during this phase of instantiation.
735
736 function Has_Contracts (Decl : Node_Id) return Boolean;
737 -- Determine whether a formal subprogram has a Pre- or Postcondition,
738 -- in which case a subprogram wrapper has to be built for the actual.
739
740 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
741 -- Determine whether the parameter types and the return type of Subp
742 -- are fully defined at the point of instantiation.
743
744 function Has_Null_Default (N : Node_Id) return Boolean is
745 (Nkind (N) in N_Formal_Subprogram_Declaration
746 and then Nkind (Specification (N)) = N_Procedure_Specification
747 and then Null_Present (Specification (N)));
748 -- True if N is the declaration of a formal procedure with "is null"
749 -- as the default.
750
751 procedure Hide_Current_Scope;
752 -- When instantiating a generic child unit, the parent context must be
753 -- present, but the instance and all entities that may be generated
754 -- must be inserted in the current scope. We leave the current scope
755 -- on the stack, but make its entities invisible to avoid visibility
756 -- problems. This is reversed at the end of the instantiation. This is
757 -- not done for the instantiation of the bodies, which only require the
758 -- instances of the generic parents to be in scope.
759
760 function In_Main_Context (E : Entity_Id) return Boolean;
761 -- Check whether an instantiation is in the context of the main unit.
762 -- Used to determine whether its body should be elaborated to allow
763 -- front-end inlining.
764
765 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
766 -- Add the context clause of the unit containing a generic unit to a
767 -- compilation unit that is, or contains, an instantiation.
768
769 procedure Init_Env;
770 -- Establish environment for subsequent instantiation. Separated from
771 -- Save_Env because data-structures for visibility handling must be
772 -- initialized before call to Check_Generic_Child_Unit.
773
774 procedure Inline_Instance_Body
775 (N : Node_Id;
776 Gen_Unit : Entity_Id;
777 Act_Decl : Node_Id);
778 -- If front-end inlining is requested, instantiate the package body,
779 -- and preserve the visibility of its compilation unit, to insure
780 -- that successive instantiations succeed.
781
782 procedure Insert_Freeze_Node_For_Instance
783 (N : Node_Id;
784 F_Node : Node_Id);
785 -- N denotes a package or a subprogram instantiation and F_Node is the
786 -- associated freeze node. Insert the freeze node before the first source
787 -- body which follows immediately after N. If no such body is found, the
788 -- freeze node is inserted at the end of the declarative region which
789 -- contains N, unless the instantiation is done in a package spec that is
790 -- not at library level, in which case it is inserted at the outer level.
791 -- This can also be invoked to insert the freeze node of a package that
792 -- encloses an instantiation, in which case N may denote an arbitrary node.
793
794 procedure Install_Formal_Packages (Par : Entity_Id);
795 -- Install the visible part of any formal of the parent that is a formal
796 -- package. Note that for the case of a formal package with a box, this
797 -- includes the formal part of the formal package (12.7(10/2)).
798
799 procedure Install_Hidden_Primitives
800 (Prims_List : in out Elist_Id;
801 Gen_T : Entity_Id;
802 Act_T : Entity_Id);
803 -- Remove suffix 'P' from hidden primitives of Act_T to match the
804 -- visibility of primitives of Gen_T. The list of primitives to which
805 -- the suffix is removed is added to Prims_List to restore them later.
806
807 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
808 -- When compiling an instance of a child unit the parent (which is
809 -- itself an instance) is an enclosing scope that must be made
810 -- immediately visible. This procedure is also used to install the non-
811 -- generic parent of a generic child unit when compiling its body, so
812 -- that full views of types in the parent are made visible.
813
814 -- The functions Instantiate_... perform various legality checks and build
815 -- the declarations for instantiated generic parameters. In all of these
816 -- Formal is the entity in the generic unit, Actual is the entity or
817 -- expression in the generic associations, and Analyzed_Formal is the
818 -- formal in the generic copy, which contains the semantic information to
819 -- be used to validate the actual.
820
821 function Instantiate_Object
822 (Formal : Node_Id;
823 Actual : Node_Id;
824 Analyzed_Formal : Node_Id) return List_Id;
825
826 function Instantiate_Type
827 (Formal : Node_Id;
828 Actual : Node_Id;
829 Analyzed_Formal : Node_Id;
830 Actual_Decls : List_Id) return List_Id;
831 -- Actual_Decls is the list of renamings being built; this is used for
832 -- formal derived types, to determine whether the parent type is another
833 -- formal derived type in the same generic unit.
834 -- Note that the call site appends the result of this function onto
835 -- the same list it is passing to Actual_Decls.
836
837 function Instantiate_Formal_Subprogram
838 (Formal : Node_Id;
839 Actual : Node_Id;
840 Analyzed_Formal : Node_Id) return Node_Id;
841
842 function Instantiate_Formal_Package
843 (Formal : Node_Id;
844 Actual : Node_Id;
845 Analyzed_Formal : Node_Id) return List_Id;
846 -- If the formal package is declared with a box, special visibility rules
847 -- apply to its formals: they are in the visible part of the package. This
848 -- is true in the declarative region of the formal package, that is to say
849 -- in the enclosing generic or instantiation. For an instantiation, the
850 -- parameters of the formal package are made visible in an explicit step.
851 -- Furthermore, if the actual has a visible USE clause, these formals must
852 -- be made potentially use-visible as well. On exit from the enclosing
853 -- instantiation, the reverse must be done.
854
855 -- For a formal package declared without a box, there are conformance rules
856 -- that apply to the actuals in the generic declaration and the actuals of
857 -- the actual package in the enclosing instantiation. The simplest way to
858 -- apply these rules is to repeat the instantiation of the formal package
859 -- in the context of the enclosing instance, and compare the generic
860 -- associations of this instantiation with those of the actual package.
861 -- This internal instantiation only needs to contain the renamings of the
862 -- formals: the visible and private declarations themselves need not be
863 -- created.
864
865 -- In Ada 2005, the formal package may be only partially parameterized.
866 -- In that case the visibility step must make visible those actuals whose
867 -- corresponding formals were given with a box. A final complication
868 -- involves inherited operations from formal derived types, which must
869 -- be visible if the type is.
870
871 function Is_In_Main_Unit (N : Node_Id) return Boolean;
872 -- Test if given node is in the main unit
873
874 procedure Load_Parent_Of_Generic
875 (N : Node_Id;
876 Spec : Node_Id;
877 Body_Optional : Boolean := False);
878 -- If the generic appears in a separate non-generic library unit, load the
879 -- corresponding body to retrieve the body of the generic. N is the node
880 -- for the generic instantiation, Spec is the generic package declaration.
881 --
882 -- Body_Optional is a flag that indicates that the body is being loaded to
883 -- ensure that temporaries are generated consistently when there are other
884 -- instances in the current declarative part that precede the one being
885 -- loaded. In that case a missing body is acceptable.
886
887 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
888 -- Within the generic part, entities in the formal package are
889 -- visible. To validate subsequent type declarations, indicate
890 -- the correspondence between the entities in the analyzed formal,
891 -- and the entities in the actual package. There are three packages
892 -- involved in the instantiation of a formal package: the parent
893 -- generic P1 which appears in the generic declaration, the fake
894 -- instantiation P2 which appears in the analyzed generic, and whose
895 -- visible entities may be used in subsequent formals, and the actual
896 -- P3 in the instance. To validate subsequent formals, me indicate
897 -- that the entities in P2 are mapped into those of P3. The mapping of
898 -- entities has to be done recursively for nested packages.
899
900 procedure Move_Freeze_Nodes
901 (Out_Of : Entity_Id;
902 After : Node_Id;
903 L : List_Id);
904 -- Freeze nodes can be generated in the analysis of a generic unit, but
905 -- will not be seen by the back-end. It is necessary to move those nodes
906 -- to the enclosing scope if they freeze an outer entity. We place them
907 -- at the end of the enclosing generic package, which is semantically
908 -- neutral.
909
910 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
911 -- Analyze actuals to perform name resolution. Full resolution is done
912 -- later, when the expected types are known, but names have to be captured
913 -- before installing parents of generics, that are not visible for the
914 -- actuals themselves.
915 --
916 -- If Inst is present, it is the entity of the package instance. This
917 -- entity is marked as having a limited_view actual when some actual is
918 -- a limited view. This is used to place the instance body properly.
919
920 procedure Provide_Completing_Bodies (N : Node_Id);
921 -- Generate completing bodies for all subprograms found within package or
922 -- subprogram declaration N.
923
924 procedure Remove_Parent (In_Body : Boolean := False);
925 -- Reverse effect after instantiation of child is complete
926
927 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
928 -- Determine whether Subp renames one of the subprograms defined in the
929 -- generated package Standard.
930
931 function Requires_Conformance_Checking (N : Node_Id) return Boolean;
932 -- Determine whether the formal package declaration N requires conformance
933 -- checking with actuals in instantiations.
934
935 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
936 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
937 -- set to No_Elist.
938
939 procedure Set_Instance_Env
940 (Gen_Unit : Entity_Id;
941 Act_Unit : Entity_Id);
942 -- Save current instance on saved environment, to be used to determine
943 -- the global status of entities in nested instances. Part of Save_Env.
944 -- called after verifying that the generic unit is legal for the instance,
945 -- The procedure also examines whether the generic unit is a predefined
946 -- unit, in order to set configuration switches accordingly. As a result
947 -- the procedure must be called after analyzing and freezing the actuals.
948
949 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
950 -- Associate analyzed generic parameter with corresponding instance. Used
951 -- for semantic checks at instantiation time.
952
953 function True_Parent (N : Node_Id) return Node_Id;
954 -- For a subunit, return parent of corresponding stub, else return
955 -- parent of node.
956
957 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
958 -- Verify that an attribute that appears as the default for a formal
959 -- subprogram is a function or procedure with the correct profile.
960
961 procedure Validate_Formal_Type_Default (Decl : Node_Id);
962 -- Ada_2022 AI12-205: if a default subtype_mark is present, verify
963 -- that it is the name of a type in the same class as the formal.
964 -- The treatment parallels what is done in Instantiate_Type but differs
965 -- in a few ways so that this machinery cannot be reused as is: on one
966 -- hand there are no visibility issues for a default, because it is
967 -- analyzed in the same context as the formal type definition; on the
968 -- other hand the check needs to take into acount the use of a previous
969 -- formal type in the current formal type definition (see details in
970 -- AI12-0205).
971
972 -------------------------------------------
973 -- Data Structures for Generic Renamings --
974 -------------------------------------------
975
976 -- The map Generic_Renamings associates generic entities with their
977 -- corresponding actuals. Currently used to validate type instances. It
978 -- will eventually be used for all generic parameters to eliminate the
979 -- need for overload resolution in the instance.
980
981 type Assoc_Ptr is new Int;
982
983 Assoc_Null : constant Assoc_Ptr := -1;
984
985 type Assoc is record
986 Gen_Id : Entity_Id;
987 Act_Id : Entity_Id;
988 Next_In_HTable : Assoc_Ptr;
989 end record;
990
991 package Generic_Renamings is new Table.Table
992 (Table_Component_Type => Assoc,
993 Table_Index_Type => Assoc_Ptr,
994 Table_Low_Bound => 0,
995 Table_Initial => 10,
996 Table_Increment => 100,
997 Table_Name => "Generic_Renamings");
998
999 -- Variable to hold enclosing instantiation. When the environment is
1000 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
1001
1002 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
1003
1004 -- Hash table for associations
1005
1006 HTable_Size : constant := 37;
1007 type HTable_Range is range 0 .. HTable_Size - 1;
1008
1009 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
1010 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
1011 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
1012 function Hash (F : Entity_Id) return HTable_Range;
1013
1014 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
1015 Header_Num => HTable_Range,
1016 Element => Assoc,
1017 Elmt_Ptr => Assoc_Ptr,
1018 Null_Ptr => Assoc_Null,
1019 Set_Next => Set_Next_Assoc,
1020 Next => Next_Assoc,
1021 Key => Entity_Id,
1022 Get_Key => Get_Gen_Id,
1023 Hash => Hash,
1024 Equal => "=");
1025
1026 Exchanged_Views : Elist_Id;
1027 -- This list holds the private views that have been exchanged during
1028 -- instantiation to restore the visibility of the generic declaration.
1029 -- (see comments above). After instantiation, the current visibility is
1030 -- reestablished by means of a traversal of this list.
1031
1032 Hidden_Entities : Elist_Id;
1033 -- This list holds the entities of the current scope that are removed
1034 -- from immediate visibility when instantiating a child unit. Their
1035 -- visibility is restored in Remove_Parent.
1036
1037 -- Because instantiations can be recursive, the following must be saved
1038 -- on entry and restored on exit from an instantiation (spec or body).
1039 -- This is done by the two procedures Save_Env and Restore_Env. For
1040 -- package and subprogram instantiations (but not for the body instances)
1041 -- the action of Save_Env is done in two steps: Init_Env is called before
1042 -- Check_Generic_Child_Unit, because setting the parent instances requires
1043 -- that the visibility data structures be properly initialized. Once the
1044 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
1045
1046 Parent_Unit_Visible : Boolean := False;
1047 -- Parent_Unit_Visible is used when the generic is a child unit, and
1048 -- indicates whether the ultimate parent of the generic is visible in the
1049 -- instantiation environment. It is used to reset the visibility of the
1050 -- parent at the end of the instantiation (see Remove_Parent).
1051
1052 Instance_Parent_Unit : Entity_Id := Empty;
1053 -- This records the ultimate parent unit of an instance of a generic
1054 -- child unit and is used in conjunction with Parent_Unit_Visible to
1055 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
1056
1057 type Instance_Env is record
1058 Instantiated_Parent : Assoc;
1059 Exchanged_Views : Elist_Id;
1060 Hidden_Entities : Elist_Id;
1061 Current_Sem_Unit : Unit_Number_Type;
1062 Parent_Unit_Visible : Boolean := False;
1063 Instance_Parent_Unit : Entity_Id := Empty;
1064 Switches : Config_Switches_Type;
1065 end record;
1066
1067 package Instance_Envs is new Table.Table (
1068 Table_Component_Type => Instance_Env,
1069 Table_Index_Type => Int,
1070 Table_Low_Bound => 0,
1071 Table_Initial => 32,
1072 Table_Increment => 100,
1073 Table_Name => "Instance_Envs");
1074
1075 procedure Restore_Private_Views
1076 (Pack_Id : Entity_Id;
1077 Is_Package : Boolean := True);
1078 -- Restore the private views of external types, and unmark the generic
1079 -- renamings of actuals, so that they become compatible subtypes again.
1080 -- For subprograms, Pack_Id is the package constructed to hold the
1081 -- renamings.
1082
1083 procedure Switch_View (T : Entity_Id);
1084 -- Switch the partial and full views of a type and its private
1085 -- dependents (i.e. its subtypes and derived types).
1086
1087 ------------------------------------
1088 -- Structures for Error Reporting --
1089 ------------------------------------
1090
1091 Instantiation_Node : Node_Id;
1092 -- Used by subprograms that validate instantiation of formal parameters
1093 -- where there might be no actual on which to place the error message.
1094 -- Also used to locate the instantiation node for generic subunits.
1095
1096 Instantiation_Error : exception;
1097 -- When there is a semantic error in the generic parameter matching,
1098 -- there is no point in continuing the instantiation, because the
1099 -- number of cascaded errors is unpredictable. This exception aborts
1100 -- the instantiation process altogether.
1101
1102 S_Adjustment : Sloc_Adjustment;
1103 -- Offset created for each node in an instantiation, in order to keep
1104 -- track of the source position of the instantiation in each of its nodes.
1105 -- A subsequent semantic error or warning on a construct of the instance
1106 -- points to both places: the original generic node, and the point of
1107 -- instantiation. See Sinput and Sinput.L for additional details.
1108
1109 ------------------------------------------------------------
1110 -- Data structure for keeping track when inside a Generic --
1111 ------------------------------------------------------------
1112
1113 -- The following table is used to save values of the Inside_A_Generic
1114 -- flag (see spec of Sem) when they are saved by Start_Generic.
1115
1116 package Generic_Flags is new Table.Table (
1117 Table_Component_Type => Boolean,
1118 Table_Index_Type => Int,
1119 Table_Low_Bound => 0,
1120 Table_Initial => 32,
1121 Table_Increment => 200,
1122 Table_Name => "Generic_Flags");
1123
1124 ------------------
1125 -- Associations --
1126 ------------------
1127
1128 package Associations is
1129
1130 type Actual_Kind is
1131 (None,
1132 None_Use_Clause,
1133 -- Used when the "formal" is a use clause; there is no corresponding
1134 -- actual.
1135 Box_Subp_Default,
1136 -- Used for "is <>" as a subprogram default
1137 Box_Actual,
1138 -- Used for explicit "name => <>" and "others => <>" in formal
1139 -- packages.
1140 Name_Exp,
1141 -- Name or expression or ....
1142 -- Used for an explicit_generic_actual_parameter, and also for the
1143 -- default_expression of an in-mode formal, the default_subtype_mark
1144 -- of a formal type, and the default_name of a formal subprogram.
1145 Null_Default,
1146 -- Used for "is null" as a subprogram default.
1147 Exp_Func_Default,
1148 -- Used for "is (expression)" as a subprogram default,
1149 -- which is a language extension (and is different from "is name"
1150 -- without parentheses).
1151 Dummy_Assoc
1152 -- Used for the dummy associations that are created in
1153 -- Save_Global_Defaults. These have Explicit_Generic_Actual_Parameter
1154 -- = Empty and Box_Present = False
1155 );
1156 -- ???We wouldn't need this enumeration type if we created new node
1157 -- kinds for N_Box_Subp_Default, N_Box_Actual, N_Null_Default, and
1158 -- N_Exp_Func_Default.
1159
1160 type Generic_Actual_Rec (Kind : Actual_Kind := None) is record
1161 -- Representation of one generic actual parameter
1162 case Kind is
1163 when None | None_Use_Clause | Box_Subp_Default | Box_Actual |
1164 Null_Default | Dummy_Assoc =>
1165 null;
1166 when Name_Exp | Exp_Func_Default =>
1167 Name_Exp : Node_Id;
1168 end case;
1169 end record;
1170
1171 type Actual_Origin_Enum is
1172 (None, From_Explicit_Actual, From_Default, From_Inference,
1173 From_Others_Box);
1174 -- Indication of where the Actual came from -- explicitly in the
1175 -- instantiation, inferred from some other type, or defaulted.
1176
1177 type Inference_Reason is
1178 -- Reason an actual type corresponding to a formal type was (or could
1179 -- be) inferred from the actual type corresponding to another formal
1180 -- type.
1181 (Designated_Type, -- designated type from formal access
1182 Index_Type, -- index type from formal array
1183 Component_Type, -- component type from formal array
1184 Discriminant_Type); -- discriminant type from formal discriminated
1185
1186 function Image (Reason : Inference_Reason) return String is
1187 (case Reason is
1188 when Designated_Type => "designated type",
1189 when Index_Type => "index type",
1190 when Component_Type => "component type",
1191 when Discriminant_Type => "discriminant type");
1192
1193 type Assoc_Index is new Pos;
1194 subtype Assoc_Count is Assoc_Index'Base range 0 .. Assoc_Index'Last;
1195
1196 type Assoc_Rec is record
1197 -- Association between a single formal/actual pair. But we store both
1198 -- the unanalyzed and analyzed formal.
1199
1200 Un_Formal, An_Formal : Node_Id; -- unanalyzed and analyzed formals
1201 -- An_Formal is the node in the generic copy that corresponds to
1202 -- Un_Formal. The semantic information on this node is used to
1203 -- perform legality checks on the actuals. Because semantic analysis
1204 -- can introduce some anonymous entities or modify the declaration
1205 -- node itself, the correspondence between the two lists is not
1206 -- one-one. In addition to anonymous types, a formal "=" will
1207 -- introduce an implicit equal and opposite "/=".
1208
1209 Explicit_Assoc : Opt_N_Generic_Association_Id;
1210 -- Explicit association, if any, from the source or generated.
1211
1212 Actual : Generic_Actual_Rec;
1213 -- Generic actual parameter corresponding to Un_Formal/An_Formal,
1214 -- possibly from defaults or others/boxes.
1215
1216 Actual_Origin : Actual_Origin_Enum;
1217 -- Reason why Actual was set; where it came from
1218
1219 Info_Inferred_Actual : Opt_Type_Kind_Id;
1220 -- An inferred actual is always a type entity, not a box, and not
1221 -- something like T'Base. This is used only for messages and
1222 -- assertions. It contains the type that was, or could have been,
1223 -- inferred.
1224
1225 Inferred_From : Assoc_Index;
1226 -- Index of a later Assoc_Rec in the same Gen_Assocs_Rec from which
1227 -- this one was inferred, or could be inferred.
1228 -- Valid only if Info_Inferred_Actual is present.
1229
1230 Reason : Inference_Reason;
1231 -- Reason the type was inferred, or could have been inferred.
1232 -- Valid only if Info_Inferred_Actual is present.
1233 end record; -- Assoc_Rec
1234
1235 type Assoc_Array is array (Assoc_Index range <>) of Assoc_Rec;
1236 -- One element for each formal and (if legal) for each corresponding
1237 -- actual.
1238
1239 type Gen_Assocs_Rec (Num_Assocs : Assoc_Count) is record
1240 -- Representation of formal/actual matching. Num_Assocs
1241 -- is the number of formals and (if legal) the number
1242 -- of actuals.
1243 Gen_Unit : Entity_Id;
1244 -- the generic unit being instantiated
1245 Others_Present : Boolean;
1246 -- True if "others => <>" (only for formal packages)
1247 Assocs : Assoc_Array (1 .. Num_Assocs);
1248 -- One for each formal/actual pair; defaulted and inferred actuals
1249 -- are included.
1250 end record;
1251
1252 function Match_Assocs
1253 (I_Node : Node_Id; Formals : List_Id; F_Copy : List_Id)
1254 return Gen_Assocs_Rec;
1255 -- I_Node is the instantiation node. Formals is the list of unanalyzed
1256 -- formals. F_Copy is the analyzed list of formals in the generic copy.
1257 -- Return a Gen_Assocs_Rec with formals, explicit actuals, and default
1258 -- actuals filled in. Check legality rules related to formal/actual
1259 -- matching.
1260
1261 procedure Note_Potential_Inference
1262 (I_Node : Node_Id; Gen_Assocs : Gen_Assocs_Rec);
1263 -- If -gnatd_I, print "info:" messages about type inference that could
1264 -- have been done.
1265
1266 end Associations;
1267
1268 procedure Analyze_One_Association
1269 (I_Node : Node_Id; -- instantiation node
1270 Assoc : Associations.Assoc_Rec;
1271 -- Logical 'in out' parameters:
1272 Result_Renamings : List_Id;
1273 Default_Actuals : List_Id;
1274 Actuals_To_Freeze : Elist_Id);
1275 -- Called by Analyze_Associations for each association. The renamings
1276 -- are appended onto Result_Renamings. Defaulted actuals are appended
1277 -- onto Default_Actuals, and actuals that require freezing are
1278 -- appended onto Actuals_To_Freeze.
1279
1280 procedure Check_Fixed_Point_Warning
1281 (Gen_Assocs : Associations.Gen_Assocs_Rec;
1282 Renamings : List_Id);
1283 -- Warn if any actual is a fixed-point type that has user-defined
1284 -- arithmetic operators, but there is no corresponding formal in the
1285 -- generic, in which case the predefined operators will be used. This
1286 -- merits a warning because of the special semantics of fixed point
1287 -- operators. However, do not warn if the formal is private, because there
1288 -- can be no arithmetic operators in the generic so there no danger of
1289 -- confusion.
1290
1291 ------------------
1292 -- Associations --
1293 ------------------
1294
1295 package body Associations is
1296
1297 generic
1298 with procedure Action (F : Node_Id; Index : Assoc_Index);
1299 procedure Formal_Iter (Formals : List_Id);
1300 -- Iterate through the unanalyzed formals, calling Action for each one.
1301 -- Skip pragmas, but do not skip use clauses.
1302
1303 function Num_Formals (Formals : List_Id) return Assoc_Count;
1304 -- Note: does not include pragmas that occur in the Formals list;
1305 -- it does include use clauses.
1306
1307 generic
1308 with procedure Action (F : Node_Id; Index : Assoc_Index);
1309 procedure An_Formal_Iter (An_Formals : List_Id);
1310 -- Iterate through the analyzed formals, calling Action for each one
1311 -- that corresponds to an unanalyzed formal. This should call Action
1312 -- exactly the same number of times that Formal_Iter calls its Action.
1313 -- Skip pragmas, but do not skip use clauses. Skip extraneous
1314 -- analyzed formals in cases where there are multiple ones
1315 -- corresponding to a particular unanalyzed one.
1316
1317 function Num_An_Formals (F_Copy : List_Id) return Assoc_Count;
1318 -- Number of analyzed formals that correspond directly to unanalyzed
1319 -- formals. There are all sorts of other things in F_Copy, which
1320 -- are not counted.
1321
1322 procedure Check_Box (I_Node, Actual : Node_Id);
1323 -- Check for errors in "others => <>" and "Name => <>"
1324
1325 function Default (Un_Formal : Node_Id) return Generic_Actual_Rec;
1326 -- Return the default for a given formal, which can be a name,
1327 -- expression, box, etc.
1328
1329 procedure Match_Positional
1330 (Src_Assoc : in out Node_Id; Assoc : in out Assoc_Rec);
1331 -- Called by Match_Assocs to match one positional parameter association.
1332 -- If the current formal (in Assoc) is not a use clause, then there is a
1333 -- match, and we set Assoc.Actual and move Src_Assoc to the next one.
1334
1335 procedure Match_Named
1336 (Src_Assoc : Node_Id; Assoc : in out Assoc_Rec;
1337 Found : in out Boolean);
1338 -- Called by Match_Assocs to match one named parameter association.
1339 -- If the current formal (in Assoc) is not a use clause, and the
1340 -- selector name matches the formal name, then there is a match,
1341 -- and we set Assoc.Actual. We also set the Selector_Name to denote
1342 -- the matched formal, and set Found to True.
1343
1344 procedure Inference_Msg
1345 (Gen_Unit : Entity_Id;
1346 Inferred_To, Inferred_From : Assoc_Rec;
1347 Was_Inferred : Boolean);
1348 -- If Was_Inferred is True, this prints out an "info:" message
1349 -- showing the inference.
1350 -- If Was_Inferred is False, the message says that it could have
1351 -- been inferred.
1352
1353 function Find_Assoc
1354 (Gen_Assocs : Gen_Assocs_Rec; F : Entity_Id) return Assoc_Index;
1355 -- Return the index of F in Gen_Assocs.Assocs, which must be
1356 -- present.
1357
1358 procedure Maybe_Infer_One
1359 (Gen_Assocs : in out Gen_Assocs_Rec;
1360 FF, AA : N_Entity_Id; Inferred_From : Assoc_Index;
1361 Reason : Inference_Reason);
1362 -- If it makes sense to infer that formal FF is associated with
1363 -- actual AA, then do so.
1364
1365 procedure Infer_From_Access
1366 (Gen_Assocs : in out Gen_Assocs_Rec;
1367 Index : Assoc_Index;
1368 F : Node_Id;
1369 A_Full : Entity_Id);
1370 -- Try to infer the designated type
1371
1372 procedure Infer_From_Array
1373 (Gen_Assocs : in out Gen_Assocs_Rec;
1374 Index : Assoc_Index;
1375 F : Node_Id;
1376 A_Full : Entity_Id);
1377 -- Try to infer the index and component types
1378
1379 procedure Infer_From_Discriminated
1380 (Gen_Assocs : in out Gen_Assocs_Rec;
1381 Index : Assoc_Index;
1382 F : Node_Id;
1383 A_Full : Entity_Id);
1384 -- Try to infer the types of discriminants
1385
1386 procedure Infer_Actuals (Gen_Assocs : in out Gen_Assocs_Rec);
1387 -- Called by Match_Assocs after processing explicit and defaulted
1388 -- parameters to infer any that are still missing.
1389
1390 -----------------
1391 -- Formal_Iter --
1392 -----------------
1393
1394 -- Formal_Iter is straightforward; An_Formal_Iter is not.
1395
1396 procedure Formal_Iter (Formals : List_Id) is
1397 F : Node_Id := First (Formals);
1398 Index : Assoc_Index := 1;
1399 begin
1400 while Present (F) loop
1401 case Nkind (F) is
1402 when N_Formal_Object_Declaration
1403 | N_Formal_Type_Declaration
1404 | N_Formal_Subprogram_Declaration
1405 | N_Formal_Package_Declaration
1406 | N_Use_Package_Clause
1407 | N_Use_Type_Clause
1408 =>
1409 Action (F, Index);
1410 Index := Index + 1;
1411 when N_Pragma =>
1412 null;
1413 when others =>
1414 raise Program_Error;
1415 end case;
1416
1417 Next (F);
1418 end loop;
1419 end Formal_Iter;
1420
1421 -----------------
1422 -- Num_Formals --
1423 -----------------
1424
1425 function Num_Formals (Formals : List_Id) return Assoc_Count is
1426 Result : Assoc_Count := 0;
1427 procedure Action (Ignore_F : Node_Id; Ignore : Assoc_Index);
1428 procedure Action (Ignore_F : Node_Id; Ignore : Assoc_Index) is
1429 begin
1430 Result := Result + 1;
1431 end Action;
1432 procedure Iter is new Formal_Iter (Action);
1433 begin
1434 Iter (Formals);
1435 return Result;
1436 end Num_Formals;
1437
1438 --------------------
1439 -- An_Formal_Iter --
1440 --------------------
1441
1442 procedure An_Formal_Iter (An_Formals : List_Id) is
1443 F : Node_Id := First (An_Formals);
1444 Index : Assoc_Index := 1;
1445 begin
1446 -- The correspondence between unanalyzed and analyzed formals is not
1447 -- one-one; hence this needs to do some fancy footwork to skip some
1448 -- items in the analyzed formals list. In each case where multiple
1449 -- items in An_Formals correspond to a particular unanalyzed formal,
1450 -- we must pick the "main" one.
1451
1452 while Present (F) loop
1453 case Nkind (F) is
1454 when N_Use_Package_Clause | N_Use_Type_Clause =>
1455 Action (F, Index);
1456 Index := Index + 1;
1457
1458 when N_Formal_Object_Declaration
1459 | N_Formal_Type_Declaration
1460 | N_Formal_Subprogram_Declaration
1461 | N_Package_Declaration
1462 | N_Full_Type_Declaration
1463 | N_Private_Type_Declaration
1464 | N_Private_Extension_Declaration
1465 =>
1466 if Is_Internal_Name (Chars (Defining_Entity (F))) then
1467 null;
1468 else
1469 Action (F, Index);
1470 Index := Index + 1;
1471
1472 -- Skip full type of derived type
1473
1474 if Nkind (F) = N_Full_Type_Declaration
1475 and then Nkind (Type_Definition (F)) =
1476 N_Derived_Type_Definition
1477 and then Present (Next (F))
1478 and then Nkind (Next (F)) = N_Full_Type_Declaration
1479 and then Chars (Defining_Identifier (F)) =
1480 Chars (Defining_Identifier (Next (F)))
1481 then
1482 Next (F);
1483 end if;
1484 end if;
1485
1486 when N_Subtype_Declaration =>
1487 if Nkind (Original_Node (F)) in N_Formal_Type_Declaration
1488 then
1489 pragma Assert
1490 (not Is_Internal_Name (Chars (Defining_Entity (F))));
1491 Action (F, Index);
1492 Index := Index + 1;
1493
1494 elsif Nkind (Original_Node (F)) in N_Full_Type_Declaration
1495 then
1496 null;
1497 else
1498 -- subtype of a formal object
1499
1500 pragma Assert
1501 (Nkind (Next (F)) = N_Formal_Object_Declaration);
1502 end if;
1503
1504 when N_Pragma =>
1505 null;
1506
1507 when N_Formal_Package_Declaration =>
1508 -- If there were no errors, this would have been transformed
1509 -- into an N_Package_Declaration.
1510
1511 Check_Error_Detected;
1512 pragma Assert (Error_Posted (F));
1513 Abandon_Instantiation (Instantiation_Node);
1514
1515 when others =>
1516 raise Program_Error;
1517 end case;
1518
1519 Next (F);
1520 end loop;
1521 end An_Formal_Iter;
1522
1523 --------------------
1524 -- Num_An_Formals --
1525 --------------------
1526
1527 function Num_An_Formals (F_Copy : List_Id) return Assoc_Count is
1528 Result : Assoc_Count := 0;
1529 procedure Action (Ignore_F : Node_Id; Ignore : Assoc_Index);
1530 procedure Action (Ignore_F : Node_Id; Ignore : Assoc_Index) is
1531 begin
1532 Result := Result + 1;
1533 end Action;
1534 procedure Iter is new An_Formal_Iter (Action);
1535 begin
1536 Iter (F_Copy);
1537 return Result;
1538 end Num_An_Formals;
1539
1540 ---------------
1541 -- Check_Box --
1542 ---------------
1543
1544 procedure Check_Box (I_Node, Actual : Node_Id) is
1545 begin
1546 -- "... => <>" is allowed only in formal packages, not old-fashioned
1547 -- instantiations.
1548
1549 if Nkind (I_Node) /= N_Formal_Package_Declaration
1550 and then Comes_From_Source (I_Node)
1551 then
1552 if Actual in N_Others_Choice_Id then
1553 Error_Msg_N
1554 ("OTHERS association not allowed in an instance", Actual);
1555 elsif Box_Present (Actual) then
1556 Error_Msg_N
1557 ("box association not allowed in an instance", Actual);
1558 end if;
1559 end if;
1560
1561 -- "others => <>" must come last
1562
1563 if Actual in N_Others_Choice_Id
1564 and then Present (Next (Actual))
1565 then
1566 Error_Msg_N
1567 ("OTHERS must be last association", Actual);
1568 end if;
1569 end Check_Box;
1570
1571 -------------
1572 -- Default --
1573 -------------
1574
1575 function Default (Un_Formal : Node_Id) return Generic_Actual_Rec is
1576 begin
1577 return Result : Generic_Actual_Rec do
1578 case Nkind (Un_Formal) is
1579 when N_Formal_Object_Declaration =>
1580 if Present (Default_Expression (Un_Formal)) then
1581 Result := (Name_Exp, Default_Expression (Un_Formal));
1582 end if;
1583 when N_Formal_Type_Declaration =>
1584 if Present (Default_Subtype_Mark (Un_Formal)) then
1585 Result := (Name_Exp, Default_Subtype_Mark (Un_Formal));
1586 end if;
1587 when N_Formal_Subprogram_Declaration =>
1588 if Present (Default_Name (Un_Formal)) then
1589 pragma Assert (Result.Kind = None);
1590 Result := (Name_Exp, Default_Name (Un_Formal));
1591 end if;
1592
1593 if Box_Present (Un_Formal) then
1594 pragma Assert (Result.Kind = None);
1595 Result := (Kind => Box_Subp_Default);
1596 end if;
1597
1598 if Present (Expression (Un_Formal)) then
1599 pragma Assert (Result.Kind = None);
1600 Result := (Exp_Func_Default, Expression (Un_Formal));
1601 end if;
1602
1603 if Has_Null_Default (Un_Formal) then
1604 pragma Assert (Result.Kind = None);
1605 Result := (Kind => Null_Default);
1606 end if;
1607
1608 when N_Formal_Package_Declaration => null;
1609
1610 when others => raise Program_Error;
1611 end case;
1612 pragma Assert
1613 (if Result.Kind in Name_Exp | Exp_Func_Default then
1614 Present (Result.Name_Exp));
1615 end return;
1616 end Default;
1617
1618 ----------------------
1619 -- Match_Positional --
1620 ----------------------
1621
1622 procedure Match_Positional
1623 (Src_Assoc : in out Node_Id; Assoc : in out Assoc_Rec) is
1624 begin
1625 if Nkind (Assoc.Un_Formal) not in
1626 N_Use_Package_Clause | N_Use_Type_Clause
1627 then
1628 pragma Assert (No (Assoc.Explicit_Assoc));
1629 pragma Assert (Assoc.Actual.Kind = None);
1630 Assoc.Explicit_Assoc := Src_Assoc;
1631
1632 -- A "<>" without "name =>" is illegal syntax
1633
1634 if Box_Present (Src_Assoc) then
1635 Assoc.Actual := (Kind => Box_Actual);
1636 if False then -- ???
1637 -- Disable this for now, because we have various
1638 -- code that needs to be updated.
1639 Error_Msg_N
1640 ("box requires named notation", Src_Assoc);
1641 end if;
1642 else
1643 Assoc.Actual :=
1644 (Name_Exp,
1645 Explicit_Generic_Actual_Parameter (Src_Assoc));
1646 pragma Assert (Present (Assoc.Actual.Name_Exp));
1647 end if;
1648 Assoc.Actual_Origin := From_Explicit_Actual;
1649
1650 Next (Src_Assoc);
1651 end if;
1652 end Match_Positional;
1653
1654 -----------------
1655 -- Match_Named --
1656 -----------------
1657
1658 procedure Match_Named
1659 (Src_Assoc : Node_Id; Assoc : in out Assoc_Rec;
1660 Found : in out Boolean) is
1661 begin
1662 if Nkind (Assoc.Un_Formal) not in
1663 N_Use_Package_Clause | N_Use_Type_Clause
1664 and then Chars (Selector_Name (Src_Assoc)) =
1665 Chars (Defining_Entity (Assoc.Un_Formal))
1666 then
1667 if Found then -- second formal with the same name
1668 pragma Assert (Comes_From_Source (Src_Assoc));
1669 Error_Msg_N
1670 ("named association not allowed for " &
1671 "overloaded formal", Src_Assoc);
1672 Abandon_Instantiation (Instantiation_Node);
1673 end if;
1674
1675 if Assoc.Actual.Kind /= None then
1676 if Comes_From_Source (Src_Assoc) then
1677 Error_Msg_NE
1678 ("duplicate actual for &",
1679 Src_Assoc, Selector_Name (Src_Assoc));
1680 end if;
1681 else
1682 Assoc.Explicit_Assoc := Src_Assoc;
1683 if Box_Present (Src_Assoc) then
1684 Assoc.Actual := (Kind => Box_Actual);
1685
1686 else
1687 if No (Explicit_Generic_Actual_Parameter (Src_Assoc)) then
1688 Assoc.Actual := (Kind => Dummy_Assoc);
1689 else
1690 Assoc.Actual :=
1691 (Name_Exp,
1692 Explicit_Generic_Actual_Parameter (Src_Assoc));
1693 end if;
1694
1695 -- Set Entity (etc.) of the selector name:
1696
1697 declare
1698 A_F : constant Entity_Id :=
1699 Defining_Entity (Assoc.An_Formal);
1700 Orig_F : constant Node_Id :=
1701 Original_Node (Assoc.An_Formal);
1702 Sel : constant Node_Id :=
1703 Selector_Name (Assoc.Explicit_Assoc);
1704 begin
1705 Set_Entity (Sel, A_F);
1706 Set_Etype (Sel, Etype (A_F));
1707
1708 if Nkind (Orig_F) = N_Formal_Package_Declaration then
1709 Generate_Reference (Defining_Identifier (Orig_F), Sel);
1710 -- ???Original_Node makes no sense, but we're
1711 -- preserving the old behavior.
1712 else
1713 Generate_Reference (A_F, Sel);
1714 end if;
1715 end;
1716 end if;
1717
1718 Assoc.Actual_Origin := From_Explicit_Actual;
1719 Found := True;
1720 end if;
1721 end if;
1722 end Match_Named;
1723
1724 ------------------
1725 -- Match_Assocs --
1726 ------------------
1727
1728 function Match_Assocs
1729 (I_Node : Node_Id; Formals : List_Id; F_Copy : List_Id)
1730 return Gen_Assocs_Rec
1731 is
1732 Src_Assocs : constant List_Id := Generic_Associations (I_Node);
1733 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
1734 begin
1735 pragma Assert
1736 (Num_An_Formals (F_Copy) = Num_Formals (Formals)
1737 or else Serious_Errors_Detected > 0);
1738
1739 return Result : Gen_Assocs_Rec (Num_Assocs => Num_Formals (Formals))
1740 do
1741 Result.Gen_Unit := Gen_Unit;
1742 Result.Others_Present := False;
1743
1744 -- Loop through the unanalyzed formals:
1745
1746 declare
1747 procedure Set_Formal (F : Node_Id; Index : Assoc_Index);
1748 -- Initialize one Assoc_Rec so the formal is set.
1749 -- Use a dummy assoc for use clauses.
1750
1751 procedure Set_Formal (F : Node_Id; Index : Assoc_Index) is
1752 Assoc : Assoc_Rec renames Result.Assocs (Index);
1753 begin
1754 if Nkind (F) in N_Use_Package_Clause | N_Use_Type_Clause then
1755 Assoc :=
1756 (Un_Formal => F,
1757 An_Formal => Empty,
1758 Explicit_Assoc => Empty,
1759 Actual => (Kind => None_Use_Clause),
1760 Actual_Origin => None,
1761 Info_Inferred_Actual => Empty,
1762 Inferred_From => <>,
1763 Reason => <>);
1764 else
1765 Assoc :=
1766 (Un_Formal => F,
1767 An_Formal => Empty,
1768 Explicit_Assoc => Empty,
1769 Actual => <>,
1770 Actual_Origin => None,
1771 Info_Inferred_Actual => Empty,
1772 Inferred_From => <>,
1773 Reason => <>);
1774 end if;
1775 end Set_Formal;
1776 procedure Iter is new Formal_Iter (Set_Formal);
1777 begin
1778 Iter (Formals);
1779 end;
1780
1781 -- Loop through the analyzed copy of the formals:
1782
1783 declare
1784 procedure Set_An_Formal (F : Node_Id; Index : Assoc_Index);
1785 procedure Set_An_Formal (F : Node_Id; Index : Assoc_Index) is
1786 Assoc : Assoc_Rec renames Result.Assocs (Index);
1787 begin
1788 Assoc.An_Formal := F;
1789 if Nkind (F) in N_Use_Package_Clause | N_Use_Type_Clause then
1790 pragma Assert
1791 (Nkind (Assoc.Un_Formal) = Nkind (Assoc.An_Formal));
1792
1793 else
1794 case Nkind (Assoc.Un_Formal) is
1795 when N_Formal_Object_Declaration
1796 | N_Formal_Subprogram_Declaration
1797 =>
1798 pragma Assert
1799 (Nkind (Assoc.Un_Formal) =
1800 Nkind (Assoc.An_Formal));
1801
1802 when N_Formal_Type_Declaration =>
1803 pragma Assert
1804 (Nkind (Original_Node (Assoc.An_Formal)) =
1805 N_Formal_Type_Declaration);
1806 pragma Assert
1807 (Nkind (Assoc.An_Formal) in
1808 N_Formal_Type_Declaration
1809 | N_Full_Type_Declaration
1810 | N_Private_Type_Declaration
1811 | N_Private_Extension_Declaration
1812 | N_Subtype_Declaration);
1813
1814 when N_Formal_Package_Declaration =>
1815 pragma Assert
1816 (Nkind (Original_Node (Assoc.An_Formal)) =
1817 N_Formal_Package_Declaration);
1818 pragma Assert
1819 (Nkind (Assoc.An_Formal) = N_Package_Declaration);
1820
1821 when others => pragma Assert (False);
1822 end case;
1823
1824 pragma Assert
1825 (Chars (Defining_Entity (Assoc.Un_Formal)) =
1826 Chars (Defining_Entity (Assoc.An_Formal)));
1827 end if;
1828 end Set_An_Formal;
1829
1830 procedure Iter is new An_Formal_Iter (Set_An_Formal);
1831 begin
1832 pragma Assert
1833 (Num_An_Formals (F_Copy) = Result.Assocs'Last
1834 or else Serious_Errors_Detected > 0);
1835 Iter (F_Copy);
1836 end;
1837
1838 -- Loop through actual source associations:
1839
1840 declare
1841 Src_Assoc : Node_Id := First (Src_Assocs);
1842 -- Generic association from the source
1843
1844 function Positional return Boolean is
1845 (Present (Src_Assoc)
1846 and then Src_Assoc not in N_Others_Choice_Id
1847 and then No (Selector_Name (Src_Assoc)));
1848 -- True if Src_Assoc is position; i.e. not named and not others
1849 begin
1850 -- Loop through positional actuals:
1851
1852 for Index in Result.Assocs'Range loop
1853 exit when not Positional;
1854 Match_Positional (Src_Assoc, Result.Assocs (Index));
1855 end loop;
1856
1857 if Positional then
1858 Error_Msg_Sloc := Sloc (Gen_Unit);
1859 Error_Msg_NE
1860 ("unmatched actual in instantiation of & declared#",
1861 Src_Assoc, Gen_Unit);
1862 else
1863 -- Loop through named actuals and "others => <>":
1864
1865 while Present (Src_Assoc) loop
1866 Check_Box (I_Node, Src_Assoc);
1867 if Src_Assoc in N_Others_Choice_Id then
1868 Result.Others_Present := True;
1869 exit;
1870 end if;
1871
1872 if Positional then
1873 Error_Msg_N
1874 ("invalid positional actual after named one",
1875 Src_Assoc);
1876 else
1877 -- For actual "X => ...", find formal whose name is X.
1878 -- Complain if X has already been specified (could be
1879 -- by a positional association, or by a previous named
1880 -- one). Also complain if there's more than one X.
1881 -- See RM-12.3(9/3) and 12.7(4.1/3).
1882 -- However, this rule does not apply to generated
1883 -- code,because for nested instances, we routinely
1884 -- generate things like:
1885 -- X => ..., X => ...
1886 -- where the first one refers to the first formal X,
1887 -- and the second one refers to the second formal X,
1888 -- and so on. (The X's are formal subprograms in this
1889 -- case.)
1890
1891 declare
1892 Found : Boolean := False;
1893 begin
1894 for Index in Result.Assocs'Range loop
1895 Match_Named
1896 (Src_Assoc, Result.Assocs (Index), Found);
1897 exit when Found
1898 and then not Comes_From_Source (Src_Assoc);
1899 end loop;
1900
1901 if not Found and then Comes_From_Source (Src_Assoc)
1902 then
1903 Error_Msg_Sloc := Sloc (Gen_Unit);
1904 Error_Msg_NE
1905 ("unmatched actual &",
1906 Src_Assoc, Selector_Name (Src_Assoc));
1907 Error_Msg_NE
1908 ("\in instantiation of & declared#",
1909 Src_Assoc, Gen_Unit);
1910 end if;
1911 end;
1912 end if;
1913
1914 Next (Src_Assoc);
1915 end loop;
1916 end if;
1917 end;
1918
1919 -- Fill in defaults. For each formal F with no associated actual,
1920 -- if there is "others => <>", set the actual to "F => <>".
1921 -- Otherwise, if the formal has a default, set the actual to
1922 -- "F => default". Otherwise leave it Empty.
1923 -- (If Empty, it could be inferred, or it could be an error).
1924
1925 for Index in Result.Assocs'Range loop
1926 declare
1927 Assoc : Assoc_Rec renames Result.Assocs (Index);
1928 begin
1929 if Assoc.Actual.Kind = None then
1930 pragma Assert (No (Assoc.Explicit_Assoc));
1931 if Result.Others_Present then
1932 Assoc.Actual := (Kind => Box_Actual);
1933 Assoc.Actual_Origin := From_Others_Box;
1934 else
1935 Assoc.Actual := Default (Assoc.Un_Formal);
1936 if Assoc.Actual.Kind /= None then
1937 Assoc.Actual_Origin := From_Default;
1938 end if;
1939 end if;
1940 end if;
1941 end;
1942 end loop;
1943
1944 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1945 Infer_Actuals (Gen_Assocs => Result);
1946 end if;
1947
1948 -- Check for missing actuals
1949
1950 for Index in Result.Assocs'Range loop
1951 if Result.Assocs (Index).Actual.Kind = None then
1952 Error_Msg_Sloc := Sloc (Gen_Unit);
1953 Error_Msg_NE
1954 ("missing actual &",
1955 Instantiation_Node,
1956 Defining_Entity (Result.Assocs (Index).Un_Formal));
1957 Error_Msg_NE
1958 ("\in instantiation of & declared#",
1959 Instantiation_Node, Gen_Unit);
1960 Abandon_Instantiation (Instantiation_Node);
1961 end if;
1962 end loop;
1963 end return;
1964 end Match_Assocs;
1965
1966 -------------------
1967 -- Inference_Msg --
1968 -------------------
1969
1970 procedure Inference_Msg
1971 (Gen_Unit : Entity_Id;
1972 Inferred_To, Inferred_From : Assoc_Rec;
1973 Was_Inferred : Boolean)
1974 is
1975 pragma Assert (Debug_Flag_Underscore_II); -- This is only for -gnatd_I
1976
1977 Was : constant String := (if Was_Inferred then "" else "could have ");
1978
1979 -- "if True" below to leave out some verbosity for now:
1980 Inst : constant String :=
1981 (if True then ""
1982 else " gen: " & Get_Name_String (Chars (Gen_Unit)));
1983 Decl : constant String := (if True then "" else " declared # ");
1984
1985 R : constant String := " (" & Image (Inferred_To.Reason) & ")";
1986
1987 Mess : constant String :=
1988 "info: " & Was & "inferred `% ='> &`" & Decl & Inst & R;
1989 Mess_2 : constant String :=
1990 "info: `% ='> ...`";
1991 begin
1992 Error_Msg_Name_1 := Chars (Defining_Entity (Inferred_To.An_Formal));
1993 Error_Msg_Sloc := Sloc (Inferred_To.Info_Inferred_Actual);
1994 if not In_Instance then
1995 if Debug_Flag_Underscore_II then
1996 Error_Msg_NE
1997 (Mess, Inferred_From.Actual.Name_Exp,
1998 Inferred_To.Info_Inferred_Actual);
1999 Error_Msg_Name_1 :=
2000 Chars (Defining_Identifier (Inferred_From.An_Formal));
2001 Error_Msg_N (Mess_2, Inferred_From.Actual.Name_Exp);
2002 end if;
2003 end if;
2004 end Inference_Msg;
2005
2006 ------------------------------
2007 -- Note_Potential_Inference --
2008 ------------------------------
2009
2010 procedure Note_Potential_Inference
2011 (I_Node : Node_Id; Gen_Assocs : Gen_Assocs_Rec)
2012 is
2013 begin
2014 if not Debug_Flag_Underscore_II or else Serious_Errors_Detected > 0
2015 then
2016 return;
2017 end if;
2018
2019 for Index in Gen_Assocs.Assocs'Range loop
2020 declare
2021 Assoc : Assoc_Rec renames Gen_Assocs.Assocs (Index);
2022 begin
2023 if Assoc.Actual_Origin = From_Explicit_Actual
2024 and then Present (Assoc.Info_Inferred_Actual)
2025 and then In_Extended_Main_Source_Unit (I_Node)
2026 and then not In_Internal_Unit (I_Node)
2027 then
2028 Inference_Msg
2029 (Gen_Assocs.Gen_Unit,
2030 Inferred_To => Assoc,
2031 Inferred_From => Gen_Assocs.Assocs (Assoc.Inferred_From),
2032 Was_Inferred => False);
2033 end if;
2034 end;
2035 end loop;
2036 end Note_Potential_Inference;
2037
2038 --------------
2039 -- Find_Assoc --
2040 --------------
2041
2042 function Find_Assoc
2043 (Gen_Assocs : Gen_Assocs_Rec; F : Entity_Id) return Assoc_Index
2044 is
2045 begin
2046 for Index in Gen_Assocs.Assocs'Range loop
2047 declare
2048 An_F : constant Node_Id := Gen_Assocs.Assocs (Index).An_Formal;
2049 begin
2050 if Nkind (An_F) not in N_Use_Package_Clause | N_Use_Type_Clause
2051 and then Defining_Entity (An_F) = F
2052 then
2053 return Index;
2054 end if;
2055 end;
2056 end loop;
2057
2058 raise Program_Error; -- it must be present
2059 end Find_Assoc;
2060
2061 ---------------------
2062 -- Maybe_Infer_One --
2063 ---------------------
2064
2065 procedure Maybe_Infer_One
2066 (Gen_Assocs : in out Gen_Assocs_Rec;
2067 FF, AA : N_Entity_Id; Inferred_From : Assoc_Index;
2068 Reason : Inference_Reason)
2069 is
2070 begin
2071 if not (Is_Generic_Type (FF)
2072 and then Scope (FF) = Gen_Assocs.Gen_Unit)
2073 then
2074 return; -- no inference if not a formal type of this generic
2075 end if;
2076
2077 if Is_Internal_Name (Chars (FF)) or else Is_Itype (AA) then
2078 return; -- no inference if internally generated
2079 end if;
2080
2081 declare
2082 Index : constant Assoc_Index := Find_Assoc (Gen_Assocs, FF);
2083 Assoc : Assoc_Rec renames Gen_Assocs.Assocs (Index);
2084 pragma Assert (Defining_Entity (Assoc.An_Formal) = FF);
2085
2086 From_Actual : constant Node_Id :=
2087 Gen_Assocs.Assocs (Inferred_From).Actual.Name_Exp;
2088
2089 begin
2090 Assoc.Info_Inferred_Actual := AA;
2091 Assoc.Inferred_From := Inferred_From;
2092 Assoc.Reason := Reason;
2093
2094 if Assoc.Actual.Kind = None then
2095 Assoc.Actual :=
2096 (Name_Exp, New_Occurrence_Of (AA, Sloc (From_Actual)));
2097 Assoc.Actual_Origin := From_Inference;
2098
2099 Error_Msg_GNAT_Extension
2100 ("type inference of generic parameters",
2101 Sloc (From_Actual));
2102
2103 if Debug_Flag_Underscore_II then
2104 Inference_Msg
2105 (Gen_Assocs.Gen_Unit,
2106 Inferred_To => Assoc,
2107 Inferred_From => Gen_Assocs.Assocs (Assoc.Inferred_From),
2108 Was_Inferred => True);
2109 end if;
2110 end if;
2111 end;
2112 end Maybe_Infer_One;
2113
2114 -------------------
2115 -- Infer_Actuals --
2116 -------------------
2117
2118 procedure Infer_From_Access
2119 (Gen_Assocs : in out Gen_Assocs_Rec;
2120 Index : Assoc_Index;
2121 F : Node_Id;
2122 A_Full : Entity_Id)
2123 is
2124 begin
2125 if Ekind (A_Full) in Access_Kind then
2126 declare
2127 FF : constant Entity_Id :=
2128 Designated_Type (Defining_Entity (F));
2129 AA : constant Entity_Id := Designated_Type (A_Full);
2130 begin
2131 Maybe_Infer_One
2132 (Gen_Assocs,
2133 FF,
2134 AA,
2135 Inferred_From => Index,
2136 Reason => Designated_Type);
2137 end;
2138 end if;
2139 end Infer_From_Access;
2140
2141 procedure Infer_From_Array
2142 (Gen_Assocs : in out Gen_Assocs_Rec;
2143 Index : Assoc_Index;
2144 F : Node_Id;
2145 A_Full : Entity_Id)
2146 is
2147 begin
2148 if Ekind (A_Full) in Array_Kind then
2149 declare
2150 F_Index_Type : Opt_N_Is_Index_Id :=
2151 First_Index (Defining_Entity (F));
2152 A_Index_Type : Opt_N_Is_Index_Id :=
2153 First_Index (A_Full);
2154 begin
2155 while Present (F_Index_Type) and then Present (A_Index_Type)
2156 loop
2157 Maybe_Infer_One
2158 (Gen_Assocs,
2159 Etype (F_Index_Type),
2160 Etype (A_Index_Type),
2161 Inferred_From => Index,
2162 Reason => Index_Type);
2163
2164 Next_Index (F_Index_Type);
2165 Next_Index (A_Index_Type);
2166 end loop;
2167 end;
2168
2169 declare
2170 F_Comp_Type : constant Type_Kind_Id :=
2171 Component_Type (Defining_Entity (F));
2172 A_Comp_Type : constant Type_Kind_Id :=
2173 Component_Type (A_Full);
2174 begin
2175 Maybe_Infer_One
2176 (Gen_Assocs,
2177 F_Comp_Type,
2178 A_Comp_Type,
2179 Inferred_From => Index,
2180 Reason => Component_Type);
2181 end;
2182 end if;
2183 end Infer_From_Array;
2184
2185 procedure Infer_From_Discriminated
2186 (Gen_Assocs : in out Gen_Assocs_Rec;
2187 Index : Assoc_Index;
2188 F : Node_Id;
2189 A_Full : Entity_Id)
2190 is
2191 begin
2192 if Has_Discriminants (Defining_Entity (F))
2193 and then Present (A_Full)
2194 and then Has_Discriminants (A_Full)
2195 and then Number_Discriminants (A_Full) =
2196 Number_Discriminants (Defining_Entity (F))
2197 then
2198 declare
2199 F_Discrim : Node_Id := First_Discriminant (Defining_Entity (F));
2200 A_Discrim : Node_Id := First_Discriminant (A_Full);
2201 begin
2202 while Present (F_Discrim) loop
2203 Maybe_Infer_One
2204 (Gen_Assocs,
2205 Etype (F_Discrim),
2206 Etype (A_Discrim),
2207 Inferred_From => Index,
2208 Reason => Discriminant_Type);
2209
2210 Next_Discriminant (F_Discrim);
2211 Next_Discriminant (A_Discrim);
2212 end loop;
2213 pragma Assert (No (A_Discrim)); -- same number as F_Discrim
2214 end;
2215 end if;
2216 end Infer_From_Discriminated;
2217
2218 procedure Infer_Actuals (Gen_Assocs : in out Gen_Assocs_Rec) is
2219 -- Note that we can infer FROM defaults, but we cannot infer TO a
2220 -- parameter that has a default. We can also infer from inferred
2221 -- types.
2222
2223 -- We don't need to check that multiple inferences get the same
2224 -- answer; the second one will get a type mismatch or nonstatically
2225 -- matching error.
2226
2227 -- This code needs to be robust, in the sense of tolerating illegal
2228 -- code, because we have not yet checked all legality rules. For
2229 -- example, if a formal type F has a discriminant whose type is
2230 -- another formal type, then we want to infer the type of the
2231 -- discriminant from the actual for F. That actual must have
2232 -- discriminants, but we have not checked that rule yet, so we
2233 -- need to tolerate an actual for F that has no discriminants.
2234
2235 begin
2236 -- For each parameter, check whether we can infer FROM that one TO
2237 -- other ones.
2238
2239 -- Process the parameters in reverse order, because the inferred type
2240 -- always comes before the parameter it is inferred from. This
2241 -- ensures that we can do the inference in one pass, including in
2242 -- cases where an inferred type leads to another inferred type.
2243 -- For example, an array type that allows us to infer the component
2244 -- type, which is an access type that allows us to infer the
2245 -- designated type. The reverse loop implies that we will see the
2246 -- array type, then the access type, then the designated type.
2247
2248 for Index in reverse Gen_Assocs.Assocs'Range loop -- NB: "reverse"
2249 if Gen_Assocs.Assocs (Index).Actual.Kind = Name_Exp then
2250 declare
2251 F : constant Node_Id := Gen_Assocs.Assocs (Index).An_Formal;
2252 A_E : constant Node_Id :=
2253 Gen_Assocs.Assocs (Index).Actual.Name_Exp;
2254 A_Full : Entity_Id := Empty;
2255 begin
2256 if Nkind (A_E) in N_Has_Entity then
2257 A_Full := Entity (A_E);
2258
2259 if Present (A_Full)
2260 and then Ekind (A_Full) in Incomplete_Kind
2261 and then Present (Full_View (A_Full))
2262 then
2263 A_Full := Full_View (A_Full);
2264 end if;
2265 end if;
2266
2267 if Nkind (F) = N_Formal_Type_Declaration
2268 and then Present (A_Full)
2269 then
2270 case Ekind (Defining_Entity (F)) is
2271 when E_Access_Type | E_General_Access_Type =>
2272 Infer_From_Access (Gen_Assocs, Index, F, A_Full);
2273
2274 when E_Access_Subtype
2275 | E_Access_Attribute_Type
2276 | E_Allocator_Type
2277 | E_Anonymous_Access_Type
2278 =>
2279 raise Program_Error;
2280
2281 when E_Array_Type | E_Array_Subtype =>
2282 Infer_From_Array (Gen_Assocs, Index, F, A_Full);
2283
2284 when E_String_Literal_Subtype =>
2285 raise Program_Error;
2286
2287 when others =>
2288 null;
2289 end case;
2290
2291 Infer_From_Discriminated (Gen_Assocs, Index, F, A_Full);
2292 end if;
2293 end;
2294 end if;
2295 end loop;
2296 end Infer_Actuals;
2297
2298 end Associations;
2299
2300 ---------------------------
2301 -- Abandon_Instantiation --
2302 ---------------------------
2303
2304 procedure Abandon_Instantiation (N : Node_Id) is
2305 begin
2306 Error_Msg_N ("instantiation abandoned!", N);
2307 raise Instantiation_Error;
2308 end Abandon_Instantiation;
2309
2310 ----------------------------------
2311 -- Adjust_Inherited_Pragma_Sloc --
2312 ----------------------------------
2313
2314 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
2315 begin
2316 Adjust_Instantiation_Sloc (N, S_Adjustment);
2317 end Adjust_Inherited_Pragma_Sloc;
2318
2319 --------------------------
2320 -- Analyze_Associations --
2321 --------------------------
2322
2323 function Analyze_Associations
2324 (I_Node : Node_Id;
2325 Formals : List_Id;
2326 F_Copy : List_Id) return List_Id
2327 is
2328 use Associations;
2329
2330 Result_Renamings : constant List_Id := New_List;
2331 -- To be returned. Includes "renamings" broadly interpreted
2332 -- (e.g. subtypes are used for types).
2333
2334 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
2335 Default_Actuals : constant List_Id := New_List;
2336
2337 Gen_Assocs : constant Gen_Assocs_Rec :=
2338 Match_Assocs (I_Node, Formals, F_Copy);
2339
2340 begin
2341 for Matching_Actual_Index in Gen_Assocs.Assocs'Range loop
2342 declare
2343 Assoc : Assoc_Rec renames
2344 Gen_Assocs.Assocs (Matching_Actual_Index);
2345 begin
2346 if Nkind (Assoc.Un_Formal) = N_Formal_Package_Declaration
2347 and then Error_Posted (Assoc.An_Formal)
2348 then
2349 -- Restrict this to N_Formal_Package_Declaration,
2350 -- because otherwise we miss errors.
2351 Abandon_Instantiation (Instantiation_Node);
2352 end if;
2353
2354 if Nkind (Assoc.Un_Formal) in
2355 N_Use_Package_Clause | N_Use_Type_Clause
2356 then
2357 -- Copy the use clause to where it belongs:
2358 Append (New_Copy_Tree (Assoc.Un_Formal), Result_Renamings);
2359
2360 else
2361 Analyze_One_Association
2362 (I_Node, Assoc,
2363 Result_Renamings, Default_Actuals, Actuals_To_Freeze);
2364 end if;
2365 end;
2366 end loop;
2367
2368 -- An instantiation freezes all generic actuals, except for incomplete
2369 -- types and subprograms that are not fully defined at the point of
2370 -- instantiation.
2371
2372 declare
2373 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2374 begin
2375 while Present (Elmt) loop
2376 Freeze_Before (I_Node, Node (Elmt));
2377 Next_Elmt (Elmt);
2378 end loop;
2379 end;
2380
2381 -- If there are defaults, normalize the tree by adding explicit
2382 -- associations for them. This is required if the instance appears
2383 -- within a generic.
2384
2385 if not Is_Empty_List (Default_Actuals) then
2386 declare
2387 Default : Node_Id;
2388
2389 begin
2390 Default := First (Default_Actuals);
2391 while Present (Default) loop
2392 Mark_Rewrite_Insertion (Default);
2393 Next (Default);
2394 end loop;
2395
2396 if No (Generic_Associations (I_Node)) then
2397 Set_Generic_Associations (I_Node, Default_Actuals);
2398 else
2399 Append_List_To (Generic_Associations (I_Node), Default_Actuals);
2400 end if;
2401 end;
2402 end if;
2403
2404 Note_Potential_Inference (I_Node, Gen_Assocs);
2405
2406 Check_Fixed_Point_Warning (Gen_Assocs, Result_Renamings);
2407
2408 return Result_Renamings;
2409 end Analyze_Associations;
2410
2411 -----------------------------
2412 -- Analyze_One_Association --
2413 -----------------------------
2414
2415 procedure Analyze_One_Association
2416 (I_Node : Node_Id;
2417 Assoc : Associations.Assoc_Rec;
2418 -- Logical 'in out' parameters:
2419 Result_Renamings : List_Id;
2420 Default_Actuals : List_Id;
2421 Actuals_To_Freeze : Elist_Id)
2422 is
2423 use Associations;
2424
2425 procedure Process_Box_Actual (Formal : Node_Id);
2426 -- Called for "Formal => <>", and also if "Formal => ..." is missing,
2427 -- but there is "others => <>". Add a copy of the declaration of the
2428 -- generic formal to the Result_Renamings.
2429
2430 ------------------------
2431 -- Process_Box_Actual --
2432 ------------------------
2433
2434 procedure Process_Box_Actual (Formal : Node_Id) is
2435 pragma Assert (Assoc.Actual.Kind = Box_Actual);
2436 F_Id : constant Entity_Id := Defining_Entity (Formal);
2437 Decl : constant Node_Id := New_Copy_Tree (Formal);
2438 Id : constant Entity_Id :=
2439 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
2440 begin
2441 if Nkind (Formal) in N_Formal_Subprogram_Declaration then
2442 Set_Defining_Unit_Name (Specification (Decl), Id);
2443
2444 else
2445 Set_Defining_Identifier (Decl, Id);
2446 end if;
2447
2448 Append (Decl, Result_Renamings);
2449 end Process_Box_Actual;
2450
2451 Match : Node_Id;
2452
2453 -- Start of processing for Analyze_One_Association
2454
2455 begin
2456 pragma Assert (Assoc.Actual_Origin /= None);
2457
2458 if Assoc.Actual_Origin = From_Explicit_Actual
2459 and then Assoc.Actual.Kind = Name_Exp
2460 then
2461 Match := Assoc.Actual.Name_Exp;
2462
2463 if Is_Entity_Name (Match) and then Present (Entity (Match)) then
2464 Set_Used_As_Generic_Actual (Entity (Match));
2465 end if;
2466 else
2467 Match := Empty;
2468 end if;
2469
2470 case Nkind (Assoc.Un_Formal) is
2471 when N_Formal_Object_Declaration =>
2472 if Assoc.Actual.Kind = Box_Actual then
2473 Process_Box_Actual (Assoc.Un_Formal);
2474
2475 else
2476 Append_List
2477 (Instantiate_Object (Assoc.Un_Formal, Match, Assoc.An_Formal),
2478 Result_Renamings);
2479
2480 -- GNATprove: For a defaulted in-mode parameter, create
2481 -- an entry in the list of defaulted actuals, for
2482 -- GNATprove use. Do not include these defaults for an
2483 -- instance nested within a generic, because the defaults
2484 -- are also used in the analysis of the enclosing
2485 -- generic, and only defaulted subprograms are relevant
2486 -- there.
2487
2488 if No (Match) and then not Inside_A_Generic then
2489 Append_To (Default_Actuals,
2490 Make_Generic_Association (Sloc (I_Node),
2491 Selector_Name =>
2492 New_Occurrence_Of
2493 (Defining_Identifier
2494 (Assoc.Un_Formal), Sloc (I_Node)),
2495 Explicit_Generic_Actual_Parameter =>
2496 New_Copy_Tree (Default_Expression (Assoc.Un_Formal))));
2497 end if;
2498 end if;
2499
2500 -- If the object is a call to an expression function, this
2501 -- is a freezing point for it.
2502
2503 if Is_Entity_Name (Match)
2504 and then Present (Entity (Match))
2505 and then Nkind
2506 (Original_Node (Unit_Declaration_Node (Entity (Match))))
2507 = N_Expression_Function
2508 then
2509 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2510 end if;
2511
2512 when N_Formal_Type_Declaration =>
2513 if Assoc.Actual.Kind = Box_Actual then
2514 Process_Box_Actual (Assoc.Un_Formal);
2515
2516 elsif No (Match) then
2517 -- No explicit actual; try default
2518
2519 if Present (Default_Subtype_Mark (Assoc.Un_Formal)) then
2520 Match := New_Copy (Default_Subtype_Mark (Assoc.Un_Formal));
2521
2522 -- If the Entity of the default subtype denoted by the
2523 -- unanalyzed formal has not been set, then it must refer
2524 -- to another formal type of the enclosing generic. So we
2525 -- locate the subtype "renaming" in Result_Renamings that
2526 -- corresponds to the formal type (by comparing the simple
2527 -- names), and set Match's Entity to the entity denoted by
2528 -- that subtype's subtype_indication (which will denote the
2529 -- actual subtype corresponding to the other formal type).
2530 -- This must be done before calling Instantiate_Type, since
2531 -- that function relies heavily on the entity being set.
2532 -- (Note also that there's similar code inside procedure
2533 -- Validate_Derived_Type_Instance that deals with retrieving
2534 -- the ancestor type of formal derived types.)
2535
2536 if No (Entity (Match)) then
2537 declare
2538 pragma Assert (Is_Non_Empty_List (Result_Renamings));
2539
2540 Decl : Node_Id := First (Result_Renamings);
2541
2542 begin
2543 -- Locate subtype referenced by the default subtype
2544 -- in the list of renamings.
2545
2546 while Present (Decl) loop
2547 if Nkind (Decl) = N_Subtype_Declaration
2548 and then
2549 Chars (Match) =
2550 Chars (Defining_Identifier (Decl))
2551 then
2552 Set_Entity
2553 (Match,
2554 Entity (Subtype_Indication (Decl)));
2555
2556 exit;
2557
2558 else
2559 Next (Decl);
2560 end if;
2561 end loop;
2562
2563 pragma Assert (Present (Entity (Match)));
2564 end;
2565 end if;
2566
2567 Append_List
2568 (Instantiate_Type
2569 (Assoc.Un_Formal, Match, Assoc.An_Formal,
2570 Result_Renamings),
2571 Result_Renamings);
2572 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2573
2574 -- No explicit actual and no default; must be inference
2575
2576 else
2577 pragma Assert (Assoc.Actual_Origin = From_Inference);
2578
2579 Match := Assoc.Actual.Name_Exp;
2580 Append_List
2581 (Instantiate_Type
2582 (Assoc.Un_Formal,
2583 Match,
2584 Assoc.An_Formal,
2585 Result_Renamings),
2586 Result_Renamings);
2587 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2588 end if;
2589
2590 else
2591 Analyze (Match);
2592
2593 -- Rewrite mutably tagged types to be their class-wide
2594 -- equivalent type.
2595
2596 if Ekind (Etype (Match)) /= E_Void
2597 and then Is_Mutably_Tagged_Type (Etype (Match))
2598 then
2599 Rewrite (Match, New_Occurrence_Of
2600 (Class_Wide_Equivalent_Type
2601 (Etype (Match)), Sloc (Match)));
2602 Analyze (Match);
2603 end if;
2604
2605 Append_List
2606 (Instantiate_Type
2607 (Assoc.Un_Formal, Match, Assoc.An_Formal,
2608 Result_Renamings),
2609 Result_Renamings);
2610
2611 -- An instantiation is a freeze point for the actuals,
2612 -- unless this is a rewritten formal package, or the
2613 -- formal is an Ada 2012 formal incomplete type.
2614
2615 if Nkind (I_Node) = N_Formal_Package_Declaration
2616 or else
2617 (Ada_Version >= Ada_2012
2618 and then
2619 Ekind (Defining_Identifier (Assoc.An_Formal)) =
2620 E_Incomplete_Type)
2621 then
2622 null;
2623
2624 else
2625 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2626 end if;
2627 end if;
2628
2629 -- A remote access-to-class-wide type is not a legal actual
2630 -- for a generic formal of an access type (E.2.2(17/2)).
2631 -- In GNAT an exception to this rule is introduced when
2632 -- the formal is marked as remote using implementation
2633 -- defined aspect/pragma Remote_Access_Type. In that case
2634 -- the actual must be remote as well.
2635
2636 -- If the current instantiation is the construction of a
2637 -- local copy for a formal package the actuals may be
2638 -- defaulted, and there is no matching actual to check.
2639
2640 if Nkind (Assoc.An_Formal) = N_Formal_Type_Declaration
2641 and then
2642 Nkind (Formal_Type_Definition (Assoc.An_Formal)) =
2643 N_Access_To_Object_Definition
2644 and then Present (Match)
2645 then
2646 declare
2647 Formal_Ent : constant Entity_Id :=
2648 Defining_Identifier (Assoc.An_Formal);
2649 begin
2650 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
2651 = Is_Remote_Types (Formal_Ent)
2652 then
2653 -- Remoteness of formal and actual match
2654
2655 null;
2656
2657 elsif Is_Remote_Types (Formal_Ent) then
2658
2659 -- Remote formal, non-remote actual
2660
2661 Error_Msg_NE
2662 ("actual for& must be remote", Match, Formal_Ent);
2663
2664 else
2665 -- Non-remote formal, remote actual
2666
2667 Error_Msg_NE
2668 ("actual for& may not be remote",
2669 Match, Formal_Ent);
2670 end if;
2671 end;
2672 end if;
2673
2674 when N_Formal_Subprogram_Declaration =>
2675 -- If there is no corresponding actual, this may be case
2676 -- of partial parameterization, or else the formal has a
2677 -- default or a box.
2678
2679 if Assoc.Actual.Kind = Box_Actual then
2680 Process_Box_Actual (Assoc.Un_Formal);
2681
2682 else
2683 Append_To (Result_Renamings,
2684 Instantiate_Formal_Subprogram
2685 (Assoc.Un_Formal, Match, Assoc.An_Formal));
2686
2687 -- If formal subprogram has contracts, create wrappers
2688 -- for it. This is an expansion activity that cannot
2689 -- take place e.g. within an enclosing generic unit.
2690
2691 if Has_Contracts (Assoc.An_Formal)
2692 and then (Expander_Active or GNATprove_Mode)
2693 then
2694 Build_Subprogram_Wrappers
2695 (Match, Assoc.An_Formal, Result_Renamings);
2696 end if;
2697
2698 -- An instantiation is a freeze point for the actuals,
2699 -- unless this is a rewritten formal package.
2700
2701 if Nkind (I_Node) /= N_Formal_Package_Declaration
2702 and then Nkind (Match) = N_Identifier
2703 and then Is_Subprogram (Entity (Match))
2704
2705 -- The actual subprogram may rename a routine defined
2706 -- in Standard. Avoid freezing such renamings because
2707 -- subprograms coming from Standard cannot be frozen.
2708
2709 and then
2710 not Renames_Standard_Subprogram (Entity (Match))
2711
2712 -- If the actual subprogram comes from a different
2713 -- unit, it is already frozen, either by a body in
2714 -- that unit or by the end of the declarative part
2715 -- of the unit. This check avoids the freezing of
2716 -- subprograms defined in Standard which are used
2717 -- as generic actuals.
2718
2719 and then In_Same_Code_Unit (Entity (Match), I_Node)
2720 and then Has_Fully_Defined_Profile (Entity (Match))
2721 then
2722 -- Mark the subprogram as having a delayed freeze
2723 -- since this may be an out-of-order action.
2724
2725 Set_Has_Delayed_Freeze (Entity (Match));
2726 Append_Elmt (Entity (Match), Actuals_To_Freeze);
2727 end if;
2728 end if;
2729
2730 -- If this is a nested generic, preserve default for later
2731 -- instantiations. We do this as well for GNATprove use,
2732 -- so that the list of generic associations is complete.
2733
2734 if No (Match) and then Box_Present (Assoc.Un_Formal) then
2735 declare
2736 Subp : constant Entity_Id :=
2737 Defining_Unit_Name
2738 (Specification (Last (Result_Renamings)));
2739
2740 begin
2741 Append_To (Default_Actuals,
2742 Make_Generic_Association (Sloc (I_Node),
2743 Selector_Name =>
2744 New_Occurrence_Of (Subp, Sloc (I_Node)),
2745 Explicit_Generic_Actual_Parameter =>
2746 New_Occurrence_Of (Subp, Sloc (I_Node))));
2747 end;
2748 end if;
2749
2750 when N_Formal_Package_Declaration =>
2751 if Assoc.Actual.Kind = Box_Actual then
2752 Process_Box_Actual (Assoc.Un_Formal);
2753
2754 else
2755 Analyze (Match);
2756 Append_List
2757 (Instantiate_Formal_Package
2758 (Assoc.Un_Formal, Match, Assoc.An_Formal),
2759 Result_Renamings);
2760
2761 -- Determine whether the actual package needs an explicit
2762 -- freeze node. This is only the case if the actual is
2763 -- declared in the same unit and has a body. Normally
2764 -- packages do not have explicit freeze nodes, and gigi
2765 -- only uses them to elaborate entities in a package
2766 -- body.
2767
2768 Explicit_Freeze_Check : declare
2769 Actual : constant Entity_Id := Entity (Match);
2770 Gen_Par : Entity_Id;
2771
2772 Needs_Freezing : Boolean;
2773 P : Node_Id;
2774
2775 procedure Check_Generic_Parent;
2776 -- The actual may be an instantiation of a unit
2777 -- declared in a previous instantiation. If that
2778 -- one is also in the current compilation, it must
2779 -- itself be frozen before the actual. The actual
2780 -- may be an instantiation of a generic child unit,
2781 -- in which case the same applies to the instance
2782 -- of the parent which must be frozen before the
2783 -- actual.
2784 -- Should this itself be recursive ???
2785
2786 --------------------------
2787 -- Check_Generic_Parent --
2788 --------------------------
2789
2790 procedure Check_Generic_Parent is
2791 Inst : constant Node_Id :=
2792 Get_Unit_Instantiation_Node (Actual);
2793 Par : Entity_Id;
2794
2795 begin
2796 Par := Empty;
2797
2798 if Nkind (Parent (Actual)) = N_Package_Specification
2799 then
2800 Par := Scope (Generic_Parent (Parent (Actual)));
2801
2802 if Is_Generic_Instance (Par) then
2803 null;
2804
2805 -- If the actual is a child generic unit, check
2806 -- whether the instantiation of the parent is
2807 -- also local and must also be frozen now. We
2808 -- must retrieve the instance node to locate the
2809 -- parent instance if any.
2810
2811 elsif Ekind (Par) = E_Generic_Package
2812 and then Is_Child_Unit (Gen_Par)
2813 and then Ekind (Scope (Gen_Par)) =
2814 E_Generic_Package
2815 then
2816 if Nkind (Inst) = N_Package_Instantiation
2817 and then Nkind (Name (Inst)) =
2818 N_Expanded_Name
2819 then
2820 -- Retrieve entity of parent instance
2821
2822 Par := Entity (Prefix (Name (Inst)));
2823 end if;
2824
2825 else
2826 Par := Empty;
2827 end if;
2828 end if;
2829
2830 if Present (Par)
2831 and then Is_Generic_Instance (Par)
2832 and then Scope (Par) = Current_Scope
2833 and then
2834 (No (Freeze_Node (Par))
2835 or else
2836 not Is_List_Member (Freeze_Node (Par)))
2837 then
2838 Set_Has_Delayed_Freeze (Par);
2839 Append_Elmt (Par, Actuals_To_Freeze);
2840 end if;
2841 end Check_Generic_Parent;
2842
2843 -- Start of processing for Explicit_Freeze_Check
2844
2845 begin
2846 if Present (Renamed_Entity (Actual)) then
2847 Gen_Par :=
2848 Generic_Parent (Specification
2849 (Unit_Declaration_Node
2850 (Renamed_Entity (Actual))));
2851 else
2852 Gen_Par :=
2853 Generic_Parent (Specification
2854 (Unit_Declaration_Node (Actual)));
2855 end if;
2856
2857 if not Expander_Active
2858 or else not Has_Completion (Actual)
2859 or else not In_Same_Source_Unit (I_Node, Actual)
2860 or else Is_Frozen (Actual)
2861 or else
2862 (Present (Renamed_Entity (Actual))
2863 and then
2864 not In_Same_Source_Unit
2865 (I_Node, (Renamed_Entity (Actual))))
2866 then
2867 null;
2868
2869 else
2870 -- Finally we want to exclude such freeze nodes
2871 -- from statement sequences, which freeze
2872 -- everything before them.
2873 -- Is this strictly necessary ???
2874
2875 Needs_Freezing := True;
2876
2877 P := Parent (I_Node);
2878 while Nkind (P) /= N_Compilation_Unit loop
2879 if Nkind (P) = N_Handled_Sequence_Of_Statements
2880 then
2881 Needs_Freezing := False;
2882 exit;
2883 end if;
2884
2885 P := Parent (P);
2886 end loop;
2887
2888 if Needs_Freezing then
2889 Check_Generic_Parent;
2890
2891 -- If the actual is a renaming of a proper
2892 -- instance of the formal package, indicate
2893 -- that it is the instance that must be frozen.
2894
2895 if Nkind (Parent (Actual)) =
2896 N_Package_Renaming_Declaration
2897 then
2898 Set_Has_Delayed_Freeze
2899 (Renamed_Entity (Actual));
2900 Append_Elmt
2901 (Renamed_Entity (Actual),
2902 Actuals_To_Freeze);
2903 else
2904 Set_Has_Delayed_Freeze (Actual);
2905 Append_Elmt (Actual, Actuals_To_Freeze);
2906 end if;
2907 end if;
2908 end if;
2909 end Explicit_Freeze_Check;
2910 end if;
2911
2912 when others =>
2913 raise Program_Error;
2914 end case;
2915
2916 -- Check for correct use of Ghost entities in generic
2917 -- instantiations (SPARK RM 6.9(10)).
2918
2919 Check_Ghost_Context_In_Generic_Association
2920 (Actual => Match,
2921 Formal => Defining_Entity (Assoc.An_Formal));
2922 end Analyze_One_Association;
2923
2924 -------------------------------
2925 -- Analyze_Formal_Array_Type --
2926 -------------------------------
2927
2928 procedure Analyze_Formal_Array_Type
2929 (T : in out Entity_Id;
2930 Def : Node_Id)
2931 is
2932 DSS : Node_Id;
2933
2934 begin
2935 -- Treated like a non-generic array declaration, with additional
2936 -- semantic checks.
2937
2938 Enter_Name (T);
2939
2940 if Nkind (Def) = N_Constrained_Array_Definition then
2941 DSS := First (Discrete_Subtype_Definitions (Def));
2942 while Present (DSS) loop
2943 if Nkind (DSS) in N_Subtype_Indication
2944 | N_Range
2945 | N_Attribute_Reference
2946 then
2947 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2948 end if;
2949
2950 Next (DSS);
2951 end loop;
2952 end if;
2953
2954 Array_Type_Declaration (T, Def);
2955 Set_Is_Generic_Type (Base_Type (T));
2956
2957 if Ekind (Component_Type (T)) = E_Incomplete_Type
2958 and then No (Full_View (Component_Type (T)))
2959 then
2960 Error_Msg_N ("premature usage of incomplete type", Def);
2961
2962 -- Check that range constraint is not allowed on the component type
2963 -- of a generic formal array type (AARM 12.5.3(3))
2964
2965 elsif Is_Internal (Component_Type (T))
2966 and then Present (Subtype_Indication (Component_Definition (Def)))
2967 and then Nkind (Original_Node
2968 (Subtype_Indication (Component_Definition (Def)))) =
2969 N_Subtype_Indication
2970 then
2971 Error_Msg_N
2972 ("in a formal, a subtype indication can only be "
2973 & "a subtype mark (RM 12.5.3(3))",
2974 Subtype_Indication (Component_Definition (Def)));
2975 end if;
2976
2977 end Analyze_Formal_Array_Type;
2978
2979 ---------------------------------------------
2980 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2981 ---------------------------------------------
2982
2983 -- As for other generic types, we create a valid type representation with
2984 -- legal but arbitrary attributes, whose values are never considered
2985 -- static. For all scalar types we introduce an anonymous base type, with
2986 -- the same attributes. We choose the corresponding integer type to be
2987 -- Standard_Integer.
2988 -- Here and in other similar routines, the Sloc of the generated internal
2989 -- type must be the same as the sloc of the defining identifier of the
2990 -- formal type declaration, to provide proper source navigation.
2991
2992 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2993 (T : Entity_Id;
2994 Def : Node_Id)
2995 is
2996 Loc : constant Source_Ptr := Sloc (Def);
2997
2998 Base : constant Entity_Id :=
2999 New_Internal_Entity
3000 (E_Decimal_Fixed_Point_Type,
3001 Current_Scope,
3002 Sloc (Defining_Identifier (Parent (Def))), 'G');
3003
3004 Int_Base : constant Entity_Id := Standard_Integer;
3005 Delta_Val : constant Ureal := Ureal_1;
3006 Digs_Val : constant Uint := Uint_6;
3007
3008 function Make_Dummy_Bound return Node_Id;
3009 -- Return a properly typed universal real literal to use as a bound
3010
3011 ----------------------
3012 -- Make_Dummy_Bound --
3013 ----------------------
3014
3015 function Make_Dummy_Bound return Node_Id is
3016 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
3017 begin
3018 Set_Etype (Bound, Universal_Real);
3019 return Bound;
3020 end Make_Dummy_Bound;
3021
3022 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
3023
3024 begin
3025 Enter_Name (T);
3026
3027 Set_Etype (Base, Base);
3028 Set_Size_Info (Base, Int_Base);
3029 Set_RM_Size (Base, RM_Size (Int_Base));
3030 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
3031 Set_Digits_Value (Base, Digs_Val);
3032 Set_Delta_Value (Base, Delta_Val);
3033 Set_Small_Value (Base, Delta_Val);
3034 Set_Scalar_Range (Base,
3035 Make_Range (Loc,
3036 Low_Bound => Make_Dummy_Bound,
3037 High_Bound => Make_Dummy_Bound));
3038
3039 Set_Is_Generic_Type (Base);
3040 Set_Parent (Base, Parent (Def));
3041
3042 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
3043 Set_Etype (T, Base);
3044 Set_Size_Info (T, Int_Base);
3045 Set_RM_Size (T, RM_Size (Int_Base));
3046 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
3047 Set_Digits_Value (T, Digs_Val);
3048 Set_Delta_Value (T, Delta_Val);
3049 Set_Small_Value (T, Delta_Val);
3050 Set_Scalar_Range (T, Scalar_Range (Base));
3051 Set_Is_Constrained (T);
3052
3053 Check_Restriction (No_Fixed_Point, Def);
3054 end Analyze_Formal_Decimal_Fixed_Point_Type;
3055
3056 -------------------------------------------
3057 -- Analyze_Formal_Derived_Interface_Type --
3058 -------------------------------------------
3059
3060 procedure Analyze_Formal_Derived_Interface_Type
3061 (N : Node_Id;
3062 T : Entity_Id;
3063 Def : Node_Id)
3064 is
3065 Loc : constant Source_Ptr := Sloc (Def);
3066
3067 begin
3068 -- Rewrite as a type declaration of a derived type. This ensures that
3069 -- the interface list and primitive operations are properly captured.
3070
3071 Rewrite (N,
3072 Make_Full_Type_Declaration (Loc,
3073 Defining_Identifier => T,
3074 Type_Definition => Def));
3075
3076 -- Keep the aspects from the original node
3077
3078 Move_Aspects (Original_Node (N), N);
3079
3080 Analyze (N);
3081 Set_Is_Generic_Type (T);
3082 end Analyze_Formal_Derived_Interface_Type;
3083
3084 ---------------------------------
3085 -- Analyze_Formal_Derived_Type --
3086 ---------------------------------
3087
3088 procedure Analyze_Formal_Derived_Type
3089 (N : Node_Id;
3090 T : Entity_Id;
3091 Def : Node_Id)
3092 is
3093 Loc : constant Source_Ptr := Sloc (Def);
3094 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
3095 New_N : Node_Id;
3096
3097 begin
3098 Set_Is_Generic_Type (T);
3099
3100 if Private_Present (Def) then
3101 New_N :=
3102 Make_Private_Extension_Declaration (Loc,
3103 Defining_Identifier => T,
3104 Discriminant_Specifications => Discriminant_Specifications (N),
3105 Unknown_Discriminants_Present => Unk_Disc,
3106 Abstract_Present => Abstract_Present (Def),
3107 Limited_Present => Limited_Present (Def),
3108 Subtype_Indication => Subtype_Mark (Def),
3109 Synchronized_Present => Synchronized_Present (Def),
3110 Interface_List => Interface_List (Def));
3111 else
3112 New_N :=
3113 Make_Full_Type_Declaration (Loc,
3114 Defining_Identifier => T,
3115 Discriminant_Specifications =>
3116 Discriminant_Specifications (Parent (T)),
3117 Type_Definition =>
3118 Make_Derived_Type_Definition (Loc,
3119 Abstract_Present => Abstract_Present (Def),
3120 Limited_Present => Limited_Present (Def),
3121 Subtype_Indication => Subtype_Mark (Def)));
3122 end if;
3123
3124 Rewrite (N, New_N);
3125
3126 -- Keep the aspects from the original node
3127
3128 Move_Aspects (Original_Node (N), N);
3129
3130 Analyze (N);
3131
3132 if Unk_Disc then
3133 if not Is_Composite_Type (T) then
3134 Error_Msg_N
3135 ("unknown discriminants not allowed for elementary types", N);
3136 else
3137 Set_Has_Unknown_Discriminants (T);
3138 Set_Is_Constrained (T, False);
3139 end if;
3140 end if;
3141
3142 if Subtype_Mark (Def) <= Empty_Or_Error then
3143 pragma Assert (Serious_Errors_Detected > 0);
3144 -- avoid passing bad argument to Entity
3145 return;
3146 end if;
3147
3148 -- If the parent type has a known size, so does the formal, which makes
3149 -- legal representation clauses that involve the formal.
3150
3151 Set_Size_Known_At_Compile_Time
3152 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
3153 end Analyze_Formal_Derived_Type;
3154
3155 ----------------------------------
3156 -- Analyze_Formal_Discrete_Type --
3157 ----------------------------------
3158
3159 -- The operations defined for a discrete types are those of an enumeration
3160 -- type. The size is set to an arbitrary value, for use in analyzing the
3161 -- generic unit.
3162
3163 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
3164 Loc : constant Source_Ptr := Sloc (Def);
3165 Lo : Node_Id;
3166 Hi : Node_Id;
3167
3168 Base : constant Entity_Id :=
3169 New_Internal_Entity
3170 (E_Floating_Point_Type, Current_Scope,
3171 Sloc (Defining_Identifier (Parent (Def))), 'G');
3172
3173 begin
3174 Enter_Name (T);
3175 Mutate_Ekind (T, E_Enumeration_Subtype);
3176 Set_Etype (T, Base);
3177 Init_Size (T, 8);
3178 Reinit_Alignment (T);
3179 Set_Is_Generic_Type (T);
3180 Set_Is_Constrained (T);
3181
3182 -- For semantic analysis, the bounds of the type must be set to some
3183 -- non-static value. The simplest is to create attribute nodes for those
3184 -- bounds, that refer to the type itself. These bounds are never
3185 -- analyzed but serve as place-holders.
3186
3187 Lo :=
3188 Make_Attribute_Reference (Loc,
3189 Attribute_Name => Name_First,
3190 Prefix => New_Occurrence_Of (T, Loc));
3191 Set_Etype (Lo, T);
3192
3193 Hi :=
3194 Make_Attribute_Reference (Loc,
3195 Attribute_Name => Name_Last,
3196 Prefix => New_Occurrence_Of (T, Loc));
3197 Set_Etype (Hi, T);
3198
3199 Set_Scalar_Range (T,
3200 Make_Range (Loc,
3201 Low_Bound => Lo,
3202 High_Bound => Hi));
3203
3204 Mutate_Ekind (Base, E_Enumeration_Type);
3205 Set_Etype (Base, Base);
3206 Init_Size (Base, 8);
3207 Reinit_Alignment (Base);
3208 Set_Is_Generic_Type (Base);
3209 Set_Scalar_Range (Base, Scalar_Range (T));
3210 Set_Parent (Base, Parent (Def));
3211 end Analyze_Formal_Discrete_Type;
3212
3213 ----------------------------------
3214 -- Analyze_Formal_Floating_Type --
3215 ---------------------------------
3216
3217 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
3218 Base : constant Entity_Id :=
3219 New_Internal_Entity
3220 (E_Floating_Point_Type, Current_Scope,
3221 Sloc (Defining_Identifier (Parent (Def))), 'G');
3222
3223 begin
3224 -- The various semantic attributes are taken from the predefined type
3225 -- Float, just so that all of them are initialized. Their values are
3226 -- never used because no constant folding or expansion takes place in
3227 -- the generic itself.
3228
3229 Enter_Name (T);
3230 Mutate_Ekind (T, E_Floating_Point_Subtype);
3231 Set_Etype (T, Base);
3232 Set_Size_Info (T, (Standard_Float));
3233 Set_RM_Size (T, RM_Size (Standard_Float));
3234 Set_Digits_Value (T, Digits_Value (Standard_Float));
3235 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
3236 Set_Is_Constrained (T);
3237
3238 Set_Is_Generic_Type (Base);
3239 Set_Etype (Base, Base);
3240 Set_Size_Info (Base, (Standard_Float));
3241 Set_RM_Size (Base, RM_Size (Standard_Float));
3242 Set_Digits_Value (Base, Digits_Value (Standard_Float));
3243 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
3244 Set_Parent (Base, Parent (Def));
3245
3246 Check_Restriction (No_Floating_Point, Def);
3247 end Analyze_Formal_Floating_Type;
3248
3249 -----------------------------------
3250 -- Analyze_Formal_Interface_Type;--
3251 -----------------------------------
3252
3253 procedure Analyze_Formal_Interface_Type
3254 (N : Node_Id;
3255 T : Entity_Id;
3256 Def : Node_Id)
3257 is
3258 Loc : constant Source_Ptr := Sloc (N);
3259 New_N : Node_Id;
3260
3261 begin
3262 New_N :=
3263 Make_Full_Type_Declaration (Loc,
3264 Defining_Identifier => T,
3265 Type_Definition => Def);
3266
3267 Rewrite (N, New_N);
3268
3269 -- Keep the aspects from the original node
3270
3271 Move_Aspects (Original_Node (N), N);
3272
3273 Analyze (N);
3274 Set_Is_Generic_Type (T);
3275 end Analyze_Formal_Interface_Type;
3276
3277 ---------------------------------
3278 -- Analyze_Formal_Modular_Type --
3279 ---------------------------------
3280
3281 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
3282 begin
3283 -- Apart from their entity kind, generic modular types are treated like
3284 -- signed integer types, and have the same attributes.
3285
3286 Analyze_Formal_Signed_Integer_Type (T, Def);
3287 Mutate_Ekind (T, E_Modular_Integer_Subtype);
3288 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
3289
3290 end Analyze_Formal_Modular_Type;
3291
3292 ---------------------------------------
3293 -- Analyze_Formal_Object_Declaration --
3294 ---------------------------------------
3295
3296 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
3297 E : constant Node_Id := Default_Expression (N);
3298 Id : constant Node_Id := Defining_Identifier (N);
3299
3300 K : Entity_Kind;
3301 Parent_Installed : Boolean := False;
3302 T : Node_Id;
3303
3304 begin
3305 Enter_Name (Id);
3306
3307 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3308
3309 -- Determine the mode of the formal object
3310
3311 if Out_Present (N) then
3312 K := E_Generic_In_Out_Parameter;
3313
3314 if not In_Present (N) then
3315 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
3316 end if;
3317
3318 else
3319 K := E_Generic_In_Parameter;
3320 end if;
3321
3322 if Present (Subtype_Mark (N)) then
3323 Find_Type (Subtype_Mark (N));
3324 T := Entity (Subtype_Mark (N));
3325
3326 -- Verify that there is no redundant null exclusion
3327
3328 if Null_Exclusion_Present (N) then
3329 if not Is_Access_Type (T) then
3330 Error_Msg_N
3331 ("null exclusion can only apply to an access type", N);
3332
3333 elsif Can_Never_Be_Null (T) then
3334 Error_Msg_NE
3335 ("`NOT NULL` not allowed (& already excludes null)", N, T);
3336 end if;
3337 end if;
3338
3339 -- Ada 2005 (AI-423): Formal object with an access definition
3340
3341 else
3342 Check_Access_Definition (N);
3343 T := Access_Definition
3344 (Related_Nod => N,
3345 N => Access_Definition (N));
3346 end if;
3347
3348 if Ekind (T) = E_Incomplete_Type then
3349 declare
3350 Error_Node : Node_Id;
3351
3352 begin
3353 if Present (Subtype_Mark (N)) then
3354 Error_Node := Subtype_Mark (N);
3355 else
3356 Check_Access_Definition (N);
3357 Error_Node := Access_Definition (N);
3358 end if;
3359
3360 Error_Msg_N ("premature usage of incomplete type", Error_Node);
3361 end;
3362 end if;
3363
3364 if K = E_Generic_In_Parameter then
3365
3366 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
3367
3368 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
3369 Error_Msg_N
3370 ("generic formal of mode IN must not be of limited type", N);
3371 Explain_Limited_Type (T, N);
3372 end if;
3373
3374 if Is_Abstract_Type (T) then
3375 Error_Msg_N
3376 ("generic formal of mode IN must not be of abstract type", N);
3377 end if;
3378
3379 if Present (E) then
3380 Preanalyze_Spec_Expression (E, T);
3381
3382 -- The default for a ghost generic formal IN parameter of
3383 -- access-to-variable type should be a ghost object (SPARK
3384 -- RM 6.9(13)).
3385
3386 if Is_Access_Variable (T) then
3387 Check_Ghost_Formal_Variable
3388 (Actual => E,
3389 Formal => Id,
3390 Is_Default => True);
3391 end if;
3392
3393 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
3394 Error_Msg_N
3395 ("initialization not allowed for limited types", E);
3396 Explain_Limited_Type (T, E);
3397 end if;
3398 end if;
3399
3400 Mutate_Ekind (Id, K);
3401 Set_Etype (Id, T);
3402
3403 -- Case of generic IN OUT parameter
3404
3405 else
3406 -- If the formal has an unconstrained type, construct its actual
3407 -- subtype, as is done for subprogram formals. In this fashion, all
3408 -- its uses can refer to specific bounds.
3409
3410 Mutate_Ekind (Id, K);
3411 Set_Etype (Id, T);
3412
3413 if (Is_Array_Type (T) and then not Is_Constrained (T))
3414 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
3415 then
3416 declare
3417 Non_Freezing_Ref : constant Node_Id :=
3418 New_Occurrence_Of (Id, Sloc (Id));
3419 Decl : Node_Id;
3420
3421 begin
3422 -- Make sure the actual subtype doesn't generate bogus freezing
3423
3424 Set_Must_Not_Freeze (Non_Freezing_Ref);
3425 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
3426 Insert_Before_And_Analyze (N, Decl);
3427 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
3428 end;
3429 else
3430 Set_Actual_Subtype (Id, T);
3431 end if;
3432
3433 if Present (E) then
3434 Error_Msg_N
3435 ("initialization not allowed for `IN OUT` formals", N);
3436 end if;
3437 end if;
3438
3439 Analyze_Aspect_Specifications (N, Id);
3440
3441 if Parent_Installed then
3442 Remove_Parent;
3443 end if;
3444 end Analyze_Formal_Object_Declaration;
3445
3446 ----------------------------------------------
3447 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
3448 ----------------------------------------------
3449
3450 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
3451 (T : Entity_Id;
3452 Def : Node_Id)
3453 is
3454 Loc : constant Source_Ptr := Sloc (Def);
3455 Base : constant Entity_Id :=
3456 New_Internal_Entity
3457 (E_Ordinary_Fixed_Point_Type, Current_Scope,
3458 Sloc (Defining_Identifier (Parent (Def))), 'G');
3459
3460 begin
3461 -- The semantic attributes are set for completeness only, their values
3462 -- will never be used, since all properties of the type are non-static.
3463
3464 Enter_Name (T);
3465 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
3466 Set_Etype (T, Base);
3467 Set_Size_Info (T, Standard_Integer);
3468 Set_RM_Size (T, RM_Size (Standard_Integer));
3469 Set_Small_Value (T, Ureal_1);
3470 Set_Delta_Value (T, Ureal_1);
3471 Set_Scalar_Range (T,
3472 Make_Range (Loc,
3473 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
3474 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
3475 Set_Is_Constrained (T);
3476
3477 Set_Is_Generic_Type (Base);
3478 Set_Etype (Base, Base);
3479 Set_Size_Info (Base, Standard_Integer);
3480 Set_RM_Size (Base, RM_Size (Standard_Integer));
3481 Set_Small_Value (Base, Ureal_1);
3482 Set_Delta_Value (Base, Ureal_1);
3483 Set_Scalar_Range (Base, Scalar_Range (T));
3484 Set_Parent (Base, Parent (Def));
3485
3486 Check_Restriction (No_Fixed_Point, Def);
3487 end Analyze_Formal_Ordinary_Fixed_Point_Type;
3488
3489 ----------------------------------------
3490 -- Analyze_Formal_Package_Declaration --
3491 ----------------------------------------
3492
3493 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
3494 Gen_Id : constant Node_Id := Name (N);
3495 Loc : constant Source_Ptr := Sloc (N);
3496 Pack_Id : constant Entity_Id := Defining_Identifier (N);
3497 Formal : Entity_Id;
3498 Gen_Decl : Node_Id;
3499 Gen_Unit : Entity_Id;
3500 Renaming : Node_Id;
3501
3502 Vis_Prims_List : Elist_Id := No_Elist;
3503 -- List of primitives made temporarily visible in the instantiation
3504 -- to match the visibility of the formal type.
3505
3506 function Build_Local_Package return Node_Id;
3507 -- The formal package is rewritten so that its parameters are replaced
3508 -- with corresponding declarations. For parameters with bona fide
3509 -- associations these declarations are created by Analyze_Associations
3510 -- as for a regular instantiation. For boxed parameters, we preserve
3511 -- the formal declarations and analyze them, in order to introduce
3512 -- entities of the right kind in the environment of the formal.
3513
3514 -------------------------
3515 -- Build_Local_Package --
3516 -------------------------
3517
3518 function Build_Local_Package return Node_Id is
3519 Decls : List_Id;
3520 Pack_Decl : Node_Id;
3521
3522 begin
3523 -- Within the formal, the name of the generic package is a renaming
3524 -- of the formal (as for a regular instantiation).
3525
3526 Pack_Decl :=
3527 Make_Package_Declaration (Loc,
3528 Specification =>
3529 Copy_Generic_Node
3530 (Specification (Original_Node (Gen_Decl)),
3531 Empty, Instantiating => True));
3532
3533 Renaming :=
3534 Make_Package_Renaming_Declaration (Loc,
3535 Defining_Unit_Name =>
3536 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3537 Name => New_Occurrence_Of (Formal, Loc));
3538
3539 if Nkind (Gen_Id) = N_Identifier
3540 and then Chars (Gen_Id) = Chars (Pack_Id)
3541 then
3542 Error_Msg_NE
3543 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3544 end if;
3545
3546 -- If the formal is declared with a box, or with an others choice,
3547 -- create corresponding declarations for all entities in the formal
3548 -- part, so that names with the proper types are available in the
3549 -- specification of the formal package.
3550
3551 -- On the other hand, if there are no associations (as in "new G;"),
3552 -- then all the formals must have defaults, and this will be checked
3553 -- by the call to Analyze_Associations.
3554
3555 if Box_Present (N)
3556 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3557 then
3558 declare
3559 Formal_Decl : Node_Id;
3560
3561 begin
3562 -- TBA : for a formal package, need to recurse ???
3563
3564 Decls := New_List;
3565 Formal_Decl :=
3566 First
3567 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
3568 while Present (Formal_Decl) loop
3569 Append_To
3570 (Decls,
3571 Copy_Generic_Node
3572 (Formal_Decl, Empty, Instantiating => True));
3573 Next (Formal_Decl);
3574 end loop;
3575 end;
3576
3577 -- If generic associations are present, use Analyze_Associations to
3578 -- create the proper renaming declarations.
3579
3580 else
3581 declare
3582 Act_Tree : constant Node_Id :=
3583 Copy_Generic_Node
3584 (Original_Node (Gen_Decl), Empty,
3585 Instantiating => True);
3586
3587 begin
3588 Generic_Renamings.Set_Last (0);
3589 Generic_Renamings_HTable.Reset;
3590 Instantiation_Node := N;
3591
3592 Decls :=
3593 Analyze_Associations
3594 (I_Node => Original_Node (N),
3595 Formals => Generic_Formal_Declarations (Act_Tree),
3596 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3597
3598 Vis_Prims_List := Check_Hidden_Primitives (Decls);
3599 end;
3600 end if;
3601
3602 Append (Renaming, To => Decls);
3603
3604 -- Add generated declarations ahead of local declarations in
3605 -- the package.
3606
3607 if No (Visible_Declarations (Specification (Pack_Decl))) then
3608 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
3609 else
3610 Insert_List_Before
3611 (First (Visible_Declarations (Specification (Pack_Decl))),
3612 Decls);
3613 end if;
3614
3615 return Pack_Decl;
3616 end Build_Local_Package;
3617
3618 -- Local variables
3619
3620 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
3621 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
3622
3623 Associations : Boolean := True;
3624 New_N : Node_Id;
3625 Parent_Installed : Boolean := False;
3626 Parent_Instance : Entity_Id;
3627 Renaming_In_Par : Entity_Id;
3628
3629 -- Start of processing for Analyze_Formal_Package_Declaration
3630
3631 begin
3632 Check_Text_IO_Special_Unit (Gen_Id);
3633
3634 Init_Env;
3635 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3636 Gen_Unit := Entity (Gen_Id);
3637
3638 -- Check for a formal package that is a package renaming
3639
3640 if Present (Renamed_Entity (Gen_Unit)) then
3641
3642 -- Indicate that unit is used, before replacing it with renamed
3643 -- entity for use below.
3644
3645 if In_Extended_Main_Source_Unit (N) then
3646 Set_Is_Instantiated (Gen_Unit);
3647 Generate_Reference (Gen_Unit, N);
3648 end if;
3649
3650 Gen_Unit := Renamed_Entity (Gen_Unit);
3651 end if;
3652
3653 if Ekind (Gen_Unit) /= E_Generic_Package then
3654 Error_Msg_N ("expect generic package name", Gen_Id);
3655 Restore_Env;
3656 goto Leave;
3657
3658 elsif Gen_Unit = Current_Scope then
3659 Error_Msg_N
3660 ("generic package cannot be used as a formal package of itself",
3661 Gen_Id);
3662 Restore_Env;
3663 goto Leave;
3664
3665 elsif In_Open_Scopes (Gen_Unit) then
3666 if Is_Compilation_Unit (Gen_Unit)
3667 and then Is_Child_Unit (Current_Scope)
3668 then
3669 -- Special-case the error when the formal is a parent, and
3670 -- continue analysis to minimize cascaded errors.
3671
3672 Error_Msg_N
3673 ("generic parent cannot be used as formal package of a child "
3674 & "unit", Gen_Id);
3675
3676 else
3677 Error_Msg_N
3678 ("generic package cannot be used as a formal package within "
3679 & "itself", Gen_Id);
3680 Restore_Env;
3681 goto Leave;
3682 end if;
3683 end if;
3684
3685 -- Check that name of formal package does not hide name of generic,
3686 -- or its leading prefix. This check must be done separately because
3687 -- the name of the generic has already been analyzed.
3688
3689 declare
3690 Gen_Name : Entity_Id;
3691
3692 begin
3693 Gen_Name := Gen_Id;
3694 while Nkind (Gen_Name) = N_Expanded_Name loop
3695 Gen_Name := Prefix (Gen_Name);
3696 end loop;
3697
3698 if Chars (Gen_Name) = Chars (Pack_Id) then
3699 Error_Msg_NE
3700 ("& is hidden within declaration of formal package",
3701 Gen_Id, Gen_Name);
3702 end if;
3703 end;
3704
3705 if Box_Present (N)
3706 or else No (Generic_Associations (N))
3707 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3708 then
3709 Associations := False;
3710 end if;
3711
3712 -- If there are no generic associations, the generic parameters appear
3713 -- as local entities and are instantiated like them. We copy the generic
3714 -- package declaration as if it were an instantiation, and analyze it
3715 -- like a regular package, except that we treat the formals as
3716 -- additional visible components.
3717
3718 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3719
3720 if In_Extended_Main_Source_Unit (N) then
3721 Set_Is_Instantiated (Gen_Unit);
3722 Generate_Reference (Gen_Unit, N);
3723 end if;
3724
3725 Formal := New_Copy (Pack_Id);
3726 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3727
3728 -- Make local generic without formals. The formals will be replaced with
3729 -- internal declarations.
3730
3731 begin
3732 New_N := Build_Local_Package;
3733
3734 -- If there are errors in the parameter list, Analyze_Associations
3735 -- raises Instantiation_Error. Patch the declaration to prevent further
3736 -- exception propagation.
3737
3738 exception
3739 when Instantiation_Error =>
3740 Enter_Name (Formal);
3741 Mutate_Ekind (Formal, E_Variable);
3742 Set_Etype (Formal, Any_Type);
3743 Restore_Hidden_Primitives (Vis_Prims_List);
3744
3745 if Parent_Installed then
3746 Remove_Parent;
3747 end if;
3748
3749 goto Leave;
3750 end;
3751
3752 Rewrite (N, New_N);
3753 Set_Defining_Unit_Name (Specification (New_N), Formal);
3754 Set_Generic_Parent (Specification (N), Gen_Unit);
3755 Set_Instance_Env (Gen_Unit, Formal);
3756 Set_Is_Generic_Instance (Formal);
3757
3758 Enter_Name (Formal);
3759 Mutate_Ekind (Formal, E_Package);
3760 Set_Etype (Formal, Standard_Void_Type);
3761 Set_Inner_Instances (Formal, New_Elmt_List);
3762
3763 -- It is unclear that any aspects can apply to a formal package
3764 -- declaration, given that they look like a hidden conformance
3765 -- requirement on the corresponding actual. However, Abstract_State
3766 -- must be treated specially because it generates declarations that
3767 -- must appear before other declarations in the specification and
3768 -- must be analyzed at once.
3769
3770 if Present (Aspect_Specifications (Gen_Decl)) then
3771 if No (Aspect_Specifications (N)) then
3772 Set_Aspect_Specifications (N, New_List);
3773 end if;
3774
3775 declare
3776 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3777 New_A : Node_Id;
3778
3779 begin
3780 while Present (ASN) loop
3781 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3782 New_A :=
3783 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3784 Set_Entity (New_A, Formal);
3785 Set_Analyzed (New_A, False);
3786 Append (New_A, Aspect_Specifications (N));
3787 Analyze_Aspect_Specifications (N, Formal);
3788 exit;
3789 end if;
3790
3791 Next (ASN);
3792 end loop;
3793 end;
3794 end if;
3795
3796 Push_Scope (Formal);
3797
3798 -- Manually set the SPARK_Mode from the context because the package
3799 -- declaration is never analyzed.
3800
3801 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3802 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3803 Set_SPARK_Pragma_Inherited (Formal);
3804 Set_SPARK_Aux_Pragma_Inherited (Formal);
3805
3806 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3807
3808 -- Similarly, we have to make the name of the formal visible in the
3809 -- parent instance, to resolve properly fully qualified names that
3810 -- may appear in the generic unit. The parent instance has been
3811 -- placed on the scope stack ahead of the current scope.
3812
3813 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3814
3815 Renaming_In_Par :=
3816 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3817 Mutate_Ekind (Renaming_In_Par, E_Package);
3818 Set_Is_Not_Self_Hidden (Renaming_In_Par);
3819 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3820 Set_Scope (Renaming_In_Par, Parent_Instance);
3821 Set_Parent (Renaming_In_Par, Parent (Formal));
3822 Set_Renamed_Entity (Renaming_In_Par, Formal);
3823 Append_Entity (Renaming_In_Par, Parent_Instance);
3824 end if;
3825
3826 -- A formal package declaration behaves as a package instantiation with
3827 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3828 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3829 -- all SPARK_Mode pragmas within the generic_package_name.
3830
3831 if SPARK_Mode /= On then
3832 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3833
3834 -- Mark the formal spec in case the body is instantiated at a later
3835 -- pass. This preserves the original context in effect for the body.
3836
3837 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3838 end if;
3839
3840 Analyze (Specification (N));
3841
3842 -- The formals for which associations are provided are not visible
3843 -- outside of the formal package. The others are still declared by a
3844 -- formal parameter declaration.
3845
3846 -- If there are no associations, the only local entity to hide is the
3847 -- generated package renaming itself.
3848
3849 declare
3850 E : Entity_Id;
3851
3852 begin
3853 E := First_Entity (Formal);
3854 while Present (E) loop
3855 if Associations and then not Is_Generic_Formal (E) then
3856 Set_Is_Hidden (E);
3857 end if;
3858
3859 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3860 Set_Is_Hidden (E);
3861 exit;
3862 end if;
3863
3864 Next_Entity (E);
3865 end loop;
3866 end;
3867
3868 End_Package_Scope (Formal);
3869 Restore_Hidden_Primitives (Vis_Prims_List);
3870
3871 if Parent_Installed then
3872 Remove_Parent;
3873 end if;
3874
3875 Restore_Env;
3876
3877 -- Inside the generic unit, the formal package is a regular package, but
3878 -- no body is needed for it. Note that after instantiation, the defining
3879 -- unit name we need is in the new tree and not in the original (see
3880 -- Package_Instantiation). A generic formal package is an instance, and
3881 -- can be used as an actual for an inner instance.
3882
3883 Set_Has_Completion (Formal, True);
3884
3885 -- Add semantic information to the original defining identifier.
3886
3887 Mutate_Ekind (Pack_Id, E_Package);
3888 Set_Etype (Pack_Id, Standard_Void_Type);
3889 Set_Scope (Pack_Id, Scope (Formal));
3890 Set_Has_Completion (Pack_Id, True);
3891
3892 <<Leave>>
3893 -- Unclear that any other aspects may appear here, analyze them
3894 -- for completion, given that the grammar allows their appearance.
3895
3896 Analyze_Aspect_Specifications (N, Pack_Id);
3897
3898 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3899 end Analyze_Formal_Package_Declaration;
3900
3901 ---------------------------------
3902 -- Analyze_Formal_Private_Type --
3903 ---------------------------------
3904
3905 procedure Analyze_Formal_Private_Type
3906 (N : Node_Id;
3907 T : Entity_Id;
3908 Def : Node_Id)
3909 is
3910 begin
3911 New_Private_Type (N, T, Def);
3912
3913 -- Set the size to an arbitrary but legal value
3914
3915 Set_Size_Info (T, Standard_Integer);
3916 Set_RM_Size (T, RM_Size (Standard_Integer));
3917 end Analyze_Formal_Private_Type;
3918
3919 ------------------------------------
3920 -- Analyze_Formal_Incomplete_Type --
3921 ------------------------------------
3922
3923 procedure Analyze_Formal_Incomplete_Type
3924 (T : Entity_Id;
3925 Def : Node_Id)
3926 is
3927 begin
3928 Enter_Name (T);
3929 Mutate_Ekind (T, E_Incomplete_Type);
3930 Set_Etype (T, T);
3931 Set_Private_Dependents (T, New_Elmt_List);
3932
3933 if Tagged_Present (Def) then
3934 Set_Is_Tagged_Type (T);
3935 Make_Class_Wide_Type (T);
3936 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3937 end if;
3938 end Analyze_Formal_Incomplete_Type;
3939
3940 ----------------------------------------
3941 -- Analyze_Formal_Signed_Integer_Type --
3942 ----------------------------------------
3943
3944 procedure Analyze_Formal_Signed_Integer_Type
3945 (T : Entity_Id;
3946 Def : Node_Id)
3947 is
3948 Base : constant Entity_Id :=
3949 New_Internal_Entity
3950 (E_Signed_Integer_Type,
3951 Current_Scope,
3952 Sloc (Defining_Identifier (Parent (Def))), 'G');
3953
3954 begin
3955 Enter_Name (T);
3956
3957 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3958 Set_Etype (T, Base);
3959 Set_Size_Info (T, Standard_Integer);
3960 Set_RM_Size (T, RM_Size (Standard_Integer));
3961 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3962 Set_Is_Constrained (T);
3963
3964 Set_Is_Generic_Type (Base);
3965 Set_Size_Info (Base, Standard_Integer);
3966 Set_RM_Size (Base, RM_Size (Standard_Integer));
3967 Set_Etype (Base, Base);
3968 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3969 Set_Parent (Base, Parent (Def));
3970 end Analyze_Formal_Signed_Integer_Type;
3971
3972 -------------------------------------------
3973 -- Analyze_Formal_Subprogram_Declaration --
3974 -------------------------------------------
3975
3976 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3977 Spec : constant Node_Id := Specification (N);
3978 Def : constant Node_Id := Default_Name (N);
3979 Expr : constant Node_Id := Expression (N);
3980 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3981
3982 Parent_Installed : Boolean := False;
3983 Subp : Entity_Id;
3984
3985 begin
3986 if Nam = Error then
3987 return;
3988 end if;
3989
3990 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3991 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3992 goto Leave;
3993 end if;
3994
3995 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3996
3997 Analyze_Subprogram_Declaration (N);
3998 Set_Is_Formal_Subprogram (Nam);
3999 Set_Has_Completion (Nam);
4000
4001 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
4002 Set_Is_Abstract_Subprogram (Nam);
4003
4004 Set_Is_Dispatching_Operation (Nam);
4005
4006 -- A formal abstract procedure cannot have a null default
4007 -- (RM 12.6(4.1/2)).
4008
4009 if Has_Null_Default (N) then
4010 Error_Msg_N
4011 ("a formal abstract subprogram cannot default to null", Spec);
4012 end if;
4013
4014 -- A formal abstract function cannot have an expression default
4015 -- (expression defaults are allowed for nonabstract formal functions
4016 -- when extensions are enabled).
4017
4018 if Nkind (Spec) = N_Function_Specification
4019 and then Present (Expr)
4020 then
4021 Error_Msg_N
4022 ("a formal abstract subprogram cannot default to an expression",
4023 Spec);
4024 end if;
4025
4026 declare
4027 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
4028 begin
4029 if No (Ctrl_Type) then
4030 Error_Msg_N
4031 ("abstract formal subprogram must have a controlling type",
4032 N);
4033
4034 elsif Ada_Version >= Ada_2012
4035 and then Is_Incomplete_Type (Ctrl_Type)
4036 then
4037 Error_Msg_NE
4038 ("controlling type of abstract formal subprogram cannot "
4039 & "be incomplete type", N, Ctrl_Type);
4040
4041 else
4042 Check_Controlling_Formals (Ctrl_Type, Nam);
4043 end if;
4044 end;
4045 end if;
4046
4047 -- Default name is resolved at the point of instantiation
4048
4049 if Box_Present (N) then
4050 null;
4051
4052 -- Default name is bound at the point of generic declaration
4053
4054 elsif Present (Def) then
4055 if Nkind (Def) = N_Operator_Symbol then
4056 Find_Direct_Name (Def);
4057
4058 elsif Nkind (Def) /= N_Attribute_Reference then
4059 Analyze (Def);
4060
4061 else
4062 -- For an attribute reference, analyze the prefix and verify
4063 -- that it has the proper profile for the subprogram.
4064
4065 Analyze (Prefix (Def));
4066 Valid_Default_Attribute (Nam, Def);
4067 goto Leave;
4068 end if;
4069
4070 -- The default for a ghost generic formal procedure should be a ghost
4071 -- procedure (SPARK RM 6.9(13)).
4072
4073 if Ekind (Nam) = E_Procedure then
4074 declare
4075 Def_E : Entity_Id := Empty;
4076 begin
4077 if Nkind (Def) in N_Has_Entity then
4078 Def_E := Entity (Def);
4079 end if;
4080
4081 Check_Ghost_Formal_Procedure_Or_Package
4082 (N => Def,
4083 Actual => Def_E,
4084 Formal => Nam,
4085 Is_Default => True);
4086 end;
4087 end if;
4088
4089 -- Default name may be overloaded, in which case the interpretation
4090 -- with the correct profile must be selected, as for a renaming.
4091 -- If the definition is an indexed component, it must denote a
4092 -- member of an entry family. If it is a selected component, it
4093 -- can be a protected operation.
4094
4095 if Etype (Def) = Any_Type then
4096 goto Leave;
4097
4098 elsif Nkind (Def) = N_Selected_Component then
4099 if not Is_Overloadable (Entity (Selector_Name (Def))) then
4100 Error_Msg_N ("expect valid subprogram name as default", Def);
4101 end if;
4102
4103 elsif Nkind (Def) = N_Indexed_Component then
4104 if Is_Entity_Name (Prefix (Def)) then
4105 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
4106 Error_Msg_N ("expect valid subprogram name as default", Def);
4107 end if;
4108
4109 elsif Nkind (Prefix (Def)) = N_Selected_Component then
4110 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
4111 E_Entry_Family
4112 then
4113 Error_Msg_N ("expect valid subprogram name as default", Def);
4114 end if;
4115
4116 else
4117 Error_Msg_N ("expect valid subprogram name as default", Def);
4118 goto Leave;
4119 end if;
4120
4121 elsif Nkind (Def) = N_Character_Literal then
4122
4123 -- Needs some type checks: subprogram should be parameterless???
4124
4125 Resolve (Def, (Etype (Nam)));
4126
4127 elsif not Is_Entity_Name (Def)
4128 or else not Is_Overloadable (Entity (Def))
4129 then
4130 Error_Msg_N ("expect valid subprogram name as default", Def);
4131 goto Leave;
4132
4133 elsif not Is_Overloaded (Def) then
4134 Subp := Entity (Def);
4135
4136 if Subp = Nam then
4137 Error_Msg_N ("premature usage of formal subprogram", Def);
4138
4139 elsif not Entity_Matches_Spec (Subp, Nam) then
4140 Error_Msg_N ("no visible entity matches specification", Def);
4141 end if;
4142
4143 -- More than one interpretation, so disambiguate as for a renaming
4144
4145 else
4146 declare
4147 I : Interp_Index;
4148 I1 : Interp_Index := 0;
4149 It : Interp;
4150 It1 : Interp;
4151
4152 begin
4153 Subp := Any_Id;
4154 Get_First_Interp (Def, I, It);
4155 while Present (It.Nam) loop
4156 if Entity_Matches_Spec (It.Nam, Nam) then
4157 if Subp /= Any_Id then
4158 It1 := Disambiguate (Def, I1, I, Etype (Subp));
4159
4160 if It1 = No_Interp then
4161 Error_Msg_N ("ambiguous default subprogram", Def);
4162 else
4163 Subp := It1.Nam;
4164 end if;
4165
4166 exit;
4167
4168 else
4169 I1 := I;
4170 Subp := It.Nam;
4171 end if;
4172 end if;
4173
4174 Get_Next_Interp (I, It);
4175 end loop;
4176 end;
4177
4178 if Subp /= Any_Id then
4179
4180 -- Subprogram found, generate reference to it
4181
4182 Set_Entity (Def, Subp);
4183 Generate_Reference (Subp, Def);
4184
4185 if Subp = Nam then
4186 Error_Msg_N ("premature usage of formal subprogram", Def);
4187
4188 elsif Ekind (Subp) /= E_Operator then
4189 Check_Mode_Conformant (Subp, Nam);
4190 end if;
4191
4192 else
4193 Error_Msg_N ("no visible subprogram matches specification", N);
4194 end if;
4195 end if;
4196
4197 -- When extensions are enabled, an expression can be given as default
4198 -- for a formal function. The expression must be of the function result
4199 -- type and can reference formal parameters of the function.
4200
4201 elsif Present (Expr) then
4202 Push_Scope (Nam);
4203 Install_Formals (Nam);
4204 Preanalyze_Spec_Expression (Expr, Etype (Nam));
4205 End_Scope;
4206 end if;
4207
4208 <<Leave>>
4209 Analyze_Aspect_Specifications (N, Nam);
4210
4211 if Parent_Installed then
4212 Remove_Parent;
4213 end if;
4214 end Analyze_Formal_Subprogram_Declaration;
4215
4216 -------------------------------------
4217 -- Analyze_Formal_Type_Declaration --
4218 -------------------------------------
4219
4220 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
4221 Def : constant Node_Id := Formal_Type_Definition (N);
4222
4223 Parent_Installed : Boolean := False;
4224 T : Entity_Id;
4225
4226 begin
4227 T := Defining_Identifier (N);
4228
4229 if Present (Discriminant_Specifications (N))
4230 and then Nkind (Def) /= N_Formal_Private_Type_Definition
4231 then
4232 Error_Msg_N
4233 ("discriminants not allowed for this formal type", T);
4234 end if;
4235
4236 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
4237
4238 -- Enter the new name, and branch to specific routine
4239
4240 case Nkind (Def) is
4241 when N_Formal_Private_Type_Definition =>
4242 Analyze_Formal_Private_Type (N, T, Def);
4243
4244 when N_Formal_Derived_Type_Definition =>
4245 Analyze_Formal_Derived_Type (N, T, Def);
4246
4247 when N_Formal_Incomplete_Type_Definition =>
4248 Analyze_Formal_Incomplete_Type (T, Def);
4249
4250 when N_Formal_Discrete_Type_Definition =>
4251 Analyze_Formal_Discrete_Type (T, Def);
4252
4253 when N_Formal_Signed_Integer_Type_Definition =>
4254 Analyze_Formal_Signed_Integer_Type (T, Def);
4255
4256 when N_Formal_Modular_Type_Definition =>
4257 Analyze_Formal_Modular_Type (T, Def);
4258
4259 when N_Formal_Floating_Point_Definition =>
4260 Analyze_Formal_Floating_Type (T, Def);
4261
4262 when N_Formal_Ordinary_Fixed_Point_Definition =>
4263 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
4264
4265 when N_Formal_Decimal_Fixed_Point_Definition =>
4266 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
4267
4268 when N_Array_Type_Definition =>
4269 Analyze_Formal_Array_Type (T, Def);
4270
4271 when N_Access_Function_Definition
4272 | N_Access_Procedure_Definition
4273 | N_Access_To_Object_Definition
4274 =>
4275 Analyze_Generic_Access_Type (T, Def);
4276
4277 -- Ada 2005: a interface declaration is encoded as an abstract
4278 -- record declaration or a abstract type derivation.
4279
4280 when N_Record_Definition =>
4281 Analyze_Formal_Interface_Type (N, T, Def);
4282
4283 when N_Derived_Type_Definition =>
4284 Analyze_Formal_Derived_Interface_Type (N, T, Def);
4285
4286 when N_Error =>
4287 null;
4288
4289 when others =>
4290 raise Program_Error;
4291 end case;
4292
4293 -- A formal type declaration declares a type and its first
4294 -- subtype.
4295
4296 Set_Is_Generic_Type (T);
4297 Set_Is_First_Subtype (T);
4298
4299 if Present (Default_Subtype_Mark (Original_Node (N))) then
4300 Validate_Formal_Type_Default (N);
4301 end if;
4302
4303 Analyze_Aspect_Specifications (N, T);
4304
4305 if Parent_Installed then
4306 Remove_Parent;
4307 end if;
4308 end Analyze_Formal_Type_Declaration;
4309
4310 ------------------------------------
4311 -- Analyze_Function_Instantiation --
4312 ------------------------------------
4313
4314 procedure Analyze_Function_Instantiation (N : Node_Id) is
4315 begin
4316 Analyze_Subprogram_Instantiation (N, E_Function);
4317 end Analyze_Function_Instantiation;
4318
4319 ---------------------------------
4320 -- Analyze_Generic_Access_Type --
4321 ---------------------------------
4322
4323 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
4324 begin
4325 Enter_Name (T);
4326
4327 if Nkind (Def) = N_Access_To_Object_Definition then
4328 Access_Type_Declaration (T, Def);
4329
4330 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
4331 and then No (Full_View (Designated_Type (T)))
4332 and then not Is_Generic_Type (Designated_Type (T))
4333 then
4334 Error_Msg_N ("premature usage of incomplete type", Def);
4335
4336 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
4337 Error_Msg_N
4338 ("only a subtype mark is allowed in a formal", Def);
4339 end if;
4340
4341 else
4342 Access_Subprogram_Declaration (T, Def);
4343 end if;
4344 end Analyze_Generic_Access_Type;
4345
4346 ---------------------------------
4347 -- Analyze_Generic_Formal_Part --
4348 ---------------------------------
4349
4350 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
4351 Gen_Parm_Decl : Node_Id;
4352
4353 begin
4354 -- The generic formals are processed in the scope of the generic unit,
4355 -- where they are immediately visible. The scope is installed by the
4356 -- caller.
4357
4358 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
4359 while Present (Gen_Parm_Decl) loop
4360 Analyze (Gen_Parm_Decl);
4361 Next (Gen_Parm_Decl);
4362 end loop;
4363
4364 Generate_Reference_To_Generic_Formals (Current_Scope);
4365
4366 -- For Ada 2022, some formal parameters can carry aspects, which must
4367 -- be name-resolved at the end of the list of formal parameters (which
4368 -- has the semantics of a declaration list).
4369
4370 Analyze_Contracts (Generic_Formal_Declarations (N));
4371 end Analyze_Generic_Formal_Part;
4372
4373 ------------------------------------------
4374 -- Analyze_Generic_Package_Declaration --
4375 ------------------------------------------
4376
4377 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
4378 Decls : constant List_Id := Visible_Declarations (Specification (N));
4379 Loc : constant Source_Ptr := Sloc (N);
4380
4381 Decl : Node_Id;
4382 Id : Entity_Id;
4383 New_N : Node_Id;
4384 Renaming : Node_Id;
4385 Save_Parent : Node_Id;
4386
4387 begin
4388 -- A generic may grant access to its private enclosing context depending
4389 -- on the placement of its corresponding body. From elaboration point of
4390 -- view, the flow of execution may enter this private context, and then
4391 -- reach an external unit, thus producing a dependency on that external
4392 -- unit. For such a path to be properly discovered and encoded in the
4393 -- ALI file of the main unit, let the ABE mechanism process the body of
4394 -- the main unit, and encode all relevant invocation constructs and the
4395 -- relations between them.
4396
4397 Mark_Save_Invocation_Graph_Of_Body;
4398
4399 -- We introduce a renaming of the enclosing package, to have a usable
4400 -- entity as the prefix of an expanded name for a local entity of the
4401 -- form Par.P.Q, where P is the generic package. This is because a local
4402 -- entity named P may hide it, so that the usual visibility rules in
4403 -- the instance will not resolve properly.
4404
4405 Renaming :=
4406 Make_Package_Renaming_Declaration (Loc,
4407 Defining_Unit_Name =>
4408 Make_Defining_Identifier (Loc,
4409 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
4410 Name =>
4411 Make_Identifier (Loc, Chars (Defining_Entity (N))));
4412
4413 -- The declaration is inserted before other declarations, but before
4414 -- pragmas that may be library-unit pragmas and must appear before other
4415 -- declarations. The pragma Compile_Time_Error is not in this class, and
4416 -- may contain an expression that includes such a qualified name, so the
4417 -- renaming declaration must appear before it.
4418
4419 -- Are there other pragmas that require this special handling ???
4420
4421 if Present (Decls) then
4422 Decl := First (Decls);
4423 while Present (Decl)
4424 and then Nkind (Decl) = N_Pragma
4425 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
4426 loop
4427 Next (Decl);
4428 end loop;
4429
4430 if Present (Decl) then
4431 Insert_Before (Decl, Renaming);
4432 else
4433 Append (Renaming, Visible_Declarations (Specification (N)));
4434 end if;
4435
4436 else
4437 Set_Visible_Declarations (Specification (N), New_List (Renaming));
4438 end if;
4439
4440 -- Create copy of generic unit, and save for instantiation. If the unit
4441 -- is a child unit, do not copy the specifications for the parent, which
4442 -- are not part of the generic tree.
4443
4444 Save_Parent := Parent_Spec (N);
4445 Set_Parent_Spec (N, Empty);
4446
4447 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
4448 Set_Parent_Spec (New_N, Save_Parent);
4449 Rewrite (N, New_N);
4450
4451 -- Collect all contract-related source pragmas found within the template
4452 -- and attach them to the contract of the package spec. This contract is
4453 -- used in the capture of global references within annotations.
4454
4455 Create_Generic_Contract (N);
4456
4457 Id := Defining_Entity (N);
4458 Generate_Definition (Id);
4459
4460 -- Expansion is not applied to generic units
4461
4462 Start_Generic;
4463
4464 Enter_Name (Id);
4465 Mutate_Ekind (Id, E_Generic_Package);
4466 Set_Is_Not_Self_Hidden (Id);
4467 Set_Etype (Id, Standard_Void_Type);
4468
4469 -- Set SPARK_Mode from context
4470
4471 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4472 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
4473 Set_SPARK_Pragma_Inherited (Id);
4474 Set_SPARK_Aux_Pragma_Inherited (Id);
4475
4476 -- Preserve relevant elaboration-related attributes of the context which
4477 -- are no longer available or very expensive to recompute once analysis,
4478 -- resolution, and expansion are over.
4479
4480 Mark_Elaboration_Attributes
4481 (N_Id => Id,
4482 Checks => True,
4483 Warnings => True);
4484
4485 -- Analyze aspects now, so that generated pragmas appear in the
4486 -- declarations before building and analyzing the generic copy.
4487
4488 Analyze_Aspect_Specifications (N, Id);
4489
4490 Push_Scope (Id);
4491 Enter_Generic_Scope (Id);
4492 Set_Inner_Instances (Id, New_Elmt_List);
4493
4494 Set_Categorization_From_Pragmas (N);
4495 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4496
4497 -- Link the declaration of the generic homonym in the generic copy to
4498 -- the package it renames, so that it is always resolved properly.
4499
4500 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
4501 Set_Entity (Associated_Node (Name (Renaming)), Id);
4502
4503 -- For a library unit, we have reconstructed the entity for the unit,
4504 -- and must reset it in the library tables.
4505
4506 if Nkind (Parent (N)) = N_Compilation_Unit then
4507 Set_Cunit_Entity (Current_Sem_Unit, Id);
4508 end if;
4509
4510 Analyze_Generic_Formal_Part (N);
4511
4512 -- After processing the generic formals, analysis proceeds as for a
4513 -- non-generic package.
4514
4515 Analyze (Specification (N));
4516
4517 Validate_Categorization_Dependency (N, Id);
4518
4519 End_Generic;
4520
4521 End_Package_Scope (Id);
4522 Exit_Generic_Scope (Id);
4523
4524 -- If the generic appears within a package unit, the body of that unit
4525 -- has to be present for instantiation and inlining.
4526
4527 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
4528 Set_Body_Needed_For_Inlining
4529 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4530 end if;
4531
4532 if Nkind (Parent (N)) /= N_Compilation_Unit then
4533 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
4534 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
4535 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
4536
4537 else
4538 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4539 Validate_RT_RAT_Component (N);
4540
4541 -- If this is a spec without a body, check that generic parameters
4542 -- are referenced.
4543
4544 if not Body_Required (Parent (N)) then
4545 Check_References (Id);
4546 end if;
4547 end if;
4548
4549 -- If there is a specified storage pool in the context, create an
4550 -- aspect on the package declaration, so that it is used in any
4551 -- instance that does not override it.
4552
4553 if Present (Default_Pool) then
4554 declare
4555 ASN : Node_Id;
4556
4557 begin
4558 ASN :=
4559 Make_Aspect_Specification (Loc,
4560 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
4561 Expression => New_Copy (Default_Pool));
4562
4563 if No (Aspect_Specifications (Specification (N))) then
4564 Set_Aspect_Specifications (Specification (N), New_List (ASN));
4565 else
4566 Append (ASN, Aspect_Specifications (Specification (N)));
4567 end if;
4568 end;
4569 end if;
4570 end Analyze_Generic_Package_Declaration;
4571
4572 --------------------------------------------
4573 -- Analyze_Generic_Subprogram_Declaration --
4574 --------------------------------------------
4575
4576 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
4577 Formals : List_Id;
4578 Id : Entity_Id;
4579 New_N : Node_Id;
4580 Result_Type : Entity_Id;
4581 Save_Parent : Node_Id;
4582 Spec : Node_Id;
4583 Typ : Entity_Id;
4584
4585 begin
4586 -- A generic may grant access to its private enclosing context depending
4587 -- on the placement of its corresponding body. From elaboration point of
4588 -- view, the flow of execution may enter this private context, and then
4589 -- reach an external unit, thus producing a dependency on that external
4590 -- unit. For such a path to be properly discovered and encoded in the
4591 -- ALI file of the main unit, let the ABE mechanism process the body of
4592 -- the main unit, and encode all relevant invocation constructs and the
4593 -- relations between them.
4594
4595 Mark_Save_Invocation_Graph_Of_Body;
4596
4597 -- Create copy of generic unit, and save for instantiation. If the unit
4598 -- is a child unit, do not copy the specifications for the parent, which
4599 -- are not part of the generic tree.
4600
4601 Save_Parent := Parent_Spec (N);
4602 Set_Parent_Spec (N, Empty);
4603
4604 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
4605 Set_Parent_Spec (New_N, Save_Parent);
4606 Rewrite (N, New_N);
4607
4608 -- Collect all contract-related source pragmas found within the template
4609 -- and attach them to the contract of the subprogram spec. This contract
4610 -- is used in the capture of global references within annotations.
4611
4612 Create_Generic_Contract (N);
4613
4614 Spec := Specification (N);
4615 Id := Defining_Entity (Spec);
4616 Generate_Definition (Id);
4617
4618 if Nkind (Id) = N_Defining_Operator_Symbol then
4619 Error_Msg_N
4620 ("operator symbol not allowed for generic subprogram", Id);
4621 end if;
4622
4623 Start_Generic;
4624
4625 Enter_Name (Id);
4626 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
4627
4628 Push_Scope (Id);
4629 Enter_Generic_Scope (Id);
4630 Set_Inner_Instances (Id, New_Elmt_List);
4631 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4632
4633 Analyze_Generic_Formal_Part (N);
4634
4635 if Nkind (Spec) = N_Function_Specification then
4636 Mutate_Ekind (Id, E_Generic_Function);
4637 else
4638 Mutate_Ekind (Id, E_Generic_Procedure);
4639 end if;
4640
4641 -- Set SPARK_Mode from context
4642
4643 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4644 Set_SPARK_Pragma_Inherited (Id);
4645
4646 -- Preserve relevant elaboration-related attributes of the context which
4647 -- are no longer available or very expensive to recompute once analysis,
4648 -- resolution, and expansion are over.
4649
4650 Mark_Elaboration_Attributes
4651 (N_Id => Id,
4652 Checks => True,
4653 Warnings => True);
4654
4655 Formals := Parameter_Specifications (Spec);
4656
4657 if Present (Formals) then
4658 Process_Formals (Formals, Spec);
4659 end if;
4660
4661 if Nkind (Spec) = N_Function_Specification then
4662 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
4663 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
4664 Set_Etype (Id, Result_Type);
4665
4666 -- Check restriction imposed by AI05-073: a generic function
4667 -- cannot return an abstract type or an access to such.
4668
4669 if Is_Abstract_Type (Designated_Type (Result_Type)) then
4670 Error_Msg_N
4671 ("generic function cannot have an access result "
4672 & "that designates an abstract type", Spec);
4673 end if;
4674
4675 else
4676 Find_Type (Result_Definition (Spec));
4677 Typ := Entity (Result_Definition (Spec));
4678
4679 if Is_Abstract_Type (Typ)
4680 and then Ada_Version >= Ada_2012
4681 then
4682 Error_Msg_N
4683 ("generic function cannot have abstract result type", Spec);
4684 end if;
4685
4686 -- If a null exclusion is imposed on the result type, then create
4687 -- a null-excluding itype (an access subtype) and use it as the
4688 -- function's Etype.
4689
4690 if Is_Access_Type (Typ)
4691 and then Null_Exclusion_Present (Spec)
4692 then
4693 Set_Etype (Id,
4694 Create_Null_Excluding_Itype
4695 (T => Typ,
4696 Related_Nod => Spec,
4697 Scope_Id => Defining_Unit_Name (Spec)));
4698 else
4699 Set_Etype (Id, Typ);
4700 end if;
4701 end if;
4702
4703 else
4704 Set_Etype (Id, Standard_Void_Type);
4705 end if;
4706
4707 Set_Is_Not_Self_Hidden (Id);
4708
4709 -- Analyze the aspects of the generic copy to ensure that all generated
4710 -- pragmas (if any) perform their semantic effects.
4711
4712 Analyze_Aspect_Specifications (N, Id);
4713
4714 -- For a library unit, we have reconstructed the entity for the unit,
4715 -- and must reset it in the library tables. We also make sure that
4716 -- Body_Required is set properly in the original compilation unit node.
4717
4718 if Nkind (Parent (N)) = N_Compilation_Unit then
4719 Set_Cunit_Entity (Current_Sem_Unit, Id);
4720 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4721 end if;
4722
4723 -- If the generic appears within a package unit, the body of that unit
4724 -- has to be present for instantiation and inlining.
4725
4726 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
4727 and then Unit_Requires_Body (Id)
4728 then
4729 Set_Body_Needed_For_Inlining
4730 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4731 end if;
4732
4733 Set_Categorization_From_Pragmas (N);
4734 Validate_Categorization_Dependency (N, Id);
4735
4736 -- Capture all global references that occur within the profile of the
4737 -- generic subprogram. Aspects are not part of this processing because
4738 -- they must be delayed. If processed now, Save_Global_References will
4739 -- destroy the Associated_Node links and prevent the capture of global
4740 -- references when the contract of the generic subprogram is analyzed.
4741
4742 Save_Global_References (Original_Node (N));
4743
4744 End_Generic;
4745 End_Scope;
4746 Exit_Generic_Scope (Id);
4747 Generate_Reference_To_Formals (Id);
4748
4749 List_Inherited_Pre_Post_Aspects (Id);
4750 end Analyze_Generic_Subprogram_Declaration;
4751
4752 -----------------------------------
4753 -- Analyze_Package_Instantiation --
4754 -----------------------------------
4755
4756 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4757 -- must be replaced by gotos which jump to the end of the routine in order
4758 -- to restore the Ghost and SPARK modes.
4759
4760 procedure Analyze_Package_Instantiation (N : Node_Id) is
4761 Has_Inline_Always : Boolean := False;
4762 -- Set if the generic unit contains any subprograms with Inline_Always.
4763 -- Only relevant when back-end inlining is not enabled.
4764
4765 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4766 -- Return True if inlining is active and Gen_Unit contains inlined
4767 -- subprograms. In this case, we may either instantiate the body when
4768 -- front-end inlining is enabled, or add a pending instantiation when
4769 -- back-end inlining is enabled. In the former case, this may cause
4770 -- superfluous instantiations, but in either case we need to perform
4771 -- the instantiation of the body in the context of the instance and
4772 -- not in that of the point of inlining.
4773
4774 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4775 -- Return True if Gen_Unit needs to have its body instantiated in the
4776 -- context of N. This in particular excludes generic contexts.
4777
4778 -----------------------
4779 -- Might_Inline_Subp --
4780 -----------------------
4781
4782 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4783 E : Entity_Id;
4784
4785 begin
4786 if Inline_Processing_Required then
4787 -- No need to recompute the answer if we know it is positive
4788 -- and back-end inlining is enabled.
4789
4790 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4791 return True;
4792 end if;
4793
4794 E := First_Entity (Gen_Unit);
4795 while Present (E) loop
4796 if Is_Subprogram (E) and then Is_Inlined (E) then
4797 -- Remember if there are any subprograms with Inline_Always
4798
4799 if Has_Pragma_Inline_Always (E) then
4800 Has_Inline_Always := True;
4801 end if;
4802
4803 Set_Is_Inlined (Gen_Unit);
4804 return True;
4805 end if;
4806
4807 Next_Entity (E);
4808 end loop;
4809 end if;
4810
4811 return False;
4812 end Might_Inline_Subp;
4813
4814 -------------------------------
4815 -- Needs_Body_Instantiated --
4816 -------------------------------
4817
4818 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4819 begin
4820 -- No need to instantiate bodies in generic units
4821
4822 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4823 return False;
4824 end if;
4825
4826 -- If the instantiation is in the main unit, then the body is needed
4827
4828 if Is_In_Main_Unit (N) then
4829 return True;
4830 end if;
4831
4832 -- In GNATprove mode, never instantiate bodies outside of the main
4833 -- unit, as it does not use frontend/backend inlining in the way that
4834 -- GNAT does, so does not benefit from such instantiations. On the
4835 -- contrary, such instantiations may bring artificial constraints,
4836 -- as for example such bodies may require preprocessing.
4837
4838 if GNATprove_Mode then
4839 return False;
4840 end if;
4841
4842 -- If not, then again no need to instantiate bodies in generic units
4843
4844 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4845 return False;
4846 end if;
4847
4848 -- Here we have a special handling for back-end inlining: if inline
4849 -- processing is required, then we unconditionally want to have the
4850 -- body instantiated. The reason is that Might_Inline_Subp does not
4851 -- catch all the cases (as it does not recurse into nested packages)
4852 -- so this avoids the need to patch things up afterwards. Moreover,
4853 -- these instantiations are only performed on demand when back-end
4854 -- inlining is enabled, so this causes very little extra work.
4855
4856 if Inline_Processing_Required and then Back_End_Inlining then
4857 return True;
4858 end if;
4859
4860 -- We want to have the bodies instantiated in non-main units if
4861 -- they might contribute inlined subprograms.
4862
4863 return Might_Inline_Subp (Gen_Unit);
4864 end Needs_Body_Instantiated;
4865
4866 -- Local declarations
4867
4868 Gen_Id : constant Node_Id := Name (N);
4869 Inst_Id : constant Entity_Id := Defining_Entity (N);
4870 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4871 Loc : constant Source_Ptr := Sloc (N);
4872
4873 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4874 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4875 Saved_ISMP : constant Boolean :=
4876 Ignore_SPARK_Mode_Pragmas_In_Instance;
4877 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4878 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4879 -- Save the Ghost and SPARK mode-related data to restore on exit
4880
4881 Saved_Style_Check : constant Boolean := Style_Check;
4882 -- Save style check mode for restore on exit
4883
4884 Act_Decl : Node_Id;
4885 Act_Decl_Name : Node_Id;
4886 Act_Decl_Id : Entity_Id;
4887 Act_Spec : Node_Id;
4888 Act_Tree : Node_Id;
4889 Env_Installed : Boolean := False;
4890 Gen_Decl : Node_Id;
4891 Gen_Spec : Node_Id;
4892 Gen_Unit : Entity_Id;
4893 Inline_Now : Boolean := False;
4894 Needs_Body : Boolean;
4895 Parent_Installed : Boolean := False;
4896 Renamings : List_Id;
4897 Unit_Renaming : Node_Id;
4898
4899 Vis_Prims_List : Elist_Id := No_Elist;
4900 -- List of primitives made temporarily visible in the instantiation
4901 -- to match the visibility of the formal type
4902
4903 -- Start of processing for Analyze_Package_Instantiation
4904
4905 begin
4906 -- Preserve relevant elaboration-related attributes of the context which
4907 -- are no longer available or very expensive to recompute once analysis,
4908 -- resolution, and expansion are over.
4909
4910 Mark_Elaboration_Attributes
4911 (N_Id => N,
4912 Checks => True,
4913 Level => True,
4914 Modes => True,
4915 Warnings => True);
4916
4917 -- Very first thing: check for Text_IO special unit in case we are
4918 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4919
4920 Check_Text_IO_Special_Unit (Name (N));
4921
4922 -- Make node global for error reporting
4923
4924 Instantiation_Node := N;
4925
4926 -- Case of instantiation of a generic package
4927
4928 if Nkind (N) = N_Package_Instantiation then
4929 Act_Decl_Id := New_Copy (Defining_Entity (N));
4930
4931 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4932 Act_Decl_Name :=
4933 Make_Defining_Program_Unit_Name (Loc,
4934 Name =>
4935 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4936 Defining_Identifier => Act_Decl_Id);
4937 else
4938 Act_Decl_Name := Act_Decl_Id;
4939 end if;
4940
4941 -- Case of instantiation of a formal package
4942
4943 else
4944 Act_Decl_Id := Defining_Identifier (N);
4945 Act_Decl_Name := Act_Decl_Id;
4946 end if;
4947
4948 Generate_Definition (Act_Decl_Id);
4949 Mutate_Ekind (Act_Decl_Id, E_Package);
4950 Set_Is_Not_Self_Hidden (Act_Decl_Id);
4951
4952 -- Initialize list of incomplete actuals before analysis
4953
4954 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4955
4956 Preanalyze_Actuals (N, Act_Decl_Id);
4957
4958 -- Turn off style checking in instances. If the check is enabled on the
4959 -- generic unit, a warning in an instance would just be noise. If not
4960 -- enabled on the generic, then a warning in an instance is just wrong.
4961 -- This must be done after analyzing the actuals, which do come from
4962 -- source and are subject to style checking.
4963
4964 Style_Check := False;
4965
4966 Init_Env;
4967 Env_Installed := True;
4968
4969 -- Reset renaming map for formal types. The mapping is established
4970 -- when analyzing the generic associations, but some mappings are
4971 -- inherited from formal packages of parent units, and these are
4972 -- constructed when the parents are installed.
4973
4974 Generic_Renamings.Set_Last (0);
4975 Generic_Renamings_HTable.Reset;
4976
4977 -- Except for an abbreviated instance created to check a formal package,
4978 -- install the parent if this is a generic child unit.
4979
4980 if not Is_Abbreviated_Instance (Inst_Id) then
4981 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4982 end if;
4983
4984 Gen_Unit := Entity (Gen_Id);
4985
4986 -- A package instantiation is Ghost when it is subject to pragma Ghost
4987 -- or the generic template is Ghost. Set the mode now to ensure that
4988 -- any nodes generated during analysis and expansion are marked as
4989 -- Ghost.
4990
4991 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4992
4993 -- Verify that it is the name of a generic package
4994
4995 -- A visibility glitch: if the instance is a child unit and the generic
4996 -- is the generic unit of a parent instance (i.e. both the parent and
4997 -- the child units are instances of the same package) the name now
4998 -- denotes the renaming within the parent, not the intended generic
4999 -- unit. See if there is a homonym that is the desired generic. The
5000 -- renaming declaration must be visible inside the instance of the
5001 -- child, but not when analyzing the name in the instantiation itself.
5002
5003 if Ekind (Gen_Unit) = E_Package
5004 and then Present (Renamed_Entity (Gen_Unit))
5005 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
5006 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
5007 and then Present (Homonym (Gen_Unit))
5008 then
5009 Gen_Unit := Homonym (Gen_Unit);
5010 end if;
5011
5012 if Etype (Gen_Unit) = Any_Type then
5013 Restore_Env;
5014 goto Leave;
5015
5016 elsif Ekind (Gen_Unit) /= E_Generic_Package then
5017
5018 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
5019
5020 if From_Limited_With (Gen_Unit) then
5021 Error_Msg_N
5022 ("cannot instantiate a limited withed package", Gen_Id);
5023 else
5024 Error_Msg_NE
5025 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
5026 end if;
5027
5028 Restore_Env;
5029 goto Leave;
5030 end if;
5031
5032 if In_Extended_Main_Source_Unit (N) then
5033 Set_Is_Instantiated (Gen_Unit);
5034 Generate_Reference (Gen_Unit, N);
5035
5036 if Present (Renamed_Entity (Gen_Unit)) then
5037 Set_Is_Instantiated (Renamed_Entity (Gen_Unit));
5038 Generate_Reference (Renamed_Entity (Gen_Unit), N);
5039 end if;
5040 end if;
5041
5042 if Nkind (Gen_Id) = N_Identifier
5043 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5044 then
5045 Error_Msg_NE
5046 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5047
5048 elsif Nkind (Gen_Id) = N_Expanded_Name
5049 and then Is_Child_Unit (Gen_Unit)
5050 and then Nkind (Prefix (Gen_Id)) = N_Identifier
5051 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
5052 then
5053 Error_Msg_N
5054 ("& is hidden within declaration of instance", Prefix (Gen_Id));
5055 end if;
5056
5057 Set_Entity (Gen_Id, Gen_Unit);
5058
5059 -- If generic is a renaming, get original generic unit
5060
5061 if Present (Renamed_Entity (Gen_Unit))
5062 and then Ekind (Renamed_Entity (Gen_Unit)) = E_Generic_Package
5063 then
5064 Gen_Unit := Renamed_Entity (Gen_Unit);
5065 end if;
5066
5067 -- Verify that there are no circular instantiations
5068
5069 if In_Open_Scopes (Gen_Unit) then
5070 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5071 Restore_Env;
5072 goto Leave;
5073
5074 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5075 Error_Msg_Node_2 := Current_Scope;
5076 Error_Msg_NE
5077 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5078 Circularity_Detected := True;
5079 Restore_Env;
5080 goto Leave;
5081
5082 else
5083 Mutate_Ekind (Inst_Id, E_Package);
5084 Set_Scope (Inst_Id, Current_Scope);
5085
5086 -- If the context of the instance is subject to SPARK_Mode "off" or
5087 -- the annotation is altogether missing, set the global flag which
5088 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5089 -- the instance.
5090
5091 if SPARK_Mode /= On then
5092 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5093
5094 -- Mark the instance spec in case the body is instantiated at a
5095 -- later pass. This preserves the original context in effect for
5096 -- the body.
5097
5098 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5099 end if;
5100
5101 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5102 Gen_Spec := Specification (Gen_Decl);
5103
5104 -- Initialize renamings map, for error checking, and the list that
5105 -- holds private entities whose views have changed between generic
5106 -- definition and instantiation. If this is the instance created to
5107 -- validate an actual package, the instantiation environment is that
5108 -- of the enclosing instance.
5109
5110 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5111
5112 -- Copy original generic tree, to produce text for instantiation
5113
5114 Act_Tree :=
5115 Copy_Generic_Node
5116 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5117
5118 Act_Spec := Specification (Act_Tree);
5119
5120 -- If this is the instance created to validate an actual package,
5121 -- only the formals matter, do not examine the package spec itself.
5122
5123 if Is_Actual_Pack then
5124 Set_Visible_Declarations (Act_Spec, New_List);
5125 Set_Private_Declarations (Act_Spec, New_List);
5126 end if;
5127
5128 Renamings :=
5129 Analyze_Associations
5130 (I_Node => N,
5131 Formals => Generic_Formal_Declarations (Act_Tree),
5132 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5133
5134 Vis_Prims_List := Check_Hidden_Primitives (Renamings);
5135
5136 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
5137 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
5138 Set_Is_Generic_Instance (Act_Decl_Id);
5139 Set_Generic_Parent (Act_Spec, Gen_Unit);
5140
5141 -- References to the generic in its own declaration or its body are
5142 -- references to the instance. Add a renaming declaration for the
5143 -- generic unit itself. This declaration, as well as the renaming
5144 -- declarations for the generic formals, must remain private to the
5145 -- unit: the formals, because this is the language semantics, and
5146 -- the unit because its use is an artifact of the implementation.
5147
5148 Unit_Renaming :=
5149 Make_Package_Renaming_Declaration (Loc,
5150 Defining_Unit_Name =>
5151 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
5152 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
5153
5154 Append (Unit_Renaming, Renamings);
5155
5156 -- The renaming declarations are the first local declarations of the
5157 -- new unit.
5158
5159 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
5160 Insert_List_Before
5161 (First (Visible_Declarations (Act_Spec)), Renamings);
5162 else
5163 Set_Visible_Declarations (Act_Spec, Renamings);
5164 end if;
5165
5166 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
5167
5168 -- Propagate the aspect specifications from the package declaration
5169 -- template to the instantiated version of the package declaration.
5170
5171 if Has_Aspects (Act_Tree) then
5172 Set_Aspect_Specifications (Act_Decl,
5173 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
5174 end if;
5175
5176 -- The generic may have a generated Default_Storage_Pool aspect,
5177 -- set at the point of generic declaration. If the instance has
5178 -- that aspect, it overrides the one inherited from the generic.
5179
5180 if Has_Aspects (Gen_Spec) then
5181 if No (Aspect_Specifications (N)) then
5182 Set_Aspect_Specifications (N,
5183 (New_Copy_List_Tree
5184 (Aspect_Specifications (Gen_Spec))));
5185
5186 else
5187 declare
5188 Inherited_Aspects : constant List_Id :=
5189 New_Copy_List_Tree
5190 (Aspect_Specifications (Gen_Spec));
5191
5192 ASN1 : Node_Id;
5193 ASN2 : Node_Id;
5194 Pool_Present : Boolean := False;
5195
5196 begin
5197 ASN1 := First (Aspect_Specifications (N));
5198 while Present (ASN1) loop
5199 if Chars (Identifier (ASN1)) =
5200 Name_Default_Storage_Pool
5201 then
5202 Pool_Present := True;
5203 exit;
5204 end if;
5205
5206 Next (ASN1);
5207 end loop;
5208
5209 if Pool_Present then
5210
5211 -- If generic carries a default storage pool, remove it
5212 -- in favor of the instance one.
5213
5214 ASN2 := First (Inherited_Aspects);
5215 while Present (ASN2) loop
5216 if Chars (Identifier (ASN2)) =
5217 Name_Default_Storage_Pool
5218 then
5219 Remove (ASN2);
5220 exit;
5221 end if;
5222
5223 Next (ASN2);
5224 end loop;
5225 end if;
5226
5227 Prepend_List_To
5228 (Aspect_Specifications (N), Inherited_Aspects);
5229 end;
5230 end if;
5231 end if;
5232
5233 -- Save the instantiation node for a subsequent instantiation of the
5234 -- body if there is one and it needs to be instantiated here.
5235
5236 -- We instantiate the body only if we are generating code, or if we
5237 -- are generating cross-reference information, or for GNATprove use.
5238
5239 declare
5240 Enclosing_Body_Present : Boolean := False;
5241 -- If the generic unit is not a compilation unit, then a body may
5242 -- be present in its parent even if none is required. We create a
5243 -- tentative pending instantiation for the body, which will be
5244 -- discarded if none is actually present.
5245
5246 Scop : Entity_Id;
5247
5248 begin
5249 if Scope (Gen_Unit) /= Standard_Standard
5250 and then not Is_Child_Unit (Gen_Unit)
5251 then
5252 Scop := Scope (Gen_Unit);
5253 while Present (Scop) and then Scop /= Standard_Standard loop
5254 if Unit_Requires_Body (Scop) then
5255 Enclosing_Body_Present := True;
5256 exit;
5257
5258 elsif In_Open_Scopes (Scop)
5259 and then In_Package_Body (Scop)
5260 then
5261 Enclosing_Body_Present := True;
5262 exit;
5263 end if;
5264
5265 exit when Is_Compilation_Unit (Scop);
5266 Scop := Scope (Scop);
5267 end loop;
5268 end if;
5269
5270 -- If front-end inlining is enabled or there are any subprograms
5271 -- marked with Inline_Always, and this is a unit for which code
5272 -- will be generated, we instantiate the body at once.
5273
5274 -- This is done if the instance is not the main unit, and if the
5275 -- generic is not a child unit of another generic, to avoid scope
5276 -- problems and the reinstallation of parent instances.
5277
5278 if Expander_Active
5279 and then (not Is_Child_Unit (Gen_Unit)
5280 or else not Is_Generic_Unit (Scope (Gen_Unit)))
5281 and then Might_Inline_Subp (Gen_Unit)
5282 and then not Is_Actual_Pack
5283 then
5284 if not Back_End_Inlining
5285 and then (Front_End_Inlining or else Has_Inline_Always)
5286 and then (Is_In_Main_Unit (N)
5287 or else In_Main_Context (Current_Scope))
5288 and then Nkind (Parent (N)) /= N_Compilation_Unit
5289 then
5290 Inline_Now := True;
5291
5292 -- In configurable_run_time mode we force the inlining of
5293 -- predefined subprograms marked Inline_Always, to minimize
5294 -- the use of the run-time library.
5295
5296 elsif In_Predefined_Unit (Gen_Decl)
5297 and then Configurable_Run_Time_Mode
5298 and then Nkind (Parent (N)) /= N_Compilation_Unit
5299 then
5300 Inline_Now := True;
5301 end if;
5302
5303 -- If the current scope is itself an instance within a child
5304 -- unit, there will be duplications in the scope stack, and the
5305 -- unstacking mechanism in Inline_Instance_Body will fail.
5306 -- This loses some rare cases of optimization.
5307
5308 if Is_Generic_Instance (Current_Scope) then
5309 declare
5310 Curr_Unit : constant Entity_Id :=
5311 Cunit_Entity (Current_Sem_Unit);
5312 begin
5313 if Curr_Unit /= Current_Scope
5314 and then Is_Child_Unit (Curr_Unit)
5315 then
5316 Inline_Now := False;
5317 end if;
5318 end;
5319 end if;
5320 end if;
5321
5322 Needs_Body :=
5323 (Unit_Requires_Body (Gen_Unit)
5324 or else Enclosing_Body_Present
5325 or else Present (Corresponding_Body (Gen_Decl)))
5326 and then Needs_Body_Instantiated (Gen_Unit)
5327 and then not Is_Actual_Pack
5328 and then not Inline_Now
5329 and then (Operating_Mode = Generate_Code
5330 or else (Operating_Mode = Check_Semantics
5331 and then GNATprove_Mode));
5332
5333 -- If front-end inlining is enabled or there are any subprograms
5334 -- marked with Inline_Always, do not instantiate body when within
5335 -- a generic context.
5336
5337 if not Back_End_Inlining
5338 and then (Front_End_Inlining or else Has_Inline_Always)
5339 and then not Expander_Active
5340 then
5341 Needs_Body := False;
5342 end if;
5343
5344 -- If the current context is generic, and the package being
5345 -- instantiated is declared within a formal package, there is no
5346 -- body to instantiate until the enclosing generic is instantiated
5347 -- and there is an actual for the formal package. If the formal
5348 -- package has parameters, we build a regular package instance for
5349 -- it, that precedes the original formal package declaration.
5350
5351 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
5352 declare
5353 Decl : constant Node_Id :=
5354 Original_Node
5355 (Unit_Declaration_Node (Scope (Gen_Unit)));
5356 begin
5357 if Nkind (Decl) = N_Formal_Package_Declaration
5358 or else (Nkind (Decl) = N_Package_Declaration
5359 and then Is_List_Member (Decl)
5360 and then Present (Next (Decl))
5361 and then
5362 Nkind (Next (Decl)) =
5363 N_Formal_Package_Declaration)
5364 then
5365 Needs_Body := False;
5366 end if;
5367 end;
5368 end if;
5369 end;
5370
5371 -- For RCI unit calling stubs, we omit the instance body if the
5372 -- instance is the RCI library unit itself.
5373
5374 -- However there is a special case for nested instances: in this case
5375 -- we do generate the instance body, as it might be required, e.g.
5376 -- because it provides stream attributes for some type used in the
5377 -- profile of a remote subprogram. This is consistent with 12.3(12),
5378 -- which indicates that the instance body occurs at the place of the
5379 -- instantiation, and thus is part of the RCI declaration, which is
5380 -- present on all client partitions (this is E.2.3(18)).
5381
5382 -- Note that AI12-0002 may make it illegal at some point to have
5383 -- stream attributes defined in an RCI unit, in which case this
5384 -- special case will become unnecessary. In the meantime, there
5385 -- is known application code in production that depends on this
5386 -- being possible, so we definitely cannot eliminate the body in
5387 -- the case of nested instances for the time being.
5388
5389 -- When we generate a nested instance body, calling stubs for any
5390 -- relevant subprogram will be inserted immediately after the
5391 -- subprogram declarations, and will take precedence over the
5392 -- subsequent (original) body. (The stub and original body will be
5393 -- complete homographs, but this is permitted in an instance).
5394 -- (Could we do better and remove the original body???)
5395
5396 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
5397 and then Comes_From_Source (N)
5398 and then Nkind (Parent (N)) = N_Compilation_Unit
5399 then
5400 Needs_Body := False;
5401 end if;
5402
5403 -- If the context requires a full instantiation, set things up for
5404 -- subsequent construction of the body.
5405
5406 if Needs_Body then
5407 declare
5408 Fin_Scop, S : Entity_Id;
5409
5410 begin
5411 Check_Forward_Instantiation (Gen_Decl);
5412
5413 Fin_Scop := Empty;
5414
5415 -- For a package instantiation that is not a compilation unit,
5416 -- indicate that cleanup actions of the innermost enclosing
5417 -- scope for which they are generated should be delayed until
5418 -- after the package body is instantiated.
5419
5420 if Nkind (N) = N_Package_Instantiation
5421 and then not Is_Compilation_Unit (Act_Decl_Id)
5422 then
5423 S := Current_Scope;
5424
5425 while S /= Standard_Standard loop
5426 -- Cleanup actions are not generated within generic units
5427 -- or in the formal part of generic units.
5428
5429 if not Expander_Active then
5430 exit;
5431
5432 -- For package scopes, cleanup actions are generated only
5433 -- for compilation units, for spec and body separately.
5434
5435 elsif Ekind (S) = E_Package then
5436 if Is_Compilation_Unit (S) then
5437 if In_Package_Body (S) then
5438 Fin_Scop := Body_Entity (S);
5439 else
5440 Fin_Scop := S;
5441 end if;
5442
5443 Set_Delay_Cleanups (Fin_Scop);
5444 exit;
5445
5446 else
5447 S := Scope (S);
5448 end if;
5449
5450 -- Cleanup actions are generated for all dynamic scopes
5451
5452 else
5453 Fin_Scop := S;
5454 Set_Delay_Cleanups (Fin_Scop);
5455 exit;
5456 end if;
5457 end loop;
5458 end if;
5459
5460 Add_Pending_Instantiation (N, Act_Decl, Fin_Scop);
5461 end;
5462 end if;
5463
5464 Set_Categorization_From_Pragmas (Act_Decl);
5465
5466 if Parent_Installed then
5467 Hide_Current_Scope;
5468 end if;
5469
5470 Set_Instance_Spec (N, Act_Decl);
5471
5472 -- If not a compilation unit, insert the package declaration before
5473 -- the original instantiation node.
5474
5475 if Nkind (Parent (N)) /= N_Compilation_Unit then
5476 Mark_Rewrite_Insertion (Act_Decl);
5477 Insert_Before (N, Act_Decl);
5478
5479 if Has_Aspects (N) then
5480 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5481
5482 -- The pragma created for a Default_Storage_Pool aspect must
5483 -- appear ahead of the declarations in the instance spec.
5484 -- Analysis has placed it after the instance node, so remove
5485 -- it and reinsert it properly now.
5486
5487 declare
5488 ASN : constant Node_Id := First (Aspect_Specifications (N));
5489 A_Name : constant Name_Id := Chars (Identifier (ASN));
5490 Decl : Node_Id;
5491
5492 begin
5493 if A_Name = Name_Default_Storage_Pool then
5494 if No (Visible_Declarations (Act_Spec)) then
5495 Set_Visible_Declarations (Act_Spec, New_List);
5496 end if;
5497
5498 Decl := Next (N);
5499 while Present (Decl) loop
5500 if Nkind (Decl) = N_Pragma then
5501 Remove (Decl);
5502 Prepend (Decl, Visible_Declarations (Act_Spec));
5503 exit;
5504 end if;
5505
5506 Next (Decl);
5507 end loop;
5508 end if;
5509 end;
5510 end if;
5511
5512 Analyze (Act_Decl);
5513
5514 -- For an instantiation that is a compilation unit, place
5515 -- declaration on current node so context is complete for analysis
5516 -- (including nested instantiations). If this is the main unit,
5517 -- the declaration eventually replaces the instantiation node.
5518 -- If the instance body is created later, it replaces the
5519 -- instance node, and the declaration is attached to it
5520 -- (see Build_Instance_Compilation_Unit_Nodes).
5521
5522 else
5523 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
5524
5525 -- The entity for the current unit is the newly created one,
5526 -- and all semantic information is attached to it.
5527
5528 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
5529
5530 -- If this is the main unit, replace the main entity as well
5531
5532 if Current_Sem_Unit = Main_Unit then
5533 Main_Unit_Entity := Act_Decl_Id;
5534 end if;
5535 end if;
5536
5537 Set_Unit (Parent (N), Act_Decl);
5538 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5539 Set_Package_Instantiation (Act_Decl_Id, N);
5540
5541 -- Process aspect specifications of the instance node, if any, to
5542 -- take into account categorization pragmas before analyzing the
5543 -- instance.
5544
5545 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5546
5547 Analyze (Act_Decl);
5548 Set_Unit (Parent (N), N);
5549 Set_Body_Required (Parent (N), False);
5550
5551 -- We never need elaboration checks on instantiations, since by
5552 -- definition, the body instantiation is elaborated at the same
5553 -- time as the spec instantiation.
5554
5555 if Legacy_Elaboration_Checks then
5556 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5557 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5558 end if;
5559 end if;
5560
5561 if Legacy_Elaboration_Checks then
5562 Check_Elab_Instantiation (N);
5563 end if;
5564
5565 -- Save the scenario for later examination by the ABE Processing
5566 -- phase.
5567
5568 Record_Elaboration_Scenario (N);
5569
5570 -- The instantiation results in a guaranteed ABE
5571
5572 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
5573 -- Do not instantiate the corresponding body because gigi cannot
5574 -- handle certain types of premature instantiations.
5575
5576 Remove_Dead_Instance (N);
5577
5578 -- Create completing bodies for all subprogram declarations since
5579 -- their real bodies will not be instantiated.
5580
5581 Provide_Completing_Bodies (Instance_Spec (N));
5582 end if;
5583
5584 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5585
5586 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
5587 First_Private_Entity (Act_Decl_Id));
5588
5589 -- If the instantiation needs a body, the unit will be turned into
5590 -- a package body and receive its own elaboration entity. Otherwise,
5591 -- the nature of the unit is now a package declaration.
5592
5593 -- Note that the below rewriting means that Act_Decl, which has been
5594 -- analyzed and expanded, will be re-expanded as the rewritten N.
5595
5596 if Nkind (Parent (N)) = N_Compilation_Unit
5597 and then not Needs_Body
5598 then
5599 Rewrite (N, Act_Decl);
5600 end if;
5601
5602 if Present (Corresponding_Body (Gen_Decl))
5603 or else Unit_Requires_Body (Gen_Unit)
5604 then
5605 Set_Has_Completion (Act_Decl_Id);
5606 end if;
5607
5608 Check_Formal_Packages (Act_Decl_Id);
5609
5610 Restore_Hidden_Primitives (Vis_Prims_List);
5611 Restore_Private_Views (Act_Decl_Id);
5612
5613 Inherit_Context (Gen_Decl, N);
5614
5615 if Parent_Installed then
5616 Remove_Parent;
5617 end if;
5618
5619 Restore_Env;
5620 Env_Installed := False;
5621 end if;
5622
5623 Validate_Categorization_Dependency (N, Act_Decl_Id);
5624
5625 -- There used to be a check here to prevent instantiations in local
5626 -- contexts if the No_Local_Allocators restriction was active. This
5627 -- check was removed by a binding interpretation in AI-95-00130/07,
5628 -- but we retain the code for documentation purposes.
5629
5630 -- if Ekind (Act_Decl_Id) /= E_Void
5631 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
5632 -- then
5633 -- Check_Restriction (No_Local_Allocators, N);
5634 -- end if;
5635
5636 if Inline_Now then
5637 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
5638 end if;
5639
5640 -- Check that if N is an instantiation of System.Dim_Float_IO or
5641 -- System.Dim_Integer_IO, the formal type has a dimension system.
5642
5643 if Nkind (N) = N_Package_Instantiation
5644 and then Is_Dim_IO_Package_Instantiation (N)
5645 then
5646 declare
5647 Assoc : constant Node_Id := First (Generic_Associations (N));
5648 begin
5649 if not Has_Dimension_System
5650 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
5651 then
5652 Error_Msg_N ("type with a dimension system expected", Assoc);
5653 end if;
5654 end;
5655 end if;
5656
5657 <<Leave>>
5658 if Nkind (Parent (N)) /= N_Compilation_Unit then
5659 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5660 end if;
5661
5662 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5663 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5664 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5665 Style_Check := Saved_Style_Check;
5666
5667 exception
5668 when Instantiation_Error =>
5669 if Parent_Installed then
5670 Remove_Parent;
5671 end if;
5672
5673 if Env_Installed then
5674 Restore_Env;
5675 end if;
5676
5677 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5678 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5679 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5680 Style_Check := Saved_Style_Check;
5681 end Analyze_Package_Instantiation;
5682
5683 --------------------------
5684 -- Inline_Instance_Body --
5685 --------------------------
5686
5687 -- WARNING: This routine manages SPARK regions. Return statements must be
5688 -- replaced by gotos which jump to the end of the routine and restore the
5689 -- SPARK mode.
5690
5691 procedure Inline_Instance_Body
5692 (N : Node_Id;
5693 Gen_Unit : Entity_Id;
5694 Act_Decl : Node_Id)
5695 is
5696 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
5697
5698 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
5699 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5700 Gen_Comp : constant Entity_Id :=
5701 Cunit_Entity (Get_Source_Unit (Gen_Unit));
5702
5703 Scope_Stack_Depth : constant Pos :=
5704 Scope_Stack.Last - Scope_Stack.First + 1;
5705
5706 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
5707 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
5708 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
5709
5710 Curr_Scope : Entity_Id := Empty;
5711 List : Elist_Id := No_Elist; -- init to avoid warning
5712 N_Instances : Nat := 0;
5713 Num_Inner : Nat := 0;
5714 Num_Scopes : Nat := 0;
5715 Removed : Boolean := False;
5716 S : Entity_Id;
5717 Vis : Boolean;
5718
5719 begin
5720 -- Case of generic unit defined in another unit. We must remove the
5721 -- complete context of the current unit to install that of the generic.
5722
5723 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
5724
5725 -- Loop through enclosing scopes until we reach a generic instance,
5726 -- package body, or subprogram.
5727
5728 S := Current_Scope;
5729 while Present (S) and then S /= Standard_Standard loop
5730
5731 -- Save use clauses from enclosing scopes into Use_Clauses
5732
5733 loop
5734 Num_Scopes := Num_Scopes + 1;
5735
5736 Use_Clauses (Num_Scopes) :=
5737 (Scope_Stack.Table
5738 (Scope_Stack.Last - Num_Scopes + 1).First_Use_Clause);
5739 End_Use_Clauses (Use_Clauses (Num_Scopes));
5740
5741 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5742 or else Scope_Stack.Table
5743 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5744 end loop;
5745
5746 exit when Is_Generic_Instance (S)
5747 and then (In_Package_Body (S)
5748 or else Ekind (S) = E_Procedure
5749 or else Ekind (S) = E_Function);
5750 S := Scope (S);
5751 end loop;
5752
5753 Vis := Is_Immediately_Visible (Gen_Comp);
5754
5755 -- Find and save all enclosing instances
5756
5757 S := Current_Scope;
5758
5759 while Present (S)
5760 and then S /= Standard_Standard
5761 loop
5762 if Is_Generic_Instance (S) then
5763 N_Instances := N_Instances + 1;
5764 Instances (N_Instances) := S;
5765
5766 exit when In_Package_Body (S);
5767 end if;
5768
5769 S := Scope (S);
5770 end loop;
5771
5772 -- Remove context of current compilation unit, unless we are within a
5773 -- nested package instantiation, in which case the context has been
5774 -- removed previously.
5775
5776 -- If current scope is the body of a child unit, remove context of
5777 -- spec as well. If an enclosing scope is an instance body, the
5778 -- context has already been removed, but the entities in the body
5779 -- must be made invisible as well.
5780
5781 S := Current_Scope;
5782 while Present (S) and then S /= Standard_Standard loop
5783 if Is_Generic_Instance (S)
5784 and then (In_Package_Body (S)
5785 or else Ekind (S) in E_Procedure | E_Function)
5786 then
5787 -- We still have to remove the entities of the enclosing
5788 -- instance from direct visibility.
5789
5790 declare
5791 E : Entity_Id;
5792 begin
5793 E := First_Entity (S);
5794 while Present (E) loop
5795 Set_Is_Immediately_Visible (E, False);
5796 Next_Entity (E);
5797 end loop;
5798 end;
5799
5800 exit;
5801 end if;
5802
5803 if S = Curr_Unit
5804 or else (Ekind (Curr_Unit) = E_Package_Body
5805 and then S = Spec_Entity (Curr_Unit))
5806 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5807 and then S = Corresponding_Spec
5808 (Unit_Declaration_Node (Curr_Unit)))
5809 then
5810 Removed := True;
5811
5812 -- Remove entities in current scopes from visibility, so that
5813 -- instance body is compiled in a clean environment.
5814
5815 List := Save_Scope_Stack (Handle_Use => False);
5816
5817 if Is_Child_Unit (S) then
5818
5819 -- Remove child unit from stack, as well as inner scopes.
5820 -- Removing the context of a child unit removes parent units
5821 -- as well.
5822
5823 while Current_Scope /= S loop
5824 Num_Inner := Num_Inner + 1;
5825 Inner_Scopes (Num_Inner) := Current_Scope;
5826 Pop_Scope;
5827 end loop;
5828
5829 Pop_Scope;
5830 Remove_Context (Curr_Comp);
5831 Curr_Scope := S;
5832
5833 else
5834 Remove_Context (Curr_Comp);
5835 end if;
5836
5837 if Ekind (Curr_Unit) = E_Package_Body then
5838 Remove_Context (Spec_Lib_Unit (Curr_Comp));
5839 end if;
5840 end if;
5841
5842 S := Scope (S);
5843 end loop;
5844
5845 pragma Assert (Num_Inner < Num_Scopes);
5846
5847 Push_Scope (Standard_Standard);
5848 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5849
5850 -- The inlined package body is analyzed with the configuration state
5851 -- of the context prior to the scope manipulations performed above.
5852
5853 -- ??? shouldn't this also use the warning state of the context prior
5854 -- to the scope manipulations?
5855
5856 Instantiate_Package_Body
5857 (Body_Info =>
5858 ((Inst_Node => N,
5859 Act_Decl => Act_Decl,
5860 Fin_Scop => Empty,
5861 Config_Switches => Config_Attrs,
5862 Current_Sem_Unit => Current_Sem_Unit,
5863 Expander_Status => Expander_Active,
5864 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5865 Scope_Suppress => Scope_Suppress,
5866 Warnings => Save_Warnings)),
5867 Inlined_Body => True);
5868
5869 Pop_Scope;
5870
5871 -- Restore context
5872
5873 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5874
5875 -- Reset Generic_Instance flag so that use clauses can be installed
5876 -- in the proper order. (See Use_One_Package for effect of enclosing
5877 -- instances on processing of use clauses).
5878
5879 for J in 1 .. N_Instances loop
5880 Set_Is_Generic_Instance (Instances (J), False);
5881 end loop;
5882
5883 if Removed then
5884 Install_Context (Curr_Comp, Chain => False);
5885
5886 if Present (Curr_Scope)
5887 and then Is_Child_Unit (Curr_Scope)
5888 then
5889 Push_Scope (Curr_Scope);
5890 Set_Is_Immediately_Visible (Curr_Scope);
5891
5892 -- Finally, restore inner scopes as well
5893
5894 for J in reverse 1 .. Num_Inner loop
5895 Push_Scope (Inner_Scopes (J));
5896 end loop;
5897 end if;
5898
5899 Restore_Scope_Stack (List, Handle_Use => False);
5900
5901 if Present (Curr_Scope)
5902 and then
5903 (In_Private_Part (Curr_Scope)
5904 or else In_Package_Body (Curr_Scope))
5905 then
5906 -- Install private declaration of ancestor units, which are
5907 -- currently available. Restore_Scope_Stack and Install_Context
5908 -- only install the visible part of parents.
5909
5910 declare
5911 Par : Entity_Id;
5912 begin
5913 Par := Scope (Curr_Scope);
5914 while Present (Par) and then Par /= Standard_Standard loop
5915 Install_Private_Declarations (Par);
5916 Par := Scope (Par);
5917 end loop;
5918 end;
5919 end if;
5920 end if;
5921
5922 -- Restore use clauses. For a child unit, use clauses in the parents
5923 -- are restored when installing the context, so only those in inner
5924 -- scopes (and those local to the child unit itself) need to be
5925 -- installed explicitly.
5926
5927 if Is_Child_Unit (Curr_Unit) and then Removed then
5928 for J in reverse 1 .. Num_Inner + 1 loop
5929 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5930 Use_Clauses (J);
5931 Install_Use_Clauses (Use_Clauses (J));
5932 end loop;
5933
5934 else
5935 for J in reverse 1 .. Num_Scopes loop
5936 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5937 Use_Clauses (J);
5938 Install_Use_Clauses (Use_Clauses (J));
5939 end loop;
5940 end if;
5941
5942 -- Restore status of instances. If one of them is a body, make its
5943 -- local entities visible again.
5944
5945 declare
5946 E : Entity_Id;
5947 Inst : Entity_Id;
5948
5949 begin
5950 for J in 1 .. N_Instances loop
5951 Inst := Instances (J);
5952 Set_Is_Generic_Instance (Inst, True);
5953
5954 if In_Package_Body (Inst)
5955 or else Ekind (S) in E_Procedure | E_Function
5956 then
5957 E := First_Entity (Instances (J));
5958 while Present (E) loop
5959 Set_Is_Immediately_Visible (E);
5960 Next_Entity (E);
5961 end loop;
5962 end if;
5963 end loop;
5964 end;
5965
5966 -- If generic unit is in current unit, current context is correct. Note
5967 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5968 -- enclosing scopes were removed.
5969
5970 else
5971 Instantiate_Package_Body
5972 (Body_Info =>
5973 ((Inst_Node => N,
5974 Act_Decl => Act_Decl,
5975 Fin_Scop => Empty,
5976 Config_Switches => Save_Config_Switches,
5977 Current_Sem_Unit => Current_Sem_Unit,
5978 Expander_Status => Expander_Active,
5979 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5980 Scope_Suppress => Scope_Suppress,
5981 Warnings => Save_Warnings)),
5982 Inlined_Body => True);
5983 end if;
5984 end Inline_Instance_Body;
5985
5986 -------------------------------------
5987 -- Analyze_Procedure_Instantiation --
5988 -------------------------------------
5989
5990 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5991 begin
5992 Analyze_Subprogram_Instantiation (N, E_Procedure);
5993 end Analyze_Procedure_Instantiation;
5994
5995 -----------------------------------
5996 -- Need_Subprogram_Instance_Body --
5997 -----------------------------------
5998
5999 function Need_Subprogram_Instance_Body
6000 (N : Node_Id;
6001 Subp : Entity_Id) return Boolean
6002 is
6003 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
6004 -- Return True if E is an inlined subprogram, an inlined renaming or a
6005 -- subprogram nested in an inlined subprogram. The inlining machinery
6006 -- totally disregards nested subprograms since it considers that they
6007 -- will always be compiled if the parent is (see Inline.Is_Nested).
6008
6009 ------------------------------------
6010 -- Is_Inlined_Or_Child_Of_Inlined --
6011 ------------------------------------
6012
6013 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
6014 Scop : Entity_Id;
6015
6016 begin
6017 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
6018 return True;
6019 end if;
6020
6021 Scop := Scope (E);
6022 while Scop /= Standard_Standard loop
6023 if Is_Subprogram (Scop) and then Is_Inlined (Scop) then
6024 return True;
6025 end if;
6026
6027 Scop := Scope (Scop);
6028 end loop;
6029
6030 return False;
6031 end Is_Inlined_Or_Child_Of_Inlined;
6032
6033 -- Start of processing for Need_Subprogram_Instance_Body
6034
6035 begin
6036 -- Must be in the main unit or inlined (or child of inlined)
6037
6038 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
6039
6040 -- Must be generating code or analyzing code in GNATprove mode
6041
6042 and then (Operating_Mode = Generate_Code
6043 or else (Operating_Mode = Check_Semantics
6044 and then GNATprove_Mode))
6045
6046 -- The body is needed when generating code (full expansion) and in
6047 -- in GNATprove mode (special expansion) for formal verification of
6048 -- the body itself.
6049
6050 and then (Expander_Active or GNATprove_Mode)
6051
6052 -- No point in inlining if ABE is inevitable
6053
6054 and then not Is_Known_Guaranteed_ABE (N)
6055
6056 -- Or if subprogram is eliminated
6057
6058 and then not Is_Eliminated (Subp)
6059 then
6060 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
6061 return True;
6062
6063 -- Here if not inlined, or we ignore the inlining
6064
6065 else
6066 return False;
6067 end if;
6068 end Need_Subprogram_Instance_Body;
6069
6070 --------------------------------------
6071 -- Analyze_Subprogram_Instantiation --
6072 --------------------------------------
6073
6074 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
6075 -- must be replaced by gotos which jump to the end of the routine in order
6076 -- to restore the Ghost and SPARK modes.
6077
6078 procedure Analyze_Subprogram_Instantiation
6079 (N : Node_Id;
6080 K : Entity_Kind)
6081 is
6082 Errs : constant Nat := Serious_Errors_Detected;
6083 Gen_Id : constant Node_Id := Name (N);
6084 Inst_Id : constant Entity_Id := Defining_Entity (N);
6085 Anon_Id : constant Entity_Id :=
6086 Make_Defining_Identifier (Sloc (Inst_Id),
6087 Chars => New_External_Name (Chars (Inst_Id), 'R'));
6088 Loc : constant Source_Ptr := Sloc (N);
6089
6090 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
6091 Act_Decl : Node_Id;
6092 Act_Spec : Node_Id;
6093 Act_Tree : Node_Id;
6094
6095 Env_Installed : Boolean := False;
6096 Gen_Unit : Entity_Id;
6097 Gen_Decl : Node_Id;
6098 Pack_Id : Entity_Id;
6099 Parent_Installed : Boolean := False;
6100
6101 Renamings : List_Id;
6102 -- The list of declarations that link formals and actuals of the
6103 -- instance. These are subtype declarations for formal types, and
6104 -- renaming declarations for other formals. The subprogram declaration
6105 -- for the instance is then appended to the list, and the last item on
6106 -- the list is the renaming declaration for the instance.
6107
6108 procedure Analyze_Instance_And_Renamings;
6109 -- The instance must be analyzed in a context that includes the mappings
6110 -- of generic parameters into actuals. We create a package declaration
6111 -- for this purpose, and a subprogram with an internal name within the
6112 -- package. The subprogram instance is simply an alias for the internal
6113 -- subprogram, declared in the current scope.
6114
6115 procedure Build_Subprogram_Renaming;
6116 -- If the subprogram is recursive, there are occurrences of the name of
6117 -- the generic within the body, which must resolve to the current
6118 -- instance. We add a renaming declaration after the declaration, which
6119 -- is available in the instance body, as well as in the analysis of
6120 -- aspects that appear in the generic. This renaming declaration is
6121 -- inserted after the instance declaration which it renames.
6122
6123 ------------------------------------
6124 -- Analyze_Instance_And_Renamings --
6125 ------------------------------------
6126
6127 procedure Analyze_Instance_And_Renamings is
6128 Def_Ent : constant Entity_Id := Defining_Entity (N);
6129 Pack_Decl : Node_Id;
6130
6131 begin
6132 if Nkind (Parent (N)) = N_Compilation_Unit then
6133
6134 -- For the case of a compilation unit, the container package has
6135 -- the same name as the instantiation, to insure that the binder
6136 -- calls the elaboration procedure with the right name. Copy the
6137 -- entity of the instance, which may have compilation level flags
6138 -- (e.g. Is_Child_Unit) set.
6139
6140 Pack_Id := New_Copy (Def_Ent);
6141
6142 else
6143 -- Otherwise we use the name of the instantiation concatenated
6144 -- with its source position to ensure uniqueness if there are
6145 -- several instantiations with the same name.
6146
6147 Pack_Id :=
6148 Make_Defining_Identifier (Loc,
6149 Chars => New_External_Name
6150 (Related_Id => Chars (Def_Ent),
6151 Suffix => "GP",
6152 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
6153 end if;
6154
6155 Pack_Decl :=
6156 Make_Package_Declaration (Loc,
6157 Specification => Make_Package_Specification (Loc,
6158 Defining_Unit_Name => Pack_Id,
6159 Visible_Declarations => Renamings,
6160 End_Label => Empty));
6161
6162 Set_Instance_Spec (N, Pack_Decl);
6163 Set_Is_Generic_Instance (Pack_Id);
6164 Set_Debug_Info_Needed (Pack_Id);
6165
6166 -- Case of not a compilation unit
6167
6168 if Nkind (Parent (N)) /= N_Compilation_Unit then
6169 Mark_Rewrite_Insertion (Pack_Decl);
6170 Insert_Before (N, Pack_Decl);
6171 Set_Has_Completion (Pack_Id);
6172
6173 -- Case of an instantiation that is a compilation unit
6174
6175 -- Place declaration on current node so context is complete for
6176 -- analysis (including nested instantiations), and for use in a
6177 -- context_clause (see Analyze_With_Clause).
6178
6179 else
6180 Set_Unit (Parent (N), Pack_Decl);
6181 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
6182 end if;
6183
6184 Analyze (Pack_Decl);
6185 Check_Formal_Packages (Pack_Id);
6186
6187 -- Body of the enclosing package is supplied when instantiating the
6188 -- subprogram body, after semantic analysis is completed.
6189
6190 if Nkind (Parent (N)) = N_Compilation_Unit then
6191
6192 -- Remove package itself from visibility, so it does not
6193 -- conflict with subprogram.
6194
6195 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
6196
6197 -- Set name and scope of internal subprogram so that the proper
6198 -- external name will be generated. The proper scope is the scope
6199 -- of the wrapper package. We need to generate debugging info for
6200 -- the internal subprogram, so set flag accordingly.
6201
6202 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
6203 Set_Scope (Anon_Id, Scope (Pack_Id));
6204
6205 -- Mark wrapper package as referenced, to avoid spurious warnings
6206 -- if the instantiation appears in various with_ clauses of
6207 -- subunits of the main unit.
6208
6209 Set_Referenced (Pack_Id);
6210 end if;
6211
6212 Set_Is_Generic_Instance (Anon_Id);
6213 Set_Debug_Info_Needed (Anon_Id);
6214 Act_Decl_Id := New_Copy (Anon_Id);
6215
6216 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
6217 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
6218 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
6219
6220 -- Subprogram instance comes from source only if generic does
6221
6222 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
6223
6224 -- If the instance is a child unit, mark the Id accordingly. Mark
6225 -- the anonymous entity as well, which is the real subprogram and
6226 -- which is used when the instance appears in a context clause.
6227 -- Similarly, propagate the Is_Eliminated flag to handle properly
6228 -- nested eliminated subprograms.
6229
6230 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
6231 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
6232 New_Overloaded_Entity (Act_Decl_Id);
6233 Check_Eliminated (Act_Decl_Id);
6234 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
6235
6236 if Nkind (Parent (N)) = N_Compilation_Unit then
6237
6238 -- In compilation unit case, kill elaboration checks on the
6239 -- instantiation, since they are never needed - the body is
6240 -- instantiated at the same point as the spec.
6241
6242 if Legacy_Elaboration_Checks then
6243 Set_Kill_Elaboration_Checks (Act_Decl_Id);
6244 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
6245 end if;
6246
6247 Set_Is_Compilation_Unit (Anon_Id);
6248 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
6249 end if;
6250
6251 -- The instance is not a freezing point for the new subprogram.
6252 -- The anonymous subprogram may have a freeze node, created for
6253 -- some delayed aspects. This freeze node must not be inherited
6254 -- by the visible subprogram entity.
6255
6256 Set_Is_Frozen (Act_Decl_Id, False);
6257 Set_Freeze_Node (Act_Decl_Id, Empty);
6258
6259 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
6260 Valid_Operator_Definition (Act_Decl_Id);
6261 end if;
6262
6263 Set_Alias (Act_Decl_Id, Anon_Id);
6264 Set_Has_Completion (Act_Decl_Id);
6265 Set_Related_Instance (Pack_Id, Act_Decl_Id);
6266
6267 if Nkind (Parent (N)) = N_Compilation_Unit then
6268 Set_Body_Required (Parent (N), False);
6269 end if;
6270 end Analyze_Instance_And_Renamings;
6271
6272 -------------------------------
6273 -- Build_Subprogram_Renaming --
6274 -------------------------------
6275
6276 procedure Build_Subprogram_Renaming is
6277 Renaming_Decl : Node_Id;
6278 Unit_Renaming : Node_Id;
6279
6280 begin
6281 Unit_Renaming :=
6282 Make_Subprogram_Renaming_Declaration (Loc,
6283 Specification =>
6284 Copy_Generic_Node
6285 (Specification (Original_Node (Gen_Decl)),
6286 Empty,
6287 Instantiating => True),
6288 Name => New_Occurrence_Of (Anon_Id, Loc));
6289
6290 -- The generic may be a child unit. The renaming needs an identifier
6291 -- with the proper name.
6292
6293 Set_Defining_Unit_Name (Specification (Unit_Renaming),
6294 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
6295
6296 -- If there is a formal subprogram with the same name as the unit
6297 -- itself, do not add this renaming declaration, to prevent
6298 -- ambiguities when there is a call with that name in the body.
6299
6300 Renaming_Decl := First (Renamings);
6301 while Present (Renaming_Decl) loop
6302 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
6303 and then
6304 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
6305 then
6306 exit;
6307 end if;
6308
6309 Next (Renaming_Decl);
6310 end loop;
6311
6312 if No (Renaming_Decl) then
6313 Append (Unit_Renaming, Renamings);
6314 end if;
6315 end Build_Subprogram_Renaming;
6316
6317 -- Local variables
6318
6319 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
6320 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
6321 Saved_ISMP : constant Boolean :=
6322 Ignore_SPARK_Mode_Pragmas_In_Instance;
6323 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
6324 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
6325 -- Save the Ghost and SPARK mode-related data to restore on exit
6326
6327 Vis_Prims_List : Elist_Id := No_Elist;
6328 -- List of primitives made temporarily visible in the instantiation
6329 -- to match the visibility of the formal type
6330
6331 -- Start of processing for Analyze_Subprogram_Instantiation
6332
6333 begin
6334 -- Preserve relevant elaboration-related attributes of the context which
6335 -- are no longer available or very expensive to recompute once analysis,
6336 -- resolution, and expansion are over.
6337
6338 Mark_Elaboration_Attributes
6339 (N_Id => N,
6340 Checks => True,
6341 Level => True,
6342 Modes => True,
6343 Warnings => True);
6344
6345 -- Very first thing: check for special Text_IO unit in case we are
6346 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
6347 -- such an instantiation is bogus (these are packages, not subprograms),
6348 -- but we get a better error message if we do this.
6349
6350 Check_Text_IO_Special_Unit (Gen_Id);
6351
6352 -- Make node global for error reporting
6353
6354 Instantiation_Node := N;
6355
6356 -- For package instantiations we turn off style checks, because they
6357 -- will have been emitted in the generic. For subprogram instantiations
6358 -- we want to apply at least the check on overriding indicators so we
6359 -- do not modify the style check status.
6360
6361 -- The renaming declarations for the actuals do not come from source and
6362 -- will not generate spurious warnings.
6363
6364 Preanalyze_Actuals (N);
6365
6366 Init_Env;
6367 Env_Installed := True;
6368 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
6369 Gen_Unit := Entity (Gen_Id);
6370
6371 -- A subprogram instantiation is Ghost when it is subject to pragma
6372 -- Ghost or the generic template is Ghost. Set the mode now to ensure
6373 -- that any nodes generated during analysis and expansion are marked as
6374 -- Ghost.
6375
6376 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
6377
6378 Generate_Reference (Gen_Unit, Gen_Id);
6379
6380 if Nkind (Gen_Id) = N_Identifier
6381 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
6382 then
6383 Error_Msg_NE
6384 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
6385 end if;
6386
6387 if Etype (Gen_Unit) = Any_Type then
6388 Restore_Env;
6389 goto Leave;
6390 end if;
6391
6392 -- Verify that it is a generic subprogram of the right kind, and that
6393 -- it does not lead to a circular instantiation.
6394
6395 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
6396 Error_Msg_NE
6397 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
6398
6399 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
6400 Error_Msg_NE
6401 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
6402
6403 elsif In_Open_Scopes (Gen_Unit) then
6404 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
6405
6406 else
6407 Mutate_Ekind (Inst_Id, K);
6408 Set_Scope (Inst_Id, Current_Scope);
6409
6410 Set_Entity (Gen_Id, Gen_Unit);
6411
6412 if In_Extended_Main_Source_Unit (N) then
6413 Set_Is_Instantiated (Gen_Unit);
6414 Generate_Reference (Gen_Unit, N);
6415 end if;
6416
6417 -- If renaming, get original unit
6418
6419 if Present (Renamed_Entity (Gen_Unit))
6420 and then Is_Generic_Subprogram (Renamed_Entity (Gen_Unit))
6421 then
6422 Gen_Unit := Renamed_Entity (Gen_Unit);
6423 Set_Is_Instantiated (Gen_Unit);
6424 Generate_Reference (Gen_Unit, N);
6425 end if;
6426
6427 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
6428 Error_Msg_Node_2 := Current_Scope;
6429 Error_Msg_NE
6430 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
6431 Circularity_Detected := True;
6432 Restore_Hidden_Primitives (Vis_Prims_List);
6433 goto Leave;
6434 end if;
6435
6436 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
6437
6438 -- Initialize renamings map, for error checking
6439
6440 Generic_Renamings.Set_Last (0);
6441 Generic_Renamings_HTable.Reset;
6442
6443 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
6444
6445 -- Copy original generic tree, to produce text for instantiation
6446
6447 Act_Tree :=
6448 Copy_Generic_Node
6449 (Original_Node (Gen_Decl), Empty, Instantiating => True);
6450
6451 -- Inherit overriding indicator from instance node
6452
6453 Act_Spec := Specification (Act_Tree);
6454 Set_Must_Override (Act_Spec, Must_Override (N));
6455 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
6456
6457 Renamings :=
6458 Analyze_Associations
6459 (I_Node => N,
6460 Formals => Generic_Formal_Declarations (Act_Tree),
6461 F_Copy => Generic_Formal_Declarations (Gen_Decl));
6462
6463 Vis_Prims_List := Check_Hidden_Primitives (Renamings);
6464
6465 -- The subprogram itself cannot contain a nested instance, so the
6466 -- current parent is left empty.
6467
6468 Set_Instance_Env (Gen_Unit, Empty);
6469
6470 -- Build the subprogram declaration, which does not appear in the
6471 -- generic template, and give it a sloc consistent with that of the
6472 -- template.
6473
6474 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
6475 Set_Generic_Parent (Act_Spec, Gen_Unit);
6476 Act_Decl :=
6477 Make_Subprogram_Declaration (Sloc (Act_Spec),
6478 Specification => Act_Spec);
6479
6480 -- The aspects have been copied previously, but they have to be
6481 -- linked explicitly to the new subprogram declaration. Explicit
6482 -- pre/postconditions on the instance are analyzed below, in a
6483 -- separate step.
6484
6485 Move_Aspects (Act_Tree, To => Act_Decl);
6486 Set_Categorization_From_Pragmas (Act_Decl);
6487
6488 if Parent_Installed then
6489 Hide_Current_Scope;
6490 end if;
6491
6492 Append (Act_Decl, Renamings);
6493
6494 -- Contract-related source pragmas that follow a generic subprogram
6495 -- must be instantiated explicitly because they are not part of the
6496 -- subprogram template.
6497
6498 Instantiate_Subprogram_Contract
6499 (Original_Node (Gen_Decl), Renamings);
6500
6501 Build_Subprogram_Renaming;
6502
6503 -- If the context of the instance is subject to SPARK_Mode "off" or
6504 -- the annotation is altogether missing, set the global flag which
6505 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
6506 -- the instance. This should be done prior to analyzing the instance.
6507
6508 if SPARK_Mode /= On then
6509 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
6510 end if;
6511
6512 -- If the context of an instance is not subject to SPARK_Mode "off",
6513 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
6514 -- the latter should be the one applicable to the instance.
6515
6516 if not Ignore_SPARK_Mode_Pragmas_In_Instance
6517 and then Saved_SM /= Off
6518 and then Present (SPARK_Pragma (Gen_Unit))
6519 then
6520 Set_SPARK_Mode (Gen_Unit);
6521 end if;
6522
6523 -- Need to mark Anon_Id intrinsic before calling
6524 -- Analyze_Instance_And_Renamings because this flag may be propagated
6525 -- to other nodes.
6526
6527 if Is_Intrinsic_Subprogram (Gen_Unit) then
6528 Set_Is_Intrinsic_Subprogram (Anon_Id);
6529 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
6530 end if;
6531
6532 Analyze_Instance_And_Renamings;
6533
6534 -- Restore SPARK_Mode from the context after analysis of the package
6535 -- declaration, so that the SPARK_Mode on the generic spec does not
6536 -- apply to the pending instance for the instance body.
6537
6538 if not Ignore_SPARK_Mode_Pragmas_In_Instance
6539 and then Saved_SM /= Off
6540 and then Present (SPARK_Pragma (Gen_Unit))
6541 then
6542 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6543 end if;
6544
6545 -- If the generic is marked Import (Intrinsic), then so is the
6546 -- instance; this indicates that there is no body to instantiate.
6547 -- We also copy the interface name in case this is handled by the
6548 -- back-end and deal with an instance of unchecked conversion.
6549
6550 if Is_Intrinsic_Subprogram (Gen_Unit) then
6551 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
6552 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
6553
6554 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
6555 Validate_Unchecked_Conversion (N, Act_Decl_Id);
6556 end if;
6557 end if;
6558
6559 -- Inherit convention from generic unit. Intrinsic convention, as for
6560 -- an instance of unchecked conversion, is not inherited because an
6561 -- explicit Ada instance has been created.
6562
6563 if Has_Convention_Pragma (Gen_Unit)
6564 and then Convention (Gen_Unit) /= Convention_Intrinsic
6565 then
6566 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
6567 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
6568 end if;
6569
6570 Generate_Definition (Act_Decl_Id);
6571
6572 -- Inherit all inlining-related flags which apply to the generic in
6573 -- the subprogram and its declaration.
6574
6575 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
6576 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
6577
6578 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
6579 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
6580
6581 Set_Has_Pragma_Inline_Always
6582 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
6583 Set_Has_Pragma_Inline_Always
6584 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
6585
6586 Set_Has_Pragma_No_Inline
6587 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
6588 Set_Has_Pragma_No_Inline
6589 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
6590
6591 -- Propagate No_Raise if pragma applied to generic unit. This must
6592 -- be done explicitly because the pragma does not appear in generic
6593 -- declarations (unlike the aspect).
6594
6595 if No_Raise (Gen_Unit) then
6596 Set_No_Raise (Act_Decl_Id);
6597 Set_No_Raise (Anon_Id);
6598 end if;
6599
6600 -- Likewise for No_Return
6601
6602 if No_Return (Gen_Unit) then
6603 Set_No_Return (Act_Decl_Id);
6604 Set_No_Return (Anon_Id);
6605 end if;
6606
6607 -- Mark both the instance spec and the anonymous package in case the
6608 -- body is instantiated at a later pass. This preserves the original
6609 -- context in effect for the body.
6610
6611 if SPARK_Mode /= On then
6612 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
6613 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
6614 end if;
6615
6616 if Legacy_Elaboration_Checks
6617 and then not Is_Intrinsic_Subprogram (Gen_Unit)
6618 then
6619 Check_Elab_Instantiation (N);
6620 end if;
6621
6622 -- Save the scenario for later examination by the ABE Processing
6623 -- phase.
6624
6625 Record_Elaboration_Scenario (N);
6626
6627 -- The instantiation results in a guaranteed ABE. Create a completing
6628 -- body for the subprogram declaration because the real body will not
6629 -- be instantiated.
6630
6631 if Is_Known_Guaranteed_ABE (N) then
6632 Provide_Completing_Bodies (Instance_Spec (N));
6633 end if;
6634
6635 if Is_Dispatching_Operation (Act_Decl_Id)
6636 and then Ada_Version >= Ada_2005
6637 then
6638 declare
6639 Formal : Entity_Id;
6640
6641 begin
6642 Formal := First_Formal (Act_Decl_Id);
6643 while Present (Formal) loop
6644 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
6645 and then Is_Controlling_Formal (Formal)
6646 and then not Can_Never_Be_Null (Formal)
6647 then
6648 Error_Msg_NE
6649 ("access parameter& is controlling,", N, Formal);
6650 Error_Msg_NE
6651 ("\corresponding parameter of & must be explicitly "
6652 & "null-excluding", N, Gen_Id);
6653 end if;
6654
6655 Next_Formal (Formal);
6656 end loop;
6657 end;
6658 end if;
6659
6660 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
6661
6662 Validate_Categorization_Dependency (N, Act_Decl_Id);
6663
6664 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
6665 Inherit_Context (Gen_Decl, N);
6666
6667 Restore_Private_Views (Pack_Id, False);
6668
6669 -- If the context requires a full instantiation, mark node for
6670 -- subsequent construction of the body.
6671
6672 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
6673 Check_Forward_Instantiation (Gen_Decl);
6674
6675 -- The wrapper package is always delayed, because it does not
6676 -- constitute a freeze point, but to insure that the freeze node
6677 -- is placed properly, it is created directly when instantiating
6678 -- the body (otherwise the freeze node might appear to early for
6679 -- nested instantiations).
6680
6681 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6682 Rewrite (N, Unit (Parent (N)));
6683 Set_Unit (Parent (N), N);
6684 end if;
6685
6686 -- Replace instance node for library-level instantiations of
6687 -- intrinsic subprograms.
6688
6689 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6690 Rewrite (N, Unit (Parent (N)));
6691 Set_Unit (Parent (N), N);
6692 end if;
6693
6694 if Parent_Installed then
6695 Remove_Parent;
6696 end if;
6697
6698 Restore_Hidden_Primitives (Vis_Prims_List);
6699 Restore_Env;
6700 Env_Installed := False;
6701 Generic_Renamings.Set_Last (0);
6702 Generic_Renamings_HTable.Reset;
6703 end if;
6704
6705 <<Leave>>
6706 -- Analyze aspects in declaration if no errors appear in the instance.
6707
6708 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
6709 Analyze_Aspect_Specifications (N, Act_Decl_Id);
6710 end if;
6711
6712 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6713 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6714 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6715
6716 exception
6717 when Instantiation_Error =>
6718 if Parent_Installed then
6719 Remove_Parent;
6720 end if;
6721
6722 if Env_Installed then
6723 Restore_Env;
6724 end if;
6725
6726 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6727 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6728 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6729 end Analyze_Subprogram_Instantiation;
6730
6731 ---------------------------
6732 -- Get_Associated_Entity --
6733 ---------------------------
6734
6735 function Get_Associated_Entity (Id : Entity_Id) return Entity_Id is
6736 Assoc : Entity_Id;
6737
6738 begin
6739 Assoc := Associated_Entity (Id);
6740
6741 if Present (Assoc) then
6742 while Present (Associated_Entity (Assoc)) loop
6743 Assoc := Associated_Entity (Assoc);
6744 end loop;
6745 end if;
6746
6747 return Assoc;
6748 end Get_Associated_Entity;
6749
6750 -------------------------
6751 -- Get_Associated_Node --
6752 -------------------------
6753
6754 function Get_Associated_Node (N : Node_Id) return Node_Id is
6755 Assoc : Node_Id;
6756
6757 begin
6758 Assoc := Associated_Node (N);
6759
6760 if Nkind (Assoc) /= Nkind (N) then
6761 return Assoc;
6762
6763 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
6764 return Assoc;
6765
6766 else
6767 -- If the node is part of an inner generic, it may itself have been
6768 -- remapped into a further generic copy. Associated_Node is otherwise
6769 -- used for the entity of the node, and will be of a different node
6770 -- kind, or else N has been rewritten as a literal or function call.
6771
6772 while Present (Associated_Node (Assoc))
6773 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6774 loop
6775 Assoc := Associated_Node (Assoc);
6776 end loop;
6777
6778 -- Follow an additional link in case the final node was rewritten.
6779 -- This can only happen with nested generic units.
6780
6781 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6782 and then Present (Associated_Node (Assoc))
6783 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6784 | N_Explicit_Dereference
6785 | N_Integer_Literal
6786 | N_Real_Literal
6787 | N_String_Literal
6788 then
6789 Assoc := Associated_Node (Assoc);
6790 end if;
6791
6792 -- An additional special case: an unconstrained type in an object
6793 -- declaration may have been rewritten as a local subtype constrained
6794 -- by the expression in the declaration. We need to recover the
6795 -- original entity, which may be global.
6796
6797 if Present (Original_Node (Assoc))
6798 and then Nkind (Parent (N)) = N_Object_Declaration
6799 then
6800 Assoc := Original_Node (Assoc);
6801 end if;
6802
6803 return Assoc;
6804 end if;
6805 end Get_Associated_Node;
6806
6807 -----------------------------------
6808 -- Build_Subprogram_Decl_Wrapper --
6809 -----------------------------------
6810
6811 function Build_Subprogram_Decl_Wrapper
6812 (Formal_Subp : Entity_Id) return Node_Id
6813 is
6814 Loc : constant Source_Ptr := Sloc (Current_Scope);
6815 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6816 Decl : Node_Id;
6817 Subp : Entity_Id;
6818 Parm_Spec : Node_Id;
6819 Profile : List_Id := New_List;
6820 Spec : Node_Id;
6821 Form_F : Entity_Id;
6822 New_F : Entity_Id;
6823
6824 begin
6825 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6826 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6827 Set_Is_Generic_Actual_Subprogram (Subp);
6828
6829 Profile := Parameter_Specifications (
6830 New_Copy_Tree
6831 (Specification (Unit_Declaration_Node (Formal_Subp))));
6832
6833 Form_F := First_Formal (Formal_Subp);
6834 Parm_Spec := First (Profile);
6835
6836 -- Create new entities for the formals. Reset entities so that
6837 -- parameter types are properly resolved when wrapper declaration
6838 -- is analyzed.
6839
6840 while Present (Parm_Spec) loop
6841 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6842 Set_Defining_Identifier (Parm_Spec, New_F);
6843 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6844 Next (Parm_Spec);
6845 Next_Formal (Form_F);
6846 end loop;
6847
6848 if Ret_Type = Standard_Void_Type then
6849 Spec :=
6850 Make_Procedure_Specification (Loc,
6851 Defining_Unit_Name => Subp,
6852 Parameter_Specifications => Profile);
6853 else
6854 Spec :=
6855 Make_Function_Specification (Loc,
6856 Defining_Unit_Name => Subp,
6857 Parameter_Specifications => Profile,
6858 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6859 end if;
6860
6861 Decl :=
6862 Make_Subprogram_Declaration (Loc, Specification => Spec);
6863
6864 return Decl;
6865 end Build_Subprogram_Decl_Wrapper;
6866
6867 -----------------------------------
6868 -- Build_Subprogram_Body_Wrapper --
6869 -----------------------------------
6870
6871 function Build_Subprogram_Body_Wrapper
6872 (Formal_Subp : Entity_Id;
6873 Actual_Name : Node_Id) return Node_Id
6874 is
6875 Loc : constant Source_Ptr := Sloc (Current_Scope);
6876 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6877 Spec_Node : constant Node_Id :=
6878 Specification
6879 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6880 Act : Node_Id;
6881 Actuals : List_Id;
6882 Body_Node : Node_Id;
6883 Stmt : Node_Id;
6884 begin
6885 Actuals := New_List;
6886 Act := First (Parameter_Specifications (Spec_Node));
6887
6888 while Present (Act) loop
6889 Append_To (Actuals,
6890 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6891 Next (Act);
6892 end loop;
6893
6894 if Ret_Type = Standard_Void_Type then
6895 Stmt := Make_Procedure_Call_Statement (Loc,
6896 Name => Actual_Name,
6897 Parameter_Associations => Actuals);
6898
6899 else
6900 Stmt := Make_Simple_Return_Statement (Loc,
6901 Expression =>
6902 Make_Function_Call (Loc,
6903 Name => Actual_Name,
6904 Parameter_Associations => Actuals));
6905 end if;
6906
6907 Body_Node := Make_Subprogram_Body (Loc,
6908 Specification => Spec_Node,
6909 Declarations => New_List,
6910 Handled_Statement_Sequence =>
6911 Make_Handled_Sequence_Of_Statements (Loc,
6912 Statements => New_List (Stmt)));
6913
6914 return Body_Node;
6915 end Build_Subprogram_Body_Wrapper;
6916
6917 -------------------------------
6918 -- Build_Subprogram_Wrappers --
6919 -------------------------------
6920
6921 procedure Build_Subprogram_Wrappers
6922 (Match, Analyzed_Formal : Node_Id; Renamings : List_Id)
6923 is
6924 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result;
6925 -- Adjust Sloc so that errors will be reported on the instance rather
6926 -- than the generic.
6927
6928 ------------------------
6929 -- Adjust_Aspect_Sloc --
6930 ------------------------
6931
6932 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result is
6933 begin
6934 Adjust_Instantiation_Sloc (N, S_Adjustment);
6935 return OK;
6936 end Adjust_Aspect_Sloc;
6937
6938 procedure Adjust_Aspect_Slocs is new
6939 Traverse_Proc (Adjust_Aspect_Sloc);
6940
6941 Formal : constant Entity_Id :=
6942 Defining_Unit_Name (Specification (Analyzed_Formal));
6943 Aspect_Spec : Node_Id;
6944 Decl_Node : Node_Id;
6945 Actual_Name : Node_Id;
6946
6947 -- Start of processing for Build_Subprogram_Wrappers
6948
6949 begin
6950 -- Create declaration for wrapper subprogram.
6951 -- The actual can be overloaded, in which case it will be
6952 -- resolved when the call in the wrapper body is analyzed.
6953 -- We attach the possible interpretations of the actual to
6954 -- the name to be used in the call in the wrapper body.
6955
6956 if Is_Entity_Name (Match) then
6957 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
6958
6959 if Is_Overloaded (Match) then
6960 Save_Interps (Match, Actual_Name);
6961 end if;
6962
6963 else
6964 -- Use renaming declaration created when analyzing actual.
6965 -- This may be incomplete if there are several formal
6966 -- subprograms whose actual is an attribute ???
6967
6968 declare
6969 Renaming_Decl : constant Node_Id := Last (Renamings);
6970
6971 begin
6972 Actual_Name := New_Occurrence_Of
6973 (Defining_Entity (Renaming_Decl), Sloc (Match));
6974 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
6975 end;
6976 end if;
6977
6978 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
6979
6980 -- Transfer aspect specifications from formal subprogram to wrapper
6981
6982 Set_Aspect_Specifications (Decl_Node,
6983 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
6984
6985 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
6986 while Present (Aspect_Spec) loop
6987 Adjust_Aspect_Slocs (Aspect_Spec);
6988 Set_Analyzed (Aspect_Spec, False);
6989 Next (Aspect_Spec);
6990 end loop;
6991
6992 Append_To (Renamings, Decl_Node);
6993
6994 -- Create corresponding body, and append it to association list
6995 -- that appears at the head of the declarations in the instance.
6996 -- The subprogram may be called in the analysis of subsequent
6997 -- actuals.
6998
6999 Append_To (Renamings,
7000 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
7001 end Build_Subprogram_Wrappers;
7002
7003 -------------------------------------------
7004 -- Build_Instance_Compilation_Unit_Nodes --
7005 -------------------------------------------
7006
7007 procedure Build_Instance_Compilation_Unit_Nodes
7008 (N : Node_Id;
7009 Act_Body : Node_Id;
7010 Act_Decl : Node_Id)
7011 is
7012 Decl_Cunit : Node_Id;
7013 Body_Cunit : Node_Id;
7014 Citem : Node_Id;
7015 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
7016 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
7017
7018 begin
7019 -- A new compilation unit node is built for the instance declaration.
7020 -- It relocates the auxiliary declaration node from the compilation unit
7021 -- where the instance appeared, so that declarations that originally
7022 -- followed the instance will be attached to the spec compilation unit.
7023
7024 Decl_Cunit :=
7025 Make_Compilation_Unit (Sloc (N),
7026 Context_Items => Empty_List,
7027 Unit => Act_Decl,
7028 Aux_Decls_Node => Relocate_Node (Aux_Decls_Node (Parent (N))));
7029
7030 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
7031
7032 -- The new compilation unit is linked to its body, but both share the
7033 -- same file, so we do not set Body_Required on the new unit so as not
7034 -- to create a spurious dependency on a non-existent body in the ali.
7035 -- This simplifies CodePeer unit traversal.
7036
7037 -- We use the original instantiation compilation unit as the resulting
7038 -- compilation unit of the instance, since this is the main unit.
7039
7040 Rewrite (N, Act_Body);
7041
7042 Body_Cunit := Parent (N);
7043
7044 -- Set spec/body links for the two compilation units
7045
7046 Set_Body_Lib_Unit (Decl_Cunit, Body_Cunit);
7047 Set_Spec_Lib_Unit (Body_Cunit, Decl_Cunit);
7048
7049 -- Preserve the private nature of the package if needed
7050
7051 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
7052
7053 -- If the instance is not the main unit, its context, categorization
7054 -- and elaboration entity are not relevant to the compilation.
7055
7056 if Body_Cunit /= Cunit (Main_Unit) then
7057 Make_Instance_Unit (Body_Cunit, In_Main => False);
7058 return;
7059 end if;
7060
7061 -- The context clause items on the instantiation, which are now attached
7062 -- to the body compilation unit (since the body overwrote the original
7063 -- instantiation node), semantically belong on the spec, so copy them
7064 -- there. It's harmless to leave them on the body as well. In fact one
7065 -- could argue that they belong in both places.
7066
7067 Citem := First (Context_Items (Body_Cunit));
7068 while Present (Citem) loop
7069 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
7070 Next (Citem);
7071 end loop;
7072
7073 -- Propagate categorization flags on packages, so that they appear in
7074 -- the ali file for the spec of the unit.
7075
7076 if Ekind (New_Main) = E_Package then
7077 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
7078 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
7079 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
7080 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
7081 Set_Is_Remote_Call_Interface
7082 (Old_Main, Is_Remote_Call_Interface (New_Main));
7083 end if;
7084
7085 -- Make entry in Units table, so that binder can generate call to
7086 -- elaboration procedure for body, if any.
7087
7088 Make_Instance_Unit (Body_Cunit, In_Main => True);
7089 Main_Unit_Entity := New_Main;
7090 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
7091
7092 -- Build elaboration entity, since the instance may certainly generate
7093 -- elaboration code requiring a flag for protection.
7094
7095 Build_Elaboration_Entity (Decl_Cunit, New_Main);
7096 end Build_Instance_Compilation_Unit_Nodes;
7097
7098 --------------------------------
7099 -- Check_Abbreviated_Instance --
7100 --------------------------------
7101
7102 procedure Check_Abbreviated_Instance
7103 (N : Node_Id;
7104 Parent_Installed : in out Boolean)
7105 is
7106 Inst_Node : Node_Id;
7107
7108 begin
7109 if Nkind (N) = N_Package_Specification
7110 and then Is_Abbreviated_Instance (Defining_Entity (N))
7111 then
7112 Inst_Node := Get_Unit_Instantiation_Node (Defining_Entity (N));
7113 Check_Generic_Child_Unit (Name (Inst_Node), Parent_Installed);
7114 end if;
7115 end Check_Abbreviated_Instance;
7116
7117 -----------------------------
7118 -- Check_Access_Definition --
7119 -----------------------------
7120
7121 procedure Check_Access_Definition (N : Node_Id) is
7122 begin
7123 pragma Assert
7124 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
7125 null;
7126 end Check_Access_Definition;
7127
7128 -----------------------------------
7129 -- Check_Formal_Package_Instance --
7130 -----------------------------------
7131
7132 -- If the formal has specific parameters, they must match those of the
7133 -- actual. Both of them are instances, and the renaming declarations for
7134 -- their formal parameters appear in the same order in both. The analyzed
7135 -- formal has been analyzed in the context of the current instance.
7136
7137 procedure Check_Formal_Package_Instance
7138 (Formal_Pack : Entity_Id;
7139 Actual_Pack : Entity_Id)
7140 is
7141 E1 : Entity_Id := First_Entity (Actual_Pack);
7142 E2 : Entity_Id := First_Entity (Formal_Pack);
7143 Prev_E1 : Entity_Id;
7144
7145 Expr1 : Node_Id;
7146 Expr2 : Node_Id;
7147
7148 procedure Check_Mismatch (B : Boolean);
7149 -- Common error routine for mismatch between the parameters of the
7150 -- actual instance and those of the formal package.
7151
7152 function Is_Defaulted (Param : Entity_Id) return Boolean;
7153 -- If the formal package has partly box-initialized formals, skip
7154 -- conformance check for these formals. Previously the code assumed
7155 -- that box initialization for a formal package applied to all its
7156 -- formal parameters.
7157
7158 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
7159 -- The formal may come from a nested formal package, and the actual may
7160 -- have been constant-folded. To determine whether the two denote the
7161 -- same entity we may have to traverse several definitions to recover
7162 -- the ultimate entity that they refer to.
7163
7164 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
7165 -- The formal and the actual must be identical, but if both are
7166 -- given by attributes they end up renaming different generated bodies,
7167 -- and we must verify that the attributes themselves match.
7168
7169 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
7170 -- Similarly, if the formal comes from a nested formal package, the
7171 -- actual may designate the formal through multiple renamings, which
7172 -- have to be followed to determine the original variable in question.
7173
7174 --------------------
7175 -- Check_Mismatch --
7176 --------------------
7177
7178 procedure Check_Mismatch (B : Boolean) is
7179 -- A Formal_Type_Declaration for a derived private type is rewritten
7180 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
7181 -- which is why we examine the original node.
7182
7183 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
7184
7185 begin
7186 if Kind = N_Formal_Type_Declaration then
7187 return;
7188
7189 elsif Kind in N_Formal_Object_Declaration
7190 | N_Formal_Package_Declaration
7191 | N_Formal_Subprogram_Declaration
7192 then
7193 null;
7194
7195 -- Ada 2012: If both formal and actual are incomplete types they
7196 -- are conformant.
7197
7198 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
7199 null;
7200
7201 elsif B then
7202 Error_Msg_NE
7203 ("actual for & in actual instance does not match formal",
7204 Parent (Actual_Pack), E1);
7205 end if;
7206 end Check_Mismatch;
7207
7208 ------------------
7209 -- Is_Defaulted --
7210 ------------------
7211
7212 function Is_Defaulted (Param : Entity_Id) return Boolean is
7213 Assoc : Node_Id;
7214
7215 begin
7216 Assoc :=
7217 First (Generic_Associations (Parent
7218 (Associated_Formal_Package (Actual_Pack))));
7219
7220 while Present (Assoc) loop
7221 if Nkind (Assoc) = N_Others_Choice then
7222 return True;
7223
7224 elsif Nkind (Assoc) = N_Generic_Association
7225 and then Chars (Selector_Name (Assoc)) = Chars (Param)
7226 then
7227 return Box_Present (Assoc);
7228 end if;
7229
7230 Next (Assoc);
7231 end loop;
7232
7233 return False;
7234 end Is_Defaulted;
7235
7236 --------------------------------
7237 -- Same_Instantiated_Constant --
7238 --------------------------------
7239
7240 function Same_Instantiated_Constant
7241 (E1, E2 : Entity_Id) return Boolean
7242 is
7243 Ent : Entity_Id;
7244
7245 begin
7246 Ent := E2;
7247 while Present (Ent) loop
7248 if E1 = Ent then
7249 return True;
7250
7251 elsif Ekind (Ent) /= E_Constant then
7252 return False;
7253
7254 elsif Is_Entity_Name (Constant_Value (Ent)) then
7255 if Entity (Constant_Value (Ent)) = E1 then
7256 return True;
7257 else
7258 Ent := Entity (Constant_Value (Ent));
7259 end if;
7260
7261 -- The actual may be a constant that has been folded. Recover
7262 -- original name.
7263
7264 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
7265 Ent := Entity (Original_Node (Constant_Value (Ent)));
7266
7267 else
7268 return False;
7269 end if;
7270 end loop;
7271
7272 return False;
7273 end Same_Instantiated_Constant;
7274
7275 --------------------------------
7276 -- Same_Instantiated_Function --
7277 --------------------------------
7278
7279 function Same_Instantiated_Function
7280 (E1, E2 : Entity_Id) return Boolean
7281 is
7282 U1, U2 : Node_Id;
7283 begin
7284 if Alias (E1) = Alias (E2) then
7285 return True;
7286
7287 elsif Present (Alias (E2)) then
7288 U1 := Original_Node (Unit_Declaration_Node (E1));
7289 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
7290
7291 return Nkind (U1) = N_Subprogram_Renaming_Declaration
7292 and then Nkind (Name (U1)) = N_Attribute_Reference
7293
7294 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
7295 and then Nkind (Name (U2)) = N_Attribute_Reference
7296
7297 and then
7298 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
7299 else
7300 return False;
7301 end if;
7302 end Same_Instantiated_Function;
7303
7304 --------------------------------
7305 -- Same_Instantiated_Variable --
7306 --------------------------------
7307
7308 function Same_Instantiated_Variable
7309 (E1, E2 : Entity_Id) return Boolean
7310 is
7311 function Original_Entity (E : Entity_Id) return Entity_Id;
7312 -- Follow chain of renamings to the ultimate ancestor
7313
7314 ---------------------
7315 -- Original_Entity --
7316 ---------------------
7317
7318 function Original_Entity (E : Entity_Id) return Entity_Id is
7319 Orig : Entity_Id;
7320
7321 begin
7322 Orig := E;
7323 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
7324 and then Present (Renamed_Object (Orig))
7325 and then Is_Entity_Name (Renamed_Object (Orig))
7326 loop
7327 Orig := Entity (Renamed_Object (Orig));
7328 end loop;
7329
7330 return Orig;
7331 end Original_Entity;
7332
7333 -- Start of processing for Same_Instantiated_Variable
7334
7335 begin
7336 return Ekind (E1) = Ekind (E2)
7337 and then Original_Entity (E1) = Original_Entity (E2);
7338 end Same_Instantiated_Variable;
7339
7340 -- Start of processing for Check_Formal_Package_Instance
7341
7342 begin
7343 Prev_E1 := E1;
7344 while Present (E1) and then Present (E2) loop
7345 exit when Ekind (E1) = E_Package
7346 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
7347
7348 -- If the formal is the renaming of the formal package, this
7349 -- is the end of its formal part, which may occur before the
7350 -- end of the formal part in the actual in the presence of
7351 -- defaulted parameters in the formal package.
7352
7353 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
7354 and then Renamed_Entity (E2) = Scope (E2);
7355
7356 -- The analysis of the actual may generate additional internal
7357 -- entities. If the formal is defaulted, there is no corresponding
7358 -- analysis and the internal entities must be skipped, until we
7359 -- find corresponding entities again.
7360
7361 if Comes_From_Source (E2)
7362 and then not Comes_From_Source (E1)
7363 and then Chars (E1) /= Chars (E2)
7364 then
7365 while Present (E1) and then Chars (E1) /= Chars (E2) loop
7366 Next_Entity (E1);
7367 end loop;
7368 end if;
7369
7370 if No (E1) then
7371 return;
7372
7373 -- Entities may be declared without full declaration, such as
7374 -- itypes and predefined operators (concatenation for arrays, eg).
7375 -- Skip it and keep the formal entity to find a later match for it.
7376
7377 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
7378 E1 := Prev_E1;
7379 goto Next_E;
7380
7381 -- If the formal entity comes from a formal declaration, it was
7382 -- defaulted in the formal package, and no check is needed on it.
7383
7384 elsif Nkind (Original_Node (Parent (E2))) in
7385 N_Formal_Object_Declaration | N_Formal_Type_Declaration
7386 then
7387 -- If the formal is a tagged type the corresponding class-wide
7388 -- type has been generated as well, and it must be skipped.
7389 -- Likewise, for a formal discrete type, the base type has been
7390 -- generated as well (see Analyze_Formal_Discrete_Type).
7391
7392 if Is_Type (E2)
7393 and then (Is_Tagged_Type (E2) or else Is_Enumeration_Type (E2))
7394 then
7395 Next_Entity (E2);
7396 end if;
7397
7398 goto Next_E;
7399
7400 -- Ditto for defaulted formal subprograms.
7401
7402 elsif Is_Overloadable (E1)
7403 and then Nkind (Unit_Declaration_Node (E2)) in
7404 N_Formal_Subprogram_Declaration
7405 then
7406 goto Next_E;
7407
7408 elsif Is_Defaulted (E1) then
7409 goto Next_E;
7410
7411 elsif Is_Type (E1) then
7412
7413 -- Subtypes must statically match. E1, E2 are the local entities
7414 -- that are subtypes of the actuals. Itypes generated for other
7415 -- parameters need not be checked, the check will be performed
7416 -- on the parameters themselves.
7417
7418 -- If E2 is a formal type declaration, it is a defaulted parameter
7419 -- and needs no checking.
7420
7421 if not Is_Itype (E1) and then not Is_Itype (E2) then
7422 Check_Mismatch
7423 (not Is_Type (E2)
7424 or else Etype (E1) /= Etype (E2)
7425 or else not Subtypes_Statically_Match (E1, E2));
7426 end if;
7427
7428 elsif Ekind (E1) = E_Constant then
7429
7430 -- IN parameters must denote the same static value, or the same
7431 -- constant, or the literal null.
7432
7433 Expr1 := Expression (Parent (E1));
7434
7435 if Ekind (E2) /= E_Constant then
7436 Check_Mismatch (True);
7437 goto Next_E;
7438 else
7439 Expr2 := Expression (Parent (E2));
7440 end if;
7441
7442 if Is_OK_Static_Expression (Expr1) then
7443 if not Is_OK_Static_Expression (Expr2) then
7444 Check_Mismatch (True);
7445
7446 elsif Is_Discrete_Type (Etype (E1)) then
7447 declare
7448 V1 : constant Uint := Expr_Value (Expr1);
7449 V2 : constant Uint := Expr_Value (Expr2);
7450 begin
7451 Check_Mismatch (V1 /= V2);
7452 end;
7453
7454 elsif Is_Real_Type (Etype (E1)) then
7455 declare
7456 V1 : constant Ureal := Expr_Value_R (Expr1);
7457 V2 : constant Ureal := Expr_Value_R (Expr2);
7458 begin
7459 Check_Mismatch (V1 /= V2);
7460 end;
7461
7462 elsif Is_String_Type (Etype (E1))
7463 and then Nkind (Expr1) = N_String_Literal
7464 then
7465 if Nkind (Expr2) /= N_String_Literal then
7466 Check_Mismatch (True);
7467 else
7468 Check_Mismatch
7469 (not String_Equal (Strval (Expr1), Strval (Expr2)));
7470 end if;
7471 end if;
7472
7473 elsif Is_Entity_Name (Expr1) then
7474 if Is_Entity_Name (Expr2) then
7475 if Entity (Expr1) = Entity (Expr2) then
7476 null;
7477 else
7478 Check_Mismatch
7479 (not Same_Instantiated_Constant
7480 (Entity (Expr1), Entity (Expr2)));
7481 end if;
7482
7483 else
7484 Check_Mismatch (True);
7485 end if;
7486
7487 elsif Is_Entity_Name (Original_Node (Expr1))
7488 and then Is_Entity_Name (Expr2)
7489 and then Same_Instantiated_Constant
7490 (Entity (Original_Node (Expr1)), Entity (Expr2))
7491 then
7492 null;
7493
7494 elsif Nkind (Expr1) = N_Null then
7495 Check_Mismatch (Nkind (Expr1) /= N_Null);
7496
7497 else
7498 Check_Mismatch (True);
7499 end if;
7500
7501 elsif Ekind (E1) = E_Variable then
7502 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
7503
7504 elsif Ekind (E1) = E_Package then
7505 Check_Mismatch
7506 (Ekind (E1) /= Ekind (E2)
7507 or else (Present (Renamed_Entity (E2))
7508 and then Renamed_Entity (E1) /=
7509 Renamed_Entity (E2)));
7510
7511 elsif Is_Overloadable (E1) then
7512 -- Verify that the actual subprograms match. Note that actuals
7513 -- that are attributes are rewritten as subprograms. If the
7514 -- subprogram in the formal package is defaulted, no check is
7515 -- needed. Note that this can only happen in Ada 2005 when the
7516 -- formal package can be partially parameterized.
7517
7518 if Nkind (Unit_Declaration_Node (E1)) =
7519 N_Subprogram_Renaming_Declaration
7520 and then From_Default (Unit_Declaration_Node (E1))
7521 then
7522 null;
7523
7524 -- If the formal package has an "others" box association that
7525 -- covers this formal, there is no need for a check either.
7526
7527 elsif Nkind (Unit_Declaration_Node (E2)) in
7528 N_Formal_Subprogram_Declaration
7529 and then Box_Present (Unit_Declaration_Node (E2))
7530 then
7531 null;
7532
7533 -- No check needed if subprogram is a defaulted null procedure
7534
7535 elsif No (Alias (E2))
7536 and then Ekind (E2) = E_Procedure
7537 and then
7538 Null_Present (Specification (Unit_Declaration_Node (E2)))
7539 then
7540 null;
7541
7542 -- Otherwise the actual in the formal and the actual in the
7543 -- instantiation of the formal must match, up to renamings.
7544
7545 else
7546 Check_Mismatch
7547 (Ekind (E2) /= Ekind (E1)
7548 or else not Same_Instantiated_Function (E1, E2));
7549 end if;
7550
7551 -- No check is needed if this is the body of a subprogram that is
7552 -- implicitly created in the case of class-wide predefined functions.
7553
7554 elsif Ekind (E1) = E_Subprogram_Body then
7555 null;
7556
7557 else
7558 raise Program_Error;
7559 end if;
7560
7561 <<Next_E>>
7562 Prev_E1 := E1;
7563 Next_Entity (E1);
7564 Next_Entity (E2);
7565 end loop;
7566 end Check_Formal_Package_Instance;
7567
7568 -------------------------------
7569 -- Check_Fixed_Point_Warning --
7570 -------------------------------
7571
7572 procedure Check_Fixed_Point_Warning
7573 (Gen_Assocs : Associations.Gen_Assocs_Rec;
7574 Renamings : List_Id)
7575 is
7576 use Associations;
7577 begin
7578 for Type_Index in Gen_Assocs.Assocs'Range loop
7579 declare
7580 Assoc : Assoc_Rec renames Gen_Assocs.Assocs (Type_Index);
7581 begin
7582 if Nkind (Assoc.An_Formal) = N_Formal_Type_Declaration
7583 and then Is_Fixed_Point_Type (Defining_Entity (Assoc.An_Formal))
7584 and then Assoc.Actual.Kind = Name_Exp
7585 then
7586 declare
7587 Typ : constant Entity_Id := Entity (Assoc.Actual.Name_Exp);
7588 pragma Assert (Is_Fixed_Point_Type (Typ));
7589
7590 Prims : constant Elist_Id :=
7591 Collect_Primitive_Operations (Typ);
7592 Elem : Elmt_Id := First_Elmt (Prims);
7593 Formal : Node_Id;
7594 Op : Entity_Id;
7595 begin
7596 -- Locate primitive operations of the type that are
7597 -- arithmetic operations.
7598
7599 while Present (Elem) loop
7600 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
7601
7602 -- Check whether the generic unit has a formal
7603 -- subprogram of the same name. This does not check
7604 -- types but is good enough to justify a warning.
7605
7606 Op := Alias (Node (Elem));
7607
7608 for Op_Index in Type_Index + 1 ..
7609 Gen_Assocs.Assocs'Last
7610 loop
7611 Formal := Gen_Assocs.Assocs (Op_Index).Un_Formal;
7612
7613 if Nkind (Formal) =
7614 N_Formal_Concrete_Subprogram_Declaration
7615 and then Chars (Defining_Entity (Formal)) =
7616 Chars (Node (Elem))
7617 then
7618 goto OK;
7619
7620 elsif Nkind (Formal) = N_Formal_Package_Declaration
7621 then
7622 declare
7623 Assoc : Node_Id;
7624 Ent : Entity_Id;
7625
7626 begin
7627 -- Locate corresponding actual, and check
7628 -- whether it includes a fixed-point type.
7629
7630 Assoc := First (Renamings);
7631 while Present (Assoc) loop
7632 exit when
7633 Nkind (Assoc) =
7634 N_Package_Renaming_Declaration
7635 and then
7636 Chars (Defining_Unit_Name (Assoc)) =
7637 Chars (Defining_Identifier (Formal));
7638
7639 Next (Assoc);
7640 end loop;
7641
7642 if Present (Assoc) then
7643 -- If the formal package declares a
7644 -- fixed-point type, and the user-defined
7645 -- operator is derived from a generic
7646 -- instance package, the fixed-point type
7647 -- does not use the corresponding
7648 -- predefined op.
7649
7650 Ent :=
7651 First_Entity (Entity (Name (Assoc)));
7652 while Present (Ent) loop
7653 if Is_Fixed_Point_Type (Ent)
7654 and then Present (Op)
7655 and then
7656 Is_Generic_Instance (Scope (Op))
7657 then
7658 goto OK;
7659 end if;
7660
7661 Next_Entity (Ent);
7662 end loop;
7663 end if;
7664 end;
7665 end if;
7666 end loop;
7667
7668 Error_Msg_Sloc := Sloc (Node (Elem));
7669 Error_Msg_NE
7670 ("?instance uses predefined, not primitive, " &
7671 "operator&#",
7672 Assoc.Actual.Name_Exp, Node (Elem));
7673 <<OK>> null;
7674 end if;
7675
7676 Next_Elmt (Elem);
7677 end loop;
7678 end;
7679 end if;
7680 end;
7681 end loop;
7682 end Check_Fixed_Point_Warning;
7683
7684 ---------------------------
7685 -- Check_Formal_Packages --
7686 ---------------------------
7687
7688 procedure Check_Formal_Packages (P_Id : Entity_Id) is
7689 E : Entity_Id;
7690 Formal_P : Entity_Id;
7691 Formal_Decl : Node_Id;
7692
7693 begin
7694 -- Iterate through the declarations in the instance, looking for package
7695 -- renaming declarations that denote instances of formal packages, until
7696 -- we find the renaming of the current package itself. The declaration
7697 -- of a formal package that requires conformance checking is followed by
7698 -- an internal entity that is the abbreviated instance.
7699
7700 E := First_Entity (P_Id);
7701 while Present (E) loop
7702 if Ekind (E) = E_Package then
7703 exit when Renamed_Entity (E) = P_Id;
7704
7705 if Nkind (Parent (E)) = N_Package_Renaming_Declaration then
7706 Formal_Decl := Parent (Associated_Formal_Package (E));
7707
7708 if Requires_Conformance_Checking (Formal_Decl) then
7709 Formal_P := Next_Entity (E);
7710
7711 -- If the instance is within an enclosing instance body
7712 -- there is no need to verify the legality of current formal
7713 -- packages because they were legal in the generic body.
7714 -- This optimization may be applicable elsewhere, and it
7715 -- also removes spurious errors that may arise with
7716 -- on-the-fly inlining and confusion between private and
7717 -- full views.
7718
7719 if not In_Instance_Body then
7720 Check_Formal_Package_Instance (Formal_P, E);
7721 end if;
7722
7723 -- Restore the visibility of formals of the formal instance
7724 -- that are not defaulted, and are hidden within the current
7725 -- generic. These formals may be visible within an enclosing
7726 -- generic.
7727
7728 declare
7729 Elmt : Elmt_Id;
7730 begin
7731 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
7732 while Present (Elmt) loop
7733 Set_Is_Hidden (Node (Elmt), False);
7734 Next_Elmt (Elmt);
7735 end loop;
7736 end;
7737
7738 -- After checking, remove the internal validating package.
7739 -- It is only needed for semantic checks, and as it may
7740 -- contain generic formal declarations it should not reach
7741 -- gigi.
7742
7743 Remove (Unit_Declaration_Node (Formal_P));
7744 end if;
7745 end if;
7746 end if;
7747
7748 Next_Entity (E);
7749 end loop;
7750 end Check_Formal_Packages;
7751
7752 ---------------------------------
7753 -- Check_Forward_Instantiation --
7754 ---------------------------------
7755
7756 procedure Check_Forward_Instantiation (Decl : Node_Id) is
7757 S : Entity_Id;
7758 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
7759
7760 begin
7761 -- The instantiation appears before the generic body if we are in the
7762 -- scope of the unit containing the generic, either in its spec or in
7763 -- the package body, and before the generic body.
7764
7765 if Ekind (Gen_Comp) = E_Package_Body then
7766 Gen_Comp := Spec_Entity (Gen_Comp);
7767 end if;
7768
7769 if In_Open_Scopes (Gen_Comp)
7770 and then No (Corresponding_Body (Decl))
7771 then
7772 S := Current_Scope;
7773
7774 while Present (S)
7775 and then not Is_Compilation_Unit (S)
7776 and then not Is_Child_Unit (S)
7777 loop
7778 if Ekind (S) = E_Package then
7779 Set_Has_Forward_Instantiation (S);
7780 end if;
7781
7782 S := Scope (S);
7783 end loop;
7784 end if;
7785 end Check_Forward_Instantiation;
7786
7787 ---------------------------
7788 -- Check_Generic_Actuals --
7789 ---------------------------
7790
7791 -- The visibility of the actuals may be different between the point of
7792 -- generic instantiation and the instantiation of the body.
7793
7794 procedure Check_Generic_Actuals
7795 (Instance : Entity_Id;
7796 Is_Formal_Box : Boolean)
7797 is
7798 Gen_Id : constant Entity_Id :=
7799 (if Is_Generic_Unit (Instance) then
7800 Instance
7801 elsif Is_Wrapper_Package (Instance) then
7802 Generic_Parent
7803 (Specification
7804 (Unit_Declaration_Node (Related_Instance (Instance))))
7805 else
7806 Generic_Parent (Package_Specification (Instance)));
7807 -- The generic unit
7808
7809 Parent_Scope : constant Entity_Id := Scope (Gen_Id);
7810 -- The enclosing scope of the generic unit
7811
7812 procedure Check_Actual_Type (Typ : Entity_Id);
7813 -- If the type of the actual is a private type declared in the enclosing
7814 -- scope of the generic, either directly or through packages nested in
7815 -- bodies, but not a derived type of a private type declared elsewhere,
7816 -- then the body of the generic sees the full view of the type because
7817 -- it has to appear in the package body. If the type is private now then
7818 -- exchange views to restore the proper visibility in the instance.
7819
7820 -----------------------
7821 -- Check_Actual_Type --
7822 -----------------------
7823
7824 procedure Check_Actual_Type (Typ : Entity_Id) is
7825 Btyp : constant Entity_Id := Base_Type (Typ);
7826
7827 function Scope_Within_Body_Or_Same
7828 (Inner : Entity_Id;
7829 Outer : Entity_Id) return Boolean;
7830 -- Determine whether scope Inner is within the body of scope Outer
7831 -- or is Outer itself.
7832
7833 -------------------------------
7834 -- Scope_Within_Body_Or_Same --
7835 -------------------------------
7836
7837 function Scope_Within_Body_Or_Same
7838 (Inner : Entity_Id;
7839 Outer : Entity_Id) return Boolean
7840 is
7841 Curr : Entity_Id := Inner;
7842
7843 begin
7844 while Curr /= Standard_Standard loop
7845 if Curr = Outer then
7846 return True;
7847
7848 elsif Is_Package_Body_Entity (Curr) then
7849 Curr := Scope (Curr);
7850
7851 else
7852 exit;
7853 end if;
7854 end loop;
7855
7856 return False;
7857 end Scope_Within_Body_Or_Same;
7858
7859 -- Start of processing for Check_Actual_Type
7860
7861 begin
7862 -- The exchange is only needed if the generic is defined
7863 -- within a package which is not a common ancestor of the
7864 -- scope of the instance, and is not already in scope.
7865
7866 if Is_Private_Type (Btyp)
7867 and then not Has_Private_Ancestor (Btyp)
7868 and then Ekind (Parent_Scope) in E_Package | E_Generic_Package
7869 and then Scope_Within_Body_Or_Same (Parent_Scope, Scope (Btyp))
7870 and then Parent_Scope /= Scope (Instance)
7871 and then not Is_Child_Unit (Gen_Id)
7872 then
7873 Switch_View (Btyp);
7874
7875 -- If the type of the entity is a subtype, it may also have
7876 -- to be made visible, together with the base type of its
7877 -- full view, after exchange.
7878
7879 if Is_Private_Type (Typ) then
7880 Switch_View (Typ);
7881 Switch_View (Base_Type (Typ));
7882 end if;
7883 end if;
7884 end Check_Actual_Type;
7885
7886 -- Local variables
7887
7888 Astype : Entity_Id;
7889 E : Entity_Id;
7890 Formal : Node_Id;
7891
7892 -- Start of processing for Check_Generic_Actuals
7893
7894 begin
7895 E := First_Entity (Instance);
7896 while Present (E) loop
7897 if Is_Type (E)
7898 and then Nkind (Parent (E)) = N_Subtype_Declaration
7899 and then Scope (Etype (E)) /= Instance
7900 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7901 then
7902 declare
7903 Indic : constant Node_Id := Subtype_Indication (Parent (E));
7904
7905 begin
7906 -- Restore the proper view of the actual from the information
7907 -- saved earlier by Instantiate_Type.
7908
7909 Check_Private_View (Indic);
7910
7911 -- If this view is an array type, check its component type.
7912 -- This handles the case of an array type whose component
7913 -- type is private, used as the actual in an instantiation
7914 -- of a generic construct declared in the same package as
7915 -- the component type and taking an array type with this
7916 -- component type as formal type parameter.
7917
7918 if Is_Array_Type (Etype (Indic)) then
7919 Check_Actual_Type
7920 (Component_Type_For_Private_View (Etype (Indic)));
7921 end if;
7922 end;
7923
7924 -- If the actual is itself the formal of a parent instance,
7925 -- then also restore the proper view of its actual and so on.
7926 -- That's necessary for nested instantiations of the form
7927
7928 -- generic
7929 -- type Component is private;
7930 -- type Array_Type is array (Positive range <>) of Component;
7931 -- procedure Proc;
7932
7933 -- when the outermost actuals have inconsistent views, because
7934 -- the Component_Type of Array_Type of the inner instantiations
7935 -- is the actual of Component of the outermost one and not that
7936 -- of the corresponding inner instantiations.
7937
7938 Astype := Ancestor_Subtype (E);
7939 while Present (Astype)
7940 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7941 and then Present (Generic_Parent_Type (Parent (Astype)))
7942 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7943 loop
7944 Check_Private_View (Subtype_Indication (Parent (Astype)));
7945 Astype := Ancestor_Subtype (Astype);
7946 end loop;
7947
7948 Set_Is_Generic_Actual_Type (E);
7949
7950 if Is_Private_Type (E) and then Present (Full_View (E)) then
7951 Set_Is_Generic_Actual_Type (Full_View (E));
7952 end if;
7953
7954 Set_Is_Hidden (E, False);
7955 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7956
7957 -- We constructed the generic actual type as a subtype of the
7958 -- supplied type. This means that it normally would not inherit
7959 -- subtype specific attributes of the actual, which is wrong for
7960 -- the generic case.
7961
7962 Astype := Ancestor_Subtype (E);
7963
7964 if No (Astype) then
7965
7966 -- This can happen when E is an itype that is the full view of
7967 -- a private type completed, e.g. with a constrained array. In
7968 -- that case, use the first subtype, which will carry size
7969 -- information. The base type itself is unconstrained and will
7970 -- not carry it.
7971
7972 Astype := First_Subtype (E);
7973 end if;
7974
7975 Set_Size_Info (E, Astype);
7976 Copy_RM_Size (To => E, From => Astype);
7977 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7978
7979 if Is_Discrete_Or_Fixed_Point_Type (E) then
7980 Set_RM_Size (E, RM_Size (Astype));
7981 end if;
7982
7983 elsif Ekind (E) = E_Package then
7984
7985 -- If this is the renaming for the current instance, we're done.
7986 -- Otherwise it is a formal package. If the corresponding formal
7987 -- was declared with a box, the (instantiations of the) generic
7988 -- formal part are also visible. Otherwise, ignore the entity
7989 -- created to validate the actuals.
7990
7991 if Renamed_Entity (E) = Instance then
7992 exit;
7993
7994 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7995 null;
7996
7997 -- The visibility of a formal of an enclosing generic is already
7998 -- correct.
7999
8000 elsif Denotes_Formal_Package (E) then
8001 null;
8002
8003 elsif Present (Associated_Formal_Package (E))
8004 and then not Is_Generic_Formal (E)
8005 then
8006 Check_Generic_Actuals
8007 (Renamed_Entity (E),
8008 Is_Formal_Box =>
8009 Box_Present (Parent (Associated_Formal_Package (E))));
8010
8011 Set_Is_Hidden (E, False);
8012 end if;
8013
8014 -- If this is a subprogram instance (in a wrapper package) the
8015 -- actual is fully visible.
8016
8017 elsif Is_Wrapper_Package (Instance) then
8018 Set_Is_Hidden (E, False);
8019
8020 -- If the formal package is declared with a box, or if the formal
8021 -- parameter is defaulted, it is visible in the body.
8022
8023 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
8024 Set_Is_Hidden (E, False);
8025 end if;
8026
8027 -- Check directly the type of the actual objects, including the
8028 -- component type for array types.
8029
8030 if Ekind (E) in E_Constant | E_Variable then
8031 Check_Actual_Type (Etype (E));
8032
8033 if Is_Array_Type (Etype (E)) then
8034 Check_Actual_Type (Component_Type (Etype (E)));
8035 end if;
8036
8037 -- As well as the type of formal parameters of actual subprograms
8038
8039 elsif Ekind (E) in E_Function | E_Procedure
8040 and then Is_Generic_Actual_Subprogram (E)
8041 and then Present (Alias (E))
8042 then
8043 Formal := First_Formal (Alias (E));
8044 while Present (Formal) loop
8045 Check_Actual_Type (Etype (Formal));
8046 Next_Formal (Formal);
8047 end loop;
8048 end if;
8049
8050 Next_Entity (E);
8051 end loop;
8052 end Check_Generic_Actuals;
8053
8054 ------------------------------
8055 -- Check_Generic_Child_Unit --
8056 ------------------------------
8057
8058 procedure Check_Generic_Child_Unit
8059 (Gen_Id : Node_Id;
8060 Parent_Installed : in out Boolean)
8061 is
8062 Loc : constant Source_Ptr := Sloc (Gen_Id);
8063 Gen_Par : Entity_Id := Empty;
8064 E : Entity_Id;
8065 Inst_Par : Entity_Id := Empty;
8066 S : Node_Id;
8067
8068 function Find_Generic_Child
8069 (Scop : Entity_Id;
8070 Id : Node_Id) return Entity_Id;
8071 -- Search generic parent for possible child unit with the given name
8072
8073 function In_Enclosing_Instance return Boolean;
8074 -- Within an instance of the parent, the child unit may be denoted by
8075 -- a simple name, or an abbreviated expanded name. Examine enclosing
8076 -- scopes to locate a possible parent instantiation.
8077
8078 ------------------------
8079 -- Find_Generic_Child --
8080 ------------------------
8081
8082 function Find_Generic_Child
8083 (Scop : Entity_Id;
8084 Id : Node_Id) return Entity_Id
8085 is
8086 E : Entity_Id;
8087
8088 begin
8089 -- If entity of name is already set, instance has already been
8090 -- resolved, e.g. in an enclosing instantiation.
8091
8092 if Present (Entity (Id)) then
8093 if Scope (Entity (Id)) = Scop then
8094 return Entity (Id);
8095 else
8096 return Empty;
8097 end if;
8098
8099 else
8100 E := First_Entity (Scop);
8101 while Present (E) loop
8102 if Chars (E) = Chars (Id)
8103 and then Is_Child_Unit (E)
8104 then
8105 if Is_Child_Unit (E)
8106 and then not Is_Visible_Lib_Unit (E)
8107 then
8108 Error_Msg_NE
8109 ("generic child unit& is not visible", Gen_Id, E);
8110 end if;
8111
8112 Set_Entity (Id, E);
8113 return E;
8114 end if;
8115
8116 Next_Entity (E);
8117 end loop;
8118
8119 return Empty;
8120 end if;
8121 end Find_Generic_Child;
8122
8123 ---------------------------
8124 -- In_Enclosing_Instance --
8125 ---------------------------
8126
8127 function In_Enclosing_Instance return Boolean is
8128 Enclosing_Instance : Node_Id;
8129 Instance_Decl : Node_Id;
8130
8131 begin
8132 -- We do not inline any call that contains instantiations, except
8133 -- for instantiations of Unchecked_Conversion, so if we are within
8134 -- an inlined body the current instance does not require parents.
8135
8136 if In_Inlined_Body then
8137 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
8138 return False;
8139 end if;
8140
8141 -- Loop to check enclosing scopes
8142
8143 Enclosing_Instance := Current_Scope;
8144 while Present (Enclosing_Instance) loop
8145 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
8146
8147 if Ekind (Enclosing_Instance) = E_Package
8148 and then Is_Generic_Instance (Enclosing_Instance)
8149 and then Present
8150 (Generic_Parent (Specification (Instance_Decl)))
8151 then
8152 -- Check whether the generic we are looking for is a child of
8153 -- this instance.
8154
8155 E := Find_Generic_Child
8156 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
8157 exit when Present (E);
8158
8159 else
8160 E := Empty;
8161 end if;
8162
8163 Enclosing_Instance := Scope (Enclosing_Instance);
8164 end loop;
8165
8166 if No (E) then
8167
8168 -- Not a child unit
8169
8170 Analyze (Gen_Id);
8171 return False;
8172
8173 else
8174 Rewrite (Gen_Id,
8175 Make_Expanded_Name (Loc,
8176 Chars => Chars (E),
8177 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
8178 Selector_Name => New_Occurrence_Of (E, Loc)));
8179
8180 Set_Entity (Gen_Id, E);
8181 Set_Etype (Gen_Id, Etype (E));
8182 Parent_Installed := False; -- Already in scope.
8183 return True;
8184 end if;
8185 end In_Enclosing_Instance;
8186
8187 -- Start of processing for Check_Generic_Child_Unit
8188
8189 begin
8190 -- If the name of the generic is given by a selected component, it may
8191 -- be the name of a generic child unit, and the prefix is the name of an
8192 -- instance of the parent, in which case the child unit must be visible.
8193 -- If this instance is not in scope, it must be placed there and removed
8194 -- after instantiation, because what is being instantiated is not the
8195 -- original child, but the corresponding child present in the instance
8196 -- of the parent.
8197
8198 -- If the child is instantiated within the parent, it can be given by
8199 -- a simple name. In this case the instance is already in scope, but
8200 -- the child generic must be recovered from the generic parent as well.
8201
8202 if Nkind (Gen_Id) = N_Selected_Component then
8203 S := Selector_Name (Gen_Id);
8204 Analyze (Prefix (Gen_Id));
8205 Inst_Par := Entity (Prefix (Gen_Id));
8206
8207 if Ekind (Inst_Par) = E_Package
8208 and then Present (Renamed_Entity (Inst_Par))
8209 then
8210 Inst_Par := Renamed_Entity (Inst_Par);
8211 end if;
8212
8213 if Ekind (Inst_Par) = E_Package then
8214 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
8215 Gen_Par := Generic_Parent (Parent (Inst_Par));
8216
8217 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
8218 and then
8219 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
8220 then
8221 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
8222 end if;
8223
8224 elsif Ekind (Inst_Par) = E_Generic_Package
8225 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
8226 then
8227 -- A formal package may be a real child package, and not the
8228 -- implicit instance within a parent. In this case the child is
8229 -- not visible and has to be retrieved explicitly as well.
8230
8231 Gen_Par := Inst_Par;
8232 end if;
8233
8234 if Present (Gen_Par) then
8235
8236 -- The prefix denotes an instantiation. The entity itself may be a
8237 -- nested generic, or a child unit.
8238
8239 E := Find_Generic_Child (Gen_Par, S);
8240
8241 if Present (E) then
8242 Change_Selected_Component_To_Expanded_Name (Gen_Id);
8243 Set_Entity (Gen_Id, E);
8244 Set_Etype (Gen_Id, Etype (E));
8245 Set_Entity (S, E);
8246 Set_Etype (S, Etype (E));
8247
8248 -- Indicate that this is a reference to the parent
8249
8250 if In_Extended_Main_Source_Unit (Gen_Id) then
8251 Set_Is_Instantiated (Inst_Par);
8252 end if;
8253
8254 -- A common mistake is to replicate the naming scheme of a
8255 -- hierarchy by instantiating a generic child directly, rather
8256 -- than the implicit child in a parent instance:
8257
8258 -- generic .. package Gpar is ..
8259 -- generic .. package Gpar.Child is ..
8260 -- package Par is new Gpar ();
8261
8262 -- with Gpar.Child;
8263 -- package Par.Child is new Gpar.Child ();
8264 -- rather than Par.Child
8265
8266 -- In this case the instantiation is within Par, which is an
8267 -- instance, but Gpar does not denote Par because we are not IN
8268 -- the instance of Gpar, so this is illegal. The test below
8269 -- recognizes this particular case.
8270
8271 declare
8272 -- We want to reject the final instantiation in
8273 -- generic package G1 is end G1;
8274 -- generic package G1.G2 is end G1.G2;
8275 -- with G1; package I1 is new G1;
8276 -- with G1.G2; package I1.I2 is new G1.G2;
8277 -- because the use of G1.G2 should instead be either
8278 -- I1.G2 or simply G2. However, the tree that is built
8279 -- in this case is wrong. In the expanded copy
8280 -- of G2, we need (and therefore generate) a renaming
8281 -- package G1 renames I1;
8282 -- but this renaming should not participate in resolving
8283 -- this occurrence of the name "G1.G2"; unfortunately,
8284 -- it does. Rather than correct this error, we compensate
8285 -- for it in this function.
8286 --
8287 -- We also perform another adjustment here. If we are
8288 -- currently inside a generic package, then that
8289 -- generic package needs to be treated as a package.
8290 -- For example, if a generic Aaa declares a nested generic
8291 -- Bbb (perhaps as a child unit) then Aaa can also legally
8292 -- declare an instance of Aaa.Bbb.
8293
8294 function Adjusted_Inst_Par_Ekind return Entity_Kind;
8295
8296 -----------------------------
8297 -- Adjusted_Inst_Par_Ekind --
8298 -----------------------------
8299
8300 function Adjusted_Inst_Par_Ekind return Entity_Kind is
8301 Prefix_Entity : Entity_Id;
8302 Inst_Par_GP : Node_Id;
8303 Inst_Par_Parent : Node_Id := Parent (Inst_Par);
8304 begin
8305 if Nkind (Inst_Par_Parent) = N_Defining_Program_Unit_Name
8306 then
8307 Inst_Par_Parent := Parent (Inst_Par_Parent);
8308 end if;
8309
8310 Inst_Par_GP := Generic_Parent (Inst_Par_Parent);
8311
8312 if Nkind (Gen_Id) = N_Expanded_Name
8313 and then Present (Inst_Par_GP)
8314 and then Ekind (Inst_Par_GP) = E_Generic_Package
8315 then
8316 Prefix_Entity := Entity (Prefix (Gen_Id));
8317
8318 if Present (Prefix_Entity)
8319 and then not Comes_From_Source (Prefix_Entity)
8320 and then Nkind (Parent (Prefix_Entity)) =
8321 N_Package_Renaming_Declaration
8322 and then Chars (Prefix_Entity) = Chars (Inst_Par_GP)
8323 then
8324 return E_Generic_Package;
8325 end if;
8326 end if;
8327
8328 if Ekind (Inst_Par) = E_Generic_Package
8329 and then In_Open_Scopes (Inst_Par)
8330 then
8331 -- If we are inside a generic package then
8332 -- treat it as a package.
8333 return E_Package;
8334 end if;
8335
8336 -- The usual path
8337 return Ekind (Inst_Par);
8338 end Adjusted_Inst_Par_Ekind;
8339
8340 begin
8341 if Is_Child_Unit (E)
8342 and then (No (Inst_Par)
8343 or else Adjusted_Inst_Par_Ekind =
8344 E_Generic_Package)
8345 and then (not In_Instance
8346 or else Nkind (Parent (Parent (Gen_Id))) =
8347 N_Compilation_Unit)
8348 then
8349 Error_Msg_N
8350 ("prefix of generic child unit must be " &
8351 "instance of parent",
8352 Gen_Id);
8353 end if;
8354 end;
8355
8356 if not In_Open_Scopes (Inst_Par)
8357 and then Nkind (Parent (Gen_Id)) not in
8358 N_Generic_Renaming_Declaration
8359 then
8360 Install_Parent (Inst_Par);
8361 Parent_Installed := True;
8362
8363 elsif In_Open_Scopes (Inst_Par) then
8364
8365 -- If the parent is already installed, install the actuals
8366 -- for its formal packages. This is necessary when the child
8367 -- instance is a child of the parent instance: in this case,
8368 -- the parent is placed on the scope stack but the formal
8369 -- packages are not made visible.
8370
8371 Install_Formal_Packages (Inst_Par);
8372 end if;
8373
8374 else
8375 -- If the generic parent does not contain an entity that
8376 -- corresponds to the selector, the instance doesn't either.
8377 -- Analyzing the node will yield the appropriate error message.
8378 -- If the entity is not a child unit, then it is an inner
8379 -- generic in the parent.
8380
8381 Analyze (Gen_Id);
8382 end if;
8383
8384 else
8385 Analyze (Gen_Id);
8386
8387 if Is_Child_Unit (Entity (Gen_Id))
8388 and then
8389 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
8390 and then not In_Open_Scopes (Inst_Par)
8391 then
8392 Install_Parent (Inst_Par);
8393 Parent_Installed := True;
8394
8395 -- The generic unit may be the renaming of the implicit child
8396 -- present in an instance. In that case the parent instance is
8397 -- obtained from the name of the renamed entity.
8398
8399 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
8400 and then Present (Renamed_Entity (Entity (Gen_Id)))
8401 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
8402 then
8403 declare
8404 Renamed_Package : constant Node_Id :=
8405 Name (Parent (Entity (Gen_Id)));
8406 begin
8407 if Nkind (Renamed_Package) = N_Expanded_Name then
8408 Inst_Par := Entity (Prefix (Renamed_Package));
8409 Install_Parent (Inst_Par);
8410 Parent_Installed := True;
8411 end if;
8412 end;
8413 end if;
8414 end if;
8415
8416 elsif Nkind (Gen_Id) = N_Expanded_Name then
8417
8418 -- Entity already present, analyze prefix, whose meaning may be an
8419 -- instance in the current context. If it is an instance of a
8420 -- relative within another, the proper parent may still have to be
8421 -- installed, if they are not of the same generation.
8422
8423 Analyze (Prefix (Gen_Id));
8424
8425 -- Prevent cascaded errors
8426
8427 if Etype (Prefix (Gen_Id)) = Any_Type then
8428 return;
8429 end if;
8430
8431 -- In the unlikely case that a local declaration hides the name of
8432 -- the parent package, locate it on the homonym chain. If the context
8433 -- is an instance of the parent, the renaming entity is flagged as
8434 -- such.
8435
8436 Inst_Par := Entity (Prefix (Gen_Id));
8437 while Present (Inst_Par)
8438 and then not Is_Package_Or_Generic_Package (Inst_Par)
8439 loop
8440 Inst_Par := Homonym (Inst_Par);
8441 end loop;
8442
8443 pragma Assert (Present (Inst_Par));
8444 Set_Entity (Prefix (Gen_Id), Inst_Par);
8445
8446 if In_Enclosing_Instance then
8447 null;
8448
8449 elsif Present (Entity (Gen_Id))
8450 and then No (Renamed_Entity (Entity (Gen_Id)))
8451 and then Is_Child_Unit (Entity (Gen_Id))
8452 and then not In_Open_Scopes (Inst_Par)
8453 then
8454 Install_Parent (Inst_Par);
8455 Parent_Installed := True;
8456
8457 -- Handle renaming of generic child unit
8458
8459 elsif Present (Entity (Gen_Id))
8460 and then Present (Renamed_Entity (Entity (Gen_Id)))
8461 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
8462 then
8463 declare
8464 E : Entity_Id;
8465 Ren_Decl : Node_Id;
8466
8467 begin
8468 -- The entity of the renamed generic child unit does not
8469 -- have any reference to the instantiated parent. In order to
8470 -- locate it we traverse the scope containing the renaming
8471 -- declaration; the instance of the parent is available in
8472 -- the prefix of the renaming declaration. For example:
8473
8474 -- package A is
8475 -- package Inst_Par is new ...
8476 -- generic package Ren_Child renames Ins_Par.Child;
8477 -- end;
8478
8479 -- with A;
8480 -- package B is
8481 -- package Inst_Child is new A.Ren_Child;
8482 -- end;
8483
8484 E := First_Entity (Entity (Prefix (Gen_Id)));
8485 while Present (E) loop
8486 if not Is_Object (E)
8487 and then Present (Renamed_Entity (E))
8488 and then
8489 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
8490 then
8491 Ren_Decl := Parent (E);
8492 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
8493
8494 if not In_Open_Scopes (Inst_Par) then
8495 Install_Parent (Inst_Par);
8496 Parent_Installed := True;
8497 end if;
8498
8499 exit;
8500 end if;
8501
8502 E := Next_Entity (E);
8503 end loop;
8504 end;
8505 end if;
8506
8507 elsif In_Enclosing_Instance then
8508
8509 -- The child unit is found in some enclosing scope
8510
8511 null;
8512
8513 else
8514 Analyze (Gen_Id);
8515
8516 -- If this is the renaming of the implicit child in a parent
8517 -- instance, recover the parent name and install it.
8518
8519 if Is_Entity_Name (Gen_Id) then
8520 E := Entity (Gen_Id);
8521
8522 if Is_Generic_Unit (E)
8523 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
8524 and then Is_Child_Unit (Renamed_Entity (E))
8525 and then Is_Generic_Unit (Scope (Renamed_Entity (E)))
8526 and then Nkind (Name (Parent (E))) = N_Expanded_Name
8527 then
8528 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
8529 Inst_Par := Entity (Prefix (Gen_Id));
8530
8531 if not In_Open_Scopes (Inst_Par) then
8532 Install_Parent (Inst_Par);
8533 Parent_Installed := True;
8534 end if;
8535
8536 -- If it is a child unit of a non-generic parent, it may be
8537 -- use-visible and given by a direct name. Install parent as
8538 -- for other cases.
8539
8540 elsif Is_Generic_Unit (E)
8541 and then Is_Child_Unit (E)
8542 and then
8543 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
8544 and then not Is_Generic_Unit (Scope (E))
8545 then
8546 if not In_Open_Scopes (Scope (E)) then
8547 Install_Parent (Scope (E));
8548 Parent_Installed := True;
8549 end if;
8550 end if;
8551 end if;
8552 end if;
8553 end Check_Generic_Child_Unit;
8554
8555 -----------------------------
8556 -- Check_Hidden_Child_Unit --
8557 -----------------------------
8558
8559 procedure Check_Hidden_Child_Unit
8560 (N : Node_Id;
8561 Gen_Unit : Entity_Id;
8562 Act_Decl_Id : Entity_Id)
8563 is
8564 Gen_Id : constant Node_Id := Name (N);
8565
8566 begin
8567 if Is_Child_Unit (Gen_Unit)
8568 and then Is_Child_Unit (Act_Decl_Id)
8569 and then Nkind (Gen_Id) = N_Expanded_Name
8570 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
8571 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
8572 then
8573 Error_Msg_Node_2 := Scope (Act_Decl_Id);
8574 Error_Msg_NE
8575 ("generic unit & is implicitly declared in &",
8576 Defining_Unit_Name (N), Gen_Unit);
8577 Error_Msg_N ("\instance must have different name",
8578 Defining_Unit_Name (N));
8579 end if;
8580 end Check_Hidden_Child_Unit;
8581
8582 ------------------------
8583 -- Check_Private_View --
8584 ------------------------
8585
8586 procedure Check_Private_View (N : Node_Id) is
8587 Comparison : constant Boolean := Nkind (N) in N_Op_Compare;
8588 Typ : constant Entity_Id :=
8589 (if Comparison then Compare_Type (N) else Etype (N));
8590
8591 procedure Check_Private_Type (T : Entity_Id; Private_View : Boolean);
8592 -- Check that the available view of T matches Private_View and, if not,
8593 -- switch the view of T or of its base type.
8594
8595 procedure Check_Private_Type (T : Entity_Id; Private_View : Boolean) is
8596 BT : constant Entity_Id := Base_Type (T);
8597
8598 begin
8599 -- If the full declaration was not visible in the generic, stop here
8600
8601 if Private_View then
8602 return;
8603 end if;
8604
8605 -- Exchange views if the type was not private in the generic but is
8606 -- private at the point of instantiation. Do not exchange views if
8607 -- the scope of the type is in scope. This can happen if both generic
8608 -- and instance are sibling units, or if type is defined in a parent.
8609 -- In this case the visibility of the type will be correct for all
8610 -- semantic checks.
8611
8612 if Is_Private_Type (T)
8613 and then Present (Full_View (T))
8614 and then not In_Open_Scopes (Scope (T))
8615 then
8616 Switch_View (T);
8617
8618 -- Finally, a nonprivate subtype may have a private base type, which
8619 -- must be exchanged for consistency. This can happen when a package
8620 -- body is instantiated, when the scope stack is empty but in fact
8621 -- the subtype and the base type are declared in an enclosing scope.
8622
8623 -- Note that in this case we introduce an inconsistency in the view
8624 -- set, because we switch the base type BT, but there could be some
8625 -- private dependent subtypes of BT which remain unswitched. Such
8626 -- subtypes might need to be switched at a later point (see specific
8627 -- provision for that case in Switch_View).
8628
8629 elsif not Is_Private_Type (T)
8630 and then Is_Private_Type (BT)
8631 and then Present (Full_View (BT))
8632 and then not In_Open_Scopes (BT)
8633 then
8634 Prepend_Elmt (Full_View (BT), Exchanged_Views);
8635 Exchange_Declarations (BT);
8636 end if;
8637 end Check_Private_Type;
8638
8639 -- Start of processing for Check_Private_View
8640
8641 begin
8642 if Present (Typ) then
8643 -- If the type appears in a subtype declaration, the subtype in
8644 -- instance must have a view compatible with that of its parent,
8645 -- which must be exchanged (see corresponding code in Restore_
8646 -- Private_Views) so we make an exception to the open scope rule
8647 -- implemented by Check_Private_Type above.
8648
8649 if Has_Private_View (N)
8650 and then not Is_Private_Type (Typ)
8651 and then not Has_Been_Exchanged (Typ)
8652 and then (not In_Open_Scopes (Scope (Typ))
8653 or else Nkind (Parent (N)) = N_Subtype_Declaration)
8654 then
8655 declare
8656 Assoc : constant Node_Id := Get_Associated_Node (N);
8657
8658 begin
8659 -- In the generic, only the private declaration was visible
8660
8661 Prepend_Elmt (Typ, Exchanged_Views);
8662 Exchange_Declarations
8663 (if Comparison then Compare_Type (Assoc) else Etype (Assoc));
8664 end;
8665
8666 -- Check that the available views of Typ match their respective flag.
8667 -- Note that the type of a visible discriminant is never private.
8668
8669 else
8670 Check_Private_Type (Typ, Has_Private_View (N));
8671
8672 if Is_Access_Type (Typ) then
8673 Check_Private_Type
8674 (Designated_Type (Typ), Has_Secondary_Private_View (N));
8675
8676 elsif Is_Array_Type (Typ) then
8677 Check_Private_Type
8678 (Component_Type_For_Private_View (Typ),
8679 Has_Secondary_Private_View (N));
8680
8681 elsif (Is_Record_Type (Typ) or else Is_Concurrent_Type (Typ))
8682 and then Has_Discriminants (Typ)
8683 then
8684 declare
8685 Disc : Entity_Id;
8686
8687 begin
8688 Disc := First_Discriminant (Typ);
8689 while Present (Disc) loop
8690 Check_Private_Type (Etype (Disc), False);
8691 Next_Discriminant (Disc);
8692 end loop;
8693 end;
8694 end if;
8695 end if;
8696 end if;
8697 end Check_Private_View;
8698
8699 -----------------------------
8700 -- Check_Hidden_Primitives --
8701 -----------------------------
8702
8703 function Check_Hidden_Primitives (Renamings : List_Id) return Elist_Id is
8704 Actual : Node_Id;
8705 Gen_T : Entity_Id;
8706 Result : Elist_Id := No_Elist;
8707
8708 begin
8709 if No (Renamings) then
8710 return No_Elist;
8711 end if;
8712
8713 -- Traverse the list of associations between formals and actuals
8714 -- searching for renamings of tagged types
8715
8716 Actual := First (Renamings);
8717 while Present (Actual) loop
8718 if Nkind (Actual) = N_Subtype_Declaration then
8719 Gen_T := Generic_Parent_Type (Actual);
8720
8721 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
8722
8723 -- Traverse the list of primitives of the actual types
8724 -- searching for hidden primitives that are visible in the
8725 -- corresponding generic formal; leave them visible and
8726 -- append them to Result to restore their decoration later.
8727
8728 Install_Hidden_Primitives
8729 (Prims_List => Result,
8730 Gen_T => Gen_T,
8731 Act_T => Entity (Subtype_Indication (Actual)));
8732 end if;
8733 end if;
8734
8735 Next (Actual);
8736 end loop;
8737
8738 return Result;
8739 end Check_Hidden_Primitives;
8740
8741 -------------------------------------
8742 -- Component_Type_For_Private_View --
8743 -------------------------------------
8744
8745 function Component_Type_For_Private_View (T : Entity_Id) return Entity_Id is
8746 Typ : constant Entity_Id := Component_Type (T);
8747
8748 begin
8749 if Is_Array_Type (Typ) and then not Has_Private_Declaration (Typ) then
8750 return Component_Type_For_Private_View (Typ);
8751 else
8752 return Typ;
8753 end if;
8754 end Component_Type_For_Private_View;
8755
8756 --------------------------
8757 -- Contains_Instance_Of --
8758 --------------------------
8759
8760 function Contains_Instance_Of
8761 (Inner : Entity_Id;
8762 Outer : Entity_Id;
8763 N : Node_Id) return Boolean
8764 is
8765 Elmt : Elmt_Id;
8766 Scop : Entity_Id;
8767
8768 begin
8769 Scop := Outer;
8770
8771 -- Verify that there are no circular instantiations. We check whether
8772 -- the unit contains an instance of the current scope or some enclosing
8773 -- scope (in case one of the instances appears in a subunit). Longer
8774 -- circularities involving subunits might seem too pathological to
8775 -- consider, but they were not too pathological for the authors of
8776 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
8777 -- enclosing generic scopes as containing an instance.
8778
8779 loop
8780 -- Within a generic subprogram body, the scope is not generic, to
8781 -- allow for recursive subprograms. Use the declaration to determine
8782 -- whether this is a generic unit.
8783
8784 if Ekind (Scop) = E_Generic_Package
8785 or else (Is_Subprogram (Scop)
8786 and then Nkind (Unit_Declaration_Node (Scop)) =
8787 N_Generic_Subprogram_Declaration)
8788 then
8789 Elmt := First_Elmt (Inner_Instances (Inner));
8790
8791 while Present (Elmt) loop
8792 if Node (Elmt) = Scop then
8793 Error_Msg_Node_2 := Inner;
8794 Error_Msg_NE
8795 ("circular instantiation: & instantiated within &!",
8796 N, Scop);
8797 return True;
8798
8799 elsif Node (Elmt) = Inner then
8800 return True;
8801
8802 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
8803 Error_Msg_Node_2 := Inner;
8804 Error_Msg_NE
8805 ("circular instantiation: & instantiated within &!",
8806 N, Node (Elmt));
8807 return True;
8808 end if;
8809
8810 Next_Elmt (Elmt);
8811 end loop;
8812
8813 -- Indicate that Inner is being instantiated within Scop
8814
8815 Append_Elmt (Inner, Inner_Instances (Scop));
8816 end if;
8817
8818 if Scop = Standard_Standard then
8819 exit;
8820 else
8821 Scop := Scope (Scop);
8822 end if;
8823 end loop;
8824
8825 return False;
8826 end Contains_Instance_Of;
8827
8828 -----------------------
8829 -- Copy_Generic_Node --
8830 -----------------------
8831
8832 function Copy_Generic_Node
8833 (N : Node_Id;
8834 Parent_Id : Node_Id;
8835 Instantiating : Boolean) return Node_Id
8836 is
8837 Ent : Entity_Id;
8838 New_N : Node_Id;
8839
8840 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
8841 -- Check the given value of one of the Fields referenced by the current
8842 -- node to determine whether to copy it recursively. The field may hold
8843 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
8844 -- Char) in which case it need not be copied.
8845
8846 procedure Copy_Descendants;
8847 -- Common utility for various nodes
8848
8849 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
8850 -- Make copy of element list
8851
8852 function Copy_Generic_List
8853 (L : List_Id;
8854 Parent_Id : Node_Id) return List_Id;
8855 -- Apply Copy_Generic_Node recursively to the members of a node list
8856
8857 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
8858 -- True if an identifier is part of the defining program unit name of
8859 -- a child unit.
8860 -- Consider removing this subprogram now that ASIS no longer uses it.
8861
8862 ----------------------
8863 -- Copy_Descendants --
8864 ----------------------
8865
8866 procedure Copy_Descendants is
8867 procedure Walk is new
8868 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
8869 begin
8870 Walk (New_N, N);
8871 end Copy_Descendants;
8872
8873 -----------------------------
8874 -- Copy_Generic_Descendant --
8875 -----------------------------
8876
8877 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
8878 begin
8879 if D = Union_Id (Empty) then
8880 return D;
8881
8882 elsif D in Node_Range then
8883 return Union_Id
8884 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
8885
8886 elsif D in List_Range then
8887 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
8888
8889 elsif D in Elist_Range then
8890 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
8891
8892 -- Nothing else is copyable (e.g. Uint values), return as is
8893
8894 else
8895 return D;
8896 end if;
8897 end Copy_Generic_Descendant;
8898
8899 ------------------------
8900 -- Copy_Generic_Elist --
8901 ------------------------
8902
8903 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
8904 M : Elmt_Id;
8905 L : Elist_Id;
8906
8907 begin
8908 if Present (E) then
8909 L := New_Elmt_List;
8910 M := First_Elmt (E);
8911 while Present (M) loop
8912 Append_Elmt
8913 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
8914 Next_Elmt (M);
8915 end loop;
8916
8917 return L;
8918
8919 else
8920 return No_Elist;
8921 end if;
8922 end Copy_Generic_Elist;
8923
8924 -----------------------
8925 -- Copy_Generic_List --
8926 -----------------------
8927
8928 function Copy_Generic_List
8929 (L : List_Id;
8930 Parent_Id : Node_Id) return List_Id
8931 is
8932 N : Node_Id;
8933 New_L : List_Id;
8934
8935 begin
8936 if Present (L) then
8937 New_L := New_List;
8938 Set_Parent (New_L, Parent_Id);
8939
8940 N := First (L);
8941 while Present (N) loop
8942 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
8943 Next (N);
8944 end loop;
8945
8946 return New_L;
8947
8948 else
8949 return No_List;
8950 end if;
8951 end Copy_Generic_List;
8952
8953 ---------------------------
8954 -- In_Defining_Unit_Name --
8955 ---------------------------
8956
8957 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
8958 begin
8959 return
8960 Present (Parent (Nam))
8961 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
8962 or else
8963 (Nkind (Parent (Nam)) = N_Expanded_Name
8964 and then In_Defining_Unit_Name (Parent (Nam))));
8965 end In_Defining_Unit_Name;
8966
8967 -- Start of processing for Copy_Generic_Node
8968
8969 begin
8970 if N = Empty then
8971 return N;
8972 end if;
8973
8974 New_N := New_Copy (N);
8975
8976 -- If we are instantiating, we want to adjust the sloc based on the
8977 -- current S_Adjustment. However, if this is the root node of a subunit,
8978 -- we need to defer that adjustment to below (see "elsif Instantiating
8979 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8980 -- computed the adjustment.
8981
8982 if Instantiating
8983 and then not (Nkind (N) in N_Proper_Body
8984 and then Was_Originally_Stub (N))
8985 then
8986 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8987 end if;
8988
8989 if not Is_List_Member (N) then
8990 Set_Parent (New_N, Parent_Id);
8991 end if;
8992
8993 -- Special casing for identifiers and other entity names and operators
8994
8995 if Nkind (N) in N_Character_Literal
8996 | N_Expanded_Name
8997 | N_Identifier
8998 | N_Operator_Symbol
8999 | N_Op
9000 then
9001 if not Instantiating then
9002
9003 -- Link both nodes in order to assign subsequently the entity of
9004 -- the copy to the original node, in case this is a global
9005 -- reference.
9006
9007 Set_Associated_Node (N, New_N);
9008
9009 -- If we are within an instantiation, this is a nested generic
9010 -- that has already been analyzed at the point of definition.
9011 -- We must preserve references that were global to the enclosing
9012 -- parent at that point. Other occurrences, whether global or
9013 -- local to the current generic, must be resolved anew, so we
9014 -- reset the entity in the generic copy. A global reference has a
9015 -- smaller depth than the parent, or else the same depth in case
9016 -- both are distinct compilation units.
9017
9018 -- A child unit is implicitly declared within the enclosing parent
9019 -- but is in fact global to it, and must be preserved.
9020
9021 -- It is also possible for Current_Instantiated_Parent to be
9022 -- defined, and for this not to be a nested generic, namely if
9023 -- the unit is loaded through Rtsfind. In that case, the entity of
9024 -- New_N is only a link to the associated node, and not a defining
9025 -- occurrence.
9026
9027 -- The entities for parent units in the defining_program_unit of a
9028 -- generic child unit are established when the context of the unit
9029 -- is first analyzed, before the generic copy is made. They are
9030 -- preserved in the copy for use in e.g. ASIS queries.
9031
9032 Ent := Entity (New_N);
9033
9034 if No (Current_Instantiated_Parent.Gen_Id) then
9035 if No (Ent)
9036 or else Nkind (Ent) /= N_Defining_Identifier
9037 or else not In_Defining_Unit_Name (N)
9038 then
9039 Set_Associated_Node (New_N, Empty);
9040 end if;
9041
9042 elsif No (Ent)
9043 or else Nkind (Ent) not in N_Entity
9044 or else No (Scope (Ent))
9045 or else
9046 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
9047 and then not Is_Child_Unit (Ent))
9048 or else
9049 (Scope_Depth_Set (Scope (Ent))
9050 and then
9051 Scope_Depth (Scope (Ent)) >
9052 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
9053 and then
9054 Get_Source_Unit (Ent) =
9055 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
9056 then
9057 Set_Associated_Node (New_N, Empty);
9058 end if;
9059
9060 -- Case of instantiating identifier or some other name or operator
9061
9062 else
9063 -- If the associated node is still defined, the entity in it
9064 -- is global, and must be copied to the instance. If this copy
9065 -- is being made for a body to inline, it is applied to an
9066 -- instantiated tree, and the entity is already present and
9067 -- must be also preserved.
9068
9069 declare
9070 Assoc : constant Node_Id := Get_Associated_Node (N);
9071
9072 begin
9073 if Present (Assoc) then
9074 if Nkind (Assoc) = Nkind (N) then
9075 Set_Entity (New_N, Entity (Assoc));
9076 Check_Private_View (N);
9077
9078 -- The node is a reference to a global type and acts as the
9079 -- subtype mark of a qualified expression created in order
9080 -- to aid resolution of accidental overloading in instances.
9081 -- Since N is a reference to a type, the Associated_Node of
9082 -- N denotes an entity rather than another identifier. See
9083 -- Qualify_Universal_Operands for details.
9084
9085 elsif Nkind (N) = N_Identifier
9086 and then Nkind (Parent (N)) = N_Qualified_Expression
9087 and then Subtype_Mark (Parent (N)) = N
9088 and then Is_Qualified_Universal_Literal (Parent (N))
9089 then
9090 Set_Entity (New_N, Assoc);
9091
9092 -- Cope with the rewriting into expanded name that may have
9093 -- occurred in between, e.g. in Check_Generic_Child_Unit for
9094 -- generic renaming declarations.
9095
9096 elsif Nkind (Assoc) = N_Expanded_Name then
9097 Rewrite (N, New_Copy_Tree (Assoc));
9098 Set_Associated_Node (N, Assoc);
9099 return Copy_Generic_Node (N, Parent_Id, Instantiating);
9100
9101 -- The name in the call may be a selected component if the
9102 -- call has not been analyzed yet, as may be the case for
9103 -- pre/post conditions in a generic unit.
9104
9105 elsif Nkind (Assoc) = N_Function_Call
9106 and then Is_Entity_Name (Name (Assoc))
9107 then
9108 Set_Entity (New_N, Entity (Name (Assoc)));
9109 Check_Private_View (N);
9110
9111 elsif Nkind (Assoc) in N_Entity
9112 and then (Expander_Active
9113 or else (GNATprove_Mode
9114 and then not In_Spec_Expression
9115 and then not Inside_A_Generic))
9116 then
9117 -- Inlining case: we are copying a tree that contains
9118 -- global entities, which are preserved in the copy to be
9119 -- used for subsequent inlining.
9120
9121 null;
9122
9123 else
9124 Set_Entity (New_N, Empty);
9125 end if;
9126 end if;
9127 end;
9128 end if;
9129
9130 -- For expanded name, we must copy the Prefix and Selector_Name
9131
9132 if Nkind (N) = N_Expanded_Name then
9133 Set_Prefix
9134 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
9135
9136 Set_Selector_Name (New_N,
9137 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
9138
9139 -- For operators, copy the operands
9140
9141 elsif Nkind (N) in N_Op then
9142 if Nkind (N) in N_Binary_Op then
9143 Set_Left_Opnd (New_N,
9144 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
9145 end if;
9146
9147 Set_Right_Opnd (New_N,
9148 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
9149 end if;
9150
9151 -- Establish a link between an entity from the generic template and the
9152 -- corresponding entity in the generic copy to be analyzed.
9153
9154 elsif Nkind (N) in N_Entity then
9155 if not Instantiating then
9156 Set_Associated_Entity (N, New_N);
9157 end if;
9158
9159 -- Clear any existing link the copy may inherit from the replicated
9160 -- generic template entity.
9161
9162 Set_Associated_Entity (New_N, Empty);
9163
9164 -- Special casing for stubs
9165
9166 elsif Nkind (N) in N_Body_Stub then
9167
9168 -- In any case, we must copy the specification or defining
9169 -- identifier as appropriate.
9170
9171 if Nkind (N) = N_Subprogram_Body_Stub then
9172 Set_Specification (New_N,
9173 Copy_Generic_Node (Specification (N), New_N, Instantiating));
9174
9175 else
9176 Set_Defining_Identifier (New_N,
9177 Copy_Generic_Node
9178 (Defining_Identifier (N), New_N, Instantiating));
9179 end if;
9180
9181 -- If we are not instantiating, then this is where we load and
9182 -- analyze subunits, i.e. at the point where the stub occurs. A
9183 -- more permissive system might defer this analysis to the point
9184 -- of instantiation, but this seems too complicated for now.
9185
9186 if not Instantiating then
9187 declare
9188 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
9189 Subunit : Node_Id;
9190 Unum : Unit_Number_Type;
9191 New_Body : Node_Id;
9192
9193 begin
9194 -- Make sure that, if it is a subunit of the main unit that is
9195 -- preprocessed and if -gnateG is specified, the preprocessed
9196 -- file will be written.
9197
9198 Lib.Analysing_Subunit_Of_Main :=
9199 Lib.In_Extended_Main_Source_Unit (N);
9200 Unum :=
9201 Load_Unit
9202 (Load_Name => Subunit_Name,
9203 Required => False,
9204 Subunit => True,
9205 Error_Node => N);
9206 Lib.Analysing_Subunit_Of_Main := False;
9207
9208 -- If the proper body is not found, a warning message will be
9209 -- emitted when analyzing the stub, or later at the point of
9210 -- instantiation. Here we just leave the stub as is.
9211
9212 if Unum = No_Unit then
9213 Subunits_Missing := True;
9214 goto Subunit_Not_Found;
9215 end if;
9216
9217 Subunit := Cunit (Unum);
9218
9219 if Nkind (Unit (Subunit)) /= N_Subunit then
9220 Error_Msg_N
9221 ("found child unit instead of expected SEPARATE subunit",
9222 Subunit);
9223 Error_Msg_Sloc := Sloc (N);
9224 Error_Msg_N ("\to complete stub #", Subunit);
9225 goto Subunit_Not_Found;
9226 end if;
9227
9228 -- We must create a generic copy of the subunit, in order to
9229 -- perform semantic analysis on it, and we must replace the
9230 -- stub in the original generic unit with the subunit, in order
9231 -- to preserve non-local references within.
9232
9233 -- Only the proper body needs to be copied. The context clause
9234 -- and Spec_Or_Body_Lib_Unit are simply inherited by the
9235 -- generic copy. Note that the copy (which may be recursive
9236 -- if there are nested subunits) must be done first, before
9237 -- attaching it to the enclosing generic.
9238
9239 New_Body :=
9240 Copy_Generic_Node
9241 (Proper_Body (Unit (Subunit)),
9242 Empty, Instantiating => False);
9243
9244 -- Now place the original proper body in the original generic
9245 -- unit. This is a body, not a compilation unit.
9246
9247 Rewrite (N, Proper_Body (Unit (Subunit)));
9248 Set_Is_Compilation_Unit (Defining_Entity (N), False);
9249 Set_Was_Originally_Stub (N);
9250
9251 -- Finally replace the body of the subunit with its copy, and
9252 -- make this new subunit into the library unit of the generic
9253 -- copy, which does not have stubs any longer.
9254
9255 Set_Proper_Body (Unit (Subunit), New_Body);
9256 Set_Stub_Subunit (New_N, Subunit);
9257 Inherit_Context (Unit (Subunit), N);
9258 end;
9259
9260 -- If we are instantiating, this must be an error case, since
9261 -- otherwise we would have replaced the stub node by the proper body
9262 -- that corresponds. So just ignore it in the copy (i.e. we have
9263 -- copied it, and that is good enough).
9264
9265 else
9266 null;
9267 end if;
9268
9269 <<Subunit_Not_Found>> null;
9270
9271 -- If the node is a compilation unit, it is the subunit of a stub that
9272 -- has already been loaded. The parent unit is a compilation unit and
9273 -- need not (and cannot) be copied.
9274
9275 -- When the proper body of the stub is analyzed, the Subunit_Parent
9276 -- field is used to establish the proper context (see Sem_Ch10).
9277
9278 -- The other fields of a compilation unit are copied as usual
9279
9280 elsif Nkind (N) = N_Compilation_Unit then
9281 pragma Assert (Unit (N) in N_Subunit_Id);
9282
9283 -- This code can only be executed when not instantiating, because in
9284 -- the copy made for an instantiation, the compilation unit node has
9285 -- disappeared at the point that a stub is replaced by its proper
9286 -- body.
9287
9288 pragma Assert (not Instantiating);
9289
9290 Set_Context_Items (New_N,
9291 Copy_Generic_List (Context_Items (N), New_N));
9292
9293 Set_Unit (New_N,
9294 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
9295
9296 Set_First_Inlined_Subprogram (New_N,
9297 Copy_Generic_Node
9298 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
9299
9300 Set_Aux_Decls_Node
9301 (New_N,
9302 Copy_Generic_Node
9303 (Aux_Decls_Node (N), New_N, Instantiating => False));
9304
9305 -- For an assignment node, the assignment is known to be semantically
9306 -- legal if we are instantiating the template. This avoids incorrect
9307 -- diagnostics in generated code.
9308
9309 elsif Nkind (N) = N_Assignment_Statement then
9310
9311 -- Copy name and expression fields in usual manner
9312
9313 Set_Name (New_N,
9314 Copy_Generic_Node (Name (N), New_N, Instantiating));
9315
9316 Set_Expression (New_N,
9317 Copy_Generic_Node (Expression (N), New_N, Instantiating));
9318
9319 if Instantiating then
9320 Set_Assignment_OK (Name (New_N), True);
9321 end if;
9322
9323 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
9324 if not Instantiating then
9325 Set_Associated_Node (N, New_N);
9326
9327 else
9328 -- If, in the generic, the aggregate has a global composite type
9329 -- and, at the point of instantiation, the type has a private view
9330 -- then install the full view.
9331
9332 declare
9333 Assoc : constant Node_Id := Get_Associated_Node (N);
9334
9335 begin
9336 if Present (Assoc)
9337 and then Nkind (Assoc) = Nkind (N)
9338 and then Present (Etype (Assoc))
9339 and then Is_Private_Type (Etype (Assoc))
9340 then
9341 Switch_View (Etype (Assoc));
9342 end if;
9343 end;
9344
9345 -- Moreover, for a full aggregate, if the type is a derived tagged
9346 -- type and has a global ancestor, then also restore the full view
9347 -- of this ancestor and do so up to the root type. Beware that the
9348 -- Ancestor_Type field is overloaded, so test that it's an entity.
9349
9350 if Nkind (N) = N_Aggregate
9351 and then Present (Ancestor_Type (N))
9352 and then Nkind (Ancestor_Type (N)) in N_Entity
9353 then
9354 declare
9355 Typ : Entity_Id := Ancestor_Type (N);
9356
9357 begin
9358 loop
9359 if Is_Private_Type (Typ) then
9360 Switch_View (Typ);
9361 end if;
9362
9363 exit when Etype (Typ) = Typ;
9364
9365 Typ := Etype (Typ);
9366 end loop;
9367 end;
9368 end if;
9369 end if;
9370
9371 -- Do not copy the associated node, which points to the generic copy
9372 -- of the aggregate.
9373
9374 if Nkind (N) = N_Aggregate then
9375 Set_Aggregate_Bounds
9376 (New_N,
9377 Node_Id (Copy_Generic_Descendant
9378 (Union_Id (Aggregate_Bounds (N)))));
9379
9380 elsif Nkind (N) = N_Extension_Aggregate then
9381 Set_Ancestor_Part
9382 (New_N,
9383 Node_Id (Copy_Generic_Descendant
9384 (Union_Id (Ancestor_Part (N)))));
9385
9386 else
9387 pragma Assert (False);
9388 end if;
9389
9390 Set_Expressions
9391 (New_N,
9392 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
9393 Set_Component_Associations
9394 (New_N,
9395 List_Id (Copy_Generic_Descendant
9396 (Union_Id (Component_Associations (N)))));
9397 Set_Etype
9398 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
9399
9400 -- Allocators do not have an identifier denoting the access type, so we
9401 -- must locate it through the expression to check whether the views are
9402 -- consistent.
9403
9404 elsif Nkind (N) = N_Allocator
9405 and then Nkind (Expression (N)) = N_Qualified_Expression
9406 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
9407 and then Instantiating
9408 then
9409 declare
9410 T : constant Node_Id :=
9411 Get_Associated_Node (Subtype_Mark (Expression (N)));
9412 Acc_T : Entity_Id;
9413
9414 begin
9415 if Present (T) then
9416
9417 -- Retrieve the allocator node in the generic copy
9418
9419 Acc_T := Etype (Parent (Parent (T)));
9420
9421 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
9422 Switch_View (Acc_T);
9423 end if;
9424 end if;
9425
9426 Copy_Descendants;
9427 end;
9428
9429 -- Loop parameter specifications do not have an identifier denoting the
9430 -- index type, so we must locate it through the defining identifier to
9431 -- check whether the views are consistent.
9432
9433 elsif Nkind (N) = N_Loop_Parameter_Specification
9434 and then Instantiating
9435 then
9436 declare
9437 Id : constant Entity_Id :=
9438 Get_Associated_Entity (Defining_Identifier (N));
9439
9440 Index_T : Entity_Id;
9441
9442 begin
9443 if Present (Id) and then Present (Etype (Id)) then
9444 Index_T := First_Subtype (Etype (Id));
9445
9446 if Present (Index_T) and then Is_Private_Type (Index_T) then
9447 Switch_View (Index_T);
9448 end if;
9449 end if;
9450
9451 Copy_Descendants;
9452 end;
9453
9454 -- For a proper body, we must catch the case of a proper body that
9455 -- replaces a stub. This represents the point at which a separate
9456 -- compilation unit, and hence template file, may be referenced, so we
9457 -- must make a new source instantiation entry for the template of the
9458 -- subunit, and ensure that all nodes in the subunit are adjusted using
9459 -- this new source instantiation entry.
9460
9461 elsif Nkind (N) in N_Proper_Body then
9462 declare
9463 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
9464 begin
9465 if Instantiating and then Was_Originally_Stub (N) then
9466 Create_Instantiation_Source
9467 (Instantiation_Node,
9468 Defining_Entity (N),
9469 S_Adjustment);
9470
9471 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
9472 end if;
9473
9474 -- Now copy the fields of the proper body, using the new
9475 -- adjustment factor if one was needed as per test above.
9476
9477 Copy_Descendants;
9478
9479 -- Restore the original adjustment factor
9480
9481 S_Adjustment := Save_Adjustment;
9482 end;
9483
9484 elsif Nkind (N) = N_Pragma and then Instantiating then
9485
9486 -- Do not copy Comment or Ident pragmas their content is relevant to
9487 -- the generic unit, not to the instantiating unit.
9488
9489 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
9490 New_N := Make_Null_Statement (Sloc (N));
9491
9492 -- Do not copy pragmas generated from aspects because the pragmas do
9493 -- not carry any semantic information, plus they will be regenerated
9494 -- in the instance.
9495
9496 -- However, generating C we need to copy them since postconditions
9497 -- are inlined by the front end, and the front-end inlining machinery
9498 -- relies on this routine to perform inlining.
9499
9500 elsif From_Aspect_Specification (N) then
9501 New_N := Make_Null_Statement (Sloc (N));
9502
9503 else
9504 Copy_Descendants;
9505 end if;
9506
9507 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
9508
9509 -- No descendant fields need traversing
9510
9511 null;
9512
9513 elsif Nkind (N) = N_String_Literal
9514 and then Present (Etype (N))
9515 and then Instantiating
9516 then
9517 -- If the string is declared in an outer scope, the string_literal
9518 -- subtype created for it may have the wrong scope. Force reanalysis
9519 -- of the constant to generate a new itype in the proper context.
9520
9521 Set_Etype (New_N, Empty);
9522 Set_Analyzed (New_N, False);
9523
9524 -- For the remaining nodes, copy their descendants recursively
9525
9526 else
9527 Copy_Descendants;
9528
9529 if Instantiating and then Nkind (N) = N_Subprogram_Body then
9530 Set_Generic_Parent (Specification (New_N), N);
9531
9532 -- Should preserve Corresponding_Spec??? (12.3(14))
9533 end if;
9534 end if;
9535
9536 -- Propagate dimensions if present, so that they are reflected in the
9537 -- instance.
9538
9539 if Nkind (N) in N_Has_Etype
9540 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
9541 and then Present (Etype (N))
9542 and then Is_Floating_Point_Type (Etype (N))
9543 and then Has_Dimension_System (Etype (N))
9544 then
9545 Copy_Dimensions (N, New_N);
9546 end if;
9547
9548 return New_N;
9549 end Copy_Generic_Node;
9550
9551 ----------------------------
9552 -- Denotes_Formal_Package --
9553 ----------------------------
9554
9555 function Denotes_Formal_Package
9556 (Pack : Entity_Id;
9557 On_Exit : Boolean := False;
9558 Instance : Entity_Id := Empty) return Boolean
9559 is
9560 Par : Entity_Id;
9561 Scop : constant Entity_Id := Scope (Pack);
9562 E : Entity_Id;
9563
9564 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
9565 -- The package in question may be an actual for a previous formal
9566 -- package P of the current instance, so examine its actuals as well.
9567 -- This must be recursive over other formal packages.
9568
9569 ----------------------------------
9570 -- Is_Actual_Of_Previous_Formal --
9571 ----------------------------------
9572
9573 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
9574 E1 : Entity_Id;
9575
9576 begin
9577 E1 := First_Entity (P);
9578 while Present (E1) and then E1 /= Instance loop
9579 if Ekind (E1) = E_Package
9580 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
9581 then
9582 if Renamed_Entity (E1) = Pack then
9583 return True;
9584
9585 elsif E1 = P or else Renamed_Entity (E1) = P then
9586 return False;
9587
9588 elsif Is_Actual_Of_Previous_Formal (E1) then
9589 return True;
9590 end if;
9591 end if;
9592
9593 Next_Entity (E1);
9594 end loop;
9595
9596 return False;
9597 end Is_Actual_Of_Previous_Formal;
9598
9599 -- Start of processing for Denotes_Formal_Package
9600
9601 begin
9602 if On_Exit then
9603 Par :=
9604 Instance_Envs.Table
9605 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
9606 else
9607 Par := Current_Instantiated_Parent.Act_Id;
9608 end if;
9609
9610 if Ekind (Scop) = E_Generic_Package
9611 or else Nkind (Unit_Declaration_Node (Scop)) =
9612 N_Generic_Subprogram_Declaration
9613 then
9614 return True;
9615
9616 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
9617 N_Formal_Package_Declaration
9618 then
9619 return True;
9620
9621 elsif No (Par) then
9622 return False;
9623
9624 else
9625 -- Check whether this package is associated with a formal package of
9626 -- the enclosing instantiation. Iterate over the list of renamings.
9627
9628 E := First_Entity (Par);
9629 while Present (E) loop
9630 if Ekind (E) /= E_Package
9631 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
9632 then
9633 null;
9634
9635 elsif Renamed_Entity (E) = Par then
9636 return False;
9637
9638 elsif Renamed_Entity (E) = Pack then
9639 return True;
9640
9641 elsif Is_Actual_Of_Previous_Formal (E) then
9642 return True;
9643
9644 end if;
9645
9646 Next_Entity (E);
9647 end loop;
9648
9649 return False;
9650 end if;
9651 end Denotes_Formal_Package;
9652
9653 -----------------
9654 -- End_Generic --
9655 -----------------
9656
9657 procedure End_Generic is
9658 begin
9659 -- ??? More things could be factored out in this routine. Should
9660 -- probably be done at a later stage.
9661
9662 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
9663 Generic_Flags.Decrement_Last;
9664
9665 Expander_Mode_Restore;
9666 end End_Generic;
9667
9668 -------------
9669 -- Earlier --
9670 -------------
9671
9672 function Earlier (N1, N2 : Node_Id) return Boolean is
9673 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
9674 -- Find distance from given node to enclosing compilation unit
9675
9676 ----------------
9677 -- Find_Depth --
9678 ----------------
9679
9680 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
9681 begin
9682 while Present (P)
9683 and then Nkind (P) /= N_Compilation_Unit
9684 loop
9685 P := True_Parent (P);
9686 D := D + 1;
9687 end loop;
9688 end Find_Depth;
9689
9690 -- Local declarations
9691
9692 D1 : Integer := 0;
9693 D2 : Integer := 0;
9694 P1 : Node_Id := N1;
9695 P2 : Node_Id := N2;
9696 T1 : Source_Ptr;
9697 T2 : Source_Ptr;
9698
9699 -- Start of processing for Earlier
9700
9701 begin
9702 Find_Depth (P1, D1);
9703 Find_Depth (P2, D2);
9704
9705 if P1 /= P2 then
9706 return False;
9707 else
9708 P1 := N1;
9709 P2 := N2;
9710 end if;
9711
9712 while D1 > D2 loop
9713 P1 := True_Parent (P1);
9714 D1 := D1 - 1;
9715 end loop;
9716
9717 while D2 > D1 loop
9718 P2 := True_Parent (P2);
9719 D2 := D2 - 1;
9720 end loop;
9721
9722 -- At this point P1 and P2 are at the same distance from the root.
9723 -- We examine their parents until we find a common declarative list.
9724 -- If we reach the root, N1 and N2 do not descend from the same
9725 -- declarative list (e.g. one is nested in the declarative part and
9726 -- the other is in a block in the statement part) and the earlier
9727 -- one is already frozen.
9728
9729 while not Is_List_Member (P1)
9730 or else not Is_List_Member (P2)
9731 or else not In_Same_List (P1, P2)
9732 loop
9733 P1 := True_Parent (P1);
9734 P2 := True_Parent (P2);
9735
9736 if Nkind (Parent (P1)) = N_Subunit then
9737 P1 := Corresponding_Stub (Parent (P1));
9738 end if;
9739
9740 if Nkind (Parent (P2)) = N_Subunit then
9741 P2 := Corresponding_Stub (Parent (P2));
9742 end if;
9743
9744 if P1 = P2 then
9745 return False;
9746 end if;
9747 end loop;
9748
9749 -- Expanded code usually shares the source location of the original
9750 -- construct it was generated for. This however may not necessarily
9751 -- reflect the true location of the code within the tree.
9752
9753 -- Before comparing the slocs of the two nodes, make sure that we are
9754 -- working with correct source locations. Assume that P1 is to the left
9755 -- of P2. If either one does not come from source, traverse the common
9756 -- list heading towards the other node and locate the first source
9757 -- statement.
9758
9759 -- P1 P2
9760 -- ----+===+===+--------------+===+===+----
9761 -- expanded code expanded code
9762
9763 if not Comes_From_Source (P1) then
9764 while Present (P1) loop
9765
9766 -- Neither P2 nor a source statement were located during the
9767 -- search. If we reach the end of the list, then P1 does not
9768 -- occur earlier than P2.
9769
9770 -- ---->
9771 -- start --- P2 ----- P1 --- end
9772
9773 if No (Next (P1)) then
9774 return False;
9775
9776 -- We encounter P2 while going to the right of the list. This
9777 -- means that P1 does indeed appear earlier.
9778
9779 -- ---->
9780 -- start --- P1 ===== P2 --- end
9781 -- expanded code in between
9782
9783 elsif P1 = P2 then
9784 return True;
9785
9786 -- No need to look any further since we have located a source
9787 -- statement.
9788
9789 elsif Comes_From_Source (P1) then
9790 exit;
9791 end if;
9792
9793 -- Keep going right
9794
9795 Next (P1);
9796 end loop;
9797 end if;
9798
9799 if not Comes_From_Source (P2) then
9800 while Present (P2) loop
9801
9802 -- Neither P1 nor a source statement were located during the
9803 -- search. If we reach the start of the list, then P1 does not
9804 -- occur earlier than P2.
9805
9806 -- <----
9807 -- start --- P2 --- P1 --- end
9808
9809 if No (Prev (P2)) then
9810 return False;
9811
9812 -- We encounter P1 while going to the left of the list. This
9813 -- means that P1 does indeed appear earlier.
9814
9815 -- <----
9816 -- start --- P1 ===== P2 --- end
9817 -- expanded code in between
9818
9819 elsif P2 = P1 then
9820 return True;
9821
9822 -- No need to look any further since we have located a source
9823 -- statement.
9824
9825 elsif Comes_From_Source (P2) then
9826 exit;
9827 end if;
9828
9829 -- Keep going left
9830
9831 Prev (P2);
9832 end loop;
9833 end if;
9834
9835 -- At this point either both nodes came from source or we approximated
9836 -- their source locations through neighboring source statements.
9837
9838 T1 := Top_Level_Location (Sloc (P1));
9839 T2 := Top_Level_Location (Sloc (P2));
9840
9841 -- When two nodes come from the same instance, they have identical top
9842 -- level locations. To determine proper relation within the tree, check
9843 -- their locations within the template.
9844
9845 if T1 = T2 then
9846 return Sloc (P1) < Sloc (P2);
9847
9848 -- The two nodes either come from unrelated instances or do not come
9849 -- from instantiated code at all.
9850
9851 else
9852 return T1 < T2;
9853 end if;
9854 end Earlier;
9855
9856 ----------------------
9857 -- Find_Actual_Type --
9858 ----------------------
9859
9860 function Find_Actual_Type
9861 (Typ : Entity_Id;
9862 Gen_Typ : Entity_Id) return Entity_Id
9863 is
9864 Gen_Scope : constant Entity_Id := Scope (Gen_Typ);
9865 T : Entity_Id;
9866
9867 begin
9868 -- Special processing only applies to child units
9869
9870 if not Is_Child_Unit (Gen_Scope) then
9871 return Get_Instance_Of (Typ);
9872
9873 -- If designated or component type is itself a formal of the child unit,
9874 -- its instance is available.
9875
9876 elsif Scope (Typ) = Gen_Scope then
9877 return Get_Instance_Of (Typ);
9878
9879 -- If the array or access type is not declared in the parent unit,
9880 -- no special processing needed.
9881
9882 elsif not Is_Generic_Type (Typ)
9883 and then Scope (Gen_Scope) /= Scope (Typ)
9884 then
9885 return Get_Instance_Of (Typ);
9886
9887 -- Otherwise, retrieve designated or component type by visibility
9888
9889 else
9890 T := Current_Entity (Typ);
9891 while Present (T) loop
9892 if In_Open_Scopes (Scope (T)) then
9893 return T;
9894 elsif Is_Generic_Actual_Type (T) then
9895 return T;
9896 end if;
9897
9898 T := Homonym (T);
9899 end loop;
9900
9901 return Typ;
9902 end if;
9903 end Find_Actual_Type;
9904
9905 -----------------------------
9906 -- Freeze_Package_Instance --
9907 -----------------------------
9908
9909 procedure Freeze_Package_Instance
9910 (N : Node_Id;
9911 Gen_Body : Node_Id;
9912 Gen_Decl : Node_Id;
9913 Act_Id : Entity_Id)
9914 is
9915 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9916 -- Check if the generic definition and the instantiation come from
9917 -- a common scope, in which case the instance must be frozen after
9918 -- the generic body.
9919
9920 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9921 -- If the instance is nested inside a generic unit, the Sloc of the
9922 -- instance indicates the place of the original definition, not the
9923 -- point of the current enclosing instance. Pending a better usage of
9924 -- Slocs to indicate instantiation places, we determine the place of
9925 -- origin of a node by finding the maximum sloc of any ancestor node.
9926
9927 -- Why is this not equivalent to Top_Level_Location ???
9928
9929 -------------------
9930 -- In_Same_Scope --
9931 -------------------
9932
9933 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9934 Act_Scop : Entity_Id := Scope (Act_Id);
9935 Gen_Scop : Entity_Id := Scope (Gen_Id);
9936
9937 begin
9938 while Act_Scop /= Standard_Standard
9939 and then Gen_Scop /= Standard_Standard
9940 loop
9941 if Act_Scop = Gen_Scop then
9942 return True;
9943 end if;
9944
9945 Act_Scop := Scope (Act_Scop);
9946 Gen_Scop := Scope (Gen_Scop);
9947 end loop;
9948
9949 return False;
9950 end In_Same_Scope;
9951
9952 ---------------
9953 -- True_Sloc --
9954 ---------------
9955
9956 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9957 N1 : Node_Id;
9958 Res : Source_Ptr;
9959
9960 begin
9961 Res := Sloc (N);
9962 N1 := N;
9963 while Present (N1) and then N1 /= Act_Unit loop
9964 if Sloc (N1) > Res then
9965 Res := Sloc (N1);
9966 end if;
9967
9968 N1 := Parent (N1);
9969 end loop;
9970
9971 return Res;
9972 end True_Sloc;
9973
9974 -- Local variables
9975
9976 Gen_Id : constant Entity_Id := Get_Generic_Entity (N);
9977 Par_Id : constant Entity_Id := Scope (Gen_Id);
9978 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9979 Gen_Unit : constant Node_Id :=
9980 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9981
9982 Body_Unit : Node_Id;
9983 F_Node : Node_Id;
9984 Must_Delay : Boolean;
9985 Orig_Body : Node_Id;
9986
9987 -- Start of processing for Freeze_Package_Instance
9988
9989 begin
9990 -- If the body is a subunit, the freeze point is the corresponding stub
9991 -- in the current compilation, not the subunit itself.
9992
9993 if Nkind (Parent (Gen_Body)) = N_Subunit then
9994 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9995 else
9996 Orig_Body := Gen_Body;
9997 end if;
9998
9999 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
10000
10001 -- If the instantiation and the generic definition appear in the same
10002 -- package declaration, this is an early instantiation. If they appear
10003 -- in the same declarative part, it is an early instantiation only if
10004 -- the generic body appears textually later, and the generic body is
10005 -- also in the main unit.
10006
10007 -- If instance is nested within a subprogram, and the generic body
10008 -- is not, the instance is delayed because the enclosing body is. If
10009 -- instance and body are within the same scope, or the same subprogram
10010 -- body, indicate explicitly that the instance is delayed.
10011
10012 Must_Delay :=
10013 (Gen_Unit = Act_Unit
10014 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
10015 | N_Package_Declaration
10016 or else (Gen_Unit = Body_Unit
10017 and then
10018 True_Sloc (N, Act_Unit) < Sloc (Orig_Body)))
10019 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
10020 and then In_Same_Scope (Gen_Id, Act_Id));
10021
10022 -- If this is an early instantiation, the freeze node is placed after
10023 -- the generic body. Otherwise, if the generic appears in an instance,
10024 -- we cannot freeze the current instance until the outer one is frozen.
10025 -- This is only relevant if the current instance is nested within some
10026 -- inner scope not itself within the outer instance. If this scope is
10027 -- a package body in the same declarative part as the outer instance,
10028 -- then that body needs to be frozen after the outer instance. Finally,
10029 -- if no delay is needed, we place the freeze node at the end of the
10030 -- current declarative part.
10031
10032 if No (Freeze_Node (Act_Id))
10033 or else not Is_List_Member (Freeze_Node (Act_Id))
10034 then
10035 Ensure_Freeze_Node (Act_Id);
10036 F_Node := Freeze_Node (Act_Id);
10037
10038 if Must_Delay then
10039 Insert_After (Orig_Body, F_Node);
10040
10041 elsif Is_Generic_Instance (Par_Id)
10042 and then Present (Freeze_Node (Par_Id))
10043 and then Scope (Act_Id) /= Par_Id
10044 then
10045 -- Freeze instance of inner generic after instance of enclosing
10046 -- generic.
10047
10048 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N) then
10049
10050 -- Handle the following case:
10051
10052 -- package Parent_Inst is new ...
10053 -- freeze Parent_Inst []
10054
10055 -- procedure P ... -- this body freezes Parent_Inst
10056
10057 -- package Inst is new ...
10058
10059 -- In this particular scenario, the freeze node for Inst must
10060 -- be inserted in the same manner as that of Parent_Inst,
10061 -- before the next source body or at the end of the declarative
10062 -- list (body not available). If body P did not exist and
10063 -- Parent_Inst was frozen after Inst, either by a body
10064 -- following Inst or at the end of the declarative region,
10065 -- the freeze node for Inst must be inserted after that of
10066 -- Parent_Inst. This relation is established by comparing
10067 -- the Slocs of Parent_Inst freeze node and Inst.
10068 -- We examine the parents of the enclosing lists to handle
10069 -- the case where the parent instance is in the visible part
10070 -- of a package declaration, and the inner instance is in
10071 -- the corresponding private part.
10072
10073 if Parent (List_Containing (Freeze_Node (Par_Id)))
10074 = Parent (List_Containing (N))
10075 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
10076 then
10077 Insert_Freeze_Node_For_Instance (N, F_Node);
10078 else
10079 Insert_After (Freeze_Node (Par_Id), F_Node);
10080 end if;
10081
10082 -- Freeze package enclosing instance of inner generic after
10083 -- instance of enclosing generic.
10084
10085 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
10086 and then In_Same_Declarative_Part
10087 (Parent (Freeze_Node (Par_Id)), Parent (N))
10088 then
10089 declare
10090 Enclosing : Entity_Id;
10091
10092 begin
10093 Enclosing := Corresponding_Spec (Parent (N));
10094
10095 if No (Enclosing) then
10096 Enclosing := Defining_Entity (Parent (N));
10097 end if;
10098
10099 Insert_Freeze_Node_For_Instance (N, F_Node);
10100 Ensure_Freeze_Node (Enclosing);
10101
10102 if not Is_List_Member (Freeze_Node (Enclosing)) then
10103
10104 -- The enclosing context is a subunit, insert the freeze
10105 -- node after the stub.
10106
10107 if Nkind (Parent (Parent (N))) = N_Subunit then
10108 Insert_Freeze_Node_For_Instance
10109 (Corresponding_Stub (Parent (Parent (N))),
10110 Freeze_Node (Enclosing));
10111
10112 -- The enclosing context is a package with a stub body
10113 -- which has already been replaced by the real body.
10114 -- Insert the freeze node after the actual body.
10115
10116 elsif Ekind (Enclosing) = E_Package
10117 and then Present (Body_Entity (Enclosing))
10118 and then Was_Originally_Stub
10119 (Parent (Body_Entity (Enclosing)))
10120 then
10121 Insert_Freeze_Node_For_Instance
10122 (Parent (Body_Entity (Enclosing)),
10123 Freeze_Node (Enclosing));
10124
10125 -- The parent instance has been frozen before the body of
10126 -- the enclosing package, insert the freeze node after
10127 -- the body.
10128
10129 elsif In_Same_List (Freeze_Node (Par_Id), Parent (N))
10130 and then
10131 Sloc (Freeze_Node (Par_Id)) <= Sloc (Parent (N))
10132 then
10133 Insert_Freeze_Node_For_Instance
10134 (Parent (N), Freeze_Node (Enclosing));
10135
10136 else
10137 Insert_After
10138 (Freeze_Node (Par_Id), Freeze_Node (Enclosing));
10139 end if;
10140 end if;
10141 end;
10142
10143 else
10144 Insert_Freeze_Node_For_Instance (N, F_Node);
10145 end if;
10146
10147 else
10148 Insert_Freeze_Node_For_Instance (N, F_Node);
10149 end if;
10150 end if;
10151 end Freeze_Package_Instance;
10152
10153 --------------------------------
10154 -- Freeze_Subprogram_Instance --
10155 --------------------------------
10156
10157 procedure Freeze_Subprogram_Instance
10158 (N : Node_Id;
10159 Gen_Body : Node_Id;
10160 Pack_Id : Entity_Id)
10161 is
10162 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
10163 -- Find innermost package body that encloses the given node, and which
10164 -- is not a compilation unit. Freeze nodes for the instance, or for its
10165 -- enclosing body, may be inserted after the enclosing_body of the
10166 -- generic unit. Used to determine proper placement of freeze node for
10167 -- both package and subprogram instances.
10168
10169 function Package_Freeze_Node (B : Node_Id) return Node_Id;
10170 -- Find entity for given package body, and locate or create a freeze
10171 -- node for it.
10172
10173 ----------------------------
10174 -- Enclosing_Package_Body --
10175 ----------------------------
10176
10177 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
10178 P : Node_Id;
10179
10180 begin
10181 P := Parent (N);
10182 while Present (P)
10183 and then Nkind (Parent (P)) /= N_Compilation_Unit
10184 loop
10185 if Nkind (P) = N_Package_Body then
10186 if Nkind (Parent (P)) = N_Subunit then
10187 return Corresponding_Stub (Parent (P));
10188 else
10189 return P;
10190 end if;
10191 end if;
10192
10193 P := True_Parent (P);
10194 end loop;
10195
10196 return Empty;
10197 end Enclosing_Package_Body;
10198
10199 -------------------------
10200 -- Package_Freeze_Node --
10201 -------------------------
10202
10203 function Package_Freeze_Node (B : Node_Id) return Node_Id is
10204 Id : Entity_Id;
10205
10206 begin
10207 if Nkind (B) = N_Package_Body then
10208 Id := Corresponding_Spec (B);
10209 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
10210 Id := Corresponding_Spec (Proper_Body (Unit (Stub_Subunit (B))));
10211 end if;
10212
10213 Ensure_Freeze_Node (Id);
10214 return Freeze_Node (Id);
10215 end Package_Freeze_Node;
10216
10217 -- Local variables
10218
10219 Enc_G : constant Node_Id := Enclosing_Package_Body (Gen_Body);
10220 Enc_N : constant Node_Id := Enclosing_Package_Body (N);
10221 Par_Id : constant Entity_Id := Scope (Get_Generic_Entity (N));
10222
10223 Enc_G_F : Node_Id;
10224 F_Node : Node_Id;
10225
10226 -- Start of processing for Freeze_Subprogram_Instance
10227
10228 begin
10229 -- If the instance and the generic body appear within the same unit, and
10230 -- the instance precedes the generic, the freeze node for the instance
10231 -- must appear after that of the generic. If the generic is nested
10232 -- within another instance I2, then current instance must be frozen
10233 -- after I2. In both cases, the freeze nodes are those of enclosing
10234 -- packages. Otherwise, the freeze node is placed at the end of the
10235 -- current declarative part.
10236
10237 Ensure_Freeze_Node (Pack_Id);
10238 F_Node := Freeze_Node (Pack_Id);
10239
10240 if Is_Generic_Instance (Par_Id)
10241 and then Present (Freeze_Node (Par_Id))
10242 and then In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N)
10243 then
10244 -- The parent was a premature instantiation. Insert freeze node at
10245 -- the end the current declarative part.
10246
10247 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par_Id)) then
10248 Insert_Freeze_Node_For_Instance (N, F_Node);
10249
10250 -- Handle the following case:
10251 --
10252 -- package Parent_Inst is new ...
10253 -- freeze Parent_Inst []
10254 --
10255 -- procedure P ... -- this body freezes Parent_Inst
10256 --
10257 -- procedure Inst is new ...
10258 --
10259 -- In this particular scenario, the freeze node for Inst must be
10260 -- inserted in the same manner as that of Parent_Inst - before the
10261 -- next source body or at the end of the declarative list (body not
10262 -- available). If body P did not exist and Parent_Inst was frozen
10263 -- after Inst, either by a body following Inst or at the end of the
10264 -- declarative region, the freeze node for Inst must be inserted
10265 -- after that of Parent_Inst. This relation is established by
10266 -- comparing the Slocs of Parent_Inst freeze node and Inst.
10267
10268 elsif In_Same_List (Freeze_Node (Par_Id), N)
10269 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
10270 then
10271 Insert_Freeze_Node_For_Instance (N, F_Node);
10272
10273 else
10274 Insert_After (Freeze_Node (Par_Id), F_Node);
10275 end if;
10276
10277 -- The body enclosing the instance should be frozen after the body that
10278 -- includes the generic, because the body of the instance may make
10279 -- references to entities therein. If the two are not in the same
10280 -- declarative part, or if the one enclosing the instance is frozen
10281 -- already, freeze the instance at the end of the current declarative
10282 -- part.
10283
10284 elsif Is_Generic_Instance (Par_Id)
10285 and then Present (Freeze_Node (Par_Id))
10286 and then Present (Enc_N)
10287 then
10288 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), Enc_N)
10289 then
10290 -- The enclosing package may contain several instances. Rather
10291 -- than computing the earliest point at which to insert its freeze
10292 -- node, we place it at the end of the declarative part of the
10293 -- parent of the generic.
10294
10295 Insert_Freeze_Node_For_Instance
10296 (Freeze_Node (Par_Id), Package_Freeze_Node (Enc_N));
10297 end if;
10298
10299 Insert_Freeze_Node_For_Instance (N, F_Node);
10300
10301 elsif Present (Enc_G)
10302 and then Present (Enc_N)
10303 and then Enc_G /= Enc_N
10304 and then Earlier (N, Gen_Body)
10305 then
10306 -- Freeze package that encloses instance, and place node after the
10307 -- package that encloses generic. If enclosing package is already
10308 -- frozen we have to assume it is at the proper place. This may be a
10309 -- potential ABE that requires dynamic checking. Do not add a freeze
10310 -- node if the package that encloses the generic is inside the body
10311 -- that encloses the instance, because the freeze node would be in
10312 -- the wrong scope. Additional contortions needed if the bodies are
10313 -- within a subunit.
10314
10315 declare
10316 Enclosing_Body : Node_Id;
10317
10318 begin
10319 if Nkind (Enc_N) = N_Package_Body_Stub then
10320 Enclosing_Body := Proper_Body (Unit (Stub_Subunit (Enc_N)));
10321 else
10322 Enclosing_Body := Enc_N;
10323 end if;
10324
10325 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
10326 Insert_Freeze_Node_For_Instance
10327 (Enc_G, Package_Freeze_Node (Enc_N));
10328 end if;
10329 end;
10330
10331 -- Freeze enclosing subunit before instance
10332
10333 Enc_G_F := Package_Freeze_Node (Enc_G);
10334
10335 if not Is_List_Member (Enc_G_F) then
10336 Insert_After (Enc_G, Enc_G_F);
10337 end if;
10338
10339 Insert_Freeze_Node_For_Instance (N, F_Node);
10340
10341 else
10342 -- If none of the above, insert freeze node at the end of the current
10343 -- declarative part.
10344
10345 Insert_Freeze_Node_For_Instance (N, F_Node);
10346 end if;
10347 end Freeze_Subprogram_Instance;
10348
10349 ----------------
10350 -- Get_Gen_Id --
10351 ----------------
10352
10353 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
10354 begin
10355 return Generic_Renamings.Table (E).Gen_Id;
10356 end Get_Gen_Id;
10357
10358 ---------------------
10359 -- Get_Instance_Of --
10360 ---------------------
10361
10362 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
10363 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
10364
10365 begin
10366 if Res /= Assoc_Null then
10367 return Generic_Renamings.Table (Res).Act_Id;
10368
10369 else
10370 -- On exit, entity is not instantiated: not a generic parameter, or
10371 -- else parameter of an inner generic unit.
10372
10373 return A;
10374 end if;
10375 end Get_Instance_Of;
10376
10377 ---------------------------------
10378 -- Get_Unit_Instantiation_Node --
10379 ---------------------------------
10380
10381 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
10382 Decl : Node_Id := Unit_Declaration_Node (A);
10383 Inst : Node_Id;
10384
10385 begin
10386 -- If the Package_Instantiation attribute has been set on the package
10387 -- entity, then use it directly when it (or its Original_Node) refers
10388 -- to an N_Package_Instantiation node. In principle it should be
10389 -- possible to have this field set in all cases, which should be
10390 -- investigated, and would allow this function to be significantly
10391 -- simplified. ???
10392
10393 Inst := Package_Instantiation (A);
10394
10395 if Present (Inst) then
10396 if Nkind (Inst) = N_Package_Instantiation then
10397 return Inst;
10398
10399 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
10400 return Original_Node (Inst);
10401 end if;
10402 end if;
10403
10404 -- If the instantiation is a compilation unit that does not need body
10405 -- then the instantiation node has been rewritten as a package
10406 -- declaration for the instance, and we return the original node.
10407
10408 -- If it is a compilation unit and the instance node has not been
10409 -- rewritten, then it is still the unit of the compilation. Finally, if
10410 -- a body is present, this is a parent of the main unit whose body has
10411 -- been compiled for inlining purposes, and the instantiation node has
10412 -- been rewritten with the instance body.
10413
10414 -- Otherwise the instantiation node appears after the declaration. If
10415 -- the entity is a formal package, the declaration may have been
10416 -- rewritten as a generic declaration (in the case of a formal with box)
10417 -- or left as a formal package declaration if it has actuals, and is
10418 -- found with a forward search.
10419
10420 if Nkind (Parent (Decl)) = N_Compilation_Unit then
10421 if Nkind (Decl) = N_Package_Declaration
10422 and then Present (Corresponding_Body (Decl))
10423 then
10424 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
10425 end if;
10426
10427 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
10428 return Original_Node (Decl);
10429 else
10430 return Unit (Parent (Decl));
10431 end if;
10432
10433 elsif Nkind (Decl) = N_Package_Declaration
10434 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
10435 then
10436 return Original_Node (Decl);
10437
10438 else
10439 Inst := Next (Decl);
10440 while Nkind (Inst) not in N_Formal_Package_Declaration
10441 | N_Function_Instantiation
10442 | N_Package_Instantiation
10443 | N_Procedure_Instantiation
10444 loop
10445 Next (Inst);
10446 end loop;
10447
10448 return Inst;
10449 end if;
10450 end Get_Unit_Instantiation_Node;
10451
10452 ------------------------
10453 -- Has_Been_Exchanged --
10454 ------------------------
10455
10456 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
10457 Next : Elmt_Id;
10458
10459 begin
10460 Next := First_Elmt (Exchanged_Views);
10461 while Present (Next) loop
10462 if Full_View (Node (Next)) = E then
10463 return True;
10464 end if;
10465
10466 Next_Elmt (Next);
10467 end loop;
10468
10469 return False;
10470 end Has_Been_Exchanged;
10471
10472 -------------------
10473 -- Has_Contracts --
10474 -------------------
10475
10476 function Has_Contracts (Decl : Node_Id) return Boolean is
10477 A_List : constant List_Id := Aspect_Specifications (Decl);
10478 A_Spec : Node_Id;
10479 A_Id : Aspect_Id;
10480 begin
10481 A_Spec := First (A_List);
10482 while Present (A_Spec) loop
10483 A_Id := Get_Aspect_Id (A_Spec);
10484 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
10485 return True;
10486 end if;
10487
10488 Next (A_Spec);
10489 end loop;
10490
10491 return False;
10492 end Has_Contracts;
10493
10494 -------------------------------
10495 -- Has_Fully_Defined_Profile --
10496 -------------------------------
10497
10498 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
10499 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
10500 -- Determine whethet type Typ is fully defined
10501
10502 ---------------------------
10503 -- Is_Fully_Defined_Type --
10504 ---------------------------
10505
10506 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
10507 begin
10508 -- A private type without a full view is not fully defined
10509
10510 if Is_Private_Type (Typ)
10511 and then No (Full_View (Typ))
10512 then
10513 return False;
10514
10515 -- An incomplete type is never fully defined
10516
10517 elsif Is_Incomplete_Type (Typ) then
10518 return False;
10519
10520 -- All other types are fully defined
10521
10522 else
10523 return True;
10524 end if;
10525 end Is_Fully_Defined_Type;
10526
10527 -- Local declarations
10528
10529 Param : Entity_Id;
10530
10531 -- Start of processing for Has_Fully_Defined_Profile
10532
10533 begin
10534 -- Check the parameters
10535
10536 Param := First_Formal (Subp);
10537 while Present (Param) loop
10538 if not Is_Fully_Defined_Type (Etype (Param)) then
10539 return False;
10540 end if;
10541
10542 Next_Formal (Param);
10543 end loop;
10544
10545 -- Check the return type
10546
10547 return Is_Fully_Defined_Type (Etype (Subp));
10548 end Has_Fully_Defined_Profile;
10549
10550 ----------
10551 -- Hash --
10552 ----------
10553
10554 function Hash (F : Entity_Id) return HTable_Range is
10555 begin
10556 return HTable_Range (F mod HTable_Size);
10557 end Hash;
10558
10559 ------------------------
10560 -- Hide_Current_Scope --
10561 ------------------------
10562
10563 procedure Hide_Current_Scope is
10564 C : constant Entity_Id := Current_Scope;
10565 E : Entity_Id;
10566
10567 begin
10568 Set_Is_Hidden_Open_Scope (C);
10569
10570 E := First_Entity (C);
10571 while Present (E) loop
10572 if Is_Immediately_Visible (E) then
10573 Set_Is_Immediately_Visible (E, False);
10574 Append_Elmt (E, Hidden_Entities);
10575 end if;
10576
10577 Next_Entity (E);
10578 end loop;
10579
10580 -- Make the scope name invisible as well. This is necessary, but might
10581 -- conflict with calls to Rtsfind later on, in case the scope is a
10582 -- predefined one. There is no clean solution to this problem, so for
10583 -- now we depend on the user not redefining Standard itself in one of
10584 -- the parent units.
10585
10586 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
10587 Set_Is_Immediately_Visible (C, False);
10588 Append_Elmt (C, Hidden_Entities);
10589 end if;
10590
10591 end Hide_Current_Scope;
10592
10593 --------------
10594 -- Init_Env --
10595 --------------
10596
10597 procedure Init_Env is
10598 Saved : Instance_Env;
10599
10600 begin
10601 Saved.Instantiated_Parent := Current_Instantiated_Parent;
10602 Saved.Exchanged_Views := Exchanged_Views;
10603 Saved.Hidden_Entities := Hidden_Entities;
10604 Saved.Current_Sem_Unit := Current_Sem_Unit;
10605 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
10606 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
10607
10608 -- Save configuration switches. These may be reset if the unit is a
10609 -- predefined unit, and the current mode is not Ada 2005.
10610
10611 Saved.Switches := Save_Config_Switches;
10612
10613 Instance_Envs.Append (Saved);
10614
10615 Exchanged_Views := New_Elmt_List;
10616 Hidden_Entities := New_Elmt_List;
10617
10618 -- Make dummy entry for Instantiated parent. If generic unit is legal,
10619 -- this is set properly in Set_Instance_Env.
10620
10621 Current_Instantiated_Parent :=
10622 (Current_Scope, Current_Scope, Assoc_Null);
10623 end Init_Env;
10624
10625 ---------------------
10626 -- In_Main_Context --
10627 ---------------------
10628
10629 function In_Main_Context (E : Entity_Id) return Boolean is
10630 Context : List_Id;
10631 Clause : Node_Id;
10632 Nam : Node_Id;
10633
10634 begin
10635 if not Is_Compilation_Unit (E)
10636 or else Ekind (E) /= E_Package
10637 or else In_Private_Part (E)
10638 then
10639 return False;
10640 end if;
10641
10642 Context := Context_Items (Cunit (Main_Unit));
10643
10644 Clause := First (Context);
10645 while Present (Clause) loop
10646 if Nkind (Clause) = N_With_Clause then
10647 Nam := Name (Clause);
10648
10649 -- If the current scope is part of the context of the main unit,
10650 -- analysis of the corresponding with_clause is not complete, and
10651 -- the entity is not set. We use the Chars field directly, which
10652 -- might produce false positives in rare cases, but guarantees
10653 -- that we produce all the instance bodies we will need.
10654
10655 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
10656 or else (Nkind (Nam) = N_Selected_Component
10657 and then Chars (Selector_Name (Nam)) = Chars (E))
10658 then
10659 return True;
10660 end if;
10661 end if;
10662
10663 Next (Clause);
10664 end loop;
10665
10666 return False;
10667 end In_Main_Context;
10668
10669 ---------------------
10670 -- Inherit_Context --
10671 ---------------------
10672
10673 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
10674 Current_Context : List_Id;
10675 Current_Unit : Node_Id;
10676 Item : Node_Id;
10677 New_I : Node_Id;
10678
10679 Clause : Node_Id;
10680 OK : Boolean;
10681 Lib_Unit : Node_Id;
10682
10683 begin
10684 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
10685
10686 -- The inherited context is attached to the enclosing compilation
10687 -- unit. This is either the main unit, or the declaration for the
10688 -- main unit (in case the instantiation appears within the package
10689 -- declaration and the main unit is its body).
10690
10691 Current_Unit := Parent (Inst);
10692 while Present (Current_Unit)
10693 and then Nkind (Current_Unit) /= N_Compilation_Unit
10694 loop
10695 Current_Unit := Parent (Current_Unit);
10696 end loop;
10697
10698 Current_Context := Context_Items (Current_Unit);
10699
10700 Item := First (Context_Items (Parent (Gen_Decl)));
10701 while Present (Item) loop
10702 if Nkind (Item) = N_With_Clause then
10703 Lib_Unit := Withed_Lib_Unit (Item);
10704
10705 -- Take care to prevent direct cyclic with's
10706
10707 if Lib_Unit /= Current_Unit then
10708
10709 -- Do not add a unit if it is already in the context
10710
10711 Clause := First (Current_Context);
10712 OK := True;
10713 while Present (Clause) loop
10714 if Nkind (Clause) = N_With_Clause
10715 and then Withed_Lib_Unit (Clause) = Lib_Unit
10716 then
10717 OK := False;
10718 exit;
10719 end if;
10720
10721 Next (Clause);
10722 end loop;
10723
10724 if OK then
10725 New_I := New_Copy (Item);
10726 Set_Is_Implicit_With (New_I);
10727
10728 Append (New_I, Current_Context);
10729 end if;
10730 end if;
10731 end if;
10732
10733 Next (Item);
10734 end loop;
10735 end if;
10736 end Inherit_Context;
10737
10738 ----------------
10739 -- Initialize --
10740 ----------------
10741
10742 procedure Initialize is
10743 begin
10744 Generic_Renamings.Init;
10745 Instance_Envs.Init;
10746 Generic_Flags.Init;
10747 Generic_Renamings_HTable.Reset;
10748 Circularity_Detected := False;
10749 Exchanged_Views := No_Elist;
10750 Hidden_Entities := No_Elist;
10751 end Initialize;
10752
10753 -------------------------------------
10754 -- Insert_Freeze_Node_For_Instance --
10755 -------------------------------------
10756
10757 procedure Insert_Freeze_Node_For_Instance
10758 (N : Node_Id;
10759 F_Node : Node_Id)
10760 is
10761 function Enclosing_Body (N : Node_Id) return Node_Id;
10762 -- Find enclosing package or subprogram body, if any. Freeze node may
10763 -- be placed at end of current declarative list if previous instance
10764 -- and current one have different enclosing bodies.
10765
10766 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
10767 -- Find the local instance, if any, that declares the generic that is
10768 -- being instantiated. If present, the freeze node for this instance
10769 -- must follow the freeze node for the previous instance.
10770
10771 --------------------
10772 -- Enclosing_Body --
10773 --------------------
10774
10775 function Enclosing_Body (N : Node_Id) return Node_Id is
10776 P : Node_Id;
10777
10778 begin
10779 P := Parent (N);
10780 while Present (P)
10781 and then Nkind (Parent (P)) /= N_Compilation_Unit
10782 loop
10783 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
10784 if Nkind (Parent (P)) = N_Subunit then
10785 return Corresponding_Stub (Parent (P));
10786 else
10787 return P;
10788 end if;
10789 end if;
10790
10791 P := True_Parent (P);
10792 end loop;
10793
10794 return Empty;
10795 end Enclosing_Body;
10796
10797 -----------------------
10798 -- Previous_Instance --
10799 -----------------------
10800
10801 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
10802 S : Entity_Id;
10803
10804 begin
10805 S := Scope (Gen);
10806 while Present (S) and then S /= Standard_Standard loop
10807 if Is_Generic_Instance (S)
10808 and then In_Same_Source_Unit (S, N)
10809 then
10810 return S;
10811 end if;
10812
10813 S := Scope (S);
10814 end loop;
10815
10816 return Empty;
10817 end Previous_Instance;
10818
10819 -- Local variables
10820
10821 Decl : Node_Id;
10822 Decls : List_Id;
10823 Inst : Entity_Id;
10824 Origin : Entity_Id;
10825 Par_Inst : Node_Id;
10826 Par_N : Node_Id;
10827
10828 -- Start of processing for Insert_Freeze_Node_For_Instance
10829
10830 begin
10831 -- Nothing to do if the freeze node has already been inserted
10832
10833 if Is_List_Member (F_Node) then
10834 return;
10835 end if;
10836
10837 Inst := Entity (F_Node);
10838
10839 -- When processing a subprogram instantiation, utilize the actual
10840 -- subprogram instantiation rather than its package wrapper as it
10841 -- carries all the context information.
10842
10843 if Is_Wrapper_Package (Inst) then
10844 Inst := Related_Instance (Inst);
10845 end if;
10846
10847 Par_Inst := Parent (Inst);
10848
10849 -- If this is a package instance, check whether the generic is declared
10850 -- in a previous instance and the current instance is not within the
10851 -- previous one.
10852
10853 if Present (Generic_Parent (Par_Inst)) and then Is_In_Main_Unit (N) then
10854 declare
10855 Enclosing_N : constant Node_Id := Enclosing_Body (N);
10856 Par_I : constant Entity_Id :=
10857 Previous_Instance (Generic_Parent (Par_Inst));
10858 Scop : Entity_Id;
10859
10860 begin
10861 if Present (Par_I) and then Earlier (N, Freeze_Node (Par_I)) then
10862 Scop := Scope (Inst);
10863
10864 -- If the current instance is within the one that contains
10865 -- the generic, the freeze node for the current one must
10866 -- appear in the current declarative part. Ditto, if the
10867 -- current instance is within another package instance or
10868 -- within a body that does not enclose the current instance.
10869 -- In these three cases the freeze node of the previous
10870 -- instance is not relevant.
10871
10872 while Present (Scop) and then Scop /= Standard_Standard loop
10873 exit when Scop = Par_I
10874 or else
10875 (Is_Generic_Instance (Scop)
10876 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
10877 Scop := Scope (Scop);
10878 end loop;
10879
10880 -- Previous instance encloses current instance
10881
10882 if Scop = Par_I then
10883 null;
10884
10885 -- If the next node is a source body we must freeze in the
10886 -- current scope as well.
10887
10888 elsif Present (Next (N))
10889 and then Nkind (Next (N)) in N_Subprogram_Body
10890 | N_Package_Body
10891 and then Comes_From_Source (Next (N))
10892 then
10893 null;
10894
10895 -- Current instance is within an unrelated instance
10896
10897 elsif Is_Generic_Instance (Scop) then
10898 null;
10899
10900 -- Current instance is within an unrelated body
10901
10902 elsif Present (Enclosing_N)
10903 and then Enclosing_N /= Enclosing_Body (Par_I)
10904 then
10905 null;
10906
10907 else
10908 Insert_After (Freeze_Node (Par_I), F_Node);
10909 return;
10910 end if;
10911 end if;
10912 end;
10913 end if;
10914
10915 Decl := N;
10916 Decls := List_Containing (N);
10917 Par_N := Parent (Decls);
10918 Origin := Empty;
10919
10920 -- Determine the proper freeze point of an instantiation
10921
10922 if Is_Generic_Instance (Inst) then
10923 loop
10924 -- When the instantiation occurs in a package spec, append the
10925 -- freeze node to the private declarations (if any).
10926
10927 if Nkind (Par_N) = N_Package_Specification
10928 and then Decls = Visible_Declarations (Par_N)
10929 and then not Is_Empty_List (Private_Declarations (Par_N))
10930 then
10931 Decls := Private_Declarations (Par_N);
10932 Decl := First (Decls);
10933 end if;
10934
10935 -- We adhere to the general rule of a package or subprogram body
10936 -- causing freezing of anything before it in the same declarative
10937 -- region. In this respect, the proper freeze point of a package
10938 -- instantiation is before the first source body which follows, or
10939 -- before a stub. This ensures that entities from the instance are
10940 -- already frozen and therefore usable in source bodies.
10941
10942 if Nkind (Par_N) /= N_Package_Declaration
10943 and then
10944 not In_Same_Source_Unit (Generic_Parent (Par_Inst), Inst)
10945 then
10946 while Present (Decl) loop
10947 if ((Nkind (Decl) in N_Lib_Unit_Body
10948 or else
10949 Nkind (Decl) in N_Body_Stub)
10950 and then Comes_From_Source (Decl))
10951 or else (Present (Origin)
10952 and then Nkind (Decl) in N_Generic_Instantiation
10953 and then Instance_Spec (Decl) /= Origin)
10954 then
10955 Set_Sloc (F_Node, Sloc (Decl));
10956 Insert_Before (Decl, F_Node);
10957 return;
10958 end if;
10959
10960 Next (Decl);
10961 end loop;
10962 end if;
10963
10964 -- When the instantiation occurs in a package spec and there is
10965 -- no source body which follows, and the package has a body but
10966 -- is delayed, then insert immediately before its freeze node.
10967
10968 if Nkind (Par_N) = N_Package_Specification
10969 and then Present (Corresponding_Body (Parent (Par_N)))
10970 and then Present (Freeze_Node (Defining_Entity (Par_N)))
10971 then
10972 Set_Sloc (F_Node, Sloc (Freeze_Node (Defining_Entity (Par_N))));
10973 Insert_Before (Freeze_Node (Defining_Entity (Par_N)), F_Node);
10974 return;
10975
10976 -- When the instantiation occurs in a package spec and there is
10977 -- no source body which follows, not even of the package itself,
10978 -- then insert into the declaration list of the outer level, but
10979 -- do not jump over following instantiations in this list because
10980 -- they may have a body that has not materialized yet, see above.
10981
10982 elsif Nkind (Par_N) = N_Package_Specification
10983 and then No (Corresponding_Body (Parent (Par_N)))
10984 and then Is_List_Member (Parent (Par_N))
10985 then
10986 Decl := Parent (Par_N);
10987 Decls := List_Containing (Decl);
10988 Par_N := Parent (Decls);
10989 Origin := Decl;
10990
10991 -- In a package declaration, or if no source body which follows
10992 -- and at library level, then insert at end of list.
10993
10994 else
10995 exit;
10996 end if;
10997 end loop;
10998 end if;
10999
11000 -- Insert and adjust the Sloc of the freeze node
11001
11002 Set_Sloc (F_Node, Sloc (Last (Decls)));
11003 Insert_After (Last (Decls), F_Node);
11004 end Insert_Freeze_Node_For_Instance;
11005
11006 -----------------------------
11007 -- Install_Formal_Packages --
11008 -----------------------------
11009
11010 procedure Install_Formal_Packages (Par : Entity_Id) is
11011 E : Entity_Id;
11012 Gen : Entity_Id;
11013 Gen_E : Entity_Id := Empty;
11014
11015 begin
11016 E := First_Entity (Par);
11017
11018 -- If we are installing an instance parent, locate the formal packages
11019 -- of its generic parent.
11020
11021 if Is_Generic_Instance (Par) then
11022 Gen := Generic_Parent (Package_Specification (Par));
11023 Gen_E := First_Entity (Gen);
11024 end if;
11025
11026 while Present (E) loop
11027 if Ekind (E) = E_Package
11028 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
11029 then
11030 -- If this is the renaming for the parent instance, done
11031
11032 if Renamed_Entity (E) = Par then
11033 exit;
11034
11035 -- The visibility of a formal of an enclosing generic is already
11036 -- correct.
11037
11038 elsif Denotes_Formal_Package (E) then
11039 null;
11040
11041 elsif Present (Associated_Formal_Package (E)) then
11042 Check_Generic_Actuals (Renamed_Entity (E), True);
11043 Set_Is_Hidden (E, False);
11044
11045 -- Find formal package in generic unit that corresponds to
11046 -- (instance of) formal package in instance.
11047
11048 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
11049 Next_Entity (Gen_E);
11050 end loop;
11051
11052 if Present (Gen_E) then
11053 Map_Formal_Package_Entities (Gen_E, E);
11054 end if;
11055 end if;
11056 end if;
11057
11058 Next_Entity (E);
11059
11060 if Present (Gen_E) then
11061 Next_Entity (Gen_E);
11062 end if;
11063 end loop;
11064 end Install_Formal_Packages;
11065
11066 --------------------
11067 -- Install_Parent --
11068 --------------------
11069
11070 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
11071 Ancestors : constant Elist_Id := New_Elmt_List;
11072 S : constant Entity_Id := Current_Scope;
11073 Inst_Par : Entity_Id;
11074 First_Par : Entity_Id;
11075 Inst_Node : Node_Id;
11076 Gen_Par : Entity_Id;
11077 First_Gen : Entity_Id;
11078 Elmt : Elmt_Id;
11079
11080 procedure Install_Noninstance_Specs (Par : Entity_Id);
11081 -- Install the scopes of noninstance parent units ending with Par
11082
11083 procedure Install_Spec (Par : Entity_Id);
11084 -- The child unit is within the declarative part of the parent, so the
11085 -- declarations within the parent are immediately visible.
11086
11087 -------------------------------
11088 -- Install_Noninstance_Specs --
11089 -------------------------------
11090
11091 procedure Install_Noninstance_Specs (Par : Entity_Id) is
11092 begin
11093 if Present (Par)
11094 and then Par /= Standard_Standard
11095 and then not In_Open_Scopes (Par)
11096 then
11097 Install_Noninstance_Specs (Scope (Par));
11098 Install_Spec (Par);
11099 end if;
11100 end Install_Noninstance_Specs;
11101
11102 ------------------
11103 -- Install_Spec --
11104 ------------------
11105
11106 procedure Install_Spec (Par : Entity_Id) is
11107 Spec : constant Node_Id := Package_Specification (Par);
11108
11109 begin
11110 -- If this parent of the child instance is a top-level unit,
11111 -- then record the unit and its visibility for later resetting in
11112 -- Remove_Parent. We exclude units that are generic instances, as we
11113 -- only want to record this information for the ultimate top-level
11114 -- noninstance parent (is that always correct???).
11115
11116 if Scope (Par) = Standard_Standard
11117 and then not Is_Generic_Instance (Par)
11118 then
11119 Parent_Unit_Visible := Is_Immediately_Visible (Par);
11120 Instance_Parent_Unit := Par;
11121 end if;
11122
11123 -- Open the parent scope and make it and its declarations visible.
11124 -- If this point is not within a body, then only the visible
11125 -- declarations should be made visible, and installation of the
11126 -- private declarations is deferred until the appropriate point
11127 -- within analysis of the spec being instantiated (see the handling
11128 -- of parent visibility in Analyze_Package_Specification). This is
11129 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
11130 -- private view problems that occur when compiling instantiations of
11131 -- a generic child of that package (Generic_Dispatching_Constructor).
11132 -- If the instance freezes a tagged type, inlinings of operations
11133 -- from Ada.Tags may need the full view of type Tag. If inlining took
11134 -- proper account of establishing visibility of inlined subprograms'
11135 -- parents then it should be possible to remove this
11136 -- special check. ???
11137
11138 Push_Scope (Par);
11139 Set_Is_Immediately_Visible (Par);
11140 Install_Visible_Declarations (Par);
11141 Set_Use (Visible_Declarations (Spec));
11142
11143 if In_Body or else Is_RTU (Par, Ada_Tags) then
11144 Install_Private_Declarations (Par);
11145 Set_Use (Private_Declarations (Spec));
11146 end if;
11147 end Install_Spec;
11148
11149 -- Start of processing for Install_Parent
11150
11151 begin
11152 -- We need to install the parent instance to compile the instantiation
11153 -- of the child, but the child instance must appear in the current
11154 -- scope. Given that we cannot place the parent above the current scope
11155 -- in the scope stack, we duplicate the current scope and unstack both
11156 -- after the instantiation is complete.
11157
11158 -- If the parent is itself the instantiation of a child unit, we must
11159 -- also stack the instantiation of its parent, and so on. Each such
11160 -- ancestor is the prefix of the name in a prior instantiation.
11161
11162 -- If this is a nested instance, the parent unit itself resolves to
11163 -- a renaming of the parent instance, whose declaration we need.
11164
11165 -- Finally, the parent may be a generic (not an instance) when the
11166 -- child unit appears as a formal package.
11167
11168 Inst_Par := P;
11169
11170 if Present (Renamed_Entity (Inst_Par)) then
11171 Inst_Par := Renamed_Entity (Inst_Par);
11172 end if;
11173
11174 First_Par := Inst_Par;
11175
11176 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
11177
11178 First_Gen := Gen_Par;
11179
11180 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
11181
11182 -- Load grandparent instance as well
11183
11184 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
11185
11186 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
11187 Inst_Par := Entity (Prefix (Name (Inst_Node)));
11188
11189 if Present (Renamed_Entity (Inst_Par)) then
11190 Inst_Par := Renamed_Entity (Inst_Par);
11191 end if;
11192
11193 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
11194
11195 if Present (Gen_Par) then
11196 Prepend_Elmt (Inst_Par, Ancestors);
11197
11198 else
11199 -- Parent is not the name of an instantiation
11200
11201 Install_Noninstance_Specs (Inst_Par);
11202 exit;
11203 end if;
11204
11205 else
11206 -- Previous error
11207
11208 exit;
11209 end if;
11210 end loop;
11211
11212 if Present (First_Gen) then
11213 Append_Elmt (First_Par, Ancestors);
11214 else
11215 Install_Noninstance_Specs (First_Par);
11216 end if;
11217
11218 if not Is_Empty_Elmt_List (Ancestors) then
11219 Elmt := First_Elmt (Ancestors);
11220 while Present (Elmt) loop
11221 Install_Spec (Node (Elmt));
11222 Install_Formal_Packages (Node (Elmt));
11223 Next_Elmt (Elmt);
11224 end loop;
11225 end if;
11226
11227 if not In_Body then
11228 Push_Scope (S);
11229 end if;
11230 end Install_Parent;
11231
11232 -------------------------------
11233 -- Install_Hidden_Primitives --
11234 -------------------------------
11235
11236 procedure Install_Hidden_Primitives
11237 (Prims_List : in out Elist_Id;
11238 Gen_T : Entity_Id;
11239 Act_T : Entity_Id)
11240 is
11241 Elmt : Elmt_Id;
11242 List : Elist_Id := No_Elist;
11243 Prim_G_Elmt : Elmt_Id;
11244 Prim_A_Elmt : Elmt_Id;
11245 Prim_G : Node_Id;
11246 Prim_A : Node_Id;
11247
11248 begin
11249 -- No action needed in case of serious errors because we cannot trust
11250 -- in the order of primitives
11251
11252 if Serious_Errors_Detected > 0 then
11253 return;
11254
11255 -- No action possible if we don't have available the list of primitive
11256 -- operations
11257
11258 elsif No (Gen_T)
11259 or else not Is_Record_Type (Gen_T)
11260 or else not Is_Tagged_Type (Gen_T)
11261 or else not Is_Record_Type (Act_T)
11262 or else not Is_Tagged_Type (Act_T)
11263 then
11264 return;
11265
11266 -- There is no need to handle interface types since their primitives
11267 -- cannot be hidden
11268
11269 elsif Is_Interface (Gen_T) then
11270 return;
11271 end if;
11272
11273 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
11274
11275 if not Is_Class_Wide_Type (Act_T) then
11276 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
11277 else
11278 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
11279 end if;
11280
11281 loop
11282 -- Skip predefined primitives in the generic formal
11283
11284 while Present (Prim_G_Elmt)
11285 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
11286 loop
11287 Next_Elmt (Prim_G_Elmt);
11288 end loop;
11289
11290 -- Skip predefined primitives in the generic actual
11291
11292 while Present (Prim_A_Elmt)
11293 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
11294 loop
11295 Next_Elmt (Prim_A_Elmt);
11296 end loop;
11297
11298 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
11299
11300 Prim_G := Node (Prim_G_Elmt);
11301 Prim_A := Node (Prim_A_Elmt);
11302
11303 -- There is no need to handle interface primitives because their
11304 -- primitives are not hidden
11305
11306 exit when Present (Interface_Alias (Prim_G));
11307
11308 -- Here we install one hidden primitive
11309
11310 if Chars (Prim_G) /= Chars (Prim_A)
11311 and then Has_Suffix (Prim_A, 'P')
11312 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
11313 then
11314 Set_Chars (Prim_A, Chars (Prim_G));
11315 Append_New_Elmt (Prim_A, To => List);
11316 end if;
11317
11318 Next_Elmt (Prim_A_Elmt);
11319 Next_Elmt (Prim_G_Elmt);
11320 end loop;
11321
11322 -- Append the elements to the list of temporarily visible primitives
11323 -- avoiding duplicates.
11324
11325 if Present (List) then
11326 if No (Prims_List) then
11327 Prims_List := New_Elmt_List;
11328 end if;
11329
11330 Elmt := First_Elmt (List);
11331 while Present (Elmt) loop
11332 Append_Unique_Elmt (Node (Elmt), Prims_List);
11333 Next_Elmt (Elmt);
11334 end loop;
11335 end if;
11336 end Install_Hidden_Primitives;
11337
11338 ---------------------------------
11339 -- Renames_Standard_Subprogram --
11340 ---------------------------------
11341
11342 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
11343 Id : Entity_Id;
11344
11345 begin
11346 Id := Alias (Subp);
11347 while Present (Id) loop
11348 if Scope (Id) = Standard_Standard then
11349 return True;
11350 end if;
11351
11352 Id := Alias (Id);
11353 end loop;
11354
11355 return False;
11356 end Renames_Standard_Subprogram;
11357
11358 -------------------------------
11359 -- Restore_Hidden_Primitives --
11360 -------------------------------
11361
11362 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
11363 Prim_Elmt : Elmt_Id;
11364 Prim : Node_Id;
11365
11366 begin
11367 if Present (Prims_List) then
11368 Prim_Elmt := First_Elmt (Prims_List);
11369 while Present (Prim_Elmt) loop
11370 Prim := Node (Prim_Elmt);
11371 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
11372 Next_Elmt (Prim_Elmt);
11373 end loop;
11374
11375 Prims_List := No_Elist;
11376 end if;
11377 end Restore_Hidden_Primitives;
11378
11379 --------------------------------
11380 -- Instantiate_Formal_Package --
11381 --------------------------------
11382
11383 function Instantiate_Formal_Package
11384 (Formal : Node_Id;
11385 Actual : Node_Id;
11386 Analyzed_Formal : Node_Id) return List_Id
11387 is
11388 Loc : constant Source_Ptr := Sloc (Actual);
11389 Hidden_Formals : constant Elist_Id := New_Elmt_List;
11390
11391 Actual_Pack : Entity_Id;
11392 Formal_Pack : Entity_Id;
11393 Gen_Parent : Entity_Id;
11394 Decls : List_Id;
11395 Nod : Node_Id;
11396 Parent_Spec : Node_Id;
11397
11398 procedure Find_Matching_Actual
11399 (F : Node_Id;
11400 Act : in out Entity_Id);
11401 -- We need to associate each formal entity in the formal package with
11402 -- the corresponding entity in the actual package. The actual package
11403 -- has been analyzed and possibly expanded, and as a result there is
11404 -- no one-to-one correspondence between the two lists (for example,
11405 -- the actual may include subtypes, itypes, and inherited primitive
11406 -- operations, interspersed among the renaming declarations for the
11407 -- actuals). We retrieve the corresponding actual by name because each
11408 -- actual has the same name as the formal, and they do appear in the
11409 -- same order.
11410
11411 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
11412 -- Retrieve entity of defining entity of generic formal parameter.
11413 -- Only the declarations of formals need to be considered when
11414 -- linking them to actuals, but the declarative list may include
11415 -- internal entities generated during analysis, and those are ignored.
11416
11417 procedure Match_Formal_Entity
11418 (Formal_Node : Node_Id;
11419 Formal_Ent : Entity_Id;
11420 Actual_Ent : Entity_Id);
11421 -- Associates the formal entity with the actual. In the case where
11422 -- Formal_Ent is a formal package, this procedure iterates through all
11423 -- of its formals and enters associations between the actuals occurring
11424 -- in the formal package's corresponding actual package (given by
11425 -- Actual_Ent) and the formal package's formal parameters. This
11426 -- procedure recurses if any of the parameters is itself a package.
11427
11428 function Is_Instance_Of
11429 (Act_Spec : Entity_Id;
11430 Gen_Anc : Entity_Id) return Boolean;
11431 -- The actual can be an instantiation of a generic within another
11432 -- instance, in which case there is no direct link from it to the
11433 -- original generic ancestor. In that case, we recognize that the
11434 -- ultimate ancestor is the same by examining names and scopes.
11435
11436 procedure Process_Nested_Formal (Formal : Entity_Id);
11437 -- If the current formal is declared with a box, its own formals are
11438 -- visible in the instance, as they were in the generic, and their
11439 -- Hidden flag must be reset. If some of these formals are themselves
11440 -- packages declared with a box, the processing must be recursive.
11441
11442 --------------------------
11443 -- Find_Matching_Actual --
11444 --------------------------
11445
11446 procedure Find_Matching_Actual
11447 (F : Node_Id;
11448 Act : in out Entity_Id)
11449 is
11450 Formal_Ent : Entity_Id;
11451
11452 begin
11453 case Nkind (Original_Node (F)) is
11454 when N_Formal_Object_Declaration
11455 | N_Formal_Type_Declaration
11456 =>
11457 Formal_Ent := Defining_Identifier (F);
11458
11459 while Present (Act)
11460 and then Chars (Act) /= Chars (Formal_Ent)
11461 loop
11462 Next_Entity (Act);
11463 end loop;
11464
11465 when N_Formal_Package_Declaration
11466 | N_Formal_Subprogram_Declaration
11467 | N_Generic_Package_Declaration
11468 | N_Package_Declaration
11469 =>
11470 Formal_Ent := Defining_Entity (F);
11471
11472 while Present (Act)
11473 and then Chars (Act) /= Chars (Formal_Ent)
11474 loop
11475 Next_Entity (Act);
11476 end loop;
11477
11478 when others =>
11479 raise Program_Error;
11480 end case;
11481 end Find_Matching_Actual;
11482
11483 -------------------------
11484 -- Match_Formal_Entity --
11485 -------------------------
11486
11487 procedure Match_Formal_Entity
11488 (Formal_Node : Node_Id;
11489 Formal_Ent : Entity_Id;
11490 Actual_Ent : Entity_Id)
11491 is
11492 Act_Pkg : Entity_Id;
11493
11494 begin
11495 Set_Instance_Of (Formal_Ent, Actual_Ent);
11496
11497 if Ekind (Actual_Ent) = E_Package then
11498
11499 -- Record associations for each parameter
11500
11501 Act_Pkg := Actual_Ent;
11502
11503 declare
11504 A_Ent : Entity_Id := First_Entity (Act_Pkg);
11505 F_Ent : Entity_Id;
11506 F_Node : Node_Id;
11507
11508 Gen_Decl : Node_Id;
11509 Formals : List_Id;
11510 Actual : Entity_Id;
11511
11512 begin
11513 -- Retrieve the actual given in the formal package declaration
11514
11515 Actual := Entity (Name (Original_Node (Formal_Node)));
11516
11517 -- The actual in the formal package declaration may be a
11518 -- renamed generic package, in which case we want to retrieve
11519 -- the original generic in order to traverse its formal part.
11520
11521 if Present (Renamed_Entity (Actual)) then
11522 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
11523 else
11524 Gen_Decl := Unit_Declaration_Node (Actual);
11525 end if;
11526
11527 Formals := Generic_Formal_Declarations (Gen_Decl);
11528
11529 if Present (Formals) then
11530 F_Node := First_Non_Pragma (Formals);
11531 else
11532 F_Node := Empty;
11533 end if;
11534
11535 while Present (A_Ent)
11536 and then Present (F_Node)
11537 and then A_Ent /= First_Private_Entity (Act_Pkg)
11538 loop
11539 F_Ent := Get_Formal_Entity (F_Node);
11540
11541 if Present (F_Ent) then
11542
11543 -- This is a formal of the original package. Record
11544 -- association and recurse.
11545
11546 Find_Matching_Actual (F_Node, A_Ent);
11547 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
11548 Next_Entity (A_Ent);
11549 end if;
11550
11551 Next_Non_Pragma (F_Node);
11552 end loop;
11553 end;
11554 end if;
11555 end Match_Formal_Entity;
11556
11557 -----------------------
11558 -- Get_Formal_Entity --
11559 -----------------------
11560
11561 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
11562 Kind : constant Node_Kind := Nkind (Original_Node (N));
11563
11564 begin
11565 case Kind is
11566 when N_Formal_Object_Declaration =>
11567 return Defining_Identifier (N);
11568
11569 when N_Formal_Type_Declaration =>
11570 return Defining_Identifier (N);
11571
11572 when N_Formal_Subprogram_Declaration =>
11573 return Defining_Unit_Name (Specification (N));
11574
11575 when N_Formal_Package_Declaration =>
11576 return Defining_Identifier (Original_Node (N));
11577
11578 -- All other declarations are introduced by semantic analysis and
11579 -- have no match in the actual.
11580
11581 when others =>
11582 return Empty;
11583 end case;
11584 end Get_Formal_Entity;
11585
11586 --------------------
11587 -- Is_Instance_Of --
11588 --------------------
11589
11590 function Is_Instance_Of
11591 (Act_Spec : Entity_Id;
11592 Gen_Anc : Entity_Id) return Boolean
11593 is
11594 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
11595
11596 begin
11597 if No (Gen_Par) then
11598 return False;
11599
11600 -- Simplest case: the generic parent of the actual is the formal
11601
11602 elsif Gen_Par = Gen_Anc then
11603 return True;
11604
11605 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
11606 return False;
11607
11608 -- The actual may be obtained through several instantiations. Its
11609 -- scope must itself be an instance of a generic declared in the
11610 -- same scope as the formal. Any other case is detected above.
11611
11612 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
11613 return False;
11614
11615 else
11616 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
11617 end if;
11618 end Is_Instance_Of;
11619
11620 ---------------------------
11621 -- Process_Nested_Formal --
11622 ---------------------------
11623
11624 procedure Process_Nested_Formal (Formal : Entity_Id) is
11625 Ent : Entity_Id;
11626
11627 begin
11628 if Present (Associated_Formal_Package (Formal))
11629 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
11630 then
11631 Ent := First_Entity (Formal);
11632 while Present (Ent) loop
11633 Set_Is_Hidden (Ent, False);
11634 Set_Is_Visible_Formal (Ent);
11635 Set_Is_Potentially_Use_Visible
11636 (Ent, Is_Potentially_Use_Visible (Formal));
11637
11638 if Ekind (Ent) = E_Package then
11639 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
11640 Process_Nested_Formal (Ent);
11641 end if;
11642
11643 Next_Entity (Ent);
11644 end loop;
11645 end if;
11646 end Process_Nested_Formal;
11647
11648 -- Start of processing for Instantiate_Formal_Package
11649
11650 begin
11651 Analyze (Actual);
11652
11653 -- The actual must be a package instance, or else a current instance
11654 -- such as a parent generic within the body of a generic child.
11655
11656 if not Is_Entity_Name (Actual)
11657 or else not Is_Package_Or_Generic_Package (Entity (Actual))
11658 then
11659 Error_Msg_N
11660 ("expect package instance to instantiate formal", Actual);
11661 Abandon_Instantiation (Actual);
11662
11663 else
11664 Actual_Pack := Entity (Actual);
11665 Set_Is_Instantiated (Actual_Pack);
11666
11667 -- The actual may be a renamed package, or an outer generic formal
11668 -- package whose instantiation is converted into a renaming.
11669
11670 if Present (Renamed_Entity (Actual_Pack)) then
11671 Actual_Pack := Renamed_Entity (Actual_Pack);
11672 end if;
11673
11674 -- The analyzed formal is expected to be the result of the rewriting
11675 -- of the formal package into a regular package by analysis.
11676
11677 pragma Assert (Nkind (Analyzed_Formal) = N_Package_Declaration
11678 and then Nkind (Original_Node (Analyzed_Formal)) =
11679 N_Formal_Package_Declaration);
11680
11681 Gen_Parent := Generic_Parent (Specification (Analyzed_Formal));
11682 Formal_Pack := Defining_Unit_Name (Specification (Analyzed_Formal));
11683
11684 -- The actual for a ghost generic formal package should be a ghost
11685 -- package (SPARK RM 6.9(14)).
11686
11687 Check_Ghost_Formal_Procedure_Or_Package
11688 (N => Actual,
11689 Actual => Actual_Pack,
11690 Formal => Formal_Pack);
11691
11692 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
11693 Parent_Spec := Package_Specification (Actual_Pack);
11694 else
11695 Parent_Spec := Parent (Actual_Pack);
11696 end if;
11697
11698 if Gen_Parent = Any_Id then
11699 Error_Msg_N
11700 ("previous error in declaration of formal package", Actual);
11701 Abandon_Instantiation (Actual);
11702
11703 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
11704 null;
11705
11706 -- If this is the current instance of an enclosing generic, that unit
11707 -- is the generic package we need.
11708
11709 elsif In_Open_Scopes (Actual_Pack)
11710 and then Ekind (Actual_Pack) = E_Generic_Package
11711 then
11712 null;
11713
11714 else
11715 Error_Msg_NE
11716 ("actual parameter must be instance of&", Actual, Gen_Parent);
11717 Abandon_Instantiation (Actual);
11718 end if;
11719
11720 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
11721 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
11722
11723 Nod :=
11724 Make_Package_Renaming_Declaration (Loc,
11725 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
11726 Name => New_Occurrence_Of (Actual_Pack, Loc));
11727
11728 Set_Associated_Formal_Package
11729 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
11730 Decls := New_List (Nod);
11731
11732 -- If the formal F has a box, then the generic declarations are
11733 -- visible in the generic G. In an instance of G, the corresponding
11734 -- entities in the actual for F (which are the actuals for the
11735 -- instantiation of the generic that F denotes) must also be made
11736 -- visible for analysis of the current instance. On exit from the
11737 -- current instance, those entities are made private again. If the
11738 -- actual is currently in use, these entities are also use-visible.
11739
11740 -- The loop through the actual entities also steps through the formal
11741 -- entities and enters associations from formals to actuals into the
11742 -- renaming map. This is necessary to properly handle checking of
11743 -- actual parameter associations for later formals that depend on
11744 -- actuals declared in the formal package.
11745
11746 -- In Ada 2005, partial parameterization requires that we make
11747 -- visible the actuals corresponding to formals that were defaulted
11748 -- in the formal package. There formals are identified because they
11749 -- remain formal generics within the formal package, rather than
11750 -- being renamings of the actuals supplied.
11751
11752 declare
11753 Gen_Decl : constant Node_Id :=
11754 Unit_Declaration_Node (Gen_Parent);
11755 Formals : constant List_Id :=
11756 Generic_Formal_Declarations (Gen_Decl);
11757
11758 Actual_Ent : Entity_Id;
11759 Actual_Of_Formal : Node_Id;
11760 Formal_Node : Node_Id;
11761 Formal_Ent : Entity_Id;
11762
11763 begin
11764 if Present (Formals) then
11765 Formal_Node := First_Non_Pragma (Formals);
11766 else
11767 Formal_Node := Empty;
11768 end if;
11769
11770 Actual_Ent := First_Entity (Actual_Pack);
11771 Actual_Of_Formal :=
11772 First (Visible_Declarations (Specification (Analyzed_Formal)));
11773 while Present (Actual_Ent)
11774 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
11775 loop
11776 if Present (Formal_Node) then
11777 Formal_Ent := Get_Formal_Entity (Formal_Node);
11778
11779 if Present (Formal_Ent) then
11780 Find_Matching_Actual (Formal_Node, Actual_Ent);
11781 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
11782
11783 -- We iterate at the same time over the actuals of the
11784 -- local package created for the formal, to determine
11785 -- which one of the formals of the original generic were
11786 -- defaulted in the formal. The corresponding actual
11787 -- entities are visible in the enclosing instance.
11788
11789 if Box_Present (Formal)
11790 or else
11791 (Present (Actual_Of_Formal)
11792 and then
11793 Is_Generic_Formal
11794 (Get_Formal_Entity (Actual_Of_Formal)))
11795 then
11796 Set_Is_Hidden (Actual_Ent, False);
11797 Set_Is_Visible_Formal (Actual_Ent);
11798 Set_Is_Potentially_Use_Visible
11799 (Actual_Ent, In_Use (Actual_Pack));
11800
11801 if Ekind (Actual_Ent) = E_Package then
11802 Process_Nested_Formal (Actual_Ent);
11803 end if;
11804
11805 else
11806 if not Is_Hidden (Actual_Ent) then
11807 Append_Elmt (Actual_Ent, Hidden_Formals);
11808 end if;
11809
11810 Set_Is_Hidden (Actual_Ent);
11811 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
11812 end if;
11813 end if;
11814
11815 Next_Non_Pragma (Formal_Node);
11816
11817 -- If the actual of the local package created for the formal
11818 -- is itself an instantiated formal package, then it could
11819 -- have given rise to additional declarations, see the code
11820 -- dealing with conformance checking below.
11821
11822 if Nkind (Actual_Of_Formal) = N_Package_Renaming_Declaration
11823 and then Requires_Conformance_Checking
11824 (Declaration_Node
11825 (Associated_Formal_Package
11826 (Defining_Entity (Actual_Of_Formal))))
11827 then
11828 Next (Actual_Of_Formal);
11829 pragma Assert
11830 (Nkind (Actual_Of_Formal) = N_Package_Declaration);
11831 Next (Actual_Of_Formal);
11832 pragma Assert
11833 (Nkind (Actual_Of_Formal) = N_Package_Instantiation);
11834 end if;
11835
11836 Next (Actual_Of_Formal);
11837
11838 -- A formal subprogram may be overloaded, so advance in
11839 -- the list of actuals to make sure we do not match two
11840 -- successive formals to the same actual. This is only
11841 -- relevant for overloadable entities, others have
11842 -- distinct names.
11843
11844 if Is_Overloadable (Actual_Ent) then
11845 Next_Entity (Actual_Ent);
11846 end if;
11847
11848 else
11849 -- No further formals to match, but the generic part may
11850 -- contain inherited operation that are not hidden in the
11851 -- enclosing instance.
11852
11853 Next_Entity (Actual_Ent);
11854 end if;
11855 end loop;
11856
11857 -- Inherited subprograms generated by formal derived types are
11858 -- also visible if the types are.
11859
11860 Actual_Ent := First_Entity (Actual_Pack);
11861 while Present (Actual_Ent)
11862 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
11863 loop
11864 if Is_Overloadable (Actual_Ent)
11865 and then
11866 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
11867 and then
11868 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
11869 then
11870 Set_Is_Hidden (Actual_Ent, False);
11871 Set_Is_Potentially_Use_Visible
11872 (Actual_Ent, In_Use (Actual_Pack));
11873 end if;
11874
11875 Next_Entity (Actual_Ent);
11876 end loop;
11877 end;
11878
11879 -- If the formal requires conformance checking, reanalyze it as an
11880 -- abbreviated instantiation, to verify the matching rules of 12.7.
11881 -- The actual checks are performed after the generic associations
11882 -- have been analyzed, to guarantee the same visibility for this
11883 -- instantiation and for the actuals.
11884
11885 -- In Ada 2005, the generic associations for the formal can include
11886 -- defaulted parameters. These are ignored during check. This
11887 -- internal instantiation is removed from the tree after conformance
11888 -- checking, because it contains formal declarations for those
11889 -- defaulted parameters, and those should not reach the back-end.
11890
11891 -- This processing needs to be synchronized with the pattern matching
11892 -- done in the main loop of the above block that starts with the test
11893 -- on Requires_Conformance_Checking.
11894
11895 if Requires_Conformance_Checking (Formal) then
11896 declare
11897 I_Pack : constant Entity_Id := Make_Temporary (Loc, 'P');
11898 I_Nam : Node_Id;
11899
11900 begin
11901 Set_Is_Internal (I_Pack);
11902 Mutate_Ekind (I_Pack, E_Package);
11903
11904 -- Insert the package into the list of its hidden entities so
11905 -- that the list is not empty for Is_Abbreviated_Instance.
11906
11907 Append_Elmt (I_Pack, Hidden_Formals);
11908
11909 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
11910
11911 -- If the generic is a child unit, Check_Generic_Child_Unit
11912 -- needs its original name in case it is qualified.
11913
11914 if Is_Child_Unit (Gen_Parent) then
11915 I_Nam :=
11916 New_Copy_Tree (Name (Original_Node (Analyzed_Formal)));
11917 pragma Assert (Entity (I_Nam) = Gen_Parent
11918 or else Renamed_Entity (Entity (I_Nam)) = Gen_Parent);
11919
11920 else
11921 I_Nam :=
11922 New_Occurrence_Of (Get_Instance_Of (Gen_Parent), Loc);
11923 end if;
11924
11925 Append_To (Decls,
11926 Make_Package_Instantiation (Loc,
11927 Defining_Unit_Name => I_Pack,
11928 Name => I_Nam,
11929 Generic_Associations => Generic_Associations (Formal)));
11930 end;
11931 end if;
11932
11933 return Decls;
11934 end if;
11935 end Instantiate_Formal_Package;
11936
11937 -----------------------------------
11938 -- Instantiate_Formal_Subprogram --
11939 -----------------------------------
11940
11941 function Instantiate_Formal_Subprogram
11942 (Formal : Node_Id;
11943 Actual : Node_Id;
11944 Analyzed_Formal : Node_Id) return Node_Id
11945 is
11946 Analyzed_S : constant Entity_Id :=
11947 Defining_Unit_Name (Specification (Analyzed_Formal));
11948 Formal_Sub : constant Entity_Id :=
11949 Defining_Unit_Name (Specification (Formal));
11950
11951 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
11952 -- If the generic is a child unit, the parent has been installed on the
11953 -- scope stack, but a default subprogram cannot resolve to something
11954 -- on the parent because that parent is not really part of the visible
11955 -- context (it is there to resolve explicit local entities). If the
11956 -- default has resolved in this way, we remove the entity from immediate
11957 -- visibility and analyze the node again to emit an error message or
11958 -- find another visible candidate.
11959
11960 procedure Valid_Actual_Subprogram (Act : Node_Id);
11961 -- Perform legality check and raise exception on failure
11962
11963 -----------------------
11964 -- From_Parent_Scope --
11965 -----------------------
11966
11967 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
11968 Gen_Scope : Node_Id;
11969
11970 begin
11971 Gen_Scope := Scope (Analyzed_S);
11972 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
11973 if Scope (Subp) = Scope (Gen_Scope) then
11974 return True;
11975 end if;
11976
11977 Gen_Scope := Scope (Gen_Scope);
11978 end loop;
11979
11980 return False;
11981 end From_Parent_Scope;
11982
11983 -----------------------------
11984 -- Valid_Actual_Subprogram --
11985 -----------------------------
11986
11987 procedure Valid_Actual_Subprogram (Act : Node_Id) is
11988 Act_E : Entity_Id;
11989
11990 begin
11991 if Is_Entity_Name (Act) then
11992 Act_E := Entity (Act);
11993
11994 elsif Nkind (Act) = N_Selected_Component
11995 and then Is_Entity_Name (Selector_Name (Act))
11996 then
11997 Act_E := Entity (Selector_Name (Act));
11998
11999 else
12000 Act_E := Empty;
12001 end if;
12002
12003 -- The actual for a ghost generic formal procedure should be a ghost
12004 -- procedure (SPARK RM 6.9(14)).
12005
12006 if Present (Act_E)
12007 and then Ekind (Act_E) = E_Procedure
12008 then
12009 Check_Ghost_Formal_Procedure_Or_Package
12010 (N => Act,
12011 Actual => Act_E,
12012 Formal => Analyzed_S);
12013 end if;
12014
12015 if (Present (Act_E) and then Is_Overloadable (Act_E))
12016 or else Nkind (Act) in N_Attribute_Reference
12017 | N_Indexed_Component
12018 | N_Character_Literal
12019 | N_Explicit_Dereference
12020 then
12021 return;
12022 end if;
12023
12024 Error_Msg_NE
12025 ("expect subprogram or entry name in instantiation of &",
12026 Instantiation_Node, Formal_Sub);
12027 Abandon_Instantiation (Instantiation_Node);
12028 end Valid_Actual_Subprogram;
12029
12030 -- Local variables
12031
12032 Decl_Node : Node_Id;
12033 Loc : Source_Ptr;
12034 Nam : Node_Id;
12035 New_Spec : Node_Id;
12036 New_Subp : Entity_Id;
12037
12038 -- Start of processing for Instantiate_Formal_Subprogram
12039
12040 begin
12041 New_Spec := New_Copy_Tree (Specification (Formal));
12042
12043 -- The tree copy has created the proper instantiation sloc for the
12044 -- new specification. Use this location for all other constructed
12045 -- declarations.
12046
12047 Loc := Sloc (Defining_Unit_Name (New_Spec));
12048
12049 -- Create new entity for the actual (New_Copy_Tree does not), and
12050 -- indicate that it is an actual.
12051
12052 -- If the actual is not an entity (i.e. an attribute reference)
12053 -- and the formal includes aspect specifications for contracts,
12054 -- we create an internal name for the renaming declaration. The
12055 -- constructed wrapper contains a call to the entity in the renaming.
12056 -- This is an expansion activity, as is the wrapper creation.
12057
12058 if Ada_Version >= Ada_2022
12059 and then Has_Contracts (Analyzed_Formal)
12060 and then not Is_Entity_Name (Actual)
12061 and then Expander_Active
12062 then
12063 New_Subp := Make_Temporary (Sloc (Actual), 'S');
12064 else
12065 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
12066 end if;
12067
12068 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
12069 Set_Is_Generic_Actual_Subprogram (New_Subp);
12070 Set_Defining_Unit_Name (New_Spec, New_Subp);
12071
12072 -- Create new entities for the each of the formals in the specification
12073 -- of the renaming declaration built for the actual.
12074
12075 if Present (Parameter_Specifications (New_Spec)) then
12076 declare
12077 F : Node_Id;
12078 F_Id : Entity_Id;
12079
12080 begin
12081 F := First (Parameter_Specifications (New_Spec));
12082 while Present (F) loop
12083 F_Id := Defining_Identifier (F);
12084
12085 Set_Defining_Identifier (F,
12086 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
12087 Next (F);
12088 end loop;
12089 end;
12090 end if;
12091
12092 -- Find entity of actual. If the actual is an attribute reference, it
12093 -- cannot be resolved here (its formal is missing) but is handled
12094 -- instead in Attribute_Renaming. If the actual is overloaded, it is
12095 -- fully resolved subsequently, when the renaming declaration for the
12096 -- formal is analyzed. If it is an explicit dereference, resolve the
12097 -- prefix but not the actual itself, to prevent interpretation as call.
12098
12099 if Present (Actual) then
12100 Loc := Sloc (Actual);
12101 Set_Sloc (New_Spec, Loc);
12102
12103 if Nkind (Actual) = N_Operator_Symbol then
12104 Find_Direct_Name (Actual);
12105
12106 elsif Nkind (Actual) = N_Explicit_Dereference then
12107 Analyze (Prefix (Actual));
12108
12109 elsif Nkind (Actual) /= N_Attribute_Reference then
12110 Analyze (Actual);
12111 end if;
12112
12113 Valid_Actual_Subprogram (Actual);
12114 Nam := Actual;
12115
12116 elsif Present (Default_Name (Formal)) then
12117 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
12118 | N_Selected_Component
12119 | N_Indexed_Component
12120 | N_Character_Literal
12121 and then Present (Entity (Default_Name (Formal)))
12122 then
12123 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
12124 else
12125 Nam := New_Copy (Default_Name (Formal));
12126 Set_Sloc (Nam, Loc);
12127 end if;
12128
12129 elsif Box_Present (Formal) then
12130
12131 -- Actual is resolved at the point of instantiation. Create an
12132 -- identifier or operator with the same name as the formal.
12133
12134 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
12135 Nam :=
12136 Make_Operator_Symbol (Loc,
12137 Chars => Chars (Formal_Sub),
12138 Strval => No_String);
12139 else
12140 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
12141 end if;
12142
12143 elsif Has_Null_Default (Formal) then
12144 -- Generate null body for procedure, for use in the instance
12145
12146 Decl_Node :=
12147 Make_Subprogram_Body (Loc,
12148 Specification => New_Spec,
12149 Declarations => New_List,
12150 Handled_Statement_Sequence =>
12151 Make_Handled_Sequence_Of_Statements (Loc,
12152 Statements => New_List (Make_Null_Statement (Loc))));
12153
12154 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
12155
12156 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
12157
12158 Copy_Ghost_Aspect (Formal, To => Decl_Node);
12159
12160 -- Eliminate the calls to it when optimization is enabled
12161
12162 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
12163 return Decl_Node;
12164
12165 -- Handle case of a formal function with an expression default (allowed
12166 -- when extensions are enabled).
12167
12168 elsif Nkind (Specification (Formal)) = N_Function_Specification
12169 and then Present (Expression (Formal))
12170 then
12171 -- Generate body for function, for use in the instance
12172
12173 declare
12174 Expr : constant Node_Id := New_Copy (Expression (Formal));
12175 Stmt : constant Node_Id := Make_Simple_Return_Statement (Loc);
12176 begin
12177 Set_Sloc (Expr, Loc);
12178 Set_Expression (Stmt, Expr);
12179
12180 Decl_Node :=
12181 Make_Subprogram_Body (Loc,
12182 Specification => New_Spec,
12183 Declarations => New_List,
12184 Handled_Statement_Sequence =>
12185 Make_Handled_Sequence_Of_Statements (Loc,
12186 Statements => New_List (Stmt)));
12187 end;
12188
12189 -- RM 12.6 (16.2/2): Like a null procedure default, the function
12190 -- has convention Intrinsic.
12191
12192 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
12193
12194 -- Inline calls to it when optimization is enabled
12195
12196 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
12197 return Decl_Node;
12198
12199 else
12200 pragma Assert (False);
12201 end if;
12202
12203 Decl_Node :=
12204 Make_Subprogram_Renaming_Declaration (Loc,
12205 Specification => New_Spec,
12206 Name => Nam);
12207
12208 -- If we do not have an actual and the formal specified <> then set to
12209 -- get proper default.
12210
12211 if No (Actual) and then Box_Present (Formal) then
12212 Set_From_Default (Decl_Node);
12213 end if;
12214
12215 -- Gather possible interpretations for the actual before analyzing the
12216 -- instance. If overloaded, it will be resolved when analyzing the
12217 -- renaming declaration.
12218
12219 if Box_Present (Formal) and then No (Actual) then
12220 Analyze (Nam);
12221
12222 if Is_Child_Unit (Scope (Analyzed_S))
12223 and then Present (Entity (Nam))
12224 then
12225 if not Is_Overloaded (Nam) then
12226 if From_Parent_Scope (Entity (Nam)) then
12227 Set_Is_Immediately_Visible (Entity (Nam), False);
12228 Set_Entity (Nam, Empty);
12229 Set_Etype (Nam, Empty);
12230
12231 Analyze (Nam);
12232 Set_Is_Immediately_Visible (Entity (Nam));
12233 end if;
12234
12235 else
12236 declare
12237 I : Interp_Index;
12238 It : Interp;
12239
12240 begin
12241 Get_First_Interp (Nam, I, It);
12242 while Present (It.Nam) loop
12243 if From_Parent_Scope (It.Nam) then
12244 Remove_Interp (I);
12245 end if;
12246
12247 Get_Next_Interp (I, It);
12248 end loop;
12249 end;
12250 end if;
12251 end if;
12252 end if;
12253
12254 -- The generic instantiation freezes the actual. This can only be done
12255 -- once the actual is resolved, in the analysis of the renaming
12256 -- declaration. To make the formal subprogram entity available, we set
12257 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
12258 -- This is also needed in Analyze_Subprogram_Renaming for the processing
12259 -- of formal abstract subprograms.
12260
12261 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
12262
12263 -- We cannot analyze the renaming declaration, and thus find the actual,
12264 -- until all the actuals are assembled in the instance. For subsequent
12265 -- checks of other actuals, indicate the node that will hold the
12266 -- instance of this formal.
12267
12268 Set_Instance_Of (Analyzed_S, Nam);
12269
12270 if Nkind (Actual) = N_Selected_Component
12271 and then Is_Task_Type (Etype (Prefix (Actual)))
12272 and then not Is_Frozen (Etype (Prefix (Actual)))
12273 then
12274 -- The renaming declaration will create a body, which must appear
12275 -- outside of the instantiation, We move the renaming declaration
12276 -- out of the instance, and create an additional renaming inside,
12277 -- to prevent freezing anomalies.
12278
12279 declare
12280 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
12281
12282 begin
12283 Set_Defining_Unit_Name (New_Spec, Anon_Id);
12284 Insert_Before (Instantiation_Node, Decl_Node);
12285 Analyze (Decl_Node);
12286
12287 -- Now create renaming within the instance
12288
12289 Decl_Node :=
12290 Make_Subprogram_Renaming_Declaration (Loc,
12291 Specification => New_Copy_Tree (New_Spec),
12292 Name => New_Occurrence_Of (Anon_Id, Loc));
12293
12294 Set_Defining_Unit_Name (Specification (Decl_Node),
12295 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
12296 end;
12297 end if;
12298
12299 return Decl_Node;
12300 end Instantiate_Formal_Subprogram;
12301
12302 ------------------------
12303 -- Instantiate_Object --
12304 ------------------------
12305
12306 function Instantiate_Object
12307 (Formal : Node_Id;
12308 Actual : Node_Id;
12309 Analyzed_Formal : Node_Id) return List_Id
12310 is
12311 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
12312 A_Gen_Obj : constant Entity_Id :=
12313 Defining_Identifier (Analyzed_Formal);
12314 Acc_Def : Node_Id := Empty;
12315 Act_Assoc : constant Node_Id :=
12316 (if No (Actual) then Empty else Parent (Actual));
12317 Actual_Decl : Node_Id := Empty;
12318 Decl_Node : Node_Id;
12319 Def : Node_Id;
12320 Ftyp : Entity_Id;
12321 List : constant List_Id := New_List;
12322 Loc : constant Source_Ptr := Sloc (Actual);
12323 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
12324 Subt_Decl : Node_Id := Empty;
12325 Subt_Mark : Node_Id := Empty;
12326
12327 -- Start of processing for Instantiate_Object
12328
12329 begin
12330 -- Formal may be an anonymous access
12331
12332 if Present (Subtype_Mark (Formal)) then
12333 Subt_Mark := Subtype_Mark (Formal);
12334 else
12335 Check_Access_Definition (Formal);
12336 Acc_Def := Access_Definition (Formal);
12337 end if;
12338
12339 Set_Parent (List, Act_Assoc);
12340
12341 -- OUT present
12342
12343 if Out_Present (Formal) then
12344
12345 -- An IN OUT generic actual must be a name. The instantiation is a
12346 -- renaming declaration. The actual is the name being renamed. We
12347 -- use the actual directly, rather than a copy, because it is not
12348 -- used further in the list of actuals, and because a copy or a use
12349 -- of Relocate_Node is incorrect if the instance is nested within a
12350 -- generic. In order to simplify e.g. ASIS queries, the
12351 -- Generic_Parent field links the declaration to the generic
12352 -- association.
12353
12354 if Present (Subt_Mark) then
12355 Decl_Node :=
12356 Make_Object_Renaming_Declaration (Loc,
12357 Defining_Identifier => New_Copy (Gen_Obj),
12358 Subtype_Mark => New_Copy_Tree (Subt_Mark),
12359 Name => Actual);
12360
12361 else pragma Assert (Present (Acc_Def));
12362 Decl_Node :=
12363 Make_Object_Renaming_Declaration (Loc,
12364 Defining_Identifier => New_Copy (Gen_Obj),
12365 Access_Definition => New_Copy_Tree (Acc_Def),
12366 Name => Actual);
12367 end if;
12368
12369 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
12370
12371 -- The analysis of the actual may produce Insert_Action nodes, so
12372 -- the declaration must have a context in which to attach them.
12373
12374 Append (Decl_Node, List);
12375 Analyze (Actual);
12376
12377 -- Return if the analysis of the actual reported some error
12378
12379 if Etype (Actual) = Any_Type then
12380 return List;
12381 end if;
12382
12383 -- This check is performed here because Analyze_Object_Renaming will
12384 -- not check it when Comes_From_Source is False. Note though that the
12385 -- check for the actual being the name of an object will be performed
12386 -- in Analyze_Object_Renaming.
12387
12388 if Is_Object_Reference (Actual)
12389 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
12390 then
12391 Error_Msg_N
12392 ("illegal discriminant-dependent component for in out parameter",
12393 Actual);
12394 elsif Depends_On_Mutably_Tagged_Ext_Comp (Actual) then
12395 Error_Msg_N
12396 ("illegal mutably tagged component for in out parameter",
12397 Actual);
12398 end if;
12399
12400 -- The actual has to be resolved in order to check that it is a
12401 -- variable (due to cases such as F (1), where F returns access to
12402 -- an array, and for overloaded prefixes).
12403
12404 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
12405
12406 -- If the type of the formal is not itself a formal, and the current
12407 -- unit is a child unit, the formal type must be declared in a
12408 -- parent, and must be retrieved by visibility.
12409
12410 if Ftyp = Orig_Ftyp
12411 and then Is_Generic_Unit (Scope (Ftyp))
12412 and then Is_Child_Unit (Scope (A_Gen_Obj))
12413 then
12414 declare
12415 Temp : constant Node_Id :=
12416 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
12417 begin
12418 Set_Entity (Temp, Empty);
12419 Find_Type (Temp);
12420 Ftyp := Entity (Temp);
12421 end;
12422 end if;
12423
12424 if Is_Private_Type (Ftyp)
12425 and then not Is_Private_Type (Etype (Actual))
12426 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
12427 or else Base_Type (Etype (Actual)) = Ftyp)
12428 then
12429 -- If the actual has the type of the full view of the formal, or
12430 -- else a non-private subtype of the formal, then the visibility
12431 -- of the formal type has changed. Add to the actuals a subtype
12432 -- declaration that will force the exchange of views in the body
12433 -- of the instance as well.
12434
12435 Subt_Decl :=
12436 Make_Subtype_Declaration (Loc,
12437 Defining_Identifier => Make_Temporary (Loc, 'P'),
12438 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
12439
12440 Prepend (Subt_Decl, List);
12441
12442 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
12443 Exchange_Declarations (Ftyp);
12444 end if;
12445
12446 Resolve (Actual, Ftyp);
12447
12448 if not Denotes_Variable (Actual) then
12449 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
12450
12451 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
12452
12453 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
12454 -- the type of the actual shall resolve to a specific anonymous
12455 -- access type.
12456
12457 if Ada_Version < Ada_2005
12458 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
12459 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
12460 then
12461 Error_Msg_NE
12462 ("type of actual does not match type of&", Actual, Gen_Obj);
12463 end if;
12464 end if;
12465
12466 Note_Possible_Modification (Actual, Sure => True);
12467
12468 -- Check for instantiation with atomic/volatile/VFA object actual for
12469 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
12470
12471 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
12472 Error_Msg_NE
12473 ("cannot instantiate nonatomic formal & of mode in out",
12474 Actual, Gen_Obj);
12475 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
12476
12477 elsif Is_Volatile_Object_Ref (Actual)
12478 and then not Is_Volatile (Orig_Ftyp)
12479 then
12480 Error_Msg_NE
12481 ("cannot instantiate nonvolatile formal & of mode in out",
12482 Actual, Gen_Obj);
12483 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
12484
12485 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
12486 and then not Is_Volatile_Full_Access (Orig_Ftyp)
12487 then
12488 Error_Msg_NE
12489 ("cannot instantiate nonfull access formal & of mode in out",
12490 Actual, Gen_Obj);
12491 Error_Msg_N
12492 ("\with full access object actual (RM C.6(12))", Actual);
12493 end if;
12494
12495 -- Check for instantiation on nonatomic subcomponent of a full access
12496 -- object in Ada 2022 (RM C.6 (12)).
12497
12498 if Ada_Version >= Ada_2022
12499 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
12500 and then not Is_Atomic_Object (Actual)
12501 then
12502 Error_Msg_NE
12503 ("cannot instantiate formal & of mode in out with actual",
12504 Actual, Gen_Obj);
12505 Error_Msg_N
12506 ("\nonatomic subcomponent of full access object (RM C.6(12))",
12507 Actual);
12508 end if;
12509
12510 -- The actual for a ghost generic formal IN OUT parameter should be a
12511 -- ghost object (SPARK RM 6.9(14)).
12512
12513 Check_Ghost_Formal_Variable
12514 (Actual => Actual,
12515 Formal => A_Gen_Obj);
12516
12517 -- Formal in-mode parameter
12518
12519 else
12520 -- The instantiation of a generic formal in-mode parameter is a
12521 -- constant declaration. The actual is the expression for that
12522 -- declaration. Its type is a full copy of the type of the
12523 -- formal. This may be an access to subprogram, for which we need
12524 -- to generate entities for the formals in the new signature.
12525
12526 if Present (Actual) then
12527 if Present (Subt_Mark) then
12528 Def := New_Copy_Tree (Subt_Mark);
12529 else
12530 pragma Assert (Present (Acc_Def));
12531 Def := New_Copy_Tree (Acc_Def);
12532 end if;
12533
12534 Decl_Node :=
12535 Make_Object_Declaration (Loc,
12536 Defining_Identifier => New_Copy (Gen_Obj),
12537 Constant_Present => True,
12538 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
12539 Object_Definition => Def,
12540 Expression => Actual);
12541
12542 Copy_Ghost_Aspect (Formal, To => Decl_Node);
12543 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
12544
12545 -- A generic formal object of a tagged type is defined to be
12546 -- aliased so the new constant must also be treated as aliased.
12547
12548 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
12549 Set_Aliased_Present (Decl_Node);
12550 end if;
12551
12552 Append (Decl_Node, List);
12553
12554 -- The actual for a ghost generic formal IN parameter of
12555 -- access-to-variable type should be a ghost object (SPARK
12556 -- RM 6.9(14)).
12557
12558 if Is_Access_Variable (Etype (A_Gen_Obj)) then
12559 Check_Ghost_Formal_Variable
12560 (Actual => Actual,
12561 Formal => A_Gen_Obj);
12562 end if;
12563
12564 -- No need to repeat (pre-)analysis of some expression nodes
12565 -- already handled in Preanalyze_Actuals.
12566
12567 if Nkind (Actual) /= N_Allocator then
12568 Analyze (Actual);
12569
12570 -- Return if the analysis of the actual reported some error
12571
12572 if Etype (Actual) = Any_Type then
12573 return List;
12574 end if;
12575 end if;
12576
12577 declare
12578 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
12579 Typ : Entity_Id;
12580
12581 begin
12582 Typ := Get_Instance_Of (Formal_Type);
12583
12584 -- If the actual appears in the current or an enclosing scope,
12585 -- use its type directly. This is relevant if it has an actual
12586 -- subtype that is distinct from its nominal one. This cannot
12587 -- be done in general because the type of the actual may
12588 -- depend on other actuals, and only be fully determined when
12589 -- the enclosing instance is analyzed.
12590
12591 if Present (Etype (Actual))
12592 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
12593 then
12594 Freeze_Before (Instantiation_Node, Etype (Actual));
12595 else
12596 Freeze_Before (Instantiation_Node, Typ);
12597 end if;
12598
12599 -- If the actual is an aggregate, perform name resolution on
12600 -- its components (the analysis of an aggregate does not do it)
12601 -- to capture local names that may be hidden if the generic is
12602 -- a child unit.
12603
12604 if Nkind (Actual) = N_Aggregate then
12605 Preanalyze_And_Resolve (Actual, Typ);
12606 end if;
12607
12608 if Is_Limited_Type (Typ)
12609 and then not OK_For_Limited_Init (Typ, Actual)
12610 then
12611 Error_Msg_N
12612 ("initialization not allowed for limited types", Actual);
12613 Explain_Limited_Type (Typ, Actual);
12614 end if;
12615 end;
12616
12617 elsif Present (Default_Expression (Formal)) then
12618
12619 -- Use default to construct declaration
12620
12621 if Present (Subt_Mark) then
12622 Def := New_Copy_Tree (Subt_Mark);
12623 else
12624 pragma Assert (Present (Acc_Def));
12625 Def := New_Copy_Tree (Acc_Def);
12626 end if;
12627
12628 Decl_Node :=
12629 Make_Object_Declaration (Sloc (Formal),
12630 Defining_Identifier => New_Copy (Gen_Obj),
12631 Constant_Present => True,
12632 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
12633 Object_Definition => Def,
12634 Expression => New_Copy_Tree
12635 (Default_Expression (Formal)));
12636
12637 Copy_Ghost_Aspect (Formal, To => Decl_Node);
12638 Set_Corresponding_Generic_Association
12639 (Decl_Node, Expression (Decl_Node));
12640
12641 Append (Decl_Node, List);
12642 Set_Analyzed (Expression (Decl_Node), False);
12643
12644 else
12645 pragma Assert (False);
12646 end if;
12647 end if;
12648
12649 if Nkind (Actual) in N_Has_Entity
12650 and then Present (Entity (Actual))
12651 then
12652 Actual_Decl := Parent (Entity (Actual));
12653 end if;
12654
12655 -- Ada 2005 (AI-423) refined by AI12-0287:
12656 -- For an object_renaming_declaration with a null_exclusion or an
12657 -- access_definition that has a null_exclusion, the subtype of the
12658 -- object_name shall exclude null. In addition, if the
12659 -- object_renaming_declaration occurs within the body of a generic unit
12660 -- G or within the body of a generic unit declared within the
12661 -- declarative region of generic unit G, then:
12662 -- * if the object_name statically denotes a generic formal object of
12663 -- mode in out of G, then the declaration of that object shall have a
12664 -- null_exclusion;
12665 -- * if the object_name statically denotes a call of a generic formal
12666 -- function of G, then the declaration of the result of that function
12667 -- shall have a null_exclusion.
12668
12669 if Ada_Version >= Ada_2005
12670 and then Present (Actual_Decl)
12671 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
12672 | N_Object_Declaration
12673 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
12674 and then not Has_Null_Exclusion (Actual_Decl)
12675 and then Has_Null_Exclusion (Analyzed_Formal)
12676 and then Ekind (Defining_Identifier (Analyzed_Formal))
12677 = E_Generic_In_Out_Parameter
12678 and then ((In_Generic_Scope (Entity (Actual))
12679 and then In_Package_Body (Scope (Entity (Actual))))
12680 or else not Can_Never_Be_Null (Etype (Actual)))
12681 then
12682 Error_Msg_Sloc := Sloc (Analyzed_Formal);
12683 Error_Msg_N
12684 ("actual must exclude null to match generic formal#", Actual);
12685 end if;
12686
12687 return List;
12688 end Instantiate_Object;
12689
12690 ------------------------------
12691 -- Instantiate_Package_Body --
12692 ------------------------------
12693
12694 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12695 -- must be replaced by gotos which jump to the end of the routine in order
12696 -- to restore the Ghost and SPARK modes.
12697
12698 procedure Instantiate_Package_Body
12699 (Body_Info : Pending_Body_Info;
12700 Inlined_Body : Boolean := False;
12701 Body_Optional : Boolean := False)
12702 is
12703 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12704 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12705 Act_Spec : constant Node_Id := Specification (Act_Decl);
12706 Ctx_Parents : Elist_Id := No_Elist;
12707 Ctx_Top : Int := 0;
12708 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12709 Gen_Id : constant Node_Id := Name (Inst_Node);
12710 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12711 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12712 Loc : constant Source_Ptr := Sloc (Inst_Node);
12713
12714 procedure Check_Initialized_Types;
12715 -- In a generic package body, an entity of a generic private type may
12716 -- appear uninitialized. This is suspicious, unless the actual is a
12717 -- fully initialized type.
12718
12719 procedure Install_Parents_Of_Generic_Context
12720 (Inst_Scope : Entity_Id;
12721 Ctx_Parents : out Elist_Id);
12722 -- Inst_Scope is the scope where the instance appears within; when it
12723 -- appears within a generic child package G, this routine collects and
12724 -- installs the enclosing packages of G in the scopes stack; installed
12725 -- packages are returned in Ctx_Parents.
12726
12727 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
12728 -- Reverse effect after instantiation is complete
12729
12730 -----------------------------
12731 -- Check_Initialized_Types --
12732 -----------------------------
12733
12734 procedure Check_Initialized_Types is
12735 Decl : Node_Id;
12736 Formal : Entity_Id;
12737 Actual : Entity_Id;
12738 Uninit_Var : Entity_Id;
12739
12740 begin
12741 Decl := First (Generic_Formal_Declarations (Gen_Decl));
12742 while Present (Decl) loop
12743 Uninit_Var := Empty;
12744
12745 if Nkind (Decl) = N_Private_Extension_Declaration then
12746 Uninit_Var := Uninitialized_Variable (Decl);
12747
12748 elsif Nkind (Decl) = N_Formal_Type_Declaration
12749 and then Nkind (Formal_Type_Definition (Decl)) =
12750 N_Formal_Private_Type_Definition
12751 then
12752 Uninit_Var :=
12753 Uninitialized_Variable (Formal_Type_Definition (Decl));
12754 end if;
12755
12756 if Present (Uninit_Var) then
12757 Formal := Defining_Identifier (Decl);
12758 Actual := First_Entity (Act_Decl_Id);
12759
12760 -- For each formal there is a subtype declaration that renames
12761 -- the actual and has the same name as the formal. Locate the
12762 -- formal for warning message about uninitialized variables
12763 -- in the generic, for which the actual type should be a fully
12764 -- initialized type.
12765
12766 while Present (Actual) loop
12767 exit when Ekind (Actual) = E_Package
12768 and then Present (Renamed_Entity (Actual));
12769
12770 if Chars (Actual) = Chars (Formal)
12771 and then not Is_Scalar_Type (Actual)
12772 and then not Is_Fully_Initialized_Type (Actual)
12773 and then Warn_On_No_Value_Assigned
12774 then
12775 Error_Msg_Node_2 := Formal;
12776 Error_Msg_NE
12777 ("generic unit has uninitialized variable& of "
12778 & "formal private type &?v?", Actual, Uninit_Var);
12779 Error_Msg_NE
12780 ("actual type for& should be fully initialized type?v?",
12781 Actual, Formal);
12782 exit;
12783 end if;
12784
12785 Next_Entity (Actual);
12786 end loop;
12787 end if;
12788
12789 Next (Decl);
12790 end loop;
12791 end Check_Initialized_Types;
12792
12793 ----------------------------------------
12794 -- Install_Parents_Of_Generic_Context --
12795 ----------------------------------------
12796
12797 procedure Install_Parents_Of_Generic_Context
12798 (Inst_Scope : Entity_Id;
12799 Ctx_Parents : out Elist_Id)
12800 is
12801 Elmt : Elmt_Id;
12802 S : Entity_Id;
12803
12804 begin
12805 Ctx_Parents := New_Elmt_List;
12806
12807 -- Collect context parents (ie. parents where the instantiation
12808 -- appears within).
12809
12810 S := Inst_Scope;
12811 while S /= Standard_Standard loop
12812 Prepend_Elmt (S, Ctx_Parents);
12813 S := Scope (S);
12814 end loop;
12815
12816 -- Install enclosing parents
12817
12818 Elmt := First_Elmt (Ctx_Parents);
12819 while Present (Elmt) loop
12820 Push_Scope (Node (Elmt));
12821 Set_Is_Immediately_Visible (Node (Elmt));
12822 Next_Elmt (Elmt);
12823 end loop;
12824 end Install_Parents_Of_Generic_Context;
12825
12826 ---------------------------------------
12827 -- Remove_Parents_Of_Generic_Context --
12828 ---------------------------------------
12829
12830 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
12831 Elmt : Elmt_Id;
12832
12833 begin
12834 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
12835
12836 Elmt := Last_Elmt (Ctx_Parents);
12837 while Present (Elmt) loop
12838 pragma Assert (Current_Scope = Node (Elmt));
12839 Set_Is_Immediately_Visible (Current_Scope, False);
12840 Pop_Scope;
12841
12842 Remove_Last_Elmt (Ctx_Parents);
12843 Elmt := Last_Elmt (Ctx_Parents);
12844 end loop;
12845 end Remove_Parents_Of_Generic_Context;
12846
12847 -- Local variables
12848
12849 -- The following constants capture the context prior to instantiating
12850 -- the package body.
12851
12852 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12853 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12854 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12855 Saved_ISMP : constant Boolean :=
12856 Ignore_SPARK_Mode_Pragmas_In_Instance;
12857 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12858 Local_Suppress_Stack_Top;
12859 Saved_SC : constant Boolean := Style_Check;
12860 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12861 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12862 Saved_SS : constant Suppress_Record := Scope_Suppress;
12863 Saved_Warn : constant Warnings_State := Save_Warnings;
12864
12865 Act_Body : Node_Id;
12866 Act_Body_Id : Entity_Id;
12867 Act_Body_Name : Node_Id;
12868 Gen_Body : Node_Id;
12869 Gen_Body_Id : Node_Id;
12870 Par_Ent : Entity_Id := Empty;
12871 Par_Installed : Boolean := False;
12872 Par_Vis : Boolean := False;
12873
12874 Scope_Check_Id : Entity_Id;
12875 Scope_Check_Last : Nat;
12876 -- Value of Current_Scope before calls to Install_Parents; used to check
12877 -- that scopes are correctly removed after instantiation.
12878
12879 Vis_Prims_List : Elist_Id := No_Elist;
12880 -- List of primitives made temporarily visible in the instantiation
12881 -- to match the visibility of the formal type.
12882
12883 -- Start of processing for Instantiate_Package_Body
12884
12885 begin
12886 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12887
12888 -- The instance body may already have been processed, as the parent of
12889 -- another instance that is inlined (Load_Parent_Of_Generic).
12890
12891 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12892 return;
12893 end if;
12894
12895 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12896
12897 -- Re-establish the state of information on which checks are suppressed.
12898 -- This information was set in Body_Info at the point of instantiation,
12899 -- and now we restore it so that the instance is compiled using the
12900 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12901
12902 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12903 Scope_Suppress := Body_Info.Scope_Suppress;
12904
12905 Restore_Config_Switches (Body_Info.Config_Switches);
12906 Restore_Warnings (Body_Info.Warnings);
12907
12908 if No (Gen_Body_Id) then
12909
12910 -- Do not look for parent of generic body if none is required.
12911 -- This may happen when the routine is called as part of the
12912 -- Pending_Instantiations processing, when nested instances
12913 -- may precede the one generated from the main unit.
12914
12915 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
12916 and then Body_Optional
12917 then
12918 goto Leave;
12919 else
12920 Load_Parent_Of_Generic
12921 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12922
12923 -- Surprisingly enough, loading the body of the parent can cause
12924 -- the body to be instantiated and the double instantiation needs
12925 -- to be prevented in order to avoid giving bogus semantic errors.
12926
12927 -- This case can occur because of the Collect_Previous_Instances
12928 -- machinery of Load_Parent_Of_Generic, which will instantiate
12929 -- bodies that are deemed to be ahead of the body of the parent
12930 -- in the compilation unit. But the relative position of these
12931 -- bodies is computed using the mere comparison of their Sloc.
12932
12933 -- Now suppose that you have two generic packages G and H, with
12934 -- G containing a mere instantiation of H:
12935
12936 -- generic
12937 -- package H is
12938
12939 -- generic
12940 -- package Nested_G is
12941 -- ...
12942 -- end Nested_G;
12943
12944 -- end H;
12945
12946 -- with H;
12947
12948 -- generic
12949 -- package G is
12950
12951 -- package My_H is new H;
12952
12953 -- end G;
12954
12955 -- and a third package Q instantiating G and Nested_G:
12956
12957 -- with G;
12958
12959 -- package Q is
12960
12961 -- package My_G is new G;
12962
12963 -- package My_Nested_G is new My_G.My_H.Nested_G;
12964
12965 -- end Q;
12966
12967 -- The body to be instantiated is that of My_Nested_G and its
12968 -- parent is the instance My_G.My_H. This latter instantiation
12969 -- is done when My_G is analyzed, i.e. after the declarations
12970 -- of My_G and My_Nested_G have been parsed; as a result, the
12971 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
12972
12973 -- Therefore loading the body of My_G.My_H will cause the body
12974 -- of My_Nested_G to be instantiated because it is deemed to be
12975 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
12976 -- will again be invoked on My_G.My_H, but this time with the
12977 -- Collect_Previous_Instances machinery disabled, so there is
12978 -- no endless mutual recursion and things are done in order.
12979
12980 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12981 goto Leave;
12982 end if;
12983
12984 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12985 end if;
12986 end if;
12987
12988 -- Establish global variable for sloc adjustment and for error recovery
12989 -- In the case of an instance body for an instantiation with actuals
12990 -- from a limited view, the instance body is placed at the beginning
12991 -- of the enclosing package body: use the body entity as the source
12992 -- location for nodes of the instance body.
12993
12994 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
12995 declare
12996 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12997 Body_Id : constant Node_Id :=
12998 Corresponding_Body (Unit_Declaration_Node (Scop));
12999
13000 begin
13001 Instantiation_Node := Body_Id;
13002 end;
13003 else
13004 Instantiation_Node := Inst_Node;
13005 end if;
13006
13007 -- The package being instantiated may be subject to pragma Ghost. Set
13008 -- the mode now to ensure that any nodes generated during instantiation
13009 -- are properly marked as Ghost.
13010
13011 Set_Ghost_Mode (Act_Decl_Id);
13012
13013 if Present (Gen_Body_Id) then
13014 Save_Env (Gen_Unit, Act_Decl_Id);
13015 Style_Check := False;
13016
13017 -- If the context of the instance is subject to SPARK_Mode "off", the
13018 -- annotation is missing, or the body is instantiated at a later pass
13019 -- and its spec ignored SPARK_Mode pragma, set the global flag which
13020 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
13021 -- instance.
13022
13023 if SPARK_Mode /= On
13024 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
13025 then
13026 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
13027 end if;
13028
13029 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
13030 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
13031
13032 Create_Instantiation_Source
13033 (Inst_Node, Gen_Body_Id, S_Adjustment);
13034
13035 Act_Body :=
13036 Copy_Generic_Node
13037 (Original_Node (Gen_Body), Empty, Instantiating => True);
13038
13039 -- Create proper (possibly qualified) defining name for the body, to
13040 -- correspond to the one in the spec.
13041
13042 Act_Body_Id :=
13043 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
13044 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
13045
13046 -- Some attributes of spec entity are not inherited by body entity
13047
13048 Set_Handler_Records (Act_Body_Id, No_List);
13049
13050 if Nkind (Defining_Unit_Name (Act_Spec)) =
13051 N_Defining_Program_Unit_Name
13052 then
13053 Act_Body_Name :=
13054 Make_Defining_Program_Unit_Name (Loc,
13055 Name =>
13056 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
13057 Defining_Identifier => Act_Body_Id);
13058 else
13059 Act_Body_Name := Act_Body_Id;
13060 end if;
13061
13062 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
13063
13064 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
13065 Check_Generic_Actuals (Act_Decl_Id, False);
13066 Check_Initialized_Types;
13067
13068 -- Install primitives hidden at the point of the instantiation but
13069 -- visible when processing the generic formals
13070
13071 declare
13072 E : Entity_Id;
13073
13074 begin
13075 E := First_Entity (Act_Decl_Id);
13076 while Present (E) loop
13077 if Is_Type (E)
13078 and then not Is_Itype (E)
13079 and then Is_Generic_Actual_Type (E)
13080 and then Is_Tagged_Type (E)
13081 then
13082 Install_Hidden_Primitives
13083 (Prims_List => Vis_Prims_List,
13084 Gen_T => Generic_Parent_Type (Parent (E)),
13085 Act_T => E);
13086 end if;
13087
13088 Next_Entity (E);
13089 end loop;
13090 end;
13091
13092 Scope_Check_Id := Current_Scope;
13093 Scope_Check_Last := Scope_Stack.Last;
13094
13095 -- If the instantiation appears within a generic child some actual
13096 -- parameter may be the current instance of the enclosing generic
13097 -- parent.
13098
13099 declare
13100 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
13101
13102 begin
13103 if Is_Child_Unit (Inst_Scope)
13104 and then Ekind (Inst_Scope) = E_Generic_Package
13105 and then Present (Generic_Associations (Inst_Node))
13106 then
13107 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
13108
13109 -- Hide them from visibility; required to avoid conflicts
13110 -- installing the parent instance.
13111
13112 if Present (Ctx_Parents) then
13113 Push_Scope (Standard_Standard);
13114 Ctx_Top := Scope_Stack.Last;
13115 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
13116 end if;
13117 end if;
13118 end;
13119
13120 -- If it is a child unit, make the parent instance (which is an
13121 -- instance of the parent of the generic) visible.
13122
13123 -- 1) The child unit's parent is an explicit parent instance (the
13124 -- prefix of the name of the generic unit):
13125
13126 -- package Child_Package is new Parent_Instance.Child_Unit;
13127
13128 -- 2) The child unit's parent is an implicit parent instance (e.g.
13129 -- when instantiating a sibling package):
13130
13131 -- generic
13132 -- package Parent.Second_Child is
13133 -- ...
13134
13135 -- generic
13136 -- package Parent.First_Child is
13137 -- package Sibling_Package is new Second_Child;
13138
13139 -- 3) The child unit's parent is not an instance, so the scope is
13140 -- simply the one of the unit.
13141
13142 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
13143 and then Nkind (Gen_Id) = N_Expanded_Name
13144 then
13145 Par_Ent := Entity (Prefix (Gen_Id));
13146
13147 elsif Ekind (Scope (Gen_Unit)) = E_Generic_Package
13148 and then Ekind (Scope (Act_Decl_Id)) = E_Package
13149 and then Is_Generic_Instance (Scope (Act_Decl_Id))
13150 and then Nkind
13151 (Name (Get_Unit_Instantiation_Node
13152 (Scope (Act_Decl_Id)))) = N_Expanded_Name
13153 then
13154 Par_Ent := Entity
13155 (Prefix (Name (Get_Unit_Instantiation_Node
13156 (Scope (Act_Decl_Id)))));
13157
13158 elsif Is_Child_Unit (Gen_Unit) then
13159 Par_Ent := Scope (Gen_Unit);
13160 end if;
13161
13162 if Present (Par_Ent) then
13163 Par_Vis := Is_Immediately_Visible (Par_Ent);
13164 Install_Parent (Par_Ent, In_Body => True);
13165 Par_Installed := True;
13166 end if;
13167
13168 -- If the instantiation is a library unit, and this is the main unit,
13169 -- then build the resulting compilation unit nodes for the instance.
13170 -- If this is a compilation unit but it is not the main unit, then it
13171 -- is the body of a unit in the context, that is being compiled
13172 -- because it is encloses some inlined unit or another generic unit
13173 -- being instantiated. In that case, this body is not part of the
13174 -- current compilation, and is not attached to the tree, but its
13175 -- parent must be set for analysis.
13176
13177 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
13178
13179 -- Replace instance node with body of instance, and create new
13180 -- node for corresponding instance declaration.
13181
13182 Build_Instance_Compilation_Unit_Nodes
13183 (Inst_Node, Act_Body, Act_Decl);
13184
13185 -- If the instantiation appears within a generic child package
13186 -- enable visibility of current instance of enclosing generic
13187 -- parents.
13188
13189 if Present (Ctx_Parents) then
13190 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
13191 Analyze (Inst_Node);
13192 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
13193 else
13194 Analyze (Inst_Node);
13195 end if;
13196
13197 if Parent (Inst_Node) = Cunit (Main_Unit) then
13198
13199 -- If the instance is a child unit itself, then set the scope
13200 -- of the expanded body to be the parent of the instantiation
13201 -- (ensuring that the fully qualified name will be generated
13202 -- for the elaboration subprogram).
13203
13204 if Nkind (Defining_Unit_Name (Act_Spec)) =
13205 N_Defining_Program_Unit_Name
13206 then
13207 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
13208 end if;
13209 end if;
13210
13211 -- Case where instantiation is not a library unit
13212
13213 else
13214 -- Handle the case of an instance with incomplete actual types.
13215 -- The instance body cannot be placed just after the declaration
13216 -- because full views have not been seen yet. Any use of the non-
13217 -- limited views in the instance body requires the presence of a
13218 -- regular with_clause in the enclosing unit. Therefore we place
13219 -- the instance body at the beginning of the enclosing body, and
13220 -- the freeze node for the instance is then placed after the body.
13221
13222 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id))
13223 and then Ekind (Scope (Act_Decl_Id)) = E_Package
13224 then
13225 declare
13226 Scop : constant Entity_Id := Scope (Act_Decl_Id);
13227 Body_Id : constant Node_Id :=
13228 Corresponding_Body (Unit_Declaration_Node (Scop));
13229
13230 F_Node : Node_Id;
13231
13232 begin
13233 pragma Assert (Present (Body_Id));
13234
13235 Prepend (Act_Body, Declarations (Parent (Body_Id)));
13236
13237 if Expander_Active then
13238 Ensure_Freeze_Node (Act_Decl_Id);
13239 F_Node := Freeze_Node (Act_Decl_Id);
13240 Set_Is_Frozen (Act_Decl_Id, False);
13241 if Is_List_Member (F_Node) then
13242 Remove (F_Node);
13243 end if;
13244
13245 Insert_After (Act_Body, F_Node);
13246 end if;
13247 end;
13248
13249 else
13250 Insert_Before (Inst_Node, Act_Body);
13251 Mark_Rewrite_Insertion (Act_Body);
13252
13253 -- Insert the freeze node for the instance if need be
13254
13255 if Expander_Active then
13256 Freeze_Package_Instance
13257 (Inst_Node, Gen_Body, Gen_Decl, Act_Decl_Id);
13258 Set_Is_Frozen (Act_Decl_Id);
13259 end if;
13260 end if;
13261
13262 -- If the instantiation appears within a generic child package
13263 -- enable visibility of current instance of enclosing generic
13264 -- parents.
13265
13266 if Present (Ctx_Parents) then
13267 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
13268 Analyze (Act_Body);
13269 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
13270 else
13271 Analyze (Act_Body);
13272 end if;
13273 end if;
13274
13275 Inherit_Context (Gen_Body, Inst_Node);
13276
13277 if Par_Installed then
13278 Remove_Parent (In_Body => True);
13279
13280 -- Restore the previous visibility of the parent
13281
13282 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
13283 end if;
13284
13285 -- Remove the parent instances if they have been placed on the scope
13286 -- stack to compile the body.
13287
13288 if Present (Ctx_Parents) then
13289 pragma Assert (Scope_Stack.Last = Ctx_Top
13290 and then Current_Scope = Standard_Standard);
13291 Pop_Scope;
13292
13293 Remove_Parents_Of_Generic_Context (Ctx_Parents);
13294 end if;
13295
13296 pragma Assert (Current_Scope = Scope_Check_Id);
13297 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
13298
13299 Restore_Hidden_Primitives (Vis_Prims_List);
13300
13301 -- Restore the private views that were made visible when the body of
13302 -- the instantiation was created. Note that, in the case where one of
13303 -- these private views is declared in the parent, there is a nesting
13304 -- issue with the calls to Install_Parent and Remove_Parent made in
13305 -- between above with In_Body set to True, because these calls also
13306 -- want to swap and restore this private view respectively. In this
13307 -- case, the call to Install_Parent does nothing, but the call to
13308 -- Remove_Parent does restore the private view, thus undercutting the
13309 -- call to Restore_Private_Views. That's OK under the condition that
13310 -- the two mechanisms swap exactly the same entities, in particular
13311 -- the private entities dependent on the primary private entities.
13312
13313 Restore_Private_Views (Act_Decl_Id);
13314
13315 -- Remove the current unit from visibility if this is an instance
13316 -- that is not elaborated on the fly for inlining purposes.
13317
13318 if not Inlined_Body then
13319 Set_Is_Immediately_Visible (Act_Decl_Id, False);
13320 end if;
13321
13322 Restore_Env;
13323
13324 -- If we have no body, and the unit requires a body, then complain. This
13325 -- complaint is suppressed if we have detected other errors (since a
13326 -- common reason for missing the body is that it had errors).
13327 -- In CodePeer mode, a warning has been emitted already, no need for
13328 -- further messages.
13329
13330 elsif Unit_Requires_Body (Gen_Unit)
13331 and then not Body_Optional
13332 then
13333 if CodePeer_Mode then
13334 null;
13335
13336 elsif Serious_Errors_Detected = 0 then
13337 Error_Msg_NE
13338 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
13339
13340 -- Don't attempt to perform any cleanup actions if some other error
13341 -- was already detected, since this can cause blowups.
13342
13343 else
13344 goto Leave;
13345 end if;
13346
13347 -- Case of package that does not need a body
13348
13349 else
13350 -- If the instantiation of the declaration is a library unit, rewrite
13351 -- the original package instantiation as a package declaration in the
13352 -- compilation unit node.
13353
13354 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
13355 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
13356 Rewrite (Inst_Node, Act_Decl);
13357
13358 -- Generate elaboration entity, in case spec has elaboration code.
13359 -- This cannot be done when the instance is analyzed, because it
13360 -- is not known yet whether the body exists.
13361
13362 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
13363 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
13364
13365 -- If the instantiation is not a library unit, then append the
13366 -- declaration to the list of implicitly generated entities, unless
13367 -- it is already a list member which means that it was already
13368 -- processed
13369
13370 elsif not Is_List_Member (Act_Decl) then
13371 Mark_Rewrite_Insertion (Act_Decl);
13372 Insert_Before (Inst_Node, Act_Decl);
13373 end if;
13374 end if;
13375
13376 <<Leave>>
13377
13378 -- Restore the context that was in effect prior to instantiating the
13379 -- package body.
13380
13381 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
13382 Local_Suppress_Stack_Top := Saved_LSST;
13383 Scope_Suppress := Saved_SS;
13384 Style_Check := Saved_SC;
13385
13386 Expander_Mode_Restore;
13387 Restore_Config_Switches (Saved_CS);
13388 Restore_Ghost_Region (Saved_GM, Saved_IGR);
13389 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
13390 Restore_Warnings (Saved_Warn);
13391 end Instantiate_Package_Body;
13392
13393 ---------------------------------
13394 -- Instantiate_Subprogram_Body --
13395 ---------------------------------
13396
13397 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
13398 -- must be replaced by gotos which jump to the end of the routine in order
13399 -- to restore the Ghost and SPARK modes.
13400
13401 procedure Instantiate_Subprogram_Body
13402 (Body_Info : Pending_Body_Info;
13403 Body_Optional : Boolean := False)
13404 is
13405 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
13406 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
13407 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
13408 Gen_Id : constant Node_Id := Name (Inst_Node);
13409 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
13410 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
13411 Loc : constant Source_Ptr := Sloc (Inst_Node);
13412 Pack_Id : constant Entity_Id :=
13413 Defining_Unit_Name (Parent (Act_Decl));
13414
13415 -- The following constants capture the context prior to instantiating
13416 -- the subprogram body.
13417
13418 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
13419 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
13420 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
13421 Saved_ISMP : constant Boolean :=
13422 Ignore_SPARK_Mode_Pragmas_In_Instance;
13423 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
13424 Local_Suppress_Stack_Top;
13425 Saved_SC : constant Boolean := Style_Check;
13426 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
13427 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
13428 Saved_SS : constant Suppress_Record := Scope_Suppress;
13429 Saved_Warn : constant Warnings_State := Save_Warnings;
13430
13431 Act_Body : Node_Id;
13432 Act_Body_Id : Entity_Id;
13433 Gen_Body : Node_Id;
13434 Gen_Body_Id : Node_Id;
13435 Pack_Body : Node_Id;
13436 Par_Ent : Entity_Id := Empty;
13437 Par_Installed : Boolean := False;
13438 Par_Vis : Boolean := False;
13439 Ret_Expr : Node_Id;
13440
13441 begin
13442 Gen_Body_Id := Corresponding_Body (Gen_Decl);
13443
13444 -- Subprogram body may have been created already because of an inline
13445 -- pragma, or because of multiple elaborations of the enclosing package
13446 -- when several instances of the subprogram appear in the main unit.
13447
13448 if Present (Corresponding_Body (Act_Decl)) then
13449 return;
13450 end if;
13451
13452 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
13453
13454 -- Re-establish the state of information on which checks are suppressed.
13455 -- This information was set in Body_Info at the point of instantiation,
13456 -- and now we restore it so that the instance is compiled using the
13457 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
13458
13459 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
13460 Scope_Suppress := Body_Info.Scope_Suppress;
13461
13462 Restore_Config_Switches (Body_Info.Config_Switches);
13463 Restore_Warnings (Body_Info.Warnings);
13464
13465 if No (Gen_Body_Id) then
13466
13467 -- For imported generic subprogram, no body to compile, complete
13468 -- the spec entity appropriately.
13469
13470 if Is_Imported (Gen_Unit) then
13471 Set_Is_Imported (Act_Decl_Id);
13472 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
13473 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
13474 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
13475 Set_Has_Completion (Act_Decl_Id);
13476 goto Leave;
13477
13478 -- For other cases, compile the body
13479
13480 else
13481 Load_Parent_Of_Generic
13482 (Inst_Node, Specification (Gen_Decl), Body_Optional);
13483 Gen_Body_Id := Corresponding_Body (Gen_Decl);
13484 end if;
13485 end if;
13486
13487 Instantiation_Node := Inst_Node;
13488
13489 -- The subprogram being instantiated may be subject to pragma Ghost. Set
13490 -- the mode now to ensure that any nodes generated during instantiation
13491 -- are properly marked as Ghost.
13492
13493 Set_Ghost_Mode (Act_Decl_Id);
13494
13495 if Present (Gen_Body_Id) then
13496 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
13497
13498 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
13499
13500 -- Either body is not present, or context is non-expanding, as
13501 -- when compiling a subunit. Mark the instance as completed, and
13502 -- diagnose a missing body when needed.
13503
13504 if Expander_Active
13505 and then Operating_Mode = Generate_Code
13506 then
13507 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
13508 end if;
13509
13510 Set_Has_Completion (Act_Decl_Id);
13511 goto Leave;
13512 end if;
13513
13514 Save_Env (Gen_Unit, Act_Decl_Id);
13515 Style_Check := False;
13516
13517 -- If the context of the instance is subject to SPARK_Mode "off", the
13518 -- annotation is missing, or the body is instantiated at a later pass
13519 -- and its spec ignored SPARK_Mode pragma, set the global flag which
13520 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
13521 -- instance.
13522
13523 if SPARK_Mode /= On
13524 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
13525 then
13526 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
13527 end if;
13528
13529 -- If the context of an instance is not subject to SPARK_Mode "off",
13530 -- and the generic body is subject to an explicit SPARK_Mode pragma,
13531 -- the latter should be the one applicable to the instance.
13532
13533 if not Ignore_SPARK_Mode_Pragmas_In_Instance
13534 and then SPARK_Mode /= Off
13535 and then Present (SPARK_Pragma (Gen_Body_Id))
13536 then
13537 Set_SPARK_Mode (Gen_Body_Id);
13538 end if;
13539
13540 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
13541 Create_Instantiation_Source
13542 (Inst_Node,
13543 Gen_Body_Id,
13544 S_Adjustment);
13545
13546 Act_Body :=
13547 Copy_Generic_Node
13548 (Original_Node (Gen_Body), Empty, Instantiating => True);
13549
13550 -- Create proper defining name for the body, to correspond to the one
13551 -- in the spec.
13552
13553 Act_Body_Id :=
13554 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
13555
13556 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
13557 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
13558
13559 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
13560 Set_Has_Completion (Act_Decl_Id);
13561 Check_Generic_Actuals (Pack_Id, False);
13562
13563 -- Generate a reference to link the visible subprogram instance to
13564 -- the generic body, which for navigation purposes is the only
13565 -- available source for the instance.
13566
13567 Generate_Reference
13568 (Related_Instance (Pack_Id),
13569 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
13570
13571 -- If it is a child unit, make the parent instance (which is an
13572 -- instance of the parent of the generic) visible. The parent
13573 -- instance is the prefix of the name of the generic unit.
13574
13575 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
13576 and then Nkind (Gen_Id) = N_Expanded_Name
13577 then
13578 Par_Ent := Entity (Prefix (Gen_Id));
13579 elsif Is_Child_Unit (Gen_Unit) then
13580 Par_Ent := Scope (Gen_Unit);
13581 end if;
13582
13583 if Present (Par_Ent) then
13584 Par_Vis := Is_Immediately_Visible (Par_Ent);
13585 Install_Parent (Par_Ent, In_Body => True);
13586 Par_Installed := True;
13587 end if;
13588
13589 -- Subprogram body is placed in the body of wrapper package,
13590 -- whose spec contains the subprogram declaration as well as
13591 -- the renaming declarations for the generic parameters.
13592
13593 Pack_Body :=
13594 Make_Package_Body (Loc,
13595 Defining_Unit_Name => New_Copy (Pack_Id),
13596 Declarations => New_List (Act_Body));
13597
13598 Set_Corresponding_Spec (Pack_Body, Pack_Id);
13599
13600 -- If the instantiation is a library unit, then build resulting
13601 -- compilation unit nodes for the instance. The declaration of
13602 -- the enclosing package is the grandparent of the subprogram
13603 -- declaration. First replace the instantiation node as the unit
13604 -- of the corresponding compilation.
13605
13606 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
13607 if Parent (Inst_Node) = Cunit (Main_Unit) then
13608 Set_Unit (Parent (Inst_Node), Inst_Node);
13609 Build_Instance_Compilation_Unit_Nodes
13610 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
13611 Analyze (Inst_Node);
13612 else
13613 Set_Parent (Pack_Body, Parent (Inst_Node));
13614 Analyze (Pack_Body);
13615 end if;
13616
13617 else
13618 Insert_Before (Inst_Node, Pack_Body);
13619 Mark_Rewrite_Insertion (Pack_Body);
13620
13621 -- Insert the freeze node for the instance if need be
13622
13623 if Expander_Active then
13624 Freeze_Subprogram_Instance (Inst_Node, Gen_Body, Pack_Id);
13625 end if;
13626
13627 Analyze (Pack_Body);
13628 end if;
13629
13630 Inherit_Context (Gen_Body, Inst_Node);
13631
13632 Restore_Private_Views (Pack_Id, False);
13633
13634 if Par_Installed then
13635 Remove_Parent (In_Body => True);
13636
13637 -- Restore the previous visibility of the parent
13638
13639 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
13640 end if;
13641
13642 Restore_Env;
13643
13644 -- Body not found. Error was emitted already. If there were no previous
13645 -- errors, this may be an instance whose scope is a premature instance.
13646 -- In that case we must insure that the (legal) program does raise
13647 -- program error if executed. We generate a subprogram body for this
13648 -- purpose.
13649
13650 elsif Serious_Errors_Detected = 0
13651 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
13652 then
13653 if Body_Optional then
13654 goto Leave;
13655
13656 elsif Ekind (Act_Decl_Id) = E_Procedure then
13657 Act_Body :=
13658 Make_Subprogram_Body (Loc,
13659 Specification =>
13660 Make_Procedure_Specification (Loc,
13661 Defining_Unit_Name =>
13662 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
13663 Parameter_Specifications =>
13664 New_Copy_List
13665 (Parameter_Specifications (Parent (Act_Decl_Id)))),
13666
13667 Declarations => Empty_List,
13668 Handled_Statement_Sequence =>
13669 Make_Handled_Sequence_Of_Statements (Loc,
13670 Statements => New_List (
13671 Make_Raise_Program_Error (Loc,
13672 Reason => PE_Access_Before_Elaboration))));
13673
13674 else
13675 Ret_Expr :=
13676 Make_Raise_Program_Error (Loc,
13677 Reason => PE_Access_Before_Elaboration);
13678
13679 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
13680 Set_Analyzed (Ret_Expr);
13681
13682 Act_Body :=
13683 Make_Subprogram_Body (Loc,
13684 Specification =>
13685 Make_Function_Specification (Loc,
13686 Defining_Unit_Name =>
13687 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
13688 Parameter_Specifications =>
13689 New_Copy_List
13690 (Parameter_Specifications (Parent (Act_Decl_Id))),
13691 Result_Definition =>
13692 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
13693
13694 Declarations => Empty_List,
13695 Handled_Statement_Sequence =>
13696 Make_Handled_Sequence_Of_Statements (Loc,
13697 Statements => New_List (
13698 Make_Simple_Return_Statement (Loc, Ret_Expr))));
13699 end if;
13700
13701 Pack_Body :=
13702 Make_Package_Body (Loc,
13703 Defining_Unit_Name => New_Copy (Pack_Id),
13704 Declarations => New_List (Act_Body));
13705
13706 Insert_After (Inst_Node, Pack_Body);
13707 Set_Corresponding_Spec (Pack_Body, Pack_Id);
13708 Analyze (Pack_Body);
13709 end if;
13710
13711 <<Leave>>
13712
13713 -- Restore the context that was in effect prior to instantiating the
13714 -- subprogram body.
13715
13716 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
13717 Local_Suppress_Stack_Top := Saved_LSST;
13718 Scope_Suppress := Saved_SS;
13719 Style_Check := Saved_SC;
13720
13721 Expander_Mode_Restore;
13722 Restore_Config_Switches (Saved_CS);
13723 Restore_Ghost_Region (Saved_GM, Saved_IGR);
13724 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
13725 Restore_Warnings (Saved_Warn);
13726 end Instantiate_Subprogram_Body;
13727
13728 ----------------------
13729 -- Instantiate_Type --
13730 ----------------------
13731
13732 function Instantiate_Type
13733 (Formal : Node_Id;
13734 Actual : Node_Id;
13735 Analyzed_Formal : Node_Id;
13736 Actual_Decls : List_Id) return List_Id
13737 is
13738 A_Gen_T : constant Entity_Id :=
13739 Defining_Identifier (Analyzed_Formal);
13740 Def : constant Node_Id := Formal_Type_Definition (Formal);
13741 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
13742 Act_T : Entity_Id;
13743 Ancestor : Entity_Id := Empty;
13744 Decl_Node : Node_Id;
13745 Decl_Nodes : List_Id; -- result
13746 Loc : Source_Ptr;
13747 Subt : Entity_Id;
13748
13749 procedure Check_Shared_Variable_Control_Aspects;
13750 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
13751 -- that may be specified for a formal type are obeyed by the actual.
13752
13753 procedure Diagnose_Predicated_Actual;
13754 -- There are a number of constructs in which a discrete type with
13755 -- predicates is illegal, e.g. as an index in an array type declaration.
13756 -- If a generic type is used is such a construct in a generic package
13757 -- declaration, it carries the flag No_Predicate_On_Actual. It is part
13758 -- of the generic contract that the actual cannot have predicates.
13759
13760 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
13761 -- Check that base types are the same and that the subtypes match
13762 -- statically. Used in several of the validation subprograms for
13763 -- actuals in instantiations.
13764
13765 procedure Validate_Array_Type_Instance;
13766 procedure Validate_Access_Subprogram_Instance;
13767 procedure Validate_Access_Type_Instance;
13768 procedure Validate_Derived_Type_Instance;
13769 procedure Validate_Derived_Interface_Type_Instance;
13770 procedure Validate_Discriminated_Formal_Type;
13771 procedure Validate_Interface_Type_Instance;
13772 procedure Validate_Private_Type_Instance;
13773 procedure Validate_Incomplete_Type_Instance;
13774 -- These procedures perform validation tests for the named case.
13775 -- Validate_Discriminated_Formal_Type is shared by formal private
13776 -- types and Ada 2012 formal incomplete types.
13777
13778 --------------------------------------------
13779 -- Check_Shared_Variable_Control_Aspects --
13780 --------------------------------------------
13781
13782 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
13783 -- that may be specified for the formal are obeyed by the actual.
13784 -- If the formal is a derived type the aspect specifications must match.
13785 -- NOTE: AI12-0282 implies that matching of aspects is required between
13786 -- formal and actual in all cases, but this is too restrictive.
13787 -- In particular it violates a language design rule: a limited private
13788 -- indefinite formal can be matched by any actual. The current code
13789 -- reflects an older and more permissive version of RM C.6 (12/5).
13790
13791 procedure Check_Shared_Variable_Control_Aspects is
13792 begin
13793 if Ada_Version >= Ada_2022 then
13794 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
13795 Error_Msg_NE
13796 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
13797
13798 elsif Is_Derived_Type (A_Gen_T)
13799 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
13800 then
13801 Error_Msg_NE
13802 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
13803 end if;
13804
13805 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
13806 Error_Msg_NE
13807 ("actual for& must have Volatile aspect",
13808 Actual, A_Gen_T);
13809
13810 elsif Is_Derived_Type (A_Gen_T)
13811 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
13812 then
13813 Error_Msg_NE
13814 ("actual for& has different Volatile aspect",
13815 Actual, A_Gen_T);
13816 end if;
13817
13818 -- We assume that an array type whose atomic component type
13819 -- is Atomic is equivalent to an array type with the explicit
13820 -- aspect Has_Atomic_Components. This is a reasonable inference
13821 -- from the intent of AI12-0282, and makes it legal to use an
13822 -- actual that does not have the identical aspect as the formal.
13823 -- Ditto for volatile components.
13824
13825 declare
13826 Actual_Atomic_Comp : constant Boolean :=
13827 Has_Atomic_Components (Act_T)
13828 or else (Is_Array_Type (Act_T)
13829 and then Is_Atomic (Component_Type (Act_T)));
13830 begin
13831 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
13832 Error_Msg_NE
13833 ("formal and actual for& must agree on atomic components",
13834 Actual, A_Gen_T);
13835 end if;
13836 end;
13837
13838 declare
13839 Actual_Volatile_Comp : constant Boolean :=
13840 Has_Volatile_Components (Act_T)
13841 or else (Is_Array_Type (Act_T)
13842 and then Is_Volatile (Component_Type (Act_T)));
13843 begin
13844 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
13845 then
13846 Error_Msg_NE
13847 ("actual for& must have volatile components",
13848 Actual, A_Gen_T);
13849 end if;
13850 end;
13851
13852 -- The following two aspects do not require exact matching,
13853 -- but only one-way agreement. See RM C.6.
13854
13855 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
13856 then
13857 Error_Msg_NE
13858 ("actual for& must have Independent aspect specified",
13859 Actual, A_Gen_T);
13860 end if;
13861
13862 if Has_Independent_Components (A_Gen_T)
13863 and then not Has_Independent_Components (Act_T)
13864 then
13865 Error_Msg_NE
13866 ("actual for& must have Independent_Components specified",
13867 Actual, A_Gen_T);
13868 end if;
13869 end if;
13870 end Check_Shared_Variable_Control_Aspects;
13871
13872 ---------------------------------
13873 -- Diagnose_Predicated_Actual --
13874 ---------------------------------
13875
13876 procedure Diagnose_Predicated_Actual is
13877 begin
13878 if No_Predicate_On_Actual (A_Gen_T)
13879 and then Has_Predicates (Act_T)
13880 then
13881 Error_Msg_NE
13882 ("actual for& cannot be a type with predicate",
13883 Instantiation_Node, A_Gen_T);
13884
13885 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
13886 and then Has_Predicates (Act_T)
13887 and then not Has_Static_Predicate_Aspect (Act_T)
13888 then
13889 Error_Msg_NE
13890 ("actual for& cannot be a type with a dynamic predicate",
13891 Instantiation_Node, A_Gen_T);
13892 end if;
13893 end Diagnose_Predicated_Actual;
13894
13895 --------------------
13896 -- Subtypes_Match --
13897 --------------------
13898
13899 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
13900 T : constant Entity_Id := Get_Instance_Of (Gen_T);
13901
13902 begin
13903 -- Check that the base types, root types (when dealing with class
13904 -- wide types), or designated types (when dealing with anonymous
13905 -- access types) of Gen_T and Act_T are statically matching subtypes.
13906
13907 return (Base_Type (Base_Type (T)) = Base_Type (Act_T)
13908 and then Subtypes_Statically_Match (T, Act_T))
13909
13910 or else (Is_Class_Wide_Type (Gen_T)
13911 and then Is_Class_Wide_Type (Act_T)
13912 and then Subtypes_Match
13913 (Get_Instance_Of (Root_Type (Gen_T)),
13914 Root_Type (Act_T)))
13915
13916 or else (Is_Anonymous_Access_Type (Gen_T)
13917 and then Ekind (Act_T) = Ekind (Gen_T)
13918 and then Subtypes_Statically_Match
13919 (Designated_Type (Gen_T), Designated_Type (Act_T)));
13920 end Subtypes_Match;
13921
13922 -----------------------------------------
13923 -- Validate_Access_Subprogram_Instance --
13924 -----------------------------------------
13925
13926 procedure Validate_Access_Subprogram_Instance is
13927 begin
13928 if not Is_Access_Type (Act_T)
13929 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
13930 then
13931 Error_Msg_NE
13932 ("expect access type in instantiation of &", Actual, Gen_T);
13933 Abandon_Instantiation (Actual);
13934 end if;
13935
13936 -- According to AI05-288, actuals for access_to_subprograms must be
13937 -- subtype conformant with the generic formal. Previous to AI05-288
13938 -- only mode conformance was required.
13939
13940 -- This is a binding interpretation that applies to previous versions
13941 -- of the language, no need to maintain previous weaker checks.
13942
13943 Check_Subtype_Conformant
13944 (Designated_Type (Act_T),
13945 Designated_Type (A_Gen_T),
13946 Actual,
13947 Get_Inst => True);
13948
13949 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
13950 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
13951 Error_Msg_NE
13952 ("protected access type not allowed for formal &",
13953 Actual, Gen_T);
13954 end if;
13955
13956 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
13957 Error_Msg_NE
13958 ("expect protected access type for formal &",
13959 Actual, Gen_T);
13960 end if;
13961
13962 -- If the formal has a specified convention (which in most cases
13963 -- will be StdCall) verify that the actual has the same convention.
13964
13965 if Has_Convention_Pragma (A_Gen_T)
13966 and then Convention (A_Gen_T) /= Convention (Act_T)
13967 then
13968 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
13969 Error_Msg_NE
13970 ("actual for formal & must have convention %", Actual, Gen_T);
13971 end if;
13972
13973 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13974 Error_Msg_NE
13975 ("non null exclusion of actual and formal & do not match",
13976 Actual, Gen_T);
13977 end if;
13978 end Validate_Access_Subprogram_Instance;
13979
13980 -----------------------------------
13981 -- Validate_Access_Type_Instance --
13982 -----------------------------------
13983
13984 procedure Validate_Access_Type_Instance is
13985 Desig_Type : constant Entity_Id :=
13986 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
13987 Desig_Act : Entity_Id;
13988
13989 begin
13990 if not Is_Access_Type (Act_T) then
13991 Error_Msg_NE
13992 ("expect access type in instantiation of &", Actual, Gen_T);
13993 Abandon_Instantiation (Actual);
13994 end if;
13995
13996 if Is_Access_Constant (A_Gen_T) then
13997 if not Is_Access_Constant (Act_T) then
13998 Error_Msg_N
13999 ("actual type must be access-to-constant type", Actual);
14000 Abandon_Instantiation (Actual);
14001 end if;
14002 else
14003 if Is_Access_Constant (Act_T) then
14004 Error_Msg_N
14005 ("actual type must be access-to-variable type", Actual);
14006 Abandon_Instantiation (Actual);
14007
14008 elsif Ekind (A_Gen_T) = E_General_Access_Type
14009 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
14010 then
14011 Error_Msg_N
14012 ("actual must be general access type!", Actual);
14013 Error_Msg_NE -- CODEFIX
14014 ("\add ALL to }!", Actual, Act_T);
14015 Abandon_Instantiation (Actual);
14016 end if;
14017 end if;
14018
14019 -- The designated subtypes, that is to say the subtypes introduced
14020 -- by an access type declaration (and not by a subtype declaration)
14021 -- must match.
14022
14023 Desig_Act := Designated_Type (Base_Type (Act_T));
14024
14025 -- The designated type may have been introduced through a limited_
14026 -- with clause, in which case retrieve the non-limited view. This
14027 -- applies to incomplete types as well as to class-wide types.
14028
14029 if From_Limited_With (Desig_Act) then
14030 Desig_Act := Available_View (Desig_Act);
14031 end if;
14032
14033 if not Subtypes_Match (Desig_Type, Desig_Act) then
14034 Error_Msg_NE
14035 ("designated type of actual does not match that of formal &",
14036 Actual, Gen_T);
14037
14038 if not Predicates_Match (Desig_Type, Desig_Act) then
14039 Error_Msg_N ("\predicates do not match", Actual);
14040 end if;
14041
14042 Abandon_Instantiation (Actual);
14043 end if;
14044
14045 -- Ada 2005: null-exclusion indicators of the two types must agree
14046
14047 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
14048 Error_Msg_NE
14049 ("non null exclusion of actual and formal & do not match",
14050 Actual, Gen_T);
14051 end if;
14052
14053 -- formal/actual extended access match required (regardless of
14054 -- whether a formal extended access type is currently possible)
14055
14056 if Is_Extended_Access_Type (Act_T)
14057 /= Is_Extended_Access_Type (A_Gen_T)
14058 then
14059 Error_Msg_N
14060 ("actual type must" &
14061 String'(if Is_Extended_Access_Type (A_Gen_T)
14062 then ""
14063 else " not") &
14064 " be extended access type", Actual);
14065
14066 Abandon_Instantiation (Actual);
14067 end if;
14068 end Validate_Access_Type_Instance;
14069
14070 ----------------------------------
14071 -- Validate_Array_Type_Instance --
14072 ----------------------------------
14073
14074 procedure Validate_Array_Type_Instance is
14075 I1 : Node_Id;
14076 I2 : Node_Id;
14077 T2 : Entity_Id;
14078
14079 function Formal_Dimensions return Nat;
14080 -- Count number of dimensions in array type formal
14081
14082 -----------------------
14083 -- Formal_Dimensions --
14084 -----------------------
14085
14086 function Formal_Dimensions return Nat is
14087 Dims : List_Id;
14088
14089 begin
14090 if Nkind (Def) = N_Constrained_Array_Definition then
14091 Dims := Discrete_Subtype_Definitions (Def);
14092 else
14093 Dims := Subtype_Marks (Def);
14094 end if;
14095
14096 return List_Length (Dims);
14097 end Formal_Dimensions;
14098
14099 -- Start of processing for Validate_Array_Type_Instance
14100
14101 begin
14102 if not Is_Array_Type (Act_T) then
14103 Error_Msg_NE
14104 ("expect array type in instantiation of &", Actual, Gen_T);
14105 Abandon_Instantiation (Actual);
14106
14107 elsif Nkind (Def) = N_Constrained_Array_Definition then
14108 if not Is_Constrained (Act_T) then
14109 Error_Msg_NE
14110 ("expect constrained array in instantiation of &",
14111 Actual, Gen_T);
14112 Abandon_Instantiation (Actual);
14113 end if;
14114
14115 else
14116 if Is_Constrained (Act_T) then
14117 Error_Msg_NE
14118 ("expect unconstrained array in instantiation of &",
14119 Actual, Gen_T);
14120 Abandon_Instantiation (Actual);
14121 end if;
14122 end if;
14123
14124 if Formal_Dimensions /= Number_Dimensions (Act_T) then
14125 Error_Msg_NE
14126 ("dimensions of actual do not match formal &", Actual, Gen_T);
14127 Abandon_Instantiation (Actual);
14128 end if;
14129
14130 I1 := First_Index (A_Gen_T);
14131 I2 := First_Index (Act_T);
14132 for J in 1 .. Formal_Dimensions loop
14133
14134 -- If the indexes of the actual were given by a subtype_mark,
14135 -- the index was transformed into a range attribute. Retrieve
14136 -- the original type mark for checking.
14137
14138 if Is_Entity_Name (Original_Node (I2)) then
14139 T2 := Entity (Original_Node (I2));
14140 else
14141 T2 := Etype (I2);
14142 end if;
14143
14144 -- In the case of a fixed-lower-bound subtype, we want to check
14145 -- against the index type's range rather than the range of the
14146 -- subtype (which will be seen as unconstrained, and whose bounds
14147 -- won't generally match those of the formal unconstrained array
14148 -- type's corresponding index type).
14149
14150 if Is_Fixed_Lower_Bound_Index_Subtype (T2) then
14151 T2 := Etype (Scalar_Range (T2));
14152 end if;
14153
14154 if not Subtypes_Match
14155 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
14156 then
14157 Error_Msg_NE
14158 ("index types of actual do not match those of formal &",
14159 Actual, Gen_T);
14160 Abandon_Instantiation (Actual);
14161 end if;
14162
14163 Next_Index (I1);
14164 Next_Index (I2);
14165 end loop;
14166
14167 -- Check matching subtypes. Note that there are complex visibility
14168 -- issues when the generic is a child unit and some aspect of the
14169 -- generic type is declared in a parent unit of the generic. We do
14170 -- the test to handle this special case only after a direct check
14171 -- for static matching has failed. The case where both the component
14172 -- type and the array type are separate formals, and the component
14173 -- type is a private view may also require special checking in
14174 -- Subtypes_Match. Finally, we assume that a child instance where
14175 -- the component type comes from a formal of a parent instance is
14176 -- correct because the generic was correct. A more precise check
14177 -- seems too complex to install???
14178
14179 if Subtypes_Match
14180 (Component_Type (A_Gen_T), Component_Type (Act_T))
14181 or else
14182 Subtypes_Match
14183 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
14184 Component_Type (Act_T))
14185 or else
14186 (not Inside_A_Generic
14187 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
14188 then
14189 null;
14190 else
14191 Error_Msg_NE
14192 ("component subtype of actual does not match that of formal &",
14193 Actual, Gen_T);
14194 Abandon_Instantiation (Actual);
14195 end if;
14196
14197 if Has_Aliased_Components (A_Gen_T)
14198 and then not Has_Aliased_Components (Act_T)
14199 then
14200 Error_Msg_NE
14201 ("actual must have aliased components to match formal type &",
14202 Actual, Gen_T);
14203 end if;
14204 end Validate_Array_Type_Instance;
14205
14206 -----------------------------------------------
14207 -- Validate_Derived_Interface_Type_Instance --
14208 -----------------------------------------------
14209
14210 procedure Validate_Derived_Interface_Type_Instance is
14211 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
14212 Elmt : Elmt_Id;
14213
14214 begin
14215 -- First apply interface instance checks
14216
14217 Validate_Interface_Type_Instance;
14218
14219 -- Verify that immediate parent interface is an ancestor of
14220 -- the actual.
14221
14222 if Present (Par)
14223 and then not Interface_Present_In_Ancestor (Act_T, Par)
14224 then
14225 Error_Msg_NE
14226 ("interface actual must include progenitor&", Actual, Par);
14227 end if;
14228
14229 -- Now verify that the actual includes all other ancestors of
14230 -- the formal.
14231
14232 Elmt := First_Elmt (Interfaces (A_Gen_T));
14233 while Present (Elmt) loop
14234 if not Interface_Present_In_Ancestor
14235 (Act_T, Get_Instance_Of (Node (Elmt)))
14236 then
14237 Error_Msg_NE
14238 ("interface actual must include progenitor&",
14239 Actual, Node (Elmt));
14240 end if;
14241
14242 Next_Elmt (Elmt);
14243 end loop;
14244 end Validate_Derived_Interface_Type_Instance;
14245
14246 ------------------------------------
14247 -- Validate_Derived_Type_Instance --
14248 ------------------------------------
14249
14250 procedure Validate_Derived_Type_Instance is
14251 Actual_Discr : Entity_Id;
14252 Ancestor_Discr : Entity_Id;
14253
14254 begin
14255 -- Verify that the actual includes the progenitors of the formal,
14256 -- if any. The formal may depend on previous formals and their
14257 -- instance, so we must examine instance of interfaces if present.
14258 -- The actual may be an extension of an interface, in which case
14259 -- it does not appear in the interface list, so this must be
14260 -- checked separately.
14261
14262 if Present (Interface_List (Def)) then
14263 if not Has_Interfaces (Act_T) then
14264 Error_Msg_NE
14265 ("actual must implement all interfaces of formal&",
14266 Actual, A_Gen_T);
14267
14268 else
14269 declare
14270 Act_Iface_List : Elist_Id;
14271 Iface : Node_Id;
14272 Iface_Ent : Entity_Id;
14273
14274 function Instance_Exists (I : Entity_Id) return Boolean;
14275 -- If the interface entity is declared in a generic unit,
14276 -- this can only be legal if we are within an instantiation
14277 -- of a child of that generic. There is currently no
14278 -- mechanism to relate an interface declared within a
14279 -- generic to the corresponding interface in an instance,
14280 -- so we traverse the list of interfaces of the actual,
14281 -- looking for a name match.
14282
14283 ---------------------
14284 -- Instance_Exists --
14285 ---------------------
14286
14287 function Instance_Exists (I : Entity_Id) return Boolean is
14288 Iface_Elmt : Elmt_Id;
14289
14290 begin
14291 Iface_Elmt := First_Elmt (Act_Iface_List);
14292 while Present (Iface_Elmt) loop
14293 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
14294 and then Chars (Node (Iface_Elmt)) = Chars (I)
14295 then
14296 return True;
14297 end if;
14298
14299 Next_Elmt (Iface_Elmt);
14300 end loop;
14301
14302 return False;
14303 end Instance_Exists;
14304
14305 begin
14306 Iface := First (Abstract_Interface_List (A_Gen_T));
14307 Collect_Interfaces (Act_T, Act_Iface_List);
14308
14309 while Present (Iface) loop
14310 Iface_Ent := Get_Instance_Of (Entity (Iface));
14311
14312 if Is_Ancestor (Iface_Ent, Act_T)
14313 or else Is_Progenitor (Iface_Ent, Act_T)
14314 then
14315 null;
14316
14317 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
14318 and then Instance_Exists (Iface_Ent)
14319 then
14320 null;
14321
14322 else
14323 Error_Msg_Name_1 := Chars (Act_T);
14324 Error_Msg_NE
14325 ("actual% must implement interface&",
14326 Actual, Etype (Iface));
14327 end if;
14328
14329 Next (Iface);
14330 end loop;
14331 end;
14332 end if;
14333 end if;
14334
14335 -- If the parent type in the generic declaration is itself a previous
14336 -- formal type, then it is local to the generic and absent from the
14337 -- analyzed generic definition. In that case the ancestor is the
14338 -- instance of the formal (which must have been instantiated
14339 -- previously), unless the ancestor is itself a formal derived type.
14340 -- In this latter case (which is the subject of Corrigendum 8652/0038
14341 -- (AI-202) the ancestor of the formals is the ancestor of its
14342 -- parent. Otherwise, the analyzed generic carries the parent type.
14343 -- If the parent type is defined in a previous formal package, then
14344 -- the scope of that formal package is that of the generic type
14345 -- itself, and it has already been mapped into the corresponding type
14346 -- in the actual package.
14347
14348 -- Common case: parent type defined outside of the generic
14349
14350 if Is_Entity_Name (Subtype_Mark (Def))
14351 and then Present (Entity (Subtype_Mark (Def)))
14352 then
14353 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
14354
14355 -- Check whether parent is defined in a previous formal package
14356
14357 elsif
14358 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
14359 then
14360 Ancestor :=
14361 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
14362
14363 -- The type may be a local derivation, or a type extension of a
14364 -- previous formal, or of a formal of a parent package.
14365
14366 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
14367 or else
14368 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
14369 then
14370 -- Check whether the parent is another formal derived type in the
14371 -- same generic unit.
14372
14373 if Etype (A_Gen_T) /= A_Gen_T
14374 and then Is_Generic_Type (Etype (A_Gen_T))
14375 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
14376 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
14377 then
14378 -- Locate ancestor of parent from the subtype declaration
14379 -- created for the actual.
14380
14381 declare
14382 Decl : Node_Id;
14383
14384 begin
14385 Decl := First (Actual_Decls);
14386 while Present (Decl) loop
14387 if Nkind (Decl) = N_Subtype_Declaration
14388 and then Chars (Defining_Identifier (Decl)) =
14389 Chars (Etype (A_Gen_T))
14390 then
14391 Ancestor := Generic_Parent_Type (Decl);
14392 exit;
14393 else
14394 Next (Decl);
14395 end if;
14396 end loop;
14397 end;
14398
14399 pragma Assert (Present (Ancestor));
14400
14401 -- The ancestor itself may be a previous formal that has been
14402 -- instantiated.
14403
14404 Ancestor := Get_Instance_Of (Ancestor);
14405
14406 else
14407 Ancestor := Get_Instance_Of (Etype (Get_Instance_Of (A_Gen_T)));
14408 end if;
14409
14410 -- Check whether parent is a previous formal of the current generic
14411
14412 elsif Is_Derived_Type (A_Gen_T)
14413 and then Is_Generic_Type (Etype (A_Gen_T))
14414 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
14415 then
14416 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
14417
14418 -- An unusual case: the actual is a type declared in a parent unit,
14419 -- but is not a formal type so there is no instance_of for it.
14420 -- Retrieve it by analyzing the record extension.
14421
14422 elsif Is_Child_Unit (Scope (A_Gen_T))
14423 and then In_Open_Scopes (Scope (Act_T))
14424 and then Is_Generic_Instance (Scope (Act_T))
14425 then
14426 Analyze (Subtype_Mark (Def));
14427 Ancestor := Entity (Subtype_Mark (Def));
14428
14429 else
14430 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
14431 end if;
14432
14433 -- If the formal derived type has pragma Preelaborable_Initialization
14434 -- then the actual type must have preelaborable initialization.
14435
14436 if Known_To_Have_Preelab_Init (A_Gen_T)
14437 and then not Has_Preelaborable_Initialization (Act_T)
14438 then
14439 Error_Msg_NE
14440 ("actual for & must have preelaborable initialization",
14441 Actual, Gen_T);
14442 end if;
14443
14444 -- Ada 2005 (AI-251)
14445
14446 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
14447 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
14448 Error_Msg_NE
14449 ("(Ada 2005) expected type implementing & in instantiation",
14450 Actual, Ancestor);
14451 end if;
14452
14453 -- Finally verify that the (instance of) the ancestor is an ancestor
14454 -- of the actual.
14455
14456 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
14457 Error_Msg_NE
14458 ("expect type derived from & in instantiation",
14459 Actual, First_Subtype (Ancestor));
14460 Abandon_Instantiation (Actual);
14461 end if;
14462
14463 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
14464 -- that the formal type declaration has been rewritten as a private
14465 -- extension.
14466
14467 if Ada_Version >= Ada_2005
14468 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
14469 and then Synchronized_Present (Parent (A_Gen_T))
14470 then
14471 -- The actual must be a synchronized tagged type
14472
14473 if not Is_Tagged_Type (Act_T) then
14474 Error_Msg_N
14475 ("actual of synchronized type must be tagged", Actual);
14476 Abandon_Instantiation (Actual);
14477
14478 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
14479 and then Nkind (Type_Definition (Parent (Act_T))) =
14480 N_Derived_Type_Definition
14481 and then not Synchronized_Present
14482 (Type_Definition (Parent (Act_T)))
14483 then
14484 Error_Msg_N
14485 ("actual of synchronized type must be synchronized", Actual);
14486 Abandon_Instantiation (Actual);
14487 end if;
14488 end if;
14489
14490 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
14491 -- removes the second instance of the phrase "or allow pass by copy".
14492
14493 -- For Ada 2022, the aspect may be specified explicitly for the
14494 -- formal regardless of whether an ancestor obeys it.
14495
14496 if Is_Atomic (Act_T)
14497 and then not Is_Atomic (Ancestor)
14498 and then not Is_Atomic (A_Gen_T)
14499 then
14500 Error_Msg_N
14501 ("cannot have atomic actual type for non-atomic formal type",
14502 Actual);
14503
14504 elsif Is_Volatile (Act_T)
14505 and then not Is_Volatile (Ancestor)
14506 and then not Is_Volatile (A_Gen_T)
14507 then
14508 Error_Msg_N
14509 ("cannot have volatile actual type for non-volatile formal type",
14510 Actual);
14511 end if;
14512
14513 -- It should not be necessary to check for unknown discriminants on
14514 -- Formal, but for some reason Has_Unknown_Discriminants is false for
14515 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
14516 -- needs fixing. ???
14517
14518 if Is_Definite_Subtype (A_Gen_T)
14519 and then not Unknown_Discriminants_Present (Formal)
14520 and then not Is_Definite_Subtype (Act_T)
14521 then
14522 Error_Msg_N ("actual subtype must be constrained", Actual);
14523 Abandon_Instantiation (Actual);
14524 end if;
14525
14526 if not Unknown_Discriminants_Present (Formal) then
14527 if Is_Constrained (Ancestor) then
14528 if not Is_Constrained (Act_T) then
14529 Error_Msg_N ("actual subtype must be constrained", Actual);
14530 Abandon_Instantiation (Actual);
14531 end if;
14532
14533 -- Ancestor is unconstrained, Check if generic formal and actual
14534 -- agree on constrainedness. The check only applies to array types
14535 -- and discriminated types.
14536
14537 elsif Is_Constrained (Act_T) then
14538 if Ekind (Ancestor) = E_Access_Type
14539 or else (not Is_Constrained (A_Gen_T)
14540 and then Is_Composite_Type (A_Gen_T))
14541 then
14542 Error_Msg_N ("actual subtype must be unconstrained", Actual);
14543 Abandon_Instantiation (Actual);
14544 end if;
14545
14546 -- A class-wide type is only allowed if the formal has unknown
14547 -- discriminants.
14548
14549 elsif Is_Class_Wide_Type (Act_T)
14550 and then not Has_Unknown_Discriminants (Ancestor)
14551 then
14552 Error_Msg_NE
14553 ("actual for & cannot be a class-wide type", Actual, Gen_T);
14554 Abandon_Instantiation (Actual);
14555
14556 -- Otherwise, the formal and actual must have the same number
14557 -- of discriminants and each discriminant of the actual must
14558 -- correspond to a discriminant of the formal.
14559
14560 elsif Has_Discriminants (Act_T)
14561 and then not Has_Unknown_Discriminants (Act_T)
14562 and then Has_Discriminants (Ancestor)
14563 then
14564 Actual_Discr := First_Discriminant (Act_T);
14565 Ancestor_Discr := First_Discriminant (Ancestor);
14566 while Present (Actual_Discr)
14567 and then Present (Ancestor_Discr)
14568 loop
14569 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
14570 No (Corresponding_Discriminant (Actual_Discr))
14571 then
14572 Error_Msg_NE
14573 ("discriminant & does not correspond "
14574 & "to ancestor discriminant", Actual, Actual_Discr);
14575 Abandon_Instantiation (Actual);
14576 end if;
14577
14578 Next_Discriminant (Actual_Discr);
14579 Next_Discriminant (Ancestor_Discr);
14580 end loop;
14581
14582 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
14583 Error_Msg_NE
14584 ("actual for & must have same number of discriminants",
14585 Actual, Gen_T);
14586 Abandon_Instantiation (Actual);
14587 end if;
14588
14589 -- This case should be caught by the earlier check for
14590 -- constrainedness, but the check here is added for completeness.
14591
14592 elsif Has_Discriminants (Act_T)
14593 and then not Has_Unknown_Discriminants (Act_T)
14594 then
14595 Error_Msg_NE
14596 ("actual for & must not have discriminants", Actual, Gen_T);
14597 Abandon_Instantiation (Actual);
14598
14599 elsif Has_Discriminants (Ancestor) then
14600 Error_Msg_NE
14601 ("actual for & must have known discriminants", Actual, Gen_T);
14602 Abandon_Instantiation (Actual);
14603 end if;
14604
14605 if not Subtypes_Statically_Compatible
14606 (Act_T, Ancestor, Formal_Derived_Matching => True)
14607 then
14608 Error_Msg_NE
14609 ("actual for & must be statically compatible with ancestor",
14610 Actual, Gen_T);
14611
14612 if not Predicates_Compatible (Act_T, Ancestor) then
14613 Error_Msg_N
14614 ("\predicate on actual is not compatible with ancestor",
14615 Actual);
14616 end if;
14617
14618 Abandon_Instantiation (Actual);
14619 end if;
14620 end if;
14621
14622 -- If the formal and actual types are abstract, check that there
14623 -- are no abstract primitives of the actual type that correspond to
14624 -- nonabstract primitives of the formal type (second sentence of
14625 -- RM95 3.9.3(9)).
14626
14627 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
14628 Check_Abstract_Primitives : declare
14629 Gen_Prims : constant Elist_Id :=
14630 Primitive_Operations (A_Gen_T);
14631 Gen_Elmt : Elmt_Id;
14632 Gen_Subp : Entity_Id;
14633 Anc_Subp : Entity_Id;
14634 Anc_Formal : Entity_Id;
14635 Anc_F_Type : Entity_Id;
14636
14637 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
14638 Act_Elmt : Elmt_Id;
14639 Act_Subp : Entity_Id;
14640 Act_Formal : Entity_Id;
14641 Act_F_Type : Entity_Id;
14642
14643 Subprograms_Correspond : Boolean;
14644
14645 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
14646 -- Returns true if T2 is derived directly or indirectly from
14647 -- T1, including derivations from interfaces. T1 and T2 are
14648 -- required to be specific tagged base types.
14649
14650 ------------------------
14651 -- Is_Tagged_Ancestor --
14652 ------------------------
14653
14654 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
14655 is
14656 Intfc_Elmt : Elmt_Id;
14657
14658 begin
14659 -- The predicate is satisfied if the types are the same
14660
14661 if T1 = T2 then
14662 return True;
14663
14664 -- If we've reached the top of the derivation chain then
14665 -- we know that T1 is not an ancestor of T2.
14666
14667 elsif Etype (T2) = T2 then
14668 return False;
14669
14670 -- Proceed to check T2's immediate parent
14671
14672 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
14673 return True;
14674
14675 -- Finally, check to see if T1 is an ancestor of any of T2's
14676 -- progenitors.
14677
14678 else
14679 Intfc_Elmt := First_Elmt (Interfaces (T2));
14680 while Present (Intfc_Elmt) loop
14681 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
14682 return True;
14683 end if;
14684
14685 Next_Elmt (Intfc_Elmt);
14686 end loop;
14687 end if;
14688
14689 return False;
14690 end Is_Tagged_Ancestor;
14691
14692 -- Start of processing for Check_Abstract_Primitives
14693
14694 begin
14695 -- Loop over all of the formal derived type's primitives
14696
14697 Gen_Elmt := First_Elmt (Gen_Prims);
14698 while Present (Gen_Elmt) loop
14699 Gen_Subp := Node (Gen_Elmt);
14700
14701 -- If the primitive of the formal is not abstract, then
14702 -- determine whether there is a corresponding primitive of
14703 -- the actual type that's abstract.
14704
14705 if not Is_Abstract_Subprogram (Gen_Subp) then
14706 Act_Elmt := First_Elmt (Act_Prims);
14707 while Present (Act_Elmt) loop
14708 Act_Subp := Node (Act_Elmt);
14709
14710 -- If we find an abstract primitive of the actual,
14711 -- then we need to test whether it corresponds to the
14712 -- subprogram from which the generic formal primitive
14713 -- is inherited.
14714
14715 if Is_Abstract_Subprogram (Act_Subp) then
14716 Anc_Subp := Alias (Gen_Subp);
14717
14718 -- Test whether we have a corresponding primitive
14719 -- by comparing names, kinds, formal types, and
14720 -- result types.
14721
14722 if Chars (Anc_Subp) = Chars (Act_Subp)
14723 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
14724 then
14725 Anc_Formal := First_Formal (Anc_Subp);
14726 Act_Formal := First_Formal (Act_Subp);
14727 while Present (Anc_Formal)
14728 and then Present (Act_Formal)
14729 loop
14730 Anc_F_Type := Etype (Anc_Formal);
14731 Act_F_Type := Etype (Act_Formal);
14732
14733 if Ekind (Anc_F_Type) =
14734 E_Anonymous_Access_Type
14735 then
14736 Anc_F_Type := Designated_Type (Anc_F_Type);
14737
14738 if Ekind (Act_F_Type) =
14739 E_Anonymous_Access_Type
14740 then
14741 Act_F_Type :=
14742 Designated_Type (Act_F_Type);
14743 else
14744 exit;
14745 end if;
14746
14747 elsif
14748 Ekind (Act_F_Type) = E_Anonymous_Access_Type
14749 then
14750 exit;
14751 end if;
14752
14753 Anc_F_Type := Base_Type (Anc_F_Type);
14754 Act_F_Type := Base_Type (Act_F_Type);
14755
14756 -- If the formal is controlling, then the
14757 -- the type of the actual primitive's formal
14758 -- must be derived directly or indirectly
14759 -- from the type of the ancestor primitive's
14760 -- formal.
14761
14762 if Is_Controlling_Formal (Anc_Formal) then
14763 if not Is_Tagged_Ancestor
14764 (Anc_F_Type, Act_F_Type)
14765 then
14766 exit;
14767 end if;
14768
14769 -- Otherwise the types of the formals must
14770 -- be the same.
14771
14772 elsif Anc_F_Type /= Act_F_Type then
14773 exit;
14774 end if;
14775
14776 Next_Formal (Anc_Formal);
14777 Next_Formal (Act_Formal);
14778 end loop;
14779
14780 -- If we traversed through all of the formals
14781 -- then so far the subprograms correspond, so
14782 -- now check that any result types correspond.
14783
14784 if No (Anc_Formal) and then No (Act_Formal) then
14785 Subprograms_Correspond := True;
14786
14787 if Ekind (Act_Subp) = E_Function then
14788 Anc_F_Type := Etype (Anc_Subp);
14789 Act_F_Type := Etype (Act_Subp);
14790
14791 if Ekind (Anc_F_Type) =
14792 E_Anonymous_Access_Type
14793 then
14794 Anc_F_Type :=
14795 Designated_Type (Anc_F_Type);
14796
14797 if Ekind (Act_F_Type) =
14798 E_Anonymous_Access_Type
14799 then
14800 Act_F_Type :=
14801 Designated_Type (Act_F_Type);
14802 else
14803 Subprograms_Correspond := False;
14804 end if;
14805
14806 elsif
14807 Ekind (Act_F_Type)
14808 = E_Anonymous_Access_Type
14809 then
14810 Subprograms_Correspond := False;
14811 end if;
14812
14813 Anc_F_Type := Base_Type (Anc_F_Type);
14814 Act_F_Type := Base_Type (Act_F_Type);
14815
14816 -- Now either the result types must be
14817 -- the same or, if the result type is
14818 -- controlling, the result type of the
14819 -- actual primitive must descend from the
14820 -- result type of the ancestor primitive.
14821
14822 if Subprograms_Correspond
14823 and then Anc_F_Type /= Act_F_Type
14824 and then
14825 Has_Controlling_Result (Anc_Subp)
14826 and then not Is_Tagged_Ancestor
14827 (Anc_F_Type, Act_F_Type)
14828 then
14829 Subprograms_Correspond := False;
14830 end if;
14831 end if;
14832
14833 -- Found a matching subprogram belonging to
14834 -- formal ancestor type, so actual subprogram
14835 -- corresponds and this violates 3.9.3(9).
14836
14837 if Subprograms_Correspond then
14838 Error_Msg_NE
14839 ("abstract subprogram & overrides "
14840 & "nonabstract subprogram of ancestor",
14841 Actual, Act_Subp);
14842 end if;
14843 end if;
14844 end if;
14845 end if;
14846
14847 Next_Elmt (Act_Elmt);
14848 end loop;
14849 end if;
14850
14851 Next_Elmt (Gen_Elmt);
14852 end loop;
14853 end Check_Abstract_Primitives;
14854 end if;
14855
14856 -- Verify that limitedness matches. If parent is a limited
14857 -- interface then the generic formal is not unless declared
14858 -- explicitly so. If not declared limited, the actual cannot be
14859 -- limited (see AI05-0087).
14860
14861 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
14862 if not In_Instance then
14863 Error_Msg_NE
14864 ("actual for non-limited & cannot be a limited type",
14865 Actual, Gen_T);
14866 Explain_Limited_Type (Act_T, Actual);
14867 Abandon_Instantiation (Actual);
14868 end if;
14869 end if;
14870
14871 -- Check for AI12-0036
14872
14873 declare
14874 Formal_Is_Private_Extension : constant Boolean :=
14875 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
14876
14877 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
14878
14879 begin
14880 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
14881 if not In_Instance then
14882 if Actual_Is_Tagged then
14883 Error_Msg_NE
14884 ("actual for & cannot be a tagged type", Actual, Gen_T);
14885 else
14886 Error_Msg_NE
14887 ("actual for & must be a tagged type", Actual, Gen_T);
14888 end if;
14889
14890 Abandon_Instantiation (Actual);
14891 end if;
14892 end if;
14893 end;
14894 end Validate_Derived_Type_Instance;
14895
14896 ----------------------------------------
14897 -- Validate_Discriminated_Formal_Type --
14898 ----------------------------------------
14899
14900 procedure Validate_Discriminated_Formal_Type is
14901 Formal_Discr : Entity_Id;
14902 Actual_Discr : Entity_Id;
14903 Formal_Subt : Entity_Id;
14904
14905 begin
14906 if Has_Discriminants (A_Gen_T) then
14907 if not Has_Discriminants (Act_T) then
14908 Error_Msg_NE
14909 ("actual for & must have discriminants", Actual, Gen_T);
14910 Abandon_Instantiation (Actual);
14911
14912 elsif Is_Constrained (Act_T) then
14913 Error_Msg_NE
14914 ("actual for & must be unconstrained", Actual, Gen_T);
14915 Abandon_Instantiation (Actual);
14916
14917 else
14918 Formal_Discr := First_Discriminant (A_Gen_T);
14919 Actual_Discr := First_Discriminant (Act_T);
14920 while Formal_Discr /= Empty loop
14921 if Actual_Discr = Empty then
14922 Error_Msg_N
14923 ("discriminants on actual do not match formal",
14924 Actual);
14925 Abandon_Instantiation (Actual);
14926 end if;
14927
14928 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
14929
14930 -- Access discriminants match if designated types do
14931
14932 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
14933 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
14934 E_Anonymous_Access_Type
14935 and then
14936 Subtypes_Match
14937 (Get_Instance_Of
14938 (Designated_Type (Base_Type (Formal_Subt))),
14939 Designated_Type (Base_Type (Etype (Actual_Discr))))
14940 then
14941 null;
14942
14943 elsif Base_Type (Formal_Subt) /=
14944 Base_Type (Etype (Actual_Discr))
14945 then
14946 Error_Msg_N
14947 ("types of actual discriminants must match formal",
14948 Actual);
14949 Abandon_Instantiation (Actual);
14950
14951 elsif not Subtypes_Statically_Match
14952 (Formal_Subt, Etype (Actual_Discr))
14953 and then Ada_Version >= Ada_95
14954 then
14955 Error_Msg_N
14956 ("subtypes of actual discriminants must match formal",
14957 Actual);
14958 Abandon_Instantiation (Actual);
14959 end if;
14960
14961 Next_Discriminant (Formal_Discr);
14962 Next_Discriminant (Actual_Discr);
14963 end loop;
14964
14965 if Actual_Discr /= Empty then
14966 Error_Msg_NE
14967 ("discriminants on actual do not match formal",
14968 Actual, Gen_T);
14969 Abandon_Instantiation (Actual);
14970 end if;
14971 end if;
14972 end if;
14973 end Validate_Discriminated_Formal_Type;
14974
14975 ---------------------------------------
14976 -- Validate_Incomplete_Type_Instance --
14977 ---------------------------------------
14978
14979 procedure Validate_Incomplete_Type_Instance is
14980 begin
14981 if not Is_Tagged_Type (Act_T)
14982 and then Is_Tagged_Type (A_Gen_T)
14983 then
14984 Error_Msg_NE
14985 ("actual for & must be a tagged type", Actual, Gen_T);
14986 end if;
14987
14988 Validate_Discriminated_Formal_Type;
14989 end Validate_Incomplete_Type_Instance;
14990
14991 --------------------------------------
14992 -- Validate_Interface_Type_Instance --
14993 --------------------------------------
14994
14995 procedure Validate_Interface_Type_Instance is
14996 begin
14997 if not Is_Interface (Act_T) then
14998 Error_Msg_NE
14999 ("actual for formal interface type must be an interface",
15000 Actual, Gen_T);
15001
15002 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
15003 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
15004 or else Is_Protected_Interface (A_Gen_T) /=
15005 Is_Protected_Interface (Act_T)
15006 or else Is_Synchronized_Interface (A_Gen_T) /=
15007 Is_Synchronized_Interface (Act_T)
15008 then
15009 Error_Msg_NE
15010 ("actual for interface& does not match (RM 12.5.5(4))",
15011 Actual, Gen_T);
15012 end if;
15013 end Validate_Interface_Type_Instance;
15014
15015 ------------------------------------
15016 -- Validate_Private_Type_Instance --
15017 ------------------------------------
15018
15019 procedure Validate_Private_Type_Instance is
15020 begin
15021 if Is_Limited_Type (Act_T)
15022 and then not Is_Limited_Type (A_Gen_T)
15023 then
15024 if In_Instance then
15025 null;
15026 else
15027 Error_Msg_NE
15028 ("actual for non-limited & cannot be a limited type", Actual,
15029 Gen_T);
15030 Explain_Limited_Type (Act_T, Actual);
15031 Abandon_Instantiation (Actual);
15032 end if;
15033
15034 elsif Known_To_Have_Preelab_Init (A_Gen_T)
15035 and then not Has_Preelaborable_Initialization (Act_T)
15036 then
15037 Error_Msg_NE
15038 ("actual for & must have preelaborable initialization", Actual,
15039 Gen_T);
15040
15041 elsif not Is_Definite_Subtype (Act_T)
15042 and then Is_Definite_Subtype (A_Gen_T)
15043 and then No (Class_Wide_Equivalent_Type (Act_T))
15044 and then Ada_Version >= Ada_95
15045 then
15046 Error_Msg_NE
15047 ("actual for & must be a definite subtype", Actual, Gen_T);
15048
15049 elsif not Is_Tagged_Type (Act_T)
15050 and then Is_Tagged_Type (A_Gen_T)
15051 then
15052 Error_Msg_NE
15053 ("actual for & must be a tagged type", Actual, Gen_T);
15054
15055 -- For generic formal tagged types with the First_Controlling_Param
15056 -- aspect, ensure that the actual type also has this aspect.
15057
15058 elsif Is_Tagged_Type (Act_T)
15059 and then Is_Tagged_Type (A_Gen_T)
15060 and then not Has_First_Controlling_Parameter_Aspect (Act_T)
15061 and then Has_First_Controlling_Parameter_Aspect (A_Gen_T)
15062 then
15063 Error_Msg_NE
15064 ("actual for & must be a 'First_'Controlling_'Parameter tagged "
15065 & "type", Actual, Gen_T);
15066 end if;
15067
15068 Validate_Discriminated_Formal_Type;
15069 Ancestor := Gen_T;
15070 end Validate_Private_Type_Instance;
15071
15072 -- Start of processing for Instantiate_Type
15073
15074 begin
15075 if not Is_Entity_Name (Actual)
15076 or else not Is_Type (Entity (Actual))
15077 then
15078 Error_Msg_NE
15079 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
15080 Abandon_Instantiation (Actual);
15081 end if;
15082
15083 Act_T := Entity (Actual);
15084
15085 -- Obtain the class-wide equivalent type and use it for the
15086 -- instantiation instead of a mutably tagged type.
15087
15088 if Present (Class_Wide_Equivalent_Type (Act_T)) then
15089 Act_T := Class_Wide_Equivalent_Type (Act_T);
15090 end if;
15091
15092 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
15093 -- as a generic actual parameter if the corresponding formal type
15094 -- does not have a known_discriminant_part, or is a formal derived
15095 -- type that is an Unchecked_Union type.
15096
15097 if Is_Unchecked_Union (Base_Type (Act_T)) then
15098 if not Has_Discriminants (A_Gen_T)
15099 or else (Is_Derived_Type (A_Gen_T)
15100 and then Is_Unchecked_Union (A_Gen_T))
15101 then
15102 null;
15103 else
15104 Error_Msg_N ("unchecked union cannot be the actual for a "
15105 & "discriminated formal type", Act_T);
15106
15107 end if;
15108 end if;
15109
15110 -- Deal with fixed/floating restrictions
15111
15112 if Is_Floating_Point_Type (Act_T) then
15113 Check_Restriction (No_Floating_Point, Actual);
15114 elsif Is_Fixed_Point_Type (Act_T) then
15115 Check_Restriction (No_Fixed_Point, Actual);
15116 end if;
15117
15118 -- Deal with error of using incomplete type as generic actual.
15119 -- This includes limited views of a type, even if the non-limited
15120 -- view may be available.
15121
15122 if Ekind (Act_T) = E_Incomplete_Type
15123 or else (Is_Class_Wide_Type (Act_T)
15124 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
15125 then
15126 -- If the formal is an incomplete type, the actual can be
15127 -- incomplete as well, but if an actual incomplete type has
15128 -- a full view, then we'll retrieve that.
15129
15130 if Ekind (A_Gen_T) = E_Incomplete_Type
15131 and then No (Full_View (Act_T))
15132 then
15133 null;
15134
15135 elsif Is_Class_Wide_Type (Act_T)
15136 or else No (Full_View (Act_T))
15137 then
15138 Error_Msg_N ("premature use of incomplete type", Actual);
15139 Abandon_Instantiation (Actual);
15140
15141 else
15142 Act_T := Full_View (Act_T);
15143 Set_Entity (Actual, Act_T);
15144
15145 if Has_Private_Component (Act_T) then
15146 Error_Msg_N
15147 ("premature use of type with private component", Actual);
15148 end if;
15149 end if;
15150
15151 -- Deal with error of premature use of private type as generic actual,
15152 -- which is allowed for incomplete formals.
15153
15154 elsif Ekind (A_Gen_T) /= E_Incomplete_Type then
15155 if Is_Private_Type (Act_T)
15156 and then Is_Private_Type (Base_Type (Act_T))
15157 and then not Is_Generic_Type (Act_T)
15158 and then not Is_Derived_Type (Act_T)
15159 and then No (Full_View (Root_Type (Act_T)))
15160 then
15161 Error_Msg_N ("premature use of private type", Actual);
15162
15163 elsif Has_Private_Component (Act_T) then
15164 Error_Msg_N
15165 ("premature use of type with private component", Actual);
15166 end if;
15167 end if;
15168
15169 Set_Instance_Of (A_Gen_T, Act_T);
15170
15171 -- If the type is generic, the class-wide type may also be used
15172
15173 if Is_Tagged_Type (A_Gen_T)
15174 and then Is_Tagged_Type (Act_T)
15175 and then not Is_Class_Wide_Type (A_Gen_T)
15176 then
15177 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
15178 Class_Wide_Type (Act_T));
15179 end if;
15180
15181 if not Is_Abstract_Type (A_Gen_T)
15182 and then Is_Abstract_Type (Act_T)
15183 then
15184 Error_Msg_N
15185 ("actual of non-abstract formal cannot be abstract", Actual);
15186 end if;
15187
15188 -- A generic scalar type is a first subtype for which we generate
15189 -- an anonymous base type. Indicate that the instance of this base
15190 -- is the base type of the actual.
15191
15192 if Is_Scalar_Type (A_Gen_T) then
15193 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
15194 end if;
15195
15196 Check_Shared_Variable_Control_Aspects;
15197
15198 if not Error_Posted (Act_T) then
15199 case Nkind (Def) is
15200 when N_Formal_Private_Type_Definition =>
15201 Validate_Private_Type_Instance;
15202
15203 when N_Formal_Incomplete_Type_Definition =>
15204 Validate_Incomplete_Type_Instance;
15205
15206 when N_Formal_Derived_Type_Definition =>
15207 Validate_Derived_Type_Instance;
15208
15209 when N_Formal_Discrete_Type_Definition =>
15210 if not Is_Discrete_Type (Act_T) then
15211 Error_Msg_NE
15212 ("expect discrete type in instantiation of&",
15213 Actual, Gen_T);
15214 Abandon_Instantiation (Actual);
15215 end if;
15216
15217 Diagnose_Predicated_Actual;
15218
15219 when N_Formal_Signed_Integer_Type_Definition =>
15220 if not Is_Signed_Integer_Type (Act_T) then
15221 Error_Msg_NE
15222 ("expect signed integer type in instantiation of&",
15223 Actual, Gen_T);
15224 Abandon_Instantiation (Actual);
15225 end if;
15226
15227 Diagnose_Predicated_Actual;
15228
15229 when N_Formal_Modular_Type_Definition =>
15230 if not Is_Modular_Integer_Type (Act_T) then
15231 Error_Msg_NE
15232 ("expect modular type in instantiation of &",
15233 Actual, Gen_T);
15234 Abandon_Instantiation (Actual);
15235 end if;
15236
15237 Diagnose_Predicated_Actual;
15238
15239 when N_Formal_Floating_Point_Definition =>
15240 if not Is_Floating_Point_Type (Act_T) then
15241 Error_Msg_NE
15242 ("expect float type in instantiation of &", Actual, Gen_T);
15243 Abandon_Instantiation (Actual);
15244 end if;
15245
15246 when N_Formal_Ordinary_Fixed_Point_Definition =>
15247 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
15248 Error_Msg_NE
15249 ("expect ordinary fixed point type in instantiation of &",
15250 Actual, Gen_T);
15251 Abandon_Instantiation (Actual);
15252 end if;
15253
15254 when N_Formal_Decimal_Fixed_Point_Definition =>
15255 if not Is_Decimal_Fixed_Point_Type (Act_T) then
15256 Error_Msg_NE
15257 ("expect decimal type in instantiation of &",
15258 Actual, Gen_T);
15259 Abandon_Instantiation (Actual);
15260 end if;
15261
15262 when N_Array_Type_Definition =>
15263 Validate_Array_Type_Instance;
15264
15265 when N_Access_To_Object_Definition =>
15266 Validate_Access_Type_Instance;
15267
15268 when N_Access_Function_Definition
15269 | N_Access_Procedure_Definition
15270 =>
15271 Validate_Access_Subprogram_Instance;
15272
15273 when N_Record_Definition =>
15274 Validate_Interface_Type_Instance;
15275
15276 when N_Derived_Type_Definition =>
15277 Validate_Derived_Interface_Type_Instance;
15278
15279 when others =>
15280 raise Program_Error;
15281 end case;
15282 end if;
15283
15284 Subt := New_Copy (Gen_T);
15285
15286 -- Use adjusted sloc of subtype name as the location for other nodes in
15287 -- the subtype declaration.
15288
15289 Loc := Sloc (Subt);
15290
15291 Decl_Node :=
15292 Make_Subtype_Declaration (Loc,
15293 Defining_Identifier => Subt,
15294 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
15295
15296 Copy_Ghost_Aspect (Formal, To => Decl_Node);
15297
15298 -- Record whether the actual is private at this point, so that
15299 -- Check_Generic_Actuals can restore its proper view before the
15300 -- semantic analysis of the instance.
15301
15302 if Is_Private_Type (Act_T) then
15303 Set_Has_Private_View (Subtype_Indication (Decl_Node));
15304
15305 elsif (Is_Access_Type (Act_T)
15306 and then Is_Private_Type (Designated_Type (Act_T)))
15307 or else (Is_Array_Type (Act_T)
15308 and then
15309 Is_Private_Type (Component_Type_For_Private_View (Act_T)))
15310 then
15311 Set_Has_Secondary_Private_View (Subtype_Indication (Decl_Node));
15312 end if;
15313
15314 -- In Ada 2012 the actual may be a limited view. Indicate that
15315 -- the local subtype must be treated as such.
15316
15317 if From_Limited_With (Act_T) then
15318 Mutate_Ekind (Subt, E_Incomplete_Subtype);
15319 Set_From_Limited_With (Subt);
15320 end if;
15321
15322 Decl_Nodes := New_List (Decl_Node);
15323
15324 -- Flag actual derived types so their elaboration produces the
15325 -- appropriate renamings for the primitive operations of the ancestor.
15326 -- Flag actual for formal private types as well, to determine whether
15327 -- operations in the private part may override inherited operations.
15328 -- If the formal has an interface list, the ancestor is not the
15329 -- parent, but the analyzed formal that includes the interface
15330 -- operations of all its progenitors.
15331
15332 -- Same treatment for formal private types, so we can check whether the
15333 -- type is tagged limited when validating derivations in the private
15334 -- part. (See AI05-096).
15335
15336 if Nkind (Def) = N_Formal_Derived_Type_Definition then
15337 if Present (Interface_List (Def)) then
15338 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
15339 else
15340 Set_Generic_Parent_Type (Decl_Node, Ancestor);
15341 end if;
15342
15343 elsif Nkind (Def) in N_Formal_Private_Type_Definition
15344 | N_Formal_Incomplete_Type_Definition
15345 then
15346 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
15347 end if;
15348
15349 -- If the actual is a synchronized type that implements an interface,
15350 -- the primitive operations are attached to the corresponding record,
15351 -- and we have to treat it as an additional generic actual, so that its
15352 -- primitive operations become visible in the instance. The task or
15353 -- protected type itself does not carry primitive operations.
15354
15355 if Is_Concurrent_Type (Act_T)
15356 and then Is_Tagged_Type (Act_T)
15357 and then Present (Corresponding_Record_Type (Act_T))
15358 and then Present (Ancestor)
15359 and then Is_Interface (Ancestor)
15360 then
15361 declare
15362 Corr_Rec : constant Entity_Id :=
15363 Corresponding_Record_Type (Act_T);
15364 New_Corr : Entity_Id;
15365 Corr_Decl : Node_Id;
15366
15367 begin
15368 New_Corr := Make_Temporary (Loc, 'S');
15369 Corr_Decl :=
15370 Make_Subtype_Declaration (Loc,
15371 Defining_Identifier => New_Corr,
15372 Subtype_Indication =>
15373 New_Occurrence_Of (Corr_Rec, Loc));
15374 Append_To (Decl_Nodes, Corr_Decl);
15375
15376 if Ekind (Act_T) = E_Task_Type then
15377 Mutate_Ekind (Subt, E_Task_Subtype);
15378 else
15379 Mutate_Ekind (Subt, E_Protected_Subtype);
15380 end if;
15381
15382 Set_Corresponding_Record_Type (Subt, Corr_Rec);
15383 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
15384 Set_Generic_Parent_Type (Decl_Node, Empty);
15385 end;
15386 end if;
15387
15388 -- For a floating-point type, capture dimension info if any, because
15389 -- the generated subtype declaration does not come from source and
15390 -- will not process dimensions.
15391
15392 if Is_Floating_Point_Type (Act_T) then
15393 Copy_Dimensions (Act_T, Subt);
15394 end if;
15395
15396 return Decl_Nodes;
15397 end Instantiate_Type;
15398
15399 -----------------------------
15400 -- Is_Abbreviated_Instance --
15401 -----------------------------
15402
15403 function Is_Abbreviated_Instance (E : Entity_Id) return Boolean is
15404 begin
15405 return Ekind (E) = E_Package
15406 and then Present (Hidden_In_Formal_Instance (E));
15407 end Is_Abbreviated_Instance;
15408
15409 ---------------------
15410 -- Is_In_Main_Unit --
15411 ---------------------
15412
15413 function Is_In_Main_Unit (N : Node_Id) return Boolean is
15414 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
15415 Current_Unit : Node_Id;
15416
15417 begin
15418 if Unum = Main_Unit then
15419 return True;
15420
15421 -- If the current unit is a subunit then it is either the main unit or
15422 -- is being compiled as part of the main unit.
15423
15424 elsif Nkind (N) = N_Compilation_Unit then
15425 return Nkind (Unit (N)) = N_Subunit;
15426 end if;
15427
15428 Current_Unit := Parent (N);
15429 while Present (Current_Unit)
15430 and then Nkind (Current_Unit) /= N_Compilation_Unit
15431 loop
15432 Current_Unit := Parent (Current_Unit);
15433 end loop;
15434
15435 -- The instantiation node is in the main unit, or else the current node
15436 -- (perhaps as the result of nested instantiations) is in the main unit,
15437 -- or in the declaration of the main unit, which in this last case must
15438 -- be a body.
15439
15440 return
15441 Current_Unit = Cunit (Main_Unit)
15442 or else Current_Unit = Other_Comp_Unit (Cunit (Main_Unit))
15443 or else (Present (Current_Unit)
15444 and then Present (Other_Comp_Unit (Current_Unit))
15445 and then Is_In_Main_Unit (Other_Comp_Unit (Current_Unit)));
15446 end Is_In_Main_Unit;
15447
15448 ----------------------------
15449 -- Load_Parent_Of_Generic --
15450 ----------------------------
15451
15452 procedure Load_Parent_Of_Generic
15453 (N : Node_Id;
15454 Spec : Node_Id;
15455 Body_Optional : Boolean := False)
15456 is
15457 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
15458 Saved_Style_Check : constant Boolean := Style_Check;
15459 Saved_Warn : constant Warnings_State := Save_Warnings;
15460 True_Parent : Node_Id;
15461 Inst_Node : Node_Id;
15462 OK : Boolean;
15463 Previous_Instances : constant Elist_Id := New_Elmt_List;
15464
15465 procedure Collect_Previous_Instances (Decls : List_Id);
15466 -- Collect all instantiations in the given list of declarations, that
15467 -- precede the generic that we need to load. If the bodies of these
15468 -- instantiations are available, we must analyze them, to ensure that
15469 -- the public symbols generated are the same when the unit is compiled
15470 -- to generate code, and when it is compiled in the context of a unit
15471 -- that needs a particular nested instance. This process is applied to
15472 -- both package and subprogram instances.
15473
15474 --------------------------------
15475 -- Collect_Previous_Instances --
15476 --------------------------------
15477
15478 procedure Collect_Previous_Instances (Decls : List_Id) is
15479 Decl : Node_Id;
15480
15481 begin
15482 Decl := First (Decls);
15483 while Present (Decl) loop
15484 if Sloc (Decl) >= Sloc (Inst_Node) then
15485 return;
15486
15487 -- If Decl is an instantiation, then record it as requiring
15488 -- instantiation of the corresponding body, except if it is an
15489 -- abbreviated instantiation generated internally for conformance
15490 -- checking purposes only for the case of a formal package
15491 -- declared without a box (see Instantiate_Formal_Package). Such
15492 -- an instantiation does not generate any code (the actual code
15493 -- comes from actual) and thus does not need to be analyzed here.
15494 -- If the instantiation appears with a generic package body it is
15495 -- not analyzed here either.
15496
15497 elsif Nkind (Decl) = N_Package_Instantiation
15498 and then not Is_Abbreviated_Instance (Defining_Entity (Decl))
15499 then
15500 Append_Elmt (Decl, Previous_Instances);
15501
15502 -- For a subprogram instantiation, omit instantiations intrinsic
15503 -- operations (Unchecked_Conversions, etc.) that have no bodies.
15504
15505 elsif Nkind (Decl) in N_Function_Instantiation
15506 | N_Procedure_Instantiation
15507 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
15508 then
15509 Append_Elmt (Decl, Previous_Instances);
15510
15511 elsif Nkind (Decl) = N_Package_Declaration then
15512 Collect_Previous_Instances
15513 (Visible_Declarations (Specification (Decl)));
15514 Collect_Previous_Instances
15515 (Private_Declarations (Specification (Decl)));
15516
15517 -- Previous non-generic bodies may contain instances as well
15518
15519 elsif Nkind (Decl) = N_Package_Body
15520 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
15521 then
15522 Collect_Previous_Instances (Declarations (Decl));
15523
15524 elsif Nkind (Decl) = N_Subprogram_Body
15525 and then not Acts_As_Spec (Decl)
15526 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
15527 then
15528 Collect_Previous_Instances (Declarations (Decl));
15529 end if;
15530
15531 Next (Decl);
15532 end loop;
15533 end Collect_Previous_Instances;
15534
15535 -- Start of processing for Load_Parent_Of_Generic
15536
15537 begin
15538 if not In_Same_Source_Unit (N, Spec)
15539 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
15540 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
15541 and then not Is_In_Main_Unit (Spec))
15542 then
15543 -- Find body of parent of spec, and analyze it. A special case arises
15544 -- when the parent is an instantiation, that is to say when we are
15545 -- currently instantiating a nested generic. In that case, there is
15546 -- no separate file for the body of the enclosing instance. Instead,
15547 -- the enclosing body must be instantiated as if it were a pending
15548 -- instantiation, in order to produce the body for the nested generic
15549 -- we require now. Note that in that case the generic may be defined
15550 -- in a package body, the instance defined in the same package body,
15551 -- and the original enclosing body may not be in the main unit.
15552
15553 Inst_Node := Empty;
15554
15555 True_Parent := Parent (Spec);
15556 while Present (True_Parent)
15557 and then Nkind (True_Parent) /= N_Compilation_Unit
15558 loop
15559 if Nkind (True_Parent) = N_Package_Declaration
15560 and then
15561 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
15562 then
15563 -- Parent is a compilation unit that is an instantiation, and
15564 -- instantiation node has been replaced with package decl.
15565
15566 Inst_Node := Original_Node (True_Parent);
15567 exit;
15568
15569 elsif Nkind (True_Parent) = N_Package_Declaration
15570 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
15571 and then
15572 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
15573 then
15574 -- Parent is a compilation unit that is an instantiation, but
15575 -- instantiation node has not been replaced with package decl.
15576
15577 Inst_Node := Unit (Parent (True_Parent));
15578 exit;
15579
15580 elsif Nkind (True_Parent) = N_Package_Declaration
15581 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
15582 and then Present (Generic_Parent (Specification (True_Parent)))
15583 then
15584 -- Parent is an instantiation within another specification.
15585 -- Declaration for instance has been inserted before original
15586 -- instantiation node. A direct link would be preferable?
15587
15588 Inst_Node := Next (True_Parent);
15589 while Present (Inst_Node)
15590 and then Nkind (Inst_Node) /= N_Package_Instantiation
15591 loop
15592 Next (Inst_Node);
15593 end loop;
15594
15595 -- If the instance appears within a generic, and the generic
15596 -- unit is defined within a formal package of the enclosing
15597 -- generic, there is no generic body available, and none
15598 -- needed. A more precise test should be used ???
15599
15600 if No (Inst_Node) then
15601 return;
15602 end if;
15603
15604 exit;
15605
15606 -- If an ancestor of the generic comes from a formal package
15607 -- there is no source for the ancestor body. This is detected
15608 -- by examining the scope of the ancestor and its declaration.
15609 -- The body, if any is needed, will be available when the
15610 -- current unit (containing a formal package) is instantiated.
15611
15612 elsif Nkind (True_Parent) = N_Package_Specification
15613 and then Present (Generic_Parent (True_Parent))
15614 and then Nkind
15615 (Original_Node (Unit_Declaration_Node
15616 (Scope (Generic_Parent (True_Parent)))))
15617 = N_Formal_Package_Declaration
15618 then
15619 return;
15620
15621 else
15622 True_Parent := Parent (True_Parent);
15623 end if;
15624 end loop;
15625
15626 -- Case where we are currently instantiating a nested generic
15627
15628 if Present (Inst_Node) then
15629 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
15630
15631 -- Instantiation node and declaration of instantiated package
15632 -- were exchanged when only the declaration was needed.
15633 -- Restore instantiation node before proceeding with body.
15634
15635 Set_Unit (Parent (True_Parent), Inst_Node);
15636 end if;
15637
15638 -- Now complete instantiation of enclosing body, if it appears in
15639 -- some other unit. If it appears in the current unit, the body
15640 -- will have been instantiated already.
15641
15642 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
15643
15644 -- We need to determine the expander mode to instantiate the
15645 -- enclosing body. Because the generic body we need may use
15646 -- global entities declared in the enclosing package (including
15647 -- aggregates) it is in general necessary to compile this body
15648 -- with expansion enabled, except if we are within a generic
15649 -- package, in which case the usual generic rule applies.
15650
15651 declare
15652 Exp_Status : Boolean := True;
15653 Scop : Entity_Id;
15654
15655 begin
15656 -- Loop through scopes looking for generic package
15657
15658 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
15659 while Present (Scop)
15660 and then Scop /= Standard_Standard
15661 loop
15662 if Ekind (Scop) = E_Generic_Package then
15663 Exp_Status := False;
15664 exit;
15665 end if;
15666
15667 Scop := Scope (Scop);
15668 end loop;
15669
15670 -- Collect previous instantiations in the unit that contains
15671 -- the desired generic.
15672
15673 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
15674 and then not Body_Optional
15675 then
15676 declare
15677 Decl : Elmt_Id;
15678 Info : Pending_Body_Info;
15679 Par : Node_Id;
15680
15681 begin
15682 Par := Parent (Inst_Node);
15683 while Present (Par) loop
15684 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
15685 Par := Parent (Par);
15686 end loop;
15687
15688 pragma Assert (Present (Par));
15689
15690 if Nkind (Par) = N_Package_Body then
15691 Collect_Previous_Instances (Declarations (Par));
15692
15693 elsif Nkind (Par) = N_Package_Declaration then
15694 Collect_Previous_Instances
15695 (Visible_Declarations (Specification (Par)));
15696 Collect_Previous_Instances
15697 (Private_Declarations (Specification (Par)));
15698
15699 else
15700 -- Enclosing unit is a subprogram body. In this
15701 -- case all instance bodies are processed in order
15702 -- and there is no need to collect them separately.
15703
15704 null;
15705 end if;
15706
15707 Decl := First_Elmt (Previous_Instances);
15708 while Present (Decl) loop
15709 Info :=
15710 (Inst_Node => Node (Decl),
15711 Act_Decl =>
15712 Instance_Spec (Node (Decl)),
15713 Fin_Scop => Empty,
15714 Config_Switches => Save_Config_Switches,
15715 Current_Sem_Unit =>
15716 Get_Code_Unit (Sloc (Node (Decl))),
15717 Expander_Status => Exp_Status,
15718 Local_Suppress_Stack_Top =>
15719 Local_Suppress_Stack_Top,
15720 Scope_Suppress => Scope_Suppress,
15721 Warnings => Save_Warnings);
15722
15723 -- Package instance
15724
15725 if Nkind (Node (Decl)) = N_Package_Instantiation
15726 then
15727 Instantiate_Package_Body
15728 (Info, Body_Optional => True);
15729
15730 -- Subprogram instance
15731
15732 else
15733 -- The instance_spec is in the wrapper package,
15734 -- usually followed by its local renaming
15735 -- declaration. See Build_Subprogram_Renaming
15736 -- for details. If the instance carries aspects,
15737 -- these result in the corresponding pragmas,
15738 -- inserted after the subprogram declaration.
15739 -- They must be skipped as well when retrieving
15740 -- the desired spec. Some of them may have been
15741 -- rewritten as null statements.
15742 -- A direct link would be more robust ???
15743
15744 declare
15745 Decl : Node_Id :=
15746 (Last (Visible_Declarations
15747 (Specification (Info.Act_Decl))));
15748 begin
15749 while Nkind (Decl) in
15750 N_Null_Statement |
15751 N_Pragma |
15752 N_Subprogram_Renaming_Declaration
15753 loop
15754 Decl := Prev (Decl);
15755 end loop;
15756
15757 Info.Act_Decl := Decl;
15758 end;
15759
15760 Instantiate_Subprogram_Body
15761 (Info, Body_Optional => True);
15762 end if;
15763
15764 Next_Elmt (Decl);
15765 end loop;
15766 end;
15767 end if;
15768
15769 Instantiate_Package_Body
15770 (Body_Info =>
15771 ((Inst_Node => Inst_Node,
15772 Act_Decl => True_Parent,
15773 Fin_Scop => Empty,
15774 Config_Switches => Save_Config_Switches,
15775 Current_Sem_Unit =>
15776 Get_Code_Unit (Sloc (Inst_Node)),
15777 Expander_Status => Exp_Status,
15778 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
15779 Scope_Suppress => Scope_Suppress,
15780 Warnings => Save_Warnings)),
15781 Body_Optional => Body_Optional);
15782 end;
15783 end if;
15784
15785 -- Case where we are not instantiating a nested generic
15786
15787 else
15788 Opt.Style_Check := False;
15789 Expander_Mode_Save_And_Set (True);
15790 Load_Needed_Body (Comp_Unit, OK);
15791 Opt.Style_Check := Saved_Style_Check;
15792 Restore_Warnings (Saved_Warn);
15793 Expander_Mode_Restore;
15794
15795 if not OK
15796 and then Unit_Requires_Body (Defining_Entity (Spec))
15797 and then not Body_Optional
15798 then
15799 declare
15800 Bname : constant Unit_Name_Type :=
15801 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
15802
15803 begin
15804 -- In CodePeer mode, the missing body may make the analysis
15805 -- incomplete, but we do not treat it as fatal.
15806
15807 if CodePeer_Mode then
15808 return;
15809
15810 else
15811 Error_Msg_Unit_1 := Bname;
15812 Error_Msg_N ("this instantiation requires$!", N);
15813 Error_Msg_File_1 :=
15814 Get_File_Name (Bname, Subunit => False);
15815 Error_Msg_N ("\but file{ was not found!", N);
15816 raise Unrecoverable_Error;
15817 end if;
15818 end;
15819 end if;
15820 end if;
15821 end if;
15822
15823 -- If loading parent of the generic caused an instantiation circularity,
15824 -- we abandon compilation at this point, because otherwise in some cases
15825 -- we get into trouble with infinite recursions after this point.
15826
15827 if Circularity_Detected then
15828 raise Unrecoverable_Error;
15829 end if;
15830 end Load_Parent_Of_Generic;
15831
15832 ---------------------------------
15833 -- Map_Formal_Package_Entities --
15834 ---------------------------------
15835
15836 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
15837 E1 : Entity_Id;
15838 E2 : Entity_Id;
15839
15840 begin
15841 Set_Instance_Of (Form, Act);
15842
15843 -- Traverse formal and actual package to map the corresponding entities.
15844 -- We skip over internal entities that may be generated during semantic
15845 -- analysis, and find the matching entities by name, given that they
15846 -- must appear in the same order.
15847
15848 E1 := First_Entity (Form);
15849 E2 := First_Entity (Act);
15850 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
15851 -- Could this test be a single condition??? Seems like it could, and
15852 -- isn't FPE (Form) a constant anyway???
15853
15854 if not Is_Internal (E1)
15855 and then Present (Parent (E1))
15856 and then not Is_Class_Wide_Type (E1)
15857 and then not Is_Internal_Name (Chars (E1))
15858 then
15859 while Present (E2) and then Chars (E2) /= Chars (E1) loop
15860 Next_Entity (E2);
15861 end loop;
15862
15863 if No (E2) then
15864 exit;
15865 else
15866 Set_Instance_Of (E1, E2);
15867
15868 if Is_Type (E1) and then Is_Tagged_Type (E2) then
15869 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
15870 end if;
15871
15872 if Ekind (E1) = E_Package and then No (Renamed_Entity (E1)) then
15873 Map_Formal_Package_Entities (E1, E2);
15874 end if;
15875 end if;
15876 end if;
15877
15878 Next_Entity (E1);
15879 end loop;
15880 end Map_Formal_Package_Entities;
15881
15882 -----------------------
15883 -- Move_Freeze_Nodes --
15884 -----------------------
15885
15886 procedure Move_Freeze_Nodes
15887 (Out_Of : Entity_Id;
15888 After : Node_Id;
15889 L : List_Id)
15890 is
15891 Decl : Node_Id;
15892 Next_Decl : Node_Id;
15893 Next_Node : Node_Id := After;
15894 Spec : Node_Id;
15895
15896 function Is_Outer_Type (T : Entity_Id) return Boolean;
15897 -- Check whether entity is declared in a scope external to that of the
15898 -- generic unit.
15899
15900 -------------------
15901 -- Is_Outer_Type --
15902 -------------------
15903
15904 function Is_Outer_Type (T : Entity_Id) return Boolean is
15905 Scop : Entity_Id := Scope (T);
15906
15907 begin
15908 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
15909 return True;
15910
15911 else
15912 while Scop /= Standard_Standard loop
15913 if Scop = Out_Of then
15914 return False;
15915 else
15916 Scop := Scope (Scop);
15917 end if;
15918 end loop;
15919
15920 return True;
15921 end if;
15922 end Is_Outer_Type;
15923
15924 -- Start of processing for Move_Freeze_Nodes
15925
15926 begin
15927 if No (L) then
15928 return;
15929 end if;
15930
15931 -- First remove the freeze nodes that may appear before all other
15932 -- declarations.
15933
15934 Decl := First (L);
15935 while Present (Decl)
15936 and then Nkind (Decl) = N_Freeze_Entity
15937 and then Is_Outer_Type (Entity (Decl))
15938 loop
15939 Decl := Remove_Head (L);
15940 Insert_After (Next_Node, Decl);
15941 Set_Analyzed (Decl, False);
15942 Next_Node := Decl;
15943 Decl := First (L);
15944 end loop;
15945
15946 -- Next scan the list of declarations and remove each freeze node that
15947 -- appears ahead of the current node.
15948
15949 while Present (Decl) loop
15950 while Present (Next (Decl))
15951 and then Nkind (Next (Decl)) = N_Freeze_Entity
15952 and then Is_Outer_Type (Entity (Next (Decl)))
15953 loop
15954 Next_Decl := Remove_Next (Decl);
15955 Insert_After (Next_Node, Next_Decl);
15956 Set_Analyzed (Next_Decl, False);
15957 Next_Node := Next_Decl;
15958 end loop;
15959
15960 -- If the declaration is a nested package or concurrent type, then
15961 -- recurse. Nested generic packages will have been processed from the
15962 -- inside out.
15963
15964 case Nkind (Decl) is
15965 when N_Package_Declaration =>
15966 Spec := Specification (Decl);
15967
15968 when N_Task_Type_Declaration =>
15969 Spec := Task_Definition (Decl);
15970
15971 when N_Protected_Type_Declaration =>
15972 Spec := Protected_Definition (Decl);
15973
15974 when others =>
15975 Spec := Empty;
15976 end case;
15977
15978 if Present (Spec) then
15979 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
15980 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
15981 end if;
15982
15983 Next (Decl);
15984 end loop;
15985 end Move_Freeze_Nodes;
15986
15987 ----------------
15988 -- Next_Assoc --
15989 ----------------
15990
15991 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
15992 begin
15993 return Generic_Renamings.Table (E).Next_In_HTable;
15994 end Next_Assoc;
15995
15996 ------------------------
15997 -- Preanalyze_Actuals --
15998 ------------------------
15999
16000 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
16001 procedure Perform_Appropriate_Analysis (N : Node_Id);
16002 -- Determine if the actuals we are analyzing come from a generic
16003 -- instantiation that is a library unit and dispatch accordingly.
16004
16005 ----------------------------------
16006 -- Perform_Appropriate_Analysis --
16007 ----------------------------------
16008
16009 procedure Perform_Appropriate_Analysis (N : Node_Id) is
16010 begin
16011 -- When we have a library instantiation we cannot allow any expansion
16012 -- to occur, since there may be no place to put it. Instead, in that
16013 -- case we perform a preanalysis of the actual.
16014
16015 if Present (Inst) and then Is_Compilation_Unit (Inst) then
16016 Preanalyze (N);
16017 else
16018 Analyze (N);
16019 end if;
16020 end Perform_Appropriate_Analysis;
16021
16022 -- Local variables
16023
16024 Errs : constant Nat := Serious_Errors_Detected;
16025
16026 Assoc : Node_Id;
16027 Act : Node_Id;
16028
16029 Cur : Entity_Id := Empty;
16030 -- Current homograph of the instance name
16031
16032 Vis : Boolean := False;
16033 -- Saved visibility status of the current homograph
16034
16035 -- Start of processing for Preanalyze_Actuals
16036
16037 begin
16038 Assoc := First (Generic_Associations (N));
16039
16040 -- If the instance is a child unit, its name may hide an outer homonym,
16041 -- so make it invisible to perform name resolution on the actuals.
16042
16043 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
16044 and then Present
16045 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
16046 then
16047 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
16048
16049 if Is_Compilation_Unit (Cur) then
16050 Vis := Is_Immediately_Visible (Cur);
16051 Set_Is_Immediately_Visible (Cur, False);
16052 else
16053 Cur := Empty;
16054 end if;
16055 end if;
16056
16057 while Present (Assoc) loop
16058 if Nkind (Assoc) /= N_Others_Choice then
16059 Act := Explicit_Generic_Actual_Parameter (Assoc);
16060
16061 -- Within a nested instantiation, a defaulted actual is an empty
16062 -- association, so nothing to analyze. If the subprogram actual
16063 -- is an attribute, analyze prefix only, because actual is not a
16064 -- complete attribute reference.
16065
16066 -- If actual is an allocator, analyze expression only. The full
16067 -- analysis can generate code, and if instance is a compilation
16068 -- unit we have to wait until the package instance is installed
16069 -- to have a proper place to insert this code.
16070
16071 -- String literals may be operators, but at this point we do not
16072 -- know whether the actual is a formal subprogram or a string.
16073
16074 if No (Act) then
16075 null;
16076
16077 elsif Nkind (Act) = N_Attribute_Reference then
16078 Perform_Appropriate_Analysis (Prefix (Act));
16079
16080 elsif Nkind (Act) = N_Explicit_Dereference then
16081 Perform_Appropriate_Analysis (Prefix (Act));
16082
16083 elsif Nkind (Act) = N_Allocator then
16084 declare
16085 Expr : constant Node_Id := Expression (Act);
16086
16087 begin
16088 if Nkind (Expr) = N_Subtype_Indication then
16089 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
16090
16091 -- Analyze separately each discriminant constraint, when
16092 -- given with a named association.
16093
16094 declare
16095 Constr : Node_Id;
16096
16097 begin
16098 Constr := First (Constraints (Constraint (Expr)));
16099 while Present (Constr) loop
16100 if Nkind (Constr) = N_Discriminant_Association then
16101 Perform_Appropriate_Analysis
16102 (Expression (Constr));
16103 else
16104 Perform_Appropriate_Analysis (Constr);
16105 end if;
16106
16107 Next (Constr);
16108 end loop;
16109 end;
16110
16111 else
16112 Perform_Appropriate_Analysis (Expr);
16113 end if;
16114 end;
16115
16116 elsif Nkind (Act) /= N_Operator_Symbol then
16117 Perform_Appropriate_Analysis (Act);
16118
16119 -- Within a package instance, mark actuals that are limited
16120 -- views, so their use can be moved to the body of the
16121 -- enclosing unit.
16122
16123 if Is_Entity_Name (Act)
16124 and then Is_Type (Entity (Act))
16125 and then From_Limited_With (Entity (Act))
16126 and then Present (Inst)
16127 then
16128 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
16129 end if;
16130 end if;
16131
16132 if Errs /= Serious_Errors_Detected then
16133
16134 -- Do a minimal analysis of the generic, to prevent spurious
16135 -- warnings complaining about the generic being unreferenced,
16136 -- before abandoning the instantiation.
16137
16138 Perform_Appropriate_Analysis (Name (N));
16139
16140 if Is_Entity_Name (Name (N))
16141 and then Etype (Name (N)) /= Any_Type
16142 then
16143 Generate_Reference (Entity (Name (N)), Name (N));
16144 Set_Is_Instantiated (Entity (Name (N)));
16145 end if;
16146
16147 if Present (Cur) then
16148
16149 -- For the case of a child instance hiding an outer homonym,
16150 -- provide additional warning which might explain the error.
16151
16152 Set_Is_Immediately_Visible (Cur, Vis);
16153 Error_Msg_NE
16154 ("& hides outer unit with the same name??",
16155 N, Defining_Unit_Name (N));
16156 end if;
16157
16158 Abandon_Instantiation (Act);
16159 end if;
16160 end if;
16161
16162 Next (Assoc);
16163 end loop;
16164
16165 if Present (Cur) then
16166 Set_Is_Immediately_Visible (Cur, Vis);
16167 end if;
16168 end Preanalyze_Actuals;
16169
16170 -------------------------------
16171 -- Provide_Completing_Bodies --
16172 -------------------------------
16173
16174 procedure Provide_Completing_Bodies (N : Node_Id) is
16175 procedure Build_Completing_Body (Subp_Decl : Node_Id);
16176 -- Generate the completing body for subprogram declaration Subp_Decl
16177
16178 procedure Provide_Completing_Bodies_In (Decls : List_Id);
16179 -- Generating completing bodies for all subprograms found in declarative
16180 -- list Decls.
16181
16182 ---------------------------
16183 -- Build_Completing_Body --
16184 ---------------------------
16185
16186 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
16187 Loc : constant Source_Ptr := Sloc (Subp_Decl);
16188 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
16189 Spec : Node_Id;
16190
16191 begin
16192 -- Nothing to do if the subprogram already has a completing body
16193
16194 if Present (Corresponding_Body (Subp_Decl)) then
16195 return;
16196
16197 -- Mark the function as having a valid return statement even though
16198 -- the body contains a single raise statement.
16199
16200 elsif Ekind (Subp_Id) = E_Function then
16201 Set_Return_Present (Subp_Id);
16202 end if;
16203
16204 -- Clone the specification to obtain new entities and reset the only
16205 -- semantic field.
16206
16207 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
16208 Set_Generic_Parent (Spec, Empty);
16209
16210 -- Generate:
16211 -- function Func ... return ... is
16212 -- <or>
16213 -- procedure Proc ... is
16214 -- begin
16215 -- raise Program_Error with "access before elaboration";
16216 -- edn Proc;
16217
16218 Insert_After_And_Analyze (Subp_Decl,
16219 Make_Subprogram_Body (Loc,
16220 Specification => Spec,
16221 Declarations => New_List,
16222 Handled_Statement_Sequence =>
16223 Make_Handled_Sequence_Of_Statements (Loc,
16224 Statements => New_List (
16225 Make_Raise_Program_Error (Loc,
16226 Reason => PE_Access_Before_Elaboration)))));
16227 end Build_Completing_Body;
16228
16229 ----------------------------------
16230 -- Provide_Completing_Bodies_In --
16231 ----------------------------------
16232
16233 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
16234 Decl : Node_Id;
16235
16236 begin
16237 if Present (Decls) then
16238 Decl := First (Decls);
16239 while Present (Decl) loop
16240 Provide_Completing_Bodies (Decl);
16241 Next (Decl);
16242 end loop;
16243 end if;
16244 end Provide_Completing_Bodies_In;
16245
16246 -- Local variables
16247
16248 Spec : Node_Id;
16249
16250 -- Start of processing for Provide_Completing_Bodies
16251
16252 begin
16253 if Nkind (N) = N_Package_Declaration then
16254 Spec := Specification (N);
16255
16256 Push_Scope (Defining_Entity (N));
16257 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
16258 Provide_Completing_Bodies_In (Private_Declarations (Spec));
16259 Pop_Scope;
16260
16261 elsif Nkind (N) = N_Subprogram_Declaration then
16262 Build_Completing_Body (N);
16263 end if;
16264 end Provide_Completing_Bodies;
16265
16266 -------------------
16267 -- Remove_Parent --
16268 -------------------
16269
16270 procedure Remove_Parent (In_Body : Boolean := False) is
16271 S : Entity_Id := Current_Scope;
16272 -- S is the scope containing the instantiation just completed. The scope
16273 -- stack contains the parent instances of the instantiation, followed by
16274 -- the original S.
16275
16276 Cur_P : Entity_Id;
16277 E : Entity_Id;
16278 P : Entity_Id;
16279 Hidden : Elmt_Id;
16280
16281 begin
16282 -- After child instantiation is complete, remove from scope stack the
16283 -- extra copy of the current scope, and then remove parent instances.
16284
16285 if not In_Body then
16286 Pop_Scope;
16287
16288 while Current_Scope /= S loop
16289 P := Current_Scope;
16290 End_Package_Scope (Current_Scope);
16291
16292 if In_Open_Scopes (P) then
16293 E := First_Entity (P);
16294 while Present (E) loop
16295 Set_Is_Immediately_Visible (E, True);
16296 Next_Entity (E);
16297 end loop;
16298
16299 -- If instantiation is declared in a block, it is the enclosing
16300 -- scope that might be a parent instance. Note that only one
16301 -- block can be involved, because the parent instances have
16302 -- been installed within it.
16303
16304 if Ekind (P) = E_Block then
16305 Cur_P := Scope (P);
16306 else
16307 Cur_P := P;
16308 end if;
16309
16310 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
16311 -- We are within an instance of some sibling. Retain
16312 -- visibility of parent, for proper subsequent cleanup, and
16313 -- reinstall private declarations as well.
16314
16315 Set_In_Private_Part (P);
16316 Install_Private_Declarations (P);
16317 end if;
16318
16319 -- If the ultimate parent is a top-level unit recorded in
16320 -- Instance_Parent_Unit, then reset its visibility to what it was
16321 -- before instantiation. (It's not clear what the purpose is of
16322 -- testing whether Scope (P) is In_Open_Scopes, but that test was
16323 -- present before the ultimate parent test was added.???)
16324
16325 elsif not In_Open_Scopes (Scope (P))
16326 or else (P = Instance_Parent_Unit
16327 and then not Parent_Unit_Visible)
16328 then
16329 Set_Is_Immediately_Visible (P, False);
16330
16331 -- If the current scope is itself an instantiation of a generic
16332 -- nested within P, and we are in the private part of body of this
16333 -- instantiation, restore the full views of P, that were removed
16334 -- in End_Package_Scope above. This obscure case can occur when a
16335 -- subunit of a generic contains an instance of a child unit of
16336 -- its generic parent unit.
16337
16338 elsif S = Current_Scope and then Is_Generic_Instance (S)
16339 and then (In_Package_Body (S) or else In_Private_Part (S))
16340 then
16341 declare
16342 Par : constant Entity_Id :=
16343 Generic_Parent (Package_Specification (S));
16344 begin
16345 if Present (Par)
16346 and then P = Scope (Par)
16347 then
16348 Set_In_Private_Part (P);
16349 Install_Private_Declarations (P);
16350 end if;
16351 end;
16352 end if;
16353 end loop;
16354
16355 -- Reset visibility of entities in the enclosing scope
16356
16357 Set_Is_Hidden_Open_Scope (Current_Scope, False);
16358
16359 Hidden := First_Elmt (Hidden_Entities);
16360 while Present (Hidden) loop
16361 Set_Is_Immediately_Visible (Node (Hidden), True);
16362 Next_Elmt (Hidden);
16363 end loop;
16364
16365 else
16366 -- Each body is analyzed separately, and there is no context that
16367 -- needs preserving from one body instance to the next, so remove all
16368 -- parent scopes that have been installed.
16369
16370 while Present (S) loop
16371 End_Package_Scope (S);
16372 Set_Is_Immediately_Visible (S, False);
16373 S := Current_Scope;
16374 exit when S = Standard_Standard;
16375 end loop;
16376 end if;
16377 end Remove_Parent;
16378
16379 -----------------------------------
16380 -- Requires_Conformance_Checking --
16381 -----------------------------------
16382
16383 function Requires_Conformance_Checking (N : Node_Id) return Boolean is
16384 begin
16385 -- No conformance checking required if the generic actual part is empty,
16386 -- or is a box or an others_clause (necessarily with a box).
16387
16388 return Present (Generic_Associations (N))
16389 and then not Box_Present (N)
16390 and then Nkind (First (Generic_Associations (N))) /= N_Others_Choice;
16391 end Requires_Conformance_Checking;
16392
16393 -----------------
16394 -- Restore_Env --
16395 -----------------
16396
16397 procedure Restore_Env is
16398 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
16399
16400 begin
16401 if No (Current_Instantiated_Parent.Act_Id) then
16402 -- Restore environment after subprogram inlining
16403
16404 Restore_Private_Views (Empty);
16405 end if;
16406
16407 Current_Instantiated_Parent := Saved.Instantiated_Parent;
16408 Exchanged_Views := Saved.Exchanged_Views;
16409 Hidden_Entities := Saved.Hidden_Entities;
16410 Current_Sem_Unit := Saved.Current_Sem_Unit;
16411 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
16412 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
16413
16414 Restore_Config_Switches (Saved.Switches);
16415
16416 Instance_Envs.Decrement_Last;
16417 end Restore_Env;
16418
16419 ---------------------------
16420 -- Restore_Private_Views --
16421 ---------------------------
16422
16423 procedure Restore_Private_Views
16424 (Pack_Id : Entity_Id;
16425 Is_Package : Boolean := True)
16426 is
16427 M : Elmt_Id;
16428 E : Entity_Id;
16429 Typ : Entity_Id;
16430 Dep_Elmt : Elmt_Id;
16431 Dep_Typ : Node_Id;
16432
16433 procedure Restore_Nested_Formal (Formal : Entity_Id);
16434 -- Hide the generic formals of formal packages declared with box which
16435 -- were reachable in the current instantiation.
16436
16437 ---------------------------
16438 -- Restore_Nested_Formal --
16439 ---------------------------
16440
16441 procedure Restore_Nested_Formal (Formal : Entity_Id) is
16442 pragma Assert (Ekind (Formal) = E_Package);
16443 Ent : Entity_Id;
16444 begin
16445 if Present (Renamed_Entity (Formal))
16446 and then Denotes_Formal_Package (Renamed_Entity (Formal), True)
16447 then
16448 return;
16449
16450 elsif Present (Associated_Formal_Package (Formal)) then
16451 Ent := First_Entity (Formal);
16452 while Present (Ent) loop
16453 exit when Ekind (Ent) = E_Package
16454 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
16455
16456 Set_Is_Hidden (Ent);
16457 Set_Is_Potentially_Use_Visible (Ent, False);
16458
16459 -- If package, then recurse
16460
16461 if Ekind (Ent) = E_Package then
16462 Restore_Nested_Formal (Ent);
16463 end if;
16464
16465 Next_Entity (Ent);
16466 end loop;
16467 end if;
16468 end Restore_Nested_Formal;
16469
16470 -- Start of processing for Restore_Private_Views
16471
16472 begin
16473 M := First_Elmt (Exchanged_Views);
16474 while Present (M) loop
16475 Typ := Node (M);
16476
16477 -- Subtypes of types whose views have been exchanged, and that are
16478 -- defined within the instance, were not on the Private_Dependents
16479 -- list on entry to the instance, so they have to be exchanged
16480 -- explicitly now, in order to remain consistent with the view of the
16481 -- parent type.
16482
16483 if Ekind (Typ) in E_Private_Type
16484 | E_Limited_Private_Type
16485 | E_Record_Type_With_Private
16486 then
16487 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
16488 while Present (Dep_Elmt) loop
16489 Dep_Typ := Node (Dep_Elmt);
16490
16491 if Scope (Dep_Typ) = Pack_Id
16492 and then Present (Full_View (Dep_Typ))
16493 then
16494 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
16495 Exchange_Declarations (Dep_Typ);
16496 end if;
16497
16498 Next_Elmt (Dep_Elmt);
16499 end loop;
16500 end if;
16501
16502 Exchange_Declarations (Typ);
16503 Next_Elmt (M);
16504 end loop;
16505
16506 if No (Pack_Id) then
16507 return;
16508 end if;
16509
16510 -- Make the generic formal parameters private, and make the formal types
16511 -- into subtypes of the actuals again.
16512
16513 E := First_Entity (Pack_Id);
16514 while Present (E) loop
16515 Set_Is_Hidden (E, True);
16516
16517 if Is_Type (E)
16518 and then Nkind (Parent (E)) = N_Subtype_Declaration
16519 then
16520 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
16521 -- as it is needed to identify the subtype with the type it
16522 -- renames, when there are conversions between access types
16523 -- to these.
16524
16525 if GNATprove_Mode then
16526 null;
16527
16528 -- If the actual for E is itself a generic actual type from
16529 -- an enclosing instance, E is still a generic actual type
16530 -- outside of the current instance. This matter when resolving
16531 -- an overloaded call that may be ambiguous in the enclosing
16532 -- instance, when two of its actuals coincide.
16533
16534 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
16535 and then Is_Generic_Actual_Type
16536 (Entity (Subtype_Indication (Parent (E))))
16537 then
16538 null;
16539 else
16540 Set_Is_Generic_Actual_Type (E, False);
16541
16542 -- It might seem reasonable to clear the Is_Generic_Actual_Type
16543 -- flag also on the Full_View if the type is private, since it
16544 -- was set also on this Full_View. However, this flag is relied
16545 -- upon by Covers to spot "types exported from instantiations"
16546 -- which are implicit Full_Views built for instantiations made
16547 -- on private types and we get type mismatches if we do it when
16548 -- the block exchanging the declarations below triggers ???
16549
16550 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
16551 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
16552 -- end if;
16553 end if;
16554
16555 -- An unusual case of aliasing: the actual may also be directly
16556 -- visible in the generic, and be private there, while it is fully
16557 -- visible in the context of the instance. The internal subtype
16558 -- is private in the instance but has full visibility like its
16559 -- parent in the enclosing scope. This enforces the invariant that
16560 -- the privacy status of all private dependents of a type coincide
16561 -- with that of the parent type. This can only happen when a
16562 -- generic child unit is instantiated within a sibling.
16563
16564 if Is_Private_Type (E)
16565 and then not Is_Private_Type (Etype (E))
16566 then
16567 Exchange_Declarations (E);
16568 end if;
16569
16570 elsif Ekind (E) = E_Package then
16571
16572 -- The end of the renaming list is the renaming of the generic
16573 -- package itself. If the instance is a subprogram, all entities
16574 -- in the corresponding package are renamings. If this entity is
16575 -- a formal package, make its own formals private as well. The
16576 -- actual in this case is itself the renaming of an instantiation.
16577 -- If the entity is not a package renaming, it is the entity
16578 -- created to validate formal package actuals: ignore it.
16579
16580 -- If the actual is itself a formal package for the enclosing
16581 -- generic, or the actual for such a formal package, it remains
16582 -- visible on exit from the instance, and therefore nothing needs
16583 -- to be done either, except to keep it accessible.
16584
16585 if Is_Package and then Renamed_Entity (E) = Pack_Id then
16586 exit;
16587
16588 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
16589 null;
16590
16591 elsif
16592 Denotes_Formal_Package (Renamed_Entity (E), True, Pack_Id)
16593 then
16594 Set_Is_Hidden (E, False);
16595
16596 else
16597 declare
16598 Act_P : constant Entity_Id := Renamed_Entity (E);
16599 Id : Entity_Id;
16600
16601 begin
16602 Id := First_Entity (Act_P);
16603 while Present (Id)
16604 and then Id /= First_Private_Entity (Act_P)
16605 loop
16606 exit when Ekind (Id) = E_Package
16607 and then Renamed_Entity (Id) = Act_P;
16608
16609 Set_Is_Hidden (Id, True);
16610 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
16611
16612 if Ekind (Id) = E_Package then
16613 Restore_Nested_Formal (Id);
16614 end if;
16615
16616 Next_Entity (Id);
16617 end loop;
16618 end;
16619 end if;
16620 end if;
16621
16622 Next_Entity (E);
16623 end loop;
16624 end Restore_Private_Views;
16625
16626 --------------
16627 -- Save_Env --
16628 --------------
16629
16630 procedure Save_Env
16631 (Gen_Unit : Entity_Id;
16632 Act_Unit : Entity_Id)
16633 is
16634 begin
16635 Init_Env;
16636 Set_Instance_Env (Gen_Unit, Act_Unit);
16637 end Save_Env;
16638
16639 ----------------------------
16640 -- Save_Global_References --
16641 ----------------------------
16642
16643 procedure Save_Global_References (Templ : Node_Id) is
16644
16645 -- ??? it is horrible to use global variables in highly recursive code
16646
16647 E : Entity_Id;
16648 -- The entity of the current associated node
16649
16650 Gen_Scope : Entity_Id;
16651 -- The scope of the generic for which references are being saved
16652
16653 N2 : Node_Id;
16654 -- The current associated node
16655
16656 function Is_Global (E : Entity_Id) return Boolean;
16657 -- Check whether entity is defined outside of generic unit. Examine the
16658 -- scope of an entity, and the scope of the scope, etc, until we find
16659 -- either Standard, in which case the entity is global, or the generic
16660 -- unit itself, which indicates that the entity is local. If the entity
16661 -- is the generic unit itself, as in the case of a recursive call, or
16662 -- the enclosing generic unit, if different from the current scope, then
16663 -- it is local as well, because it will be replaced at the point of
16664 -- instantiation. On the other hand, if it is a reference to a child
16665 -- unit of a common ancestor, which appears in an instantiation, it is
16666 -- global because it is used to denote a specific compilation unit at
16667 -- the time the instantiations will be analyzed.
16668
16669 procedure Qualify_Universal_Operands
16670 (Op : Node_Id;
16671 Func_Call : Node_Id);
16672 -- Op denotes a binary or unary operator in generic template Templ. Node
16673 -- Func_Call is the function call alternative of the operator within the
16674 -- the analyzed copy of the template. Change each operand which yields a
16675 -- universal type by wrapping it into a qualified expression
16676 --
16677 -- Actual_Typ'(Operand)
16678 --
16679 -- where Actual_Typ is the type of corresponding actual parameter of
16680 -- Operand in Func_Call.
16681
16682 procedure Reset_Entity (N : Node_Id);
16683 -- Save semantic information on global entity so that it is not resolved
16684 -- again at instantiation time.
16685
16686 procedure Save_Entity_Descendants (N : Node_Id);
16687 -- Apply Save_Global_References to the two syntactic descendants of
16688 -- non-terminal nodes that carry an Associated_Node and are processed
16689 -- through Reset_Entity. Once the global entity (if any) has been
16690 -- captured together with its type, only two syntactic descendants need
16691 -- to be traversed to complete the processing of the tree rooted at N.
16692 -- This applies to Selected_Components, Expanded_Names, and to Operator
16693 -- nodes. N can also be a character literal, identifier, or operator
16694 -- symbol node, but the call has no effect in these cases.
16695
16696 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
16697 -- Default actuals in nested instances must be handled specially
16698 -- because there is no link to them from the original tree. When an
16699 -- actual subprogram is given by a default, we add an explicit generic
16700 -- association for it in the instantiation node. When we save the
16701 -- global references on the name of the instance, we recover the list
16702 -- of generic associations, and add an explicit one to the original
16703 -- generic tree, through which a global actual can be preserved.
16704 -- Similarly, if a child unit is instantiated within a sibling, in the
16705 -- context of the parent, we must preserve the identifier of the parent
16706 -- so that it can be properly resolved in a subsequent instantiation.
16707
16708 procedure Save_Global_Descendant (D : Union_Id);
16709 -- Apply Save_References recursively to the descendants of node D
16710
16711 procedure Save_References (N : Node_Id);
16712 -- This is the recursive procedure that does the work, once the
16713 -- enclosing generic scope has been established.
16714
16715 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
16716 -- If the type of N2 is global to the generic unit, save the type in
16717 -- the generic node. Just as we perform name capture for explicit
16718 -- references within the generic, we must capture the global types
16719 -- of local entities because they may participate in resolution in
16720 -- the instance.
16721
16722 ---------------
16723 -- Is_Global --
16724 ---------------
16725
16726 function Is_Global (E : Entity_Id) return Boolean is
16727 Se : Entity_Id;
16728
16729 function Is_Instance_Node (Decl : Node_Id) return Boolean;
16730 -- Determine whether the parent node of a reference to a child unit
16731 -- denotes an instantiation or a formal package, in which case the
16732 -- reference to the child unit is global, even if it appears within
16733 -- the current scope (e.g. when the instance appears within the body
16734 -- of an ancestor).
16735
16736 ----------------------
16737 -- Is_Instance_Node --
16738 ----------------------
16739
16740 function Is_Instance_Node (Decl : Node_Id) return Boolean is
16741 begin
16742 return Nkind (Decl) in N_Generic_Instantiation
16743 or else
16744 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
16745 end Is_Instance_Node;
16746
16747 -- Start of processing for Is_Global
16748
16749 begin
16750 if E = Gen_Scope then
16751 return False;
16752
16753 elsif E = Standard_Standard then
16754 return True;
16755
16756 -- E should be an entity, but it is not always
16757
16758 elsif Nkind (E) not in N_Entity then
16759 return False;
16760
16761 elsif Nkind (E) /= N_Expanded_Name
16762 and then Is_Child_Unit (E)
16763 and then (Is_Instance_Node (Parent (N2))
16764 or else (Nkind (Parent (N2)) = N_Expanded_Name
16765 and then N2 = Selector_Name (Parent (N2))
16766 and then
16767 Is_Instance_Node (Parent (Parent (N2)))))
16768 then
16769 return True;
16770
16771 else
16772 -- E may be an expanded name - typically an operator - in which
16773 -- case we must find its enclosing scope since expanded names
16774 -- don't have corresponding scopes.
16775
16776 if Nkind (E) = N_Expanded_Name then
16777 Se := Find_Enclosing_Scope (E);
16778
16779 -- Otherwise, E is an entity and will have Scope set
16780
16781 else
16782 Se := Scope (E);
16783 end if;
16784
16785 while Se /= Gen_Scope loop
16786 if Se = Standard_Standard then
16787 return True;
16788 else
16789 Se := Scope (Se);
16790 end if;
16791 end loop;
16792
16793 return False;
16794 end if;
16795 end Is_Global;
16796
16797 --------------------------------
16798 -- Qualify_Universal_Operands --
16799 --------------------------------
16800
16801 procedure Qualify_Universal_Operands
16802 (Op : Node_Id;
16803 Func_Call : Node_Id)
16804 is
16805 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
16806 -- Rewrite operand Opnd as a qualified expression of the form
16807 --
16808 -- Actual_Typ'(Opnd)
16809 --
16810 -- where Actual is the corresponding actual parameter of Opnd in
16811 -- function call Func_Call.
16812
16813 function Qualify_Type
16814 (Loc : Source_Ptr;
16815 Typ : Entity_Id) return Node_Id;
16816 -- Qualify type Typ by creating a selected component of the form
16817 --
16818 -- Scope_Of_Typ.Typ
16819
16820 ---------------------
16821 -- Qualify_Operand --
16822 ---------------------
16823
16824 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
16825 Loc : constant Source_Ptr := Sloc (Opnd);
16826 Typ : constant Entity_Id := Etype (Actual);
16827 Mark : Node_Id;
16828 Qual : Node_Id;
16829
16830 begin
16831 -- Qualify the operand when it is of a universal type. Note that
16832 -- the template is unanalyzed and it is not possible to directly
16833 -- query the type. This transformation is not done when the type
16834 -- of the actual is internally generated because the type will be
16835 -- regenerated in the instance.
16836
16837 if Yields_Universal_Type (Opnd)
16838 and then Comes_From_Source (Typ)
16839 and then not Is_Hidden (Typ)
16840 then
16841 -- The type of the actual may be a global reference. Save this
16842 -- information by creating a reference to it.
16843
16844 if Is_Global (Typ) then
16845 Mark := New_Occurrence_Of (Typ, Loc);
16846
16847 -- Otherwise rely on resolution to find the proper type within
16848 -- the instance.
16849
16850 else
16851 Mark := Qualify_Type (Loc, Typ);
16852 end if;
16853
16854 Qual :=
16855 Make_Qualified_Expression (Loc,
16856 Subtype_Mark => Mark,
16857 Expression => Relocate_Node (Opnd));
16858
16859 -- Mark the qualification to distinguish it from other source
16860 -- constructs and signal the instantiation mechanism that this
16861 -- node requires special processing. See Copy_Generic_Node for
16862 -- details.
16863
16864 Set_Is_Qualified_Universal_Literal (Qual);
16865
16866 Rewrite (Opnd, Qual);
16867 end if;
16868 end Qualify_Operand;
16869
16870 ------------------
16871 -- Qualify_Type --
16872 ------------------
16873
16874 function Qualify_Type
16875 (Loc : Source_Ptr;
16876 Typ : Entity_Id) return Node_Id
16877 is
16878 Scop : constant Entity_Id := Scope (Typ);
16879 Result : Node_Id;
16880
16881 begin
16882 Result := Make_Identifier (Loc, Chars (Typ));
16883
16884 if Present (Scop) and then not Is_Generic_Unit (Scop) then
16885 Result :=
16886 Make_Selected_Component (Loc,
16887 Prefix => Make_Identifier (Loc, Chars (Scop)),
16888 Selector_Name => Result);
16889 end if;
16890
16891 return Result;
16892 end Qualify_Type;
16893
16894 -- Local variables
16895
16896 Actuals : constant List_Id := Parameter_Associations (Func_Call);
16897
16898 -- Start of processing for Qualify_Universal_Operands
16899
16900 begin
16901 if Nkind (Op) in N_Binary_Op then
16902 Qualify_Operand (Left_Opnd (Op), First (Actuals));
16903 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
16904
16905 elsif Nkind (Op) in N_Unary_Op then
16906 Qualify_Operand (Right_Opnd (Op), First (Actuals));
16907 end if;
16908 end Qualify_Universal_Operands;
16909
16910 ------------------
16911 -- Reset_Entity --
16912 ------------------
16913
16914 procedure Reset_Entity (N : Node_Id) is
16915 function Top_Ancestor (E : Entity_Id) return Entity_Id;
16916 -- Find the ultimate ancestor of the current unit. If it is not a
16917 -- generic unit, then the name of the current unit in the prefix of
16918 -- an expanded name must be replaced with its generic homonym to
16919 -- ensure that it will be properly resolved in an instance.
16920
16921 ------------------
16922 -- Top_Ancestor --
16923 ------------------
16924
16925 function Top_Ancestor (E : Entity_Id) return Entity_Id is
16926 Par : Entity_Id;
16927
16928 begin
16929 Par := E;
16930 while Is_Child_Unit (Par) loop
16931 Par := Scope (Par);
16932 end loop;
16933
16934 return Par;
16935 end Top_Ancestor;
16936
16937 -- Start of processing for Reset_Entity
16938
16939 begin
16940 N2 := Get_Associated_Node (N);
16941 E := Entity (N2);
16942
16943 if Present (E) then
16944
16945 -- If the node is an entry call to an entry in an enclosing task,
16946 -- it is rewritten as a selected component. No global entity to
16947 -- preserve in this case, since the expansion will be redone in
16948 -- the instance.
16949
16950 if Nkind (E) not in N_Entity then
16951 Set_Associated_Node (N, Empty);
16952 Set_Etype (N, Empty);
16953 return;
16954 end if;
16955
16956 -- If the entity is an itype created as a subtype of an access
16957 -- type with a null exclusion restore source entity for proper
16958 -- visibility. The itype will be created anew in the instance.
16959
16960 if Is_Itype (E)
16961 and then Ekind (E) = E_Access_Subtype
16962 and then Is_Entity_Name (N)
16963 and then Chars (Etype (E)) = Chars (N)
16964 then
16965 E := Etype (E);
16966 Set_Entity (N2, E);
16967 Set_Etype (N2, E);
16968 end if;
16969
16970 if Is_Global (E) then
16971 Set_Global_Type (N, N2);
16972
16973 elsif Nkind (N) = N_Op_Concat
16974 and then Is_Generic_Type (Etype (N2))
16975 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
16976 or else
16977 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
16978 and then Is_Intrinsic_Subprogram (E)
16979 then
16980 null;
16981
16982 -- Entity is local. Mark generic node as unresolved. Note that now
16983 -- it does not have an entity.
16984
16985 else
16986 Set_Associated_Node (N, Empty);
16987 Set_Etype (N, Empty);
16988 end if;
16989
16990 if Nkind (Parent (N)) in N_Generic_Instantiation
16991 and then N = Name (Parent (N))
16992 then
16993 Save_Global_Defaults (Parent (N), Parent (N2));
16994 end if;
16995
16996 elsif Nkind (Parent (N)) = N_Selected_Component
16997 and then Nkind (Parent (N2)) = N_Expanded_Name
16998 then
16999 -- In case of previous errors, the tree might be malformed
17000
17001 if No (Entity (Parent (N2))) then
17002 null;
17003
17004 elsif Is_Global (Entity (Parent (N2))) then
17005 Change_Selected_Component_To_Expanded_Name (Parent (N));
17006 Set_Associated_Node (Parent (N), Parent (N2));
17007 Set_Global_Type (Parent (N), Parent (N2));
17008 Save_Entity_Descendants (N);
17009
17010 -- If this is a reference to the current generic entity, replace
17011 -- by the name of the generic homonym of the current package. This
17012 -- is because in an instantiation Par.P.Q will not resolve to the
17013 -- name of the instance, whose enclosing scope is not necessarily
17014 -- Par. We use the generic homonym rather that the name of the
17015 -- generic itself because it may be hidden by a local declaration.
17016
17017 elsif In_Open_Scopes (Entity (Parent (N2)))
17018 and then not
17019 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
17020 then
17021 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
17022 Rewrite (Parent (N),
17023 Make_Identifier (Sloc (N),
17024 Chars =>
17025 Chars (Generic_Homonym (Entity (Parent (N2))))));
17026 else
17027 Rewrite (Parent (N),
17028 Make_Identifier (Sloc (N),
17029 Chars => Chars (Selector_Name (Parent (N2)))));
17030 end if;
17031 end if;
17032
17033 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
17034 and then Parent (N) = Name (Parent (Parent (N)))
17035 then
17036 Save_Global_Defaults
17037 (Parent (Parent (N)), Parent (Parent (N2)));
17038 end if;
17039
17040 -- A selected component may denote a static constant that has been
17041 -- folded. If the static constant is global to the generic, capture
17042 -- its value. Otherwise the folding will happen in any instantiation.
17043
17044 elsif Nkind (Parent (N)) = N_Selected_Component
17045 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
17046 then
17047 if Present (Entity (Original_Node (Parent (N2))))
17048 and then Is_Global (Entity (Original_Node (Parent (N2))))
17049 then
17050 Rewrite (Parent (N), New_Copy (Parent (N2)));
17051 Set_Analyzed (Parent (N), False);
17052 end if;
17053
17054 -- A selected component may be transformed into a parameterless
17055 -- function call. If the called entity is global, rewrite the node
17056 -- appropriately, i.e. as an extended name for the global entity.
17057
17058 elsif Nkind (Parent (N)) = N_Selected_Component
17059 and then Nkind (Parent (N2)) = N_Function_Call
17060 and then N = Selector_Name (Parent (N))
17061 then
17062 if No (Parameter_Associations (Parent (N2))) then
17063 if Is_Global (Entity (Name (Parent (N2)))) then
17064 Change_Selected_Component_To_Expanded_Name (Parent (N));
17065 Set_Associated_Node (Parent (N), Name (Parent (N2)));
17066 Set_Global_Type (Parent (N), Name (Parent (N2)));
17067 Save_Entity_Descendants (N);
17068
17069 else
17070 Set_Is_Prefixed_Call (Parent (N));
17071 Set_Associated_Node (N, Empty);
17072 Set_Etype (N, Empty);
17073 end if;
17074
17075 -- In Ada 2005, X.F may be a call to a primitive operation,
17076 -- rewritten as F (X). This rewriting will be done again in an
17077 -- instance, so keep the original node. Global entities will be
17078 -- captured as for other constructs. Indicate that this must
17079 -- resolve as a call, to prevent accidental overloading in the
17080 -- instance, if both a component and a primitive operation appear
17081 -- as candidates.
17082
17083 else
17084 Set_Is_Prefixed_Call (Parent (N));
17085 end if;
17086
17087 -- Entity is local. Reset in generic unit, so that node is resolved
17088 -- anew at the point of instantiation.
17089
17090 else
17091 Set_Associated_Node (N, Empty);
17092 Set_Etype (N, Empty);
17093 end if;
17094 end Reset_Entity;
17095
17096 -----------------------------
17097 -- Save_Entity_Descendants --
17098 -----------------------------
17099
17100 procedure Save_Entity_Descendants (N : Node_Id) is
17101 begin
17102 case Nkind (N) is
17103 when N_Binary_Op =>
17104 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
17105 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
17106
17107 when N_Unary_Op =>
17108 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
17109
17110 when N_Expanded_Name
17111 | N_Selected_Component
17112 =>
17113 Save_Global_Descendant (Union_Id (Prefix (N)));
17114 Save_Global_Descendant (Union_Id (Selector_Name (N)));
17115
17116 when N_Character_Literal
17117 | N_Identifier
17118 | N_Operator_Symbol
17119 =>
17120 null;
17121
17122 when others =>
17123 raise Program_Error;
17124 end case;
17125 end Save_Entity_Descendants;
17126
17127 --------------------------
17128 -- Save_Global_Defaults --
17129 --------------------------
17130
17131 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
17132 Loc : constant Source_Ptr := Sloc (N1);
17133 Assoc2 : constant List_Id := Generic_Associations (N2);
17134 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
17135 Assoc1 : List_Id;
17136 Act1 : Node_Id;
17137 Act2 : Node_Id;
17138 Def : Node_Id;
17139 Ndec : Node_Id;
17140 Subp : Entity_Id;
17141 Actual : Entity_Id;
17142
17143 begin
17144 Assoc1 := Generic_Associations (N1);
17145
17146 if Present (Assoc1) then
17147 Act1 := First (Assoc1);
17148 else
17149 Act1 := Empty;
17150 Set_Generic_Associations (N1, New_List);
17151 Assoc1 := Generic_Associations (N1);
17152 end if;
17153
17154 if Present (Assoc2) then
17155 Act2 := First (Assoc2);
17156 else
17157 return;
17158 end if;
17159
17160 while Present (Act1) and then Present (Act2) loop
17161 Next (Act1);
17162 Next (Act2);
17163 end loop;
17164
17165 -- Find the associations added for default subprograms
17166
17167 if Present (Act2) then
17168 while Nkind (Act2) /= N_Generic_Association
17169 or else No (Entity (Selector_Name (Act2)))
17170 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
17171 loop
17172 Next (Act2);
17173 end loop;
17174
17175 -- Add a similar association if the default is global. The
17176 -- renaming declaration for the actual has been analyzed, and
17177 -- its alias is the program it renames. Link the actual in the
17178 -- original generic tree with the node in the analyzed tree.
17179
17180 while Present (Act2) loop
17181 Subp := Entity (Selector_Name (Act2));
17182 Def := Explicit_Generic_Actual_Parameter (Act2);
17183
17184 -- Following test is defence against rubbish errors
17185
17186 if No (Alias (Subp)) then
17187 return;
17188 end if;
17189
17190 -- Retrieve the resolved actual from the renaming declaration
17191 -- created for the instantiated formal.
17192
17193 Actual := Entity (Name (Parent (Parent (Subp))));
17194 Set_Entity (Def, Actual);
17195 Set_Etype (Def, Etype (Actual));
17196
17197 if Is_Global (Actual) then
17198 Ndec :=
17199 Make_Generic_Association (Loc,
17200 Selector_Name =>
17201 New_Occurrence_Of (Subp, Loc),
17202 Explicit_Generic_Actual_Parameter =>
17203 New_Occurrence_Of (Actual, Loc));
17204
17205 Set_Associated_Node
17206 (Explicit_Generic_Actual_Parameter (Ndec), Def);
17207
17208 Append (Ndec, Assoc1);
17209
17210 -- If there are other defaults, add a dummy association in case
17211 -- there are other defaulted formals with the same name.
17212 -- Note that we are creating an N_Generic_Association with
17213 -- neither Explicit_Generic_Actual_Parameter nor Box_Present.
17214
17215 elsif Present (Next (Act2)) then
17216 Ndec :=
17217 Make_Generic_Association (Loc,
17218 Selector_Name => New_Occurrence_Of (Subp, Loc),
17219 Explicit_Generic_Actual_Parameter => Empty);
17220
17221 Append (Ndec, Assoc1);
17222 end if;
17223
17224 Next (Act2);
17225 end loop;
17226 end if;
17227
17228 if Nkind (Name (N1)) = N_Identifier
17229 and then Is_Child_Unit (Gen_Id)
17230 and then Is_Global (Gen_Id)
17231 and then Is_Generic_Unit (Scope (Gen_Id))
17232 and then In_Open_Scopes (Scope (Gen_Id))
17233 then
17234 -- This is an instantiation of a child unit within a sibling, so
17235 -- that the generic parent is in scope. An eventual instance must
17236 -- occur within the scope of an instance of the parent. Make name
17237 -- in instance into an expanded name, to preserve the identifier
17238 -- of the parent, so it can be resolved subsequently.
17239
17240 Rewrite (Name (N2),
17241 Make_Expanded_Name (Loc,
17242 Chars => Chars (Gen_Id),
17243 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
17244 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
17245 Set_Entity (Name (N2), Gen_Id);
17246
17247 Rewrite (Name (N1),
17248 Make_Expanded_Name (Loc,
17249 Chars => Chars (Gen_Id),
17250 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
17251 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
17252
17253 Set_Associated_Node (Name (N1), Name (N2));
17254 Set_Associated_Node (Prefix (Name (N1)), Empty);
17255 Set_Associated_Node
17256 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
17257 Set_Etype (Name (N1), Etype (Gen_Id));
17258 end if;
17259 end Save_Global_Defaults;
17260
17261 ----------------------------
17262 -- Save_Global_Descendant --
17263 ----------------------------
17264
17265 procedure Save_Global_Descendant (D : Union_Id) is
17266 N1 : Node_Id;
17267
17268 begin
17269 if D in Node_Range then
17270 if D = Union_Id (Empty) then
17271 null;
17272
17273 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
17274 Save_References (Node_Id (D));
17275 end if;
17276
17277 elsif D in List_Range then
17278 pragma Assert (D /= Union_Id (No_List));
17279 -- Because No_List = Empty, which is in Node_Range above
17280
17281 N1 := First (List_Id (D));
17282 while Present (N1) loop
17283 Save_References (N1);
17284 Next (N1);
17285 end loop;
17286
17287 -- Element list or other non-node field, nothing to do
17288
17289 else
17290 null;
17291 end if;
17292 end Save_Global_Descendant;
17293
17294 ---------------------
17295 -- Save_References --
17296 ---------------------
17297
17298 -- This is the recursive procedure that does the work once the enclosing
17299 -- generic scope has been established. We have to treat specially a
17300 -- number of node rewritings that are required by semantic processing
17301 -- and which change the kind of nodes in the generic copy: typically
17302 -- constant-folding, replacing an operator node by a string literal, or
17303 -- a selected component by an expanded name. In each of those cases, the
17304 -- transformation is propagated to the generic unit.
17305
17306 procedure Save_References (N : Node_Id) is
17307 Loc : constant Source_Ptr := Sloc (N);
17308
17309 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
17310 -- Determine whether arbitrary node Nod requires delayed capture of
17311 -- global references within its aspect specifications.
17312
17313 procedure Save_References_In_Aggregate (N : Node_Id);
17314 -- Save all global references in [extension] aggregate node N
17315
17316 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
17317 -- Save all global references in a character literal or operator
17318 -- symbol denoted by N.
17319
17320 procedure Save_References_In_Descendants (N : Node_Id);
17321 -- Save all global references in all descendants of node N
17322
17323 procedure Save_References_In_Identifier (N : Node_Id);
17324 -- Save all global references in identifier node N
17325
17326 procedure Save_References_In_Operator (N : Node_Id);
17327 -- Save all global references in operator node N
17328
17329 procedure Save_References_In_Pragma (Prag : Node_Id);
17330 -- Save all global references found within the expression of pragma
17331 -- Prag.
17332
17333 ---------------------------
17334 -- Requires_Delayed_Save --
17335 ---------------------------
17336
17337 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
17338 begin
17339 -- Generic packages and subprograms require delayed capture of
17340 -- global references within their aspects due to the timing of
17341 -- annotation analysis.
17342
17343 if Nkind (Nod) in N_Generic_Package_Declaration
17344 | N_Generic_Subprogram_Declaration
17345 | N_Package_Body
17346 | N_Package_Body_Stub
17347 | N_Subprogram_Body
17348 | N_Subprogram_Body_Stub
17349 then
17350 -- Since the capture of global references is done on the
17351 -- unanalyzed generic template, there is no information around
17352 -- to infer the context. Use the Associated_Entity linkages to
17353 -- peek into the analyzed generic copy and determine what the
17354 -- template corresponds to.
17355
17356 if Nod = Templ then
17357 return
17358 Is_Generic_Declaration_Or_Body
17359 (Unit_Declaration_Node
17360 (Get_Associated_Entity (Defining_Entity (Nod))));
17361
17362 -- Otherwise the generic unit being processed is not the top
17363 -- level template. It is safe to capture of global references
17364 -- within the generic unit because at this point the top level
17365 -- copy is fully analyzed.
17366
17367 else
17368 return False;
17369 end if;
17370
17371 -- Otherwise capture the global references without interference
17372
17373 else
17374 return False;
17375 end if;
17376 end Requires_Delayed_Save;
17377
17378 ----------------------------------
17379 -- Save_References_In_Aggregate --
17380 ----------------------------------
17381
17382 procedure Save_References_In_Aggregate (N : Node_Id) is
17383 Qual : Node_Id := Empty;
17384 Typ : Entity_Id := Empty;
17385
17386 begin
17387 N2 := Get_Associated_Node (N);
17388
17389 if Present (N2) then
17390 Typ := Etype (N2);
17391
17392 -- In an instance within a generic, use the name of the actual
17393 -- and not the original generic parameter. If the actual is
17394 -- global in the current generic it must be preserved for its
17395 -- instantiation.
17396
17397 if Parent_Kind (Typ) = N_Subtype_Declaration
17398 and then Present (Generic_Parent_Type (Parent (Typ)))
17399 then
17400 Typ := Base_Type (Typ);
17401 Set_Etype (N2, Typ);
17402 end if;
17403 end if;
17404
17405 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
17406 Set_Associated_Node (N, Empty);
17407
17408 -- For a full aggregate, if the type is local but is a derived
17409 -- tagged type of a global ancestor, we will need to have the
17410 -- full view of this global ancestor available in the instance
17411 -- in order to analyze the full aggregate.
17412
17413 if Present (N2)
17414 and then Nkind (N2) = N_Aggregate
17415 and then Present (Typ)
17416 and then Is_Tagged_Type (Typ)
17417 and then Is_Derived_Type (Typ)
17418 then
17419 declare
17420 Root_Typ : constant Entity_Id := Root_Type (Typ);
17421
17422 Parent_Typ : Entity_Id := Typ;
17423
17424 begin
17425 loop
17426 Parent_Typ := Etype (Parent_Typ);
17427
17428 if Is_Global (Parent_Typ) then
17429 Set_Ancestor_Type (N, Parent_Typ);
17430 exit;
17431 end if;
17432
17433 exit when Parent_Typ = Root_Typ;
17434 end loop;
17435 end;
17436 end if;
17437
17438 -- If the aggregate is an actual in a subprogram call, it has
17439 -- been resolved in the current context to some local type. The
17440 -- enclosing call may have been disambiguated by the aggregate,
17441 -- and this disambiguation might fail at instantiation time
17442 -- because the type to which the aggregate did resolve is not
17443 -- preserved. In order to preserve some of this information,
17444 -- wrap the aggregate in a qualified expression, using the id
17445 -- of its type. For further disambiguation we qualify the type
17446 -- name with its scope recursively (if visible and not hidden
17447 -- by a local homograph) because both will have corresponding
17448 -- entities in an instance. This resolves most of the problems
17449 -- with missing type information on aggregates in instances.
17450
17451 if Present (N2)
17452 and then Nkind (N2) = Nkind (N)
17453 and then Nkind (Parent (N2)) in N_Subprogram_Call
17454 and then Present (Typ)
17455 and then Comes_From_Source (Typ)
17456 then
17457 declare
17458 function Qualify_Name (S, E : Entity_Id) return Node_Id is
17459 (if E = S
17460 then Make_Identifier (Loc, Chars (E))
17461 else Make_Selected_Component (Loc,
17462 Prefix => Qualify_Name (S, Scope (E)),
17463 Selector_Name =>
17464 Make_Identifier (Loc, Chars (E))));
17465 -- Return the qualified name of E up to scope S
17466
17467 Nam : Node_Id;
17468 S : Entity_Id;
17469
17470 begin
17471 S := Scope (Typ);
17472 while not Is_Immediately_Visible (S) loop
17473 S := Scope (S);
17474 exit when Is_Generic_Unit (S);
17475 end loop;
17476
17477 if not Is_Generic_Unit (S)
17478 and then (not In_Open_Scopes (S)
17479 or else Current_Entity (S) = S)
17480 then
17481 Nam := Qualify_Name (S, Typ);
17482 else
17483 Nam := Make_Identifier (Loc, Chars (Typ));
17484 end if;
17485
17486 Qual :=
17487 Make_Qualified_Expression (Loc,
17488 Subtype_Mark => Nam,
17489 Expression => Relocate_Node (N));
17490 end;
17491 end if;
17492
17493 -- For a full aggregate, if the type is global and a derived
17494 -- tagged type, we will also need to have the full view of its
17495 -- ancestor available in the instance in order to analyze the
17496 -- full aggregate.
17497
17498 elsif Present (N2)
17499 and then Nkind (N2) = N_Aggregate
17500 and then Present (Typ)
17501 and then Is_Tagged_Type (Typ)
17502 and then Is_Derived_Type (Typ)
17503 then
17504 Set_Ancestor_Type (N, Etype (Typ));
17505 end if;
17506
17507 if Nkind (N) = N_Aggregate then
17508 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
17509
17510 elsif Nkind (N) = N_Extension_Aggregate then
17511 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
17512
17513 else
17514 pragma Assert (False);
17515 end if;
17516
17517 Save_Global_Descendant (Union_Id (Expressions (N)));
17518 Save_Global_Descendant (Union_Id (Component_Associations (N)));
17519 Save_Global_Descendant (Union_Id (Etype (N)));
17520
17521 if Present (Qual) then
17522 Rewrite (N, Qual);
17523 end if;
17524 end Save_References_In_Aggregate;
17525
17526 ----------------------------------------------
17527 -- Save_References_In_Char_Lit_Or_Op_Symbol --
17528 ----------------------------------------------
17529
17530 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
17531 begin
17532 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
17533 Reset_Entity (N);
17534
17535 elsif Nkind (N) = N_Operator_Symbol
17536 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
17537 then
17538 Change_Operator_Symbol_To_String_Literal (N);
17539 end if;
17540 end Save_References_In_Char_Lit_Or_Op_Symbol;
17541
17542 ------------------------------------
17543 -- Save_References_In_Descendants --
17544 ------------------------------------
17545
17546 procedure Save_References_In_Descendants (N : Node_Id) is
17547 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
17548 begin
17549 Walk (N);
17550 end Save_References_In_Descendants;
17551
17552 -----------------------------------
17553 -- Save_References_In_Identifier --
17554 -----------------------------------
17555
17556 procedure Save_References_In_Identifier (N : Node_Id) is
17557 begin
17558 -- The node did not undergo a transformation
17559
17560 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
17561 -- If this is a discriminant reference, always save it.
17562 -- It is used in the instance to find the corresponding
17563 -- discriminant positionally rather than by name.
17564
17565 Set_Original_Discriminant
17566 (N, Original_Discriminant (Get_Associated_Node (N)));
17567
17568 Reset_Entity (N);
17569
17570 -- The analysis of the generic copy transformed the identifier
17571 -- into another construct. Propagate the changes to the template.
17572
17573 else
17574 N2 := Get_Associated_Node (N);
17575
17576 -- The identifier denotes a call to a parameterless function.
17577 -- Mark the node as resolved when the function is external.
17578
17579 if Nkind (N2) = N_Function_Call then
17580 E := Entity (Name (N2));
17581
17582 if Present (E) and then Is_Global (E) then
17583 Set_Global_Type (N, N2);
17584 else
17585 Set_Associated_Node (N, Empty);
17586 Set_Etype (N, Empty);
17587 end if;
17588
17589 -- The identifier denotes a named number that was constant
17590 -- folded. Preserve the original name for ASIS and undo the
17591 -- constant folding which will be repeated in the instance.
17592 -- Is this still needed???
17593
17594 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
17595 and then Is_Entity_Name (Original_Node (N2))
17596 then
17597 Set_Associated_Node (N, Original_Node (N2));
17598 Reset_Entity (N);
17599
17600 -- The identifier resolved to a string literal. Propagate this
17601 -- information to the generic template.
17602
17603 elsif Nkind (N2) = N_String_Literal then
17604 Rewrite (N, New_Copy (N2));
17605
17606 -- The identifier is rewritten as a dereference if it is the
17607 -- prefix of an implicit dereference. Preserve the original
17608 -- tree as the analysis of the instance will expand the node
17609 -- again, but preserve the resolved entity if it is global.
17610
17611 elsif Nkind (N2) = N_Explicit_Dereference then
17612 if Is_Entity_Name (Prefix (N2))
17613 and then Present (Entity (Prefix (N2)))
17614 and then Is_Global (Entity (Prefix (N2)))
17615 then
17616 Set_Associated_Node (N, Prefix (N2));
17617 Set_Global_Type (N, Prefix (N2));
17618
17619 elsif Nkind (Prefix (N2)) = N_Function_Call
17620 and then Is_Entity_Name (Name (Prefix (N2)))
17621 and then Present (Entity (Name (Prefix (N2))))
17622 and then Is_Global (Entity (Name (Prefix (N2))))
17623 then
17624 Rewrite (N,
17625 Make_Explicit_Dereference (Loc,
17626 Prefix =>
17627 Make_Function_Call (Loc,
17628 Name =>
17629 New_Occurrence_Of
17630 (Entity (Name (Prefix (N2))), Loc))));
17631 Set_Associated_Node
17632 (Name (Prefix (N)), Name (Prefix (N2)));
17633 Set_Global_Type (Name (Prefix (N)), Name (Prefix (N2)));
17634
17635 else
17636 Set_Associated_Node (N, Empty);
17637 Set_Etype (N, Empty);
17638 end if;
17639
17640 -- The subtype mark of a nominally unconstrained object is
17641 -- rewritten as a subtype indication using the bounds of the
17642 -- expression. Recover the original subtype mark.
17643
17644 elsif Nkind (N2) = N_Subtype_Indication
17645 and then Is_Entity_Name (Original_Node (N2))
17646 then
17647 Set_Associated_Node (N, Original_Node (N2));
17648 Reset_Entity (N);
17649 end if;
17650 end if;
17651 end Save_References_In_Identifier;
17652
17653 ---------------------------------
17654 -- Save_References_In_Operator --
17655 ---------------------------------
17656
17657 procedure Save_References_In_Operator (N : Node_Id) is
17658 begin
17659 N2 := Get_Associated_Node (N);
17660
17661 -- The node did not undergo a transformation
17662
17663 if Nkind (N) = Nkind (N2) then
17664 if Nkind (N) = N_Op_Concat then
17665 Set_Is_Component_Left_Opnd
17666 (N, Is_Component_Left_Opnd (N2));
17667 Set_Is_Component_Right_Opnd
17668 (N, Is_Component_Right_Opnd (N2));
17669 end if;
17670
17671 Reset_Entity (N);
17672
17673 -- The analysis of the generic copy transformed the operator into
17674 -- some other construct. Propagate the changes to the template if
17675 -- applicable.
17676
17677 else
17678 -- The operator resoved to a function call
17679
17680 if Nkind (N2) = N_Function_Call then
17681
17682 -- Add explicit qualifications in the generic template for
17683 -- all operands of universal type. This aids resolution by
17684 -- preserving the actual type of a literal or an attribute
17685 -- that yields a universal result.
17686
17687 Qualify_Universal_Operands (N, N2);
17688
17689 E := Entity (Name (N2));
17690
17691 if Present (E) and then Is_Global (E) then
17692 Set_Global_Type (N, N2);
17693 else
17694 Set_Associated_Node (N, Empty);
17695 Set_Etype (N, Empty);
17696 end if;
17697
17698 -- The operator was folded into a literal
17699
17700 elsif Nkind (N2) in N_Integer_Literal
17701 | N_Real_Literal
17702 | N_String_Literal
17703 then
17704 if Present (Original_Node (N2))
17705 and then Nkind (Original_Node (N2)) = Nkind (N)
17706 then
17707 -- Operation was constant-folded. Whenever possible,
17708 -- recover semantic information from unfolded node.
17709 -- This was initially done for ASIS but is apparently
17710 -- needed also for e.g. compiling a-nbnbin.adb.
17711
17712 Set_Associated_Node (N, Original_Node (N2));
17713
17714 if Nkind (N) = N_Op_Concat then
17715 Set_Is_Component_Left_Opnd (N,
17716 Is_Component_Left_Opnd (Get_Associated_Node (N)));
17717 Set_Is_Component_Right_Opnd (N,
17718 Is_Component_Right_Opnd (Get_Associated_Node (N)));
17719 end if;
17720
17721 Reset_Entity (N);
17722
17723 -- Propagate the constant folding back to the template
17724
17725 else
17726 Rewrite (N, New_Copy (N2));
17727 Set_Analyzed (N, False);
17728 end if;
17729
17730 -- The operator was folded into an enumeration literal. Retain
17731 -- the entity to avoid spurious ambiguities if it is overloaded
17732 -- at the point of instantiation or inlining.
17733
17734 elsif Nkind (N2) = N_Identifier
17735 and then Ekind (Entity (N2)) = E_Enumeration_Literal
17736 then
17737 Rewrite (N, New_Copy (N2));
17738 Set_Analyzed (N, False);
17739 end if;
17740 end if;
17741
17742 -- Complete the operands check if node has not been constant
17743 -- folded.
17744
17745 if Nkind (N) in N_Op then
17746 Save_Entity_Descendants (N);
17747 end if;
17748 end Save_References_In_Operator;
17749
17750 -------------------------------
17751 -- Save_References_In_Pragma --
17752 -------------------------------
17753
17754 procedure Save_References_In_Pragma (Prag : Node_Id) is
17755 Context : Node_Id;
17756 Do_Save : Boolean := True;
17757
17758 begin
17759 -- Do not save global references in pragmas generated from aspects
17760 -- because the pragmas will be regenerated at instantiation time.
17761
17762 if From_Aspect_Specification (Prag) then
17763 Do_Save := False;
17764
17765 -- The capture of global references within contract-related source
17766 -- pragmas associated with generic packages, subprograms or their
17767 -- respective bodies must be delayed due to timing of annotation
17768 -- analysis. Global references are still captured in routine
17769 -- Save_Global_References_In_Contract.
17770
17771 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
17772 if Is_Package_Contract_Annotation (Prag) then
17773 Context := Find_Related_Package_Or_Body (Prag);
17774 else
17775 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
17776 Context := Find_Related_Declaration_Or_Body (Prag);
17777 end if;
17778
17779 -- The use of Original_Node accounts for the case when the
17780 -- related context is generic template.
17781
17782 if Requires_Delayed_Save (Original_Node (Context)) then
17783 Do_Save := False;
17784 end if;
17785 end if;
17786
17787 -- For all other cases, save all global references within the
17788 -- descendants, but skip the following semantic fields:
17789 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
17790
17791 if Do_Save then
17792 Save_Global_Descendant
17793 (Union_Id (Pragma_Argument_Associations (N)));
17794 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
17795 end if;
17796 end Save_References_In_Pragma;
17797
17798 -- Start of processing for Save_References
17799
17800 begin
17801 if N = Empty then
17802 null;
17803
17804 -- Aggregates
17805
17806 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
17807 Save_References_In_Aggregate (N);
17808
17809 -- Character literals, operator symbols
17810
17811 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
17812 Save_References_In_Char_Lit_Or_Op_Symbol (N);
17813
17814 -- Defining identifiers
17815
17816 elsif Nkind (N) in N_Entity then
17817 null;
17818
17819 -- Identifiers
17820
17821 elsif Nkind (N) = N_Identifier then
17822 Save_References_In_Identifier (N);
17823
17824 -- Operators
17825
17826 elsif Nkind (N) in N_Op then
17827 Save_References_In_Operator (N);
17828
17829 -- Pragmas
17830
17831 elsif Nkind (N) = N_Pragma then
17832 Save_References_In_Pragma (N);
17833
17834 elsif Nkind (N) = N_Aspect_Specification then
17835 declare
17836 P : constant Node_Id := Parent (N);
17837 Expr : Node_Id;
17838 begin
17839
17840 if Permits_Aspect_Specifications (P) then
17841
17842 -- The capture of global references within aspects
17843 -- associated with generic packages, subprograms or
17844 -- their bodies must be delayed due to timing of
17845 -- annotation analysis. Global references are still
17846 -- captured in routine Save_Global_References_In_Contract.
17847
17848 if Requires_Delayed_Save (Original_Node (P)) then
17849 null;
17850
17851 -- Otherwise save all global references within the
17852 -- aspects
17853
17854 else
17855 Expr := Expression (N);
17856
17857 if Present (Expr) then
17858 Save_Global_References (Expr);
17859 end if;
17860 end if;
17861 end if;
17862 end;
17863
17864 -- Do not walk the node pointed to by Label_Construct twice
17865
17866 elsif Nkind (N) = N_Implicit_Label_Declaration then
17867 null;
17868
17869 else
17870 Save_References_In_Descendants (N);
17871 end if;
17872
17873 end Save_References;
17874
17875 ---------------------
17876 -- Set_Global_Type --
17877 ---------------------
17878
17879 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
17880 Comparison : constant Boolean := Nkind (N2) in N_Op_Compare;
17881 Typ : constant Entity_Id :=
17882 (if Comparison then Compare_Type (N2) else Etype (N2));
17883
17884 begin
17885 -- For a comparison (or equality) operator, the Etype is Boolean, so
17886 -- it is always global. But the type subject to the Has_Private_View
17887 -- processing is the Compare_Type, so we must specifically check it.
17888
17889 if Comparison then
17890 Set_Etype (N, Etype (N2));
17891
17892 if not Is_Global (Typ) then
17893 return;
17894 end if;
17895
17896 Set_Compare_Type (N, Typ);
17897
17898 else
17899 Set_Etype (N, Typ);
17900 end if;
17901
17902 -- If the entity of N is not the associated node, this is a
17903 -- nested generic and it has an associated node as well, whose
17904 -- type is already the full view (see below). Indicate that the
17905 -- original node has a private view.
17906
17907 if Entity (N) /= N2 then
17908 if Has_Private_View (Entity (N)) then
17909 Set_Has_Private_View (N);
17910 end if;
17911
17912 if Has_Secondary_Private_View (Entity (N)) then
17913 Set_Has_Secondary_Private_View (N);
17914 end if;
17915 end if;
17916
17917 -- If not a private type, deal with a secondary private view
17918
17919 if not Is_Private_Type (Typ) then
17920 if (Is_Access_Type (Typ)
17921 and then Is_Private_Type (Designated_Type (Typ)))
17922 or else (Is_Array_Type (Typ)
17923 and then
17924 Is_Private_Type (Component_Type_For_Private_View (Typ)))
17925 then
17926 Set_Has_Secondary_Private_View (N);
17927 end if;
17928
17929 -- If it is a derivation of a private type in a context where no
17930 -- full view is needed, nothing to do either.
17931
17932 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
17933 null;
17934
17935 -- Otherwise mark the type for flipping and set the full view on N2
17936 -- when available, which is necessary for Check_Private_View to swap
17937 -- back the views in case the full declaration of Typ is visible in
17938 -- the instantiation context. Note that this will be problematic if
17939 -- N2 is re-analyzed later, e.g. if it's a default value in a call.
17940
17941 else
17942 Set_Has_Private_View (N);
17943
17944 if Present (Full_View (Typ)) then
17945 if Comparison then
17946 Set_Compare_Type (N2, Full_View (Typ));
17947 else
17948 Set_Etype (N2, Full_View (Typ));
17949 end if;
17950 end if;
17951 end if;
17952
17953 if Is_Floating_Point_Type (Typ)
17954 and then Has_Dimension_System (Typ)
17955 then
17956 Copy_Dimensions (N2, N);
17957 end if;
17958 end Set_Global_Type;
17959
17960 -- Start of processing for Save_Global_References
17961
17962 begin
17963 Gen_Scope := Current_Scope;
17964
17965 -- If the generic unit is a child unit, references to entities in the
17966 -- parent are treated as local, because they will be resolved anew in
17967 -- the context of the instance of the parent.
17968
17969 while Is_Child_Unit (Gen_Scope)
17970 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
17971 loop
17972 Gen_Scope := Scope (Gen_Scope);
17973 end loop;
17974
17975 Save_References (Templ);
17976 end Save_Global_References;
17977
17978 ---------------------------------------
17979 -- Save_Global_References_In_Aspects --
17980 ---------------------------------------
17981
17982 procedure Save_Global_References_In_Aspects (N : Node_Id) is
17983 Asp : Node_Id;
17984 Expr : Node_Id;
17985
17986 begin
17987 Asp := First (Aspect_Specifications (N));
17988 while Present (Asp) loop
17989 Expr := Expression (Asp);
17990
17991 if Present (Expr) then
17992 Save_Global_References (Expr);
17993 end if;
17994
17995 Next (Asp);
17996 end loop;
17997 end Save_Global_References_In_Aspects;
17998
17999 ------------------------------------------
18000 -- Set_Copied_Sloc_For_Inherited_Pragma --
18001 ------------------------------------------
18002
18003 procedure Set_Copied_Sloc_For_Inherited_Pragma
18004 (N : Node_Id;
18005 E : Entity_Id)
18006 is
18007 begin
18008 Create_Instantiation_Source (N, E,
18009 Inlined_Body => False,
18010 Inherited_Pragma => True,
18011 Factor => S_Adjustment);
18012 end Set_Copied_Sloc_For_Inherited_Pragma;
18013
18014 --------------------------------------
18015 -- Set_Copied_Sloc_For_Inlined_Body --
18016 --------------------------------------
18017
18018 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
18019 begin
18020 Create_Instantiation_Source (N, E,
18021 Inlined_Body => True,
18022 Inherited_Pragma => False,
18023 Factor => S_Adjustment);
18024 end Set_Copied_Sloc_For_Inlined_Body;
18025
18026 ---------------------
18027 -- Set_Instance_Of --
18028 ---------------------
18029
18030 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
18031 begin
18032 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
18033 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
18034 Generic_Renamings.Increment_Last;
18035 end Set_Instance_Of;
18036
18037 --------------------
18038 -- Set_Next_Assoc --
18039 --------------------
18040
18041 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
18042 begin
18043 Generic_Renamings.Table (E).Next_In_HTable := Next;
18044 end Set_Next_Assoc;
18045
18046 -------------------
18047 -- Start_Generic --
18048 -------------------
18049
18050 procedure Start_Generic is
18051 begin
18052 -- ??? More things could be factored out in this routine.
18053 -- Should probably be done at a later stage.
18054
18055 Generic_Flags.Append (Inside_A_Generic);
18056 Inside_A_Generic := True;
18057
18058 Expander_Mode_Save_And_Set (False);
18059 end Start_Generic;
18060
18061 ----------------------
18062 -- Set_Instance_Env --
18063 ----------------------
18064
18065 -- WARNING: This routine manages SPARK regions
18066
18067 procedure Set_Instance_Env
18068 (Gen_Unit : Entity_Id;
18069 Act_Unit : Entity_Id)
18070 is
18071 Saved_AE : constant Boolean := Assertions_Enabled;
18072 Saved_CPL : constant Node_Id := Check_Policy_List;
18073 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
18074 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
18075 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
18076
18077 begin
18078 -- Regardless of the current mode, predefined units are analyzed in the
18079 -- most current Ada mode, and earlier version Ada checks do not apply
18080 -- to predefined units. Nothing needs to be done for non-internal units.
18081 -- These are always analyzed in the current mode.
18082
18083 if In_Internal_Unit (Gen_Unit) then
18084
18085 -- The following call resets all configuration attributes to default
18086 -- or the xxx_Config versions of the attributes when the current sem
18087 -- unit is the main unit. At the same time, internal units must also
18088 -- inherit certain configuration attributes from their context. It
18089 -- is unclear what these two sets are.
18090
18091 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
18092
18093 -- Reinstall relevant configuration attributes of the context
18094
18095 Assertions_Enabled := Saved_AE;
18096 Check_Policy_List := Saved_CPL;
18097 Dynamic_Elaboration_Checks := Saved_DEC;
18098
18099 Install_SPARK_Mode (Saved_SM, Saved_SMP);
18100 end if;
18101
18102 Current_Instantiated_Parent :=
18103 (Gen_Id => Gen_Unit,
18104 Act_Id => Act_Unit,
18105 Next_In_HTable => Assoc_Null);
18106 end Set_Instance_Env;
18107
18108 -----------------
18109 -- Switch_View --
18110 -----------------
18111
18112 procedure Switch_View (T : Entity_Id) is
18113 BT : constant Entity_Id := Base_Type (T);
18114 Priv_Elmt : Elmt_Id := No_Elmt;
18115 Priv_Sub : Entity_Id;
18116
18117 begin
18118 -- T may be private but its base type may have been exchanged through
18119 -- some other occurrence, in which case there is nothing to switch
18120 -- besides T itself. Note that a private dependent subtype of a private
18121 -- type might not have been switched even if the base type has been,
18122 -- because of the last branch of Check_Private_View (see comment there).
18123
18124 if not Is_Private_Type (BT) then
18125 Prepend_Elmt (Full_View (T), Exchanged_Views);
18126 Exchange_Declarations (T);
18127 return;
18128 end if;
18129
18130 Priv_Elmt := First_Elmt (Private_Dependents (BT));
18131
18132 if Present (Full_View (BT)) then
18133 Prepend_Elmt (Full_View (BT), Exchanged_Views);
18134 Exchange_Declarations (BT);
18135 end if;
18136
18137 while Present (Priv_Elmt) loop
18138 Priv_Sub := Node (Priv_Elmt);
18139
18140 if Present (Full_View (Priv_Sub)) then
18141 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
18142 Exchange_Declarations (Priv_Sub);
18143 end if;
18144
18145 Next_Elmt (Priv_Elmt);
18146 end loop;
18147 end Switch_View;
18148
18149 -----------------
18150 -- True_Parent --
18151 -----------------
18152
18153 function True_Parent (N : Node_Id) return Node_Id is
18154 begin
18155 if Nkind (Parent (N)) = N_Subunit then
18156 return Parent (Corresponding_Stub (Parent (N)));
18157 else
18158 return Parent (N);
18159 end if;
18160 end True_Parent;
18161
18162 -----------------------------
18163 -- Valid_Default_Attribute --
18164 -----------------------------
18165
18166 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
18167 Attr_Id : constant Attribute_Id :=
18168 Get_Attribute_Id (Attribute_Name (Def));
18169 T : constant Entity_Id := Entity (Prefix (Def));
18170 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
18171 Num_F : Nat;
18172 OK : Boolean;
18173
18174 begin
18175 if No (T) or else T = Any_Id then
18176 return;
18177 end if;
18178
18179 Num_F := Number_Formals (Nam);
18180
18181 case Attr_Id is
18182 when Attribute_Adjacent
18183 | Attribute_Ceiling
18184 | Attribute_Copy_Sign
18185 | Attribute_Floor
18186 | Attribute_Fraction
18187 | Attribute_Machine
18188 | Attribute_Model
18189 | Attribute_Remainder
18190 | Attribute_Rounding
18191 | Attribute_Unbiased_Rounding
18192 =>
18193 OK := Is_Fun
18194 and then Num_F = 1
18195 and then Is_Floating_Point_Type (T);
18196
18197 when Attribute_Image
18198 | Attribute_Pred
18199 | Attribute_Succ
18200 | Attribute_Value
18201 | Attribute_Wide_Image
18202 | Attribute_Wide_Value
18203 =>
18204 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
18205
18206 when Attribute_Max
18207 | Attribute_Min
18208 =>
18209 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
18210
18211 when Attribute_Input =>
18212 OK := (Is_Fun and then Num_F = 1);
18213
18214 when Attribute_Output
18215 | Attribute_Put_Image
18216 | Attribute_Read
18217 | Attribute_Write
18218 =>
18219 OK := not Is_Fun and then Num_F = 2;
18220
18221 when others =>
18222 OK := False;
18223 end case;
18224
18225 if not OK then
18226 Error_Msg_N
18227 ("attribute reference has wrong profile for subprogram", Def);
18228 end if;
18229 end Valid_Default_Attribute;
18230
18231 ----------------------------------
18232 -- Validate_Formal_Type_Default --
18233 ----------------------------------
18234
18235 procedure Validate_Formal_Type_Default (Decl : Node_Id) is
18236 Default : constant Node_Id :=
18237 Default_Subtype_Mark (Original_Node (Decl));
18238 Formal : constant Entity_Id := Defining_Identifier (Decl);
18239
18240 Def_Sub : Entity_Id; -- Default subtype mark
18241 Type_Def : Node_Id;
18242
18243 procedure Check_Discriminated_Formal;
18244 -- Check that discriminants of default for private or incomplete
18245 -- type match those of formal type.
18246
18247 function Reference_Formal (N : Node_Id) return Traverse_Result;
18248 -- Check whether formal type definition mentions a previous formal
18249 -- type of the same generic.
18250
18251 ----------------------
18252 -- Reference_Formal --
18253 ----------------------
18254
18255 function Reference_Formal (N : Node_Id) return Traverse_Result is
18256 begin
18257 if Is_Entity_Name (N)
18258 and then Present (Entity (N))
18259 and then Scope (Entity (N)) = Current_Scope
18260 then
18261 return Abandon;
18262 else
18263 return OK;
18264 end if;
18265 end Reference_Formal;
18266
18267 function Depends_On_Other_Formals is
18268 new Traverse_Func (Reference_Formal);
18269
18270 function Default_Subtype_Matches
18271 (Gen_T, Def_T : Entity_Id) return Boolean;
18272
18273 procedure Validate_Array_Type_Default;
18274 -- Verify that dimension, indices, and component types of default
18275 -- are compatible with formal array type definition.
18276
18277 procedure Validate_Derived_Type_Default;
18278 -- Verify that ancestor and progenitor types match.
18279
18280 ---------------------------------
18281 -- Check_Discriminated_Formal --
18282 ---------------------------------
18283
18284 procedure Check_Discriminated_Formal is
18285 Formal_Discr : Entity_Id;
18286 Actual_Discr : Entity_Id;
18287 Formal_Subt : Entity_Id;
18288
18289 begin
18290 if Has_Discriminants (Formal) then
18291 if not Has_Discriminants (Def_Sub) then
18292 Error_Msg_NE
18293 ("default for & must have discriminants", Default, Formal);
18294
18295 elsif Is_Constrained (Def_Sub) then
18296 Error_Msg_NE
18297 ("default for & must be unconstrained", Default, Formal);
18298
18299 else
18300 Formal_Discr := First_Discriminant (Formal);
18301 Actual_Discr := First_Discriminant (Def_Sub);
18302 while Formal_Discr /= Empty loop
18303 if Actual_Discr = Empty then
18304 Error_Msg_N
18305 ("discriminants on Formal do not match formal",
18306 Default);
18307 end if;
18308
18309 Formal_Subt := Etype (Formal_Discr);
18310
18311 -- Access discriminants match if designated types do
18312
18313 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
18314 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
18315 E_Anonymous_Access_Type
18316 and then
18317 Base_Type
18318 (Designated_Type (Base_Type (Formal_Subt))) =
18319 Base_Type
18320 (Designated_Type (Base_Type (Etype (Actual_Discr))))
18321 and then
18322 Subtypes_Statically_Match
18323 (Designated_Type (Base_Type (Formal_Subt)),
18324 Designated_Type (Base_Type (Etype (Actual_Discr))))
18325 then
18326 null;
18327
18328 elsif Base_Type (Formal_Subt) /=
18329 Base_Type (Etype (Actual_Discr))
18330 then
18331 Error_Msg_N
18332 ("types of discriminants of default must match formal",
18333 Default);
18334
18335 elsif not Subtypes_Statically_Match
18336 (Formal_Subt, Etype (Actual_Discr))
18337 and then Ada_Version >= Ada_95
18338 then
18339 Error_Msg_N
18340 ("subtypes of discriminants of default "
18341 & "must match formal",
18342 Default);
18343 end if;
18344
18345 Next_Discriminant (Formal_Discr);
18346 Next_Discriminant (Actual_Discr);
18347 end loop;
18348
18349 if Actual_Discr /= Empty then
18350 Error_Msg_NE
18351 ("discriminants on default do not match formal",
18352 Default, Formal);
18353 end if;
18354 end if;
18355 end if;
18356 end Check_Discriminated_Formal;
18357
18358 ---------------------------
18359 -- Default_Subtype_Matches --
18360 ---------------------------
18361
18362 function Default_Subtype_Matches
18363 (Gen_T, Def_T : Entity_Id) return Boolean
18364 is
18365 begin
18366 -- Check that the base types, root types (when dealing with class
18367 -- wide types), or designated types (when dealing with anonymous
18368 -- access types) of Gen_T and Def_T are statically matching subtypes.
18369
18370 return (Base_Type (Gen_T) = Base_Type (Def_T)
18371 and then Subtypes_Statically_Match (Gen_T, Def_T))
18372
18373 or else (Is_Class_Wide_Type (Gen_T)
18374 and then Is_Class_Wide_Type (Def_T)
18375 and then Default_Subtype_Matches
18376 (Root_Type (Gen_T), Root_Type (Def_T)))
18377
18378 or else (Is_Anonymous_Access_Type (Gen_T)
18379 and then Ekind (Def_T) = Ekind (Gen_T)
18380 and then Subtypes_Statically_Match
18381 (Designated_Type (Gen_T), Designated_Type (Def_T)));
18382
18383 end Default_Subtype_Matches;
18384
18385 ----------------------------------
18386 -- Validate_Array_Type_Default --
18387 ----------------------------------
18388
18389 procedure Validate_Array_Type_Default is
18390 I1, I2 : Node_Id;
18391 T2 : Entity_Id;
18392 begin
18393 if not Is_Array_Type (Def_Sub) then
18394 Error_Msg_NE ("default for& must be an array type ",
18395 Default, Formal);
18396 return;
18397
18398 elsif Number_Dimensions (Def_Sub) /= Number_Dimensions (Formal)
18399 or else Is_Constrained (Def_Sub) /=
18400 Is_Constrained (Formal)
18401 then
18402 Error_Msg_NE ("default array type does not match&",
18403 Default, Formal);
18404 return;
18405 end if;
18406
18407 I1 := First_Index (Formal);
18408 I2 := First_Index (Def_Sub);
18409 for J in 1 .. Number_Dimensions (Formal) loop
18410
18411 -- If the indexes of the actual were given by a subtype_mark,
18412 -- the index was transformed into a range attribute. Retrieve
18413 -- the original type mark for checking.
18414
18415 if Is_Entity_Name (Original_Node (I2)) then
18416 T2 := Entity (Original_Node (I2));
18417 else
18418 T2 := Etype (I2);
18419 end if;
18420
18421 if not Subtypes_Statically_Match (Etype (I1), T2) then
18422 Error_Msg_NE
18423 ("index types of default do not match those of formal &",
18424 Default, Formal);
18425 end if;
18426
18427 Next_Index (I1);
18428 Next_Index (I2);
18429 end loop;
18430
18431 if not Default_Subtype_Matches
18432 (Component_Type (Formal), Component_Type (Def_Sub))
18433 then
18434 Error_Msg_NE
18435 ("component subtype of default does not match that of formal &",
18436 Default, Formal);
18437 end if;
18438
18439 if Has_Aliased_Components (Formal)
18440 and then not Has_Aliased_Components (Default)
18441 then
18442 Error_Msg_NE
18443 ("default must have aliased components to match formal type &",
18444 Default, Formal);
18445 end if;
18446 end Validate_Array_Type_Default;
18447
18448 -----------------------------------
18449 -- Validate_Derived_Type_Default --
18450 -----------------------------------
18451
18452 procedure Validate_Derived_Type_Default is
18453 begin
18454 if not Is_Ancestor (Etype (Base_Type (Formal)), Def_Sub) then
18455 Error_Msg_NE ("default must be a descendent of&",
18456 Default, Etype (Formal));
18457 end if;
18458
18459 if Has_Interfaces (Formal) then
18460 if not Has_Interfaces (Def_Sub) then
18461 Error_Msg_NE
18462 ("default must implement all interfaces of formal&",
18463 Default, Formal);
18464
18465 else
18466 declare
18467 Iface : Node_Id;
18468 Iface_Ent : Entity_Id;
18469
18470 begin
18471 Iface := First (Abstract_Interface_List (Formal));
18472
18473 while Present (Iface) loop
18474 Iface_Ent := Entity (Iface);
18475
18476 if Is_Ancestor (Iface_Ent, Def_Sub)
18477 or else Is_Progenitor (Iface_Ent, Def_Sub)
18478 then
18479 null;
18480
18481 else
18482 Error_Msg_NE
18483 ("Default must implement interface&",
18484 Default, Etype (Iface));
18485 end if;
18486
18487 Next (Iface);
18488 end loop;
18489 end;
18490 end if;
18491 end if;
18492 end Validate_Derived_Type_Default;
18493
18494 -- Start of processing for Validate_Formal_Type_Default
18495
18496 begin
18497 Analyze (Default);
18498 if not Is_Entity_Name (Default)
18499 or else not Is_Type (Entity (Default))
18500 then
18501 Error_Msg_N
18502 ("Expect type name for default of formal type", Default);
18503 return;
18504 else
18505 Def_Sub := Entity (Default);
18506 end if;
18507
18508 -- Formal derived_type declarations are transformed into full
18509 -- type declarations or Private_Type_Extensions for ease of processing.
18510
18511 if Nkind (Decl) = N_Full_Type_Declaration then
18512 Type_Def := Type_Definition (Decl);
18513
18514 elsif Nkind (Decl) = N_Private_Extension_Declaration then
18515 Type_Def := Subtype_Indication (Decl);
18516
18517 else
18518 Type_Def := Formal_Type_Definition (Decl);
18519 end if;
18520
18521 if Depends_On_Other_Formals (Type_Def) = Abandon
18522 and then Scope (Def_Sub) /= Current_Scope
18523 then
18524 Error_Msg_N ("default of formal type that depends on "
18525 & "other formals must be a previous formal type", Default);
18526 return;
18527
18528 elsif Def_Sub = Formal then
18529 Error_Msg_N
18530 ("default for formal type cannot be formal itsef", Default);
18531 return;
18532 end if;
18533
18534 case Nkind (Type_Def) is
18535
18536 when N_Formal_Private_Type_Definition =>
18537 if (Is_Abstract_Type (Formal)
18538 and then not Is_Abstract_Type (Def_Sub))
18539 or else (Is_Limited_Type (Formal)
18540 and then not Is_Limited_Type (Def_Sub))
18541 then
18542 Error_Msg_NE
18543 ("default for private type$ does not match",
18544 Default, Formal);
18545 end if;
18546
18547 Check_Discriminated_Formal;
18548
18549 when N_Formal_Derived_Type_Definition =>
18550 Check_Discriminated_Formal;
18551 Validate_Derived_Type_Default;
18552
18553 when N_Formal_Incomplete_Type_Definition =>
18554 if Is_Tagged_Type (Formal)
18555 and then not Is_Tagged_Type (Def_Sub)
18556 then
18557 Error_Msg_NE
18558 ("default for & must be a tagged type", Default, Formal);
18559 end if;
18560
18561 Check_Discriminated_Formal;
18562
18563 when N_Formal_Discrete_Type_Definition =>
18564 if not Is_Discrete_Type (Def_Sub) then
18565 Error_Msg_NE ("default for& must be a discrete type",
18566 Default, Formal);
18567 end if;
18568
18569 when N_Formal_Signed_Integer_Type_Definition =>
18570 if not Is_Integer_Type (Def_Sub) then
18571 Error_Msg_NE ("default for& must be a discrete type",
18572 Default, Formal);
18573 end if;
18574
18575 when N_Formal_Modular_Type_Definition =>
18576 if not Is_Modular_Integer_Type (Def_Sub) then
18577 Error_Msg_NE ("default for& must be a modular_integer Type",
18578 Default, Formal);
18579 end if;
18580
18581 when N_Formal_Floating_Point_Definition =>
18582 if not Is_Floating_Point_Type (Def_Sub) then
18583 Error_Msg_NE ("default for& must be a floating_point type",
18584 Default, Formal);
18585 end if;
18586
18587 when N_Formal_Ordinary_Fixed_Point_Definition =>
18588 if not Is_Ordinary_Fixed_Point_Type (Def_Sub) then
18589 Error_Msg_NE ("default for& must be "
18590 & "an ordinary_fixed_point type ",
18591 Default, Formal);
18592 end if;
18593
18594 when N_Formal_Decimal_Fixed_Point_Definition =>
18595 if not Is_Decimal_Fixed_Point_Type (Def_Sub) then
18596 Error_Msg_NE ("default for& must be "
18597 & "an Decimal_fixed_point type ",
18598 Default, Formal);
18599 end if;
18600
18601 when N_Array_Type_Definition =>
18602 Validate_Array_Type_Default;
18603
18604 when N_Access_Function_Definition |
18605 N_Access_Procedure_Definition =>
18606 if Ekind (Def_Sub) /= E_Access_Subprogram_Type then
18607 Error_Msg_NE ("default for& must be an Access_To_Subprogram",
18608 Default, Formal);
18609 end if;
18610 Check_Subtype_Conformant
18611 (Designated_Type (Formal), Designated_Type (Def_Sub));
18612
18613 when N_Access_To_Object_Definition =>
18614 if not Is_Access_Object_Type (Def_Sub) then
18615 Error_Msg_NE ("default for& must be an Access_To_Object",
18616 Default, Formal);
18617
18618 elsif not Default_Subtype_Matches
18619 (Designated_Type (Formal), Designated_Type (Def_Sub))
18620 then
18621 Error_Msg_NE ("designated type of defaul does not match "
18622 & "designated type of formal type",
18623 Default, Formal);
18624 end if;
18625
18626 when N_Record_Definition => -- Formal interface type
18627 if Is_Tagged_Type (Def_Sub) then
18628 if not Is_Interface (Def_Sub) then
18629 Error_Msg_NE
18630 ("default for formal interface type must be an interface",
18631 Default, Formal);
18632
18633 elsif Is_Limited_Type (Def_Sub) /= Is_Limited_Type (Formal)
18634 or else Is_Task_Interface (Formal) /=
18635 Is_Task_Interface (Def_Sub)
18636 or else Is_Protected_Interface (Formal) /=
18637 Is_Protected_Interface (Def_Sub)
18638 or else Is_Synchronized_Interface (Formal) /=
18639 Is_Synchronized_Interface (Def_Sub)
18640 then
18641 Error_Msg_NE
18642 ("default for interface& does not match", Def_Sub, Formal);
18643 end if;
18644 end if;
18645
18646 when N_Derived_Type_Definition =>
18647 Validate_Derived_Type_Default;
18648
18649 when N_Identifier => -- case of a private extension
18650 Validate_Derived_Type_Default;
18651
18652 when N_Error =>
18653 null;
18654
18655 when others =>
18656 raise Program_Error;
18657 end case;
18658 end Validate_Formal_Type_Default;
18659
18660 package body Instance_Context is
18661
18662 --------------------
18663 -- Save_And_Reset --
18664 --------------------
18665
18666 function Save_And_Reset return Context is
18667 begin
18668 return Result : Context (0 .. Integer (Generic_Renamings.Last)) do
18669 for Index in Result'Range loop
18670 declare
18671 Indexed_Assoc : Assoc renames Generic_Renamings.Table
18672 (Assoc_Ptr (Index));
18673 Result_Pair : Binding_Pair renames Result (Index);
18674 begin
18675 -- If we have called Increment_Last but have not yet
18676 -- initialized the new last element of the table, then
18677 -- that last element might be invalid. Saving and
18678 -- restoring (especially restoring, it turns out) invalid
18679 -- values can result in exceptions if predicate checking
18680 -- is enabled, so replace invalid values with Empty.
18681
18682 if Indexed_Assoc.Gen_Id'Valid then
18683 Result_Pair.Formal_Id := Indexed_Assoc.Gen_Id;
18684 else
18685 pragma Assert (Index = Result'Last);
18686 Result_Pair.Formal_Id := Empty;
18687 end if;
18688
18689 if Indexed_Assoc.Act_Id'Valid then
18690 Result_Pair.Actual_Id := Indexed_Assoc.Act_Id;
18691 else
18692 pragma Assert (Index = Result'Last);
18693 Result_Pair.Actual_Id := Empty;
18694 end if;
18695 end;
18696 end loop;
18697
18698 Generic_Renamings.Init;
18699 Generic_Renamings.Set_Last (-1);
18700 Generic_Renamings_HTable.Reset;
18701 end return;
18702 end Save_And_Reset;
18703
18704 -------------
18705 -- Restore --
18706 -------------
18707
18708 procedure Restore (Saved : Context) is
18709 begin
18710 Generic_Renamings.Init;
18711 Generic_Renamings.Set_Last (0);
18712 Generic_Renamings_HTable.Reset;
18713 Generic_Renamings.Increment_Last;
18714 for Pair of Saved loop
18715 Set_Instance_Of (Pair.Formal_Id, Pair.Actual_Id);
18716 end loop;
18717 Generic_Renamings.Decrement_Last;
18718 end Restore;
18719
18720 end Instance_Context;
18721 end Sem_Ch12;