]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_ch12.adb
[Ada] Minor reformattings
[gcc.git] / gcc / ada / sem_ch12.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2021, 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 Einfo; use Einfo;
30 with Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Fname; use Fname;
36 with Fname.UF; use Fname.UF;
37 with Freeze; use Freeze;
38 with Ghost; use Ghost;
39 with Itypes; use Itypes;
40 with Lib; use Lib;
41 with Lib.Load; use Lib.Load;
42 with Lib.Xref; use Lib.Xref;
43 with Nlists; use Nlists;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Rident; use Rident;
48 with Restrict; use Restrict;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch7; use Sem_Ch7;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Dim; use Sem_Dim;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Elab; use Sem_Elab;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Type; use Sem_Type;
67 with Sem_Util; use Sem_Util;
68 with Sem_Warn; use Sem_Warn;
69 with Stand; use Stand;
70 with Sinfo; use Sinfo;
71 with Sinfo.Nodes; use Sinfo.Nodes;
72 with Sinfo.Utils; use Sinfo.Utils;
73 with Sinfo.CN; use Sinfo.CN;
74 with Sinput; use Sinput;
75 with Sinput.L; use Sinput.L;
76 with Snames; use Snames;
77 with Stringt; use Stringt;
78 with Uname; use Uname;
79 with Table;
80 with Tbuild; use Tbuild;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
83 with Warnsw; use Warnsw;
84
85 with GNAT.HTable;
86
87 package body Sem_Ch12 is
88
89 ----------------------------------------------------------
90 -- Implementation of Generic Analysis and Instantiation --
91 ----------------------------------------------------------
92
93 -- GNAT implements generics by macro expansion. No attempt is made to share
94 -- generic instantiations (for now). Analysis of a generic definition does
95 -- not perform any expansion action, but the expander must be called on the
96 -- tree for each instantiation, because the expansion may of course depend
97 -- on the generic actuals. All of this is best achieved as follows:
98 --
99 -- a) Semantic analysis of a generic unit is performed on a copy of the
100 -- tree for the generic unit. All tree modifications that follow analysis
101 -- do not affect the original tree. Links are kept between the original
102 -- tree and the copy, in order to recognize non-local references within
103 -- the generic, and propagate them to each instance (recall that name
104 -- resolution is done on the generic declaration: generics are not really
105 -- macros). This is summarized in the following diagram:
106
107 -- .-----------. .----------.
108 -- | semantic |<--------------| generic |
109 -- | copy | | unit |
110 -- | |==============>| |
111 -- |___________| global |__________|
112 -- references | | |
113 -- | | |
114 -- .-----|--|.
115 -- | .-----|---.
116 -- | | .----------.
117 -- | | | generic |
118 -- |__| | |
119 -- |__| instance |
120 -- |__________|
121
122 -- b) Each instantiation copies the original tree, and inserts into it a
123 -- series of declarations that describe the mapping between generic formals
124 -- and actuals. For example, a generic In OUT parameter is an object
125 -- renaming of the corresponding actual, etc. Generic IN parameters are
126 -- constant declarations.
127
128 -- c) In order to give the right visibility for these renamings, we use
129 -- a different scheme for package and subprogram instantiations. For
130 -- packages, the list of renamings is inserted into the package
131 -- specification, before the visible declarations of the package. The
132 -- renamings are analyzed before any of the text of the instance, and are
133 -- thus visible at the right place. Furthermore, outside of the instance,
134 -- the generic parameters are visible and denote their corresponding
135 -- actuals.
136
137 -- For subprograms, we create a container package to hold the renamings
138 -- and the subprogram instance itself. Analysis of the package makes the
139 -- renaming declarations visible to the subprogram. After analyzing the
140 -- package, the defining entity for the subprogram is touched-up so that
141 -- it appears declared in the current scope, and not inside the container
142 -- package.
143
144 -- If the instantiation is a compilation unit, the container package is
145 -- given the same name as the subprogram instance. This ensures that
146 -- the elaboration procedure called by the binder, using the compilation
147 -- unit name, calls in fact the elaboration procedure for the package.
148
149 -- Not surprisingly, private types complicate this approach. By saving in
150 -- the original generic object the non-local references, we guarantee that
151 -- the proper entities are referenced at the point of instantiation.
152 -- However, for private types, this by itself does not insure that the
153 -- proper VIEW of the entity is used (the full type may be visible at the
154 -- point of generic definition, but not at instantiation, or vice-versa).
155 -- In order to reference the proper view, we special-case any reference
156 -- to private types in the generic object, by saving both views, one in
157 -- the generic and one in the semantic copy. At time of instantiation, we
158 -- check whether the two views are consistent, and exchange declarations if
159 -- necessary, in order to restore the correct visibility. Similarly, if
160 -- the instance view is private when the generic view was not, we perform
161 -- the exchange. After completing the instantiation, we restore the
162 -- current visibility. The flag Has_Private_View marks identifiers in the
163 -- the generic unit that require checking.
164
165 -- Visibility within nested generic units requires special handling.
166 -- Consider the following scheme:
167
168 -- type Global is ... -- outside of generic unit.
169 -- generic ...
170 -- package Outer is
171 -- ...
172 -- type Semi_Global is ... -- global to inner.
173
174 -- generic ... -- 1
175 -- procedure inner (X1 : Global; X2 : Semi_Global);
176
177 -- procedure in2 is new inner (...); -- 4
178 -- end Outer;
179
180 -- package New_Outer is new Outer (...); -- 2
181 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
182
183 -- The semantic analysis of Outer captures all occurrences of Global.
184 -- The semantic analysis of Inner (at 1) captures both occurrences of
185 -- Global and Semi_Global.
186
187 -- At point 2 (instantiation of Outer), we also produce a generic copy
188 -- of Inner, even though Inner is, at that point, not being instantiated.
189 -- (This is just part of the semantic analysis of New_Outer).
190
191 -- Critically, references to Global within Inner must be preserved, while
192 -- references to Semi_Global should not preserved, because they must now
193 -- resolve to an entity within New_Outer. To distinguish between these, we
194 -- use a global variable, Current_Instantiated_Parent, which is set when
195 -- performing a generic copy during instantiation (at 2). This variable is
196 -- used when performing a generic copy that is not an instantiation, but
197 -- that is nested within one, as the occurrence of 1 within 2. The analysis
198 -- of a nested generic only preserves references that are global to the
199 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
200 -- determine whether a reference is external to the given parent.
201
202 -- The instantiation at point 3 requires no special treatment. The method
203 -- works as well for further nestings of generic units, but of course the
204 -- variable Current_Instantiated_Parent must be stacked because nested
205 -- instantiations can occur, e.g. the occurrence of 4 within 2.
206
207 -- The instantiation of package and subprogram bodies is handled in a
208 -- similar manner, except that it is delayed until after semantic
209 -- analysis is complete. In this fashion complex cross-dependencies
210 -- between several package declarations and bodies containing generics
211 -- can be compiled which otherwise would diagnose spurious circularities.
212
213 -- For example, it is possible to compile two packages A and B that
214 -- have the following structure:
215
216 -- package A is package B is
217 -- generic ... generic ...
218 -- package G_A is package G_B is
219
220 -- with B; with A;
221 -- package body A is package body B is
222 -- package N_B is new G_B (..) package N_A is new G_A (..)
223
224 -- The table Pending_Instantiations in package Inline is used to keep
225 -- track of body instantiations that are delayed in this manner. Inline
226 -- handles the actual calls to do the body instantiations. This activity
227 -- is part of Inline, since the processing occurs at the same point, and
228 -- for essentially the same reason, as the handling of inlined routines.
229
230 ----------------------------------------------
231 -- Detection of Instantiation Circularities --
232 ----------------------------------------------
233
234 -- If we have a chain of instantiations that is circular, this is static
235 -- error which must be detected at compile time. The detection of these
236 -- circularities is carried out at the point that we insert a generic
237 -- instance spec or body. If there is a circularity, then the analysis of
238 -- the offending spec or body will eventually result in trying to load the
239 -- same unit again, and we detect this problem as we analyze the package
240 -- instantiation for the second time.
241
242 -- At least in some cases after we have detected the circularity, we get
243 -- into trouble if we try to keep going. The following flag is set if a
244 -- circularity is detected, and used to abandon compilation after the
245 -- messages have been posted.
246
247 Circularity_Detected : Boolean := False;
248 -- It should really be reset upon encountering a new main unit, but in
249 -- practice we do not use multiple main units so this is not critical.
250
251 -----------------------------------------
252 -- Implementation of Generic Contracts --
253 -----------------------------------------
254
255 -- A "contract" is a collection of aspects and pragmas that either verify a
256 -- property of a construct at runtime or classify the data flow to and from
257 -- the construct in some fashion.
258
259 -- Generic packages, subprograms and their respective bodies may be subject
260 -- to the following contract-related aspects or pragmas collectively known
261 -- as annotations:
262
263 -- package subprogram [body]
264 -- Abstract_State Contract_Cases
265 -- Initial_Condition Depends
266 -- Initializes Extensions_Visible
267 -- Global
268 -- package body Post
269 -- Refined_State Post_Class
270 -- Postcondition
271 -- Pre
272 -- Pre_Class
273 -- Precondition
274 -- Refined_Depends
275 -- Refined_Global
276 -- Refined_Post
277 -- Subprogram_Variant
278 -- Test_Case
279
280 -- Most package contract annotations utilize forward references to classify
281 -- data declared within the package [body]. Subprogram annotations then use
282 -- the classifications to further refine them. These inter dependencies are
283 -- problematic with respect to the implementation of generics because their
284 -- analysis, capture of global references and instantiation does not mesh
285 -- well with the existing mechanism.
286
287 -- 1) Analysis of generic contracts is carried out the same way non-generic
288 -- contracts are analyzed:
289
290 -- 1.1) General rule - a contract is analyzed after all related aspects
291 -- and pragmas are analyzed. This is done by routines
292
293 -- Analyze_Package_Body_Contract
294 -- Analyze_Package_Contract
295 -- Analyze_Subprogram_Body_Contract
296 -- Analyze_Subprogram_Contract
297
298 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
299 -- are processed.
300
301 -- 1.3) Compilation unit body - the contract is analyzed at the end of
302 -- the body declaration list.
303
304 -- 1.4) Package - the contract is analyzed at the end of the private or
305 -- visible declarations, prior to analyzing the contracts of any nested
306 -- packages or subprograms.
307
308 -- 1.5) Package body - the contract is analyzed at the end of the body
309 -- declaration list, prior to analyzing the contracts of any nested
310 -- packages or subprograms.
311
312 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
313 -- package or a subprogram, then its contract is analyzed at the end of
314 -- the enclosing declarations, otherwise the subprogram is a compilation
315 -- unit 1.2).
316
317 -- 1.7) Subprogram body - if the subprogram body is declared inside a
318 -- block, a package body or a subprogram body, then its contract is
319 -- analyzed at the end of the enclosing declarations, otherwise the
320 -- subprogram is a compilation unit 1.3).
321
322 -- 2) Capture of global references within contracts is done after capturing
323 -- global references within the generic template. There are two reasons for
324 -- this delay - pragma annotations are not part of the generic template in
325 -- the case of a generic subprogram declaration, and analysis of contracts
326 -- is delayed.
327
328 -- Contract-related source pragmas within generic templates are prepared
329 -- for delayed capture of global references by routine
330
331 -- Create_Generic_Contract
332
333 -- The routine associates these pragmas with the contract of the template.
334 -- In the case of a generic subprogram declaration, the routine creates
335 -- generic templates for the pragmas declared after the subprogram because
336 -- they are not part of the template.
337
338 -- generic -- template starts
339 -- procedure Gen_Proc (Input : Integer); -- template ends
340 -- pragma Precondition (Input > 0); -- requires own template
341
342 -- 2.1) The capture of global references with aspect specifications and
343 -- source pragmas that apply to a generic unit must be suppressed when
344 -- the generic template is being processed because the contracts have not
345 -- been analyzed yet. Any attempts to capture global references at that
346 -- point will destroy the Associated_Node linkages and leave the template
347 -- undecorated. This delay is controlled by routine
348
349 -- Requires_Delayed_Save
350
351 -- 2.2) The real capture of global references within a contract is done
352 -- after the contract has been analyzed, by routine
353
354 -- Save_Global_References_In_Contract
355
356 -- 3) The instantiation of a generic contract occurs as part of the
357 -- instantiation of the contract owner. Generic subprogram declarations
358 -- require additional processing when the contract is specified by pragmas
359 -- because the pragmas are not part of the generic template. This is done
360 -- by routine
361
362 -- Instantiate_Subprogram_Contract
363
364 --------------------------------------------------
365 -- Formal packages and partial parameterization --
366 --------------------------------------------------
367
368 -- When compiling a generic, a formal package is a local instantiation. If
369 -- declared with a box, its generic formals are visible in the enclosing
370 -- generic. If declared with a partial list of actuals, those actuals that
371 -- are defaulted (covered by an Others clause, or given an explicit box
372 -- initialization) are also visible in the enclosing generic, while those
373 -- that have a corresponding actual are not.
374
375 -- In our source model of instantiation, the same visibility must be
376 -- present in the spec and body of an instance: the names of the formals
377 -- that are defaulted must be made visible within the instance, and made
378 -- invisible (hidden) after the instantiation is complete, so that they
379 -- are not accessible outside of the instance.
380
381 -- In a generic, a formal package is treated like a special instantiation.
382 -- Our Ada 95 compiler handled formals with and without box in different
383 -- ways. With partial parameterization, we use a single model for both.
384 -- We create a package declaration that consists of the specification of
385 -- the generic package, and a set of declarations that map the actuals
386 -- into local renamings, just as we do for bona fide instantiations. For
387 -- defaulted parameters and formals with a box, we copy directly the
388 -- declarations of the formals into this local package. The result is a
389 -- package whose visible declarations may include generic formals. This
390 -- package is only used for type checking and visibility analysis, and
391 -- never reaches the back end, so it can freely violate the placement
392 -- rules for generic formal declarations.
393
394 -- The list of declarations (renamings and copies of formals) is built
395 -- by Analyze_Associations, just as for regular instantiations.
396
397 -- At the point of instantiation, conformance checking must be applied only
398 -- to those parameters that were specified in the formals. We perform this
399 -- checking by creating another internal instantiation, this one including
400 -- only the renamings and the formals (the rest of the package spec is not
401 -- relevant to conformance checking). We can then traverse two lists: the
402 -- list of actuals in the instance that corresponds to the formal package,
403 -- and the list of actuals produced for this bogus instantiation. We apply
404 -- the conformance rules to those actuals that are not defaulted, i.e.
405 -- which still appear as generic formals.
406
407 -- When we compile an instance body we must make the right parameters
408 -- visible again. The predicate Is_Generic_Formal indicates which of the
409 -- formals should have its Is_Hidden flag reset.
410
411 -----------------------
412 -- Local subprograms --
413 -----------------------
414
415 procedure Abandon_Instantiation (N : Node_Id);
416 pragma No_Return (Abandon_Instantiation);
417 -- Posts an error message "instantiation abandoned" at the indicated node
418 -- and then raises the exception Instantiation_Error to do it.
419
420 procedure Analyze_Formal_Array_Type
421 (T : in out Entity_Id;
422 Def : Node_Id);
423 -- A formal array type is treated like an array type declaration, and
424 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
425 -- in-out, because in the case of an anonymous type the entity is
426 -- actually created in the procedure.
427
428 -- The following procedures treat other kinds of formal parameters
429
430 procedure Analyze_Formal_Derived_Interface_Type
431 (N : Node_Id;
432 T : Entity_Id;
433 Def : Node_Id);
434
435 procedure Analyze_Formal_Derived_Type
436 (N : Node_Id;
437 T : Entity_Id;
438 Def : Node_Id);
439
440 procedure Analyze_Formal_Interface_Type
441 (N : Node_Id;
442 T : Entity_Id;
443 Def : Node_Id);
444
445 -- The following subprograms create abbreviated declarations for formal
446 -- scalar types. We introduce an anonymous base of the proper class for
447 -- each of them, and define the formals as constrained first subtypes of
448 -- their bases. The bounds are expressions that are non-static in the
449 -- generic.
450
451 procedure Analyze_Formal_Decimal_Fixed_Point_Type
452 (T : Entity_Id; Def : Node_Id);
453 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
454 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
455 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
456 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
457 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
458 (T : Entity_Id; Def : Node_Id);
459
460 procedure Analyze_Formal_Private_Type
461 (N : Node_Id;
462 T : Entity_Id;
463 Def : Node_Id);
464 -- Creates a new private type, which does not require completion
465
466 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
467 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
468
469 procedure Analyze_Generic_Formal_Part (N : Node_Id);
470 -- Analyze generic formal part
471
472 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
473 -- Create a new access type with the given designated type
474
475 function Analyze_Associations
476 (I_Node : Node_Id;
477 Formals : List_Id;
478 F_Copy : List_Id) return List_Id;
479 -- At instantiation time, build the list of associations between formals
480 -- and actuals. Each association becomes a renaming declaration for the
481 -- formal entity. F_Copy is the analyzed list of formals in the generic
482 -- copy. It is used to apply legality checks to the actuals. I_Node is the
483 -- instantiation node itself.
484
485 procedure Analyze_Subprogram_Instantiation
486 (N : Node_Id;
487 K : Entity_Kind);
488
489 procedure Build_Instance_Compilation_Unit_Nodes
490 (N : Node_Id;
491 Act_Body : Node_Id;
492 Act_Decl : Node_Id);
493 -- This procedure is used in the case where the generic instance of a
494 -- subprogram body or package body is a library unit. In this case, the
495 -- original library unit node for the generic instantiation must be
496 -- replaced by the resulting generic body, and a link made to a new
497 -- compilation unit node for the generic declaration. The argument N is
498 -- the original generic instantiation. Act_Body and Act_Decl are the body
499 -- and declaration of the instance (either package body and declaration
500 -- nodes or subprogram body and declaration nodes depending on the case).
501 -- On return, the node N has been rewritten with the actual body.
502
503 function Build_Subprogram_Decl_Wrapper
504 (Formal_Subp : Entity_Id) return Node_Id;
505 -- Ada 2020 allows formal subprograms to carry pre/postconditions.
506 -- At the point of instantiation these contracts apply to uses of
507 -- the actual subprogram. This is implemented by creating wrapper
508 -- subprograms instead of the renamings previously used to link
509 -- formal subprograms and the corresponding actuals. If the actual
510 -- is not an entity (e.g. an attribute reference) a renaming is
511 -- created to handle the expansion of the attribute.
512
513 function Build_Subprogram_Body_Wrapper
514 (Formal_Subp : Entity_Id;
515 Actual_Name : Node_Id) return Node_Id;
516 -- The body of the wrapper is a call to the actual, with the generated
517 -- pre/postconditon checks added.
518
519 procedure Check_Access_Definition (N : Node_Id);
520 -- Subsidiary routine to null exclusion processing. Perform an assertion
521 -- check on Ada version and the presence of an access definition in N.
522
523 procedure Check_Formal_Packages (P_Id : Entity_Id);
524 -- Apply the following to all formal packages in generic associations.
525 -- Restore the visibility of the formals of the instance that are not
526 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
527 -- created for formal instances that are not defaulted.
528
529 procedure Check_Formal_Package_Instance
530 (Formal_Pack : Entity_Id;
531 Actual_Pack : Entity_Id);
532 -- Verify that the actuals of the actual instance match the actuals of
533 -- the template for a formal package that is not declared with a box.
534
535 procedure Check_Forward_Instantiation (Decl : Node_Id);
536 -- If the generic is a local entity and the corresponding body has not
537 -- been seen yet, flag enclosing packages to indicate that it will be
538 -- elaborated after the generic body. Subprograms declared in the same
539 -- package cannot be inlined by the front end because front-end inlining
540 -- requires a strict linear order of elaboration.
541
542 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
543 -- Check if some association between formals and actuals requires to make
544 -- visible primitives of a tagged type, and make those primitives visible.
545 -- Return the list of primitives whose visibility is modified (to restore
546 -- their visibility later through Restore_Hidden_Primitives). If no
547 -- candidate is found then return No_Elist.
548
549 procedure Check_Hidden_Child_Unit
550 (N : Node_Id;
551 Gen_Unit : Entity_Id;
552 Act_Decl_Id : Entity_Id);
553 -- If the generic unit is an implicit child instance within a parent
554 -- instance, we need to make an explicit test that it is not hidden by
555 -- a child instance of the same name and parent.
556
557 procedure Check_Generic_Actuals
558 (Instance : Entity_Id;
559 Is_Formal_Box : Boolean);
560 -- Similar to previous one. Check the actuals in the instantiation,
561 -- whose views can change between the point of instantiation and the point
562 -- of instantiation of the body. In addition, mark the generic renamings
563 -- as generic actuals, so that they are not compatible with other actuals.
564 -- Recurse on an actual that is a formal package whose declaration has
565 -- a box.
566
567 function Contains_Instance_Of
568 (Inner : Entity_Id;
569 Outer : Entity_Id;
570 N : Node_Id) return Boolean;
571 -- Inner is instantiated within the generic Outer. Check whether Inner
572 -- directly or indirectly contains an instance of Outer or of one of its
573 -- parents, in the case of a subunit. Each generic unit holds a list of
574 -- the entities instantiated within (at any depth). This procedure
575 -- determines whether the set of such lists contains a cycle, i.e. an
576 -- illegal circular instantiation.
577
578 function Denotes_Formal_Package
579 (Pack : Entity_Id;
580 On_Exit : Boolean := False;
581 Instance : Entity_Id := Empty) return Boolean;
582 -- Returns True if E is a formal package of an enclosing generic, or
583 -- the actual for such a formal in an enclosing instantiation. If such
584 -- a package is used as a formal in an nested generic, or as an actual
585 -- in a nested instantiation, the visibility of ITS formals should not
586 -- be modified. When called from within Restore_Private_Views, the flag
587 -- On_Exit is true, to indicate that the search for a possible enclosing
588 -- instance should ignore the current one. In that case Instance denotes
589 -- the declaration for which this is an actual. This declaration may be
590 -- an instantiation in the source, or the internal instantiation that
591 -- corresponds to the actual for a formal package.
592
593 function Earlier (N1, N2 : Node_Id) return Boolean;
594 -- Yields True if N1 and N2 appear in the same compilation unit,
595 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
596 -- traversal of the tree for the unit. Used to determine the placement
597 -- of freeze nodes for instance bodies that may depend on other instances.
598
599 function Find_Actual_Type
600 (Typ : Entity_Id;
601 Gen_Type : Entity_Id) return Entity_Id;
602 -- When validating the actual types of a child instance, check whether
603 -- the formal is a formal type of the parent unit, and retrieve the current
604 -- actual for it. Typ is the entity in the analyzed formal type declaration
605 -- (component or index type of an array type, or designated type of an
606 -- access formal) and Gen_Type is the enclosing analyzed formal array
607 -- or access type. The desired actual may be a formal of a parent, or may
608 -- be declared in a formal package of a parent. In both cases it is a
609 -- generic actual type because it appears within a visible instance.
610 -- Finally, it may be declared in a parent unit without being a formal
611 -- of that unit, in which case it must be retrieved by visibility.
612 -- Ambiguities may still arise if two homonyms are declared in two formal
613 -- packages, and the prefix of the formal type may be needed to resolve
614 -- the ambiguity in the instance ???
615
616 procedure Freeze_Subprogram_Body
617 (Inst_Node : Node_Id;
618 Gen_Body : Node_Id;
619 Pack_Id : Entity_Id);
620 -- The generic body may appear textually after the instance, including
621 -- in the proper body of a stub, or within a different package instance.
622 -- Given that the instance can only be elaborated after the generic, we
623 -- place freeze_nodes for the instance and/or for packages that may enclose
624 -- the instance and the generic, so that the back-end can establish the
625 -- proper order of elaboration.
626
627 function Get_Associated_Node (N : Node_Id) return Node_Id;
628 -- In order to propagate semantic information back from the analyzed copy
629 -- to the original generic, we maintain links between selected nodes in the
630 -- generic and their corresponding copies. At the end of generic analysis,
631 -- the routine Save_Global_References traverses the generic tree, examines
632 -- the semantic information, and preserves the links to those nodes that
633 -- contain global information. At instantiation, the information from the
634 -- associated node is placed on the new copy, so that name resolution is
635 -- not repeated.
636 --
637 -- Three kinds of source nodes have associated nodes:
638 --
639 -- a) those that can reference (denote) entities, that is identifiers,
640 -- character literals, expanded_names, operator symbols, operators,
641 -- and attribute reference nodes. These nodes have an Entity field
642 -- and are the set of nodes that are in N_Has_Entity.
643 --
644 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
645 --
646 -- c) selected components (N_Selected_Component)
647 --
648 -- For the first class, the associated node preserves the entity if it is
649 -- global. If the generic contains nested instantiations, the associated
650 -- node itself has been recopied, and a chain of them must be followed.
651 --
652 -- For aggregates, the associated node allows retrieval of the type, which
653 -- may otherwise not appear in the generic. The view of this type may be
654 -- different between generic and instantiation, and the full view can be
655 -- installed before the instantiation is analyzed. For aggregates of type
656 -- extensions, the same view exchange may have to be performed for some of
657 -- the ancestor types, if their view is private at the point of
658 -- instantiation.
659 --
660 -- Nodes that are selected components in the parse tree may be rewritten
661 -- as expanded names after resolution, and must be treated as potential
662 -- entity holders, which is why they also have an Associated_Node.
663 --
664 -- Nodes that do not come from source, such as freeze nodes, do not appear
665 -- in the generic tree, and need not have an associated node.
666 --
667 -- The associated node is stored in the Associated_Node field. Note that
668 -- this field overlaps Entity, which is fine, because the whole point is
669 -- that we don't need or want the normal Entity field in this situation.
670
671 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
672 -- Traverse the Exchanged_Views list to see if a type was private
673 -- and has already been flipped during this phase of instantiation.
674
675 function Has_Contracts (Decl : Node_Id) return Boolean;
676 -- Determine whether a formal subprogram has a Pre- or Postcondition,
677 -- in which case a subprogram wrapper has to be built for the actual.
678
679 procedure Hide_Current_Scope;
680 -- When instantiating a generic child unit, the parent context must be
681 -- present, but the instance and all entities that may be generated
682 -- must be inserted in the current scope. We leave the current scope
683 -- on the stack, but make its entities invisible to avoid visibility
684 -- problems. This is reversed at the end of the instantiation. This is
685 -- not done for the instantiation of the bodies, which only require the
686 -- instances of the generic parents to be in scope.
687
688 function In_Main_Context (E : Entity_Id) return Boolean;
689 -- Check whether an instantiation is in the context of the main unit.
690 -- Used to determine whether its body should be elaborated to allow
691 -- front-end inlining.
692
693 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
694 -- Add the context clause of the unit containing a generic unit to a
695 -- compilation unit that is, or contains, an instantiation.
696
697 procedure Init_Env;
698 -- Establish environment for subsequent instantiation. Separated from
699 -- Save_Env because data-structures for visibility handling must be
700 -- initialized before call to Check_Generic_Child_Unit.
701
702 procedure Inline_Instance_Body
703 (N : Node_Id;
704 Gen_Unit : Entity_Id;
705 Act_Decl : Node_Id);
706 -- If front-end inlining is requested, instantiate the package body,
707 -- and preserve the visibility of its compilation unit, to insure
708 -- that successive instantiations succeed.
709
710 procedure Insert_Freeze_Node_For_Instance
711 (N : Node_Id;
712 F_Node : Node_Id);
713 -- N denotes a package or a subprogram instantiation and F_Node is the
714 -- associated freeze node. Insert the freeze node before the first source
715 -- body which follows immediately after N. If no such body is found, the
716 -- freeze node is inserted at the end of the declarative region which
717 -- contains N.
718
719 procedure Install_Body
720 (Act_Body : Node_Id;
721 N : Node_Id;
722 Gen_Body : Node_Id;
723 Gen_Decl : Node_Id);
724 -- If the instantiation happens textually before the body of the generic,
725 -- the instantiation of the body must be analyzed after the generic body,
726 -- and not at the point of instantiation. Such early instantiations can
727 -- happen if the generic and the instance appear in a package declaration
728 -- because the generic body can only appear in the corresponding package
729 -- body. Early instantiations can also appear if generic, instance and
730 -- body are all in the declarative part of a subprogram or entry. Entities
731 -- of packages that are early instantiations are delayed, and their freeze
732 -- node appears after the generic body. This rather complex machinery is
733 -- needed when nested instantiations are present, because the source does
734 -- not carry any indication of where the corresponding instance bodies must
735 -- be installed and frozen.
736
737 procedure Install_Formal_Packages (Par : Entity_Id);
738 -- Install the visible part of any formal of the parent that is a formal
739 -- package. Note that for the case of a formal package with a box, this
740 -- includes the formal part of the formal package (12.7(10/2)).
741
742 procedure Install_Hidden_Primitives
743 (Prims_List : in out Elist_Id;
744 Gen_T : Entity_Id;
745 Act_T : Entity_Id);
746 -- Remove suffix 'P' from hidden primitives of Act_T to match the
747 -- visibility of primitives of Gen_T. The list of primitives to which
748 -- the suffix is removed is added to Prims_List to restore them later.
749
750 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
751 -- When compiling an instance of a child unit the parent (which is
752 -- itself an instance) is an enclosing scope that must be made
753 -- immediately visible. This procedure is also used to install the non-
754 -- generic parent of a generic child unit when compiling its body, so
755 -- that full views of types in the parent are made visible.
756
757 -- The functions Instantiate_XXX perform various legality checks and build
758 -- the declarations for instantiated generic parameters. In all of these
759 -- Formal is the entity in the generic unit, Actual is the entity of
760 -- expression in the generic associations, and Analyzed_Formal is the
761 -- formal in the generic copy, which contains the semantic information to
762 -- be used to validate the actual.
763
764 function Instantiate_Object
765 (Formal : Node_Id;
766 Actual : Node_Id;
767 Analyzed_Formal : Node_Id) return List_Id;
768
769 function Instantiate_Type
770 (Formal : Node_Id;
771 Actual : Node_Id;
772 Analyzed_Formal : Node_Id;
773 Actual_Decls : List_Id) return List_Id;
774
775 function Instantiate_Formal_Subprogram
776 (Formal : Node_Id;
777 Actual : Node_Id;
778 Analyzed_Formal : Node_Id) return Node_Id;
779
780 function Instantiate_Formal_Package
781 (Formal : Node_Id;
782 Actual : Node_Id;
783 Analyzed_Formal : Node_Id) return List_Id;
784 -- If the formal package is declared with a box, special visibility rules
785 -- apply to its formals: they are in the visible part of the package. This
786 -- is true in the declarative region of the formal package, that is to say
787 -- in the enclosing generic or instantiation. For an instantiation, the
788 -- parameters of the formal package are made visible in an explicit step.
789 -- Furthermore, if the actual has a visible USE clause, these formals must
790 -- be made potentially use-visible as well. On exit from the enclosing
791 -- instantiation, the reverse must be done.
792
793 -- For a formal package declared without a box, there are conformance rules
794 -- that apply to the actuals in the generic declaration and the actuals of
795 -- the actual package in the enclosing instantiation. The simplest way to
796 -- apply these rules is to repeat the instantiation of the formal package
797 -- in the context of the enclosing instance, and compare the generic
798 -- associations of this instantiation with those of the actual package.
799 -- This internal instantiation only needs to contain the renamings of the
800 -- formals: the visible and private declarations themselves need not be
801 -- created.
802
803 -- In Ada 2005, the formal package may be only partially parameterized.
804 -- In that case the visibility step must make visible those actuals whose
805 -- corresponding formals were given with a box. A final complication
806 -- involves inherited operations from formal derived types, which must
807 -- be visible if the type is.
808
809 function Is_In_Main_Unit (N : Node_Id) return Boolean;
810 -- Test if given node is in the main unit
811
812 procedure Load_Parent_Of_Generic
813 (N : Node_Id;
814 Spec : Node_Id;
815 Body_Optional : Boolean := False);
816 -- If the generic appears in a separate non-generic library unit, load the
817 -- corresponding body to retrieve the body of the generic. N is the node
818 -- for the generic instantiation, Spec is the generic package declaration.
819 --
820 -- Body_Optional is a flag that indicates that the body is being loaded to
821 -- ensure that temporaries are generated consistently when there are other
822 -- instances in the current declarative part that precede the one being
823 -- loaded. In that case a missing body is acceptable.
824
825 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
826 -- Within the generic part, entities in the formal package are
827 -- visible. To validate subsequent type declarations, indicate
828 -- the correspondence between the entities in the analyzed formal,
829 -- and the entities in the actual package. There are three packages
830 -- involved in the instantiation of a formal package: the parent
831 -- generic P1 which appears in the generic declaration, the fake
832 -- instantiation P2 which appears in the analyzed generic, and whose
833 -- visible entities may be used in subsequent formals, and the actual
834 -- P3 in the instance. To validate subsequent formals, me indicate
835 -- that the entities in P2 are mapped into those of P3. The mapping of
836 -- entities has to be done recursively for nested packages.
837
838 procedure Move_Freeze_Nodes
839 (Out_Of : Entity_Id;
840 After : Node_Id;
841 L : List_Id);
842 -- Freeze nodes can be generated in the analysis of a generic unit, but
843 -- will not be seen by the back-end. It is necessary to move those nodes
844 -- to the enclosing scope if they freeze an outer entity. We place them
845 -- at the end of the enclosing generic package, which is semantically
846 -- neutral.
847
848 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
849 -- Analyze actuals to perform name resolution. Full resolution is done
850 -- later, when the expected types are known, but names have to be captured
851 -- before installing parents of generics, that are not visible for the
852 -- actuals themselves.
853 --
854 -- If Inst is present, it is the entity of the package instance. This
855 -- entity is marked as having a limited_view actual when some actual is
856 -- a limited view. This is used to place the instance body properly.
857
858 procedure Provide_Completing_Bodies (N : Node_Id);
859 -- Generate completing bodies for all subprograms found within package or
860 -- subprogram declaration N.
861
862 procedure Remove_Parent (In_Body : Boolean := False);
863 -- Reverse effect after instantiation of child is complete
864
865 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
866 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
867 -- set to No_Elist.
868
869 procedure Set_Instance_Env
870 (Gen_Unit : Entity_Id;
871 Act_Unit : Entity_Id);
872 -- Save current instance on saved environment, to be used to determine
873 -- the global status of entities in nested instances. Part of Save_Env.
874 -- called after verifying that the generic unit is legal for the instance,
875 -- The procedure also examines whether the generic unit is a predefined
876 -- unit, in order to set configuration switches accordingly. As a result
877 -- the procedure must be called after analyzing and freezing the actuals.
878
879 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
880 -- Associate analyzed generic parameter with corresponding instance. Used
881 -- for semantic checks at instantiation time.
882
883 function True_Parent (N : Node_Id) return Node_Id;
884 -- For a subunit, return parent of corresponding stub, else return
885 -- parent of node.
886
887 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
888 -- Verify that an attribute that appears as the default for a formal
889 -- subprogram is a function or procedure with the correct profile.
890
891 -------------------------------------------
892 -- Data Structures for Generic Renamings --
893 -------------------------------------------
894
895 -- The map Generic_Renamings associates generic entities with their
896 -- corresponding actuals. Currently used to validate type instances. It
897 -- will eventually be used for all generic parameters to eliminate the
898 -- need for overload resolution in the instance.
899
900 type Assoc_Ptr is new Int;
901
902 Assoc_Null : constant Assoc_Ptr := -1;
903
904 type Assoc is record
905 Gen_Id : Entity_Id;
906 Act_Id : Entity_Id;
907 Next_In_HTable : Assoc_Ptr;
908 end record;
909
910 package Generic_Renamings is new Table.Table
911 (Table_Component_Type => Assoc,
912 Table_Index_Type => Assoc_Ptr,
913 Table_Low_Bound => 0,
914 Table_Initial => 10,
915 Table_Increment => 100,
916 Table_Name => "Generic_Renamings");
917
918 -- Variable to hold enclosing instantiation. When the environment is
919 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
920
921 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
922
923 -- Hash table for associations
924
925 HTable_Size : constant := 37;
926 type HTable_Range is range 0 .. HTable_Size - 1;
927
928 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
929 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
930 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
931 function Hash (F : Entity_Id) return HTable_Range;
932
933 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
934 Header_Num => HTable_Range,
935 Element => Assoc,
936 Elmt_Ptr => Assoc_Ptr,
937 Null_Ptr => Assoc_Null,
938 Set_Next => Set_Next_Assoc,
939 Next => Next_Assoc,
940 Key => Entity_Id,
941 Get_Key => Get_Gen_Id,
942 Hash => Hash,
943 Equal => "=");
944
945 Exchanged_Views : Elist_Id;
946 -- This list holds the private views that have been exchanged during
947 -- instantiation to restore the visibility of the generic declaration.
948 -- (see comments above). After instantiation, the current visibility is
949 -- reestablished by means of a traversal of this list.
950
951 Hidden_Entities : Elist_Id;
952 -- This list holds the entities of the current scope that are removed
953 -- from immediate visibility when instantiating a child unit. Their
954 -- visibility is restored in Remove_Parent.
955
956 -- Because instantiations can be recursive, the following must be saved
957 -- on entry and restored on exit from an instantiation (spec or body).
958 -- This is done by the two procedures Save_Env and Restore_Env. For
959 -- package and subprogram instantiations (but not for the body instances)
960 -- the action of Save_Env is done in two steps: Init_Env is called before
961 -- Check_Generic_Child_Unit, because setting the parent instances requires
962 -- that the visibility data structures be properly initialized. Once the
963 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
964
965 Parent_Unit_Visible : Boolean := False;
966 -- Parent_Unit_Visible is used when the generic is a child unit, and
967 -- indicates whether the ultimate parent of the generic is visible in the
968 -- instantiation environment. It is used to reset the visibility of the
969 -- parent at the end of the instantiation (see Remove_Parent).
970
971 Instance_Parent_Unit : Entity_Id := Empty;
972 -- This records the ultimate parent unit of an instance of a generic
973 -- child unit and is used in conjunction with Parent_Unit_Visible to
974 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
975
976 type Instance_Env is record
977 Instantiated_Parent : Assoc;
978 Exchanged_Views : Elist_Id;
979 Hidden_Entities : Elist_Id;
980 Current_Sem_Unit : Unit_Number_Type;
981 Parent_Unit_Visible : Boolean := False;
982 Instance_Parent_Unit : Entity_Id := Empty;
983 Switches : Config_Switches_Type;
984 end record;
985
986 package Instance_Envs is new Table.Table (
987 Table_Component_Type => Instance_Env,
988 Table_Index_Type => Int,
989 Table_Low_Bound => 0,
990 Table_Initial => 32,
991 Table_Increment => 100,
992 Table_Name => "Instance_Envs");
993
994 procedure Restore_Private_Views
995 (Pack_Id : Entity_Id;
996 Is_Package : Boolean := True);
997 -- Restore the private views of external types, and unmark the generic
998 -- renamings of actuals, so that they become compatible subtypes again.
999 -- For subprograms, Pack_Id is the package constructed to hold the
1000 -- renamings.
1001
1002 procedure Switch_View (T : Entity_Id);
1003 -- Switch the partial and full views of a type and its private
1004 -- dependents (i.e. its subtypes and derived types).
1005
1006 ------------------------------------
1007 -- Structures for Error Reporting --
1008 ------------------------------------
1009
1010 Instantiation_Node : Node_Id;
1011 -- Used by subprograms that validate instantiation of formal parameters
1012 -- where there might be no actual on which to place the error message.
1013 -- Also used to locate the instantiation node for generic subunits.
1014
1015 Instantiation_Error : exception;
1016 -- When there is a semantic error in the generic parameter matching,
1017 -- there is no point in continuing the instantiation, because the
1018 -- number of cascaded errors is unpredictable. This exception aborts
1019 -- the instantiation process altogether.
1020
1021 S_Adjustment : Sloc_Adjustment;
1022 -- Offset created for each node in an instantiation, in order to keep
1023 -- track of the source position of the instantiation in each of its nodes.
1024 -- A subsequent semantic error or warning on a construct of the instance
1025 -- points to both places: the original generic node, and the point of
1026 -- instantiation. See Sinput and Sinput.L for additional details.
1027
1028 ------------------------------------------------------------
1029 -- Data structure for keeping track when inside a Generic --
1030 ------------------------------------------------------------
1031
1032 -- The following table is used to save values of the Inside_A_Generic
1033 -- flag (see spec of Sem) when they are saved by Start_Generic.
1034
1035 package Generic_Flags is new Table.Table (
1036 Table_Component_Type => Boolean,
1037 Table_Index_Type => Int,
1038 Table_Low_Bound => 0,
1039 Table_Initial => 32,
1040 Table_Increment => 200,
1041 Table_Name => "Generic_Flags");
1042
1043 ---------------------------
1044 -- Abandon_Instantiation --
1045 ---------------------------
1046
1047 procedure Abandon_Instantiation (N : Node_Id) is
1048 begin
1049 Error_Msg_N ("\instantiation abandoned!", N);
1050 raise Instantiation_Error;
1051 end Abandon_Instantiation;
1052
1053 ----------------------------------
1054 -- Adjust_Inherited_Pragma_Sloc --
1055 ----------------------------------
1056
1057 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1058 begin
1059 Adjust_Instantiation_Sloc (N, S_Adjustment);
1060 end Adjust_Inherited_Pragma_Sloc;
1061
1062 --------------------------
1063 -- Analyze_Associations --
1064 --------------------------
1065
1066 function Analyze_Associations
1067 (I_Node : Node_Id;
1068 Formals : List_Id;
1069 F_Copy : List_Id) return List_Id
1070 is
1071 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1072 Assoc_List : constant List_Id := New_List;
1073 Default_Actuals : constant List_Id := New_List;
1074 Gen_Unit : constant Entity_Id :=
1075 Defining_Entity (Parent (F_Copy));
1076
1077 Actuals : List_Id;
1078 Actual : Node_Id;
1079 Analyzed_Formal : Node_Id;
1080 First_Named : Node_Id := Empty;
1081 Formal : Node_Id;
1082 Match : Node_Id;
1083 Named : Node_Id;
1084 Saved_Formal : Node_Id;
1085
1086 Default_Formals : constant List_Id := New_List;
1087 -- If an Others_Choice is present, some of the formals may be defaulted.
1088 -- To simplify the treatment of visibility in an instance, we introduce
1089 -- individual defaults for each such formal. These defaults are
1090 -- appended to the list of associations and replace the Others_Choice.
1091
1092 Found_Assoc : Node_Id;
1093 -- Association for the current formal being match. Empty if there are
1094 -- no remaining actuals, or if there is no named association with the
1095 -- name of the formal.
1096
1097 Is_Named_Assoc : Boolean;
1098 Num_Matched : Nat := 0;
1099 Num_Actuals : Nat := 0;
1100
1101 Others_Present : Boolean := False;
1102 Others_Choice : Node_Id := Empty;
1103 -- In Ada 2005, indicates partial parameterization of a formal
1104 -- package. As usual an other association must be last in the list.
1105
1106 procedure Build_Subprogram_Wrappers;
1107 -- Ada 2020: AI12-0272 introduces pre/postconditions for formal
1108 -- subprograms. The implementation of making the formal into a renaming
1109 -- of the actual does not work, given that subprogram renaming cannot
1110 -- carry aspect specifications. Instead we must create subprogram
1111 -- wrappers whose body is a call to the actual, and whose declaration
1112 -- carries the aspects of the formal.
1113
1114 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1115 -- Warn if an actual fixed-point type has user-defined arithmetic
1116 -- operations, but there is no corresponding formal in the generic,
1117 -- in which case the predefined operations will be used. This merits
1118 -- a warning because of the special semantics of fixed point ops.
1119
1120 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1121 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1122 -- cannot have a named association for it. AI05-0025 extends this rule
1123 -- to formals of formal packages by AI05-0025, and it also applies to
1124 -- box-initialized formals.
1125
1126 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1127 -- Determine whether the parameter types and the return type of Subp
1128 -- are fully defined at the point of instantiation.
1129
1130 function Matching_Actual
1131 (F : Entity_Id;
1132 A_F : Entity_Id) return Node_Id;
1133 -- Find actual that corresponds to a given a formal parameter. If the
1134 -- actuals are positional, return the next one, if any. If the actuals
1135 -- are named, scan the parameter associations to find the right one.
1136 -- A_F is the corresponding entity in the analyzed generic, which is
1137 -- placed on the selector name.
1138 --
1139 -- In Ada 2005, a named association may be given with a box, in which
1140 -- case Matching_Actual sets Found_Assoc to the generic association,
1141 -- but return Empty for the actual itself. In this case the code below
1142 -- creates a corresponding declaration for the formal.
1143
1144 function Partial_Parameterization return Boolean;
1145 -- Ada 2005: if no match is found for a given formal, check if the
1146 -- association for it includes a box, or whether the associations
1147 -- include an Others clause.
1148
1149 procedure Process_Default (F : Entity_Id);
1150 -- Add a copy of the declaration of generic formal F to the list of
1151 -- associations, and add an explicit box association for F if there
1152 -- is none yet, and the default comes from an Others_Choice.
1153
1154 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1155 -- Determine whether Subp renames one of the subprograms defined in the
1156 -- generated package Standard.
1157
1158 procedure Set_Analyzed_Formal;
1159 -- Find the node in the generic copy that corresponds to a given formal.
1160 -- The semantic information on this node is used to perform legality
1161 -- checks on the actuals. Because semantic analysis can introduce some
1162 -- anonymous entities or modify the declaration node itself, the
1163 -- correspondence between the two lists is not one-one. In addition to
1164 -- anonymous types, the presence a formal equality will introduce an
1165 -- implicit declaration for the corresponding inequality.
1166
1167 -----------------------------------------
1168 -- procedure Build_Subprogram_Wrappers --
1169 -----------------------------------------
1170
1171 procedure Build_Subprogram_Wrappers is
1172 Formal : constant Entity_Id :=
1173 Defining_Unit_Name (Specification (Analyzed_Formal));
1174 Aspect_Spec : Node_Id;
1175 Decl_Node : Node_Id;
1176 Actual_Name : Node_Id;
1177
1178 begin
1179 -- Create declaration for wrapper subprogram
1180 -- The actual can be overloaded, in which case it will be
1181 -- resolved when the call in the wrapper body is analyzed.
1182 -- We attach the possible interpretations of the actual to
1183 -- the name to be used in the call in the wrapper body.
1184
1185 if Is_Entity_Name (Match) then
1186 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
1187
1188 if Is_Overloaded (Match) then
1189 Save_Interps (Match, Actual_Name);
1190 end if;
1191
1192 else
1193 -- Use renaming declaration created when analyzing actual.
1194 -- This may be incomplete if there are several formal
1195 -- subprograms whose actual is an attribute ???
1196
1197 declare
1198 Renaming_Decl : constant Node_Id := Last (Assoc_List);
1199
1200 begin
1201 Actual_Name := New_Occurrence_Of
1202 (Defining_Entity (Renaming_Decl), Sloc (Match));
1203 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
1204 end;
1205 end if;
1206
1207 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
1208
1209 -- Transfer aspect specifications from formal subprogram to wrapper
1210
1211 Set_Aspect_Specifications (Decl_Node,
1212 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
1213
1214 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
1215 while Present (Aspect_Spec) loop
1216 Set_Analyzed (Aspect_Spec, False);
1217 Next (Aspect_Spec);
1218 end loop;
1219
1220 Append_To (Assoc_List, Decl_Node);
1221
1222 -- Create corresponding body, and append it to association list
1223 -- that appears at the head of the declarations in the instance.
1224 -- The subprogram may be called in the analysis of subsequent
1225 -- actuals.
1226
1227 Append_To (Assoc_List,
1228 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
1229 end Build_Subprogram_Wrappers;
1230
1231 ----------------------------------------
1232 -- Check_Overloaded_Formal_Subprogram --
1233 ----------------------------------------
1234
1235 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1236 Temp_Formal : Entity_Id;
1237
1238 begin
1239 Temp_Formal := First (Formals);
1240 while Present (Temp_Formal) loop
1241 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1242 and then Temp_Formal /= Formal
1243 and then
1244 Chars (Defining_Unit_Name (Specification (Formal))) =
1245 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1246 then
1247 if Present (Found_Assoc) then
1248 Error_Msg_N
1249 ("named association not allowed for overloaded formal",
1250 Found_Assoc);
1251
1252 else
1253 Error_Msg_N
1254 ("named association not allowed for overloaded formal",
1255 Others_Choice);
1256 end if;
1257
1258 Abandon_Instantiation (Instantiation_Node);
1259 end if;
1260
1261 Next (Temp_Formal);
1262 end loop;
1263 end Check_Overloaded_Formal_Subprogram;
1264
1265 -------------------------------
1266 -- Check_Fixed_Point_Actual --
1267 -------------------------------
1268
1269 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1270 Typ : constant Entity_Id := Entity (Actual);
1271 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1272 Elem : Elmt_Id;
1273 Formal : Node_Id;
1274 Op : Entity_Id;
1275
1276 begin
1277 -- Locate primitive operations of the type that are arithmetic
1278 -- operations.
1279
1280 Elem := First_Elmt (Prims);
1281 while Present (Elem) loop
1282 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1283
1284 -- Check whether the generic unit has a formal subprogram of
1285 -- the same name. This does not check types but is good enough
1286 -- to justify a warning.
1287
1288 Formal := First_Non_Pragma (Formals);
1289 Op := Alias (Node (Elem));
1290
1291 while Present (Formal) loop
1292 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1293 and then Chars (Defining_Entity (Formal)) =
1294 Chars (Node (Elem))
1295 then
1296 exit;
1297
1298 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1299 declare
1300 Assoc : Node_Id;
1301 Ent : Entity_Id;
1302
1303 begin
1304 -- Locate corresponding actual, and check whether it
1305 -- includes a fixed-point type.
1306
1307 Assoc := First (Assoc_List);
1308 while Present (Assoc) loop
1309 exit when
1310 Nkind (Assoc) = N_Package_Renaming_Declaration
1311 and then Chars (Defining_Unit_Name (Assoc)) =
1312 Chars (Defining_Identifier (Formal));
1313
1314 Next (Assoc);
1315 end loop;
1316
1317 if Present (Assoc) then
1318
1319 -- If formal package declares a fixed-point type,
1320 -- and the user-defined operator is derived from
1321 -- a generic instance package, the fixed-point type
1322 -- does not use the corresponding predefined op.
1323
1324 Ent := First_Entity (Entity (Name (Assoc)));
1325 while Present (Ent) loop
1326 if Is_Fixed_Point_Type (Ent)
1327 and then Present (Op)
1328 and then Is_Generic_Instance (Scope (Op))
1329 then
1330 return;
1331 end if;
1332
1333 Next_Entity (Ent);
1334 end loop;
1335 end if;
1336 end;
1337 end if;
1338
1339 Next (Formal);
1340 end loop;
1341
1342 if No (Formal) then
1343 Error_Msg_Sloc := Sloc (Node (Elem));
1344 Error_Msg_NE
1345 ("?instance uses predefined operation, not primitive "
1346 & "operation&#", Actual, Node (Elem));
1347 end if;
1348 end if;
1349
1350 Next_Elmt (Elem);
1351 end loop;
1352 end Check_Fixed_Point_Actual;
1353
1354 -------------------------------
1355 -- Has_Fully_Defined_Profile --
1356 -------------------------------
1357
1358 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1359 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1360 -- Determine whethet type Typ is fully defined
1361
1362 ---------------------------
1363 -- Is_Fully_Defined_Type --
1364 ---------------------------
1365
1366 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1367 begin
1368 -- A private type without a full view is not fully defined
1369
1370 if Is_Private_Type (Typ)
1371 and then No (Full_View (Typ))
1372 then
1373 return False;
1374
1375 -- An incomplete type is never fully defined
1376
1377 elsif Is_Incomplete_Type (Typ) then
1378 return False;
1379
1380 -- All other types are fully defined
1381
1382 else
1383 return True;
1384 end if;
1385 end Is_Fully_Defined_Type;
1386
1387 -- Local declarations
1388
1389 Param : Entity_Id;
1390
1391 -- Start of processing for Has_Fully_Defined_Profile
1392
1393 begin
1394 -- Check the parameters
1395
1396 Param := First_Formal (Subp);
1397 while Present (Param) loop
1398 if not Is_Fully_Defined_Type (Etype (Param)) then
1399 return False;
1400 end if;
1401
1402 Next_Formal (Param);
1403 end loop;
1404
1405 -- Check the return type
1406
1407 return Is_Fully_Defined_Type (Etype (Subp));
1408 end Has_Fully_Defined_Profile;
1409
1410 ---------------------
1411 -- Matching_Actual --
1412 ---------------------
1413
1414 function Matching_Actual
1415 (F : Entity_Id;
1416 A_F : Entity_Id) return Node_Id
1417 is
1418 Prev : Node_Id;
1419 Act : Node_Id;
1420
1421 begin
1422 Is_Named_Assoc := False;
1423
1424 -- End of list of purely positional parameters
1425
1426 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1427 Found_Assoc := Empty;
1428 Act := Empty;
1429
1430 -- Case of positional parameter corresponding to current formal
1431
1432 elsif No (Selector_Name (Actual)) then
1433 Found_Assoc := Actual;
1434 Act := Explicit_Generic_Actual_Parameter (Actual);
1435 Num_Matched := Num_Matched + 1;
1436 Next (Actual);
1437
1438 -- Otherwise scan list of named actuals to find the one with the
1439 -- desired name. All remaining actuals have explicit names.
1440
1441 else
1442 Is_Named_Assoc := True;
1443 Found_Assoc := Empty;
1444 Act := Empty;
1445 Prev := Empty;
1446
1447 while Present (Actual) loop
1448 if Nkind (Actual) = N_Others_Choice then
1449 Found_Assoc := Empty;
1450 Act := Empty;
1451
1452 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1453 Set_Entity (Selector_Name (Actual), A_F);
1454 Set_Etype (Selector_Name (Actual), Etype (A_F));
1455 Generate_Reference (A_F, Selector_Name (Actual));
1456
1457 Found_Assoc := Actual;
1458 Act := Explicit_Generic_Actual_Parameter (Actual);
1459 Num_Matched := Num_Matched + 1;
1460 exit;
1461 end if;
1462
1463 Prev := Actual;
1464 Next (Actual);
1465 end loop;
1466
1467 -- Reset for subsequent searches. In most cases the named
1468 -- associations are in order. If they are not, we reorder them
1469 -- to avoid scanning twice the same actual. This is not just a
1470 -- question of efficiency: there may be multiple defaults with
1471 -- boxes that have the same name. In a nested instantiation we
1472 -- insert actuals for those defaults, and cannot rely on their
1473 -- names to disambiguate them.
1474
1475 if Actual = First_Named then
1476 Next (First_Named);
1477
1478 elsif Present (Actual) then
1479 Insert_Before (First_Named, Remove_Next (Prev));
1480 end if;
1481
1482 Actual := First_Named;
1483 end if;
1484
1485 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1486 Set_Used_As_Generic_Actual (Entity (Act));
1487 end if;
1488
1489 return Act;
1490 end Matching_Actual;
1491
1492 ------------------------------
1493 -- Partial_Parameterization --
1494 ------------------------------
1495
1496 function Partial_Parameterization return Boolean is
1497 begin
1498 return Others_Present
1499 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1500 end Partial_Parameterization;
1501
1502 ---------------------
1503 -- Process_Default --
1504 ---------------------
1505
1506 procedure Process_Default (F : Entity_Id) is
1507 Loc : constant Source_Ptr := Sloc (I_Node);
1508 F_Id : constant Entity_Id := Defining_Entity (F);
1509 Decl : Node_Id;
1510 Default : Node_Id;
1511 Id : Entity_Id;
1512
1513 begin
1514 -- Append copy of formal declaration to associations, and create new
1515 -- defining identifier for it.
1516
1517 Decl := New_Copy_Tree (F);
1518 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1519
1520 if Nkind (F) in N_Formal_Subprogram_Declaration then
1521 Set_Defining_Unit_Name (Specification (Decl), Id);
1522
1523 else
1524 Set_Defining_Identifier (Decl, Id);
1525 end if;
1526
1527 Append (Decl, Assoc_List);
1528
1529 if No (Found_Assoc) then
1530 Default :=
1531 Make_Generic_Association (Loc,
1532 Selector_Name =>
1533 New_Occurrence_Of (Id, Loc),
1534 Explicit_Generic_Actual_Parameter => Empty);
1535 Set_Box_Present (Default);
1536 Append (Default, Default_Formals);
1537 end if;
1538 end Process_Default;
1539
1540 ---------------------------------
1541 -- Renames_Standard_Subprogram --
1542 ---------------------------------
1543
1544 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1545 Id : Entity_Id;
1546
1547 begin
1548 Id := Alias (Subp);
1549 while Present (Id) loop
1550 if Scope (Id) = Standard_Standard then
1551 return True;
1552 end if;
1553
1554 Id := Alias (Id);
1555 end loop;
1556
1557 return False;
1558 end Renames_Standard_Subprogram;
1559
1560 -------------------------
1561 -- Set_Analyzed_Formal --
1562 -------------------------
1563
1564 procedure Set_Analyzed_Formal is
1565 Kind : Node_Kind;
1566
1567 begin
1568 while Present (Analyzed_Formal) loop
1569 Kind := Nkind (Analyzed_Formal);
1570
1571 case Nkind (Formal) is
1572 when N_Formal_Subprogram_Declaration =>
1573 exit when Kind in N_Formal_Subprogram_Declaration
1574 and then
1575 Chars
1576 (Defining_Unit_Name (Specification (Formal))) =
1577 Chars
1578 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1579
1580 when N_Formal_Package_Declaration =>
1581 exit when Kind in N_Formal_Package_Declaration
1582 | N_Generic_Package_Declaration
1583 | N_Package_Declaration;
1584
1585 when N_Use_Package_Clause
1586 | N_Use_Type_Clause
1587 =>
1588 exit;
1589
1590 when others =>
1591
1592 -- Skip freeze nodes, and nodes inserted to replace
1593 -- unrecognized pragmas.
1594
1595 exit when
1596 Kind not in N_Formal_Subprogram_Declaration
1597 and then Kind not in N_Subprogram_Declaration
1598 | N_Freeze_Entity
1599 | N_Null_Statement
1600 | N_Itype_Reference
1601 and then Chars (Defining_Identifier (Formal)) =
1602 Chars (Defining_Identifier (Analyzed_Formal));
1603 end case;
1604
1605 Next (Analyzed_Formal);
1606 end loop;
1607 end Set_Analyzed_Formal;
1608
1609 -- Start of processing for Analyze_Associations
1610
1611 begin
1612 Actuals := Generic_Associations (I_Node);
1613
1614 if Present (Actuals) then
1615
1616 -- Check for an Others choice, indicating a partial parameterization
1617 -- for a formal package.
1618
1619 Actual := First (Actuals);
1620 while Present (Actual) loop
1621 if Nkind (Actual) = N_Others_Choice then
1622 Others_Present := True;
1623 Others_Choice := Actual;
1624
1625 if Present (Next (Actual)) then
1626 Error_Msg_N ("OTHERS must be last association", Actual);
1627 end if;
1628
1629 -- This subprogram is used both for formal packages and for
1630 -- instantiations. For the latter, associations must all be
1631 -- explicit.
1632
1633 if Nkind (I_Node) /= N_Formal_Package_Declaration
1634 and then Comes_From_Source (I_Node)
1635 then
1636 Error_Msg_N
1637 ("OTHERS association not allowed in an instance",
1638 Actual);
1639 end if;
1640
1641 -- In any case, nothing to do after the others association
1642
1643 exit;
1644
1645 elsif Box_Present (Actual)
1646 and then Comes_From_Source (I_Node)
1647 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1648 then
1649 Error_Msg_N
1650 ("box association not allowed in an instance", Actual);
1651 end if;
1652
1653 Next (Actual);
1654 end loop;
1655
1656 -- If named associations are present, save first named association
1657 -- (it may of course be Empty) to facilitate subsequent name search.
1658
1659 First_Named := First (Actuals);
1660 while Present (First_Named)
1661 and then Nkind (First_Named) /= N_Others_Choice
1662 and then No (Selector_Name (First_Named))
1663 loop
1664 Num_Actuals := Num_Actuals + 1;
1665 Next (First_Named);
1666 end loop;
1667 end if;
1668
1669 Named := First_Named;
1670 while Present (Named) loop
1671 if Nkind (Named) /= N_Others_Choice
1672 and then No (Selector_Name (Named))
1673 then
1674 Error_Msg_N ("invalid positional actual after named one", Named);
1675 Abandon_Instantiation (Named);
1676 end if;
1677
1678 -- A named association may lack an actual parameter, if it was
1679 -- introduced for a default subprogram that turns out to be local
1680 -- to the outer instantiation. If it has a box association it must
1681 -- correspond to some formal in the generic.
1682
1683 if Nkind (Named) /= N_Others_Choice
1684 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1685 or else Box_Present (Named))
1686 then
1687 Num_Actuals := Num_Actuals + 1;
1688 end if;
1689
1690 Next (Named);
1691 end loop;
1692
1693 if Present (Formals) then
1694 Formal := First_Non_Pragma (Formals);
1695 Analyzed_Formal := First_Non_Pragma (F_Copy);
1696
1697 if Present (Actuals) then
1698 Actual := First (Actuals);
1699
1700 -- All formals should have default values
1701
1702 else
1703 Actual := Empty;
1704 end if;
1705
1706 while Present (Formal) loop
1707 Set_Analyzed_Formal;
1708 Saved_Formal := Next_Non_Pragma (Formal);
1709
1710 case Nkind (Formal) is
1711 when N_Formal_Object_Declaration =>
1712 Match :=
1713 Matching_Actual
1714 (Defining_Identifier (Formal),
1715 Defining_Identifier (Analyzed_Formal));
1716
1717 if No (Match) and then Partial_Parameterization then
1718 Process_Default (Formal);
1719
1720 else
1721 Append_List
1722 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1723 Assoc_List);
1724
1725 -- For a defaulted in_parameter, create an entry in the
1726 -- the list of defaulted actuals, for GNATprove use. Do
1727 -- not included these defaults for an instance nested
1728 -- within a generic, because the defaults are also used
1729 -- in the analysis of the enclosing generic, and only
1730 -- defaulted subprograms are relevant there.
1731
1732 if No (Match) and then not Inside_A_Generic then
1733 Append_To (Default_Actuals,
1734 Make_Generic_Association (Sloc (I_Node),
1735 Selector_Name =>
1736 New_Occurrence_Of
1737 (Defining_Identifier (Formal), Sloc (I_Node)),
1738 Explicit_Generic_Actual_Parameter =>
1739 New_Copy_Tree (Default_Expression (Formal))));
1740 end if;
1741 end if;
1742
1743 -- If the object is a call to an expression function, this
1744 -- is a freezing point for it.
1745
1746 if Is_Entity_Name (Match)
1747 and then Present (Entity (Match))
1748 and then Nkind
1749 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1750 = N_Expression_Function
1751 then
1752 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1753 end if;
1754
1755 when N_Formal_Type_Declaration =>
1756 Match :=
1757 Matching_Actual
1758 (Defining_Identifier (Formal),
1759 Defining_Identifier (Analyzed_Formal));
1760
1761 if No (Match) then
1762 if Partial_Parameterization then
1763 Process_Default (Formal);
1764
1765 else
1766 Error_Msg_Sloc := Sloc (Gen_Unit);
1767 Error_Msg_NE
1768 ("missing actual&",
1769 Instantiation_Node, Defining_Identifier (Formal));
1770 Error_Msg_NE
1771 ("\in instantiation of & declared#",
1772 Instantiation_Node, Gen_Unit);
1773 Abandon_Instantiation (Instantiation_Node);
1774 end if;
1775
1776 else
1777 Analyze (Match);
1778 Append_List
1779 (Instantiate_Type
1780 (Formal, Match, Analyzed_Formal, Assoc_List),
1781 Assoc_List);
1782
1783 -- Warn when an actual is a fixed-point with user-
1784 -- defined promitives. The warning is superfluous
1785 -- if the formal is private, because there can be
1786 -- no arithmetic operations in the generic so there
1787 -- no danger of confusion.
1788
1789 if Is_Fixed_Point_Type (Entity (Match))
1790 and then not Is_Private_Type
1791 (Defining_Identifier (Analyzed_Formal))
1792 then
1793 Check_Fixed_Point_Actual (Match);
1794 end if;
1795
1796 -- An instantiation is a freeze point for the actuals,
1797 -- unless this is a rewritten formal package, or the
1798 -- formal is an Ada 2012 formal incomplete type.
1799
1800 if Nkind (I_Node) = N_Formal_Package_Declaration
1801 or else
1802 (Ada_Version >= Ada_2012
1803 and then
1804 Ekind (Defining_Identifier (Analyzed_Formal)) =
1805 E_Incomplete_Type)
1806 then
1807 null;
1808
1809 else
1810 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1811 end if;
1812 end if;
1813
1814 -- A remote access-to-class-wide type is not a legal actual
1815 -- for a generic formal of an access type (E.2.2(17/2)).
1816 -- In GNAT an exception to this rule is introduced when
1817 -- the formal is marked as remote using implementation
1818 -- defined aspect/pragma Remote_Access_Type. In that case
1819 -- the actual must be remote as well.
1820
1821 -- If the current instantiation is the construction of a
1822 -- local copy for a formal package the actuals may be
1823 -- defaulted, and there is no matching actual to check.
1824
1825 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1826 and then
1827 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1828 N_Access_To_Object_Definition
1829 and then Present (Match)
1830 then
1831 declare
1832 Formal_Ent : constant Entity_Id :=
1833 Defining_Identifier (Analyzed_Formal);
1834 begin
1835 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1836 = Is_Remote_Types (Formal_Ent)
1837 then
1838 -- Remoteness of formal and actual match
1839
1840 null;
1841
1842 elsif Is_Remote_Types (Formal_Ent) then
1843
1844 -- Remote formal, non-remote actual
1845
1846 Error_Msg_NE
1847 ("actual for& must be remote", Match, Formal_Ent);
1848
1849 else
1850 -- Non-remote formal, remote actual
1851
1852 Error_Msg_NE
1853 ("actual for& may not be remote",
1854 Match, Formal_Ent);
1855 end if;
1856 end;
1857 end if;
1858
1859 when N_Formal_Subprogram_Declaration =>
1860 Match :=
1861 Matching_Actual
1862 (Defining_Unit_Name (Specification (Formal)),
1863 Defining_Unit_Name (Specification (Analyzed_Formal)));
1864
1865 -- If the formal subprogram has the same name as another
1866 -- formal subprogram of the generic, then a named
1867 -- association is illegal (12.3(9)). Exclude named
1868 -- associations that are generated for a nested instance.
1869
1870 if Present (Match)
1871 and then Is_Named_Assoc
1872 and then Comes_From_Source (Found_Assoc)
1873 then
1874 Check_Overloaded_Formal_Subprogram (Formal);
1875 end if;
1876
1877 -- If there is no corresponding actual, this may be case
1878 -- of partial parameterization, or else the formal has a
1879 -- default or a box.
1880
1881 if No (Match) and then Partial_Parameterization then
1882 Process_Default (Formal);
1883
1884 if Nkind (I_Node) = N_Formal_Package_Declaration then
1885 Check_Overloaded_Formal_Subprogram (Formal);
1886 end if;
1887
1888 else
1889 Append_To (Assoc_List,
1890 Instantiate_Formal_Subprogram
1891 (Formal, Match, Analyzed_Formal));
1892
1893 -- If formal subprogram has contracts, create wrappers
1894 -- for it. This is an expansion activity that cannot
1895 -- take place e.g. within an enclosing generic unit.
1896
1897 if Has_Contracts (Analyzed_Formal)
1898 and then Expander_Active
1899 then
1900 Build_Subprogram_Wrappers;
1901 end if;
1902
1903 -- An instantiation is a freeze point for the actuals,
1904 -- unless this is a rewritten formal package.
1905
1906 if Nkind (I_Node) /= N_Formal_Package_Declaration
1907 and then Nkind (Match) = N_Identifier
1908 and then Is_Subprogram (Entity (Match))
1909
1910 -- The actual subprogram may rename a routine defined
1911 -- in Standard. Avoid freezing such renamings because
1912 -- subprograms coming from Standard cannot be frozen.
1913
1914 and then
1915 not Renames_Standard_Subprogram (Entity (Match))
1916
1917 -- If the actual subprogram comes from a different
1918 -- unit, it is already frozen, either by a body in
1919 -- that unit or by the end of the declarative part
1920 -- of the unit. This check avoids the freezing of
1921 -- subprograms defined in Standard which are used
1922 -- as generic actuals.
1923
1924 and then In_Same_Code_Unit (Entity (Match), I_Node)
1925 and then Has_Fully_Defined_Profile (Entity (Match))
1926 then
1927 -- Mark the subprogram as having a delayed freeze
1928 -- since this may be an out-of-order action.
1929
1930 Set_Has_Delayed_Freeze (Entity (Match));
1931 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1932 end if;
1933 end if;
1934
1935 -- If this is a nested generic, preserve default for later
1936 -- instantiations. We do this as well for GNATprove use,
1937 -- so that the list of generic associations is complete.
1938
1939 if No (Match) and then Box_Present (Formal) then
1940 declare
1941 Subp : constant Entity_Id :=
1942 Defining_Unit_Name
1943 (Specification (Last (Assoc_List)));
1944
1945 begin
1946 Append_To (Default_Actuals,
1947 Make_Generic_Association (Sloc (I_Node),
1948 Selector_Name =>
1949 New_Occurrence_Of (Subp, Sloc (I_Node)),
1950 Explicit_Generic_Actual_Parameter =>
1951 New_Occurrence_Of (Subp, Sloc (I_Node))));
1952 end;
1953 end if;
1954
1955 when N_Formal_Package_Declaration =>
1956 -- The name of the formal package may be hidden by the
1957 -- formal parameter itself.
1958
1959 if Error_Posted (Analyzed_Formal) then
1960 Abandon_Instantiation (Instantiation_Node);
1961
1962 else
1963 Match :=
1964 Matching_Actual
1965 (Defining_Identifier (Formal),
1966 Defining_Identifier
1967 (Original_Node (Analyzed_Formal)));
1968 end if;
1969
1970 if No (Match) then
1971 if Partial_Parameterization then
1972 Process_Default (Formal);
1973
1974 else
1975 Error_Msg_Sloc := Sloc (Gen_Unit);
1976 Error_Msg_NE
1977 ("missing actual&",
1978 Instantiation_Node, Defining_Identifier (Formal));
1979 Error_Msg_NE
1980 ("\in instantiation of & declared#",
1981 Instantiation_Node, Gen_Unit);
1982
1983 Abandon_Instantiation (Instantiation_Node);
1984 end if;
1985
1986 else
1987 Analyze (Match);
1988 Append_List
1989 (Instantiate_Formal_Package
1990 (Formal, Match, Analyzed_Formal),
1991 Assoc_List);
1992
1993 -- Determine whether the actual package needs an explicit
1994 -- freeze node. This is only the case if the actual is
1995 -- declared in the same unit and has a body. Normally
1996 -- packages do not have explicit freeze nodes, and gigi
1997 -- only uses them to elaborate entities in a package
1998 -- body.
1999
2000 Explicit_Freeze_Check : declare
2001 Actual : constant Entity_Id := Entity (Match);
2002 Gen_Par : Entity_Id;
2003
2004 Needs_Freezing : Boolean;
2005 P : Node_Id;
2006
2007 procedure Check_Generic_Parent;
2008 -- The actual may be an instantiation of a unit
2009 -- declared in a previous instantiation. If that
2010 -- one is also in the current compilation, it must
2011 -- itself be frozen before the actual. The actual
2012 -- may be an instantiation of a generic child unit,
2013 -- in which case the same applies to the instance
2014 -- of the parent which must be frozen before the
2015 -- actual.
2016 -- Should this itself be recursive ???
2017
2018 --------------------------
2019 -- Check_Generic_Parent --
2020 --------------------------
2021
2022 procedure Check_Generic_Parent is
2023 Inst : constant Node_Id :=
2024 Next (Unit_Declaration_Node (Actual));
2025 Par : Entity_Id;
2026
2027 begin
2028 Par := Empty;
2029
2030 if Nkind (Parent (Actual)) = N_Package_Specification
2031 then
2032 Par := Scope (Generic_Parent (Parent (Actual)));
2033
2034 if Is_Generic_Instance (Par) then
2035 null;
2036
2037 -- If the actual is a child generic unit, check
2038 -- whether the instantiation of the parent is
2039 -- also local and must also be frozen now. We
2040 -- must retrieve the instance node to locate the
2041 -- parent instance if any.
2042
2043 elsif Ekind (Par) = E_Generic_Package
2044 and then Is_Child_Unit (Gen_Par)
2045 and then Ekind (Scope (Gen_Par)) =
2046 E_Generic_Package
2047 then
2048 if Nkind (Inst) = N_Package_Instantiation
2049 and then Nkind (Name (Inst)) =
2050 N_Expanded_Name
2051 then
2052 -- Retrieve entity of parent instance
2053
2054 Par := Entity (Prefix (Name (Inst)));
2055 end if;
2056
2057 else
2058 Par := Empty;
2059 end if;
2060 end if;
2061
2062 if Present (Par)
2063 and then Is_Generic_Instance (Par)
2064 and then Scope (Par) = Current_Scope
2065 and then
2066 (No (Freeze_Node (Par))
2067 or else
2068 not Is_List_Member (Freeze_Node (Par)))
2069 then
2070 Set_Has_Delayed_Freeze (Par);
2071 Append_Elmt (Par, Actuals_To_Freeze);
2072 end if;
2073 end Check_Generic_Parent;
2074
2075 -- Start of processing for Explicit_Freeze_Check
2076
2077 begin
2078 if Present (Renamed_Entity (Actual)) then
2079 Gen_Par :=
2080 Generic_Parent (Specification
2081 (Unit_Declaration_Node
2082 (Renamed_Entity (Actual))));
2083 else
2084 Gen_Par :=
2085 Generic_Parent (Specification
2086 (Unit_Declaration_Node (Actual)));
2087 end if;
2088
2089 if not Expander_Active
2090 or else not Has_Completion (Actual)
2091 or else not In_Same_Source_Unit (I_Node, Actual)
2092 or else Is_Frozen (Actual)
2093 or else
2094 (Present (Renamed_Entity (Actual))
2095 and then
2096 not In_Same_Source_Unit
2097 (I_Node, (Renamed_Entity (Actual))))
2098 then
2099 null;
2100
2101 else
2102 -- Finally we want to exclude such freeze nodes
2103 -- from statement sequences, which freeze
2104 -- everything before them.
2105 -- Is this strictly necessary ???
2106
2107 Needs_Freezing := True;
2108
2109 P := Parent (I_Node);
2110 while Nkind (P) /= N_Compilation_Unit loop
2111 if Nkind (P) = N_Handled_Sequence_Of_Statements
2112 then
2113 Needs_Freezing := False;
2114 exit;
2115 end if;
2116
2117 P := Parent (P);
2118 end loop;
2119
2120 if Needs_Freezing then
2121 Check_Generic_Parent;
2122
2123 -- If the actual is a renaming of a proper
2124 -- instance of the formal package, indicate
2125 -- that it is the instance that must be frozen.
2126
2127 if Nkind (Parent (Actual)) =
2128 N_Package_Renaming_Declaration
2129 then
2130 Set_Has_Delayed_Freeze
2131 (Renamed_Entity (Actual));
2132 Append_Elmt
2133 (Renamed_Entity (Actual),
2134 Actuals_To_Freeze);
2135 else
2136 Set_Has_Delayed_Freeze (Actual);
2137 Append_Elmt (Actual, Actuals_To_Freeze);
2138 end if;
2139 end if;
2140 end if;
2141 end Explicit_Freeze_Check;
2142 end if;
2143
2144 -- For use type and use package appearing in the generic part,
2145 -- we have already copied them, so we can just move them where
2146 -- they belong (we mustn't recopy them since this would mess up
2147 -- the Sloc values).
2148
2149 when N_Use_Package_Clause
2150 | N_Use_Type_Clause
2151 =>
2152 if Nkind (Original_Node (I_Node)) =
2153 N_Formal_Package_Declaration
2154 then
2155 Append (New_Copy_Tree (Formal), Assoc_List);
2156 else
2157 Remove (Formal);
2158 Append (Formal, Assoc_List);
2159 end if;
2160
2161 when others =>
2162 raise Program_Error;
2163 end case;
2164
2165 Formal := Saved_Formal;
2166 Next_Non_Pragma (Analyzed_Formal);
2167 end loop;
2168
2169 if Num_Actuals > Num_Matched then
2170 Error_Msg_Sloc := Sloc (Gen_Unit);
2171
2172 if Present (Selector_Name (Actual)) then
2173 Error_Msg_NE
2174 ("unmatched actual &", Actual, Selector_Name (Actual));
2175 Error_Msg_NE
2176 ("\in instantiation of & declared#", Actual, Gen_Unit);
2177 else
2178 Error_Msg_NE
2179 ("unmatched actual in instantiation of & declared#",
2180 Actual, Gen_Unit);
2181 end if;
2182 end if;
2183
2184 elsif Present (Actuals) then
2185 Error_Msg_N
2186 ("too many actuals in generic instantiation", Instantiation_Node);
2187 end if;
2188
2189 -- An instantiation freezes all generic actuals. The only exceptions
2190 -- to this are incomplete types and subprograms which are not fully
2191 -- defined at the point of instantiation.
2192
2193 declare
2194 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2195 begin
2196 while Present (Elmt) loop
2197 Freeze_Before (I_Node, Node (Elmt));
2198 Next_Elmt (Elmt);
2199 end loop;
2200 end;
2201
2202 -- If there are default subprograms, normalize the tree by adding
2203 -- explicit associations for them. This is required if the instance
2204 -- appears within a generic.
2205
2206 if not Is_Empty_List (Default_Actuals) then
2207 declare
2208 Default : Node_Id;
2209
2210 begin
2211 Default := First (Default_Actuals);
2212 while Present (Default) loop
2213 Mark_Rewrite_Insertion (Default);
2214 Next (Default);
2215 end loop;
2216
2217 if No (Actuals) then
2218 Set_Generic_Associations (I_Node, Default_Actuals);
2219 else
2220 Append_List_To (Actuals, Default_Actuals);
2221 end if;
2222 end;
2223 end if;
2224
2225 -- If this is a formal package, normalize the parameter list by adding
2226 -- explicit box associations for the formals that are covered by an
2227 -- Others_Choice.
2228
2229 if not Is_Empty_List (Default_Formals) then
2230 Append_List (Default_Formals, Formals);
2231 end if;
2232
2233 return Assoc_List;
2234 end Analyze_Associations;
2235
2236 -------------------------------
2237 -- Analyze_Formal_Array_Type --
2238 -------------------------------
2239
2240 procedure Analyze_Formal_Array_Type
2241 (T : in out Entity_Id;
2242 Def : Node_Id)
2243 is
2244 DSS : Node_Id;
2245
2246 begin
2247 -- Treated like a non-generic array declaration, with additional
2248 -- semantic checks.
2249
2250 Enter_Name (T);
2251
2252 if Nkind (Def) = N_Constrained_Array_Definition then
2253 DSS := First (Discrete_Subtype_Definitions (Def));
2254 while Present (DSS) loop
2255 if Nkind (DSS) in N_Subtype_Indication
2256 | N_Range
2257 | N_Attribute_Reference
2258 then
2259 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2260 end if;
2261
2262 Next (DSS);
2263 end loop;
2264 end if;
2265
2266 Array_Type_Declaration (T, Def);
2267 Set_Is_Generic_Type (Base_Type (T));
2268
2269 if Ekind (Component_Type (T)) = E_Incomplete_Type
2270 and then No (Full_View (Component_Type (T)))
2271 then
2272 Error_Msg_N ("premature usage of incomplete type", Def);
2273
2274 -- Check that range constraint is not allowed on the component type
2275 -- of a generic formal array type (AARM 12.5.3(3))
2276
2277 elsif Is_Internal (Component_Type (T))
2278 and then Present (Subtype_Indication (Component_Definition (Def)))
2279 and then Nkind (Original_Node
2280 (Subtype_Indication (Component_Definition (Def)))) =
2281 N_Subtype_Indication
2282 then
2283 Error_Msg_N
2284 ("in a formal, a subtype indication can only be "
2285 & "a subtype mark (RM 12.5.3(3))",
2286 Subtype_Indication (Component_Definition (Def)));
2287 end if;
2288
2289 end Analyze_Formal_Array_Type;
2290
2291 ---------------------------------------------
2292 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2293 ---------------------------------------------
2294
2295 -- As for other generic types, we create a valid type representation with
2296 -- legal but arbitrary attributes, whose values are never considered
2297 -- static. For all scalar types we introduce an anonymous base type, with
2298 -- the same attributes. We choose the corresponding integer type to be
2299 -- Standard_Integer.
2300 -- Here and in other similar routines, the Sloc of the generated internal
2301 -- type must be the same as the sloc of the defining identifier of the
2302 -- formal type declaration, to provide proper source navigation.
2303
2304 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2305 (T : Entity_Id;
2306 Def : Node_Id)
2307 is
2308 Loc : constant Source_Ptr := Sloc (Def);
2309
2310 Base : constant Entity_Id :=
2311 New_Internal_Entity
2312 (E_Decimal_Fixed_Point_Type,
2313 Current_Scope,
2314 Sloc (Defining_Identifier (Parent (Def))), 'G');
2315
2316 Int_Base : constant Entity_Id := Standard_Integer;
2317 Delta_Val : constant Ureal := Ureal_1;
2318 Digs_Val : constant Uint := Uint_6;
2319
2320 function Make_Dummy_Bound return Node_Id;
2321 -- Return a properly typed universal real literal to use as a bound
2322
2323 ----------------------
2324 -- Make_Dummy_Bound --
2325 ----------------------
2326
2327 function Make_Dummy_Bound return Node_Id is
2328 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2329 begin
2330 Set_Etype (Bound, Universal_Real);
2331 return Bound;
2332 end Make_Dummy_Bound;
2333
2334 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2335
2336 begin
2337 Enter_Name (T);
2338
2339 Set_Etype (Base, Base);
2340 Set_Size_Info (Base, Int_Base);
2341 Set_RM_Size (Base, RM_Size (Int_Base));
2342 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2343 Set_Digits_Value (Base, Digs_Val);
2344 Set_Delta_Value (Base, Delta_Val);
2345 Set_Small_Value (Base, Delta_Val);
2346 Set_Scalar_Range (Base,
2347 Make_Range (Loc,
2348 Low_Bound => Make_Dummy_Bound,
2349 High_Bound => Make_Dummy_Bound));
2350
2351 Set_Is_Generic_Type (Base);
2352 Set_Parent (Base, Parent (Def));
2353
2354 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2355 Set_Etype (T, Base);
2356 Set_Size_Info (T, Int_Base);
2357 Set_RM_Size (T, RM_Size (Int_Base));
2358 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2359 Set_Digits_Value (T, Digs_Val);
2360 Set_Delta_Value (T, Delta_Val);
2361 Set_Small_Value (T, Delta_Val);
2362 Set_Scalar_Range (T, Scalar_Range (Base));
2363 Set_Is_Constrained (T);
2364
2365 Check_Restriction (No_Fixed_Point, Def);
2366 end Analyze_Formal_Decimal_Fixed_Point_Type;
2367
2368 -------------------------------------------
2369 -- Analyze_Formal_Derived_Interface_Type --
2370 -------------------------------------------
2371
2372 procedure Analyze_Formal_Derived_Interface_Type
2373 (N : Node_Id;
2374 T : Entity_Id;
2375 Def : Node_Id)
2376 is
2377 Loc : constant Source_Ptr := Sloc (Def);
2378
2379 begin
2380 -- Rewrite as a type declaration of a derived type. This ensures that
2381 -- the interface list and primitive operations are properly captured.
2382
2383 Rewrite (N,
2384 Make_Full_Type_Declaration (Loc,
2385 Defining_Identifier => T,
2386 Type_Definition => Def));
2387 Analyze (N);
2388 Set_Is_Generic_Type (T);
2389 end Analyze_Formal_Derived_Interface_Type;
2390
2391 ---------------------------------
2392 -- Analyze_Formal_Derived_Type --
2393 ---------------------------------
2394
2395 procedure Analyze_Formal_Derived_Type
2396 (N : Node_Id;
2397 T : Entity_Id;
2398 Def : Node_Id)
2399 is
2400 Loc : constant Source_Ptr := Sloc (Def);
2401 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2402 New_N : Node_Id;
2403
2404 begin
2405 Set_Is_Generic_Type (T);
2406
2407 if Private_Present (Def) then
2408 New_N :=
2409 Make_Private_Extension_Declaration (Loc,
2410 Defining_Identifier => T,
2411 Discriminant_Specifications => Discriminant_Specifications (N),
2412 Unknown_Discriminants_Present => Unk_Disc,
2413 Subtype_Indication => Subtype_Mark (Def),
2414 Interface_List => Interface_List (Def));
2415
2416 Set_Abstract_Present (New_N, Abstract_Present (Def));
2417 Set_Limited_Present (New_N, Limited_Present (Def));
2418 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2419
2420 else
2421 New_N :=
2422 Make_Full_Type_Declaration (Loc,
2423 Defining_Identifier => T,
2424 Discriminant_Specifications =>
2425 Discriminant_Specifications (Parent (T)),
2426 Type_Definition =>
2427 Make_Derived_Type_Definition (Loc,
2428 Subtype_Indication => Subtype_Mark (Def)));
2429
2430 Set_Abstract_Present
2431 (Type_Definition (New_N), Abstract_Present (Def));
2432 Set_Limited_Present
2433 (Type_Definition (New_N), Limited_Present (Def));
2434 end if;
2435
2436 Rewrite (N, New_N);
2437 Analyze (N);
2438
2439 if Unk_Disc then
2440 if not Is_Composite_Type (T) then
2441 Error_Msg_N
2442 ("unknown discriminants not allowed for elementary types", N);
2443 else
2444 Set_Has_Unknown_Discriminants (T);
2445 Set_Is_Constrained (T, False);
2446 end if;
2447 end if;
2448
2449 -- If the parent type has a known size, so does the formal, which makes
2450 -- legal representation clauses that involve the formal.
2451
2452 Set_Size_Known_At_Compile_Time
2453 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2454 end Analyze_Formal_Derived_Type;
2455
2456 ----------------------------------
2457 -- Analyze_Formal_Discrete_Type --
2458 ----------------------------------
2459
2460 -- The operations defined for a discrete types are those of an enumeration
2461 -- type. The size is set to an arbitrary value, for use in analyzing the
2462 -- generic unit.
2463
2464 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2465 Loc : constant Source_Ptr := Sloc (Def);
2466 Lo : Node_Id;
2467 Hi : Node_Id;
2468
2469 Base : constant Entity_Id :=
2470 New_Internal_Entity
2471 (E_Floating_Point_Type, Current_Scope,
2472 Sloc (Defining_Identifier (Parent (Def))), 'G');
2473
2474 begin
2475 Enter_Name (T);
2476 Mutate_Ekind (T, E_Enumeration_Subtype);
2477 Set_Etype (T, Base);
2478 Init_Size (T, 8);
2479 Init_Alignment (T);
2480 Set_Is_Generic_Type (T);
2481 Set_Is_Constrained (T);
2482
2483 -- For semantic analysis, the bounds of the type must be set to some
2484 -- non-static value. The simplest is to create attribute nodes for those
2485 -- bounds, that refer to the type itself. These bounds are never
2486 -- analyzed but serve as place-holders.
2487
2488 Lo :=
2489 Make_Attribute_Reference (Loc,
2490 Attribute_Name => Name_First,
2491 Prefix => New_Occurrence_Of (T, Loc));
2492 Set_Etype (Lo, T);
2493
2494 Hi :=
2495 Make_Attribute_Reference (Loc,
2496 Attribute_Name => Name_Last,
2497 Prefix => New_Occurrence_Of (T, Loc));
2498 Set_Etype (Hi, T);
2499
2500 Set_Scalar_Range (T,
2501 Make_Range (Loc,
2502 Low_Bound => Lo,
2503 High_Bound => Hi));
2504
2505 Mutate_Ekind (Base, E_Enumeration_Type);
2506 Set_Etype (Base, Base);
2507 Init_Size (Base, 8);
2508 Init_Alignment (Base);
2509 Set_Is_Generic_Type (Base);
2510 Set_Scalar_Range (Base, Scalar_Range (T));
2511 Set_Parent (Base, Parent (Def));
2512 end Analyze_Formal_Discrete_Type;
2513
2514 ----------------------------------
2515 -- Analyze_Formal_Floating_Type --
2516 ---------------------------------
2517
2518 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2519 Base : constant Entity_Id :=
2520 New_Internal_Entity
2521 (E_Floating_Point_Type, Current_Scope,
2522 Sloc (Defining_Identifier (Parent (Def))), 'G');
2523
2524 begin
2525 -- The various semantic attributes are taken from the predefined type
2526 -- Float, just so that all of them are initialized. Their values are
2527 -- never used because no constant folding or expansion takes place in
2528 -- the generic itself.
2529
2530 Enter_Name (T);
2531 Mutate_Ekind (T, E_Floating_Point_Subtype);
2532 Set_Etype (T, Base);
2533 Set_Size_Info (T, (Standard_Float));
2534 Set_RM_Size (T, RM_Size (Standard_Float));
2535 Set_Digits_Value (T, Digits_Value (Standard_Float));
2536 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2537 Set_Is_Constrained (T);
2538
2539 Set_Is_Generic_Type (Base);
2540 Set_Etype (Base, Base);
2541 Set_Size_Info (Base, (Standard_Float));
2542 Set_RM_Size (Base, RM_Size (Standard_Float));
2543 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2544 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2545 Set_Parent (Base, Parent (Def));
2546
2547 Check_Restriction (No_Floating_Point, Def);
2548 end Analyze_Formal_Floating_Type;
2549
2550 -----------------------------------
2551 -- Analyze_Formal_Interface_Type;--
2552 -----------------------------------
2553
2554 procedure Analyze_Formal_Interface_Type
2555 (N : Node_Id;
2556 T : Entity_Id;
2557 Def : Node_Id)
2558 is
2559 Loc : constant Source_Ptr := Sloc (N);
2560 New_N : Node_Id;
2561
2562 begin
2563 New_N :=
2564 Make_Full_Type_Declaration (Loc,
2565 Defining_Identifier => T,
2566 Type_Definition => Def);
2567
2568 Rewrite (N, New_N);
2569 Analyze (N);
2570 Set_Is_Generic_Type (T);
2571 end Analyze_Formal_Interface_Type;
2572
2573 ---------------------------------
2574 -- Analyze_Formal_Modular_Type --
2575 ---------------------------------
2576
2577 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2578 begin
2579 -- Apart from their entity kind, generic modular types are treated like
2580 -- signed integer types, and have the same attributes.
2581
2582 Analyze_Formal_Signed_Integer_Type (T, Def);
2583 Mutate_Ekind (T, E_Modular_Integer_Subtype);
2584 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
2585
2586 end Analyze_Formal_Modular_Type;
2587
2588 ---------------------------------------
2589 -- Analyze_Formal_Object_Declaration --
2590 ---------------------------------------
2591
2592 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2593 E : constant Node_Id := Default_Expression (N);
2594 Id : constant Node_Id := Defining_Identifier (N);
2595 K : Entity_Kind;
2596 T : Node_Id;
2597
2598 begin
2599 Enter_Name (Id);
2600
2601 -- Determine the mode of the formal object
2602
2603 if Out_Present (N) then
2604 K := E_Generic_In_Out_Parameter;
2605
2606 if not In_Present (N) then
2607 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2608 end if;
2609
2610 else
2611 K := E_Generic_In_Parameter;
2612 end if;
2613
2614 if Present (Subtype_Mark (N)) then
2615 Find_Type (Subtype_Mark (N));
2616 T := Entity (Subtype_Mark (N));
2617
2618 -- Verify that there is no redundant null exclusion
2619
2620 if Null_Exclusion_Present (N) then
2621 if not Is_Access_Type (T) then
2622 Error_Msg_N
2623 ("null exclusion can only apply to an access type", N);
2624
2625 elsif Can_Never_Be_Null (T) then
2626 Error_Msg_NE
2627 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2628 end if;
2629 end if;
2630
2631 -- Ada 2005 (AI-423): Formal object with an access definition
2632
2633 else
2634 Check_Access_Definition (N);
2635 T := Access_Definition
2636 (Related_Nod => N,
2637 N => Access_Definition (N));
2638 end if;
2639
2640 if Ekind (T) = E_Incomplete_Type then
2641 declare
2642 Error_Node : Node_Id;
2643
2644 begin
2645 if Present (Subtype_Mark (N)) then
2646 Error_Node := Subtype_Mark (N);
2647 else
2648 Check_Access_Definition (N);
2649 Error_Node := Access_Definition (N);
2650 end if;
2651
2652 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2653 end;
2654 end if;
2655
2656 if K = E_Generic_In_Parameter then
2657
2658 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2659
2660 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2661 Error_Msg_N
2662 ("generic formal of mode IN must not be of limited type", N);
2663 Explain_Limited_Type (T, N);
2664 end if;
2665
2666 if Is_Abstract_Type (T) then
2667 Error_Msg_N
2668 ("generic formal of mode IN must not be of abstract type", N);
2669 end if;
2670
2671 if Present (E) then
2672 Preanalyze_Spec_Expression (E, T);
2673
2674 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2675 Error_Msg_N
2676 ("initialization not allowed for limited types", E);
2677 Explain_Limited_Type (T, E);
2678 end if;
2679 end if;
2680
2681 Mutate_Ekind (Id, K);
2682 Set_Etype (Id, T);
2683
2684 -- Case of generic IN OUT parameter
2685
2686 else
2687 -- If the formal has an unconstrained type, construct its actual
2688 -- subtype, as is done for subprogram formals. In this fashion, all
2689 -- its uses can refer to specific bounds.
2690
2691 Mutate_Ekind (Id, K);
2692 Set_Etype (Id, T);
2693
2694 if (Is_Array_Type (T) and then not Is_Constrained (T))
2695 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2696 then
2697 declare
2698 Non_Freezing_Ref : constant Node_Id :=
2699 New_Occurrence_Of (Id, Sloc (Id));
2700 Decl : Node_Id;
2701
2702 begin
2703 -- Make sure the actual subtype doesn't generate bogus freezing
2704
2705 Set_Must_Not_Freeze (Non_Freezing_Ref);
2706 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2707 Insert_Before_And_Analyze (N, Decl);
2708 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2709 end;
2710 else
2711 Set_Actual_Subtype (Id, T);
2712 end if;
2713
2714 if Present (E) then
2715 Error_Msg_N
2716 ("initialization not allowed for `IN OUT` formals", N);
2717 end if;
2718 end if;
2719
2720 if Has_Aspects (N) then
2721 Analyze_Aspect_Specifications (N, Id);
2722 end if;
2723 end Analyze_Formal_Object_Declaration;
2724
2725 ----------------------------------------------
2726 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2727 ----------------------------------------------
2728
2729 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2730 (T : Entity_Id;
2731 Def : Node_Id)
2732 is
2733 Loc : constant Source_Ptr := Sloc (Def);
2734 Base : constant Entity_Id :=
2735 New_Internal_Entity
2736 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2737 Sloc (Defining_Identifier (Parent (Def))), 'G');
2738
2739 begin
2740 -- The semantic attributes are set for completeness only, their values
2741 -- will never be used, since all properties of the type are non-static.
2742
2743 Enter_Name (T);
2744 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2745 Set_Etype (T, Base);
2746 Set_Size_Info (T, Standard_Integer);
2747 Set_RM_Size (T, RM_Size (Standard_Integer));
2748 Set_Small_Value (T, Ureal_1);
2749 Set_Delta_Value (T, Ureal_1);
2750 Set_Scalar_Range (T,
2751 Make_Range (Loc,
2752 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2753 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2754 Set_Is_Constrained (T);
2755
2756 Set_Is_Generic_Type (Base);
2757 Set_Etype (Base, Base);
2758 Set_Size_Info (Base, Standard_Integer);
2759 Set_RM_Size (Base, RM_Size (Standard_Integer));
2760 Set_Small_Value (Base, Ureal_1);
2761 Set_Delta_Value (Base, Ureal_1);
2762 Set_Scalar_Range (Base, Scalar_Range (T));
2763 Set_Parent (Base, Parent (Def));
2764
2765 Check_Restriction (No_Fixed_Point, Def);
2766 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2767
2768 ----------------------------------------
2769 -- Analyze_Formal_Package_Declaration --
2770 ----------------------------------------
2771
2772 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2773 Gen_Id : constant Node_Id := Name (N);
2774 Loc : constant Source_Ptr := Sloc (N);
2775 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2776 Formal : Entity_Id;
2777 Gen_Decl : Node_Id;
2778 Gen_Unit : Entity_Id;
2779 Renaming : Node_Id;
2780
2781 Vis_Prims_List : Elist_Id := No_Elist;
2782 -- List of primitives made temporarily visible in the instantiation
2783 -- to match the visibility of the formal type.
2784
2785 function Build_Local_Package return Node_Id;
2786 -- The formal package is rewritten so that its parameters are replaced
2787 -- with corresponding declarations. For parameters with bona fide
2788 -- associations these declarations are created by Analyze_Associations
2789 -- as for a regular instantiation. For boxed parameters, we preserve
2790 -- the formal declarations and analyze them, in order to introduce
2791 -- entities of the right kind in the environment of the formal.
2792
2793 -------------------------
2794 -- Build_Local_Package --
2795 -------------------------
2796
2797 function Build_Local_Package return Node_Id is
2798 Decls : List_Id;
2799 Pack_Decl : Node_Id;
2800
2801 begin
2802 -- Within the formal, the name of the generic package is a renaming
2803 -- of the formal (as for a regular instantiation).
2804
2805 Pack_Decl :=
2806 Make_Package_Declaration (Loc,
2807 Specification =>
2808 Copy_Generic_Node
2809 (Specification (Original_Node (Gen_Decl)),
2810 Empty, Instantiating => True));
2811
2812 Renaming :=
2813 Make_Package_Renaming_Declaration (Loc,
2814 Defining_Unit_Name =>
2815 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2816 Name => New_Occurrence_Of (Formal, Loc));
2817
2818 if Nkind (Gen_Id) = N_Identifier
2819 and then Chars (Gen_Id) = Chars (Pack_Id)
2820 then
2821 Error_Msg_NE
2822 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2823 end if;
2824
2825 -- If the formal is declared with a box, or with an others choice,
2826 -- create corresponding declarations for all entities in the formal
2827 -- part, so that names with the proper types are available in the
2828 -- specification of the formal package.
2829
2830 -- On the other hand, if there are no associations, then all the
2831 -- formals must have defaults, and this will be checked by the
2832 -- call to Analyze_Associations.
2833
2834 if Box_Present (N)
2835 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2836 then
2837 declare
2838 Formal_Decl : Node_Id;
2839
2840 begin
2841 -- TBA : for a formal package, need to recurse ???
2842
2843 Decls := New_List;
2844 Formal_Decl :=
2845 First
2846 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2847 while Present (Formal_Decl) loop
2848 Append_To
2849 (Decls,
2850 Copy_Generic_Node
2851 (Formal_Decl, Empty, Instantiating => True));
2852 Next (Formal_Decl);
2853 end loop;
2854 end;
2855
2856 -- If generic associations are present, use Analyze_Associations to
2857 -- create the proper renaming declarations.
2858
2859 else
2860 declare
2861 Act_Tree : constant Node_Id :=
2862 Copy_Generic_Node
2863 (Original_Node (Gen_Decl), Empty,
2864 Instantiating => True);
2865
2866 begin
2867 Generic_Renamings.Set_Last (0);
2868 Generic_Renamings_HTable.Reset;
2869 Instantiation_Node := N;
2870
2871 Decls :=
2872 Analyze_Associations
2873 (I_Node => Original_Node (N),
2874 Formals => Generic_Formal_Declarations (Act_Tree),
2875 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2876
2877 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2878 end;
2879 end if;
2880
2881 Append (Renaming, To => Decls);
2882
2883 -- Add generated declarations ahead of local declarations in
2884 -- the package.
2885
2886 if No (Visible_Declarations (Specification (Pack_Decl))) then
2887 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2888 else
2889 Insert_List_Before
2890 (First (Visible_Declarations (Specification (Pack_Decl))),
2891 Decls);
2892 end if;
2893
2894 return Pack_Decl;
2895 end Build_Local_Package;
2896
2897 -- Local variables
2898
2899 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2900 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2901
2902 Associations : Boolean := True;
2903 New_N : Node_Id;
2904 Parent_Installed : Boolean := False;
2905 Parent_Instance : Entity_Id;
2906 Renaming_In_Par : Entity_Id;
2907
2908 -- Start of processing for Analyze_Formal_Package_Declaration
2909
2910 begin
2911 Check_Text_IO_Special_Unit (Gen_Id);
2912
2913 Init_Env;
2914 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2915 Gen_Unit := Entity (Gen_Id);
2916
2917 -- Check for a formal package that is a package renaming
2918
2919 if Present (Renamed_Object (Gen_Unit)) then
2920
2921 -- Indicate that unit is used, before replacing it with renamed
2922 -- entity for use below.
2923
2924 if In_Extended_Main_Source_Unit (N) then
2925 Set_Is_Instantiated (Gen_Unit);
2926 Generate_Reference (Gen_Unit, N);
2927 end if;
2928
2929 Gen_Unit := Renamed_Object (Gen_Unit);
2930 end if;
2931
2932 if Ekind (Gen_Unit) /= E_Generic_Package then
2933 Error_Msg_N ("expect generic package name", Gen_Id);
2934 Restore_Env;
2935 goto Leave;
2936
2937 elsif Gen_Unit = Current_Scope then
2938 Error_Msg_N
2939 ("generic package cannot be used as a formal package of itself",
2940 Gen_Id);
2941 Restore_Env;
2942 goto Leave;
2943
2944 elsif In_Open_Scopes (Gen_Unit) then
2945 if Is_Compilation_Unit (Gen_Unit)
2946 and then Is_Child_Unit (Current_Scope)
2947 then
2948 -- Special-case the error when the formal is a parent, and
2949 -- continue analysis to minimize cascaded errors.
2950
2951 Error_Msg_N
2952 ("generic parent cannot be used as formal package of a child "
2953 & "unit", Gen_Id);
2954
2955 else
2956 Error_Msg_N
2957 ("generic package cannot be used as a formal package within "
2958 & "itself", Gen_Id);
2959 Restore_Env;
2960 goto Leave;
2961 end if;
2962 end if;
2963
2964 -- Check that name of formal package does not hide name of generic,
2965 -- or its leading prefix. This check must be done separately because
2966 -- the name of the generic has already been analyzed.
2967
2968 declare
2969 Gen_Name : Entity_Id;
2970
2971 begin
2972 Gen_Name := Gen_Id;
2973 while Nkind (Gen_Name) = N_Expanded_Name loop
2974 Gen_Name := Prefix (Gen_Name);
2975 end loop;
2976
2977 if Chars (Gen_Name) = Chars (Pack_Id) then
2978 Error_Msg_NE
2979 ("& is hidden within declaration of formal package",
2980 Gen_Id, Gen_Name);
2981 end if;
2982 end;
2983
2984 if Box_Present (N)
2985 or else No (Generic_Associations (N))
2986 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2987 then
2988 Associations := False;
2989 end if;
2990
2991 -- If there are no generic associations, the generic parameters appear
2992 -- as local entities and are instantiated like them. We copy the generic
2993 -- package declaration as if it were an instantiation, and analyze it
2994 -- like a regular package, except that we treat the formals as
2995 -- additional visible components.
2996
2997 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2998
2999 if In_Extended_Main_Source_Unit (N) then
3000 Set_Is_Instantiated (Gen_Unit);
3001 Generate_Reference (Gen_Unit, N);
3002 end if;
3003
3004 Formal := New_Copy (Pack_Id);
3005 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3006
3007 -- Make local generic without formals. The formals will be replaced with
3008 -- internal declarations.
3009
3010 begin
3011 New_N := Build_Local_Package;
3012
3013 -- If there are errors in the parameter list, Analyze_Associations
3014 -- raises Instantiation_Error. Patch the declaration to prevent further
3015 -- exception propagation.
3016
3017 exception
3018 when Instantiation_Error =>
3019 Enter_Name (Formal);
3020 Mutate_Ekind (Formal, E_Variable);
3021 Set_Etype (Formal, Any_Type);
3022 Restore_Hidden_Primitives (Vis_Prims_List);
3023
3024 if Parent_Installed then
3025 Remove_Parent;
3026 end if;
3027
3028 goto Leave;
3029 end;
3030
3031 Rewrite (N, New_N);
3032 Set_Defining_Unit_Name (Specification (New_N), Formal);
3033 Set_Generic_Parent (Specification (N), Gen_Unit);
3034 Set_Instance_Env (Gen_Unit, Formal);
3035 Set_Is_Generic_Instance (Formal);
3036
3037 Enter_Name (Formal);
3038 Mutate_Ekind (Formal, E_Package);
3039 Set_Etype (Formal, Standard_Void_Type);
3040 Set_Inner_Instances (Formal, New_Elmt_List);
3041
3042 -- It is unclear that any aspects can apply to a formal package
3043 -- declaration, given that they look like a hidden conformance
3044 -- requirement on the corresponding actual. However, Abstract_State
3045 -- must be treated specially because it generates declarations that
3046 -- must appear before other declarations in the specification and
3047 -- must be analyzed at once.
3048
3049 if Present (Aspect_Specifications (Gen_Decl)) then
3050 if No (Aspect_Specifications (N)) then
3051 Set_Aspect_Specifications (N, New_List);
3052 Set_Has_Aspects (N);
3053 end if;
3054
3055 declare
3056 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3057 New_A : Node_Id;
3058
3059 begin
3060 while Present (ASN) loop
3061 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3062 New_A :=
3063 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3064 Set_Entity (New_A, Formal);
3065 Set_Analyzed (New_A, False);
3066 Append (New_A, Aspect_Specifications (N));
3067 Analyze_Aspect_Specifications (N, Formal);
3068 exit;
3069 end if;
3070
3071 Next (ASN);
3072 end loop;
3073 end;
3074 end if;
3075
3076 Push_Scope (Formal);
3077
3078 -- Manually set the SPARK_Mode from the context because the package
3079 -- declaration is never analyzed.
3080
3081 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3082 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3083 Set_SPARK_Pragma_Inherited (Formal);
3084 Set_SPARK_Aux_Pragma_Inherited (Formal);
3085
3086 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3087
3088 -- Similarly, we have to make the name of the formal visible in the
3089 -- parent instance, to resolve properly fully qualified names that
3090 -- may appear in the generic unit. The parent instance has been
3091 -- placed on the scope stack ahead of the current scope.
3092
3093 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3094
3095 Renaming_In_Par :=
3096 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3097 Mutate_Ekind (Renaming_In_Par, E_Package);
3098 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3099 Set_Scope (Renaming_In_Par, Parent_Instance);
3100 Set_Parent (Renaming_In_Par, Parent (Formal));
3101 Set_Renamed_Object (Renaming_In_Par, Formal);
3102 Append_Entity (Renaming_In_Par, Parent_Instance);
3103 end if;
3104
3105 -- A formal package declaration behaves as a package instantiation with
3106 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3107 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3108 -- all SPARK_Mode pragmas within the generic_package_name.
3109
3110 if SPARK_Mode /= On then
3111 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3112
3113 -- Mark the formal spec in case the body is instantiated at a later
3114 -- pass. This preserves the original context in effect for the body.
3115
3116 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3117 end if;
3118
3119 Analyze (Specification (N));
3120
3121 -- The formals for which associations are provided are not visible
3122 -- outside of the formal package. The others are still declared by a
3123 -- formal parameter declaration.
3124
3125 -- If there are no associations, the only local entity to hide is the
3126 -- generated package renaming itself.
3127
3128 declare
3129 E : Entity_Id;
3130
3131 begin
3132 E := First_Entity (Formal);
3133 while Present (E) loop
3134 if Associations and then not Is_Generic_Formal (E) then
3135 Set_Is_Hidden (E);
3136 end if;
3137
3138 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3139 Set_Is_Hidden (E);
3140 exit;
3141 end if;
3142
3143 Next_Entity (E);
3144 end loop;
3145 end;
3146
3147 End_Package_Scope (Formal);
3148 Restore_Hidden_Primitives (Vis_Prims_List);
3149
3150 if Parent_Installed then
3151 Remove_Parent;
3152 end if;
3153
3154 Restore_Env;
3155
3156 -- Inside the generic unit, the formal package is a regular package, but
3157 -- no body is needed for it. Note that after instantiation, the defining
3158 -- unit name we need is in the new tree and not in the original (see
3159 -- Package_Instantiation). A generic formal package is an instance, and
3160 -- can be used as an actual for an inner instance.
3161
3162 Set_Has_Completion (Formal, True);
3163
3164 -- Add semantic information to the original defining identifier.
3165
3166 Mutate_Ekind (Pack_Id, E_Package);
3167 Set_Etype (Pack_Id, Standard_Void_Type);
3168 Set_Scope (Pack_Id, Scope (Formal));
3169 Set_Has_Completion (Pack_Id, True);
3170
3171 <<Leave>>
3172 if Has_Aspects (N) then
3173 -- Unclear that any other aspects may appear here, snalyze them
3174 -- for completion, given that the grammar allows their appearance.
3175
3176 Analyze_Aspect_Specifications (N, Pack_Id);
3177 end if;
3178
3179 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3180 end Analyze_Formal_Package_Declaration;
3181
3182 ---------------------------------
3183 -- Analyze_Formal_Private_Type --
3184 ---------------------------------
3185
3186 procedure Analyze_Formal_Private_Type
3187 (N : Node_Id;
3188 T : Entity_Id;
3189 Def : Node_Id)
3190 is
3191 begin
3192 New_Private_Type (N, T, Def);
3193
3194 -- Set the size to an arbitrary but legal value
3195
3196 Set_Size_Info (T, Standard_Integer);
3197 Set_RM_Size (T, RM_Size (Standard_Integer));
3198 end Analyze_Formal_Private_Type;
3199
3200 ------------------------------------
3201 -- Analyze_Formal_Incomplete_Type --
3202 ------------------------------------
3203
3204 procedure Analyze_Formal_Incomplete_Type
3205 (T : Entity_Id;
3206 Def : Node_Id)
3207 is
3208 begin
3209 Enter_Name (T);
3210 Mutate_Ekind (T, E_Incomplete_Type);
3211 Set_Etype (T, T);
3212 Set_Private_Dependents (T, New_Elmt_List);
3213
3214 if Tagged_Present (Def) then
3215 Set_Is_Tagged_Type (T);
3216 Make_Class_Wide_Type (T);
3217 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3218 end if;
3219 end Analyze_Formal_Incomplete_Type;
3220
3221 ----------------------------------------
3222 -- Analyze_Formal_Signed_Integer_Type --
3223 ----------------------------------------
3224
3225 procedure Analyze_Formal_Signed_Integer_Type
3226 (T : Entity_Id;
3227 Def : Node_Id)
3228 is
3229 Base : constant Entity_Id :=
3230 New_Internal_Entity
3231 (E_Signed_Integer_Type,
3232 Current_Scope,
3233 Sloc (Defining_Identifier (Parent (Def))), 'G');
3234
3235 begin
3236 Enter_Name (T);
3237
3238 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3239 Set_Etype (T, Base);
3240 Set_Size_Info (T, Standard_Integer);
3241 Set_RM_Size (T, RM_Size (Standard_Integer));
3242 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3243 Set_Is_Constrained (T);
3244
3245 Set_Is_Generic_Type (Base);
3246 Set_Size_Info (Base, Standard_Integer);
3247 Set_RM_Size (Base, RM_Size (Standard_Integer));
3248 Set_Etype (Base, Base);
3249 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3250 Set_Parent (Base, Parent (Def));
3251 end Analyze_Formal_Signed_Integer_Type;
3252
3253 -------------------------------------------
3254 -- Analyze_Formal_Subprogram_Declaration --
3255 -------------------------------------------
3256
3257 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3258 Spec : constant Node_Id := Specification (N);
3259 Def : constant Node_Id := Default_Name (N);
3260 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3261 Subp : Entity_Id;
3262
3263 begin
3264 if Nam = Error then
3265 return;
3266 end if;
3267
3268 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3269 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3270 goto Leave;
3271 end if;
3272
3273 Analyze_Subprogram_Declaration (N);
3274 Set_Is_Formal_Subprogram (Nam);
3275 Set_Has_Completion (Nam);
3276
3277 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3278 Set_Is_Abstract_Subprogram (Nam);
3279
3280 Set_Is_Dispatching_Operation (Nam);
3281
3282 -- A formal abstract procedure cannot have a null default
3283 -- (RM 12.6(4.1/2)).
3284
3285 if Nkind (Spec) = N_Procedure_Specification
3286 and then Null_Present (Spec)
3287 then
3288 Error_Msg_N
3289 ("a formal abstract subprogram cannot default to null", Spec);
3290 end if;
3291
3292 declare
3293 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3294 begin
3295 if No (Ctrl_Type) then
3296 Error_Msg_N
3297 ("abstract formal subprogram must have a controlling type",
3298 N);
3299
3300 elsif Ada_Version >= Ada_2012
3301 and then Is_Incomplete_Type (Ctrl_Type)
3302 then
3303 Error_Msg_NE
3304 ("controlling type of abstract formal subprogram cannot "
3305 & "be incomplete type", N, Ctrl_Type);
3306
3307 else
3308 Check_Controlling_Formals (Ctrl_Type, Nam);
3309 end if;
3310 end;
3311 end if;
3312
3313 -- Default name is resolved at the point of instantiation
3314
3315 if Box_Present (N) then
3316 null;
3317
3318 -- Else default is bound at the point of generic declaration
3319
3320 elsif Present (Def) then
3321 if Nkind (Def) = N_Operator_Symbol then
3322 Find_Direct_Name (Def);
3323
3324 elsif Nkind (Def) /= N_Attribute_Reference then
3325 Analyze (Def);
3326
3327 else
3328 -- For an attribute reference, analyze the prefix and verify
3329 -- that it has the proper profile for the subprogram.
3330
3331 Analyze (Prefix (Def));
3332 Valid_Default_Attribute (Nam, Def);
3333 goto Leave;
3334 end if;
3335
3336 -- Default name may be overloaded, in which case the interpretation
3337 -- with the correct profile must be selected, as for a renaming.
3338 -- If the definition is an indexed component, it must denote a
3339 -- member of an entry family. If it is a selected component, it
3340 -- can be a protected operation.
3341
3342 if Etype (Def) = Any_Type then
3343 goto Leave;
3344
3345 elsif Nkind (Def) = N_Selected_Component then
3346 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3347 Error_Msg_N ("expect valid subprogram name as default", Def);
3348 end if;
3349
3350 elsif Nkind (Def) = N_Indexed_Component then
3351 if Is_Entity_Name (Prefix (Def)) then
3352 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3353 Error_Msg_N ("expect valid subprogram name as default", Def);
3354 end if;
3355
3356 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3357 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3358 E_Entry_Family
3359 then
3360 Error_Msg_N ("expect valid subprogram name as default", Def);
3361 end if;
3362
3363 else
3364 Error_Msg_N ("expect valid subprogram name as default", Def);
3365 goto Leave;
3366 end if;
3367
3368 elsif Nkind (Def) = N_Character_Literal then
3369
3370 -- Needs some type checks: subprogram should be parameterless???
3371
3372 Resolve (Def, (Etype (Nam)));
3373
3374 elsif not Is_Entity_Name (Def)
3375 or else not Is_Overloadable (Entity (Def))
3376 then
3377 Error_Msg_N ("expect valid subprogram name as default", Def);
3378 goto Leave;
3379
3380 elsif not Is_Overloaded (Def) then
3381 Subp := Entity (Def);
3382
3383 if Subp = Nam then
3384 Error_Msg_N ("premature usage of formal subprogram", Def);
3385
3386 elsif not Entity_Matches_Spec (Subp, Nam) then
3387 Error_Msg_N ("no visible entity matches specification", Def);
3388 end if;
3389
3390 -- More than one interpretation, so disambiguate as for a renaming
3391
3392 else
3393 declare
3394 I : Interp_Index;
3395 I1 : Interp_Index := 0;
3396 It : Interp;
3397 It1 : Interp;
3398
3399 begin
3400 Subp := Any_Id;
3401 Get_First_Interp (Def, I, It);
3402 while Present (It.Nam) loop
3403 if Entity_Matches_Spec (It.Nam, Nam) then
3404 if Subp /= Any_Id then
3405 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3406
3407 if It1 = No_Interp then
3408 Error_Msg_N ("ambiguous default subprogram", Def);
3409 else
3410 Subp := It1.Nam;
3411 end if;
3412
3413 exit;
3414
3415 else
3416 I1 := I;
3417 Subp := It.Nam;
3418 end if;
3419 end if;
3420
3421 Get_Next_Interp (I, It);
3422 end loop;
3423 end;
3424
3425 if Subp /= Any_Id then
3426
3427 -- Subprogram found, generate reference to it
3428
3429 Set_Entity (Def, Subp);
3430 Generate_Reference (Subp, Def);
3431
3432 if Subp = Nam then
3433 Error_Msg_N ("premature usage of formal subprogram", Def);
3434
3435 elsif Ekind (Subp) /= E_Operator then
3436 Check_Mode_Conformant (Subp, Nam);
3437 end if;
3438
3439 else
3440 Error_Msg_N ("no visible subprogram matches specification", N);
3441 end if;
3442 end if;
3443 end if;
3444
3445 <<Leave>>
3446 if Has_Aspects (N) then
3447 Analyze_Aspect_Specifications (N, Nam);
3448 end if;
3449
3450 end Analyze_Formal_Subprogram_Declaration;
3451
3452 -------------------------------------
3453 -- Analyze_Formal_Type_Declaration --
3454 -------------------------------------
3455
3456 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3457 Def : constant Node_Id := Formal_Type_Definition (N);
3458 T : Entity_Id;
3459
3460 begin
3461 T := Defining_Identifier (N);
3462
3463 if Present (Discriminant_Specifications (N))
3464 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3465 then
3466 Error_Msg_N
3467 ("discriminants not allowed for this formal type", T);
3468 end if;
3469
3470 -- Enter the new name, and branch to specific routine
3471
3472 case Nkind (Def) is
3473 when N_Formal_Private_Type_Definition =>
3474 Analyze_Formal_Private_Type (N, T, Def);
3475
3476 when N_Formal_Derived_Type_Definition =>
3477 Analyze_Formal_Derived_Type (N, T, Def);
3478
3479 when N_Formal_Incomplete_Type_Definition =>
3480 Analyze_Formal_Incomplete_Type (T, Def);
3481
3482 when N_Formal_Discrete_Type_Definition =>
3483 Analyze_Formal_Discrete_Type (T, Def);
3484
3485 when N_Formal_Signed_Integer_Type_Definition =>
3486 Analyze_Formal_Signed_Integer_Type (T, Def);
3487
3488 when N_Formal_Modular_Type_Definition =>
3489 Analyze_Formal_Modular_Type (T, Def);
3490
3491 when N_Formal_Floating_Point_Definition =>
3492 Analyze_Formal_Floating_Type (T, Def);
3493
3494 when N_Formal_Ordinary_Fixed_Point_Definition =>
3495 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3496
3497 when N_Formal_Decimal_Fixed_Point_Definition =>
3498 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3499
3500 when N_Array_Type_Definition =>
3501 Analyze_Formal_Array_Type (T, Def);
3502
3503 when N_Access_Function_Definition
3504 | N_Access_Procedure_Definition
3505 | N_Access_To_Object_Definition
3506 =>
3507 Analyze_Generic_Access_Type (T, Def);
3508
3509 -- Ada 2005: a interface declaration is encoded as an abstract
3510 -- record declaration or a abstract type derivation.
3511
3512 when N_Record_Definition =>
3513 Analyze_Formal_Interface_Type (N, T, Def);
3514
3515 when N_Derived_Type_Definition =>
3516 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3517
3518 when N_Error =>
3519 null;
3520
3521 when others =>
3522 raise Program_Error;
3523 end case;
3524
3525 -- A formal type declaration declares a type and its first
3526 -- subtype.
3527
3528 Set_Is_Generic_Type (T);
3529 Set_Is_First_Subtype (T);
3530
3531 if Has_Aspects (N) then
3532 Analyze_Aspect_Specifications (N, T);
3533 end if;
3534 end Analyze_Formal_Type_Declaration;
3535
3536 ------------------------------------
3537 -- Analyze_Function_Instantiation --
3538 ------------------------------------
3539
3540 procedure Analyze_Function_Instantiation (N : Node_Id) is
3541 begin
3542 Analyze_Subprogram_Instantiation (N, E_Function);
3543 end Analyze_Function_Instantiation;
3544
3545 ---------------------------------
3546 -- Analyze_Generic_Access_Type --
3547 ---------------------------------
3548
3549 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3550 begin
3551 Enter_Name (T);
3552
3553 if Nkind (Def) = N_Access_To_Object_Definition then
3554 Access_Type_Declaration (T, Def);
3555
3556 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3557 and then No (Full_View (Designated_Type (T)))
3558 and then not Is_Generic_Type (Designated_Type (T))
3559 then
3560 Error_Msg_N ("premature usage of incomplete type", Def);
3561
3562 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3563 Error_Msg_N
3564 ("only a subtype mark is allowed in a formal", Def);
3565 end if;
3566
3567 else
3568 Access_Subprogram_Declaration (T, Def);
3569 end if;
3570 end Analyze_Generic_Access_Type;
3571
3572 ---------------------------------
3573 -- Analyze_Generic_Formal_Part --
3574 ---------------------------------
3575
3576 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3577 Gen_Parm_Decl : Node_Id;
3578
3579 begin
3580 -- The generic formals are processed in the scope of the generic unit,
3581 -- where they are immediately visible. The scope is installed by the
3582 -- caller.
3583
3584 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3585 while Present (Gen_Parm_Decl) loop
3586 Analyze (Gen_Parm_Decl);
3587 Next (Gen_Parm_Decl);
3588 end loop;
3589
3590 Generate_Reference_To_Generic_Formals (Current_Scope);
3591
3592 -- For Ada 2020, some formal parameters can carry aspects, which must
3593 -- be name-resolved at the end of the list of formal parameters (which
3594 -- has the semantics of a declaration list).
3595
3596 Analyze_Contracts (Generic_Formal_Declarations (N));
3597 end Analyze_Generic_Formal_Part;
3598
3599 ------------------------------------------
3600 -- Analyze_Generic_Package_Declaration --
3601 ------------------------------------------
3602
3603 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3604 Decls : constant List_Id := Visible_Declarations (Specification (N));
3605 Loc : constant Source_Ptr := Sloc (N);
3606
3607 Decl : Node_Id;
3608 Id : Entity_Id;
3609 New_N : Node_Id;
3610 Renaming : Node_Id;
3611 Save_Parent : Node_Id;
3612
3613 begin
3614 -- A generic may grant access to its private enclosing context depending
3615 -- on the placement of its corresponding body. From elaboration point of
3616 -- view, the flow of execution may enter this private context, and then
3617 -- reach an external unit, thus producing a dependency on that external
3618 -- unit. For such a path to be properly discovered and encoded in the
3619 -- ALI file of the main unit, let the ABE mechanism process the body of
3620 -- the main unit, and encode all relevant invocation constructs and the
3621 -- relations between them.
3622
3623 Mark_Save_Invocation_Graph_Of_Body;
3624
3625 -- We introduce a renaming of the enclosing package, to have a usable
3626 -- entity as the prefix of an expanded name for a local entity of the
3627 -- form Par.P.Q, where P is the generic package. This is because a local
3628 -- entity named P may hide it, so that the usual visibility rules in
3629 -- the instance will not resolve properly.
3630
3631 Renaming :=
3632 Make_Package_Renaming_Declaration (Loc,
3633 Defining_Unit_Name =>
3634 Make_Defining_Identifier (Loc,
3635 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3636 Name =>
3637 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3638
3639 -- The declaration is inserted before other declarations, but before
3640 -- pragmas that may be library-unit pragmas and must appear before other
3641 -- declarations. The pragma Compile_Time_Error is not in this class, and
3642 -- may contain an expression that includes such a qualified name, so the
3643 -- renaming declaration must appear before it.
3644
3645 -- Are there other pragmas that require this special handling ???
3646
3647 if Present (Decls) then
3648 Decl := First (Decls);
3649 while Present (Decl)
3650 and then Nkind (Decl) = N_Pragma
3651 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3652 loop
3653 Next (Decl);
3654 end loop;
3655
3656 if Present (Decl) then
3657 Insert_Before (Decl, Renaming);
3658 else
3659 Append (Renaming, Visible_Declarations (Specification (N)));
3660 end if;
3661
3662 else
3663 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3664 end if;
3665
3666 -- Create copy of generic unit, and save for instantiation. If the unit
3667 -- is a child unit, do not copy the specifications for the parent, which
3668 -- are not part of the generic tree.
3669
3670 Save_Parent := Parent_Spec (N);
3671 Set_Parent_Spec (N, Empty);
3672
3673 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3674 Set_Parent_Spec (New_N, Save_Parent);
3675 Rewrite (N, New_N);
3676
3677 -- Once the contents of the generic copy and the template are swapped,
3678 -- do the same for their respective aspect specifications.
3679
3680 Exchange_Aspects (N, New_N);
3681
3682 -- Collect all contract-related source pragmas found within the template
3683 -- and attach them to the contract of the package spec. This contract is
3684 -- used in the capture of global references within annotations.
3685
3686 Create_Generic_Contract (N);
3687
3688 Id := Defining_Entity (N);
3689 Generate_Definition (Id);
3690
3691 -- Expansion is not applied to generic units
3692
3693 Start_Generic;
3694
3695 Enter_Name (Id);
3696 Mutate_Ekind (Id, E_Generic_Package);
3697 Set_Etype (Id, Standard_Void_Type);
3698
3699 -- Set SPARK_Mode from context
3700
3701 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3702 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3703 Set_SPARK_Pragma_Inherited (Id);
3704 Set_SPARK_Aux_Pragma_Inherited (Id);
3705
3706 -- Preserve relevant elaboration-related attributes of the context which
3707 -- are no longer available or very expensive to recompute once analysis,
3708 -- resolution, and expansion are over.
3709
3710 Mark_Elaboration_Attributes
3711 (N_Id => Id,
3712 Checks => True,
3713 Warnings => True);
3714
3715 -- Analyze aspects now, so that generated pragmas appear in the
3716 -- declarations before building and analyzing the generic copy.
3717
3718 if Has_Aspects (N) then
3719 Analyze_Aspect_Specifications (N, Id);
3720 end if;
3721
3722 Push_Scope (Id);
3723 Enter_Generic_Scope (Id);
3724 Set_Inner_Instances (Id, New_Elmt_List);
3725
3726 Set_Categorization_From_Pragmas (N);
3727 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3728
3729 -- Link the declaration of the generic homonym in the generic copy to
3730 -- the package it renames, so that it is always resolved properly.
3731
3732 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3733 Set_Entity (Associated_Node (Name (Renaming)), Id);
3734
3735 -- For a library unit, we have reconstructed the entity for the unit,
3736 -- and must reset it in the library tables.
3737
3738 if Nkind (Parent (N)) = N_Compilation_Unit then
3739 Set_Cunit_Entity (Current_Sem_Unit, Id);
3740 end if;
3741
3742 Analyze_Generic_Formal_Part (N);
3743
3744 -- After processing the generic formals, analysis proceeds as for a
3745 -- non-generic package.
3746
3747 Analyze (Specification (N));
3748
3749 Validate_Categorization_Dependency (N, Id);
3750
3751 End_Generic;
3752
3753 End_Package_Scope (Id);
3754 Exit_Generic_Scope (Id);
3755
3756 -- If the generic appears within a package unit, the body of that unit
3757 -- has to be present for instantiation and inlining.
3758
3759 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3760 Set_Body_Needed_For_Inlining
3761 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3762 end if;
3763
3764 if Nkind (Parent (N)) /= N_Compilation_Unit then
3765 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3766 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3767 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3768
3769 else
3770 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3771 Validate_RT_RAT_Component (N);
3772
3773 -- If this is a spec without a body, check that generic parameters
3774 -- are referenced.
3775
3776 if not Body_Required (Parent (N)) then
3777 Check_References (Id);
3778 end if;
3779 end if;
3780
3781 -- If there is a specified storage pool in the context, create an
3782 -- aspect on the package declaration, so that it is used in any
3783 -- instance that does not override it.
3784
3785 if Present (Default_Pool) then
3786 declare
3787 ASN : Node_Id;
3788
3789 begin
3790 ASN :=
3791 Make_Aspect_Specification (Loc,
3792 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3793 Expression => New_Copy (Default_Pool));
3794
3795 if No (Aspect_Specifications (Specification (N))) then
3796 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3797 else
3798 Append (ASN, Aspect_Specifications (Specification (N)));
3799 end if;
3800 end;
3801 end if;
3802 end Analyze_Generic_Package_Declaration;
3803
3804 --------------------------------------------
3805 -- Analyze_Generic_Subprogram_Declaration --
3806 --------------------------------------------
3807
3808 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3809 Formals : List_Id;
3810 Id : Entity_Id;
3811 New_N : Node_Id;
3812 Result_Type : Entity_Id;
3813 Save_Parent : Node_Id;
3814 Spec : Node_Id;
3815 Typ : Entity_Id;
3816
3817 begin
3818 -- A generic may grant access to its private enclosing context depending
3819 -- on the placement of its corresponding body. From elaboration point of
3820 -- view, the flow of execution may enter this private context, and then
3821 -- reach an external unit, thus producing a dependency on that external
3822 -- unit. For such a path to be properly discovered and encoded in the
3823 -- ALI file of the main unit, let the ABE mechanism process the body of
3824 -- the main unit, and encode all relevant invocation constructs and the
3825 -- relations between them.
3826
3827 Mark_Save_Invocation_Graph_Of_Body;
3828
3829 -- Create copy of generic unit, and save for instantiation. If the unit
3830 -- is a child unit, do not copy the specifications for the parent, which
3831 -- are not part of the generic tree.
3832
3833 Save_Parent := Parent_Spec (N);
3834 Set_Parent_Spec (N, Empty);
3835
3836 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3837 Set_Parent_Spec (New_N, Save_Parent);
3838 Rewrite (N, New_N);
3839
3840 -- Once the contents of the generic copy and the template are swapped,
3841 -- do the same for their respective aspect specifications.
3842
3843 Exchange_Aspects (N, New_N);
3844
3845 -- Collect all contract-related source pragmas found within the template
3846 -- and attach them to the contract of the subprogram spec. This contract
3847 -- is used in the capture of global references within annotations.
3848
3849 Create_Generic_Contract (N);
3850
3851 Spec := Specification (N);
3852 Id := Defining_Entity (Spec);
3853 Generate_Definition (Id);
3854
3855 if Nkind (Id) = N_Defining_Operator_Symbol then
3856 Error_Msg_N
3857 ("operator symbol not allowed for generic subprogram", Id);
3858 end if;
3859
3860 Start_Generic;
3861
3862 Enter_Name (Id);
3863 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3864
3865 Push_Scope (Id);
3866 Enter_Generic_Scope (Id);
3867 Set_Inner_Instances (Id, New_Elmt_List);
3868 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3869
3870 Analyze_Generic_Formal_Part (N);
3871
3872 if Nkind (Spec) = N_Function_Specification then
3873 Mutate_Ekind (Id, E_Generic_Function);
3874 else
3875 Mutate_Ekind (Id, E_Generic_Procedure);
3876 end if;
3877
3878 -- Set SPARK_Mode from context
3879
3880 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3881 Set_SPARK_Pragma_Inherited (Id);
3882
3883 -- Preserve relevant elaboration-related attributes of the context which
3884 -- are no longer available or very expensive to recompute once analysis,
3885 -- resolution, and expansion are over.
3886
3887 Mark_Elaboration_Attributes
3888 (N_Id => Id,
3889 Checks => True,
3890 Warnings => True);
3891
3892 Formals := Parameter_Specifications (Spec);
3893
3894 if Present (Formals) then
3895 Process_Formals (Formals, Spec);
3896 end if;
3897
3898 if Nkind (Spec) = N_Function_Specification then
3899 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3900 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3901 Set_Etype (Id, Result_Type);
3902
3903 -- Check restriction imposed by AI05-073: a generic function
3904 -- cannot return an abstract type or an access to such.
3905
3906 -- This is a binding interpretation should it apply to earlier
3907 -- versions of Ada as well as Ada 2012???
3908
3909 if Is_Abstract_Type (Designated_Type (Result_Type))
3910 and then Ada_Version >= Ada_2012
3911 then
3912 Error_Msg_N
3913 ("generic function cannot have an access result "
3914 & "that designates an abstract type", Spec);
3915 end if;
3916
3917 else
3918 Find_Type (Result_Definition (Spec));
3919 Typ := Entity (Result_Definition (Spec));
3920
3921 if Is_Abstract_Type (Typ)
3922 and then Ada_Version >= Ada_2012
3923 then
3924 Error_Msg_N
3925 ("generic function cannot have abstract result type", Spec);
3926 end if;
3927
3928 -- If a null exclusion is imposed on the result type, then create
3929 -- a null-excluding itype (an access subtype) and use it as the
3930 -- function's Etype.
3931
3932 if Is_Access_Type (Typ)
3933 and then Null_Exclusion_Present (Spec)
3934 then
3935 Set_Etype (Id,
3936 Create_Null_Excluding_Itype
3937 (T => Typ,
3938 Related_Nod => Spec,
3939 Scope_Id => Defining_Unit_Name (Spec)));
3940 else
3941 Set_Etype (Id, Typ);
3942 end if;
3943 end if;
3944
3945 else
3946 Set_Etype (Id, Standard_Void_Type);
3947 end if;
3948
3949 -- Analyze the aspects of the generic copy to ensure that all generated
3950 -- pragmas (if any) perform their semantic effects.
3951
3952 if Has_Aspects (N) then
3953 Analyze_Aspect_Specifications (N, Id);
3954 end if;
3955
3956 -- For a library unit, we have reconstructed the entity for the unit,
3957 -- and must reset it in the library tables. We also make sure that
3958 -- Body_Required is set properly in the original compilation unit node.
3959
3960 if Nkind (Parent (N)) = N_Compilation_Unit then
3961 Set_Cunit_Entity (Current_Sem_Unit, Id);
3962 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3963 end if;
3964
3965 -- If the generic appears within a package unit, the body of that unit
3966 -- has to be present for instantiation and inlining.
3967
3968 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3969 and then Unit_Requires_Body (Id)
3970 then
3971 Set_Body_Needed_For_Inlining
3972 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3973 end if;
3974
3975 Set_Categorization_From_Pragmas (N);
3976 Validate_Categorization_Dependency (N, Id);
3977
3978 -- Capture all global references that occur within the profile of the
3979 -- generic subprogram. Aspects are not part of this processing because
3980 -- they must be delayed. If processed now, Save_Global_References will
3981 -- destroy the Associated_Node links and prevent the capture of global
3982 -- references when the contract of the generic subprogram is analyzed.
3983
3984 Save_Global_References (Original_Node (N));
3985
3986 End_Generic;
3987 End_Scope;
3988 Exit_Generic_Scope (Id);
3989 Generate_Reference_To_Formals (Id);
3990
3991 List_Inherited_Pre_Post_Aspects (Id);
3992 end Analyze_Generic_Subprogram_Declaration;
3993
3994 -----------------------------------
3995 -- Analyze_Package_Instantiation --
3996 -----------------------------------
3997
3998 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3999 -- must be replaced by gotos which jump to the end of the routine in order
4000 -- to restore the Ghost and SPARK modes.
4001
4002 procedure Analyze_Package_Instantiation (N : Node_Id) is
4003 Has_Inline_Always : Boolean := False;
4004 -- Set if the generic unit contains any subprograms with Inline_Always.
4005 -- Only relevant when back-end inlining is not enabled.
4006
4007 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4008 -- Return True if inlining is active and Gen_Unit contains inlined
4009 -- subprograms. In this case, we may either instantiate the body when
4010 -- front-end inlining is enabled, or add a pending instantiation when
4011 -- back-end inlining is enabled. In the former case, this may cause
4012 -- superfluous instantiations, but in either case we need to perform
4013 -- the instantiation of the body in the context of the instance and
4014 -- not in that of the point of inlining.
4015
4016 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4017 -- Return True if Gen_Unit needs to have its body instantiated in the
4018 -- context of N. This in particular excludes generic contexts.
4019
4020 -----------------------
4021 -- Might_Inline_Subp --
4022 -----------------------
4023
4024 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4025 E : Entity_Id;
4026
4027 begin
4028 if Inline_Processing_Required then
4029 -- No need to recompute the answer if we know it is positive
4030 -- and back-end inlining is enabled.
4031
4032 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4033 return True;
4034 end if;
4035
4036 E := First_Entity (Gen_Unit);
4037 while Present (E) loop
4038 if Is_Subprogram (E) and then Is_Inlined (E) then
4039 -- Remember if there are any subprograms with Inline_Always
4040
4041 if Has_Pragma_Inline_Always (E) then
4042 Has_Inline_Always := True;
4043 end if;
4044
4045 Set_Is_Inlined (Gen_Unit);
4046 return True;
4047 end if;
4048
4049 Next_Entity (E);
4050 end loop;
4051 end if;
4052
4053 return False;
4054 end Might_Inline_Subp;
4055
4056 -------------------------------
4057 -- Needs_Body_Instantiated --
4058 -------------------------------
4059
4060 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4061 begin
4062 -- No need to instantiate bodies in generic units
4063
4064 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4065 return False;
4066 end if;
4067
4068 -- If the instantiation is in the main unit, then the body is needed
4069
4070 if Is_In_Main_Unit (N) then
4071 return True;
4072 end if;
4073
4074 -- In GNATprove mode, never instantiate bodies outside of the main
4075 -- unit, as it does not use frontend/backend inlining in the way that
4076 -- GNAT does, so does not benefit from such instantiations. On the
4077 -- contrary, such instantiations may bring artificial constraints,
4078 -- as for example such bodies may require preprocessing.
4079
4080 if GNATprove_Mode then
4081 return False;
4082 end if;
4083
4084 -- If not, then again no need to instantiate bodies in generic units
4085
4086 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4087 return False;
4088 end if;
4089
4090 -- Here we have a special handling for back-end inlining: if inline
4091 -- processing is required, then we unconditionally want to have the
4092 -- body instantiated. The reason is that Might_Inline_Subp does not
4093 -- catch all the cases (as it does not recurse into nested packages)
4094 -- so this avoids the need to patch things up afterwards. Moreover,
4095 -- these instantiations are only performed on demand when back-end
4096 -- inlining is enabled, so this causes very little extra work.
4097
4098 if Inline_Processing_Required and then Back_End_Inlining then
4099 return True;
4100 end if;
4101
4102 -- We want to have the bodies instantiated in non-main units if
4103 -- they might contribute inlined subprograms.
4104
4105 return Might_Inline_Subp (Gen_Unit);
4106 end Needs_Body_Instantiated;
4107
4108 -- Local declarations
4109
4110 Gen_Id : constant Node_Id := Name (N);
4111 Inst_Id : constant Entity_Id := Defining_Entity (N);
4112 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4113 Loc : constant Source_Ptr := Sloc (N);
4114
4115 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4116 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4117 Saved_ISMP : constant Boolean :=
4118 Ignore_SPARK_Mode_Pragmas_In_Instance;
4119 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4120 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4121 -- Save the Ghost and SPARK mode-related data to restore on exit
4122
4123 Saved_Style_Check : constant Boolean := Style_Check;
4124 -- Save style check mode for restore on exit
4125
4126 Act_Decl : Node_Id;
4127 Act_Decl_Name : Node_Id;
4128 Act_Decl_Id : Entity_Id;
4129 Act_Spec : Node_Id;
4130 Act_Tree : Node_Id;
4131 Env_Installed : Boolean := False;
4132 Gen_Decl : Node_Id;
4133 Gen_Spec : Node_Id;
4134 Gen_Unit : Entity_Id;
4135 Inline_Now : Boolean := False;
4136 Needs_Body : Boolean;
4137 Parent_Installed : Boolean := False;
4138 Renaming_List : List_Id;
4139 Unit_Renaming : Node_Id;
4140
4141 Vis_Prims_List : Elist_Id := No_Elist;
4142 -- List of primitives made temporarily visible in the instantiation
4143 -- to match the visibility of the formal type
4144
4145 -- Start of processing for Analyze_Package_Instantiation
4146
4147 begin
4148 -- Preserve relevant elaboration-related attributes of the context which
4149 -- are no longer available or very expensive to recompute once analysis,
4150 -- resolution, and expansion are over.
4151
4152 Mark_Elaboration_Attributes
4153 (N_Id => N,
4154 Checks => True,
4155 Level => True,
4156 Modes => True,
4157 Warnings => True);
4158
4159 -- Very first thing: check for Text_IO special unit in case we are
4160 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4161
4162 Check_Text_IO_Special_Unit (Name (N));
4163
4164 -- Make node global for error reporting
4165
4166 Instantiation_Node := N;
4167
4168 -- Case of instantiation of a generic package
4169
4170 if Nkind (N) = N_Package_Instantiation then
4171 Act_Decl_Id := New_Copy (Defining_Entity (N));
4172 Set_Comes_From_Source (Act_Decl_Id, True);
4173
4174 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4175 Act_Decl_Name :=
4176 Make_Defining_Program_Unit_Name (Loc,
4177 Name =>
4178 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4179 Defining_Identifier => Act_Decl_Id);
4180 else
4181 Act_Decl_Name := Act_Decl_Id;
4182 end if;
4183
4184 -- Case of instantiation of a formal package
4185
4186 else
4187 Act_Decl_Id := Defining_Identifier (N);
4188 Act_Decl_Name := Act_Decl_Id;
4189 end if;
4190
4191 Generate_Definition (Act_Decl_Id);
4192 Mutate_Ekind (Act_Decl_Id, E_Package);
4193
4194 -- Initialize list of incomplete actuals before analysis
4195
4196 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4197
4198 Preanalyze_Actuals (N, Act_Decl_Id);
4199
4200 -- Turn off style checking in instances. If the check is enabled on the
4201 -- generic unit, a warning in an instance would just be noise. If not
4202 -- enabled on the generic, then a warning in an instance is just wrong.
4203 -- This must be done after analyzing the actuals, which do come from
4204 -- source and are subject to style checking.
4205
4206 Style_Check := False;
4207
4208 Init_Env;
4209 Env_Installed := True;
4210
4211 -- Reset renaming map for formal types. The mapping is established
4212 -- when analyzing the generic associations, but some mappings are
4213 -- inherited from formal packages of parent units, and these are
4214 -- constructed when the parents are installed.
4215
4216 Generic_Renamings.Set_Last (0);
4217 Generic_Renamings_HTable.Reset;
4218
4219 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4220 Gen_Unit := Entity (Gen_Id);
4221
4222 -- A package instantiation is Ghost when it is subject to pragma Ghost
4223 -- or the generic template is Ghost. Set the mode now to ensure that
4224 -- any nodes generated during analysis and expansion are marked as
4225 -- Ghost.
4226
4227 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4228
4229 -- Verify that it is the name of a generic package
4230
4231 -- A visibility glitch: if the instance is a child unit and the generic
4232 -- is the generic unit of a parent instance (i.e. both the parent and
4233 -- the child units are instances of the same package) the name now
4234 -- denotes the renaming within the parent, not the intended generic
4235 -- unit. See if there is a homonym that is the desired generic. The
4236 -- renaming declaration must be visible inside the instance of the
4237 -- child, but not when analyzing the name in the instantiation itself.
4238
4239 if Ekind (Gen_Unit) = E_Package
4240 and then Present (Renamed_Entity (Gen_Unit))
4241 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4242 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4243 and then Present (Homonym (Gen_Unit))
4244 then
4245 Gen_Unit := Homonym (Gen_Unit);
4246 end if;
4247
4248 if Etype (Gen_Unit) = Any_Type then
4249 Restore_Env;
4250 goto Leave;
4251
4252 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4253
4254 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4255
4256 if From_Limited_With (Gen_Unit) then
4257 Error_Msg_N
4258 ("cannot instantiate a limited withed package", Gen_Id);
4259 else
4260 Error_Msg_NE
4261 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4262 end if;
4263
4264 Restore_Env;
4265 goto Leave;
4266 end if;
4267
4268 if In_Extended_Main_Source_Unit (N) then
4269 Set_Is_Instantiated (Gen_Unit);
4270 Generate_Reference (Gen_Unit, N);
4271
4272 if Present (Renamed_Object (Gen_Unit)) then
4273 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4274 Generate_Reference (Renamed_Object (Gen_Unit), N);
4275 end if;
4276 end if;
4277
4278 if Nkind (Gen_Id) = N_Identifier
4279 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4280 then
4281 Error_Msg_NE
4282 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4283
4284 elsif Nkind (Gen_Id) = N_Expanded_Name
4285 and then Is_Child_Unit (Gen_Unit)
4286 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4287 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4288 then
4289 Error_Msg_N
4290 ("& is hidden within declaration of instance", Prefix (Gen_Id));
4291 end if;
4292
4293 Set_Entity (Gen_Id, Gen_Unit);
4294
4295 -- If generic is a renaming, get original generic unit
4296
4297 if Present (Renamed_Object (Gen_Unit))
4298 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4299 then
4300 Gen_Unit := Renamed_Object (Gen_Unit);
4301 end if;
4302
4303 -- Verify that there are no circular instantiations
4304
4305 if In_Open_Scopes (Gen_Unit) then
4306 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4307 Restore_Env;
4308 goto Leave;
4309
4310 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4311 Error_Msg_Node_2 := Current_Scope;
4312 Error_Msg_NE
4313 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
4314 Circularity_Detected := True;
4315 Restore_Env;
4316 goto Leave;
4317
4318 else
4319 Mutate_Ekind (Inst_Id, E_Package);
4320 Set_Scope (Inst_Id, Current_Scope);
4321
4322 -- If the context of the instance is subject to SPARK_Mode "off" or
4323 -- the annotation is altogether missing, set the global flag which
4324 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4325 -- the instance.
4326
4327 if SPARK_Mode /= On then
4328 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4329
4330 -- Mark the instance spec in case the body is instantiated at a
4331 -- later pass. This preserves the original context in effect for
4332 -- the body.
4333
4334 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4335 end if;
4336
4337 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4338 Gen_Spec := Specification (Gen_Decl);
4339
4340 -- Initialize renamings map, for error checking, and the list that
4341 -- holds private entities whose views have changed between generic
4342 -- definition and instantiation. If this is the instance created to
4343 -- validate an actual package, the instantiation environment is that
4344 -- of the enclosing instance.
4345
4346 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4347
4348 -- Copy original generic tree, to produce text for instantiation
4349
4350 Act_Tree :=
4351 Copy_Generic_Node
4352 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4353
4354 Act_Spec := Specification (Act_Tree);
4355
4356 -- If this is the instance created to validate an actual package,
4357 -- only the formals matter, do not examine the package spec itself.
4358
4359 if Is_Actual_Pack then
4360 Set_Visible_Declarations (Act_Spec, New_List);
4361 Set_Private_Declarations (Act_Spec, New_List);
4362 end if;
4363
4364 Renaming_List :=
4365 Analyze_Associations
4366 (I_Node => N,
4367 Formals => Generic_Formal_Declarations (Act_Tree),
4368 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4369
4370 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4371
4372 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4373 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4374 Set_Is_Generic_Instance (Act_Decl_Id);
4375 Set_Generic_Parent (Act_Spec, Gen_Unit);
4376
4377 -- References to the generic in its own declaration or its body are
4378 -- references to the instance. Add a renaming declaration for the
4379 -- generic unit itself. This declaration, as well as the renaming
4380 -- declarations for the generic formals, must remain private to the
4381 -- unit: the formals, because this is the language semantics, and
4382 -- the unit because its use is an artifact of the implementation.
4383
4384 Unit_Renaming :=
4385 Make_Package_Renaming_Declaration (Loc,
4386 Defining_Unit_Name =>
4387 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4388 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4389
4390 Append (Unit_Renaming, Renaming_List);
4391
4392 -- The renaming declarations are the first local declarations of the
4393 -- new unit.
4394
4395 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4396 Insert_List_Before
4397 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4398 else
4399 Set_Visible_Declarations (Act_Spec, Renaming_List);
4400 end if;
4401
4402 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4403
4404 -- Propagate the aspect specifications from the package declaration
4405 -- template to the instantiated version of the package declaration.
4406
4407 if Has_Aspects (Act_Tree) then
4408 Set_Aspect_Specifications (Act_Decl,
4409 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4410 end if;
4411
4412 -- The generic may have a generated Default_Storage_Pool aspect,
4413 -- set at the point of generic declaration. If the instance has
4414 -- that aspect, it overrides the one inherited from the generic.
4415
4416 if Has_Aspects (Gen_Spec) then
4417 if No (Aspect_Specifications (N)) then
4418 Set_Aspect_Specifications (N,
4419 (New_Copy_List_Tree
4420 (Aspect_Specifications (Gen_Spec))));
4421
4422 else
4423 declare
4424 Inherited_Aspects : constant List_Id :=
4425 New_Copy_List_Tree
4426 (Aspect_Specifications (Gen_Spec));
4427
4428 ASN1 : Node_Id;
4429 ASN2 : Node_Id;
4430 Pool_Present : Boolean := False;
4431
4432 begin
4433 ASN1 := First (Aspect_Specifications (N));
4434 while Present (ASN1) loop
4435 if Chars (Identifier (ASN1)) =
4436 Name_Default_Storage_Pool
4437 then
4438 Pool_Present := True;
4439 exit;
4440 end if;
4441
4442 Next (ASN1);
4443 end loop;
4444
4445 if Pool_Present then
4446
4447 -- If generic carries a default storage pool, remove it
4448 -- in favor of the instance one.
4449
4450 ASN2 := First (Inherited_Aspects);
4451 while Present (ASN2) loop
4452 if Chars (Identifier (ASN2)) =
4453 Name_Default_Storage_Pool
4454 then
4455 Remove (ASN2);
4456 exit;
4457 end if;
4458
4459 Next (ASN2);
4460 end loop;
4461 end if;
4462
4463 Prepend_List_To
4464 (Aspect_Specifications (N), Inherited_Aspects);
4465 end;
4466 end if;
4467 end if;
4468
4469 -- Save the instantiation node for a subsequent instantiation of the
4470 -- body if there is one and it needs to be instantiated here.
4471
4472 -- We instantiate the body only if we are generating code, or if we
4473 -- are generating cross-reference information, or for GNATprove use.
4474
4475 declare
4476 Enclosing_Body_Present : Boolean := False;
4477 -- If the generic unit is not a compilation unit, then a body may
4478 -- be present in its parent even if none is required. We create a
4479 -- tentative pending instantiation for the body, which will be
4480 -- discarded if none is actually present.
4481
4482 Scop : Entity_Id;
4483
4484 begin
4485 if Scope (Gen_Unit) /= Standard_Standard
4486 and then not Is_Child_Unit (Gen_Unit)
4487 then
4488 Scop := Scope (Gen_Unit);
4489 while Present (Scop) and then Scop /= Standard_Standard loop
4490 if Unit_Requires_Body (Scop) then
4491 Enclosing_Body_Present := True;
4492 exit;
4493
4494 elsif In_Open_Scopes (Scop)
4495 and then In_Package_Body (Scop)
4496 then
4497 Enclosing_Body_Present := True;
4498 exit;
4499 end if;
4500
4501 exit when Is_Compilation_Unit (Scop);
4502 Scop := Scope (Scop);
4503 end loop;
4504 end if;
4505
4506 -- If front-end inlining is enabled or there are any subprograms
4507 -- marked with Inline_Always, and this is a unit for which code
4508 -- will be generated, we instantiate the body at once.
4509
4510 -- This is done if the instance is not the main unit, and if the
4511 -- generic is not a child unit of another generic, to avoid scope
4512 -- problems and the reinstallation of parent instances.
4513
4514 if Expander_Active
4515 and then (not Is_Child_Unit (Gen_Unit)
4516 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4517 and then Might_Inline_Subp (Gen_Unit)
4518 and then not Is_Actual_Pack
4519 then
4520 if not Back_End_Inlining
4521 and then (Front_End_Inlining or else Has_Inline_Always)
4522 and then (Is_In_Main_Unit (N)
4523 or else In_Main_Context (Current_Scope))
4524 and then Nkind (Parent (N)) /= N_Compilation_Unit
4525 then
4526 Inline_Now := True;
4527
4528 -- In configurable_run_time mode we force the inlining of
4529 -- predefined subprograms marked Inline_Always, to minimize
4530 -- the use of the run-time library.
4531
4532 elsif In_Predefined_Unit (Gen_Decl)
4533 and then Configurable_Run_Time_Mode
4534 and then Nkind (Parent (N)) /= N_Compilation_Unit
4535 then
4536 Inline_Now := True;
4537 end if;
4538
4539 -- If the current scope is itself an instance within a child
4540 -- unit, there will be duplications in the scope stack, and the
4541 -- unstacking mechanism in Inline_Instance_Body will fail.
4542 -- This loses some rare cases of optimization, and might be
4543 -- improved some day, if we can find a proper abstraction for
4544 -- "the complete compilation context" that can be saved and
4545 -- restored. ???
4546
4547 if Is_Generic_Instance (Current_Scope) then
4548 declare
4549 Curr_Unit : constant Entity_Id :=
4550 Cunit_Entity (Current_Sem_Unit);
4551 begin
4552 if Curr_Unit /= Current_Scope
4553 and then Is_Child_Unit (Curr_Unit)
4554 then
4555 Inline_Now := False;
4556 end if;
4557 end;
4558 end if;
4559 end if;
4560
4561 Needs_Body :=
4562 (Unit_Requires_Body (Gen_Unit)
4563 or else Enclosing_Body_Present
4564 or else Present (Corresponding_Body (Gen_Decl)))
4565 and then Needs_Body_Instantiated (Gen_Unit)
4566 and then not Is_Actual_Pack
4567 and then not Inline_Now
4568 and then (Operating_Mode = Generate_Code
4569 or else (Operating_Mode = Check_Semantics
4570 and then GNATprove_Mode));
4571
4572 -- If front-end inlining is enabled or there are any subprograms
4573 -- marked with Inline_Always, do not instantiate body when within
4574 -- a generic context.
4575
4576 if not Back_End_Inlining
4577 and then (Front_End_Inlining or else Has_Inline_Always)
4578 and then not Expander_Active
4579 then
4580 Needs_Body := False;
4581 end if;
4582
4583 -- If the current context is generic, and the package being
4584 -- instantiated is declared within a formal package, there is no
4585 -- body to instantiate until the enclosing generic is instantiated
4586 -- and there is an actual for the formal package. If the formal
4587 -- package has parameters, we build a regular package instance for
4588 -- it, that precedes the original formal package declaration.
4589
4590 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4591 declare
4592 Decl : constant Node_Id :=
4593 Original_Node
4594 (Unit_Declaration_Node (Scope (Gen_Unit)));
4595 begin
4596 if Nkind (Decl) = N_Formal_Package_Declaration
4597 or else (Nkind (Decl) = N_Package_Declaration
4598 and then Is_List_Member (Decl)
4599 and then Present (Next (Decl))
4600 and then
4601 Nkind (Next (Decl)) =
4602 N_Formal_Package_Declaration)
4603 then
4604 Needs_Body := False;
4605 end if;
4606 end;
4607 end if;
4608 end;
4609
4610 -- For RCI unit calling stubs, we omit the instance body if the
4611 -- instance is the RCI library unit itself.
4612
4613 -- However there is a special case for nested instances: in this case
4614 -- we do generate the instance body, as it might be required, e.g.
4615 -- because it provides stream attributes for some type used in the
4616 -- profile of a remote subprogram. This is consistent with 12.3(12),
4617 -- which indicates that the instance body occurs at the place of the
4618 -- instantiation, and thus is part of the RCI declaration, which is
4619 -- present on all client partitions (this is E.2.3(18)).
4620
4621 -- Note that AI12-0002 may make it illegal at some point to have
4622 -- stream attributes defined in an RCI unit, in which case this
4623 -- special case will become unnecessary. In the meantime, there
4624 -- is known application code in production that depends on this
4625 -- being possible, so we definitely cannot eliminate the body in
4626 -- the case of nested instances for the time being.
4627
4628 -- When we generate a nested instance body, calling stubs for any
4629 -- relevant subprogram will be inserted immediately after the
4630 -- subprogram declarations, and will take precedence over the
4631 -- subsequent (original) body. (The stub and original body will be
4632 -- complete homographs, but this is permitted in an instance).
4633 -- (Could we do better and remove the original body???)
4634
4635 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4636 and then Comes_From_Source (N)
4637 and then Nkind (Parent (N)) = N_Compilation_Unit
4638 then
4639 Needs_Body := False;
4640 end if;
4641
4642 if Needs_Body then
4643 -- Indicate that the enclosing scopes contain an instantiation,
4644 -- and that cleanup actions should be delayed until after the
4645 -- instance body is expanded.
4646
4647 Check_Forward_Instantiation (Gen_Decl);
4648 if Nkind (N) = N_Package_Instantiation then
4649 declare
4650 Enclosing_Master : Entity_Id;
4651
4652 begin
4653 -- Loop to search enclosing masters
4654
4655 Enclosing_Master := Current_Scope;
4656 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4657 if Ekind (Enclosing_Master) = E_Package then
4658 if Is_Compilation_Unit (Enclosing_Master) then
4659 if In_Package_Body (Enclosing_Master) then
4660 Set_Delay_Subprogram_Descriptors
4661 (Body_Entity (Enclosing_Master));
4662 else
4663 Set_Delay_Subprogram_Descriptors
4664 (Enclosing_Master);
4665 end if;
4666
4667 exit Scope_Loop;
4668
4669 else
4670 Enclosing_Master := Scope (Enclosing_Master);
4671 end if;
4672
4673 elsif Is_Generic_Unit (Enclosing_Master)
4674 or else Ekind (Enclosing_Master) = E_Void
4675 then
4676 -- Cleanup actions will eventually be performed on the
4677 -- enclosing subprogram or package instance, if any.
4678 -- Enclosing scope is void in the formal part of a
4679 -- generic subprogram.
4680
4681 exit Scope_Loop;
4682
4683 else
4684 if Ekind (Enclosing_Master) = E_Entry
4685 and then
4686 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4687 then
4688 if not Expander_Active then
4689 exit Scope_Loop;
4690 else
4691 Enclosing_Master :=
4692 Protected_Body_Subprogram (Enclosing_Master);
4693 end if;
4694 end if;
4695
4696 Set_Delay_Cleanups (Enclosing_Master);
4697
4698 while Ekind (Enclosing_Master) = E_Block loop
4699 Enclosing_Master := Scope (Enclosing_Master);
4700 end loop;
4701
4702 if Is_Subprogram (Enclosing_Master) then
4703 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4704
4705 elsif Is_Task_Type (Enclosing_Master) then
4706 declare
4707 TBP : constant Node_Id :=
4708 Get_Task_Body_Procedure
4709 (Enclosing_Master);
4710 begin
4711 if Present (TBP) then
4712 Set_Delay_Subprogram_Descriptors (TBP);
4713 Set_Delay_Cleanups (TBP);
4714 end if;
4715 end;
4716 end if;
4717
4718 exit Scope_Loop;
4719 end if;
4720 end loop Scope_Loop;
4721 end;
4722
4723 -- Make entry in table
4724
4725 Add_Pending_Instantiation (N, Act_Decl);
4726 end if;
4727 end if;
4728
4729 Set_Categorization_From_Pragmas (Act_Decl);
4730
4731 if Parent_Installed then
4732 Hide_Current_Scope;
4733 end if;
4734
4735 Set_Instance_Spec (N, Act_Decl);
4736
4737 -- If not a compilation unit, insert the package declaration before
4738 -- the original instantiation node.
4739
4740 if Nkind (Parent (N)) /= N_Compilation_Unit then
4741 Mark_Rewrite_Insertion (Act_Decl);
4742 Insert_Before (N, Act_Decl);
4743
4744 if Has_Aspects (N) then
4745 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4746
4747 -- The pragma created for a Default_Storage_Pool aspect must
4748 -- appear ahead of the declarations in the instance spec.
4749 -- Analysis has placed it after the instance node, so remove
4750 -- it and reinsert it properly now.
4751
4752 declare
4753 ASN : constant Node_Id := First (Aspect_Specifications (N));
4754 A_Name : constant Name_Id := Chars (Identifier (ASN));
4755 Decl : Node_Id;
4756
4757 begin
4758 if A_Name = Name_Default_Storage_Pool then
4759 if No (Visible_Declarations (Act_Spec)) then
4760 Set_Visible_Declarations (Act_Spec, New_List);
4761 end if;
4762
4763 Decl := Next (N);
4764 while Present (Decl) loop
4765 if Nkind (Decl) = N_Pragma then
4766 Remove (Decl);
4767 Prepend (Decl, Visible_Declarations (Act_Spec));
4768 exit;
4769 end if;
4770
4771 Next (Decl);
4772 end loop;
4773 end if;
4774 end;
4775 end if;
4776
4777 Analyze (Act_Decl);
4778
4779 -- For an instantiation that is a compilation unit, place
4780 -- declaration on current node so context is complete for analysis
4781 -- (including nested instantiations). If this is the main unit,
4782 -- the declaration eventually replaces the instantiation node.
4783 -- If the instance body is created later, it replaces the
4784 -- instance node, and the declaration is attached to it
4785 -- (see Build_Instance_Compilation_Unit_Nodes).
4786
4787 else
4788 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4789
4790 -- The entity for the current unit is the newly created one,
4791 -- and all semantic information is attached to it.
4792
4793 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4794
4795 -- If this is the main unit, replace the main entity as well
4796
4797 if Current_Sem_Unit = Main_Unit then
4798 Main_Unit_Entity := Act_Decl_Id;
4799 end if;
4800 end if;
4801
4802 Set_Unit (Parent (N), Act_Decl);
4803 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4804 Set_Package_Instantiation (Act_Decl_Id, N);
4805
4806 -- Process aspect specifications of the instance node, if any, to
4807 -- take into account categorization pragmas before analyzing the
4808 -- instance.
4809
4810 if Has_Aspects (N) then
4811 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4812 end if;
4813
4814 Analyze (Act_Decl);
4815 Set_Unit (Parent (N), N);
4816 Set_Body_Required (Parent (N), False);
4817
4818 -- We never need elaboration checks on instantiations, since by
4819 -- definition, the body instantiation is elaborated at the same
4820 -- time as the spec instantiation.
4821
4822 if Legacy_Elaboration_Checks then
4823 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4824 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4825 end if;
4826 end if;
4827
4828 if Legacy_Elaboration_Checks then
4829 Check_Elab_Instantiation (N);
4830 end if;
4831
4832 -- Save the scenario for later examination by the ABE Processing
4833 -- phase.
4834
4835 Record_Elaboration_Scenario (N);
4836
4837 -- The instantiation results in a guaranteed ABE
4838
4839 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4840 -- Do not instantiate the corresponding body because gigi cannot
4841 -- handle certain types of premature instantiations.
4842
4843 Remove_Dead_Instance (N);
4844
4845 -- Create completing bodies for all subprogram declarations since
4846 -- their real bodies will not be instantiated.
4847
4848 Provide_Completing_Bodies (Instance_Spec (N));
4849 end if;
4850
4851 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4852
4853 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4854 First_Private_Entity (Act_Decl_Id));
4855
4856 -- If the instantiation will receive a body, the unit will be
4857 -- transformed into a package body, and receive its own elaboration
4858 -- entity. Otherwise, the nature of the unit is now a package
4859 -- declaration.
4860
4861 if Nkind (Parent (N)) = N_Compilation_Unit
4862 and then not Needs_Body
4863 then
4864 Rewrite (N, Act_Decl);
4865 end if;
4866
4867 if Present (Corresponding_Body (Gen_Decl))
4868 or else Unit_Requires_Body (Gen_Unit)
4869 then
4870 Set_Has_Completion (Act_Decl_Id);
4871 end if;
4872
4873 Check_Formal_Packages (Act_Decl_Id);
4874
4875 Restore_Hidden_Primitives (Vis_Prims_List);
4876 Restore_Private_Views (Act_Decl_Id);
4877
4878 Inherit_Context (Gen_Decl, N);
4879
4880 if Parent_Installed then
4881 Remove_Parent;
4882 end if;
4883
4884 Restore_Env;
4885 Env_Installed := False;
4886 end if;
4887
4888 Validate_Categorization_Dependency (N, Act_Decl_Id);
4889
4890 -- There used to be a check here to prevent instantiations in local
4891 -- contexts if the No_Local_Allocators restriction was active. This
4892 -- check was removed by a binding interpretation in AI-95-00130/07,
4893 -- but we retain the code for documentation purposes.
4894
4895 -- if Ekind (Act_Decl_Id) /= E_Void
4896 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4897 -- then
4898 -- Check_Restriction (No_Local_Allocators, N);
4899 -- end if;
4900
4901 if Inline_Now then
4902 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4903 end if;
4904
4905 -- Check that if N is an instantiation of System.Dim_Float_IO or
4906 -- System.Dim_Integer_IO, the formal type has a dimension system.
4907
4908 if Nkind (N) = N_Package_Instantiation
4909 and then Is_Dim_IO_Package_Instantiation (N)
4910 then
4911 declare
4912 Assoc : constant Node_Id := First (Generic_Associations (N));
4913 begin
4914 if not Has_Dimension_System
4915 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4916 then
4917 Error_Msg_N ("type with a dimension system expected", Assoc);
4918 end if;
4919 end;
4920 end if;
4921
4922 <<Leave>>
4923 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4924 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4925 end if;
4926
4927 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4928 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4929 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4930 Style_Check := Saved_Style_Check;
4931
4932 exception
4933 when Instantiation_Error =>
4934 if Parent_Installed then
4935 Remove_Parent;
4936 end if;
4937
4938 if Env_Installed then
4939 Restore_Env;
4940 end if;
4941
4942 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4943 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4944 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4945 Style_Check := Saved_Style_Check;
4946 end Analyze_Package_Instantiation;
4947
4948 --------------------------
4949 -- Inline_Instance_Body --
4950 --------------------------
4951
4952 -- WARNING: This routine manages SPARK regions. Return statements must be
4953 -- replaced by gotos which jump to the end of the routine and restore the
4954 -- SPARK mode.
4955
4956 procedure Inline_Instance_Body
4957 (N : Node_Id;
4958 Gen_Unit : Entity_Id;
4959 Act_Decl : Node_Id)
4960 is
4961 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4962
4963 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4964 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4965 Gen_Comp : constant Entity_Id :=
4966 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4967
4968 Scope_Stack_Depth : constant Pos :=
4969 Scope_Stack.Last - Scope_Stack.First + 1;
4970
4971 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4972 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4973 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4974
4975 Curr_Scope : Entity_Id := Empty;
4976 List : Elist_Id := No_Elist; -- init to avoid warning
4977 N_Instances : Nat := 0;
4978 Num_Inner : Nat := 0;
4979 Num_Scopes : Nat := 0;
4980 Removed : Boolean := False;
4981 S : Entity_Id;
4982 Vis : Boolean;
4983
4984 begin
4985 -- Case of generic unit defined in another unit. We must remove the
4986 -- complete context of the current unit to install that of the generic.
4987
4988 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4989
4990 -- Add some comments for the following two loops ???
4991
4992 S := Current_Scope;
4993 while Present (S) and then S /= Standard_Standard loop
4994 loop
4995 Num_Scopes := Num_Scopes + 1;
4996
4997 Use_Clauses (Num_Scopes) :=
4998 (Scope_Stack.Table
4999 (Scope_Stack.Last - Num_Scopes + 1).
5000 First_Use_Clause);
5001 End_Use_Clauses (Use_Clauses (Num_Scopes));
5002
5003 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5004 or else Scope_Stack.Table
5005 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5006 end loop;
5007
5008 exit when Is_Generic_Instance (S)
5009 and then (In_Package_Body (S)
5010 or else Ekind (S) = E_Procedure
5011 or else Ekind (S) = E_Function);
5012 S := Scope (S);
5013 end loop;
5014
5015 Vis := Is_Immediately_Visible (Gen_Comp);
5016
5017 -- Find and save all enclosing instances
5018
5019 S := Current_Scope;
5020
5021 while Present (S)
5022 and then S /= Standard_Standard
5023 loop
5024 if Is_Generic_Instance (S) then
5025 N_Instances := N_Instances + 1;
5026 Instances (N_Instances) := S;
5027
5028 exit when In_Package_Body (S);
5029 end if;
5030
5031 S := Scope (S);
5032 end loop;
5033
5034 -- Remove context of current compilation unit, unless we are within a
5035 -- nested package instantiation, in which case the context has been
5036 -- removed previously.
5037
5038 -- If current scope is the body of a child unit, remove context of
5039 -- spec as well. If an enclosing scope is an instance body, the
5040 -- context has already been removed, but the entities in the body
5041 -- must be made invisible as well.
5042
5043 S := Current_Scope;
5044 while Present (S) and then S /= Standard_Standard loop
5045 if Is_Generic_Instance (S)
5046 and then (In_Package_Body (S)
5047 or else Ekind (S) in E_Procedure | E_Function)
5048 then
5049 -- We still have to remove the entities of the enclosing
5050 -- instance from direct visibility.
5051
5052 declare
5053 E : Entity_Id;
5054 begin
5055 E := First_Entity (S);
5056 while Present (E) loop
5057 Set_Is_Immediately_Visible (E, False);
5058 Next_Entity (E);
5059 end loop;
5060 end;
5061
5062 exit;
5063 end if;
5064
5065 if S = Curr_Unit
5066 or else (Ekind (Curr_Unit) = E_Package_Body
5067 and then S = Spec_Entity (Curr_Unit))
5068 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5069 and then S = Corresponding_Spec
5070 (Unit_Declaration_Node (Curr_Unit)))
5071 then
5072 Removed := True;
5073
5074 -- Remove entities in current scopes from visibility, so that
5075 -- instance body is compiled in a clean environment.
5076
5077 List := Save_Scope_Stack (Handle_Use => False);
5078
5079 if Is_Child_Unit (S) then
5080
5081 -- Remove child unit from stack, as well as inner scopes.
5082 -- Removing the context of a child unit removes parent units
5083 -- as well.
5084
5085 while Current_Scope /= S loop
5086 Num_Inner := Num_Inner + 1;
5087 Inner_Scopes (Num_Inner) := Current_Scope;
5088 Pop_Scope;
5089 end loop;
5090
5091 Pop_Scope;
5092 Remove_Context (Curr_Comp);
5093 Curr_Scope := S;
5094
5095 else
5096 Remove_Context (Curr_Comp);
5097 end if;
5098
5099 if Ekind (Curr_Unit) = E_Package_Body then
5100 Remove_Context (Library_Unit (Curr_Comp));
5101 end if;
5102 end if;
5103
5104 S := Scope (S);
5105 end loop;
5106
5107 pragma Assert (Num_Inner < Num_Scopes);
5108
5109 Push_Scope (Standard_Standard);
5110 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5111
5112 -- The inlined package body is analyzed with the configuration state
5113 -- of the context prior to the scope manipulations performed above.
5114
5115 -- ??? shouldn't this also use the warning state of the context prior
5116 -- to the scope manipulations?
5117
5118 Instantiate_Package_Body
5119 (Body_Info =>
5120 ((Act_Decl => Act_Decl,
5121 Config_Switches => Config_Attrs,
5122 Current_Sem_Unit => Current_Sem_Unit,
5123 Expander_Status => Expander_Active,
5124 Inst_Node => N,
5125 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5126 Scope_Suppress => Scope_Suppress,
5127 Warnings => Save_Warnings)),
5128 Inlined_Body => True);
5129
5130 Pop_Scope;
5131
5132 -- Restore context
5133
5134 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5135
5136 -- Reset Generic_Instance flag so that use clauses can be installed
5137 -- in the proper order. (See Use_One_Package for effect of enclosing
5138 -- instances on processing of use clauses).
5139
5140 for J in 1 .. N_Instances loop
5141 Set_Is_Generic_Instance (Instances (J), False);
5142 end loop;
5143
5144 if Removed then
5145 Install_Context (Curr_Comp, Chain => False);
5146
5147 if Present (Curr_Scope)
5148 and then Is_Child_Unit (Curr_Scope)
5149 then
5150 Push_Scope (Curr_Scope);
5151 Set_Is_Immediately_Visible (Curr_Scope);
5152
5153 -- Finally, restore inner scopes as well
5154
5155 for J in reverse 1 .. Num_Inner loop
5156 Push_Scope (Inner_Scopes (J));
5157 end loop;
5158 end if;
5159
5160 Restore_Scope_Stack (List, Handle_Use => False);
5161
5162 if Present (Curr_Scope)
5163 and then
5164 (In_Private_Part (Curr_Scope)
5165 or else In_Package_Body (Curr_Scope))
5166 then
5167 -- Install private declaration of ancestor units, which are
5168 -- currently available. Restore_Scope_Stack and Install_Context
5169 -- only install the visible part of parents.
5170
5171 declare
5172 Par : Entity_Id;
5173 begin
5174 Par := Scope (Curr_Scope);
5175 while (Present (Par)) and then Par /= Standard_Standard loop
5176 Install_Private_Declarations (Par);
5177 Par := Scope (Par);
5178 end loop;
5179 end;
5180 end if;
5181 end if;
5182
5183 -- Restore use clauses. For a child unit, use clauses in the parents
5184 -- are restored when installing the context, so only those in inner
5185 -- scopes (and those local to the child unit itself) need to be
5186 -- installed explicitly.
5187
5188 if Is_Child_Unit (Curr_Unit) and then Removed then
5189 for J in reverse 1 .. Num_Inner + 1 loop
5190 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5191 Use_Clauses (J);
5192 Install_Use_Clauses (Use_Clauses (J));
5193 end loop;
5194
5195 else
5196 for J in reverse 1 .. Num_Scopes loop
5197 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5198 Use_Clauses (J);
5199 Install_Use_Clauses (Use_Clauses (J));
5200 end loop;
5201 end if;
5202
5203 -- Restore status of instances. If one of them is a body, make its
5204 -- local entities visible again.
5205
5206 declare
5207 E : Entity_Id;
5208 Inst : Entity_Id;
5209
5210 begin
5211 for J in 1 .. N_Instances loop
5212 Inst := Instances (J);
5213 Set_Is_Generic_Instance (Inst, True);
5214
5215 if In_Package_Body (Inst)
5216 or else Ekind (S) in E_Procedure | E_Function
5217 then
5218 E := First_Entity (Instances (J));
5219 while Present (E) loop
5220 Set_Is_Immediately_Visible (E);
5221 Next_Entity (E);
5222 end loop;
5223 end if;
5224 end loop;
5225 end;
5226
5227 -- If generic unit is in current unit, current context is correct. Note
5228 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5229 -- enclosing scopes were removed.
5230
5231 else
5232 Instantiate_Package_Body
5233 (Body_Info =>
5234 ((Act_Decl => Act_Decl,
5235 Config_Switches => Save_Config_Switches,
5236 Current_Sem_Unit => Current_Sem_Unit,
5237 Expander_Status => Expander_Active,
5238 Inst_Node => N,
5239 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5240 Scope_Suppress => Scope_Suppress,
5241 Warnings => Save_Warnings)),
5242 Inlined_Body => True);
5243 end if;
5244 end Inline_Instance_Body;
5245
5246 -------------------------------------
5247 -- Analyze_Procedure_Instantiation --
5248 -------------------------------------
5249
5250 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5251 begin
5252 Analyze_Subprogram_Instantiation (N, E_Procedure);
5253 end Analyze_Procedure_Instantiation;
5254
5255 -----------------------------------
5256 -- Need_Subprogram_Instance_Body --
5257 -----------------------------------
5258
5259 function Need_Subprogram_Instance_Body
5260 (N : Node_Id;
5261 Subp : Entity_Id) return Boolean
5262 is
5263 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5264 -- Return True if E is an inlined subprogram, an inlined renaming or a
5265 -- subprogram nested in an inlined subprogram. The inlining machinery
5266 -- totally disregards nested subprograms since it considers that they
5267 -- will always be compiled if the parent is (see Inline.Is_Nested).
5268
5269 ------------------------------------
5270 -- Is_Inlined_Or_Child_Of_Inlined --
5271 ------------------------------------
5272
5273 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5274 Scop : Entity_Id;
5275
5276 begin
5277 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5278 return True;
5279 end if;
5280
5281 Scop := Scope (E);
5282 while Scop /= Standard_Standard loop
5283 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5284 return True;
5285 end if;
5286
5287 Scop := Scope (Scop);
5288 end loop;
5289
5290 return False;
5291 end Is_Inlined_Or_Child_Of_Inlined;
5292
5293 begin
5294 -- Must be in the main unit or inlined (or child of inlined)
5295
5296 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5297
5298 -- Must be generating code or analyzing code in GNATprove mode
5299
5300 and then (Operating_Mode = Generate_Code
5301 or else (Operating_Mode = Check_Semantics
5302 and then GNATprove_Mode))
5303
5304 -- The body is needed when generating code (full expansion) and in
5305 -- in GNATprove mode (special expansion) for formal verification of
5306 -- the body itself.
5307
5308 and then (Expander_Active or GNATprove_Mode)
5309
5310 -- No point in inlining if ABE is inevitable
5311
5312 and then not Is_Known_Guaranteed_ABE (N)
5313
5314 -- Or if subprogram is eliminated
5315
5316 and then not Is_Eliminated (Subp)
5317 then
5318 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5319 return True;
5320
5321 -- Here if not inlined, or we ignore the inlining
5322
5323 else
5324 return False;
5325 end if;
5326 end Need_Subprogram_Instance_Body;
5327
5328 --------------------------------------
5329 -- Analyze_Subprogram_Instantiation --
5330 --------------------------------------
5331
5332 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5333 -- must be replaced by gotos which jump to the end of the routine in order
5334 -- to restore the Ghost and SPARK modes.
5335
5336 procedure Analyze_Subprogram_Instantiation
5337 (N : Node_Id;
5338 K : Entity_Kind)
5339 is
5340 Errs : constant Nat := Serious_Errors_Detected;
5341 Gen_Id : constant Node_Id := Name (N);
5342 Inst_Id : constant Entity_Id := Defining_Entity (N);
5343 Anon_Id : constant Entity_Id :=
5344 Make_Defining_Identifier (Sloc (Inst_Id),
5345 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5346 Loc : constant Source_Ptr := Sloc (N);
5347
5348 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5349 Act_Decl : Node_Id;
5350 Act_Spec : Node_Id;
5351 Act_Tree : Node_Id;
5352
5353 Env_Installed : Boolean := False;
5354 Gen_Unit : Entity_Id;
5355 Gen_Decl : Node_Id;
5356 Pack_Id : Entity_Id;
5357 Parent_Installed : Boolean := False;
5358
5359 Renaming_List : List_Id;
5360 -- The list of declarations that link formals and actuals of the
5361 -- instance. These are subtype declarations for formal types, and
5362 -- renaming declarations for other formals. The subprogram declaration
5363 -- for the instance is then appended to the list, and the last item on
5364 -- the list is the renaming declaration for the instance.
5365
5366 procedure Analyze_Instance_And_Renamings;
5367 -- The instance must be analyzed in a context that includes the mappings
5368 -- of generic parameters into actuals. We create a package declaration
5369 -- for this purpose, and a subprogram with an internal name within the
5370 -- package. The subprogram instance is simply an alias for the internal
5371 -- subprogram, declared in the current scope.
5372
5373 procedure Build_Subprogram_Renaming;
5374 -- If the subprogram is recursive, there are occurrences of the name of
5375 -- the generic within the body, which must resolve to the current
5376 -- instance. We add a renaming declaration after the declaration, which
5377 -- is available in the instance body, as well as in the analysis of
5378 -- aspects that appear in the generic. This renaming declaration is
5379 -- inserted after the instance declaration which it renames.
5380
5381 ------------------------------------
5382 -- Analyze_Instance_And_Renamings --
5383 ------------------------------------
5384
5385 procedure Analyze_Instance_And_Renamings is
5386 Def_Ent : constant Entity_Id := Defining_Entity (N);
5387 Pack_Decl : Node_Id;
5388
5389 begin
5390 if Nkind (Parent (N)) = N_Compilation_Unit then
5391
5392 -- For the case of a compilation unit, the container package has
5393 -- the same name as the instantiation, to insure that the binder
5394 -- calls the elaboration procedure with the right name. Copy the
5395 -- entity of the instance, which may have compilation level flags
5396 -- (e.g. Is_Child_Unit) set.
5397
5398 Pack_Id := New_Copy (Def_Ent);
5399
5400 else
5401 -- Otherwise we use the name of the instantiation concatenated
5402 -- with its source position to ensure uniqueness if there are
5403 -- several instantiations with the same name.
5404
5405 Pack_Id :=
5406 Make_Defining_Identifier (Loc,
5407 Chars => New_External_Name
5408 (Related_Id => Chars (Def_Ent),
5409 Suffix => "GP",
5410 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5411 end if;
5412
5413 Pack_Decl :=
5414 Make_Package_Declaration (Loc,
5415 Specification => Make_Package_Specification (Loc,
5416 Defining_Unit_Name => Pack_Id,
5417 Visible_Declarations => Renaming_List,
5418 End_Label => Empty));
5419
5420 Set_Instance_Spec (N, Pack_Decl);
5421 Set_Is_Generic_Instance (Pack_Id);
5422 Set_Debug_Info_Needed (Pack_Id);
5423
5424 -- Case of not a compilation unit
5425
5426 if Nkind (Parent (N)) /= N_Compilation_Unit then
5427 Mark_Rewrite_Insertion (Pack_Decl);
5428 Insert_Before (N, Pack_Decl);
5429 Set_Has_Completion (Pack_Id);
5430
5431 -- Case of an instantiation that is a compilation unit
5432
5433 -- Place declaration on current node so context is complete for
5434 -- analysis (including nested instantiations), and for use in a
5435 -- context_clause (see Analyze_With_Clause).
5436
5437 else
5438 Set_Unit (Parent (N), Pack_Decl);
5439 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5440 end if;
5441
5442 Analyze (Pack_Decl);
5443 Check_Formal_Packages (Pack_Id);
5444
5445 -- Body of the enclosing package is supplied when instantiating the
5446 -- subprogram body, after semantic analysis is completed.
5447
5448 if Nkind (Parent (N)) = N_Compilation_Unit then
5449
5450 -- Remove package itself from visibility, so it does not
5451 -- conflict with subprogram.
5452
5453 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5454
5455 -- Set name and scope of internal subprogram so that the proper
5456 -- external name will be generated. The proper scope is the scope
5457 -- of the wrapper package. We need to generate debugging info for
5458 -- the internal subprogram, so set flag accordingly.
5459
5460 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5461 Set_Scope (Anon_Id, Scope (Pack_Id));
5462
5463 -- Mark wrapper package as referenced, to avoid spurious warnings
5464 -- if the instantiation appears in various with_ clauses of
5465 -- subunits of the main unit.
5466
5467 Set_Referenced (Pack_Id);
5468 end if;
5469
5470 Set_Is_Generic_Instance (Anon_Id);
5471 Set_Debug_Info_Needed (Anon_Id);
5472 Act_Decl_Id := New_Copy (Anon_Id);
5473
5474 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5475 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5476 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5477
5478 -- Subprogram instance comes from source only if generic does
5479
5480 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5481
5482 -- If the instance is a child unit, mark the Id accordingly. Mark
5483 -- the anonymous entity as well, which is the real subprogram and
5484 -- which is used when the instance appears in a context clause.
5485 -- Similarly, propagate the Is_Eliminated flag to handle properly
5486 -- nested eliminated subprograms.
5487
5488 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5489 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5490 New_Overloaded_Entity (Act_Decl_Id);
5491 Check_Eliminated (Act_Decl_Id);
5492 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5493
5494 if Nkind (Parent (N)) = N_Compilation_Unit then
5495
5496 -- In compilation unit case, kill elaboration checks on the
5497 -- instantiation, since they are never needed - the body is
5498 -- instantiated at the same point as the spec.
5499
5500 if Legacy_Elaboration_Checks then
5501 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5502 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5503 end if;
5504
5505 Set_Is_Compilation_Unit (Anon_Id);
5506 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5507 end if;
5508
5509 -- The instance is not a freezing point for the new subprogram.
5510 -- The anonymous subprogram may have a freeze node, created for
5511 -- some delayed aspects. This freeze node must not be inherited
5512 -- by the visible subprogram entity.
5513
5514 Set_Is_Frozen (Act_Decl_Id, False);
5515 Set_Freeze_Node (Act_Decl_Id, Empty);
5516
5517 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5518 Valid_Operator_Definition (Act_Decl_Id);
5519 end if;
5520
5521 Set_Alias (Act_Decl_Id, Anon_Id);
5522 Set_Has_Completion (Act_Decl_Id);
5523 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5524
5525 if Nkind (Parent (N)) = N_Compilation_Unit then
5526 Set_Body_Required (Parent (N), False);
5527 end if;
5528 end Analyze_Instance_And_Renamings;
5529
5530 -------------------------------
5531 -- Build_Subprogram_Renaming --
5532 -------------------------------
5533
5534 procedure Build_Subprogram_Renaming is
5535 Renaming_Decl : Node_Id;
5536 Unit_Renaming : Node_Id;
5537
5538 begin
5539 Unit_Renaming :=
5540 Make_Subprogram_Renaming_Declaration (Loc,
5541 Specification =>
5542 Copy_Generic_Node
5543 (Specification (Original_Node (Gen_Decl)),
5544 Empty,
5545 Instantiating => True),
5546 Name => New_Occurrence_Of (Anon_Id, Loc));
5547
5548 -- The generic may be a child unit. The renaming needs an identifier
5549 -- with the proper name.
5550
5551 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5552 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5553
5554 -- If there is a formal subprogram with the same name as the unit
5555 -- itself, do not add this renaming declaration, to prevent
5556 -- ambiguities when there is a call with that name in the body.
5557 -- This is a partial and ugly fix for one ACATS test. ???
5558
5559 Renaming_Decl := First (Renaming_List);
5560 while Present (Renaming_Decl) loop
5561 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5562 and then
5563 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5564 then
5565 exit;
5566 end if;
5567
5568 Next (Renaming_Decl);
5569 end loop;
5570
5571 if No (Renaming_Decl) then
5572 Append (Unit_Renaming, Renaming_List);
5573 end if;
5574 end Build_Subprogram_Renaming;
5575
5576 -- Local variables
5577
5578 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5579 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5580 Saved_ISMP : constant Boolean :=
5581 Ignore_SPARK_Mode_Pragmas_In_Instance;
5582 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5583 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5584 -- Save the Ghost and SPARK mode-related data to restore on exit
5585
5586 Vis_Prims_List : Elist_Id := No_Elist;
5587 -- List of primitives made temporarily visible in the instantiation
5588 -- to match the visibility of the formal type
5589
5590 -- Start of processing for Analyze_Subprogram_Instantiation
5591
5592 begin
5593 -- Preserve relevant elaboration-related attributes of the context which
5594 -- are no longer available or very expensive to recompute once analysis,
5595 -- resolution, and expansion are over.
5596
5597 Mark_Elaboration_Attributes
5598 (N_Id => N,
5599 Checks => True,
5600 Level => True,
5601 Modes => True,
5602 Warnings => True);
5603
5604 -- Very first thing: check for special Text_IO unit in case we are
5605 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5606 -- such an instantiation is bogus (these are packages, not subprograms),
5607 -- but we get a better error message if we do this.
5608
5609 Check_Text_IO_Special_Unit (Gen_Id);
5610
5611 -- Make node global for error reporting
5612
5613 Instantiation_Node := N;
5614
5615 -- For package instantiations we turn off style checks, because they
5616 -- will have been emitted in the generic. For subprogram instantiations
5617 -- we want to apply at least the check on overriding indicators so we
5618 -- do not modify the style check status.
5619
5620 -- The renaming declarations for the actuals do not come from source and
5621 -- will not generate spurious warnings.
5622
5623 Preanalyze_Actuals (N);
5624
5625 Init_Env;
5626 Env_Installed := True;
5627 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5628 Gen_Unit := Entity (Gen_Id);
5629
5630 -- A subprogram instantiation is Ghost when it is subject to pragma
5631 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5632 -- that any nodes generated during analysis and expansion are marked as
5633 -- Ghost.
5634
5635 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5636
5637 Generate_Reference (Gen_Unit, Gen_Id);
5638
5639 if Nkind (Gen_Id) = N_Identifier
5640 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5641 then
5642 Error_Msg_NE
5643 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5644 end if;
5645
5646 if Etype (Gen_Unit) = Any_Type then
5647 Restore_Env;
5648 goto Leave;
5649 end if;
5650
5651 -- Verify that it is a generic subprogram of the right kind, and that
5652 -- it does not lead to a circular instantiation.
5653
5654 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5655 Error_Msg_NE
5656 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5657
5658 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5659 Error_Msg_NE
5660 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5661
5662 elsif In_Open_Scopes (Gen_Unit) then
5663 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5664
5665 else
5666 Mutate_Ekind (Inst_Id, K);
5667 Set_Scope (Inst_Id, Current_Scope);
5668
5669 Set_Entity (Gen_Id, Gen_Unit);
5670 Set_Is_Instantiated (Gen_Unit);
5671
5672 if In_Extended_Main_Source_Unit (N) then
5673 Generate_Reference (Gen_Unit, N);
5674 end if;
5675
5676 -- If renaming, get original unit
5677
5678 if Present (Renamed_Object (Gen_Unit))
5679 and then Is_Generic_Subprogram (Renamed_Object (Gen_Unit))
5680 then
5681 Gen_Unit := Renamed_Object (Gen_Unit);
5682 Set_Is_Instantiated (Gen_Unit);
5683 Generate_Reference (Gen_Unit, N);
5684 end if;
5685
5686 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5687 Error_Msg_Node_2 := Current_Scope;
5688 Error_Msg_NE
5689 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5690 Circularity_Detected := True;
5691 Restore_Hidden_Primitives (Vis_Prims_List);
5692 goto Leave;
5693 end if;
5694
5695 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5696
5697 -- Initialize renamings map, for error checking
5698
5699 Generic_Renamings.Set_Last (0);
5700 Generic_Renamings_HTable.Reset;
5701
5702 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5703
5704 -- Copy original generic tree, to produce text for instantiation
5705
5706 Act_Tree :=
5707 Copy_Generic_Node
5708 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5709
5710 -- Inherit overriding indicator from instance node
5711
5712 Act_Spec := Specification (Act_Tree);
5713 Set_Must_Override (Act_Spec, Must_Override (N));
5714 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5715
5716 Renaming_List :=
5717 Analyze_Associations
5718 (I_Node => N,
5719 Formals => Generic_Formal_Declarations (Act_Tree),
5720 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5721
5722 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5723
5724 -- The subprogram itself cannot contain a nested instance, so the
5725 -- current parent is left empty.
5726
5727 Set_Instance_Env (Gen_Unit, Empty);
5728
5729 -- Build the subprogram declaration, which does not appear in the
5730 -- generic template, and give it a sloc consistent with that of the
5731 -- template.
5732
5733 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5734 Set_Generic_Parent (Act_Spec, Gen_Unit);
5735 Act_Decl :=
5736 Make_Subprogram_Declaration (Sloc (Act_Spec),
5737 Specification => Act_Spec);
5738
5739 -- The aspects have been copied previously, but they have to be
5740 -- linked explicitly to the new subprogram declaration. Explicit
5741 -- pre/postconditions on the instance are analyzed below, in a
5742 -- separate step.
5743
5744 Move_Aspects (Act_Tree, To => Act_Decl);
5745 Set_Categorization_From_Pragmas (Act_Decl);
5746
5747 if Parent_Installed then
5748 Hide_Current_Scope;
5749 end if;
5750
5751 Append (Act_Decl, Renaming_List);
5752
5753 -- Contract-related source pragmas that follow a generic subprogram
5754 -- must be instantiated explicitly because they are not part of the
5755 -- subprogram template.
5756
5757 Instantiate_Subprogram_Contract
5758 (Original_Node (Gen_Decl), Renaming_List);
5759
5760 Build_Subprogram_Renaming;
5761
5762 -- If the context of the instance is subject to SPARK_Mode "off" or
5763 -- the annotation is altogether missing, set the global flag which
5764 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5765 -- the instance. This should be done prior to analyzing the instance.
5766
5767 if SPARK_Mode /= On then
5768 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5769 end if;
5770
5771 -- If the context of an instance is not subject to SPARK_Mode "off",
5772 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5773 -- the latter should be the one applicable to the instance.
5774
5775 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5776 and then Saved_SM /= Off
5777 and then Present (SPARK_Pragma (Gen_Unit))
5778 then
5779 Set_SPARK_Mode (Gen_Unit);
5780 end if;
5781
5782 Analyze_Instance_And_Renamings;
5783
5784 -- Restore SPARK_Mode from the context after analysis of the package
5785 -- declaration, so that the SPARK_Mode on the generic spec does not
5786 -- apply to the pending instance for the instance body.
5787
5788 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5789 and then Saved_SM /= Off
5790 and then Present (SPARK_Pragma (Gen_Unit))
5791 then
5792 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5793 end if;
5794
5795 -- If the generic is marked Import (Intrinsic), then so is the
5796 -- instance. This indicates that there is no body to instantiate. If
5797 -- generic is marked inline, so it the instance, and the anonymous
5798 -- subprogram it renames. If inlined, or else if inlining is enabled
5799 -- for the compilation, we generate the instance body even if it is
5800 -- not within the main unit.
5801
5802 if Is_Intrinsic_Subprogram (Gen_Unit) then
5803 Set_Is_Intrinsic_Subprogram (Anon_Id);
5804 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5805
5806 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5807 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5808 end if;
5809 end if;
5810
5811 -- Inherit convention from generic unit. Intrinsic convention, as for
5812 -- an instance of unchecked conversion, is not inherited because an
5813 -- explicit Ada instance has been created.
5814
5815 if Has_Convention_Pragma (Gen_Unit)
5816 and then Convention (Gen_Unit) /= Convention_Intrinsic
5817 then
5818 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5819 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5820 end if;
5821
5822 Generate_Definition (Act_Decl_Id);
5823
5824 -- Inherit all inlining-related flags which apply to the generic in
5825 -- the subprogram and its declaration.
5826
5827 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5828 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5829
5830 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5831 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5832
5833 Set_Has_Pragma_Inline_Always
5834 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5835 Set_Has_Pragma_Inline_Always
5836 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5837
5838 Set_Has_Pragma_No_Inline
5839 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5840 Set_Has_Pragma_No_Inline
5841 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5842
5843 -- Propagate No_Return if pragma applied to generic unit. This must
5844 -- be done explicitly because pragma does not appear in generic
5845 -- declaration (unlike the aspect case).
5846
5847 if No_Return (Gen_Unit) then
5848 Set_No_Return (Act_Decl_Id);
5849 Set_No_Return (Anon_Id);
5850 end if;
5851
5852 -- Mark both the instance spec and the anonymous package in case the
5853 -- body is instantiated at a later pass. This preserves the original
5854 -- context in effect for the body.
5855
5856 if SPARK_Mode /= On then
5857 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5858 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5859 end if;
5860
5861 if Legacy_Elaboration_Checks
5862 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5863 then
5864 Check_Elab_Instantiation (N);
5865 end if;
5866
5867 -- Save the scenario for later examination by the ABE Processing
5868 -- phase.
5869
5870 Record_Elaboration_Scenario (N);
5871
5872 -- The instantiation results in a guaranteed ABE. Create a completing
5873 -- body for the subprogram declaration because the real body will not
5874 -- be instantiated.
5875
5876 if Is_Known_Guaranteed_ABE (N) then
5877 Provide_Completing_Bodies (Instance_Spec (N));
5878 end if;
5879
5880 if Is_Dispatching_Operation (Act_Decl_Id)
5881 and then Ada_Version >= Ada_2005
5882 then
5883 declare
5884 Formal : Entity_Id;
5885
5886 begin
5887 Formal := First_Formal (Act_Decl_Id);
5888 while Present (Formal) loop
5889 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5890 and then Is_Controlling_Formal (Formal)
5891 and then not Can_Never_Be_Null (Formal)
5892 then
5893 Error_Msg_NE
5894 ("access parameter& is controlling,", N, Formal);
5895 Error_Msg_NE
5896 ("\corresponding parameter of & must be explicitly "
5897 & "null-excluding", N, Gen_Id);
5898 end if;
5899
5900 Next_Formal (Formal);
5901 end loop;
5902 end;
5903 end if;
5904
5905 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5906
5907 Validate_Categorization_Dependency (N, Act_Decl_Id);
5908
5909 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5910 Inherit_Context (Gen_Decl, N);
5911
5912 Restore_Private_Views (Pack_Id, False);
5913
5914 -- If the context requires a full instantiation, mark node for
5915 -- subsequent construction of the body.
5916
5917 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5918 Check_Forward_Instantiation (Gen_Decl);
5919
5920 -- The wrapper package is always delayed, because it does not
5921 -- constitute a freeze point, but to insure that the freeze node
5922 -- is placed properly, it is created directly when instantiating
5923 -- the body (otherwise the freeze node might appear to early for
5924 -- nested instantiations).
5925
5926 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5927 Rewrite (N, Unit (Parent (N)));
5928 Set_Unit (Parent (N), N);
5929 end if;
5930
5931 -- Replace instance node for library-level instantiations of
5932 -- intrinsic subprograms.
5933
5934 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5935 Rewrite (N, Unit (Parent (N)));
5936 Set_Unit (Parent (N), N);
5937 end if;
5938
5939 if Parent_Installed then
5940 Remove_Parent;
5941 end if;
5942
5943 Restore_Hidden_Primitives (Vis_Prims_List);
5944 Restore_Env;
5945 Env_Installed := False;
5946 Generic_Renamings.Set_Last (0);
5947 Generic_Renamings_HTable.Reset;
5948 end if;
5949
5950 <<Leave>>
5951 -- Analyze aspects in declaration if no errors appear in the instance.
5952
5953 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5954 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5955 end if;
5956
5957 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5958 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5959 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5960
5961 exception
5962 when Instantiation_Error =>
5963 if Parent_Installed then
5964 Remove_Parent;
5965 end if;
5966
5967 if Env_Installed then
5968 Restore_Env;
5969 end if;
5970
5971 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5972 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5973 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5974 end Analyze_Subprogram_Instantiation;
5975
5976 -------------------------
5977 -- Get_Associated_Node --
5978 -------------------------
5979
5980 function Get_Associated_Node (N : Node_Id) return Node_Id is
5981 Assoc : Node_Id;
5982
5983 begin
5984 Assoc := Associated_Node (N);
5985
5986 if Nkind (Assoc) /= Nkind (N) then
5987 return Assoc;
5988
5989 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
5990 return Assoc;
5991
5992 else
5993 -- If the node is part of an inner generic, it may itself have been
5994 -- remapped into a further generic copy. Associated_Node is otherwise
5995 -- used for the entity of the node, and will be of a different node
5996 -- kind, or else N has been rewritten as a literal or function call.
5997
5998 while Present (Associated_Node (Assoc))
5999 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6000 loop
6001 Assoc := Associated_Node (Assoc);
6002 end loop;
6003
6004 -- Follow an additional link in case the final node was rewritten.
6005 -- This can only happen with nested generic units.
6006
6007 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6008 and then Present (Associated_Node (Assoc))
6009 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6010 | N_Explicit_Dereference
6011 | N_Integer_Literal
6012 | N_Real_Literal
6013 | N_String_Literal
6014 then
6015 Assoc := Associated_Node (Assoc);
6016 end if;
6017
6018 -- An additional special case: an unconstrained type in an object
6019 -- declaration may have been rewritten as a local subtype constrained
6020 -- by the expression in the declaration. We need to recover the
6021 -- original entity, which may be global.
6022
6023 if Present (Original_Node (Assoc))
6024 and then Nkind (Parent (N)) = N_Object_Declaration
6025 then
6026 Assoc := Original_Node (Assoc);
6027 end if;
6028
6029 return Assoc;
6030 end if;
6031 end Get_Associated_Node;
6032
6033 ----------------------------
6034 -- Build_Function_Wrapper --
6035 ----------------------------
6036
6037 function Build_Function_Wrapper
6038 (Formal_Subp : Entity_Id;
6039 Actual_Subp : Entity_Id) return Node_Id
6040 is
6041 Loc : constant Source_Ptr := Sloc (Current_Scope);
6042 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6043 Actuals : List_Id;
6044 Decl : Node_Id;
6045 Func_Name : Node_Id;
6046 Func : Entity_Id;
6047 Parm_Type : Node_Id;
6048 Profile : List_Id := New_List;
6049 Spec : Node_Id;
6050 Act_F : Entity_Id;
6051 Form_F : Entity_Id;
6052 New_F : Entity_Id;
6053
6054 begin
6055 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
6056
6057 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6058 Mutate_Ekind (Func, E_Function);
6059 Set_Is_Generic_Actual_Subprogram (Func);
6060
6061 Actuals := New_List;
6062 Profile := New_List;
6063
6064 Act_F := First_Formal (Actual_Subp);
6065 Form_F := First_Formal (Formal_Subp);
6066 while Present (Form_F) loop
6067
6068 -- Create new formal for profile of wrapper, and add a reference
6069 -- to it in the list of actuals for the enclosing call. The name
6070 -- must be that of the formal in the formal subprogram, because
6071 -- calls to it in the generic body may use named associations.
6072
6073 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6074
6075 Parm_Type :=
6076 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
6077
6078 Append_To (Profile,
6079 Make_Parameter_Specification (Loc,
6080 Defining_Identifier => New_F,
6081 Parameter_Type => Parm_Type));
6082
6083 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
6084 Next_Formal (Form_F);
6085
6086 if Present (Act_F) then
6087 Next_Formal (Act_F);
6088 end if;
6089 end loop;
6090
6091 Spec :=
6092 Make_Function_Specification (Loc,
6093 Defining_Unit_Name => Func,
6094 Parameter_Specifications => Profile,
6095 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6096
6097 Decl :=
6098 Make_Expression_Function (Loc,
6099 Specification => Spec,
6100 Expression =>
6101 Make_Function_Call (Loc,
6102 Name => Func_Name,
6103 Parameter_Associations => Actuals));
6104
6105 return Decl;
6106 end Build_Function_Wrapper;
6107
6108 ----------------------------
6109 -- Build_Operator_Wrapper --
6110 ----------------------------
6111
6112 function Build_Operator_Wrapper
6113 (Formal_Subp : Entity_Id;
6114 Actual_Subp : Entity_Id) return Node_Id
6115 is
6116 Loc : constant Source_Ptr := Sloc (Current_Scope);
6117 Ret_Type : constant Entity_Id :=
6118 Get_Instance_Of (Etype (Formal_Subp));
6119 Op_Type : constant Entity_Id :=
6120 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6121 Is_Binary : constant Boolean :=
6122 Present (Next_Formal (First_Formal (Formal_Subp)));
6123
6124 Decl : Node_Id;
6125 Expr : Node_Id := Empty;
6126 F1, F2 : Entity_Id;
6127 Func : Entity_Id;
6128 Op_Name : Name_Id;
6129 Spec : Node_Id;
6130 L, R : Node_Id;
6131
6132 begin
6133 Op_Name := Chars (Actual_Subp);
6134
6135 -- Create entities for wrapper function and its formals
6136
6137 F1 := Make_Temporary (Loc, 'A');
6138 F2 := Make_Temporary (Loc, 'B');
6139 L := New_Occurrence_Of (F1, Loc);
6140 R := New_Occurrence_Of (F2, Loc);
6141
6142 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6143 Mutate_Ekind (Func, E_Function);
6144 Set_Is_Generic_Actual_Subprogram (Func);
6145
6146 Spec :=
6147 Make_Function_Specification (Loc,
6148 Defining_Unit_Name => Func,
6149 Parameter_Specifications => New_List (
6150 Make_Parameter_Specification (Loc,
6151 Defining_Identifier => F1,
6152 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6153 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6154
6155 if Is_Binary then
6156 Append_To (Parameter_Specifications (Spec),
6157 Make_Parameter_Specification (Loc,
6158 Defining_Identifier => F2,
6159 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6160 end if;
6161
6162 -- Build expression as a function call, or as an operator node
6163 -- that corresponds to the name of the actual, starting with
6164 -- binary operators.
6165
6166 if Op_Name not in Any_Operator_Name then
6167 Expr :=
6168 Make_Function_Call (Loc,
6169 Name =>
6170 New_Occurrence_Of (Actual_Subp, Loc),
6171 Parameter_Associations => New_List (L));
6172
6173 if Is_Binary then
6174 Append_To (Parameter_Associations (Expr), R);
6175 end if;
6176
6177 -- Binary operators
6178
6179 elsif Is_Binary then
6180 if Op_Name = Name_Op_And then
6181 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6182 elsif Op_Name = Name_Op_Or then
6183 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6184 elsif Op_Name = Name_Op_Xor then
6185 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6186 elsif Op_Name = Name_Op_Eq then
6187 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6188 elsif Op_Name = Name_Op_Ne then
6189 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6190 elsif Op_Name = Name_Op_Le then
6191 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6192 elsif Op_Name = Name_Op_Gt then
6193 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6194 elsif Op_Name = Name_Op_Ge then
6195 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6196 elsif Op_Name = Name_Op_Lt then
6197 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6198 elsif Op_Name = Name_Op_Add then
6199 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6200 elsif Op_Name = Name_Op_Subtract then
6201 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6202 elsif Op_Name = Name_Op_Concat then
6203 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6204 elsif Op_Name = Name_Op_Multiply then
6205 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6206 elsif Op_Name = Name_Op_Divide then
6207 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6208 elsif Op_Name = Name_Op_Mod then
6209 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6210 elsif Op_Name = Name_Op_Rem then
6211 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6212 elsif Op_Name = Name_Op_Expon then
6213 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6214 end if;
6215
6216 -- Unary operators
6217
6218 else
6219 if Op_Name = Name_Op_Add then
6220 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6221 elsif Op_Name = Name_Op_Subtract then
6222 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6223 elsif Op_Name = Name_Op_Abs then
6224 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6225 elsif Op_Name = Name_Op_Not then
6226 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6227 end if;
6228 end if;
6229
6230 Decl :=
6231 Make_Expression_Function (Loc,
6232 Specification => Spec,
6233 Expression => Expr);
6234
6235 return Decl;
6236 end Build_Operator_Wrapper;
6237
6238 -----------------------------------
6239 -- Build_Subprogram_Decl_Wrapper --
6240 -----------------------------------
6241
6242 function Build_Subprogram_Decl_Wrapper
6243 (Formal_Subp : Entity_Id) return Node_Id
6244 is
6245 Loc : constant Source_Ptr := Sloc (Current_Scope);
6246 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6247 Decl : Node_Id;
6248 Subp : Entity_Id;
6249 Parm_Spec : Node_Id;
6250 Profile : List_Id := New_List;
6251 Spec : Node_Id;
6252 Form_F : Entity_Id;
6253 New_F : Entity_Id;
6254
6255 begin
6256
6257 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6258 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6259 Set_Is_Generic_Actual_Subprogram (Subp);
6260
6261 Profile := Parameter_Specifications (
6262 New_Copy_Tree
6263 (Specification (Unit_Declaration_Node (Formal_Subp))));
6264
6265 Form_F := First_Formal (Formal_Subp);
6266 Parm_Spec := First (Profile);
6267
6268 -- Create new entities for the formals. Reset entities so that
6269 -- parameter types are properly resolved when wrapper declaration
6270 -- is analyzed.
6271
6272 while Present (Parm_Spec) loop
6273 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6274 Set_Defining_Identifier (Parm_Spec, New_F);
6275 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6276 Next (Parm_Spec);
6277 Next_Formal (Form_F);
6278 end loop;
6279
6280 if Ret_Type = Standard_Void_Type then
6281 Spec :=
6282 Make_Procedure_Specification (Loc,
6283 Defining_Unit_Name => Subp,
6284 Parameter_Specifications => Profile);
6285 else
6286 Spec :=
6287 Make_Function_Specification (Loc,
6288 Defining_Unit_Name => Subp,
6289 Parameter_Specifications => Profile,
6290 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6291 end if;
6292
6293 Decl :=
6294 Make_Subprogram_Declaration (Loc, Specification => Spec);
6295
6296 return Decl;
6297 end Build_Subprogram_Decl_Wrapper;
6298
6299 -----------------------------------
6300 -- Build_Subprogram_Body_Wrapper --
6301 -----------------------------------
6302
6303 function Build_Subprogram_Body_Wrapper
6304 (Formal_Subp : Entity_Id;
6305 Actual_Name : Node_Id) return Node_Id
6306 is
6307 Loc : constant Source_Ptr := Sloc (Current_Scope);
6308 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6309 Spec_Node : constant Node_Id :=
6310 Specification
6311 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6312 Act : Node_Id;
6313 Actuals : List_Id;
6314 Body_Node : Node_Id;
6315 Stmt : Node_Id;
6316 begin
6317 Actuals := New_List;
6318 Act := First (Parameter_Specifications (Spec_Node));
6319
6320 while Present (Act) loop
6321 Append_To (Actuals,
6322 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6323 Next (Act);
6324 end loop;
6325
6326 if Ret_Type = Standard_Void_Type then
6327 Stmt := Make_Procedure_Call_Statement (Loc,
6328 Name => Actual_Name,
6329 Parameter_Associations => Actuals);
6330
6331 else
6332 Stmt := Make_Simple_Return_Statement (Loc,
6333 Expression =>
6334 Make_Function_Call (Loc,
6335 Name => Actual_Name,
6336 Parameter_Associations => Actuals));
6337 end if;
6338
6339 Body_Node := Make_Subprogram_Body (Loc,
6340 Specification => Spec_Node,
6341 Declarations => New_List,
6342 Handled_Statement_Sequence =>
6343 Make_Handled_Sequence_Of_Statements (Loc,
6344 Statements => New_List (Stmt)));
6345
6346 return Body_Node;
6347 end Build_Subprogram_Body_Wrapper;
6348
6349 -------------------------------------------
6350 -- Build_Instance_Compilation_Unit_Nodes --
6351 -------------------------------------------
6352
6353 procedure Build_Instance_Compilation_Unit_Nodes
6354 (N : Node_Id;
6355 Act_Body : Node_Id;
6356 Act_Decl : Node_Id)
6357 is
6358 Decl_Cunit : Node_Id;
6359 Body_Cunit : Node_Id;
6360 Citem : Node_Id;
6361 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6362 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6363
6364 begin
6365 -- A new compilation unit node is built for the instance declaration
6366
6367 Decl_Cunit :=
6368 Make_Compilation_Unit (Sloc (N),
6369 Context_Items => Empty_List,
6370 Unit => Act_Decl,
6371 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6372
6373 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6374
6375 -- The new compilation unit is linked to its body, but both share the
6376 -- same file, so we do not set Body_Required on the new unit so as not
6377 -- to create a spurious dependency on a non-existent body in the ali.
6378 -- This simplifies CodePeer unit traversal.
6379
6380 -- We use the original instantiation compilation unit as the resulting
6381 -- compilation unit of the instance, since this is the main unit.
6382
6383 Rewrite (N, Act_Body);
6384
6385 -- Propagate the aspect specifications from the package body template to
6386 -- the instantiated version of the package body.
6387
6388 if Has_Aspects (Act_Body) then
6389 Set_Aspect_Specifications
6390 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6391 end if;
6392
6393 Body_Cunit := Parent (N);
6394
6395 -- The two compilation unit nodes are linked by the Library_Unit field
6396
6397 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6398 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6399
6400 -- Preserve the private nature of the package if needed
6401
6402 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6403
6404 -- If the instance is not the main unit, its context, categorization
6405 -- and elaboration entity are not relevant to the compilation.
6406
6407 if Body_Cunit /= Cunit (Main_Unit) then
6408 Make_Instance_Unit (Body_Cunit, In_Main => False);
6409 return;
6410 end if;
6411
6412 -- The context clause items on the instantiation, which are now attached
6413 -- to the body compilation unit (since the body overwrote the original
6414 -- instantiation node), semantically belong on the spec, so copy them
6415 -- there. It's harmless to leave them on the body as well. In fact one
6416 -- could argue that they belong in both places.
6417
6418 Citem := First (Context_Items (Body_Cunit));
6419 while Present (Citem) loop
6420 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6421 Next (Citem);
6422 end loop;
6423
6424 -- Propagate categorization flags on packages, so that they appear in
6425 -- the ali file for the spec of the unit.
6426
6427 if Ekind (New_Main) = E_Package then
6428 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6429 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6430 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6431 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6432 Set_Is_Remote_Call_Interface
6433 (Old_Main, Is_Remote_Call_Interface (New_Main));
6434 end if;
6435
6436 -- Make entry in Units table, so that binder can generate call to
6437 -- elaboration procedure for body, if any.
6438
6439 Make_Instance_Unit (Body_Cunit, In_Main => True);
6440 Main_Unit_Entity := New_Main;
6441 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6442
6443 -- Build elaboration entity, since the instance may certainly generate
6444 -- elaboration code requiring a flag for protection.
6445
6446 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6447 end Build_Instance_Compilation_Unit_Nodes;
6448
6449 -----------------------------
6450 -- Check_Access_Definition --
6451 -----------------------------
6452
6453 procedure Check_Access_Definition (N : Node_Id) is
6454 begin
6455 pragma Assert
6456 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6457 null;
6458 end Check_Access_Definition;
6459
6460 -----------------------------------
6461 -- Check_Formal_Package_Instance --
6462 -----------------------------------
6463
6464 -- If the formal has specific parameters, they must match those of the
6465 -- actual. Both of them are instances, and the renaming declarations for
6466 -- their formal parameters appear in the same order in both. The analyzed
6467 -- formal has been analyzed in the context of the current instance.
6468
6469 procedure Check_Formal_Package_Instance
6470 (Formal_Pack : Entity_Id;
6471 Actual_Pack : Entity_Id)
6472 is
6473 E1 : Entity_Id := First_Entity (Actual_Pack);
6474 E2 : Entity_Id := First_Entity (Formal_Pack);
6475 Prev_E1 : Entity_Id;
6476
6477 Expr1 : Node_Id;
6478 Expr2 : Node_Id;
6479
6480 procedure Check_Mismatch (B : Boolean);
6481 -- Common error routine for mismatch between the parameters of the
6482 -- actual instance and those of the formal package.
6483
6484 function Is_Defaulted (Param : Entity_Id) return Boolean;
6485 -- If the formal package has partly box-initialized formals, skip
6486 -- conformance check for these formals. Previously the code assumed
6487 -- that box initialization for a formal package applied to all its
6488 -- formal parameters.
6489
6490 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6491 -- The formal may come from a nested formal package, and the actual may
6492 -- have been constant-folded. To determine whether the two denote the
6493 -- same entity we may have to traverse several definitions to recover
6494 -- the ultimate entity that they refer to.
6495
6496 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6497 -- The formal and the actual must be identical, but if both are
6498 -- given by attributes they end up renaming different generated bodies,
6499 -- and we must verify that the attributes themselves match.
6500
6501 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6502 -- Similarly, if the formal comes from a nested formal package, the
6503 -- actual may designate the formal through multiple renamings, which
6504 -- have to be followed to determine the original variable in question.
6505
6506 --------------------
6507 -- Check_Mismatch --
6508 --------------------
6509
6510 procedure Check_Mismatch (B : Boolean) is
6511 -- A Formal_Type_Declaration for a derived private type is rewritten
6512 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6513 -- which is why we examine the original node.
6514
6515 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6516
6517 begin
6518 if Kind = N_Formal_Type_Declaration then
6519 return;
6520
6521 elsif Kind in N_Formal_Object_Declaration
6522 | N_Formal_Package_Declaration
6523 | N_Formal_Subprogram_Declaration
6524 then
6525 null;
6526
6527 -- Ada 2012: If both formal and actual are incomplete types they
6528 -- are conformant.
6529
6530 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6531 null;
6532
6533 elsif B then
6534 Error_Msg_NE
6535 ("actual for & in actual instance does not match formal",
6536 Parent (Actual_Pack), E1);
6537 end if;
6538 end Check_Mismatch;
6539
6540 ------------------
6541 -- Is_Defaulted --
6542 ------------------
6543
6544 function Is_Defaulted (Param : Entity_Id) return Boolean is
6545 Assoc : Node_Id;
6546
6547 begin
6548 Assoc :=
6549 First (Generic_Associations (Parent
6550 (Associated_Formal_Package (Actual_Pack))));
6551
6552 while Present (Assoc) loop
6553 if Nkind (Assoc) = N_Others_Choice then
6554 return True;
6555
6556 elsif Nkind (Assoc) = N_Generic_Association
6557 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6558 then
6559 return Box_Present (Assoc);
6560 end if;
6561
6562 Next (Assoc);
6563 end loop;
6564
6565 return False;
6566 end Is_Defaulted;
6567
6568 --------------------------------
6569 -- Same_Instantiated_Constant --
6570 --------------------------------
6571
6572 function Same_Instantiated_Constant
6573 (E1, E2 : Entity_Id) return Boolean
6574 is
6575 Ent : Entity_Id;
6576
6577 begin
6578 Ent := E2;
6579 while Present (Ent) loop
6580 if E1 = Ent then
6581 return True;
6582
6583 elsif Ekind (Ent) /= E_Constant then
6584 return False;
6585
6586 elsif Is_Entity_Name (Constant_Value (Ent)) then
6587 if Entity (Constant_Value (Ent)) = E1 then
6588 return True;
6589 else
6590 Ent := Entity (Constant_Value (Ent));
6591 end if;
6592
6593 -- The actual may be a constant that has been folded. Recover
6594 -- original name.
6595
6596 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6597 Ent := Entity (Original_Node (Constant_Value (Ent)));
6598
6599 else
6600 return False;
6601 end if;
6602 end loop;
6603
6604 return False;
6605 end Same_Instantiated_Constant;
6606
6607 --------------------------------
6608 -- Same_Instantiated_Function --
6609 --------------------------------
6610
6611 function Same_Instantiated_Function
6612 (E1, E2 : Entity_Id) return Boolean
6613 is
6614 U1, U2 : Node_Id;
6615 begin
6616 if Alias (E1) = Alias (E2) then
6617 return True;
6618
6619 elsif Present (Alias (E2)) then
6620 U1 := Original_Node (Unit_Declaration_Node (E1));
6621 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6622
6623 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6624 and then Nkind (Name (U1)) = N_Attribute_Reference
6625
6626 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6627 and then Nkind (Name (U2)) = N_Attribute_Reference
6628
6629 and then
6630 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6631 else
6632 return False;
6633 end if;
6634 end Same_Instantiated_Function;
6635
6636 --------------------------------
6637 -- Same_Instantiated_Variable --
6638 --------------------------------
6639
6640 function Same_Instantiated_Variable
6641 (E1, E2 : Entity_Id) return Boolean
6642 is
6643 function Original_Entity (E : Entity_Id) return Entity_Id;
6644 -- Follow chain of renamings to the ultimate ancestor
6645
6646 ---------------------
6647 -- Original_Entity --
6648 ---------------------
6649
6650 function Original_Entity (E : Entity_Id) return Entity_Id is
6651 Orig : Entity_Id;
6652
6653 begin
6654 Orig := E;
6655 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6656 and then Present (Renamed_Object (Orig))
6657 and then Is_Entity_Name (Renamed_Object (Orig))
6658 loop
6659 Orig := Entity (Renamed_Object (Orig));
6660 end loop;
6661
6662 return Orig;
6663 end Original_Entity;
6664
6665 -- Start of processing for Same_Instantiated_Variable
6666
6667 begin
6668 return Ekind (E1) = Ekind (E2)
6669 and then Original_Entity (E1) = Original_Entity (E2);
6670 end Same_Instantiated_Variable;
6671
6672 -- Start of processing for Check_Formal_Package_Instance
6673
6674 begin
6675 Prev_E1 := E1;
6676 while Present (E1) and then Present (E2) loop
6677 exit when Ekind (E1) = E_Package
6678 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6679
6680 -- If the formal is the renaming of the formal package, this
6681 -- is the end of its formal part, which may occur before the
6682 -- end of the formal part in the actual in the presence of
6683 -- defaulted parameters in the formal package.
6684
6685 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6686 and then Renamed_Entity (E2) = Scope (E2);
6687
6688 -- The analysis of the actual may generate additional internal
6689 -- entities. If the formal is defaulted, there is no corresponding
6690 -- analysis and the internal entities must be skipped, until we
6691 -- find corresponding entities again.
6692
6693 if Comes_From_Source (E2)
6694 and then not Comes_From_Source (E1)
6695 and then Chars (E1) /= Chars (E2)
6696 then
6697 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6698 Next_Entity (E1);
6699 end loop;
6700 end if;
6701
6702 if No (E1) then
6703 return;
6704
6705 -- Entities may be declared without full declaration, such as
6706 -- itypes and predefined operators (concatenation for arrays, eg).
6707 -- Skip it and keep the formal entity to find a later match for it.
6708
6709 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6710 E1 := Prev_E1;
6711 goto Next_E;
6712
6713 -- If the formal entity comes from a formal declaration, it was
6714 -- defaulted in the formal package, and no check is needed on it.
6715
6716 elsif Nkind (Original_Node (Parent (E2))) in
6717 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6718 then
6719 -- If the formal is a tagged type the corresponding class-wide
6720 -- type has been generated as well, and it must be skipped.
6721
6722 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6723 Next_Entity (E2);
6724 end if;
6725
6726 goto Next_E;
6727
6728 -- Ditto for defaulted formal subprograms.
6729
6730 elsif Is_Overloadable (E1)
6731 and then Nkind (Unit_Declaration_Node (E2)) in
6732 N_Formal_Subprogram_Declaration
6733 then
6734 goto Next_E;
6735
6736 elsif Is_Defaulted (E1) then
6737 goto Next_E;
6738
6739 elsif Is_Type (E1) then
6740
6741 -- Subtypes must statically match. E1, E2 are the local entities
6742 -- that are subtypes of the actuals. Itypes generated for other
6743 -- parameters need not be checked, the check will be performed
6744 -- on the parameters themselves.
6745
6746 -- If E2 is a formal type declaration, it is a defaulted parameter
6747 -- and needs no checking.
6748
6749 if not Is_Itype (E1) and then not Is_Itype (E2) then
6750 Check_Mismatch
6751 (not Is_Type (E2)
6752 or else Etype (E1) /= Etype (E2)
6753 or else not Subtypes_Statically_Match (E1, E2));
6754 end if;
6755
6756 elsif Ekind (E1) = E_Constant then
6757
6758 -- IN parameters must denote the same static value, or the same
6759 -- constant, or the literal null.
6760
6761 Expr1 := Expression (Parent (E1));
6762
6763 if Ekind (E2) /= E_Constant then
6764 Check_Mismatch (True);
6765 goto Next_E;
6766 else
6767 Expr2 := Expression (Parent (E2));
6768 end if;
6769
6770 if Is_OK_Static_Expression (Expr1) then
6771 if not Is_OK_Static_Expression (Expr2) then
6772 Check_Mismatch (True);
6773
6774 elsif Is_Discrete_Type (Etype (E1)) then
6775 declare
6776 V1 : constant Uint := Expr_Value (Expr1);
6777 V2 : constant Uint := Expr_Value (Expr2);
6778 begin
6779 Check_Mismatch (V1 /= V2);
6780 end;
6781
6782 elsif Is_Real_Type (Etype (E1)) then
6783 declare
6784 V1 : constant Ureal := Expr_Value_R (Expr1);
6785 V2 : constant Ureal := Expr_Value_R (Expr2);
6786 begin
6787 Check_Mismatch (V1 /= V2);
6788 end;
6789
6790 elsif Is_String_Type (Etype (E1))
6791 and then Nkind (Expr1) = N_String_Literal
6792 then
6793 if Nkind (Expr2) /= N_String_Literal then
6794 Check_Mismatch (True);
6795 else
6796 Check_Mismatch
6797 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6798 end if;
6799 end if;
6800
6801 elsif Is_Entity_Name (Expr1) then
6802 if Is_Entity_Name (Expr2) then
6803 if Entity (Expr1) = Entity (Expr2) then
6804 null;
6805 else
6806 Check_Mismatch
6807 (not Same_Instantiated_Constant
6808 (Entity (Expr1), Entity (Expr2)));
6809 end if;
6810
6811 else
6812 Check_Mismatch (True);
6813 end if;
6814
6815 elsif Is_Entity_Name (Original_Node (Expr1))
6816 and then Is_Entity_Name (Expr2)
6817 and then Same_Instantiated_Constant
6818 (Entity (Original_Node (Expr1)), Entity (Expr2))
6819 then
6820 null;
6821
6822 elsif Nkind (Expr1) = N_Null then
6823 Check_Mismatch (Nkind (Expr1) /= N_Null);
6824
6825 else
6826 Check_Mismatch (True);
6827 end if;
6828
6829 elsif Ekind (E1) = E_Variable then
6830 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6831
6832 elsif Ekind (E1) = E_Package then
6833 Check_Mismatch
6834 (Ekind (E1) /= Ekind (E2)
6835 or else (Present (Renamed_Object (E2))
6836 and then Renamed_Object (E1) /=
6837 Renamed_Object (E2)));
6838
6839 elsif Is_Overloadable (E1) then
6840 -- Verify that the actual subprograms match. Note that actuals
6841 -- that are attributes are rewritten as subprograms. If the
6842 -- subprogram in the formal package is defaulted, no check is
6843 -- needed. Note that this can only happen in Ada 2005 when the
6844 -- formal package can be partially parameterized.
6845
6846 if Nkind (Unit_Declaration_Node (E1)) =
6847 N_Subprogram_Renaming_Declaration
6848 and then From_Default (Unit_Declaration_Node (E1))
6849 then
6850 null;
6851
6852 -- If the formal package has an "others" box association that
6853 -- covers this formal, there is no need for a check either.
6854
6855 elsif Nkind (Unit_Declaration_Node (E2)) in
6856 N_Formal_Subprogram_Declaration
6857 and then Box_Present (Unit_Declaration_Node (E2))
6858 then
6859 null;
6860
6861 -- No check needed if subprogram is a defaulted null procedure
6862
6863 elsif No (Alias (E2))
6864 and then Ekind (E2) = E_Procedure
6865 and then
6866 Null_Present (Specification (Unit_Declaration_Node (E2)))
6867 then
6868 null;
6869
6870 -- Otherwise the actual in the formal and the actual in the
6871 -- instantiation of the formal must match, up to renamings.
6872
6873 else
6874 Check_Mismatch
6875 (Ekind (E2) /= Ekind (E1)
6876 or else not Same_Instantiated_Function (E1, E2));
6877 end if;
6878
6879 else
6880 raise Program_Error;
6881 end if;
6882
6883 <<Next_E>>
6884 Prev_E1 := E1;
6885 Next_Entity (E1);
6886 Next_Entity (E2);
6887 end loop;
6888 end Check_Formal_Package_Instance;
6889
6890 ---------------------------
6891 -- Check_Formal_Packages --
6892 ---------------------------
6893
6894 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6895 E : Entity_Id;
6896 Formal_P : Entity_Id;
6897 Formal_Decl : Node_Id;
6898 begin
6899 -- Iterate through the declarations in the instance, looking for package
6900 -- renaming declarations that denote instances of formal packages. Stop
6901 -- when we find the renaming of the current package itself. The
6902 -- declaration for a formal package without a box is followed by an
6903 -- internal entity that repeats the instantiation.
6904
6905 E := First_Entity (P_Id);
6906 while Present (E) loop
6907 if Ekind (E) = E_Package then
6908 if Renamed_Object (E) = P_Id then
6909 exit;
6910
6911 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6912 null;
6913
6914 else
6915 Formal_Decl := Parent (Associated_Formal_Package (E));
6916
6917 -- Nothing to check if the formal has a box or an others_clause
6918 -- (necessarily with a box), or no associations altogether
6919
6920 if Box_Present (Formal_Decl)
6921 or else No (Generic_Associations (Formal_Decl))
6922 then
6923 null;
6924
6925 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6926 N_Others_Choice
6927 then
6928 -- The internal validating package was generated but formal
6929 -- and instance are known to be compatible.
6930
6931 Formal_P := Next_Entity (E);
6932 Remove (Unit_Declaration_Node (Formal_P));
6933
6934 else
6935 Formal_P := Next_Entity (E);
6936
6937 -- If the instance is within an enclosing instance body
6938 -- there is no need to verify the legality of current formal
6939 -- packages because they were legal in the generic body.
6940 -- This optimization may be applicable elsewhere, and it
6941 -- also removes spurious errors that may arise with
6942 -- on-the-fly inlining and confusion between private and
6943 -- full views.
6944
6945 if not In_Instance_Body then
6946 Check_Formal_Package_Instance (Formal_P, E);
6947 end if;
6948
6949 -- Restore the visibility of formals of the formal instance
6950 -- that are not defaulted, and are hidden within the current
6951 -- generic. These formals may be visible within an enclosing
6952 -- generic.
6953
6954 declare
6955 Elmt : Elmt_Id;
6956 begin
6957 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6958 while Present (Elmt) loop
6959 Set_Is_Hidden (Node (Elmt), False);
6960 Next_Elmt (Elmt);
6961 end loop;
6962 end;
6963
6964 -- After checking, remove the internal validating package.
6965 -- It is only needed for semantic checks, and as it may
6966 -- contain generic formal declarations it should not reach
6967 -- gigi.
6968
6969 Remove (Unit_Declaration_Node (Formal_P));
6970 end if;
6971 end if;
6972 end if;
6973
6974 Next_Entity (E);
6975 end loop;
6976 end Check_Formal_Packages;
6977
6978 ---------------------------------
6979 -- Check_Forward_Instantiation --
6980 ---------------------------------
6981
6982 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6983 S : Entity_Id;
6984 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6985
6986 begin
6987 -- The instantiation appears before the generic body if we are in the
6988 -- scope of the unit containing the generic, either in its spec or in
6989 -- the package body, and before the generic body.
6990
6991 if Ekind (Gen_Comp) = E_Package_Body then
6992 Gen_Comp := Spec_Entity (Gen_Comp);
6993 end if;
6994
6995 if In_Open_Scopes (Gen_Comp)
6996 and then No (Corresponding_Body (Decl))
6997 then
6998 S := Current_Scope;
6999
7000 while Present (S)
7001 and then not Is_Compilation_Unit (S)
7002 and then not Is_Child_Unit (S)
7003 loop
7004 if Ekind (S) = E_Package then
7005 Set_Has_Forward_Instantiation (S);
7006 end if;
7007
7008 S := Scope (S);
7009 end loop;
7010 end if;
7011 end Check_Forward_Instantiation;
7012
7013 ---------------------------
7014 -- Check_Generic_Actuals --
7015 ---------------------------
7016
7017 -- The visibility of the actuals may be different between the point of
7018 -- generic instantiation and the instantiation of the body.
7019
7020 procedure Check_Generic_Actuals
7021 (Instance : Entity_Id;
7022 Is_Formal_Box : Boolean)
7023 is
7024 E : Entity_Id;
7025 Astype : Entity_Id;
7026
7027 begin
7028 E := First_Entity (Instance);
7029 while Present (E) loop
7030 if Is_Type (E)
7031 and then Nkind (Parent (E)) = N_Subtype_Declaration
7032 and then Scope (Etype (E)) /= Instance
7033 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7034 then
7035 -- Restore the proper view of the actual from the information
7036 -- saved earlier by Instantiate_Type.
7037
7038 Check_Private_View (Subtype_Indication (Parent (E)));
7039
7040 -- If the actual is itself the formal of a parent instance,
7041 -- then also restore the proper view of its actual and so on.
7042 -- That's necessary for nested instantiations of the form
7043
7044 -- generic
7045 -- type Component is private;
7046 -- type Array_Type is array (Positive range <>) of Component;
7047 -- procedure Proc;
7048
7049 -- when the outermost actuals have inconsistent views, because
7050 -- the Component_Type of Array_Type of the inner instantiations
7051 -- is the actual of Component of the outermost one and not that
7052 -- of the corresponding inner instantiations.
7053
7054 Astype := Ancestor_Subtype (E);
7055 while Present (Astype)
7056 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7057 and then Present (Generic_Parent_Type (Parent (Astype)))
7058 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7059 loop
7060 Check_Private_View (Subtype_Indication (Parent (Astype)));
7061 Astype := Ancestor_Subtype (Astype);
7062 end loop;
7063
7064 Set_Is_Generic_Actual_Type (E);
7065
7066 if Is_Private_Type (E) and then Present (Full_View (E)) then
7067 Set_Is_Generic_Actual_Type (Full_View (E));
7068 end if;
7069
7070 Set_Is_Hidden (E, False);
7071 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7072
7073 -- We constructed the generic actual type as a subtype of the
7074 -- supplied type. This means that it normally would not inherit
7075 -- subtype specific attributes of the actual, which is wrong for
7076 -- the generic case.
7077
7078 Astype := Ancestor_Subtype (E);
7079
7080 if No (Astype) then
7081
7082 -- This can happen when E is an itype that is the full view of
7083 -- a private type completed, e.g. with a constrained array. In
7084 -- that case, use the first subtype, which will carry size
7085 -- information. The base type itself is unconstrained and will
7086 -- not carry it.
7087
7088 Astype := First_Subtype (E);
7089 end if;
7090
7091 Set_Size_Info (E, (Astype));
7092 Set_RM_Size (E, RM_Size (Astype));
7093 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7094
7095 if Is_Discrete_Or_Fixed_Point_Type (E) then
7096 Set_RM_Size (E, RM_Size (Astype));
7097 end if;
7098
7099 elsif Ekind (E) = E_Package then
7100
7101 -- If this is the renaming for the current instance, we're done.
7102 -- Otherwise it is a formal package. If the corresponding formal
7103 -- was declared with a box, the (instantiations of the) generic
7104 -- formal part are also visible. Otherwise, ignore the entity
7105 -- created to validate the actuals.
7106
7107 if Renamed_Object (E) = Instance then
7108 exit;
7109
7110 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7111 null;
7112
7113 -- The visibility of a formal of an enclosing generic is already
7114 -- correct.
7115
7116 elsif Denotes_Formal_Package (E) then
7117 null;
7118
7119 elsif Present (Associated_Formal_Package (E))
7120 and then not Is_Generic_Formal (E)
7121 then
7122 if Box_Present (Parent (Associated_Formal_Package (E))) then
7123 Check_Generic_Actuals (Renamed_Object (E), True);
7124
7125 else
7126 Check_Generic_Actuals (Renamed_Object (E), False);
7127 end if;
7128
7129 Set_Is_Hidden (E, False);
7130 end if;
7131
7132 -- If this is a subprogram instance (in a wrapper package) the
7133 -- actual is fully visible.
7134
7135 elsif Is_Wrapper_Package (Instance) then
7136 Set_Is_Hidden (E, False);
7137
7138 -- If the formal package is declared with a box, or if the formal
7139 -- parameter is defaulted, it is visible in the body.
7140
7141 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7142 Set_Is_Hidden (E, False);
7143 end if;
7144
7145 if Ekind (E) = E_Constant then
7146
7147 -- If the type of the actual is a private type declared in the
7148 -- enclosing scope of the generic unit, the body of the generic
7149 -- sees the full view of the type (because it has to appear in
7150 -- the corresponding package body). If the type is private now,
7151 -- exchange views to restore the proper visiblity in the instance.
7152
7153 declare
7154 Typ : constant Entity_Id := Base_Type (Etype (E));
7155 -- The type of the actual
7156
7157 Gen_Id : Entity_Id;
7158 -- The generic unit
7159
7160 Parent_Scope : Entity_Id;
7161 -- The enclosing scope of the generic unit
7162
7163 begin
7164 if Is_Wrapper_Package (Instance) then
7165 Gen_Id :=
7166 Generic_Parent
7167 (Specification
7168 (Unit_Declaration_Node
7169 (Related_Instance (Instance))));
7170 else
7171 Gen_Id :=
7172 Generic_Parent (Package_Specification (Instance));
7173 end if;
7174
7175 Parent_Scope := Scope (Gen_Id);
7176
7177 -- The exchange is only needed if the generic is defined
7178 -- within a package which is not a common ancestor of the
7179 -- scope of the instance, and is not already in scope.
7180
7181 if Is_Private_Type (Typ)
7182 and then Scope (Typ) = Parent_Scope
7183 and then Scope (Instance) /= Parent_Scope
7184 and then Ekind (Parent_Scope) = E_Package
7185 and then not Is_Child_Unit (Gen_Id)
7186 then
7187 Switch_View (Typ);
7188
7189 -- If the type of the entity is a subtype, it may also have
7190 -- to be made visible, together with the base type of its
7191 -- full view, after exchange.
7192
7193 if Is_Private_Type (Etype (E)) then
7194 Switch_View (Etype (E));
7195 Switch_View (Base_Type (Etype (E)));
7196 end if;
7197 end if;
7198 end;
7199 end if;
7200
7201 Next_Entity (E);
7202 end loop;
7203 end Check_Generic_Actuals;
7204
7205 ------------------------------
7206 -- Check_Generic_Child_Unit --
7207 ------------------------------
7208
7209 procedure Check_Generic_Child_Unit
7210 (Gen_Id : Node_Id;
7211 Parent_Installed : in out Boolean)
7212 is
7213 Loc : constant Source_Ptr := Sloc (Gen_Id);
7214 Gen_Par : Entity_Id := Empty;
7215 E : Entity_Id;
7216 Inst_Par : Entity_Id;
7217 S : Node_Id;
7218
7219 function Find_Generic_Child
7220 (Scop : Entity_Id;
7221 Id : Node_Id) return Entity_Id;
7222 -- Search generic parent for possible child unit with the given name
7223
7224 function In_Enclosing_Instance return Boolean;
7225 -- Within an instance of the parent, the child unit may be denoted by
7226 -- a simple name, or an abbreviated expanded name. Examine enclosing
7227 -- scopes to locate a possible parent instantiation.
7228
7229 ------------------------
7230 -- Find_Generic_Child --
7231 ------------------------
7232
7233 function Find_Generic_Child
7234 (Scop : Entity_Id;
7235 Id : Node_Id) return Entity_Id
7236 is
7237 E : Entity_Id;
7238
7239 begin
7240 -- If entity of name is already set, instance has already been
7241 -- resolved, e.g. in an enclosing instantiation.
7242
7243 if Present (Entity (Id)) then
7244 if Scope (Entity (Id)) = Scop then
7245 return Entity (Id);
7246 else
7247 return Empty;
7248 end if;
7249
7250 else
7251 E := First_Entity (Scop);
7252 while Present (E) loop
7253 if Chars (E) = Chars (Id)
7254 and then Is_Child_Unit (E)
7255 then
7256 if Is_Child_Unit (E)
7257 and then not Is_Visible_Lib_Unit (E)
7258 then
7259 Error_Msg_NE
7260 ("generic child unit& is not visible", Gen_Id, E);
7261 end if;
7262
7263 Set_Entity (Id, E);
7264 return E;
7265 end if;
7266
7267 Next_Entity (E);
7268 end loop;
7269
7270 return Empty;
7271 end if;
7272 end Find_Generic_Child;
7273
7274 ---------------------------
7275 -- In_Enclosing_Instance --
7276 ---------------------------
7277
7278 function In_Enclosing_Instance return Boolean is
7279 Enclosing_Instance : Node_Id;
7280 Instance_Decl : Node_Id;
7281
7282 begin
7283 -- We do not inline any call that contains instantiations, except
7284 -- for instantiations of Unchecked_Conversion, so if we are within
7285 -- an inlined body the current instance does not require parents.
7286
7287 if In_Inlined_Body then
7288 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7289 return False;
7290 end if;
7291
7292 -- Loop to check enclosing scopes
7293
7294 Enclosing_Instance := Current_Scope;
7295 while Present (Enclosing_Instance) loop
7296 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7297
7298 if Ekind (Enclosing_Instance) = E_Package
7299 and then Is_Generic_Instance (Enclosing_Instance)
7300 and then Present
7301 (Generic_Parent (Specification (Instance_Decl)))
7302 then
7303 -- Check whether the generic we are looking for is a child of
7304 -- this instance.
7305
7306 E := Find_Generic_Child
7307 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7308 exit when Present (E);
7309
7310 else
7311 E := Empty;
7312 end if;
7313
7314 Enclosing_Instance := Scope (Enclosing_Instance);
7315 end loop;
7316
7317 if No (E) then
7318
7319 -- Not a child unit
7320
7321 Analyze (Gen_Id);
7322 return False;
7323
7324 else
7325 Rewrite (Gen_Id,
7326 Make_Expanded_Name (Loc,
7327 Chars => Chars (E),
7328 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7329 Selector_Name => New_Occurrence_Of (E, Loc)));
7330
7331 Set_Entity (Gen_Id, E);
7332 Set_Etype (Gen_Id, Etype (E));
7333 Parent_Installed := False; -- Already in scope.
7334 return True;
7335 end if;
7336 end In_Enclosing_Instance;
7337
7338 -- Start of processing for Check_Generic_Child_Unit
7339
7340 begin
7341 -- If the name of the generic is given by a selected component, it may
7342 -- be the name of a generic child unit, and the prefix is the name of an
7343 -- instance of the parent, in which case the child unit must be visible.
7344 -- If this instance is not in scope, it must be placed there and removed
7345 -- after instantiation, because what is being instantiated is not the
7346 -- original child, but the corresponding child present in the instance
7347 -- of the parent.
7348
7349 -- If the child is instantiated within the parent, it can be given by
7350 -- a simple name. In this case the instance is already in scope, but
7351 -- the child generic must be recovered from the generic parent as well.
7352
7353 if Nkind (Gen_Id) = N_Selected_Component then
7354 S := Selector_Name (Gen_Id);
7355 Analyze (Prefix (Gen_Id));
7356 Inst_Par := Entity (Prefix (Gen_Id));
7357
7358 if Ekind (Inst_Par) = E_Package
7359 and then Present (Renamed_Object (Inst_Par))
7360 then
7361 Inst_Par := Renamed_Object (Inst_Par);
7362 end if;
7363
7364 if Ekind (Inst_Par) = E_Package then
7365 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7366 Gen_Par := Generic_Parent (Parent (Inst_Par));
7367
7368 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7369 and then
7370 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7371 then
7372 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7373 end if;
7374
7375 elsif Ekind (Inst_Par) = E_Generic_Package
7376 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7377 then
7378 -- A formal package may be a real child package, and not the
7379 -- implicit instance within a parent. In this case the child is
7380 -- not visible and has to be retrieved explicitly as well.
7381
7382 Gen_Par := Inst_Par;
7383 end if;
7384
7385 if Present (Gen_Par) then
7386
7387 -- The prefix denotes an instantiation. The entity itself may be a
7388 -- nested generic, or a child unit.
7389
7390 E := Find_Generic_Child (Gen_Par, S);
7391
7392 if Present (E) then
7393 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7394 Set_Entity (Gen_Id, E);
7395 Set_Etype (Gen_Id, Etype (E));
7396 Set_Entity (S, E);
7397 Set_Etype (S, Etype (E));
7398
7399 -- Indicate that this is a reference to the parent
7400
7401 if In_Extended_Main_Source_Unit (Gen_Id) then
7402 Set_Is_Instantiated (Inst_Par);
7403 end if;
7404
7405 -- A common mistake is to replicate the naming scheme of a
7406 -- hierarchy by instantiating a generic child directly, rather
7407 -- than the implicit child in a parent instance:
7408
7409 -- generic .. package Gpar is ..
7410 -- generic .. package Gpar.Child is ..
7411 -- package Par is new Gpar ();
7412
7413 -- with Gpar.Child;
7414 -- package Par.Child is new Gpar.Child ();
7415 -- rather than Par.Child
7416
7417 -- In this case the instantiation is within Par, which is an
7418 -- instance, but Gpar does not denote Par because we are not IN
7419 -- the instance of Gpar, so this is illegal. The test below
7420 -- recognizes this particular case.
7421
7422 if Is_Child_Unit (E)
7423 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7424 and then (not In_Instance
7425 or else Nkind (Parent (Parent (Gen_Id))) =
7426 N_Compilation_Unit)
7427 then
7428 Error_Msg_N
7429 ("prefix of generic child unit must be instance of parent",
7430 Gen_Id);
7431 end if;
7432
7433 if not In_Open_Scopes (Inst_Par)
7434 and then Nkind (Parent (Gen_Id)) not in
7435 N_Generic_Renaming_Declaration
7436 then
7437 Install_Parent (Inst_Par);
7438 Parent_Installed := True;
7439
7440 elsif In_Open_Scopes (Inst_Par) then
7441
7442 -- If the parent is already installed, install the actuals
7443 -- for its formal packages. This is necessary when the child
7444 -- instance is a child of the parent instance: in this case,
7445 -- the parent is placed on the scope stack but the formal
7446 -- packages are not made visible.
7447
7448 Install_Formal_Packages (Inst_Par);
7449 end if;
7450
7451 else
7452 -- If the generic parent does not contain an entity that
7453 -- corresponds to the selector, the instance doesn't either.
7454 -- Analyzing the node will yield the appropriate error message.
7455 -- If the entity is not a child unit, then it is an inner
7456 -- generic in the parent.
7457
7458 Analyze (Gen_Id);
7459 end if;
7460
7461 else
7462 Analyze (Gen_Id);
7463
7464 if Is_Child_Unit (Entity (Gen_Id))
7465 and then
7466 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7467 and then not In_Open_Scopes (Inst_Par)
7468 then
7469 Install_Parent (Inst_Par);
7470 Parent_Installed := True;
7471
7472 -- The generic unit may be the renaming of the implicit child
7473 -- present in an instance. In that case the parent instance is
7474 -- obtained from the name of the renamed entity.
7475
7476 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7477 and then Present (Renamed_Entity (Entity (Gen_Id)))
7478 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7479 then
7480 declare
7481 Renamed_Package : constant Node_Id :=
7482 Name (Parent (Entity (Gen_Id)));
7483 begin
7484 if Nkind (Renamed_Package) = N_Expanded_Name then
7485 Inst_Par := Entity (Prefix (Renamed_Package));
7486 Install_Parent (Inst_Par);
7487 Parent_Installed := True;
7488 end if;
7489 end;
7490 end if;
7491 end if;
7492
7493 elsif Nkind (Gen_Id) = N_Expanded_Name then
7494
7495 -- Entity already present, analyze prefix, whose meaning may be an
7496 -- instance in the current context. If it is an instance of a
7497 -- relative within another, the proper parent may still have to be
7498 -- installed, if they are not of the same generation.
7499
7500 Analyze (Prefix (Gen_Id));
7501
7502 -- Prevent cascaded errors
7503
7504 if Etype (Prefix (Gen_Id)) = Any_Type then
7505 return;
7506 end if;
7507
7508 -- In the unlikely case that a local declaration hides the name of
7509 -- the parent package, locate it on the homonym chain. If the context
7510 -- is an instance of the parent, the renaming entity is flagged as
7511 -- such.
7512
7513 Inst_Par := Entity (Prefix (Gen_Id));
7514 while Present (Inst_Par)
7515 and then not Is_Package_Or_Generic_Package (Inst_Par)
7516 loop
7517 Inst_Par := Homonym (Inst_Par);
7518 end loop;
7519
7520 pragma Assert (Present (Inst_Par));
7521 Set_Entity (Prefix (Gen_Id), Inst_Par);
7522
7523 if In_Enclosing_Instance then
7524 null;
7525
7526 elsif Present (Entity (Gen_Id))
7527 and then No (Renamed_Entity (Entity (Gen_Id)))
7528 and then Is_Child_Unit (Entity (Gen_Id))
7529 and then not In_Open_Scopes (Inst_Par)
7530 then
7531 Install_Parent (Inst_Par);
7532 Parent_Installed := True;
7533
7534 -- Handle renaming of generic child unit
7535
7536 elsif Present (Entity (Gen_Id))
7537 and then Present (Renamed_Entity (Entity (Gen_Id)))
7538 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7539 then
7540 declare
7541 E : Entity_Id;
7542 Ren_Decl : Node_Id;
7543
7544 begin
7545 -- The entity of the renamed generic child unit does not
7546 -- have any reference to the instantiated parent. In order to
7547 -- locate it we traverse the scope containing the renaming
7548 -- declaration; the instance of the parent is available in
7549 -- the prefix of the renaming declaration. For example:
7550
7551 -- package A is
7552 -- package Inst_Par is new ...
7553 -- generic package Ren_Child renames Ins_Par.Child;
7554 -- end;
7555
7556 -- with A;
7557 -- package B is
7558 -- package Inst_Child is new A.Ren_Child;
7559 -- end;
7560
7561 E := First_Entity (Entity (Prefix (Gen_Id)));
7562 while Present (E) loop
7563 if Present (Renamed_Entity (E))
7564 and then
7565 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7566 then
7567 Ren_Decl := Parent (E);
7568 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7569
7570 if not In_Open_Scopes (Inst_Par) then
7571 Install_Parent (Inst_Par);
7572 Parent_Installed := True;
7573 end if;
7574
7575 exit;
7576 end if;
7577
7578 E := Next_Entity (E);
7579 end loop;
7580 end;
7581 end if;
7582
7583 elsif In_Enclosing_Instance then
7584
7585 -- The child unit is found in some enclosing scope
7586
7587 null;
7588
7589 else
7590 Analyze (Gen_Id);
7591
7592 -- If this is the renaming of the implicit child in a parent
7593 -- instance, recover the parent name and install it.
7594
7595 if Is_Entity_Name (Gen_Id) then
7596 E := Entity (Gen_Id);
7597
7598 if Is_Generic_Unit (E)
7599 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7600 and then Is_Child_Unit (Renamed_Object (E))
7601 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7602 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7603 then
7604 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7605 Inst_Par := Entity (Prefix (Gen_Id));
7606
7607 if not In_Open_Scopes (Inst_Par) then
7608 Install_Parent (Inst_Par);
7609 Parent_Installed := True;
7610 end if;
7611
7612 -- If it is a child unit of a non-generic parent, it may be
7613 -- use-visible and given by a direct name. Install parent as
7614 -- for other cases.
7615
7616 elsif Is_Generic_Unit (E)
7617 and then Is_Child_Unit (E)
7618 and then
7619 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7620 and then not Is_Generic_Unit (Scope (E))
7621 then
7622 if not In_Open_Scopes (Scope (E)) then
7623 Install_Parent (Scope (E));
7624 Parent_Installed := True;
7625 end if;
7626 end if;
7627 end if;
7628 end if;
7629 end Check_Generic_Child_Unit;
7630
7631 -----------------------------
7632 -- Check_Hidden_Child_Unit --
7633 -----------------------------
7634
7635 procedure Check_Hidden_Child_Unit
7636 (N : Node_Id;
7637 Gen_Unit : Entity_Id;
7638 Act_Decl_Id : Entity_Id)
7639 is
7640 Gen_Id : constant Node_Id := Name (N);
7641
7642 begin
7643 if Is_Child_Unit (Gen_Unit)
7644 and then Is_Child_Unit (Act_Decl_Id)
7645 and then Nkind (Gen_Id) = N_Expanded_Name
7646 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7647 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7648 then
7649 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7650 Error_Msg_NE
7651 ("generic unit & is implicitly declared in &",
7652 Defining_Unit_Name (N), Gen_Unit);
7653 Error_Msg_N ("\instance must have different name",
7654 Defining_Unit_Name (N));
7655 end if;
7656 end Check_Hidden_Child_Unit;
7657
7658 ------------------------
7659 -- Check_Private_View --
7660 ------------------------
7661
7662 procedure Check_Private_View (N : Node_Id) is
7663 T : constant Entity_Id := Etype (N);
7664 BT : Entity_Id;
7665
7666 begin
7667 -- Exchange views if the type was not private in the generic but is
7668 -- private at the point of instantiation. Do not exchange views if
7669 -- the scope of the type is in scope. This can happen if both generic
7670 -- and instance are sibling units, or if type is defined in a parent.
7671 -- In this case the visibility of the type will be correct for all
7672 -- semantic checks.
7673
7674 if Present (T) then
7675 BT := Base_Type (T);
7676
7677 if Is_Private_Type (T)
7678 and then not Has_Private_View (N)
7679 and then Present (Full_View (T))
7680 and then not In_Open_Scopes (Scope (T))
7681 then
7682 -- In the generic, the full declaration was visible
7683
7684 Switch_View (T);
7685
7686 elsif Has_Private_View (N)
7687 and then not Is_Private_Type (T)
7688 and then not Has_Been_Exchanged (T)
7689 and then (not In_Open_Scopes (Scope (T))
7690 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7691 then
7692 -- In the generic, only the private declaration was visible
7693
7694 -- If the type appears in a subtype declaration, the subtype in
7695 -- instance must have a view compatible with that of its parent,
7696 -- which must be exchanged (see corresponding code in Restore_
7697 -- Private_Views) so we make an exception to the open scope rule.
7698
7699 Prepend_Elmt (T, Exchanged_Views);
7700 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7701
7702 -- Finally, a non-private subtype may have a private base type, which
7703 -- must be exchanged for consistency. This can happen when a package
7704 -- body is instantiated, when the scope stack is empty but in fact
7705 -- the subtype and the base type are declared in an enclosing scope.
7706
7707 -- Note that in this case we introduce an inconsistency in the view
7708 -- set, because we switch the base type BT, but there could be some
7709 -- private dependent subtypes of BT which remain unswitched. Such
7710 -- subtypes might need to be switched at a later point (see specific
7711 -- provision for that case in Switch_View).
7712
7713 elsif not Is_Private_Type (T)
7714 and then not Has_Private_View (N)
7715 and then Is_Private_Type (BT)
7716 and then Present (Full_View (BT))
7717 and then not Is_Generic_Type (BT)
7718 and then not In_Open_Scopes (BT)
7719 then
7720 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7721 Exchange_Declarations (BT);
7722 end if;
7723 end if;
7724 end Check_Private_View;
7725
7726 -----------------------------
7727 -- Check_Hidden_Primitives --
7728 -----------------------------
7729
7730 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7731 Actual : Node_Id;
7732 Gen_T : Entity_Id;
7733 Result : Elist_Id := No_Elist;
7734
7735 begin
7736 if No (Assoc_List) then
7737 return No_Elist;
7738 end if;
7739
7740 -- Traverse the list of associations between formals and actuals
7741 -- searching for renamings of tagged types
7742
7743 Actual := First (Assoc_List);
7744 while Present (Actual) loop
7745 if Nkind (Actual) = N_Subtype_Declaration then
7746 Gen_T := Generic_Parent_Type (Actual);
7747
7748 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7749
7750 -- Traverse the list of primitives of the actual types
7751 -- searching for hidden primitives that are visible in the
7752 -- corresponding generic formal; leave them visible and
7753 -- append them to Result to restore their decoration later.
7754
7755 Install_Hidden_Primitives
7756 (Prims_List => Result,
7757 Gen_T => Gen_T,
7758 Act_T => Entity (Subtype_Indication (Actual)));
7759 end if;
7760 end if;
7761
7762 Next (Actual);
7763 end loop;
7764
7765 return Result;
7766 end Check_Hidden_Primitives;
7767
7768 --------------------------
7769 -- Contains_Instance_Of --
7770 --------------------------
7771
7772 function Contains_Instance_Of
7773 (Inner : Entity_Id;
7774 Outer : Entity_Id;
7775 N : Node_Id) return Boolean
7776 is
7777 Elmt : Elmt_Id;
7778 Scop : Entity_Id;
7779
7780 begin
7781 Scop := Outer;
7782
7783 -- Verify that there are no circular instantiations. We check whether
7784 -- the unit contains an instance of the current scope or some enclosing
7785 -- scope (in case one of the instances appears in a subunit). Longer
7786 -- circularities involving subunits might seem too pathological to
7787 -- consider, but they were not too pathological for the authors of
7788 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7789 -- enclosing generic scopes as containing an instance.
7790
7791 loop
7792 -- Within a generic subprogram body, the scope is not generic, to
7793 -- allow for recursive subprograms. Use the declaration to determine
7794 -- whether this is a generic unit.
7795
7796 if Ekind (Scop) = E_Generic_Package
7797 or else (Is_Subprogram (Scop)
7798 and then Nkind (Unit_Declaration_Node (Scop)) =
7799 N_Generic_Subprogram_Declaration)
7800 then
7801 Elmt := First_Elmt (Inner_Instances (Inner));
7802
7803 while Present (Elmt) loop
7804 if Node (Elmt) = Scop then
7805 Error_Msg_Node_2 := Inner;
7806 Error_Msg_NE
7807 ("circular instantiation: & instantiated within &!",
7808 N, Scop);
7809 return True;
7810
7811 elsif Node (Elmt) = Inner then
7812 return True;
7813
7814 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7815 Error_Msg_Node_2 := Inner;
7816 Error_Msg_NE
7817 ("circular instantiation: & instantiated within &!",
7818 N, Node (Elmt));
7819 return True;
7820 end if;
7821
7822 Next_Elmt (Elmt);
7823 end loop;
7824
7825 -- Indicate that Inner is being instantiated within Scop
7826
7827 Append_Elmt (Inner, Inner_Instances (Scop));
7828 end if;
7829
7830 if Scop = Standard_Standard then
7831 exit;
7832 else
7833 Scop := Scope (Scop);
7834 end if;
7835 end loop;
7836
7837 return False;
7838 end Contains_Instance_Of;
7839
7840 -----------------------
7841 -- Copy_Generic_Node --
7842 -----------------------
7843
7844 function Copy_Generic_Node
7845 (N : Node_Id;
7846 Parent_Id : Node_Id;
7847 Instantiating : Boolean) return Node_Id
7848 is
7849 Ent : Entity_Id;
7850 New_N : Node_Id;
7851
7852 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7853 -- Check the given value of one of the Fields referenced by the current
7854 -- node to determine whether to copy it recursively. The field may hold
7855 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7856 -- Char) in which case it need not be copied.
7857
7858 procedure Copy_Descendants;
7859 -- Common utility for various nodes
7860
7861 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7862 -- Make copy of element list
7863
7864 function Copy_Generic_List
7865 (L : List_Id;
7866 Parent_Id : Node_Id) return List_Id;
7867 -- Apply Copy_Node recursively to the members of a node list
7868
7869 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7870 -- True if an identifier is part of the defining program unit name of
7871 -- a child unit.
7872 -- Consider removing this subprogram now that ASIS no longer uses it.
7873
7874 ----------------------
7875 -- Copy_Descendants --
7876 ----------------------
7877
7878 procedure Copy_Descendants is
7879 procedure Walk is new
7880 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
7881 begin
7882 Walk (New_N, N);
7883 end Copy_Descendants;
7884
7885 -----------------------------
7886 -- Copy_Generic_Descendant --
7887 -----------------------------
7888
7889 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7890 begin
7891 if D = Union_Id (Empty) then
7892 return D;
7893
7894 elsif D in Node_Range then
7895 return Union_Id
7896 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7897
7898 elsif D in List_Range then
7899 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7900
7901 elsif D in Elist_Range then
7902 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7903
7904 -- Nothing else is copyable (e.g. Uint values), return as is
7905
7906 else
7907 return D;
7908 end if;
7909 end Copy_Generic_Descendant;
7910
7911 ------------------------
7912 -- Copy_Generic_Elist --
7913 ------------------------
7914
7915 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7916 M : Elmt_Id;
7917 L : Elist_Id;
7918
7919 begin
7920 if Present (E) then
7921 L := New_Elmt_List;
7922 M := First_Elmt (E);
7923 while Present (M) loop
7924 Append_Elmt
7925 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7926 Next_Elmt (M);
7927 end loop;
7928
7929 return L;
7930
7931 else
7932 return No_Elist;
7933 end if;
7934 end Copy_Generic_Elist;
7935
7936 -----------------------
7937 -- Copy_Generic_List --
7938 -----------------------
7939
7940 function Copy_Generic_List
7941 (L : List_Id;
7942 Parent_Id : Node_Id) return List_Id
7943 is
7944 N : Node_Id;
7945 New_L : List_Id;
7946
7947 begin
7948 if Present (L) then
7949 New_L := New_List;
7950 Set_Parent (New_L, Parent_Id);
7951
7952 N := First (L);
7953 while Present (N) loop
7954 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7955 Next (N);
7956 end loop;
7957
7958 return New_L;
7959
7960 else
7961 return No_List;
7962 end if;
7963 end Copy_Generic_List;
7964
7965 ---------------------------
7966 -- In_Defining_Unit_Name --
7967 ---------------------------
7968
7969 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7970 begin
7971 return
7972 Present (Parent (Nam))
7973 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7974 or else
7975 (Nkind (Parent (Nam)) = N_Expanded_Name
7976 and then In_Defining_Unit_Name (Parent (Nam))));
7977 end In_Defining_Unit_Name;
7978
7979 -- Start of processing for Copy_Generic_Node
7980
7981 begin
7982 if N = Empty then
7983 return N;
7984 end if;
7985
7986 New_N := New_Copy (N);
7987
7988 -- Copy aspects if present
7989
7990 if Has_Aspects (N) then
7991 Set_Has_Aspects (New_N, False);
7992 Set_Aspect_Specifications
7993 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7994 end if;
7995
7996 -- If we are instantiating, we want to adjust the sloc based on the
7997 -- current S_Adjustment. However, if this is the root node of a subunit,
7998 -- we need to defer that adjustment to below (see "elsif Instantiating
7999 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8000 -- computed the adjustment.
8001
8002 if Instantiating
8003 and then not (Nkind (N) in N_Proper_Body
8004 and then Was_Originally_Stub (N))
8005 then
8006 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8007 end if;
8008
8009 if not Is_List_Member (N) then
8010 Set_Parent (New_N, Parent_Id);
8011 end if;
8012
8013 -- Special casing for identifiers and other entity names and operators
8014
8015 if Nkind (New_N) in N_Character_Literal
8016 | N_Expanded_Name
8017 | N_Identifier
8018 | N_Operator_Symbol
8019 | N_Op
8020 then
8021 if not Instantiating then
8022
8023 -- Link both nodes in order to assign subsequently the entity of
8024 -- the copy to the original node, in case this is a global
8025 -- reference.
8026
8027 Set_Associated_Node (N, New_N);
8028
8029 -- If we are within an instantiation, this is a nested generic
8030 -- that has already been analyzed at the point of definition.
8031 -- We must preserve references that were global to the enclosing
8032 -- parent at that point. Other occurrences, whether global or
8033 -- local to the current generic, must be resolved anew, so we
8034 -- reset the entity in the generic copy. A global reference has a
8035 -- smaller depth than the parent, or else the same depth in case
8036 -- both are distinct compilation units.
8037
8038 -- A child unit is implicitly declared within the enclosing parent
8039 -- but is in fact global to it, and must be preserved.
8040
8041 -- It is also possible for Current_Instantiated_Parent to be
8042 -- defined, and for this not to be a nested generic, namely if
8043 -- the unit is loaded through Rtsfind. In that case, the entity of
8044 -- New_N is only a link to the associated node, and not a defining
8045 -- occurrence.
8046
8047 -- The entities for parent units in the defining_program_unit of a
8048 -- generic child unit are established when the context of the unit
8049 -- is first analyzed, before the generic copy is made. They are
8050 -- preserved in the copy for use in e.g. ASIS queries.
8051
8052 Ent := Entity (New_N);
8053
8054 if No (Current_Instantiated_Parent.Gen_Id) then
8055 if No (Ent)
8056 or else Nkind (Ent) /= N_Defining_Identifier
8057 or else not In_Defining_Unit_Name (N)
8058 then
8059 Set_Associated_Node (New_N, Empty);
8060 end if;
8061
8062 elsif No (Ent)
8063 or else Nkind (Ent) not in N_Entity
8064 or else No (Scope (Ent))
8065 or else
8066 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8067 and then not Is_Child_Unit (Ent))
8068 or else
8069 (Scope_Depth (Scope (Ent)) >
8070 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8071 and then
8072 Get_Source_Unit (Ent) =
8073 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8074 then
8075 Set_Associated_Node (New_N, Empty);
8076 end if;
8077
8078 -- Case of instantiating identifier or some other name or operator
8079
8080 else
8081 -- If the associated node is still defined, the entity in it
8082 -- is global, and must be copied to the instance. If this copy
8083 -- is being made for a body to inline, it is applied to an
8084 -- instantiated tree, and the entity is already present and
8085 -- must be also preserved.
8086
8087 declare
8088 Assoc : constant Node_Id := Get_Associated_Node (N);
8089
8090 begin
8091 if Present (Assoc) then
8092 if Nkind (Assoc) = Nkind (N) then
8093 Set_Entity (New_N, Entity (Assoc));
8094 Check_Private_View (N);
8095
8096 -- Here we deal with a very peculiar case for which the
8097 -- Has_Private_View mechanism is not sufficient, because
8098 -- the reference to the type is implicit in the tree,
8099 -- that is to say, it's not referenced from a node but
8100 -- only from another type, namely through Component_Type.
8101
8102 -- package P is
8103
8104 -- type Pt is private;
8105
8106 -- generic
8107 -- type Ft is array (Positive range <>) of Pt;
8108 -- package G is
8109 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8110 -- end G;
8111
8112 -- private
8113 -- type Pt is new Boolean;
8114 -- end P;
8115
8116 -- package body P is
8117 -- package body G is
8118 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8119 -- begin
8120 -- if (F1 < F2) /= Lt then
8121 -- null;
8122 -- end if;
8123 -- end Check;
8124 -- end G;
8125 -- end P;
8126
8127 -- type Arr is array (Positive range <>) of P.Pt;
8128
8129 -- package Inst is new P.G (Arr);
8130
8131 -- Pt is a global type for the generic package G and it
8132 -- is not referenced in its body, but only as component
8133 -- type of Ft, which is a local type. This means that no
8134 -- references to Pt or Ft are seen during the copy of the
8135 -- body, the only reference to Pt being seen is when the
8136 -- actuals are checked by Check_Generic_Actuals, but Pt
8137 -- is still private at this point. In the end, the views
8138 -- of Pt are not switched in the body and, therefore, the
8139 -- array comparison is rejected because the component is
8140 -- still private.
8141
8142 -- Adding e.g. a dummy variable of type Pt in the body is
8143 -- sufficient to make everything work, so we generate an
8144 -- artificial reference to Pt on the fly and thus force
8145 -- the switching of views on the grounds that, if the
8146 -- comparison was accepted during the semantic analysis
8147 -- of the generic, this means that the component cannot
8148 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8149
8150 if Nkind (Assoc) in N_Op_Compare
8151 and then Present (Etype (Left_Opnd (Assoc)))
8152 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8153 and then Present (Etype (Right_Opnd (Assoc)))
8154 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8155 then
8156 declare
8157 Ltyp : constant Entity_Id :=
8158 Etype (Left_Opnd (Assoc));
8159 Rtyp : constant Entity_Id :=
8160 Etype (Right_Opnd (Assoc));
8161 begin
8162 if Is_Private_Type (Component_Type (Ltyp)) then
8163 Check_Private_View
8164 (New_Occurrence_Of (Component_Type (Ltyp),
8165 Sloc (N)));
8166 end if;
8167 if Is_Private_Type (Component_Type (Rtyp)) then
8168 Check_Private_View
8169 (New_Occurrence_Of (Component_Type (Rtyp),
8170 Sloc (N)));
8171 end if;
8172 end;
8173
8174 -- Here is a similar case, for the Designated_Type of an
8175 -- access type that is present as target type in a type
8176 -- conversion from another access type. In this case, if
8177 -- the base types of the designated types are different
8178 -- and the conversion was accepted during the semantic
8179 -- analysis of the generic, this means that the target
8180 -- type cannot have been private (see Valid_Conversion).
8181
8182 elsif Nkind (Assoc) = N_Identifier
8183 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8184 and then Subtype_Mark (Parent (Assoc)) = Assoc
8185 and then Present (Etype (Assoc))
8186 and then Is_Access_Type (Etype (Assoc))
8187 and then Present (Etype (Expression (Parent (Assoc))))
8188 and then
8189 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8190 then
8191 declare
8192 Targ_Desig : constant Entity_Id :=
8193 Designated_Type (Etype (Assoc));
8194 Expr_Desig : constant Entity_Id :=
8195 Designated_Type
8196 (Etype (Expression (Parent (Assoc))));
8197 begin
8198 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8199 and then Is_Private_Type (Targ_Desig)
8200 then
8201 Check_Private_View
8202 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8203 end if;
8204 end;
8205 end if;
8206
8207 -- The node is a reference to a global type and acts as the
8208 -- subtype mark of a qualified expression created in order
8209 -- to aid resolution of accidental overloading in instances.
8210 -- Since N is a reference to a type, the Associated_Node of
8211 -- N denotes an entity rather than another identifier. See
8212 -- Qualify_Universal_Operands for details.
8213
8214 elsif Nkind (N) = N_Identifier
8215 and then Nkind (Parent (N)) = N_Qualified_Expression
8216 and then Subtype_Mark (Parent (N)) = N
8217 and then Is_Qualified_Universal_Literal (Parent (N))
8218 then
8219 Set_Entity (New_N, Assoc);
8220
8221 -- The name in the call may be a selected component if the
8222 -- call has not been analyzed yet, as may be the case for
8223 -- pre/post conditions in a generic unit.
8224
8225 elsif Nkind (Assoc) = N_Function_Call
8226 and then Is_Entity_Name (Name (Assoc))
8227 then
8228 Set_Entity (New_N, Entity (Name (Assoc)));
8229
8230 elsif Nkind (Assoc) in N_Entity
8231 and then Expander_Active
8232 then
8233 -- Inlining case: we are copying a tree that contains
8234 -- global entities, which are preserved in the copy to be
8235 -- used for subsequent inlining.
8236
8237 null;
8238
8239 else
8240 Set_Entity (New_N, Empty);
8241 end if;
8242 end if;
8243 end;
8244 end if;
8245
8246 -- For expanded name, we must copy the Prefix and Selector_Name
8247
8248 if Nkind (N) = N_Expanded_Name then
8249 Set_Prefix
8250 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8251
8252 Set_Selector_Name (New_N,
8253 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8254
8255 -- For operators, copy the operands
8256
8257 elsif Nkind (N) in N_Op then
8258 if Nkind (N) in N_Binary_Op then
8259 Set_Left_Opnd (New_N,
8260 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8261 end if;
8262
8263 Set_Right_Opnd (New_N,
8264 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8265 end if;
8266
8267 -- Establish a link between an entity from the generic template and the
8268 -- corresponding entity in the generic copy to be analyzed.
8269
8270 elsif Nkind (N) in N_Entity then
8271 if not Instantiating then
8272 Set_Associated_Entity (N, New_N);
8273 end if;
8274
8275 -- Clear any existing link the copy may inherit from the replicated
8276 -- generic template entity.
8277
8278 Set_Associated_Entity (New_N, Empty);
8279
8280 -- Special casing for stubs
8281
8282 elsif Nkind (N) in N_Body_Stub then
8283
8284 -- In any case, we must copy the specification or defining
8285 -- identifier as appropriate.
8286
8287 if Nkind (N) = N_Subprogram_Body_Stub then
8288 Set_Specification (New_N,
8289 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8290
8291 else
8292 Set_Defining_Identifier (New_N,
8293 Copy_Generic_Node
8294 (Defining_Identifier (N), New_N, Instantiating));
8295 end if;
8296
8297 -- If we are not instantiating, then this is where we load and
8298 -- analyze subunits, i.e. at the point where the stub occurs. A
8299 -- more permissive system might defer this analysis to the point
8300 -- of instantiation, but this seems too complicated for now.
8301
8302 if not Instantiating then
8303 declare
8304 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8305 Subunit : Node_Id;
8306 Unum : Unit_Number_Type;
8307 New_Body : Node_Id;
8308
8309 begin
8310 -- Make sure that, if it is a subunit of the main unit that is
8311 -- preprocessed and if -gnateG is specified, the preprocessed
8312 -- file will be written.
8313
8314 Lib.Analysing_Subunit_Of_Main :=
8315 Lib.In_Extended_Main_Source_Unit (N);
8316 Unum :=
8317 Load_Unit
8318 (Load_Name => Subunit_Name,
8319 Required => False,
8320 Subunit => True,
8321 Error_Node => N);
8322 Lib.Analysing_Subunit_Of_Main := False;
8323
8324 -- If the proper body is not found, a warning message will be
8325 -- emitted when analyzing the stub, or later at the point of
8326 -- instantiation. Here we just leave the stub as is.
8327
8328 if Unum = No_Unit then
8329 Subunits_Missing := True;
8330 goto Subunit_Not_Found;
8331 end if;
8332
8333 Subunit := Cunit (Unum);
8334
8335 if Nkind (Unit (Subunit)) /= N_Subunit then
8336 Error_Msg_N
8337 ("found child unit instead of expected SEPARATE subunit",
8338 Subunit);
8339 Error_Msg_Sloc := Sloc (N);
8340 Error_Msg_N ("\to complete stub #", Subunit);
8341 goto Subunit_Not_Found;
8342 end if;
8343
8344 -- We must create a generic copy of the subunit, in order to
8345 -- perform semantic analysis on it, and we must replace the
8346 -- stub in the original generic unit with the subunit, in order
8347 -- to preserve non-local references within.
8348
8349 -- Only the proper body needs to be copied. Library_Unit and
8350 -- context clause are simply inherited by the generic copy.
8351 -- Note that the copy (which may be recursive if there are
8352 -- nested subunits) must be done first, before attaching it to
8353 -- the enclosing generic.
8354
8355 New_Body :=
8356 Copy_Generic_Node
8357 (Proper_Body (Unit (Subunit)),
8358 Empty, Instantiating => False);
8359
8360 -- Now place the original proper body in the original generic
8361 -- unit. This is a body, not a compilation unit.
8362
8363 Rewrite (N, Proper_Body (Unit (Subunit)));
8364 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8365 Set_Was_Originally_Stub (N);
8366
8367 -- Finally replace the body of the subunit with its copy, and
8368 -- make this new subunit into the library unit of the generic
8369 -- copy, which does not have stubs any longer.
8370
8371 Set_Proper_Body (Unit (Subunit), New_Body);
8372 Set_Library_Unit (New_N, Subunit);
8373 Inherit_Context (Unit (Subunit), N);
8374 end;
8375
8376 -- If we are instantiating, this must be an error case, since
8377 -- otherwise we would have replaced the stub node by the proper body
8378 -- that corresponds. So just ignore it in the copy (i.e. we have
8379 -- copied it, and that is good enough).
8380
8381 else
8382 null;
8383 end if;
8384
8385 <<Subunit_Not_Found>> null;
8386
8387 -- If the node is a compilation unit, it is the subunit of a stub, which
8388 -- has been loaded already (see code below). In this case, the library
8389 -- unit field of N points to the parent unit (which is a compilation
8390 -- unit) and need not (and cannot) be copied.
8391
8392 -- When the proper body of the stub is analyzed, the library_unit link
8393 -- is used to establish the proper context (see sem_ch10).
8394
8395 -- The other fields of a compilation unit are copied as usual
8396
8397 elsif Nkind (N) = N_Compilation_Unit then
8398
8399 -- This code can only be executed when not instantiating, because in
8400 -- the copy made for an instantiation, the compilation unit node has
8401 -- disappeared at the point that a stub is replaced by its proper
8402 -- body.
8403
8404 pragma Assert (not Instantiating);
8405
8406 Set_Context_Items (New_N,
8407 Copy_Generic_List (Context_Items (N), New_N));
8408
8409 Set_Unit (New_N,
8410 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8411
8412 Set_First_Inlined_Subprogram (New_N,
8413 Copy_Generic_Node
8414 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8415
8416 Set_Aux_Decls_Node
8417 (New_N,
8418 Copy_Generic_Node
8419 (Aux_Decls_Node (N), New_N, Instantiating => False));
8420
8421 -- For an assignment node, the assignment is known to be semantically
8422 -- legal if we are instantiating the template. This avoids incorrect
8423 -- diagnostics in generated code.
8424
8425 elsif Nkind (N) = N_Assignment_Statement then
8426
8427 -- Copy name and expression fields in usual manner
8428
8429 Set_Name (New_N,
8430 Copy_Generic_Node (Name (N), New_N, Instantiating));
8431
8432 Set_Expression (New_N,
8433 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8434
8435 if Instantiating then
8436 Set_Assignment_OK (Name (New_N), True);
8437 end if;
8438
8439 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8440 if not Instantiating then
8441 Set_Associated_Node (N, New_N);
8442
8443 else
8444 if Present (Get_Associated_Node (N))
8445 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8446 then
8447 -- In the generic the aggregate has some composite type. If at
8448 -- the point of instantiation the type has a private view,
8449 -- install the full view (and that of its ancestors, if any).
8450
8451 declare
8452 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8453 Rt : Entity_Id;
8454
8455 begin
8456 if Present (T) and then Is_Private_Type (T) then
8457 Switch_View (T);
8458 end if;
8459
8460 if Present (T)
8461 and then Is_Tagged_Type (T)
8462 and then Is_Derived_Type (T)
8463 then
8464 Rt := Root_Type (T);
8465
8466 loop
8467 T := Etype (T);
8468
8469 if Is_Private_Type (T) then
8470 Switch_View (T);
8471 end if;
8472
8473 exit when T = Rt;
8474 end loop;
8475 end if;
8476 end;
8477 end if;
8478 end if;
8479
8480 -- Do not copy the associated node, which points to the generic copy
8481 -- of the aggregate.
8482 -- ????We ought to be able to get rid of all the Union_Id conversions
8483
8484 if Nkind (N) = N_Aggregate then
8485 Set_Aggregate_Bounds
8486 (New_N,
8487 Node_Id (Copy_Generic_Descendant
8488 (Union_Id (Aggregate_Bounds (N)))));
8489
8490 elsif Nkind (N) = N_Extension_Aggregate then
8491 Set_Ancestor_Part
8492 (New_N,
8493 Node_Id (Copy_Generic_Descendant
8494 (Union_Id (Ancestor_Part (N)))));
8495
8496 else
8497 pragma Assert (False);
8498 end if;
8499
8500 Set_Expressions
8501 (New_N,
8502 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
8503 Set_Component_Associations
8504 (New_N,
8505 List_Id (Copy_Generic_Descendant
8506 (Union_Id (Component_Associations (N)))));
8507 Set_Etype
8508 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
8509
8510 -- Allocators do not have an identifier denoting the access type, so we
8511 -- must locate it through the expression to check whether the views are
8512 -- consistent.
8513
8514 elsif Nkind (N) = N_Allocator
8515 and then Nkind (Expression (N)) = N_Qualified_Expression
8516 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8517 and then Instantiating
8518 then
8519 declare
8520 T : constant Node_Id :=
8521 Get_Associated_Node (Subtype_Mark (Expression (N)));
8522 Acc_T : Entity_Id;
8523
8524 begin
8525 if Present (T) then
8526
8527 -- Retrieve the allocator node in the generic copy
8528
8529 Acc_T := Etype (Parent (Parent (T)));
8530
8531 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8532 Switch_View (Acc_T);
8533 end if;
8534 end if;
8535
8536 Copy_Descendants;
8537 end;
8538
8539 -- For a proper body, we must catch the case of a proper body that
8540 -- replaces a stub. This represents the point at which a separate
8541 -- compilation unit, and hence template file, may be referenced, so we
8542 -- must make a new source instantiation entry for the template of the
8543 -- subunit, and ensure that all nodes in the subunit are adjusted using
8544 -- this new source instantiation entry.
8545
8546 elsif Nkind (N) in N_Proper_Body then
8547 declare
8548 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8549 begin
8550 if Instantiating and then Was_Originally_Stub (N) then
8551 Create_Instantiation_Source
8552 (Instantiation_Node,
8553 Defining_Entity (N),
8554 S_Adjustment);
8555
8556 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8557 end if;
8558
8559 -- Now copy the fields of the proper body, using the new
8560 -- adjustment factor if one was needed as per test above.
8561
8562 Copy_Descendants;
8563
8564 -- Restore the original adjustment factor
8565
8566 S_Adjustment := Save_Adjustment;
8567 end;
8568
8569 elsif Nkind (N) = N_Pragma and then Instantiating then
8570
8571 -- Do not copy Comment or Ident pragmas their content is relevant to
8572 -- the generic unit, not to the instantiating unit.
8573
8574 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8575 New_N := Make_Null_Statement (Sloc (N));
8576
8577 -- Do not copy pragmas generated from aspects because the pragmas do
8578 -- not carry any semantic information, plus they will be regenerated
8579 -- in the instance.
8580
8581 -- However, generating C we need to copy them since postconditions
8582 -- are inlined by the front end, and the front-end inlining machinery
8583 -- relies on this routine to perform inlining.
8584
8585 elsif From_Aspect_Specification (N)
8586 and then not Modify_Tree_For_C
8587 then
8588 New_N := Make_Null_Statement (Sloc (N));
8589
8590 else
8591 Copy_Descendants;
8592 end if;
8593
8594 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8595
8596 -- No descendant fields need traversing
8597
8598 null;
8599
8600 elsif Nkind (N) = N_String_Literal
8601 and then Present (Etype (N))
8602 and then Instantiating
8603 then
8604 -- If the string is declared in an outer scope, the string_literal
8605 -- subtype created for it may have the wrong scope. Force reanalysis
8606 -- of the constant to generate a new itype in the proper context.
8607
8608 Set_Etype (New_N, Empty);
8609 Set_Analyzed (New_N, False);
8610
8611 -- For the remaining nodes, copy their descendants recursively
8612
8613 else
8614 Copy_Descendants;
8615
8616 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8617 Set_Generic_Parent (Specification (New_N), N);
8618
8619 -- Should preserve Corresponding_Spec??? (12.3(14))
8620 end if;
8621 end if;
8622
8623 -- Propagate dimensions if present, so that they are reflected in the
8624 -- instance.
8625
8626 if Nkind (N) in N_Has_Etype
8627 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8628 and then Present (Etype (N))
8629 and then Is_Floating_Point_Type (Etype (N))
8630 and then Has_Dimension_System (Etype (N))
8631 then
8632 Copy_Dimensions (N, New_N);
8633 end if;
8634
8635 return New_N;
8636 end Copy_Generic_Node;
8637
8638 ----------------------------
8639 -- Denotes_Formal_Package --
8640 ----------------------------
8641
8642 function Denotes_Formal_Package
8643 (Pack : Entity_Id;
8644 On_Exit : Boolean := False;
8645 Instance : Entity_Id := Empty) return Boolean
8646 is
8647 Par : Entity_Id;
8648 Scop : constant Entity_Id := Scope (Pack);
8649 E : Entity_Id;
8650
8651 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8652 -- The package in question may be an actual for a previous formal
8653 -- package P of the current instance, so examine its actuals as well.
8654 -- This must be recursive over other formal packages.
8655
8656 ----------------------------------
8657 -- Is_Actual_Of_Previous_Formal --
8658 ----------------------------------
8659
8660 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8661 E1 : Entity_Id;
8662
8663 begin
8664 E1 := First_Entity (P);
8665 while Present (E1) and then E1 /= Instance loop
8666 if Ekind (E1) = E_Package
8667 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8668 then
8669 if Renamed_Object (E1) = Pack then
8670 return True;
8671
8672 elsif E1 = P or else Renamed_Object (E1) = P then
8673 return False;
8674
8675 elsif Is_Actual_Of_Previous_Formal (E1) then
8676 return True;
8677 end if;
8678 end if;
8679
8680 Next_Entity (E1);
8681 end loop;
8682
8683 return False;
8684 end Is_Actual_Of_Previous_Formal;
8685
8686 -- Start of processing for Denotes_Formal_Package
8687
8688 begin
8689 if On_Exit then
8690 Par :=
8691 Instance_Envs.Table
8692 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8693 else
8694 Par := Current_Instantiated_Parent.Act_Id;
8695 end if;
8696
8697 if Ekind (Scop) = E_Generic_Package
8698 or else Nkind (Unit_Declaration_Node (Scop)) =
8699 N_Generic_Subprogram_Declaration
8700 then
8701 return True;
8702
8703 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8704 N_Formal_Package_Declaration
8705 then
8706 return True;
8707
8708 elsif No (Par) then
8709 return False;
8710
8711 else
8712 -- Check whether this package is associated with a formal package of
8713 -- the enclosing instantiation. Iterate over the list of renamings.
8714
8715 E := First_Entity (Par);
8716 while Present (E) loop
8717 if Ekind (E) /= E_Package
8718 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8719 then
8720 null;
8721
8722 elsif Renamed_Object (E) = Par then
8723 return False;
8724
8725 elsif Renamed_Object (E) = Pack then
8726 return True;
8727
8728 elsif Is_Actual_Of_Previous_Formal (E) then
8729 return True;
8730
8731 end if;
8732
8733 Next_Entity (E);
8734 end loop;
8735
8736 return False;
8737 end if;
8738 end Denotes_Formal_Package;
8739
8740 -----------------
8741 -- End_Generic --
8742 -----------------
8743
8744 procedure End_Generic is
8745 begin
8746 -- ??? More things could be factored out in this routine. Should
8747 -- probably be done at a later stage.
8748
8749 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8750 Generic_Flags.Decrement_Last;
8751
8752 Expander_Mode_Restore;
8753 end End_Generic;
8754
8755 -------------
8756 -- Earlier --
8757 -------------
8758
8759 function Earlier (N1, N2 : Node_Id) return Boolean is
8760 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8761 -- Find distance from given node to enclosing compilation unit
8762
8763 ----------------
8764 -- Find_Depth --
8765 ----------------
8766
8767 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8768 begin
8769 while Present (P)
8770 and then Nkind (P) /= N_Compilation_Unit
8771 loop
8772 P := True_Parent (P);
8773 D := D + 1;
8774 end loop;
8775 end Find_Depth;
8776
8777 -- Local declarations
8778
8779 D1 : Integer := 0;
8780 D2 : Integer := 0;
8781 P1 : Node_Id := N1;
8782 P2 : Node_Id := N2;
8783 T1 : Source_Ptr;
8784 T2 : Source_Ptr;
8785
8786 -- Start of processing for Earlier
8787
8788 begin
8789 Find_Depth (P1, D1);
8790 Find_Depth (P2, D2);
8791
8792 if P1 /= P2 then
8793 return False;
8794 else
8795 P1 := N1;
8796 P2 := N2;
8797 end if;
8798
8799 while D1 > D2 loop
8800 P1 := True_Parent (P1);
8801 D1 := D1 - 1;
8802 end loop;
8803
8804 while D2 > D1 loop
8805 P2 := True_Parent (P2);
8806 D2 := D2 - 1;
8807 end loop;
8808
8809 -- At this point P1 and P2 are at the same distance from the root.
8810 -- We examine their parents until we find a common declarative list.
8811 -- If we reach the root, N1 and N2 do not descend from the same
8812 -- declarative list (e.g. one is nested in the declarative part and
8813 -- the other is in a block in the statement part) and the earlier
8814 -- one is already frozen.
8815
8816 while not Is_List_Member (P1)
8817 or else not Is_List_Member (P2)
8818 or else not In_Same_List (P1, P2)
8819 loop
8820 P1 := True_Parent (P1);
8821 P2 := True_Parent (P2);
8822
8823 if Nkind (Parent (P1)) = N_Subunit then
8824 P1 := Corresponding_Stub (Parent (P1));
8825 end if;
8826
8827 if Nkind (Parent (P2)) = N_Subunit then
8828 P2 := Corresponding_Stub (Parent (P2));
8829 end if;
8830
8831 if P1 = P2 then
8832 return False;
8833 end if;
8834 end loop;
8835
8836 -- Expanded code usually shares the source location of the original
8837 -- construct it was generated for. This however may not necessarily
8838 -- reflect the true location of the code within the tree.
8839
8840 -- Before comparing the slocs of the two nodes, make sure that we are
8841 -- working with correct source locations. Assume that P1 is to the left
8842 -- of P2. If either one does not come from source, traverse the common
8843 -- list heading towards the other node and locate the first source
8844 -- statement.
8845
8846 -- P1 P2
8847 -- ----+===+===+--------------+===+===+----
8848 -- expanded code expanded code
8849
8850 if not Comes_From_Source (P1) then
8851 while Present (P1) loop
8852
8853 -- Neither P2 nor a source statement were located during the
8854 -- search. If we reach the end of the list, then P1 does not
8855 -- occur earlier than P2.
8856
8857 -- ---->
8858 -- start --- P2 ----- P1 --- end
8859
8860 if No (Next (P1)) then
8861 return False;
8862
8863 -- We encounter P2 while going to the right of the list. This
8864 -- means that P1 does indeed appear earlier.
8865
8866 -- ---->
8867 -- start --- P1 ===== P2 --- end
8868 -- expanded code in between
8869
8870 elsif P1 = P2 then
8871 return True;
8872
8873 -- No need to look any further since we have located a source
8874 -- statement.
8875
8876 elsif Comes_From_Source (P1) then
8877 exit;
8878 end if;
8879
8880 -- Keep going right
8881
8882 Next (P1);
8883 end loop;
8884 end if;
8885
8886 if not Comes_From_Source (P2) then
8887 while Present (P2) loop
8888
8889 -- Neither P1 nor a source statement were located during the
8890 -- search. If we reach the start of the list, then P1 does not
8891 -- occur earlier than P2.
8892
8893 -- <----
8894 -- start --- P2 --- P1 --- end
8895
8896 if No (Prev (P2)) then
8897 return False;
8898
8899 -- We encounter P1 while going to the left of the list. This
8900 -- means that P1 does indeed appear earlier.
8901
8902 -- <----
8903 -- start --- P1 ===== P2 --- end
8904 -- expanded code in between
8905
8906 elsif P2 = P1 then
8907 return True;
8908
8909 -- No need to look any further since we have located a source
8910 -- statement.
8911
8912 elsif Comes_From_Source (P2) then
8913 exit;
8914 end if;
8915
8916 -- Keep going left
8917
8918 Prev (P2);
8919 end loop;
8920 end if;
8921
8922 -- At this point either both nodes came from source or we approximated
8923 -- their source locations through neighboring source statements.
8924
8925 T1 := Top_Level_Location (Sloc (P1));
8926 T2 := Top_Level_Location (Sloc (P2));
8927
8928 -- When two nodes come from the same instance, they have identical top
8929 -- level locations. To determine proper relation within the tree, check
8930 -- their locations within the template.
8931
8932 if T1 = T2 then
8933 return Sloc (P1) < Sloc (P2);
8934
8935 -- The two nodes either come from unrelated instances or do not come
8936 -- from instantiated code at all.
8937
8938 else
8939 return T1 < T2;
8940 end if;
8941 end Earlier;
8942
8943 ----------------------
8944 -- Find_Actual_Type --
8945 ----------------------
8946
8947 function Find_Actual_Type
8948 (Typ : Entity_Id;
8949 Gen_Type : Entity_Id) return Entity_Id
8950 is
8951 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8952 T : Entity_Id;
8953
8954 begin
8955 -- Special processing only applies to child units
8956
8957 if not Is_Child_Unit (Gen_Scope) then
8958 return Get_Instance_Of (Typ);
8959
8960 -- If designated or component type is itself a formal of the child unit,
8961 -- its instance is available.
8962
8963 elsif Scope (Typ) = Gen_Scope then
8964 return Get_Instance_Of (Typ);
8965
8966 -- If the array or access type is not declared in the parent unit,
8967 -- no special processing needed.
8968
8969 elsif not Is_Generic_Type (Typ)
8970 and then Scope (Gen_Scope) /= Scope (Typ)
8971 then
8972 return Get_Instance_Of (Typ);
8973
8974 -- Otherwise, retrieve designated or component type by visibility
8975
8976 else
8977 T := Current_Entity (Typ);
8978 while Present (T) loop
8979 if In_Open_Scopes (Scope (T)) then
8980 return T;
8981 elsif Is_Generic_Actual_Type (T) then
8982 return T;
8983 end if;
8984
8985 T := Homonym (T);
8986 end loop;
8987
8988 return Typ;
8989 end if;
8990 end Find_Actual_Type;
8991
8992 ----------------------------
8993 -- Freeze_Subprogram_Body --
8994 ----------------------------
8995
8996 procedure Freeze_Subprogram_Body
8997 (Inst_Node : Node_Id;
8998 Gen_Body : Node_Id;
8999 Pack_Id : Entity_Id)
9000 is
9001 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
9002 Par : constant Entity_Id := Scope (Gen_Unit);
9003 Enc_G : Entity_Id;
9004 Enc_G_F : Node_Id;
9005 Enc_I : Node_Id;
9006 F_Node : Node_Id;
9007
9008 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
9009 -- Find innermost package body that encloses the given node, and which
9010 -- is not a compilation unit. Freeze nodes for the instance, or for its
9011 -- enclosing body, may be inserted after the enclosing_body of the
9012 -- generic unit. Used to determine proper placement of freeze node for
9013 -- both package and subprogram instances.
9014
9015 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9016 -- Find entity for given package body, and locate or create a freeze
9017 -- node for it.
9018
9019 ----------------------------
9020 -- Enclosing_Package_Body --
9021 ----------------------------
9022
9023 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9024 P : Node_Id;
9025
9026 begin
9027 P := Parent (N);
9028 while Present (P)
9029 and then Nkind (Parent (P)) /= N_Compilation_Unit
9030 loop
9031 if Nkind (P) = N_Package_Body then
9032 if Nkind (Parent (P)) = N_Subunit then
9033 return Corresponding_Stub (Parent (P));
9034 else
9035 return P;
9036 end if;
9037 end if;
9038
9039 P := True_Parent (P);
9040 end loop;
9041
9042 return Empty;
9043 end Enclosing_Package_Body;
9044
9045 -------------------------
9046 -- Package_Freeze_Node --
9047 -------------------------
9048
9049 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9050 Id : Entity_Id;
9051
9052 begin
9053 if Nkind (B) = N_Package_Body then
9054 Id := Corresponding_Spec (B);
9055 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9056 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9057 end if;
9058
9059 Ensure_Freeze_Node (Id);
9060 return Freeze_Node (Id);
9061 end Package_Freeze_Node;
9062
9063 -- Start of processing for Freeze_Subprogram_Body
9064
9065 begin
9066 -- If the instance and the generic body appear within the same unit, and
9067 -- the instance precedes the generic, the freeze node for the instance
9068 -- must appear after that of the generic. If the generic is nested
9069 -- within another instance I2, then current instance must be frozen
9070 -- after I2. In both cases, the freeze nodes are those of enclosing
9071 -- packages. Otherwise, the freeze node is placed at the end of the
9072 -- current declarative part.
9073
9074 Enc_G := Enclosing_Package_Body (Gen_Body);
9075 Enc_I := Enclosing_Package_Body (Inst_Node);
9076 Ensure_Freeze_Node (Pack_Id);
9077 F_Node := Freeze_Node (Pack_Id);
9078
9079 if Is_Generic_Instance (Par)
9080 and then Present (Freeze_Node (Par))
9081 and then In_Same_Declarative_Part
9082 (Parent (Freeze_Node (Par)), Inst_Node)
9083 then
9084 -- The parent was a premature instantiation. Insert freeze node at
9085 -- the end the current declarative part.
9086
9087 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
9088 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9089
9090 -- Handle the following case:
9091 --
9092 -- package Parent_Inst is new ...
9093 -- Parent_Inst []
9094 --
9095 -- procedure P ... -- this body freezes Parent_Inst
9096 --
9097 -- procedure Inst is new ...
9098 --
9099 -- In this particular scenario, the freeze node for Inst must be
9100 -- inserted in the same manner as that of Parent_Inst - before the
9101 -- next source body or at the end of the declarative list (body not
9102 -- available). If body P did not exist and Parent_Inst was frozen
9103 -- after Inst, either by a body following Inst or at the end of the
9104 -- declarative region, the freeze node for Inst must be inserted
9105 -- after that of Parent_Inst. This relation is established by
9106 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9107
9108 elsif In_Same_List (Get_Unit_Instantiation_Node (Par), Inst_Node)
9109 and then Sloc (Freeze_Node (Par)) <= Sloc (Inst_Node)
9110 then
9111 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9112
9113 else
9114 Insert_After (Freeze_Node (Par), F_Node);
9115 end if;
9116
9117 -- The body enclosing the instance should be frozen after the body that
9118 -- includes the generic, because the body of the instance may make
9119 -- references to entities therein. If the two are not in the same
9120 -- declarative part, or if the one enclosing the instance is frozen
9121 -- already, freeze the instance at the end of the current declarative
9122 -- part.
9123
9124 elsif Is_Generic_Instance (Par)
9125 and then Present (Freeze_Node (Par))
9126 and then Present (Enc_I)
9127 then
9128 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I) then
9129 -- The enclosing package may contain several instances. Rather
9130 -- than computing the earliest point at which to insert its freeze
9131 -- node, we place it at the end of the declarative part of the
9132 -- parent of the generic.
9133
9134 Insert_Freeze_Node_For_Instance
9135 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
9136 end if;
9137
9138 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9139
9140 elsif Present (Enc_G)
9141 and then Present (Enc_I)
9142 and then Enc_G /= Enc_I
9143 and then Earlier (Inst_Node, Gen_Body)
9144 then
9145 -- Freeze package that encloses instance, and place node after the
9146 -- package that encloses generic. If enclosing package is already
9147 -- frozen we have to assume it is at the proper place. This may be a
9148 -- potential ABE that requires dynamic checking. Do not add a freeze
9149 -- node if the package that encloses the generic is inside the body
9150 -- that encloses the instance, because the freeze node would be in
9151 -- the wrong scope. Additional contortions needed if the bodies are
9152 -- within a subunit.
9153
9154 declare
9155 Enclosing_Body : Node_Id;
9156
9157 begin
9158 if Nkind (Enc_I) = N_Package_Body_Stub then
9159 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
9160 else
9161 Enclosing_Body := Enc_I;
9162 end if;
9163
9164 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9165 Insert_Freeze_Node_For_Instance
9166 (Enc_G, Package_Freeze_Node (Enc_I));
9167 end if;
9168 end;
9169
9170 -- Freeze enclosing subunit before instance
9171
9172 Enc_G_F := Package_Freeze_Node (Enc_G);
9173
9174 if not Is_List_Member (Enc_G_F) then
9175 Insert_After (Enc_G, Enc_G_F);
9176 end if;
9177
9178 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9179
9180 else
9181 -- If none of the above, insert freeze node at the end of the current
9182 -- declarative part.
9183
9184 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9185 end if;
9186 end Freeze_Subprogram_Body;
9187
9188 ----------------
9189 -- Get_Gen_Id --
9190 ----------------
9191
9192 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9193 begin
9194 return Generic_Renamings.Table (E).Gen_Id;
9195 end Get_Gen_Id;
9196
9197 ---------------------
9198 -- Get_Instance_Of --
9199 ---------------------
9200
9201 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9202 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9203
9204 begin
9205 if Res /= Assoc_Null then
9206 return Generic_Renamings.Table (Res).Act_Id;
9207
9208 else
9209 -- On exit, entity is not instantiated: not a generic parameter, or
9210 -- else parameter of an inner generic unit.
9211
9212 return A;
9213 end if;
9214 end Get_Instance_Of;
9215
9216 ---------------------------------
9217 -- Get_Unit_Instantiation_Node --
9218 ---------------------------------
9219
9220 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9221 Decl : Node_Id := Unit_Declaration_Node (A);
9222 Inst : Node_Id;
9223
9224 begin
9225 -- If the Package_Instantiation attribute has been set on the package
9226 -- entity, then use it directly when it (or its Original_Node) refers
9227 -- to an N_Package_Instantiation node. In principle it should be
9228 -- possible to have this field set in all cases, which should be
9229 -- investigated, and would allow this function to be significantly
9230 -- simplified. ???
9231
9232 Inst := Package_Instantiation (A);
9233
9234 if Present (Inst) then
9235 if Nkind (Inst) = N_Package_Instantiation then
9236 return Inst;
9237
9238 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9239 return Original_Node (Inst);
9240 end if;
9241 end if;
9242
9243 -- If the instantiation is a compilation unit that does not need body
9244 -- then the instantiation node has been rewritten as a package
9245 -- declaration for the instance, and we return the original node.
9246
9247 -- If it is a compilation unit and the instance node has not been
9248 -- rewritten, then it is still the unit of the compilation. Finally, if
9249 -- a body is present, this is a parent of the main unit whose body has
9250 -- been compiled for inlining purposes, and the instantiation node has
9251 -- been rewritten with the instance body.
9252
9253 -- Otherwise the instantiation node appears after the declaration. If
9254 -- the entity is a formal package, the declaration may have been
9255 -- rewritten as a generic declaration (in the case of a formal with box)
9256 -- or left as a formal package declaration if it has actuals, and is
9257 -- found with a forward search.
9258
9259 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9260 if Nkind (Decl) = N_Package_Declaration
9261 and then Present (Corresponding_Body (Decl))
9262 then
9263 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9264 end if;
9265
9266 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9267 return Original_Node (Decl);
9268 else
9269 return Unit (Parent (Decl));
9270 end if;
9271
9272 elsif Nkind (Decl) = N_Package_Declaration
9273 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9274 then
9275 return Original_Node (Decl);
9276
9277 else
9278 Inst := Next (Decl);
9279 while Nkind (Inst) not in N_Formal_Package_Declaration
9280 | N_Function_Instantiation
9281 | N_Package_Instantiation
9282 | N_Procedure_Instantiation
9283 loop
9284 Next (Inst);
9285 end loop;
9286
9287 return Inst;
9288 end if;
9289 end Get_Unit_Instantiation_Node;
9290
9291 ------------------------
9292 -- Has_Been_Exchanged --
9293 ------------------------
9294
9295 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9296 Next : Elmt_Id;
9297
9298 begin
9299 Next := First_Elmt (Exchanged_Views);
9300 while Present (Next) loop
9301 if Full_View (Node (Next)) = E then
9302 return True;
9303 end if;
9304
9305 Next_Elmt (Next);
9306 end loop;
9307
9308 return False;
9309 end Has_Been_Exchanged;
9310
9311 -------------------
9312 -- Has_Contracts --
9313 -------------------
9314
9315 function Has_Contracts (Decl : Node_Id) return Boolean is
9316 A_List : constant List_Id := Aspect_Specifications (Decl);
9317 A_Spec : Node_Id;
9318 A_Id : Aspect_Id;
9319 begin
9320 if No (A_List) then
9321 return False;
9322 else
9323 A_Spec := First (A_List);
9324 while Present (A_Spec) loop
9325 A_Id := Get_Aspect_Id (A_Spec);
9326 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9327 return True;
9328 end if;
9329
9330 Next (A_Spec);
9331 end loop;
9332
9333 return False;
9334 end if;
9335 end Has_Contracts;
9336
9337 ----------
9338 -- Hash --
9339 ----------
9340
9341 function Hash (F : Entity_Id) return HTable_Range is
9342 begin
9343 return HTable_Range (F mod HTable_Size);
9344 end Hash;
9345
9346 ------------------------
9347 -- Hide_Current_Scope --
9348 ------------------------
9349
9350 procedure Hide_Current_Scope is
9351 C : constant Entity_Id := Current_Scope;
9352 E : Entity_Id;
9353
9354 begin
9355 Set_Is_Hidden_Open_Scope (C);
9356
9357 E := First_Entity (C);
9358 while Present (E) loop
9359 if Is_Immediately_Visible (E) then
9360 Set_Is_Immediately_Visible (E, False);
9361 Append_Elmt (E, Hidden_Entities);
9362 end if;
9363
9364 Next_Entity (E);
9365 end loop;
9366
9367 -- Make the scope name invisible as well. This is necessary, but might
9368 -- conflict with calls to Rtsfind later on, in case the scope is a
9369 -- predefined one. There is no clean solution to this problem, so for
9370 -- now we depend on the user not redefining Standard itself in one of
9371 -- the parent units.
9372
9373 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9374 Set_Is_Immediately_Visible (C, False);
9375 Append_Elmt (C, Hidden_Entities);
9376 end if;
9377
9378 end Hide_Current_Scope;
9379
9380 --------------
9381 -- Init_Env --
9382 --------------
9383
9384 procedure Init_Env is
9385 Saved : Instance_Env;
9386
9387 begin
9388 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9389 Saved.Exchanged_Views := Exchanged_Views;
9390 Saved.Hidden_Entities := Hidden_Entities;
9391 Saved.Current_Sem_Unit := Current_Sem_Unit;
9392 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9393 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9394
9395 -- Save configuration switches. These may be reset if the unit is a
9396 -- predefined unit, and the current mode is not Ada 2005.
9397
9398 Saved.Switches := Save_Config_Switches;
9399
9400 Instance_Envs.Append (Saved);
9401
9402 Exchanged_Views := New_Elmt_List;
9403 Hidden_Entities := New_Elmt_List;
9404
9405 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9406 -- this is set properly in Set_Instance_Env.
9407
9408 Current_Instantiated_Parent :=
9409 (Current_Scope, Current_Scope, Assoc_Null);
9410 end Init_Env;
9411
9412 ---------------------
9413 -- In_Main_Context --
9414 ---------------------
9415
9416 function In_Main_Context (E : Entity_Id) return Boolean is
9417 Context : List_Id;
9418 Clause : Node_Id;
9419 Nam : Node_Id;
9420
9421 begin
9422 if not Is_Compilation_Unit (E)
9423 or else Ekind (E) /= E_Package
9424 or else In_Private_Part (E)
9425 then
9426 return False;
9427 end if;
9428
9429 Context := Context_Items (Cunit (Main_Unit));
9430
9431 Clause := First (Context);
9432 while Present (Clause) loop
9433 if Nkind (Clause) = N_With_Clause then
9434 Nam := Name (Clause);
9435
9436 -- If the current scope is part of the context of the main unit,
9437 -- analysis of the corresponding with_clause is not complete, and
9438 -- the entity is not set. We use the Chars field directly, which
9439 -- might produce false positives in rare cases, but guarantees
9440 -- that we produce all the instance bodies we will need.
9441
9442 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9443 or else (Nkind (Nam) = N_Selected_Component
9444 and then Chars (Selector_Name (Nam)) = Chars (E))
9445 then
9446 return True;
9447 end if;
9448 end if;
9449
9450 Next (Clause);
9451 end loop;
9452
9453 return False;
9454 end In_Main_Context;
9455
9456 ---------------------
9457 -- Inherit_Context --
9458 ---------------------
9459
9460 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9461 Current_Context : List_Id;
9462 Current_Unit : Node_Id;
9463 Item : Node_Id;
9464 New_I : Node_Id;
9465
9466 Clause : Node_Id;
9467 OK : Boolean;
9468 Lib_Unit : Node_Id;
9469
9470 begin
9471 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9472
9473 -- The inherited context is attached to the enclosing compilation
9474 -- unit. This is either the main unit, or the declaration for the
9475 -- main unit (in case the instantiation appears within the package
9476 -- declaration and the main unit is its body).
9477
9478 Current_Unit := Parent (Inst);
9479 while Present (Current_Unit)
9480 and then Nkind (Current_Unit) /= N_Compilation_Unit
9481 loop
9482 Current_Unit := Parent (Current_Unit);
9483 end loop;
9484
9485 Current_Context := Context_Items (Current_Unit);
9486
9487 Item := First (Context_Items (Parent (Gen_Decl)));
9488 while Present (Item) loop
9489 if Nkind (Item) = N_With_Clause then
9490 Lib_Unit := Library_Unit (Item);
9491
9492 -- Take care to prevent direct cyclic with's
9493
9494 if Lib_Unit /= Current_Unit then
9495
9496 -- Do not add a unit if it is already in the context
9497
9498 Clause := First (Current_Context);
9499 OK := True;
9500 while Present (Clause) loop
9501 if Nkind (Clause) = N_With_Clause
9502 and then Library_Unit (Clause) = Lib_Unit
9503 then
9504 OK := False;
9505 exit;
9506 end if;
9507
9508 Next (Clause);
9509 end loop;
9510
9511 if OK then
9512 New_I := New_Copy (Item);
9513 Set_Implicit_With (New_I);
9514
9515 Append (New_I, Current_Context);
9516 end if;
9517 end if;
9518 end if;
9519
9520 Next (Item);
9521 end loop;
9522 end if;
9523 end Inherit_Context;
9524
9525 ----------------
9526 -- Initialize --
9527 ----------------
9528
9529 procedure Initialize is
9530 begin
9531 Generic_Renamings.Init;
9532 Instance_Envs.Init;
9533 Generic_Flags.Init;
9534 Generic_Renamings_HTable.Reset;
9535 Circularity_Detected := False;
9536 Exchanged_Views := No_Elist;
9537 Hidden_Entities := No_Elist;
9538 end Initialize;
9539
9540 -------------------------------------
9541 -- Insert_Freeze_Node_For_Instance --
9542 -------------------------------------
9543
9544 procedure Insert_Freeze_Node_For_Instance
9545 (N : Node_Id;
9546 F_Node : Node_Id)
9547 is
9548 Decl : Node_Id;
9549 Decls : List_Id;
9550 Inst : Entity_Id;
9551 Par_N : Node_Id;
9552
9553 function Enclosing_Body (N : Node_Id) return Node_Id;
9554 -- Find enclosing package or subprogram body, if any. Freeze node may
9555 -- be placed at end of current declarative list if previous instance
9556 -- and current one have different enclosing bodies.
9557
9558 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9559 -- Find the local instance, if any, that declares the generic that is
9560 -- being instantiated. If present, the freeze node for this instance
9561 -- must follow the freeze node for the previous instance.
9562
9563 --------------------
9564 -- Enclosing_Body --
9565 --------------------
9566
9567 function Enclosing_Body (N : Node_Id) return Node_Id is
9568 P : Node_Id;
9569
9570 begin
9571 P := Parent (N);
9572 while Present (P)
9573 and then Nkind (Parent (P)) /= N_Compilation_Unit
9574 loop
9575 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9576 if Nkind (Parent (P)) = N_Subunit then
9577 return Corresponding_Stub (Parent (P));
9578 else
9579 return P;
9580 end if;
9581 end if;
9582
9583 P := True_Parent (P);
9584 end loop;
9585
9586 return Empty;
9587 end Enclosing_Body;
9588
9589 -----------------------
9590 -- Previous_Instance --
9591 -----------------------
9592
9593 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9594 S : Entity_Id;
9595
9596 begin
9597 S := Scope (Gen);
9598 while Present (S) and then S /= Standard_Standard loop
9599 if Is_Generic_Instance (S)
9600 and then In_Same_Source_Unit (S, N)
9601 then
9602 return S;
9603 end if;
9604
9605 S := Scope (S);
9606 end loop;
9607
9608 return Empty;
9609 end Previous_Instance;
9610
9611 -- Start of processing for Insert_Freeze_Node_For_Instance
9612
9613 begin
9614 if not Is_List_Member (F_Node) then
9615 Decl := N;
9616 Decls := List_Containing (N);
9617 Inst := Entity (F_Node);
9618 Par_N := Parent (Decls);
9619
9620 -- When processing a subprogram instantiation, utilize the actual
9621 -- subprogram instantiation rather than its package wrapper as it
9622 -- carries all the context information.
9623
9624 if Is_Wrapper_Package (Inst) then
9625 Inst := Related_Instance (Inst);
9626 end if;
9627
9628 -- If this is a package instance, check whether the generic is
9629 -- declared in a previous instance and the current instance is
9630 -- not within the previous one.
9631
9632 if Present (Generic_Parent (Parent (Inst)))
9633 and then Is_In_Main_Unit (N)
9634 then
9635 declare
9636 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9637 Par_I : constant Entity_Id :=
9638 Previous_Instance
9639 (Generic_Parent (Parent (Inst)));
9640 Scop : Entity_Id;
9641
9642 begin
9643 if Present (Par_I)
9644 and then Earlier (N, Freeze_Node (Par_I))
9645 then
9646 Scop := Scope (Inst);
9647
9648 -- If the current instance is within the one that contains
9649 -- the generic, the freeze node for the current one must
9650 -- appear in the current declarative part. Ditto, if the
9651 -- current instance is within another package instance or
9652 -- within a body that does not enclose the current instance.
9653 -- In these three cases the freeze node of the previous
9654 -- instance is not relevant.
9655
9656 while Present (Scop) and then Scop /= Standard_Standard loop
9657 exit when Scop = Par_I
9658 or else
9659 (Is_Generic_Instance (Scop)
9660 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9661 Scop := Scope (Scop);
9662 end loop;
9663
9664 -- Previous instance encloses current instance
9665
9666 if Scop = Par_I then
9667 null;
9668
9669 -- If the next node is a source body we must freeze in
9670 -- the current scope as well.
9671
9672 elsif Present (Next (N))
9673 and then Nkind (Next (N)) in N_Subprogram_Body
9674 | N_Package_Body
9675 and then Comes_From_Source (Next (N))
9676 then
9677 null;
9678
9679 -- Current instance is within an unrelated instance
9680
9681 elsif Is_Generic_Instance (Scop) then
9682 null;
9683
9684 -- Current instance is within an unrelated body
9685
9686 elsif Present (Enclosing_N)
9687 and then Enclosing_N /= Enclosing_Body (Par_I)
9688 then
9689 null;
9690
9691 else
9692 Insert_After (Freeze_Node (Par_I), F_Node);
9693 return;
9694 end if;
9695 end if;
9696 end;
9697 end if;
9698
9699 -- When the instantiation occurs in a package declaration, append the
9700 -- freeze node to the private declarations (if any).
9701
9702 if Nkind (Par_N) = N_Package_Specification
9703 and then Decls = Visible_Declarations (Par_N)
9704 and then Present (Private_Declarations (Par_N))
9705 and then not Is_Empty_List (Private_Declarations (Par_N))
9706 then
9707 Decls := Private_Declarations (Par_N);
9708 Decl := First (Decls);
9709 end if;
9710
9711 -- Determine the proper freeze point of a package instantiation. We
9712 -- adhere to the general rule of a package or subprogram body causing
9713 -- freezing of anything before it in the same declarative region. In
9714 -- this case, the proper freeze point of a package instantiation is
9715 -- before the first source body which follows, or before a stub. This
9716 -- ensures that entities coming from the instance are already frozen
9717 -- and usable in source bodies.
9718
9719 if Nkind (Par_N) /= N_Package_Declaration
9720 and then Ekind (Inst) = E_Package
9721 and then Is_Generic_Instance (Inst)
9722 and then
9723 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9724 then
9725 while Present (Decl) loop
9726 if (Nkind (Decl) in N_Unit_Body
9727 or else
9728 Nkind (Decl) in N_Body_Stub)
9729 and then Comes_From_Source (Decl)
9730 then
9731 Insert_Before (Decl, F_Node);
9732 return;
9733 end if;
9734
9735 Next (Decl);
9736 end loop;
9737 end if;
9738
9739 -- In a package declaration, or if no previous body, insert at end
9740 -- of list.
9741
9742 Set_Sloc (F_Node, Sloc (Last (Decls)));
9743 Insert_After (Last (Decls), F_Node);
9744 end if;
9745 end Insert_Freeze_Node_For_Instance;
9746
9747 ------------------
9748 -- Install_Body --
9749 ------------------
9750
9751 procedure Install_Body
9752 (Act_Body : Node_Id;
9753 N : Node_Id;
9754 Gen_Body : Node_Id;
9755 Gen_Decl : Node_Id)
9756 is
9757 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9758 -- Check if the generic definition and the instantiation come from
9759 -- a common scope, in which case the instance must be frozen after
9760 -- the generic body.
9761
9762 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9763 -- If the instance is nested inside a generic unit, the Sloc of the
9764 -- instance indicates the place of the original definition, not the
9765 -- point of the current enclosing instance. Pending a better usage of
9766 -- Slocs to indicate instantiation places, we determine the place of
9767 -- origin of a node by finding the maximum sloc of any ancestor node.
9768 -- Why is this not equivalent to Top_Level_Location ???
9769
9770 -------------------
9771 -- In_Same_Scope --
9772 -------------------
9773
9774 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9775 Act_Scop : Entity_Id := Scope (Act_Id);
9776 Gen_Scop : Entity_Id := Scope (Gen_Id);
9777
9778 begin
9779 while Act_Scop /= Standard_Standard
9780 and then Gen_Scop /= Standard_Standard
9781 loop
9782 if Act_Scop = Gen_Scop then
9783 return True;
9784 end if;
9785
9786 Act_Scop := Scope (Act_Scop);
9787 Gen_Scop := Scope (Gen_Scop);
9788 end loop;
9789
9790 return False;
9791 end In_Same_Scope;
9792
9793 ---------------
9794 -- True_Sloc --
9795 ---------------
9796
9797 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9798 N1 : Node_Id;
9799 Res : Source_Ptr;
9800
9801 begin
9802 Res := Sloc (N);
9803 N1 := N;
9804 while Present (N1) and then N1 /= Act_Unit loop
9805 if Sloc (N1) > Res then
9806 Res := Sloc (N1);
9807 end if;
9808
9809 N1 := Parent (N1);
9810 end loop;
9811
9812 return Res;
9813 end True_Sloc;
9814
9815 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9816 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9817 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9818 Par : constant Entity_Id := Scope (Gen_Id);
9819 Gen_Unit : constant Node_Id :=
9820 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9821
9822 Body_Unit : Node_Id;
9823 F_Node : Node_Id;
9824 Must_Delay : Boolean;
9825 Orig_Body : Node_Id := Gen_Body;
9826
9827 -- Start of processing for Install_Body
9828
9829 begin
9830 -- Handle first the case of an instance with incomplete actual types.
9831 -- The instance body cannot be placed after the declaration because
9832 -- full views have not been seen yet. Any use of the non-limited views
9833 -- in the instance body requires the presence of a regular with_clause
9834 -- in the enclosing unit, and will fail if this with_clause is missing.
9835 -- We place the instance body at the beginning of the enclosing body,
9836 -- which is the unit being compiled. The freeze node for the instance
9837 -- is then placed after the instance body.
9838
9839 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9840 and then Expander_Active
9841 and then Ekind (Scope (Act_Id)) = E_Package
9842 then
9843 declare
9844 Scop : constant Entity_Id := Scope (Act_Id);
9845 Body_Id : constant Node_Id :=
9846 Corresponding_Body (Unit_Declaration_Node (Scop));
9847
9848 begin
9849 Ensure_Freeze_Node (Act_Id);
9850 F_Node := Freeze_Node (Act_Id);
9851 if Present (Body_Id) then
9852 Set_Is_Frozen (Act_Id, False);
9853 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9854 if Is_List_Member (F_Node) then
9855 Remove (F_Node);
9856 end if;
9857
9858 Insert_After (Act_Body, F_Node);
9859 end if;
9860 end;
9861 return;
9862 end if;
9863
9864 -- If the body is a subunit, the freeze point is the corresponding stub
9865 -- in the current compilation, not the subunit itself.
9866
9867 if Nkind (Parent (Gen_Body)) = N_Subunit then
9868 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9869 else
9870 Orig_Body := Gen_Body;
9871 end if;
9872
9873 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9874
9875 -- If the instantiation and the generic definition appear in the same
9876 -- package declaration, this is an early instantiation. If they appear
9877 -- in the same declarative part, it is an early instantiation only if
9878 -- the generic body appears textually later, and the generic body is
9879 -- also in the main unit.
9880
9881 -- If instance is nested within a subprogram, and the generic body
9882 -- is not, the instance is delayed because the enclosing body is. If
9883 -- instance and body are within the same scope, or the same subprogram
9884 -- body, indicate explicitly that the instance is delayed.
9885
9886 Must_Delay :=
9887 (Gen_Unit = Act_Unit
9888 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9889 | N_Package_Declaration
9890 or else (Gen_Unit = Body_Unit
9891 and then True_Sloc (N, Act_Unit) <
9892 Sloc (Orig_Body)))
9893 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9894 and then In_Same_Scope (Gen_Id, Act_Id));
9895
9896 -- If this is an early instantiation, the freeze node is placed after
9897 -- the generic body. Otherwise, if the generic appears in an instance,
9898 -- we cannot freeze the current instance until the outer one is frozen.
9899 -- This is only relevant if the current instance is nested within some
9900 -- inner scope not itself within the outer instance. If this scope is
9901 -- a package body in the same declarative part as the outer instance,
9902 -- then that body needs to be frozen after the outer instance. Finally,
9903 -- if no delay is needed, we place the freeze node at the end of the
9904 -- current declarative part.
9905
9906 if Expander_Active
9907 and then (No (Freeze_Node (Act_Id))
9908 or else not Is_List_Member (Freeze_Node (Act_Id)))
9909 then
9910 Ensure_Freeze_Node (Act_Id);
9911 F_Node := Freeze_Node (Act_Id);
9912
9913 if Must_Delay then
9914 Insert_After (Orig_Body, F_Node);
9915
9916 elsif Is_Generic_Instance (Par)
9917 and then Present (Freeze_Node (Par))
9918 and then Scope (Act_Id) /= Par
9919 then
9920 -- Freeze instance of inner generic after instance of enclosing
9921 -- generic.
9922
9923 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9924
9925 -- Handle the following case:
9926
9927 -- package Parent_Inst is new ...
9928 -- Parent_Inst []
9929
9930 -- procedure P ... -- this body freezes Parent_Inst
9931
9932 -- package Inst is new ...
9933
9934 -- In this particular scenario, the freeze node for Inst must
9935 -- be inserted in the same manner as that of Parent_Inst,
9936 -- before the next source body or at the end of the declarative
9937 -- list (body not available). If body P did not exist and
9938 -- Parent_Inst was frozen after Inst, either by a body
9939 -- following Inst or at the end of the declarative region,
9940 -- the freeze node for Inst must be inserted after that of
9941 -- Parent_Inst. This relation is established by comparing
9942 -- the Slocs of Parent_Inst freeze node and Inst.
9943 -- We examine the parents of the enclosing lists to handle
9944 -- the case where the parent instance is in the visible part
9945 -- of a package declaration, and the inner instance is in
9946 -- the corresponding private part.
9947
9948 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9949 = Parent (List_Containing (N))
9950 and then Sloc (Freeze_Node (Par)) <= Sloc (N)
9951 then
9952 Insert_Freeze_Node_For_Instance (N, F_Node);
9953 else
9954 Insert_After (Freeze_Node (Par), F_Node);
9955 end if;
9956
9957 -- Freeze package enclosing instance of inner generic after
9958 -- instance of enclosing generic.
9959
9960 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9961 and then In_Same_Declarative_Part
9962 (Parent (Freeze_Node (Par)), Parent (N))
9963 then
9964 declare
9965 Enclosing : Entity_Id;
9966
9967 begin
9968 Enclosing := Corresponding_Spec (Parent (N));
9969
9970 if No (Enclosing) then
9971 Enclosing := Defining_Entity (Parent (N));
9972 end if;
9973
9974 Insert_Freeze_Node_For_Instance (N, F_Node);
9975 Ensure_Freeze_Node (Enclosing);
9976
9977 if not Is_List_Member (Freeze_Node (Enclosing)) then
9978
9979 -- The enclosing context is a subunit, insert the freeze
9980 -- node after the stub.
9981
9982 if Nkind (Parent (Parent (N))) = N_Subunit then
9983 Insert_Freeze_Node_For_Instance
9984 (Corresponding_Stub (Parent (Parent (N))),
9985 Freeze_Node (Enclosing));
9986
9987 -- The enclosing context is a package with a stub body
9988 -- which has already been replaced by the real body.
9989 -- Insert the freeze node after the actual body.
9990
9991 elsif Ekind (Enclosing) = E_Package
9992 and then Present (Body_Entity (Enclosing))
9993 and then Was_Originally_Stub
9994 (Parent (Body_Entity (Enclosing)))
9995 then
9996 Insert_Freeze_Node_For_Instance
9997 (Parent (Body_Entity (Enclosing)),
9998 Freeze_Node (Enclosing));
9999
10000 -- The parent instance has been frozen before the body of
10001 -- the enclosing package, insert the freeze node after
10002 -- the body.
10003
10004 elsif In_Same_List (Freeze_Node (Par), Parent (N))
10005 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
10006 then
10007 Insert_Freeze_Node_For_Instance
10008 (Parent (N), Freeze_Node (Enclosing));
10009
10010 else
10011 Insert_After
10012 (Freeze_Node (Par), Freeze_Node (Enclosing));
10013 end if;
10014 end if;
10015 end;
10016
10017 else
10018 Insert_Freeze_Node_For_Instance (N, F_Node);
10019 end if;
10020
10021 else
10022 Insert_Freeze_Node_For_Instance (N, F_Node);
10023 end if;
10024 end if;
10025
10026 Set_Is_Frozen (Act_Id);
10027 Insert_Before (N, Act_Body);
10028 Mark_Rewrite_Insertion (Act_Body);
10029 end Install_Body;
10030
10031 -----------------------------
10032 -- Install_Formal_Packages --
10033 -----------------------------
10034
10035 procedure Install_Formal_Packages (Par : Entity_Id) is
10036 E : Entity_Id;
10037 Gen : Entity_Id;
10038 Gen_E : Entity_Id := Empty;
10039
10040 begin
10041 E := First_Entity (Par);
10042
10043 -- If we are installing an instance parent, locate the formal packages
10044 -- of its generic parent.
10045
10046 if Is_Generic_Instance (Par) then
10047 Gen := Generic_Parent (Package_Specification (Par));
10048 Gen_E := First_Entity (Gen);
10049 end if;
10050
10051 while Present (E) loop
10052 if Ekind (E) = E_Package
10053 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10054 then
10055 -- If this is the renaming for the parent instance, done
10056
10057 if Renamed_Object (E) = Par then
10058 exit;
10059
10060 -- The visibility of a formal of an enclosing generic is already
10061 -- correct.
10062
10063 elsif Denotes_Formal_Package (E) then
10064 null;
10065
10066 elsif Present (Associated_Formal_Package (E)) then
10067 Check_Generic_Actuals (Renamed_Object (E), True);
10068 Set_Is_Hidden (E, False);
10069
10070 -- Find formal package in generic unit that corresponds to
10071 -- (instance of) formal package in instance.
10072
10073 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10074 Next_Entity (Gen_E);
10075 end loop;
10076
10077 if Present (Gen_E) then
10078 Map_Formal_Package_Entities (Gen_E, E);
10079 end if;
10080 end if;
10081 end if;
10082
10083 Next_Entity (E);
10084
10085 if Present (Gen_E) then
10086 Next_Entity (Gen_E);
10087 end if;
10088 end loop;
10089 end Install_Formal_Packages;
10090
10091 --------------------
10092 -- Install_Parent --
10093 --------------------
10094
10095 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10096 Ancestors : constant Elist_Id := New_Elmt_List;
10097 S : constant Entity_Id := Current_Scope;
10098 Inst_Par : Entity_Id;
10099 First_Par : Entity_Id;
10100 Inst_Node : Node_Id;
10101 Gen_Par : Entity_Id;
10102 First_Gen : Entity_Id;
10103 Elmt : Elmt_Id;
10104
10105 procedure Install_Noninstance_Specs (Par : Entity_Id);
10106 -- Install the scopes of noninstance parent units ending with Par
10107
10108 procedure Install_Spec (Par : Entity_Id);
10109 -- The child unit is within the declarative part of the parent, so the
10110 -- declarations within the parent are immediately visible.
10111
10112 -------------------------------
10113 -- Install_Noninstance_Specs --
10114 -------------------------------
10115
10116 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10117 begin
10118 if Present (Par)
10119 and then Par /= Standard_Standard
10120 and then not In_Open_Scopes (Par)
10121 then
10122 Install_Noninstance_Specs (Scope (Par));
10123 Install_Spec (Par);
10124 end if;
10125 end Install_Noninstance_Specs;
10126
10127 ------------------
10128 -- Install_Spec --
10129 ------------------
10130
10131 procedure Install_Spec (Par : Entity_Id) is
10132 Spec : constant Node_Id := Package_Specification (Par);
10133
10134 begin
10135 -- If this parent of the child instance is a top-level unit,
10136 -- then record the unit and its visibility for later resetting in
10137 -- Remove_Parent. We exclude units that are generic instances, as we
10138 -- only want to record this information for the ultimate top-level
10139 -- noninstance parent (is that always correct???).
10140
10141 if Scope (Par) = Standard_Standard
10142 and then not Is_Generic_Instance (Par)
10143 then
10144 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10145 Instance_Parent_Unit := Par;
10146 end if;
10147
10148 -- Open the parent scope and make it and its declarations visible.
10149 -- If this point is not within a body, then only the visible
10150 -- declarations should be made visible, and installation of the
10151 -- private declarations is deferred until the appropriate point
10152 -- within analysis of the spec being instantiated (see the handling
10153 -- of parent visibility in Analyze_Package_Specification). This is
10154 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10155 -- private view problems that occur when compiling instantiations of
10156 -- a generic child of that package (Generic_Dispatching_Constructor).
10157 -- If the instance freezes a tagged type, inlinings of operations
10158 -- from Ada.Tags may need the full view of type Tag. If inlining took
10159 -- proper account of establishing visibility of inlined subprograms'
10160 -- parents then it should be possible to remove this
10161 -- special check. ???
10162
10163 Push_Scope (Par);
10164 Set_Is_Immediately_Visible (Par);
10165 Install_Visible_Declarations (Par);
10166 Set_Use (Visible_Declarations (Spec));
10167
10168 if In_Body or else Is_RTU (Par, Ada_Tags) then
10169 Install_Private_Declarations (Par);
10170 Set_Use (Private_Declarations (Spec));
10171 end if;
10172 end Install_Spec;
10173
10174 -- Start of processing for Install_Parent
10175
10176 begin
10177 -- We need to install the parent instance to compile the instantiation
10178 -- of the child, but the child instance must appear in the current
10179 -- scope. Given that we cannot place the parent above the current scope
10180 -- in the scope stack, we duplicate the current scope and unstack both
10181 -- after the instantiation is complete.
10182
10183 -- If the parent is itself the instantiation of a child unit, we must
10184 -- also stack the instantiation of its parent, and so on. Each such
10185 -- ancestor is the prefix of the name in a prior instantiation.
10186
10187 -- If this is a nested instance, the parent unit itself resolves to
10188 -- a renaming of the parent instance, whose declaration we need.
10189
10190 -- Finally, the parent may be a generic (not an instance) when the
10191 -- child unit appears as a formal package.
10192
10193 Inst_Par := P;
10194
10195 if Present (Renamed_Entity (Inst_Par)) then
10196 Inst_Par := Renamed_Entity (Inst_Par);
10197 end if;
10198
10199 First_Par := Inst_Par;
10200
10201 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10202
10203 First_Gen := Gen_Par;
10204
10205 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10206
10207 -- Load grandparent instance as well
10208
10209 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10210
10211 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10212 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10213
10214 if Present (Renamed_Entity (Inst_Par)) then
10215 Inst_Par := Renamed_Entity (Inst_Par);
10216 end if;
10217
10218 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10219
10220 if Present (Gen_Par) then
10221 Prepend_Elmt (Inst_Par, Ancestors);
10222
10223 else
10224 -- Parent is not the name of an instantiation
10225
10226 Install_Noninstance_Specs (Inst_Par);
10227 exit;
10228 end if;
10229
10230 else
10231 -- Previous error
10232
10233 exit;
10234 end if;
10235 end loop;
10236
10237 if Present (First_Gen) then
10238 Append_Elmt (First_Par, Ancestors);
10239 else
10240 Install_Noninstance_Specs (First_Par);
10241 end if;
10242
10243 if not Is_Empty_Elmt_List (Ancestors) then
10244 Elmt := First_Elmt (Ancestors);
10245 while Present (Elmt) loop
10246 Install_Spec (Node (Elmt));
10247 Install_Formal_Packages (Node (Elmt));
10248 Next_Elmt (Elmt);
10249 end loop;
10250 end if;
10251
10252 if not In_Body then
10253 Push_Scope (S);
10254 end if;
10255 end Install_Parent;
10256
10257 -------------------------------
10258 -- Install_Hidden_Primitives --
10259 -------------------------------
10260
10261 procedure Install_Hidden_Primitives
10262 (Prims_List : in out Elist_Id;
10263 Gen_T : Entity_Id;
10264 Act_T : Entity_Id)
10265 is
10266 Elmt : Elmt_Id;
10267 List : Elist_Id := No_Elist;
10268 Prim_G_Elmt : Elmt_Id;
10269 Prim_A_Elmt : Elmt_Id;
10270 Prim_G : Node_Id;
10271 Prim_A : Node_Id;
10272
10273 begin
10274 -- No action needed in case of serious errors because we cannot trust
10275 -- in the order of primitives
10276
10277 if Serious_Errors_Detected > 0 then
10278 return;
10279
10280 -- No action possible if we don't have available the list of primitive
10281 -- operations
10282
10283 elsif No (Gen_T)
10284 or else not Is_Record_Type (Gen_T)
10285 or else not Is_Tagged_Type (Gen_T)
10286 or else not Is_Record_Type (Act_T)
10287 or else not Is_Tagged_Type (Act_T)
10288 then
10289 return;
10290
10291 -- There is no need to handle interface types since their primitives
10292 -- cannot be hidden
10293
10294 elsif Is_Interface (Gen_T) then
10295 return;
10296 end if;
10297
10298 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10299
10300 if not Is_Class_Wide_Type (Act_T) then
10301 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10302 else
10303 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10304 end if;
10305
10306 loop
10307 -- Skip predefined primitives in the generic formal
10308
10309 while Present (Prim_G_Elmt)
10310 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10311 loop
10312 Next_Elmt (Prim_G_Elmt);
10313 end loop;
10314
10315 -- Skip predefined primitives in the generic actual
10316
10317 while Present (Prim_A_Elmt)
10318 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10319 loop
10320 Next_Elmt (Prim_A_Elmt);
10321 end loop;
10322
10323 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10324
10325 Prim_G := Node (Prim_G_Elmt);
10326 Prim_A := Node (Prim_A_Elmt);
10327
10328 -- There is no need to handle interface primitives because their
10329 -- primitives are not hidden
10330
10331 exit when Present (Interface_Alias (Prim_G));
10332
10333 -- Here we install one hidden primitive
10334
10335 if Chars (Prim_G) /= Chars (Prim_A)
10336 and then Has_Suffix (Prim_A, 'P')
10337 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10338 then
10339 Set_Chars (Prim_A, Chars (Prim_G));
10340 Append_New_Elmt (Prim_A, To => List);
10341 end if;
10342
10343 Next_Elmt (Prim_A_Elmt);
10344 Next_Elmt (Prim_G_Elmt);
10345 end loop;
10346
10347 -- Append the elements to the list of temporarily visible primitives
10348 -- avoiding duplicates.
10349
10350 if Present (List) then
10351 if No (Prims_List) then
10352 Prims_List := New_Elmt_List;
10353 end if;
10354
10355 Elmt := First_Elmt (List);
10356 while Present (Elmt) loop
10357 Append_Unique_Elmt (Node (Elmt), Prims_List);
10358 Next_Elmt (Elmt);
10359 end loop;
10360 end if;
10361 end Install_Hidden_Primitives;
10362
10363 -------------------------------
10364 -- Restore_Hidden_Primitives --
10365 -------------------------------
10366
10367 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10368 Prim_Elmt : Elmt_Id;
10369 Prim : Node_Id;
10370
10371 begin
10372 if Prims_List /= No_Elist then
10373 Prim_Elmt := First_Elmt (Prims_List);
10374 while Present (Prim_Elmt) loop
10375 Prim := Node (Prim_Elmt);
10376 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10377 Next_Elmt (Prim_Elmt);
10378 end loop;
10379
10380 Prims_List := No_Elist;
10381 end if;
10382 end Restore_Hidden_Primitives;
10383
10384 --------------------------------
10385 -- Instantiate_Formal_Package --
10386 --------------------------------
10387
10388 function Instantiate_Formal_Package
10389 (Formal : Node_Id;
10390 Actual : Node_Id;
10391 Analyzed_Formal : Node_Id) return List_Id
10392 is
10393 Loc : constant Source_Ptr := Sloc (Actual);
10394 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10395 Actual_Pack : Entity_Id;
10396 Formal_Pack : Entity_Id;
10397 Gen_Parent : Entity_Id;
10398 Decls : List_Id;
10399 Nod : Node_Id;
10400 Parent_Spec : Node_Id;
10401
10402 procedure Find_Matching_Actual
10403 (F : Node_Id;
10404 Act : in out Entity_Id);
10405 -- We need to associate each formal entity in the formal package with
10406 -- the corresponding entity in the actual package. The actual package
10407 -- has been analyzed and possibly expanded, and as a result there is
10408 -- no one-to-one correspondence between the two lists (for example,
10409 -- the actual may include subtypes, itypes, and inherited primitive
10410 -- operations, interspersed among the renaming declarations for the
10411 -- actuals). We retrieve the corresponding actual by name because each
10412 -- actual has the same name as the formal, and they do appear in the
10413 -- same order.
10414
10415 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10416 -- Retrieve entity of defining entity of generic formal parameter.
10417 -- Only the declarations of formals need to be considered when
10418 -- linking them to actuals, but the declarative list may include
10419 -- internal entities generated during analysis, and those are ignored.
10420
10421 procedure Match_Formal_Entity
10422 (Formal_Node : Node_Id;
10423 Formal_Ent : Entity_Id;
10424 Actual_Ent : Entity_Id);
10425 -- Associates the formal entity with the actual. In the case where
10426 -- Formal_Ent is a formal package, this procedure iterates through all
10427 -- of its formals and enters associations between the actuals occurring
10428 -- in the formal package's corresponding actual package (given by
10429 -- Actual_Ent) and the formal package's formal parameters. This
10430 -- procedure recurses if any of the parameters is itself a package.
10431
10432 function Is_Instance_Of
10433 (Act_Spec : Entity_Id;
10434 Gen_Anc : Entity_Id) return Boolean;
10435 -- The actual can be an instantiation of a generic within another
10436 -- instance, in which case there is no direct link from it to the
10437 -- original generic ancestor. In that case, we recognize that the
10438 -- ultimate ancestor is the same by examining names and scopes.
10439
10440 procedure Process_Nested_Formal (Formal : Entity_Id);
10441 -- If the current formal is declared with a box, its own formals are
10442 -- visible in the instance, as they were in the generic, and their
10443 -- Hidden flag must be reset. If some of these formals are themselves
10444 -- packages declared with a box, the processing must be recursive.
10445
10446 --------------------------
10447 -- Find_Matching_Actual --
10448 --------------------------
10449
10450 procedure Find_Matching_Actual
10451 (F : Node_Id;
10452 Act : in out Entity_Id)
10453 is
10454 Formal_Ent : Entity_Id;
10455
10456 begin
10457 case Nkind (Original_Node (F)) is
10458 when N_Formal_Object_Declaration
10459 | N_Formal_Type_Declaration
10460 =>
10461 Formal_Ent := Defining_Identifier (F);
10462
10463 while Present (Act)
10464 and then Chars (Act) /= Chars (Formal_Ent)
10465 loop
10466 Next_Entity (Act);
10467 end loop;
10468
10469 when N_Formal_Package_Declaration
10470 | N_Formal_Subprogram_Declaration
10471 | N_Generic_Package_Declaration
10472 | N_Package_Declaration
10473 =>
10474 Formal_Ent := Defining_Entity (F);
10475
10476 while Present (Act)
10477 and then Chars (Act) /= Chars (Formal_Ent)
10478 loop
10479 Next_Entity (Act);
10480 end loop;
10481
10482 when others =>
10483 raise Program_Error;
10484 end case;
10485 end Find_Matching_Actual;
10486
10487 -------------------------
10488 -- Match_Formal_Entity --
10489 -------------------------
10490
10491 procedure Match_Formal_Entity
10492 (Formal_Node : Node_Id;
10493 Formal_Ent : Entity_Id;
10494 Actual_Ent : Entity_Id)
10495 is
10496 Act_Pkg : Entity_Id;
10497
10498 begin
10499 Set_Instance_Of (Formal_Ent, Actual_Ent);
10500
10501 if Ekind (Actual_Ent) = E_Package then
10502
10503 -- Record associations for each parameter
10504
10505 Act_Pkg := Actual_Ent;
10506
10507 declare
10508 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10509 F_Ent : Entity_Id;
10510 F_Node : Node_Id;
10511
10512 Gen_Decl : Node_Id;
10513 Formals : List_Id;
10514 Actual : Entity_Id;
10515
10516 begin
10517 -- Retrieve the actual given in the formal package declaration
10518
10519 Actual := Entity (Name (Original_Node (Formal_Node)));
10520
10521 -- The actual in the formal package declaration may be a
10522 -- renamed generic package, in which case we want to retrieve
10523 -- the original generic in order to traverse its formal part.
10524
10525 if Present (Renamed_Entity (Actual)) then
10526 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10527 else
10528 Gen_Decl := Unit_Declaration_Node (Actual);
10529 end if;
10530
10531 Formals := Generic_Formal_Declarations (Gen_Decl);
10532
10533 if Present (Formals) then
10534 F_Node := First_Non_Pragma (Formals);
10535 else
10536 F_Node := Empty;
10537 end if;
10538
10539 while Present (A_Ent)
10540 and then Present (F_Node)
10541 and then A_Ent /= First_Private_Entity (Act_Pkg)
10542 loop
10543 F_Ent := Get_Formal_Entity (F_Node);
10544
10545 if Present (F_Ent) then
10546
10547 -- This is a formal of the original package. Record
10548 -- association and recurse.
10549
10550 Find_Matching_Actual (F_Node, A_Ent);
10551 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10552 Next_Entity (A_Ent);
10553 end if;
10554
10555 Next_Non_Pragma (F_Node);
10556 end loop;
10557 end;
10558 end if;
10559 end Match_Formal_Entity;
10560
10561 -----------------------
10562 -- Get_Formal_Entity --
10563 -----------------------
10564
10565 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10566 Kind : constant Node_Kind := Nkind (Original_Node (N));
10567 begin
10568 case Kind is
10569 when N_Formal_Object_Declaration =>
10570 return Defining_Identifier (N);
10571
10572 when N_Formal_Type_Declaration =>
10573 return Defining_Identifier (N);
10574
10575 when N_Formal_Subprogram_Declaration =>
10576 return Defining_Unit_Name (Specification (N));
10577
10578 when N_Formal_Package_Declaration =>
10579 return Defining_Identifier (Original_Node (N));
10580
10581 when N_Generic_Package_Declaration =>
10582 return Defining_Identifier (Original_Node (N));
10583
10584 -- All other declarations are introduced by semantic analysis and
10585 -- have no match in the actual.
10586
10587 when others =>
10588 return Empty;
10589 end case;
10590 end Get_Formal_Entity;
10591
10592 --------------------
10593 -- Is_Instance_Of --
10594 --------------------
10595
10596 function Is_Instance_Of
10597 (Act_Spec : Entity_Id;
10598 Gen_Anc : Entity_Id) return Boolean
10599 is
10600 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10601
10602 begin
10603 if No (Gen_Par) then
10604 return False;
10605
10606 -- Simplest case: the generic parent of the actual is the formal
10607
10608 elsif Gen_Par = Gen_Anc then
10609 return True;
10610
10611 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10612 return False;
10613
10614 -- The actual may be obtained through several instantiations. Its
10615 -- scope must itself be an instance of a generic declared in the
10616 -- same scope as the formal. Any other case is detected above.
10617
10618 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10619 return False;
10620
10621 else
10622 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10623 end if;
10624 end Is_Instance_Of;
10625
10626 ---------------------------
10627 -- Process_Nested_Formal --
10628 ---------------------------
10629
10630 procedure Process_Nested_Formal (Formal : Entity_Id) is
10631 Ent : Entity_Id;
10632
10633 begin
10634 if Present (Associated_Formal_Package (Formal))
10635 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10636 then
10637 Ent := First_Entity (Formal);
10638 while Present (Ent) loop
10639 Set_Is_Hidden (Ent, False);
10640 Set_Is_Visible_Formal (Ent);
10641 Set_Is_Potentially_Use_Visible
10642 (Ent, Is_Potentially_Use_Visible (Formal));
10643
10644 if Ekind (Ent) = E_Package then
10645 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10646 Process_Nested_Formal (Ent);
10647 end if;
10648
10649 Next_Entity (Ent);
10650 end loop;
10651 end if;
10652 end Process_Nested_Formal;
10653
10654 -- Start of processing for Instantiate_Formal_Package
10655
10656 begin
10657 Analyze (Actual);
10658
10659 -- The actual must be a package instance, or else a current instance
10660 -- such as a parent generic within the body of a generic child.
10661
10662 if not Is_Entity_Name (Actual)
10663 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10664 then
10665 Error_Msg_N
10666 ("expect package instance to instantiate formal", Actual);
10667 Abandon_Instantiation (Actual);
10668 raise Program_Error;
10669
10670 else
10671 Actual_Pack := Entity (Actual);
10672 Set_Is_Instantiated (Actual_Pack);
10673
10674 -- The actual may be a renamed package, or an outer generic formal
10675 -- package whose instantiation is converted into a renaming.
10676
10677 if Present (Renamed_Object (Actual_Pack)) then
10678 Actual_Pack := Renamed_Object (Actual_Pack);
10679 end if;
10680
10681 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10682 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10683 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10684 else
10685 Gen_Parent :=
10686 Generic_Parent (Specification (Analyzed_Formal));
10687 Formal_Pack :=
10688 Defining_Unit_Name (Specification (Analyzed_Formal));
10689 end if;
10690
10691 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10692 Parent_Spec := Package_Specification (Actual_Pack);
10693 else
10694 Parent_Spec := Parent (Actual_Pack);
10695 end if;
10696
10697 if Gen_Parent = Any_Id then
10698 Error_Msg_N
10699 ("previous error in declaration of formal package", Actual);
10700 Abandon_Instantiation (Actual);
10701
10702 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10703 null;
10704
10705 -- If this is the current instance of an enclosing generic, that unit
10706 -- is the generic package we need.
10707
10708 elsif In_Open_Scopes (Actual_Pack)
10709 and then Ekind (Actual_Pack) = E_Generic_Package
10710 then
10711 null;
10712
10713 else
10714 Error_Msg_NE
10715 ("actual parameter must be instance of&", Actual, Gen_Parent);
10716 Abandon_Instantiation (Actual);
10717 end if;
10718
10719 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10720 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10721
10722 Nod :=
10723 Make_Package_Renaming_Declaration (Loc,
10724 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10725 Name => New_Occurrence_Of (Actual_Pack, Loc));
10726
10727 Set_Associated_Formal_Package
10728 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10729 Decls := New_List (Nod);
10730
10731 -- If the formal F has a box, then the generic declarations are
10732 -- visible in the generic G. In an instance of G, the corresponding
10733 -- entities in the actual for F (which are the actuals for the
10734 -- instantiation of the generic that F denotes) must also be made
10735 -- visible for analysis of the current instance. On exit from the
10736 -- current instance, those entities are made private again. If the
10737 -- actual is currently in use, these entities are also use-visible.
10738
10739 -- The loop through the actual entities also steps through the formal
10740 -- entities and enters associations from formals to actuals into the
10741 -- renaming map. This is necessary to properly handle checking of
10742 -- actual parameter associations for later formals that depend on
10743 -- actuals declared in the formal package.
10744
10745 -- In Ada 2005, partial parameterization requires that we make
10746 -- visible the actuals corresponding to formals that were defaulted
10747 -- in the formal package. There formals are identified because they
10748 -- remain formal generics within the formal package, rather than
10749 -- being renamings of the actuals supplied.
10750
10751 declare
10752 Gen_Decl : constant Node_Id :=
10753 Unit_Declaration_Node (Gen_Parent);
10754 Formals : constant List_Id :=
10755 Generic_Formal_Declarations (Gen_Decl);
10756
10757 Actual_Ent : Entity_Id;
10758 Actual_Of_Formal : Node_Id;
10759 Formal_Node : Node_Id;
10760 Formal_Ent : Entity_Id;
10761
10762 begin
10763 if Present (Formals) then
10764 Formal_Node := First_Non_Pragma (Formals);
10765 else
10766 Formal_Node := Empty;
10767 end if;
10768
10769 Actual_Ent := First_Entity (Actual_Pack);
10770 Actual_Of_Formal :=
10771 First (Visible_Declarations (Specification (Analyzed_Formal)));
10772 while Present (Actual_Ent)
10773 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10774 loop
10775 if Present (Formal_Node) then
10776 Formal_Ent := Get_Formal_Entity (Formal_Node);
10777
10778 if Present (Formal_Ent) then
10779 Find_Matching_Actual (Formal_Node, Actual_Ent);
10780 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10781
10782 -- We iterate at the same time over the actuals of the
10783 -- local package created for the formal, to determine
10784 -- which one of the formals of the original generic were
10785 -- defaulted in the formal. The corresponding actual
10786 -- entities are visible in the enclosing instance.
10787
10788 if Box_Present (Formal)
10789 or else
10790 (Present (Actual_Of_Formal)
10791 and then
10792 Is_Generic_Formal
10793 (Get_Formal_Entity (Actual_Of_Formal)))
10794 then
10795 Set_Is_Hidden (Actual_Ent, False);
10796 Set_Is_Visible_Formal (Actual_Ent);
10797 Set_Is_Potentially_Use_Visible
10798 (Actual_Ent, In_Use (Actual_Pack));
10799
10800 if Ekind (Actual_Ent) = E_Package then
10801 Process_Nested_Formal (Actual_Ent);
10802 end if;
10803
10804 else
10805 if not Is_Hidden (Actual_Ent) then
10806 Append_Elmt (Actual_Ent, Hidden_Formals);
10807 end if;
10808
10809 Set_Is_Hidden (Actual_Ent);
10810 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10811 end if;
10812 end if;
10813
10814 Next_Non_Pragma (Formal_Node);
10815 Next (Actual_Of_Formal);
10816
10817 -- A formal subprogram may be overloaded, so advance in
10818 -- the list of actuals to make sure we do not match two
10819 -- successive formals to the same actual. This is only
10820 -- relevant for overloadable entities, others have
10821 -- distinct names.
10822
10823 if Is_Overloadable (Actual_Ent) then
10824 Next_Entity (Actual_Ent);
10825 end if;
10826
10827 else
10828 -- No further formals to match, but the generic part may
10829 -- contain inherited operation that are not hidden in the
10830 -- enclosing instance.
10831
10832 Next_Entity (Actual_Ent);
10833 end if;
10834 end loop;
10835
10836 -- Inherited subprograms generated by formal derived types are
10837 -- also visible if the types are.
10838
10839 Actual_Ent := First_Entity (Actual_Pack);
10840 while Present (Actual_Ent)
10841 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10842 loop
10843 if Is_Overloadable (Actual_Ent)
10844 and then
10845 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10846 and then
10847 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10848 then
10849 Set_Is_Hidden (Actual_Ent, False);
10850 Set_Is_Potentially_Use_Visible
10851 (Actual_Ent, In_Use (Actual_Pack));
10852 end if;
10853
10854 Next_Entity (Actual_Ent);
10855 end loop;
10856
10857 -- No conformance to check if the generic has no formal parameters
10858 -- and the formal package has no generic associations.
10859
10860 if Is_Empty_List (Formals)
10861 and then
10862 (Box_Present (Formal)
10863 or else No (Generic_Associations (Formal)))
10864 then
10865 return Decls;
10866 end if;
10867 end;
10868
10869 -- If the formal is not declared with a box, reanalyze it as an
10870 -- abbreviated instantiation, to verify the matching rules of 12.7.
10871 -- The actual checks are performed after the generic associations
10872 -- have been analyzed, to guarantee the same visibility for this
10873 -- instantiation and for the actuals.
10874
10875 -- In Ada 2005, the generic associations for the formal can include
10876 -- defaulted parameters. These are ignored during check. This
10877 -- internal instantiation is removed from the tree after conformance
10878 -- checking, because it contains formal declarations for those
10879 -- defaulted parameters, and those should not reach the back-end.
10880
10881 if not Box_Present (Formal) then
10882 declare
10883 I_Pack : constant Entity_Id :=
10884 Make_Temporary (Sloc (Actual), 'P');
10885
10886 begin
10887 Set_Is_Internal (I_Pack);
10888 Mutate_Ekind (I_Pack, E_Package);
10889 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10890
10891 Append_To (Decls,
10892 Make_Package_Instantiation (Sloc (Actual),
10893 Defining_Unit_Name => I_Pack,
10894 Name =>
10895 New_Occurrence_Of
10896 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10897 Generic_Associations => Generic_Associations (Formal)));
10898 end;
10899 end if;
10900
10901 return Decls;
10902 end if;
10903 end Instantiate_Formal_Package;
10904
10905 -----------------------------------
10906 -- Instantiate_Formal_Subprogram --
10907 -----------------------------------
10908
10909 function Instantiate_Formal_Subprogram
10910 (Formal : Node_Id;
10911 Actual : Node_Id;
10912 Analyzed_Formal : Node_Id) return Node_Id
10913 is
10914 Analyzed_S : constant Entity_Id :=
10915 Defining_Unit_Name (Specification (Analyzed_Formal));
10916 Formal_Sub : constant Entity_Id :=
10917 Defining_Unit_Name (Specification (Formal));
10918
10919 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10920 -- If the generic is a child unit, the parent has been installed on the
10921 -- scope stack, but a default subprogram cannot resolve to something
10922 -- on the parent because that parent is not really part of the visible
10923 -- context (it is there to resolve explicit local entities). If the
10924 -- default has resolved in this way, we remove the entity from immediate
10925 -- visibility and analyze the node again to emit an error message or
10926 -- find another visible candidate.
10927
10928 procedure Valid_Actual_Subprogram (Act : Node_Id);
10929 -- Perform legality check and raise exception on failure
10930
10931 -----------------------
10932 -- From_Parent_Scope --
10933 -----------------------
10934
10935 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10936 Gen_Scope : Node_Id;
10937
10938 begin
10939 Gen_Scope := Scope (Analyzed_S);
10940 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10941 if Scope (Subp) = Scope (Gen_Scope) then
10942 return True;
10943 end if;
10944
10945 Gen_Scope := Scope (Gen_Scope);
10946 end loop;
10947
10948 return False;
10949 end From_Parent_Scope;
10950
10951 -----------------------------
10952 -- Valid_Actual_Subprogram --
10953 -----------------------------
10954
10955 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10956 Act_E : Entity_Id;
10957
10958 begin
10959 if Is_Entity_Name (Act) then
10960 Act_E := Entity (Act);
10961
10962 elsif Nkind (Act) = N_Selected_Component
10963 and then Is_Entity_Name (Selector_Name (Act))
10964 then
10965 Act_E := Entity (Selector_Name (Act));
10966
10967 else
10968 Act_E := Empty;
10969 end if;
10970
10971 if (Present (Act_E) and then Is_Overloadable (Act_E))
10972 or else Nkind (Act) in N_Attribute_Reference
10973 | N_Indexed_Component
10974 | N_Character_Literal
10975 | N_Explicit_Dereference
10976 then
10977 return;
10978 end if;
10979
10980 Error_Msg_NE
10981 ("expect subprogram or entry name in instantiation of &",
10982 Instantiation_Node, Formal_Sub);
10983 Abandon_Instantiation (Instantiation_Node);
10984 end Valid_Actual_Subprogram;
10985
10986 -- Local variables
10987
10988 Decl_Node : Node_Id;
10989 Loc : Source_Ptr;
10990 Nam : Node_Id;
10991 New_Spec : Node_Id;
10992 New_Subp : Entity_Id;
10993
10994 -- Start of processing for Instantiate_Formal_Subprogram
10995
10996 begin
10997 New_Spec := New_Copy_Tree (Specification (Formal));
10998
10999 -- The tree copy has created the proper instantiation sloc for the
11000 -- new specification. Use this location for all other constructed
11001 -- declarations.
11002
11003 Loc := Sloc (Defining_Unit_Name (New_Spec));
11004
11005 -- Create new entity for the actual (New_Copy_Tree does not), and
11006 -- indicate that it is an actual.
11007
11008 -- If the actual is not an entity (i.e. an attribute reference)
11009 -- and the formal includes aspect specifications for contracts,
11010 -- we create an internal name for the renaming declaration. The
11011 -- constructed wrapper contains a call to the entity in the renaming.
11012 -- This is an expansion activity, as is the wrapper creation.
11013
11014 if Ada_Version >= Ada_2020
11015 and then Has_Contracts (Analyzed_Formal)
11016 and then not Is_Entity_Name (Actual)
11017 and then Expander_Active
11018 then
11019 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11020 Set_Defining_Unit_Name (New_Spec, New_Subp);
11021 else
11022 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11023 end if;
11024
11025 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
11026 Set_Is_Generic_Actual_Subprogram (New_Subp);
11027 Set_Defining_Unit_Name (New_Spec, New_Subp);
11028
11029 -- Create new entities for the each of the formals in the specification
11030 -- of the renaming declaration built for the actual.
11031
11032 if Present (Parameter_Specifications (New_Spec)) then
11033 declare
11034 F : Node_Id;
11035 F_Id : Entity_Id;
11036
11037 begin
11038 F := First (Parameter_Specifications (New_Spec));
11039 while Present (F) loop
11040 F_Id := Defining_Identifier (F);
11041
11042 Set_Defining_Identifier (F,
11043 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11044 Next (F);
11045 end loop;
11046 end;
11047 end if;
11048
11049 -- Find entity of actual. If the actual is an attribute reference, it
11050 -- cannot be resolved here (its formal is missing) but is handled
11051 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11052 -- fully resolved subsequently, when the renaming declaration for the
11053 -- formal is analyzed. If it is an explicit dereference, resolve the
11054 -- prefix but not the actual itself, to prevent interpretation as call.
11055
11056 if Present (Actual) then
11057 Loc := Sloc (Actual);
11058 Set_Sloc (New_Spec, Loc);
11059
11060 if Nkind (Actual) = N_Operator_Symbol then
11061 Find_Direct_Name (Actual);
11062
11063 elsif Nkind (Actual) = N_Explicit_Dereference then
11064 Analyze (Prefix (Actual));
11065
11066 elsif Nkind (Actual) /= N_Attribute_Reference then
11067 Analyze (Actual);
11068 end if;
11069
11070 Valid_Actual_Subprogram (Actual);
11071 Nam := Actual;
11072
11073 elsif Present (Default_Name (Formal)) then
11074 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11075 | N_Selected_Component
11076 | N_Indexed_Component
11077 | N_Character_Literal
11078 and then Present (Entity (Default_Name (Formal)))
11079 then
11080 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11081 else
11082 Nam := New_Copy (Default_Name (Formal));
11083 Set_Sloc (Nam, Loc);
11084 end if;
11085
11086 elsif Box_Present (Formal) then
11087
11088 -- Actual is resolved at the point of instantiation. Create an
11089 -- identifier or operator with the same name as the formal.
11090
11091 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11092 Nam :=
11093 Make_Operator_Symbol (Loc,
11094 Chars => Chars (Formal_Sub),
11095 Strval => No_String);
11096 else
11097 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11098 end if;
11099
11100 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11101 and then Null_Present (Specification (Formal))
11102 then
11103 -- Generate null body for procedure, for use in the instance
11104
11105 Decl_Node :=
11106 Make_Subprogram_Body (Loc,
11107 Specification => New_Spec,
11108 Declarations => New_List,
11109 Handled_Statement_Sequence =>
11110 Make_Handled_Sequence_Of_Statements (Loc,
11111 Statements => New_List (Make_Null_Statement (Loc))));
11112
11113 -- RM 12.6 (16 2/2): The procedure has convention Intrinsic
11114
11115 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11116
11117 -- Eliminate the calls to it when optimization is enabled
11118
11119 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11120 return Decl_Node;
11121
11122 else
11123 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11124 Error_Msg_NE
11125 ("missing actual&", Instantiation_Node, Formal_Sub);
11126 Error_Msg_NE
11127 ("\in instantiation of & declared#",
11128 Instantiation_Node, Scope (Analyzed_S));
11129 Abandon_Instantiation (Instantiation_Node);
11130 end if;
11131
11132 Decl_Node :=
11133 Make_Subprogram_Renaming_Declaration (Loc,
11134 Specification => New_Spec,
11135 Name => Nam);
11136
11137 -- If we do not have an actual and the formal specified <> then set to
11138 -- get proper default.
11139
11140 if No (Actual) and then Box_Present (Formal) then
11141 Set_From_Default (Decl_Node);
11142 end if;
11143
11144 -- Gather possible interpretations for the actual before analyzing the
11145 -- instance. If overloaded, it will be resolved when analyzing the
11146 -- renaming declaration.
11147
11148 if Box_Present (Formal) and then No (Actual) then
11149 Analyze (Nam);
11150
11151 if Is_Child_Unit (Scope (Analyzed_S))
11152 and then Present (Entity (Nam))
11153 then
11154 if not Is_Overloaded (Nam) then
11155 if From_Parent_Scope (Entity (Nam)) then
11156 Set_Is_Immediately_Visible (Entity (Nam), False);
11157 Set_Entity (Nam, Empty);
11158 Set_Etype (Nam, Empty);
11159
11160 Analyze (Nam);
11161 Set_Is_Immediately_Visible (Entity (Nam));
11162 end if;
11163
11164 else
11165 declare
11166 I : Interp_Index;
11167 It : Interp;
11168
11169 begin
11170 Get_First_Interp (Nam, I, It);
11171 while Present (It.Nam) loop
11172 if From_Parent_Scope (It.Nam) then
11173 Remove_Interp (I);
11174 end if;
11175
11176 Get_Next_Interp (I, It);
11177 end loop;
11178 end;
11179 end if;
11180 end if;
11181 end if;
11182
11183 -- The generic instantiation freezes the actual. This can only be done
11184 -- once the actual is resolved, in the analysis of the renaming
11185 -- declaration. To make the formal subprogram entity available, we set
11186 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11187 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11188 -- of formal abstract subprograms.
11189
11190 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11191
11192 -- We cannot analyze the renaming declaration, and thus find the actual,
11193 -- until all the actuals are assembled in the instance. For subsequent
11194 -- checks of other actuals, indicate the node that will hold the
11195 -- instance of this formal.
11196
11197 Set_Instance_Of (Analyzed_S, Nam);
11198
11199 if Nkind (Actual) = N_Selected_Component
11200 and then Is_Task_Type (Etype (Prefix (Actual)))
11201 and then not Is_Frozen (Etype (Prefix (Actual)))
11202 then
11203 -- The renaming declaration will create a body, which must appear
11204 -- outside of the instantiation, We move the renaming declaration
11205 -- out of the instance, and create an additional renaming inside,
11206 -- to prevent freezing anomalies.
11207
11208 declare
11209 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11210
11211 begin
11212 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11213 Insert_Before (Instantiation_Node, Decl_Node);
11214 Analyze (Decl_Node);
11215
11216 -- Now create renaming within the instance
11217
11218 Decl_Node :=
11219 Make_Subprogram_Renaming_Declaration (Loc,
11220 Specification => New_Copy_Tree (New_Spec),
11221 Name => New_Occurrence_Of (Anon_Id, Loc));
11222
11223 Set_Defining_Unit_Name (Specification (Decl_Node),
11224 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11225 end;
11226 end if;
11227
11228 return Decl_Node;
11229 end Instantiate_Formal_Subprogram;
11230
11231 ------------------------
11232 -- Instantiate_Object --
11233 ------------------------
11234
11235 function Instantiate_Object
11236 (Formal : Node_Id;
11237 Actual : Node_Id;
11238 Analyzed_Formal : Node_Id) return List_Id
11239 is
11240 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11241 A_Gen_Obj : constant Entity_Id :=
11242 Defining_Identifier (Analyzed_Formal);
11243 Acc_Def : Node_Id := Empty;
11244 Act_Assoc : constant Node_Id := Parent (Actual);
11245 Actual_Decl : Node_Id := Empty;
11246 Decl_Node : Node_Id;
11247 Def : Node_Id;
11248 Ftyp : Entity_Id;
11249 List : constant List_Id := New_List;
11250 Loc : constant Source_Ptr := Sloc (Actual);
11251 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11252 Subt_Decl : Node_Id := Empty;
11253 Subt_Mark : Node_Id := Empty;
11254
11255 -- Start of processing for Instantiate_Object
11256
11257 begin
11258 -- Formal may be an anonymous access
11259
11260 if Present (Subtype_Mark (Formal)) then
11261 Subt_Mark := Subtype_Mark (Formal);
11262 else
11263 Check_Access_Definition (Formal);
11264 Acc_Def := Access_Definition (Formal);
11265 end if;
11266
11267 -- Sloc for error message on missing actual
11268
11269 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11270
11271 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11272 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11273 end if;
11274
11275 Set_Parent (List, Parent (Actual));
11276
11277 -- OUT present
11278
11279 if Out_Present (Formal) then
11280
11281 -- An IN OUT generic actual must be a name. The instantiation is a
11282 -- renaming declaration. The actual is the name being renamed. We
11283 -- use the actual directly, rather than a copy, because it is not
11284 -- used further in the list of actuals, and because a copy or a use
11285 -- of relocate_node is incorrect if the instance is nested within a
11286 -- generic. In order to simplify e.g. ASIS queries, the
11287 -- Generic_Parent field links the declaration to the generic
11288 -- association.
11289
11290 if No (Actual) then
11291 Error_Msg_NE
11292 ("missing actual &",
11293 Instantiation_Node, Gen_Obj);
11294 Error_Msg_NE
11295 ("\in instantiation of & declared#",
11296 Instantiation_Node, Scope (A_Gen_Obj));
11297 Abandon_Instantiation (Instantiation_Node);
11298 end if;
11299
11300 if Present (Subt_Mark) then
11301 Decl_Node :=
11302 Make_Object_Renaming_Declaration (Loc,
11303 Defining_Identifier => New_Copy (Gen_Obj),
11304 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11305 Name => Actual);
11306
11307 else pragma Assert (Present (Acc_Def));
11308 Decl_Node :=
11309 Make_Object_Renaming_Declaration (Loc,
11310 Defining_Identifier => New_Copy (Gen_Obj),
11311 Access_Definition => New_Copy_Tree (Acc_Def),
11312 Name => Actual);
11313 end if;
11314
11315 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11316
11317 -- The analysis of the actual may produce Insert_Action nodes, so
11318 -- the declaration must have a context in which to attach them.
11319
11320 Append (Decl_Node, List);
11321 Analyze (Actual);
11322
11323 -- Return if the analysis of the actual reported some error
11324
11325 if Etype (Actual) = Any_Type then
11326 return List;
11327 end if;
11328
11329 -- This check is performed here because Analyze_Object_Renaming will
11330 -- not check it when Comes_From_Source is False. Note though that the
11331 -- check for the actual being the name of an object will be performed
11332 -- in Analyze_Object_Renaming.
11333
11334 if Is_Object_Reference (Actual)
11335 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11336 then
11337 Error_Msg_N
11338 ("illegal discriminant-dependent component for in out parameter",
11339 Actual);
11340 end if;
11341
11342 -- The actual has to be resolved in order to check that it is a
11343 -- variable (due to cases such as F (1), where F returns access to
11344 -- an array, and for overloaded prefixes).
11345
11346 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11347
11348 -- If the type of the formal is not itself a formal, and the current
11349 -- unit is a child unit, the formal type must be declared in a
11350 -- parent, and must be retrieved by visibility.
11351
11352 if Ftyp = Orig_Ftyp
11353 and then Is_Generic_Unit (Scope (Ftyp))
11354 and then Is_Child_Unit (Scope (A_Gen_Obj))
11355 then
11356 declare
11357 Temp : constant Node_Id :=
11358 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11359 begin
11360 Set_Entity (Temp, Empty);
11361 Find_Type (Temp);
11362 Ftyp := Entity (Temp);
11363 end;
11364 end if;
11365
11366 if Is_Private_Type (Ftyp)
11367 and then not Is_Private_Type (Etype (Actual))
11368 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11369 or else Base_Type (Etype (Actual)) = Ftyp)
11370 then
11371 -- If the actual has the type of the full view of the formal, or
11372 -- else a non-private subtype of the formal, then the visibility
11373 -- of the formal type has changed. Add to the actuals a subtype
11374 -- declaration that will force the exchange of views in the body
11375 -- of the instance as well.
11376
11377 Subt_Decl :=
11378 Make_Subtype_Declaration (Loc,
11379 Defining_Identifier => Make_Temporary (Loc, 'P'),
11380 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11381
11382 Prepend (Subt_Decl, List);
11383
11384 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11385 Exchange_Declarations (Ftyp);
11386 end if;
11387
11388 Resolve (Actual, Ftyp);
11389
11390 if not Denotes_Variable (Actual) then
11391 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11392
11393 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11394
11395 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11396 -- the type of the actual shall resolve to a specific anonymous
11397 -- access type.
11398
11399 if Ada_Version < Ada_2005
11400 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11401 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11402 then
11403 Error_Msg_NE
11404 ("type of actual does not match type of&", Actual, Gen_Obj);
11405 end if;
11406 end if;
11407
11408 Note_Possible_Modification (Actual, Sure => True);
11409
11410 -- Check for instantiation with atomic/volatile/VFA object actual for
11411 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11412
11413 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11414 Error_Msg_NE
11415 ("cannot instantiate nonatomic formal & of mode in out",
11416 Actual, Gen_Obj);
11417 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11418
11419 elsif Is_Volatile_Object_Ref (Actual)
11420 and then not Is_Volatile (Orig_Ftyp)
11421 then
11422 Error_Msg_NE
11423 ("cannot instantiate nonvolatile formal & of mode in out",
11424 Actual, Gen_Obj);
11425 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11426
11427 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
11428 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11429 then
11430 Error_Msg_NE
11431 ("cannot instantiate nonfull access formal & of mode in out",
11432 Actual, Gen_Obj);
11433 Error_Msg_N
11434 ("\with full access object actual (RM C.6(12))", Actual);
11435 end if;
11436
11437 -- Check for instantiation on nonatomic subcomponent of a full access
11438 -- object in Ada 2020 (RM C.6 (12)).
11439
11440 if Ada_Version >= Ada_2020
11441 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11442 and then not Is_Atomic_Object (Actual)
11443 then
11444 Error_Msg_NE
11445 ("cannot instantiate formal & of mode in out with actual",
11446 Actual, Gen_Obj);
11447 Error_Msg_N
11448 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11449 Actual);
11450 end if;
11451
11452 -- Check actual/formal compatibility with respect to the four
11453 -- volatility refinement aspects.
11454
11455 declare
11456 Actual_Obj : Entity_Id;
11457 N : Node_Id := Actual;
11458 begin
11459 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11460 -- pointer dereference like component selection.
11461 loop
11462 if Is_Entity_Name (N) then
11463 Actual_Obj := Entity (N);
11464 exit;
11465 end if;
11466
11467 case Nkind (N) is
11468 when N_Indexed_Component
11469 | N_Selected_Component
11470 | N_Slice
11471 | N_Explicit_Dereference
11472 =>
11473 N := Prefix (N);
11474
11475 when N_Type_Conversion =>
11476 N := Expression (N);
11477
11478 when others =>
11479 Actual_Obj := Etype (N);
11480 exit;
11481 end case;
11482 end loop;
11483
11484 Check_Volatility_Compatibility
11485 (Actual_Obj, A_Gen_Obj, "actual object",
11486 "its corresponding formal object of mode in out",
11487 Srcpos_Bearer => Actual);
11488 end;
11489
11490 -- Formal in-parameter
11491
11492 else
11493 -- The instantiation of a generic formal in-parameter is constant
11494 -- declaration. The actual is the expression for that declaration.
11495 -- Its type is a full copy of the type of the formal. This may be
11496 -- an access to subprogram, for which we need to generate entities
11497 -- for the formals in the new signature.
11498
11499 if Present (Actual) then
11500 if Present (Subt_Mark) then
11501 Def := New_Copy_Tree (Subt_Mark);
11502 else
11503 pragma Assert (Present (Acc_Def));
11504 Def := New_Copy_Tree (Acc_Def);
11505 end if;
11506
11507 Decl_Node :=
11508 Make_Object_Declaration (Loc,
11509 Defining_Identifier => New_Copy (Gen_Obj),
11510 Constant_Present => True,
11511 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11512 Object_Definition => Def,
11513 Expression => Actual);
11514
11515 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11516
11517 -- A generic formal object of a tagged type is defined to be
11518 -- aliased so the new constant must also be treated as aliased.
11519
11520 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11521 Set_Aliased_Present (Decl_Node);
11522 end if;
11523
11524 Append (Decl_Node, List);
11525
11526 -- No need to repeat (pre-)analysis of some expression nodes
11527 -- already handled in Preanalyze_Actuals.
11528
11529 if Nkind (Actual) /= N_Allocator then
11530 Analyze (Actual);
11531
11532 -- Return if the analysis of the actual reported some error
11533
11534 if Etype (Actual) = Any_Type then
11535 return List;
11536 end if;
11537 end if;
11538
11539 declare
11540 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11541 Typ : Entity_Id;
11542
11543 begin
11544 Typ := Get_Instance_Of (Formal_Type);
11545
11546 -- If the actual appears in the current or an enclosing scope,
11547 -- use its type directly. This is relevant if it has an actual
11548 -- subtype that is distinct from its nominal one. This cannot
11549 -- be done in general because the type of the actual may
11550 -- depend on other actuals, and only be fully determined when
11551 -- the enclosing instance is analyzed.
11552
11553 if Present (Etype (Actual))
11554 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11555 then
11556 Freeze_Before (Instantiation_Node, Etype (Actual));
11557 else
11558 Freeze_Before (Instantiation_Node, Typ);
11559 end if;
11560
11561 -- If the actual is an aggregate, perform name resolution on
11562 -- its components (the analysis of an aggregate does not do it)
11563 -- to capture local names that may be hidden if the generic is
11564 -- a child unit.
11565
11566 if Nkind (Actual) = N_Aggregate then
11567 Preanalyze_And_Resolve (Actual, Typ);
11568 end if;
11569
11570 if Is_Limited_Type (Typ)
11571 and then not OK_For_Limited_Init (Typ, Actual)
11572 then
11573 Error_Msg_N
11574 ("initialization not allowed for limited types", Actual);
11575 Explain_Limited_Type (Typ, Actual);
11576 end if;
11577 end;
11578
11579 elsif Present (Default_Expression (Formal)) then
11580
11581 -- Use default to construct declaration
11582
11583 if Present (Subt_Mark) then
11584 Def := New_Copy_Tree (Subt_Mark);
11585 else
11586 pragma Assert (Present (Acc_Def));
11587 Def := New_Copy_Tree (Acc_Def);
11588 end if;
11589
11590 Decl_Node :=
11591 Make_Object_Declaration (Sloc (Formal),
11592 Defining_Identifier => New_Copy (Gen_Obj),
11593 Constant_Present => True,
11594 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11595 Object_Definition => Def,
11596 Expression => New_Copy_Tree
11597 (Default_Expression (Formal)));
11598
11599 Set_Corresponding_Generic_Association
11600 (Decl_Node, Expression (Decl_Node));
11601
11602 Append (Decl_Node, List);
11603 Set_Analyzed (Expression (Decl_Node), False);
11604
11605 else
11606 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11607 Error_Msg_NE ("\in instantiation of & declared#",
11608 Instantiation_Node, Scope (A_Gen_Obj));
11609
11610 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11611
11612 -- Create dummy constant declaration so that instance can be
11613 -- analyzed, to minimize cascaded visibility errors.
11614
11615 if Present (Subt_Mark) then
11616 Def := Subt_Mark;
11617 else pragma Assert (Present (Acc_Def));
11618 Def := Acc_Def;
11619 end if;
11620
11621 Decl_Node :=
11622 Make_Object_Declaration (Loc,
11623 Defining_Identifier => New_Copy (Gen_Obj),
11624 Constant_Present => True,
11625 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11626 Object_Definition => New_Copy (Def),
11627 Expression =>
11628 Make_Attribute_Reference (Sloc (Gen_Obj),
11629 Attribute_Name => Name_First,
11630 Prefix => New_Copy (Def)));
11631
11632 Append (Decl_Node, List);
11633
11634 else
11635 Abandon_Instantiation (Instantiation_Node);
11636 end if;
11637 end if;
11638 end if;
11639
11640 if Nkind (Actual) in N_Has_Entity then
11641 Actual_Decl := Parent (Entity (Actual));
11642 end if;
11643
11644 -- Ada 2005 (AI-423) refined by AI12-0287:
11645 -- For an object_renaming_declaration with a null_exclusion or an
11646 -- access_definition that has a null_exclusion, the subtype of the
11647 -- object_name shall exclude null. In addition, if the
11648 -- object_renaming_declaration occurs within the body of a generic unit
11649 -- G or within the body of a generic unit declared within the
11650 -- declarative region of generic unit G, then:
11651 -- * if the object_name statically denotes a generic formal object of
11652 -- mode in out of G, then the declaration of that object shall have a
11653 -- null_exclusion;
11654 -- * if the object_name statically denotes a call of a generic formal
11655 -- function of G, then the declaration of the result of that function
11656 -- shall have a null_exclusion.
11657
11658 if Ada_Version >= Ada_2005
11659 and then Present (Actual_Decl)
11660 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11661 | N_Object_Declaration
11662 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11663 and then not Has_Null_Exclusion (Actual_Decl)
11664 and then Has_Null_Exclusion (Analyzed_Formal)
11665 and then Ekind (Defining_Identifier (Analyzed_Formal))
11666 = E_Generic_In_Out_Parameter
11667 and then ((In_Generic_Scope (Entity (Actual))
11668 and then In_Package_Body (Scope (Entity (Actual))))
11669 or else not Can_Never_Be_Null (Etype (Actual)))
11670 then
11671 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11672 Error_Msg_N
11673 ("actual must exclude null to match generic formal#", Actual);
11674 end if;
11675
11676 -- An effectively volatile object cannot be used as an actual in a
11677 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11678 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11679 -- legality rule, and also verifies that the actual is an object.
11680
11681 if SPARK_Mode = On
11682 and then Present (Actual)
11683 and then Is_Object_Reference (Actual)
11684 and then Is_Effectively_Volatile_Object (Actual)
11685 and then not Is_Effectively_Volatile (A_Gen_Obj)
11686 then
11687 Error_Msg_N
11688 ("volatile object cannot act as actual in generic instantiation",
11689 Actual);
11690 end if;
11691
11692 return List;
11693 end Instantiate_Object;
11694
11695 ------------------------------
11696 -- Instantiate_Package_Body --
11697 ------------------------------
11698
11699 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11700 -- must be replaced by gotos which jump to the end of the routine in order
11701 -- to restore the Ghost and SPARK modes.
11702
11703 procedure Instantiate_Package_Body
11704 (Body_Info : Pending_Body_Info;
11705 Inlined_Body : Boolean := False;
11706 Body_Optional : Boolean := False)
11707 is
11708 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11709 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11710 Act_Spec : constant Node_Id := Specification (Act_Decl);
11711 Ctx_Parents : Elist_Id := No_Elist;
11712 Ctx_Top : Int := 0;
11713 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11714 Gen_Id : constant Node_Id := Name (Inst_Node);
11715 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11716 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11717 Loc : constant Source_Ptr := Sloc (Inst_Node);
11718
11719 procedure Check_Initialized_Types;
11720 -- In a generic package body, an entity of a generic private type may
11721 -- appear uninitialized. This is suspicious, unless the actual is a
11722 -- fully initialized type.
11723
11724 procedure Install_Parents_Of_Generic_Context
11725 (Inst_Scope : Entity_Id;
11726 Ctx_Parents : out Elist_Id);
11727 -- Inst_Scope is the scope where the instance appears within; when it
11728 -- appears within a generic child package G, this routine collects and
11729 -- installs the enclosing packages of G in the scopes stack; installed
11730 -- packages are returned in Ctx_Parents.
11731
11732 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11733 -- Reverse effect after instantiation is complete
11734
11735 -----------------------------
11736 -- Check_Initialized_Types --
11737 -----------------------------
11738
11739 procedure Check_Initialized_Types is
11740 Decl : Node_Id;
11741 Formal : Entity_Id;
11742 Actual : Entity_Id;
11743 Uninit_Var : Entity_Id;
11744
11745 begin
11746 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11747 while Present (Decl) loop
11748 Uninit_Var := Empty;
11749
11750 if Nkind (Decl) = N_Private_Extension_Declaration then
11751 Uninit_Var := Uninitialized_Variable (Decl);
11752
11753 elsif Nkind (Decl) = N_Formal_Type_Declaration
11754 and then Nkind (Formal_Type_Definition (Decl)) =
11755 N_Formal_Private_Type_Definition
11756 then
11757 Uninit_Var :=
11758 Uninitialized_Variable (Formal_Type_Definition (Decl));
11759 end if;
11760
11761 if Present (Uninit_Var) then
11762 Formal := Defining_Identifier (Decl);
11763 Actual := First_Entity (Act_Decl_Id);
11764
11765 -- For each formal there is a subtype declaration that renames
11766 -- the actual and has the same name as the formal. Locate the
11767 -- formal for warning message about uninitialized variables
11768 -- in the generic, for which the actual type should be a fully
11769 -- initialized type.
11770
11771 while Present (Actual) loop
11772 exit when Ekind (Actual) = E_Package
11773 and then Present (Renamed_Object (Actual));
11774
11775 if Chars (Actual) = Chars (Formal)
11776 and then not Is_Scalar_Type (Actual)
11777 and then not Is_Fully_Initialized_Type (Actual)
11778 and then Warn_On_No_Value_Assigned
11779 then
11780 Error_Msg_Node_2 := Formal;
11781 Error_Msg_NE
11782 ("generic unit has uninitialized variable& of "
11783 & "formal private type &?v?", Actual, Uninit_Var);
11784 Error_Msg_NE
11785 ("actual type for& should be fully initialized type?v?",
11786 Actual, Formal);
11787 exit;
11788 end if;
11789
11790 Next_Entity (Actual);
11791 end loop;
11792 end if;
11793
11794 Next (Decl);
11795 end loop;
11796 end Check_Initialized_Types;
11797
11798 ----------------------------------------
11799 -- Install_Parents_Of_Generic_Context --
11800 ----------------------------------------
11801
11802 procedure Install_Parents_Of_Generic_Context
11803 (Inst_Scope : Entity_Id;
11804 Ctx_Parents : out Elist_Id)
11805 is
11806 Elmt : Elmt_Id;
11807 S : Entity_Id;
11808
11809 begin
11810 Ctx_Parents := New_Elmt_List;
11811
11812 -- Collect context parents (ie. parents where the instantiation
11813 -- appears within).
11814
11815 S := Inst_Scope;
11816 while S /= Standard_Standard loop
11817 Prepend_Elmt (S, Ctx_Parents);
11818 S := Scope (S);
11819 end loop;
11820
11821 -- Install enclosing parents
11822
11823 Elmt := First_Elmt (Ctx_Parents);
11824 while Present (Elmt) loop
11825 Push_Scope (Node (Elmt));
11826 Set_Is_Immediately_Visible (Node (Elmt));
11827 Next_Elmt (Elmt);
11828 end loop;
11829 end Install_Parents_Of_Generic_Context;
11830
11831 ---------------------------------------
11832 -- Remove_Parents_Of_Generic_Context --
11833 ---------------------------------------
11834
11835 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11836 Elmt : Elmt_Id;
11837
11838 begin
11839 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11840
11841 Elmt := Last_Elmt (Ctx_Parents);
11842 while Present (Elmt) loop
11843 pragma Assert (Current_Scope = Node (Elmt));
11844 Set_Is_Immediately_Visible (Current_Scope, False);
11845 Pop_Scope;
11846
11847 Remove_Last_Elmt (Ctx_Parents);
11848 Elmt := Last_Elmt (Ctx_Parents);
11849 end loop;
11850 end Remove_Parents_Of_Generic_Context;
11851
11852 -- Local variables
11853
11854 -- The following constants capture the context prior to instantiating
11855 -- the package body.
11856
11857 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11858 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11859 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11860 Saved_ISMP : constant Boolean :=
11861 Ignore_SPARK_Mode_Pragmas_In_Instance;
11862 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11863 Local_Suppress_Stack_Top;
11864 Saved_SC : constant Boolean := Style_Check;
11865 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11866 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11867 Saved_SS : constant Suppress_Record := Scope_Suppress;
11868 Saved_Warn : constant Warning_Record := Save_Warnings;
11869
11870 Act_Body : Node_Id;
11871 Act_Body_Id : Entity_Id;
11872 Act_Body_Name : Node_Id;
11873 Gen_Body : Node_Id;
11874 Gen_Body_Id : Node_Id;
11875 Par_Ent : Entity_Id := Empty;
11876 Par_Installed : Boolean := False;
11877 Par_Vis : Boolean := False;
11878
11879 Scope_Check_Id : Entity_Id;
11880 Scope_Check_Last : Nat;
11881 -- Value of Current_Scope before calls to Install_Parents; used to check
11882 -- that scopes are correctly removed after instantiation.
11883
11884 Vis_Prims_List : Elist_Id := No_Elist;
11885 -- List of primitives made temporarily visible in the instantiation
11886 -- to match the visibility of the formal type.
11887
11888 -- Start of processing for Instantiate_Package_Body
11889
11890 begin
11891 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11892
11893 -- The instance body may already have been processed, as the parent of
11894 -- another instance that is inlined (Load_Parent_Of_Generic).
11895
11896 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11897 return;
11898 end if;
11899
11900 -- The package being instantiated may be subject to pragma Ghost. Set
11901 -- the mode now to ensure that any nodes generated during instantiation
11902 -- are properly marked as Ghost.
11903
11904 Set_Ghost_Mode (Act_Decl_Id);
11905
11906 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11907
11908 -- Re-establish the state of information on which checks are suppressed.
11909 -- This information was set in Body_Info at the point of instantiation,
11910 -- and now we restore it so that the instance is compiled using the
11911 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11912
11913 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11914 Scope_Suppress := Body_Info.Scope_Suppress;
11915
11916 Restore_Config_Switches (Body_Info.Config_Switches);
11917 Restore_Warnings (Body_Info.Warnings);
11918
11919 if No (Gen_Body_Id) then
11920
11921 -- Do not look for parent of generic body if none is required.
11922 -- This may happen when the routine is called as part of the
11923 -- Pending_Instantiations processing, when nested instances
11924 -- may precede the one generated from the main unit.
11925
11926 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11927 and then Body_Optional
11928 then
11929 goto Leave;
11930 else
11931 Load_Parent_Of_Generic
11932 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11933
11934 -- Surprisingly enough, loading the body of the parent can cause
11935 -- the body to be instantiated and the double instantiation needs
11936 -- to be prevented in order to avoid giving bogus semantic errors.
11937
11938 -- This case can occur because of the Collect_Previous_Instances
11939 -- machinery of Load_Parent_Of_Generic, which will instantiate
11940 -- bodies that are deemed to be ahead of the body of the parent
11941 -- in the compilation unit. But the relative position of these
11942 -- bodies is computed using the mere comparison of their Sloc.
11943
11944 -- Now suppose that you have two generic packages G and H, with
11945 -- G containing a mere instantiation of H:
11946
11947 -- generic
11948 -- package H is
11949
11950 -- generic
11951 -- package Nested_G is
11952 -- ...
11953 -- end Nested_G;
11954
11955 -- end H;
11956
11957 -- with H;
11958
11959 -- generic
11960 -- package G is
11961
11962 -- package My_H is new H;
11963
11964 -- end G;
11965
11966 -- and a third package Q instantiating G and Nested_G:
11967
11968 -- with G;
11969
11970 -- package Q is
11971
11972 -- package My_G is new G;
11973
11974 -- package My_Nested_G is new My_G.My_H.Nested_G;
11975
11976 -- end Q;
11977
11978 -- The body to be instantiated is that of My_Nested_G and its
11979 -- parent is the instance My_G.My_H. This latter instantiation
11980 -- is done when My_G is analyzed, i.e. after the declarations
11981 -- of My_G and My_Nested_G have been parsed; as a result, the
11982 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11983
11984 -- Therefore loading the body of My_G.My_H will cause the body
11985 -- of My_Nested_G to be instantiated because it is deemed to be
11986 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11987 -- will again be invoked on My_G.My_H, but this time with the
11988 -- Collect_Previous_Instances machinery disabled, so there is
11989 -- no endless mutual recursion and things are done in order.
11990
11991 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11992 goto Leave;
11993 end if;
11994
11995 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11996 end if;
11997 end if;
11998
11999 -- Establish global variable for sloc adjustment and for error recovery
12000 -- In the case of an instance body for an instantiation with actuals
12001 -- from a limited view, the instance body is placed at the beginning
12002 -- of the enclosing package body: use the body entity as the source
12003 -- location for nodes of the instance body.
12004
12005 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
12006 declare
12007 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12008 Body_Id : constant Node_Id :=
12009 Corresponding_Body (Unit_Declaration_Node (Scop));
12010
12011 begin
12012 Instantiation_Node := Body_Id;
12013 end;
12014 else
12015 Instantiation_Node := Inst_Node;
12016 end if;
12017
12018 if Present (Gen_Body_Id) then
12019 Save_Env (Gen_Unit, Act_Decl_Id);
12020 Style_Check := False;
12021
12022 -- If the context of the instance is subject to SPARK_Mode "off", the
12023 -- annotation is missing, or the body is instantiated at a later pass
12024 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12025 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12026 -- instance.
12027
12028 if SPARK_Mode /= On
12029 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12030 then
12031 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12032 end if;
12033
12034 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12035 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12036
12037 Create_Instantiation_Source
12038 (Inst_Node, Gen_Body_Id, S_Adjustment);
12039
12040 Act_Body :=
12041 Copy_Generic_Node
12042 (Original_Node (Gen_Body), Empty, Instantiating => True);
12043
12044 -- Create proper (possibly qualified) defining name for the body, to
12045 -- correspond to the one in the spec.
12046
12047 Act_Body_Id :=
12048 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12049 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12050
12051 -- Some attributes of spec entity are not inherited by body entity
12052
12053 Set_Handler_Records (Act_Body_Id, No_List);
12054
12055 if Nkind (Defining_Unit_Name (Act_Spec)) =
12056 N_Defining_Program_Unit_Name
12057 then
12058 Act_Body_Name :=
12059 Make_Defining_Program_Unit_Name (Loc,
12060 Name =>
12061 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12062 Defining_Identifier => Act_Body_Id);
12063 else
12064 Act_Body_Name := Act_Body_Id;
12065 end if;
12066
12067 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12068
12069 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12070 Check_Generic_Actuals (Act_Decl_Id, False);
12071 Check_Initialized_Types;
12072
12073 -- Install primitives hidden at the point of the instantiation but
12074 -- visible when processing the generic formals
12075
12076 declare
12077 E : Entity_Id;
12078
12079 begin
12080 E := First_Entity (Act_Decl_Id);
12081 while Present (E) loop
12082 if Is_Type (E)
12083 and then not Is_Itype (E)
12084 and then Is_Generic_Actual_Type (E)
12085 and then Is_Tagged_Type (E)
12086 then
12087 Install_Hidden_Primitives
12088 (Prims_List => Vis_Prims_List,
12089 Gen_T => Generic_Parent_Type (Parent (E)),
12090 Act_T => E);
12091 end if;
12092
12093 Next_Entity (E);
12094 end loop;
12095 end;
12096
12097 Scope_Check_Id := Current_Scope;
12098 Scope_Check_Last := Scope_Stack.Last;
12099
12100 -- If the instantiation appears within a generic child some actual
12101 -- parameter may be the current instance of the enclosing generic
12102 -- parent.
12103
12104 declare
12105 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12106
12107 begin
12108 if Is_Child_Unit (Inst_Scope)
12109 and then Ekind (Inst_Scope) = E_Generic_Package
12110 and then Present (Generic_Associations (Inst_Node))
12111 then
12112 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12113
12114 -- Hide them from visibility; required to avoid conflicts
12115 -- installing the parent instance.
12116
12117 if Present (Ctx_Parents) then
12118 Push_Scope (Standard_Standard);
12119 Ctx_Top := Scope_Stack.Last;
12120 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12121 end if;
12122 end if;
12123 end;
12124
12125 -- If it is a child unit, make the parent instance (which is an
12126 -- instance of the parent of the generic) visible. The parent
12127 -- instance is the prefix of the name of the generic unit.
12128
12129 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12130 and then Nkind (Gen_Id) = N_Expanded_Name
12131 then
12132 Par_Ent := Entity (Prefix (Gen_Id));
12133 Par_Vis := Is_Immediately_Visible (Par_Ent);
12134 Install_Parent (Par_Ent, In_Body => True);
12135 Par_Installed := True;
12136
12137 elsif Is_Child_Unit (Gen_Unit) then
12138 Par_Ent := Scope (Gen_Unit);
12139 Par_Vis := Is_Immediately_Visible (Par_Ent);
12140 Install_Parent (Par_Ent, In_Body => True);
12141 Par_Installed := True;
12142 end if;
12143
12144 -- If the instantiation is a library unit, and this is the main unit,
12145 -- then build the resulting compilation unit nodes for the instance.
12146 -- If this is a compilation unit but it is not the main unit, then it
12147 -- is the body of a unit in the context, that is being compiled
12148 -- because it is encloses some inlined unit or another generic unit
12149 -- being instantiated. In that case, this body is not part of the
12150 -- current compilation, and is not attached to the tree, but its
12151 -- parent must be set for analysis.
12152
12153 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12154
12155 -- Replace instance node with body of instance, and create new
12156 -- node for corresponding instance declaration.
12157
12158 Build_Instance_Compilation_Unit_Nodes
12159 (Inst_Node, Act_Body, Act_Decl);
12160
12161 -- If the instantiation appears within a generic child package
12162 -- enable visibility of current instance of enclosing generic
12163 -- parents.
12164
12165 if Present (Ctx_Parents) then
12166 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12167 Analyze (Inst_Node);
12168 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12169 else
12170 Analyze (Inst_Node);
12171 end if;
12172
12173 if Parent (Inst_Node) = Cunit (Main_Unit) then
12174
12175 -- If the instance is a child unit itself, then set the scope
12176 -- of the expanded body to be the parent of the instantiation
12177 -- (ensuring that the fully qualified name will be generated
12178 -- for the elaboration subprogram).
12179
12180 if Nkind (Defining_Unit_Name (Act_Spec)) =
12181 N_Defining_Program_Unit_Name
12182 then
12183 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12184 end if;
12185 end if;
12186
12187 -- Case where instantiation is not a library unit
12188
12189 else
12190 -- If this is an early instantiation, i.e. appears textually
12191 -- before the corresponding body and must be elaborated first,
12192 -- indicate that the body instance is to be delayed.
12193
12194 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
12195
12196 -- If the instantiation appears within a generic child package
12197 -- enable visibility of current instance of enclosing generic
12198 -- parents.
12199
12200 if Present (Ctx_Parents) then
12201 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12202 Analyze (Act_Body);
12203 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12204 else
12205 Analyze (Act_Body);
12206 end if;
12207 end if;
12208
12209 Inherit_Context (Gen_Body, Inst_Node);
12210
12211 if Par_Installed then
12212 Remove_Parent (In_Body => True);
12213
12214 -- Restore the previous visibility of the parent
12215
12216 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12217 end if;
12218
12219 -- Remove the parent instances if they have been placed on the scope
12220 -- stack to compile the body.
12221
12222 if Present (Ctx_Parents) then
12223 pragma Assert (Scope_Stack.Last = Ctx_Top
12224 and then Current_Scope = Standard_Standard);
12225 Pop_Scope;
12226
12227 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12228 end if;
12229
12230 pragma Assert (Current_Scope = Scope_Check_Id);
12231 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12232
12233 Restore_Hidden_Primitives (Vis_Prims_List);
12234
12235 -- Restore the private views that were made visible when the body of
12236 -- the instantiation was created. Note that, in the case where one of
12237 -- these private views is declared in the parent, there is a nesting
12238 -- issue with the calls to Install_Parent and Remove_Parent made in
12239 -- between above with In_Body set to True, because these calls also
12240 -- want to swap and restore this private view respectively. In this
12241 -- case, the call to Install_Parent does nothing, but the call to
12242 -- Remove_Parent does restore the private view, thus undercutting the
12243 -- call to Restore_Private_Views. That's OK under the condition that
12244 -- the two mechanisms swap exactly the same entities, in particular
12245 -- the private entities dependent on the primary private entities.
12246
12247 Restore_Private_Views (Act_Decl_Id);
12248
12249 -- Remove the current unit from visibility if this is an instance
12250 -- that is not elaborated on the fly for inlining purposes.
12251
12252 if not Inlined_Body then
12253 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12254 end if;
12255
12256 Restore_Env;
12257
12258 -- If we have no body, and the unit requires a body, then complain. This
12259 -- complaint is suppressed if we have detected other errors (since a
12260 -- common reason for missing the body is that it had errors).
12261 -- In CodePeer mode, a warning has been emitted already, no need for
12262 -- further messages.
12263
12264 elsif Unit_Requires_Body (Gen_Unit)
12265 and then not Body_Optional
12266 then
12267 if CodePeer_Mode then
12268 null;
12269
12270 elsif Serious_Errors_Detected = 0 then
12271 Error_Msg_NE
12272 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12273
12274 -- Don't attempt to perform any cleanup actions if some other error
12275 -- was already detected, since this can cause blowups.
12276
12277 else
12278 goto Leave;
12279 end if;
12280
12281 -- Case of package that does not need a body
12282
12283 else
12284 -- If the instantiation of the declaration is a library unit, rewrite
12285 -- the original package instantiation as a package declaration in the
12286 -- compilation unit node.
12287
12288 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12289 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12290 Rewrite (Inst_Node, Act_Decl);
12291
12292 -- Generate elaboration entity, in case spec has elaboration code.
12293 -- This cannot be done when the instance is analyzed, because it
12294 -- is not known yet whether the body exists.
12295
12296 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12297 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12298
12299 -- If the instantiation is not a library unit, then append the
12300 -- declaration to the list of implicitly generated entities, unless
12301 -- it is already a list member which means that it was already
12302 -- processed
12303
12304 elsif not Is_List_Member (Act_Decl) then
12305 Mark_Rewrite_Insertion (Act_Decl);
12306 Insert_Before (Inst_Node, Act_Decl);
12307 end if;
12308 end if;
12309
12310 <<Leave>>
12311
12312 -- Restore the context that was in effect prior to instantiating the
12313 -- package body.
12314
12315 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12316 Local_Suppress_Stack_Top := Saved_LSST;
12317 Scope_Suppress := Saved_SS;
12318 Style_Check := Saved_SC;
12319
12320 Expander_Mode_Restore;
12321 Restore_Config_Switches (Saved_CS);
12322 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12323 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12324 Restore_Warnings (Saved_Warn);
12325 end Instantiate_Package_Body;
12326
12327 ---------------------------------
12328 -- Instantiate_Subprogram_Body --
12329 ---------------------------------
12330
12331 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12332 -- must be replaced by gotos which jump to the end of the routine in order
12333 -- to restore the Ghost and SPARK modes.
12334
12335 procedure Instantiate_Subprogram_Body
12336 (Body_Info : Pending_Body_Info;
12337 Body_Optional : Boolean := False)
12338 is
12339 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12340 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12341 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12342 Gen_Id : constant Node_Id := Name (Inst_Node);
12343 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12344 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12345 Loc : constant Source_Ptr := Sloc (Inst_Node);
12346 Pack_Id : constant Entity_Id :=
12347 Defining_Unit_Name (Parent (Act_Decl));
12348
12349 -- The following constants capture the context prior to instantiating
12350 -- the subprogram body.
12351
12352 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12353 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12354 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12355 Saved_ISMP : constant Boolean :=
12356 Ignore_SPARK_Mode_Pragmas_In_Instance;
12357 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12358 Local_Suppress_Stack_Top;
12359 Saved_SC : constant Boolean := Style_Check;
12360 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12361 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12362 Saved_SS : constant Suppress_Record := Scope_Suppress;
12363 Saved_Warn : constant Warning_Record := Save_Warnings;
12364
12365 Act_Body : Node_Id;
12366 Act_Body_Id : Entity_Id;
12367 Gen_Body : Node_Id;
12368 Gen_Body_Id : Node_Id;
12369 Pack_Body : Node_Id;
12370 Par_Ent : Entity_Id := Empty;
12371 Par_Installed : Boolean := False;
12372 Par_Vis : Boolean := False;
12373 Ret_Expr : Node_Id;
12374
12375 begin
12376 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12377
12378 -- Subprogram body may have been created already because of an inline
12379 -- pragma, or because of multiple elaborations of the enclosing package
12380 -- when several instances of the subprogram appear in the main unit.
12381
12382 if Present (Corresponding_Body (Act_Decl)) then
12383 return;
12384 end if;
12385
12386 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12387 -- the mode now to ensure that any nodes generated during instantiation
12388 -- are properly marked as Ghost.
12389
12390 Set_Ghost_Mode (Act_Decl_Id);
12391
12392 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12393
12394 -- Re-establish the state of information on which checks are suppressed.
12395 -- This information was set in Body_Info at the point of instantiation,
12396 -- and now we restore it so that the instance is compiled using the
12397 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12398
12399 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12400 Scope_Suppress := Body_Info.Scope_Suppress;
12401
12402 Restore_Config_Switches (Body_Info.Config_Switches);
12403 Restore_Warnings (Body_Info.Warnings);
12404
12405 if No (Gen_Body_Id) then
12406
12407 -- For imported generic subprogram, no body to compile, complete
12408 -- the spec entity appropriately.
12409
12410 if Is_Imported (Gen_Unit) then
12411 Set_Is_Imported (Act_Decl_Id);
12412 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12413 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12414 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12415 Set_Has_Completion (Act_Decl_Id);
12416 goto Leave;
12417
12418 -- For other cases, compile the body
12419
12420 else
12421 Load_Parent_Of_Generic
12422 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12423 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12424 end if;
12425 end if;
12426
12427 Instantiation_Node := Inst_Node;
12428
12429 if Present (Gen_Body_Id) then
12430 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12431
12432 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12433
12434 -- Either body is not present, or context is non-expanding, as
12435 -- when compiling a subunit. Mark the instance as completed, and
12436 -- diagnose a missing body when needed.
12437
12438 if Expander_Active
12439 and then Operating_Mode = Generate_Code
12440 then
12441 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12442 end if;
12443
12444 Set_Has_Completion (Act_Decl_Id);
12445 goto Leave;
12446 end if;
12447
12448 Save_Env (Gen_Unit, Act_Decl_Id);
12449 Style_Check := False;
12450
12451 -- If the context of the instance is subject to SPARK_Mode "off", the
12452 -- annotation is missing, or the body is instantiated at a later pass
12453 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12454 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12455 -- instance.
12456
12457 if SPARK_Mode /= On
12458 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12459 then
12460 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12461 end if;
12462
12463 -- If the context of an instance is not subject to SPARK_Mode "off",
12464 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12465 -- the latter should be the one applicable to the instance.
12466
12467 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12468 and then SPARK_Mode /= Off
12469 and then Present (SPARK_Pragma (Gen_Body_Id))
12470 then
12471 Set_SPARK_Mode (Gen_Body_Id);
12472 end if;
12473
12474 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12475 Create_Instantiation_Source
12476 (Inst_Node,
12477 Gen_Body_Id,
12478 S_Adjustment);
12479
12480 Act_Body :=
12481 Copy_Generic_Node
12482 (Original_Node (Gen_Body), Empty, Instantiating => True);
12483
12484 -- Create proper defining name for the body, to correspond to the one
12485 -- in the spec.
12486
12487 Act_Body_Id :=
12488 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12489
12490 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12491 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12492
12493 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12494 Set_Has_Completion (Act_Decl_Id);
12495 Check_Generic_Actuals (Pack_Id, False);
12496
12497 -- Generate a reference to link the visible subprogram instance to
12498 -- the generic body, which for navigation purposes is the only
12499 -- available source for the instance.
12500
12501 Generate_Reference
12502 (Related_Instance (Pack_Id),
12503 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12504
12505 -- If it is a child unit, make the parent instance (which is an
12506 -- instance of the parent of the generic) visible. The parent
12507 -- instance is the prefix of the name of the generic unit.
12508
12509 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12510 and then Nkind (Gen_Id) = N_Expanded_Name
12511 then
12512 Par_Ent := Entity (Prefix (Gen_Id));
12513 Par_Vis := Is_Immediately_Visible (Par_Ent);
12514 Install_Parent (Par_Ent, In_Body => True);
12515 Par_Installed := True;
12516
12517 elsif Is_Child_Unit (Gen_Unit) then
12518 Par_Ent := Scope (Gen_Unit);
12519 Par_Vis := Is_Immediately_Visible (Par_Ent);
12520 Install_Parent (Par_Ent, In_Body => True);
12521 Par_Installed := True;
12522 end if;
12523
12524 -- Subprogram body is placed in the body of wrapper package,
12525 -- whose spec contains the subprogram declaration as well as
12526 -- the renaming declarations for the generic parameters.
12527
12528 Pack_Body :=
12529 Make_Package_Body (Loc,
12530 Defining_Unit_Name => New_Copy (Pack_Id),
12531 Declarations => New_List (Act_Body));
12532
12533 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12534
12535 -- If the instantiation is a library unit, then build resulting
12536 -- compilation unit nodes for the instance. The declaration of
12537 -- the enclosing package is the grandparent of the subprogram
12538 -- declaration. First replace the instantiation node as the unit
12539 -- of the corresponding compilation.
12540
12541 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12542 if Parent (Inst_Node) = Cunit (Main_Unit) then
12543 Set_Unit (Parent (Inst_Node), Inst_Node);
12544 Build_Instance_Compilation_Unit_Nodes
12545 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12546 Analyze (Inst_Node);
12547 else
12548 Set_Parent (Pack_Body, Parent (Inst_Node));
12549 Analyze (Pack_Body);
12550 end if;
12551
12552 else
12553 Insert_Before (Inst_Node, Pack_Body);
12554 Mark_Rewrite_Insertion (Pack_Body);
12555 Analyze (Pack_Body);
12556
12557 if Expander_Active then
12558 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
12559 end if;
12560 end if;
12561
12562 Inherit_Context (Gen_Body, Inst_Node);
12563
12564 Restore_Private_Views (Pack_Id, False);
12565
12566 if Par_Installed then
12567 Remove_Parent (In_Body => True);
12568
12569 -- Restore the previous visibility of the parent
12570
12571 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12572 end if;
12573
12574 Restore_Env;
12575
12576 -- Body not found. Error was emitted already. If there were no previous
12577 -- errors, this may be an instance whose scope is a premature instance.
12578 -- In that case we must insure that the (legal) program does raise
12579 -- program error if executed. We generate a subprogram body for this
12580 -- purpose. See DEC ac30vso.
12581
12582 -- Should not reference proprietary DEC tests in comments ???
12583
12584 elsif Serious_Errors_Detected = 0
12585 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12586 then
12587 if Body_Optional then
12588 goto Leave;
12589
12590 elsif Ekind (Act_Decl_Id) = E_Procedure then
12591 Act_Body :=
12592 Make_Subprogram_Body (Loc,
12593 Specification =>
12594 Make_Procedure_Specification (Loc,
12595 Defining_Unit_Name =>
12596 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12597 Parameter_Specifications =>
12598 New_Copy_List
12599 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12600
12601 Declarations => Empty_List,
12602 Handled_Statement_Sequence =>
12603 Make_Handled_Sequence_Of_Statements (Loc,
12604 Statements => New_List (
12605 Make_Raise_Program_Error (Loc,
12606 Reason => PE_Access_Before_Elaboration))));
12607
12608 else
12609 Ret_Expr :=
12610 Make_Raise_Program_Error (Loc,
12611 Reason => PE_Access_Before_Elaboration);
12612
12613 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12614 Set_Analyzed (Ret_Expr);
12615
12616 Act_Body :=
12617 Make_Subprogram_Body (Loc,
12618 Specification =>
12619 Make_Function_Specification (Loc,
12620 Defining_Unit_Name =>
12621 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12622 Parameter_Specifications =>
12623 New_Copy_List
12624 (Parameter_Specifications (Parent (Act_Decl_Id))),
12625 Result_Definition =>
12626 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12627
12628 Declarations => Empty_List,
12629 Handled_Statement_Sequence =>
12630 Make_Handled_Sequence_Of_Statements (Loc,
12631 Statements => New_List (
12632 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12633 end if;
12634
12635 Pack_Body :=
12636 Make_Package_Body (Loc,
12637 Defining_Unit_Name => New_Copy (Pack_Id),
12638 Declarations => New_List (Act_Body));
12639
12640 Insert_After (Inst_Node, Pack_Body);
12641 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12642 Analyze (Pack_Body);
12643 end if;
12644
12645 <<Leave>>
12646
12647 -- Restore the context that was in effect prior to instantiating the
12648 -- subprogram body.
12649
12650 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12651 Local_Suppress_Stack_Top := Saved_LSST;
12652 Scope_Suppress := Saved_SS;
12653 Style_Check := Saved_SC;
12654
12655 Expander_Mode_Restore;
12656 Restore_Config_Switches (Saved_CS);
12657 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12658 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12659 Restore_Warnings (Saved_Warn);
12660 end Instantiate_Subprogram_Body;
12661
12662 ----------------------
12663 -- Instantiate_Type --
12664 ----------------------
12665
12666 function Instantiate_Type
12667 (Formal : Node_Id;
12668 Actual : Node_Id;
12669 Analyzed_Formal : Node_Id;
12670 Actual_Decls : List_Id) return List_Id
12671 is
12672 A_Gen_T : constant Entity_Id :=
12673 Defining_Identifier (Analyzed_Formal);
12674 Def : constant Node_Id := Formal_Type_Definition (Formal);
12675 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12676 Act_T : Entity_Id;
12677 Ancestor : Entity_Id := Empty;
12678 Decl_Node : Node_Id;
12679 Decl_Nodes : List_Id;
12680 Loc : Source_Ptr;
12681 Subt : Entity_Id;
12682
12683 procedure Check_Shared_Variable_Control_Aspects;
12684 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12685 -- that may be specified for a formal type are obeyed by the actual.
12686
12687 procedure Diagnose_Predicated_Actual;
12688 -- There are a number of constructs in which a discrete type with
12689 -- predicates is illegal, e.g. as an index in an array type declaration.
12690 -- If a generic type is used is such a construct in a generic package
12691 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12692 -- of the generic contract that the actual cannot have predicates.
12693
12694 procedure Validate_Array_Type_Instance;
12695 procedure Validate_Access_Subprogram_Instance;
12696 procedure Validate_Access_Type_Instance;
12697 procedure Validate_Derived_Type_Instance;
12698 procedure Validate_Derived_Interface_Type_Instance;
12699 procedure Validate_Discriminated_Formal_Type;
12700 procedure Validate_Interface_Type_Instance;
12701 procedure Validate_Private_Type_Instance;
12702 procedure Validate_Incomplete_Type_Instance;
12703 -- These procedures perform validation tests for the named case.
12704 -- Validate_Discriminated_Formal_Type is shared by formal private
12705 -- types and Ada 2012 formal incomplete types.
12706
12707 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12708 -- Check that base types are the same and that the subtypes match
12709 -- statically. Used in several of the above.
12710
12711 --------------------------------------------
12712 -- Check_Shared_Variable_Control_Aspects --
12713 --------------------------------------------
12714
12715 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12716 -- that may be specified for the formal are obeyed by the actual.
12717 -- If the formal is a derived type the aspect specifications must match.
12718 -- NOTE: AI12-0282 implies that matching of aspects is required between
12719 -- formal and actual in all cases, but this is too restrictive.
12720 -- In particular it violates a language design rule: a limited private
12721 -- indefinite formal can be matched by any actual. The current code
12722 -- reflects an older and more permissive version of RM C.6 (12/5).
12723
12724 procedure Check_Shared_Variable_Control_Aspects is
12725 begin
12726 if Ada_Version >= Ada_2020 then
12727 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12728 Error_Msg_NE
12729 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12730
12731 elsif Is_Derived_Type (A_Gen_T)
12732 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12733 then
12734 Error_Msg_NE
12735 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12736 end if;
12737
12738 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12739 Error_Msg_NE
12740 ("actual for& must have Volatile aspect",
12741 Actual, A_Gen_T);
12742
12743 elsif Is_Derived_Type (A_Gen_T)
12744 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12745 then
12746 Error_Msg_NE
12747 ("actual for& has different Volatile aspect",
12748 Actual, A_Gen_T);
12749 end if;
12750
12751 -- We assume that an array type whose atomic component type
12752 -- is Atomic is equivalent to an array type with the explicit
12753 -- aspect Has_Atomic_Components. This is a reasonable inference
12754 -- from the intent of AI12-0282, and makes it legal to use an
12755 -- actual that does not have the identical aspect as the formal.
12756 -- Ditto for volatile components.
12757
12758 declare
12759 Actual_Atomic_Comp : constant Boolean :=
12760 Has_Atomic_Components (Act_T)
12761 or else (Is_Array_Type (Act_T)
12762 and then Is_Atomic (Component_Type (Act_T)));
12763 begin
12764 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12765 Error_Msg_NE
12766 ("formal and actual for& must agree on atomic components",
12767 Actual, A_Gen_T);
12768 end if;
12769 end;
12770
12771 declare
12772 Actual_Volatile_Comp : constant Boolean :=
12773 Has_Volatile_Components (Act_T)
12774 or else (Is_Array_Type (Act_T)
12775 and then Is_Volatile (Component_Type (Act_T)));
12776 begin
12777 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12778 then
12779 Error_Msg_NE
12780 ("actual for& must have volatile components",
12781 Actual, A_Gen_T);
12782 end if;
12783 end;
12784
12785 -- The following two aspects do not require exact matching,
12786 -- but only one-way agreement. See RM C.6.
12787
12788 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12789 then
12790 Error_Msg_NE
12791 ("actual for& must have Independent aspect specified",
12792 Actual, A_Gen_T);
12793 end if;
12794
12795 if Has_Independent_Components (A_Gen_T)
12796 and then not Has_Independent_Components (Act_T)
12797 then
12798 Error_Msg_NE
12799 ("actual for& must have Independent_Components specified",
12800 Actual, A_Gen_T);
12801 end if;
12802
12803 -- Check actual/formal compatibility with respect to the four
12804 -- volatility refinement aspects.
12805
12806 Check_Volatility_Compatibility
12807 (Act_T, A_Gen_T,
12808 "actual type", "its corresponding formal type",
12809 Srcpos_Bearer => Act_T);
12810 end if;
12811 end Check_Shared_Variable_Control_Aspects;
12812
12813 ---------------------------------
12814 -- Diagnose_Predicated_Actual --
12815 ---------------------------------
12816
12817 procedure Diagnose_Predicated_Actual is
12818 begin
12819 if No_Predicate_On_Actual (A_Gen_T)
12820 and then Has_Predicates (Act_T)
12821 then
12822 Error_Msg_NE
12823 ("actual for& cannot be a type with predicate",
12824 Instantiation_Node, A_Gen_T);
12825
12826 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12827 and then Has_Predicates (Act_T)
12828 and then not Has_Static_Predicate_Aspect (Act_T)
12829 then
12830 Error_Msg_NE
12831 ("actual for& cannot be a type with a dynamic predicate",
12832 Instantiation_Node, A_Gen_T);
12833 end if;
12834 end Diagnose_Predicated_Actual;
12835
12836 --------------------
12837 -- Subtypes_Match --
12838 --------------------
12839
12840 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12841 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12842
12843 begin
12844 -- Some detailed comments would be useful here ???
12845
12846 return ((Base_Type (T) = Act_T
12847 or else Base_Type (T) = Base_Type (Act_T))
12848 and then Subtypes_Statically_Match (T, Act_T))
12849
12850 or else (Is_Class_Wide_Type (Gen_T)
12851 and then Is_Class_Wide_Type (Act_T)
12852 and then Subtypes_Match
12853 (Get_Instance_Of (Root_Type (Gen_T)),
12854 Root_Type (Act_T)))
12855
12856 or else
12857 (Ekind (Gen_T) in E_Anonymous_Access_Subprogram_Type
12858 | E_Anonymous_Access_Type
12859 and then Ekind (Act_T) = Ekind (Gen_T)
12860 and then Subtypes_Statically_Match
12861 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12862 end Subtypes_Match;
12863
12864 -----------------------------------------
12865 -- Validate_Access_Subprogram_Instance --
12866 -----------------------------------------
12867
12868 procedure Validate_Access_Subprogram_Instance is
12869 begin
12870 if not Is_Access_Type (Act_T)
12871 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12872 then
12873 Error_Msg_NE
12874 ("expect access type in instantiation of &", Actual, Gen_T);
12875 Abandon_Instantiation (Actual);
12876 end if;
12877
12878 -- According to AI05-288, actuals for access_to_subprograms must be
12879 -- subtype conformant with the generic formal. Previous to AI05-288
12880 -- only mode conformance was required.
12881
12882 -- This is a binding interpretation that applies to previous versions
12883 -- of the language, no need to maintain previous weaker checks.
12884
12885 Check_Subtype_Conformant
12886 (Designated_Type (Act_T),
12887 Designated_Type (A_Gen_T),
12888 Actual,
12889 Get_Inst => True);
12890
12891 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12892 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12893 Error_Msg_NE
12894 ("protected access type not allowed for formal &",
12895 Actual, Gen_T);
12896 end if;
12897
12898 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12899 Error_Msg_NE
12900 ("expect protected access type for formal &",
12901 Actual, Gen_T);
12902 end if;
12903
12904 -- If the formal has a specified convention (which in most cases
12905 -- will be StdCall) verify that the actual has the same convention.
12906
12907 if Has_Convention_Pragma (A_Gen_T)
12908 and then Convention (A_Gen_T) /= Convention (Act_T)
12909 then
12910 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12911 Error_Msg_NE
12912 ("actual for formal & must have convention %", Actual, Gen_T);
12913 end if;
12914
12915 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12916 Error_Msg_NE
12917 ("non null exclusion of actual and formal & do not match",
12918 Actual, Gen_T);
12919 end if;
12920 end Validate_Access_Subprogram_Instance;
12921
12922 -----------------------------------
12923 -- Validate_Access_Type_Instance --
12924 -----------------------------------
12925
12926 procedure Validate_Access_Type_Instance is
12927 Desig_Type : constant Entity_Id :=
12928 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12929 Desig_Act : Entity_Id;
12930
12931 begin
12932 if not Is_Access_Type (Act_T) then
12933 Error_Msg_NE
12934 ("expect access type in instantiation of &", Actual, Gen_T);
12935 Abandon_Instantiation (Actual);
12936 end if;
12937
12938 if Is_Access_Constant (A_Gen_T) then
12939 if not Is_Access_Constant (Act_T) then
12940 Error_Msg_N
12941 ("actual type must be access-to-constant type", Actual);
12942 Abandon_Instantiation (Actual);
12943 end if;
12944 else
12945 if Is_Access_Constant (Act_T) then
12946 Error_Msg_N
12947 ("actual type must be access-to-variable type", Actual);
12948 Abandon_Instantiation (Actual);
12949
12950 elsif Ekind (A_Gen_T) = E_General_Access_Type
12951 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12952 then
12953 Error_Msg_N
12954 ("actual must be general access type!", Actual);
12955 Error_Msg_NE -- CODEFIX
12956 ("\add ALL to }!", Actual, Act_T);
12957 Abandon_Instantiation (Actual);
12958 end if;
12959 end if;
12960
12961 -- The designated subtypes, that is to say the subtypes introduced
12962 -- by an access type declaration (and not by a subtype declaration)
12963 -- must match.
12964
12965 Desig_Act := Designated_Type (Base_Type (Act_T));
12966
12967 -- The designated type may have been introduced through a limited_
12968 -- with clause, in which case retrieve the non-limited view. This
12969 -- applies to incomplete types as well as to class-wide types.
12970
12971 if From_Limited_With (Desig_Act) then
12972 Desig_Act := Available_View (Desig_Act);
12973 end if;
12974
12975 if not Subtypes_Match (Desig_Type, Desig_Act) then
12976 Error_Msg_NE
12977 ("designated type of actual does not match that of formal &",
12978 Actual, Gen_T);
12979
12980 if not Predicates_Match (Desig_Type, Desig_Act) then
12981 Error_Msg_N ("\predicates do not match", Actual);
12982 end if;
12983
12984 Abandon_Instantiation (Actual);
12985 end if;
12986
12987 -- Ada 2005: null-exclusion indicators of the two types must agree
12988
12989 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12990 Error_Msg_NE
12991 ("non null exclusion of actual and formal & do not match",
12992 Actual, Gen_T);
12993 end if;
12994 end Validate_Access_Type_Instance;
12995
12996 ----------------------------------
12997 -- Validate_Array_Type_Instance --
12998 ----------------------------------
12999
13000 procedure Validate_Array_Type_Instance is
13001 I1 : Node_Id;
13002 I2 : Node_Id;
13003 T2 : Entity_Id;
13004
13005 function Formal_Dimensions return Nat;
13006 -- Count number of dimensions in array type formal
13007
13008 -----------------------
13009 -- Formal_Dimensions --
13010 -----------------------
13011
13012 function Formal_Dimensions return Nat is
13013 Num : Nat := 0;
13014 Index : Node_Id;
13015
13016 begin
13017 if Nkind (Def) = N_Constrained_Array_Definition then
13018 Index := First (Discrete_Subtype_Definitions (Def));
13019 else
13020 Index := First (Subtype_Marks (Def));
13021 end if;
13022
13023 while Present (Index) loop
13024 Num := Num + 1;
13025 Next_Index (Index);
13026 end loop;
13027
13028 return Num;
13029 end Formal_Dimensions;
13030
13031 -- Start of processing for Validate_Array_Type_Instance
13032
13033 begin
13034 if not Is_Array_Type (Act_T) then
13035 Error_Msg_NE
13036 ("expect array type in instantiation of &", Actual, Gen_T);
13037 Abandon_Instantiation (Actual);
13038
13039 elsif Nkind (Def) = N_Constrained_Array_Definition then
13040 if not (Is_Constrained (Act_T)) then
13041 Error_Msg_NE
13042 ("expect constrained array in instantiation of &",
13043 Actual, Gen_T);
13044 Abandon_Instantiation (Actual);
13045 end if;
13046
13047 else
13048 if Is_Constrained (Act_T) then
13049 Error_Msg_NE
13050 ("expect unconstrained array in instantiation of &",
13051 Actual, Gen_T);
13052 Abandon_Instantiation (Actual);
13053 end if;
13054 end if;
13055
13056 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13057 Error_Msg_NE
13058 ("dimensions of actual do not match formal &", Actual, Gen_T);
13059 Abandon_Instantiation (Actual);
13060 end if;
13061
13062 I1 := First_Index (A_Gen_T);
13063 I2 := First_Index (Act_T);
13064 for J in 1 .. Formal_Dimensions loop
13065
13066 -- If the indexes of the actual were given by a subtype_mark,
13067 -- the index was transformed into a range attribute. Retrieve
13068 -- the original type mark for checking.
13069
13070 if Is_Entity_Name (Original_Node (I2)) then
13071 T2 := Entity (Original_Node (I2));
13072 else
13073 T2 := Etype (I2);
13074 end if;
13075
13076 if not Subtypes_Match
13077 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13078 then
13079 Error_Msg_NE
13080 ("index types of actual do not match those of formal &",
13081 Actual, Gen_T);
13082 Abandon_Instantiation (Actual);
13083 end if;
13084
13085 Next_Index (I1);
13086 Next_Index (I2);
13087 end loop;
13088
13089 -- Check matching subtypes. Note that there are complex visibility
13090 -- issues when the generic is a child unit and some aspect of the
13091 -- generic type is declared in a parent unit of the generic. We do
13092 -- the test to handle this special case only after a direct check
13093 -- for static matching has failed. The case where both the component
13094 -- type and the array type are separate formals, and the component
13095 -- type is a private view may also require special checking in
13096 -- Subtypes_Match. Finally, we assume that a child instance where
13097 -- the component type comes from a formal of a parent instance is
13098 -- correct because the generic was correct. A more precise check
13099 -- seems too complex to install???
13100
13101 if Subtypes_Match
13102 (Component_Type (A_Gen_T), Component_Type (Act_T))
13103 or else
13104 Subtypes_Match
13105 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13106 Component_Type (Act_T))
13107 or else
13108 (not Inside_A_Generic
13109 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13110 then
13111 null;
13112 else
13113 Error_Msg_NE
13114 ("component subtype of actual does not match that of formal &",
13115 Actual, Gen_T);
13116 Abandon_Instantiation (Actual);
13117 end if;
13118
13119 if Has_Aliased_Components (A_Gen_T)
13120 and then not Has_Aliased_Components (Act_T)
13121 then
13122 Error_Msg_NE
13123 ("actual must have aliased components to match formal type &",
13124 Actual, Gen_T);
13125 end if;
13126 end Validate_Array_Type_Instance;
13127
13128 -----------------------------------------------
13129 -- Validate_Derived_Interface_Type_Instance --
13130 -----------------------------------------------
13131
13132 procedure Validate_Derived_Interface_Type_Instance is
13133 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13134 Elmt : Elmt_Id;
13135
13136 begin
13137 -- First apply interface instance checks
13138
13139 Validate_Interface_Type_Instance;
13140
13141 -- Verify that immediate parent interface is an ancestor of
13142 -- the actual.
13143
13144 if Present (Par)
13145 and then not Interface_Present_In_Ancestor (Act_T, Par)
13146 then
13147 Error_Msg_NE
13148 ("interface actual must include progenitor&", Actual, Par);
13149 end if;
13150
13151 -- Now verify that the actual includes all other ancestors of
13152 -- the formal.
13153
13154 Elmt := First_Elmt (Interfaces (A_Gen_T));
13155 while Present (Elmt) loop
13156 if not Interface_Present_In_Ancestor
13157 (Act_T, Get_Instance_Of (Node (Elmt)))
13158 then
13159 Error_Msg_NE
13160 ("interface actual must include progenitor&",
13161 Actual, Node (Elmt));
13162 end if;
13163
13164 Next_Elmt (Elmt);
13165 end loop;
13166 end Validate_Derived_Interface_Type_Instance;
13167
13168 ------------------------------------
13169 -- Validate_Derived_Type_Instance --
13170 ------------------------------------
13171
13172 procedure Validate_Derived_Type_Instance is
13173 Actual_Discr : Entity_Id;
13174 Ancestor_Discr : Entity_Id;
13175
13176 begin
13177 -- Verify that the actual includes the progenitors of the formal,
13178 -- if any. The formal may depend on previous formals and their
13179 -- instance, so we must examine instance of interfaces if present.
13180 -- The actual may be an extension of an interface, in which case
13181 -- it does not appear in the interface list, so this must be
13182 -- checked separately.
13183
13184 if Present (Interface_List (Def)) then
13185 if not Has_Interfaces (Act_T) then
13186 Error_Msg_NE
13187 ("actual must implement all interfaces of formal&",
13188 Actual, A_Gen_T);
13189
13190 else
13191 declare
13192 Act_Iface_List : Elist_Id;
13193 Iface : Node_Id;
13194 Iface_Ent : Entity_Id;
13195
13196 function Instance_Exists (I : Entity_Id) return Boolean;
13197 -- If the interface entity is declared in a generic unit,
13198 -- this can only be legal if we are within an instantiation
13199 -- of a child of that generic. There is currently no
13200 -- mechanism to relate an interface declared within a
13201 -- generic to the corresponding interface in an instance,
13202 -- so we traverse the list of interfaces of the actual,
13203 -- looking for a name match.
13204
13205 ---------------------
13206 -- Instance_Exists --
13207 ---------------------
13208
13209 function Instance_Exists (I : Entity_Id) return Boolean is
13210 Iface_Elmt : Elmt_Id;
13211
13212 begin
13213 Iface_Elmt := First_Elmt (Act_Iface_List);
13214 while Present (Iface_Elmt) loop
13215 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13216 and then Chars (Node (Iface_Elmt)) = Chars (I)
13217 then
13218 return True;
13219 end if;
13220
13221 Next_Elmt (Iface_Elmt);
13222 end loop;
13223
13224 return False;
13225 end Instance_Exists;
13226
13227 begin
13228 Iface := First (Abstract_Interface_List (A_Gen_T));
13229 Collect_Interfaces (Act_T, Act_Iface_List);
13230
13231 while Present (Iface) loop
13232 Iface_Ent := Get_Instance_Of (Entity (Iface));
13233
13234 if Is_Ancestor (Iface_Ent, Act_T)
13235 or else Is_Progenitor (Iface_Ent, Act_T)
13236 then
13237 null;
13238
13239 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13240 and then Instance_Exists (Iface_Ent)
13241 then
13242 null;
13243
13244 else
13245 Error_Msg_Name_1 := Chars (Act_T);
13246 Error_Msg_NE
13247 ("actual% must implement interface&",
13248 Actual, Etype (Iface));
13249 end if;
13250
13251 Next (Iface);
13252 end loop;
13253 end;
13254 end if;
13255 end if;
13256
13257 -- If the parent type in the generic declaration is itself a previous
13258 -- formal type, then it is local to the generic and absent from the
13259 -- analyzed generic definition. In that case the ancestor is the
13260 -- instance of the formal (which must have been instantiated
13261 -- previously), unless the ancestor is itself a formal derived type.
13262 -- In this latter case (which is the subject of Corrigendum 8652/0038
13263 -- (AI-202) the ancestor of the formals is the ancestor of its
13264 -- parent. Otherwise, the analyzed generic carries the parent type.
13265 -- If the parent type is defined in a previous formal package, then
13266 -- the scope of that formal package is that of the generic type
13267 -- itself, and it has already been mapped into the corresponding type
13268 -- in the actual package.
13269
13270 -- Common case: parent type defined outside of the generic
13271
13272 if Is_Entity_Name (Subtype_Mark (Def))
13273 and then Present (Entity (Subtype_Mark (Def)))
13274 then
13275 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13276
13277 -- Check whether parent is defined in a previous formal package
13278
13279 elsif
13280 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13281 then
13282 Ancestor :=
13283 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13284
13285 -- The type may be a local derivation, or a type extension of a
13286 -- previous formal, or of a formal of a parent package.
13287
13288 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13289 or else
13290 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13291 then
13292 -- Check whether the parent is another derived formal type in the
13293 -- same generic unit.
13294
13295 if Etype (A_Gen_T) /= A_Gen_T
13296 and then Is_Generic_Type (Etype (A_Gen_T))
13297 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13298 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13299 then
13300 -- Locate ancestor of parent from the subtype declaration
13301 -- created for the actual.
13302
13303 declare
13304 Decl : Node_Id;
13305
13306 begin
13307 Decl := First (Actual_Decls);
13308 while Present (Decl) loop
13309 if Nkind (Decl) = N_Subtype_Declaration
13310 and then Chars (Defining_Identifier (Decl)) =
13311 Chars (Etype (A_Gen_T))
13312 then
13313 Ancestor := Generic_Parent_Type (Decl);
13314 exit;
13315 else
13316 Next (Decl);
13317 end if;
13318 end loop;
13319 end;
13320
13321 pragma Assert (Present (Ancestor));
13322
13323 -- The ancestor itself may be a previous formal that has been
13324 -- instantiated.
13325
13326 Ancestor := Get_Instance_Of (Ancestor);
13327
13328 else
13329 Ancestor :=
13330 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13331 end if;
13332
13333 -- Check whether parent is a previous formal of the current generic
13334
13335 elsif Is_Derived_Type (A_Gen_T)
13336 and then Is_Generic_Type (Etype (A_Gen_T))
13337 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13338 then
13339 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13340
13341 -- An unusual case: the actual is a type declared in a parent unit,
13342 -- but is not a formal type so there is no instance_of for it.
13343 -- Retrieve it by analyzing the record extension.
13344
13345 elsif Is_Child_Unit (Scope (A_Gen_T))
13346 and then In_Open_Scopes (Scope (Act_T))
13347 and then Is_Generic_Instance (Scope (Act_T))
13348 then
13349 Analyze (Subtype_Mark (Def));
13350 Ancestor := Entity (Subtype_Mark (Def));
13351
13352 else
13353 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13354 end if;
13355
13356 -- If the formal derived type has pragma Preelaborable_Initialization
13357 -- then the actual type must have preelaborable initialization.
13358
13359 if Known_To_Have_Preelab_Init (A_Gen_T)
13360 and then not Has_Preelaborable_Initialization (Act_T)
13361 then
13362 Error_Msg_NE
13363 ("actual for & must have preelaborable initialization",
13364 Actual, Gen_T);
13365 end if;
13366
13367 -- Ada 2005 (AI-251)
13368
13369 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13370 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13371 Error_Msg_NE
13372 ("(Ada 2005) expected type implementing & in instantiation",
13373 Actual, Ancestor);
13374 end if;
13375
13376 -- Finally verify that the (instance of) the ancestor is an ancestor
13377 -- of the actual.
13378
13379 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13380 Error_Msg_NE
13381 ("expect type derived from & in instantiation",
13382 Actual, First_Subtype (Ancestor));
13383 Abandon_Instantiation (Actual);
13384 end if;
13385
13386 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13387 -- that the formal type declaration has been rewritten as a private
13388 -- extension.
13389
13390 if Ada_Version >= Ada_2005
13391 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13392 and then Synchronized_Present (Parent (A_Gen_T))
13393 then
13394 -- The actual must be a synchronized tagged type
13395
13396 if not Is_Tagged_Type (Act_T) then
13397 Error_Msg_N
13398 ("actual of synchronized type must be tagged", Actual);
13399 Abandon_Instantiation (Actual);
13400
13401 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13402 and then Nkind (Type_Definition (Parent (Act_T))) =
13403 N_Derived_Type_Definition
13404 and then not Synchronized_Present
13405 (Type_Definition (Parent (Act_T)))
13406 then
13407 Error_Msg_N
13408 ("actual of synchronized type must be synchronized", Actual);
13409 Abandon_Instantiation (Actual);
13410 end if;
13411 end if;
13412
13413 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13414 -- removes the second instance of the phrase "or allow pass by copy".
13415
13416 -- For Ada 2020, the aspect may be specified explicitly for the
13417 -- formal regardless of whether an ancestor obeys it.
13418
13419 if Is_Atomic (Act_T)
13420 and then not Is_Atomic (Ancestor)
13421 and then not Is_Atomic (A_Gen_T)
13422 then
13423 Error_Msg_N
13424 ("cannot have atomic actual type for non-atomic formal type",
13425 Actual);
13426
13427 elsif Is_Volatile (Act_T)
13428 and then not Is_Volatile (Ancestor)
13429 and then not Is_Volatile (A_Gen_T)
13430 then
13431 Error_Msg_N
13432 ("cannot have volatile actual type for non-volatile formal type",
13433 Actual);
13434 end if;
13435
13436 -- It should not be necessary to check for unknown discriminants on
13437 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13438 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13439 -- needs fixing. ???
13440
13441 if Is_Definite_Subtype (A_Gen_T)
13442 and then not Unknown_Discriminants_Present (Formal)
13443 and then not Is_Definite_Subtype (Act_T)
13444 then
13445 Error_Msg_N ("actual subtype must be constrained", Actual);
13446 Abandon_Instantiation (Actual);
13447 end if;
13448
13449 if not Unknown_Discriminants_Present (Formal) then
13450 if Is_Constrained (Ancestor) then
13451 if not Is_Constrained (Act_T) then
13452 Error_Msg_N ("actual subtype must be constrained", Actual);
13453 Abandon_Instantiation (Actual);
13454 end if;
13455
13456 -- Ancestor is unconstrained, Check if generic formal and actual
13457 -- agree on constrainedness. The check only applies to array types
13458 -- and discriminated types.
13459
13460 elsif Is_Constrained (Act_T) then
13461 if Ekind (Ancestor) = E_Access_Type
13462 or else (not Is_Constrained (A_Gen_T)
13463 and then Is_Composite_Type (A_Gen_T))
13464 then
13465 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13466 Abandon_Instantiation (Actual);
13467 end if;
13468
13469 -- A class-wide type is only allowed if the formal has unknown
13470 -- discriminants.
13471
13472 elsif Is_Class_Wide_Type (Act_T)
13473 and then not Has_Unknown_Discriminants (Ancestor)
13474 then
13475 Error_Msg_NE
13476 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13477 Abandon_Instantiation (Actual);
13478
13479 -- Otherwise, the formal and actual must have the same number
13480 -- of discriminants and each discriminant of the actual must
13481 -- correspond to a discriminant of the formal.
13482
13483 elsif Has_Discriminants (Act_T)
13484 and then not Has_Unknown_Discriminants (Act_T)
13485 and then Has_Discriminants (Ancestor)
13486 then
13487 Actual_Discr := First_Discriminant (Act_T);
13488 Ancestor_Discr := First_Discriminant (Ancestor);
13489 while Present (Actual_Discr)
13490 and then Present (Ancestor_Discr)
13491 loop
13492 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13493 No (Corresponding_Discriminant (Actual_Discr))
13494 then
13495 Error_Msg_NE
13496 ("discriminant & does not correspond "
13497 & "to ancestor discriminant", Actual, Actual_Discr);
13498 Abandon_Instantiation (Actual);
13499 end if;
13500
13501 Next_Discriminant (Actual_Discr);
13502 Next_Discriminant (Ancestor_Discr);
13503 end loop;
13504
13505 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13506 Error_Msg_NE
13507 ("actual for & must have same number of discriminants",
13508 Actual, Gen_T);
13509 Abandon_Instantiation (Actual);
13510 end if;
13511
13512 -- This case should be caught by the earlier check for
13513 -- constrainedness, but the check here is added for completeness.
13514
13515 elsif Has_Discriminants (Act_T)
13516 and then not Has_Unknown_Discriminants (Act_T)
13517 then
13518 Error_Msg_NE
13519 ("actual for & must not have discriminants", Actual, Gen_T);
13520 Abandon_Instantiation (Actual);
13521
13522 elsif Has_Discriminants (Ancestor) then
13523 Error_Msg_NE
13524 ("actual for & must have known discriminants", Actual, Gen_T);
13525 Abandon_Instantiation (Actual);
13526 end if;
13527
13528 if not Subtypes_Statically_Compatible
13529 (Act_T, Ancestor, Formal_Derived_Matching => True)
13530 then
13531 Error_Msg_NE
13532 ("actual for & must be statically compatible with ancestor",
13533 Actual, Gen_T);
13534
13535 if not Predicates_Compatible (Act_T, Ancestor) then
13536 Error_Msg_N
13537 ("\predicate on actual is not compatible with ancestor",
13538 Actual);
13539 end if;
13540
13541 Abandon_Instantiation (Actual);
13542 end if;
13543 end if;
13544
13545 -- If the formal and actual types are abstract, check that there
13546 -- are no abstract primitives of the actual type that correspond to
13547 -- nonabstract primitives of the formal type (second sentence of
13548 -- RM95 3.9.3(9)).
13549
13550 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13551 Check_Abstract_Primitives : declare
13552 Gen_Prims : constant Elist_Id :=
13553 Primitive_Operations (A_Gen_T);
13554 Gen_Elmt : Elmt_Id;
13555 Gen_Subp : Entity_Id;
13556 Anc_Subp : Entity_Id;
13557 Anc_Formal : Entity_Id;
13558 Anc_F_Type : Entity_Id;
13559
13560 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13561 Act_Elmt : Elmt_Id;
13562 Act_Subp : Entity_Id;
13563 Act_Formal : Entity_Id;
13564 Act_F_Type : Entity_Id;
13565
13566 Subprograms_Correspond : Boolean;
13567
13568 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13569 -- Returns true if T2 is derived directly or indirectly from
13570 -- T1, including derivations from interfaces. T1 and T2 are
13571 -- required to be specific tagged base types.
13572
13573 ------------------------
13574 -- Is_Tagged_Ancestor --
13575 ------------------------
13576
13577 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13578 is
13579 Intfc_Elmt : Elmt_Id;
13580
13581 begin
13582 -- The predicate is satisfied if the types are the same
13583
13584 if T1 = T2 then
13585 return True;
13586
13587 -- If we've reached the top of the derivation chain then
13588 -- we know that T1 is not an ancestor of T2.
13589
13590 elsif Etype (T2) = T2 then
13591 return False;
13592
13593 -- Proceed to check T2's immediate parent
13594
13595 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13596 return True;
13597
13598 -- Finally, check to see if T1 is an ancestor of any of T2's
13599 -- progenitors.
13600
13601 else
13602 Intfc_Elmt := First_Elmt (Interfaces (T2));
13603 while Present (Intfc_Elmt) loop
13604 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13605 return True;
13606 end if;
13607
13608 Next_Elmt (Intfc_Elmt);
13609 end loop;
13610 end if;
13611
13612 return False;
13613 end Is_Tagged_Ancestor;
13614
13615 -- Start of processing for Check_Abstract_Primitives
13616
13617 begin
13618 -- Loop over all of the formal derived type's primitives
13619
13620 Gen_Elmt := First_Elmt (Gen_Prims);
13621 while Present (Gen_Elmt) loop
13622 Gen_Subp := Node (Gen_Elmt);
13623
13624 -- If the primitive of the formal is not abstract, then
13625 -- determine whether there is a corresponding primitive of
13626 -- the actual type that's abstract.
13627
13628 if not Is_Abstract_Subprogram (Gen_Subp) then
13629 Act_Elmt := First_Elmt (Act_Prims);
13630 while Present (Act_Elmt) loop
13631 Act_Subp := Node (Act_Elmt);
13632
13633 -- If we find an abstract primitive of the actual,
13634 -- then we need to test whether it corresponds to the
13635 -- subprogram from which the generic formal primitive
13636 -- is inherited.
13637
13638 if Is_Abstract_Subprogram (Act_Subp) then
13639 Anc_Subp := Alias (Gen_Subp);
13640
13641 -- Test whether we have a corresponding primitive
13642 -- by comparing names, kinds, formal types, and
13643 -- result types.
13644
13645 if Chars (Anc_Subp) = Chars (Act_Subp)
13646 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13647 then
13648 Anc_Formal := First_Formal (Anc_Subp);
13649 Act_Formal := First_Formal (Act_Subp);
13650 while Present (Anc_Formal)
13651 and then Present (Act_Formal)
13652 loop
13653 Anc_F_Type := Etype (Anc_Formal);
13654 Act_F_Type := Etype (Act_Formal);
13655
13656 if Ekind (Anc_F_Type) =
13657 E_Anonymous_Access_Type
13658 then
13659 Anc_F_Type := Designated_Type (Anc_F_Type);
13660
13661 if Ekind (Act_F_Type) =
13662 E_Anonymous_Access_Type
13663 then
13664 Act_F_Type :=
13665 Designated_Type (Act_F_Type);
13666 else
13667 exit;
13668 end if;
13669
13670 elsif
13671 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13672 then
13673 exit;
13674 end if;
13675
13676 Anc_F_Type := Base_Type (Anc_F_Type);
13677 Act_F_Type := Base_Type (Act_F_Type);
13678
13679 -- If the formal is controlling, then the
13680 -- the type of the actual primitive's formal
13681 -- must be derived directly or indirectly
13682 -- from the type of the ancestor primitive's
13683 -- formal.
13684
13685 if Is_Controlling_Formal (Anc_Formal) then
13686 if not Is_Tagged_Ancestor
13687 (Anc_F_Type, Act_F_Type)
13688 then
13689 exit;
13690 end if;
13691
13692 -- Otherwise the types of the formals must
13693 -- be the same.
13694
13695 elsif Anc_F_Type /= Act_F_Type then
13696 exit;
13697 end if;
13698
13699 Next_Formal (Anc_Formal);
13700 Next_Formal (Act_Formal);
13701 end loop;
13702
13703 -- If we traversed through all of the formals
13704 -- then so far the subprograms correspond, so
13705 -- now check that any result types correspond.
13706
13707 if No (Anc_Formal) and then No (Act_Formal) then
13708 Subprograms_Correspond := True;
13709
13710 if Ekind (Act_Subp) = E_Function then
13711 Anc_F_Type := Etype (Anc_Subp);
13712 Act_F_Type := Etype (Act_Subp);
13713
13714 if Ekind (Anc_F_Type) =
13715 E_Anonymous_Access_Type
13716 then
13717 Anc_F_Type :=
13718 Designated_Type (Anc_F_Type);
13719
13720 if Ekind (Act_F_Type) =
13721 E_Anonymous_Access_Type
13722 then
13723 Act_F_Type :=
13724 Designated_Type (Act_F_Type);
13725 else
13726 Subprograms_Correspond := False;
13727 end if;
13728
13729 elsif
13730 Ekind (Act_F_Type)
13731 = E_Anonymous_Access_Type
13732 then
13733 Subprograms_Correspond := False;
13734 end if;
13735
13736 Anc_F_Type := Base_Type (Anc_F_Type);
13737 Act_F_Type := Base_Type (Act_F_Type);
13738
13739 -- Now either the result types must be
13740 -- the same or, if the result type is
13741 -- controlling, the result type of the
13742 -- actual primitive must descend from the
13743 -- result type of the ancestor primitive.
13744
13745 if Subprograms_Correspond
13746 and then Anc_F_Type /= Act_F_Type
13747 and then
13748 Has_Controlling_Result (Anc_Subp)
13749 and then not Is_Tagged_Ancestor
13750 (Anc_F_Type, Act_F_Type)
13751 then
13752 Subprograms_Correspond := False;
13753 end if;
13754 end if;
13755
13756 -- Found a matching subprogram belonging to
13757 -- formal ancestor type, so actual subprogram
13758 -- corresponds and this violates 3.9.3(9).
13759
13760 if Subprograms_Correspond then
13761 Error_Msg_NE
13762 ("abstract subprogram & overrides "
13763 & "nonabstract subprogram of ancestor",
13764 Actual, Act_Subp);
13765 end if;
13766 end if;
13767 end if;
13768 end if;
13769
13770 Next_Elmt (Act_Elmt);
13771 end loop;
13772 end if;
13773
13774 Next_Elmt (Gen_Elmt);
13775 end loop;
13776 end Check_Abstract_Primitives;
13777 end if;
13778
13779 -- Verify that limitedness matches. If parent is a limited
13780 -- interface then the generic formal is not unless declared
13781 -- explicitly so. If not declared limited, the actual cannot be
13782 -- limited (see AI05-0087).
13783
13784 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13785 if not In_Instance then
13786 Error_Msg_NE
13787 ("actual for non-limited & cannot be a limited type",
13788 Actual, Gen_T);
13789 Explain_Limited_Type (Act_T, Actual);
13790 Abandon_Instantiation (Actual);
13791 end if;
13792 end if;
13793
13794 -- Check for AI12-0036
13795
13796 declare
13797 Formal_Is_Private_Extension : constant Boolean :=
13798 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13799
13800 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13801
13802 begin
13803 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13804 if not In_Instance then
13805 if Actual_Is_Tagged then
13806 Error_Msg_NE
13807 ("actual for & cannot be a tagged type", Actual, Gen_T);
13808 else
13809 Error_Msg_NE
13810 ("actual for & must be a tagged type", Actual, Gen_T);
13811 end if;
13812
13813 Abandon_Instantiation (Actual);
13814 end if;
13815 end if;
13816 end;
13817 end Validate_Derived_Type_Instance;
13818
13819 ----------------------------------------
13820 -- Validate_Discriminated_Formal_Type --
13821 ----------------------------------------
13822
13823 procedure Validate_Discriminated_Formal_Type is
13824 Formal_Discr : Entity_Id;
13825 Actual_Discr : Entity_Id;
13826 Formal_Subt : Entity_Id;
13827
13828 begin
13829 if Has_Discriminants (A_Gen_T) then
13830 if not Has_Discriminants (Act_T) then
13831 Error_Msg_NE
13832 ("actual for & must have discriminants", Actual, Gen_T);
13833 Abandon_Instantiation (Actual);
13834
13835 elsif Is_Constrained (Act_T) then
13836 Error_Msg_NE
13837 ("actual for & must be unconstrained", Actual, Gen_T);
13838 Abandon_Instantiation (Actual);
13839
13840 else
13841 Formal_Discr := First_Discriminant (A_Gen_T);
13842 Actual_Discr := First_Discriminant (Act_T);
13843 while Formal_Discr /= Empty loop
13844 if Actual_Discr = Empty then
13845 Error_Msg_N
13846 ("discriminants on actual do not match formal",
13847 Actual);
13848 Abandon_Instantiation (Actual);
13849 end if;
13850
13851 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13852
13853 -- Access discriminants match if designated types do
13854
13855 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13856 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13857 E_Anonymous_Access_Type
13858 and then
13859 Get_Instance_Of
13860 (Designated_Type (Base_Type (Formal_Subt))) =
13861 Designated_Type (Base_Type (Etype (Actual_Discr)))
13862 then
13863 null;
13864
13865 elsif Base_Type (Formal_Subt) /=
13866 Base_Type (Etype (Actual_Discr))
13867 then
13868 Error_Msg_N
13869 ("types of actual discriminants must match formal",
13870 Actual);
13871 Abandon_Instantiation (Actual);
13872
13873 elsif not Subtypes_Statically_Match
13874 (Formal_Subt, Etype (Actual_Discr))
13875 and then Ada_Version >= Ada_95
13876 then
13877 Error_Msg_N
13878 ("subtypes of actual discriminants must match formal",
13879 Actual);
13880 Abandon_Instantiation (Actual);
13881 end if;
13882
13883 Next_Discriminant (Formal_Discr);
13884 Next_Discriminant (Actual_Discr);
13885 end loop;
13886
13887 if Actual_Discr /= Empty then
13888 Error_Msg_NE
13889 ("discriminants on actual do not match formal",
13890 Actual, Gen_T);
13891 Abandon_Instantiation (Actual);
13892 end if;
13893 end if;
13894 end if;
13895 end Validate_Discriminated_Formal_Type;
13896
13897 ---------------------------------------
13898 -- Validate_Incomplete_Type_Instance --
13899 ---------------------------------------
13900
13901 procedure Validate_Incomplete_Type_Instance is
13902 begin
13903 if not Is_Tagged_Type (Act_T)
13904 and then Is_Tagged_Type (A_Gen_T)
13905 then
13906 Error_Msg_NE
13907 ("actual for & must be a tagged type", Actual, Gen_T);
13908 end if;
13909
13910 Validate_Discriminated_Formal_Type;
13911 end Validate_Incomplete_Type_Instance;
13912
13913 --------------------------------------
13914 -- Validate_Interface_Type_Instance --
13915 --------------------------------------
13916
13917 procedure Validate_Interface_Type_Instance is
13918 begin
13919 if not Is_Interface (Act_T) then
13920 Error_Msg_NE
13921 ("actual for formal interface type must be an interface",
13922 Actual, Gen_T);
13923
13924 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13925 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13926 or else Is_Protected_Interface (A_Gen_T) /=
13927 Is_Protected_Interface (Act_T)
13928 or else Is_Synchronized_Interface (A_Gen_T) /=
13929 Is_Synchronized_Interface (Act_T)
13930 then
13931 Error_Msg_NE
13932 ("actual for interface& does not match (RM 12.5.5(4))",
13933 Actual, Gen_T);
13934 end if;
13935 end Validate_Interface_Type_Instance;
13936
13937 ------------------------------------
13938 -- Validate_Private_Type_Instance --
13939 ------------------------------------
13940
13941 procedure Validate_Private_Type_Instance is
13942 begin
13943 if Is_Limited_Type (Act_T)
13944 and then not Is_Limited_Type (A_Gen_T)
13945 then
13946 if In_Instance then
13947 null;
13948 else
13949 Error_Msg_NE
13950 ("actual for non-limited & cannot be a limited type", Actual,
13951 Gen_T);
13952 Explain_Limited_Type (Act_T, Actual);
13953 Abandon_Instantiation (Actual);
13954 end if;
13955
13956 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13957 and then not Has_Preelaborable_Initialization (Act_T)
13958 then
13959 Error_Msg_NE
13960 ("actual for & must have preelaborable initialization", Actual,
13961 Gen_T);
13962
13963 elsif not Is_Definite_Subtype (Act_T)
13964 and then Is_Definite_Subtype (A_Gen_T)
13965 and then Ada_Version >= Ada_95
13966 then
13967 Error_Msg_NE
13968 ("actual for & must be a definite subtype", Actual, Gen_T);
13969
13970 elsif not Is_Tagged_Type (Act_T)
13971 and then Is_Tagged_Type (A_Gen_T)
13972 then
13973 Error_Msg_NE
13974 ("actual for & must be a tagged type", Actual, Gen_T);
13975 end if;
13976
13977 Validate_Discriminated_Formal_Type;
13978 Ancestor := Gen_T;
13979 end Validate_Private_Type_Instance;
13980
13981 -- Start of processing for Instantiate_Type
13982
13983 begin
13984 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13985 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13986 return New_List (Error);
13987
13988 elsif not Is_Entity_Name (Actual)
13989 or else not Is_Type (Entity (Actual))
13990 then
13991 Error_Msg_NE
13992 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13993 Abandon_Instantiation (Actual);
13994
13995 else
13996 Act_T := Entity (Actual);
13997
13998 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13999 -- as a generic actual parameter if the corresponding formal type
14000 -- does not have a known_discriminant_part, or is a formal derived
14001 -- type that is an Unchecked_Union type.
14002
14003 if Is_Unchecked_Union (Base_Type (Act_T)) then
14004 if not Has_Discriminants (A_Gen_T)
14005 or else (Is_Derived_Type (A_Gen_T)
14006 and then Is_Unchecked_Union (A_Gen_T))
14007 then
14008 null;
14009 else
14010 Error_Msg_N ("unchecked union cannot be the actual for a "
14011 & "discriminated formal type", Act_T);
14012
14013 end if;
14014 end if;
14015
14016 -- Deal with fixed/floating restrictions
14017
14018 if Is_Floating_Point_Type (Act_T) then
14019 Check_Restriction (No_Floating_Point, Actual);
14020 elsif Is_Fixed_Point_Type (Act_T) then
14021 Check_Restriction (No_Fixed_Point, Actual);
14022 end if;
14023
14024 -- Deal with error of using incomplete type as generic actual.
14025 -- This includes limited views of a type, even if the non-limited
14026 -- view may be available.
14027
14028 if Ekind (Act_T) = E_Incomplete_Type
14029 or else (Is_Class_Wide_Type (Act_T)
14030 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14031 then
14032 -- If the formal is an incomplete type, the actual can be
14033 -- incomplete as well.
14034
14035 if Ekind (A_Gen_T) = E_Incomplete_Type then
14036 null;
14037
14038 elsif Is_Class_Wide_Type (Act_T)
14039 or else No (Full_View (Act_T))
14040 then
14041 Error_Msg_N ("premature use of incomplete type", Actual);
14042 Abandon_Instantiation (Actual);
14043 else
14044 Act_T := Full_View (Act_T);
14045 Set_Entity (Actual, Act_T);
14046
14047 if Has_Private_Component (Act_T) then
14048 Error_Msg_N
14049 ("premature use of type with private component", Actual);
14050 end if;
14051 end if;
14052
14053 -- Deal with error of premature use of private type as generic actual
14054
14055 elsif Is_Private_Type (Act_T)
14056 and then Is_Private_Type (Base_Type (Act_T))
14057 and then not Is_Generic_Type (Act_T)
14058 and then not Is_Derived_Type (Act_T)
14059 and then No (Full_View (Root_Type (Act_T)))
14060 then
14061 -- If the formal is an incomplete type, the actual can be
14062 -- private or incomplete as well.
14063
14064 if Ekind (A_Gen_T) = E_Incomplete_Type then
14065 null;
14066 else
14067 Error_Msg_N ("premature use of private type", Actual);
14068 end if;
14069
14070 elsif Has_Private_Component (Act_T) then
14071 Error_Msg_N
14072 ("premature use of type with private component", Actual);
14073 end if;
14074
14075 Set_Instance_Of (A_Gen_T, Act_T);
14076
14077 -- If the type is generic, the class-wide type may also be used
14078
14079 if Is_Tagged_Type (A_Gen_T)
14080 and then Is_Tagged_Type (Act_T)
14081 and then not Is_Class_Wide_Type (A_Gen_T)
14082 then
14083 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14084 Class_Wide_Type (Act_T));
14085 end if;
14086
14087 if not Is_Abstract_Type (A_Gen_T)
14088 and then Is_Abstract_Type (Act_T)
14089 then
14090 Error_Msg_N
14091 ("actual of non-abstract formal cannot be abstract", Actual);
14092 end if;
14093
14094 -- A generic scalar type is a first subtype for which we generate
14095 -- an anonymous base type. Indicate that the instance of this base
14096 -- is the base type of the actual.
14097
14098 if Is_Scalar_Type (A_Gen_T) then
14099 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14100 end if;
14101 end if;
14102
14103 Check_Shared_Variable_Control_Aspects;
14104
14105 if Error_Posted (Act_T) then
14106 null;
14107 else
14108 case Nkind (Def) is
14109 when N_Formal_Private_Type_Definition =>
14110 Validate_Private_Type_Instance;
14111
14112 when N_Formal_Incomplete_Type_Definition =>
14113 Validate_Incomplete_Type_Instance;
14114
14115 when N_Formal_Derived_Type_Definition =>
14116 Validate_Derived_Type_Instance;
14117
14118 when N_Formal_Discrete_Type_Definition =>
14119 if not Is_Discrete_Type (Act_T) then
14120 Error_Msg_NE
14121 ("expect discrete type in instantiation of&",
14122 Actual, Gen_T);
14123 Abandon_Instantiation (Actual);
14124 end if;
14125
14126 Diagnose_Predicated_Actual;
14127
14128 when N_Formal_Signed_Integer_Type_Definition =>
14129 if not Is_Signed_Integer_Type (Act_T) then
14130 Error_Msg_NE
14131 ("expect signed integer type in instantiation of&",
14132 Actual, Gen_T);
14133 Abandon_Instantiation (Actual);
14134 end if;
14135
14136 Diagnose_Predicated_Actual;
14137
14138 when N_Formal_Modular_Type_Definition =>
14139 if not Is_Modular_Integer_Type (Act_T) then
14140 Error_Msg_NE
14141 ("expect modular type in instantiation of &",
14142 Actual, Gen_T);
14143 Abandon_Instantiation (Actual);
14144 end if;
14145
14146 Diagnose_Predicated_Actual;
14147
14148 when N_Formal_Floating_Point_Definition =>
14149 if not Is_Floating_Point_Type (Act_T) then
14150 Error_Msg_NE
14151 ("expect float type in instantiation of &", Actual, Gen_T);
14152 Abandon_Instantiation (Actual);
14153 end if;
14154
14155 when N_Formal_Ordinary_Fixed_Point_Definition =>
14156 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14157 Error_Msg_NE
14158 ("expect ordinary fixed point type in instantiation of &",
14159 Actual, Gen_T);
14160 Abandon_Instantiation (Actual);
14161 end if;
14162
14163 when N_Formal_Decimal_Fixed_Point_Definition =>
14164 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14165 Error_Msg_NE
14166 ("expect decimal type in instantiation of &",
14167 Actual, Gen_T);
14168 Abandon_Instantiation (Actual);
14169 end if;
14170
14171 when N_Array_Type_Definition =>
14172 Validate_Array_Type_Instance;
14173
14174 when N_Access_To_Object_Definition =>
14175 Validate_Access_Type_Instance;
14176
14177 when N_Access_Function_Definition
14178 | N_Access_Procedure_Definition
14179 =>
14180 Validate_Access_Subprogram_Instance;
14181
14182 when N_Record_Definition =>
14183 Validate_Interface_Type_Instance;
14184
14185 when N_Derived_Type_Definition =>
14186 Validate_Derived_Interface_Type_Instance;
14187
14188 when others =>
14189 raise Program_Error;
14190 end case;
14191 end if;
14192
14193 Subt := New_Copy (Gen_T);
14194
14195 -- Use adjusted sloc of subtype name as the location for other nodes in
14196 -- the subtype declaration.
14197
14198 Loc := Sloc (Subt);
14199
14200 Decl_Node :=
14201 Make_Subtype_Declaration (Loc,
14202 Defining_Identifier => Subt,
14203 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14204
14205 -- Record whether the actual is private at this point, so that
14206 -- Check_Generic_Actuals can restore its proper view before the
14207 -- semantic analysis of the instance.
14208
14209 if Is_Private_Type (Act_T) then
14210 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14211 end if;
14212
14213 -- In Ada 2012 the actual may be a limited view. Indicate that
14214 -- the local subtype must be treated as such.
14215
14216 if From_Limited_With (Act_T) then
14217 Mutate_Ekind (Subt, E_Incomplete_Subtype);
14218 Set_From_Limited_With (Subt);
14219 end if;
14220
14221 Decl_Nodes := New_List (Decl_Node);
14222
14223 -- Flag actual derived types so their elaboration produces the
14224 -- appropriate renamings for the primitive operations of the ancestor.
14225 -- Flag actual for formal private types as well, to determine whether
14226 -- operations in the private part may override inherited operations.
14227 -- If the formal has an interface list, the ancestor is not the
14228 -- parent, but the analyzed formal that includes the interface
14229 -- operations of all its progenitors.
14230
14231 -- Same treatment for formal private types, so we can check whether the
14232 -- type is tagged limited when validating derivations in the private
14233 -- part. (See AI05-096).
14234
14235 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14236 if Present (Interface_List (Def)) then
14237 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14238 else
14239 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14240 end if;
14241
14242 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14243 | N_Formal_Incomplete_Type_Definition
14244 then
14245 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14246 end if;
14247
14248 -- If the actual is a synchronized type that implements an interface,
14249 -- the primitive operations are attached to the corresponding record,
14250 -- and we have to treat it as an additional generic actual, so that its
14251 -- primitive operations become visible in the instance. The task or
14252 -- protected type itself does not carry primitive operations.
14253
14254 if Is_Concurrent_Type (Act_T)
14255 and then Is_Tagged_Type (Act_T)
14256 and then Present (Corresponding_Record_Type (Act_T))
14257 and then Present (Ancestor)
14258 and then Is_Interface (Ancestor)
14259 then
14260 declare
14261 Corr_Rec : constant Entity_Id :=
14262 Corresponding_Record_Type (Act_T);
14263 New_Corr : Entity_Id;
14264 Corr_Decl : Node_Id;
14265
14266 begin
14267 New_Corr := Make_Temporary (Loc, 'S');
14268 Corr_Decl :=
14269 Make_Subtype_Declaration (Loc,
14270 Defining_Identifier => New_Corr,
14271 Subtype_Indication =>
14272 New_Occurrence_Of (Corr_Rec, Loc));
14273 Append_To (Decl_Nodes, Corr_Decl);
14274
14275 if Ekind (Act_T) = E_Task_Type then
14276 Mutate_Ekind (Subt, E_Task_Subtype);
14277 else
14278 Mutate_Ekind (Subt, E_Protected_Subtype);
14279 end if;
14280
14281 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14282 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14283 Set_Generic_Parent_Type (Decl_Node, Empty);
14284 end;
14285 end if;
14286
14287 -- For a floating-point type, capture dimension info if any, because
14288 -- the generated subtype declaration does not come from source and
14289 -- will not process dimensions.
14290
14291 if Is_Floating_Point_Type (Act_T) then
14292 Copy_Dimensions (Act_T, Subt);
14293 end if;
14294
14295 return Decl_Nodes;
14296 end Instantiate_Type;
14297
14298 ---------------------
14299 -- Is_In_Main_Unit --
14300 ---------------------
14301
14302 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14303 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14304 Current_Unit : Node_Id;
14305
14306 begin
14307 if Unum = Main_Unit then
14308 return True;
14309
14310 -- If the current unit is a subunit then it is either the main unit or
14311 -- is being compiled as part of the main unit.
14312
14313 elsif Nkind (N) = N_Compilation_Unit then
14314 return Nkind (Unit (N)) = N_Subunit;
14315 end if;
14316
14317 Current_Unit := Parent (N);
14318 while Present (Current_Unit)
14319 and then Nkind (Current_Unit) /= N_Compilation_Unit
14320 loop
14321 Current_Unit := Parent (Current_Unit);
14322 end loop;
14323
14324 -- The instantiation node is in the main unit, or else the current node
14325 -- (perhaps as the result of nested instantiations) is in the main unit,
14326 -- or in the declaration of the main unit, which in this last case must
14327 -- be a body.
14328
14329 return
14330 Current_Unit = Cunit (Main_Unit)
14331 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14332 or else (Present (Current_Unit)
14333 and then Present (Library_Unit (Current_Unit))
14334 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14335 end Is_In_Main_Unit;
14336
14337 ----------------------------
14338 -- Load_Parent_Of_Generic --
14339 ----------------------------
14340
14341 procedure Load_Parent_Of_Generic
14342 (N : Node_Id;
14343 Spec : Node_Id;
14344 Body_Optional : Boolean := False)
14345 is
14346 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14347 Saved_Style_Check : constant Boolean := Style_Check;
14348 Saved_Warnings : constant Warning_Record := Save_Warnings;
14349 True_Parent : Node_Id;
14350 Inst_Node : Node_Id;
14351 OK : Boolean;
14352 Previous_Instances : constant Elist_Id := New_Elmt_List;
14353
14354 procedure Collect_Previous_Instances (Decls : List_Id);
14355 -- Collect all instantiations in the given list of declarations, that
14356 -- precede the generic that we need to load. If the bodies of these
14357 -- instantiations are available, we must analyze them, to ensure that
14358 -- the public symbols generated are the same when the unit is compiled
14359 -- to generate code, and when it is compiled in the context of a unit
14360 -- that needs a particular nested instance. This process is applied to
14361 -- both package and subprogram instances.
14362
14363 --------------------------------
14364 -- Collect_Previous_Instances --
14365 --------------------------------
14366
14367 procedure Collect_Previous_Instances (Decls : List_Id) is
14368 Decl : Node_Id;
14369
14370 begin
14371 Decl := First (Decls);
14372 while Present (Decl) loop
14373 if Sloc (Decl) >= Sloc (Inst_Node) then
14374 return;
14375
14376 -- If Decl is an instantiation, then record it as requiring
14377 -- instantiation of the corresponding body, except if it is an
14378 -- abbreviated instantiation generated internally for conformance
14379 -- checking purposes only for the case of a formal package
14380 -- declared without a box (see Instantiate_Formal_Package). Such
14381 -- an instantiation does not generate any code (the actual code
14382 -- comes from actual) and thus does not need to be analyzed here.
14383 -- If the instantiation appears with a generic package body it is
14384 -- not analyzed here either.
14385
14386 elsif Nkind (Decl) = N_Package_Instantiation
14387 and then not Is_Internal (Defining_Entity (Decl))
14388 then
14389 Append_Elmt (Decl, Previous_Instances);
14390
14391 -- For a subprogram instantiation, omit instantiations intrinsic
14392 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14393
14394 elsif Nkind (Decl) in N_Function_Instantiation
14395 | N_Procedure_Instantiation
14396 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14397 then
14398 Append_Elmt (Decl, Previous_Instances);
14399
14400 elsif Nkind (Decl) = N_Package_Declaration then
14401 Collect_Previous_Instances
14402 (Visible_Declarations (Specification (Decl)));
14403 Collect_Previous_Instances
14404 (Private_Declarations (Specification (Decl)));
14405
14406 -- Previous non-generic bodies may contain instances as well
14407
14408 elsif Nkind (Decl) = N_Package_Body
14409 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14410 then
14411 Collect_Previous_Instances (Declarations (Decl));
14412
14413 elsif Nkind (Decl) = N_Subprogram_Body
14414 and then not Acts_As_Spec (Decl)
14415 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14416 then
14417 Collect_Previous_Instances (Declarations (Decl));
14418 end if;
14419
14420 Next (Decl);
14421 end loop;
14422 end Collect_Previous_Instances;
14423
14424 -- Start of processing for Load_Parent_Of_Generic
14425
14426 begin
14427 if not In_Same_Source_Unit (N, Spec)
14428 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14429 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14430 and then not Is_In_Main_Unit (Spec))
14431 then
14432 -- Find body of parent of spec, and analyze it. A special case arises
14433 -- when the parent is an instantiation, that is to say when we are
14434 -- currently instantiating a nested generic. In that case, there is
14435 -- no separate file for the body of the enclosing instance. Instead,
14436 -- the enclosing body must be instantiated as if it were a pending
14437 -- instantiation, in order to produce the body for the nested generic
14438 -- we require now. Note that in that case the generic may be defined
14439 -- in a package body, the instance defined in the same package body,
14440 -- and the original enclosing body may not be in the main unit.
14441
14442 Inst_Node := Empty;
14443
14444 True_Parent := Parent (Spec);
14445 while Present (True_Parent)
14446 and then Nkind (True_Parent) /= N_Compilation_Unit
14447 loop
14448 if Nkind (True_Parent) = N_Package_Declaration
14449 and then
14450 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14451 then
14452 -- Parent is a compilation unit that is an instantiation, and
14453 -- instantiation node has been replaced with package decl.
14454
14455 Inst_Node := Original_Node (True_Parent);
14456 exit;
14457
14458 elsif Nkind (True_Parent) = N_Package_Declaration
14459 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14460 and then
14461 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14462 then
14463 -- Parent is a compilation unit that is an instantiation, but
14464 -- instantiation node has not been replaced with package decl.
14465
14466 Inst_Node := Unit (Parent (True_Parent));
14467 exit;
14468
14469 elsif Nkind (True_Parent) = N_Package_Declaration
14470 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14471 and then Present (Generic_Parent (Specification (True_Parent)))
14472 then
14473 -- Parent is an instantiation within another specification.
14474 -- Declaration for instance has been inserted before original
14475 -- instantiation node. A direct link would be preferable?
14476
14477 Inst_Node := Next (True_Parent);
14478 while Present (Inst_Node)
14479 and then Nkind (Inst_Node) /= N_Package_Instantiation
14480 loop
14481 Next (Inst_Node);
14482 end loop;
14483
14484 -- If the instance appears within a generic, and the generic
14485 -- unit is defined within a formal package of the enclosing
14486 -- generic, there is no generic body available, and none
14487 -- needed. A more precise test should be used ???
14488
14489 if No (Inst_Node) then
14490 return;
14491 end if;
14492
14493 exit;
14494
14495 -- If an ancestor of the generic comes from a formal package
14496 -- there is no source for the ancestor body. This is detected
14497 -- by examining the scope of the ancestor and its declaration.
14498 -- The body, if any is needed, will be available when the
14499 -- current unit (containing a formal package) is instantiated.
14500
14501 elsif Nkind (True_Parent) = N_Package_Specification
14502 and then Present (Generic_Parent (True_Parent))
14503 and then Nkind
14504 (Original_Node (Unit_Declaration_Node
14505 (Scope (Generic_Parent (True_Parent)))))
14506 = N_Formal_Package_Declaration
14507 then
14508 return;
14509
14510 else
14511 True_Parent := Parent (True_Parent);
14512 end if;
14513 end loop;
14514
14515 -- Case where we are currently instantiating a nested generic
14516
14517 if Present (Inst_Node) then
14518 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14519
14520 -- Instantiation node and declaration of instantiated package
14521 -- were exchanged when only the declaration was needed.
14522 -- Restore instantiation node before proceeding with body.
14523
14524 Set_Unit (Parent (True_Parent), Inst_Node);
14525 end if;
14526
14527 -- Now complete instantiation of enclosing body, if it appears in
14528 -- some other unit. If it appears in the current unit, the body
14529 -- will have been instantiated already.
14530
14531 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14532
14533 -- We need to determine the expander mode to instantiate the
14534 -- enclosing body. Because the generic body we need may use
14535 -- global entities declared in the enclosing package (including
14536 -- aggregates) it is in general necessary to compile this body
14537 -- with expansion enabled, except if we are within a generic
14538 -- package, in which case the usual generic rule applies.
14539
14540 declare
14541 Exp_Status : Boolean := True;
14542 Scop : Entity_Id;
14543
14544 begin
14545 -- Loop through scopes looking for generic package
14546
14547 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14548 while Present (Scop)
14549 and then Scop /= Standard_Standard
14550 loop
14551 if Ekind (Scop) = E_Generic_Package then
14552 Exp_Status := False;
14553 exit;
14554 end if;
14555
14556 Scop := Scope (Scop);
14557 end loop;
14558
14559 -- Collect previous instantiations in the unit that contains
14560 -- the desired generic.
14561
14562 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14563 and then not Body_Optional
14564 then
14565 declare
14566 Decl : Elmt_Id;
14567 Info : Pending_Body_Info;
14568 Par : Node_Id;
14569
14570 begin
14571 Par := Parent (Inst_Node);
14572 while Present (Par) loop
14573 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14574 Par := Parent (Par);
14575 end loop;
14576
14577 pragma Assert (Present (Par));
14578
14579 if Nkind (Par) = N_Package_Body then
14580 Collect_Previous_Instances (Declarations (Par));
14581
14582 elsif Nkind (Par) = N_Package_Declaration then
14583 Collect_Previous_Instances
14584 (Visible_Declarations (Specification (Par)));
14585 Collect_Previous_Instances
14586 (Private_Declarations (Specification (Par)));
14587
14588 else
14589 -- Enclosing unit is a subprogram body. In this
14590 -- case all instance bodies are processed in order
14591 -- and there is no need to collect them separately.
14592
14593 null;
14594 end if;
14595
14596 Decl := First_Elmt (Previous_Instances);
14597 while Present (Decl) loop
14598 Info :=
14599 (Act_Decl =>
14600 Instance_Spec (Node (Decl)),
14601 Config_Switches => Save_Config_Switches,
14602 Current_Sem_Unit =>
14603 Get_Code_Unit (Sloc (Node (Decl))),
14604 Expander_Status => Exp_Status,
14605 Inst_Node => Node (Decl),
14606 Local_Suppress_Stack_Top =>
14607 Local_Suppress_Stack_Top,
14608 Scope_Suppress => Scope_Suppress,
14609 Warnings => Save_Warnings);
14610
14611 -- Package instance
14612
14613 if Nkind (Node (Decl)) = N_Package_Instantiation
14614 then
14615 Instantiate_Package_Body
14616 (Info, Body_Optional => True);
14617
14618 -- Subprogram instance
14619
14620 else
14621 -- The instance_spec is in the wrapper package,
14622 -- usually followed by its local renaming
14623 -- declaration. See Build_Subprogram_Renaming
14624 -- for details. If the instance carries aspects,
14625 -- these result in the corresponding pragmas,
14626 -- inserted after the subprogram declaration.
14627 -- They must be skipped as well when retrieving
14628 -- the desired spec. Some of them may have been
14629 -- rewritten as null statements.
14630 -- A direct link would be more robust ???
14631
14632 declare
14633 Decl : Node_Id :=
14634 (Last (Visible_Declarations
14635 (Specification (Info.Act_Decl))));
14636 begin
14637 while Nkind (Decl) in
14638 N_Null_Statement |
14639 N_Pragma |
14640 N_Subprogram_Renaming_Declaration
14641 loop
14642 Decl := Prev (Decl);
14643 end loop;
14644
14645 Info.Act_Decl := Decl;
14646 end;
14647
14648 Instantiate_Subprogram_Body
14649 (Info, Body_Optional => True);
14650 end if;
14651
14652 Next_Elmt (Decl);
14653 end loop;
14654 end;
14655 end if;
14656
14657 Instantiate_Package_Body
14658 (Body_Info =>
14659 ((Act_Decl => True_Parent,
14660 Config_Switches => Save_Config_Switches,
14661 Current_Sem_Unit =>
14662 Get_Code_Unit (Sloc (Inst_Node)),
14663 Expander_Status => Exp_Status,
14664 Inst_Node => Inst_Node,
14665 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14666 Scope_Suppress => Scope_Suppress,
14667 Warnings => Save_Warnings)),
14668 Body_Optional => Body_Optional);
14669 end;
14670 end if;
14671
14672 -- Case where we are not instantiating a nested generic
14673
14674 else
14675 Opt.Style_Check := False;
14676 Expander_Mode_Save_And_Set (True);
14677 Load_Needed_Body (Comp_Unit, OK);
14678 Opt.Style_Check := Saved_Style_Check;
14679 Restore_Warnings (Saved_Warnings);
14680 Expander_Mode_Restore;
14681
14682 if not OK
14683 and then Unit_Requires_Body (Defining_Entity (Spec))
14684 and then not Body_Optional
14685 then
14686 declare
14687 Bname : constant Unit_Name_Type :=
14688 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14689
14690 begin
14691 -- In CodePeer mode, the missing body may make the analysis
14692 -- incomplete, but we do not treat it as fatal.
14693
14694 if CodePeer_Mode then
14695 return;
14696
14697 else
14698 Error_Msg_Unit_1 := Bname;
14699 Error_Msg_N ("this instantiation requires$!", N);
14700 Error_Msg_File_1 :=
14701 Get_File_Name (Bname, Subunit => False);
14702 Error_Msg_N ("\but file{ was not found!", N);
14703 raise Unrecoverable_Error;
14704 end if;
14705 end;
14706 end if;
14707 end if;
14708 end if;
14709
14710 -- If loading parent of the generic caused an instantiation circularity,
14711 -- we abandon compilation at this point, because otherwise in some cases
14712 -- we get into trouble with infinite recursions after this point.
14713
14714 if Circularity_Detected then
14715 raise Unrecoverable_Error;
14716 end if;
14717 end Load_Parent_Of_Generic;
14718
14719 ---------------------------------
14720 -- Map_Formal_Package_Entities --
14721 ---------------------------------
14722
14723 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14724 E1 : Entity_Id;
14725 E2 : Entity_Id;
14726
14727 begin
14728 Set_Instance_Of (Form, Act);
14729
14730 -- Traverse formal and actual package to map the corresponding entities.
14731 -- We skip over internal entities that may be generated during semantic
14732 -- analysis, and find the matching entities by name, given that they
14733 -- must appear in the same order.
14734
14735 E1 := First_Entity (Form);
14736 E2 := First_Entity (Act);
14737 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14738 -- Could this test be a single condition??? Seems like it could, and
14739 -- isn't FPE (Form) a constant anyway???
14740
14741 if not Is_Internal (E1)
14742 and then Present (Parent (E1))
14743 and then not Is_Class_Wide_Type (E1)
14744 and then not Is_Internal_Name (Chars (E1))
14745 then
14746 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14747 Next_Entity (E2);
14748 end loop;
14749
14750 if No (E2) then
14751 exit;
14752 else
14753 Set_Instance_Of (E1, E2);
14754
14755 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14756 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14757 end if;
14758
14759 if Is_Constrained (E1) then
14760 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14761 end if;
14762
14763 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14764 Map_Formal_Package_Entities (E1, E2);
14765 end if;
14766 end if;
14767 end if;
14768
14769 Next_Entity (E1);
14770 end loop;
14771 end Map_Formal_Package_Entities;
14772
14773 -----------------------
14774 -- Move_Freeze_Nodes --
14775 -----------------------
14776
14777 procedure Move_Freeze_Nodes
14778 (Out_Of : Entity_Id;
14779 After : Node_Id;
14780 L : List_Id)
14781 is
14782 Decl : Node_Id;
14783 Next_Decl : Node_Id;
14784 Next_Node : Node_Id := After;
14785 Spec : Node_Id;
14786
14787 function Is_Outer_Type (T : Entity_Id) return Boolean;
14788 -- Check whether entity is declared in a scope external to that of the
14789 -- generic unit.
14790
14791 -------------------
14792 -- Is_Outer_Type --
14793 -------------------
14794
14795 function Is_Outer_Type (T : Entity_Id) return Boolean is
14796 Scop : Entity_Id := Scope (T);
14797
14798 begin
14799 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14800 return True;
14801
14802 else
14803 while Scop /= Standard_Standard loop
14804 if Scop = Out_Of then
14805 return False;
14806 else
14807 Scop := Scope (Scop);
14808 end if;
14809 end loop;
14810
14811 return True;
14812 end if;
14813 end Is_Outer_Type;
14814
14815 -- Start of processing for Move_Freeze_Nodes
14816
14817 begin
14818 if No (L) then
14819 return;
14820 end if;
14821
14822 -- First remove the freeze nodes that may appear before all other
14823 -- declarations.
14824
14825 Decl := First (L);
14826 while Present (Decl)
14827 and then Nkind (Decl) = N_Freeze_Entity
14828 and then Is_Outer_Type (Entity (Decl))
14829 loop
14830 Decl := Remove_Head (L);
14831 Insert_After (Next_Node, Decl);
14832 Set_Analyzed (Decl, False);
14833 Next_Node := Decl;
14834 Decl := First (L);
14835 end loop;
14836
14837 -- Next scan the list of declarations and remove each freeze node that
14838 -- appears ahead of the current node.
14839
14840 while Present (Decl) loop
14841 while Present (Next (Decl))
14842 and then Nkind (Next (Decl)) = N_Freeze_Entity
14843 and then Is_Outer_Type (Entity (Next (Decl)))
14844 loop
14845 Next_Decl := Remove_Next (Decl);
14846 Insert_After (Next_Node, Next_Decl);
14847 Set_Analyzed (Next_Decl, False);
14848 Next_Node := Next_Decl;
14849 end loop;
14850
14851 -- If the declaration is a nested package or concurrent type, then
14852 -- recurse. Nested generic packages will have been processed from the
14853 -- inside out.
14854
14855 case Nkind (Decl) is
14856 when N_Package_Declaration =>
14857 Spec := Specification (Decl);
14858
14859 when N_Task_Type_Declaration =>
14860 Spec := Task_Definition (Decl);
14861
14862 when N_Protected_Type_Declaration =>
14863 Spec := Protected_Definition (Decl);
14864
14865 when others =>
14866 Spec := Empty;
14867 end case;
14868
14869 if Present (Spec) then
14870 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14871 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14872 end if;
14873
14874 Next (Decl);
14875 end loop;
14876 end Move_Freeze_Nodes;
14877
14878 ----------------
14879 -- Next_Assoc --
14880 ----------------
14881
14882 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14883 begin
14884 return Generic_Renamings.Table (E).Next_In_HTable;
14885 end Next_Assoc;
14886
14887 ------------------------
14888 -- Preanalyze_Actuals --
14889 ------------------------
14890
14891 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14892 procedure Perform_Appropriate_Analysis (N : Node_Id);
14893 -- Determine if the actuals we are analyzing come from a generic
14894 -- instantiation that is a library unit and dispatch accordingly.
14895
14896 ----------------------------------
14897 -- Perform_Appropriate_Analysis --
14898 ----------------------------------
14899
14900 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14901 begin
14902 -- When we have a library instantiation we cannot allow any expansion
14903 -- to occur, since there may be no place to put it. Instead, in that
14904 -- case we perform a preanalysis of the actual.
14905
14906 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14907 Preanalyze (N);
14908 else
14909 Analyze (N);
14910 end if;
14911 end Perform_Appropriate_Analysis;
14912
14913 -- Local variables
14914
14915 Errs : constant Nat := Serious_Errors_Detected;
14916
14917 Assoc : Node_Id;
14918 Act : Node_Id;
14919
14920 Cur : Entity_Id := Empty;
14921 -- Current homograph of the instance name
14922
14923 Vis : Boolean := False;
14924 -- Saved visibility status of the current homograph
14925
14926 -- Start of processing for Preanalyze_Actuals
14927
14928 begin
14929 Assoc := First (Generic_Associations (N));
14930
14931 -- If the instance is a child unit, its name may hide an outer homonym,
14932 -- so make it invisible to perform name resolution on the actuals.
14933
14934 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14935 and then Present
14936 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14937 then
14938 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14939
14940 if Is_Compilation_Unit (Cur) then
14941 Vis := Is_Immediately_Visible (Cur);
14942 Set_Is_Immediately_Visible (Cur, False);
14943 else
14944 Cur := Empty;
14945 end if;
14946 end if;
14947
14948 while Present (Assoc) loop
14949 if Nkind (Assoc) /= N_Others_Choice then
14950 Act := Explicit_Generic_Actual_Parameter (Assoc);
14951
14952 -- Within a nested instantiation, a defaulted actual is an empty
14953 -- association, so nothing to analyze. If the subprogram actual
14954 -- is an attribute, analyze prefix only, because actual is not a
14955 -- complete attribute reference.
14956
14957 -- If actual is an allocator, analyze expression only. The full
14958 -- analysis can generate code, and if instance is a compilation
14959 -- unit we have to wait until the package instance is installed
14960 -- to have a proper place to insert this code.
14961
14962 -- String literals may be operators, but at this point we do not
14963 -- know whether the actual is a formal subprogram or a string.
14964
14965 if No (Act) then
14966 null;
14967
14968 elsif Nkind (Act) = N_Attribute_Reference then
14969 Perform_Appropriate_Analysis (Prefix (Act));
14970
14971 elsif Nkind (Act) = N_Explicit_Dereference then
14972 Perform_Appropriate_Analysis (Prefix (Act));
14973
14974 elsif Nkind (Act) = N_Allocator then
14975 declare
14976 Expr : constant Node_Id := Expression (Act);
14977
14978 begin
14979 if Nkind (Expr) = N_Subtype_Indication then
14980 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14981
14982 -- Analyze separately each discriminant constraint, when
14983 -- given with a named association.
14984
14985 declare
14986 Constr : Node_Id;
14987
14988 begin
14989 Constr := First (Constraints (Constraint (Expr)));
14990 while Present (Constr) loop
14991 if Nkind (Constr) = N_Discriminant_Association then
14992 Perform_Appropriate_Analysis
14993 (Expression (Constr));
14994 else
14995 Perform_Appropriate_Analysis (Constr);
14996 end if;
14997
14998 Next (Constr);
14999 end loop;
15000 end;
15001
15002 else
15003 Perform_Appropriate_Analysis (Expr);
15004 end if;
15005 end;
15006
15007 elsif Nkind (Act) /= N_Operator_Symbol then
15008 Perform_Appropriate_Analysis (Act);
15009
15010 -- Within a package instance, mark actuals that are limited
15011 -- views, so their use can be moved to the body of the
15012 -- enclosing unit.
15013
15014 if Is_Entity_Name (Act)
15015 and then Is_Type (Entity (Act))
15016 and then From_Limited_With (Entity (Act))
15017 and then Present (Inst)
15018 then
15019 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15020 end if;
15021 end if;
15022
15023 if Errs /= Serious_Errors_Detected then
15024
15025 -- Do a minimal analysis of the generic, to prevent spurious
15026 -- warnings complaining about the generic being unreferenced,
15027 -- before abandoning the instantiation.
15028
15029 Perform_Appropriate_Analysis (Name (N));
15030
15031 if Is_Entity_Name (Name (N))
15032 and then Etype (Name (N)) /= Any_Type
15033 then
15034 Generate_Reference (Entity (Name (N)), Name (N));
15035 Set_Is_Instantiated (Entity (Name (N)));
15036 end if;
15037
15038 if Present (Cur) then
15039
15040 -- For the case of a child instance hiding an outer homonym,
15041 -- provide additional warning which might explain the error.
15042
15043 Set_Is_Immediately_Visible (Cur, Vis);
15044 Error_Msg_NE
15045 ("& hides outer unit with the same name??",
15046 N, Defining_Unit_Name (N));
15047 end if;
15048
15049 Abandon_Instantiation (Act);
15050 end if;
15051 end if;
15052
15053 Next (Assoc);
15054 end loop;
15055
15056 if Present (Cur) then
15057 Set_Is_Immediately_Visible (Cur, Vis);
15058 end if;
15059 end Preanalyze_Actuals;
15060
15061 -------------------------------
15062 -- Provide_Completing_Bodies --
15063 -------------------------------
15064
15065 procedure Provide_Completing_Bodies (N : Node_Id) is
15066 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15067 -- Generate the completing body for subprogram declaration Subp_Decl
15068
15069 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15070 -- Generating completing bodies for all subprograms found in declarative
15071 -- list Decls.
15072
15073 ---------------------------
15074 -- Build_Completing_Body --
15075 ---------------------------
15076
15077 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15078 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15079 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15080 Spec : Node_Id;
15081
15082 begin
15083 -- Nothing to do if the subprogram already has a completing body
15084
15085 if Present (Corresponding_Body (Subp_Decl)) then
15086 return;
15087
15088 -- Mark the function as having a valid return statement even though
15089 -- the body contains a single raise statement.
15090
15091 elsif Ekind (Subp_Id) = E_Function then
15092 Set_Return_Present (Subp_Id);
15093 end if;
15094
15095 -- Clone the specification to obtain new entities and reset the only
15096 -- semantic field.
15097
15098 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15099 Set_Generic_Parent (Spec, Empty);
15100
15101 -- Generate:
15102 -- function Func ... return ... is
15103 -- <or>
15104 -- procedure Proc ... is
15105 -- begin
15106 -- raise Program_Error with "access before elaboration";
15107 -- edn Proc;
15108
15109 Insert_After_And_Analyze (Subp_Decl,
15110 Make_Subprogram_Body (Loc,
15111 Specification => Spec,
15112 Declarations => New_List,
15113 Handled_Statement_Sequence =>
15114 Make_Handled_Sequence_Of_Statements (Loc,
15115 Statements => New_List (
15116 Make_Raise_Program_Error (Loc,
15117 Reason => PE_Access_Before_Elaboration)))));
15118 end Build_Completing_Body;
15119
15120 ----------------------------------
15121 -- Provide_Completing_Bodies_In --
15122 ----------------------------------
15123
15124 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15125 Decl : Node_Id;
15126
15127 begin
15128 if Present (Decls) then
15129 Decl := First (Decls);
15130 while Present (Decl) loop
15131 Provide_Completing_Bodies (Decl);
15132 Next (Decl);
15133 end loop;
15134 end if;
15135 end Provide_Completing_Bodies_In;
15136
15137 -- Local variables
15138
15139 Spec : Node_Id;
15140
15141 -- Start of processing for Provide_Completing_Bodies
15142
15143 begin
15144 if Nkind (N) = N_Package_Declaration then
15145 Spec := Specification (N);
15146
15147 Push_Scope (Defining_Entity (N));
15148 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15149 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15150 Pop_Scope;
15151
15152 elsif Nkind (N) = N_Subprogram_Declaration then
15153 Build_Completing_Body (N);
15154 end if;
15155 end Provide_Completing_Bodies;
15156
15157 -------------------
15158 -- Remove_Parent --
15159 -------------------
15160
15161 procedure Remove_Parent (In_Body : Boolean := False) is
15162 S : Entity_Id := Current_Scope;
15163 -- S is the scope containing the instantiation just completed. The scope
15164 -- stack contains the parent instances of the instantiation, followed by
15165 -- the original S.
15166
15167 Cur_P : Entity_Id;
15168 E : Entity_Id;
15169 P : Entity_Id;
15170 Hidden : Elmt_Id;
15171
15172 begin
15173 -- After child instantiation is complete, remove from scope stack the
15174 -- extra copy of the current scope, and then remove parent instances.
15175
15176 if not In_Body then
15177 Pop_Scope;
15178
15179 while Current_Scope /= S loop
15180 P := Current_Scope;
15181 End_Package_Scope (Current_Scope);
15182
15183 if In_Open_Scopes (P) then
15184 E := First_Entity (P);
15185 while Present (E) loop
15186 Set_Is_Immediately_Visible (E, True);
15187 Next_Entity (E);
15188 end loop;
15189
15190 -- If instantiation is declared in a block, it is the enclosing
15191 -- scope that might be a parent instance. Note that only one
15192 -- block can be involved, because the parent instances have
15193 -- been installed within it.
15194
15195 if Ekind (P) = E_Block then
15196 Cur_P := Scope (P);
15197 else
15198 Cur_P := P;
15199 end if;
15200
15201 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15202 -- We are within an instance of some sibling. Retain
15203 -- visibility of parent, for proper subsequent cleanup, and
15204 -- reinstall private declarations as well.
15205
15206 Set_In_Private_Part (P);
15207 Install_Private_Declarations (P);
15208 end if;
15209
15210 -- If the ultimate parent is a top-level unit recorded in
15211 -- Instance_Parent_Unit, then reset its visibility to what it was
15212 -- before instantiation. (It's not clear what the purpose is of
15213 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15214 -- present before the ultimate parent test was added.???)
15215
15216 elsif not In_Open_Scopes (Scope (P))
15217 or else (P = Instance_Parent_Unit
15218 and then not Parent_Unit_Visible)
15219 then
15220 Set_Is_Immediately_Visible (P, False);
15221
15222 -- If the current scope is itself an instantiation of a generic
15223 -- nested within P, and we are in the private part of body of this
15224 -- instantiation, restore the full views of P, that were removed
15225 -- in End_Package_Scope above. This obscure case can occur when a
15226 -- subunit of a generic contains an instance of a child unit of
15227 -- its generic parent unit.
15228
15229 elsif S = Current_Scope and then Is_Generic_Instance (S) then
15230 declare
15231 Par : constant Entity_Id :=
15232 Generic_Parent (Package_Specification (S));
15233 begin
15234 if Present (Par)
15235 and then P = Scope (Par)
15236 and then (In_Package_Body (S) or else In_Private_Part (S))
15237 then
15238 Set_In_Private_Part (P);
15239 Install_Private_Declarations (P);
15240 end if;
15241 end;
15242 end if;
15243 end loop;
15244
15245 -- Reset visibility of entities in the enclosing scope
15246
15247 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15248
15249 Hidden := First_Elmt (Hidden_Entities);
15250 while Present (Hidden) loop
15251 Set_Is_Immediately_Visible (Node (Hidden), True);
15252 Next_Elmt (Hidden);
15253 end loop;
15254
15255 else
15256 -- Each body is analyzed separately, and there is no context that
15257 -- needs preserving from one body instance to the next, so remove all
15258 -- parent scopes that have been installed.
15259
15260 while Present (S) loop
15261 End_Package_Scope (S);
15262 Set_Is_Immediately_Visible (S, False);
15263 S := Current_Scope;
15264 exit when S = Standard_Standard;
15265 end loop;
15266 end if;
15267 end Remove_Parent;
15268
15269 -----------------
15270 -- Restore_Env --
15271 -----------------
15272
15273 procedure Restore_Env is
15274 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15275
15276 begin
15277 if No (Current_Instantiated_Parent.Act_Id) then
15278 -- Restore environment after subprogram inlining
15279
15280 Restore_Private_Views (Empty);
15281 end if;
15282
15283 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15284 Exchanged_Views := Saved.Exchanged_Views;
15285 Hidden_Entities := Saved.Hidden_Entities;
15286 Current_Sem_Unit := Saved.Current_Sem_Unit;
15287 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15288 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15289
15290 Restore_Config_Switches (Saved.Switches);
15291
15292 Instance_Envs.Decrement_Last;
15293 end Restore_Env;
15294
15295 ---------------------------
15296 -- Restore_Private_Views --
15297 ---------------------------
15298
15299 procedure Restore_Private_Views
15300 (Pack_Id : Entity_Id;
15301 Is_Package : Boolean := True)
15302 is
15303 M : Elmt_Id;
15304 E : Entity_Id;
15305 Typ : Entity_Id;
15306 Dep_Elmt : Elmt_Id;
15307 Dep_Typ : Node_Id;
15308
15309 procedure Restore_Nested_Formal (Formal : Entity_Id);
15310 -- Hide the generic formals of formal packages declared with box which
15311 -- were reachable in the current instantiation.
15312
15313 ---------------------------
15314 -- Restore_Nested_Formal --
15315 ---------------------------
15316
15317 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15318 Ent : Entity_Id;
15319
15320 begin
15321 if Present (Renamed_Object (Formal))
15322 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
15323 then
15324 return;
15325
15326 elsif Present (Associated_Formal_Package (Formal)) then
15327 Ent := First_Entity (Formal);
15328 while Present (Ent) loop
15329 exit when Ekind (Ent) = E_Package
15330 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15331
15332 Set_Is_Hidden (Ent);
15333 Set_Is_Potentially_Use_Visible (Ent, False);
15334
15335 -- If package, then recurse
15336
15337 if Ekind (Ent) = E_Package then
15338 Restore_Nested_Formal (Ent);
15339 end if;
15340
15341 Next_Entity (Ent);
15342 end loop;
15343 end if;
15344 end Restore_Nested_Formal;
15345
15346 -- Start of processing for Restore_Private_Views
15347
15348 begin
15349 M := First_Elmt (Exchanged_Views);
15350 while Present (M) loop
15351 Typ := Node (M);
15352
15353 -- Subtypes of types whose views have been exchanged, and that are
15354 -- defined within the instance, were not on the Private_Dependents
15355 -- list on entry to the instance, so they have to be exchanged
15356 -- explicitly now, in order to remain consistent with the view of the
15357 -- parent type.
15358
15359 if Ekind (Typ) in E_Private_Type
15360 | E_Limited_Private_Type
15361 | E_Record_Type_With_Private
15362 then
15363 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15364 while Present (Dep_Elmt) loop
15365 Dep_Typ := Node (Dep_Elmt);
15366
15367 if Scope (Dep_Typ) = Pack_Id
15368 and then Present (Full_View (Dep_Typ))
15369 then
15370 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15371 Exchange_Declarations (Dep_Typ);
15372 end if;
15373
15374 Next_Elmt (Dep_Elmt);
15375 end loop;
15376 end if;
15377
15378 Exchange_Declarations (Node (M));
15379 Next_Elmt (M);
15380 end loop;
15381
15382 if No (Pack_Id) then
15383 return;
15384 end if;
15385
15386 -- Make the generic formal parameters private, and make the formal types
15387 -- into subtypes of the actuals again.
15388
15389 E := First_Entity (Pack_Id);
15390 while Present (E) loop
15391 Set_Is_Hidden (E, True);
15392
15393 if Is_Type (E)
15394 and then Nkind (Parent (E)) = N_Subtype_Declaration
15395 then
15396 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15397 -- as it is needed to identify the subtype with the type it
15398 -- renames, when there are conversions between access types
15399 -- to these.
15400
15401 if GNATprove_Mode then
15402 null;
15403
15404 -- If the actual for E is itself a generic actual type from
15405 -- an enclosing instance, E is still a generic actual type
15406 -- outside of the current instance. This matter when resolving
15407 -- an overloaded call that may be ambiguous in the enclosing
15408 -- instance, when two of its actuals coincide.
15409
15410 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
15411 and then Is_Generic_Actual_Type
15412 (Entity (Subtype_Indication (Parent (E))))
15413 then
15414 null;
15415 else
15416 Set_Is_Generic_Actual_Type (E, False);
15417
15418 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15419 -- flag also on the Full_View if the type is private, since it
15420 -- was set also on this Full_View. However, this flag is relied
15421 -- upon by Covers to spot "types exported from instantiations"
15422 -- which are implicit Full_Views built for instantiations made
15423 -- on private types and we get type mismatches if we do it when
15424 -- the block exchanging the declarations below triggers ???
15425
15426 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15427 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15428 -- end if;
15429 end if;
15430
15431 -- An unusual case of aliasing: the actual may also be directly
15432 -- visible in the generic, and be private there, while it is fully
15433 -- visible in the context of the instance. The internal subtype
15434 -- is private in the instance but has full visibility like its
15435 -- parent in the enclosing scope. This enforces the invariant that
15436 -- the privacy status of all private dependents of a type coincide
15437 -- with that of the parent type. This can only happen when a
15438 -- generic child unit is instantiated within a sibling.
15439
15440 if Is_Private_Type (E)
15441 and then not Is_Private_Type (Etype (E))
15442 then
15443 Exchange_Declarations (E);
15444 end if;
15445
15446 elsif Ekind (E) = E_Package then
15447
15448 -- The end of the renaming list is the renaming of the generic
15449 -- package itself. If the instance is a subprogram, all entities
15450 -- in the corresponding package are renamings. If this entity is
15451 -- a formal package, make its own formals private as well. The
15452 -- actual in this case is itself the renaming of an instantiation.
15453 -- If the entity is not a package renaming, it is the entity
15454 -- created to validate formal package actuals: ignore it.
15455
15456 -- If the actual is itself a formal package for the enclosing
15457 -- generic, or the actual for such a formal package, it remains
15458 -- visible on exit from the instance, and therefore nothing needs
15459 -- to be done either, except to keep it accessible.
15460
15461 if Is_Package and then Renamed_Object (E) = Pack_Id then
15462 exit;
15463
15464 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15465 null;
15466
15467 elsif
15468 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
15469 then
15470 Set_Is_Hidden (E, False);
15471
15472 else
15473 declare
15474 Act_P : constant Entity_Id := Renamed_Object (E);
15475 Id : Entity_Id;
15476
15477 begin
15478 Id := First_Entity (Act_P);
15479 while Present (Id)
15480 and then Id /= First_Private_Entity (Act_P)
15481 loop
15482 exit when Ekind (Id) = E_Package
15483 and then Renamed_Object (Id) = Act_P;
15484
15485 Set_Is_Hidden (Id, True);
15486 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15487
15488 if Ekind (Id) = E_Package then
15489 Restore_Nested_Formal (Id);
15490 end if;
15491
15492 Next_Entity (Id);
15493 end loop;
15494 end;
15495 end if;
15496 end if;
15497
15498 Next_Entity (E);
15499 end loop;
15500 end Restore_Private_Views;
15501
15502 --------------
15503 -- Save_Env --
15504 --------------
15505
15506 procedure Save_Env
15507 (Gen_Unit : Entity_Id;
15508 Act_Unit : Entity_Id)
15509 is
15510 begin
15511 Init_Env;
15512 Set_Instance_Env (Gen_Unit, Act_Unit);
15513 end Save_Env;
15514
15515 ----------------------------
15516 -- Save_Global_References --
15517 ----------------------------
15518
15519 procedure Save_Global_References (Templ : Node_Id) is
15520
15521 -- ??? it is horrible to use global variables in highly recursive code
15522
15523 E : Entity_Id;
15524 -- The entity of the current associated node
15525
15526 Gen_Scope : Entity_Id;
15527 -- The scope of the generic for which references are being saved
15528
15529 N2 : Node_Id;
15530 -- The current associated node
15531
15532 function Is_Global (E : Entity_Id) return Boolean;
15533 -- Check whether entity is defined outside of generic unit. Examine the
15534 -- scope of an entity, and the scope of the scope, etc, until we find
15535 -- either Standard, in which case the entity is global, or the generic
15536 -- unit itself, which indicates that the entity is local. If the entity
15537 -- is the generic unit itself, as in the case of a recursive call, or
15538 -- the enclosing generic unit, if different from the current scope, then
15539 -- it is local as well, because it will be replaced at the point of
15540 -- instantiation. On the other hand, if it is a reference to a child
15541 -- unit of a common ancestor, which appears in an instantiation, it is
15542 -- global because it is used to denote a specific compilation unit at
15543 -- the time the instantiations will be analyzed.
15544
15545 procedure Qualify_Universal_Operands
15546 (Op : Node_Id;
15547 Func_Call : Node_Id);
15548 -- Op denotes a binary or unary operator in generic template Templ. Node
15549 -- Func_Call is the function call alternative of the operator within the
15550 -- the analyzed copy of the template. Change each operand which yields a
15551 -- universal type by wrapping it into a qualified expression
15552 --
15553 -- Actual_Typ'(Operand)
15554 --
15555 -- where Actual_Typ is the type of corresponding actual parameter of
15556 -- Operand in Func_Call.
15557
15558 procedure Reset_Entity (N : Node_Id);
15559 -- Save semantic information on global entity so that it is not resolved
15560 -- again at instantiation time.
15561
15562 procedure Save_Entity_Descendants (N : Node_Id);
15563 -- Apply Save_Global_References to the two syntactic descendants of
15564 -- non-terminal nodes that carry an Associated_Node and are processed
15565 -- through Reset_Entity. Once the global entity (if any) has been
15566 -- captured together with its type, only two syntactic descendants need
15567 -- to be traversed to complete the processing of the tree rooted at N.
15568 -- This applies to Selected_Components, Expanded_Names, and to Operator
15569 -- nodes. N can also be a character literal, identifier, or operator
15570 -- symbol node, but the call has no effect in these cases.
15571
15572 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15573 -- Default actuals in nested instances must be handled specially
15574 -- because there is no link to them from the original tree. When an
15575 -- actual subprogram is given by a default, we add an explicit generic
15576 -- association for it in the instantiation node. When we save the
15577 -- global references on the name of the instance, we recover the list
15578 -- of generic associations, and add an explicit one to the original
15579 -- generic tree, through which a global actual can be preserved.
15580 -- Similarly, if a child unit is instantiated within a sibling, in the
15581 -- context of the parent, we must preserve the identifier of the parent
15582 -- so that it can be properly resolved in a subsequent instantiation.
15583
15584 procedure Save_Global_Descendant (D : Union_Id);
15585 -- Apply Save_References recursively to the descendants of node D
15586
15587 procedure Save_References (N : Node_Id);
15588 -- This is the recursive procedure that does the work, once the
15589 -- enclosing generic scope has been established.
15590
15591 ---------------
15592 -- Is_Global --
15593 ---------------
15594
15595 function Is_Global (E : Entity_Id) return Boolean is
15596 Se : Entity_Id;
15597
15598 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15599 -- Determine whether the parent node of a reference to a child unit
15600 -- denotes an instantiation or a formal package, in which case the
15601 -- reference to the child unit is global, even if it appears within
15602 -- the current scope (e.g. when the instance appears within the body
15603 -- of an ancestor).
15604
15605 ----------------------
15606 -- Is_Instance_Node --
15607 ----------------------
15608
15609 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15610 begin
15611 return Nkind (Decl) in N_Generic_Instantiation
15612 or else
15613 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15614 end Is_Instance_Node;
15615
15616 -- Start of processing for Is_Global
15617
15618 begin
15619 if E = Gen_Scope then
15620 return False;
15621
15622 elsif E = Standard_Standard then
15623 return True;
15624
15625 -- E should be an entity, but it is not always
15626
15627 elsif Nkind (E) not in N_Entity then -- ????
15628 return False;
15629
15630 elsif Is_Child_Unit (E)
15631 and then (Is_Instance_Node (Parent (N2))
15632 or else (Nkind (Parent (N2)) = N_Expanded_Name
15633 and then N2 = Selector_Name (Parent (N2))
15634 and then
15635 Is_Instance_Node (Parent (Parent (N2)))))
15636 then
15637 return True;
15638
15639 else
15640 Se := Scope (E);
15641 while Se /= Gen_Scope loop
15642 if Se = Standard_Standard then
15643 return True;
15644 else
15645 Se := Scope (Se);
15646 end if;
15647 end loop;
15648
15649 return False;
15650 end if;
15651 end Is_Global;
15652
15653 --------------------------------
15654 -- Qualify_Universal_Operands --
15655 --------------------------------
15656
15657 procedure Qualify_Universal_Operands
15658 (Op : Node_Id;
15659 Func_Call : Node_Id)
15660 is
15661 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15662 -- Rewrite operand Opnd as a qualified expression of the form
15663 --
15664 -- Actual_Typ'(Opnd)
15665 --
15666 -- where Actual is the corresponding actual parameter of Opnd in
15667 -- function call Func_Call.
15668
15669 function Qualify_Type
15670 (Loc : Source_Ptr;
15671 Typ : Entity_Id) return Node_Id;
15672 -- Qualify type Typ by creating a selected component of the form
15673 --
15674 -- Scope_Of_Typ.Typ
15675
15676 ---------------------
15677 -- Qualify_Operand --
15678 ---------------------
15679
15680 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15681 Loc : constant Source_Ptr := Sloc (Opnd);
15682 Typ : constant Entity_Id := Etype (Actual);
15683 Mark : Node_Id;
15684 Qual : Node_Id;
15685
15686 begin
15687 -- Qualify the operand when it is of a universal type. Note that
15688 -- the template is unanalyzed and it is not possible to directly
15689 -- query the type. This transformation is not done when the type
15690 -- of the actual is internally generated because the type will be
15691 -- regenerated in the instance.
15692
15693 if Yields_Universal_Type (Opnd)
15694 and then Comes_From_Source (Typ)
15695 and then not Is_Hidden (Typ)
15696 then
15697 -- The type of the actual may be a global reference. Save this
15698 -- information by creating a reference to it.
15699
15700 if Is_Global (Typ) then
15701 Mark := New_Occurrence_Of (Typ, Loc);
15702
15703 -- Otherwise rely on resolution to find the proper type within
15704 -- the instance.
15705
15706 else
15707 Mark := Qualify_Type (Loc, Typ);
15708 end if;
15709
15710 Qual :=
15711 Make_Qualified_Expression (Loc,
15712 Subtype_Mark => Mark,
15713 Expression => Relocate_Node (Opnd));
15714
15715 -- Mark the qualification to distinguish it from other source
15716 -- constructs and signal the instantiation mechanism that this
15717 -- node requires special processing. See Copy_Generic_Node for
15718 -- details.
15719
15720 Set_Is_Qualified_Universal_Literal (Qual);
15721
15722 Rewrite (Opnd, Qual);
15723 end if;
15724 end Qualify_Operand;
15725
15726 ------------------
15727 -- Qualify_Type --
15728 ------------------
15729
15730 function Qualify_Type
15731 (Loc : Source_Ptr;
15732 Typ : Entity_Id) return Node_Id
15733 is
15734 Scop : constant Entity_Id := Scope (Typ);
15735 Result : Node_Id;
15736
15737 begin
15738 Result := Make_Identifier (Loc, Chars (Typ));
15739
15740 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15741 Result :=
15742 Make_Selected_Component (Loc,
15743 Prefix => Make_Identifier (Loc, Chars (Scop)),
15744 Selector_Name => Result);
15745 end if;
15746
15747 return Result;
15748 end Qualify_Type;
15749
15750 -- Local variables
15751
15752 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15753
15754 -- Start of processing for Qualify_Universal_Operands
15755
15756 begin
15757 if Nkind (Op) in N_Binary_Op then
15758 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15759 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15760
15761 elsif Nkind (Op) in N_Unary_Op then
15762 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15763 end if;
15764 end Qualify_Universal_Operands;
15765
15766 ------------------
15767 -- Reset_Entity --
15768 ------------------
15769
15770 procedure Reset_Entity (N : Node_Id) is
15771 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15772 -- If the type of N2 is global to the generic unit, save the type in
15773 -- the generic node. Just as we perform name capture for explicit
15774 -- references within the generic, we must capture the global types
15775 -- of local entities because they may participate in resolution in
15776 -- the instance.
15777
15778 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15779 -- Find the ultimate ancestor of the current unit. If it is not a
15780 -- generic unit, then the name of the current unit in the prefix of
15781 -- an expanded name must be replaced with its generic homonym to
15782 -- ensure that it will be properly resolved in an instance.
15783
15784 ---------------------
15785 -- Set_Global_Type --
15786 ---------------------
15787
15788 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15789 Typ : constant Entity_Id := Etype (N2);
15790
15791 begin
15792 Set_Etype (N, Typ);
15793
15794 -- If the entity of N is not the associated node, this is a
15795 -- nested generic and it has an associated node as well, whose
15796 -- type is already the full view (see below). Indicate that the
15797 -- original node has a private view.
15798
15799 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15800 Set_Has_Private_View (N);
15801 end if;
15802
15803 -- If not a private type, nothing else to do
15804
15805 if not Is_Private_Type (Typ) then
15806 null;
15807
15808 -- If it is a derivation of a private type in a context where no
15809 -- full view is needed, nothing to do either.
15810
15811 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15812 null;
15813
15814 -- Otherwise mark the type for flipping and use the full view when
15815 -- available.
15816
15817 else
15818 Set_Has_Private_View (N);
15819
15820 if Present (Full_View (Typ)) then
15821 Set_Etype (N2, Full_View (Typ));
15822 end if;
15823 end if;
15824
15825 if Is_Floating_Point_Type (Typ)
15826 and then Has_Dimension_System (Typ)
15827 then
15828 Copy_Dimensions (N2, N);
15829 end if;
15830 end Set_Global_Type;
15831
15832 ------------------
15833 -- Top_Ancestor --
15834 ------------------
15835
15836 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15837 Par : Entity_Id;
15838
15839 begin
15840 Par := E;
15841 while Is_Child_Unit (Par) loop
15842 Par := Scope (Par);
15843 end loop;
15844
15845 return Par;
15846 end Top_Ancestor;
15847
15848 -- Start of processing for Reset_Entity
15849
15850 begin
15851 N2 := Get_Associated_Node (N);
15852 E := Entity (N2);
15853
15854 if Present (E) then
15855
15856 -- If the node is an entry call to an entry in an enclosing task,
15857 -- it is rewritten as a selected component. No global entity to
15858 -- preserve in this case, since the expansion will be redone in
15859 -- the instance.
15860
15861 if Nkind (E) not in N_Entity then
15862 Set_Associated_Node (N, Empty);
15863 Set_Etype (N, Empty);
15864 return;
15865 end if;
15866
15867 -- If the entity is an itype created as a subtype of an access
15868 -- type with a null exclusion restore source entity for proper
15869 -- visibility. The itype will be created anew in the instance.
15870
15871 if Is_Itype (E)
15872 and then Ekind (E) = E_Access_Subtype
15873 and then Is_Entity_Name (N)
15874 and then Chars (Etype (E)) = Chars (N)
15875 then
15876 E := Etype (E);
15877 Set_Entity (N2, E);
15878 Set_Etype (N2, E);
15879 end if;
15880
15881 if Is_Global (E) then
15882 Set_Global_Type (N, N2);
15883
15884 elsif Nkind (N) = N_Op_Concat
15885 and then Is_Generic_Type (Etype (N2))
15886 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15887 or else
15888 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15889 and then Is_Intrinsic_Subprogram (E)
15890 then
15891 null;
15892
15893 -- Entity is local. Mark generic node as unresolved. Note that now
15894 -- it does not have an entity.
15895
15896 else
15897 Set_Associated_Node (N, Empty);
15898 Set_Etype (N, Empty);
15899 end if;
15900
15901 if Nkind (Parent (N)) in N_Generic_Instantiation
15902 and then N = Name (Parent (N))
15903 then
15904 Save_Global_Defaults (Parent (N), Parent (N2));
15905 end if;
15906
15907 elsif Nkind (Parent (N)) = N_Selected_Component
15908 and then Nkind (Parent (N2)) = N_Expanded_Name
15909 then
15910 -- In case of previous errors, the tree might be malformed
15911
15912 if No (Entity (Parent (N2))) then
15913 null;
15914
15915 elsif Is_Global (Entity (Parent (N2))) then
15916 Change_Selected_Component_To_Expanded_Name (Parent (N));
15917 Set_Associated_Node (Parent (N), Parent (N2));
15918 Set_Global_Type (Parent (N), Parent (N2));
15919 Save_Entity_Descendants (N);
15920
15921 -- If this is a reference to the current generic entity, replace
15922 -- by the name of the generic homonym of the current package. This
15923 -- is because in an instantiation Par.P.Q will not resolve to the
15924 -- name of the instance, whose enclosing scope is not necessarily
15925 -- Par. We use the generic homonym rather that the name of the
15926 -- generic itself because it may be hidden by a local declaration.
15927
15928 elsif In_Open_Scopes (Entity (Parent (N2)))
15929 and then not
15930 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15931 then
15932 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15933 Rewrite (Parent (N),
15934 Make_Identifier (Sloc (N),
15935 Chars =>
15936 Chars (Generic_Homonym (Entity (Parent (N2))))));
15937 else
15938 Rewrite (Parent (N),
15939 Make_Identifier (Sloc (N),
15940 Chars => Chars (Selector_Name (Parent (N2)))));
15941 end if;
15942 end if;
15943
15944 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15945 and then Parent (N) = Name (Parent (Parent (N)))
15946 then
15947 Save_Global_Defaults
15948 (Parent (Parent (N)), Parent (Parent (N2)));
15949 end if;
15950
15951 -- A selected component may denote a static constant that has been
15952 -- folded. If the static constant is global to the generic, capture
15953 -- its value. Otherwise the folding will happen in any instantiation.
15954
15955 elsif Nkind (Parent (N)) = N_Selected_Component
15956 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
15957 then
15958 if Present (Entity (Original_Node (Parent (N2))))
15959 and then Is_Global (Entity (Original_Node (Parent (N2))))
15960 then
15961 Rewrite (Parent (N), New_Copy (Parent (N2)));
15962 Set_Analyzed (Parent (N), False);
15963 end if;
15964
15965 -- A selected component may be transformed into a parameterless
15966 -- function call. If the called entity is global, rewrite the node
15967 -- appropriately, i.e. as an extended name for the global entity.
15968
15969 elsif Nkind (Parent (N)) = N_Selected_Component
15970 and then Nkind (Parent (N2)) = N_Function_Call
15971 and then N = Selector_Name (Parent (N))
15972 then
15973 if No (Parameter_Associations (Parent (N2))) then
15974 if Is_Global (Entity (Name (Parent (N2)))) then
15975 Change_Selected_Component_To_Expanded_Name (Parent (N));
15976 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15977 Set_Global_Type (Parent (N), Name (Parent (N2)));
15978 Save_Entity_Descendants (N);
15979
15980 else
15981 Set_Is_Prefixed_Call (Parent (N));
15982 Set_Associated_Node (N, Empty);
15983 Set_Etype (N, Empty);
15984 end if;
15985
15986 -- In Ada 2005, X.F may be a call to a primitive operation,
15987 -- rewritten as F (X). This rewriting will be done again in an
15988 -- instance, so keep the original node. Global entities will be
15989 -- captured as for other constructs. Indicate that this must
15990 -- resolve as a call, to prevent accidental overloading in the
15991 -- instance, if both a component and a primitive operation appear
15992 -- as candidates.
15993
15994 else
15995 Set_Is_Prefixed_Call (Parent (N));
15996 end if;
15997
15998 -- Entity is local. Reset in generic unit, so that node is resolved
15999 -- anew at the point of instantiation.
16000
16001 else
16002 Set_Associated_Node (N, Empty);
16003 Set_Etype (N, Empty);
16004 end if;
16005 end Reset_Entity;
16006
16007 -----------------------------
16008 -- Save_Entity_Descendants --
16009 -----------------------------
16010
16011 procedure Save_Entity_Descendants (N : Node_Id) is
16012 begin
16013 case Nkind (N) is
16014 when N_Binary_Op =>
16015 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
16016 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16017
16018 when N_Unary_Op =>
16019 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16020
16021 when N_Expanded_Name
16022 | N_Selected_Component
16023 =>
16024 Save_Global_Descendant (Union_Id (Prefix (N)));
16025 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16026
16027 when N_Character_Literal
16028 | N_Identifier
16029 | N_Operator_Symbol
16030 =>
16031 null;
16032
16033 when others =>
16034 raise Program_Error;
16035 end case;
16036 end Save_Entity_Descendants;
16037
16038 --------------------------
16039 -- Save_Global_Defaults --
16040 --------------------------
16041
16042 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16043 Loc : constant Source_Ptr := Sloc (N1);
16044 Assoc2 : constant List_Id := Generic_Associations (N2);
16045 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16046 Assoc1 : List_Id;
16047 Act1 : Node_Id;
16048 Act2 : Node_Id;
16049 Def : Node_Id;
16050 Ndec : Node_Id;
16051 Subp : Entity_Id;
16052 Actual : Entity_Id;
16053
16054 begin
16055 Assoc1 := Generic_Associations (N1);
16056
16057 if Present (Assoc1) then
16058 Act1 := First (Assoc1);
16059 else
16060 Act1 := Empty;
16061 Set_Generic_Associations (N1, New_List);
16062 Assoc1 := Generic_Associations (N1);
16063 end if;
16064
16065 if Present (Assoc2) then
16066 Act2 := First (Assoc2);
16067 else
16068 return;
16069 end if;
16070
16071 while Present (Act1) and then Present (Act2) loop
16072 Next (Act1);
16073 Next (Act2);
16074 end loop;
16075
16076 -- Find the associations added for default subprograms
16077
16078 if Present (Act2) then
16079 while Nkind (Act2) /= N_Generic_Association
16080 or else No (Entity (Selector_Name (Act2)))
16081 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16082 loop
16083 Next (Act2);
16084 end loop;
16085
16086 -- Add a similar association if the default is global. The
16087 -- renaming declaration for the actual has been analyzed, and
16088 -- its alias is the program it renames. Link the actual in the
16089 -- original generic tree with the node in the analyzed tree.
16090
16091 while Present (Act2) loop
16092 Subp := Entity (Selector_Name (Act2));
16093 Def := Explicit_Generic_Actual_Parameter (Act2);
16094
16095 -- Following test is defence against rubbish errors
16096
16097 if No (Alias (Subp)) then
16098 return;
16099 end if;
16100
16101 -- Retrieve the resolved actual from the renaming declaration
16102 -- created for the instantiated formal.
16103
16104 Actual := Entity (Name (Parent (Parent (Subp))));
16105 Set_Entity (Def, Actual);
16106 Set_Etype (Def, Etype (Actual));
16107
16108 if Is_Global (Actual) then
16109 Ndec :=
16110 Make_Generic_Association (Loc,
16111 Selector_Name =>
16112 New_Occurrence_Of (Subp, Loc),
16113 Explicit_Generic_Actual_Parameter =>
16114 New_Occurrence_Of (Actual, Loc));
16115
16116 Set_Associated_Node
16117 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16118
16119 Append (Ndec, Assoc1);
16120
16121 -- If there are other defaults, add a dummy association in case
16122 -- there are other defaulted formals with the same name.
16123
16124 elsif Present (Next (Act2)) then
16125 Ndec :=
16126 Make_Generic_Association (Loc,
16127 Selector_Name =>
16128 New_Occurrence_Of (Subp, Loc),
16129 Explicit_Generic_Actual_Parameter => Empty);
16130
16131 Append (Ndec, Assoc1);
16132 end if;
16133
16134 Next (Act2);
16135 end loop;
16136 end if;
16137
16138 if Nkind (Name (N1)) = N_Identifier
16139 and then Is_Child_Unit (Gen_Id)
16140 and then Is_Global (Gen_Id)
16141 and then Is_Generic_Unit (Scope (Gen_Id))
16142 and then In_Open_Scopes (Scope (Gen_Id))
16143 then
16144 -- This is an instantiation of a child unit within a sibling, so
16145 -- that the generic parent is in scope. An eventual instance must
16146 -- occur within the scope of an instance of the parent. Make name
16147 -- in instance into an expanded name, to preserve the identifier
16148 -- of the parent, so it can be resolved subsequently.
16149
16150 Rewrite (Name (N2),
16151 Make_Expanded_Name (Loc,
16152 Chars => Chars (Gen_Id),
16153 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16154 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16155 Set_Entity (Name (N2), Gen_Id);
16156
16157 Rewrite (Name (N1),
16158 Make_Expanded_Name (Loc,
16159 Chars => Chars (Gen_Id),
16160 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16161 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16162
16163 Set_Associated_Node (Name (N1), Name (N2));
16164 Set_Associated_Node (Prefix (Name (N1)), Empty);
16165 Set_Associated_Node
16166 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16167 Set_Etype (Name (N1), Etype (Gen_Id));
16168 end if;
16169 end Save_Global_Defaults;
16170
16171 ----------------------------
16172 -- Save_Global_Descendant --
16173 ----------------------------
16174
16175 procedure Save_Global_Descendant (D : Union_Id) is
16176 N1 : Node_Id;
16177
16178 begin
16179 if D in Node_Range then
16180 if D = Union_Id (Empty) then
16181 null;
16182
16183 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16184 Save_References (Node_Id (D));
16185 end if;
16186
16187 elsif D in List_Range then
16188 pragma Assert (D /= Union_Id (No_List));
16189 -- Because No_List = Empty, which is in Node_Range above
16190
16191 N1 := First (List_Id (D));
16192 while Present (N1) loop
16193 Save_References (N1);
16194 Next (N1);
16195 end loop;
16196
16197 -- Element list or other non-node field, nothing to do
16198
16199 else
16200 null;
16201 end if;
16202 end Save_Global_Descendant;
16203
16204 ---------------------
16205 -- Save_References --
16206 ---------------------
16207
16208 -- This is the recursive procedure that does the work once the enclosing
16209 -- generic scope has been established. We have to treat specially a
16210 -- number of node rewritings that are required by semantic processing
16211 -- and which change the kind of nodes in the generic copy: typically
16212 -- constant-folding, replacing an operator node by a string literal, or
16213 -- a selected component by an expanded name. In each of those cases, the
16214 -- transformation is propagated to the generic unit.
16215
16216 procedure Save_References (N : Node_Id) is
16217 Loc : constant Source_Ptr := Sloc (N);
16218
16219 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16220 -- Determine whether arbitrary node Nod requires delayed capture of
16221 -- global references within its aspect specifications.
16222
16223 procedure Save_References_In_Aggregate (N : Node_Id);
16224 -- Save all global references in [extension] aggregate node N
16225
16226 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16227 -- Save all global references in a character literal or operator
16228 -- symbol denoted by N.
16229
16230 procedure Save_References_In_Descendants (N : Node_Id);
16231 -- Save all global references in all descendants of node N
16232
16233 procedure Save_References_In_Identifier (N : Node_Id);
16234 -- Save all global references in identifier node N
16235
16236 procedure Save_References_In_Operator (N : Node_Id);
16237 -- Save all global references in operator node N
16238
16239 procedure Save_References_In_Pragma (Prag : Node_Id);
16240 -- Save all global references found within the expression of pragma
16241 -- Prag.
16242
16243 ---------------------------
16244 -- Requires_Delayed_Save --
16245 ---------------------------
16246
16247 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16248 begin
16249 -- Generic packages and subprograms require delayed capture of
16250 -- global references within their aspects due to the timing of
16251 -- annotation analysis.
16252
16253 if Nkind (Nod) in N_Generic_Package_Declaration
16254 | N_Generic_Subprogram_Declaration
16255 | N_Package_Body
16256 | N_Package_Body_Stub
16257 | N_Subprogram_Body
16258 | N_Subprogram_Body_Stub
16259 then
16260 -- Since the capture of global references is done on the
16261 -- unanalyzed generic template, there is no information around
16262 -- to infer the context. Use the Associated_Entity linkages to
16263 -- peek into the analyzed generic copy and determine what the
16264 -- template corresponds to.
16265
16266 if Nod = Templ then
16267 return
16268 Is_Generic_Declaration_Or_Body
16269 (Unit_Declaration_Node
16270 (Associated_Entity (Defining_Entity (Nod))));
16271
16272 -- Otherwise the generic unit being processed is not the top
16273 -- level template. It is safe to capture of global references
16274 -- within the generic unit because at this point the top level
16275 -- copy is fully analyzed.
16276
16277 else
16278 return False;
16279 end if;
16280
16281 -- Otherwise capture the global references without interference
16282
16283 else
16284 return False;
16285 end if;
16286 end Requires_Delayed_Save;
16287
16288 ----------------------------------
16289 -- Save_References_In_Aggregate --
16290 ----------------------------------
16291
16292 procedure Save_References_In_Aggregate (N : Node_Id) is
16293 Nam : Node_Id;
16294 Qual : Node_Id := Empty;
16295 Typ : Entity_Id := Empty;
16296
16297 begin
16298 N2 := Get_Associated_Node (N);
16299
16300 if Present (N2) then
16301 Typ := Etype (N2);
16302
16303 -- In an instance within a generic, use the name of the actual
16304 -- and not the original generic parameter. If the actual is
16305 -- global in the current generic it must be preserved for its
16306 -- instantiation.
16307
16308 if Nkind (Parent (Typ)) = N_Subtype_Declaration
16309 and then Present (Generic_Parent_Type (Parent (Typ)))
16310 then
16311 Typ := Base_Type (Typ);
16312 Set_Etype (N2, Typ);
16313 end if;
16314 end if;
16315
16316 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16317 Set_Associated_Node (N, Empty);
16318
16319 -- If the aggregate is an actual in a call, it has been
16320 -- resolved in the current context, to some local type. The
16321 -- enclosing call may have been disambiguated by the aggregate,
16322 -- and this disambiguation might fail at instantiation time
16323 -- because the type to which the aggregate did resolve is not
16324 -- preserved. In order to preserve some of this information,
16325 -- wrap the aggregate in a qualified expression, using the id
16326 -- of its type. For further disambiguation we qualify the type
16327 -- name with its scope (if visible and not hidden by a local
16328 -- homograph) because both id's will have corresponding
16329 -- entities in an instance. This resolves most of the problems
16330 -- with missing type information on aggregates in instances.
16331
16332 if Present (N2)
16333 and then Nkind (N2) = Nkind (N)
16334 and then Nkind (Parent (N2)) in N_Subprogram_Call
16335 and then Present (Typ)
16336 and then Comes_From_Source (Typ)
16337 then
16338 Nam := Make_Identifier (Loc, Chars (Typ));
16339
16340 if Is_Immediately_Visible (Scope (Typ))
16341 and then
16342 (not In_Open_Scopes (Scope (Typ))
16343 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16344 then
16345 Nam :=
16346 Make_Selected_Component (Loc,
16347 Prefix =>
16348 Make_Identifier (Loc, Chars (Scope (Typ))),
16349 Selector_Name => Nam);
16350 end if;
16351
16352 Qual :=
16353 Make_Qualified_Expression (Loc,
16354 Subtype_Mark => Nam,
16355 Expression => Relocate_Node (N));
16356 end if;
16357 end if;
16358
16359 if Nkind (N) = N_Aggregate then
16360 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
16361
16362 elsif Nkind (N) = N_Extension_Aggregate then
16363 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
16364
16365 else
16366 pragma Assert (False);
16367 end if;
16368
16369 Save_Global_Descendant (Union_Id (Expressions (N)));
16370 Save_Global_Descendant (Union_Id (Component_Associations (N)));
16371 Save_Global_Descendant (Union_Id (Etype (N)));
16372
16373 if Present (Qual) then
16374 Rewrite (N, Qual);
16375 end if;
16376 end Save_References_In_Aggregate;
16377
16378 ----------------------------------------------
16379 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16380 ----------------------------------------------
16381
16382 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16383 begin
16384 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16385 Reset_Entity (N);
16386
16387 elsif Nkind (N) = N_Operator_Symbol
16388 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16389 then
16390 Change_Operator_Symbol_To_String_Literal (N);
16391 end if;
16392 end Save_References_In_Char_Lit_Or_Op_Symbol;
16393
16394 ------------------------------------
16395 -- Save_References_In_Descendants --
16396 ------------------------------------
16397
16398 procedure Save_References_In_Descendants (N : Node_Id) is
16399 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
16400 begin
16401 Walk (N);
16402 end Save_References_In_Descendants;
16403
16404 -----------------------------------
16405 -- Save_References_In_Identifier --
16406 -----------------------------------
16407
16408 procedure Save_References_In_Identifier (N : Node_Id) is
16409 begin
16410 -- The node did not undergo a transformation
16411
16412 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16413 -- If this is a discriminant reference, always save it.
16414 -- It is used in the instance to find the corresponding
16415 -- discriminant positionally rather than by name.
16416
16417 Set_Original_Discriminant
16418 (N, Original_Discriminant (Get_Associated_Node (N)));
16419
16420 Reset_Entity (N);
16421
16422 -- The analysis of the generic copy transformed the identifier
16423 -- into another construct. Propagate the changes to the template.
16424
16425 else
16426 N2 := Get_Associated_Node (N);
16427
16428 -- The identifier denotes a call to a parameterless function.
16429 -- Mark the node as resolved when the function is external.
16430
16431 if Nkind (N2) = N_Function_Call then
16432 E := Entity (Name (N2));
16433
16434 if Present (E) and then Is_Global (E) then
16435 Set_Etype (N, Etype (N2));
16436 else
16437 Set_Associated_Node (N, Empty);
16438 Set_Etype (N, Empty);
16439 end if;
16440
16441 -- The identifier denotes a named number that was constant
16442 -- folded. Preserve the original name for ASIS and undo the
16443 -- constant folding which will be repeated in the instance.
16444 -- Is this still needed???
16445
16446 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16447 and then Is_Entity_Name (Original_Node (N2))
16448 then
16449 Set_Associated_Node (N, Original_Node (N2));
16450 Reset_Entity (N);
16451
16452 -- The identifier resolved to a string literal. Propagate this
16453 -- information to the generic template.
16454
16455 elsif Nkind (N2) = N_String_Literal then
16456 Rewrite (N, New_Copy (N2));
16457
16458 -- The identifier is rewritten as a dereference if it is the
16459 -- prefix of an implicit dereference. Preserve the original
16460 -- tree as the analysis of the instance will expand the node
16461 -- again, but preserve the resolved entity if it is global.
16462
16463 elsif Nkind (N2) = N_Explicit_Dereference then
16464 if Is_Entity_Name (Prefix (N2))
16465 and then Present (Entity (Prefix (N2)))
16466 and then Is_Global (Entity (Prefix (N2)))
16467 then
16468 Set_Associated_Node (N, Prefix (N2));
16469
16470 elsif Nkind (Prefix (N2)) = N_Function_Call
16471 and then Present (Entity (Name (Prefix (N2))))
16472 and then Is_Global (Entity (Name (Prefix (N2))))
16473 then
16474 Rewrite (N,
16475 Make_Explicit_Dereference (Loc,
16476 Prefix =>
16477 Make_Function_Call (Loc,
16478 Name =>
16479 New_Occurrence_Of
16480 (Entity (Name (Prefix (N2))), Loc))));
16481
16482 else
16483 Set_Associated_Node (N, Empty);
16484 Set_Etype (N, Empty);
16485 end if;
16486
16487 -- The subtype mark of a nominally unconstrained object is
16488 -- rewritten as a subtype indication using the bounds of the
16489 -- expression. Recover the original subtype mark.
16490
16491 elsif Nkind (N2) = N_Subtype_Indication
16492 and then Is_Entity_Name (Original_Node (N2))
16493 then
16494 Set_Associated_Node (N, Original_Node (N2));
16495 Reset_Entity (N);
16496 end if;
16497 end if;
16498 end Save_References_In_Identifier;
16499
16500 ---------------------------------
16501 -- Save_References_In_Operator --
16502 ---------------------------------
16503
16504 procedure Save_References_In_Operator (N : Node_Id) is
16505 begin
16506 -- The node did not undergo a transformation
16507
16508 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16509 if Nkind (N) = N_Op_Concat then
16510 Set_Is_Component_Left_Opnd (N,
16511 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16512
16513 Set_Is_Component_Right_Opnd (N,
16514 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16515 end if;
16516
16517 Reset_Entity (N);
16518
16519 -- The analysis of the generic copy transformed the operator into
16520 -- some other construct. Propagate the changes to the template if
16521 -- applicable.
16522
16523 else
16524 N2 := Get_Associated_Node (N);
16525
16526 -- The operator resoved to a function call
16527
16528 if Nkind (N2) = N_Function_Call then
16529
16530 -- Add explicit qualifications in the generic template for
16531 -- all operands of universal type. This aids resolution by
16532 -- preserving the actual type of a literal or an attribute
16533 -- that yields a universal result.
16534
16535 Qualify_Universal_Operands (N, N2);
16536
16537 E := Entity (Name (N2));
16538
16539 if Present (E) and then Is_Global (E) then
16540 Set_Etype (N, Etype (N2));
16541 else
16542 Set_Associated_Node (N, Empty);
16543 Set_Etype (N, Empty);
16544 end if;
16545
16546 -- The operator was folded into a literal
16547
16548 elsif Nkind (N2) in N_Integer_Literal
16549 | N_Real_Literal
16550 | N_String_Literal
16551 then
16552 if Present (Original_Node (N2))
16553 and then Nkind (Original_Node (N2)) = Nkind (N)
16554 then
16555 -- Operation was constant-folded. Whenever possible,
16556 -- recover semantic information from unfolded node.
16557 -- This was initially done for ASIS but is apparently
16558 -- needed also for e.g. compiling a-nbnbin.adb.
16559
16560 Set_Associated_Node (N, Original_Node (N2));
16561
16562 if Nkind (N) = N_Op_Concat then
16563 Set_Is_Component_Left_Opnd (N,
16564 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16565 Set_Is_Component_Right_Opnd (N,
16566 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16567 end if;
16568
16569 Reset_Entity (N);
16570
16571 -- Propagate the constant folding back to the template
16572
16573 else
16574 Rewrite (N, New_Copy (N2));
16575 Set_Analyzed (N, False);
16576 end if;
16577
16578 -- The operator was folded into an enumeration literal. Retain
16579 -- the entity to avoid spurious ambiguities if it is overloaded
16580 -- at the point of instantiation or inlining.
16581
16582 elsif Nkind (N2) = N_Identifier
16583 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16584 then
16585 Rewrite (N, New_Copy (N2));
16586 Set_Analyzed (N, False);
16587 end if;
16588 end if;
16589
16590 -- Complete the operands check if node has not been constant
16591 -- folded.
16592
16593 if Nkind (N) in N_Op then
16594 Save_Entity_Descendants (N);
16595 end if;
16596 end Save_References_In_Operator;
16597
16598 -------------------------------
16599 -- Save_References_In_Pragma --
16600 -------------------------------
16601
16602 procedure Save_References_In_Pragma (Prag : Node_Id) is
16603 Context : Node_Id;
16604 Do_Save : Boolean := True;
16605
16606 begin
16607 -- Do not save global references in pragmas generated from aspects
16608 -- because the pragmas will be regenerated at instantiation time.
16609
16610 if From_Aspect_Specification (Prag) then
16611 Do_Save := False;
16612
16613 -- The capture of global references within contract-related source
16614 -- pragmas associated with generic packages, subprograms or their
16615 -- respective bodies must be delayed due to timing of annotation
16616 -- analysis. Global references are still captured in routine
16617 -- Save_Global_References_In_Contract.
16618
16619 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16620 if Is_Package_Contract_Annotation (Prag) then
16621 Context := Find_Related_Package_Or_Body (Prag);
16622 else
16623 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16624 Context := Find_Related_Declaration_Or_Body (Prag);
16625 end if;
16626
16627 -- The use of Original_Node accounts for the case when the
16628 -- related context is generic template.
16629
16630 if Requires_Delayed_Save (Original_Node (Context)) then
16631 Do_Save := False;
16632 end if;
16633 end if;
16634
16635 -- For all other cases, save all global references within the
16636 -- descendants, but skip the following semantic fields:
16637 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16638
16639 if Do_Save then
16640 Save_Global_Descendant
16641 (Union_Id (Pragma_Argument_Associations (N)));
16642 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
16643 end if;
16644 end Save_References_In_Pragma;
16645
16646 -- Start of processing for Save_References
16647
16648 begin
16649 if N = Empty then
16650 null;
16651
16652 -- Aggregates
16653
16654 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16655 Save_References_In_Aggregate (N);
16656
16657 -- Character literals, operator symbols
16658
16659 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16660 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16661
16662 -- Defining identifiers
16663
16664 elsif Nkind (N) in N_Entity then
16665 null;
16666
16667 -- Identifiers
16668
16669 elsif Nkind (N) = N_Identifier then
16670 Save_References_In_Identifier (N);
16671
16672 -- Operators
16673
16674 elsif Nkind (N) in N_Op then
16675 Save_References_In_Operator (N);
16676
16677 -- Pragmas
16678
16679 elsif Nkind (N) = N_Pragma then
16680 Save_References_In_Pragma (N);
16681
16682 else
16683 Save_References_In_Descendants (N);
16684 end if;
16685
16686 -- Save all global references found within the aspect specifications
16687 -- of the related node.
16688
16689 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16690
16691 -- The capture of global references within aspects associated with
16692 -- generic packages, subprograms or their bodies must be delayed
16693 -- due to timing of annotation analysis. Global references are
16694 -- still captured in routine Save_Global_References_In_Contract.
16695
16696 if Requires_Delayed_Save (N) then
16697 null;
16698
16699 -- Otherwise save all global references within the aspects
16700
16701 else
16702 Save_Global_References_In_Aspects (N);
16703 end if;
16704 end if;
16705 end Save_References;
16706
16707 -- Start of processing for Save_Global_References
16708
16709 begin
16710 Gen_Scope := Current_Scope;
16711
16712 -- If the generic unit is a child unit, references to entities in the
16713 -- parent are treated as local, because they will be resolved anew in
16714 -- the context of the instance of the parent.
16715
16716 while Is_Child_Unit (Gen_Scope)
16717 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16718 loop
16719 Gen_Scope := Scope (Gen_Scope);
16720 end loop;
16721
16722 Save_References (Templ);
16723 end Save_Global_References;
16724
16725 ---------------------------------------
16726 -- Save_Global_References_In_Aspects --
16727 ---------------------------------------
16728
16729 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16730 Asp : Node_Id;
16731 Expr : Node_Id;
16732
16733 begin
16734 Asp := First (Aspect_Specifications (N));
16735 while Present (Asp) loop
16736 Expr := Expression (Asp);
16737
16738 if Present (Expr) then
16739 Save_Global_References (Expr);
16740 end if;
16741
16742 Next (Asp);
16743 end loop;
16744 end Save_Global_References_In_Aspects;
16745
16746 ------------------------------------------
16747 -- Set_Copied_Sloc_For_Inherited_Pragma --
16748 ------------------------------------------
16749
16750 procedure Set_Copied_Sloc_For_Inherited_Pragma
16751 (N : Node_Id;
16752 E : Entity_Id)
16753 is
16754 begin
16755 Create_Instantiation_Source (N, E,
16756 Inlined_Body => False,
16757 Inherited_Pragma => True,
16758 Factor => S_Adjustment);
16759 end Set_Copied_Sloc_For_Inherited_Pragma;
16760
16761 --------------------------------------
16762 -- Set_Copied_Sloc_For_Inlined_Body --
16763 --------------------------------------
16764
16765 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16766 begin
16767 Create_Instantiation_Source (N, E,
16768 Inlined_Body => True,
16769 Inherited_Pragma => False,
16770 Factor => S_Adjustment);
16771 end Set_Copied_Sloc_For_Inlined_Body;
16772
16773 ---------------------
16774 -- Set_Instance_Of --
16775 ---------------------
16776
16777 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16778 begin
16779 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16780 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16781 Generic_Renamings.Increment_Last;
16782 end Set_Instance_Of;
16783
16784 --------------------
16785 -- Set_Next_Assoc --
16786 --------------------
16787
16788 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16789 begin
16790 Generic_Renamings.Table (E).Next_In_HTable := Next;
16791 end Set_Next_Assoc;
16792
16793 -------------------
16794 -- Start_Generic --
16795 -------------------
16796
16797 procedure Start_Generic is
16798 begin
16799 -- ??? More things could be factored out in this routine.
16800 -- Should probably be done at a later stage.
16801
16802 Generic_Flags.Append (Inside_A_Generic);
16803 Inside_A_Generic := True;
16804
16805 Expander_Mode_Save_And_Set (False);
16806 end Start_Generic;
16807
16808 ----------------------
16809 -- Set_Instance_Env --
16810 ----------------------
16811
16812 -- WARNING: This routine manages SPARK regions
16813
16814 procedure Set_Instance_Env
16815 (Gen_Unit : Entity_Id;
16816 Act_Unit : Entity_Id)
16817 is
16818 Saved_AE : constant Boolean := Assertions_Enabled;
16819 Saved_CPL : constant Node_Id := Check_Policy_List;
16820 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16821 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16822 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16823
16824 begin
16825 -- Regardless of the current mode, predefined units are analyzed in the
16826 -- most current Ada mode, and earlier version Ada checks do not apply
16827 -- to predefined units. Nothing needs to be done for non-internal units.
16828 -- These are always analyzed in the current mode.
16829
16830 if In_Internal_Unit (Gen_Unit) then
16831
16832 -- The following call resets all configuration attributes to default
16833 -- or the xxx_Config versions of the attributes when the current sem
16834 -- unit is the main unit. At the same time, internal units must also
16835 -- inherit certain configuration attributes from their context. It
16836 -- is unclear what these two sets are.
16837
16838 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16839
16840 -- Reinstall relevant configuration attributes of the context
16841
16842 Assertions_Enabled := Saved_AE;
16843 Check_Policy_List := Saved_CPL;
16844 Dynamic_Elaboration_Checks := Saved_DEC;
16845
16846 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16847 end if;
16848
16849 Current_Instantiated_Parent :=
16850 (Gen_Id => Gen_Unit,
16851 Act_Id => Act_Unit,
16852 Next_In_HTable => Assoc_Null);
16853 end Set_Instance_Env;
16854
16855 -----------------
16856 -- Switch_View --
16857 -----------------
16858
16859 procedure Switch_View (T : Entity_Id) is
16860 BT : constant Entity_Id := Base_Type (T);
16861 Priv_Elmt : Elmt_Id := No_Elmt;
16862 Priv_Sub : Entity_Id;
16863
16864 begin
16865 -- T may be private but its base type may have been exchanged through
16866 -- some other occurrence, in which case there is nothing to switch
16867 -- besides T itself. Note that a private dependent subtype of a private
16868 -- type might not have been switched even if the base type has been,
16869 -- because of the last branch of Check_Private_View (see comment there).
16870
16871 if not Is_Private_Type (BT) then
16872 Prepend_Elmt (Full_View (T), Exchanged_Views);
16873 Exchange_Declarations (T);
16874 return;
16875 end if;
16876
16877 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16878
16879 if Present (Full_View (BT)) then
16880 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16881 Exchange_Declarations (BT);
16882 end if;
16883
16884 while Present (Priv_Elmt) loop
16885 Priv_Sub := Node (Priv_Elmt);
16886
16887 if Present (Full_View (Priv_Sub)) then
16888 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16889 Exchange_Declarations (Priv_Sub);
16890 end if;
16891
16892 Next_Elmt (Priv_Elmt);
16893 end loop;
16894 end Switch_View;
16895
16896 -----------------
16897 -- True_Parent --
16898 -----------------
16899
16900 function True_Parent (N : Node_Id) return Node_Id is
16901 begin
16902 if Nkind (Parent (N)) = N_Subunit then
16903 return Parent (Corresponding_Stub (Parent (N)));
16904 else
16905 return Parent (N);
16906 end if;
16907 end True_Parent;
16908
16909 -----------------------------
16910 -- Valid_Default_Attribute --
16911 -----------------------------
16912
16913 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16914 Attr_Id : constant Attribute_Id :=
16915 Get_Attribute_Id (Attribute_Name (Def));
16916 T : constant Entity_Id := Entity (Prefix (Def));
16917 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16918 F : Entity_Id;
16919 Num_F : Nat;
16920 OK : Boolean;
16921
16922 begin
16923 if No (T) or else T = Any_Id then
16924 return;
16925 end if;
16926
16927 Num_F := 0;
16928 F := First_Formal (Nam);
16929 while Present (F) loop
16930 Num_F := Num_F + 1;
16931 Next_Formal (F);
16932 end loop;
16933
16934 case Attr_Id is
16935 when Attribute_Adjacent
16936 | Attribute_Ceiling
16937 | Attribute_Copy_Sign
16938 | Attribute_Floor
16939 | Attribute_Fraction
16940 | Attribute_Machine
16941 | Attribute_Model
16942 | Attribute_Remainder
16943 | Attribute_Rounding
16944 | Attribute_Unbiased_Rounding
16945 =>
16946 OK := Is_Fun
16947 and then Num_F = 1
16948 and then Is_Floating_Point_Type (T);
16949
16950 when Attribute_Image
16951 | Attribute_Pred
16952 | Attribute_Succ
16953 | Attribute_Value
16954 | Attribute_Wide_Image
16955 | Attribute_Wide_Value
16956 =>
16957 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16958
16959 when Attribute_Max
16960 | Attribute_Min
16961 =>
16962 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16963
16964 when Attribute_Input =>
16965 OK := (Is_Fun and then Num_F = 1);
16966
16967 when Attribute_Output
16968 | Attribute_Put_Image
16969 | Attribute_Read
16970 | Attribute_Write
16971 =>
16972 OK := not Is_Fun and then Num_F = 2;
16973
16974 when others =>
16975 OK := False;
16976 end case;
16977
16978 if not OK then
16979 Error_Msg_N
16980 ("attribute reference has wrong profile for subprogram", Def);
16981 end if;
16982 end Valid_Default_Attribute;
16983
16984 end Sem_Ch12;
This page took 0.821979 seconds and 5 git commands to generate.