[Ada] Reuse In_Same_List where possible
[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-2020, 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 Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Ghost; use Ghost;
37 with Itypes; use Itypes;
38 with Lib; use Lib;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Nlists; use Nlists;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Rident; use Rident;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinfo.CN; use Sinfo.CN;
70 with Sinput; use Sinput;
71 with Sinput.L; use Sinput.L;
72 with Snames; use Snames;
73 with Stringt; use Stringt;
74 with Uname; use Uname;
75 with Table;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
80
81 with GNAT.HTable;
82
83 package body Sem_Ch12 is
84
85 ----------------------------------------------------------
86 -- Implementation of Generic Analysis and Instantiation --
87 ----------------------------------------------------------
88
89 -- GNAT implements generics by macro expansion. No attempt is made to share
90 -- generic instantiations (for now). Analysis of a generic definition does
91 -- not perform any expansion action, but the expander must be called on the
92 -- tree for each instantiation, because the expansion may of course depend
93 -- on the generic actuals. All of this is best achieved as follows:
94 --
95 -- a) Semantic analysis of a generic unit is performed on a copy of the
96 -- tree for the generic unit. All tree modifications that follow analysis
97 -- do not affect the original tree. Links are kept between the original
98 -- tree and the copy, in order to recognize non-local references within
99 -- the generic, and propagate them to each instance (recall that name
100 -- resolution is done on the generic declaration: generics are not really
101 -- macros). This is summarized in the following diagram:
102
103 -- .-----------. .----------.
104 -- | semantic |<--------------| generic |
105 -- | copy | | unit |
106 -- | |==============>| |
107 -- |___________| global |__________|
108 -- references | | |
109 -- | | |
110 -- .-----|--|.
111 -- | .-----|---.
112 -- | | .----------.
113 -- | | | generic |
114 -- |__| | |
115 -- |__| instance |
116 -- |__________|
117
118 -- b) Each instantiation copies the original tree, and inserts into it a
119 -- series of declarations that describe the mapping between generic formals
120 -- and actuals. For example, a generic In OUT parameter is an object
121 -- renaming of the corresponding actual, etc. Generic IN parameters are
122 -- constant declarations.
123
124 -- c) In order to give the right visibility for these renamings, we use
125 -- a different scheme for package and subprogram instantiations. For
126 -- packages, the list of renamings is inserted into the package
127 -- specification, before the visible declarations of the package. The
128 -- renamings are analyzed before any of the text of the instance, and are
129 -- thus visible at the right place. Furthermore, outside of the instance,
130 -- the generic parameters are visible and denote their corresponding
131 -- actuals.
132
133 -- For subprograms, we create a container package to hold the renamings
134 -- and the subprogram instance itself. Analysis of the package makes the
135 -- renaming declarations visible to the subprogram. After analyzing the
136 -- package, the defining entity for the subprogram is touched-up so that
137 -- it appears declared in the current scope, and not inside the container
138 -- package.
139
140 -- If the instantiation is a compilation unit, the container package is
141 -- given the same name as the subprogram instance. This ensures that
142 -- the elaboration procedure called by the binder, using the compilation
143 -- unit name, calls in fact the elaboration procedure for the package.
144
145 -- Not surprisingly, private types complicate this approach. By saving in
146 -- the original generic object the non-local references, we guarantee that
147 -- the proper entities are referenced at the point of instantiation.
148 -- However, for private types, this by itself does not insure that the
149 -- proper VIEW of the entity is used (the full type may be visible at the
150 -- point of generic definition, but not at instantiation, or vice-versa).
151 -- In order to reference the proper view, we special-case any reference
152 -- to private types in the generic object, by saving both views, one in
153 -- the generic and one in the semantic copy. At time of instantiation, we
154 -- check whether the two views are consistent, and exchange declarations if
155 -- necessary, in order to restore the correct visibility. Similarly, if
156 -- the instance view is private when the generic view was not, we perform
157 -- the exchange. After completing the instantiation, we restore the
158 -- current visibility. The flag Has_Private_View marks identifiers in the
159 -- the generic unit that require checking.
160
161 -- Visibility within nested generic units requires special handling.
162 -- Consider the following scheme:
163
164 -- type Global is ... -- outside of generic unit.
165 -- generic ...
166 -- package Outer is
167 -- ...
168 -- type Semi_Global is ... -- global to inner.
169
170 -- generic ... -- 1
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
172
173 -- procedure in2 is new inner (...); -- 4
174 -- end Outer;
175
176 -- package New_Outer is new Outer (...); -- 2
177 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
178
179 -- The semantic analysis of Outer captures all occurrences of Global.
180 -- The semantic analysis of Inner (at 1) captures both occurrences of
181 -- Global and Semi_Global.
182
183 -- At point 2 (instantiation of Outer), we also produce a generic copy
184 -- of Inner, even though Inner is, at that point, not being instantiated.
185 -- (This is just part of the semantic analysis of New_Outer).
186
187 -- Critically, references to Global within Inner must be preserved, while
188 -- references to Semi_Global should not preserved, because they must now
189 -- resolve to an entity within New_Outer. To distinguish between these, we
190 -- use a global variable, Current_Instantiated_Parent, which is set when
191 -- performing a generic copy during instantiation (at 2). This variable is
192 -- used when performing a generic copy that is not an instantiation, but
193 -- that is nested within one, as the occurrence of 1 within 2. The analysis
194 -- of a nested generic only preserves references that are global to the
195 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
196 -- determine whether a reference is external to the given parent.
197
198 -- The instantiation at point 3 requires no special treatment. The method
199 -- works as well for further nestings of generic units, but of course the
200 -- variable Current_Instantiated_Parent must be stacked because nested
201 -- instantiations can occur, e.g. the occurrence of 4 within 2.
202
203 -- The instantiation of package and subprogram bodies is handled in a
204 -- similar manner, except that it is delayed until after semantic
205 -- analysis is complete. In this fashion complex cross-dependencies
206 -- between several package declarations and bodies containing generics
207 -- can be compiled which otherwise would diagnose spurious circularities.
208
209 -- For example, it is possible to compile two packages A and B that
210 -- have the following structure:
211
212 -- package A is package B is
213 -- generic ... generic ...
214 -- package G_A is package G_B is
215
216 -- with B; with A;
217 -- package body A is package body B is
218 -- package N_B is new G_B (..) package N_A is new G_A (..)
219
220 -- The table Pending_Instantiations in package Inline is used to keep
221 -- track of body instantiations that are delayed in this manner. Inline
222 -- handles the actual calls to do the body instantiations. This activity
223 -- is part of Inline, since the processing occurs at the same point, and
224 -- for essentially the same reason, as the handling of inlined routines.
225
226 ----------------------------------------------
227 -- Detection of Instantiation Circularities --
228 ----------------------------------------------
229
230 -- If we have a chain of instantiations that is circular, this is static
231 -- error which must be detected at compile time. The detection of these
232 -- circularities is carried out at the point that we insert a generic
233 -- instance spec or body. If there is a circularity, then the analysis of
234 -- the offending spec or body will eventually result in trying to load the
235 -- same unit again, and we detect this problem as we analyze the package
236 -- instantiation for the second time.
237
238 -- At least in some cases after we have detected the circularity, we get
239 -- into trouble if we try to keep going. The following flag is set if a
240 -- circularity is detected, and used to abandon compilation after the
241 -- messages have been posted.
242
243 Circularity_Detected : Boolean := False;
244 -- It should really be reset upon encountering a new main unit, but in
245 -- practice we do not use multiple main units so this is not critical.
246
247 -----------------------------------------
248 -- Implementation of Generic Contracts --
249 -----------------------------------------
250
251 -- A "contract" is a collection of aspects and pragmas that either verify a
252 -- property of a construct at runtime or classify the data flow to and from
253 -- the construct in some fashion.
254
255 -- Generic packages, subprograms and their respective bodies may be subject
256 -- to the following contract-related aspects or pragmas collectively known
257 -- as annotations:
258
259 -- package subprogram [body]
260 -- Abstract_State Contract_Cases
261 -- Initial_Condition Depends
262 -- Initializes Extensions_Visible
263 -- Global
264 -- package body Post
265 -- Refined_State Post_Class
266 -- Postcondition
267 -- Pre
268 -- Pre_Class
269 -- Precondition
270 -- Refined_Depends
271 -- Refined_Global
272 -- Refined_Post
273 -- Subprogram_Variant
274 -- Test_Case
275
276 -- Most package contract annotations utilize forward references to classify
277 -- data declared within the package [body]. Subprogram annotations then use
278 -- the classifications to further refine them. These inter dependencies are
279 -- problematic with respect to the implementation of generics because their
280 -- analysis, capture of global references and instantiation does not mesh
281 -- well with the existing mechanism.
282
283 -- 1) Analysis of generic contracts is carried out the same way non-generic
284 -- contracts are analyzed:
285
286 -- 1.1) General rule - a contract is analyzed after all related aspects
287 -- and pragmas are analyzed. This is done by routines
288
289 -- Analyze_Package_Body_Contract
290 -- Analyze_Package_Contract
291 -- Analyze_Subprogram_Body_Contract
292 -- Analyze_Subprogram_Contract
293
294 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
295 -- are processed.
296
297 -- 1.3) Compilation unit body - the contract is analyzed at the end of
298 -- the body declaration list.
299
300 -- 1.4) Package - the contract is analyzed at the end of the private or
301 -- visible declarations, prior to analyzing the contracts of any nested
302 -- packages or subprograms.
303
304 -- 1.5) Package body - the contract is analyzed at the end of the body
305 -- declaration list, prior to analyzing the contracts of any nested
306 -- packages or subprograms.
307
308 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
309 -- package or a subprogram, then its contract is analyzed at the end of
310 -- the enclosing declarations, otherwise the subprogram is a compilation
311 -- unit 1.2).
312
313 -- 1.7) Subprogram body - if the subprogram body is declared inside a
314 -- block, a package body or a subprogram body, then its contract is
315 -- analyzed at the end of the enclosing declarations, otherwise the
316 -- subprogram is a compilation unit 1.3).
317
318 -- 2) Capture of global references within contracts is done after capturing
319 -- global references within the generic template. There are two reasons for
320 -- this delay - pragma annotations are not part of the generic template in
321 -- the case of a generic subprogram declaration, and analysis of contracts
322 -- is delayed.
323
324 -- Contract-related source pragmas within generic templates are prepared
325 -- for delayed capture of global references by routine
326
327 -- Create_Generic_Contract
328
329 -- The routine associates these pragmas with the contract of the template.
330 -- In the case of a generic subprogram declaration, the routine creates
331 -- generic templates for the pragmas declared after the subprogram because
332 -- they are not part of the template.
333
334 -- generic -- template starts
335 -- procedure Gen_Proc (Input : Integer); -- template ends
336 -- pragma Precondition (Input > 0); -- requires own template
337
338 -- 2.1) The capture of global references with aspect specifications and
339 -- source pragmas that apply to a generic unit must be suppressed when
340 -- the generic template is being processed because the contracts have not
341 -- been analyzed yet. Any attempts to capture global references at that
342 -- point will destroy the Associated_Node linkages and leave the template
343 -- undecorated. This delay is controlled by routine
344
345 -- Requires_Delayed_Save
346
347 -- 2.2) The real capture of global references within a contract is done
348 -- after the contract has been analyzed, by routine
349
350 -- Save_Global_References_In_Contract
351
352 -- 3) The instantiation of a generic contract occurs as part of the
353 -- instantiation of the contract owner. Generic subprogram declarations
354 -- require additional processing when the contract is specified by pragmas
355 -- because the pragmas are not part of the generic template. This is done
356 -- by routine
357
358 -- Instantiate_Subprogram_Contract
359
360 --------------------------------------------------
361 -- Formal packages and partial parameterization --
362 --------------------------------------------------
363
364 -- When compiling a generic, a formal package is a local instantiation. If
365 -- declared with a box, its generic formals are visible in the enclosing
366 -- generic. If declared with a partial list of actuals, those actuals that
367 -- are defaulted (covered by an Others clause, or given an explicit box
368 -- initialization) are also visible in the enclosing generic, while those
369 -- that have a corresponding actual are not.
370
371 -- In our source model of instantiation, the same visibility must be
372 -- present in the spec and body of an instance: the names of the formals
373 -- that are defaulted must be made visible within the instance, and made
374 -- invisible (hidden) after the instantiation is complete, so that they
375 -- are not accessible outside of the instance.
376
377 -- In a generic, a formal package is treated like a special instantiation.
378 -- Our Ada 95 compiler handled formals with and without box in different
379 -- ways. With partial parameterization, we use a single model for both.
380 -- We create a package declaration that consists of the specification of
381 -- the generic package, and a set of declarations that map the actuals
382 -- into local renamings, just as we do for bona fide instantiations. For
383 -- defaulted parameters and formals with a box, we copy directly the
384 -- declarations of the formals into this local package. The result is a
385 -- package whose visible declarations may include generic formals. This
386 -- package is only used for type checking and visibility analysis, and
387 -- never reaches the back end, so it can freely violate the placement
388 -- rules for generic formal declarations.
389
390 -- The list of declarations (renamings and copies of formals) is built
391 -- by Analyze_Associations, just as for regular instantiations.
392
393 -- At the point of instantiation, conformance checking must be applied only
394 -- to those parameters that were specified in the formals. We perform this
395 -- checking by creating another internal instantiation, this one including
396 -- only the renamings and the formals (the rest of the package spec is not
397 -- relevant to conformance checking). We can then traverse two lists: the
398 -- list of actuals in the instance that corresponds to the formal package,
399 -- and the list of actuals produced for this bogus instantiation. We apply
400 -- the conformance rules to those actuals that are not defaulted, i.e.
401 -- which still appear as generic formals.
402
403 -- When we compile an instance body we must make the right parameters
404 -- visible again. The predicate Is_Generic_Formal indicates which of the
405 -- formals should have its Is_Hidden flag reset.
406
407 -----------------------
408 -- Local subprograms --
409 -----------------------
410
411 procedure Abandon_Instantiation (N : Node_Id);
412 pragma No_Return (Abandon_Instantiation);
413 -- Posts an error message "instantiation abandoned" at the indicated node
414 -- and then raises the exception Instantiation_Error to do it.
415
416 procedure Analyze_Formal_Array_Type
417 (T : in out Entity_Id;
418 Def : Node_Id);
419 -- A formal array type is treated like an array type declaration, and
420 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
421 -- in-out, because in the case of an anonymous type the entity is
422 -- actually created in the procedure.
423
424 -- The following procedures treat other kinds of formal parameters
425
426 procedure Analyze_Formal_Derived_Interface_Type
427 (N : Node_Id;
428 T : Entity_Id;
429 Def : Node_Id);
430
431 procedure Analyze_Formal_Derived_Type
432 (N : Node_Id;
433 T : Entity_Id;
434 Def : Node_Id);
435
436 procedure Analyze_Formal_Interface_Type
437 (N : Node_Id;
438 T : Entity_Id;
439 Def : Node_Id);
440
441 -- The following subprograms create abbreviated declarations for formal
442 -- scalar types. We introduce an anonymous base of the proper class for
443 -- each of them, and define the formals as constrained first subtypes of
444 -- their bases. The bounds are expressions that are non-static in the
445 -- generic.
446
447 procedure Analyze_Formal_Decimal_Fixed_Point_Type
448 (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
453 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
454 (T : Entity_Id; Def : Node_Id);
455
456 procedure Analyze_Formal_Private_Type
457 (N : Node_Id;
458 T : Entity_Id;
459 Def : Node_Id);
460 -- Creates a new private type, which does not require completion
461
462 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
463 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
464
465 procedure Analyze_Generic_Formal_Part (N : Node_Id);
466 -- Analyze generic formal part
467
468 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
469 -- Create a new access type with the given designated type
470
471 function Analyze_Associations
472 (I_Node : Node_Id;
473 Formals : List_Id;
474 F_Copy : List_Id) return List_Id;
475 -- At instantiation time, build the list of associations between formals
476 -- and actuals. Each association becomes a renaming declaration for the
477 -- formal entity. F_Copy is the analyzed list of formals in the generic
478 -- copy. It is used to apply legality checks to the actuals. I_Node is the
479 -- instantiation node itself.
480
481 procedure Analyze_Subprogram_Instantiation
482 (N : Node_Id;
483 K : Entity_Kind);
484
485 procedure Build_Instance_Compilation_Unit_Nodes
486 (N : Node_Id;
487 Act_Body : Node_Id;
488 Act_Decl : Node_Id);
489 -- This procedure is used in the case where the generic instance of a
490 -- subprogram body or package body is a library unit. In this case, the
491 -- original library unit node for the generic instantiation must be
492 -- replaced by the resulting generic body, and a link made to a new
493 -- compilation unit node for the generic declaration. The argument N is
494 -- the original generic instantiation. Act_Body and Act_Decl are the body
495 -- and declaration of the instance (either package body and declaration
496 -- nodes or subprogram body and declaration nodes depending on the case).
497 -- On return, the node N has been rewritten with the actual body.
498
499 function Build_Subprogram_Decl_Wrapper
500 (Formal_Subp : Entity_Id) return Node_Id;
501 -- Ada 2020 allows formal subprograms to carry pre/postconditions.
502 -- At the point of instantiation these contracts apply to uses of
503 -- the actual subprogram. This is implemented by creating wrapper
504 -- subprograms instead of the renamings previously used to link
505 -- formal subprograms and the corresponding actuals. If the actual
506 -- is not an entity (e.g. an attribute reference) a renaming is
507 -- created to handle the expansion of the attribute.
508
509 function Build_Subprogram_Body_Wrapper
510 (Formal_Subp : Entity_Id;
511 Actual_Name : Node_Id) return Node_Id;
512 -- The body of the wrapper is a call to the actual, with the generated
513 -- pre/postconditon checks added.
514
515 procedure Check_Access_Definition (N : Node_Id);
516 -- Subsidiary routine to null exclusion processing. Perform an assertion
517 -- check on Ada version and the presence of an access definition in N.
518
519 procedure Check_Formal_Packages (P_Id : Entity_Id);
520 -- Apply the following to all formal packages in generic associations.
521 -- Restore the visibility of the formals of the instance that are not
522 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
523 -- created for formal instances that are not defaulted.
524
525 procedure Check_Formal_Package_Instance
526 (Formal_Pack : Entity_Id;
527 Actual_Pack : Entity_Id);
528 -- Verify that the actuals of the actual instance match the actuals of
529 -- the template for a formal package that is not declared with a box.
530
531 procedure Check_Forward_Instantiation (Decl : Node_Id);
532 -- If the generic is a local entity and the corresponding body has not
533 -- been seen yet, flag enclosing packages to indicate that it will be
534 -- elaborated after the generic body. Subprograms declared in the same
535 -- package cannot be inlined by the front end because front-end inlining
536 -- requires a strict linear order of elaboration.
537
538 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
539 -- Check if some association between formals and actuals requires to make
540 -- visible primitives of a tagged type, and make those primitives visible.
541 -- Return the list of primitives whose visibility is modified (to restore
542 -- their visibility later through Restore_Hidden_Primitives). If no
543 -- candidate is found then return No_Elist.
544
545 procedure Check_Hidden_Child_Unit
546 (N : Node_Id;
547 Gen_Unit : Entity_Id;
548 Act_Decl_Id : Entity_Id);
549 -- If the generic unit is an implicit child instance within a parent
550 -- instance, we need to make an explicit test that it is not hidden by
551 -- a child instance of the same name and parent.
552
553 procedure Check_Generic_Actuals
554 (Instance : Entity_Id;
555 Is_Formal_Box : Boolean);
556 -- Similar to previous one. Check the actuals in the instantiation,
557 -- whose views can change between the point of instantiation and the point
558 -- of instantiation of the body. In addition, mark the generic renamings
559 -- as generic actuals, so that they are not compatible with other actuals.
560 -- Recurse on an actual that is a formal package whose declaration has
561 -- a box.
562
563 function Contains_Instance_Of
564 (Inner : Entity_Id;
565 Outer : Entity_Id;
566 N : Node_Id) return Boolean;
567 -- Inner is instantiated within the generic Outer. Check whether Inner
568 -- directly or indirectly contains an instance of Outer or of one of its
569 -- parents, in the case of a subunit. Each generic unit holds a list of
570 -- the entities instantiated within (at any depth). This procedure
571 -- determines whether the set of such lists contains a cycle, i.e. an
572 -- illegal circular instantiation.
573
574 function Denotes_Formal_Package
575 (Pack : Entity_Id;
576 On_Exit : Boolean := False;
577 Instance : Entity_Id := Empty) return Boolean;
578 -- Returns True if E is a formal package of an enclosing generic, or
579 -- the actual for such a formal in an enclosing instantiation. If such
580 -- a package is used as a formal in an nested generic, or as an actual
581 -- in a nested instantiation, the visibility of ITS formals should not
582 -- be modified. When called from within Restore_Private_Views, the flag
583 -- On_Exit is true, to indicate that the search for a possible enclosing
584 -- instance should ignore the current one. In that case Instance denotes
585 -- the declaration for which this is an actual. This declaration may be
586 -- an instantiation in the source, or the internal instantiation that
587 -- corresponds to the actual for a formal package.
588
589 function Earlier (N1, N2 : Node_Id) return Boolean;
590 -- Yields True if N1 and N2 appear in the same compilation unit,
591 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
592 -- traversal of the tree for the unit. Used to determine the placement
593 -- of freeze nodes for instance bodies that may depend on other instances.
594
595 function Find_Actual_Type
596 (Typ : Entity_Id;
597 Gen_Type : Entity_Id) return Entity_Id;
598 -- When validating the actual types of a child instance, check whether
599 -- the formal is a formal type of the parent unit, and retrieve the current
600 -- actual for it. Typ is the entity in the analyzed formal type declaration
601 -- (component or index type of an array type, or designated type of an
602 -- access formal) and Gen_Type is the enclosing analyzed formal array
603 -- or access type. The desired actual may be a formal of a parent, or may
604 -- be declared in a formal package of a parent. In both cases it is a
605 -- generic actual type because it appears within a visible instance.
606 -- Finally, it may be declared in a parent unit without being a formal
607 -- of that unit, in which case it must be retrieved by visibility.
608 -- Ambiguities may still arise if two homonyms are declared in two formal
609 -- packages, and the prefix of the formal type may be needed to resolve
610 -- the ambiguity in the instance ???
611
612 procedure Freeze_Subprogram_Body
613 (Inst_Node : Node_Id;
614 Gen_Body : Node_Id;
615 Pack_Id : Entity_Id);
616 -- The generic body may appear textually after the instance, including
617 -- in the proper body of a stub, or within a different package instance.
618 -- Given that the instance can only be elaborated after the generic, we
619 -- place freeze_nodes for the instance and/or for packages that may enclose
620 -- the instance and the generic, so that the back-end can establish the
621 -- proper order of elaboration.
622
623 function Get_Associated_Node (N : Node_Id) return Node_Id;
624 -- In order to propagate semantic information back from the analyzed copy
625 -- to the original generic, we maintain links between selected nodes in the
626 -- generic and their corresponding copies. At the end of generic analysis,
627 -- the routine Save_Global_References traverses the generic tree, examines
628 -- the semantic information, and preserves the links to those nodes that
629 -- contain global information. At instantiation, the information from the
630 -- associated node is placed on the new copy, so that name resolution is
631 -- not repeated.
632 --
633 -- Three kinds of source nodes have associated nodes:
634 --
635 -- a) those that can reference (denote) entities, that is identifiers,
636 -- character literals, expanded_names, operator symbols, operators,
637 -- and attribute reference nodes. These nodes have an Entity field
638 -- and are the set of nodes that are in N_Has_Entity.
639 --
640 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
641 --
642 -- c) selected components (N_Selected_Component)
643 --
644 -- For the first class, the associated node preserves the entity if it is
645 -- global. If the generic contains nested instantiations, the associated
646 -- node itself has been recopied, and a chain of them must be followed.
647 --
648 -- For aggregates, the associated node allows retrieval of the type, which
649 -- may otherwise not appear in the generic. The view of this type may be
650 -- different between generic and instantiation, and the full view can be
651 -- installed before the instantiation is analyzed. For aggregates of type
652 -- extensions, the same view exchange may have to be performed for some of
653 -- the ancestor types, if their view is private at the point of
654 -- instantiation.
655 --
656 -- Nodes that are selected components in the parse tree may be rewritten
657 -- as expanded names after resolution, and must be treated as potential
658 -- entity holders, which is why they also have an Associated_Node.
659 --
660 -- Nodes that do not come from source, such as freeze nodes, do not appear
661 -- in the generic tree, and need not have an associated node.
662 --
663 -- The associated node is stored in the Associated_Node field. Note that
664 -- this field overlaps Entity, which is fine, because the whole point is
665 -- that we don't need or want the normal Entity field in this situation.
666
667 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
668 -- Traverse the Exchanged_Views list to see if a type was private
669 -- and has already been flipped during this phase of instantiation.
670
671 function Has_Contracts (Decl : Node_Id) return Boolean;
672 -- Determine whether a formal subprogram has a Pre- or Postcondition,
673 -- in which case a subprogram wrapper has to be built for the actual.
674
675 procedure Hide_Current_Scope;
676 -- When instantiating a generic child unit, the parent context must be
677 -- present, but the instance and all entities that may be generated
678 -- must be inserted in the current scope. We leave the current scope
679 -- on the stack, but make its entities invisible to avoid visibility
680 -- problems. This is reversed at the end of the instantiation. This is
681 -- not done for the instantiation of the bodies, which only require the
682 -- instances of the generic parents to be in scope.
683
684 function In_Main_Context (E : Entity_Id) return Boolean;
685 -- Check whether an instantiation is in the context of the main unit.
686 -- Used to determine whether its body should be elaborated to allow
687 -- front-end inlining.
688
689 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
690 -- Add the context clause of the unit containing a generic unit to a
691 -- compilation unit that is, or contains, an instantiation.
692
693 procedure Init_Env;
694 -- Establish environment for subsequent instantiation. Separated from
695 -- Save_Env because data-structures for visibility handling must be
696 -- initialized before call to Check_Generic_Child_Unit.
697
698 procedure Inline_Instance_Body
699 (N : Node_Id;
700 Gen_Unit : Entity_Id;
701 Act_Decl : Node_Id);
702 -- If front-end inlining is requested, instantiate the package body,
703 -- and preserve the visibility of its compilation unit, to insure
704 -- that successive instantiations succeed.
705
706 procedure Insert_Freeze_Node_For_Instance
707 (N : Node_Id;
708 F_Node : Node_Id);
709 -- N denotes a package or a subprogram instantiation and F_Node is the
710 -- associated freeze node. Insert the freeze node before the first source
711 -- body which follows immediately after N. If no such body is found, the
712 -- freeze node is inserted at the end of the declarative region which
713 -- contains N.
714
715 procedure Install_Body
716 (Act_Body : Node_Id;
717 N : Node_Id;
718 Gen_Body : Node_Id;
719 Gen_Decl : Node_Id);
720 -- If the instantiation happens textually before the body of the generic,
721 -- the instantiation of the body must be analyzed after the generic body,
722 -- and not at the point of instantiation. Such early instantiations can
723 -- happen if the generic and the instance appear in a package declaration
724 -- because the generic body can only appear in the corresponding package
725 -- body. Early instantiations can also appear if generic, instance and
726 -- body are all in the declarative part of a subprogram or entry. Entities
727 -- of packages that are early instantiations are delayed, and their freeze
728 -- node appears after the generic body. This rather complex machinery is
729 -- needed when nested instantiations are present, because the source does
730 -- not carry any indication of where the corresponding instance bodies must
731 -- be installed and frozen.
732
733 procedure Install_Formal_Packages (Par : Entity_Id);
734 -- Install the visible part of any formal of the parent that is a formal
735 -- package. Note that for the case of a formal package with a box, this
736 -- includes the formal part of the formal package (12.7(10/2)).
737
738 procedure Install_Hidden_Primitives
739 (Prims_List : in out Elist_Id;
740 Gen_T : Entity_Id;
741 Act_T : Entity_Id);
742 -- Remove suffix 'P' from hidden primitives of Act_T to match the
743 -- visibility of primitives of Gen_T. The list of primitives to which
744 -- the suffix is removed is added to Prims_List to restore them later.
745
746 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
747 -- When compiling an instance of a child unit the parent (which is
748 -- itself an instance) is an enclosing scope that must be made
749 -- immediately visible. This procedure is also used to install the non-
750 -- generic parent of a generic child unit when compiling its body, so
751 -- that full views of types in the parent are made visible.
752
753 -- The functions Instantiate_XXX perform various legality checks and build
754 -- the declarations for instantiated generic parameters. In all of these
755 -- Formal is the entity in the generic unit, Actual is the entity of
756 -- expression in the generic associations, and Analyzed_Formal is the
757 -- formal in the generic copy, which contains the semantic information to
758 -- be used to validate the actual.
759
760 function Instantiate_Object
761 (Formal : Node_Id;
762 Actual : Node_Id;
763 Analyzed_Formal : Node_Id) return List_Id;
764
765 function Instantiate_Type
766 (Formal : Node_Id;
767 Actual : Node_Id;
768 Analyzed_Formal : Node_Id;
769 Actual_Decls : List_Id) return List_Id;
770
771 function Instantiate_Formal_Subprogram
772 (Formal : Node_Id;
773 Actual : Node_Id;
774 Analyzed_Formal : Node_Id) return Node_Id;
775
776 function Instantiate_Formal_Package
777 (Formal : Node_Id;
778 Actual : Node_Id;
779 Analyzed_Formal : Node_Id) return List_Id;
780 -- If the formal package is declared with a box, special visibility rules
781 -- apply to its formals: they are in the visible part of the package. This
782 -- is true in the declarative region of the formal package, that is to say
783 -- in the enclosing generic or instantiation. For an instantiation, the
784 -- parameters of the formal package are made visible in an explicit step.
785 -- Furthermore, if the actual has a visible USE clause, these formals must
786 -- be made potentially use-visible as well. On exit from the enclosing
787 -- instantiation, the reverse must be done.
788
789 -- For a formal package declared without a box, there are conformance rules
790 -- that apply to the actuals in the generic declaration and the actuals of
791 -- the actual package in the enclosing instantiation. The simplest way to
792 -- apply these rules is to repeat the instantiation of the formal package
793 -- in the context of the enclosing instance, and compare the generic
794 -- associations of this instantiation with those of the actual package.
795 -- This internal instantiation only needs to contain the renamings of the
796 -- formals: the visible and private declarations themselves need not be
797 -- created.
798
799 -- In Ada 2005, the formal package may be only partially parameterized.
800 -- In that case the visibility step must make visible those actuals whose
801 -- corresponding formals were given with a box. A final complication
802 -- involves inherited operations from formal derived types, which must
803 -- be visible if the type is.
804
805 function Is_In_Main_Unit (N : Node_Id) return Boolean;
806 -- Test if given node is in the main unit
807
808 procedure Load_Parent_Of_Generic
809 (N : Node_Id;
810 Spec : Node_Id;
811 Body_Optional : Boolean := False);
812 -- If the generic appears in a separate non-generic library unit, load the
813 -- corresponding body to retrieve the body of the generic. N is the node
814 -- for the generic instantiation, Spec is the generic package declaration.
815 --
816 -- Body_Optional is a flag that indicates that the body is being loaded to
817 -- ensure that temporaries are generated consistently when there are other
818 -- instances in the current declarative part that precede the one being
819 -- loaded. In that case a missing body is acceptable.
820
821 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
822 -- Within the generic part, entities in the formal package are
823 -- visible. To validate subsequent type declarations, indicate
824 -- the correspondence between the entities in the analyzed formal,
825 -- and the entities in the actual package. There are three packages
826 -- involved in the instantiation of a formal package: the parent
827 -- generic P1 which appears in the generic declaration, the fake
828 -- instantiation P2 which appears in the analyzed generic, and whose
829 -- visible entities may be used in subsequent formals, and the actual
830 -- P3 in the instance. To validate subsequent formals, me indicate
831 -- that the entities in P2 are mapped into those of P3. The mapping of
832 -- entities has to be done recursively for nested packages.
833
834 procedure Move_Freeze_Nodes
835 (Out_Of : Entity_Id;
836 After : Node_Id;
837 L : List_Id);
838 -- Freeze nodes can be generated in the analysis of a generic unit, but
839 -- will not be seen by the back-end. It is necessary to move those nodes
840 -- to the enclosing scope if they freeze an outer entity. We place them
841 -- at the end of the enclosing generic package, which is semantically
842 -- neutral.
843
844 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
845 -- Analyze actuals to perform name resolution. Full resolution is done
846 -- later, when the expected types are known, but names have to be captured
847 -- before installing parents of generics, that are not visible for the
848 -- actuals themselves.
849 --
850 -- If Inst is present, it is the entity of the package instance. This
851 -- entity is marked as having a limited_view actual when some actual is
852 -- a limited view. This is used to place the instance body properly.
853
854 procedure Provide_Completing_Bodies (N : Node_Id);
855 -- Generate completing bodies for all subprograms found within package or
856 -- subprogram declaration N.
857
858 procedure Remove_Parent (In_Body : Boolean := False);
859 -- Reverse effect after instantiation of child is complete
860
861 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
862 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
863 -- set to No_Elist.
864
865 procedure Set_Instance_Env
866 (Gen_Unit : Entity_Id;
867 Act_Unit : Entity_Id);
868 -- Save current instance on saved environment, to be used to determine
869 -- the global status of entities in nested instances. Part of Save_Env.
870 -- called after verifying that the generic unit is legal for the instance,
871 -- The procedure also examines whether the generic unit is a predefined
872 -- unit, in order to set configuration switches accordingly. As a result
873 -- the procedure must be called after analyzing and freezing the actuals.
874
875 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
876 -- Associate analyzed generic parameter with corresponding instance. Used
877 -- for semantic checks at instantiation time.
878
879 function True_Parent (N : Node_Id) return Node_Id;
880 -- For a subunit, return parent of corresponding stub, else return
881 -- parent of node.
882
883 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
884 -- Verify that an attribute that appears as the default for a formal
885 -- subprogram is a function or procedure with the correct profile.
886
887 -------------------------------------------
888 -- Data Structures for Generic Renamings --
889 -------------------------------------------
890
891 -- The map Generic_Renamings associates generic entities with their
892 -- corresponding actuals. Currently used to validate type instances. It
893 -- will eventually be used for all generic parameters to eliminate the
894 -- need for overload resolution in the instance.
895
896 type Assoc_Ptr is new Int;
897
898 Assoc_Null : constant Assoc_Ptr := -1;
899
900 type Assoc is record
901 Gen_Id : Entity_Id;
902 Act_Id : Entity_Id;
903 Next_In_HTable : Assoc_Ptr;
904 end record;
905
906 package Generic_Renamings is new Table.Table
907 (Table_Component_Type => Assoc,
908 Table_Index_Type => Assoc_Ptr,
909 Table_Low_Bound => 0,
910 Table_Initial => 10,
911 Table_Increment => 100,
912 Table_Name => "Generic_Renamings");
913
914 -- Variable to hold enclosing instantiation. When the environment is
915 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
916
917 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
918
919 -- Hash table for associations
920
921 HTable_Size : constant := 37;
922 type HTable_Range is range 0 .. HTable_Size - 1;
923
924 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
925 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
926 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
927 function Hash (F : Entity_Id) return HTable_Range;
928
929 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
930 Header_Num => HTable_Range,
931 Element => Assoc,
932 Elmt_Ptr => Assoc_Ptr,
933 Null_Ptr => Assoc_Null,
934 Set_Next => Set_Next_Assoc,
935 Next => Next_Assoc,
936 Key => Entity_Id,
937 Get_Key => Get_Gen_Id,
938 Hash => Hash,
939 Equal => "=");
940
941 Exchanged_Views : Elist_Id;
942 -- This list holds the private views that have been exchanged during
943 -- instantiation to restore the visibility of the generic declaration.
944 -- (see comments above). After instantiation, the current visibility is
945 -- reestablished by means of a traversal of this list.
946
947 Hidden_Entities : Elist_Id;
948 -- This list holds the entities of the current scope that are removed
949 -- from immediate visibility when instantiating a child unit. Their
950 -- visibility is restored in Remove_Parent.
951
952 -- Because instantiations can be recursive, the following must be saved
953 -- on entry and restored on exit from an instantiation (spec or body).
954 -- This is done by the two procedures Save_Env and Restore_Env. For
955 -- package and subprogram instantiations (but not for the body instances)
956 -- the action of Save_Env is done in two steps: Init_Env is called before
957 -- Check_Generic_Child_Unit, because setting the parent instances requires
958 -- that the visibility data structures be properly initialized. Once the
959 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
960
961 Parent_Unit_Visible : Boolean := False;
962 -- Parent_Unit_Visible is used when the generic is a child unit, and
963 -- indicates whether the ultimate parent of the generic is visible in the
964 -- instantiation environment. It is used to reset the visibility of the
965 -- parent at the end of the instantiation (see Remove_Parent).
966
967 Instance_Parent_Unit : Entity_Id := Empty;
968 -- This records the ultimate parent unit of an instance of a generic
969 -- child unit and is used in conjunction with Parent_Unit_Visible to
970 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
971
972 type Instance_Env is record
973 Instantiated_Parent : Assoc;
974 Exchanged_Views : Elist_Id;
975 Hidden_Entities : Elist_Id;
976 Current_Sem_Unit : Unit_Number_Type;
977 Parent_Unit_Visible : Boolean := False;
978 Instance_Parent_Unit : Entity_Id := Empty;
979 Switches : Config_Switches_Type;
980 end record;
981
982 package Instance_Envs is new Table.Table (
983 Table_Component_Type => Instance_Env,
984 Table_Index_Type => Int,
985 Table_Low_Bound => 0,
986 Table_Initial => 32,
987 Table_Increment => 100,
988 Table_Name => "Instance_Envs");
989
990 procedure Restore_Private_Views
991 (Pack_Id : Entity_Id;
992 Is_Package : Boolean := True);
993 -- Restore the private views of external types, and unmark the generic
994 -- renamings of actuals, so that they become compatible subtypes again.
995 -- For subprograms, Pack_Id is the package constructed to hold the
996 -- renamings.
997
998 procedure Switch_View (T : Entity_Id);
999 -- Switch the partial and full views of a type and its private
1000 -- dependents (i.e. its subtypes and derived types).
1001
1002 ------------------------------------
1003 -- Structures for Error Reporting --
1004 ------------------------------------
1005
1006 Instantiation_Node : Node_Id;
1007 -- Used by subprograms that validate instantiation of formal parameters
1008 -- where there might be no actual on which to place the error message.
1009 -- Also used to locate the instantiation node for generic subunits.
1010
1011 Instantiation_Error : exception;
1012 -- When there is a semantic error in the generic parameter matching,
1013 -- there is no point in continuing the instantiation, because the
1014 -- number of cascaded errors is unpredictable. This exception aborts
1015 -- the instantiation process altogether.
1016
1017 S_Adjustment : Sloc_Adjustment;
1018 -- Offset created for each node in an instantiation, in order to keep
1019 -- track of the source position of the instantiation in each of its nodes.
1020 -- A subsequent semantic error or warning on a construct of the instance
1021 -- points to both places: the original generic node, and the point of
1022 -- instantiation. See Sinput and Sinput.L for additional details.
1023
1024 ------------------------------------------------------------
1025 -- Data structure for keeping track when inside a Generic --
1026 ------------------------------------------------------------
1027
1028 -- The following table is used to save values of the Inside_A_Generic
1029 -- flag (see spec of Sem) when they are saved by Start_Generic.
1030
1031 package Generic_Flags is new Table.Table (
1032 Table_Component_Type => Boolean,
1033 Table_Index_Type => Int,
1034 Table_Low_Bound => 0,
1035 Table_Initial => 32,
1036 Table_Increment => 200,
1037 Table_Name => "Generic_Flags");
1038
1039 ---------------------------
1040 -- Abandon_Instantiation --
1041 ---------------------------
1042
1043 procedure Abandon_Instantiation (N : Node_Id) is
1044 begin
1045 Error_Msg_N ("\instantiation abandoned!", N);
1046 raise Instantiation_Error;
1047 end Abandon_Instantiation;
1048
1049 ----------------------------------
1050 -- Adjust_Inherited_Pragma_Sloc --
1051 ----------------------------------
1052
1053 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1054 begin
1055 Adjust_Instantiation_Sloc (N, S_Adjustment);
1056 end Adjust_Inherited_Pragma_Sloc;
1057
1058 --------------------------
1059 -- Analyze_Associations --
1060 --------------------------
1061
1062 function Analyze_Associations
1063 (I_Node : Node_Id;
1064 Formals : List_Id;
1065 F_Copy : List_Id) return List_Id
1066 is
1067 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1068 Assoc_List : constant List_Id := New_List;
1069 Default_Actuals : constant List_Id := New_List;
1070 Gen_Unit : constant Entity_Id :=
1071 Defining_Entity (Parent (F_Copy));
1072
1073 Actuals : List_Id;
1074 Actual : Node_Id;
1075 Analyzed_Formal : Node_Id;
1076 First_Named : Node_Id := Empty;
1077 Formal : Node_Id;
1078 Match : Node_Id;
1079 Named : Node_Id;
1080 Saved_Formal : Node_Id;
1081
1082 Default_Formals : constant List_Id := New_List;
1083 -- If an Others_Choice is present, some of the formals may be defaulted.
1084 -- To simplify the treatment of visibility in an instance, we introduce
1085 -- individual defaults for each such formal. These defaults are
1086 -- appended to the list of associations and replace the Others_Choice.
1087
1088 Found_Assoc : Node_Id;
1089 -- Association for the current formal being match. Empty if there are
1090 -- no remaining actuals, or if there is no named association with the
1091 -- name of the formal.
1092
1093 Is_Named_Assoc : Boolean;
1094 Num_Matched : Nat := 0;
1095 Num_Actuals : Nat := 0;
1096
1097 Others_Present : Boolean := False;
1098 Others_Choice : Node_Id := Empty;
1099 -- In Ada 2005, indicates partial parameterization of a formal
1100 -- package. As usual an other association must be last in the list.
1101
1102 procedure Build_Subprogram_Wrappers;
1103 -- Ada 2020: AI12-0272 introduces pre/postconditions for formal
1104 -- subprograms. The implementation of making the formal into a renaming
1105 -- of the actual does not work, given that subprogram renaming cannot
1106 -- carry aspect specifications. Instead we must create subprogram
1107 -- wrappers whose body is a call to the actual, and whose declaration
1108 -- carries the aspects of the formal.
1109
1110 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1111 -- Warn if an actual fixed-point type has user-defined arithmetic
1112 -- operations, but there is no corresponding formal in the generic,
1113 -- in which case the predefined operations will be used. This merits
1114 -- a warning because of the special semantics of fixed point ops.
1115
1116 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1117 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1118 -- cannot have a named association for it. AI05-0025 extends this rule
1119 -- to formals of formal packages by AI05-0025, and it also applies to
1120 -- box-initialized formals.
1121
1122 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1123 -- Determine whether the parameter types and the return type of Subp
1124 -- are fully defined at the point of instantiation.
1125
1126 function Matching_Actual
1127 (F : Entity_Id;
1128 A_F : Entity_Id) return Node_Id;
1129 -- Find actual that corresponds to a given a formal parameter. If the
1130 -- actuals are positional, return the next one, if any. If the actuals
1131 -- are named, scan the parameter associations to find the right one.
1132 -- A_F is the corresponding entity in the analyzed generic, which is
1133 -- placed on the selector name.
1134 --
1135 -- In Ada 2005, a named association may be given with a box, in which
1136 -- case Matching_Actual sets Found_Assoc to the generic association,
1137 -- but return Empty for the actual itself. In this case the code below
1138 -- creates a corresponding declaration for the formal.
1139
1140 function Partial_Parameterization return Boolean;
1141 -- Ada 2005: if no match is found for a given formal, check if the
1142 -- association for it includes a box, or whether the associations
1143 -- include an Others clause.
1144
1145 procedure Process_Default (F : Entity_Id);
1146 -- Add a copy of the declaration of generic formal F to the list of
1147 -- associations, and add an explicit box association for F if there
1148 -- is none yet, and the default comes from an Others_Choice.
1149
1150 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1151 -- Determine whether Subp renames one of the subprograms defined in the
1152 -- generated package Standard.
1153
1154 procedure Set_Analyzed_Formal;
1155 -- Find the node in the generic copy that corresponds to a given formal.
1156 -- The semantic information on this node is used to perform legality
1157 -- checks on the actuals. Because semantic analysis can introduce some
1158 -- anonymous entities or modify the declaration node itself, the
1159 -- correspondence between the two lists is not one-one. In addition to
1160 -- anonymous types, the presence a formal equality will introduce an
1161 -- implicit declaration for the corresponding inequality.
1162
1163 -----------------------------------------
1164 -- procedure Build_Subprogram_Wrappers --
1165 -----------------------------------------
1166
1167 procedure Build_Subprogram_Wrappers is
1168 Formal : constant Entity_Id :=
1169 Defining_Unit_Name (Specification (Analyzed_Formal));
1170 Aspect_Spec : Node_Id;
1171 Decl_Node : Node_Id;
1172 Actual_Name : Node_Id;
1173
1174 begin
1175 -- Create declaration for wrapper subprogram
1176 -- The actual can be overloaded, in which case it will be
1177 -- resolved when the call in the wrapper body is analyzed.
1178 -- We attach the possible interpretations of the actual to
1179 -- the name to be used in the call in the wrapper body.
1180
1181 if Is_Entity_Name (Match) then
1182 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
1183
1184 if Is_Overloaded (Match) then
1185 Save_Interps (Match, Actual_Name);
1186 end if;
1187
1188 else
1189 -- Use renaming declaration created when analyzing actual.
1190 -- This may be incomplete if there are several formal
1191 -- subprograms whose actual is an attribute ???
1192
1193 declare
1194 Renaming_Decl : constant Node_Id := Last (Assoc_List);
1195
1196 begin
1197 Actual_Name := New_Occurrence_Of
1198 (Defining_Entity (Renaming_Decl), Sloc (Match));
1199 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
1200 end;
1201 end if;
1202
1203 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
1204
1205 -- Transfer aspect specifications from formal subprogram to wrapper
1206
1207 Set_Aspect_Specifications (Decl_Node,
1208 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
1209
1210 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
1211 while Present (Aspect_Spec) loop
1212 Set_Analyzed (Aspect_Spec, False);
1213 Next (Aspect_Spec);
1214 end loop;
1215
1216 Append_To (Assoc_List, Decl_Node);
1217
1218 -- Create corresponding body, and append it to association list
1219 -- that appears at the head of the declarations in the instance.
1220 -- The subprogram may be called in the analysis of subsequent
1221 -- actuals.
1222
1223 Append_To (Assoc_List,
1224 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
1225 end Build_Subprogram_Wrappers;
1226
1227 ----------------------------------------
1228 -- Check_Overloaded_Formal_Subprogram --
1229 ----------------------------------------
1230
1231 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1232 Temp_Formal : Entity_Id;
1233
1234 begin
1235 Temp_Formal := First (Formals);
1236 while Present (Temp_Formal) loop
1237 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1238 and then Temp_Formal /= Formal
1239 and then
1240 Chars (Defining_Unit_Name (Specification (Formal))) =
1241 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1242 then
1243 if Present (Found_Assoc) then
1244 Error_Msg_N
1245 ("named association not allowed for overloaded formal",
1246 Found_Assoc);
1247
1248 else
1249 Error_Msg_N
1250 ("named association not allowed for overloaded formal",
1251 Others_Choice);
1252 end if;
1253
1254 Abandon_Instantiation (Instantiation_Node);
1255 end if;
1256
1257 Next (Temp_Formal);
1258 end loop;
1259 end Check_Overloaded_Formal_Subprogram;
1260
1261 -------------------------------
1262 -- Check_Fixed_Point_Actual --
1263 -------------------------------
1264
1265 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1266 Typ : constant Entity_Id := Entity (Actual);
1267 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1268 Elem : Elmt_Id;
1269 Formal : Node_Id;
1270 Op : Entity_Id;
1271
1272 begin
1273 -- Locate primitive operations of the type that are arithmetic
1274 -- operations.
1275
1276 Elem := First_Elmt (Prims);
1277 while Present (Elem) loop
1278 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1279
1280 -- Check whether the generic unit has a formal subprogram of
1281 -- the same name. This does not check types but is good enough
1282 -- to justify a warning.
1283
1284 Formal := First_Non_Pragma (Formals);
1285 Op := Alias (Node (Elem));
1286
1287 while Present (Formal) loop
1288 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1289 and then Chars (Defining_Entity (Formal)) =
1290 Chars (Node (Elem))
1291 then
1292 exit;
1293
1294 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1295 declare
1296 Assoc : Node_Id;
1297 Ent : Entity_Id;
1298
1299 begin
1300 -- Locate corresponding actual, and check whether it
1301 -- includes a fixed-point type.
1302
1303 Assoc := First (Assoc_List);
1304 while Present (Assoc) loop
1305 exit when
1306 Nkind (Assoc) = N_Package_Renaming_Declaration
1307 and then Chars (Defining_Unit_Name (Assoc)) =
1308 Chars (Defining_Identifier (Formal));
1309
1310 Next (Assoc);
1311 end loop;
1312
1313 if Present (Assoc) then
1314
1315 -- If formal package declares a fixed-point type,
1316 -- and the user-defined operator is derived from
1317 -- a generic instance package, the fixed-point type
1318 -- does not use the corresponding predefined op.
1319
1320 Ent := First_Entity (Entity (Name (Assoc)));
1321 while Present (Ent) loop
1322 if Is_Fixed_Point_Type (Ent)
1323 and then Present (Op)
1324 and then Is_Generic_Instance (Scope (Op))
1325 then
1326 return;
1327 end if;
1328
1329 Next_Entity (Ent);
1330 end loop;
1331 end if;
1332 end;
1333 end if;
1334
1335 Next (Formal);
1336 end loop;
1337
1338 if No (Formal) then
1339 Error_Msg_Sloc := Sloc (Node (Elem));
1340 Error_Msg_NE
1341 ("?instance uses predefined operation, not primitive "
1342 & "operation&#", Actual, Node (Elem));
1343 end if;
1344 end if;
1345
1346 Next_Elmt (Elem);
1347 end loop;
1348 end Check_Fixed_Point_Actual;
1349
1350 -------------------------------
1351 -- Has_Fully_Defined_Profile --
1352 -------------------------------
1353
1354 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1355 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1356 -- Determine whethet type Typ is fully defined
1357
1358 ---------------------------
1359 -- Is_Fully_Defined_Type --
1360 ---------------------------
1361
1362 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1363 begin
1364 -- A private type without a full view is not fully defined
1365
1366 if Is_Private_Type (Typ)
1367 and then No (Full_View (Typ))
1368 then
1369 return False;
1370
1371 -- An incomplete type is never fully defined
1372
1373 elsif Is_Incomplete_Type (Typ) then
1374 return False;
1375
1376 -- All other types are fully defined
1377
1378 else
1379 return True;
1380 end if;
1381 end Is_Fully_Defined_Type;
1382
1383 -- Local declarations
1384
1385 Param : Entity_Id;
1386
1387 -- Start of processing for Has_Fully_Defined_Profile
1388
1389 begin
1390 -- Check the parameters
1391
1392 Param := First_Formal (Subp);
1393 while Present (Param) loop
1394 if not Is_Fully_Defined_Type (Etype (Param)) then
1395 return False;
1396 end if;
1397
1398 Next_Formal (Param);
1399 end loop;
1400
1401 -- Check the return type
1402
1403 return Is_Fully_Defined_Type (Etype (Subp));
1404 end Has_Fully_Defined_Profile;
1405
1406 ---------------------
1407 -- Matching_Actual --
1408 ---------------------
1409
1410 function Matching_Actual
1411 (F : Entity_Id;
1412 A_F : Entity_Id) return Node_Id
1413 is
1414 Prev : Node_Id;
1415 Act : Node_Id;
1416
1417 begin
1418 Is_Named_Assoc := False;
1419
1420 -- End of list of purely positional parameters
1421
1422 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1423 Found_Assoc := Empty;
1424 Act := Empty;
1425
1426 -- Case of positional parameter corresponding to current formal
1427
1428 elsif No (Selector_Name (Actual)) then
1429 Found_Assoc := Actual;
1430 Act := Explicit_Generic_Actual_Parameter (Actual);
1431 Num_Matched := Num_Matched + 1;
1432 Next (Actual);
1433
1434 -- Otherwise scan list of named actuals to find the one with the
1435 -- desired name. All remaining actuals have explicit names.
1436
1437 else
1438 Is_Named_Assoc := True;
1439 Found_Assoc := Empty;
1440 Act := Empty;
1441 Prev := Empty;
1442
1443 while Present (Actual) loop
1444 if Nkind (Actual) = N_Others_Choice then
1445 Found_Assoc := Empty;
1446 Act := Empty;
1447
1448 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1449 Set_Entity (Selector_Name (Actual), A_F);
1450 Set_Etype (Selector_Name (Actual), Etype (A_F));
1451 Generate_Reference (A_F, Selector_Name (Actual));
1452
1453 Found_Assoc := Actual;
1454 Act := Explicit_Generic_Actual_Parameter (Actual);
1455 Num_Matched := Num_Matched + 1;
1456 exit;
1457 end if;
1458
1459 Prev := Actual;
1460 Next (Actual);
1461 end loop;
1462
1463 -- Reset for subsequent searches. In most cases the named
1464 -- associations are in order. If they are not, we reorder them
1465 -- to avoid scanning twice the same actual. This is not just a
1466 -- question of efficiency: there may be multiple defaults with
1467 -- boxes that have the same name. In a nested instantiation we
1468 -- insert actuals for those defaults, and cannot rely on their
1469 -- names to disambiguate them.
1470
1471 if Actual = First_Named then
1472 Next (First_Named);
1473
1474 elsif Present (Actual) then
1475 Insert_Before (First_Named, Remove_Next (Prev));
1476 end if;
1477
1478 Actual := First_Named;
1479 end if;
1480
1481 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1482 Set_Used_As_Generic_Actual (Entity (Act));
1483 end if;
1484
1485 return Act;
1486 end Matching_Actual;
1487
1488 ------------------------------
1489 -- Partial_Parameterization --
1490 ------------------------------
1491
1492 function Partial_Parameterization return Boolean is
1493 begin
1494 return Others_Present
1495 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1496 end Partial_Parameterization;
1497
1498 ---------------------
1499 -- Process_Default --
1500 ---------------------
1501
1502 procedure Process_Default (F : Entity_Id) is
1503 Loc : constant Source_Ptr := Sloc (I_Node);
1504 F_Id : constant Entity_Id := Defining_Entity (F);
1505 Decl : Node_Id;
1506 Default : Node_Id;
1507 Id : Entity_Id;
1508
1509 begin
1510 -- Append copy of formal declaration to associations, and create new
1511 -- defining identifier for it.
1512
1513 Decl := New_Copy_Tree (F);
1514 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1515
1516 if Nkind (F) in N_Formal_Subprogram_Declaration then
1517 Set_Defining_Unit_Name (Specification (Decl), Id);
1518
1519 else
1520 Set_Defining_Identifier (Decl, Id);
1521 end if;
1522
1523 Append (Decl, Assoc_List);
1524
1525 if No (Found_Assoc) then
1526 Default :=
1527 Make_Generic_Association (Loc,
1528 Selector_Name =>
1529 New_Occurrence_Of (Id, Loc),
1530 Explicit_Generic_Actual_Parameter => Empty);
1531 Set_Box_Present (Default);
1532 Append (Default, Default_Formals);
1533 end if;
1534 end Process_Default;
1535
1536 ---------------------------------
1537 -- Renames_Standard_Subprogram --
1538 ---------------------------------
1539
1540 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1541 Id : Entity_Id;
1542
1543 begin
1544 Id := Alias (Subp);
1545 while Present (Id) loop
1546 if Scope (Id) = Standard_Standard then
1547 return True;
1548 end if;
1549
1550 Id := Alias (Id);
1551 end loop;
1552
1553 return False;
1554 end Renames_Standard_Subprogram;
1555
1556 -------------------------
1557 -- Set_Analyzed_Formal --
1558 -------------------------
1559
1560 procedure Set_Analyzed_Formal is
1561 Kind : Node_Kind;
1562
1563 begin
1564 while Present (Analyzed_Formal) loop
1565 Kind := Nkind (Analyzed_Formal);
1566
1567 case Nkind (Formal) is
1568 when N_Formal_Subprogram_Declaration =>
1569 exit when Kind in N_Formal_Subprogram_Declaration
1570 and then
1571 Chars
1572 (Defining_Unit_Name (Specification (Formal))) =
1573 Chars
1574 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1575
1576 when N_Formal_Package_Declaration =>
1577 exit when Kind in N_Formal_Package_Declaration
1578 | N_Generic_Package_Declaration
1579 | N_Package_Declaration;
1580
1581 when N_Use_Package_Clause
1582 | N_Use_Type_Clause
1583 =>
1584 exit;
1585
1586 when others =>
1587
1588 -- Skip freeze nodes, and nodes inserted to replace
1589 -- unrecognized pragmas.
1590
1591 exit when
1592 Kind not in N_Formal_Subprogram_Declaration
1593 and then Kind not in N_Subprogram_Declaration
1594 | N_Freeze_Entity
1595 | N_Null_Statement
1596 | N_Itype_Reference
1597 and then Chars (Defining_Identifier (Formal)) =
1598 Chars (Defining_Identifier (Analyzed_Formal));
1599 end case;
1600
1601 Next (Analyzed_Formal);
1602 end loop;
1603 end Set_Analyzed_Formal;
1604
1605 -- Start of processing for Analyze_Associations
1606
1607 begin
1608 Actuals := Generic_Associations (I_Node);
1609
1610 if Present (Actuals) then
1611
1612 -- Check for an Others choice, indicating a partial parameterization
1613 -- for a formal package.
1614
1615 Actual := First (Actuals);
1616 while Present (Actual) loop
1617 if Nkind (Actual) = N_Others_Choice then
1618 Others_Present := True;
1619 Others_Choice := Actual;
1620
1621 if Present (Next (Actual)) then
1622 Error_Msg_N ("others must be last association", Actual);
1623 end if;
1624
1625 -- This subprogram is used both for formal packages and for
1626 -- instantiations. For the latter, associations must all be
1627 -- explicit.
1628
1629 if Nkind (I_Node) /= N_Formal_Package_Declaration
1630 and then Comes_From_Source (I_Node)
1631 then
1632 Error_Msg_N
1633 ("others association not allowed in an instance",
1634 Actual);
1635 end if;
1636
1637 -- In any case, nothing to do after the others association
1638
1639 exit;
1640
1641 elsif Box_Present (Actual)
1642 and then Comes_From_Source (I_Node)
1643 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1644 then
1645 Error_Msg_N
1646 ("box association not allowed in an instance", Actual);
1647 end if;
1648
1649 Next (Actual);
1650 end loop;
1651
1652 -- If named associations are present, save first named association
1653 -- (it may of course be Empty) to facilitate subsequent name search.
1654
1655 First_Named := First (Actuals);
1656 while Present (First_Named)
1657 and then Nkind (First_Named) /= N_Others_Choice
1658 and then No (Selector_Name (First_Named))
1659 loop
1660 Num_Actuals := Num_Actuals + 1;
1661 Next (First_Named);
1662 end loop;
1663 end if;
1664
1665 Named := First_Named;
1666 while Present (Named) loop
1667 if Nkind (Named) /= N_Others_Choice
1668 and then No (Selector_Name (Named))
1669 then
1670 Error_Msg_N ("invalid positional actual after named one", Named);
1671 Abandon_Instantiation (Named);
1672 end if;
1673
1674 -- A named association may lack an actual parameter, if it was
1675 -- introduced for a default subprogram that turns out to be local
1676 -- to the outer instantiation. If it has a box association it must
1677 -- correspond to some formal in the generic.
1678
1679 if Nkind (Named) /= N_Others_Choice
1680 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1681 or else Box_Present (Named))
1682 then
1683 Num_Actuals := Num_Actuals + 1;
1684 end if;
1685
1686 Next (Named);
1687 end loop;
1688
1689 if Present (Formals) then
1690 Formal := First_Non_Pragma (Formals);
1691 Analyzed_Formal := First_Non_Pragma (F_Copy);
1692
1693 if Present (Actuals) then
1694 Actual := First (Actuals);
1695
1696 -- All formals should have default values
1697
1698 else
1699 Actual := Empty;
1700 end if;
1701
1702 while Present (Formal) loop
1703 Set_Analyzed_Formal;
1704 Saved_Formal := Next_Non_Pragma (Formal);
1705
1706 case Nkind (Formal) is
1707 when N_Formal_Object_Declaration =>
1708 Match :=
1709 Matching_Actual
1710 (Defining_Identifier (Formal),
1711 Defining_Identifier (Analyzed_Formal));
1712
1713 if No (Match) and then Partial_Parameterization then
1714 Process_Default (Formal);
1715
1716 else
1717 Append_List
1718 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1719 Assoc_List);
1720
1721 -- For a defaulted in_parameter, create an entry in the
1722 -- the list of defaulted actuals, for GNATprove use. Do
1723 -- not included these defaults for an instance nested
1724 -- within a generic, because the defaults are also used
1725 -- in the analysis of the enclosing generic, and only
1726 -- defaulted subprograms are relevant there.
1727
1728 if No (Match) and then not Inside_A_Generic then
1729 Append_To (Default_Actuals,
1730 Make_Generic_Association (Sloc (I_Node),
1731 Selector_Name =>
1732 New_Occurrence_Of
1733 (Defining_Identifier (Formal), Sloc (I_Node)),
1734 Explicit_Generic_Actual_Parameter =>
1735 New_Copy_Tree (Default_Expression (Formal))));
1736 end if;
1737 end if;
1738
1739 -- If the object is a call to an expression function, this
1740 -- is a freezing point for it.
1741
1742 if Is_Entity_Name (Match)
1743 and then Present (Entity (Match))
1744 and then Nkind
1745 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1746 = N_Expression_Function
1747 then
1748 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1749 end if;
1750
1751 when N_Formal_Type_Declaration =>
1752 Match :=
1753 Matching_Actual
1754 (Defining_Identifier (Formal),
1755 Defining_Identifier (Analyzed_Formal));
1756
1757 if No (Match) then
1758 if Partial_Parameterization then
1759 Process_Default (Formal);
1760
1761 else
1762 Error_Msg_Sloc := Sloc (Gen_Unit);
1763 Error_Msg_NE
1764 ("missing actual&",
1765 Instantiation_Node, Defining_Identifier (Formal));
1766 Error_Msg_NE
1767 ("\in instantiation of & declared#",
1768 Instantiation_Node, Gen_Unit);
1769 Abandon_Instantiation (Instantiation_Node);
1770 end if;
1771
1772 else
1773 Analyze (Match);
1774 Append_List
1775 (Instantiate_Type
1776 (Formal, Match, Analyzed_Formal, Assoc_List),
1777 Assoc_List);
1778
1779 -- Warn when an actual is a fixed-point with user-
1780 -- defined promitives. The warning is superfluous
1781 -- if the formal is private, because there can be
1782 -- no arithmetic operations in the generic so there
1783 -- no danger of confusion.
1784
1785 if Is_Fixed_Point_Type (Entity (Match))
1786 and then not Is_Private_Type
1787 (Defining_Identifier (Analyzed_Formal))
1788 then
1789 Check_Fixed_Point_Actual (Match);
1790 end if;
1791
1792 -- An instantiation is a freeze point for the actuals,
1793 -- unless this is a rewritten formal package, or the
1794 -- formal is an Ada 2012 formal incomplete type.
1795
1796 if Nkind (I_Node) = N_Formal_Package_Declaration
1797 or else
1798 (Ada_Version >= Ada_2012
1799 and then
1800 Ekind (Defining_Identifier (Analyzed_Formal)) =
1801 E_Incomplete_Type)
1802 then
1803 null;
1804
1805 else
1806 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1807 end if;
1808 end if;
1809
1810 -- A remote access-to-class-wide type is not a legal actual
1811 -- for a generic formal of an access type (E.2.2(17/2)).
1812 -- In GNAT an exception to this rule is introduced when
1813 -- the formal is marked as remote using implementation
1814 -- defined aspect/pragma Remote_Access_Type. In that case
1815 -- the actual must be remote as well.
1816
1817 -- If the current instantiation is the construction of a
1818 -- local copy for a formal package the actuals may be
1819 -- defaulted, and there is no matching actual to check.
1820
1821 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1822 and then
1823 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1824 N_Access_To_Object_Definition
1825 and then Present (Match)
1826 then
1827 declare
1828 Formal_Ent : constant Entity_Id :=
1829 Defining_Identifier (Analyzed_Formal);
1830 begin
1831 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1832 = Is_Remote_Types (Formal_Ent)
1833 then
1834 -- Remoteness of formal and actual match
1835
1836 null;
1837
1838 elsif Is_Remote_Types (Formal_Ent) then
1839
1840 -- Remote formal, non-remote actual
1841
1842 Error_Msg_NE
1843 ("actual for& must be remote", Match, Formal_Ent);
1844
1845 else
1846 -- Non-remote formal, remote actual
1847
1848 Error_Msg_NE
1849 ("actual for& may not be remote",
1850 Match, Formal_Ent);
1851 end if;
1852 end;
1853 end if;
1854
1855 when N_Formal_Subprogram_Declaration =>
1856 Match :=
1857 Matching_Actual
1858 (Defining_Unit_Name (Specification (Formal)),
1859 Defining_Unit_Name (Specification (Analyzed_Formal)));
1860
1861 -- If the formal subprogram has the same name as another
1862 -- formal subprogram of the generic, then a named
1863 -- association is illegal (12.3(9)). Exclude named
1864 -- associations that are generated for a nested instance.
1865
1866 if Present (Match)
1867 and then Is_Named_Assoc
1868 and then Comes_From_Source (Found_Assoc)
1869 then
1870 Check_Overloaded_Formal_Subprogram (Formal);
1871 end if;
1872
1873 -- If there is no corresponding actual, this may be case
1874 -- of partial parameterization, or else the formal has a
1875 -- default or a box.
1876
1877 if No (Match) and then Partial_Parameterization then
1878 Process_Default (Formal);
1879
1880 if Nkind (I_Node) = N_Formal_Package_Declaration then
1881 Check_Overloaded_Formal_Subprogram (Formal);
1882 end if;
1883
1884 else
1885 Append_To (Assoc_List,
1886 Instantiate_Formal_Subprogram
1887 (Formal, Match, Analyzed_Formal));
1888
1889 -- If formal subprogram has contracts, create wrappers
1890 -- for it. This is an expansion activity that cannot
1891 -- take place e.g. within an enclosing generic unit.
1892
1893 if Has_Contracts (Analyzed_Formal)
1894 and then Expander_Active
1895 then
1896 Build_Subprogram_Wrappers;
1897 end if;
1898
1899 -- An instantiation is a freeze point for the actuals,
1900 -- unless this is a rewritten formal package.
1901
1902 if Nkind (I_Node) /= N_Formal_Package_Declaration
1903 and then Nkind (Match) = N_Identifier
1904 and then Is_Subprogram (Entity (Match))
1905
1906 -- The actual subprogram may rename a routine defined
1907 -- in Standard. Avoid freezing such renamings because
1908 -- subprograms coming from Standard cannot be frozen.
1909
1910 and then
1911 not Renames_Standard_Subprogram (Entity (Match))
1912
1913 -- If the actual subprogram comes from a different
1914 -- unit, it is already frozen, either by a body in
1915 -- that unit or by the end of the declarative part
1916 -- of the unit. This check avoids the freezing of
1917 -- subprograms defined in Standard which are used
1918 -- as generic actuals.
1919
1920 and then In_Same_Code_Unit (Entity (Match), I_Node)
1921 and then Has_Fully_Defined_Profile (Entity (Match))
1922 then
1923 -- Mark the subprogram as having a delayed freeze
1924 -- since this may be an out-of-order action.
1925
1926 Set_Has_Delayed_Freeze (Entity (Match));
1927 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1928 end if;
1929 end if;
1930
1931 -- If this is a nested generic, preserve default for later
1932 -- instantiations. We do this as well for GNATprove use,
1933 -- so that the list of generic associations is complete.
1934
1935 if No (Match) and then Box_Present (Formal) then
1936 declare
1937 Subp : constant Entity_Id :=
1938 Defining_Unit_Name
1939 (Specification (Last (Assoc_List)));
1940
1941 begin
1942 Append_To (Default_Actuals,
1943 Make_Generic_Association (Sloc (I_Node),
1944 Selector_Name =>
1945 New_Occurrence_Of (Subp, Sloc (I_Node)),
1946 Explicit_Generic_Actual_Parameter =>
1947 New_Occurrence_Of (Subp, Sloc (I_Node))));
1948 end;
1949 end if;
1950
1951 when N_Formal_Package_Declaration =>
1952 -- The name of the formal package may be hidden by the
1953 -- formal parameter itself.
1954
1955 if Error_Posted (Analyzed_Formal) then
1956 Abandon_Instantiation (Instantiation_Node);
1957
1958 else
1959 Match :=
1960 Matching_Actual
1961 (Defining_Identifier (Formal),
1962 Defining_Identifier
1963 (Original_Node (Analyzed_Formal)));
1964 end if;
1965
1966 if No (Match) then
1967 if Partial_Parameterization then
1968 Process_Default (Formal);
1969
1970 else
1971 Error_Msg_Sloc := Sloc (Gen_Unit);
1972 Error_Msg_NE
1973 ("missing actual&",
1974 Instantiation_Node, Defining_Identifier (Formal));
1975 Error_Msg_NE
1976 ("\in instantiation of & declared#",
1977 Instantiation_Node, Gen_Unit);
1978
1979 Abandon_Instantiation (Instantiation_Node);
1980 end if;
1981
1982 else
1983 Analyze (Match);
1984 Append_List
1985 (Instantiate_Formal_Package
1986 (Formal, Match, Analyzed_Formal),
1987 Assoc_List);
1988
1989 -- Determine whether the actual package needs an explicit
1990 -- freeze node. This is only the case if the actual is
1991 -- declared in the same unit and has a body. Normally
1992 -- packages do not have explicit freeze nodes, and gigi
1993 -- only uses them to elaborate entities in a package
1994 -- body.
1995
1996 Explicit_Freeze_Check : declare
1997 Actual : constant Entity_Id := Entity (Match);
1998 Gen_Par : Entity_Id;
1999
2000 Needs_Freezing : Boolean;
2001 P : Node_Id;
2002
2003 procedure Check_Generic_Parent;
2004 -- The actual may be an instantiation of a unit
2005 -- declared in a previous instantiation. If that
2006 -- one is also in the current compilation, it must
2007 -- itself be frozen before the actual. The actual
2008 -- may be an instantiation of a generic child unit,
2009 -- in which case the same applies to the instance
2010 -- of the parent which must be frozen before the
2011 -- actual.
2012 -- Should this itself be recursive ???
2013
2014 --------------------------
2015 -- Check_Generic_Parent --
2016 --------------------------
2017
2018 procedure Check_Generic_Parent is
2019 Inst : constant Node_Id :=
2020 Next (Unit_Declaration_Node (Actual));
2021 Par : Entity_Id;
2022
2023 begin
2024 Par := Empty;
2025
2026 if Nkind (Parent (Actual)) = N_Package_Specification
2027 then
2028 Par := Scope (Generic_Parent (Parent (Actual)));
2029
2030 if Is_Generic_Instance (Par) then
2031 null;
2032
2033 -- If the actual is a child generic unit, check
2034 -- whether the instantiation of the parent is
2035 -- also local and must also be frozen now. We
2036 -- must retrieve the instance node to locate the
2037 -- parent instance if any.
2038
2039 elsif Ekind (Par) = E_Generic_Package
2040 and then Is_Child_Unit (Gen_Par)
2041 and then Ekind (Scope (Gen_Par)) =
2042 E_Generic_Package
2043 then
2044 if Nkind (Inst) = N_Package_Instantiation
2045 and then Nkind (Name (Inst)) =
2046 N_Expanded_Name
2047 then
2048 -- Retrieve entity of parent instance
2049
2050 Par := Entity (Prefix (Name (Inst)));
2051 end if;
2052
2053 else
2054 Par := Empty;
2055 end if;
2056 end if;
2057
2058 if Present (Par)
2059 and then Is_Generic_Instance (Par)
2060 and then Scope (Par) = Current_Scope
2061 and then
2062 (No (Freeze_Node (Par))
2063 or else
2064 not Is_List_Member (Freeze_Node (Par)))
2065 then
2066 Set_Has_Delayed_Freeze (Par);
2067 Append_Elmt (Par, Actuals_To_Freeze);
2068 end if;
2069 end Check_Generic_Parent;
2070
2071 -- Start of processing for Explicit_Freeze_Check
2072
2073 begin
2074 if Present (Renamed_Entity (Actual)) then
2075 Gen_Par :=
2076 Generic_Parent (Specification
2077 (Unit_Declaration_Node
2078 (Renamed_Entity (Actual))));
2079 else
2080 Gen_Par :=
2081 Generic_Parent (Specification
2082 (Unit_Declaration_Node (Actual)));
2083 end if;
2084
2085 if not Expander_Active
2086 or else not Has_Completion (Actual)
2087 or else not In_Same_Source_Unit (I_Node, Actual)
2088 or else Is_Frozen (Actual)
2089 or else
2090 (Present (Renamed_Entity (Actual))
2091 and then
2092 not In_Same_Source_Unit
2093 (I_Node, (Renamed_Entity (Actual))))
2094 then
2095 null;
2096
2097 else
2098 -- Finally we want to exclude such freeze nodes
2099 -- from statement sequences, which freeze
2100 -- everything before them.
2101 -- Is this strictly necessary ???
2102
2103 Needs_Freezing := True;
2104
2105 P := Parent (I_Node);
2106 while Nkind (P) /= N_Compilation_Unit loop
2107 if Nkind (P) = N_Handled_Sequence_Of_Statements
2108 then
2109 Needs_Freezing := False;
2110 exit;
2111 end if;
2112
2113 P := Parent (P);
2114 end loop;
2115
2116 if Needs_Freezing then
2117 Check_Generic_Parent;
2118
2119 -- If the actual is a renaming of a proper
2120 -- instance of the formal package, indicate
2121 -- that it is the instance that must be frozen.
2122
2123 if Nkind (Parent (Actual)) =
2124 N_Package_Renaming_Declaration
2125 then
2126 Set_Has_Delayed_Freeze
2127 (Renamed_Entity (Actual));
2128 Append_Elmt
2129 (Renamed_Entity (Actual),
2130 Actuals_To_Freeze);
2131 else
2132 Set_Has_Delayed_Freeze (Actual);
2133 Append_Elmt (Actual, Actuals_To_Freeze);
2134 end if;
2135 end if;
2136 end if;
2137 end Explicit_Freeze_Check;
2138 end if;
2139
2140 -- For use type and use package appearing in the generic part,
2141 -- we have already copied them, so we can just move them where
2142 -- they belong (we mustn't recopy them since this would mess up
2143 -- the Sloc values).
2144
2145 when N_Use_Package_Clause
2146 | N_Use_Type_Clause
2147 =>
2148 if Nkind (Original_Node (I_Node)) =
2149 N_Formal_Package_Declaration
2150 then
2151 Append (New_Copy_Tree (Formal), Assoc_List);
2152 else
2153 Remove (Formal);
2154 Append (Formal, Assoc_List);
2155 end if;
2156
2157 when others =>
2158 raise Program_Error;
2159 end case;
2160
2161 Formal := Saved_Formal;
2162 Next_Non_Pragma (Analyzed_Formal);
2163 end loop;
2164
2165 if Num_Actuals > Num_Matched then
2166 Error_Msg_Sloc := Sloc (Gen_Unit);
2167
2168 if Present (Selector_Name (Actual)) then
2169 Error_Msg_NE
2170 ("unmatched actual &", Actual, Selector_Name (Actual));
2171 Error_Msg_NE
2172 ("\in instantiation of & declared#", Actual, Gen_Unit);
2173 else
2174 Error_Msg_NE
2175 ("unmatched actual in instantiation of & declared#",
2176 Actual, Gen_Unit);
2177 end if;
2178 end if;
2179
2180 elsif Present (Actuals) then
2181 Error_Msg_N
2182 ("too many actuals in generic instantiation", Instantiation_Node);
2183 end if;
2184
2185 -- An instantiation freezes all generic actuals. The only exceptions
2186 -- to this are incomplete types and subprograms which are not fully
2187 -- defined at the point of instantiation.
2188
2189 declare
2190 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2191 begin
2192 while Present (Elmt) loop
2193 Freeze_Before (I_Node, Node (Elmt));
2194 Next_Elmt (Elmt);
2195 end loop;
2196 end;
2197
2198 -- If there are default subprograms, normalize the tree by adding
2199 -- explicit associations for them. This is required if the instance
2200 -- appears within a generic.
2201
2202 if not Is_Empty_List (Default_Actuals) then
2203 declare
2204 Default : Node_Id;
2205
2206 begin
2207 Default := First (Default_Actuals);
2208 while Present (Default) loop
2209 Mark_Rewrite_Insertion (Default);
2210 Next (Default);
2211 end loop;
2212
2213 if No (Actuals) then
2214 Set_Generic_Associations (I_Node, Default_Actuals);
2215 else
2216 Append_List_To (Actuals, Default_Actuals);
2217 end if;
2218 end;
2219 end if;
2220
2221 -- If this is a formal package, normalize the parameter list by adding
2222 -- explicit box associations for the formals that are covered by an
2223 -- Others_Choice.
2224
2225 if not Is_Empty_List (Default_Formals) then
2226 Append_List (Default_Formals, Formals);
2227 end if;
2228
2229 return Assoc_List;
2230 end Analyze_Associations;
2231
2232 -------------------------------
2233 -- Analyze_Formal_Array_Type --
2234 -------------------------------
2235
2236 procedure Analyze_Formal_Array_Type
2237 (T : in out Entity_Id;
2238 Def : Node_Id)
2239 is
2240 DSS : Node_Id;
2241
2242 begin
2243 -- Treated like a non-generic array declaration, with additional
2244 -- semantic checks.
2245
2246 Enter_Name (T);
2247
2248 if Nkind (Def) = N_Constrained_Array_Definition then
2249 DSS := First (Discrete_Subtype_Definitions (Def));
2250 while Present (DSS) loop
2251 if Nkind (DSS) in N_Subtype_Indication
2252 | N_Range
2253 | N_Attribute_Reference
2254 then
2255 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2256 end if;
2257
2258 Next (DSS);
2259 end loop;
2260 end if;
2261
2262 Array_Type_Declaration (T, Def);
2263 Set_Is_Generic_Type (Base_Type (T));
2264
2265 if Ekind (Component_Type (T)) = E_Incomplete_Type
2266 and then No (Full_View (Component_Type (T)))
2267 then
2268 Error_Msg_N ("premature usage of incomplete type", Def);
2269
2270 -- Check that range constraint is not allowed on the component type
2271 -- of a generic formal array type (AARM 12.5.3(3))
2272
2273 elsif Is_Internal (Component_Type (T))
2274 and then Present (Subtype_Indication (Component_Definition (Def)))
2275 and then Nkind (Original_Node
2276 (Subtype_Indication (Component_Definition (Def)))) =
2277 N_Subtype_Indication
2278 then
2279 Error_Msg_N
2280 ("in a formal, a subtype indication can only be "
2281 & "a subtype mark (RM 12.5.3(3))",
2282 Subtype_Indication (Component_Definition (Def)));
2283 end if;
2284
2285 end Analyze_Formal_Array_Type;
2286
2287 ---------------------------------------------
2288 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2289 ---------------------------------------------
2290
2291 -- As for other generic types, we create a valid type representation with
2292 -- legal but arbitrary attributes, whose values are never considered
2293 -- static. For all scalar types we introduce an anonymous base type, with
2294 -- the same attributes. We choose the corresponding integer type to be
2295 -- Standard_Integer.
2296 -- Here and in other similar routines, the Sloc of the generated internal
2297 -- type must be the same as the sloc of the defining identifier of the
2298 -- formal type declaration, to provide proper source navigation.
2299
2300 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2301 (T : Entity_Id;
2302 Def : Node_Id)
2303 is
2304 Loc : constant Source_Ptr := Sloc (Def);
2305
2306 Base : constant Entity_Id :=
2307 New_Internal_Entity
2308 (E_Decimal_Fixed_Point_Type,
2309 Current_Scope,
2310 Sloc (Defining_Identifier (Parent (Def))), 'G');
2311
2312 Int_Base : constant Entity_Id := Standard_Integer;
2313 Delta_Val : constant Ureal := Ureal_1;
2314 Digs_Val : constant Uint := Uint_6;
2315
2316 function Make_Dummy_Bound return Node_Id;
2317 -- Return a properly typed universal real literal to use as a bound
2318
2319 ----------------------
2320 -- Make_Dummy_Bound --
2321 ----------------------
2322
2323 function Make_Dummy_Bound return Node_Id is
2324 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2325 begin
2326 Set_Etype (Bound, Universal_Real);
2327 return Bound;
2328 end Make_Dummy_Bound;
2329
2330 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2331
2332 begin
2333 Enter_Name (T);
2334
2335 Set_Etype (Base, Base);
2336 Set_Size_Info (Base, Int_Base);
2337 Set_RM_Size (Base, RM_Size (Int_Base));
2338 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2339 Set_Digits_Value (Base, Digs_Val);
2340 Set_Delta_Value (Base, Delta_Val);
2341 Set_Small_Value (Base, Delta_Val);
2342 Set_Scalar_Range (Base,
2343 Make_Range (Loc,
2344 Low_Bound => Make_Dummy_Bound,
2345 High_Bound => Make_Dummy_Bound));
2346
2347 Set_Is_Generic_Type (Base);
2348 Set_Parent (Base, Parent (Def));
2349
2350 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2351 Set_Etype (T, Base);
2352 Set_Size_Info (T, Int_Base);
2353 Set_RM_Size (T, RM_Size (Int_Base));
2354 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2355 Set_Digits_Value (T, Digs_Val);
2356 Set_Delta_Value (T, Delta_Val);
2357 Set_Small_Value (T, Delta_Val);
2358 Set_Scalar_Range (T, Scalar_Range (Base));
2359 Set_Is_Constrained (T);
2360
2361 Check_Restriction (No_Fixed_Point, Def);
2362 end Analyze_Formal_Decimal_Fixed_Point_Type;
2363
2364 -------------------------------------------
2365 -- Analyze_Formal_Derived_Interface_Type --
2366 -------------------------------------------
2367
2368 procedure Analyze_Formal_Derived_Interface_Type
2369 (N : Node_Id;
2370 T : Entity_Id;
2371 Def : Node_Id)
2372 is
2373 Loc : constant Source_Ptr := Sloc (Def);
2374
2375 begin
2376 -- Rewrite as a type declaration of a derived type. This ensures that
2377 -- the interface list and primitive operations are properly captured.
2378
2379 Rewrite (N,
2380 Make_Full_Type_Declaration (Loc,
2381 Defining_Identifier => T,
2382 Type_Definition => Def));
2383 Analyze (N);
2384 Set_Is_Generic_Type (T);
2385 end Analyze_Formal_Derived_Interface_Type;
2386
2387 ---------------------------------
2388 -- Analyze_Formal_Derived_Type --
2389 ---------------------------------
2390
2391 procedure Analyze_Formal_Derived_Type
2392 (N : Node_Id;
2393 T : Entity_Id;
2394 Def : Node_Id)
2395 is
2396 Loc : constant Source_Ptr := Sloc (Def);
2397 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2398 New_N : Node_Id;
2399
2400 begin
2401 Set_Is_Generic_Type (T);
2402
2403 if Private_Present (Def) then
2404 New_N :=
2405 Make_Private_Extension_Declaration (Loc,
2406 Defining_Identifier => T,
2407 Discriminant_Specifications => Discriminant_Specifications (N),
2408 Unknown_Discriminants_Present => Unk_Disc,
2409 Subtype_Indication => Subtype_Mark (Def),
2410 Interface_List => Interface_List (Def));
2411
2412 Set_Abstract_Present (New_N, Abstract_Present (Def));
2413 Set_Limited_Present (New_N, Limited_Present (Def));
2414 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2415
2416 else
2417 New_N :=
2418 Make_Full_Type_Declaration (Loc,
2419 Defining_Identifier => T,
2420 Discriminant_Specifications =>
2421 Discriminant_Specifications (Parent (T)),
2422 Type_Definition =>
2423 Make_Derived_Type_Definition (Loc,
2424 Subtype_Indication => Subtype_Mark (Def)));
2425
2426 Set_Abstract_Present
2427 (Type_Definition (New_N), Abstract_Present (Def));
2428 Set_Limited_Present
2429 (Type_Definition (New_N), Limited_Present (Def));
2430 end if;
2431
2432 Rewrite (N, New_N);
2433 Analyze (N);
2434
2435 if Unk_Disc then
2436 if not Is_Composite_Type (T) then
2437 Error_Msg_N
2438 ("unknown discriminants not allowed for elementary types", N);
2439 else
2440 Set_Has_Unknown_Discriminants (T);
2441 Set_Is_Constrained (T, False);
2442 end if;
2443 end if;
2444
2445 -- If the parent type has a known size, so does the formal, which makes
2446 -- legal representation clauses that involve the formal.
2447
2448 Set_Size_Known_At_Compile_Time
2449 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2450 end Analyze_Formal_Derived_Type;
2451
2452 ----------------------------------
2453 -- Analyze_Formal_Discrete_Type --
2454 ----------------------------------
2455
2456 -- The operations defined for a discrete types are those of an enumeration
2457 -- type. The size is set to an arbitrary value, for use in analyzing the
2458 -- generic unit.
2459
2460 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2461 Loc : constant Source_Ptr := Sloc (Def);
2462 Lo : Node_Id;
2463 Hi : Node_Id;
2464
2465 Base : constant Entity_Id :=
2466 New_Internal_Entity
2467 (E_Floating_Point_Type, Current_Scope,
2468 Sloc (Defining_Identifier (Parent (Def))), 'G');
2469
2470 begin
2471 Enter_Name (T);
2472 Set_Ekind (T, E_Enumeration_Subtype);
2473 Set_Etype (T, Base);
2474 Init_Size (T, 8);
2475 Init_Alignment (T);
2476 Set_Is_Generic_Type (T);
2477 Set_Is_Constrained (T);
2478
2479 -- For semantic analysis, the bounds of the type must be set to some
2480 -- non-static value. The simplest is to create attribute nodes for those
2481 -- bounds, that refer to the type itself. These bounds are never
2482 -- analyzed but serve as place-holders.
2483
2484 Lo :=
2485 Make_Attribute_Reference (Loc,
2486 Attribute_Name => Name_First,
2487 Prefix => New_Occurrence_Of (T, Loc));
2488 Set_Etype (Lo, T);
2489
2490 Hi :=
2491 Make_Attribute_Reference (Loc,
2492 Attribute_Name => Name_Last,
2493 Prefix => New_Occurrence_Of (T, Loc));
2494 Set_Etype (Hi, T);
2495
2496 Set_Scalar_Range (T,
2497 Make_Range (Loc,
2498 Low_Bound => Lo,
2499 High_Bound => Hi));
2500
2501 Set_Ekind (Base, E_Enumeration_Type);
2502 Set_Etype (Base, Base);
2503 Init_Size (Base, 8);
2504 Init_Alignment (Base);
2505 Set_Is_Generic_Type (Base);
2506 Set_Scalar_Range (Base, Scalar_Range (T));
2507 Set_Parent (Base, Parent (Def));
2508 end Analyze_Formal_Discrete_Type;
2509
2510 ----------------------------------
2511 -- Analyze_Formal_Floating_Type --
2512 ---------------------------------
2513
2514 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2515 Base : constant Entity_Id :=
2516 New_Internal_Entity
2517 (E_Floating_Point_Type, Current_Scope,
2518 Sloc (Defining_Identifier (Parent (Def))), 'G');
2519
2520 begin
2521 -- The various semantic attributes are taken from the predefined type
2522 -- Float, just so that all of them are initialized. Their values are
2523 -- never used because no constant folding or expansion takes place in
2524 -- the generic itself.
2525
2526 Enter_Name (T);
2527 Set_Ekind (T, E_Floating_Point_Subtype);
2528 Set_Etype (T, Base);
2529 Set_Size_Info (T, (Standard_Float));
2530 Set_RM_Size (T, RM_Size (Standard_Float));
2531 Set_Digits_Value (T, Digits_Value (Standard_Float));
2532 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2533 Set_Is_Constrained (T);
2534
2535 Set_Is_Generic_Type (Base);
2536 Set_Etype (Base, Base);
2537 Set_Size_Info (Base, (Standard_Float));
2538 Set_RM_Size (Base, RM_Size (Standard_Float));
2539 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2540 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2541 Set_Parent (Base, Parent (Def));
2542
2543 Check_Restriction (No_Floating_Point, Def);
2544 end Analyze_Formal_Floating_Type;
2545
2546 -----------------------------------
2547 -- Analyze_Formal_Interface_Type;--
2548 -----------------------------------
2549
2550 procedure Analyze_Formal_Interface_Type
2551 (N : Node_Id;
2552 T : Entity_Id;
2553 Def : Node_Id)
2554 is
2555 Loc : constant Source_Ptr := Sloc (N);
2556 New_N : Node_Id;
2557
2558 begin
2559 New_N :=
2560 Make_Full_Type_Declaration (Loc,
2561 Defining_Identifier => T,
2562 Type_Definition => Def);
2563
2564 Rewrite (N, New_N);
2565 Analyze (N);
2566 Set_Is_Generic_Type (T);
2567 end Analyze_Formal_Interface_Type;
2568
2569 ---------------------------------
2570 -- Analyze_Formal_Modular_Type --
2571 ---------------------------------
2572
2573 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2574 begin
2575 -- Apart from their entity kind, generic modular types are treated like
2576 -- signed integer types, and have the same attributes.
2577
2578 Analyze_Formal_Signed_Integer_Type (T, Def);
2579 Set_Ekind (T, E_Modular_Integer_Subtype);
2580 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2581
2582 end Analyze_Formal_Modular_Type;
2583
2584 ---------------------------------------
2585 -- Analyze_Formal_Object_Declaration --
2586 ---------------------------------------
2587
2588 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2589 E : constant Node_Id := Default_Expression (N);
2590 Id : constant Node_Id := Defining_Identifier (N);
2591 K : Entity_Kind;
2592 T : Node_Id;
2593
2594 begin
2595 Enter_Name (Id);
2596
2597 -- Determine the mode of the formal object
2598
2599 if Out_Present (N) then
2600 K := E_Generic_In_Out_Parameter;
2601
2602 if not In_Present (N) then
2603 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2604 end if;
2605
2606 else
2607 K := E_Generic_In_Parameter;
2608 end if;
2609
2610 if Present (Subtype_Mark (N)) then
2611 Find_Type (Subtype_Mark (N));
2612 T := Entity (Subtype_Mark (N));
2613
2614 -- Verify that there is no redundant null exclusion
2615
2616 if Null_Exclusion_Present (N) then
2617 if not Is_Access_Type (T) then
2618 Error_Msg_N
2619 ("null exclusion can only apply to an access type", N);
2620
2621 elsif Can_Never_Be_Null (T) then
2622 Error_Msg_NE
2623 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2624 end if;
2625 end if;
2626
2627 -- Ada 2005 (AI-423): Formal object with an access definition
2628
2629 else
2630 Check_Access_Definition (N);
2631 T := Access_Definition
2632 (Related_Nod => N,
2633 N => Access_Definition (N));
2634 end if;
2635
2636 if Ekind (T) = E_Incomplete_Type then
2637 declare
2638 Error_Node : Node_Id;
2639
2640 begin
2641 if Present (Subtype_Mark (N)) then
2642 Error_Node := Subtype_Mark (N);
2643 else
2644 Check_Access_Definition (N);
2645 Error_Node := Access_Definition (N);
2646 end if;
2647
2648 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2649 end;
2650 end if;
2651
2652 if K = E_Generic_In_Parameter then
2653
2654 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2655
2656 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2657 Error_Msg_N
2658 ("generic formal of mode IN must not be of limited type", N);
2659 Explain_Limited_Type (T, N);
2660 end if;
2661
2662 if Is_Abstract_Type (T) then
2663 Error_Msg_N
2664 ("generic formal of mode IN must not be of abstract type", N);
2665 end if;
2666
2667 if Present (E) then
2668 Preanalyze_Spec_Expression (E, T);
2669
2670 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2671 Error_Msg_N
2672 ("initialization not allowed for limited types", E);
2673 Explain_Limited_Type (T, E);
2674 end if;
2675 end if;
2676
2677 Set_Ekind (Id, K);
2678 Set_Etype (Id, T);
2679
2680 -- Case of generic IN OUT parameter
2681
2682 else
2683 -- If the formal has an unconstrained type, construct its actual
2684 -- subtype, as is done for subprogram formals. In this fashion, all
2685 -- its uses can refer to specific bounds.
2686
2687 Set_Ekind (Id, K);
2688 Set_Etype (Id, T);
2689
2690 if (Is_Array_Type (T) and then not Is_Constrained (T))
2691 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2692 then
2693 declare
2694 Non_Freezing_Ref : constant Node_Id :=
2695 New_Occurrence_Of (Id, Sloc (Id));
2696 Decl : Node_Id;
2697
2698 begin
2699 -- Make sure the actual subtype doesn't generate bogus freezing
2700
2701 Set_Must_Not_Freeze (Non_Freezing_Ref);
2702 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2703 Insert_Before_And_Analyze (N, Decl);
2704 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2705 end;
2706 else
2707 Set_Actual_Subtype (Id, T);
2708 end if;
2709
2710 if Present (E) then
2711 Error_Msg_N
2712 ("initialization not allowed for `IN OUT` formals", N);
2713 end if;
2714 end if;
2715
2716 if Has_Aspects (N) then
2717 Analyze_Aspect_Specifications (N, Id);
2718 end if;
2719 end Analyze_Formal_Object_Declaration;
2720
2721 ----------------------------------------------
2722 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2723 ----------------------------------------------
2724
2725 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2726 (T : Entity_Id;
2727 Def : Node_Id)
2728 is
2729 Loc : constant Source_Ptr := Sloc (Def);
2730 Base : constant Entity_Id :=
2731 New_Internal_Entity
2732 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2733 Sloc (Defining_Identifier (Parent (Def))), 'G');
2734
2735 begin
2736 -- The semantic attributes are set for completeness only, their values
2737 -- will never be used, since all properties of the type are non-static.
2738
2739 Enter_Name (T);
2740 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2741 Set_Etype (T, Base);
2742 Set_Size_Info (T, Standard_Integer);
2743 Set_RM_Size (T, RM_Size (Standard_Integer));
2744 Set_Small_Value (T, Ureal_1);
2745 Set_Delta_Value (T, Ureal_1);
2746 Set_Scalar_Range (T,
2747 Make_Range (Loc,
2748 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2749 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2750 Set_Is_Constrained (T);
2751
2752 Set_Is_Generic_Type (Base);
2753 Set_Etype (Base, Base);
2754 Set_Size_Info (Base, Standard_Integer);
2755 Set_RM_Size (Base, RM_Size (Standard_Integer));
2756 Set_Small_Value (Base, Ureal_1);
2757 Set_Delta_Value (Base, Ureal_1);
2758 Set_Scalar_Range (Base, Scalar_Range (T));
2759 Set_Parent (Base, Parent (Def));
2760
2761 Check_Restriction (No_Fixed_Point, Def);
2762 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2763
2764 ----------------------------------------
2765 -- Analyze_Formal_Package_Declaration --
2766 ----------------------------------------
2767
2768 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2769 Gen_Id : constant Node_Id := Name (N);
2770 Loc : constant Source_Ptr := Sloc (N);
2771 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2772 Formal : Entity_Id;
2773 Gen_Decl : Node_Id;
2774 Gen_Unit : Entity_Id;
2775 Renaming : Node_Id;
2776
2777 Vis_Prims_List : Elist_Id := No_Elist;
2778 -- List of primitives made temporarily visible in the instantiation
2779 -- to match the visibility of the formal type.
2780
2781 function Build_Local_Package return Node_Id;
2782 -- The formal package is rewritten so that its parameters are replaced
2783 -- with corresponding declarations. For parameters with bona fide
2784 -- associations these declarations are created by Analyze_Associations
2785 -- as for a regular instantiation. For boxed parameters, we preserve
2786 -- the formal declarations and analyze them, in order to introduce
2787 -- entities of the right kind in the environment of the formal.
2788
2789 -------------------------
2790 -- Build_Local_Package --
2791 -------------------------
2792
2793 function Build_Local_Package return Node_Id is
2794 Decls : List_Id;
2795 Pack_Decl : Node_Id;
2796
2797 begin
2798 -- Within the formal, the name of the generic package is a renaming
2799 -- of the formal (as for a regular instantiation).
2800
2801 Pack_Decl :=
2802 Make_Package_Declaration (Loc,
2803 Specification =>
2804 Copy_Generic_Node
2805 (Specification (Original_Node (Gen_Decl)),
2806 Empty, Instantiating => True));
2807
2808 Renaming :=
2809 Make_Package_Renaming_Declaration (Loc,
2810 Defining_Unit_Name =>
2811 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2812 Name => New_Occurrence_Of (Formal, Loc));
2813
2814 if Nkind (Gen_Id) = N_Identifier
2815 and then Chars (Gen_Id) = Chars (Pack_Id)
2816 then
2817 Error_Msg_NE
2818 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2819 end if;
2820
2821 -- If the formal is declared with a box, or with an others choice,
2822 -- create corresponding declarations for all entities in the formal
2823 -- part, so that names with the proper types are available in the
2824 -- specification of the formal package.
2825
2826 -- On the other hand, if there are no associations, then all the
2827 -- formals must have defaults, and this will be checked by the
2828 -- call to Analyze_Associations.
2829
2830 if Box_Present (N)
2831 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2832 then
2833 declare
2834 Formal_Decl : Node_Id;
2835
2836 begin
2837 -- TBA : for a formal package, need to recurse ???
2838
2839 Decls := New_List;
2840 Formal_Decl :=
2841 First
2842 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2843 while Present (Formal_Decl) loop
2844 Append_To
2845 (Decls,
2846 Copy_Generic_Node
2847 (Formal_Decl, Empty, Instantiating => True));
2848 Next (Formal_Decl);
2849 end loop;
2850 end;
2851
2852 -- If generic associations are present, use Analyze_Associations to
2853 -- create the proper renaming declarations.
2854
2855 else
2856 declare
2857 Act_Tree : constant Node_Id :=
2858 Copy_Generic_Node
2859 (Original_Node (Gen_Decl), Empty,
2860 Instantiating => True);
2861
2862 begin
2863 Generic_Renamings.Set_Last (0);
2864 Generic_Renamings_HTable.Reset;
2865 Instantiation_Node := N;
2866
2867 Decls :=
2868 Analyze_Associations
2869 (I_Node => Original_Node (N),
2870 Formals => Generic_Formal_Declarations (Act_Tree),
2871 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2872
2873 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2874 end;
2875 end if;
2876
2877 Append (Renaming, To => Decls);
2878
2879 -- Add generated declarations ahead of local declarations in
2880 -- the package.
2881
2882 if No (Visible_Declarations (Specification (Pack_Decl))) then
2883 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2884 else
2885 Insert_List_Before
2886 (First (Visible_Declarations (Specification (Pack_Decl))),
2887 Decls);
2888 end if;
2889
2890 return Pack_Decl;
2891 end Build_Local_Package;
2892
2893 -- Local variables
2894
2895 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2896 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2897
2898 Associations : Boolean := True;
2899 New_N : Node_Id;
2900 Parent_Installed : Boolean := False;
2901 Parent_Instance : Entity_Id;
2902 Renaming_In_Par : Entity_Id;
2903
2904 -- Start of processing for Analyze_Formal_Package_Declaration
2905
2906 begin
2907 Check_Text_IO_Special_Unit (Gen_Id);
2908
2909 Init_Env;
2910 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2911 Gen_Unit := Entity (Gen_Id);
2912
2913 -- Check for a formal package that is a package renaming
2914
2915 if Present (Renamed_Object (Gen_Unit)) then
2916
2917 -- Indicate that unit is used, before replacing it with renamed
2918 -- entity for use below.
2919
2920 if In_Extended_Main_Source_Unit (N) then
2921 Set_Is_Instantiated (Gen_Unit);
2922 Generate_Reference (Gen_Unit, N);
2923 end if;
2924
2925 Gen_Unit := Renamed_Object (Gen_Unit);
2926 end if;
2927
2928 if Ekind (Gen_Unit) /= E_Generic_Package then
2929 Error_Msg_N ("expect generic package name", Gen_Id);
2930 Restore_Env;
2931 goto Leave;
2932
2933 elsif Gen_Unit = Current_Scope then
2934 Error_Msg_N
2935 ("generic package cannot be used as a formal package of itself",
2936 Gen_Id);
2937 Restore_Env;
2938 goto Leave;
2939
2940 elsif In_Open_Scopes (Gen_Unit) then
2941 if Is_Compilation_Unit (Gen_Unit)
2942 and then Is_Child_Unit (Current_Scope)
2943 then
2944 -- Special-case the error when the formal is a parent, and
2945 -- continue analysis to minimize cascaded errors.
2946
2947 Error_Msg_N
2948 ("generic parent cannot be used as formal package of a child "
2949 & "unit", Gen_Id);
2950
2951 else
2952 Error_Msg_N
2953 ("generic package cannot be used as a formal package within "
2954 & "itself", Gen_Id);
2955 Restore_Env;
2956 goto Leave;
2957 end if;
2958 end if;
2959
2960 -- Check that name of formal package does not hide name of generic,
2961 -- or its leading prefix. This check must be done separately because
2962 -- the name of the generic has already been analyzed.
2963
2964 declare
2965 Gen_Name : Entity_Id;
2966
2967 begin
2968 Gen_Name := Gen_Id;
2969 while Nkind (Gen_Name) = N_Expanded_Name loop
2970 Gen_Name := Prefix (Gen_Name);
2971 end loop;
2972
2973 if Chars (Gen_Name) = Chars (Pack_Id) then
2974 Error_Msg_NE
2975 ("& is hidden within declaration of formal package",
2976 Gen_Id, Gen_Name);
2977 end if;
2978 end;
2979
2980 if Box_Present (N)
2981 or else No (Generic_Associations (N))
2982 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2983 then
2984 Associations := False;
2985 end if;
2986
2987 -- If there are no generic associations, the generic parameters appear
2988 -- as local entities and are instantiated like them. We copy the generic
2989 -- package declaration as if it were an instantiation, and analyze it
2990 -- like a regular package, except that we treat the formals as
2991 -- additional visible components.
2992
2993 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2994
2995 if In_Extended_Main_Source_Unit (N) then
2996 Set_Is_Instantiated (Gen_Unit);
2997 Generate_Reference (Gen_Unit, N);
2998 end if;
2999
3000 Formal := New_Copy (Pack_Id);
3001 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3002
3003 -- Make local generic without formals. The formals will be replaced with
3004 -- internal declarations.
3005
3006 begin
3007 New_N := Build_Local_Package;
3008
3009 -- If there are errors in the parameter list, Analyze_Associations
3010 -- raises Instantiation_Error. Patch the declaration to prevent further
3011 -- exception propagation.
3012
3013 exception
3014 when Instantiation_Error =>
3015 Enter_Name (Formal);
3016 Set_Ekind (Formal, E_Variable);
3017 Set_Etype (Formal, Any_Type);
3018 Restore_Hidden_Primitives (Vis_Prims_List);
3019
3020 if Parent_Installed then
3021 Remove_Parent;
3022 end if;
3023
3024 goto Leave;
3025 end;
3026
3027 Rewrite (N, New_N);
3028 Set_Defining_Unit_Name (Specification (New_N), Formal);
3029 Set_Generic_Parent (Specification (N), Gen_Unit);
3030 Set_Instance_Env (Gen_Unit, Formal);
3031 Set_Is_Generic_Instance (Formal);
3032
3033 Enter_Name (Formal);
3034 Set_Ekind (Formal, E_Package);
3035 Set_Etype (Formal, Standard_Void_Type);
3036 Set_Inner_Instances (Formal, New_Elmt_List);
3037
3038 -- It is unclear that any aspects can apply to a formal package
3039 -- declaration, given that they look like a hidden conformance
3040 -- requirement on the corresponding actual. However, Abstract_State
3041 -- must be treated specially because it generates declarations that
3042 -- must appear before other declarations in the specification and
3043 -- must be analyzed at once.
3044
3045 if Present (Aspect_Specifications (Gen_Decl)) then
3046 if No (Aspect_Specifications (N)) then
3047 Set_Aspect_Specifications (N, New_List);
3048 Set_Has_Aspects (N);
3049 end if;
3050
3051 declare
3052 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3053 New_A : Node_Id;
3054
3055 begin
3056 while Present (ASN) loop
3057 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3058 New_A :=
3059 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3060 Set_Entity (New_A, Formal);
3061 Set_Analyzed (New_A, False);
3062 Append (New_A, Aspect_Specifications (N));
3063 Analyze_Aspect_Specifications (N, Formal);
3064 exit;
3065 end if;
3066
3067 Next (ASN);
3068 end loop;
3069 end;
3070 end if;
3071
3072 Push_Scope (Formal);
3073
3074 -- Manually set the SPARK_Mode from the context because the package
3075 -- declaration is never analyzed.
3076
3077 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3078 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3079 Set_SPARK_Pragma_Inherited (Formal);
3080 Set_SPARK_Aux_Pragma_Inherited (Formal);
3081
3082 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3083
3084 -- Similarly, we have to make the name of the formal visible in the
3085 -- parent instance, to resolve properly fully qualified names that
3086 -- may appear in the generic unit. The parent instance has been
3087 -- placed on the scope stack ahead of the current scope.
3088
3089 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3090
3091 Renaming_In_Par :=
3092 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3093 Set_Ekind (Renaming_In_Par, E_Package);
3094 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3095 Set_Scope (Renaming_In_Par, Parent_Instance);
3096 Set_Parent (Renaming_In_Par, Parent (Formal));
3097 Set_Renamed_Object (Renaming_In_Par, Formal);
3098 Append_Entity (Renaming_In_Par, Parent_Instance);
3099 end if;
3100
3101 -- A formal package declaration behaves as a package instantiation with
3102 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3103 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3104 -- all SPARK_Mode pragmas within the generic_package_name.
3105
3106 if SPARK_Mode /= On then
3107 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3108
3109 -- Mark the formal spec in case the body is instantiated at a later
3110 -- pass. This preserves the original context in effect for the body.
3111
3112 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3113 end if;
3114
3115 Analyze (Specification (N));
3116
3117 -- The formals for which associations are provided are not visible
3118 -- outside of the formal package. The others are still declared by a
3119 -- formal parameter declaration.
3120
3121 -- If there are no associations, the only local entity to hide is the
3122 -- generated package renaming itself.
3123
3124 declare
3125 E : Entity_Id;
3126
3127 begin
3128 E := First_Entity (Formal);
3129 while Present (E) loop
3130 if Associations and then not Is_Generic_Formal (E) then
3131 Set_Is_Hidden (E);
3132 end if;
3133
3134 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3135 Set_Is_Hidden (E);
3136 exit;
3137 end if;
3138
3139 Next_Entity (E);
3140 end loop;
3141 end;
3142
3143 End_Package_Scope (Formal);
3144 Restore_Hidden_Primitives (Vis_Prims_List);
3145
3146 if Parent_Installed then
3147 Remove_Parent;
3148 end if;
3149
3150 Restore_Env;
3151
3152 -- Inside the generic unit, the formal package is a regular package, but
3153 -- no body is needed for it. Note that after instantiation, the defining
3154 -- unit name we need is in the new tree and not in the original (see
3155 -- Package_Instantiation). A generic formal package is an instance, and
3156 -- can be used as an actual for an inner instance.
3157
3158 Set_Has_Completion (Formal, True);
3159
3160 -- Add semantic information to the original defining identifier.
3161
3162 Set_Ekind (Pack_Id, E_Package);
3163 Set_Etype (Pack_Id, Standard_Void_Type);
3164 Set_Scope (Pack_Id, Scope (Formal));
3165 Set_Has_Completion (Pack_Id, True);
3166
3167 <<Leave>>
3168 if Has_Aspects (N) then
3169 -- Unclear that any other aspects may appear here, snalyze them
3170 -- for completion, given that the grammar allows their appearance.
3171
3172 Analyze_Aspect_Specifications (N, Pack_Id);
3173 end if;
3174
3175 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3176 end Analyze_Formal_Package_Declaration;
3177
3178 ---------------------------------
3179 -- Analyze_Formal_Private_Type --
3180 ---------------------------------
3181
3182 procedure Analyze_Formal_Private_Type
3183 (N : Node_Id;
3184 T : Entity_Id;
3185 Def : Node_Id)
3186 is
3187 begin
3188 New_Private_Type (N, T, Def);
3189
3190 -- Set the size to an arbitrary but legal value
3191
3192 Set_Size_Info (T, Standard_Integer);
3193 Set_RM_Size (T, RM_Size (Standard_Integer));
3194 end Analyze_Formal_Private_Type;
3195
3196 ------------------------------------
3197 -- Analyze_Formal_Incomplete_Type --
3198 ------------------------------------
3199
3200 procedure Analyze_Formal_Incomplete_Type
3201 (T : Entity_Id;
3202 Def : Node_Id)
3203 is
3204 begin
3205 Enter_Name (T);
3206 Set_Ekind (T, E_Incomplete_Type);
3207 Set_Etype (T, T);
3208 Set_Private_Dependents (T, New_Elmt_List);
3209
3210 if Tagged_Present (Def) then
3211 Set_Is_Tagged_Type (T);
3212 Make_Class_Wide_Type (T);
3213 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3214 end if;
3215 end Analyze_Formal_Incomplete_Type;
3216
3217 ----------------------------------------
3218 -- Analyze_Formal_Signed_Integer_Type --
3219 ----------------------------------------
3220
3221 procedure Analyze_Formal_Signed_Integer_Type
3222 (T : Entity_Id;
3223 Def : Node_Id)
3224 is
3225 Base : constant Entity_Id :=
3226 New_Internal_Entity
3227 (E_Signed_Integer_Type,
3228 Current_Scope,
3229 Sloc (Defining_Identifier (Parent (Def))), 'G');
3230
3231 begin
3232 Enter_Name (T);
3233
3234 Set_Ekind (T, E_Signed_Integer_Subtype);
3235 Set_Etype (T, Base);
3236 Set_Size_Info (T, Standard_Integer);
3237 Set_RM_Size (T, RM_Size (Standard_Integer));
3238 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3239 Set_Is_Constrained (T);
3240
3241 Set_Is_Generic_Type (Base);
3242 Set_Size_Info (Base, Standard_Integer);
3243 Set_RM_Size (Base, RM_Size (Standard_Integer));
3244 Set_Etype (Base, Base);
3245 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3246 Set_Parent (Base, Parent (Def));
3247 end Analyze_Formal_Signed_Integer_Type;
3248
3249 -------------------------------------------
3250 -- Analyze_Formal_Subprogram_Declaration --
3251 -------------------------------------------
3252
3253 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3254 Spec : constant Node_Id := Specification (N);
3255 Def : constant Node_Id := Default_Name (N);
3256 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3257 Subp : Entity_Id;
3258
3259 begin
3260 if Nam = Error then
3261 return;
3262 end if;
3263
3264 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3265 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3266 goto Leave;
3267 end if;
3268
3269 Analyze_Subprogram_Declaration (N);
3270 Set_Is_Formal_Subprogram (Nam);
3271 Set_Has_Completion (Nam);
3272
3273 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3274 Set_Is_Abstract_Subprogram (Nam);
3275
3276 Set_Is_Dispatching_Operation (Nam);
3277
3278 -- A formal abstract procedure cannot have a null default
3279 -- (RM 12.6(4.1/2)).
3280
3281 if Nkind (Spec) = N_Procedure_Specification
3282 and then Null_Present (Spec)
3283 then
3284 Error_Msg_N
3285 ("a formal abstract subprogram cannot default to null", Spec);
3286 end if;
3287
3288 declare
3289 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3290 begin
3291 if No (Ctrl_Type) then
3292 Error_Msg_N
3293 ("abstract formal subprogram must have a controlling type",
3294 N);
3295
3296 elsif Ada_Version >= Ada_2012
3297 and then Is_Incomplete_Type (Ctrl_Type)
3298 then
3299 Error_Msg_NE
3300 ("controlling type of abstract formal subprogram cannot "
3301 & "be incomplete type", N, Ctrl_Type);
3302
3303 else
3304 Check_Controlling_Formals (Ctrl_Type, Nam);
3305 end if;
3306 end;
3307 end if;
3308
3309 -- Default name is resolved at the point of instantiation
3310
3311 if Box_Present (N) then
3312 null;
3313
3314 -- Else default is bound at the point of generic declaration
3315
3316 elsif Present (Def) then
3317 if Nkind (Def) = N_Operator_Symbol then
3318 Find_Direct_Name (Def);
3319
3320 elsif Nkind (Def) /= N_Attribute_Reference then
3321 Analyze (Def);
3322
3323 else
3324 -- For an attribute reference, analyze the prefix and verify
3325 -- that it has the proper profile for the subprogram.
3326
3327 Analyze (Prefix (Def));
3328 Valid_Default_Attribute (Nam, Def);
3329 goto Leave;
3330 end if;
3331
3332 -- Default name may be overloaded, in which case the interpretation
3333 -- with the correct profile must be selected, as for a renaming.
3334 -- If the definition is an indexed component, it must denote a
3335 -- member of an entry family. If it is a selected component, it
3336 -- can be a protected operation.
3337
3338 if Etype (Def) = Any_Type then
3339 goto Leave;
3340
3341 elsif Nkind (Def) = N_Selected_Component then
3342 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3343 Error_Msg_N ("expect valid subprogram name as default", Def);
3344 end if;
3345
3346 elsif Nkind (Def) = N_Indexed_Component then
3347 if Is_Entity_Name (Prefix (Def)) then
3348 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3349 Error_Msg_N ("expect valid subprogram name as default", Def);
3350 end if;
3351
3352 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3353 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3354 E_Entry_Family
3355 then
3356 Error_Msg_N ("expect valid subprogram name as default", Def);
3357 end if;
3358
3359 else
3360 Error_Msg_N ("expect valid subprogram name as default", Def);
3361 goto Leave;
3362 end if;
3363
3364 elsif Nkind (Def) = N_Character_Literal then
3365
3366 -- Needs some type checks: subprogram should be parameterless???
3367
3368 Resolve (Def, (Etype (Nam)));
3369
3370 elsif not Is_Entity_Name (Def)
3371 or else not Is_Overloadable (Entity (Def))
3372 then
3373 Error_Msg_N ("expect valid subprogram name as default", Def);
3374 goto Leave;
3375
3376 elsif not Is_Overloaded (Def) then
3377 Subp := Entity (Def);
3378
3379 if Subp = Nam then
3380 Error_Msg_N ("premature usage of formal subprogram", Def);
3381
3382 elsif not Entity_Matches_Spec (Subp, Nam) then
3383 Error_Msg_N ("no visible entity matches specification", Def);
3384 end if;
3385
3386 -- More than one interpretation, so disambiguate as for a renaming
3387
3388 else
3389 declare
3390 I : Interp_Index;
3391 I1 : Interp_Index := 0;
3392 It : Interp;
3393 It1 : Interp;
3394
3395 begin
3396 Subp := Any_Id;
3397 Get_First_Interp (Def, I, It);
3398 while Present (It.Nam) loop
3399 if Entity_Matches_Spec (It.Nam, Nam) then
3400 if Subp /= Any_Id then
3401 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3402
3403 if It1 = No_Interp then
3404 Error_Msg_N ("ambiguous default subprogram", Def);
3405 else
3406 Subp := It1.Nam;
3407 end if;
3408
3409 exit;
3410
3411 else
3412 I1 := I;
3413 Subp := It.Nam;
3414 end if;
3415 end if;
3416
3417 Get_Next_Interp (I, It);
3418 end loop;
3419 end;
3420
3421 if Subp /= Any_Id then
3422
3423 -- Subprogram found, generate reference to it
3424
3425 Set_Entity (Def, Subp);
3426 Generate_Reference (Subp, Def);
3427
3428 if Subp = Nam then
3429 Error_Msg_N ("premature usage of formal subprogram", Def);
3430
3431 elsif Ekind (Subp) /= E_Operator then
3432 Check_Mode_Conformant (Subp, Nam);
3433 end if;
3434
3435 else
3436 Error_Msg_N ("no visible subprogram matches specification", N);
3437 end if;
3438 end if;
3439 end if;
3440
3441 <<Leave>>
3442 if Has_Aspects (N) then
3443 Analyze_Aspect_Specifications (N, Nam);
3444 end if;
3445
3446 end Analyze_Formal_Subprogram_Declaration;
3447
3448 -------------------------------------
3449 -- Analyze_Formal_Type_Declaration --
3450 -------------------------------------
3451
3452 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3453 Def : constant Node_Id := Formal_Type_Definition (N);
3454 T : Entity_Id;
3455
3456 begin
3457 T := Defining_Identifier (N);
3458
3459 if Present (Discriminant_Specifications (N))
3460 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3461 then
3462 Error_Msg_N
3463 ("discriminants not allowed for this formal type", T);
3464 end if;
3465
3466 -- Enter the new name, and branch to specific routine
3467
3468 case Nkind (Def) is
3469 when N_Formal_Private_Type_Definition =>
3470 Analyze_Formal_Private_Type (N, T, Def);
3471
3472 when N_Formal_Derived_Type_Definition =>
3473 Analyze_Formal_Derived_Type (N, T, Def);
3474
3475 when N_Formal_Incomplete_Type_Definition =>
3476 Analyze_Formal_Incomplete_Type (T, Def);
3477
3478 when N_Formal_Discrete_Type_Definition =>
3479 Analyze_Formal_Discrete_Type (T, Def);
3480
3481 when N_Formal_Signed_Integer_Type_Definition =>
3482 Analyze_Formal_Signed_Integer_Type (T, Def);
3483
3484 when N_Formal_Modular_Type_Definition =>
3485 Analyze_Formal_Modular_Type (T, Def);
3486
3487 when N_Formal_Floating_Point_Definition =>
3488 Analyze_Formal_Floating_Type (T, Def);
3489
3490 when N_Formal_Ordinary_Fixed_Point_Definition =>
3491 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3492
3493 when N_Formal_Decimal_Fixed_Point_Definition =>
3494 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3495
3496 when N_Array_Type_Definition =>
3497 Analyze_Formal_Array_Type (T, Def);
3498
3499 when N_Access_Function_Definition
3500 | N_Access_Procedure_Definition
3501 | N_Access_To_Object_Definition
3502 =>
3503 Analyze_Generic_Access_Type (T, Def);
3504
3505 -- Ada 2005: a interface declaration is encoded as an abstract
3506 -- record declaration or a abstract type derivation.
3507
3508 when N_Record_Definition =>
3509 Analyze_Formal_Interface_Type (N, T, Def);
3510
3511 when N_Derived_Type_Definition =>
3512 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3513
3514 when N_Error =>
3515 null;
3516
3517 when others =>
3518 raise Program_Error;
3519 end case;
3520
3521 -- A formal type declaration declares a type and its first
3522 -- subtype.
3523
3524 Set_Is_Generic_Type (T);
3525 Set_Is_First_Subtype (T);
3526
3527 if Has_Aspects (N) then
3528 Analyze_Aspect_Specifications (N, T);
3529 end if;
3530 end Analyze_Formal_Type_Declaration;
3531
3532 ------------------------------------
3533 -- Analyze_Function_Instantiation --
3534 ------------------------------------
3535
3536 procedure Analyze_Function_Instantiation (N : Node_Id) is
3537 begin
3538 Analyze_Subprogram_Instantiation (N, E_Function);
3539 end Analyze_Function_Instantiation;
3540
3541 ---------------------------------
3542 -- Analyze_Generic_Access_Type --
3543 ---------------------------------
3544
3545 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3546 begin
3547 Enter_Name (T);
3548
3549 if Nkind (Def) = N_Access_To_Object_Definition then
3550 Access_Type_Declaration (T, Def);
3551
3552 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3553 and then No (Full_View (Designated_Type (T)))
3554 and then not Is_Generic_Type (Designated_Type (T))
3555 then
3556 Error_Msg_N ("premature usage of incomplete type", Def);
3557
3558 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3559 Error_Msg_N
3560 ("only a subtype mark is allowed in a formal", Def);
3561 end if;
3562
3563 else
3564 Access_Subprogram_Declaration (T, Def);
3565 end if;
3566 end Analyze_Generic_Access_Type;
3567
3568 ---------------------------------
3569 -- Analyze_Generic_Formal_Part --
3570 ---------------------------------
3571
3572 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3573 Gen_Parm_Decl : Node_Id;
3574
3575 begin
3576 -- The generic formals are processed in the scope of the generic unit,
3577 -- where they are immediately visible. The scope is installed by the
3578 -- caller.
3579
3580 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3581 while Present (Gen_Parm_Decl) loop
3582 Analyze (Gen_Parm_Decl);
3583 Next (Gen_Parm_Decl);
3584 end loop;
3585
3586 Generate_Reference_To_Generic_Formals (Current_Scope);
3587
3588 -- For Ada 2020, some formal parameters can carry aspects, which must
3589 -- be name-resolved at the end of the list of formal parameters (which
3590 -- has the semantics of a declaration list).
3591
3592 Analyze_Contracts (Generic_Formal_Declarations (N));
3593 end Analyze_Generic_Formal_Part;
3594
3595 ------------------------------------------
3596 -- Analyze_Generic_Package_Declaration --
3597 ------------------------------------------
3598
3599 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3600 Decls : constant List_Id := Visible_Declarations (Specification (N));
3601 Loc : constant Source_Ptr := Sloc (N);
3602
3603 Decl : Node_Id;
3604 Id : Entity_Id;
3605 New_N : Node_Id;
3606 Renaming : Node_Id;
3607 Save_Parent : Node_Id;
3608
3609 begin
3610 -- A generic may grant access to its private enclosing context depending
3611 -- on the placement of its corresponding body. From elaboration point of
3612 -- view, the flow of execution may enter this private context, and then
3613 -- reach an external unit, thus producing a dependency on that external
3614 -- unit. For such a path to be properly discovered and encoded in the
3615 -- ALI file of the main unit, let the ABE mechanism process the body of
3616 -- the main unit, and encode all relevant invocation constructs and the
3617 -- relations between them.
3618
3619 Mark_Save_Invocation_Graph_Of_Body;
3620
3621 -- We introduce a renaming of the enclosing package, to have a usable
3622 -- entity as the prefix of an expanded name for a local entity of the
3623 -- form Par.P.Q, where P is the generic package. This is because a local
3624 -- entity named P may hide it, so that the usual visibility rules in
3625 -- the instance will not resolve properly.
3626
3627 Renaming :=
3628 Make_Package_Renaming_Declaration (Loc,
3629 Defining_Unit_Name =>
3630 Make_Defining_Identifier (Loc,
3631 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3632 Name =>
3633 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3634
3635 -- The declaration is inserted before other declarations, but before
3636 -- pragmas that may be library-unit pragmas and must appear before other
3637 -- declarations. The pragma Compile_Time_Error is not in this class, and
3638 -- may contain an expression that includes such a qualified name, so the
3639 -- renaming declaration must appear before it.
3640
3641 -- Are there other pragmas that require this special handling ???
3642
3643 if Present (Decls) then
3644 Decl := First (Decls);
3645 while Present (Decl)
3646 and then Nkind (Decl) = N_Pragma
3647 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3648 loop
3649 Next (Decl);
3650 end loop;
3651
3652 if Present (Decl) then
3653 Insert_Before (Decl, Renaming);
3654 else
3655 Append (Renaming, Visible_Declarations (Specification (N)));
3656 end if;
3657
3658 else
3659 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3660 end if;
3661
3662 -- Create copy of generic unit, and save for instantiation. If the unit
3663 -- is a child unit, do not copy the specifications for the parent, which
3664 -- are not part of the generic tree.
3665
3666 Save_Parent := Parent_Spec (N);
3667 Set_Parent_Spec (N, Empty);
3668
3669 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3670 Set_Parent_Spec (New_N, Save_Parent);
3671 Rewrite (N, New_N);
3672
3673 -- Once the contents of the generic copy and the template are swapped,
3674 -- do the same for their respective aspect specifications.
3675
3676 Exchange_Aspects (N, New_N);
3677
3678 -- Collect all contract-related source pragmas found within the template
3679 -- and attach them to the contract of the package spec. This contract is
3680 -- used in the capture of global references within annotations.
3681
3682 Create_Generic_Contract (N);
3683
3684 Id := Defining_Entity (N);
3685 Generate_Definition (Id);
3686
3687 -- Expansion is not applied to generic units
3688
3689 Start_Generic;
3690
3691 Enter_Name (Id);
3692 Set_Ekind (Id, E_Generic_Package);
3693 Set_Etype (Id, Standard_Void_Type);
3694
3695 -- Set SPARK_Mode from context
3696
3697 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3698 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3699 Set_SPARK_Pragma_Inherited (Id);
3700 Set_SPARK_Aux_Pragma_Inherited (Id);
3701
3702 -- Preserve relevant elaboration-related attributes of the context which
3703 -- are no longer available or very expensive to recompute once analysis,
3704 -- resolution, and expansion are over.
3705
3706 Mark_Elaboration_Attributes
3707 (N_Id => Id,
3708 Checks => True,
3709 Warnings => True);
3710
3711 -- Analyze aspects now, so that generated pragmas appear in the
3712 -- declarations before building and analyzing the generic copy.
3713
3714 if Has_Aspects (N) then
3715 Analyze_Aspect_Specifications (N, Id);
3716 end if;
3717
3718 Push_Scope (Id);
3719 Enter_Generic_Scope (Id);
3720 Set_Inner_Instances (Id, New_Elmt_List);
3721
3722 Set_Categorization_From_Pragmas (N);
3723 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3724
3725 -- Link the declaration of the generic homonym in the generic copy to
3726 -- the package it renames, so that it is always resolved properly.
3727
3728 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3729 Set_Entity (Associated_Node (Name (Renaming)), Id);
3730
3731 -- For a library unit, we have reconstructed the entity for the unit,
3732 -- and must reset it in the library tables.
3733
3734 if Nkind (Parent (N)) = N_Compilation_Unit then
3735 Set_Cunit_Entity (Current_Sem_Unit, Id);
3736 end if;
3737
3738 Analyze_Generic_Formal_Part (N);
3739
3740 -- After processing the generic formals, analysis proceeds as for a
3741 -- non-generic package.
3742
3743 Analyze (Specification (N));
3744
3745 Validate_Categorization_Dependency (N, Id);
3746
3747 End_Generic;
3748
3749 End_Package_Scope (Id);
3750 Exit_Generic_Scope (Id);
3751
3752 -- If the generic appears within a package unit, the body of that unit
3753 -- has to be present for instantiation and inlining.
3754
3755 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3756 Set_Body_Needed_For_Inlining
3757 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3758 end if;
3759
3760 if Nkind (Parent (N)) /= N_Compilation_Unit then
3761 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3762 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3763 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3764
3765 else
3766 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3767 Validate_RT_RAT_Component (N);
3768
3769 -- If this is a spec without a body, check that generic parameters
3770 -- are referenced.
3771
3772 if not Body_Required (Parent (N)) then
3773 Check_References (Id);
3774 end if;
3775 end if;
3776
3777 -- If there is a specified storage pool in the context, create an
3778 -- aspect on the package declaration, so that it is used in any
3779 -- instance that does not override it.
3780
3781 if Present (Default_Pool) then
3782 declare
3783 ASN : Node_Id;
3784
3785 begin
3786 ASN :=
3787 Make_Aspect_Specification (Loc,
3788 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3789 Expression => New_Copy (Default_Pool));
3790
3791 if No (Aspect_Specifications (Specification (N))) then
3792 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3793 else
3794 Append (ASN, Aspect_Specifications (Specification (N)));
3795 end if;
3796 end;
3797 end if;
3798 end Analyze_Generic_Package_Declaration;
3799
3800 --------------------------------------------
3801 -- Analyze_Generic_Subprogram_Declaration --
3802 --------------------------------------------
3803
3804 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3805 Formals : List_Id;
3806 Id : Entity_Id;
3807 New_N : Node_Id;
3808 Result_Type : Entity_Id;
3809 Save_Parent : Node_Id;
3810 Spec : Node_Id;
3811 Typ : Entity_Id;
3812
3813 begin
3814 -- A generic may grant access to its private enclosing context depending
3815 -- on the placement of its corresponding body. From elaboration point of
3816 -- view, the flow of execution may enter this private context, and then
3817 -- reach an external unit, thus producing a dependency on that external
3818 -- unit. For such a path to be properly discovered and encoded in the
3819 -- ALI file of the main unit, let the ABE mechanism process the body of
3820 -- the main unit, and encode all relevant invocation constructs and the
3821 -- relations between them.
3822
3823 Mark_Save_Invocation_Graph_Of_Body;
3824
3825 -- Create copy of generic unit, and save for instantiation. If the unit
3826 -- is a child unit, do not copy the specifications for the parent, which
3827 -- are not part of the generic tree.
3828
3829 Save_Parent := Parent_Spec (N);
3830 Set_Parent_Spec (N, Empty);
3831
3832 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3833 Set_Parent_Spec (New_N, Save_Parent);
3834 Rewrite (N, New_N);
3835
3836 -- Once the contents of the generic copy and the template are swapped,
3837 -- do the same for their respective aspect specifications.
3838
3839 Exchange_Aspects (N, New_N);
3840
3841 -- Collect all contract-related source pragmas found within the template
3842 -- and attach them to the contract of the subprogram spec. This contract
3843 -- is used in the capture of global references within annotations.
3844
3845 Create_Generic_Contract (N);
3846
3847 Spec := Specification (N);
3848 Id := Defining_Entity (Spec);
3849 Generate_Definition (Id);
3850
3851 if Nkind (Id) = N_Defining_Operator_Symbol then
3852 Error_Msg_N
3853 ("operator symbol not allowed for generic subprogram", Id);
3854 end if;
3855
3856 Start_Generic;
3857
3858 Enter_Name (Id);
3859 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3860
3861 Push_Scope (Id);
3862 Enter_Generic_Scope (Id);
3863 Set_Inner_Instances (Id, New_Elmt_List);
3864 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3865
3866 Analyze_Generic_Formal_Part (N);
3867
3868 if Nkind (Spec) = N_Function_Specification then
3869 Set_Ekind (Id, E_Generic_Function);
3870 else
3871 Set_Ekind (Id, E_Generic_Procedure);
3872 end if;
3873
3874 -- Set SPARK_Mode from context
3875
3876 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3877 Set_SPARK_Pragma_Inherited (Id);
3878
3879 -- Preserve relevant elaboration-related attributes of the context which
3880 -- are no longer available or very expensive to recompute once analysis,
3881 -- resolution, and expansion are over.
3882
3883 Mark_Elaboration_Attributes
3884 (N_Id => Id,
3885 Checks => True,
3886 Warnings => True);
3887
3888 Formals := Parameter_Specifications (Spec);
3889
3890 if Present (Formals) then
3891 Process_Formals (Formals, Spec);
3892 end if;
3893
3894 if Nkind (Spec) = N_Function_Specification then
3895 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3896 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3897 Set_Etype (Id, Result_Type);
3898
3899 -- Check restriction imposed by AI05-073: a generic function
3900 -- cannot return an abstract type or an access to such.
3901
3902 -- This is a binding interpretation should it apply to earlier
3903 -- versions of Ada as well as Ada 2012???
3904
3905 if Is_Abstract_Type (Designated_Type (Result_Type))
3906 and then Ada_Version >= Ada_2012
3907 then
3908 Error_Msg_N
3909 ("generic function cannot have an access result "
3910 & "that designates an abstract type", Spec);
3911 end if;
3912
3913 else
3914 Find_Type (Result_Definition (Spec));
3915 Typ := Entity (Result_Definition (Spec));
3916
3917 if Is_Abstract_Type (Typ)
3918 and then Ada_Version >= Ada_2012
3919 then
3920 Error_Msg_N
3921 ("generic function cannot have abstract result type", Spec);
3922 end if;
3923
3924 -- If a null exclusion is imposed on the result type, then create
3925 -- a null-excluding itype (an access subtype) and use it as the
3926 -- function's Etype.
3927
3928 if Is_Access_Type (Typ)
3929 and then Null_Exclusion_Present (Spec)
3930 then
3931 Set_Etype (Id,
3932 Create_Null_Excluding_Itype
3933 (T => Typ,
3934 Related_Nod => Spec,
3935 Scope_Id => Defining_Unit_Name (Spec)));
3936 else
3937 Set_Etype (Id, Typ);
3938 end if;
3939 end if;
3940
3941 else
3942 Set_Etype (Id, Standard_Void_Type);
3943 end if;
3944
3945 -- Analyze the aspects of the generic copy to ensure that all generated
3946 -- pragmas (if any) perform their semantic effects.
3947
3948 if Has_Aspects (N) then
3949 Analyze_Aspect_Specifications (N, Id);
3950 end if;
3951
3952 -- For a library unit, we have reconstructed the entity for the unit,
3953 -- and must reset it in the library tables. We also make sure that
3954 -- Body_Required is set properly in the original compilation unit node.
3955
3956 if Nkind (Parent (N)) = N_Compilation_Unit then
3957 Set_Cunit_Entity (Current_Sem_Unit, Id);
3958 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3959 end if;
3960
3961 -- If the generic appears within a package unit, the body of that unit
3962 -- has to be present for instantiation and inlining.
3963
3964 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3965 and then Unit_Requires_Body (Id)
3966 then
3967 Set_Body_Needed_For_Inlining
3968 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3969 end if;
3970
3971 Set_Categorization_From_Pragmas (N);
3972 Validate_Categorization_Dependency (N, Id);
3973
3974 -- Capture all global references that occur within the profile of the
3975 -- generic subprogram. Aspects are not part of this processing because
3976 -- they must be delayed. If processed now, Save_Global_References will
3977 -- destroy the Associated_Node links and prevent the capture of global
3978 -- references when the contract of the generic subprogram is analyzed.
3979
3980 Save_Global_References (Original_Node (N));
3981
3982 End_Generic;
3983 End_Scope;
3984 Exit_Generic_Scope (Id);
3985 Generate_Reference_To_Formals (Id);
3986
3987 List_Inherited_Pre_Post_Aspects (Id);
3988 end Analyze_Generic_Subprogram_Declaration;
3989
3990 -----------------------------------
3991 -- Analyze_Package_Instantiation --
3992 -----------------------------------
3993
3994 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3995 -- must be replaced by gotos which jump to the end of the routine in order
3996 -- to restore the Ghost and SPARK modes.
3997
3998 procedure Analyze_Package_Instantiation (N : Node_Id) is
3999 Has_Inline_Always : Boolean := False;
4000 -- Set if the generic unit contains any subprograms with Inline_Always.
4001 -- Only relevant when back-end inlining is not enabled.
4002
4003 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4004 -- Return True if inlining is active and Gen_Unit contains inlined
4005 -- subprograms. In this case, we may either instantiate the body when
4006 -- front-end inlining is enabled, or add a pending instantiation when
4007 -- back-end inlining is enabled. In the former case, this may cause
4008 -- superfluous instantiations, but in either case we need to perform
4009 -- the instantiation of the body in the context of the instance and
4010 -- not in that of the point of inlining.
4011
4012 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4013 -- Return True if Gen_Unit needs to have its body instantiated in the
4014 -- context of N. This in particular excludes generic contexts.
4015
4016 -----------------------
4017 -- Might_Inline_Subp --
4018 -----------------------
4019
4020 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4021 E : Entity_Id;
4022
4023 begin
4024 if Inline_Processing_Required then
4025 -- No need to recompute the answer if we know it is positive
4026 -- and back-end inlining is enabled.
4027
4028 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4029 return True;
4030 end if;
4031
4032 E := First_Entity (Gen_Unit);
4033 while Present (E) loop
4034 if Is_Subprogram (E) and then Is_Inlined (E) then
4035 -- Remember if there are any subprograms with Inline_Always
4036
4037 if Has_Pragma_Inline_Always (E) then
4038 Has_Inline_Always := True;
4039 end if;
4040
4041 Set_Is_Inlined (Gen_Unit);
4042 return True;
4043 end if;
4044
4045 Next_Entity (E);
4046 end loop;
4047 end if;
4048
4049 return False;
4050 end Might_Inline_Subp;
4051
4052 -------------------------------
4053 -- Needs_Body_Instantiated --
4054 -------------------------------
4055
4056 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4057 begin
4058 -- No need to instantiate bodies in generic units
4059
4060 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4061 return False;
4062 end if;
4063
4064 -- If the instantiation is in the main unit, then the body is needed
4065
4066 if Is_In_Main_Unit (N) then
4067 return True;
4068 end if;
4069
4070 -- In GNATprove mode, never instantiate bodies outside of the main
4071 -- unit, as it does not use frontend/backend inlining in the way that
4072 -- GNAT does, so does not benefit from such instantiations. On the
4073 -- contrary, such instantiations may bring artificial constraints,
4074 -- as for example such bodies may require preprocessing.
4075
4076 if GNATprove_Mode then
4077 return False;
4078 end if;
4079
4080 -- If not, then again no need to instantiate bodies in generic units
4081
4082 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4083 return False;
4084 end if;
4085
4086 -- Here we have a special handling for back-end inlining: if inline
4087 -- processing is required, then we unconditionally want to have the
4088 -- body instantiated. The reason is that Might_Inline_Subp does not
4089 -- catch all the cases (as it does not recurse into nested packages)
4090 -- so this avoids the need to patch things up afterwards. Moreover,
4091 -- these instantiations are only performed on demand when back-end
4092 -- inlining is enabled, so this causes very little extra work.
4093
4094 if Inline_Processing_Required and then Back_End_Inlining then
4095 return True;
4096 end if;
4097
4098 -- We want to have the bodies instantiated in non-main units if
4099 -- they might contribute inlined subprograms.
4100
4101 return Might_Inline_Subp (Gen_Unit);
4102 end Needs_Body_Instantiated;
4103
4104 -- Local declarations
4105
4106 Gen_Id : constant Node_Id := Name (N);
4107 Inst_Id : constant Entity_Id := Defining_Entity (N);
4108 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4109 Loc : constant Source_Ptr := Sloc (N);
4110
4111 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4112 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4113 Saved_ISMP : constant Boolean :=
4114 Ignore_SPARK_Mode_Pragmas_In_Instance;
4115 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4116 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4117 -- Save the Ghost and SPARK mode-related data to restore on exit
4118
4119 Saved_Style_Check : constant Boolean := Style_Check;
4120 -- Save style check mode for restore on exit
4121
4122 Act_Decl : Node_Id;
4123 Act_Decl_Name : Node_Id;
4124 Act_Decl_Id : Entity_Id;
4125 Act_Spec : Node_Id;
4126 Act_Tree : Node_Id;
4127 Env_Installed : Boolean := False;
4128 Gen_Decl : Node_Id;
4129 Gen_Spec : Node_Id;
4130 Gen_Unit : Entity_Id;
4131 Inline_Now : Boolean := False;
4132 Needs_Body : Boolean;
4133 Parent_Installed : Boolean := False;
4134 Renaming_List : List_Id;
4135 Unit_Renaming : Node_Id;
4136
4137 Vis_Prims_List : Elist_Id := No_Elist;
4138 -- List of primitives made temporarily visible in the instantiation
4139 -- to match the visibility of the formal type
4140
4141 -- Start of processing for Analyze_Package_Instantiation
4142
4143 begin
4144 -- Preserve relevant elaboration-related attributes of the context which
4145 -- are no longer available or very expensive to recompute once analysis,
4146 -- resolution, and expansion are over.
4147
4148 Mark_Elaboration_Attributes
4149 (N_Id => N,
4150 Checks => True,
4151 Level => True,
4152 Modes => True,
4153 Warnings => True);
4154
4155 -- Very first thing: check for Text_IO special unit in case we are
4156 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4157
4158 Check_Text_IO_Special_Unit (Name (N));
4159
4160 -- Make node global for error reporting
4161
4162 Instantiation_Node := N;
4163
4164 -- Case of instantiation of a generic package
4165
4166 if Nkind (N) = N_Package_Instantiation then
4167 Act_Decl_Id := New_Copy (Defining_Entity (N));
4168 Set_Comes_From_Source (Act_Decl_Id, True);
4169
4170 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4171 Act_Decl_Name :=
4172 Make_Defining_Program_Unit_Name (Loc,
4173 Name =>
4174 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4175 Defining_Identifier => Act_Decl_Id);
4176 else
4177 Act_Decl_Name := Act_Decl_Id;
4178 end if;
4179
4180 -- Case of instantiation of a formal package
4181
4182 else
4183 Act_Decl_Id := Defining_Identifier (N);
4184 Act_Decl_Name := Act_Decl_Id;
4185 end if;
4186
4187 Generate_Definition (Act_Decl_Id);
4188 Set_Ekind (Act_Decl_Id, E_Package);
4189
4190 -- Initialize list of incomplete actuals before analysis
4191
4192 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4193
4194 Preanalyze_Actuals (N, Act_Decl_Id);
4195
4196 -- Turn off style checking in instances. If the check is enabled on the
4197 -- generic unit, a warning in an instance would just be noise. If not
4198 -- enabled on the generic, then a warning in an instance is just wrong.
4199 -- This must be done after analyzing the actuals, which do come from
4200 -- source and are subject to style checking.
4201
4202 Style_Check := False;
4203
4204 Init_Env;
4205 Env_Installed := True;
4206
4207 -- Reset renaming map for formal types. The mapping is established
4208 -- when analyzing the generic associations, but some mappings are
4209 -- inherited from formal packages of parent units, and these are
4210 -- constructed when the parents are installed.
4211
4212 Generic_Renamings.Set_Last (0);
4213 Generic_Renamings_HTable.Reset;
4214
4215 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4216 Gen_Unit := Entity (Gen_Id);
4217
4218 -- A package instantiation is Ghost when it is subject to pragma Ghost
4219 -- or the generic template is Ghost. Set the mode now to ensure that
4220 -- any nodes generated during analysis and expansion are marked as
4221 -- Ghost.
4222
4223 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4224
4225 -- Verify that it is the name of a generic package
4226
4227 -- A visibility glitch: if the instance is a child unit and the generic
4228 -- is the generic unit of a parent instance (i.e. both the parent and
4229 -- the child units are instances of the same package) the name now
4230 -- denotes the renaming within the parent, not the intended generic
4231 -- unit. See if there is a homonym that is the desired generic. The
4232 -- renaming declaration must be visible inside the instance of the
4233 -- child, but not when analyzing the name in the instantiation itself.
4234
4235 if Ekind (Gen_Unit) = E_Package
4236 and then Present (Renamed_Entity (Gen_Unit))
4237 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4238 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4239 and then Present (Homonym (Gen_Unit))
4240 then
4241 Gen_Unit := Homonym (Gen_Unit);
4242 end if;
4243
4244 if Etype (Gen_Unit) = Any_Type then
4245 Restore_Env;
4246 goto Leave;
4247
4248 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4249
4250 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4251
4252 if From_Limited_With (Gen_Unit) then
4253 Error_Msg_N
4254 ("cannot instantiate a limited withed package", Gen_Id);
4255 else
4256 Error_Msg_NE
4257 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4258 end if;
4259
4260 Restore_Env;
4261 goto Leave;
4262 end if;
4263
4264 if In_Extended_Main_Source_Unit (N) then
4265 Set_Is_Instantiated (Gen_Unit);
4266 Generate_Reference (Gen_Unit, N);
4267
4268 if Present (Renamed_Object (Gen_Unit)) then
4269 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4270 Generate_Reference (Renamed_Object (Gen_Unit), N);
4271 end if;
4272 end if;
4273
4274 if Nkind (Gen_Id) = N_Identifier
4275 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4276 then
4277 Error_Msg_NE
4278 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4279
4280 elsif Nkind (Gen_Id) = N_Expanded_Name
4281 and then Is_Child_Unit (Gen_Unit)
4282 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4283 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4284 then
4285 Error_Msg_N
4286 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4287 end if;
4288
4289 Set_Entity (Gen_Id, Gen_Unit);
4290
4291 -- If generic is a renaming, get original generic unit
4292
4293 if Present (Renamed_Object (Gen_Unit))
4294 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4295 then
4296 Gen_Unit := Renamed_Object (Gen_Unit);
4297 end if;
4298
4299 -- Verify that there are no circular instantiations
4300
4301 if In_Open_Scopes (Gen_Unit) then
4302 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4303 Restore_Env;
4304 goto Leave;
4305
4306 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4307 Error_Msg_Node_2 := Current_Scope;
4308 Error_Msg_NE
4309 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4310 Circularity_Detected := True;
4311 Restore_Env;
4312 goto Leave;
4313
4314 else
4315 Set_Ekind (Inst_Id, E_Package);
4316 Set_Scope (Inst_Id, Current_Scope);
4317
4318 -- If the context of the instance is subject to SPARK_Mode "off" or
4319 -- the annotation is altogether missing, set the global flag which
4320 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4321 -- the instance.
4322
4323 if SPARK_Mode /= On then
4324 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4325
4326 -- Mark the instance spec in case the body is instantiated at a
4327 -- later pass. This preserves the original context in effect for
4328 -- the body.
4329
4330 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4331 end if;
4332
4333 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4334 Gen_Spec := Specification (Gen_Decl);
4335
4336 -- Initialize renamings map, for error checking, and the list that
4337 -- holds private entities whose views have changed between generic
4338 -- definition and instantiation. If this is the instance created to
4339 -- validate an actual package, the instantiation environment is that
4340 -- of the enclosing instance.
4341
4342 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4343
4344 -- Copy original generic tree, to produce text for instantiation
4345
4346 Act_Tree :=
4347 Copy_Generic_Node
4348 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4349
4350 Act_Spec := Specification (Act_Tree);
4351
4352 -- If this is the instance created to validate an actual package,
4353 -- only the formals matter, do not examine the package spec itself.
4354
4355 if Is_Actual_Pack then
4356 Set_Visible_Declarations (Act_Spec, New_List);
4357 Set_Private_Declarations (Act_Spec, New_List);
4358 end if;
4359
4360 Renaming_List :=
4361 Analyze_Associations
4362 (I_Node => N,
4363 Formals => Generic_Formal_Declarations (Act_Tree),
4364 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4365
4366 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4367
4368 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4369 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4370 Set_Is_Generic_Instance (Act_Decl_Id);
4371 Set_Generic_Parent (Act_Spec, Gen_Unit);
4372
4373 -- References to the generic in its own declaration or its body are
4374 -- references to the instance. Add a renaming declaration for the
4375 -- generic unit itself. This declaration, as well as the renaming
4376 -- declarations for the generic formals, must remain private to the
4377 -- unit: the formals, because this is the language semantics, and
4378 -- the unit because its use is an artifact of the implementation.
4379
4380 Unit_Renaming :=
4381 Make_Package_Renaming_Declaration (Loc,
4382 Defining_Unit_Name =>
4383 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4384 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4385
4386 Append (Unit_Renaming, Renaming_List);
4387
4388 -- The renaming declarations are the first local declarations of the
4389 -- new unit.
4390
4391 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4392 Insert_List_Before
4393 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4394 else
4395 Set_Visible_Declarations (Act_Spec, Renaming_List);
4396 end if;
4397
4398 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4399
4400 -- Propagate the aspect specifications from the package declaration
4401 -- template to the instantiated version of the package declaration.
4402
4403 if Has_Aspects (Act_Tree) then
4404 Set_Aspect_Specifications (Act_Decl,
4405 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4406 end if;
4407
4408 -- The generic may have a generated Default_Storage_Pool aspect,
4409 -- set at the point of generic declaration. If the instance has
4410 -- that aspect, it overrides the one inherited from the generic.
4411
4412 if Has_Aspects (Gen_Spec) then
4413 if No (Aspect_Specifications (N)) then
4414 Set_Aspect_Specifications (N,
4415 (New_Copy_List_Tree
4416 (Aspect_Specifications (Gen_Spec))));
4417
4418 else
4419 declare
4420 Inherited_Aspects : constant List_Id :=
4421 New_Copy_List_Tree
4422 (Aspect_Specifications (Gen_Spec));
4423
4424 ASN1 : Node_Id;
4425 ASN2 : Node_Id;
4426 Pool_Present : Boolean := False;
4427
4428 begin
4429 ASN1 := First (Aspect_Specifications (N));
4430 while Present (ASN1) loop
4431 if Chars (Identifier (ASN1)) =
4432 Name_Default_Storage_Pool
4433 then
4434 Pool_Present := True;
4435 exit;
4436 end if;
4437
4438 Next (ASN1);
4439 end loop;
4440
4441 if Pool_Present then
4442
4443 -- If generic carries a default storage pool, remove it
4444 -- in favor of the instance one.
4445
4446 ASN2 := First (Inherited_Aspects);
4447 while Present (ASN2) loop
4448 if Chars (Identifier (ASN2)) =
4449 Name_Default_Storage_Pool
4450 then
4451 Remove (ASN2);
4452 exit;
4453 end if;
4454
4455 Next (ASN2);
4456 end loop;
4457 end if;
4458
4459 Prepend_List_To
4460 (Aspect_Specifications (N), Inherited_Aspects);
4461 end;
4462 end if;
4463 end if;
4464
4465 -- Save the instantiation node for a subsequent instantiation of the
4466 -- body if there is one and it needs to be instantiated here.
4467
4468 -- We instantiate the body only if we are generating code, or if we
4469 -- are generating cross-reference information, or for GNATprove use.
4470
4471 declare
4472 Enclosing_Body_Present : Boolean := False;
4473 -- If the generic unit is not a compilation unit, then a body may
4474 -- be present in its parent even if none is required. We create a
4475 -- tentative pending instantiation for the body, which will be
4476 -- discarded if none is actually present.
4477
4478 Scop : Entity_Id;
4479
4480 begin
4481 if Scope (Gen_Unit) /= Standard_Standard
4482 and then not Is_Child_Unit (Gen_Unit)
4483 then
4484 Scop := Scope (Gen_Unit);
4485 while Present (Scop) and then Scop /= Standard_Standard loop
4486 if Unit_Requires_Body (Scop) then
4487 Enclosing_Body_Present := True;
4488 exit;
4489
4490 elsif In_Open_Scopes (Scop)
4491 and then In_Package_Body (Scop)
4492 then
4493 Enclosing_Body_Present := True;
4494 exit;
4495 end if;
4496
4497 exit when Is_Compilation_Unit (Scop);
4498 Scop := Scope (Scop);
4499 end loop;
4500 end if;
4501
4502 -- If front-end inlining is enabled or there are any subprograms
4503 -- marked with Inline_Always, and this is a unit for which code
4504 -- will be generated, we instantiate the body at once.
4505
4506 -- This is done if the instance is not the main unit, and if the
4507 -- generic is not a child unit of another generic, to avoid scope
4508 -- problems and the reinstallation of parent instances.
4509
4510 if Expander_Active
4511 and then (not Is_Child_Unit (Gen_Unit)
4512 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4513 and then Might_Inline_Subp (Gen_Unit)
4514 and then not Is_Actual_Pack
4515 then
4516 if not Back_End_Inlining
4517 and then (Front_End_Inlining or else Has_Inline_Always)
4518 and then (Is_In_Main_Unit (N)
4519 or else In_Main_Context (Current_Scope))
4520 and then Nkind (Parent (N)) /= N_Compilation_Unit
4521 then
4522 Inline_Now := True;
4523
4524 -- In configurable_run_time mode we force the inlining of
4525 -- predefined subprograms marked Inline_Always, to minimize
4526 -- the use of the run-time library.
4527
4528 elsif In_Predefined_Unit (Gen_Decl)
4529 and then Configurable_Run_Time_Mode
4530 and then Nkind (Parent (N)) /= N_Compilation_Unit
4531 then
4532 Inline_Now := True;
4533 end if;
4534
4535 -- If the current scope is itself an instance within a child
4536 -- unit, there will be duplications in the scope stack, and the
4537 -- unstacking mechanism in Inline_Instance_Body will fail.
4538 -- This loses some rare cases of optimization, and might be
4539 -- improved some day, if we can find a proper abstraction for
4540 -- "the complete compilation context" that can be saved and
4541 -- restored. ???
4542
4543 if Is_Generic_Instance (Current_Scope) then
4544 declare
4545 Curr_Unit : constant Entity_Id :=
4546 Cunit_Entity (Current_Sem_Unit);
4547 begin
4548 if Curr_Unit /= Current_Scope
4549 and then Is_Child_Unit (Curr_Unit)
4550 then
4551 Inline_Now := False;
4552 end if;
4553 end;
4554 end if;
4555 end if;
4556
4557 Needs_Body :=
4558 (Unit_Requires_Body (Gen_Unit)
4559 or else Enclosing_Body_Present
4560 or else Present (Corresponding_Body (Gen_Decl)))
4561 and then Needs_Body_Instantiated (Gen_Unit)
4562 and then not Is_Actual_Pack
4563 and then not Inline_Now
4564 and then (Operating_Mode = Generate_Code
4565 or else (Operating_Mode = Check_Semantics
4566 and then GNATprove_Mode));
4567
4568 -- If front-end inlining is enabled or there are any subprograms
4569 -- marked with Inline_Always, do not instantiate body when within
4570 -- a generic context.
4571
4572 if not Back_End_Inlining
4573 and then (Front_End_Inlining or else Has_Inline_Always)
4574 and then not Expander_Active
4575 then
4576 Needs_Body := False;
4577 end if;
4578
4579 -- If the current context is generic, and the package being
4580 -- instantiated is declared within a formal package, there is no
4581 -- body to instantiate until the enclosing generic is instantiated
4582 -- and there is an actual for the formal package. If the formal
4583 -- package has parameters, we build a regular package instance for
4584 -- it, that precedes the original formal package declaration.
4585
4586 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4587 declare
4588 Decl : constant Node_Id :=
4589 Original_Node
4590 (Unit_Declaration_Node (Scope (Gen_Unit)));
4591 begin
4592 if Nkind (Decl) = N_Formal_Package_Declaration
4593 or else (Nkind (Decl) = N_Package_Declaration
4594 and then Is_List_Member (Decl)
4595 and then Present (Next (Decl))
4596 and then
4597 Nkind (Next (Decl)) =
4598 N_Formal_Package_Declaration)
4599 then
4600 Needs_Body := False;
4601 end if;
4602 end;
4603 end if;
4604 end;
4605
4606 -- For RCI unit calling stubs, we omit the instance body if the
4607 -- instance is the RCI library unit itself.
4608
4609 -- However there is a special case for nested instances: in this case
4610 -- we do generate the instance body, as it might be required, e.g.
4611 -- because it provides stream attributes for some type used in the
4612 -- profile of a remote subprogram. This is consistent with 12.3(12),
4613 -- which indicates that the instance body occurs at the place of the
4614 -- instantiation, and thus is part of the RCI declaration, which is
4615 -- present on all client partitions (this is E.2.3(18)).
4616
4617 -- Note that AI12-0002 may make it illegal at some point to have
4618 -- stream attributes defined in an RCI unit, in which case this
4619 -- special case will become unnecessary. In the meantime, there
4620 -- is known application code in production that depends on this
4621 -- being possible, so we definitely cannot eliminate the body in
4622 -- the case of nested instances for the time being.
4623
4624 -- When we generate a nested instance body, calling stubs for any
4625 -- relevant subprogram will be inserted immediately after the
4626 -- subprogram declarations, and will take precedence over the
4627 -- subsequent (original) body. (The stub and original body will be
4628 -- complete homographs, but this is permitted in an instance).
4629 -- (Could we do better and remove the original body???)
4630
4631 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4632 and then Comes_From_Source (N)
4633 and then Nkind (Parent (N)) = N_Compilation_Unit
4634 then
4635 Needs_Body := False;
4636 end if;
4637
4638 if Needs_Body then
4639 -- Indicate that the enclosing scopes contain an instantiation,
4640 -- and that cleanup actions should be delayed until after the
4641 -- instance body is expanded.
4642
4643 Check_Forward_Instantiation (Gen_Decl);
4644 if Nkind (N) = N_Package_Instantiation then
4645 declare
4646 Enclosing_Master : Entity_Id;
4647
4648 begin
4649 -- Loop to search enclosing masters
4650
4651 Enclosing_Master := Current_Scope;
4652 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4653 if Ekind (Enclosing_Master) = E_Package then
4654 if Is_Compilation_Unit (Enclosing_Master) then
4655 if In_Package_Body (Enclosing_Master) then
4656 Set_Delay_Subprogram_Descriptors
4657 (Body_Entity (Enclosing_Master));
4658 else
4659 Set_Delay_Subprogram_Descriptors
4660 (Enclosing_Master);
4661 end if;
4662
4663 exit Scope_Loop;
4664
4665 else
4666 Enclosing_Master := Scope (Enclosing_Master);
4667 end if;
4668
4669 elsif Is_Generic_Unit (Enclosing_Master)
4670 or else Ekind (Enclosing_Master) = E_Void
4671 then
4672 -- Cleanup actions will eventually be performed on the
4673 -- enclosing subprogram or package instance, if any.
4674 -- Enclosing scope is void in the formal part of a
4675 -- generic subprogram.
4676
4677 exit Scope_Loop;
4678
4679 else
4680 if Ekind (Enclosing_Master) = E_Entry
4681 and then
4682 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4683 then
4684 if not Expander_Active then
4685 exit Scope_Loop;
4686 else
4687 Enclosing_Master :=
4688 Protected_Body_Subprogram (Enclosing_Master);
4689 end if;
4690 end if;
4691
4692 Set_Delay_Cleanups (Enclosing_Master);
4693
4694 while Ekind (Enclosing_Master) = E_Block loop
4695 Enclosing_Master := Scope (Enclosing_Master);
4696 end loop;
4697
4698 if Is_Subprogram (Enclosing_Master) then
4699 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4700
4701 elsif Is_Task_Type (Enclosing_Master) then
4702 declare
4703 TBP : constant Node_Id :=
4704 Get_Task_Body_Procedure
4705 (Enclosing_Master);
4706 begin
4707 if Present (TBP) then
4708 Set_Delay_Subprogram_Descriptors (TBP);
4709 Set_Delay_Cleanups (TBP);
4710 end if;
4711 end;
4712 end if;
4713
4714 exit Scope_Loop;
4715 end if;
4716 end loop Scope_Loop;
4717 end;
4718
4719 -- Make entry in table
4720
4721 Add_Pending_Instantiation (N, Act_Decl);
4722 end if;
4723 end if;
4724
4725 Set_Categorization_From_Pragmas (Act_Decl);
4726
4727 if Parent_Installed then
4728 Hide_Current_Scope;
4729 end if;
4730
4731 Set_Instance_Spec (N, Act_Decl);
4732
4733 -- If not a compilation unit, insert the package declaration before
4734 -- the original instantiation node.
4735
4736 if Nkind (Parent (N)) /= N_Compilation_Unit then
4737 Mark_Rewrite_Insertion (Act_Decl);
4738 Insert_Before (N, Act_Decl);
4739
4740 if Has_Aspects (N) then
4741 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4742
4743 -- The pragma created for a Default_Storage_Pool aspect must
4744 -- appear ahead of the declarations in the instance spec.
4745 -- Analysis has placed it after the instance node, so remove
4746 -- it and reinsert it properly now.
4747
4748 declare
4749 ASN : constant Node_Id := First (Aspect_Specifications (N));
4750 A_Name : constant Name_Id := Chars (Identifier (ASN));
4751 Decl : Node_Id;
4752
4753 begin
4754 if A_Name = Name_Default_Storage_Pool then
4755 if No (Visible_Declarations (Act_Spec)) then
4756 Set_Visible_Declarations (Act_Spec, New_List);
4757 end if;
4758
4759 Decl := Next (N);
4760 while Present (Decl) loop
4761 if Nkind (Decl) = N_Pragma then
4762 Remove (Decl);
4763 Prepend (Decl, Visible_Declarations (Act_Spec));
4764 exit;
4765 end if;
4766
4767 Next (Decl);
4768 end loop;
4769 end if;
4770 end;
4771 end if;
4772
4773 Analyze (Act_Decl);
4774
4775 -- For an instantiation that is a compilation unit, place
4776 -- declaration on current node so context is complete for analysis
4777 -- (including nested instantiations). If this is the main unit,
4778 -- the declaration eventually replaces the instantiation node.
4779 -- If the instance body is created later, it replaces the
4780 -- instance node, and the declaration is attached to it
4781 -- (see Build_Instance_Compilation_Unit_Nodes).
4782
4783 else
4784 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4785
4786 -- The entity for the current unit is the newly created one,
4787 -- and all semantic information is attached to it.
4788
4789 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4790
4791 -- If this is the main unit, replace the main entity as well
4792
4793 if Current_Sem_Unit = Main_Unit then
4794 Main_Unit_Entity := Act_Decl_Id;
4795 end if;
4796 end if;
4797
4798 Set_Unit (Parent (N), Act_Decl);
4799 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4800 Set_Package_Instantiation (Act_Decl_Id, N);
4801
4802 -- Process aspect specifications of the instance node, if any, to
4803 -- take into account categorization pragmas before analyzing the
4804 -- instance.
4805
4806 if Has_Aspects (N) then
4807 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4808 end if;
4809
4810 Analyze (Act_Decl);
4811 Set_Unit (Parent (N), N);
4812 Set_Body_Required (Parent (N), False);
4813
4814 -- We never need elaboration checks on instantiations, since by
4815 -- definition, the body instantiation is elaborated at the same
4816 -- time as the spec instantiation.
4817
4818 if Legacy_Elaboration_Checks then
4819 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4820 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4821 end if;
4822 end if;
4823
4824 if Legacy_Elaboration_Checks then
4825 Check_Elab_Instantiation (N);
4826 end if;
4827
4828 -- Save the scenario for later examination by the ABE Processing
4829 -- phase.
4830
4831 Record_Elaboration_Scenario (N);
4832
4833 -- The instantiation results in a guaranteed ABE
4834
4835 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4836 -- Do not instantiate the corresponding body because gigi cannot
4837 -- handle certain types of premature instantiations.
4838
4839 Remove_Dead_Instance (N);
4840
4841 -- Create completing bodies for all subprogram declarations since
4842 -- their real bodies will not be instantiated.
4843
4844 Provide_Completing_Bodies (Instance_Spec (N));
4845 end if;
4846
4847 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4848
4849 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4850 First_Private_Entity (Act_Decl_Id));
4851
4852 -- If the instantiation will receive a body, the unit will be
4853 -- transformed into a package body, and receive its own elaboration
4854 -- entity. Otherwise, the nature of the unit is now a package
4855 -- declaration.
4856
4857 if Nkind (Parent (N)) = N_Compilation_Unit
4858 and then not Needs_Body
4859 then
4860 Rewrite (N, Act_Decl);
4861 end if;
4862
4863 if Present (Corresponding_Body (Gen_Decl))
4864 or else Unit_Requires_Body (Gen_Unit)
4865 then
4866 Set_Has_Completion (Act_Decl_Id);
4867 end if;
4868
4869 Check_Formal_Packages (Act_Decl_Id);
4870
4871 Restore_Hidden_Primitives (Vis_Prims_List);
4872 Restore_Private_Views (Act_Decl_Id);
4873
4874 Inherit_Context (Gen_Decl, N);
4875
4876 if Parent_Installed then
4877 Remove_Parent;
4878 end if;
4879
4880 Restore_Env;
4881 Env_Installed := False;
4882 end if;
4883
4884 Validate_Categorization_Dependency (N, Act_Decl_Id);
4885
4886 -- There used to be a check here to prevent instantiations in local
4887 -- contexts if the No_Local_Allocators restriction was active. This
4888 -- check was removed by a binding interpretation in AI-95-00130/07,
4889 -- but we retain the code for documentation purposes.
4890
4891 -- if Ekind (Act_Decl_Id) /= E_Void
4892 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4893 -- then
4894 -- Check_Restriction (No_Local_Allocators, N);
4895 -- end if;
4896
4897 if Inline_Now then
4898 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4899 end if;
4900
4901 -- Check that if N is an instantiation of System.Dim_Float_IO or
4902 -- System.Dim_Integer_IO, the formal type has a dimension system.
4903
4904 if Nkind (N) = N_Package_Instantiation
4905 and then Is_Dim_IO_Package_Instantiation (N)
4906 then
4907 declare
4908 Assoc : constant Node_Id := First (Generic_Associations (N));
4909 begin
4910 if not Has_Dimension_System
4911 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4912 then
4913 Error_Msg_N ("type with a dimension system expected", Assoc);
4914 end if;
4915 end;
4916 end if;
4917
4918 <<Leave>>
4919 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4920 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4921 end if;
4922
4923 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4924 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4925 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4926 Style_Check := Saved_Style_Check;
4927
4928 exception
4929 when Instantiation_Error =>
4930 if Parent_Installed then
4931 Remove_Parent;
4932 end if;
4933
4934 if Env_Installed then
4935 Restore_Env;
4936 end if;
4937
4938 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4939 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4940 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4941 Style_Check := Saved_Style_Check;
4942 end Analyze_Package_Instantiation;
4943
4944 --------------------------
4945 -- Inline_Instance_Body --
4946 --------------------------
4947
4948 -- WARNING: This routine manages SPARK regions. Return statements must be
4949 -- replaced by gotos which jump to the end of the routine and restore the
4950 -- SPARK mode.
4951
4952 procedure Inline_Instance_Body
4953 (N : Node_Id;
4954 Gen_Unit : Entity_Id;
4955 Act_Decl : Node_Id)
4956 is
4957 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4958
4959 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4960 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4961 Gen_Comp : constant Entity_Id :=
4962 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4963
4964 Scope_Stack_Depth : constant Pos :=
4965 Scope_Stack.Last - Scope_Stack.First + 1;
4966
4967 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4968 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4969 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4970
4971 Curr_Scope : Entity_Id := Empty;
4972 List : Elist_Id := No_Elist; -- init to avoid warning
4973 N_Instances : Nat := 0;
4974 Num_Inner : Nat := 0;
4975 Num_Scopes : Nat := 0;
4976 Removed : Boolean := False;
4977 S : Entity_Id;
4978 Vis : Boolean;
4979
4980 begin
4981 -- Case of generic unit defined in another unit. We must remove the
4982 -- complete context of the current unit to install that of the generic.
4983
4984 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4985
4986 -- Add some comments for the following two loops ???
4987
4988 S := Current_Scope;
4989 while Present (S) and then S /= Standard_Standard loop
4990 loop
4991 Num_Scopes := Num_Scopes + 1;
4992
4993 Use_Clauses (Num_Scopes) :=
4994 (Scope_Stack.Table
4995 (Scope_Stack.Last - Num_Scopes + 1).
4996 First_Use_Clause);
4997 End_Use_Clauses (Use_Clauses (Num_Scopes));
4998
4999 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5000 or else Scope_Stack.Table
5001 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5002 end loop;
5003
5004 exit when Is_Generic_Instance (S)
5005 and then (In_Package_Body (S)
5006 or else Ekind (S) = E_Procedure
5007 or else Ekind (S) = E_Function);
5008 S := Scope (S);
5009 end loop;
5010
5011 Vis := Is_Immediately_Visible (Gen_Comp);
5012
5013 -- Find and save all enclosing instances
5014
5015 S := Current_Scope;
5016
5017 while Present (S)
5018 and then S /= Standard_Standard
5019 loop
5020 if Is_Generic_Instance (S) then
5021 N_Instances := N_Instances + 1;
5022 Instances (N_Instances) := S;
5023
5024 exit when In_Package_Body (S);
5025 end if;
5026
5027 S := Scope (S);
5028 end loop;
5029
5030 -- Remove context of current compilation unit, unless we are within a
5031 -- nested package instantiation, in which case the context has been
5032 -- removed previously.
5033
5034 -- If current scope is the body of a child unit, remove context of
5035 -- spec as well. If an enclosing scope is an instance body, the
5036 -- context has already been removed, but the entities in the body
5037 -- must be made invisible as well.
5038
5039 S := Current_Scope;
5040 while Present (S) and then S /= Standard_Standard loop
5041 if Is_Generic_Instance (S)
5042 and then (In_Package_Body (S)
5043 or else Ekind (S) in E_Procedure | E_Function)
5044 then
5045 -- We still have to remove the entities of the enclosing
5046 -- instance from direct visibility.
5047
5048 declare
5049 E : Entity_Id;
5050 begin
5051 E := First_Entity (S);
5052 while Present (E) loop
5053 Set_Is_Immediately_Visible (E, False);
5054 Next_Entity (E);
5055 end loop;
5056 end;
5057
5058 exit;
5059 end if;
5060
5061 if S = Curr_Unit
5062 or else (Ekind (Curr_Unit) = E_Package_Body
5063 and then S = Spec_Entity (Curr_Unit))
5064 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5065 and then S = Corresponding_Spec
5066 (Unit_Declaration_Node (Curr_Unit)))
5067 then
5068 Removed := True;
5069
5070 -- Remove entities in current scopes from visibility, so that
5071 -- instance body is compiled in a clean environment.
5072
5073 List := Save_Scope_Stack (Handle_Use => False);
5074
5075 if Is_Child_Unit (S) then
5076
5077 -- Remove child unit from stack, as well as inner scopes.
5078 -- Removing the context of a child unit removes parent units
5079 -- as well.
5080
5081 while Current_Scope /= S loop
5082 Num_Inner := Num_Inner + 1;
5083 Inner_Scopes (Num_Inner) := Current_Scope;
5084 Pop_Scope;
5085 end loop;
5086
5087 Pop_Scope;
5088 Remove_Context (Curr_Comp);
5089 Curr_Scope := S;
5090
5091 else
5092 Remove_Context (Curr_Comp);
5093 end if;
5094
5095 if Ekind (Curr_Unit) = E_Package_Body then
5096 Remove_Context (Library_Unit (Curr_Comp));
5097 end if;
5098 end if;
5099
5100 S := Scope (S);
5101 end loop;
5102
5103 pragma Assert (Num_Inner < Num_Scopes);
5104
5105 Push_Scope (Standard_Standard);
5106 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5107
5108 -- The inlined package body is analyzed with the configuration state
5109 -- of the context prior to the scope manipulations performed above.
5110
5111 -- ??? shouldn't this also use the warning state of the context prior
5112 -- to the scope manipulations?
5113
5114 Instantiate_Package_Body
5115 (Body_Info =>
5116 ((Act_Decl => Act_Decl,
5117 Config_Switches => Config_Attrs,
5118 Current_Sem_Unit => Current_Sem_Unit,
5119 Expander_Status => Expander_Active,
5120 Inst_Node => N,
5121 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5122 Scope_Suppress => Scope_Suppress,
5123 Warnings => Save_Warnings)),
5124 Inlined_Body => True);
5125
5126 Pop_Scope;
5127
5128 -- Restore context
5129
5130 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5131
5132 -- Reset Generic_Instance flag so that use clauses can be installed
5133 -- in the proper order. (See Use_One_Package for effect of enclosing
5134 -- instances on processing of use clauses).
5135
5136 for J in 1 .. N_Instances loop
5137 Set_Is_Generic_Instance (Instances (J), False);
5138 end loop;
5139
5140 if Removed then
5141 Install_Context (Curr_Comp, Chain => False);
5142
5143 if Present (Curr_Scope)
5144 and then Is_Child_Unit (Curr_Scope)
5145 then
5146 Push_Scope (Curr_Scope);
5147 Set_Is_Immediately_Visible (Curr_Scope);
5148
5149 -- Finally, restore inner scopes as well
5150
5151 for J in reverse 1 .. Num_Inner loop
5152 Push_Scope (Inner_Scopes (J));
5153 end loop;
5154 end if;
5155
5156 Restore_Scope_Stack (List, Handle_Use => False);
5157
5158 if Present (Curr_Scope)
5159 and then
5160 (In_Private_Part (Curr_Scope)
5161 or else In_Package_Body (Curr_Scope))
5162 then
5163 -- Install private declaration of ancestor units, which are
5164 -- currently available. Restore_Scope_Stack and Install_Context
5165 -- only install the visible part of parents.
5166
5167 declare
5168 Par : Entity_Id;
5169 begin
5170 Par := Scope (Curr_Scope);
5171 while (Present (Par)) and then Par /= Standard_Standard loop
5172 Install_Private_Declarations (Par);
5173 Par := Scope (Par);
5174 end loop;
5175 end;
5176 end if;
5177 end if;
5178
5179 -- Restore use clauses. For a child unit, use clauses in the parents
5180 -- are restored when installing the context, so only those in inner
5181 -- scopes (and those local to the child unit itself) need to be
5182 -- installed explicitly.
5183
5184 if Is_Child_Unit (Curr_Unit) and then Removed then
5185 for J in reverse 1 .. Num_Inner + 1 loop
5186 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5187 Use_Clauses (J);
5188 Install_Use_Clauses (Use_Clauses (J));
5189 end loop;
5190
5191 else
5192 for J in reverse 1 .. Num_Scopes loop
5193 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5194 Use_Clauses (J);
5195 Install_Use_Clauses (Use_Clauses (J));
5196 end loop;
5197 end if;
5198
5199 -- Restore status of instances. If one of them is a body, make its
5200 -- local entities visible again.
5201
5202 declare
5203 E : Entity_Id;
5204 Inst : Entity_Id;
5205
5206 begin
5207 for J in 1 .. N_Instances loop
5208 Inst := Instances (J);
5209 Set_Is_Generic_Instance (Inst, True);
5210
5211 if In_Package_Body (Inst)
5212 or else Ekind (S) in E_Procedure | E_Function
5213 then
5214 E := First_Entity (Instances (J));
5215 while Present (E) loop
5216 Set_Is_Immediately_Visible (E);
5217 Next_Entity (E);
5218 end loop;
5219 end if;
5220 end loop;
5221 end;
5222
5223 -- If generic unit is in current unit, current context is correct. Note
5224 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5225 -- enclosing scopes were removed.
5226
5227 else
5228 Instantiate_Package_Body
5229 (Body_Info =>
5230 ((Act_Decl => Act_Decl,
5231 Config_Switches => Save_Config_Switches,
5232 Current_Sem_Unit => Current_Sem_Unit,
5233 Expander_Status => Expander_Active,
5234 Inst_Node => N,
5235 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5236 Scope_Suppress => Scope_Suppress,
5237 Warnings => Save_Warnings)),
5238 Inlined_Body => True);
5239 end if;
5240 end Inline_Instance_Body;
5241
5242 -------------------------------------
5243 -- Analyze_Procedure_Instantiation --
5244 -------------------------------------
5245
5246 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5247 begin
5248 Analyze_Subprogram_Instantiation (N, E_Procedure);
5249 end Analyze_Procedure_Instantiation;
5250
5251 -----------------------------------
5252 -- Need_Subprogram_Instance_Body --
5253 -----------------------------------
5254
5255 function Need_Subprogram_Instance_Body
5256 (N : Node_Id;
5257 Subp : Entity_Id) return Boolean
5258 is
5259 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5260 -- Return True if E is an inlined subprogram, an inlined renaming or a
5261 -- subprogram nested in an inlined subprogram. The inlining machinery
5262 -- totally disregards nested subprograms since it considers that they
5263 -- will always be compiled if the parent is (see Inline.Is_Nested).
5264
5265 ------------------------------------
5266 -- Is_Inlined_Or_Child_Of_Inlined --
5267 ------------------------------------
5268
5269 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5270 Scop : Entity_Id;
5271
5272 begin
5273 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5274 return True;
5275 end if;
5276
5277 Scop := Scope (E);
5278 while Scop /= Standard_Standard loop
5279 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5280 return True;
5281 end if;
5282
5283 Scop := Scope (Scop);
5284 end loop;
5285
5286 return False;
5287 end Is_Inlined_Or_Child_Of_Inlined;
5288
5289 begin
5290 -- Must be in the main unit or inlined (or child of inlined)
5291
5292 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5293
5294 -- Must be generating code or analyzing code in GNATprove mode
5295
5296 and then (Operating_Mode = Generate_Code
5297 or else (Operating_Mode = Check_Semantics
5298 and then GNATprove_Mode))
5299
5300 -- The body is needed when generating code (full expansion) and in
5301 -- in GNATprove mode (special expansion) for formal verification of
5302 -- the body itself.
5303
5304 and then (Expander_Active or GNATprove_Mode)
5305
5306 -- No point in inlining if ABE is inevitable
5307
5308 and then not Is_Known_Guaranteed_ABE (N)
5309
5310 -- Or if subprogram is eliminated
5311
5312 and then not Is_Eliminated (Subp)
5313 then
5314 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5315 return True;
5316
5317 -- Here if not inlined, or we ignore the inlining
5318
5319 else
5320 return False;
5321 end if;
5322 end Need_Subprogram_Instance_Body;
5323
5324 --------------------------------------
5325 -- Analyze_Subprogram_Instantiation --
5326 --------------------------------------
5327
5328 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5329 -- must be replaced by gotos which jump to the end of the routine in order
5330 -- to restore the Ghost and SPARK modes.
5331
5332 procedure Analyze_Subprogram_Instantiation
5333 (N : Node_Id;
5334 K : Entity_Kind)
5335 is
5336 Errs : constant Nat := Serious_Errors_Detected;
5337 Gen_Id : constant Node_Id := Name (N);
5338 Inst_Id : constant Entity_Id := Defining_Entity (N);
5339 Anon_Id : constant Entity_Id :=
5340 Make_Defining_Identifier (Sloc (Inst_Id),
5341 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5342 Loc : constant Source_Ptr := Sloc (N);
5343
5344 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5345 Act_Decl : Node_Id;
5346 Act_Spec : Node_Id;
5347 Act_Tree : Node_Id;
5348
5349 Env_Installed : Boolean := False;
5350 Gen_Unit : Entity_Id;
5351 Gen_Decl : Node_Id;
5352 Pack_Id : Entity_Id;
5353 Parent_Installed : Boolean := False;
5354
5355 Renaming_List : List_Id;
5356 -- The list of declarations that link formals and actuals of the
5357 -- instance. These are subtype declarations for formal types, and
5358 -- renaming declarations for other formals. The subprogram declaration
5359 -- for the instance is then appended to the list, and the last item on
5360 -- the list is the renaming declaration for the instance.
5361
5362 procedure Analyze_Instance_And_Renamings;
5363 -- The instance must be analyzed in a context that includes the mappings
5364 -- of generic parameters into actuals. We create a package declaration
5365 -- for this purpose, and a subprogram with an internal name within the
5366 -- package. The subprogram instance is simply an alias for the internal
5367 -- subprogram, declared in the current scope.
5368
5369 procedure Build_Subprogram_Renaming;
5370 -- If the subprogram is recursive, there are occurrences of the name of
5371 -- the generic within the body, which must resolve to the current
5372 -- instance. We add a renaming declaration after the declaration, which
5373 -- is available in the instance body, as well as in the analysis of
5374 -- aspects that appear in the generic. This renaming declaration is
5375 -- inserted after the instance declaration which it renames.
5376
5377 ------------------------------------
5378 -- Analyze_Instance_And_Renamings --
5379 ------------------------------------
5380
5381 procedure Analyze_Instance_And_Renamings is
5382 Def_Ent : constant Entity_Id := Defining_Entity (N);
5383 Pack_Decl : Node_Id;
5384
5385 begin
5386 if Nkind (Parent (N)) = N_Compilation_Unit then
5387
5388 -- For the case of a compilation unit, the container package has
5389 -- the same name as the instantiation, to insure that the binder
5390 -- calls the elaboration procedure with the right name. Copy the
5391 -- entity of the instance, which may have compilation level flags
5392 -- (e.g. Is_Child_Unit) set.
5393
5394 Pack_Id := New_Copy (Def_Ent);
5395
5396 else
5397 -- Otherwise we use the name of the instantiation concatenated
5398 -- with its source position to ensure uniqueness if there are
5399 -- several instantiations with the same name.
5400
5401 Pack_Id :=
5402 Make_Defining_Identifier (Loc,
5403 Chars => New_External_Name
5404 (Related_Id => Chars (Def_Ent),
5405 Suffix => "GP",
5406 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5407 end if;
5408
5409 Pack_Decl :=
5410 Make_Package_Declaration (Loc,
5411 Specification => Make_Package_Specification (Loc,
5412 Defining_Unit_Name => Pack_Id,
5413 Visible_Declarations => Renaming_List,
5414 End_Label => Empty));
5415
5416 Set_Instance_Spec (N, Pack_Decl);
5417 Set_Is_Generic_Instance (Pack_Id);
5418 Set_Debug_Info_Needed (Pack_Id);
5419
5420 -- Case of not a compilation unit
5421
5422 if Nkind (Parent (N)) /= N_Compilation_Unit then
5423 Mark_Rewrite_Insertion (Pack_Decl);
5424 Insert_Before (N, Pack_Decl);
5425 Set_Has_Completion (Pack_Id);
5426
5427 -- Case of an instantiation that is a compilation unit
5428
5429 -- Place declaration on current node so context is complete for
5430 -- analysis (including nested instantiations), and for use in a
5431 -- context_clause (see Analyze_With_Clause).
5432
5433 else
5434 Set_Unit (Parent (N), Pack_Decl);
5435 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5436 end if;
5437
5438 Analyze (Pack_Decl);
5439 Check_Formal_Packages (Pack_Id);
5440
5441 -- Body of the enclosing package is supplied when instantiating the
5442 -- subprogram body, after semantic analysis is completed.
5443
5444 if Nkind (Parent (N)) = N_Compilation_Unit then
5445
5446 -- Remove package itself from visibility, so it does not
5447 -- conflict with subprogram.
5448
5449 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5450
5451 -- Set name and scope of internal subprogram so that the proper
5452 -- external name will be generated. The proper scope is the scope
5453 -- of the wrapper package. We need to generate debugging info for
5454 -- the internal subprogram, so set flag accordingly.
5455
5456 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5457 Set_Scope (Anon_Id, Scope (Pack_Id));
5458
5459 -- Mark wrapper package as referenced, to avoid spurious warnings
5460 -- if the instantiation appears in various with_ clauses of
5461 -- subunits of the main unit.
5462
5463 Set_Referenced (Pack_Id);
5464 end if;
5465
5466 Set_Is_Generic_Instance (Anon_Id);
5467 Set_Debug_Info_Needed (Anon_Id);
5468 Act_Decl_Id := New_Copy (Anon_Id);
5469
5470 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5471 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5472 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5473
5474 -- Subprogram instance comes from source only if generic does
5475
5476 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5477
5478 -- If the instance is a child unit, mark the Id accordingly. Mark
5479 -- the anonymous entity as well, which is the real subprogram and
5480 -- which is used when the instance appears in a context clause.
5481 -- Similarly, propagate the Is_Eliminated flag to handle properly
5482 -- nested eliminated subprograms.
5483
5484 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5485 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5486 New_Overloaded_Entity (Act_Decl_Id);
5487 Check_Eliminated (Act_Decl_Id);
5488 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5489
5490 if Nkind (Parent (N)) = N_Compilation_Unit then
5491
5492 -- In compilation unit case, kill elaboration checks on the
5493 -- instantiation, since they are never needed - the body is
5494 -- instantiated at the same point as the spec.
5495
5496 if Legacy_Elaboration_Checks then
5497 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5498 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5499 end if;
5500
5501 Set_Is_Compilation_Unit (Anon_Id);
5502 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5503 end if;
5504
5505 -- The instance is not a freezing point for the new subprogram.
5506 -- The anonymous subprogram may have a freeze node, created for
5507 -- some delayed aspects. This freeze node must not be inherited
5508 -- by the visible subprogram entity.
5509
5510 Set_Is_Frozen (Act_Decl_Id, False);
5511 Set_Freeze_Node (Act_Decl_Id, Empty);
5512
5513 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5514 Valid_Operator_Definition (Act_Decl_Id);
5515 end if;
5516
5517 Set_Alias (Act_Decl_Id, Anon_Id);
5518 Set_Has_Completion (Act_Decl_Id);
5519 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5520
5521 if Nkind (Parent (N)) = N_Compilation_Unit then
5522 Set_Body_Required (Parent (N), False);
5523 end if;
5524 end Analyze_Instance_And_Renamings;
5525
5526 -------------------------------
5527 -- Build_Subprogram_Renaming --
5528 -------------------------------
5529
5530 procedure Build_Subprogram_Renaming is
5531 Renaming_Decl : Node_Id;
5532 Unit_Renaming : Node_Id;
5533
5534 begin
5535 Unit_Renaming :=
5536 Make_Subprogram_Renaming_Declaration (Loc,
5537 Specification =>
5538 Copy_Generic_Node
5539 (Specification (Original_Node (Gen_Decl)),
5540 Empty,
5541 Instantiating => True),
5542 Name => New_Occurrence_Of (Anon_Id, Loc));
5543
5544 -- The generic may be a child unit. The renaming needs an identifier
5545 -- with the proper name.
5546
5547 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5548 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5549
5550 -- If there is a formal subprogram with the same name as the unit
5551 -- itself, do not add this renaming declaration, to prevent
5552 -- ambiguities when there is a call with that name in the body.
5553 -- This is a partial and ugly fix for one ACATS test. ???
5554
5555 Renaming_Decl := First (Renaming_List);
5556 while Present (Renaming_Decl) loop
5557 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5558 and then
5559 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5560 then
5561 exit;
5562 end if;
5563
5564 Next (Renaming_Decl);
5565 end loop;
5566
5567 if No (Renaming_Decl) then
5568 Append (Unit_Renaming, Renaming_List);
5569 end if;
5570 end Build_Subprogram_Renaming;
5571
5572 -- Local variables
5573
5574 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5575 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5576 Saved_ISMP : constant Boolean :=
5577 Ignore_SPARK_Mode_Pragmas_In_Instance;
5578 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5579 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5580 -- Save the Ghost and SPARK mode-related data to restore on exit
5581
5582 Vis_Prims_List : Elist_Id := No_Elist;
5583 -- List of primitives made temporarily visible in the instantiation
5584 -- to match the visibility of the formal type
5585
5586 -- Start of processing for Analyze_Subprogram_Instantiation
5587
5588 begin
5589 -- Preserve relevant elaboration-related attributes of the context which
5590 -- are no longer available or very expensive to recompute once analysis,
5591 -- resolution, and expansion are over.
5592
5593 Mark_Elaboration_Attributes
5594 (N_Id => N,
5595 Checks => True,
5596 Level => True,
5597 Modes => True,
5598 Warnings => True);
5599
5600 -- Very first thing: check for special Text_IO unit in case we are
5601 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5602 -- such an instantiation is bogus (these are packages, not subprograms),
5603 -- but we get a better error message if we do this.
5604
5605 Check_Text_IO_Special_Unit (Gen_Id);
5606
5607 -- Make node global for error reporting
5608
5609 Instantiation_Node := N;
5610
5611 -- For package instantiations we turn off style checks, because they
5612 -- will have been emitted in the generic. For subprogram instantiations
5613 -- we want to apply at least the check on overriding indicators so we
5614 -- do not modify the style check status.
5615
5616 -- The renaming declarations for the actuals do not come from source and
5617 -- will not generate spurious warnings.
5618
5619 Preanalyze_Actuals (N);
5620
5621 Init_Env;
5622 Env_Installed := True;
5623 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5624 Gen_Unit := Entity (Gen_Id);
5625
5626 -- A subprogram instantiation is Ghost when it is subject to pragma
5627 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5628 -- that any nodes generated during analysis and expansion are marked as
5629 -- Ghost.
5630
5631 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5632
5633 Generate_Reference (Gen_Unit, Gen_Id);
5634
5635 if Nkind (Gen_Id) = N_Identifier
5636 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5637 then
5638 Error_Msg_NE
5639 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5640 end if;
5641
5642 if Etype (Gen_Unit) = Any_Type then
5643 Restore_Env;
5644 goto Leave;
5645 end if;
5646
5647 -- Verify that it is a generic subprogram of the right kind, and that
5648 -- it does not lead to a circular instantiation.
5649
5650 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5651 Error_Msg_NE
5652 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5653
5654 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5655 Error_Msg_NE
5656 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5657
5658 elsif In_Open_Scopes (Gen_Unit) then
5659 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5660
5661 else
5662 Set_Ekind (Inst_Id, K);
5663 Set_Scope (Inst_Id, Current_Scope);
5664
5665 Set_Entity (Gen_Id, Gen_Unit);
5666 Set_Is_Instantiated (Gen_Unit);
5667
5668 if In_Extended_Main_Source_Unit (N) then
5669 Generate_Reference (Gen_Unit, N);
5670 end if;
5671
5672 -- If renaming, get original unit
5673
5674 if Present (Renamed_Object (Gen_Unit))
5675 and then Is_Generic_Subprogram (Renamed_Object (Gen_Unit))
5676 then
5677 Gen_Unit := Renamed_Object (Gen_Unit);
5678 Set_Is_Instantiated (Gen_Unit);
5679 Generate_Reference (Gen_Unit, N);
5680 end if;
5681
5682 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5683 Error_Msg_Node_2 := Current_Scope;
5684 Error_Msg_NE
5685 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5686 Circularity_Detected := True;
5687 Restore_Hidden_Primitives (Vis_Prims_List);
5688 goto Leave;
5689 end if;
5690
5691 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5692
5693 -- Initialize renamings map, for error checking
5694
5695 Generic_Renamings.Set_Last (0);
5696 Generic_Renamings_HTable.Reset;
5697
5698 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5699
5700 -- Copy original generic tree, to produce text for instantiation
5701
5702 Act_Tree :=
5703 Copy_Generic_Node
5704 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5705
5706 -- Inherit overriding indicator from instance node
5707
5708 Act_Spec := Specification (Act_Tree);
5709 Set_Must_Override (Act_Spec, Must_Override (N));
5710 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5711
5712 Renaming_List :=
5713 Analyze_Associations
5714 (I_Node => N,
5715 Formals => Generic_Formal_Declarations (Act_Tree),
5716 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5717
5718 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5719
5720 -- The subprogram itself cannot contain a nested instance, so the
5721 -- current parent is left empty.
5722
5723 Set_Instance_Env (Gen_Unit, Empty);
5724
5725 -- Build the subprogram declaration, which does not appear in the
5726 -- generic template, and give it a sloc consistent with that of the
5727 -- template.
5728
5729 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5730 Set_Generic_Parent (Act_Spec, Gen_Unit);
5731 Act_Decl :=
5732 Make_Subprogram_Declaration (Sloc (Act_Spec),
5733 Specification => Act_Spec);
5734
5735 -- The aspects have been copied previously, but they have to be
5736 -- linked explicitly to the new subprogram declaration. Explicit
5737 -- pre/postconditions on the instance are analyzed below, in a
5738 -- separate step.
5739
5740 Move_Aspects (Act_Tree, To => Act_Decl);
5741 Set_Categorization_From_Pragmas (Act_Decl);
5742
5743 if Parent_Installed then
5744 Hide_Current_Scope;
5745 end if;
5746
5747 Append (Act_Decl, Renaming_List);
5748
5749 -- Contract-related source pragmas that follow a generic subprogram
5750 -- must be instantiated explicitly because they are not part of the
5751 -- subprogram template.
5752
5753 Instantiate_Subprogram_Contract
5754 (Original_Node (Gen_Decl), Renaming_List);
5755
5756 Build_Subprogram_Renaming;
5757
5758 -- If the context of the instance is subject to SPARK_Mode "off" or
5759 -- the annotation is altogether missing, set the global flag which
5760 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5761 -- the instance. This should be done prior to analyzing the instance.
5762
5763 if SPARK_Mode /= On then
5764 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5765 end if;
5766
5767 -- If the context of an instance is not subject to SPARK_Mode "off",
5768 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5769 -- the latter should be the one applicable to the instance.
5770
5771 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5772 and then Saved_SM /= Off
5773 and then Present (SPARK_Pragma (Gen_Unit))
5774 then
5775 Set_SPARK_Mode (Gen_Unit);
5776 end if;
5777
5778 Analyze_Instance_And_Renamings;
5779
5780 -- Restore SPARK_Mode from the context after analysis of the package
5781 -- declaration, so that the SPARK_Mode on the generic spec does not
5782 -- apply to the pending instance for the instance body.
5783
5784 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5785 and then Saved_SM /= Off
5786 and then Present (SPARK_Pragma (Gen_Unit))
5787 then
5788 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5789 end if;
5790
5791 -- If the generic is marked Import (Intrinsic), then so is the
5792 -- instance. This indicates that there is no body to instantiate. If
5793 -- generic is marked inline, so it the instance, and the anonymous
5794 -- subprogram it renames. If inlined, or else if inlining is enabled
5795 -- for the compilation, we generate the instance body even if it is
5796 -- not within the main unit.
5797
5798 if Is_Intrinsic_Subprogram (Gen_Unit) then
5799 Set_Is_Intrinsic_Subprogram (Anon_Id);
5800 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5801
5802 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5803 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5804 end if;
5805 end if;
5806
5807 -- Inherit convention from generic unit. Intrinsic convention, as for
5808 -- an instance of unchecked conversion, is not inherited because an
5809 -- explicit Ada instance has been created.
5810
5811 if Has_Convention_Pragma (Gen_Unit)
5812 and then Convention (Gen_Unit) /= Convention_Intrinsic
5813 then
5814 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5815 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5816 end if;
5817
5818 Generate_Definition (Act_Decl_Id);
5819
5820 -- Inherit all inlining-related flags which apply to the generic in
5821 -- the subprogram and its declaration.
5822
5823 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5824 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5825
5826 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5827 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5828
5829 Set_Has_Pragma_Inline_Always
5830 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5831 Set_Has_Pragma_Inline_Always
5832 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5833
5834 Set_Has_Pragma_No_Inline
5835 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5836 Set_Has_Pragma_No_Inline
5837 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5838
5839 -- Propagate No_Return if pragma applied to generic unit. This must
5840 -- be done explicitly because pragma does not appear in generic
5841 -- declaration (unlike the aspect case).
5842
5843 if No_Return (Gen_Unit) then
5844 Set_No_Return (Act_Decl_Id);
5845 Set_No_Return (Anon_Id);
5846 end if;
5847
5848 -- Mark both the instance spec and the anonymous package in case the
5849 -- body is instantiated at a later pass. This preserves the original
5850 -- context in effect for the body.
5851
5852 if SPARK_Mode /= On then
5853 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5854 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5855 end if;
5856
5857 if Legacy_Elaboration_Checks
5858 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5859 then
5860 Check_Elab_Instantiation (N);
5861 end if;
5862
5863 -- Save the scenario for later examination by the ABE Processing
5864 -- phase.
5865
5866 Record_Elaboration_Scenario (N);
5867
5868 -- The instantiation results in a guaranteed ABE. Create a completing
5869 -- body for the subprogram declaration because the real body will not
5870 -- be instantiated.
5871
5872 if Is_Known_Guaranteed_ABE (N) then
5873 Provide_Completing_Bodies (Instance_Spec (N));
5874 end if;
5875
5876 if Is_Dispatching_Operation (Act_Decl_Id)
5877 and then Ada_Version >= Ada_2005
5878 then
5879 declare
5880 Formal : Entity_Id;
5881
5882 begin
5883 Formal := First_Formal (Act_Decl_Id);
5884 while Present (Formal) loop
5885 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5886 and then Is_Controlling_Formal (Formal)
5887 and then not Can_Never_Be_Null (Formal)
5888 then
5889 Error_Msg_NE
5890 ("access parameter& is controlling,", N, Formal);
5891 Error_Msg_NE
5892 ("\corresponding parameter of & must be explicitly "
5893 & "null-excluding", N, Gen_Id);
5894 end if;
5895
5896 Next_Formal (Formal);
5897 end loop;
5898 end;
5899 end if;
5900
5901 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5902
5903 Validate_Categorization_Dependency (N, Act_Decl_Id);
5904
5905 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5906 Inherit_Context (Gen_Decl, N);
5907
5908 Restore_Private_Views (Pack_Id, False);
5909
5910 -- If the context requires a full instantiation, mark node for
5911 -- subsequent construction of the body.
5912
5913 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5914 Check_Forward_Instantiation (Gen_Decl);
5915
5916 -- The wrapper package is always delayed, because it does not
5917 -- constitute a freeze point, but to insure that the freeze node
5918 -- is placed properly, it is created directly when instantiating
5919 -- the body (otherwise the freeze node might appear to early for
5920 -- nested instantiations).
5921
5922 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5923 Rewrite (N, Unit (Parent (N)));
5924 Set_Unit (Parent (N), N);
5925 end if;
5926
5927 -- Replace instance node for library-level instantiations of
5928 -- intrinsic subprograms.
5929
5930 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5931 Rewrite (N, Unit (Parent (N)));
5932 Set_Unit (Parent (N), N);
5933 end if;
5934
5935 if Parent_Installed then
5936 Remove_Parent;
5937 end if;
5938
5939 Restore_Hidden_Primitives (Vis_Prims_List);
5940 Restore_Env;
5941 Env_Installed := False;
5942 Generic_Renamings.Set_Last (0);
5943 Generic_Renamings_HTable.Reset;
5944 end if;
5945
5946 <<Leave>>
5947 -- Analyze aspects in declaration if no errors appear in the instance.
5948
5949 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5950 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5951 end if;
5952
5953 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5954 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5955 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5956
5957 exception
5958 when Instantiation_Error =>
5959 if Parent_Installed then
5960 Remove_Parent;
5961 end if;
5962
5963 if Env_Installed then
5964 Restore_Env;
5965 end if;
5966
5967 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5968 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5969 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5970 end Analyze_Subprogram_Instantiation;
5971
5972 -------------------------
5973 -- Get_Associated_Node --
5974 -------------------------
5975
5976 function Get_Associated_Node (N : Node_Id) return Node_Id is
5977 Assoc : Node_Id;
5978
5979 begin
5980 Assoc := Associated_Node (N);
5981
5982 if Nkind (Assoc) /= Nkind (N) then
5983 return Assoc;
5984
5985 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
5986 return Assoc;
5987
5988 else
5989 -- If the node is part of an inner generic, it may itself have been
5990 -- remapped into a further generic copy. Associated_Node is otherwise
5991 -- used for the entity of the node, and will be of a different node
5992 -- kind, or else N has been rewritten as a literal or function call.
5993
5994 while Present (Associated_Node (Assoc))
5995 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5996 loop
5997 Assoc := Associated_Node (Assoc);
5998 end loop;
5999
6000 -- Follow an additional link in case the final node was rewritten.
6001 -- This can only happen with nested generic units.
6002
6003 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6004 and then Present (Associated_Node (Assoc))
6005 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6006 | N_Explicit_Dereference
6007 | N_Integer_Literal
6008 | N_Real_Literal
6009 | N_String_Literal
6010 then
6011 Assoc := Associated_Node (Assoc);
6012 end if;
6013
6014 -- An additional special case: an unconstrained type in an object
6015 -- declaration may have been rewritten as a local subtype constrained
6016 -- by the expression in the declaration. We need to recover the
6017 -- original entity, which may be global.
6018
6019 if Present (Original_Node (Assoc))
6020 and then Nkind (Parent (N)) = N_Object_Declaration
6021 then
6022 Assoc := Original_Node (Assoc);
6023 end if;
6024
6025 return Assoc;
6026 end if;
6027 end Get_Associated_Node;
6028
6029 ----------------------------
6030 -- Build_Function_Wrapper --
6031 ----------------------------
6032
6033 function Build_Function_Wrapper
6034 (Formal_Subp : Entity_Id;
6035 Actual_Subp : Entity_Id) return Node_Id
6036 is
6037 Loc : constant Source_Ptr := Sloc (Current_Scope);
6038 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6039 Actuals : List_Id;
6040 Decl : Node_Id;
6041 Func_Name : Node_Id;
6042 Func : Entity_Id;
6043 Parm_Type : Node_Id;
6044 Profile : List_Id := New_List;
6045 Spec : Node_Id;
6046 Act_F : Entity_Id;
6047 Form_F : Entity_Id;
6048 New_F : Entity_Id;
6049
6050 begin
6051 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
6052
6053 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6054 Set_Ekind (Func, E_Function);
6055 Set_Is_Generic_Actual_Subprogram (Func);
6056
6057 Actuals := New_List;
6058 Profile := New_List;
6059
6060 Act_F := First_Formal (Actual_Subp);
6061 Form_F := First_Formal (Formal_Subp);
6062 while Present (Form_F) loop
6063
6064 -- Create new formal for profile of wrapper, and add a reference
6065 -- to it in the list of actuals for the enclosing call. The name
6066 -- must be that of the formal in the formal subprogram, because
6067 -- calls to it in the generic body may use named associations.
6068
6069 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6070
6071 Parm_Type :=
6072 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
6073
6074 Append_To (Profile,
6075 Make_Parameter_Specification (Loc,
6076 Defining_Identifier => New_F,
6077 Parameter_Type => Parm_Type));
6078
6079 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
6080 Next_Formal (Form_F);
6081
6082 if Present (Act_F) then
6083 Next_Formal (Act_F);
6084 end if;
6085 end loop;
6086
6087 Spec :=
6088 Make_Function_Specification (Loc,
6089 Defining_Unit_Name => Func,
6090 Parameter_Specifications => Profile,
6091 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6092
6093 Decl :=
6094 Make_Expression_Function (Loc,
6095 Specification => Spec,
6096 Expression =>
6097 Make_Function_Call (Loc,
6098 Name => Func_Name,
6099 Parameter_Associations => Actuals));
6100
6101 return Decl;
6102 end Build_Function_Wrapper;
6103
6104 ----------------------------
6105 -- Build_Operator_Wrapper --
6106 ----------------------------
6107
6108 function Build_Operator_Wrapper
6109 (Formal_Subp : Entity_Id;
6110 Actual_Subp : Entity_Id) return Node_Id
6111 is
6112 Loc : constant Source_Ptr := Sloc (Current_Scope);
6113 Ret_Type : constant Entity_Id :=
6114 Get_Instance_Of (Etype (Formal_Subp));
6115 Op_Type : constant Entity_Id :=
6116 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6117 Is_Binary : constant Boolean :=
6118 Present (Next_Formal (First_Formal (Formal_Subp)));
6119
6120 Decl : Node_Id;
6121 Expr : Node_Id := Empty;
6122 F1, F2 : Entity_Id;
6123 Func : Entity_Id;
6124 Op_Name : Name_Id;
6125 Spec : Node_Id;
6126 L, R : Node_Id;
6127
6128 begin
6129 Op_Name := Chars (Actual_Subp);
6130
6131 -- Create entities for wrapper function and its formals
6132
6133 F1 := Make_Temporary (Loc, 'A');
6134 F2 := Make_Temporary (Loc, 'B');
6135 L := New_Occurrence_Of (F1, Loc);
6136 R := New_Occurrence_Of (F2, Loc);
6137
6138 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6139 Set_Ekind (Func, E_Function);
6140 Set_Is_Generic_Actual_Subprogram (Func);
6141
6142 Spec :=
6143 Make_Function_Specification (Loc,
6144 Defining_Unit_Name => Func,
6145 Parameter_Specifications => New_List (
6146 Make_Parameter_Specification (Loc,
6147 Defining_Identifier => F1,
6148 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6149 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6150
6151 if Is_Binary then
6152 Append_To (Parameter_Specifications (Spec),
6153 Make_Parameter_Specification (Loc,
6154 Defining_Identifier => F2,
6155 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6156 end if;
6157
6158 -- Build expression as a function call, or as an operator node
6159 -- that corresponds to the name of the actual, starting with
6160 -- binary operators.
6161
6162 if Op_Name not in Any_Operator_Name then
6163 Expr :=
6164 Make_Function_Call (Loc,
6165 Name =>
6166 New_Occurrence_Of (Actual_Subp, Loc),
6167 Parameter_Associations => New_List (L));
6168
6169 if Is_Binary then
6170 Append_To (Parameter_Associations (Expr), R);
6171 end if;
6172
6173 -- Binary operators
6174
6175 elsif Is_Binary then
6176 if Op_Name = Name_Op_And then
6177 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6178 elsif Op_Name = Name_Op_Or then
6179 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6180 elsif Op_Name = Name_Op_Xor then
6181 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6182 elsif Op_Name = Name_Op_Eq then
6183 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6184 elsif Op_Name = Name_Op_Ne then
6185 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6186 elsif Op_Name = Name_Op_Le then
6187 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6188 elsif Op_Name = Name_Op_Gt then
6189 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6190 elsif Op_Name = Name_Op_Ge then
6191 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6192 elsif Op_Name = Name_Op_Lt then
6193 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6194 elsif Op_Name = Name_Op_Add then
6195 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6196 elsif Op_Name = Name_Op_Subtract then
6197 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6198 elsif Op_Name = Name_Op_Concat then
6199 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6200 elsif Op_Name = Name_Op_Multiply then
6201 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6202 elsif Op_Name = Name_Op_Divide then
6203 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6204 elsif Op_Name = Name_Op_Mod then
6205 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6206 elsif Op_Name = Name_Op_Rem then
6207 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6208 elsif Op_Name = Name_Op_Expon then
6209 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6210 end if;
6211
6212 -- Unary operators
6213
6214 else
6215 if Op_Name = Name_Op_Add then
6216 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6217 elsif Op_Name = Name_Op_Subtract then
6218 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6219 elsif Op_Name = Name_Op_Abs then
6220 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6221 elsif Op_Name = Name_Op_Not then
6222 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6223 end if;
6224 end if;
6225
6226 Decl :=
6227 Make_Expression_Function (Loc,
6228 Specification => Spec,
6229 Expression => Expr);
6230
6231 return Decl;
6232 end Build_Operator_Wrapper;
6233
6234 -----------------------------------
6235 -- Build_Subprogram_Decl_Wrapper --
6236 -----------------------------------
6237
6238 function Build_Subprogram_Decl_Wrapper
6239 (Formal_Subp : Entity_Id) return Node_Id
6240 is
6241 Loc : constant Source_Ptr := Sloc (Current_Scope);
6242 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6243 Decl : Node_Id;
6244 Subp : Entity_Id;
6245 Parm_Spec : Node_Id;
6246 Profile : List_Id := New_List;
6247 Spec : Node_Id;
6248 Form_F : Entity_Id;
6249 New_F : Entity_Id;
6250
6251 begin
6252
6253 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6254 Set_Ekind (Subp, Ekind (Formal_Subp));
6255 Set_Is_Generic_Actual_Subprogram (Subp);
6256
6257 Profile := Parameter_Specifications (
6258 New_Copy_Tree
6259 (Specification (Unit_Declaration_Node (Formal_Subp))));
6260
6261 Form_F := First_Formal (Formal_Subp);
6262 Parm_Spec := First (Profile);
6263
6264 -- Create new entities for the formals. Reset entities so that
6265 -- parameter types are properly resolved when wrapper declaration
6266 -- is analyzed.
6267
6268 while Present (Parm_Spec) loop
6269 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6270 Set_Defining_Identifier (Parm_Spec, New_F);
6271 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6272 Next (Parm_Spec);
6273 Next_Formal (Form_F);
6274 end loop;
6275
6276 if Ret_Type = Standard_Void_Type then
6277 Spec :=
6278 Make_Procedure_Specification (Loc,
6279 Defining_Unit_Name => Subp,
6280 Parameter_Specifications => Profile);
6281 else
6282 Spec :=
6283 Make_Function_Specification (Loc,
6284 Defining_Unit_Name => Subp,
6285 Parameter_Specifications => Profile,
6286 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6287 end if;
6288
6289 Decl :=
6290 Make_Subprogram_Declaration (Loc, Specification => Spec);
6291
6292 return Decl;
6293 end Build_Subprogram_Decl_Wrapper;
6294
6295 -----------------------------------
6296 -- Build_Subprogram_Body_Wrapper --
6297 -----------------------------------
6298
6299 function Build_Subprogram_Body_Wrapper
6300 (Formal_Subp : Entity_Id;
6301 Actual_Name : Node_Id) return Node_Id
6302 is
6303 Loc : constant Source_Ptr := Sloc (Current_Scope);
6304 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6305 Spec_Node : constant Node_Id :=
6306 Specification
6307 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6308 Act : Node_Id;
6309 Actuals : List_Id;
6310 Body_Node : Node_Id;
6311 Stmt : Node_Id;
6312 begin
6313 Actuals := New_List;
6314 Act := First (Parameter_Specifications (Spec_Node));
6315
6316 while Present (Act) loop
6317 Append_To (Actuals,
6318 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6319 Next (Act);
6320 end loop;
6321
6322 if Ret_Type = Standard_Void_Type then
6323 Stmt := Make_Procedure_Call_Statement (Loc,
6324 Name => Actual_Name,
6325 Parameter_Associations => Actuals);
6326
6327 else
6328 Stmt := Make_Simple_Return_Statement (Loc,
6329 Expression =>
6330 Make_Function_Call (Loc,
6331 Name => Actual_Name,
6332 Parameter_Associations => Actuals));
6333 end if;
6334
6335 Body_Node := Make_Subprogram_Body (Loc,
6336 Specification => Spec_Node,
6337 Declarations => New_List,
6338 Handled_Statement_Sequence =>
6339 Make_Handled_Sequence_Of_Statements (Loc,
6340 Statements => New_List (Stmt)));
6341
6342 return Body_Node;
6343 end Build_Subprogram_Body_Wrapper;
6344
6345 -------------------------------------------
6346 -- Build_Instance_Compilation_Unit_Nodes --
6347 -------------------------------------------
6348
6349 procedure Build_Instance_Compilation_Unit_Nodes
6350 (N : Node_Id;
6351 Act_Body : Node_Id;
6352 Act_Decl : Node_Id)
6353 is
6354 Decl_Cunit : Node_Id;
6355 Body_Cunit : Node_Id;
6356 Citem : Node_Id;
6357 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6358 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6359
6360 begin
6361 -- A new compilation unit node is built for the instance declaration
6362
6363 Decl_Cunit :=
6364 Make_Compilation_Unit (Sloc (N),
6365 Context_Items => Empty_List,
6366 Unit => Act_Decl,
6367 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6368
6369 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6370
6371 -- The new compilation unit is linked to its body, but both share the
6372 -- same file, so we do not set Body_Required on the new unit so as not
6373 -- to create a spurious dependency on a non-existent body in the ali.
6374 -- This simplifies CodePeer unit traversal.
6375
6376 -- We use the original instantiation compilation unit as the resulting
6377 -- compilation unit of the instance, since this is the main unit.
6378
6379 Rewrite (N, Act_Body);
6380
6381 -- Propagate the aspect specifications from the package body template to
6382 -- the instantiated version of the package body.
6383
6384 if Has_Aspects (Act_Body) then
6385 Set_Aspect_Specifications
6386 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6387 end if;
6388
6389 Body_Cunit := Parent (N);
6390
6391 -- The two compilation unit nodes are linked by the Library_Unit field
6392
6393 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6394 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6395
6396 -- Preserve the private nature of the package if needed
6397
6398 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6399
6400 -- If the instance is not the main unit, its context, categorization
6401 -- and elaboration entity are not relevant to the compilation.
6402
6403 if Body_Cunit /= Cunit (Main_Unit) then
6404 Make_Instance_Unit (Body_Cunit, In_Main => False);
6405 return;
6406 end if;
6407
6408 -- The context clause items on the instantiation, which are now attached
6409 -- to the body compilation unit (since the body overwrote the original
6410 -- instantiation node), semantically belong on the spec, so copy them
6411 -- there. It's harmless to leave them on the body as well. In fact one
6412 -- could argue that they belong in both places.
6413
6414 Citem := First (Context_Items (Body_Cunit));
6415 while Present (Citem) loop
6416 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6417 Next (Citem);
6418 end loop;
6419
6420 -- Propagate categorization flags on packages, so that they appear in
6421 -- the ali file for the spec of the unit.
6422
6423 if Ekind (New_Main) = E_Package then
6424 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6425 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6426 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6427 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6428 Set_Is_Remote_Call_Interface
6429 (Old_Main, Is_Remote_Call_Interface (New_Main));
6430 end if;
6431
6432 -- Make entry in Units table, so that binder can generate call to
6433 -- elaboration procedure for body, if any.
6434
6435 Make_Instance_Unit (Body_Cunit, In_Main => True);
6436 Main_Unit_Entity := New_Main;
6437 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6438
6439 -- Build elaboration entity, since the instance may certainly generate
6440 -- elaboration code requiring a flag for protection.
6441
6442 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6443 end Build_Instance_Compilation_Unit_Nodes;
6444
6445 -----------------------------
6446 -- Check_Access_Definition --
6447 -----------------------------
6448
6449 procedure Check_Access_Definition (N : Node_Id) is
6450 begin
6451 pragma Assert
6452 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6453 null;
6454 end Check_Access_Definition;
6455
6456 -----------------------------------
6457 -- Check_Formal_Package_Instance --
6458 -----------------------------------
6459
6460 -- If the formal has specific parameters, they must match those of the
6461 -- actual. Both of them are instances, and the renaming declarations for
6462 -- their formal parameters appear in the same order in both. The analyzed
6463 -- formal has been analyzed in the context of the current instance.
6464
6465 procedure Check_Formal_Package_Instance
6466 (Formal_Pack : Entity_Id;
6467 Actual_Pack : Entity_Id)
6468 is
6469 E1 : Entity_Id := First_Entity (Actual_Pack);
6470 E2 : Entity_Id := First_Entity (Formal_Pack);
6471 Prev_E1 : Entity_Id;
6472
6473 Expr1 : Node_Id;
6474 Expr2 : Node_Id;
6475
6476 procedure Check_Mismatch (B : Boolean);
6477 -- Common error routine for mismatch between the parameters of the
6478 -- actual instance and those of the formal package.
6479
6480 function Is_Defaulted (Param : Entity_Id) return Boolean;
6481 -- If the formal package has partly box-initialized formals, skip
6482 -- conformance check for these formals. Previously the code assumed
6483 -- that box initialization for a formal package applied to all its
6484 -- formal parameters.
6485
6486 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6487 -- The formal may come from a nested formal package, and the actual may
6488 -- have been constant-folded. To determine whether the two denote the
6489 -- same entity we may have to traverse several definitions to recover
6490 -- the ultimate entity that they refer to.
6491
6492 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6493 -- The formal and the actual must be identical, but if both are
6494 -- given by attributes they end up renaming different generated bodies,
6495 -- and we must verify that the attributes themselves match.
6496
6497 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6498 -- Similarly, if the formal comes from a nested formal package, the
6499 -- actual may designate the formal through multiple renamings, which
6500 -- have to be followed to determine the original variable in question.
6501
6502 --------------------
6503 -- Check_Mismatch --
6504 --------------------
6505
6506 procedure Check_Mismatch (B : Boolean) is
6507 -- A Formal_Type_Declaration for a derived private type is rewritten
6508 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6509 -- which is why we examine the original node.
6510
6511 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6512
6513 begin
6514 if Kind = N_Formal_Type_Declaration then
6515 return;
6516
6517 elsif Kind in N_Formal_Object_Declaration
6518 | N_Formal_Package_Declaration
6519 | N_Formal_Subprogram_Declaration
6520 then
6521 null;
6522
6523 -- Ada 2012: If both formal and actual are incomplete types they
6524 -- are conformant.
6525
6526 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6527 null;
6528
6529 elsif B then
6530 Error_Msg_NE
6531 ("actual for & in actual instance does not match formal",
6532 Parent (Actual_Pack), E1);
6533 end if;
6534 end Check_Mismatch;
6535
6536 ------------------
6537 -- Is_Defaulted --
6538 ------------------
6539
6540 function Is_Defaulted (Param : Entity_Id) return Boolean is
6541 Assoc : Node_Id;
6542
6543 begin
6544 Assoc :=
6545 First (Generic_Associations (Parent
6546 (Associated_Formal_Package (Actual_Pack))));
6547
6548 while Present (Assoc) loop
6549 if Nkind (Assoc) = N_Others_Choice then
6550 return True;
6551
6552 elsif Nkind (Assoc) = N_Generic_Association
6553 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6554 then
6555 return Box_Present (Assoc);
6556 end if;
6557
6558 Next (Assoc);
6559 end loop;
6560
6561 return False;
6562 end Is_Defaulted;
6563
6564 --------------------------------
6565 -- Same_Instantiated_Constant --
6566 --------------------------------
6567
6568 function Same_Instantiated_Constant
6569 (E1, E2 : Entity_Id) return Boolean
6570 is
6571 Ent : Entity_Id;
6572
6573 begin
6574 Ent := E2;
6575 while Present (Ent) loop
6576 if E1 = Ent then
6577 return True;
6578
6579 elsif Ekind (Ent) /= E_Constant then
6580 return False;
6581
6582 elsif Is_Entity_Name (Constant_Value (Ent)) then
6583 if Entity (Constant_Value (Ent)) = E1 then
6584 return True;
6585 else
6586 Ent := Entity (Constant_Value (Ent));
6587 end if;
6588
6589 -- The actual may be a constant that has been folded. Recover
6590 -- original name.
6591
6592 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6593 Ent := Entity (Original_Node (Constant_Value (Ent)));
6594
6595 else
6596 return False;
6597 end if;
6598 end loop;
6599
6600 return False;
6601 end Same_Instantiated_Constant;
6602
6603 --------------------------------
6604 -- Same_Instantiated_Function --
6605 --------------------------------
6606
6607 function Same_Instantiated_Function
6608 (E1, E2 : Entity_Id) return Boolean
6609 is
6610 U1, U2 : Node_Id;
6611 begin
6612 if Alias (E1) = Alias (E2) then
6613 return True;
6614
6615 elsif Present (Alias (E2)) then
6616 U1 := Original_Node (Unit_Declaration_Node (E1));
6617 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6618
6619 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6620 and then Nkind (Name (U1)) = N_Attribute_Reference
6621
6622 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6623 and then Nkind (Name (U2)) = N_Attribute_Reference
6624
6625 and then
6626 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6627 else
6628 return False;
6629 end if;
6630 end Same_Instantiated_Function;
6631
6632 --------------------------------
6633 -- Same_Instantiated_Variable --
6634 --------------------------------
6635
6636 function Same_Instantiated_Variable
6637 (E1, E2 : Entity_Id) return Boolean
6638 is
6639 function Original_Entity (E : Entity_Id) return Entity_Id;
6640 -- Follow chain of renamings to the ultimate ancestor
6641
6642 ---------------------
6643 -- Original_Entity --
6644 ---------------------
6645
6646 function Original_Entity (E : Entity_Id) return Entity_Id is
6647 Orig : Entity_Id;
6648
6649 begin
6650 Orig := E;
6651 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6652 and then Present (Renamed_Object (Orig))
6653 and then Is_Entity_Name (Renamed_Object (Orig))
6654 loop
6655 Orig := Entity (Renamed_Object (Orig));
6656 end loop;
6657
6658 return Orig;
6659 end Original_Entity;
6660
6661 -- Start of processing for Same_Instantiated_Variable
6662
6663 begin
6664 return Ekind (E1) = Ekind (E2)
6665 and then Original_Entity (E1) = Original_Entity (E2);
6666 end Same_Instantiated_Variable;
6667
6668 -- Start of processing for Check_Formal_Package_Instance
6669
6670 begin
6671 Prev_E1 := E1;
6672 while Present (E1) and then Present (E2) loop
6673 exit when Ekind (E1) = E_Package
6674 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6675
6676 -- If the formal is the renaming of the formal package, this
6677 -- is the end of its formal part, which may occur before the
6678 -- end of the formal part in the actual in the presence of
6679 -- defaulted parameters in the formal package.
6680
6681 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6682 and then Renamed_Entity (E2) = Scope (E2);
6683
6684 -- The analysis of the actual may generate additional internal
6685 -- entities. If the formal is defaulted, there is no corresponding
6686 -- analysis and the internal entities must be skipped, until we
6687 -- find corresponding entities again.
6688
6689 if Comes_From_Source (E2)
6690 and then not Comes_From_Source (E1)
6691 and then Chars (E1) /= Chars (E2)
6692 then
6693 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6694 Next_Entity (E1);
6695 end loop;
6696 end if;
6697
6698 if No (E1) then
6699 return;
6700
6701 -- Entities may be declared without full declaration, such as
6702 -- itypes and predefined operators (concatenation for arrays, eg).
6703 -- Skip it and keep the formal entity to find a later match for it.
6704
6705 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6706 E1 := Prev_E1;
6707 goto Next_E;
6708
6709 -- If the formal entity comes from a formal declaration, it was
6710 -- defaulted in the formal package, and no check is needed on it.
6711
6712 elsif Nkind (Original_Node (Parent (E2))) in
6713 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6714 then
6715 -- If the formal is a tagged type the corresponding class-wide
6716 -- type has been generated as well, and it must be skipped.
6717
6718 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6719 Next_Entity (E2);
6720 end if;
6721
6722 goto Next_E;
6723
6724 -- Ditto for defaulted formal subprograms.
6725
6726 elsif Is_Overloadable (E1)
6727 and then Nkind (Unit_Declaration_Node (E2)) in
6728 N_Formal_Subprogram_Declaration
6729 then
6730 goto Next_E;
6731
6732 elsif Is_Defaulted (E1) then
6733 goto Next_E;
6734
6735 elsif Is_Type (E1) then
6736
6737 -- Subtypes must statically match. E1, E2 are the local entities
6738 -- that are subtypes of the actuals. Itypes generated for other
6739 -- parameters need not be checked, the check will be performed
6740 -- on the parameters themselves.
6741
6742 -- If E2 is a formal type declaration, it is a defaulted parameter
6743 -- and needs no checking.
6744
6745 if not Is_Itype (E1) and then not Is_Itype (E2) then
6746 Check_Mismatch
6747 (not Is_Type (E2)
6748 or else Etype (E1) /= Etype (E2)
6749 or else not Subtypes_Statically_Match (E1, E2));
6750 end if;
6751
6752 elsif Ekind (E1) = E_Constant then
6753
6754 -- IN parameters must denote the same static value, or the same
6755 -- constant, or the literal null.
6756
6757 Expr1 := Expression (Parent (E1));
6758
6759 if Ekind (E2) /= E_Constant then
6760 Check_Mismatch (True);
6761 goto Next_E;
6762 else
6763 Expr2 := Expression (Parent (E2));
6764 end if;
6765
6766 if Is_OK_Static_Expression (Expr1) then
6767 if not Is_OK_Static_Expression (Expr2) then
6768 Check_Mismatch (True);
6769
6770 elsif Is_Discrete_Type (Etype (E1)) then
6771 declare
6772 V1 : constant Uint := Expr_Value (Expr1);
6773 V2 : constant Uint := Expr_Value (Expr2);
6774 begin
6775 Check_Mismatch (V1 /= V2);
6776 end;
6777
6778 elsif Is_Real_Type (Etype (E1)) then
6779 declare
6780 V1 : constant Ureal := Expr_Value_R (Expr1);
6781 V2 : constant Ureal := Expr_Value_R (Expr2);
6782 begin
6783 Check_Mismatch (V1 /= V2);
6784 end;
6785
6786 elsif Is_String_Type (Etype (E1))
6787 and then Nkind (Expr1) = N_String_Literal
6788 then
6789 if Nkind (Expr2) /= N_String_Literal then
6790 Check_Mismatch (True);
6791 else
6792 Check_Mismatch
6793 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6794 end if;
6795 end if;
6796
6797 elsif Is_Entity_Name (Expr1) then
6798 if Is_Entity_Name (Expr2) then
6799 if Entity (Expr1) = Entity (Expr2) then
6800 null;
6801 else
6802 Check_Mismatch
6803 (not Same_Instantiated_Constant
6804 (Entity (Expr1), Entity (Expr2)));
6805 end if;
6806
6807 else
6808 Check_Mismatch (True);
6809 end if;
6810
6811 elsif Is_Entity_Name (Original_Node (Expr1))
6812 and then Is_Entity_Name (Expr2)
6813 and then Same_Instantiated_Constant
6814 (Entity (Original_Node (Expr1)), Entity (Expr2))
6815 then
6816 null;
6817
6818 elsif Nkind (Expr1) = N_Null then
6819 Check_Mismatch (Nkind (Expr1) /= N_Null);
6820
6821 else
6822 Check_Mismatch (True);
6823 end if;
6824
6825 elsif Ekind (E1) = E_Variable then
6826 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6827
6828 elsif Ekind (E1) = E_Package then
6829 Check_Mismatch
6830 (Ekind (E1) /= Ekind (E2)
6831 or else (Present (Renamed_Object (E2))
6832 and then Renamed_Object (E1) /=
6833 Renamed_Object (E2)));
6834
6835 elsif Is_Overloadable (E1) then
6836 -- Verify that the actual subprograms match. Note that actuals
6837 -- that are attributes are rewritten as subprograms. If the
6838 -- subprogram in the formal package is defaulted, no check is
6839 -- needed. Note that this can only happen in Ada 2005 when the
6840 -- formal package can be partially parameterized.
6841
6842 if Nkind (Unit_Declaration_Node (E1)) =
6843 N_Subprogram_Renaming_Declaration
6844 and then From_Default (Unit_Declaration_Node (E1))
6845 then
6846 null;
6847
6848 -- If the formal package has an "others" box association that
6849 -- covers this formal, there is no need for a check either.
6850
6851 elsif Nkind (Unit_Declaration_Node (E2)) in
6852 N_Formal_Subprogram_Declaration
6853 and then Box_Present (Unit_Declaration_Node (E2))
6854 then
6855 null;
6856
6857 -- No check needed if subprogram is a defaulted null procedure
6858
6859 elsif No (Alias (E2))
6860 and then Ekind (E2) = E_Procedure
6861 and then
6862 Null_Present (Specification (Unit_Declaration_Node (E2)))
6863 then
6864 null;
6865
6866 -- Otherwise the actual in the formal and the actual in the
6867 -- instantiation of the formal must match, up to renamings.
6868
6869 else
6870 Check_Mismatch
6871 (Ekind (E2) /= Ekind (E1)
6872 or else not Same_Instantiated_Function (E1, E2));
6873 end if;
6874
6875 else
6876 raise Program_Error;
6877 end if;
6878
6879 <<Next_E>>
6880 Prev_E1 := E1;
6881 Next_Entity (E1);
6882 Next_Entity (E2);
6883 end loop;
6884 end Check_Formal_Package_Instance;
6885
6886 ---------------------------
6887 -- Check_Formal_Packages --
6888 ---------------------------
6889
6890 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6891 E : Entity_Id;
6892 Formal_P : Entity_Id;
6893 Formal_Decl : Node_Id;
6894 begin
6895 -- Iterate through the declarations in the instance, looking for package
6896 -- renaming declarations that denote instances of formal packages. Stop
6897 -- when we find the renaming of the current package itself. The
6898 -- declaration for a formal package without a box is followed by an
6899 -- internal entity that repeats the instantiation.
6900
6901 E := First_Entity (P_Id);
6902 while Present (E) loop
6903 if Ekind (E) = E_Package then
6904 if Renamed_Object (E) = P_Id then
6905 exit;
6906
6907 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6908 null;
6909
6910 else
6911 Formal_Decl := Parent (Associated_Formal_Package (E));
6912
6913 -- Nothing to check if the formal has a box or an others_clause
6914 -- (necessarily with a box), or no associations altogether
6915
6916 if Box_Present (Formal_Decl)
6917 or else No (Generic_Associations (Formal_Decl))
6918 then
6919 null;
6920
6921 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6922 N_Others_Choice
6923 then
6924 -- The internal validating package was generated but formal
6925 -- and instance are known to be compatible.
6926
6927 Formal_P := Next_Entity (E);
6928 Remove (Unit_Declaration_Node (Formal_P));
6929
6930 else
6931 Formal_P := Next_Entity (E);
6932
6933 -- If the instance is within an enclosing instance body
6934 -- there is no need to verify the legality of current formal
6935 -- packages because they were legal in the generic body.
6936 -- This optimization may be applicable elsewhere, and it
6937 -- also removes spurious errors that may arise with
6938 -- on-the-fly inlining and confusion between private and
6939 -- full views.
6940
6941 if not In_Instance_Body then
6942 Check_Formal_Package_Instance (Formal_P, E);
6943 end if;
6944
6945 -- Restore the visibility of formals of the formal instance
6946 -- that are not defaulted, and are hidden within the current
6947 -- generic. These formals may be visible within an enclosing
6948 -- generic.
6949
6950 declare
6951 Elmt : Elmt_Id;
6952 begin
6953 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6954 while Present (Elmt) loop
6955 Set_Is_Hidden (Node (Elmt), False);
6956 Next_Elmt (Elmt);
6957 end loop;
6958 end;
6959
6960 -- After checking, remove the internal validating package.
6961 -- It is only needed for semantic checks, and as it may
6962 -- contain generic formal declarations it should not reach
6963 -- gigi.
6964
6965 Remove (Unit_Declaration_Node (Formal_P));
6966 end if;
6967 end if;
6968 end if;
6969
6970 Next_Entity (E);
6971 end loop;
6972 end Check_Formal_Packages;
6973
6974 ---------------------------------
6975 -- Check_Forward_Instantiation --
6976 ---------------------------------
6977
6978 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6979 S : Entity_Id;
6980 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6981
6982 begin
6983 -- The instantiation appears before the generic body if we are in the
6984 -- scope of the unit containing the generic, either in its spec or in
6985 -- the package body, and before the generic body.
6986
6987 if Ekind (Gen_Comp) = E_Package_Body then
6988 Gen_Comp := Spec_Entity (Gen_Comp);
6989 end if;
6990
6991 if In_Open_Scopes (Gen_Comp)
6992 and then No (Corresponding_Body (Decl))
6993 then
6994 S := Current_Scope;
6995
6996 while Present (S)
6997 and then not Is_Compilation_Unit (S)
6998 and then not Is_Child_Unit (S)
6999 loop
7000 if Ekind (S) = E_Package then
7001 Set_Has_Forward_Instantiation (S);
7002 end if;
7003
7004 S := Scope (S);
7005 end loop;
7006 end if;
7007 end Check_Forward_Instantiation;
7008
7009 ---------------------------
7010 -- Check_Generic_Actuals --
7011 ---------------------------
7012
7013 -- The visibility of the actuals may be different between the point of
7014 -- generic instantiation and the instantiation of the body.
7015
7016 procedure Check_Generic_Actuals
7017 (Instance : Entity_Id;
7018 Is_Formal_Box : Boolean)
7019 is
7020 E : Entity_Id;
7021 Astype : Entity_Id;
7022
7023 begin
7024 E := First_Entity (Instance);
7025 while Present (E) loop
7026 if Is_Type (E)
7027 and then Nkind (Parent (E)) = N_Subtype_Declaration
7028 and then Scope (Etype (E)) /= Instance
7029 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7030 then
7031 -- Restore the proper view of the actual from the information
7032 -- saved earlier by Instantiate_Type.
7033
7034 Check_Private_View (Subtype_Indication (Parent (E)));
7035
7036 -- If the actual is itself the formal of a parent instance,
7037 -- then also restore the proper view of its actual and so on.
7038 -- That's necessary for nested instantiations of the form
7039
7040 -- generic
7041 -- type Component is private;
7042 -- type Array_Type is array (Positive range <>) of Component;
7043 -- procedure Proc;
7044
7045 -- when the outermost actuals have inconsistent views, because
7046 -- the Component_Type of Array_Type of the inner instantiations
7047 -- is the actual of Component of the outermost one and not that
7048 -- of the corresponding inner instantiations.
7049
7050 Astype := Ancestor_Subtype (E);
7051 while Present (Astype)
7052 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7053 and then Present (Generic_Parent_Type (Parent (Astype)))
7054 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7055 loop
7056 Check_Private_View (Subtype_Indication (Parent (Astype)));
7057 Astype := Ancestor_Subtype (Astype);
7058 end loop;
7059
7060 Set_Is_Generic_Actual_Type (E);
7061
7062 if Is_Private_Type (E) and then Present (Full_View (E)) then
7063 Set_Is_Generic_Actual_Type (Full_View (E));
7064 end if;
7065
7066 Set_Is_Hidden (E, False);
7067 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7068
7069 -- We constructed the generic actual type as a subtype of the
7070 -- supplied type. This means that it normally would not inherit
7071 -- subtype specific attributes of the actual, which is wrong for
7072 -- the generic case.
7073
7074 Astype := Ancestor_Subtype (E);
7075
7076 if No (Astype) then
7077
7078 -- This can happen when E is an itype that is the full view of
7079 -- a private type completed, e.g. with a constrained array. In
7080 -- that case, use the first subtype, which will carry size
7081 -- information. The base type itself is unconstrained and will
7082 -- not carry it.
7083
7084 Astype := First_Subtype (E);
7085 end if;
7086
7087 Set_Size_Info (E, (Astype));
7088 Set_RM_Size (E, RM_Size (Astype));
7089 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7090
7091 if Is_Discrete_Or_Fixed_Point_Type (E) then
7092 Set_RM_Size (E, RM_Size (Astype));
7093 end if;
7094
7095 elsif Ekind (E) = E_Package then
7096
7097 -- If this is the renaming for the current instance, we're done.
7098 -- Otherwise it is a formal package. If the corresponding formal
7099 -- was declared with a box, the (instantiations of the) generic
7100 -- formal part are also visible. Otherwise, ignore the entity
7101 -- created to validate the actuals.
7102
7103 if Renamed_Object (E) = Instance then
7104 exit;
7105
7106 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7107 null;
7108
7109 -- The visibility of a formal of an enclosing generic is already
7110 -- correct.
7111
7112 elsif Denotes_Formal_Package (E) then
7113 null;
7114
7115 elsif Present (Associated_Formal_Package (E))
7116 and then not Is_Generic_Formal (E)
7117 then
7118 if Box_Present (Parent (Associated_Formal_Package (E))) then
7119 Check_Generic_Actuals (Renamed_Object (E), True);
7120
7121 else
7122 Check_Generic_Actuals (Renamed_Object (E), False);
7123 end if;
7124
7125 Set_Is_Hidden (E, False);
7126 end if;
7127
7128 -- If this is a subprogram instance (in a wrapper package) the
7129 -- actual is fully visible.
7130
7131 elsif Is_Wrapper_Package (Instance) then
7132 Set_Is_Hidden (E, False);
7133
7134 -- If the formal package is declared with a box, or if the formal
7135 -- parameter is defaulted, it is visible in the body.
7136
7137 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7138 Set_Is_Hidden (E, False);
7139 end if;
7140
7141 if Ekind (E) = E_Constant then
7142
7143 -- If the type of the actual is a private type declared in the
7144 -- enclosing scope of the generic unit, the body of the generic
7145 -- sees the full view of the type (because it has to appear in
7146 -- the corresponding package body). If the type is private now,
7147 -- exchange views to restore the proper visiblity in the instance.
7148
7149 declare
7150 Typ : constant Entity_Id := Base_Type (Etype (E));
7151 -- The type of the actual
7152
7153 Gen_Id : Entity_Id;
7154 -- The generic unit
7155
7156 Parent_Scope : Entity_Id;
7157 -- The enclosing scope of the generic unit
7158
7159 begin
7160 if Is_Wrapper_Package (Instance) then
7161 Gen_Id :=
7162 Generic_Parent
7163 (Specification
7164 (Unit_Declaration_Node
7165 (Related_Instance (Instance))));
7166 else
7167 Gen_Id :=
7168 Generic_Parent (Package_Specification (Instance));
7169 end if;
7170
7171 Parent_Scope := Scope (Gen_Id);
7172
7173 -- The exchange is only needed if the generic is defined
7174 -- within a package which is not a common ancestor of the
7175 -- scope of the instance, and is not already in scope.
7176
7177 if Is_Private_Type (Typ)
7178 and then Scope (Typ) = Parent_Scope
7179 and then Scope (Instance) /= Parent_Scope
7180 and then Ekind (Parent_Scope) = E_Package
7181 and then not Is_Child_Unit (Gen_Id)
7182 then
7183 Switch_View (Typ);
7184
7185 -- If the type of the entity is a subtype, it may also have
7186 -- to be made visible, together with the base type of its
7187 -- full view, after exchange.
7188
7189 if Is_Private_Type (Etype (E)) then
7190 Switch_View (Etype (E));
7191 Switch_View (Base_Type (Etype (E)));
7192 end if;
7193 end if;
7194 end;
7195 end if;
7196
7197 Next_Entity (E);
7198 end loop;
7199 end Check_Generic_Actuals;
7200
7201 ------------------------------
7202 -- Check_Generic_Child_Unit --
7203 ------------------------------
7204
7205 procedure Check_Generic_Child_Unit
7206 (Gen_Id : Node_Id;
7207 Parent_Installed : in out Boolean)
7208 is
7209 Loc : constant Source_Ptr := Sloc (Gen_Id);
7210 Gen_Par : Entity_Id := Empty;
7211 E : Entity_Id;
7212 Inst_Par : Entity_Id;
7213 S : Node_Id;
7214
7215 function Find_Generic_Child
7216 (Scop : Entity_Id;
7217 Id : Node_Id) return Entity_Id;
7218 -- Search generic parent for possible child unit with the given name
7219
7220 function In_Enclosing_Instance return Boolean;
7221 -- Within an instance of the parent, the child unit may be denoted by
7222 -- a simple name, or an abbreviated expanded name. Examine enclosing
7223 -- scopes to locate a possible parent instantiation.
7224
7225 ------------------------
7226 -- Find_Generic_Child --
7227 ------------------------
7228
7229 function Find_Generic_Child
7230 (Scop : Entity_Id;
7231 Id : Node_Id) return Entity_Id
7232 is
7233 E : Entity_Id;
7234
7235 begin
7236 -- If entity of name is already set, instance has already been
7237 -- resolved, e.g. in an enclosing instantiation.
7238
7239 if Present (Entity (Id)) then
7240 if Scope (Entity (Id)) = Scop then
7241 return Entity (Id);
7242 else
7243 return Empty;
7244 end if;
7245
7246 else
7247 E := First_Entity (Scop);
7248 while Present (E) loop
7249 if Chars (E) = Chars (Id)
7250 and then Is_Child_Unit (E)
7251 then
7252 if Is_Child_Unit (E)
7253 and then not Is_Visible_Lib_Unit (E)
7254 then
7255 Error_Msg_NE
7256 ("generic child unit& is not visible", Gen_Id, E);
7257 end if;
7258
7259 Set_Entity (Id, E);
7260 return E;
7261 end if;
7262
7263 Next_Entity (E);
7264 end loop;
7265
7266 return Empty;
7267 end if;
7268 end Find_Generic_Child;
7269
7270 ---------------------------
7271 -- In_Enclosing_Instance --
7272 ---------------------------
7273
7274 function In_Enclosing_Instance return Boolean is
7275 Enclosing_Instance : Node_Id;
7276 Instance_Decl : Node_Id;
7277
7278 begin
7279 -- We do not inline any call that contains instantiations, except
7280 -- for instantiations of Unchecked_Conversion, so if we are within
7281 -- an inlined body the current instance does not require parents.
7282
7283 if In_Inlined_Body then
7284 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7285 return False;
7286 end if;
7287
7288 -- Loop to check enclosing scopes
7289
7290 Enclosing_Instance := Current_Scope;
7291 while Present (Enclosing_Instance) loop
7292 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7293
7294 if Ekind (Enclosing_Instance) = E_Package
7295 and then Is_Generic_Instance (Enclosing_Instance)
7296 and then Present
7297 (Generic_Parent (Specification (Instance_Decl)))
7298 then
7299 -- Check whether the generic we are looking for is a child of
7300 -- this instance.
7301
7302 E := Find_Generic_Child
7303 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7304 exit when Present (E);
7305
7306 else
7307 E := Empty;
7308 end if;
7309
7310 Enclosing_Instance := Scope (Enclosing_Instance);
7311 end loop;
7312
7313 if No (E) then
7314
7315 -- Not a child unit
7316
7317 Analyze (Gen_Id);
7318 return False;
7319
7320 else
7321 Rewrite (Gen_Id,
7322 Make_Expanded_Name (Loc,
7323 Chars => Chars (E),
7324 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7325 Selector_Name => New_Occurrence_Of (E, Loc)));
7326
7327 Set_Entity (Gen_Id, E);
7328 Set_Etype (Gen_Id, Etype (E));
7329 Parent_Installed := False; -- Already in scope.
7330 return True;
7331 end if;
7332 end In_Enclosing_Instance;
7333
7334 -- Start of processing for Check_Generic_Child_Unit
7335
7336 begin
7337 -- If the name of the generic is given by a selected component, it may
7338 -- be the name of a generic child unit, and the prefix is the name of an
7339 -- instance of the parent, in which case the child unit must be visible.
7340 -- If this instance is not in scope, it must be placed there and removed
7341 -- after instantiation, because what is being instantiated is not the
7342 -- original child, but the corresponding child present in the instance
7343 -- of the parent.
7344
7345 -- If the child is instantiated within the parent, it can be given by
7346 -- a simple name. In this case the instance is already in scope, but
7347 -- the child generic must be recovered from the generic parent as well.
7348
7349 if Nkind (Gen_Id) = N_Selected_Component then
7350 S := Selector_Name (Gen_Id);
7351 Analyze (Prefix (Gen_Id));
7352 Inst_Par := Entity (Prefix (Gen_Id));
7353
7354 if Ekind (Inst_Par) = E_Package
7355 and then Present (Renamed_Object (Inst_Par))
7356 then
7357 Inst_Par := Renamed_Object (Inst_Par);
7358 end if;
7359
7360 if Ekind (Inst_Par) = E_Package then
7361 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7362 Gen_Par := Generic_Parent (Parent (Inst_Par));
7363
7364 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7365 and then
7366 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7367 then
7368 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7369 end if;
7370
7371 elsif Ekind (Inst_Par) = E_Generic_Package
7372 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7373 then
7374 -- A formal package may be a real child package, and not the
7375 -- implicit instance within a parent. In this case the child is
7376 -- not visible and has to be retrieved explicitly as well.
7377
7378 Gen_Par := Inst_Par;
7379 end if;
7380
7381 if Present (Gen_Par) then
7382
7383 -- The prefix denotes an instantiation. The entity itself may be a
7384 -- nested generic, or a child unit.
7385
7386 E := Find_Generic_Child (Gen_Par, S);
7387
7388 if Present (E) then
7389 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7390 Set_Entity (Gen_Id, E);
7391 Set_Etype (Gen_Id, Etype (E));
7392 Set_Entity (S, E);
7393 Set_Etype (S, Etype (E));
7394
7395 -- Indicate that this is a reference to the parent
7396
7397 if In_Extended_Main_Source_Unit (Gen_Id) then
7398 Set_Is_Instantiated (Inst_Par);
7399 end if;
7400
7401 -- A common mistake is to replicate the naming scheme of a
7402 -- hierarchy by instantiating a generic child directly, rather
7403 -- than the implicit child in a parent instance:
7404
7405 -- generic .. package Gpar is ..
7406 -- generic .. package Gpar.Child is ..
7407 -- package Par is new Gpar ();
7408
7409 -- with Gpar.Child;
7410 -- package Par.Child is new Gpar.Child ();
7411 -- rather than Par.Child
7412
7413 -- In this case the instantiation is within Par, which is an
7414 -- instance, but Gpar does not denote Par because we are not IN
7415 -- the instance of Gpar, so this is illegal. The test below
7416 -- recognizes this particular case.
7417
7418 if Is_Child_Unit (E)
7419 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7420 and then (not In_Instance
7421 or else Nkind (Parent (Parent (Gen_Id))) =
7422 N_Compilation_Unit)
7423 then
7424 Error_Msg_N
7425 ("prefix of generic child unit must be instance of parent",
7426 Gen_Id);
7427 end if;
7428
7429 if not In_Open_Scopes (Inst_Par)
7430 and then Nkind (Parent (Gen_Id)) not in
7431 N_Generic_Renaming_Declaration
7432 then
7433 Install_Parent (Inst_Par);
7434 Parent_Installed := True;
7435
7436 elsif In_Open_Scopes (Inst_Par) then
7437
7438 -- If the parent is already installed, install the actuals
7439 -- for its formal packages. This is necessary when the child
7440 -- instance is a child of the parent instance: in this case,
7441 -- the parent is placed on the scope stack but the formal
7442 -- packages are not made visible.
7443
7444 Install_Formal_Packages (Inst_Par);
7445 end if;
7446
7447 else
7448 -- If the generic parent does not contain an entity that
7449 -- corresponds to the selector, the instance doesn't either.
7450 -- Analyzing the node will yield the appropriate error message.
7451 -- If the entity is not a child unit, then it is an inner
7452 -- generic in the parent.
7453
7454 Analyze (Gen_Id);
7455 end if;
7456
7457 else
7458 Analyze (Gen_Id);
7459
7460 if Is_Child_Unit (Entity (Gen_Id))
7461 and then
7462 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7463 and then not In_Open_Scopes (Inst_Par)
7464 then
7465 Install_Parent (Inst_Par);
7466 Parent_Installed := True;
7467
7468 -- The generic unit may be the renaming of the implicit child
7469 -- present in an instance. In that case the parent instance is
7470 -- obtained from the name of the renamed entity.
7471
7472 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7473 and then Present (Renamed_Entity (Entity (Gen_Id)))
7474 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7475 then
7476 declare
7477 Renamed_Package : constant Node_Id :=
7478 Name (Parent (Entity (Gen_Id)));
7479 begin
7480 if Nkind (Renamed_Package) = N_Expanded_Name then
7481 Inst_Par := Entity (Prefix (Renamed_Package));
7482 Install_Parent (Inst_Par);
7483 Parent_Installed := True;
7484 end if;
7485 end;
7486 end if;
7487 end if;
7488
7489 elsif Nkind (Gen_Id) = N_Expanded_Name then
7490
7491 -- Entity already present, analyze prefix, whose meaning may be an
7492 -- instance in the current context. If it is an instance of a
7493 -- relative within another, the proper parent may still have to be
7494 -- installed, if they are not of the same generation.
7495
7496 Analyze (Prefix (Gen_Id));
7497
7498 -- Prevent cascaded errors
7499
7500 if Etype (Prefix (Gen_Id)) = Any_Type then
7501 return;
7502 end if;
7503
7504 -- In the unlikely case that a local declaration hides the name of
7505 -- the parent package, locate it on the homonym chain. If the context
7506 -- is an instance of the parent, the renaming entity is flagged as
7507 -- such.
7508
7509 Inst_Par := Entity (Prefix (Gen_Id));
7510 while Present (Inst_Par)
7511 and then not Is_Package_Or_Generic_Package (Inst_Par)
7512 loop
7513 Inst_Par := Homonym (Inst_Par);
7514 end loop;
7515
7516 pragma Assert (Present (Inst_Par));
7517 Set_Entity (Prefix (Gen_Id), Inst_Par);
7518
7519 if In_Enclosing_Instance then
7520 null;
7521
7522 elsif Present (Entity (Gen_Id))
7523 and then No (Renamed_Entity (Entity (Gen_Id)))
7524 and then Is_Child_Unit (Entity (Gen_Id))
7525 and then not In_Open_Scopes (Inst_Par)
7526 then
7527 Install_Parent (Inst_Par);
7528 Parent_Installed := True;
7529
7530 -- Handle renaming of generic child unit
7531
7532 elsif Present (Entity (Gen_Id))
7533 and then Present (Renamed_Entity (Entity (Gen_Id)))
7534 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7535 then
7536 declare
7537 E : Entity_Id;
7538 Ren_Decl : Node_Id;
7539
7540 begin
7541 -- The entity of the renamed generic child unit does not
7542 -- have any reference to the instantiated parent. In order to
7543 -- locate it we traverse the scope containing the renaming
7544 -- declaration; the instance of the parent is available in
7545 -- the prefix of the renaming declaration. For example:
7546
7547 -- package A is
7548 -- package Inst_Par is new ...
7549 -- generic package Ren_Child renames Ins_Par.Child;
7550 -- end;
7551
7552 -- with A;
7553 -- package B is
7554 -- package Inst_Child is new A.Ren_Child;
7555 -- end;
7556
7557 E := First_Entity (Entity (Prefix (Gen_Id)));
7558 while Present (E) loop
7559 if Present (Renamed_Entity (E))
7560 and then
7561 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7562 then
7563 Ren_Decl := Parent (E);
7564 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7565
7566 if not In_Open_Scopes (Inst_Par) then
7567 Install_Parent (Inst_Par);
7568 Parent_Installed := True;
7569 end if;
7570
7571 exit;
7572 end if;
7573
7574 E := Next_Entity (E);
7575 end loop;
7576 end;
7577 end if;
7578
7579 elsif In_Enclosing_Instance then
7580
7581 -- The child unit is found in some enclosing scope
7582
7583 null;
7584
7585 else
7586 Analyze (Gen_Id);
7587
7588 -- If this is the renaming of the implicit child in a parent
7589 -- instance, recover the parent name and install it.
7590
7591 if Is_Entity_Name (Gen_Id) then
7592 E := Entity (Gen_Id);
7593
7594 if Is_Generic_Unit (E)
7595 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7596 and then Is_Child_Unit (Renamed_Object (E))
7597 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7598 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7599 then
7600 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7601 Inst_Par := Entity (Prefix (Gen_Id));
7602
7603 if not In_Open_Scopes (Inst_Par) then
7604 Install_Parent (Inst_Par);
7605 Parent_Installed := True;
7606 end if;
7607
7608 -- If it is a child unit of a non-generic parent, it may be
7609 -- use-visible and given by a direct name. Install parent as
7610 -- for other cases.
7611
7612 elsif Is_Generic_Unit (E)
7613 and then Is_Child_Unit (E)
7614 and then
7615 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7616 and then not Is_Generic_Unit (Scope (E))
7617 then
7618 if not In_Open_Scopes (Scope (E)) then
7619 Install_Parent (Scope (E));
7620 Parent_Installed := True;
7621 end if;
7622 end if;
7623 end if;
7624 end if;
7625 end Check_Generic_Child_Unit;
7626
7627 -----------------------------
7628 -- Check_Hidden_Child_Unit --
7629 -----------------------------
7630
7631 procedure Check_Hidden_Child_Unit
7632 (N : Node_Id;
7633 Gen_Unit : Entity_Id;
7634 Act_Decl_Id : Entity_Id)
7635 is
7636 Gen_Id : constant Node_Id := Name (N);
7637
7638 begin
7639 if Is_Child_Unit (Gen_Unit)
7640 and then Is_Child_Unit (Act_Decl_Id)
7641 and then Nkind (Gen_Id) = N_Expanded_Name
7642 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7643 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7644 then
7645 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7646 Error_Msg_NE
7647 ("generic unit & is implicitly declared in &",
7648 Defining_Unit_Name (N), Gen_Unit);
7649 Error_Msg_N ("\instance must have different name",
7650 Defining_Unit_Name (N));
7651 end if;
7652 end Check_Hidden_Child_Unit;
7653
7654 ------------------------
7655 -- Check_Private_View --
7656 ------------------------
7657
7658 procedure Check_Private_View (N : Node_Id) is
7659 T : constant Entity_Id := Etype (N);
7660 BT : Entity_Id;
7661
7662 begin
7663 -- Exchange views if the type was not private in the generic but is
7664 -- private at the point of instantiation. Do not exchange views if
7665 -- the scope of the type is in scope. This can happen if both generic
7666 -- and instance are sibling units, or if type is defined in a parent.
7667 -- In this case the visibility of the type will be correct for all
7668 -- semantic checks.
7669
7670 if Present (T) then
7671 BT := Base_Type (T);
7672
7673 if Is_Private_Type (T)
7674 and then not Has_Private_View (N)
7675 and then Present (Full_View (T))
7676 and then not In_Open_Scopes (Scope (T))
7677 then
7678 -- In the generic, the full declaration was visible
7679
7680 Switch_View (T);
7681
7682 elsif Has_Private_View (N)
7683 and then not Is_Private_Type (T)
7684 and then not Has_Been_Exchanged (T)
7685 and then (not In_Open_Scopes (Scope (T))
7686 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7687 then
7688 -- In the generic, only the private declaration was visible
7689
7690 -- If the type appears in a subtype declaration, the subtype in
7691 -- instance must have a view compatible with that of its parent,
7692 -- which must be exchanged (see corresponding code in Restore_
7693 -- Private_Views) so we make an exception to the open scope rule.
7694
7695 Prepend_Elmt (T, Exchanged_Views);
7696 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7697
7698 -- Finally, a non-private subtype may have a private base type, which
7699 -- must be exchanged for consistency. This can happen when a package
7700 -- body is instantiated, when the scope stack is empty but in fact
7701 -- the subtype and the base type are declared in an enclosing scope.
7702
7703 -- Note that in this case we introduce an inconsistency in the view
7704 -- set, because we switch the base type BT, but there could be some
7705 -- private dependent subtypes of BT which remain unswitched. Such
7706 -- subtypes might need to be switched at a later point (see specific
7707 -- provision for that case in Switch_View).
7708
7709 elsif not Is_Private_Type (T)
7710 and then not Has_Private_View (N)
7711 and then Is_Private_Type (BT)
7712 and then Present (Full_View (BT))
7713 and then not Is_Generic_Type (BT)
7714 and then not In_Open_Scopes (BT)
7715 then
7716 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7717 Exchange_Declarations (BT);
7718 end if;
7719 end if;
7720 end Check_Private_View;
7721
7722 -----------------------------
7723 -- Check_Hidden_Primitives --
7724 -----------------------------
7725
7726 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7727 Actual : Node_Id;
7728 Gen_T : Entity_Id;
7729 Result : Elist_Id := No_Elist;
7730
7731 begin
7732 if No (Assoc_List) then
7733 return No_Elist;
7734 end if;
7735
7736 -- Traverse the list of associations between formals and actuals
7737 -- searching for renamings of tagged types
7738
7739 Actual := First (Assoc_List);
7740 while Present (Actual) loop
7741 if Nkind (Actual) = N_Subtype_Declaration then
7742 Gen_T := Generic_Parent_Type (Actual);
7743
7744 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7745
7746 -- Traverse the list of primitives of the actual types
7747 -- searching for hidden primitives that are visible in the
7748 -- corresponding generic formal; leave them visible and
7749 -- append them to Result to restore their decoration later.
7750
7751 Install_Hidden_Primitives
7752 (Prims_List => Result,
7753 Gen_T => Gen_T,
7754 Act_T => Entity (Subtype_Indication (Actual)));
7755 end if;
7756 end if;
7757
7758 Next (Actual);
7759 end loop;
7760
7761 return Result;
7762 end Check_Hidden_Primitives;
7763
7764 --------------------------
7765 -- Contains_Instance_Of --
7766 --------------------------
7767
7768 function Contains_Instance_Of
7769 (Inner : Entity_Id;
7770 Outer : Entity_Id;
7771 N : Node_Id) return Boolean
7772 is
7773 Elmt : Elmt_Id;
7774 Scop : Entity_Id;
7775
7776 begin
7777 Scop := Outer;
7778
7779 -- Verify that there are no circular instantiations. We check whether
7780 -- the unit contains an instance of the current scope or some enclosing
7781 -- scope (in case one of the instances appears in a subunit). Longer
7782 -- circularities involving subunits might seem too pathological to
7783 -- consider, but they were not too pathological for the authors of
7784 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7785 -- enclosing generic scopes as containing an instance.
7786
7787 loop
7788 -- Within a generic subprogram body, the scope is not generic, to
7789 -- allow for recursive subprograms. Use the declaration to determine
7790 -- whether this is a generic unit.
7791
7792 if Ekind (Scop) = E_Generic_Package
7793 or else (Is_Subprogram (Scop)
7794 and then Nkind (Unit_Declaration_Node (Scop)) =
7795 N_Generic_Subprogram_Declaration)
7796 then
7797 Elmt := First_Elmt (Inner_Instances (Inner));
7798
7799 while Present (Elmt) loop
7800 if Node (Elmt) = Scop then
7801 Error_Msg_Node_2 := Inner;
7802 Error_Msg_NE
7803 ("circular Instantiation: & instantiated within &!",
7804 N, Scop);
7805 return True;
7806
7807 elsif Node (Elmt) = Inner then
7808 return True;
7809
7810 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7811 Error_Msg_Node_2 := Inner;
7812 Error_Msg_NE
7813 ("circular Instantiation: & instantiated within &!",
7814 N, Node (Elmt));
7815 return True;
7816 end if;
7817
7818 Next_Elmt (Elmt);
7819 end loop;
7820
7821 -- Indicate that Inner is being instantiated within Scop
7822
7823 Append_Elmt (Inner, Inner_Instances (Scop));
7824 end if;
7825
7826 if Scop = Standard_Standard then
7827 exit;
7828 else
7829 Scop := Scope (Scop);
7830 end if;
7831 end loop;
7832
7833 return False;
7834 end Contains_Instance_Of;
7835
7836 -----------------------
7837 -- Copy_Generic_Node --
7838 -----------------------
7839
7840 function Copy_Generic_Node
7841 (N : Node_Id;
7842 Parent_Id : Node_Id;
7843 Instantiating : Boolean) return Node_Id
7844 is
7845 Ent : Entity_Id;
7846 New_N : Node_Id;
7847
7848 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7849 -- Check the given value of one of the Fields referenced by the current
7850 -- node to determine whether to copy it recursively. The field may hold
7851 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7852 -- Char) in which case it need not be copied.
7853
7854 procedure Copy_Descendants;
7855 -- Common utility for various nodes
7856
7857 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7858 -- Make copy of element list
7859
7860 function Copy_Generic_List
7861 (L : List_Id;
7862 Parent_Id : Node_Id) return List_Id;
7863 -- Apply Copy_Node recursively to the members of a node list
7864
7865 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7866 -- True if an identifier is part of the defining program unit name of
7867 -- a child unit.
7868 -- Consider removing this subprogram now that ASIS no longer uses it.
7869
7870 ----------------------
7871 -- Copy_Descendants --
7872 ----------------------
7873
7874 procedure Copy_Descendants is
7875 use Atree.Unchecked_Access;
7876 -- This code section is part of the implementation of an untyped
7877 -- tree traversal, so it needs direct access to node fields.
7878
7879 begin
7880 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7881 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7882 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7883 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7884 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7885 end Copy_Descendants;
7886
7887 -----------------------------
7888 -- Copy_Generic_Descendant --
7889 -----------------------------
7890
7891 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7892 begin
7893 if D = Union_Id (Empty) then
7894 return D;
7895
7896 elsif D in Node_Range then
7897 return Union_Id
7898 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7899
7900 elsif D in List_Range then
7901 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7902
7903 elsif D in Elist_Range then
7904 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7905
7906 -- Nothing else is copyable (e.g. Uint values), return as is
7907
7908 else
7909 return D;
7910 end if;
7911 end Copy_Generic_Descendant;
7912
7913 ------------------------
7914 -- Copy_Generic_Elist --
7915 ------------------------
7916
7917 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7918 M : Elmt_Id;
7919 L : Elist_Id;
7920
7921 begin
7922 if Present (E) then
7923 L := New_Elmt_List;
7924 M := First_Elmt (E);
7925 while Present (M) loop
7926 Append_Elmt
7927 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7928 Next_Elmt (M);
7929 end loop;
7930
7931 return L;
7932
7933 else
7934 return No_Elist;
7935 end if;
7936 end Copy_Generic_Elist;
7937
7938 -----------------------
7939 -- Copy_Generic_List --
7940 -----------------------
7941
7942 function Copy_Generic_List
7943 (L : List_Id;
7944 Parent_Id : Node_Id) return List_Id
7945 is
7946 N : Node_Id;
7947 New_L : List_Id;
7948
7949 begin
7950 if Present (L) then
7951 New_L := New_List;
7952 Set_Parent (New_L, Parent_Id);
7953
7954 N := First (L);
7955 while Present (N) loop
7956 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7957 Next (N);
7958 end loop;
7959
7960 return New_L;
7961
7962 else
7963 return No_List;
7964 end if;
7965 end Copy_Generic_List;
7966
7967 ---------------------------
7968 -- In_Defining_Unit_Name --
7969 ---------------------------
7970
7971 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7972 begin
7973 return
7974 Present (Parent (Nam))
7975 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7976 or else
7977 (Nkind (Parent (Nam)) = N_Expanded_Name
7978 and then In_Defining_Unit_Name (Parent (Nam))));
7979 end In_Defining_Unit_Name;
7980
7981 -- Start of processing for Copy_Generic_Node
7982
7983 begin
7984 if N = Empty then
7985 return N;
7986 end if;
7987
7988 New_N := New_Copy (N);
7989
7990 -- Copy aspects if present
7991
7992 if Has_Aspects (N) then
7993 Set_Has_Aspects (New_N, False);
7994 Set_Aspect_Specifications
7995 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7996 end if;
7997
7998 -- If we are instantiating, we want to adjust the sloc based on the
7999 -- current S_Adjustment. However, if this is the root node of a subunit,
8000 -- we need to defer that adjustment to below (see "elsif Instantiating
8001 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8002 -- computed the adjustment.
8003
8004 if Instantiating
8005 and then not (Nkind (N) in N_Proper_Body
8006 and then Was_Originally_Stub (N))
8007 then
8008 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8009 end if;
8010
8011 if not Is_List_Member (N) then
8012 Set_Parent (New_N, Parent_Id);
8013 end if;
8014
8015 -- Special casing for identifiers and other entity names and operators
8016
8017 if Nkind (New_N) in N_Character_Literal
8018 | N_Expanded_Name
8019 | N_Identifier
8020 | N_Operator_Symbol
8021 | N_Op
8022 then
8023 if not Instantiating then
8024
8025 -- Link both nodes in order to assign subsequently the entity of
8026 -- the copy to the original node, in case this is a global
8027 -- reference.
8028
8029 Set_Associated_Node (N, New_N);
8030
8031 -- If we are within an instantiation, this is a nested generic
8032 -- that has already been analyzed at the point of definition.
8033 -- We must preserve references that were global to the enclosing
8034 -- parent at that point. Other occurrences, whether global or
8035 -- local to the current generic, must be resolved anew, so we
8036 -- reset the entity in the generic copy. A global reference has a
8037 -- smaller depth than the parent, or else the same depth in case
8038 -- both are distinct compilation units.
8039
8040 -- A child unit is implicitly declared within the enclosing parent
8041 -- but is in fact global to it, and must be preserved.
8042
8043 -- It is also possible for Current_Instantiated_Parent to be
8044 -- defined, and for this not to be a nested generic, namely if
8045 -- the unit is loaded through Rtsfind. In that case, the entity of
8046 -- New_N is only a link to the associated node, and not a defining
8047 -- occurrence.
8048
8049 -- The entities for parent units in the defining_program_unit of a
8050 -- generic child unit are established when the context of the unit
8051 -- is first analyzed, before the generic copy is made. They are
8052 -- preserved in the copy for use in e.g. ASIS queries.
8053
8054 Ent := Entity (New_N);
8055
8056 if No (Current_Instantiated_Parent.Gen_Id) then
8057 if No (Ent)
8058 or else Nkind (Ent) /= N_Defining_Identifier
8059 or else not In_Defining_Unit_Name (N)
8060 then
8061 Set_Associated_Node (New_N, Empty);
8062 end if;
8063
8064 elsif No (Ent)
8065 or else Nkind (Ent) not in N_Entity
8066 or else No (Scope (Ent))
8067 or else
8068 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8069 and then not Is_Child_Unit (Ent))
8070 or else
8071 (Scope_Depth (Scope (Ent)) >
8072 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8073 and then
8074 Get_Source_Unit (Ent) =
8075 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8076 then
8077 Set_Associated_Node (New_N, Empty);
8078 end if;
8079
8080 -- Case of instantiating identifier or some other name or operator
8081
8082 else
8083 -- If the associated node is still defined, the entity in it
8084 -- is global, and must be copied to the instance. If this copy
8085 -- is being made for a body to inline, it is applied to an
8086 -- instantiated tree, and the entity is already present and
8087 -- must be also preserved.
8088
8089 declare
8090 Assoc : constant Node_Id := Get_Associated_Node (N);
8091
8092 begin
8093 if Present (Assoc) then
8094 if Nkind (Assoc) = Nkind (N) then
8095 Set_Entity (New_N, Entity (Assoc));
8096 Check_Private_View (N);
8097
8098 -- Here we deal with a very peculiar case for which the
8099 -- Has_Private_View mechanism is not sufficient, because
8100 -- the reference to the type is implicit in the tree,
8101 -- that is to say, it's not referenced from a node but
8102 -- only from another type, namely through Component_Type.
8103
8104 -- package P is
8105
8106 -- type Pt is private;
8107
8108 -- generic
8109 -- type Ft is array (Positive range <>) of Pt;
8110 -- package G is
8111 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8112 -- end G;
8113
8114 -- private
8115 -- type Pt is new Boolean;
8116 -- end P;
8117
8118 -- package body P is
8119 -- package body G is
8120 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8121 -- begin
8122 -- if (F1 < F2) /= Lt then
8123 -- null;
8124 -- end if;
8125 -- end Check;
8126 -- end G;
8127 -- end P;
8128
8129 -- type Arr is array (Positive range <>) of P.Pt;
8130
8131 -- package Inst is new P.G (Arr);
8132
8133 -- Pt is a global type for the generic package G and it
8134 -- is not referenced in its body, but only as component
8135 -- type of Ft, which is a local type. This means that no
8136 -- references to Pt or Ft are seen during the copy of the
8137 -- body, the only reference to Pt being seen is when the
8138 -- actuals are checked by Check_Generic_Actuals, but Pt
8139 -- is still private at this point. In the end, the views
8140 -- of Pt are not switched in the body and, therefore, the
8141 -- array comparison is rejected because the component is
8142 -- still private.
8143
8144 -- Adding e.g. a dummy variable of type Pt in the body is
8145 -- sufficient to make everything work, so we generate an
8146 -- artificial reference to Pt on the fly and thus force
8147 -- the switching of views on the grounds that, if the
8148 -- comparison was accepted during the semantic analysis
8149 -- of the generic, this means that the component cannot
8150 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8151
8152 if Nkind (Assoc) in N_Op_Compare
8153 and then Present (Etype (Left_Opnd (Assoc)))
8154 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8155 and then Present (Etype (Right_Opnd (Assoc)))
8156 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8157 then
8158 declare
8159 Ltyp : constant Entity_Id :=
8160 Etype (Left_Opnd (Assoc));
8161 Rtyp : constant Entity_Id :=
8162 Etype (Right_Opnd (Assoc));
8163 begin
8164 if Is_Private_Type (Component_Type (Ltyp)) then
8165 Check_Private_View
8166 (New_Occurrence_Of (Component_Type (Ltyp),
8167 Sloc (N)));
8168 end if;
8169 if Is_Private_Type (Component_Type (Rtyp)) then
8170 Check_Private_View
8171 (New_Occurrence_Of (Component_Type (Rtyp),
8172 Sloc (N)));
8173 end if;
8174 end;
8175
8176 -- Here is a similar case, for the Designated_Type of an
8177 -- access type that is present as target type in a type
8178 -- conversion from another access type. In this case, if
8179 -- the base types of the designated types are different
8180 -- and the conversion was accepted during the semantic
8181 -- analysis of the generic, this means that the target
8182 -- type cannot have been private (see Valid_Conversion).
8183
8184 elsif Nkind (Assoc) = N_Identifier
8185 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8186 and then Subtype_Mark (Parent (Assoc)) = Assoc
8187 and then Present (Etype (Assoc))
8188 and then Is_Access_Type (Etype (Assoc))
8189 and then Present (Etype (Expression (Parent (Assoc))))
8190 and then
8191 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8192 then
8193 declare
8194 Targ_Desig : constant Entity_Id :=
8195 Designated_Type (Etype (Assoc));
8196 Expr_Desig : constant Entity_Id :=
8197 Designated_Type
8198 (Etype (Expression (Parent (Assoc))));
8199 begin
8200 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8201 and then Is_Private_Type (Targ_Desig)
8202 then
8203 Check_Private_View
8204 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8205 end if;
8206 end;
8207 end if;
8208
8209 -- The node is a reference to a global type and acts as the
8210 -- subtype mark of a qualified expression created in order
8211 -- to aid resolution of accidental overloading in instances.
8212 -- Since N is a reference to a type, the Associated_Node of
8213 -- N denotes an entity rather than another identifier. See
8214 -- Qualify_Universal_Operands for details.
8215
8216 elsif Nkind (N) = N_Identifier
8217 and then Nkind (Parent (N)) = N_Qualified_Expression
8218 and then Subtype_Mark (Parent (N)) = N
8219 and then Is_Qualified_Universal_Literal (Parent (N))
8220 then
8221 Set_Entity (New_N, Assoc);
8222
8223 -- The name in the call may be a selected component if the
8224 -- call has not been analyzed yet, as may be the case for
8225 -- pre/post conditions in a generic unit.
8226
8227 elsif Nkind (Assoc) = N_Function_Call
8228 and then Is_Entity_Name (Name (Assoc))
8229 then
8230 Set_Entity (New_N, Entity (Name (Assoc)));
8231
8232 elsif Nkind (Assoc) in N_Entity
8233 and then Expander_Active
8234 then
8235 -- Inlining case: we are copying a tree that contains
8236 -- global entities, which are preserved in the copy to be
8237 -- used for subsequent inlining.
8238
8239 null;
8240
8241 else
8242 Set_Entity (New_N, Empty);
8243 end if;
8244 end if;
8245 end;
8246 end if;
8247
8248 -- For expanded name, we must copy the Prefix and Selector_Name
8249
8250 if Nkind (N) = N_Expanded_Name then
8251 Set_Prefix
8252 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8253
8254 Set_Selector_Name (New_N,
8255 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8256
8257 -- For operators, copy the operands
8258
8259 elsif Nkind (N) in N_Op then
8260 if Nkind (N) in N_Binary_Op then
8261 Set_Left_Opnd (New_N,
8262 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8263 end if;
8264
8265 Set_Right_Opnd (New_N,
8266 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8267 end if;
8268
8269 -- Establish a link between an entity from the generic template and the
8270 -- corresponding entity in the generic copy to be analyzed.
8271
8272 elsif Nkind (N) in N_Entity then
8273 if not Instantiating then
8274 Set_Associated_Entity (N, New_N);
8275 end if;
8276
8277 -- Clear any existing link the copy may inherit from the replicated
8278 -- generic template entity.
8279
8280 Set_Associated_Entity (New_N, Empty);
8281
8282 -- Special casing for stubs
8283
8284 elsif Nkind (N) in N_Body_Stub then
8285
8286 -- In any case, we must copy the specification or defining
8287 -- identifier as appropriate.
8288
8289 if Nkind (N) = N_Subprogram_Body_Stub then
8290 Set_Specification (New_N,
8291 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8292
8293 else
8294 Set_Defining_Identifier (New_N,
8295 Copy_Generic_Node
8296 (Defining_Identifier (N), New_N, Instantiating));
8297 end if;
8298
8299 -- If we are not instantiating, then this is where we load and
8300 -- analyze subunits, i.e. at the point where the stub occurs. A
8301 -- more permissive system might defer this analysis to the point
8302 -- of instantiation, but this seems too complicated for now.
8303
8304 if not Instantiating then
8305 declare
8306 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8307 Subunit : Node_Id;
8308 Unum : Unit_Number_Type;
8309 New_Body : Node_Id;
8310
8311 begin
8312 -- Make sure that, if it is a subunit of the main unit that is
8313 -- preprocessed and if -gnateG is specified, the preprocessed
8314 -- file will be written.
8315
8316 Lib.Analysing_Subunit_Of_Main :=
8317 Lib.In_Extended_Main_Source_Unit (N);
8318 Unum :=
8319 Load_Unit
8320 (Load_Name => Subunit_Name,
8321 Required => False,
8322 Subunit => True,
8323 Error_Node => N);
8324 Lib.Analysing_Subunit_Of_Main := False;
8325
8326 -- If the proper body is not found, a warning message will be
8327 -- emitted when analyzing the stub, or later at the point of
8328 -- instantiation. Here we just leave the stub as is.
8329
8330 if Unum = No_Unit then
8331 Subunits_Missing := True;
8332 goto Subunit_Not_Found;
8333 end if;
8334
8335 Subunit := Cunit (Unum);
8336
8337 if Nkind (Unit (Subunit)) /= N_Subunit then
8338 Error_Msg_N
8339 ("found child unit instead of expected SEPARATE subunit",
8340 Subunit);
8341 Error_Msg_Sloc := Sloc (N);
8342 Error_Msg_N ("\to complete stub #", Subunit);
8343 goto Subunit_Not_Found;
8344 end if;
8345
8346 -- We must create a generic copy of the subunit, in order to
8347 -- perform semantic analysis on it, and we must replace the
8348 -- stub in the original generic unit with the subunit, in order
8349 -- to preserve non-local references within.
8350
8351 -- Only the proper body needs to be copied. Library_Unit and
8352 -- context clause are simply inherited by the generic copy.
8353 -- Note that the copy (which may be recursive if there are
8354 -- nested subunits) must be done first, before attaching it to
8355 -- the enclosing generic.
8356
8357 New_Body :=
8358 Copy_Generic_Node
8359 (Proper_Body (Unit (Subunit)),
8360 Empty, Instantiating => False);
8361
8362 -- Now place the original proper body in the original generic
8363 -- unit. This is a body, not a compilation unit.
8364
8365 Rewrite (N, Proper_Body (Unit (Subunit)));
8366 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8367 Set_Was_Originally_Stub (N);
8368
8369 -- Finally replace the body of the subunit with its copy, and
8370 -- make this new subunit into the library unit of the generic
8371 -- copy, which does not have stubs any longer.
8372
8373 Set_Proper_Body (Unit (Subunit), New_Body);
8374 Set_Library_Unit (New_N, Subunit);
8375 Inherit_Context (Unit (Subunit), N);
8376 end;
8377
8378 -- If we are instantiating, this must be an error case, since
8379 -- otherwise we would have replaced the stub node by the proper body
8380 -- that corresponds. So just ignore it in the copy (i.e. we have
8381 -- copied it, and that is good enough).
8382
8383 else
8384 null;
8385 end if;
8386
8387 <<Subunit_Not_Found>> null;
8388
8389 -- If the node is a compilation unit, it is the subunit of a stub, which
8390 -- has been loaded already (see code below). In this case, the library
8391 -- unit field of N points to the parent unit (which is a compilation
8392 -- unit) and need not (and cannot) be copied.
8393
8394 -- When the proper body of the stub is analyzed, the library_unit link
8395 -- is used to establish the proper context (see sem_ch10).
8396
8397 -- The other fields of a compilation unit are copied as usual
8398
8399 elsif Nkind (N) = N_Compilation_Unit then
8400
8401 -- This code can only be executed when not instantiating, because in
8402 -- the copy made for an instantiation, the compilation unit node has
8403 -- disappeared at the point that a stub is replaced by its proper
8404 -- body.
8405
8406 pragma Assert (not Instantiating);
8407
8408 Set_Context_Items (New_N,
8409 Copy_Generic_List (Context_Items (N), New_N));
8410
8411 Set_Unit (New_N,
8412 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8413
8414 Set_First_Inlined_Subprogram (New_N,
8415 Copy_Generic_Node
8416 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8417
8418 Set_Aux_Decls_Node
8419 (New_N,
8420 Copy_Generic_Node
8421 (Aux_Decls_Node (N), New_N, Instantiating => False));
8422
8423 -- For an assignment node, the assignment is known to be semantically
8424 -- legal if we are instantiating the template. This avoids incorrect
8425 -- diagnostics in generated code.
8426
8427 elsif Nkind (N) = N_Assignment_Statement then
8428
8429 -- Copy name and expression fields in usual manner
8430
8431 Set_Name (New_N,
8432 Copy_Generic_Node (Name (N), New_N, Instantiating));
8433
8434 Set_Expression (New_N,
8435 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8436
8437 if Instantiating then
8438 Set_Assignment_OK (Name (New_N), True);
8439 end if;
8440
8441 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8442 if not Instantiating then
8443 Set_Associated_Node (N, New_N);
8444
8445 else
8446 if Present (Get_Associated_Node (N))
8447 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8448 then
8449 -- In the generic the aggregate has some composite type. If at
8450 -- the point of instantiation the type has a private view,
8451 -- install the full view (and that of its ancestors, if any).
8452
8453 declare
8454 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8455 Rt : Entity_Id;
8456
8457 begin
8458 if Present (T) and then Is_Private_Type (T) then
8459 Switch_View (T);
8460 end if;
8461
8462 if Present (T)
8463 and then Is_Tagged_Type (T)
8464 and then Is_Derived_Type (T)
8465 then
8466 Rt := Root_Type (T);
8467
8468 loop
8469 T := Etype (T);
8470
8471 if Is_Private_Type (T) then
8472 Switch_View (T);
8473 end if;
8474
8475 exit when T = Rt;
8476 end loop;
8477 end if;
8478 end;
8479 end if;
8480 end if;
8481
8482 -- Do not copy the associated node, which points to the generic copy
8483 -- of the aggregate.
8484
8485 declare
8486 use Atree.Unchecked_Access;
8487 -- This code section is part of the implementation of an untyped
8488 -- tree traversal, so it needs direct access to node fields.
8489
8490 begin
8491 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8492 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8493 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8494 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8495 end;
8496
8497 -- Allocators do not have an identifier denoting the access type, so we
8498 -- must locate it through the expression to check whether the views are
8499 -- consistent.
8500
8501 elsif Nkind (N) = N_Allocator
8502 and then Nkind (Expression (N)) = N_Qualified_Expression
8503 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8504 and then Instantiating
8505 then
8506 declare
8507 T : constant Node_Id :=
8508 Get_Associated_Node (Subtype_Mark (Expression (N)));
8509 Acc_T : Entity_Id;
8510
8511 begin
8512 if Present (T) then
8513
8514 -- Retrieve the allocator node in the generic copy
8515
8516 Acc_T := Etype (Parent (Parent (T)));
8517
8518 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8519 Switch_View (Acc_T);
8520 end if;
8521 end if;
8522
8523 Copy_Descendants;
8524 end;
8525
8526 -- For a proper body, we must catch the case of a proper body that
8527 -- replaces a stub. This represents the point at which a separate
8528 -- compilation unit, and hence template file, may be referenced, so we
8529 -- must make a new source instantiation entry for the template of the
8530 -- subunit, and ensure that all nodes in the subunit are adjusted using
8531 -- this new source instantiation entry.
8532
8533 elsif Nkind (N) in N_Proper_Body then
8534 declare
8535 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8536 begin
8537 if Instantiating and then Was_Originally_Stub (N) then
8538 Create_Instantiation_Source
8539 (Instantiation_Node,
8540 Defining_Entity (N),
8541 S_Adjustment);
8542
8543 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8544 end if;
8545
8546 -- Now copy the fields of the proper body, using the new
8547 -- adjustment factor if one was needed as per test above.
8548
8549 Copy_Descendants;
8550
8551 -- Restore the original adjustment factor
8552
8553 S_Adjustment := Save_Adjustment;
8554 end;
8555
8556 elsif Nkind (N) = N_Pragma and then Instantiating then
8557
8558 -- Do not copy Comment or Ident pragmas their content is relevant to
8559 -- the generic unit, not to the instantiating unit.
8560
8561 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8562 New_N := Make_Null_Statement (Sloc (N));
8563
8564 -- Do not copy pragmas generated from aspects because the pragmas do
8565 -- not carry any semantic information, plus they will be regenerated
8566 -- in the instance.
8567
8568 -- However, generating C we need to copy them since postconditions
8569 -- are inlined by the front end, and the front-end inlining machinery
8570 -- relies on this routine to perform inlining.
8571
8572 elsif From_Aspect_Specification (N)
8573 and then not Modify_Tree_For_C
8574 then
8575 New_N := Make_Null_Statement (Sloc (N));
8576
8577 else
8578 Copy_Descendants;
8579 end if;
8580
8581 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8582
8583 -- No descendant fields need traversing
8584
8585 null;
8586
8587 elsif Nkind (N) = N_String_Literal
8588 and then Present (Etype (N))
8589 and then Instantiating
8590 then
8591 -- If the string is declared in an outer scope, the string_literal
8592 -- subtype created for it may have the wrong scope. Force reanalysis
8593 -- of the constant to generate a new itype in the proper context.
8594
8595 Set_Etype (New_N, Empty);
8596 Set_Analyzed (New_N, False);
8597
8598 -- For the remaining nodes, copy their descendants recursively
8599
8600 else
8601 Copy_Descendants;
8602
8603 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8604 Set_Generic_Parent (Specification (New_N), N);
8605
8606 -- Should preserve Corresponding_Spec??? (12.3(14))
8607 end if;
8608 end if;
8609
8610 -- Propagate dimensions if present, so that they are reflected in the
8611 -- instance.
8612
8613 if Nkind (N) in N_Has_Etype
8614 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8615 and then Present (Etype (N))
8616 and then Is_Floating_Point_Type (Etype (N))
8617 and then Has_Dimension_System (Etype (N))
8618 then
8619 Copy_Dimensions (N, New_N);
8620 end if;
8621
8622 return New_N;
8623 end Copy_Generic_Node;
8624
8625 ----------------------------
8626 -- Denotes_Formal_Package --
8627 ----------------------------
8628
8629 function Denotes_Formal_Package
8630 (Pack : Entity_Id;
8631 On_Exit : Boolean := False;
8632 Instance : Entity_Id := Empty) return Boolean
8633 is
8634 Par : Entity_Id;
8635 Scop : constant Entity_Id := Scope (Pack);
8636 E : Entity_Id;
8637
8638 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8639 -- The package in question may be an actual for a previous formal
8640 -- package P of the current instance, so examine its actuals as well.
8641 -- This must be recursive over other formal packages.
8642
8643 ----------------------------------
8644 -- Is_Actual_Of_Previous_Formal --
8645 ----------------------------------
8646
8647 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8648 E1 : Entity_Id;
8649
8650 begin
8651 E1 := First_Entity (P);
8652 while Present (E1) and then E1 /= Instance loop
8653 if Ekind (E1) = E_Package
8654 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8655 then
8656 if Renamed_Object (E1) = Pack then
8657 return True;
8658
8659 elsif E1 = P or else Renamed_Object (E1) = P then
8660 return False;
8661
8662 elsif Is_Actual_Of_Previous_Formal (E1) then
8663 return True;
8664 end if;
8665 end if;
8666
8667 Next_Entity (E1);
8668 end loop;
8669
8670 return False;
8671 end Is_Actual_Of_Previous_Formal;
8672
8673 -- Start of processing for Denotes_Formal_Package
8674
8675 begin
8676 if On_Exit then
8677 Par :=
8678 Instance_Envs.Table
8679 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8680 else
8681 Par := Current_Instantiated_Parent.Act_Id;
8682 end if;
8683
8684 if Ekind (Scop) = E_Generic_Package
8685 or else Nkind (Unit_Declaration_Node (Scop)) =
8686 N_Generic_Subprogram_Declaration
8687 then
8688 return True;
8689
8690 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8691 N_Formal_Package_Declaration
8692 then
8693 return True;
8694
8695 elsif No (Par) then
8696 return False;
8697
8698 else
8699 -- Check whether this package is associated with a formal package of
8700 -- the enclosing instantiation. Iterate over the list of renamings.
8701
8702 E := First_Entity (Par);
8703 while Present (E) loop
8704 if Ekind (E) /= E_Package
8705 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8706 then
8707 null;
8708
8709 elsif Renamed_Object (E) = Par then
8710 return False;
8711
8712 elsif Renamed_Object (E) = Pack then
8713 return True;
8714
8715 elsif Is_Actual_Of_Previous_Formal (E) then
8716 return True;
8717
8718 end if;
8719
8720 Next_Entity (E);
8721 end loop;
8722
8723 return False;
8724 end if;
8725 end Denotes_Formal_Package;
8726
8727 -----------------
8728 -- End_Generic --
8729 -----------------
8730
8731 procedure End_Generic is
8732 begin
8733 -- ??? More things could be factored out in this routine. Should
8734 -- probably be done at a later stage.
8735
8736 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8737 Generic_Flags.Decrement_Last;
8738
8739 Expander_Mode_Restore;
8740 end End_Generic;
8741
8742 -------------
8743 -- Earlier --
8744 -------------
8745
8746 function Earlier (N1, N2 : Node_Id) return Boolean is
8747 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8748 -- Find distance from given node to enclosing compilation unit
8749
8750 ----------------
8751 -- Find_Depth --
8752 ----------------
8753
8754 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8755 begin
8756 while Present (P)
8757 and then Nkind (P) /= N_Compilation_Unit
8758 loop
8759 P := True_Parent (P);
8760 D := D + 1;
8761 end loop;
8762 end Find_Depth;
8763
8764 -- Local declarations
8765
8766 D1 : Integer := 0;
8767 D2 : Integer := 0;
8768 P1 : Node_Id := N1;
8769 P2 : Node_Id := N2;
8770 T1 : Source_Ptr;
8771 T2 : Source_Ptr;
8772
8773 -- Start of processing for Earlier
8774
8775 begin
8776 Find_Depth (P1, D1);
8777 Find_Depth (P2, D2);
8778
8779 if P1 /= P2 then
8780 return False;
8781 else
8782 P1 := N1;
8783 P2 := N2;
8784 end if;
8785
8786 while D1 > D2 loop
8787 P1 := True_Parent (P1);
8788 D1 := D1 - 1;
8789 end loop;
8790
8791 while D2 > D1 loop
8792 P2 := True_Parent (P2);
8793 D2 := D2 - 1;
8794 end loop;
8795
8796 -- At this point P1 and P2 are at the same distance from the root.
8797 -- We examine their parents until we find a common declarative list.
8798 -- If we reach the root, N1 and N2 do not descend from the same
8799 -- declarative list (e.g. one is nested in the declarative part and
8800 -- the other is in a block in the statement part) and the earlier
8801 -- one is already frozen.
8802
8803 while not Is_List_Member (P1)
8804 or else not Is_List_Member (P2)
8805 or else not In_Same_List (P1, P2)
8806 loop
8807 P1 := True_Parent (P1);
8808 P2 := True_Parent (P2);
8809
8810 if Nkind (Parent (P1)) = N_Subunit then
8811 P1 := Corresponding_Stub (Parent (P1));
8812 end if;
8813
8814 if Nkind (Parent (P2)) = N_Subunit then
8815 P2 := Corresponding_Stub (Parent (P2));
8816 end if;
8817
8818 if P1 = P2 then
8819 return False;
8820 end if;
8821 end loop;
8822
8823 -- Expanded code usually shares the source location of the original
8824 -- construct it was generated for. This however may not necessarily
8825 -- reflect the true location of the code within the tree.
8826
8827 -- Before comparing the slocs of the two nodes, make sure that we are
8828 -- working with correct source locations. Assume that P1 is to the left
8829 -- of P2. If either one does not come from source, traverse the common
8830 -- list heading towards the other node and locate the first source
8831 -- statement.
8832
8833 -- P1 P2
8834 -- ----+===+===+--------------+===+===+----
8835 -- expanded code expanded code
8836
8837 if not Comes_From_Source (P1) then
8838 while Present (P1) loop
8839
8840 -- Neither P2 nor a source statement were located during the
8841 -- search. If we reach the end of the list, then P1 does not
8842 -- occur earlier than P2.
8843
8844 -- ---->
8845 -- start --- P2 ----- P1 --- end
8846
8847 if No (Next (P1)) then
8848 return False;
8849
8850 -- We encounter P2 while going to the right of the list. This
8851 -- means that P1 does indeed appear earlier.
8852
8853 -- ---->
8854 -- start --- P1 ===== P2 --- end
8855 -- expanded code in between
8856
8857 elsif P1 = P2 then
8858 return True;
8859
8860 -- No need to look any further since we have located a source
8861 -- statement.
8862
8863 elsif Comes_From_Source (P1) then
8864 exit;
8865 end if;
8866
8867 -- Keep going right
8868
8869 Next (P1);
8870 end loop;
8871 end if;
8872
8873 if not Comes_From_Source (P2) then
8874 while Present (P2) loop
8875
8876 -- Neither P1 nor a source statement were located during the
8877 -- search. If we reach the start of the list, then P1 does not
8878 -- occur earlier than P2.
8879
8880 -- <----
8881 -- start --- P2 --- P1 --- end
8882
8883 if No (Prev (P2)) then
8884 return False;
8885
8886 -- We encounter P1 while going to the left of the list. This
8887 -- means that P1 does indeed appear earlier.
8888
8889 -- <----
8890 -- start --- P1 ===== P2 --- end
8891 -- expanded code in between
8892
8893 elsif P2 = P1 then
8894 return True;
8895
8896 -- No need to look any further since we have located a source
8897 -- statement.
8898
8899 elsif Comes_From_Source (P2) then
8900 exit;
8901 end if;
8902
8903 -- Keep going left
8904
8905 Prev (P2);
8906 end loop;
8907 end if;
8908
8909 -- At this point either both nodes came from source or we approximated
8910 -- their source locations through neighboring source statements.
8911
8912 T1 := Top_Level_Location (Sloc (P1));
8913 T2 := Top_Level_Location (Sloc (P2));
8914
8915 -- When two nodes come from the same instance, they have identical top
8916 -- level locations. To determine proper relation within the tree, check
8917 -- their locations within the template.
8918
8919 if T1 = T2 then
8920 return Sloc (P1) < Sloc (P2);
8921
8922 -- The two nodes either come from unrelated instances or do not come
8923 -- from instantiated code at all.
8924
8925 else
8926 return T1 < T2;
8927 end if;
8928 end Earlier;
8929
8930 ----------------------
8931 -- Find_Actual_Type --
8932 ----------------------
8933
8934 function Find_Actual_Type
8935 (Typ : Entity_Id;
8936 Gen_Type : Entity_Id) return Entity_Id
8937 is
8938 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8939 T : Entity_Id;
8940
8941 begin
8942 -- Special processing only applies to child units
8943
8944 if not Is_Child_Unit (Gen_Scope) then
8945 return Get_Instance_Of (Typ);
8946
8947 -- If designated or component type is itself a formal of the child unit,
8948 -- its instance is available.
8949
8950 elsif Scope (Typ) = Gen_Scope then
8951 return Get_Instance_Of (Typ);
8952
8953 -- If the array or access type is not declared in the parent unit,
8954 -- no special processing needed.
8955
8956 elsif not Is_Generic_Type (Typ)
8957 and then Scope (Gen_Scope) /= Scope (Typ)
8958 then
8959 return Get_Instance_Of (Typ);
8960
8961 -- Otherwise, retrieve designated or component type by visibility
8962
8963 else
8964 T := Current_Entity (Typ);
8965 while Present (T) loop
8966 if In_Open_Scopes (Scope (T)) then
8967 return T;
8968 elsif Is_Generic_Actual_Type (T) then
8969 return T;
8970 end if;
8971
8972 T := Homonym (T);
8973 end loop;
8974
8975 return Typ;
8976 end if;
8977 end Find_Actual_Type;
8978
8979 ----------------------------
8980 -- Freeze_Subprogram_Body --
8981 ----------------------------
8982
8983 procedure Freeze_Subprogram_Body
8984 (Inst_Node : Node_Id;
8985 Gen_Body : Node_Id;
8986 Pack_Id : Entity_Id)
8987 is
8988 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8989 Par : constant Entity_Id := Scope (Gen_Unit);
8990 Enc_G : Entity_Id;
8991 Enc_G_F : Node_Id;
8992 Enc_I : Node_Id;
8993 F_Node : Node_Id;
8994
8995 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8996 -- Find innermost package body that encloses the given node, and which
8997 -- is not a compilation unit. Freeze nodes for the instance, or for its
8998 -- enclosing body, may be inserted after the enclosing_body of the
8999 -- generic unit. Used to determine proper placement of freeze node for
9000 -- both package and subprogram instances.
9001
9002 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9003 -- Find entity for given package body, and locate or create a freeze
9004 -- node for it.
9005
9006 ----------------------------
9007 -- Enclosing_Package_Body --
9008 ----------------------------
9009
9010 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9011 P : Node_Id;
9012
9013 begin
9014 P := Parent (N);
9015 while Present (P)
9016 and then Nkind (Parent (P)) /= N_Compilation_Unit
9017 loop
9018 if Nkind (P) = N_Package_Body then
9019 if Nkind (Parent (P)) = N_Subunit then
9020 return Corresponding_Stub (Parent (P));
9021 else
9022 return P;
9023 end if;
9024 end if;
9025
9026 P := True_Parent (P);
9027 end loop;
9028
9029 return Empty;
9030 end Enclosing_Package_Body;
9031
9032 -------------------------
9033 -- Package_Freeze_Node --
9034 -------------------------
9035
9036 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9037 Id : Entity_Id;
9038
9039 begin
9040 if Nkind (B) = N_Package_Body then
9041 Id := Corresponding_Spec (B);
9042 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9043 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9044 end if;
9045
9046 Ensure_Freeze_Node (Id);
9047 return Freeze_Node (Id);
9048 end Package_Freeze_Node;
9049
9050 -- Start of processing for Freeze_Subprogram_Body
9051
9052 begin
9053 -- If the instance and the generic body appear within the same unit, and
9054 -- the instance precedes the generic, the freeze node for the instance
9055 -- must appear after that of the generic. If the generic is nested
9056 -- within another instance I2, then current instance must be frozen
9057 -- after I2. In both cases, the freeze nodes are those of enclosing
9058 -- packages. Otherwise, the freeze node is placed at the end of the
9059 -- current declarative part.
9060
9061 Enc_G := Enclosing_Package_Body (Gen_Body);
9062 Enc_I := Enclosing_Package_Body (Inst_Node);
9063 Ensure_Freeze_Node (Pack_Id);
9064 F_Node := Freeze_Node (Pack_Id);
9065
9066 if Is_Generic_Instance (Par)
9067 and then Present (Freeze_Node (Par))
9068 and then In_Same_Declarative_Part
9069 (Parent (Freeze_Node (Par)), Inst_Node)
9070 then
9071 -- The parent was a premature instantiation. Insert freeze node at
9072 -- the end the current declarative part.
9073
9074 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
9075 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9076
9077 -- Handle the following case:
9078 --
9079 -- package Parent_Inst is new ...
9080 -- Parent_Inst []
9081 --
9082 -- procedure P ... -- this body freezes Parent_Inst
9083 --
9084 -- procedure Inst is new ...
9085 --
9086 -- In this particular scenario, the freeze node for Inst must be
9087 -- inserted in the same manner as that of Parent_Inst - before the
9088 -- next source body or at the end of the declarative list (body not
9089 -- available). If body P did not exist and Parent_Inst was frozen
9090 -- after Inst, either by a body following Inst or at the end of the
9091 -- declarative region, the freeze node for Inst must be inserted
9092 -- after that of Parent_Inst. This relation is established by
9093 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9094
9095 elsif In_Same_List (Get_Unit_Instantiation_Node (Par), Inst_Node)
9096 and then Sloc (Freeze_Node (Par)) <= Sloc (Inst_Node)
9097 then
9098 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9099
9100 else
9101 Insert_After (Freeze_Node (Par), F_Node);
9102 end if;
9103
9104 -- The body enclosing the instance should be frozen after the body that
9105 -- includes the generic, because the body of the instance may make
9106 -- references to entities therein. If the two are not in the same
9107 -- declarative part, or if the one enclosing the instance is frozen
9108 -- already, freeze the instance at the end of the current declarative
9109 -- part.
9110
9111 elsif Is_Generic_Instance (Par)
9112 and then Present (Freeze_Node (Par))
9113 and then Present (Enc_I)
9114 then
9115 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I) then
9116 -- The enclosing package may contain several instances. Rather
9117 -- than computing the earliest point at which to insert its freeze
9118 -- node, we place it at the end of the declarative part of the
9119 -- parent of the generic.
9120
9121 Insert_Freeze_Node_For_Instance
9122 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
9123 end if;
9124
9125 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9126
9127 elsif Present (Enc_G)
9128 and then Present (Enc_I)
9129 and then Enc_G /= Enc_I
9130 and then Earlier (Inst_Node, Gen_Body)
9131 then
9132 -- Freeze package that encloses instance, and place node after the
9133 -- package that encloses generic. If enclosing package is already
9134 -- frozen we have to assume it is at the proper place. This may be a
9135 -- potential ABE that requires dynamic checking. Do not add a freeze
9136 -- node if the package that encloses the generic is inside the body
9137 -- that encloses the instance, because the freeze node would be in
9138 -- the wrong scope. Additional contortions needed if the bodies are
9139 -- within a subunit.
9140
9141 declare
9142 Enclosing_Body : Node_Id;
9143
9144 begin
9145 if Nkind (Enc_I) = N_Package_Body_Stub then
9146 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
9147 else
9148 Enclosing_Body := Enc_I;
9149 end if;
9150
9151 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9152 Insert_Freeze_Node_For_Instance
9153 (Enc_G, Package_Freeze_Node (Enc_I));
9154 end if;
9155 end;
9156
9157 -- Freeze enclosing subunit before instance
9158
9159 Enc_G_F := Package_Freeze_Node (Enc_G);
9160
9161 if not Is_List_Member (Enc_G_F) then
9162 Insert_After (Enc_G, Enc_G_F);
9163 end if;
9164
9165 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9166
9167 else
9168 -- If none of the above, insert freeze node at the end of the current
9169 -- declarative part.
9170
9171 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9172 end if;
9173 end Freeze_Subprogram_Body;
9174
9175 ----------------
9176 -- Get_Gen_Id --
9177 ----------------
9178
9179 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9180 begin
9181 return Generic_Renamings.Table (E).Gen_Id;
9182 end Get_Gen_Id;
9183
9184 ---------------------
9185 -- Get_Instance_Of --
9186 ---------------------
9187
9188 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9189 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9190
9191 begin
9192 if Res /= Assoc_Null then
9193 return Generic_Renamings.Table (Res).Act_Id;
9194
9195 else
9196 -- On exit, entity is not instantiated: not a generic parameter, or
9197 -- else parameter of an inner generic unit.
9198
9199 return A;
9200 end if;
9201 end Get_Instance_Of;
9202
9203 ---------------------------------
9204 -- Get_Unit_Instantiation_Node --
9205 ---------------------------------
9206
9207 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9208 Decl : Node_Id := Unit_Declaration_Node (A);
9209 Inst : Node_Id;
9210
9211 begin
9212 -- If the Package_Instantiation attribute has been set on the package
9213 -- entity, then use it directly when it (or its Original_Node) refers
9214 -- to an N_Package_Instantiation node. In principle it should be
9215 -- possible to have this field set in all cases, which should be
9216 -- investigated, and would allow this function to be significantly
9217 -- simplified. ???
9218
9219 Inst := Package_Instantiation (A);
9220
9221 if Present (Inst) then
9222 if Nkind (Inst) = N_Package_Instantiation then
9223 return Inst;
9224
9225 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9226 return Original_Node (Inst);
9227 end if;
9228 end if;
9229
9230 -- If the instantiation is a compilation unit that does not need body
9231 -- then the instantiation node has been rewritten as a package
9232 -- declaration for the instance, and we return the original node.
9233
9234 -- If it is a compilation unit and the instance node has not been
9235 -- rewritten, then it is still the unit of the compilation. Finally, if
9236 -- a body is present, this is a parent of the main unit whose body has
9237 -- been compiled for inlining purposes, and the instantiation node has
9238 -- been rewritten with the instance body.
9239
9240 -- Otherwise the instantiation node appears after the declaration. If
9241 -- the entity is a formal package, the declaration may have been
9242 -- rewritten as a generic declaration (in the case of a formal with box)
9243 -- or left as a formal package declaration if it has actuals, and is
9244 -- found with a forward search.
9245
9246 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9247 if Nkind (Decl) = N_Package_Declaration
9248 and then Present (Corresponding_Body (Decl))
9249 then
9250 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9251 end if;
9252
9253 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9254 return Original_Node (Decl);
9255 else
9256 return Unit (Parent (Decl));
9257 end if;
9258
9259 elsif Nkind (Decl) = N_Package_Declaration
9260 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9261 then
9262 return Original_Node (Decl);
9263
9264 else
9265 Inst := Next (Decl);
9266 while Nkind (Inst) not in N_Formal_Package_Declaration
9267 | N_Function_Instantiation
9268 | N_Package_Instantiation
9269 | N_Procedure_Instantiation
9270 loop
9271 Next (Inst);
9272 end loop;
9273
9274 return Inst;
9275 end if;
9276 end Get_Unit_Instantiation_Node;
9277
9278 ------------------------
9279 -- Has_Been_Exchanged --
9280 ------------------------
9281
9282 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9283 Next : Elmt_Id;
9284
9285 begin
9286 Next := First_Elmt (Exchanged_Views);
9287 while Present (Next) loop
9288 if Full_View (Node (Next)) = E then
9289 return True;
9290 end if;
9291
9292 Next_Elmt (Next);
9293 end loop;
9294
9295 return False;
9296 end Has_Been_Exchanged;
9297
9298 -------------------
9299 -- Has_Contracts --
9300 -------------------
9301
9302 function Has_Contracts (Decl : Node_Id) return Boolean is
9303 A_List : constant List_Id := Aspect_Specifications (Decl);
9304 A_Spec : Node_Id;
9305 A_Id : Aspect_Id;
9306 begin
9307 if No (A_List) then
9308 return False;
9309 else
9310 A_Spec := First (A_List);
9311 while Present (A_Spec) loop
9312 A_Id := Get_Aspect_Id (A_Spec);
9313 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9314 return True;
9315 end if;
9316
9317 Next (A_Spec);
9318 end loop;
9319
9320 return False;
9321 end if;
9322 end Has_Contracts;
9323
9324 ----------
9325 -- Hash --
9326 ----------
9327
9328 function Hash (F : Entity_Id) return HTable_Range is
9329 begin
9330 return HTable_Range (F mod HTable_Size);
9331 end Hash;
9332
9333 ------------------------
9334 -- Hide_Current_Scope --
9335 ------------------------
9336
9337 procedure Hide_Current_Scope is
9338 C : constant Entity_Id := Current_Scope;
9339 E : Entity_Id;
9340
9341 begin
9342 Set_Is_Hidden_Open_Scope (C);
9343
9344 E := First_Entity (C);
9345 while Present (E) loop
9346 if Is_Immediately_Visible (E) then
9347 Set_Is_Immediately_Visible (E, False);
9348 Append_Elmt (E, Hidden_Entities);
9349 end if;
9350
9351 Next_Entity (E);
9352 end loop;
9353
9354 -- Make the scope name invisible as well. This is necessary, but might
9355 -- conflict with calls to Rtsfind later on, in case the scope is a
9356 -- predefined one. There is no clean solution to this problem, so for
9357 -- now we depend on the user not redefining Standard itself in one of
9358 -- the parent units.
9359
9360 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9361 Set_Is_Immediately_Visible (C, False);
9362 Append_Elmt (C, Hidden_Entities);
9363 end if;
9364
9365 end Hide_Current_Scope;
9366
9367 --------------
9368 -- Init_Env --
9369 --------------
9370
9371 procedure Init_Env is
9372 Saved : Instance_Env;
9373
9374 begin
9375 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9376 Saved.Exchanged_Views := Exchanged_Views;
9377 Saved.Hidden_Entities := Hidden_Entities;
9378 Saved.Current_Sem_Unit := Current_Sem_Unit;
9379 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9380 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9381
9382 -- Save configuration switches. These may be reset if the unit is a
9383 -- predefined unit, and the current mode is not Ada 2005.
9384
9385 Saved.Switches := Save_Config_Switches;
9386
9387 Instance_Envs.Append (Saved);
9388
9389 Exchanged_Views := New_Elmt_List;
9390 Hidden_Entities := New_Elmt_List;
9391
9392 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9393 -- this is set properly in Set_Instance_Env.
9394
9395 Current_Instantiated_Parent :=
9396 (Current_Scope, Current_Scope, Assoc_Null);
9397 end Init_Env;
9398
9399 ---------------------
9400 -- In_Main_Context --
9401 ---------------------
9402
9403 function In_Main_Context (E : Entity_Id) return Boolean is
9404 Context : List_Id;
9405 Clause : Node_Id;
9406 Nam : Node_Id;
9407
9408 begin
9409 if not Is_Compilation_Unit (E)
9410 or else Ekind (E) /= E_Package
9411 or else In_Private_Part (E)
9412 then
9413 return False;
9414 end if;
9415
9416 Context := Context_Items (Cunit (Main_Unit));
9417
9418 Clause := First (Context);
9419 while Present (Clause) loop
9420 if Nkind (Clause) = N_With_Clause then
9421 Nam := Name (Clause);
9422
9423 -- If the current scope is part of the context of the main unit,
9424 -- analysis of the corresponding with_clause is not complete, and
9425 -- the entity is not set. We use the Chars field directly, which
9426 -- might produce false positives in rare cases, but guarantees
9427 -- that we produce all the instance bodies we will need.
9428
9429 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9430 or else (Nkind (Nam) = N_Selected_Component
9431 and then Chars (Selector_Name (Nam)) = Chars (E))
9432 then
9433 return True;
9434 end if;
9435 end if;
9436
9437 Next (Clause);
9438 end loop;
9439
9440 return False;
9441 end In_Main_Context;
9442
9443 ---------------------
9444 -- Inherit_Context --
9445 ---------------------
9446
9447 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9448 Current_Context : List_Id;
9449 Current_Unit : Node_Id;
9450 Item : Node_Id;
9451 New_I : Node_Id;
9452
9453 Clause : Node_Id;
9454 OK : Boolean;
9455 Lib_Unit : Node_Id;
9456
9457 begin
9458 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9459
9460 -- The inherited context is attached to the enclosing compilation
9461 -- unit. This is either the main unit, or the declaration for the
9462 -- main unit (in case the instantiation appears within the package
9463 -- declaration and the main unit is its body).
9464
9465 Current_Unit := Parent (Inst);
9466 while Present (Current_Unit)
9467 and then Nkind (Current_Unit) /= N_Compilation_Unit
9468 loop
9469 Current_Unit := Parent (Current_Unit);
9470 end loop;
9471
9472 Current_Context := Context_Items (Current_Unit);
9473
9474 Item := First (Context_Items (Parent (Gen_Decl)));
9475 while Present (Item) loop
9476 if Nkind (Item) = N_With_Clause then
9477 Lib_Unit := Library_Unit (Item);
9478
9479 -- Take care to prevent direct cyclic with's
9480
9481 if Lib_Unit /= Current_Unit then
9482
9483 -- Do not add a unit if it is already in the context
9484
9485 Clause := First (Current_Context);
9486 OK := True;
9487 while Present (Clause) loop
9488 if Nkind (Clause) = N_With_Clause
9489 and then Library_Unit (Clause) = Lib_Unit
9490 then
9491 OK := False;
9492 exit;
9493 end if;
9494
9495 Next (Clause);
9496 end loop;
9497
9498 if OK then
9499 New_I := New_Copy (Item);
9500 Set_Implicit_With (New_I);
9501
9502 Append (New_I, Current_Context);
9503 end if;
9504 end if;
9505 end if;
9506
9507 Next (Item);
9508 end loop;
9509 end if;
9510 end Inherit_Context;
9511
9512 ----------------
9513 -- Initialize --
9514 ----------------
9515
9516 procedure Initialize is
9517 begin
9518 Generic_Renamings.Init;
9519 Instance_Envs.Init;
9520 Generic_Flags.Init;
9521 Generic_Renamings_HTable.Reset;
9522 Circularity_Detected := False;
9523 Exchanged_Views := No_Elist;
9524 Hidden_Entities := No_Elist;
9525 end Initialize;
9526
9527 -------------------------------------
9528 -- Insert_Freeze_Node_For_Instance --
9529 -------------------------------------
9530
9531 procedure Insert_Freeze_Node_For_Instance
9532 (N : Node_Id;
9533 F_Node : Node_Id)
9534 is
9535 Decl : Node_Id;
9536 Decls : List_Id;
9537 Inst : Entity_Id;
9538 Par_N : Node_Id;
9539
9540 function Enclosing_Body (N : Node_Id) return Node_Id;
9541 -- Find enclosing package or subprogram body, if any. Freeze node may
9542 -- be placed at end of current declarative list if previous instance
9543 -- and current one have different enclosing bodies.
9544
9545 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9546 -- Find the local instance, if any, that declares the generic that is
9547 -- being instantiated. If present, the freeze node for this instance
9548 -- must follow the freeze node for the previous instance.
9549
9550 --------------------
9551 -- Enclosing_Body --
9552 --------------------
9553
9554 function Enclosing_Body (N : Node_Id) return Node_Id is
9555 P : Node_Id;
9556
9557 begin
9558 P := Parent (N);
9559 while Present (P)
9560 and then Nkind (Parent (P)) /= N_Compilation_Unit
9561 loop
9562 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9563 if Nkind (Parent (P)) = N_Subunit then
9564 return Corresponding_Stub (Parent (P));
9565 else
9566 return P;
9567 end if;
9568 end if;
9569
9570 P := True_Parent (P);
9571 end loop;
9572
9573 return Empty;
9574 end Enclosing_Body;
9575
9576 -----------------------
9577 -- Previous_Instance --
9578 -----------------------
9579
9580 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9581 S : Entity_Id;
9582
9583 begin
9584 S := Scope (Gen);
9585 while Present (S) and then S /= Standard_Standard loop
9586 if Is_Generic_Instance (S)
9587 and then In_Same_Source_Unit (S, N)
9588 then
9589 return S;
9590 end if;
9591
9592 S := Scope (S);
9593 end loop;
9594
9595 return Empty;
9596 end Previous_Instance;
9597
9598 -- Start of processing for Insert_Freeze_Node_For_Instance
9599
9600 begin
9601 if not Is_List_Member (F_Node) then
9602 Decl := N;
9603 Decls := List_Containing (N);
9604 Inst := Entity (F_Node);
9605 Par_N := Parent (Decls);
9606
9607 -- When processing a subprogram instantiation, utilize the actual
9608 -- subprogram instantiation rather than its package wrapper as it
9609 -- carries all the context information.
9610
9611 if Is_Wrapper_Package (Inst) then
9612 Inst := Related_Instance (Inst);
9613 end if;
9614
9615 -- If this is a package instance, check whether the generic is
9616 -- declared in a previous instance and the current instance is
9617 -- not within the previous one.
9618
9619 if Present (Generic_Parent (Parent (Inst)))
9620 and then Is_In_Main_Unit (N)
9621 then
9622 declare
9623 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9624 Par_I : constant Entity_Id :=
9625 Previous_Instance
9626 (Generic_Parent (Parent (Inst)));
9627 Scop : Entity_Id;
9628
9629 begin
9630 if Present (Par_I)
9631 and then Earlier (N, Freeze_Node (Par_I))
9632 then
9633 Scop := Scope (Inst);
9634
9635 -- If the current instance is within the one that contains
9636 -- the generic, the freeze node for the current one must
9637 -- appear in the current declarative part. Ditto, if the
9638 -- current instance is within another package instance or
9639 -- within a body that does not enclose the current instance.
9640 -- In these three cases the freeze node of the previous
9641 -- instance is not relevant.
9642
9643 while Present (Scop) and then Scop /= Standard_Standard loop
9644 exit when Scop = Par_I
9645 or else
9646 (Is_Generic_Instance (Scop)
9647 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9648 Scop := Scope (Scop);
9649 end loop;
9650
9651 -- Previous instance encloses current instance
9652
9653 if Scop = Par_I then
9654 null;
9655
9656 -- If the next node is a source body we must freeze in
9657 -- the current scope as well.
9658
9659 elsif Present (Next (N))
9660 and then Nkind (Next (N)) in N_Subprogram_Body
9661 | N_Package_Body
9662 and then Comes_From_Source (Next (N))
9663 then
9664 null;
9665
9666 -- Current instance is within an unrelated instance
9667
9668 elsif Is_Generic_Instance (Scop) then
9669 null;
9670
9671 -- Current instance is within an unrelated body
9672
9673 elsif Present (Enclosing_N)
9674 and then Enclosing_N /= Enclosing_Body (Par_I)
9675 then
9676 null;
9677
9678 else
9679 Insert_After (Freeze_Node (Par_I), F_Node);
9680 return;
9681 end if;
9682 end if;
9683 end;
9684 end if;
9685
9686 -- When the instantiation occurs in a package declaration, append the
9687 -- freeze node to the private declarations (if any).
9688
9689 if Nkind (Par_N) = N_Package_Specification
9690 and then Decls = Visible_Declarations (Par_N)
9691 and then Present (Private_Declarations (Par_N))
9692 and then not Is_Empty_List (Private_Declarations (Par_N))
9693 then
9694 Decls := Private_Declarations (Par_N);
9695 Decl := First (Decls);
9696 end if;
9697
9698 -- Determine the proper freeze point of a package instantiation. We
9699 -- adhere to the general rule of a package or subprogram body causing
9700 -- freezing of anything before it in the same declarative region. In
9701 -- this case, the proper freeze point of a package instantiation is
9702 -- before the first source body which follows, or before a stub. This
9703 -- ensures that entities coming from the instance are already frozen
9704 -- and usable in source bodies.
9705
9706 if Nkind (Par_N) /= N_Package_Declaration
9707 and then Ekind (Inst) = E_Package
9708 and then Is_Generic_Instance (Inst)
9709 and then
9710 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9711 then
9712 while Present (Decl) loop
9713 if (Nkind (Decl) in N_Unit_Body
9714 or else
9715 Nkind (Decl) in N_Body_Stub)
9716 and then Comes_From_Source (Decl)
9717 then
9718 Insert_Before (Decl, F_Node);
9719 return;
9720 end if;
9721
9722 Next (Decl);
9723 end loop;
9724 end if;
9725
9726 -- In a package declaration, or if no previous body, insert at end
9727 -- of list.
9728
9729 Set_Sloc (F_Node, Sloc (Last (Decls)));
9730 Insert_After (Last (Decls), F_Node);
9731 end if;
9732 end Insert_Freeze_Node_For_Instance;
9733
9734 ------------------
9735 -- Install_Body --
9736 ------------------
9737
9738 procedure Install_Body
9739 (Act_Body : Node_Id;
9740 N : Node_Id;
9741 Gen_Body : Node_Id;
9742 Gen_Decl : Node_Id)
9743 is
9744 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9745 -- Check if the generic definition and the instantiation come from
9746 -- a common scope, in which case the instance must be frozen after
9747 -- the generic body.
9748
9749 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9750 -- If the instance is nested inside a generic unit, the Sloc of the
9751 -- instance indicates the place of the original definition, not the
9752 -- point of the current enclosing instance. Pending a better usage of
9753 -- Slocs to indicate instantiation places, we determine the place of
9754 -- origin of a node by finding the maximum sloc of any ancestor node.
9755 -- Why is this not equivalent to Top_Level_Location ???
9756
9757 -------------------
9758 -- In_Same_Scope --
9759 -------------------
9760
9761 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9762 Act_Scop : Entity_Id := Scope (Act_Id);
9763 Gen_Scop : Entity_Id := Scope (Gen_Id);
9764
9765 begin
9766 while Act_Scop /= Standard_Standard
9767 and then Gen_Scop /= Standard_Standard
9768 loop
9769 if Act_Scop = Gen_Scop then
9770 return True;
9771 end if;
9772
9773 Act_Scop := Scope (Act_Scop);
9774 Gen_Scop := Scope (Gen_Scop);
9775 end loop;
9776
9777 return False;
9778 end In_Same_Scope;
9779
9780 ---------------
9781 -- True_Sloc --
9782 ---------------
9783
9784 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9785 N1 : Node_Id;
9786 Res : Source_Ptr;
9787
9788 begin
9789 Res := Sloc (N);
9790 N1 := N;
9791 while Present (N1) and then N1 /= Act_Unit loop
9792 if Sloc (N1) > Res then
9793 Res := Sloc (N1);
9794 end if;
9795
9796 N1 := Parent (N1);
9797 end loop;
9798
9799 return Res;
9800 end True_Sloc;
9801
9802 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9803 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9804 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9805 Par : constant Entity_Id := Scope (Gen_Id);
9806 Gen_Unit : constant Node_Id :=
9807 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9808
9809 Body_Unit : Node_Id;
9810 F_Node : Node_Id;
9811 Must_Delay : Boolean;
9812 Orig_Body : Node_Id := Gen_Body;
9813
9814 -- Start of processing for Install_Body
9815
9816 begin
9817 -- Handle first the case of an instance with incomplete actual types.
9818 -- The instance body cannot be placed after the declaration because
9819 -- full views have not been seen yet. Any use of the non-limited views
9820 -- in the instance body requires the presence of a regular with_clause
9821 -- in the enclosing unit, and will fail if this with_clause is missing.
9822 -- We place the instance body at the beginning of the enclosing body,
9823 -- which is the unit being compiled. The freeze node for the instance
9824 -- is then placed after the instance body.
9825
9826 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9827 and then Expander_Active
9828 and then Ekind (Scope (Act_Id)) = E_Package
9829 then
9830 declare
9831 Scop : constant Entity_Id := Scope (Act_Id);
9832 Body_Id : constant Node_Id :=
9833 Corresponding_Body (Unit_Declaration_Node (Scop));
9834
9835 begin
9836 Ensure_Freeze_Node (Act_Id);
9837 F_Node := Freeze_Node (Act_Id);
9838 if Present (Body_Id) then
9839 Set_Is_Frozen (Act_Id, False);
9840 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9841 if Is_List_Member (F_Node) then
9842 Remove (F_Node);
9843 end if;
9844
9845 Insert_After (Act_Body, F_Node);
9846 end if;
9847 end;
9848 return;
9849 end if;
9850
9851 -- If the body is a subunit, the freeze point is the corresponding stub
9852 -- in the current compilation, not the subunit itself.
9853
9854 if Nkind (Parent (Gen_Body)) = N_Subunit then
9855 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9856 else
9857 Orig_Body := Gen_Body;
9858 end if;
9859
9860 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9861
9862 -- If the instantiation and the generic definition appear in the same
9863 -- package declaration, this is an early instantiation. If they appear
9864 -- in the same declarative part, it is an early instantiation only if
9865 -- the generic body appears textually later, and the generic body is
9866 -- also in the main unit.
9867
9868 -- If instance is nested within a subprogram, and the generic body
9869 -- is not, the instance is delayed because the enclosing body is. If
9870 -- instance and body are within the same scope, or the same subprogram
9871 -- body, indicate explicitly that the instance is delayed.
9872
9873 Must_Delay :=
9874 (Gen_Unit = Act_Unit
9875 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9876 | N_Package_Declaration
9877 or else (Gen_Unit = Body_Unit
9878 and then True_Sloc (N, Act_Unit) <
9879 Sloc (Orig_Body)))
9880 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9881 and then In_Same_Scope (Gen_Id, Act_Id));
9882
9883 -- If this is an early instantiation, the freeze node is placed after
9884 -- the generic body. Otherwise, if the generic appears in an instance,
9885 -- we cannot freeze the current instance until the outer one is frozen.
9886 -- This is only relevant if the current instance is nested within some
9887 -- inner scope not itself within the outer instance. If this scope is
9888 -- a package body in the same declarative part as the outer instance,
9889 -- then that body needs to be frozen after the outer instance. Finally,
9890 -- if no delay is needed, we place the freeze node at the end of the
9891 -- current declarative part.
9892
9893 if Expander_Active
9894 and then (No (Freeze_Node (Act_Id))
9895 or else not Is_List_Member (Freeze_Node (Act_Id)))
9896 then
9897 Ensure_Freeze_Node (Act_Id);
9898 F_Node := Freeze_Node (Act_Id);
9899
9900 if Must_Delay then
9901 Insert_After (Orig_Body, F_Node);
9902
9903 elsif Is_Generic_Instance (Par)
9904 and then Present (Freeze_Node (Par))
9905 and then Scope (Act_Id) /= Par
9906 then
9907 -- Freeze instance of inner generic after instance of enclosing
9908 -- generic.
9909
9910 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9911
9912 -- Handle the following case:
9913
9914 -- package Parent_Inst is new ...
9915 -- Parent_Inst []
9916
9917 -- procedure P ... -- this body freezes Parent_Inst
9918
9919 -- package Inst is new ...
9920
9921 -- In this particular scenario, the freeze node for Inst must
9922 -- be inserted in the same manner as that of Parent_Inst,
9923 -- before the next source body or at the end of the declarative
9924 -- list (body not available). If body P did not exist and
9925 -- Parent_Inst was frozen after Inst, either by a body
9926 -- following Inst or at the end of the declarative region,
9927 -- the freeze node for Inst must be inserted after that of
9928 -- Parent_Inst. This relation is established by comparing
9929 -- the Slocs of Parent_Inst freeze node and Inst.
9930 -- We examine the parents of the enclosing lists to handle
9931 -- the case where the parent instance is in the visible part
9932 -- of a package declaration, and the inner instance is in
9933 -- the corresponding private part.
9934
9935 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9936 = Parent (List_Containing (N))
9937 and then Sloc (Freeze_Node (Par)) <= Sloc (N)
9938 then
9939 Insert_Freeze_Node_For_Instance (N, F_Node);
9940 else
9941 Insert_After (Freeze_Node (Par), F_Node);
9942 end if;
9943
9944 -- Freeze package enclosing instance of inner generic after
9945 -- instance of enclosing generic.
9946
9947 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9948 and then In_Same_Declarative_Part
9949 (Parent (Freeze_Node (Par)), Parent (N))
9950 then
9951 declare
9952 Enclosing : Entity_Id;
9953
9954 begin
9955 Enclosing := Corresponding_Spec (Parent (N));
9956
9957 if No (Enclosing) then
9958 Enclosing := Defining_Entity (Parent (N));
9959 end if;
9960
9961 Insert_Freeze_Node_For_Instance (N, F_Node);
9962 Ensure_Freeze_Node (Enclosing);
9963
9964 if not Is_List_Member (Freeze_Node (Enclosing)) then
9965
9966 -- The enclosing context is a subunit, insert the freeze
9967 -- node after the stub.
9968
9969 if Nkind (Parent (Parent (N))) = N_Subunit then
9970 Insert_Freeze_Node_For_Instance
9971 (Corresponding_Stub (Parent (Parent (N))),
9972 Freeze_Node (Enclosing));
9973
9974 -- The enclosing context is a package with a stub body
9975 -- which has already been replaced by the real body.
9976 -- Insert the freeze node after the actual body.
9977
9978 elsif Ekind (Enclosing) = E_Package
9979 and then Present (Body_Entity (Enclosing))
9980 and then Was_Originally_Stub
9981 (Parent (Body_Entity (Enclosing)))
9982 then
9983 Insert_Freeze_Node_For_Instance
9984 (Parent (Body_Entity (Enclosing)),
9985 Freeze_Node (Enclosing));
9986
9987 -- The parent instance has been frozen before the body of
9988 -- the enclosing package, insert the freeze node after
9989 -- the body.
9990
9991 elsif In_Same_List (Freeze_Node (Par), Parent (N))
9992 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9993 then
9994 Insert_Freeze_Node_For_Instance
9995 (Parent (N), Freeze_Node (Enclosing));
9996
9997 else
9998 Insert_After
9999 (Freeze_Node (Par), Freeze_Node (Enclosing));
10000 end if;
10001 end if;
10002 end;
10003
10004 else
10005 Insert_Freeze_Node_For_Instance (N, F_Node);
10006 end if;
10007
10008 else
10009 Insert_Freeze_Node_For_Instance (N, F_Node);
10010 end if;
10011 end if;
10012
10013 Set_Is_Frozen (Act_Id);
10014 Insert_Before (N, Act_Body);
10015 Mark_Rewrite_Insertion (Act_Body);
10016 end Install_Body;
10017
10018 -----------------------------
10019 -- Install_Formal_Packages --
10020 -----------------------------
10021
10022 procedure Install_Formal_Packages (Par : Entity_Id) is
10023 E : Entity_Id;
10024 Gen : Entity_Id;
10025 Gen_E : Entity_Id := Empty;
10026
10027 begin
10028 E := First_Entity (Par);
10029
10030 -- If we are installing an instance parent, locate the formal packages
10031 -- of its generic parent.
10032
10033 if Is_Generic_Instance (Par) then
10034 Gen := Generic_Parent (Package_Specification (Par));
10035 Gen_E := First_Entity (Gen);
10036 end if;
10037
10038 while Present (E) loop
10039 if Ekind (E) = E_Package
10040 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10041 then
10042 -- If this is the renaming for the parent instance, done
10043
10044 if Renamed_Object (E) = Par then
10045 exit;
10046
10047 -- The visibility of a formal of an enclosing generic is already
10048 -- correct.
10049
10050 elsif Denotes_Formal_Package (E) then
10051 null;
10052
10053 elsif Present (Associated_Formal_Package (E)) then
10054 Check_Generic_Actuals (Renamed_Object (E), True);
10055 Set_Is_Hidden (E, False);
10056
10057 -- Find formal package in generic unit that corresponds to
10058 -- (instance of) formal package in instance.
10059
10060 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10061 Next_Entity (Gen_E);
10062 end loop;
10063
10064 if Present (Gen_E) then
10065 Map_Formal_Package_Entities (Gen_E, E);
10066 end if;
10067 end if;
10068 end if;
10069
10070 Next_Entity (E);
10071
10072 if Present (Gen_E) then
10073 Next_Entity (Gen_E);
10074 end if;
10075 end loop;
10076 end Install_Formal_Packages;
10077
10078 --------------------
10079 -- Install_Parent --
10080 --------------------
10081
10082 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10083 Ancestors : constant Elist_Id := New_Elmt_List;
10084 S : constant Entity_Id := Current_Scope;
10085 Inst_Par : Entity_Id;
10086 First_Par : Entity_Id;
10087 Inst_Node : Node_Id;
10088 Gen_Par : Entity_Id;
10089 First_Gen : Entity_Id;
10090 Elmt : Elmt_Id;
10091
10092 procedure Install_Noninstance_Specs (Par : Entity_Id);
10093 -- Install the scopes of noninstance parent units ending with Par
10094
10095 procedure Install_Spec (Par : Entity_Id);
10096 -- The child unit is within the declarative part of the parent, so the
10097 -- declarations within the parent are immediately visible.
10098
10099 -------------------------------
10100 -- Install_Noninstance_Specs --
10101 -------------------------------
10102
10103 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10104 begin
10105 if Present (Par)
10106 and then Par /= Standard_Standard
10107 and then not In_Open_Scopes (Par)
10108 then
10109 Install_Noninstance_Specs (Scope (Par));
10110 Install_Spec (Par);
10111 end if;
10112 end Install_Noninstance_Specs;
10113
10114 ------------------
10115 -- Install_Spec --
10116 ------------------
10117
10118 procedure Install_Spec (Par : Entity_Id) is
10119 Spec : constant Node_Id := Package_Specification (Par);
10120
10121 begin
10122 -- If this parent of the child instance is a top-level unit,
10123 -- then record the unit and its visibility for later resetting in
10124 -- Remove_Parent. We exclude units that are generic instances, as we
10125 -- only want to record this information for the ultimate top-level
10126 -- noninstance parent (is that always correct???).
10127
10128 if Scope (Par) = Standard_Standard
10129 and then not Is_Generic_Instance (Par)
10130 then
10131 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10132 Instance_Parent_Unit := Par;
10133 end if;
10134
10135 -- Open the parent scope and make it and its declarations visible.
10136 -- If this point is not within a body, then only the visible
10137 -- declarations should be made visible, and installation of the
10138 -- private declarations is deferred until the appropriate point
10139 -- within analysis of the spec being instantiated (see the handling
10140 -- of parent visibility in Analyze_Package_Specification). This is
10141 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10142 -- private view problems that occur when compiling instantiations of
10143 -- a generic child of that package (Generic_Dispatching_Constructor).
10144 -- If the instance freezes a tagged type, inlinings of operations
10145 -- from Ada.Tags may need the full view of type Tag. If inlining took
10146 -- proper account of establishing visibility of inlined subprograms'
10147 -- parents then it should be possible to remove this
10148 -- special check. ???
10149
10150 Push_Scope (Par);
10151 Set_Is_Immediately_Visible (Par);
10152 Install_Visible_Declarations (Par);
10153 Set_Use (Visible_Declarations (Spec));
10154
10155 if In_Body or else Is_RTU (Par, Ada_Tags) then
10156 Install_Private_Declarations (Par);
10157 Set_Use (Private_Declarations (Spec));
10158 end if;
10159 end Install_Spec;
10160
10161 -- Start of processing for Install_Parent
10162
10163 begin
10164 -- We need to install the parent instance to compile the instantiation
10165 -- of the child, but the child instance must appear in the current
10166 -- scope. Given that we cannot place the parent above the current scope
10167 -- in the scope stack, we duplicate the current scope and unstack both
10168 -- after the instantiation is complete.
10169
10170 -- If the parent is itself the instantiation of a child unit, we must
10171 -- also stack the instantiation of its parent, and so on. Each such
10172 -- ancestor is the prefix of the name in a prior instantiation.
10173
10174 -- If this is a nested instance, the parent unit itself resolves to
10175 -- a renaming of the parent instance, whose declaration we need.
10176
10177 -- Finally, the parent may be a generic (not an instance) when the
10178 -- child unit appears as a formal package.
10179
10180 Inst_Par := P;
10181
10182 if Present (Renamed_Entity (Inst_Par)) then
10183 Inst_Par := Renamed_Entity (Inst_Par);
10184 end if;
10185
10186 First_Par := Inst_Par;
10187
10188 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10189
10190 First_Gen := Gen_Par;
10191
10192 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10193
10194 -- Load grandparent instance as well
10195
10196 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10197
10198 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10199 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10200
10201 if Present (Renamed_Entity (Inst_Par)) then
10202 Inst_Par := Renamed_Entity (Inst_Par);
10203 end if;
10204
10205 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10206
10207 if Present (Gen_Par) then
10208 Prepend_Elmt (Inst_Par, Ancestors);
10209
10210 else
10211 -- Parent is not the name of an instantiation
10212
10213 Install_Noninstance_Specs (Inst_Par);
10214 exit;
10215 end if;
10216
10217 else
10218 -- Previous error
10219
10220 exit;
10221 end if;
10222 end loop;
10223
10224 if Present (First_Gen) then
10225 Append_Elmt (First_Par, Ancestors);
10226 else
10227 Install_Noninstance_Specs (First_Par);
10228 end if;
10229
10230 if not Is_Empty_Elmt_List (Ancestors) then
10231 Elmt := First_Elmt (Ancestors);
10232 while Present (Elmt) loop
10233 Install_Spec (Node (Elmt));
10234 Install_Formal_Packages (Node (Elmt));
10235 Next_Elmt (Elmt);
10236 end loop;
10237 end if;
10238
10239 if not In_Body then
10240 Push_Scope (S);
10241 end if;
10242 end Install_Parent;
10243
10244 -------------------------------
10245 -- Install_Hidden_Primitives --
10246 -------------------------------
10247
10248 procedure Install_Hidden_Primitives
10249 (Prims_List : in out Elist_Id;
10250 Gen_T : Entity_Id;
10251 Act_T : Entity_Id)
10252 is
10253 Elmt : Elmt_Id;
10254 List : Elist_Id := No_Elist;
10255 Prim_G_Elmt : Elmt_Id;
10256 Prim_A_Elmt : Elmt_Id;
10257 Prim_G : Node_Id;
10258 Prim_A : Node_Id;
10259
10260 begin
10261 -- No action needed in case of serious errors because we cannot trust
10262 -- in the order of primitives
10263
10264 if Serious_Errors_Detected > 0 then
10265 return;
10266
10267 -- No action possible if we don't have available the list of primitive
10268 -- operations
10269
10270 elsif No (Gen_T)
10271 or else not Is_Record_Type (Gen_T)
10272 or else not Is_Tagged_Type (Gen_T)
10273 or else not Is_Record_Type (Act_T)
10274 or else not Is_Tagged_Type (Act_T)
10275 then
10276 return;
10277
10278 -- There is no need to handle interface types since their primitives
10279 -- cannot be hidden
10280
10281 elsif Is_Interface (Gen_T) then
10282 return;
10283 end if;
10284
10285 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10286
10287 if not Is_Class_Wide_Type (Act_T) then
10288 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10289 else
10290 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10291 end if;
10292
10293 loop
10294 -- Skip predefined primitives in the generic formal
10295
10296 while Present (Prim_G_Elmt)
10297 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10298 loop
10299 Next_Elmt (Prim_G_Elmt);
10300 end loop;
10301
10302 -- Skip predefined primitives in the generic actual
10303
10304 while Present (Prim_A_Elmt)
10305 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10306 loop
10307 Next_Elmt (Prim_A_Elmt);
10308 end loop;
10309
10310 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10311
10312 Prim_G := Node (Prim_G_Elmt);
10313 Prim_A := Node (Prim_A_Elmt);
10314
10315 -- There is no need to handle interface primitives because their
10316 -- primitives are not hidden
10317
10318 exit when Present (Interface_Alias (Prim_G));
10319
10320 -- Here we install one hidden primitive
10321
10322 if Chars (Prim_G) /= Chars (Prim_A)
10323 and then Has_Suffix (Prim_A, 'P')
10324 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10325 then
10326 Set_Chars (Prim_A, Chars (Prim_G));
10327 Append_New_Elmt (Prim_A, To => List);
10328 end if;
10329
10330 Next_Elmt (Prim_A_Elmt);
10331 Next_Elmt (Prim_G_Elmt);
10332 end loop;
10333
10334 -- Append the elements to the list of temporarily visible primitives
10335 -- avoiding duplicates.
10336
10337 if Present (List) then
10338 if No (Prims_List) then
10339 Prims_List := New_Elmt_List;
10340 end if;
10341
10342 Elmt := First_Elmt (List);
10343 while Present (Elmt) loop
10344 Append_Unique_Elmt (Node (Elmt), Prims_List);
10345 Next_Elmt (Elmt);
10346 end loop;
10347 end if;
10348 end Install_Hidden_Primitives;
10349
10350 -------------------------------
10351 -- Restore_Hidden_Primitives --
10352 -------------------------------
10353
10354 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10355 Prim_Elmt : Elmt_Id;
10356 Prim : Node_Id;
10357
10358 begin
10359 if Prims_List /= No_Elist then
10360 Prim_Elmt := First_Elmt (Prims_List);
10361 while Present (Prim_Elmt) loop
10362 Prim := Node (Prim_Elmt);
10363 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10364 Next_Elmt (Prim_Elmt);
10365 end loop;
10366
10367 Prims_List := No_Elist;
10368 end if;
10369 end Restore_Hidden_Primitives;
10370
10371 --------------------------------
10372 -- Instantiate_Formal_Package --
10373 --------------------------------
10374
10375 function Instantiate_Formal_Package
10376 (Formal : Node_Id;
10377 Actual : Node_Id;
10378 Analyzed_Formal : Node_Id) return List_Id
10379 is
10380 Loc : constant Source_Ptr := Sloc (Actual);
10381 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10382 Actual_Pack : Entity_Id;
10383 Formal_Pack : Entity_Id;
10384 Gen_Parent : Entity_Id;
10385 Decls : List_Id;
10386 Nod : Node_Id;
10387 Parent_Spec : Node_Id;
10388
10389 procedure Find_Matching_Actual
10390 (F : Node_Id;
10391 Act : in out Entity_Id);
10392 -- We need to associate each formal entity in the formal package with
10393 -- the corresponding entity in the actual package. The actual package
10394 -- has been analyzed and possibly expanded, and as a result there is
10395 -- no one-to-one correspondence between the two lists (for example,
10396 -- the actual may include subtypes, itypes, and inherited primitive
10397 -- operations, interspersed among the renaming declarations for the
10398 -- actuals). We retrieve the corresponding actual by name because each
10399 -- actual has the same name as the formal, and they do appear in the
10400 -- same order.
10401
10402 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10403 -- Retrieve entity of defining entity of generic formal parameter.
10404 -- Only the declarations of formals need to be considered when
10405 -- linking them to actuals, but the declarative list may include
10406 -- internal entities generated during analysis, and those are ignored.
10407
10408 procedure Match_Formal_Entity
10409 (Formal_Node : Node_Id;
10410 Formal_Ent : Entity_Id;
10411 Actual_Ent : Entity_Id);
10412 -- Associates the formal entity with the actual. In the case where
10413 -- Formal_Ent is a formal package, this procedure iterates through all
10414 -- of its formals and enters associations between the actuals occurring
10415 -- in the formal package's corresponding actual package (given by
10416 -- Actual_Ent) and the formal package's formal parameters. This
10417 -- procedure recurses if any of the parameters is itself a package.
10418
10419 function Is_Instance_Of
10420 (Act_Spec : Entity_Id;
10421 Gen_Anc : Entity_Id) return Boolean;
10422 -- The actual can be an instantiation of a generic within another
10423 -- instance, in which case there is no direct link from it to the
10424 -- original generic ancestor. In that case, we recognize that the
10425 -- ultimate ancestor is the same by examining names and scopes.
10426
10427 procedure Process_Nested_Formal (Formal : Entity_Id);
10428 -- If the current formal is declared with a box, its own formals are
10429 -- visible in the instance, as they were in the generic, and their
10430 -- Hidden flag must be reset. If some of these formals are themselves
10431 -- packages declared with a box, the processing must be recursive.
10432
10433 --------------------------
10434 -- Find_Matching_Actual --
10435 --------------------------
10436
10437 procedure Find_Matching_Actual
10438 (F : Node_Id;
10439 Act : in out Entity_Id)
10440 is
10441 Formal_Ent : Entity_Id;
10442
10443 begin
10444 case Nkind (Original_Node (F)) is
10445 when N_Formal_Object_Declaration
10446 | N_Formal_Type_Declaration
10447 =>
10448 Formal_Ent := Defining_Identifier (F);
10449
10450 while Present (Act)
10451 and then Chars (Act) /= Chars (Formal_Ent)
10452 loop
10453 Next_Entity (Act);
10454 end loop;
10455
10456 when N_Formal_Package_Declaration
10457 | N_Formal_Subprogram_Declaration
10458 | N_Generic_Package_Declaration
10459 | N_Package_Declaration
10460 =>
10461 Formal_Ent := Defining_Entity (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 others =>
10470 raise Program_Error;
10471 end case;
10472 end Find_Matching_Actual;
10473
10474 -------------------------
10475 -- Match_Formal_Entity --
10476 -------------------------
10477
10478 procedure Match_Formal_Entity
10479 (Formal_Node : Node_Id;
10480 Formal_Ent : Entity_Id;
10481 Actual_Ent : Entity_Id)
10482 is
10483 Act_Pkg : Entity_Id;
10484
10485 begin
10486 Set_Instance_Of (Formal_Ent, Actual_Ent);
10487
10488 if Ekind (Actual_Ent) = E_Package then
10489
10490 -- Record associations for each parameter
10491
10492 Act_Pkg := Actual_Ent;
10493
10494 declare
10495 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10496 F_Ent : Entity_Id;
10497 F_Node : Node_Id;
10498
10499 Gen_Decl : Node_Id;
10500 Formals : List_Id;
10501 Actual : Entity_Id;
10502
10503 begin
10504 -- Retrieve the actual given in the formal package declaration
10505
10506 Actual := Entity (Name (Original_Node (Formal_Node)));
10507
10508 -- The actual in the formal package declaration may be a
10509 -- renamed generic package, in which case we want to retrieve
10510 -- the original generic in order to traverse its formal part.
10511
10512 if Present (Renamed_Entity (Actual)) then
10513 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10514 else
10515 Gen_Decl := Unit_Declaration_Node (Actual);
10516 end if;
10517
10518 Formals := Generic_Formal_Declarations (Gen_Decl);
10519
10520 if Present (Formals) then
10521 F_Node := First_Non_Pragma (Formals);
10522 else
10523 F_Node := Empty;
10524 end if;
10525
10526 while Present (A_Ent)
10527 and then Present (F_Node)
10528 and then A_Ent /= First_Private_Entity (Act_Pkg)
10529 loop
10530 F_Ent := Get_Formal_Entity (F_Node);
10531
10532 if Present (F_Ent) then
10533
10534 -- This is a formal of the original package. Record
10535 -- association and recurse.
10536
10537 Find_Matching_Actual (F_Node, A_Ent);
10538 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10539 Next_Entity (A_Ent);
10540 end if;
10541
10542 Next_Non_Pragma (F_Node);
10543 end loop;
10544 end;
10545 end if;
10546 end Match_Formal_Entity;
10547
10548 -----------------------
10549 -- Get_Formal_Entity --
10550 -----------------------
10551
10552 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10553 Kind : constant Node_Kind := Nkind (Original_Node (N));
10554 begin
10555 case Kind is
10556 when N_Formal_Object_Declaration =>
10557 return Defining_Identifier (N);
10558
10559 when N_Formal_Type_Declaration =>
10560 return Defining_Identifier (N);
10561
10562 when N_Formal_Subprogram_Declaration =>
10563 return Defining_Unit_Name (Specification (N));
10564
10565 when N_Formal_Package_Declaration =>
10566 return Defining_Identifier (Original_Node (N));
10567
10568 when N_Generic_Package_Declaration =>
10569 return Defining_Identifier (Original_Node (N));
10570
10571 -- All other declarations are introduced by semantic analysis and
10572 -- have no match in the actual.
10573
10574 when others =>
10575 return Empty;
10576 end case;
10577 end Get_Formal_Entity;
10578
10579 --------------------
10580 -- Is_Instance_Of --
10581 --------------------
10582
10583 function Is_Instance_Of
10584 (Act_Spec : Entity_Id;
10585 Gen_Anc : Entity_Id) return Boolean
10586 is
10587 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10588
10589 begin
10590 if No (Gen_Par) then
10591 return False;
10592
10593 -- Simplest case: the generic parent of the actual is the formal
10594
10595 elsif Gen_Par = Gen_Anc then
10596 return True;
10597
10598 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10599 return False;
10600
10601 -- The actual may be obtained through several instantiations. Its
10602 -- scope must itself be an instance of a generic declared in the
10603 -- same scope as the formal. Any other case is detected above.
10604
10605 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10606 return False;
10607
10608 else
10609 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10610 end if;
10611 end Is_Instance_Of;
10612
10613 ---------------------------
10614 -- Process_Nested_Formal --
10615 ---------------------------
10616
10617 procedure Process_Nested_Formal (Formal : Entity_Id) is
10618 Ent : Entity_Id;
10619
10620 begin
10621 if Present (Associated_Formal_Package (Formal))
10622 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10623 then
10624 Ent := First_Entity (Formal);
10625 while Present (Ent) loop
10626 Set_Is_Hidden (Ent, False);
10627 Set_Is_Visible_Formal (Ent);
10628 Set_Is_Potentially_Use_Visible
10629 (Ent, Is_Potentially_Use_Visible (Formal));
10630
10631 if Ekind (Ent) = E_Package then
10632 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10633 Process_Nested_Formal (Ent);
10634 end if;
10635
10636 Next_Entity (Ent);
10637 end loop;
10638 end if;
10639 end Process_Nested_Formal;
10640
10641 -- Start of processing for Instantiate_Formal_Package
10642
10643 begin
10644 Analyze (Actual);
10645
10646 -- The actual must be a package instance, or else a current instance
10647 -- such as a parent generic within the body of a generic child.
10648
10649 if not Is_Entity_Name (Actual)
10650 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10651 then
10652 Error_Msg_N
10653 ("expect package instance to instantiate formal", Actual);
10654 Abandon_Instantiation (Actual);
10655 raise Program_Error;
10656
10657 else
10658 Actual_Pack := Entity (Actual);
10659 Set_Is_Instantiated (Actual_Pack);
10660
10661 -- The actual may be a renamed package, or an outer generic formal
10662 -- package whose instantiation is converted into a renaming.
10663
10664 if Present (Renamed_Object (Actual_Pack)) then
10665 Actual_Pack := Renamed_Object (Actual_Pack);
10666 end if;
10667
10668 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10669 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10670 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10671 else
10672 Gen_Parent :=
10673 Generic_Parent (Specification (Analyzed_Formal));
10674 Formal_Pack :=
10675 Defining_Unit_Name (Specification (Analyzed_Formal));
10676 end if;
10677
10678 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10679 Parent_Spec := Package_Specification (Actual_Pack);
10680 else
10681 Parent_Spec := Parent (Actual_Pack);
10682 end if;
10683
10684 if Gen_Parent = Any_Id then
10685 Error_Msg_N
10686 ("previous error in declaration of formal package", Actual);
10687 Abandon_Instantiation (Actual);
10688
10689 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10690 null;
10691
10692 -- If this is the current instance of an enclosing generic, that unit
10693 -- is the generic package we need.
10694
10695 elsif In_Open_Scopes (Actual_Pack)
10696 and then Ekind (Actual_Pack) = E_Generic_Package
10697 then
10698 null;
10699
10700 else
10701 Error_Msg_NE
10702 ("actual parameter must be instance of&", Actual, Gen_Parent);
10703 Abandon_Instantiation (Actual);
10704 end if;
10705
10706 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10707 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10708
10709 Nod :=
10710 Make_Package_Renaming_Declaration (Loc,
10711 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10712 Name => New_Occurrence_Of (Actual_Pack, Loc));
10713
10714 Set_Associated_Formal_Package
10715 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10716 Decls := New_List (Nod);
10717
10718 -- If the formal F has a box, then the generic declarations are
10719 -- visible in the generic G. In an instance of G, the corresponding
10720 -- entities in the actual for F (which are the actuals for the
10721 -- instantiation of the generic that F denotes) must also be made
10722 -- visible for analysis of the current instance. On exit from the
10723 -- current instance, those entities are made private again. If the
10724 -- actual is currently in use, these entities are also use-visible.
10725
10726 -- The loop through the actual entities also steps through the formal
10727 -- entities and enters associations from formals to actuals into the
10728 -- renaming map. This is necessary to properly handle checking of
10729 -- actual parameter associations for later formals that depend on
10730 -- actuals declared in the formal package.
10731
10732 -- In Ada 2005, partial parameterization requires that we make
10733 -- visible the actuals corresponding to formals that were defaulted
10734 -- in the formal package. There formals are identified because they
10735 -- remain formal generics within the formal package, rather than
10736 -- being renamings of the actuals supplied.
10737
10738 declare
10739 Gen_Decl : constant Node_Id :=
10740 Unit_Declaration_Node (Gen_Parent);
10741 Formals : constant List_Id :=
10742 Generic_Formal_Declarations (Gen_Decl);
10743
10744 Actual_Ent : Entity_Id;
10745 Actual_Of_Formal : Node_Id;
10746 Formal_Node : Node_Id;
10747 Formal_Ent : Entity_Id;
10748
10749 begin
10750 if Present (Formals) then
10751 Formal_Node := First_Non_Pragma (Formals);
10752 else
10753 Formal_Node := Empty;
10754 end if;
10755
10756 Actual_Ent := First_Entity (Actual_Pack);
10757 Actual_Of_Formal :=
10758 First (Visible_Declarations (Specification (Analyzed_Formal)));
10759 while Present (Actual_Ent)
10760 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10761 loop
10762 if Present (Formal_Node) then
10763 Formal_Ent := Get_Formal_Entity (Formal_Node);
10764
10765 if Present (Formal_Ent) then
10766 Find_Matching_Actual (Formal_Node, Actual_Ent);
10767 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10768
10769 -- We iterate at the same time over the actuals of the
10770 -- local package created for the formal, to determine
10771 -- which one of the formals of the original generic were
10772 -- defaulted in the formal. The corresponding actual
10773 -- entities are visible in the enclosing instance.
10774
10775 if Box_Present (Formal)
10776 or else
10777 (Present (Actual_Of_Formal)
10778 and then
10779 Is_Generic_Formal
10780 (Get_Formal_Entity (Actual_Of_Formal)))
10781 then
10782 Set_Is_Hidden (Actual_Ent, False);
10783 Set_Is_Visible_Formal (Actual_Ent);
10784 Set_Is_Potentially_Use_Visible
10785 (Actual_Ent, In_Use (Actual_Pack));
10786
10787 if Ekind (Actual_Ent) = E_Package then
10788 Process_Nested_Formal (Actual_Ent);
10789 end if;
10790
10791 else
10792 if not Is_Hidden (Actual_Ent) then
10793 Append_Elmt (Actual_Ent, Hidden_Formals);
10794 end if;
10795
10796 Set_Is_Hidden (Actual_Ent);
10797 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10798 end if;
10799 end if;
10800
10801 Next_Non_Pragma (Formal_Node);
10802 Next (Actual_Of_Formal);
10803
10804 else
10805 -- No further formals to match, but the generic part may
10806 -- contain inherited operation that are not hidden in the
10807 -- enclosing instance.
10808
10809 Next_Entity (Actual_Ent);
10810 end if;
10811 end loop;
10812
10813 -- Inherited subprograms generated by formal derived types are
10814 -- also visible if the types are.
10815
10816 Actual_Ent := First_Entity (Actual_Pack);
10817 while Present (Actual_Ent)
10818 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10819 loop
10820 if Is_Overloadable (Actual_Ent)
10821 and then
10822 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10823 and then
10824 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10825 then
10826 Set_Is_Hidden (Actual_Ent, False);
10827 Set_Is_Potentially_Use_Visible
10828 (Actual_Ent, In_Use (Actual_Pack));
10829 end if;
10830
10831 Next_Entity (Actual_Ent);
10832 end loop;
10833
10834 -- No conformance to check if the generic has no formal parameters
10835 -- and the formal package has no generic associations.
10836
10837 if Is_Empty_List (Formals)
10838 and then
10839 (Box_Present (Formal)
10840 or else No (Generic_Associations (Formal)))
10841 then
10842 return Decls;
10843 end if;
10844 end;
10845
10846 -- If the formal is not declared with a box, reanalyze it as an
10847 -- abbreviated instantiation, to verify the matching rules of 12.7.
10848 -- The actual checks are performed after the generic associations
10849 -- have been analyzed, to guarantee the same visibility for this
10850 -- instantiation and for the actuals.
10851
10852 -- In Ada 2005, the generic associations for the formal can include
10853 -- defaulted parameters. These are ignored during check. This
10854 -- internal instantiation is removed from the tree after conformance
10855 -- checking, because it contains formal declarations for those
10856 -- defaulted parameters, and those should not reach the back-end.
10857
10858 if not Box_Present (Formal) then
10859 declare
10860 I_Pack : constant Entity_Id :=
10861 Make_Temporary (Sloc (Actual), 'P');
10862
10863 begin
10864 Set_Is_Internal (I_Pack);
10865 Set_Ekind (I_Pack, E_Package);
10866 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10867
10868 Append_To (Decls,
10869 Make_Package_Instantiation (Sloc (Actual),
10870 Defining_Unit_Name => I_Pack,
10871 Name =>
10872 New_Occurrence_Of
10873 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10874 Generic_Associations => Generic_Associations (Formal)));
10875 end;
10876 end if;
10877
10878 return Decls;
10879 end if;
10880 end Instantiate_Formal_Package;
10881
10882 -----------------------------------
10883 -- Instantiate_Formal_Subprogram --
10884 -----------------------------------
10885
10886 function Instantiate_Formal_Subprogram
10887 (Formal : Node_Id;
10888 Actual : Node_Id;
10889 Analyzed_Formal : Node_Id) return Node_Id
10890 is
10891 Analyzed_S : constant Entity_Id :=
10892 Defining_Unit_Name (Specification (Analyzed_Formal));
10893 Formal_Sub : constant Entity_Id :=
10894 Defining_Unit_Name (Specification (Formal));
10895
10896 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10897 -- If the generic is a child unit, the parent has been installed on the
10898 -- scope stack, but a default subprogram cannot resolve to something
10899 -- on the parent because that parent is not really part of the visible
10900 -- context (it is there to resolve explicit local entities). If the
10901 -- default has resolved in this way, we remove the entity from immediate
10902 -- visibility and analyze the node again to emit an error message or
10903 -- find another visible candidate.
10904
10905 procedure Valid_Actual_Subprogram (Act : Node_Id);
10906 -- Perform legality check and raise exception on failure
10907
10908 -----------------------
10909 -- From_Parent_Scope --
10910 -----------------------
10911
10912 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10913 Gen_Scope : Node_Id;
10914
10915 begin
10916 Gen_Scope := Scope (Analyzed_S);
10917 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10918 if Scope (Subp) = Scope (Gen_Scope) then
10919 return True;
10920 end if;
10921
10922 Gen_Scope := Scope (Gen_Scope);
10923 end loop;
10924
10925 return False;
10926 end From_Parent_Scope;
10927
10928 -----------------------------
10929 -- Valid_Actual_Subprogram --
10930 -----------------------------
10931
10932 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10933 Act_E : Entity_Id;
10934
10935 begin
10936 if Is_Entity_Name (Act) then
10937 Act_E := Entity (Act);
10938
10939 elsif Nkind (Act) = N_Selected_Component
10940 and then Is_Entity_Name (Selector_Name (Act))
10941 then
10942 Act_E := Entity (Selector_Name (Act));
10943
10944 else
10945 Act_E := Empty;
10946 end if;
10947
10948 if (Present (Act_E) and then Is_Overloadable (Act_E))
10949 or else Nkind (Act) in N_Attribute_Reference
10950 | N_Indexed_Component
10951 | N_Character_Literal
10952 | N_Explicit_Dereference
10953 then
10954 return;
10955 end if;
10956
10957 Error_Msg_NE
10958 ("expect subprogram or entry name in instantiation of &",
10959 Instantiation_Node, Formal_Sub);
10960 Abandon_Instantiation (Instantiation_Node);
10961 end Valid_Actual_Subprogram;
10962
10963 -- Local variables
10964
10965 Decl_Node : Node_Id;
10966 Loc : Source_Ptr;
10967 Nam : Node_Id;
10968 New_Spec : Node_Id;
10969 New_Subp : Entity_Id;
10970
10971 -- Start of processing for Instantiate_Formal_Subprogram
10972
10973 begin
10974 New_Spec := New_Copy_Tree (Specification (Formal));
10975
10976 -- The tree copy has created the proper instantiation sloc for the
10977 -- new specification. Use this location for all other constructed
10978 -- declarations.
10979
10980 Loc := Sloc (Defining_Unit_Name (New_Spec));
10981
10982 -- Create new entity for the actual (New_Copy_Tree does not), and
10983 -- indicate that it is an actual.
10984
10985 -- If the actual is not an entity (i.e. an attribute reference)
10986 -- and the formal includes aspect specifications for contracts,
10987 -- we create an internal name for the renaming declaration. The
10988 -- constructed wrapper contains a call to the entity in the renaming.
10989 -- This is an expansion activity, as is the wrapper creation.
10990
10991 if Ada_Version >= Ada_2020
10992 and then Has_Contracts (Analyzed_Formal)
10993 and then not Is_Entity_Name (Actual)
10994 and then Expander_Active
10995 then
10996 New_Subp := Make_Temporary (Sloc (Actual), 'S');
10997 Set_Defining_Unit_Name (New_Spec, New_Subp);
10998 else
10999 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11000 end if;
11001
11002 Set_Ekind (New_Subp, Ekind (Analyzed_S));
11003 Set_Is_Generic_Actual_Subprogram (New_Subp);
11004 Set_Defining_Unit_Name (New_Spec, New_Subp);
11005
11006 -- Create new entities for the each of the formals in the specification
11007 -- of the renaming declaration built for the actual.
11008
11009 if Present (Parameter_Specifications (New_Spec)) then
11010 declare
11011 F : Node_Id;
11012 F_Id : Entity_Id;
11013
11014 begin
11015 F := First (Parameter_Specifications (New_Spec));
11016 while Present (F) loop
11017 F_Id := Defining_Identifier (F);
11018
11019 Set_Defining_Identifier (F,
11020 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11021 Next (F);
11022 end loop;
11023 end;
11024 end if;
11025
11026 -- Find entity of actual. If the actual is an attribute reference, it
11027 -- cannot be resolved here (its formal is missing) but is handled
11028 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11029 -- fully resolved subsequently, when the renaming declaration for the
11030 -- formal is analyzed. If it is an explicit dereference, resolve the
11031 -- prefix but not the actual itself, to prevent interpretation as call.
11032
11033 if Present (Actual) then
11034 Loc := Sloc (Actual);
11035 Set_Sloc (New_Spec, Loc);
11036
11037 if Nkind (Actual) = N_Operator_Symbol then
11038 Find_Direct_Name (Actual);
11039
11040 elsif Nkind (Actual) = N_Explicit_Dereference then
11041 Analyze (Prefix (Actual));
11042
11043 elsif Nkind (Actual) /= N_Attribute_Reference then
11044 Analyze (Actual);
11045 end if;
11046
11047 Valid_Actual_Subprogram (Actual);
11048 Nam := Actual;
11049
11050 elsif Present (Default_Name (Formal)) then
11051 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11052 | N_Selected_Component
11053 | N_Indexed_Component
11054 | N_Character_Literal
11055 and then Present (Entity (Default_Name (Formal)))
11056 then
11057 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11058 else
11059 Nam := New_Copy (Default_Name (Formal));
11060 Set_Sloc (Nam, Loc);
11061 end if;
11062
11063 elsif Box_Present (Formal) then
11064
11065 -- Actual is resolved at the point of instantiation. Create an
11066 -- identifier or operator with the same name as the formal.
11067
11068 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11069 Nam :=
11070 Make_Operator_Symbol (Loc,
11071 Chars => Chars (Formal_Sub),
11072 Strval => No_String);
11073 else
11074 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11075 end if;
11076
11077 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11078 and then Null_Present (Specification (Formal))
11079 then
11080 -- Generate null body for procedure, for use in the instance
11081
11082 Decl_Node :=
11083 Make_Subprogram_Body (Loc,
11084 Specification => New_Spec,
11085 Declarations => New_List,
11086 Handled_Statement_Sequence =>
11087 Make_Handled_Sequence_Of_Statements (Loc,
11088 Statements => New_List (Make_Null_Statement (Loc))));
11089
11090 -- RM 12.6 (16 2/2): The procedure has convention Intrinsic
11091
11092 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11093
11094 -- Eliminate the calls to it when optimization is enabled
11095
11096 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11097 return Decl_Node;
11098
11099 else
11100 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11101 Error_Msg_NE
11102 ("missing actual&", Instantiation_Node, Formal_Sub);
11103 Error_Msg_NE
11104 ("\in instantiation of & declared#",
11105 Instantiation_Node, Scope (Analyzed_S));
11106 Abandon_Instantiation (Instantiation_Node);
11107 end if;
11108
11109 Decl_Node :=
11110 Make_Subprogram_Renaming_Declaration (Loc,
11111 Specification => New_Spec,
11112 Name => Nam);
11113
11114 -- If we do not have an actual and the formal specified <> then set to
11115 -- get proper default.
11116
11117 if No (Actual) and then Box_Present (Formal) then
11118 Set_From_Default (Decl_Node);
11119 end if;
11120
11121 -- Gather possible interpretations for the actual before analyzing the
11122 -- instance. If overloaded, it will be resolved when analyzing the
11123 -- renaming declaration.
11124
11125 if Box_Present (Formal) and then No (Actual) then
11126 Analyze (Nam);
11127
11128 if Is_Child_Unit (Scope (Analyzed_S))
11129 and then Present (Entity (Nam))
11130 then
11131 if not Is_Overloaded (Nam) then
11132 if From_Parent_Scope (Entity (Nam)) then
11133 Set_Is_Immediately_Visible (Entity (Nam), False);
11134 Set_Entity (Nam, Empty);
11135 Set_Etype (Nam, Empty);
11136
11137 Analyze (Nam);
11138 Set_Is_Immediately_Visible (Entity (Nam));
11139 end if;
11140
11141 else
11142 declare
11143 I : Interp_Index;
11144 It : Interp;
11145
11146 begin
11147 Get_First_Interp (Nam, I, It);
11148 while Present (It.Nam) loop
11149 if From_Parent_Scope (It.Nam) then
11150 Remove_Interp (I);
11151 end if;
11152
11153 Get_Next_Interp (I, It);
11154 end loop;
11155 end;
11156 end if;
11157 end if;
11158 end if;
11159
11160 -- The generic instantiation freezes the actual. This can only be done
11161 -- once the actual is resolved, in the analysis of the renaming
11162 -- declaration. To make the formal subprogram entity available, we set
11163 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11164 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11165 -- of formal abstract subprograms.
11166
11167 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11168
11169 -- We cannot analyze the renaming declaration, and thus find the actual,
11170 -- until all the actuals are assembled in the instance. For subsequent
11171 -- checks of other actuals, indicate the node that will hold the
11172 -- instance of this formal.
11173
11174 Set_Instance_Of (Analyzed_S, Nam);
11175
11176 if Nkind (Actual) = N_Selected_Component
11177 and then Is_Task_Type (Etype (Prefix (Actual)))
11178 and then not Is_Frozen (Etype (Prefix (Actual)))
11179 then
11180 -- The renaming declaration will create a body, which must appear
11181 -- outside of the instantiation, We move the renaming declaration
11182 -- out of the instance, and create an additional renaming inside,
11183 -- to prevent freezing anomalies.
11184
11185 declare
11186 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11187
11188 begin
11189 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11190 Insert_Before (Instantiation_Node, Decl_Node);
11191 Analyze (Decl_Node);
11192
11193 -- Now create renaming within the instance
11194
11195 Decl_Node :=
11196 Make_Subprogram_Renaming_Declaration (Loc,
11197 Specification => New_Copy_Tree (New_Spec),
11198 Name => New_Occurrence_Of (Anon_Id, Loc));
11199
11200 Set_Defining_Unit_Name (Specification (Decl_Node),
11201 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11202 end;
11203 end if;
11204
11205 return Decl_Node;
11206 end Instantiate_Formal_Subprogram;
11207
11208 ------------------------
11209 -- Instantiate_Object --
11210 ------------------------
11211
11212 function Instantiate_Object
11213 (Formal : Node_Id;
11214 Actual : Node_Id;
11215 Analyzed_Formal : Node_Id) return List_Id
11216 is
11217 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11218 A_Gen_Obj : constant Entity_Id :=
11219 Defining_Identifier (Analyzed_Formal);
11220 Acc_Def : Node_Id := Empty;
11221 Act_Assoc : constant Node_Id := Parent (Actual);
11222 Actual_Decl : Node_Id := Empty;
11223 Decl_Node : Node_Id;
11224 Def : Node_Id;
11225 Ftyp : Entity_Id;
11226 List : constant List_Id := New_List;
11227 Loc : constant Source_Ptr := Sloc (Actual);
11228 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11229 Subt_Decl : Node_Id := Empty;
11230 Subt_Mark : Node_Id := Empty;
11231
11232 -- Start of processing for Instantiate_Object
11233
11234 begin
11235 -- Formal may be an anonymous access
11236
11237 if Present (Subtype_Mark (Formal)) then
11238 Subt_Mark := Subtype_Mark (Formal);
11239 else
11240 Check_Access_Definition (Formal);
11241 Acc_Def := Access_Definition (Formal);
11242 end if;
11243
11244 -- Sloc for error message on missing actual
11245
11246 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11247
11248 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11249 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11250 end if;
11251
11252 Set_Parent (List, Parent (Actual));
11253
11254 -- OUT present
11255
11256 if Out_Present (Formal) then
11257
11258 -- An IN OUT generic actual must be a name. The instantiation is a
11259 -- renaming declaration. The actual is the name being renamed. We
11260 -- use the actual directly, rather than a copy, because it is not
11261 -- used further in the list of actuals, and because a copy or a use
11262 -- of relocate_node is incorrect if the instance is nested within a
11263 -- generic. In order to simplify e.g. ASIS queries, the
11264 -- Generic_Parent field links the declaration to the generic
11265 -- association.
11266
11267 if No (Actual) then
11268 Error_Msg_NE
11269 ("missing actual &",
11270 Instantiation_Node, Gen_Obj);
11271 Error_Msg_NE
11272 ("\in instantiation of & declared#",
11273 Instantiation_Node, Scope (A_Gen_Obj));
11274 Abandon_Instantiation (Instantiation_Node);
11275 end if;
11276
11277 if Present (Subt_Mark) then
11278 Decl_Node :=
11279 Make_Object_Renaming_Declaration (Loc,
11280 Defining_Identifier => New_Copy (Gen_Obj),
11281 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11282 Name => Actual);
11283
11284 else pragma Assert (Present (Acc_Def));
11285 Decl_Node :=
11286 Make_Object_Renaming_Declaration (Loc,
11287 Defining_Identifier => New_Copy (Gen_Obj),
11288 Access_Definition => New_Copy_Tree (Acc_Def),
11289 Name => Actual);
11290 end if;
11291
11292 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11293
11294 -- The analysis of the actual may produce Insert_Action nodes, so
11295 -- the declaration must have a context in which to attach them.
11296
11297 Append (Decl_Node, List);
11298 Analyze (Actual);
11299
11300 -- Return if the analysis of the actual reported some error
11301
11302 if Etype (Actual) = Any_Type then
11303 return List;
11304 end if;
11305
11306 -- This check is performed here because Analyze_Object_Renaming will
11307 -- not check it when Comes_From_Source is False. Note though that the
11308 -- check for the actual being the name of an object will be performed
11309 -- in Analyze_Object_Renaming.
11310
11311 if Is_Object_Reference (Actual)
11312 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11313 then
11314 Error_Msg_N
11315 ("illegal discriminant-dependent component for in out parameter",
11316 Actual);
11317 end if;
11318
11319 -- The actual has to be resolved in order to check that it is a
11320 -- variable (due to cases such as F (1), where F returns access to
11321 -- an array, and for overloaded prefixes).
11322
11323 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11324
11325 -- If the type of the formal is not itself a formal, and the current
11326 -- unit is a child unit, the formal type must be declared in a
11327 -- parent, and must be retrieved by visibility.
11328
11329 if Ftyp = Orig_Ftyp
11330 and then Is_Generic_Unit (Scope (Ftyp))
11331 and then Is_Child_Unit (Scope (A_Gen_Obj))
11332 then
11333 declare
11334 Temp : constant Node_Id :=
11335 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11336 begin
11337 Set_Entity (Temp, Empty);
11338 Find_Type (Temp);
11339 Ftyp := Entity (Temp);
11340 end;
11341 end if;
11342
11343 if Is_Private_Type (Ftyp)
11344 and then not Is_Private_Type (Etype (Actual))
11345 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11346 or else Base_Type (Etype (Actual)) = Ftyp)
11347 then
11348 -- If the actual has the type of the full view of the formal, or
11349 -- else a non-private subtype of the formal, then the visibility
11350 -- of the formal type has changed. Add to the actuals a subtype
11351 -- declaration that will force the exchange of views in the body
11352 -- of the instance as well.
11353
11354 Subt_Decl :=
11355 Make_Subtype_Declaration (Loc,
11356 Defining_Identifier => Make_Temporary (Loc, 'P'),
11357 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11358
11359 Prepend (Subt_Decl, List);
11360
11361 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11362 Exchange_Declarations (Ftyp);
11363 end if;
11364
11365 Resolve (Actual, Ftyp);
11366
11367 if not Denotes_Variable (Actual) then
11368 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11369
11370 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11371
11372 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11373 -- the type of the actual shall resolve to a specific anonymous
11374 -- access type.
11375
11376 if Ada_Version < Ada_2005
11377 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11378 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11379 then
11380 Error_Msg_NE
11381 ("type of actual does not match type of&", Actual, Gen_Obj);
11382 end if;
11383 end if;
11384
11385 Note_Possible_Modification (Actual, Sure => True);
11386
11387 -- Check for instantiation with atomic/volatile/VFA object actual for
11388 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11389
11390 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11391 Error_Msg_NE
11392 ("cannot instantiate nonatomic formal & of mode in out",
11393 Actual, Gen_Obj);
11394 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11395
11396 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11397 then
11398 Error_Msg_NE
11399 ("cannot instantiate nonvolatile formal & of mode in out",
11400 Actual, Gen_Obj);
11401 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11402
11403 elsif Is_Volatile_Full_Access_Object (Actual)
11404 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11405 then
11406 Error_Msg_NE
11407 ("cannot instantiate nonfull access formal & of mode in out",
11408 Actual, Gen_Obj);
11409 Error_Msg_N
11410 ("\with full access object actual (RM C.6(12))", Actual);
11411 end if;
11412
11413 -- Check for instantiation on nonatomic subcomponent of a full access
11414 -- object in Ada 2020 (RM C.6 (12)).
11415
11416 if Ada_Version >= Ada_2020
11417 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11418 and then not Is_Atomic_Object (Actual)
11419 then
11420 Error_Msg_NE
11421 ("cannot instantiate formal & of mode in out with actual",
11422 Actual, Gen_Obj);
11423 Error_Msg_N
11424 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11425 Actual);
11426 end if;
11427
11428 -- Check actual/formal compatibility with respect to the four
11429 -- volatility refinement aspects.
11430
11431 declare
11432 Actual_Obj : Entity_Id;
11433 N : Node_Id := Actual;
11434 begin
11435 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11436 -- pointer dereference like component selection.
11437 loop
11438 if Is_Entity_Name (N) then
11439 Actual_Obj := Entity (N);
11440 exit;
11441 end if;
11442
11443 case Nkind (N) is
11444 when N_Indexed_Component
11445 | N_Selected_Component
11446 | N_Slice
11447 | N_Explicit_Dereference
11448 =>
11449 N := Prefix (N);
11450
11451 when N_Type_Conversion =>
11452 N := Expression (N);
11453
11454 when others =>
11455 Actual_Obj := Etype (N);
11456 exit;
11457 end case;
11458 end loop;
11459
11460 Check_Volatility_Compatibility
11461 (Actual_Obj, A_Gen_Obj, "actual object",
11462 "its corresponding formal object of mode in out",
11463 Srcpos_Bearer => Actual);
11464 end;
11465
11466 -- Formal in-parameter
11467
11468 else
11469 -- The instantiation of a generic formal in-parameter is constant
11470 -- declaration. The actual is the expression for that declaration.
11471 -- Its type is a full copy of the type of the formal. This may be
11472 -- an access to subprogram, for which we need to generate entities
11473 -- for the formals in the new signature.
11474
11475 if Present (Actual) then
11476 if Present (Subt_Mark) then
11477 Def := New_Copy_Tree (Subt_Mark);
11478 else
11479 pragma Assert (Present (Acc_Def));
11480 Def := New_Copy_Tree (Acc_Def);
11481 end if;
11482
11483 Decl_Node :=
11484 Make_Object_Declaration (Loc,
11485 Defining_Identifier => New_Copy (Gen_Obj),
11486 Constant_Present => True,
11487 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11488 Object_Definition => Def,
11489 Expression => Actual);
11490
11491 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11492
11493 -- A generic formal object of a tagged type is defined to be
11494 -- aliased so the new constant must also be treated as aliased.
11495
11496 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11497 Set_Aliased_Present (Decl_Node);
11498 end if;
11499
11500 Append (Decl_Node, List);
11501
11502 -- No need to repeat (pre-)analysis of some expression nodes
11503 -- already handled in Preanalyze_Actuals.
11504
11505 if Nkind (Actual) /= N_Allocator then
11506 Analyze (Actual);
11507
11508 -- Return if the analysis of the actual reported some error
11509
11510 if Etype (Actual) = Any_Type then
11511 return List;
11512 end if;
11513 end if;
11514
11515 declare
11516 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11517 Typ : Entity_Id;
11518
11519 begin
11520 Typ := Get_Instance_Of (Formal_Type);
11521
11522 -- If the actual appears in the current or an enclosing scope,
11523 -- use its type directly. This is relevant if it has an actual
11524 -- subtype that is distinct from its nominal one. This cannot
11525 -- be done in general because the type of the actual may
11526 -- depend on other actuals, and only be fully determined when
11527 -- the enclosing instance is analyzed.
11528
11529 if Present (Etype (Actual))
11530 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11531 then
11532 Freeze_Before (Instantiation_Node, Etype (Actual));
11533 else
11534 Freeze_Before (Instantiation_Node, Typ);
11535 end if;
11536
11537 -- If the actual is an aggregate, perform name resolution on
11538 -- its components (the analysis of an aggregate does not do it)
11539 -- to capture local names that may be hidden if the generic is
11540 -- a child unit.
11541
11542 if Nkind (Actual) = N_Aggregate then
11543 Preanalyze_And_Resolve (Actual, Typ);
11544 end if;
11545
11546 if Is_Limited_Type (Typ)
11547 and then not OK_For_Limited_Init (Typ, Actual)
11548 then
11549 Error_Msg_N
11550 ("initialization not allowed for limited types", Actual);
11551 Explain_Limited_Type (Typ, Actual);
11552 end if;
11553 end;
11554
11555 elsif Present (Default_Expression (Formal)) then
11556
11557 -- Use default to construct declaration
11558
11559 if Present (Subt_Mark) then
11560 Def := New_Copy (Subt_Mark);
11561 else
11562 pragma Assert (Present (Acc_Def));
11563 Def := New_Copy_Tree (Acc_Def);
11564 end if;
11565
11566 Decl_Node :=
11567 Make_Object_Declaration (Sloc (Formal),
11568 Defining_Identifier => New_Copy (Gen_Obj),
11569 Constant_Present => True,
11570 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11571 Object_Definition => Def,
11572 Expression => New_Copy_Tree
11573 (Default_Expression (Formal)));
11574
11575 Set_Corresponding_Generic_Association
11576 (Decl_Node, Expression (Decl_Node));
11577
11578 Append (Decl_Node, List);
11579 Set_Analyzed (Expression (Decl_Node), False);
11580
11581 else
11582 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11583 Error_Msg_NE ("\in instantiation of & declared#",
11584 Instantiation_Node, Scope (A_Gen_Obj));
11585
11586 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11587
11588 -- Create dummy constant declaration so that instance can be
11589 -- analyzed, to minimize cascaded visibility errors.
11590
11591 if Present (Subt_Mark) then
11592 Def := Subt_Mark;
11593 else pragma Assert (Present (Acc_Def));
11594 Def := Acc_Def;
11595 end if;
11596
11597 Decl_Node :=
11598 Make_Object_Declaration (Loc,
11599 Defining_Identifier => New_Copy (Gen_Obj),
11600 Constant_Present => True,
11601 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11602 Object_Definition => New_Copy (Def),
11603 Expression =>
11604 Make_Attribute_Reference (Sloc (Gen_Obj),
11605 Attribute_Name => Name_First,
11606 Prefix => New_Copy (Def)));
11607
11608 Append (Decl_Node, List);
11609
11610 else
11611 Abandon_Instantiation (Instantiation_Node);
11612 end if;
11613 end if;
11614 end if;
11615
11616 if Nkind (Actual) in N_Has_Entity then
11617 Actual_Decl := Parent (Entity (Actual));
11618 end if;
11619
11620 -- Ada 2005 (AI-423) refined by AI12-0287:
11621 -- For an object_renaming_declaration with a null_exclusion or an
11622 -- access_definition that has a null_exclusion, the subtype of the
11623 -- object_name shall exclude null. In addition, if the
11624 -- object_renaming_declaration occurs within the body of a generic unit
11625 -- G or within the body of a generic unit declared within the
11626 -- declarative region of generic unit G, then:
11627 -- * if the object_name statically denotes a generic formal object of
11628 -- mode in out of G, then the declaration of that object shall have a
11629 -- null_exclusion;
11630 -- * if the object_name statically denotes a call of a generic formal
11631 -- function of G, then the declaration of the result of that function
11632 -- shall have a null_exclusion.
11633
11634 if Ada_Version >= Ada_2005
11635 and then Present (Actual_Decl)
11636 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11637 | N_Object_Declaration
11638 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11639 and then not Has_Null_Exclusion (Actual_Decl)
11640 and then Has_Null_Exclusion (Analyzed_Formal)
11641 and then Ekind (Defining_Identifier (Analyzed_Formal))
11642 = E_Generic_In_Out_Parameter
11643 and then ((In_Generic_Scope (Entity (Actual))
11644 and then In_Package_Body (Scope (Entity (Actual))))
11645 or else not Can_Never_Be_Null (Etype (Actual)))
11646 then
11647 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11648 Error_Msg_N
11649 ("actual must exclude null to match generic formal#", Actual);
11650 end if;
11651
11652 -- An effectively volatile object cannot be used as an actual in a
11653 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11654 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11655 -- legality rule, and also verifies that the actual is an object.
11656
11657 if SPARK_Mode = On
11658 and then Present (Actual)
11659 and then Is_Object_Reference (Actual)
11660 and then Is_Effectively_Volatile_Object (Actual)
11661 and then not Is_Effectively_Volatile (A_Gen_Obj)
11662 then
11663 Error_Msg_N
11664 ("volatile object cannot act as actual in generic instantiation",
11665 Actual);
11666 end if;
11667
11668 return List;
11669 end Instantiate_Object;
11670
11671 ------------------------------
11672 -- Instantiate_Package_Body --
11673 ------------------------------
11674
11675 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11676 -- must be replaced by gotos which jump to the end of the routine in order
11677 -- to restore the Ghost and SPARK modes.
11678
11679 procedure Instantiate_Package_Body
11680 (Body_Info : Pending_Body_Info;
11681 Inlined_Body : Boolean := False;
11682 Body_Optional : Boolean := False)
11683 is
11684 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11685 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11686 Act_Spec : constant Node_Id := Specification (Act_Decl);
11687 Ctx_Parents : Elist_Id := No_Elist;
11688 Ctx_Top : Int := 0;
11689 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11690 Gen_Id : constant Node_Id := Name (Inst_Node);
11691 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11692 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11693 Loc : constant Source_Ptr := Sloc (Inst_Node);
11694
11695 procedure Check_Initialized_Types;
11696 -- In a generic package body, an entity of a generic private type may
11697 -- appear uninitialized. This is suspicious, unless the actual is a
11698 -- fully initialized type.
11699
11700 procedure Install_Parents_Of_Generic_Context
11701 (Inst_Scope : Entity_Id;
11702 Ctx_Parents : out Elist_Id);
11703 -- Inst_Scope is the scope where the instance appears within; when it
11704 -- appears within a generic child package G, this routine collects and
11705 -- installs the enclosing packages of G in the scopes stack; installed
11706 -- packages are returned in Ctx_Parents.
11707
11708 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11709 -- Reverse effect after instantiation is complete
11710
11711 -----------------------------
11712 -- Check_Initialized_Types --
11713 -----------------------------
11714
11715 procedure Check_Initialized_Types is
11716 Decl : Node_Id;
11717 Formal : Entity_Id;
11718 Actual : Entity_Id;
11719 Uninit_Var : Entity_Id;
11720
11721 begin
11722 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11723 while Present (Decl) loop
11724 Uninit_Var := Empty;
11725
11726 if Nkind (Decl) = N_Private_Extension_Declaration then
11727 Uninit_Var := Uninitialized_Variable (Decl);
11728
11729 elsif Nkind (Decl) = N_Formal_Type_Declaration
11730 and then Nkind (Formal_Type_Definition (Decl)) =
11731 N_Formal_Private_Type_Definition
11732 then
11733 Uninit_Var :=
11734 Uninitialized_Variable (Formal_Type_Definition (Decl));
11735 end if;
11736
11737 if Present (Uninit_Var) then
11738 Formal := Defining_Identifier (Decl);
11739 Actual := First_Entity (Act_Decl_Id);
11740
11741 -- For each formal there is a subtype declaration that renames
11742 -- the actual and has the same name as the formal. Locate the
11743 -- formal for warning message about uninitialized variables
11744 -- in the generic, for which the actual type should be a fully
11745 -- initialized type.
11746
11747 while Present (Actual) loop
11748 exit when Ekind (Actual) = E_Package
11749 and then Present (Renamed_Object (Actual));
11750
11751 if Chars (Actual) = Chars (Formal)
11752 and then not Is_Scalar_Type (Actual)
11753 and then not Is_Fully_Initialized_Type (Actual)
11754 and then Warn_On_No_Value_Assigned
11755 then
11756 Error_Msg_Node_2 := Formal;
11757 Error_Msg_NE
11758 ("generic unit has uninitialized variable& of "
11759 & "formal private type &?v?", Actual, Uninit_Var);
11760 Error_Msg_NE
11761 ("actual type for& should be fully initialized type?v?",
11762 Actual, Formal);
11763 exit;
11764 end if;
11765
11766 Next_Entity (Actual);
11767 end loop;
11768 end if;
11769
11770 Next (Decl);
11771 end loop;
11772 end Check_Initialized_Types;
11773
11774 ----------------------------------------
11775 -- Install_Parents_Of_Generic_Context --
11776 ----------------------------------------
11777
11778 procedure Install_Parents_Of_Generic_Context
11779 (Inst_Scope : Entity_Id;
11780 Ctx_Parents : out Elist_Id)
11781 is
11782 Elmt : Elmt_Id;
11783 S : Entity_Id;
11784
11785 begin
11786 Ctx_Parents := New_Elmt_List;
11787
11788 -- Collect context parents (ie. parents where the instantiation
11789 -- appears within).
11790
11791 S := Inst_Scope;
11792 while S /= Standard_Standard loop
11793 Prepend_Elmt (S, Ctx_Parents);
11794 S := Scope (S);
11795 end loop;
11796
11797 -- Install enclosing parents
11798
11799 Elmt := First_Elmt (Ctx_Parents);
11800 while Present (Elmt) loop
11801 Push_Scope (Node (Elmt));
11802 Set_Is_Immediately_Visible (Node (Elmt));
11803 Next_Elmt (Elmt);
11804 end loop;
11805 end Install_Parents_Of_Generic_Context;
11806
11807 ---------------------------------------
11808 -- Remove_Parents_Of_Generic_Context --
11809 ---------------------------------------
11810
11811 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11812 Elmt : Elmt_Id;
11813
11814 begin
11815 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11816
11817 Elmt := Last_Elmt (Ctx_Parents);
11818 while Present (Elmt) loop
11819 pragma Assert (Current_Scope = Node (Elmt));
11820 Set_Is_Immediately_Visible (Current_Scope, False);
11821 Pop_Scope;
11822
11823 Remove_Last_Elmt (Ctx_Parents);
11824 Elmt := Last_Elmt (Ctx_Parents);
11825 end loop;
11826 end Remove_Parents_Of_Generic_Context;
11827
11828 -- Local variables
11829
11830 -- The following constants capture the context prior to instantiating
11831 -- the package body.
11832
11833 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11834 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11835 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11836 Saved_ISMP : constant Boolean :=
11837 Ignore_SPARK_Mode_Pragmas_In_Instance;
11838 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11839 Local_Suppress_Stack_Top;
11840 Saved_SC : constant Boolean := Style_Check;
11841 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11842 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11843 Saved_SS : constant Suppress_Record := Scope_Suppress;
11844 Saved_Warn : constant Warning_Record := Save_Warnings;
11845
11846 Act_Body : Node_Id;
11847 Act_Body_Id : Entity_Id;
11848 Act_Body_Name : Node_Id;
11849 Gen_Body : Node_Id;
11850 Gen_Body_Id : Node_Id;
11851 Par_Ent : Entity_Id := Empty;
11852 Par_Installed : Boolean := False;
11853 Par_Vis : Boolean := False;
11854
11855 Scope_Check_Id : Entity_Id;
11856 Scope_Check_Last : Nat;
11857 -- Value of Current_Scope before calls to Install_Parents; used to check
11858 -- that scopes are correctly removed after instantiation.
11859
11860 Vis_Prims_List : Elist_Id := No_Elist;
11861 -- List of primitives made temporarily visible in the instantiation
11862 -- to match the visibility of the formal type.
11863
11864 -- Start of processing for Instantiate_Package_Body
11865
11866 begin
11867 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11868
11869 -- The instance body may already have been processed, as the parent of
11870 -- another instance that is inlined (Load_Parent_Of_Generic).
11871
11872 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11873 return;
11874 end if;
11875
11876 -- The package being instantiated may be subject to pragma Ghost. Set
11877 -- the mode now to ensure that any nodes generated during instantiation
11878 -- are properly marked as Ghost.
11879
11880 Set_Ghost_Mode (Act_Decl_Id);
11881
11882 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11883
11884 -- Re-establish the state of information on which checks are suppressed.
11885 -- This information was set in Body_Info at the point of instantiation,
11886 -- and now we restore it so that the instance is compiled using the
11887 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11888
11889 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11890 Scope_Suppress := Body_Info.Scope_Suppress;
11891
11892 Restore_Config_Switches (Body_Info.Config_Switches);
11893 Restore_Warnings (Body_Info.Warnings);
11894
11895 if No (Gen_Body_Id) then
11896
11897 -- Do not look for parent of generic body if none is required.
11898 -- This may happen when the routine is called as part of the
11899 -- Pending_Instantiations processing, when nested instances
11900 -- may precede the one generated from the main unit.
11901
11902 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11903 and then Body_Optional
11904 then
11905 goto Leave;
11906 else
11907 Load_Parent_Of_Generic
11908 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11909
11910 -- Surprisingly enough, loading the body of the parent can cause
11911 -- the body to be instantiated and the double instantiation needs
11912 -- to be prevented in order to avoid giving bogus semantic errors.
11913
11914 -- This case can occur because of the Collect_Previous_Instances
11915 -- machinery of Load_Parent_Of_Generic, which will instantiate
11916 -- bodies that are deemed to be ahead of the body of the parent
11917 -- in the compilation unit. But the relative position of these
11918 -- bodies is computed using the mere comparison of their Sloc.
11919
11920 -- Now suppose that you have two generic packages G and H, with
11921 -- G containing a mere instantiation of H:
11922
11923 -- generic
11924 -- package H is
11925
11926 -- generic
11927 -- package Nested_G is
11928 -- ...
11929 -- end Nested_G;
11930
11931 -- end H;
11932
11933 -- with H;
11934
11935 -- generic
11936 -- package G is
11937
11938 -- package My_H is new H;
11939
11940 -- end G;
11941
11942 -- and a third package Q instantiating G and Nested_G:
11943
11944 -- with G;
11945
11946 -- package Q is
11947
11948 -- package My_G is new G;
11949
11950 -- package My_Nested_G is new My_G.My_H.Nested_G;
11951
11952 -- end Q;
11953
11954 -- The body to be instantiated is that of My_Nested_G and its
11955 -- parent is the instance My_G.My_H. This latter instantiation
11956 -- is done when My_G is analyzed, i.e. after the declarations
11957 -- of My_G and My_Nested_G have been parsed; as a result, the
11958 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11959
11960 -- Therefore loading the body of My_G.My_H will cause the body
11961 -- of My_Nested_G to be instantiated because it is deemed to be
11962 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11963 -- will again be invoked on My_G.My_H, but this time with the
11964 -- Collect_Previous_Instances machinery disabled, so there is
11965 -- no endless mutual recursion and things are done in order.
11966
11967 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11968 goto Leave;
11969 end if;
11970
11971 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11972 end if;
11973 end if;
11974
11975 -- Establish global variable for sloc adjustment and for error recovery
11976 -- In the case of an instance body for an instantiation with actuals
11977 -- from a limited view, the instance body is placed at the beginning
11978 -- of the enclosing package body: use the body entity as the source
11979 -- location for nodes of the instance body.
11980
11981 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11982 declare
11983 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11984 Body_Id : constant Node_Id :=
11985 Corresponding_Body (Unit_Declaration_Node (Scop));
11986
11987 begin
11988 Instantiation_Node := Body_Id;
11989 end;
11990 else
11991 Instantiation_Node := Inst_Node;
11992 end if;
11993
11994 if Present (Gen_Body_Id) then
11995 Save_Env (Gen_Unit, Act_Decl_Id);
11996 Style_Check := False;
11997
11998 -- If the context of the instance is subject to SPARK_Mode "off", the
11999 -- annotation is missing, or the body is instantiated at a later pass
12000 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12001 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12002 -- instance.
12003
12004 if SPARK_Mode /= On
12005 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12006 then
12007 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12008 end if;
12009
12010 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12011 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12012
12013 Create_Instantiation_Source
12014 (Inst_Node, Gen_Body_Id, S_Adjustment);
12015
12016 Act_Body :=
12017 Copy_Generic_Node
12018 (Original_Node (Gen_Body), Empty, Instantiating => True);
12019
12020 -- Create proper (possibly qualified) defining name for the body, to
12021 -- correspond to the one in the spec.
12022
12023 Act_Body_Id :=
12024 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12025 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12026
12027 -- Some attributes of spec entity are not inherited by body entity
12028
12029 Set_Handler_Records (Act_Body_Id, No_List);
12030
12031 if Nkind (Defining_Unit_Name (Act_Spec)) =
12032 N_Defining_Program_Unit_Name
12033 then
12034 Act_Body_Name :=
12035 Make_Defining_Program_Unit_Name (Loc,
12036 Name =>
12037 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12038 Defining_Identifier => Act_Body_Id);
12039 else
12040 Act_Body_Name := Act_Body_Id;
12041 end if;
12042
12043 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12044
12045 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12046 Check_Generic_Actuals (Act_Decl_Id, False);
12047 Check_Initialized_Types;
12048
12049 -- Install primitives hidden at the point of the instantiation but
12050 -- visible when processing the generic formals
12051
12052 declare
12053 E : Entity_Id;
12054
12055 begin
12056 E := First_Entity (Act_Decl_Id);
12057 while Present (E) loop
12058 if Is_Type (E)
12059 and then not Is_Itype (E)
12060 and then Is_Generic_Actual_Type (E)
12061 and then Is_Tagged_Type (E)
12062 then
12063 Install_Hidden_Primitives
12064 (Prims_List => Vis_Prims_List,
12065 Gen_T => Generic_Parent_Type (Parent (E)),
12066 Act_T => E);
12067 end if;
12068
12069 Next_Entity (E);
12070 end loop;
12071 end;
12072
12073 Scope_Check_Id := Current_Scope;
12074 Scope_Check_Last := Scope_Stack.Last;
12075
12076 -- If the instantiation appears within a generic child some actual
12077 -- parameter may be the current instance of the enclosing generic
12078 -- parent.
12079
12080 declare
12081 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12082
12083 begin
12084 if Is_Child_Unit (Inst_Scope)
12085 and then Ekind (Inst_Scope) = E_Generic_Package
12086 and then Present (Generic_Associations (Inst_Node))
12087 then
12088 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12089
12090 -- Hide them from visibility; required to avoid conflicts
12091 -- installing the parent instance.
12092
12093 if Present (Ctx_Parents) then
12094 Push_Scope (Standard_Standard);
12095 Ctx_Top := Scope_Stack.Last;
12096 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12097 end if;
12098 end if;
12099 end;
12100
12101 -- If it is a child unit, make the parent instance (which is an
12102 -- instance of the parent of the generic) visible. The parent
12103 -- instance is the prefix of the name of the generic unit.
12104
12105 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12106 and then Nkind (Gen_Id) = N_Expanded_Name
12107 then
12108 Par_Ent := Entity (Prefix (Gen_Id));
12109 Par_Vis := Is_Immediately_Visible (Par_Ent);
12110 Install_Parent (Par_Ent, In_Body => True);
12111 Par_Installed := True;
12112
12113 elsif Is_Child_Unit (Gen_Unit) then
12114 Par_Ent := Scope (Gen_Unit);
12115 Par_Vis := Is_Immediately_Visible (Par_Ent);
12116 Install_Parent (Par_Ent, In_Body => True);
12117 Par_Installed := True;
12118 end if;
12119
12120 -- If the instantiation is a library unit, and this is the main unit,
12121 -- then build the resulting compilation unit nodes for the instance.
12122 -- If this is a compilation unit but it is not the main unit, then it
12123 -- is the body of a unit in the context, that is being compiled
12124 -- because it is encloses some inlined unit or another generic unit
12125 -- being instantiated. In that case, this body is not part of the
12126 -- current compilation, and is not attached to the tree, but its
12127 -- parent must be set for analysis.
12128
12129 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12130
12131 -- Replace instance node with body of instance, and create new
12132 -- node for corresponding instance declaration.
12133
12134 Build_Instance_Compilation_Unit_Nodes
12135 (Inst_Node, Act_Body, Act_Decl);
12136
12137 -- If the instantiation appears within a generic child package
12138 -- enable visibility of current instance of enclosing generic
12139 -- parents.
12140
12141 if Present (Ctx_Parents) then
12142 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12143 Analyze (Inst_Node);
12144 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12145 else
12146 Analyze (Inst_Node);
12147 end if;
12148
12149 if Parent (Inst_Node) = Cunit (Main_Unit) then
12150
12151 -- If the instance is a child unit itself, then set the scope
12152 -- of the expanded body to be the parent of the instantiation
12153 -- (ensuring that the fully qualified name will be generated
12154 -- for the elaboration subprogram).
12155
12156 if Nkind (Defining_Unit_Name (Act_Spec)) =
12157 N_Defining_Program_Unit_Name
12158 then
12159 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12160 end if;
12161 end if;
12162
12163 -- Case where instantiation is not a library unit
12164
12165 else
12166 -- If this is an early instantiation, i.e. appears textually
12167 -- before the corresponding body and must be elaborated first,
12168 -- indicate that the body instance is to be delayed.
12169
12170 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
12171
12172 -- If the instantiation appears within a generic child package
12173 -- enable visibility of current instance of enclosing generic
12174 -- parents.
12175
12176 if Present (Ctx_Parents) then
12177 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12178 Analyze (Act_Body);
12179 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12180 else
12181 Analyze (Act_Body);
12182 end if;
12183 end if;
12184
12185 Inherit_Context (Gen_Body, Inst_Node);
12186
12187 if Par_Installed then
12188 Remove_Parent (In_Body => True);
12189
12190 -- Restore the previous visibility of the parent
12191
12192 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12193 end if;
12194
12195 -- Remove the parent instances if they have been placed on the scope
12196 -- stack to compile the body.
12197
12198 if Present (Ctx_Parents) then
12199 pragma Assert (Scope_Stack.Last = Ctx_Top
12200 and then Current_Scope = Standard_Standard);
12201 Pop_Scope;
12202
12203 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12204 end if;
12205
12206 pragma Assert (Current_Scope = Scope_Check_Id);
12207 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12208
12209 Restore_Hidden_Primitives (Vis_Prims_List);
12210
12211 -- Restore the private views that were made visible when the body of
12212 -- the instantiation was created. Note that, in the case where one of
12213 -- these private views is declared in the parent, there is a nesting
12214 -- issue with the calls to Install_Parent and Remove_Parent made in
12215 -- between above with In_Body set to True, because these calls also
12216 -- want to swap and restore this private view respectively. In this
12217 -- case, the call to Install_Parent does nothing, but the call to
12218 -- Remove_Parent does restore the private view, thus undercutting the
12219 -- call to Restore_Private_Views. That's OK under the condition that
12220 -- the two mechanisms swap exactly the same entities, in particular
12221 -- the private entities dependent on the primary private entities.
12222
12223 Restore_Private_Views (Act_Decl_Id);
12224
12225 -- Remove the current unit from visibility if this is an instance
12226 -- that is not elaborated on the fly for inlining purposes.
12227
12228 if not Inlined_Body then
12229 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12230 end if;
12231
12232 Restore_Env;
12233
12234 -- If we have no body, and the unit requires a body, then complain. This
12235 -- complaint is suppressed if we have detected other errors (since a
12236 -- common reason for missing the body is that it had errors).
12237 -- In CodePeer mode, a warning has been emitted already, no need for
12238 -- further messages.
12239
12240 elsif Unit_Requires_Body (Gen_Unit)
12241 and then not Body_Optional
12242 then
12243 if CodePeer_Mode then
12244 null;
12245
12246 elsif Serious_Errors_Detected = 0 then
12247 Error_Msg_NE
12248 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12249
12250 -- Don't attempt to perform any cleanup actions if some other error
12251 -- was already detected, since this can cause blowups.
12252
12253 else
12254 goto Leave;
12255 end if;
12256
12257 -- Case of package that does not need a body
12258
12259 else
12260 -- If the instantiation of the declaration is a library unit, rewrite
12261 -- the original package instantiation as a package declaration in the
12262 -- compilation unit node.
12263
12264 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12265 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12266 Rewrite (Inst_Node, Act_Decl);
12267
12268 -- Generate elaboration entity, in case spec has elaboration code.
12269 -- This cannot be done when the instance is analyzed, because it
12270 -- is not known yet whether the body exists.
12271
12272 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12273 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12274
12275 -- If the instantiation is not a library unit, then append the
12276 -- declaration to the list of implicitly generated entities, unless
12277 -- it is already a list member which means that it was already
12278 -- processed
12279
12280 elsif not Is_List_Member (Act_Decl) then
12281 Mark_Rewrite_Insertion (Act_Decl);
12282 Insert_Before (Inst_Node, Act_Decl);
12283 end if;
12284 end if;
12285
12286 <<Leave>>
12287
12288 -- Restore the context that was in effect prior to instantiating the
12289 -- package body.
12290
12291 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12292 Local_Suppress_Stack_Top := Saved_LSST;
12293 Scope_Suppress := Saved_SS;
12294 Style_Check := Saved_SC;
12295
12296 Expander_Mode_Restore;
12297 Restore_Config_Switches (Saved_CS);
12298 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12299 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12300 Restore_Warnings (Saved_Warn);
12301 end Instantiate_Package_Body;
12302
12303 ---------------------------------
12304 -- Instantiate_Subprogram_Body --
12305 ---------------------------------
12306
12307 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12308 -- must be replaced by gotos which jump to the end of the routine in order
12309 -- to restore the Ghost and SPARK modes.
12310
12311 procedure Instantiate_Subprogram_Body
12312 (Body_Info : Pending_Body_Info;
12313 Body_Optional : Boolean := False)
12314 is
12315 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12316 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12317 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12318 Gen_Id : constant Node_Id := Name (Inst_Node);
12319 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12320 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12321 Loc : constant Source_Ptr := Sloc (Inst_Node);
12322 Pack_Id : constant Entity_Id :=
12323 Defining_Unit_Name (Parent (Act_Decl));
12324
12325 -- The following constants capture the context prior to instantiating
12326 -- the subprogram body.
12327
12328 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12329 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12330 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12331 Saved_ISMP : constant Boolean :=
12332 Ignore_SPARK_Mode_Pragmas_In_Instance;
12333 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12334 Local_Suppress_Stack_Top;
12335 Saved_SC : constant Boolean := Style_Check;
12336 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12337 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12338 Saved_SS : constant Suppress_Record := Scope_Suppress;
12339 Saved_Warn : constant Warning_Record := Save_Warnings;
12340
12341 Act_Body : Node_Id;
12342 Act_Body_Id : Entity_Id;
12343 Gen_Body : Node_Id;
12344 Gen_Body_Id : Node_Id;
12345 Pack_Body : Node_Id;
12346 Par_Ent : Entity_Id := Empty;
12347 Par_Installed : Boolean := False;
12348 Par_Vis : Boolean := False;
12349 Ret_Expr : Node_Id;
12350
12351 begin
12352 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12353
12354 -- Subprogram body may have been created already because of an inline
12355 -- pragma, or because of multiple elaborations of the enclosing package
12356 -- when several instances of the subprogram appear in the main unit.
12357
12358 if Present (Corresponding_Body (Act_Decl)) then
12359 return;
12360 end if;
12361
12362 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12363 -- the mode now to ensure that any nodes generated during instantiation
12364 -- are properly marked as Ghost.
12365
12366 Set_Ghost_Mode (Act_Decl_Id);
12367
12368 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12369
12370 -- Re-establish the state of information on which checks are suppressed.
12371 -- This information was set in Body_Info at the point of instantiation,
12372 -- and now we restore it so that the instance is compiled using the
12373 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12374
12375 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12376 Scope_Suppress := Body_Info.Scope_Suppress;
12377
12378 Restore_Config_Switches (Body_Info.Config_Switches);
12379 Restore_Warnings (Body_Info.Warnings);
12380
12381 if No (Gen_Body_Id) then
12382
12383 -- For imported generic subprogram, no body to compile, complete
12384 -- the spec entity appropriately.
12385
12386 if Is_Imported (Gen_Unit) then
12387 Set_Is_Imported (Act_Decl_Id);
12388 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12389 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12390 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12391 Set_Has_Completion (Act_Decl_Id);
12392 goto Leave;
12393
12394 -- For other cases, compile the body
12395
12396 else
12397 Load_Parent_Of_Generic
12398 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12399 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12400 end if;
12401 end if;
12402
12403 Instantiation_Node := Inst_Node;
12404
12405 if Present (Gen_Body_Id) then
12406 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12407
12408 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12409
12410 -- Either body is not present, or context is non-expanding, as
12411 -- when compiling a subunit. Mark the instance as completed, and
12412 -- diagnose a missing body when needed.
12413
12414 if Expander_Active
12415 and then Operating_Mode = Generate_Code
12416 then
12417 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12418 end if;
12419
12420 Set_Has_Completion (Act_Decl_Id);
12421 goto Leave;
12422 end if;
12423
12424 Save_Env (Gen_Unit, Act_Decl_Id);
12425 Style_Check := False;
12426
12427 -- If the context of the instance is subject to SPARK_Mode "off", the
12428 -- annotation is missing, or the body is instantiated at a later pass
12429 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12430 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12431 -- instance.
12432
12433 if SPARK_Mode /= On
12434 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12435 then
12436 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12437 end if;
12438
12439 -- If the context of an instance is not subject to SPARK_Mode "off",
12440 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12441 -- the latter should be the one applicable to the instance.
12442
12443 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12444 and then SPARK_Mode /= Off
12445 and then Present (SPARK_Pragma (Gen_Body_Id))
12446 then
12447 Set_SPARK_Mode (Gen_Body_Id);
12448 end if;
12449
12450 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12451 Create_Instantiation_Source
12452 (Inst_Node,
12453 Gen_Body_Id,
12454 S_Adjustment);
12455
12456 Act_Body :=
12457 Copy_Generic_Node
12458 (Original_Node (Gen_Body), Empty, Instantiating => True);
12459
12460 -- Create proper defining name for the body, to correspond to the one
12461 -- in the spec.
12462
12463 Act_Body_Id :=
12464 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12465
12466 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12467 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12468
12469 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12470 Set_Has_Completion (Act_Decl_Id);
12471 Check_Generic_Actuals (Pack_Id, False);
12472
12473 -- Generate a reference to link the visible subprogram instance to
12474 -- the generic body, which for navigation purposes is the only
12475 -- available source for the instance.
12476
12477 Generate_Reference
12478 (Related_Instance (Pack_Id),
12479 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12480
12481 -- If it is a child unit, make the parent instance (which is an
12482 -- instance of the parent of the generic) visible. The parent
12483 -- instance is the prefix of the name of the generic unit.
12484
12485 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12486 and then Nkind (Gen_Id) = N_Expanded_Name
12487 then
12488 Par_Ent := Entity (Prefix (Gen_Id));
12489 Par_Vis := Is_Immediately_Visible (Par_Ent);
12490 Install_Parent (Par_Ent, In_Body => True);
12491 Par_Installed := True;
12492
12493 elsif Is_Child_Unit (Gen_Unit) then
12494 Par_Ent := Scope (Gen_Unit);
12495 Par_Vis := Is_Immediately_Visible (Par_Ent);
12496 Install_Parent (Par_Ent, In_Body => True);
12497 Par_Installed := True;
12498 end if;
12499
12500 -- Subprogram body is placed in the body of wrapper package,
12501 -- whose spec contains the subprogram declaration as well as
12502 -- the renaming declarations for the generic parameters.
12503
12504 Pack_Body :=
12505 Make_Package_Body (Loc,
12506 Defining_Unit_Name => New_Copy (Pack_Id),
12507 Declarations => New_List (Act_Body));
12508
12509 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12510
12511 -- If the instantiation is a library unit, then build resulting
12512 -- compilation unit nodes for the instance. The declaration of
12513 -- the enclosing package is the grandparent of the subprogram
12514 -- declaration. First replace the instantiation node as the unit
12515 -- of the corresponding compilation.
12516
12517 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12518 if Parent (Inst_Node) = Cunit (Main_Unit) then
12519 Set_Unit (Parent (Inst_Node), Inst_Node);
12520 Build_Instance_Compilation_Unit_Nodes
12521 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12522 Analyze (Inst_Node);
12523 else
12524 Set_Parent (Pack_Body, Parent (Inst_Node));
12525 Analyze (Pack_Body);
12526 end if;
12527
12528 else
12529 Insert_Before (Inst_Node, Pack_Body);
12530 Mark_Rewrite_Insertion (Pack_Body);
12531 Analyze (Pack_Body);
12532
12533 if Expander_Active then
12534 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
12535 end if;
12536 end if;
12537
12538 Inherit_Context (Gen_Body, Inst_Node);
12539
12540 Restore_Private_Views (Pack_Id, False);
12541
12542 if Par_Installed then
12543 Remove_Parent (In_Body => True);
12544
12545 -- Restore the previous visibility of the parent
12546
12547 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12548 end if;
12549
12550 Restore_Env;
12551
12552 -- Body not found. Error was emitted already. If there were no previous
12553 -- errors, this may be an instance whose scope is a premature instance.
12554 -- In that case we must insure that the (legal) program does raise
12555 -- program error if executed. We generate a subprogram body for this
12556 -- purpose. See DEC ac30vso.
12557
12558 -- Should not reference proprietary DEC tests in comments ???
12559
12560 elsif Serious_Errors_Detected = 0
12561 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12562 then
12563 if Body_Optional then
12564 goto Leave;
12565
12566 elsif Ekind (Act_Decl_Id) = E_Procedure then
12567 Act_Body :=
12568 Make_Subprogram_Body (Loc,
12569 Specification =>
12570 Make_Procedure_Specification (Loc,
12571 Defining_Unit_Name =>
12572 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12573 Parameter_Specifications =>
12574 New_Copy_List
12575 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12576
12577 Declarations => Empty_List,
12578 Handled_Statement_Sequence =>
12579 Make_Handled_Sequence_Of_Statements (Loc,
12580 Statements => New_List (
12581 Make_Raise_Program_Error (Loc,
12582 Reason => PE_Access_Before_Elaboration))));
12583
12584 else
12585 Ret_Expr :=
12586 Make_Raise_Program_Error (Loc,
12587 Reason => PE_Access_Before_Elaboration);
12588
12589 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12590 Set_Analyzed (Ret_Expr);
12591
12592 Act_Body :=
12593 Make_Subprogram_Body (Loc,
12594 Specification =>
12595 Make_Function_Specification (Loc,
12596 Defining_Unit_Name =>
12597 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12598 Parameter_Specifications =>
12599 New_Copy_List
12600 (Parameter_Specifications (Parent (Act_Decl_Id))),
12601 Result_Definition =>
12602 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12603
12604 Declarations => Empty_List,
12605 Handled_Statement_Sequence =>
12606 Make_Handled_Sequence_Of_Statements (Loc,
12607 Statements => New_List (
12608 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12609 end if;
12610
12611 Pack_Body :=
12612 Make_Package_Body (Loc,
12613 Defining_Unit_Name => New_Copy (Pack_Id),
12614 Declarations => New_List (Act_Body));
12615
12616 Insert_After (Inst_Node, Pack_Body);
12617 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12618 Analyze (Pack_Body);
12619 end if;
12620
12621 <<Leave>>
12622
12623 -- Restore the context that was in effect prior to instantiating the
12624 -- subprogram body.
12625
12626 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12627 Local_Suppress_Stack_Top := Saved_LSST;
12628 Scope_Suppress := Saved_SS;
12629 Style_Check := Saved_SC;
12630
12631 Expander_Mode_Restore;
12632 Restore_Config_Switches (Saved_CS);
12633 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12634 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12635 Restore_Warnings (Saved_Warn);
12636 end Instantiate_Subprogram_Body;
12637
12638 ----------------------
12639 -- Instantiate_Type --
12640 ----------------------
12641
12642 function Instantiate_Type
12643 (Formal : Node_Id;
12644 Actual : Node_Id;
12645 Analyzed_Formal : Node_Id;
12646 Actual_Decls : List_Id) return List_Id
12647 is
12648 A_Gen_T : constant Entity_Id :=
12649 Defining_Identifier (Analyzed_Formal);
12650 Def : constant Node_Id := Formal_Type_Definition (Formal);
12651 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12652 Act_T : Entity_Id;
12653 Ancestor : Entity_Id := Empty;
12654 Decl_Node : Node_Id;
12655 Decl_Nodes : List_Id;
12656 Loc : Source_Ptr;
12657 Subt : Entity_Id;
12658
12659 procedure Check_Shared_Variable_Control_Aspects;
12660 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12661 -- that may be specified for a formal type are obeyed by the actual.
12662
12663 procedure Diagnose_Predicated_Actual;
12664 -- There are a number of constructs in which a discrete type with
12665 -- predicates is illegal, e.g. as an index in an array type declaration.
12666 -- If a generic type is used is such a construct in a generic package
12667 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12668 -- of the generic contract that the actual cannot have predicates.
12669
12670 procedure Validate_Array_Type_Instance;
12671 procedure Validate_Access_Subprogram_Instance;
12672 procedure Validate_Access_Type_Instance;
12673 procedure Validate_Derived_Type_Instance;
12674 procedure Validate_Derived_Interface_Type_Instance;
12675 procedure Validate_Discriminated_Formal_Type;
12676 procedure Validate_Interface_Type_Instance;
12677 procedure Validate_Private_Type_Instance;
12678 procedure Validate_Incomplete_Type_Instance;
12679 -- These procedures perform validation tests for the named case.
12680 -- Validate_Discriminated_Formal_Type is shared by formal private
12681 -- types and Ada 2012 formal incomplete types.
12682
12683 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12684 -- Check that base types are the same and that the subtypes match
12685 -- statically. Used in several of the above.
12686
12687 --------------------------------------------
12688 -- Check_Shared_Variable_Control_Aspects --
12689 --------------------------------------------
12690
12691 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12692 -- that may be specified for the formal are obeyed by the actual.
12693 -- If the formal is a derived type the aspect specifications must match.
12694 -- NOTE: AI12-0282 implies that matching of aspects is required between
12695 -- formal and actual in all cases, but this is too restrictive.
12696 -- In particular it violates a language design rule: a limited private
12697 -- indefinite formal can be matched by any actual. The current code
12698 -- reflects an older and more permissive version of RM C.6 (12/5).
12699
12700 procedure Check_Shared_Variable_Control_Aspects is
12701 begin
12702 if Ada_Version >= Ada_2020 then
12703 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12704 Error_Msg_NE
12705 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12706
12707 elsif Is_Derived_Type (A_Gen_T)
12708 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12709 then
12710 Error_Msg_NE
12711 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12712 end if;
12713
12714 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12715 Error_Msg_NE
12716 ("actual for& must have Volatile aspect",
12717 Actual, A_Gen_T);
12718
12719 elsif Is_Derived_Type (A_Gen_T)
12720 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12721 then
12722 Error_Msg_NE
12723 ("actual for& has different Volatile aspect",
12724 Actual, A_Gen_T);
12725 end if;
12726
12727 -- We assume that an array type whose atomic component type
12728 -- is Atomic is equivalent to an array type with the explicit
12729 -- aspect Has_Atomic_Components. This is a reasonable inference
12730 -- from the intent of AI12-0282, and makes it legal to use an
12731 -- actual that does not have the identical aspect as the formal.
12732 -- Ditto for volatile components.
12733
12734 declare
12735 Actual_Atomic_Comp : constant Boolean :=
12736 Has_Atomic_Components (Act_T)
12737 or else (Is_Array_Type (Act_T)
12738 and then Is_Atomic (Component_Type (Act_T)));
12739 begin
12740 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12741 Error_Msg_NE
12742 ("formal and actual for& must agree on atomic components",
12743 Actual, A_Gen_T);
12744 end if;
12745 end;
12746
12747 declare
12748 Actual_Volatile_Comp : constant Boolean :=
12749 Has_Volatile_Components (Act_T)
12750 or else (Is_Array_Type (Act_T)
12751 and then Is_Volatile (Component_Type (Act_T)));
12752 begin
12753 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12754 then
12755 Error_Msg_NE
12756 ("actual for& must have volatile components",
12757 Actual, A_Gen_T);
12758 end if;
12759 end;
12760
12761 -- The following two aspects do not require exact matching,
12762 -- but only one-way agreement. See RM C.6.
12763
12764 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12765 then
12766 Error_Msg_NE
12767 ("actual for& must have Independent aspect specified",
12768 Actual, A_Gen_T);
12769 end if;
12770
12771 if Has_Independent_Components (A_Gen_T)
12772 and then not Has_Independent_Components (Act_T)
12773 then
12774 Error_Msg_NE
12775 ("actual for& must have Independent_Components specified",
12776 Actual, A_Gen_T);
12777 end if;
12778
12779 -- Check actual/formal compatibility with respect to the four
12780 -- volatility refinement aspects.
12781
12782 Check_Volatility_Compatibility
12783 (Act_T, A_Gen_T,
12784 "actual type", "its corresponding formal type",
12785 Srcpos_Bearer => Act_T);
12786 end if;
12787 end Check_Shared_Variable_Control_Aspects;
12788
12789 ---------------------------------
12790 -- Diagnose_Predicated_Actual --
12791 ---------------------------------
12792
12793 procedure Diagnose_Predicated_Actual is
12794 begin
12795 if No_Predicate_On_Actual (A_Gen_T)
12796 and then Has_Predicates (Act_T)
12797 then
12798 Error_Msg_NE
12799 ("actual for& cannot be a type with predicate",
12800 Instantiation_Node, A_Gen_T);
12801
12802 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12803 and then Has_Predicates (Act_T)
12804 and then not Has_Static_Predicate_Aspect (Act_T)
12805 then
12806 Error_Msg_NE
12807 ("actual for& cannot be a type with a dynamic predicate",
12808 Instantiation_Node, A_Gen_T);
12809 end if;
12810 end Diagnose_Predicated_Actual;
12811
12812 --------------------
12813 -- Subtypes_Match --
12814 --------------------
12815
12816 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12817 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12818
12819 begin
12820 -- Some detailed comments would be useful here ???
12821
12822 return ((Base_Type (T) = Act_T
12823 or else Base_Type (T) = Base_Type (Act_T))
12824 and then Subtypes_Statically_Match (T, Act_T))
12825
12826 or else (Is_Class_Wide_Type (Gen_T)
12827 and then Is_Class_Wide_Type (Act_T)
12828 and then Subtypes_Match
12829 (Get_Instance_Of (Root_Type (Gen_T)),
12830 Root_Type (Act_T)))
12831
12832 or else
12833 (Ekind (Gen_T) in E_Anonymous_Access_Subprogram_Type
12834 | E_Anonymous_Access_Type
12835 and then Ekind (Act_T) = Ekind (Gen_T)
12836 and then Subtypes_Statically_Match
12837 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12838 end Subtypes_Match;
12839
12840 -----------------------------------------
12841 -- Validate_Access_Subprogram_Instance --
12842 -----------------------------------------
12843
12844 procedure Validate_Access_Subprogram_Instance is
12845 begin
12846 if not Is_Access_Type (Act_T)
12847 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12848 then
12849 Error_Msg_NE
12850 ("expect access type in instantiation of &", Actual, Gen_T);
12851 Abandon_Instantiation (Actual);
12852 end if;
12853
12854 -- According to AI05-288, actuals for access_to_subprograms must be
12855 -- subtype conformant with the generic formal. Previous to AI05-288
12856 -- only mode conformance was required.
12857
12858 -- This is a binding interpretation that applies to previous versions
12859 -- of the language, no need to maintain previous weaker checks.
12860
12861 Check_Subtype_Conformant
12862 (Designated_Type (Act_T),
12863 Designated_Type (A_Gen_T),
12864 Actual,
12865 Get_Inst => True);
12866
12867 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12868 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12869 Error_Msg_NE
12870 ("protected access type not allowed for formal &",
12871 Actual, Gen_T);
12872 end if;
12873
12874 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12875 Error_Msg_NE
12876 ("expect protected access type for formal &",
12877 Actual, Gen_T);
12878 end if;
12879
12880 -- If the formal has a specified convention (which in most cases
12881 -- will be StdCall) verify that the actual has the same convention.
12882
12883 if Has_Convention_Pragma (A_Gen_T)
12884 and then Convention (A_Gen_T) /= Convention (Act_T)
12885 then
12886 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12887 Error_Msg_NE
12888 ("actual for formal & must have convention %", Actual, Gen_T);
12889 end if;
12890
12891 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12892 Error_Msg_NE
12893 ("non null exclusion of actual and formal & do not match",
12894 Actual, Gen_T);
12895 end if;
12896 end Validate_Access_Subprogram_Instance;
12897
12898 -----------------------------------
12899 -- Validate_Access_Type_Instance --
12900 -----------------------------------
12901
12902 procedure Validate_Access_Type_Instance is
12903 Desig_Type : constant Entity_Id :=
12904 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12905 Desig_Act : Entity_Id;
12906
12907 begin
12908 if not Is_Access_Type (Act_T) then
12909 Error_Msg_NE
12910 ("expect access type in instantiation of &", Actual, Gen_T);
12911 Abandon_Instantiation (Actual);
12912 end if;
12913
12914 if Is_Access_Constant (A_Gen_T) then
12915 if not Is_Access_Constant (Act_T) then
12916 Error_Msg_N
12917 ("actual type must be access-to-constant type", Actual);
12918 Abandon_Instantiation (Actual);
12919 end if;
12920 else
12921 if Is_Access_Constant (Act_T) then
12922 Error_Msg_N
12923 ("actual type must be access-to-variable type", Actual);
12924 Abandon_Instantiation (Actual);
12925
12926 elsif Ekind (A_Gen_T) = E_General_Access_Type
12927 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12928 then
12929 Error_Msg_N -- CODEFIX
12930 ("actual must be general access type!", Actual);
12931 Error_Msg_NE -- CODEFIX
12932 ("add ALL to }!", Actual, Act_T);
12933 Abandon_Instantiation (Actual);
12934 end if;
12935 end if;
12936
12937 -- The designated subtypes, that is to say the subtypes introduced
12938 -- by an access type declaration (and not by a subtype declaration)
12939 -- must match.
12940
12941 Desig_Act := Designated_Type (Base_Type (Act_T));
12942
12943 -- The designated type may have been introduced through a limited_
12944 -- with clause, in which case retrieve the non-limited view. This
12945 -- applies to incomplete types as well as to class-wide types.
12946
12947 if From_Limited_With (Desig_Act) then
12948 Desig_Act := Available_View (Desig_Act);
12949 end if;
12950
12951 if not Subtypes_Match (Desig_Type, Desig_Act) then
12952 Error_Msg_NE
12953 ("designated type of actual does not match that of formal &",
12954 Actual, Gen_T);
12955
12956 if not Predicates_Match (Desig_Type, Desig_Act) then
12957 Error_Msg_N ("\predicates do not match", Actual);
12958 end if;
12959
12960 Abandon_Instantiation (Actual);
12961
12962 elsif Is_Access_Type (Designated_Type (Act_T))
12963 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12964 /=
12965 Is_Constrained (Designated_Type (Desig_Type))
12966 then
12967 Error_Msg_NE
12968 ("designated type of actual does not match that of formal &",
12969 Actual, Gen_T);
12970
12971 if not Predicates_Match (Desig_Type, Desig_Act) then
12972 Error_Msg_N ("\predicates do not match", Actual);
12973 end if;
12974
12975 Abandon_Instantiation (Actual);
12976 end if;
12977
12978 -- Ada 2005: null-exclusion indicators of the two types must agree
12979
12980 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12981 Error_Msg_NE
12982 ("non null exclusion of actual and formal & do not match",
12983 Actual, Gen_T);
12984 end if;
12985 end Validate_Access_Type_Instance;
12986
12987 ----------------------------------
12988 -- Validate_Array_Type_Instance --
12989 ----------------------------------
12990
12991 procedure Validate_Array_Type_Instance is
12992 I1 : Node_Id;
12993 I2 : Node_Id;
12994 T2 : Entity_Id;
12995
12996 function Formal_Dimensions return Nat;
12997 -- Count number of dimensions in array type formal
12998
12999 -----------------------
13000 -- Formal_Dimensions --
13001 -----------------------
13002
13003 function Formal_Dimensions return Nat is
13004 Num : Nat := 0;
13005 Index : Node_Id;
13006
13007 begin
13008 if Nkind (Def) = N_Constrained_Array_Definition then
13009 Index := First (Discrete_Subtype_Definitions (Def));
13010 else
13011 Index := First (Subtype_Marks (Def));
13012 end if;
13013
13014 while Present (Index) loop
13015 Num := Num + 1;
13016 Next_Index (Index);
13017 end loop;
13018
13019 return Num;
13020 end Formal_Dimensions;
13021
13022 -- Start of processing for Validate_Array_Type_Instance
13023
13024 begin
13025 if not Is_Array_Type (Act_T) then
13026 Error_Msg_NE
13027 ("expect array type in instantiation of &", Actual, Gen_T);
13028 Abandon_Instantiation (Actual);
13029
13030 elsif Nkind (Def) = N_Constrained_Array_Definition then
13031 if not (Is_Constrained (Act_T)) then
13032 Error_Msg_NE
13033 ("expect constrained array in instantiation of &",
13034 Actual, Gen_T);
13035 Abandon_Instantiation (Actual);
13036 end if;
13037
13038 else
13039 if Is_Constrained (Act_T) then
13040 Error_Msg_NE
13041 ("expect unconstrained array in instantiation of &",
13042 Actual, Gen_T);
13043 Abandon_Instantiation (Actual);
13044 end if;
13045 end if;
13046
13047 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13048 Error_Msg_NE
13049 ("dimensions of actual do not match formal &", Actual, Gen_T);
13050 Abandon_Instantiation (Actual);
13051 end if;
13052
13053 I1 := First_Index (A_Gen_T);
13054 I2 := First_Index (Act_T);
13055 for J in 1 .. Formal_Dimensions loop
13056
13057 -- If the indexes of the actual were given by a subtype_mark,
13058 -- the index was transformed into a range attribute. Retrieve
13059 -- the original type mark for checking.
13060
13061 if Is_Entity_Name (Original_Node (I2)) then
13062 T2 := Entity (Original_Node (I2));
13063 else
13064 T2 := Etype (I2);
13065 end if;
13066
13067 if not Subtypes_Match
13068 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13069 then
13070 Error_Msg_NE
13071 ("index types of actual do not match those of formal &",
13072 Actual, Gen_T);
13073 Abandon_Instantiation (Actual);
13074 end if;
13075
13076 Next_Index (I1);
13077 Next_Index (I2);
13078 end loop;
13079
13080 -- Check matching subtypes. Note that there are complex visibility
13081 -- issues when the generic is a child unit and some aspect of the
13082 -- generic type is declared in a parent unit of the generic. We do
13083 -- the test to handle this special case only after a direct check
13084 -- for static matching has failed. The case where both the component
13085 -- type and the array type are separate formals, and the component
13086 -- type is a private view may also require special checking in
13087 -- Subtypes_Match. Finally, we assume that a child instance where
13088 -- the component type comes from a formal of a parent instance is
13089 -- correct because the generic was correct. A more precise check
13090 -- seems too complex to install???
13091
13092 if Subtypes_Match
13093 (Component_Type (A_Gen_T), Component_Type (Act_T))
13094 or else
13095 Subtypes_Match
13096 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13097 Component_Type (Act_T))
13098 or else
13099 (not Inside_A_Generic
13100 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13101 then
13102 null;
13103 else
13104 Error_Msg_NE
13105 ("component subtype of actual does not match that of formal &",
13106 Actual, Gen_T);
13107 Abandon_Instantiation (Actual);
13108 end if;
13109
13110 if Has_Aliased_Components (A_Gen_T)
13111 and then not Has_Aliased_Components (Act_T)
13112 then
13113 Error_Msg_NE
13114 ("actual must have aliased components to match formal type &",
13115 Actual, Gen_T);
13116 end if;
13117 end Validate_Array_Type_Instance;
13118
13119 -----------------------------------------------
13120 -- Validate_Derived_Interface_Type_Instance --
13121 -----------------------------------------------
13122
13123 procedure Validate_Derived_Interface_Type_Instance is
13124 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13125 Elmt : Elmt_Id;
13126
13127 begin
13128 -- First apply interface instance checks
13129
13130 Validate_Interface_Type_Instance;
13131
13132 -- Verify that immediate parent interface is an ancestor of
13133 -- the actual.
13134
13135 if Present (Par)
13136 and then not Interface_Present_In_Ancestor (Act_T, Par)
13137 then
13138 Error_Msg_NE
13139 ("interface actual must include progenitor&", Actual, Par);
13140 end if;
13141
13142 -- Now verify that the actual includes all other ancestors of
13143 -- the formal.
13144
13145 Elmt := First_Elmt (Interfaces (A_Gen_T));
13146 while Present (Elmt) loop
13147 if not Interface_Present_In_Ancestor
13148 (Act_T, Get_Instance_Of (Node (Elmt)))
13149 then
13150 Error_Msg_NE
13151 ("interface actual must include progenitor&",
13152 Actual, Node (Elmt));
13153 end if;
13154
13155 Next_Elmt (Elmt);
13156 end loop;
13157 end Validate_Derived_Interface_Type_Instance;
13158
13159 ------------------------------------
13160 -- Validate_Derived_Type_Instance --
13161 ------------------------------------
13162
13163 procedure Validate_Derived_Type_Instance is
13164 Actual_Discr : Entity_Id;
13165 Ancestor_Discr : Entity_Id;
13166
13167 begin
13168 -- Verify that the actual includes the progenitors of the formal,
13169 -- if any. The formal may depend on previous formals and their
13170 -- instance, so we must examine instance of interfaces if present.
13171 -- The actual may be an extension of an interface, in which case
13172 -- it does not appear in the interface list, so this must be
13173 -- checked separately.
13174
13175 if Present (Interface_List (Def)) then
13176 if not Has_Interfaces (Act_T) then
13177 Error_Msg_NE
13178 ("actual must implement all interfaces of formal&",
13179 Actual, A_Gen_T);
13180
13181 else
13182 declare
13183 Act_Iface_List : Elist_Id;
13184 Iface : Node_Id;
13185 Iface_Ent : Entity_Id;
13186
13187 function Instance_Exists (I : Entity_Id) return Boolean;
13188 -- If the interface entity is declared in a generic unit,
13189 -- this can only be legal if we are within an instantiation
13190 -- of a child of that generic. There is currently no
13191 -- mechanism to relate an interface declared within a
13192 -- generic to the corresponding interface in an instance,
13193 -- so we traverse the list of interfaces of the actual,
13194 -- looking for a name match.
13195
13196 ---------------------
13197 -- Instance_Exists --
13198 ---------------------
13199
13200 function Instance_Exists (I : Entity_Id) return Boolean is
13201 Iface_Elmt : Elmt_Id;
13202
13203 begin
13204 Iface_Elmt := First_Elmt (Act_Iface_List);
13205 while Present (Iface_Elmt) loop
13206 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13207 and then Chars (Node (Iface_Elmt)) = Chars (I)
13208 then
13209 return True;
13210 end if;
13211
13212 Next_Elmt (Iface_Elmt);
13213 end loop;
13214
13215 return False;
13216 end Instance_Exists;
13217
13218 begin
13219 Iface := First (Abstract_Interface_List (A_Gen_T));
13220 Collect_Interfaces (Act_T, Act_Iface_List);
13221
13222 while Present (Iface) loop
13223 Iface_Ent := Get_Instance_Of (Entity (Iface));
13224
13225 if Is_Ancestor (Iface_Ent, Act_T)
13226 or else Is_Progenitor (Iface_Ent, Act_T)
13227 then
13228 null;
13229
13230 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13231 and then Instance_Exists (Iface_Ent)
13232 then
13233 null;
13234
13235 else
13236 Error_Msg_Name_1 := Chars (Act_T);
13237 Error_Msg_NE
13238 ("Actual% must implement interface&",
13239 Actual, Etype (Iface));
13240 end if;
13241
13242 Next (Iface);
13243 end loop;
13244 end;
13245 end if;
13246 end if;
13247
13248 -- If the parent type in the generic declaration is itself a previous
13249 -- formal type, then it is local to the generic and absent from the
13250 -- analyzed generic definition. In that case the ancestor is the
13251 -- instance of the formal (which must have been instantiated
13252 -- previously), unless the ancestor is itself a formal derived type.
13253 -- In this latter case (which is the subject of Corrigendum 8652/0038
13254 -- (AI-202) the ancestor of the formals is the ancestor of its
13255 -- parent. Otherwise, the analyzed generic carries the parent type.
13256 -- If the parent type is defined in a previous formal package, then
13257 -- the scope of that formal package is that of the generic type
13258 -- itself, and it has already been mapped into the corresponding type
13259 -- in the actual package.
13260
13261 -- Common case: parent type defined outside of the generic
13262
13263 if Is_Entity_Name (Subtype_Mark (Def))
13264 and then Present (Entity (Subtype_Mark (Def)))
13265 then
13266 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13267
13268 -- Check whether parent is defined in a previous formal package
13269
13270 elsif
13271 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13272 then
13273 Ancestor :=
13274 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13275
13276 -- The type may be a local derivation, or a type extension of a
13277 -- previous formal, or of a formal of a parent package.
13278
13279 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13280 or else
13281 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13282 then
13283 -- Check whether the parent is another derived formal type in the
13284 -- same generic unit.
13285
13286 if Etype (A_Gen_T) /= A_Gen_T
13287 and then Is_Generic_Type (Etype (A_Gen_T))
13288 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13289 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13290 then
13291 -- Locate ancestor of parent from the subtype declaration
13292 -- created for the actual.
13293
13294 declare
13295 Decl : Node_Id;
13296
13297 begin
13298 Decl := First (Actual_Decls);
13299 while Present (Decl) loop
13300 if Nkind (Decl) = N_Subtype_Declaration
13301 and then Chars (Defining_Identifier (Decl)) =
13302 Chars (Etype (A_Gen_T))
13303 then
13304 Ancestor := Generic_Parent_Type (Decl);
13305 exit;
13306 else
13307 Next (Decl);
13308 end if;
13309 end loop;
13310 end;
13311
13312 pragma Assert (Present (Ancestor));
13313
13314 -- The ancestor itself may be a previous formal that has been
13315 -- instantiated.
13316
13317 Ancestor := Get_Instance_Of (Ancestor);
13318
13319 else
13320 Ancestor :=
13321 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13322 end if;
13323
13324 -- Check whether parent is a previous formal of the current generic
13325
13326 elsif Is_Derived_Type (A_Gen_T)
13327 and then Is_Generic_Type (Etype (A_Gen_T))
13328 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13329 then
13330 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13331
13332 -- An unusual case: the actual is a type declared in a parent unit,
13333 -- but is not a formal type so there is no instance_of for it.
13334 -- Retrieve it by analyzing the record extension.
13335
13336 elsif Is_Child_Unit (Scope (A_Gen_T))
13337 and then In_Open_Scopes (Scope (Act_T))
13338 and then Is_Generic_Instance (Scope (Act_T))
13339 then
13340 Analyze (Subtype_Mark (Def));
13341 Ancestor := Entity (Subtype_Mark (Def));
13342
13343 else
13344 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13345 end if;
13346
13347 -- If the formal derived type has pragma Preelaborable_Initialization
13348 -- then the actual type must have preelaborable initialization.
13349
13350 if Known_To_Have_Preelab_Init (A_Gen_T)
13351 and then not Has_Preelaborable_Initialization (Act_T)
13352 then
13353 Error_Msg_NE
13354 ("actual for & must have preelaborable initialization",
13355 Actual, Gen_T);
13356 end if;
13357
13358 -- Ada 2005 (AI-251)
13359
13360 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13361 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13362 Error_Msg_NE
13363 ("(Ada 2005) expected type implementing & in instantiation",
13364 Actual, Ancestor);
13365 end if;
13366
13367 -- Finally verify that the (instance of) the ancestor is an ancestor
13368 -- of the actual.
13369
13370 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13371 Error_Msg_NE
13372 ("expect type derived from & in instantiation",
13373 Actual, First_Subtype (Ancestor));
13374 Abandon_Instantiation (Actual);
13375 end if;
13376
13377 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13378 -- that the formal type declaration has been rewritten as a private
13379 -- extension.
13380
13381 if Ada_Version >= Ada_2005
13382 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13383 and then Synchronized_Present (Parent (A_Gen_T))
13384 then
13385 -- The actual must be a synchronized tagged type
13386
13387 if not Is_Tagged_Type (Act_T) then
13388 Error_Msg_N
13389 ("actual of synchronized type must be tagged", Actual);
13390 Abandon_Instantiation (Actual);
13391
13392 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13393 and then Nkind (Type_Definition (Parent (Act_T))) =
13394 N_Derived_Type_Definition
13395 and then not Synchronized_Present
13396 (Type_Definition (Parent (Act_T)))
13397 then
13398 Error_Msg_N
13399 ("actual of synchronized type must be synchronized", Actual);
13400 Abandon_Instantiation (Actual);
13401 end if;
13402 end if;
13403
13404 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13405 -- removes the second instance of the phrase "or allow pass by copy".
13406
13407 -- For Ada 2020, the aspect may be specified explicitly for the
13408 -- formal regardless of whether an ancestor obeys it.
13409
13410 if Is_Atomic (Act_T)
13411 and then not Is_Atomic (Ancestor)
13412 and then not Is_Atomic (A_Gen_T)
13413 then
13414 Error_Msg_N
13415 ("cannot have atomic actual type for non-atomic formal type",
13416 Actual);
13417
13418 elsif Is_Volatile (Act_T)
13419 and then not Is_Volatile (Ancestor)
13420 and then not Is_Volatile (A_Gen_T)
13421 then
13422 Error_Msg_N
13423 ("cannot have volatile actual type for non-volatile formal type",
13424 Actual);
13425 end if;
13426
13427 -- It should not be necessary to check for unknown discriminants on
13428 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13429 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13430 -- needs fixing. ???
13431
13432 if Is_Definite_Subtype (A_Gen_T)
13433 and then not Unknown_Discriminants_Present (Formal)
13434 and then not Is_Definite_Subtype (Act_T)
13435 then
13436 Error_Msg_N ("actual subtype must be constrained", Actual);
13437 Abandon_Instantiation (Actual);
13438 end if;
13439
13440 if not Unknown_Discriminants_Present (Formal) then
13441 if Is_Constrained (Ancestor) then
13442 if not Is_Constrained (Act_T) then
13443 Error_Msg_N ("actual subtype must be constrained", Actual);
13444 Abandon_Instantiation (Actual);
13445 end if;
13446
13447 -- Ancestor is unconstrained, Check if generic formal and actual
13448 -- agree on constrainedness. The check only applies to array types
13449 -- and discriminated types.
13450
13451 elsif Is_Constrained (Act_T) then
13452 if Ekind (Ancestor) = E_Access_Type
13453 or else (not Is_Constrained (A_Gen_T)
13454 and then Is_Composite_Type (A_Gen_T))
13455 then
13456 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13457 Abandon_Instantiation (Actual);
13458 end if;
13459
13460 -- A class-wide type is only allowed if the formal has unknown
13461 -- discriminants.
13462
13463 elsif Is_Class_Wide_Type (Act_T)
13464 and then not Has_Unknown_Discriminants (Ancestor)
13465 then
13466 Error_Msg_NE
13467 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13468 Abandon_Instantiation (Actual);
13469
13470 -- Otherwise, the formal and actual must have the same number
13471 -- of discriminants and each discriminant of the actual must
13472 -- correspond to a discriminant of the formal.
13473
13474 elsif Has_Discriminants (Act_T)
13475 and then not Has_Unknown_Discriminants (Act_T)
13476 and then Has_Discriminants (Ancestor)
13477 then
13478 Actual_Discr := First_Discriminant (Act_T);
13479 Ancestor_Discr := First_Discriminant (Ancestor);
13480 while Present (Actual_Discr)
13481 and then Present (Ancestor_Discr)
13482 loop
13483 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13484 No (Corresponding_Discriminant (Actual_Discr))
13485 then
13486 Error_Msg_NE
13487 ("discriminant & does not correspond "
13488 & "to ancestor discriminant", Actual, Actual_Discr);
13489 Abandon_Instantiation (Actual);
13490 end if;
13491
13492 Next_Discriminant (Actual_Discr);
13493 Next_Discriminant (Ancestor_Discr);
13494 end loop;
13495
13496 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13497 Error_Msg_NE
13498 ("actual for & must have same number of discriminants",
13499 Actual, Gen_T);
13500 Abandon_Instantiation (Actual);
13501 end if;
13502
13503 -- This case should be caught by the earlier check for
13504 -- constrainedness, but the check here is added for completeness.
13505
13506 elsif Has_Discriminants (Act_T)
13507 and then not Has_Unknown_Discriminants (Act_T)
13508 then
13509 Error_Msg_NE
13510 ("actual for & must not have discriminants", Actual, Gen_T);
13511 Abandon_Instantiation (Actual);
13512
13513 elsif Has_Discriminants (Ancestor) then
13514 Error_Msg_NE
13515 ("actual for & must have known discriminants", Actual, Gen_T);
13516 Abandon_Instantiation (Actual);
13517 end if;
13518
13519 if not Subtypes_Statically_Compatible
13520 (Act_T, Ancestor, Formal_Derived_Matching => True)
13521 then
13522 Error_Msg_NE
13523 ("actual for & must be statically compatible with ancestor",
13524 Actual, Gen_T);
13525
13526 if not Predicates_Compatible (Act_T, Ancestor) then
13527 Error_Msg_N
13528 ("\predicate on actual is not compatible with ancestor",
13529 Actual);
13530 end if;
13531
13532 Abandon_Instantiation (Actual);
13533 end if;
13534 end if;
13535
13536 -- If the formal and actual types are abstract, check that there
13537 -- are no abstract primitives of the actual type that correspond to
13538 -- nonabstract primitives of the formal type (second sentence of
13539 -- RM95 3.9.3(9)).
13540
13541 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13542 Check_Abstract_Primitives : declare
13543 Gen_Prims : constant Elist_Id :=
13544 Primitive_Operations (A_Gen_T);
13545 Gen_Elmt : Elmt_Id;
13546 Gen_Subp : Entity_Id;
13547 Anc_Subp : Entity_Id;
13548 Anc_Formal : Entity_Id;
13549 Anc_F_Type : Entity_Id;
13550
13551 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13552 Act_Elmt : Elmt_Id;
13553 Act_Subp : Entity_Id;
13554 Act_Formal : Entity_Id;
13555 Act_F_Type : Entity_Id;
13556
13557 Subprograms_Correspond : Boolean;
13558
13559 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13560 -- Returns true if T2 is derived directly or indirectly from
13561 -- T1, including derivations from interfaces. T1 and T2 are
13562 -- required to be specific tagged base types.
13563
13564 ------------------------
13565 -- Is_Tagged_Ancestor --
13566 ------------------------
13567
13568 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13569 is
13570 Intfc_Elmt : Elmt_Id;
13571
13572 begin
13573 -- The predicate is satisfied if the types are the same
13574
13575 if T1 = T2 then
13576 return True;
13577
13578 -- If we've reached the top of the derivation chain then
13579 -- we know that T1 is not an ancestor of T2.
13580
13581 elsif Etype (T2) = T2 then
13582 return False;
13583
13584 -- Proceed to check T2's immediate parent
13585
13586 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13587 return True;
13588
13589 -- Finally, check to see if T1 is an ancestor of any of T2's
13590 -- progenitors.
13591
13592 else
13593 Intfc_Elmt := First_Elmt (Interfaces (T2));
13594 while Present (Intfc_Elmt) loop
13595 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13596 return True;
13597 end if;
13598
13599 Next_Elmt (Intfc_Elmt);
13600 end loop;
13601 end if;
13602
13603 return False;
13604 end Is_Tagged_Ancestor;
13605
13606 -- Start of processing for Check_Abstract_Primitives
13607
13608 begin
13609 -- Loop over all of the formal derived type's primitives
13610
13611 Gen_Elmt := First_Elmt (Gen_Prims);
13612 while Present (Gen_Elmt) loop
13613 Gen_Subp := Node (Gen_Elmt);
13614
13615 -- If the primitive of the formal is not abstract, then
13616 -- determine whether there is a corresponding primitive of
13617 -- the actual type that's abstract.
13618
13619 if not Is_Abstract_Subprogram (Gen_Subp) then
13620 Act_Elmt := First_Elmt (Act_Prims);
13621 while Present (Act_Elmt) loop
13622 Act_Subp := Node (Act_Elmt);
13623
13624 -- If we find an abstract primitive of the actual,
13625 -- then we need to test whether it corresponds to the
13626 -- subprogram from which the generic formal primitive
13627 -- is inherited.
13628
13629 if Is_Abstract_Subprogram (Act_Subp) then
13630 Anc_Subp := Alias (Gen_Subp);
13631
13632 -- Test whether we have a corresponding primitive
13633 -- by comparing names, kinds, formal types, and
13634 -- result types.
13635
13636 if Chars (Anc_Subp) = Chars (Act_Subp)
13637 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13638 then
13639 Anc_Formal := First_Formal (Anc_Subp);
13640 Act_Formal := First_Formal (Act_Subp);
13641 while Present (Anc_Formal)
13642 and then Present (Act_Formal)
13643 loop
13644 Anc_F_Type := Etype (Anc_Formal);
13645 Act_F_Type := Etype (Act_Formal);
13646
13647 if Ekind (Anc_F_Type) =
13648 E_Anonymous_Access_Type
13649 then
13650 Anc_F_Type := Designated_Type (Anc_F_Type);
13651
13652 if Ekind (Act_F_Type) =
13653 E_Anonymous_Access_Type
13654 then
13655 Act_F_Type :=
13656 Designated_Type (Act_F_Type);
13657 else
13658 exit;
13659 end if;
13660
13661 elsif
13662 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13663 then
13664 exit;
13665 end if;
13666
13667 Anc_F_Type := Base_Type (Anc_F_Type);
13668 Act_F_Type := Base_Type (Act_F_Type);
13669
13670 -- If the formal is controlling, then the
13671 -- the type of the actual primitive's formal
13672 -- must be derived directly or indirectly
13673 -- from the type of the ancestor primitive's
13674 -- formal.
13675
13676 if Is_Controlling_Formal (Anc_Formal) then
13677 if not Is_Tagged_Ancestor
13678 (Anc_F_Type, Act_F_Type)
13679 then
13680 exit;
13681 end if;
13682
13683 -- Otherwise the types of the formals must
13684 -- be the same.
13685
13686 elsif Anc_F_Type /= Act_F_Type then
13687 exit;
13688 end if;
13689
13690 Next_Entity (Anc_Formal);
13691 Next_Entity (Act_Formal);
13692 end loop;
13693
13694 -- If we traversed through all of the formals
13695 -- then so far the subprograms correspond, so
13696 -- now check that any result types correspond.
13697
13698 if No (Anc_Formal) and then No (Act_Formal) then
13699 Subprograms_Correspond := True;
13700
13701 if Ekind (Act_Subp) = E_Function then
13702 Anc_F_Type := Etype (Anc_Subp);
13703 Act_F_Type := Etype (Act_Subp);
13704
13705 if Ekind (Anc_F_Type) =
13706 E_Anonymous_Access_Type
13707 then
13708 Anc_F_Type :=
13709 Designated_Type (Anc_F_Type);
13710
13711 if Ekind (Act_F_Type) =
13712 E_Anonymous_Access_Type
13713 then
13714 Act_F_Type :=
13715 Designated_Type (Act_F_Type);
13716 else
13717 Subprograms_Correspond := False;
13718 end if;
13719
13720 elsif
13721 Ekind (Act_F_Type)
13722 = E_Anonymous_Access_Type
13723 then
13724 Subprograms_Correspond := False;
13725 end if;
13726
13727 Anc_F_Type := Base_Type (Anc_F_Type);
13728 Act_F_Type := Base_Type (Act_F_Type);
13729
13730 -- Now either the result types must be
13731 -- the same or, if the result type is
13732 -- controlling, the result type of the
13733 -- actual primitive must descend from the
13734 -- result type of the ancestor primitive.
13735
13736 if Subprograms_Correspond
13737 and then Anc_F_Type /= Act_F_Type
13738 and then
13739 Has_Controlling_Result (Anc_Subp)
13740 and then not Is_Tagged_Ancestor
13741 (Anc_F_Type, Act_F_Type)
13742 then
13743 Subprograms_Correspond := False;
13744 end if;
13745 end if;
13746
13747 -- Found a matching subprogram belonging to
13748 -- formal ancestor type, so actual subprogram
13749 -- corresponds and this violates 3.9.3(9).
13750
13751 if Subprograms_Correspond then
13752 Error_Msg_NE
13753 ("abstract subprogram & overrides "
13754 & "nonabstract subprogram of ancestor",
13755 Actual, Act_Subp);
13756 end if;
13757 end if;
13758 end if;
13759 end if;
13760
13761 Next_Elmt (Act_Elmt);
13762 end loop;
13763 end if;
13764
13765 Next_Elmt (Gen_Elmt);
13766 end loop;
13767 end Check_Abstract_Primitives;
13768 end if;
13769
13770 -- Verify that limitedness matches. If parent is a limited
13771 -- interface then the generic formal is not unless declared
13772 -- explicitly so. If not declared limited, the actual cannot be
13773 -- limited (see AI05-0087).
13774
13775 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13776 if not In_Instance then
13777 Error_Msg_NE
13778 ("actual for non-limited & cannot be a limited type",
13779 Actual, Gen_T);
13780 Explain_Limited_Type (Act_T, Actual);
13781 Abandon_Instantiation (Actual);
13782 end if;
13783 end if;
13784
13785 -- Check for AI12-0036
13786
13787 declare
13788 Formal_Is_Private_Extension : constant Boolean :=
13789 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13790
13791 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13792
13793 begin
13794 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13795 if not In_Instance then
13796 if Actual_Is_Tagged then
13797 Error_Msg_NE
13798 ("actual for & cannot be a tagged type", Actual, Gen_T);
13799 else
13800 Error_Msg_NE
13801 ("actual for & must be a tagged type", Actual, Gen_T);
13802 end if;
13803
13804 Abandon_Instantiation (Actual);
13805 end if;
13806 end if;
13807 end;
13808 end Validate_Derived_Type_Instance;
13809
13810 ----------------------------------------
13811 -- Validate_Discriminated_Formal_Type --
13812 ----------------------------------------
13813
13814 procedure Validate_Discriminated_Formal_Type is
13815 Formal_Discr : Entity_Id;
13816 Actual_Discr : Entity_Id;
13817 Formal_Subt : Entity_Id;
13818
13819 begin
13820 if Has_Discriminants (A_Gen_T) then
13821 if not Has_Discriminants (Act_T) then
13822 Error_Msg_NE
13823 ("actual for & must have discriminants", Actual, Gen_T);
13824 Abandon_Instantiation (Actual);
13825
13826 elsif Is_Constrained (Act_T) then
13827 Error_Msg_NE
13828 ("actual for & must be unconstrained", Actual, Gen_T);
13829 Abandon_Instantiation (Actual);
13830
13831 else
13832 Formal_Discr := First_Discriminant (A_Gen_T);
13833 Actual_Discr := First_Discriminant (Act_T);
13834 while Formal_Discr /= Empty loop
13835 if Actual_Discr = Empty then
13836 Error_Msg_NE
13837 ("discriminants on actual do not match formal",
13838 Actual, Gen_T);
13839 Abandon_Instantiation (Actual);
13840 end if;
13841
13842 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13843
13844 -- Access discriminants match if designated types do
13845
13846 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13847 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13848 E_Anonymous_Access_Type
13849 and then
13850 Get_Instance_Of
13851 (Designated_Type (Base_Type (Formal_Subt))) =
13852 Designated_Type (Base_Type (Etype (Actual_Discr)))
13853 then
13854 null;
13855
13856 elsif Base_Type (Formal_Subt) /=
13857 Base_Type (Etype (Actual_Discr))
13858 then
13859 Error_Msg_NE
13860 ("types of actual discriminants must match formal",
13861 Actual, Gen_T);
13862 Abandon_Instantiation (Actual);
13863
13864 elsif not Subtypes_Statically_Match
13865 (Formal_Subt, Etype (Actual_Discr))
13866 and then Ada_Version >= Ada_95
13867 then
13868 Error_Msg_NE
13869 ("subtypes of actual discriminants must match formal",
13870 Actual, Gen_T);
13871 Abandon_Instantiation (Actual);
13872 end if;
13873
13874 Next_Discriminant (Formal_Discr);
13875 Next_Discriminant (Actual_Discr);
13876 end loop;
13877
13878 if Actual_Discr /= Empty then
13879 Error_Msg_NE
13880 ("discriminants on actual do not match formal",
13881 Actual, Gen_T);
13882 Abandon_Instantiation (Actual);
13883 end if;
13884 end if;
13885 end if;
13886 end Validate_Discriminated_Formal_Type;
13887
13888 ---------------------------------------
13889 -- Validate_Incomplete_Type_Instance --
13890 ---------------------------------------
13891
13892 procedure Validate_Incomplete_Type_Instance is
13893 begin
13894 if not Is_Tagged_Type (Act_T)
13895 and then Is_Tagged_Type (A_Gen_T)
13896 then
13897 Error_Msg_NE
13898 ("actual for & must be a tagged type", Actual, Gen_T);
13899 end if;
13900
13901 Validate_Discriminated_Formal_Type;
13902 end Validate_Incomplete_Type_Instance;
13903
13904 --------------------------------------
13905 -- Validate_Interface_Type_Instance --
13906 --------------------------------------
13907
13908 procedure Validate_Interface_Type_Instance is
13909 begin
13910 if not Is_Interface (Act_T) then
13911 Error_Msg_NE
13912 ("actual for formal interface type must be an interface",
13913 Actual, Gen_T);
13914
13915 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13916 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13917 or else Is_Protected_Interface (A_Gen_T) /=
13918 Is_Protected_Interface (Act_T)
13919 or else Is_Synchronized_Interface (A_Gen_T) /=
13920 Is_Synchronized_Interface (Act_T)
13921 then
13922 Error_Msg_NE
13923 ("actual for interface& does not match (RM 12.5.5(4))",
13924 Actual, Gen_T);
13925 end if;
13926 end Validate_Interface_Type_Instance;
13927
13928 ------------------------------------
13929 -- Validate_Private_Type_Instance --
13930 ------------------------------------
13931
13932 procedure Validate_Private_Type_Instance is
13933 begin
13934 if Is_Limited_Type (Act_T)
13935 and then not Is_Limited_Type (A_Gen_T)
13936 then
13937 if In_Instance then
13938 null;
13939 else
13940 Error_Msg_NE
13941 ("actual for non-limited & cannot be a limited type", Actual,
13942 Gen_T);
13943 Explain_Limited_Type (Act_T, Actual);
13944 Abandon_Instantiation (Actual);
13945 end if;
13946
13947 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13948 and then not Has_Preelaborable_Initialization (Act_T)
13949 then
13950 Error_Msg_NE
13951 ("actual for & must have preelaborable initialization", Actual,
13952 Gen_T);
13953
13954 elsif not Is_Definite_Subtype (Act_T)
13955 and then Is_Definite_Subtype (A_Gen_T)
13956 and then Ada_Version >= Ada_95
13957 then
13958 Error_Msg_NE
13959 ("actual for & must be a definite subtype", Actual, Gen_T);
13960
13961 elsif not Is_Tagged_Type (Act_T)
13962 and then Is_Tagged_Type (A_Gen_T)
13963 then
13964 Error_Msg_NE
13965 ("actual for & must be a tagged type", Actual, Gen_T);
13966 end if;
13967
13968 Validate_Discriminated_Formal_Type;
13969 Ancestor := Gen_T;
13970 end Validate_Private_Type_Instance;
13971
13972 -- Start of processing for Instantiate_Type
13973
13974 begin
13975 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13976 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13977 return New_List (Error);
13978
13979 elsif not Is_Entity_Name (Actual)
13980 or else not Is_Type (Entity (Actual))
13981 then
13982 Error_Msg_NE
13983 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13984 Abandon_Instantiation (Actual);
13985
13986 else
13987 Act_T := Entity (Actual);
13988
13989 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13990 -- as a generic actual parameter if the corresponding formal type
13991 -- does not have a known_discriminant_part, or is a formal derived
13992 -- type that is an Unchecked_Union type.
13993
13994 if Is_Unchecked_Union (Base_Type (Act_T)) then
13995 if not Has_Discriminants (A_Gen_T)
13996 or else (Is_Derived_Type (A_Gen_T)
13997 and then Is_Unchecked_Union (A_Gen_T))
13998 then
13999 null;
14000 else
14001 Error_Msg_N ("unchecked union cannot be the actual for a "
14002 & "discriminated formal type", Act_T);
14003
14004 end if;
14005 end if;
14006
14007 -- Deal with fixed/floating restrictions
14008
14009 if Is_Floating_Point_Type (Act_T) then
14010 Check_Restriction (No_Floating_Point, Actual);
14011 elsif Is_Fixed_Point_Type (Act_T) then
14012 Check_Restriction (No_Fixed_Point, Actual);
14013 end if;
14014
14015 -- Deal with error of using incomplete type as generic actual.
14016 -- This includes limited views of a type, even if the non-limited
14017 -- view may be available.
14018
14019 if Ekind (Act_T) = E_Incomplete_Type
14020 or else (Is_Class_Wide_Type (Act_T)
14021 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14022 then
14023 -- If the formal is an incomplete type, the actual can be
14024 -- incomplete as well.
14025
14026 if Ekind (A_Gen_T) = E_Incomplete_Type then
14027 null;
14028
14029 elsif Is_Class_Wide_Type (Act_T)
14030 or else No (Full_View (Act_T))
14031 then
14032 Error_Msg_N ("premature use of incomplete type", Actual);
14033 Abandon_Instantiation (Actual);
14034 else
14035 Act_T := Full_View (Act_T);
14036 Set_Entity (Actual, Act_T);
14037
14038 if Has_Private_Component (Act_T) then
14039 Error_Msg_N
14040 ("premature use of type with private component", Actual);
14041 end if;
14042 end if;
14043
14044 -- Deal with error of premature use of private type as generic actual
14045
14046 elsif Is_Private_Type (Act_T)
14047 and then Is_Private_Type (Base_Type (Act_T))
14048 and then not Is_Generic_Type (Act_T)
14049 and then not Is_Derived_Type (Act_T)
14050 and then No (Full_View (Root_Type (Act_T)))
14051 then
14052 -- If the formal is an incomplete type, the actual can be
14053 -- private or incomplete as well.
14054
14055 if Ekind (A_Gen_T) = E_Incomplete_Type then
14056 null;
14057 else
14058 Error_Msg_N ("premature use of private type", Actual);
14059 end if;
14060
14061 elsif Has_Private_Component (Act_T) then
14062 Error_Msg_N
14063 ("premature use of type with private component", Actual);
14064 end if;
14065
14066 Set_Instance_Of (A_Gen_T, Act_T);
14067
14068 -- If the type is generic, the class-wide type may also be used
14069
14070 if Is_Tagged_Type (A_Gen_T)
14071 and then Is_Tagged_Type (Act_T)
14072 and then not Is_Class_Wide_Type (A_Gen_T)
14073 then
14074 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14075 Class_Wide_Type (Act_T));
14076 end if;
14077
14078 if not Is_Abstract_Type (A_Gen_T)
14079 and then Is_Abstract_Type (Act_T)
14080 then
14081 Error_Msg_N
14082 ("actual of non-abstract formal cannot be abstract", Actual);
14083 end if;
14084
14085 -- A generic scalar type is a first subtype for which we generate
14086 -- an anonymous base type. Indicate that the instance of this base
14087 -- is the base type of the actual.
14088
14089 if Is_Scalar_Type (A_Gen_T) then
14090 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14091 end if;
14092 end if;
14093
14094 Check_Shared_Variable_Control_Aspects;
14095
14096 if Error_Posted (Act_T) then
14097 null;
14098 else
14099 case Nkind (Def) is
14100 when N_Formal_Private_Type_Definition =>
14101 Validate_Private_Type_Instance;
14102
14103 when N_Formal_Incomplete_Type_Definition =>
14104 Validate_Incomplete_Type_Instance;
14105
14106 when N_Formal_Derived_Type_Definition =>
14107 Validate_Derived_Type_Instance;
14108
14109 when N_Formal_Discrete_Type_Definition =>
14110 if not Is_Discrete_Type (Act_T) then
14111 Error_Msg_NE
14112 ("expect discrete type in instantiation of&",
14113 Actual, Gen_T);
14114 Abandon_Instantiation (Actual);
14115 end if;
14116
14117 Diagnose_Predicated_Actual;
14118
14119 when N_Formal_Signed_Integer_Type_Definition =>
14120 if not Is_Signed_Integer_Type (Act_T) then
14121 Error_Msg_NE
14122 ("expect signed integer type in instantiation of&",
14123 Actual, Gen_T);
14124 Abandon_Instantiation (Actual);
14125 end if;
14126
14127 Diagnose_Predicated_Actual;
14128
14129 when N_Formal_Modular_Type_Definition =>
14130 if not Is_Modular_Integer_Type (Act_T) then
14131 Error_Msg_NE
14132 ("expect modular type in instantiation of &",
14133 Actual, Gen_T);
14134 Abandon_Instantiation (Actual);
14135 end if;
14136
14137 Diagnose_Predicated_Actual;
14138
14139 when N_Formal_Floating_Point_Definition =>
14140 if not Is_Floating_Point_Type (Act_T) then
14141 Error_Msg_NE
14142 ("expect float type in instantiation of &", Actual, Gen_T);
14143 Abandon_Instantiation (Actual);
14144 end if;
14145
14146 when N_Formal_Ordinary_Fixed_Point_Definition =>
14147 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14148 Error_Msg_NE
14149 ("expect ordinary fixed point type in instantiation of &",
14150 Actual, Gen_T);
14151 Abandon_Instantiation (Actual);
14152 end if;
14153
14154 when N_Formal_Decimal_Fixed_Point_Definition =>
14155 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14156 Error_Msg_NE
14157 ("expect decimal type in instantiation of &",
14158 Actual, Gen_T);
14159 Abandon_Instantiation (Actual);
14160 end if;
14161
14162 when N_Array_Type_Definition =>
14163 Validate_Array_Type_Instance;
14164
14165 when N_Access_To_Object_Definition =>
14166 Validate_Access_Type_Instance;
14167
14168 when N_Access_Function_Definition
14169 | N_Access_Procedure_Definition
14170 =>
14171 Validate_Access_Subprogram_Instance;
14172
14173 when N_Record_Definition =>
14174 Validate_Interface_Type_Instance;
14175
14176 when N_Derived_Type_Definition =>
14177 Validate_Derived_Interface_Type_Instance;
14178
14179 when others =>
14180 raise Program_Error;
14181 end case;
14182 end if;
14183
14184 Subt := New_Copy (Gen_T);
14185
14186 -- Use adjusted sloc of subtype name as the location for other nodes in
14187 -- the subtype declaration.
14188
14189 Loc := Sloc (Subt);
14190
14191 Decl_Node :=
14192 Make_Subtype_Declaration (Loc,
14193 Defining_Identifier => Subt,
14194 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14195
14196 -- Record whether the actual is private at this point, so that
14197 -- Check_Generic_Actuals can restore its proper view before the
14198 -- semantic analysis of the instance.
14199
14200 if Is_Private_Type (Act_T) then
14201 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14202 end if;
14203
14204 -- In Ada 2012 the actual may be a limited view. Indicate that
14205 -- the local subtype must be treated as such.
14206
14207 if From_Limited_With (Act_T) then
14208 Set_Ekind (Subt, E_Incomplete_Subtype);
14209 Set_From_Limited_With (Subt);
14210 end if;
14211
14212 Decl_Nodes := New_List (Decl_Node);
14213
14214 -- Flag actual derived types so their elaboration produces the
14215 -- appropriate renamings for the primitive operations of the ancestor.
14216 -- Flag actual for formal private types as well, to determine whether
14217 -- operations in the private part may override inherited operations.
14218 -- If the formal has an interface list, the ancestor is not the
14219 -- parent, but the analyzed formal that includes the interface
14220 -- operations of all its progenitors.
14221
14222 -- Same treatment for formal private types, so we can check whether the
14223 -- type is tagged limited when validating derivations in the private
14224 -- part. (See AI05-096).
14225
14226 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14227 if Present (Interface_List (Def)) then
14228 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14229 else
14230 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14231 end if;
14232
14233 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14234 | N_Formal_Incomplete_Type_Definition
14235 then
14236 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14237 end if;
14238
14239 -- If the actual is a synchronized type that implements an interface,
14240 -- the primitive operations are attached to the corresponding record,
14241 -- and we have to treat it as an additional generic actual, so that its
14242 -- primitive operations become visible in the instance. The task or
14243 -- protected type itself does not carry primitive operations.
14244
14245 if Is_Concurrent_Type (Act_T)
14246 and then Is_Tagged_Type (Act_T)
14247 and then Present (Corresponding_Record_Type (Act_T))
14248 and then Present (Ancestor)
14249 and then Is_Interface (Ancestor)
14250 then
14251 declare
14252 Corr_Rec : constant Entity_Id :=
14253 Corresponding_Record_Type (Act_T);
14254 New_Corr : Entity_Id;
14255 Corr_Decl : Node_Id;
14256
14257 begin
14258 New_Corr := Make_Temporary (Loc, 'S');
14259 Corr_Decl :=
14260 Make_Subtype_Declaration (Loc,
14261 Defining_Identifier => New_Corr,
14262 Subtype_Indication =>
14263 New_Occurrence_Of (Corr_Rec, Loc));
14264 Append_To (Decl_Nodes, Corr_Decl);
14265
14266 if Ekind (Act_T) = E_Task_Type then
14267 Set_Ekind (Subt, E_Task_Subtype);
14268 else
14269 Set_Ekind (Subt, E_Protected_Subtype);
14270 end if;
14271
14272 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14273 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14274 Set_Generic_Parent_Type (Decl_Node, Empty);
14275 end;
14276 end if;
14277
14278 -- For a floating-point type, capture dimension info if any, because
14279 -- the generated subtype declaration does not come from source and
14280 -- will not process dimensions.
14281
14282 if Is_Floating_Point_Type (Act_T) then
14283 Copy_Dimensions (Act_T, Subt);
14284 end if;
14285
14286 return Decl_Nodes;
14287 end Instantiate_Type;
14288
14289 ---------------------
14290 -- Is_In_Main_Unit --
14291 ---------------------
14292
14293 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14294 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14295 Current_Unit : Node_Id;
14296
14297 begin
14298 if Unum = Main_Unit then
14299 return True;
14300
14301 -- If the current unit is a subunit then it is either the main unit or
14302 -- is being compiled as part of the main unit.
14303
14304 elsif Nkind (N) = N_Compilation_Unit then
14305 return Nkind (Unit (N)) = N_Subunit;
14306 end if;
14307
14308 Current_Unit := Parent (N);
14309 while Present (Current_Unit)
14310 and then Nkind (Current_Unit) /= N_Compilation_Unit
14311 loop
14312 Current_Unit := Parent (Current_Unit);
14313 end loop;
14314
14315 -- The instantiation node is in the main unit, or else the current node
14316 -- (perhaps as the result of nested instantiations) is in the main unit,
14317 -- or in the declaration of the main unit, which in this last case must
14318 -- be a body.
14319
14320 return
14321 Current_Unit = Cunit (Main_Unit)
14322 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14323 or else (Present (Current_Unit)
14324 and then Present (Library_Unit (Current_Unit))
14325 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14326 end Is_In_Main_Unit;
14327
14328 ----------------------------
14329 -- Load_Parent_Of_Generic --
14330 ----------------------------
14331
14332 procedure Load_Parent_Of_Generic
14333 (N : Node_Id;
14334 Spec : Node_Id;
14335 Body_Optional : Boolean := False)
14336 is
14337 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14338 Saved_Style_Check : constant Boolean := Style_Check;
14339 Saved_Warnings : constant Warning_Record := Save_Warnings;
14340 True_Parent : Node_Id;
14341 Inst_Node : Node_Id;
14342 OK : Boolean;
14343 Previous_Instances : constant Elist_Id := New_Elmt_List;
14344
14345 procedure Collect_Previous_Instances (Decls : List_Id);
14346 -- Collect all instantiations in the given list of declarations, that
14347 -- precede the generic that we need to load. If the bodies of these
14348 -- instantiations are available, we must analyze them, to ensure that
14349 -- the public symbols generated are the same when the unit is compiled
14350 -- to generate code, and when it is compiled in the context of a unit
14351 -- that needs a particular nested instance. This process is applied to
14352 -- both package and subprogram instances.
14353
14354 --------------------------------
14355 -- Collect_Previous_Instances --
14356 --------------------------------
14357
14358 procedure Collect_Previous_Instances (Decls : List_Id) is
14359 Decl : Node_Id;
14360
14361 begin
14362 Decl := First (Decls);
14363 while Present (Decl) loop
14364 if Sloc (Decl) >= Sloc (Inst_Node) then
14365 return;
14366
14367 -- If Decl is an instantiation, then record it as requiring
14368 -- instantiation of the corresponding body, except if it is an
14369 -- abbreviated instantiation generated internally for conformance
14370 -- checking purposes only for the case of a formal package
14371 -- declared without a box (see Instantiate_Formal_Package). Such
14372 -- an instantiation does not generate any code (the actual code
14373 -- comes from actual) and thus does not need to be analyzed here.
14374 -- If the instantiation appears with a generic package body it is
14375 -- not analyzed here either.
14376
14377 elsif Nkind (Decl) = N_Package_Instantiation
14378 and then not Is_Internal (Defining_Entity (Decl))
14379 then
14380 Append_Elmt (Decl, Previous_Instances);
14381
14382 -- For a subprogram instantiation, omit instantiations intrinsic
14383 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14384
14385 elsif Nkind (Decl) in N_Function_Instantiation
14386 | N_Procedure_Instantiation
14387 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14388 then
14389 Append_Elmt (Decl, Previous_Instances);
14390
14391 elsif Nkind (Decl) = N_Package_Declaration then
14392 Collect_Previous_Instances
14393 (Visible_Declarations (Specification (Decl)));
14394 Collect_Previous_Instances
14395 (Private_Declarations (Specification (Decl)));
14396
14397 -- Previous non-generic bodies may contain instances as well
14398
14399 elsif Nkind (Decl) = N_Package_Body
14400 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14401 then
14402 Collect_Previous_Instances (Declarations (Decl));
14403
14404 elsif Nkind (Decl) = N_Subprogram_Body
14405 and then not Acts_As_Spec (Decl)
14406 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14407 then
14408 Collect_Previous_Instances (Declarations (Decl));
14409 end if;
14410
14411 Next (Decl);
14412 end loop;
14413 end Collect_Previous_Instances;
14414
14415 -- Start of processing for Load_Parent_Of_Generic
14416
14417 begin
14418 if not In_Same_Source_Unit (N, Spec)
14419 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14420 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14421 and then not Is_In_Main_Unit (Spec))
14422 then
14423 -- Find body of parent of spec, and analyze it. A special case arises
14424 -- when the parent is an instantiation, that is to say when we are
14425 -- currently instantiating a nested generic. In that case, there is
14426 -- no separate file for the body of the enclosing instance. Instead,
14427 -- the enclosing body must be instantiated as if it were a pending
14428 -- instantiation, in order to produce the body for the nested generic
14429 -- we require now. Note that in that case the generic may be defined
14430 -- in a package body, the instance defined in the same package body,
14431 -- and the original enclosing body may not be in the main unit.
14432
14433 Inst_Node := Empty;
14434
14435 True_Parent := Parent (Spec);
14436 while Present (True_Parent)
14437 and then Nkind (True_Parent) /= N_Compilation_Unit
14438 loop
14439 if Nkind (True_Parent) = N_Package_Declaration
14440 and then
14441 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14442 then
14443 -- Parent is a compilation unit that is an instantiation, and
14444 -- instantiation node has been replaced with package decl.
14445
14446 Inst_Node := Original_Node (True_Parent);
14447 exit;
14448
14449 elsif Nkind (True_Parent) = N_Package_Declaration
14450 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14451 and then
14452 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14453 then
14454 -- Parent is a compilation unit that is an instantiation, but
14455 -- instantiation node has not been replaced with package decl.
14456
14457 Inst_Node := Unit (Parent (True_Parent));
14458 exit;
14459
14460 elsif Nkind (True_Parent) = N_Package_Declaration
14461 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14462 and then Present (Generic_Parent (Specification (True_Parent)))
14463 then
14464 -- Parent is an instantiation within another specification.
14465 -- Declaration for instance has been inserted before original
14466 -- instantiation node. A direct link would be preferable?
14467
14468 Inst_Node := Next (True_Parent);
14469 while Present (Inst_Node)
14470 and then Nkind (Inst_Node) /= N_Package_Instantiation
14471 loop
14472 Next (Inst_Node);
14473 end loop;
14474
14475 -- If the instance appears within a generic, and the generic
14476 -- unit is defined within a formal package of the enclosing
14477 -- generic, there is no generic body available, and none
14478 -- needed. A more precise test should be used ???
14479
14480 if No (Inst_Node) then
14481 return;
14482 end if;
14483
14484 exit;
14485
14486 -- If an ancestor of the generic comes from a formal package
14487 -- there is no source for the ancestor body. This is detected
14488 -- by examining the scope of the ancestor and its declaration.
14489 -- The body, if any is needed, will be available when the
14490 -- current unit (containing a formal package) is instantiated.
14491
14492 elsif Nkind (True_Parent) = N_Package_Specification
14493 and then Present (Generic_Parent (True_Parent))
14494 and then Nkind
14495 (Original_Node (Unit_Declaration_Node
14496 (Scope (Generic_Parent (True_Parent)))))
14497 = N_Formal_Package_Declaration
14498 then
14499 return;
14500
14501 else
14502 True_Parent := Parent (True_Parent);
14503 end if;
14504 end loop;
14505
14506 -- Case where we are currently instantiating a nested generic
14507
14508 if Present (Inst_Node) then
14509 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14510
14511 -- Instantiation node and declaration of instantiated package
14512 -- were exchanged when only the declaration was needed.
14513 -- Restore instantiation node before proceeding with body.
14514
14515 Set_Unit (Parent (True_Parent), Inst_Node);
14516 end if;
14517
14518 -- Now complete instantiation of enclosing body, if it appears in
14519 -- some other unit. If it appears in the current unit, the body
14520 -- will have been instantiated already.
14521
14522 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14523
14524 -- We need to determine the expander mode to instantiate the
14525 -- enclosing body. Because the generic body we need may use
14526 -- global entities declared in the enclosing package (including
14527 -- aggregates) it is in general necessary to compile this body
14528 -- with expansion enabled, except if we are within a generic
14529 -- package, in which case the usual generic rule applies.
14530
14531 declare
14532 Exp_Status : Boolean := True;
14533 Scop : Entity_Id;
14534
14535 begin
14536 -- Loop through scopes looking for generic package
14537
14538 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14539 while Present (Scop)
14540 and then Scop /= Standard_Standard
14541 loop
14542 if Ekind (Scop) = E_Generic_Package then
14543 Exp_Status := False;
14544 exit;
14545 end if;
14546
14547 Scop := Scope (Scop);
14548 end loop;
14549
14550 -- Collect previous instantiations in the unit that contains
14551 -- the desired generic.
14552
14553 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14554 and then not Body_Optional
14555 then
14556 declare
14557 Decl : Elmt_Id;
14558 Info : Pending_Body_Info;
14559 Par : Node_Id;
14560
14561 begin
14562 Par := Parent (Inst_Node);
14563 while Present (Par) loop
14564 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14565 Par := Parent (Par);
14566 end loop;
14567
14568 pragma Assert (Present (Par));
14569
14570 if Nkind (Par) = N_Package_Body then
14571 Collect_Previous_Instances (Declarations (Par));
14572
14573 elsif Nkind (Par) = N_Package_Declaration then
14574 Collect_Previous_Instances
14575 (Visible_Declarations (Specification (Par)));
14576 Collect_Previous_Instances
14577 (Private_Declarations (Specification (Par)));
14578
14579 else
14580 -- Enclosing unit is a subprogram body. In this
14581 -- case all instance bodies are processed in order
14582 -- and there is no need to collect them separately.
14583
14584 null;
14585 end if;
14586
14587 Decl := First_Elmt (Previous_Instances);
14588 while Present (Decl) loop
14589 Info :=
14590 (Act_Decl =>
14591 Instance_Spec (Node (Decl)),
14592 Config_Switches => Save_Config_Switches,
14593 Current_Sem_Unit =>
14594 Get_Code_Unit (Sloc (Node (Decl))),
14595 Expander_Status => Exp_Status,
14596 Inst_Node => Node (Decl),
14597 Local_Suppress_Stack_Top =>
14598 Local_Suppress_Stack_Top,
14599 Scope_Suppress => Scope_Suppress,
14600 Warnings => Save_Warnings);
14601
14602 -- Package instance
14603
14604 if Nkind (Node (Decl)) = N_Package_Instantiation
14605 then
14606 Instantiate_Package_Body
14607 (Info, Body_Optional => True);
14608
14609 -- Subprogram instance
14610
14611 else
14612 -- The instance_spec is in the wrapper package,
14613 -- usually followed by its local renaming
14614 -- declaration. See Build_Subprogram_Renaming
14615 -- for details. If the instance carries aspects,
14616 -- these result in the corresponding pragmas,
14617 -- inserted after the subprogram declaration.
14618 -- They must be skipped as well when retrieving
14619 -- the desired spec. Some of them may have been
14620 -- rewritten as null statements.
14621 -- A direct link would be more robust ???
14622
14623 declare
14624 Decl : Node_Id :=
14625 (Last (Visible_Declarations
14626 (Specification (Info.Act_Decl))));
14627 begin
14628 while Nkind (Decl) in
14629 N_Null_Statement |
14630 N_Pragma |
14631 N_Subprogram_Renaming_Declaration
14632 loop
14633 Decl := Prev (Decl);
14634 end loop;
14635
14636 Info.Act_Decl := Decl;
14637 end;
14638
14639 Instantiate_Subprogram_Body
14640 (Info, Body_Optional => True);
14641 end if;
14642
14643 Next_Elmt (Decl);
14644 end loop;
14645 end;
14646 end if;
14647
14648 Instantiate_Package_Body
14649 (Body_Info =>
14650 ((Act_Decl => True_Parent,
14651 Config_Switches => Save_Config_Switches,
14652 Current_Sem_Unit =>
14653 Get_Code_Unit (Sloc (Inst_Node)),
14654 Expander_Status => Exp_Status,
14655 Inst_Node => Inst_Node,
14656 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14657 Scope_Suppress => Scope_Suppress,
14658 Warnings => Save_Warnings)),
14659 Body_Optional => Body_Optional);
14660 end;
14661 end if;
14662
14663 -- Case where we are not instantiating a nested generic
14664
14665 else
14666 Opt.Style_Check := False;
14667 Expander_Mode_Save_And_Set (True);
14668 Load_Needed_Body (Comp_Unit, OK);
14669 Opt.Style_Check := Saved_Style_Check;
14670 Restore_Warnings (Saved_Warnings);
14671 Expander_Mode_Restore;
14672
14673 if not OK
14674 and then Unit_Requires_Body (Defining_Entity (Spec))
14675 and then not Body_Optional
14676 then
14677 declare
14678 Bname : constant Unit_Name_Type :=
14679 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14680
14681 begin
14682 -- In CodePeer mode, the missing body may make the analysis
14683 -- incomplete, but we do not treat it as fatal.
14684
14685 if CodePeer_Mode then
14686 return;
14687
14688 else
14689 Error_Msg_Unit_1 := Bname;
14690 Error_Msg_N ("this instantiation requires$!", N);
14691 Error_Msg_File_1 :=
14692 Get_File_Name (Bname, Subunit => False);
14693 Error_Msg_N ("\but file{ was not found!", N);
14694 raise Unrecoverable_Error;
14695 end if;
14696 end;
14697 end if;
14698 end if;
14699 end if;
14700
14701 -- If loading parent of the generic caused an instantiation circularity,
14702 -- we abandon compilation at this point, because otherwise in some cases
14703 -- we get into trouble with infinite recursions after this point.
14704
14705 if Circularity_Detected then
14706 raise Unrecoverable_Error;
14707 end if;
14708 end Load_Parent_Of_Generic;
14709
14710 ---------------------------------
14711 -- Map_Formal_Package_Entities --
14712 ---------------------------------
14713
14714 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14715 E1 : Entity_Id;
14716 E2 : Entity_Id;
14717
14718 begin
14719 Set_Instance_Of (Form, Act);
14720
14721 -- Traverse formal and actual package to map the corresponding entities.
14722 -- We skip over internal entities that may be generated during semantic
14723 -- analysis, and find the matching entities by name, given that they
14724 -- must appear in the same order.
14725
14726 E1 := First_Entity (Form);
14727 E2 := First_Entity (Act);
14728 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14729 -- Could this test be a single condition??? Seems like it could, and
14730 -- isn't FPE (Form) a constant anyway???
14731
14732 if not Is_Internal (E1)
14733 and then Present (Parent (E1))
14734 and then not Is_Class_Wide_Type (E1)
14735 and then not Is_Internal_Name (Chars (E1))
14736 then
14737 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14738 Next_Entity (E2);
14739 end loop;
14740
14741 if No (E2) then
14742 exit;
14743 else
14744 Set_Instance_Of (E1, E2);
14745
14746 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14747 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14748 end if;
14749
14750 if Is_Constrained (E1) then
14751 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14752 end if;
14753
14754 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14755 Map_Formal_Package_Entities (E1, E2);
14756 end if;
14757 end if;
14758 end if;
14759
14760 Next_Entity (E1);
14761 end loop;
14762 end Map_Formal_Package_Entities;
14763
14764 -----------------------
14765 -- Move_Freeze_Nodes --
14766 -----------------------
14767
14768 procedure Move_Freeze_Nodes
14769 (Out_Of : Entity_Id;
14770 After : Node_Id;
14771 L : List_Id)
14772 is
14773 Decl : Node_Id;
14774 Next_Decl : Node_Id;
14775 Next_Node : Node_Id := After;
14776 Spec : Node_Id;
14777
14778 function Is_Outer_Type (T : Entity_Id) return Boolean;
14779 -- Check whether entity is declared in a scope external to that of the
14780 -- generic unit.
14781
14782 -------------------
14783 -- Is_Outer_Type --
14784 -------------------
14785
14786 function Is_Outer_Type (T : Entity_Id) return Boolean is
14787 Scop : Entity_Id := Scope (T);
14788
14789 begin
14790 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14791 return True;
14792
14793 else
14794 while Scop /= Standard_Standard loop
14795 if Scop = Out_Of then
14796 return False;
14797 else
14798 Scop := Scope (Scop);
14799 end if;
14800 end loop;
14801
14802 return True;
14803 end if;
14804 end Is_Outer_Type;
14805
14806 -- Start of processing for Move_Freeze_Nodes
14807
14808 begin
14809 if No (L) then
14810 return;
14811 end if;
14812
14813 -- First remove the freeze nodes that may appear before all other
14814 -- declarations.
14815
14816 Decl := First (L);
14817 while Present (Decl)
14818 and then Nkind (Decl) = N_Freeze_Entity
14819 and then Is_Outer_Type (Entity (Decl))
14820 loop
14821 Decl := Remove_Head (L);
14822 Insert_After (Next_Node, Decl);
14823 Set_Analyzed (Decl, False);
14824 Next_Node := Decl;
14825 Decl := First (L);
14826 end loop;
14827
14828 -- Next scan the list of declarations and remove each freeze node that
14829 -- appears ahead of the current node.
14830
14831 while Present (Decl) loop
14832 while Present (Next (Decl))
14833 and then Nkind (Next (Decl)) = N_Freeze_Entity
14834 and then Is_Outer_Type (Entity (Next (Decl)))
14835 loop
14836 Next_Decl := Remove_Next (Decl);
14837 Insert_After (Next_Node, Next_Decl);
14838 Set_Analyzed (Next_Decl, False);
14839 Next_Node := Next_Decl;
14840 end loop;
14841
14842 -- If the declaration is a nested package or concurrent type, then
14843 -- recurse. Nested generic packages will have been processed from the
14844 -- inside out.
14845
14846 case Nkind (Decl) is
14847 when N_Package_Declaration =>
14848 Spec := Specification (Decl);
14849
14850 when N_Task_Type_Declaration =>
14851 Spec := Task_Definition (Decl);
14852
14853 when N_Protected_Type_Declaration =>
14854 Spec := Protected_Definition (Decl);
14855
14856 when others =>
14857 Spec := Empty;
14858 end case;
14859
14860 if Present (Spec) then
14861 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14862 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14863 end if;
14864
14865 Next (Decl);
14866 end loop;
14867 end Move_Freeze_Nodes;
14868
14869 ----------------
14870 -- Next_Assoc --
14871 ----------------
14872
14873 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14874 begin
14875 return Generic_Renamings.Table (E).Next_In_HTable;
14876 end Next_Assoc;
14877
14878 ------------------------
14879 -- Preanalyze_Actuals --
14880 ------------------------
14881
14882 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14883 procedure Perform_Appropriate_Analysis (N : Node_Id);
14884 -- Determine if the actuals we are analyzing come from a generic
14885 -- instantiation that is a library unit and dispatch accordingly.
14886
14887 ----------------------------------
14888 -- Perform_Appropriate_Analysis --
14889 ----------------------------------
14890
14891 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14892 begin
14893 -- When we have a library instantiation we cannot allow any expansion
14894 -- to occur, since there may be no place to put it. Instead, in that
14895 -- case we perform a preanalysis of the actual.
14896
14897 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14898 Preanalyze (N);
14899 else
14900 Analyze (N);
14901 end if;
14902 end Perform_Appropriate_Analysis;
14903
14904 -- Local variables
14905
14906 Errs : constant Nat := Serious_Errors_Detected;
14907
14908 Assoc : Node_Id;
14909 Act : Node_Id;
14910
14911 Cur : Entity_Id := Empty;
14912 -- Current homograph of the instance name
14913
14914 Vis : Boolean := False;
14915 -- Saved visibility status of the current homograph
14916
14917 -- Start of processing for Preanalyze_Actuals
14918
14919 begin
14920 Assoc := First (Generic_Associations (N));
14921
14922 -- If the instance is a child unit, its name may hide an outer homonym,
14923 -- so make it invisible to perform name resolution on the actuals.
14924
14925 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14926 and then Present
14927 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14928 then
14929 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14930
14931 if Is_Compilation_Unit (Cur) then
14932 Vis := Is_Immediately_Visible (Cur);
14933 Set_Is_Immediately_Visible (Cur, False);
14934 else
14935 Cur := Empty;
14936 end if;
14937 end if;
14938
14939 while Present (Assoc) loop
14940 if Nkind (Assoc) /= N_Others_Choice then
14941 Act := Explicit_Generic_Actual_Parameter (Assoc);
14942
14943 -- Within a nested instantiation, a defaulted actual is an empty
14944 -- association, so nothing to analyze. If the subprogram actual
14945 -- is an attribute, analyze prefix only, because actual is not a
14946 -- complete attribute reference.
14947
14948 -- If actual is an allocator, analyze expression only. The full
14949 -- analysis can generate code, and if instance is a compilation
14950 -- unit we have to wait until the package instance is installed
14951 -- to have a proper place to insert this code.
14952
14953 -- String literals may be operators, but at this point we do not
14954 -- know whether the actual is a formal subprogram or a string.
14955
14956 if No (Act) then
14957 null;
14958
14959 elsif Nkind (Act) = N_Attribute_Reference then
14960 Perform_Appropriate_Analysis (Prefix (Act));
14961
14962 elsif Nkind (Act) = N_Explicit_Dereference then
14963 Perform_Appropriate_Analysis (Prefix (Act));
14964
14965 elsif Nkind (Act) = N_Allocator then
14966 declare
14967 Expr : constant Node_Id := Expression (Act);
14968
14969 begin
14970 if Nkind (Expr) = N_Subtype_Indication then
14971 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14972
14973 -- Analyze separately each discriminant constraint, when
14974 -- given with a named association.
14975
14976 declare
14977 Constr : Node_Id;
14978
14979 begin
14980 Constr := First (Constraints (Constraint (Expr)));
14981 while Present (Constr) loop
14982 if Nkind (Constr) = N_Discriminant_Association then
14983 Perform_Appropriate_Analysis
14984 (Expression (Constr));
14985 else
14986 Perform_Appropriate_Analysis (Constr);
14987 end if;
14988
14989 Next (Constr);
14990 end loop;
14991 end;
14992
14993 else
14994 Perform_Appropriate_Analysis (Expr);
14995 end if;
14996 end;
14997
14998 elsif Nkind (Act) /= N_Operator_Symbol then
14999 Perform_Appropriate_Analysis (Act);
15000
15001 -- Within a package instance, mark actuals that are limited
15002 -- views, so their use can be moved to the body of the
15003 -- enclosing unit.
15004
15005 if Is_Entity_Name (Act)
15006 and then Is_Type (Entity (Act))
15007 and then From_Limited_With (Entity (Act))
15008 and then Present (Inst)
15009 then
15010 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15011 end if;
15012 end if;
15013
15014 if Errs /= Serious_Errors_Detected then
15015
15016 -- Do a minimal analysis of the generic, to prevent spurious
15017 -- warnings complaining about the generic being unreferenced,
15018 -- before abandoning the instantiation.
15019
15020 Perform_Appropriate_Analysis (Name (N));
15021
15022 if Is_Entity_Name (Name (N))
15023 and then Etype (Name (N)) /= Any_Type
15024 then
15025 Generate_Reference (Entity (Name (N)), Name (N));
15026 Set_Is_Instantiated (Entity (Name (N)));
15027 end if;
15028
15029 if Present (Cur) then
15030
15031 -- For the case of a child instance hiding an outer homonym,
15032 -- provide additional warning which might explain the error.
15033
15034 Set_Is_Immediately_Visible (Cur, Vis);
15035 Error_Msg_NE
15036 ("& hides outer unit with the same name??",
15037 N, Defining_Unit_Name (N));
15038 end if;
15039
15040 Abandon_Instantiation (Act);
15041 end if;
15042 end if;
15043
15044 Next (Assoc);
15045 end loop;
15046
15047 if Present (Cur) then
15048 Set_Is_Immediately_Visible (Cur, Vis);
15049 end if;
15050 end Preanalyze_Actuals;
15051
15052 -------------------------------
15053 -- Provide_Completing_Bodies --
15054 -------------------------------
15055
15056 procedure Provide_Completing_Bodies (N : Node_Id) is
15057 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15058 -- Generate the completing body for subprogram declaration Subp_Decl
15059
15060 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15061 -- Generating completing bodies for all subprograms found in declarative
15062 -- list Decls.
15063
15064 ---------------------------
15065 -- Build_Completing_Body --
15066 ---------------------------
15067
15068 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15069 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15070 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15071 Spec : Node_Id;
15072
15073 begin
15074 -- Nothing to do if the subprogram already has a completing body
15075
15076 if Present (Corresponding_Body (Subp_Decl)) then
15077 return;
15078
15079 -- Mark the function as having a valid return statement even though
15080 -- the body contains a single raise statement.
15081
15082 elsif Ekind (Subp_Id) = E_Function then
15083 Set_Return_Present (Subp_Id);
15084 end if;
15085
15086 -- Clone the specification to obtain new entities and reset the only
15087 -- semantic field.
15088
15089 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15090 Set_Generic_Parent (Spec, Empty);
15091
15092 -- Generate:
15093 -- function Func ... return ... is
15094 -- <or>
15095 -- procedure Proc ... is
15096 -- begin
15097 -- raise Program_Error with "access before elaboration";
15098 -- edn Proc;
15099
15100 Insert_After_And_Analyze (Subp_Decl,
15101 Make_Subprogram_Body (Loc,
15102 Specification => Spec,
15103 Declarations => New_List,
15104 Handled_Statement_Sequence =>
15105 Make_Handled_Sequence_Of_Statements (Loc,
15106 Statements => New_List (
15107 Make_Raise_Program_Error (Loc,
15108 Reason => PE_Access_Before_Elaboration)))));
15109 end Build_Completing_Body;
15110
15111 ----------------------------------
15112 -- Provide_Completing_Bodies_In --
15113 ----------------------------------
15114
15115 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15116 Decl : Node_Id;
15117
15118 begin
15119 if Present (Decls) then
15120 Decl := First (Decls);
15121 while Present (Decl) loop
15122 Provide_Completing_Bodies (Decl);
15123 Next (Decl);
15124 end loop;
15125 end if;
15126 end Provide_Completing_Bodies_In;
15127
15128 -- Local variables
15129
15130 Spec : Node_Id;
15131
15132 -- Start of processing for Provide_Completing_Bodies
15133
15134 begin
15135 if Nkind (N) = N_Package_Declaration then
15136 Spec := Specification (N);
15137
15138 Push_Scope (Defining_Entity (N));
15139 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15140 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15141 Pop_Scope;
15142
15143 elsif Nkind (N) = N_Subprogram_Declaration then
15144 Build_Completing_Body (N);
15145 end if;
15146 end Provide_Completing_Bodies;
15147
15148 -------------------
15149 -- Remove_Parent --
15150 -------------------
15151
15152 procedure Remove_Parent (In_Body : Boolean := False) is
15153 S : Entity_Id := Current_Scope;
15154 -- S is the scope containing the instantiation just completed. The scope
15155 -- stack contains the parent instances of the instantiation, followed by
15156 -- the original S.
15157
15158 Cur_P : Entity_Id;
15159 E : Entity_Id;
15160 P : Entity_Id;
15161 Hidden : Elmt_Id;
15162
15163 begin
15164 -- After child instantiation is complete, remove from scope stack the
15165 -- extra copy of the current scope, and then remove parent instances.
15166
15167 if not In_Body then
15168 Pop_Scope;
15169
15170 while Current_Scope /= S loop
15171 P := Current_Scope;
15172 End_Package_Scope (Current_Scope);
15173
15174 if In_Open_Scopes (P) then
15175 E := First_Entity (P);
15176 while Present (E) loop
15177 Set_Is_Immediately_Visible (E, True);
15178 Next_Entity (E);
15179 end loop;
15180
15181 -- If instantiation is declared in a block, it is the enclosing
15182 -- scope that might be a parent instance. Note that only one
15183 -- block can be involved, because the parent instances have
15184 -- been installed within it.
15185
15186 if Ekind (P) = E_Block then
15187 Cur_P := Scope (P);
15188 else
15189 Cur_P := P;
15190 end if;
15191
15192 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15193 -- We are within an instance of some sibling. Retain
15194 -- visibility of parent, for proper subsequent cleanup, and
15195 -- reinstall private declarations as well.
15196
15197 Set_In_Private_Part (P);
15198 Install_Private_Declarations (P);
15199 end if;
15200
15201 -- If the ultimate parent is a top-level unit recorded in
15202 -- Instance_Parent_Unit, then reset its visibility to what it was
15203 -- before instantiation. (It's not clear what the purpose is of
15204 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15205 -- present before the ultimate parent test was added.???)
15206
15207 elsif not In_Open_Scopes (Scope (P))
15208 or else (P = Instance_Parent_Unit
15209 and then not Parent_Unit_Visible)
15210 then
15211 Set_Is_Immediately_Visible (P, False);
15212
15213 -- If the current scope is itself an instantiation of a generic
15214 -- nested within P, and we are in the private part of body of this
15215 -- instantiation, restore the full views of P, that were removed
15216 -- in End_Package_Scope above. This obscure case can occur when a
15217 -- subunit of a generic contains an instance of a child unit of
15218 -- its generic parent unit.
15219
15220 elsif S = Current_Scope and then Is_Generic_Instance (S) then
15221 declare
15222 Par : constant Entity_Id :=
15223 Generic_Parent (Package_Specification (S));
15224 begin
15225 if Present (Par)
15226 and then P = Scope (Par)
15227 and then (In_Package_Body (S) or else In_Private_Part (S))
15228 then
15229 Set_In_Private_Part (P);
15230 Install_Private_Declarations (P);
15231 end if;
15232 end;
15233 end if;
15234 end loop;
15235
15236 -- Reset visibility of entities in the enclosing scope
15237
15238 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15239
15240 Hidden := First_Elmt (Hidden_Entities);
15241 while Present (Hidden) loop
15242 Set_Is_Immediately_Visible (Node (Hidden), True);
15243 Next_Elmt (Hidden);
15244 end loop;
15245
15246 else
15247 -- Each body is analyzed separately, and there is no context that
15248 -- needs preserving from one body instance to the next, so remove all
15249 -- parent scopes that have been installed.
15250
15251 while Present (S) loop
15252 End_Package_Scope (S);
15253 Set_Is_Immediately_Visible (S, False);
15254 S := Current_Scope;
15255 exit when S = Standard_Standard;
15256 end loop;
15257 end if;
15258 end Remove_Parent;
15259
15260 -----------------
15261 -- Restore_Env --
15262 -----------------
15263
15264 procedure Restore_Env is
15265 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15266
15267 begin
15268 if No (Current_Instantiated_Parent.Act_Id) then
15269 -- Restore environment after subprogram inlining
15270
15271 Restore_Private_Views (Empty);
15272 end if;
15273
15274 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15275 Exchanged_Views := Saved.Exchanged_Views;
15276 Hidden_Entities := Saved.Hidden_Entities;
15277 Current_Sem_Unit := Saved.Current_Sem_Unit;
15278 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15279 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15280
15281 Restore_Config_Switches (Saved.Switches);
15282
15283 Instance_Envs.Decrement_Last;
15284 end Restore_Env;
15285
15286 ---------------------------
15287 -- Restore_Private_Views --
15288 ---------------------------
15289
15290 procedure Restore_Private_Views
15291 (Pack_Id : Entity_Id;
15292 Is_Package : Boolean := True)
15293 is
15294 M : Elmt_Id;
15295 E : Entity_Id;
15296 Typ : Entity_Id;
15297 Dep_Elmt : Elmt_Id;
15298 Dep_Typ : Node_Id;
15299
15300 procedure Restore_Nested_Formal (Formal : Entity_Id);
15301 -- Hide the generic formals of formal packages declared with box which
15302 -- were reachable in the current instantiation.
15303
15304 ---------------------------
15305 -- Restore_Nested_Formal --
15306 ---------------------------
15307
15308 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15309 Ent : Entity_Id;
15310
15311 begin
15312 if Present (Renamed_Object (Formal))
15313 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
15314 then
15315 return;
15316
15317 elsif Present (Associated_Formal_Package (Formal)) then
15318 Ent := First_Entity (Formal);
15319 while Present (Ent) loop
15320 exit when Ekind (Ent) = E_Package
15321 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15322
15323 Set_Is_Hidden (Ent);
15324 Set_Is_Potentially_Use_Visible (Ent, False);
15325
15326 -- If package, then recurse
15327
15328 if Ekind (Ent) = E_Package then
15329 Restore_Nested_Formal (Ent);
15330 end if;
15331
15332 Next_Entity (Ent);
15333 end loop;
15334 end if;
15335 end Restore_Nested_Formal;
15336
15337 -- Start of processing for Restore_Private_Views
15338
15339 begin
15340 M := First_Elmt (Exchanged_Views);
15341 while Present (M) loop
15342 Typ := Node (M);
15343
15344 -- Subtypes of types whose views have been exchanged, and that are
15345 -- defined within the instance, were not on the Private_Dependents
15346 -- list on entry to the instance, so they have to be exchanged
15347 -- explicitly now, in order to remain consistent with the view of the
15348 -- parent type.
15349
15350 if Ekind (Typ) in E_Private_Type
15351 | E_Limited_Private_Type
15352 | E_Record_Type_With_Private
15353 then
15354 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15355 while Present (Dep_Elmt) loop
15356 Dep_Typ := Node (Dep_Elmt);
15357
15358 if Scope (Dep_Typ) = Pack_Id
15359 and then Present (Full_View (Dep_Typ))
15360 then
15361 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15362 Exchange_Declarations (Dep_Typ);
15363 end if;
15364
15365 Next_Elmt (Dep_Elmt);
15366 end loop;
15367 end if;
15368
15369 Exchange_Declarations (Node (M));
15370 Next_Elmt (M);
15371 end loop;
15372
15373 if No (Pack_Id) then
15374 return;
15375 end if;
15376
15377 -- Make the generic formal parameters private, and make the formal types
15378 -- into subtypes of the actuals again.
15379
15380 E := First_Entity (Pack_Id);
15381 while Present (E) loop
15382 Set_Is_Hidden (E, True);
15383
15384 if Is_Type (E)
15385 and then Nkind (Parent (E)) = N_Subtype_Declaration
15386 then
15387 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15388 -- as it is needed to identify the subtype with the type it
15389 -- renames, when there are conversions between access types
15390 -- to these.
15391
15392 if GNATprove_Mode then
15393 null;
15394
15395 -- If the actual for E is itself a generic actual type from
15396 -- an enclosing instance, E is still a generic actual type
15397 -- outside of the current instance. This matter when resolving
15398 -- an overloaded call that may be ambiguous in the enclosing
15399 -- instance, when two of its actuals coincide.
15400
15401 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
15402 and then Is_Generic_Actual_Type
15403 (Entity (Subtype_Indication (Parent (E))))
15404 then
15405 null;
15406 else
15407 Set_Is_Generic_Actual_Type (E, False);
15408
15409 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15410 -- flag also on the Full_View if the type is private, since it
15411 -- was set also on this Full_View. However, this flag is relied
15412 -- upon by Covers to spot "types exported from instantiations"
15413 -- which are implicit Full_Views built for instantiations made
15414 -- on private types and we get type mismatches if we do it when
15415 -- the block exchanging the declarations below triggers ???
15416
15417 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15418 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15419 -- end if;
15420 end if;
15421
15422 -- An unusual case of aliasing: the actual may also be directly
15423 -- visible in the generic, and be private there, while it is fully
15424 -- visible in the context of the instance. The internal subtype
15425 -- is private in the instance but has full visibility like its
15426 -- parent in the enclosing scope. This enforces the invariant that
15427 -- the privacy status of all private dependents of a type coincide
15428 -- with that of the parent type. This can only happen when a
15429 -- generic child unit is instantiated within a sibling.
15430
15431 if Is_Private_Type (E)
15432 and then not Is_Private_Type (Etype (E))
15433 then
15434 Exchange_Declarations (E);
15435 end if;
15436
15437 elsif Ekind (E) = E_Package then
15438
15439 -- The end of the renaming list is the renaming of the generic
15440 -- package itself. If the instance is a subprogram, all entities
15441 -- in the corresponding package are renamings. If this entity is
15442 -- a formal package, make its own formals private as well. The
15443 -- actual in this case is itself the renaming of an instantiation.
15444 -- If the entity is not a package renaming, it is the entity
15445 -- created to validate formal package actuals: ignore it.
15446
15447 -- If the actual is itself a formal package for the enclosing
15448 -- generic, or the actual for such a formal package, it remains
15449 -- visible on exit from the instance, and therefore nothing needs
15450 -- to be done either, except to keep it accessible.
15451
15452 if Is_Package and then Renamed_Object (E) = Pack_Id then
15453 exit;
15454
15455 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15456 null;
15457
15458 elsif
15459 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
15460 then
15461 Set_Is_Hidden (E, False);
15462
15463 else
15464 declare
15465 Act_P : constant Entity_Id := Renamed_Object (E);
15466 Id : Entity_Id;
15467
15468 begin
15469 Id := First_Entity (Act_P);
15470 while Present (Id)
15471 and then Id /= First_Private_Entity (Act_P)
15472 loop
15473 exit when Ekind (Id) = E_Package
15474 and then Renamed_Object (Id) = Act_P;
15475
15476 Set_Is_Hidden (Id, True);
15477 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15478
15479 if Ekind (Id) = E_Package then
15480 Restore_Nested_Formal (Id);
15481 end if;
15482
15483 Next_Entity (Id);
15484 end loop;
15485 end;
15486 end if;
15487 end if;
15488
15489 Next_Entity (E);
15490 end loop;
15491 end Restore_Private_Views;
15492
15493 --------------
15494 -- Save_Env --
15495 --------------
15496
15497 procedure Save_Env
15498 (Gen_Unit : Entity_Id;
15499 Act_Unit : Entity_Id)
15500 is
15501 begin
15502 Init_Env;
15503 Set_Instance_Env (Gen_Unit, Act_Unit);
15504 end Save_Env;
15505
15506 ----------------------------
15507 -- Save_Global_References --
15508 ----------------------------
15509
15510 procedure Save_Global_References (Templ : Node_Id) is
15511
15512 -- ??? it is horrible to use global variables in highly recursive code
15513
15514 E : Entity_Id;
15515 -- The entity of the current associated node
15516
15517 Gen_Scope : Entity_Id;
15518 -- The scope of the generic for which references are being saved
15519
15520 N2 : Node_Id;
15521 -- The current associated node
15522
15523 function Is_Global (E : Entity_Id) return Boolean;
15524 -- Check whether entity is defined outside of generic unit. Examine the
15525 -- scope of an entity, and the scope of the scope, etc, until we find
15526 -- either Standard, in which case the entity is global, or the generic
15527 -- unit itself, which indicates that the entity is local. If the entity
15528 -- is the generic unit itself, as in the case of a recursive call, or
15529 -- the enclosing generic unit, if different from the current scope, then
15530 -- it is local as well, because it will be replaced at the point of
15531 -- instantiation. On the other hand, if it is a reference to a child
15532 -- unit of a common ancestor, which appears in an instantiation, it is
15533 -- global because it is used to denote a specific compilation unit at
15534 -- the time the instantiations will be analyzed.
15535
15536 procedure Qualify_Universal_Operands
15537 (Op : Node_Id;
15538 Func_Call : Node_Id);
15539 -- Op denotes a binary or unary operator in generic template Templ. Node
15540 -- Func_Call is the function call alternative of the operator within the
15541 -- the analyzed copy of the template. Change each operand which yields a
15542 -- universal type by wrapping it into a qualified expression
15543 --
15544 -- Actual_Typ'(Operand)
15545 --
15546 -- where Actual_Typ is the type of corresponding actual parameter of
15547 -- Operand in Func_Call.
15548
15549 procedure Reset_Entity (N : Node_Id);
15550 -- Save semantic information on global entity so that it is not resolved
15551 -- again at instantiation time.
15552
15553 procedure Save_Entity_Descendants (N : Node_Id);
15554 -- Apply Save_Global_References to the two syntactic descendants of
15555 -- non-terminal nodes that carry an Associated_Node and are processed
15556 -- through Reset_Entity. Once the global entity (if any) has been
15557 -- captured together with its type, only two syntactic descendants need
15558 -- to be traversed to complete the processing of the tree rooted at N.
15559 -- This applies to Selected_Components, Expanded_Names, and to Operator
15560 -- nodes. N can also be a character literal, identifier, or operator
15561 -- symbol node, but the call has no effect in these cases.
15562
15563 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15564 -- Default actuals in nested instances must be handled specially
15565 -- because there is no link to them from the original tree. When an
15566 -- actual subprogram is given by a default, we add an explicit generic
15567 -- association for it in the instantiation node. When we save the
15568 -- global references on the name of the instance, we recover the list
15569 -- of generic associations, and add an explicit one to the original
15570 -- generic tree, through which a global actual can be preserved.
15571 -- Similarly, if a child unit is instantiated within a sibling, in the
15572 -- context of the parent, we must preserve the identifier of the parent
15573 -- so that it can be properly resolved in a subsequent instantiation.
15574
15575 procedure Save_Global_Descendant (D : Union_Id);
15576 -- Apply Save_References recursively to the descendants of node D
15577
15578 procedure Save_References (N : Node_Id);
15579 -- This is the recursive procedure that does the work, once the
15580 -- enclosing generic scope has been established.
15581
15582 ---------------
15583 -- Is_Global --
15584 ---------------
15585
15586 function Is_Global (E : Entity_Id) return Boolean is
15587 Se : Entity_Id;
15588
15589 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15590 -- Determine whether the parent node of a reference to a child unit
15591 -- denotes an instantiation or a formal package, in which case the
15592 -- reference to the child unit is global, even if it appears within
15593 -- the current scope (e.g. when the instance appears within the body
15594 -- of an ancestor).
15595
15596 ----------------------
15597 -- Is_Instance_Node --
15598 ----------------------
15599
15600 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15601 begin
15602 return Nkind (Decl) in N_Generic_Instantiation
15603 or else
15604 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15605 end Is_Instance_Node;
15606
15607 -- Start of processing for Is_Global
15608
15609 begin
15610 if E = Gen_Scope then
15611 return False;
15612
15613 elsif E = Standard_Standard then
15614 return True;
15615
15616 elsif Is_Child_Unit (E)
15617 and then (Is_Instance_Node (Parent (N2))
15618 or else (Nkind (Parent (N2)) = N_Expanded_Name
15619 and then N2 = Selector_Name (Parent (N2))
15620 and then
15621 Is_Instance_Node (Parent (Parent (N2)))))
15622 then
15623 return True;
15624
15625 else
15626 Se := Scope (E);
15627 while Se /= Gen_Scope loop
15628 if Se = Standard_Standard then
15629 return True;
15630 else
15631 Se := Scope (Se);
15632 end if;
15633 end loop;
15634
15635 return False;
15636 end if;
15637 end Is_Global;
15638
15639 --------------------------------
15640 -- Qualify_Universal_Operands --
15641 --------------------------------
15642
15643 procedure Qualify_Universal_Operands
15644 (Op : Node_Id;
15645 Func_Call : Node_Id)
15646 is
15647 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15648 -- Rewrite operand Opnd as a qualified expression of the form
15649 --
15650 -- Actual_Typ'(Opnd)
15651 --
15652 -- where Actual is the corresponding actual parameter of Opnd in
15653 -- function call Func_Call.
15654
15655 function Qualify_Type
15656 (Loc : Source_Ptr;
15657 Typ : Entity_Id) return Node_Id;
15658 -- Qualify type Typ by creating a selected component of the form
15659 --
15660 -- Scope_Of_Typ.Typ
15661
15662 ---------------------
15663 -- Qualify_Operand --
15664 ---------------------
15665
15666 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15667 Loc : constant Source_Ptr := Sloc (Opnd);
15668 Typ : constant Entity_Id := Etype (Actual);
15669 Mark : Node_Id;
15670 Qual : Node_Id;
15671
15672 begin
15673 -- Qualify the operand when it is of a universal type. Note that
15674 -- the template is unanalyzed and it is not possible to directly
15675 -- query the type. This transformation is not done when the type
15676 -- of the actual is internally generated because the type will be
15677 -- regenerated in the instance.
15678
15679 if Yields_Universal_Type (Opnd)
15680 and then Comes_From_Source (Typ)
15681 and then not Is_Hidden (Typ)
15682 then
15683 -- The type of the actual may be a global reference. Save this
15684 -- information by creating a reference to it.
15685
15686 if Is_Global (Typ) then
15687 Mark := New_Occurrence_Of (Typ, Loc);
15688
15689 -- Otherwise rely on resolution to find the proper type within
15690 -- the instance.
15691
15692 else
15693 Mark := Qualify_Type (Loc, Typ);
15694 end if;
15695
15696 Qual :=
15697 Make_Qualified_Expression (Loc,
15698 Subtype_Mark => Mark,
15699 Expression => Relocate_Node (Opnd));
15700
15701 -- Mark the qualification to distinguish it from other source
15702 -- constructs and signal the instantiation mechanism that this
15703 -- node requires special processing. See Copy_Generic_Node for
15704 -- details.
15705
15706 Set_Is_Qualified_Universal_Literal (Qual);
15707
15708 Rewrite (Opnd, Qual);
15709 end if;
15710 end Qualify_Operand;
15711
15712 ------------------
15713 -- Qualify_Type --
15714 ------------------
15715
15716 function Qualify_Type
15717 (Loc : Source_Ptr;
15718 Typ : Entity_Id) return Node_Id
15719 is
15720 Scop : constant Entity_Id := Scope (Typ);
15721 Result : Node_Id;
15722
15723 begin
15724 Result := Make_Identifier (Loc, Chars (Typ));
15725
15726 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15727 Result :=
15728 Make_Selected_Component (Loc,
15729 Prefix => Make_Identifier (Loc, Chars (Scop)),
15730 Selector_Name => Result);
15731 end if;
15732
15733 return Result;
15734 end Qualify_Type;
15735
15736 -- Local variables
15737
15738 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15739
15740 -- Start of processing for Qualify_Universal_Operands
15741
15742 begin
15743 if Nkind (Op) in N_Binary_Op then
15744 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15745 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15746
15747 elsif Nkind (Op) in N_Unary_Op then
15748 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15749 end if;
15750 end Qualify_Universal_Operands;
15751
15752 ------------------
15753 -- Reset_Entity --
15754 ------------------
15755
15756 procedure Reset_Entity (N : Node_Id) is
15757 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15758 -- If the type of N2 is global to the generic unit, save the type in
15759 -- the generic node. Just as we perform name capture for explicit
15760 -- references within the generic, we must capture the global types
15761 -- of local entities because they may participate in resolution in
15762 -- the instance.
15763
15764 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15765 -- Find the ultimate ancestor of the current unit. If it is not a
15766 -- generic unit, then the name of the current unit in the prefix of
15767 -- an expanded name must be replaced with its generic homonym to
15768 -- ensure that it will be properly resolved in an instance.
15769
15770 ---------------------
15771 -- Set_Global_Type --
15772 ---------------------
15773
15774 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15775 Typ : constant Entity_Id := Etype (N2);
15776
15777 begin
15778 Set_Etype (N, Typ);
15779
15780 -- If the entity of N is not the associated node, this is a
15781 -- nested generic and it has an associated node as well, whose
15782 -- type is already the full view (see below). Indicate that the
15783 -- original node has a private view.
15784
15785 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15786 Set_Has_Private_View (N);
15787 end if;
15788
15789 -- If not a private type, nothing else to do
15790
15791 if not Is_Private_Type (Typ) then
15792 null;
15793
15794 -- If it is a derivation of a private type in a context where no
15795 -- full view is needed, nothing to do either.
15796
15797 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15798 null;
15799
15800 -- Otherwise mark the type for flipping and use the full view when
15801 -- available.
15802
15803 else
15804 Set_Has_Private_View (N);
15805
15806 if Present (Full_View (Typ)) then
15807 Set_Etype (N2, Full_View (Typ));
15808 end if;
15809 end if;
15810
15811 if Is_Floating_Point_Type (Typ)
15812 and then Has_Dimension_System (Typ)
15813 then
15814 Copy_Dimensions (N2, N);
15815 end if;
15816 end Set_Global_Type;
15817
15818 ------------------
15819 -- Top_Ancestor --
15820 ------------------
15821
15822 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15823 Par : Entity_Id;
15824
15825 begin
15826 Par := E;
15827 while Is_Child_Unit (Par) loop
15828 Par := Scope (Par);
15829 end loop;
15830
15831 return Par;
15832 end Top_Ancestor;
15833
15834 -- Start of processing for Reset_Entity
15835
15836 begin
15837 N2 := Get_Associated_Node (N);
15838 E := Entity (N2);
15839
15840 if Present (E) then
15841
15842 -- If the node is an entry call to an entry in an enclosing task,
15843 -- it is rewritten as a selected component. No global entity to
15844 -- preserve in this case, since the expansion will be redone in
15845 -- the instance.
15846
15847 if Nkind (E) not in N_Entity then
15848 Set_Associated_Node (N, Empty);
15849 Set_Etype (N, Empty);
15850 return;
15851 end if;
15852
15853 -- If the entity is an itype created as a subtype of an access
15854 -- type with a null exclusion restore source entity for proper
15855 -- visibility. The itype will be created anew in the instance.
15856
15857 if Is_Itype (E)
15858 and then Ekind (E) = E_Access_Subtype
15859 and then Is_Entity_Name (N)
15860 and then Chars (Etype (E)) = Chars (N)
15861 then
15862 E := Etype (E);
15863 Set_Entity (N2, E);
15864 Set_Etype (N2, E);
15865 end if;
15866
15867 if Is_Global (E) then
15868 Set_Global_Type (N, N2);
15869
15870 elsif Nkind (N) = N_Op_Concat
15871 and then Is_Generic_Type (Etype (N2))
15872 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15873 or else
15874 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15875 and then Is_Intrinsic_Subprogram (E)
15876 then
15877 null;
15878
15879 -- Entity is local. Mark generic node as unresolved. Note that now
15880 -- it does not have an entity.
15881
15882 else
15883 Set_Associated_Node (N, Empty);
15884 Set_Etype (N, Empty);
15885 end if;
15886
15887 if Nkind (Parent (N)) in N_Generic_Instantiation
15888 and then N = Name (Parent (N))
15889 then
15890 Save_Global_Defaults (Parent (N), Parent (N2));
15891 end if;
15892
15893 elsif Nkind (Parent (N)) = N_Selected_Component
15894 and then Nkind (Parent (N2)) = N_Expanded_Name
15895 then
15896 -- In case of previous errors, the tree might be malformed
15897
15898 if No (Entity (Parent (N2))) then
15899 null;
15900
15901 elsif Is_Global (Entity (Parent (N2))) then
15902 Change_Selected_Component_To_Expanded_Name (Parent (N));
15903 Set_Associated_Node (Parent (N), Parent (N2));
15904 Set_Global_Type (Parent (N), Parent (N2));
15905 Save_Entity_Descendants (N);
15906
15907 -- If this is a reference to the current generic entity, replace
15908 -- by the name of the generic homonym of the current package. This
15909 -- is because in an instantiation Par.P.Q will not resolve to the
15910 -- name of the instance, whose enclosing scope is not necessarily
15911 -- Par. We use the generic homonym rather that the name of the
15912 -- generic itself because it may be hidden by a local declaration.
15913
15914 elsif In_Open_Scopes (Entity (Parent (N2)))
15915 and then not
15916 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15917 then
15918 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15919 Rewrite (Parent (N),
15920 Make_Identifier (Sloc (N),
15921 Chars =>
15922 Chars (Generic_Homonym (Entity (Parent (N2))))));
15923 else
15924 Rewrite (Parent (N),
15925 Make_Identifier (Sloc (N),
15926 Chars => Chars (Selector_Name (Parent (N2)))));
15927 end if;
15928 end if;
15929
15930 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15931 and then Parent (N) = Name (Parent (Parent (N)))
15932 then
15933 Save_Global_Defaults
15934 (Parent (Parent (N)), Parent (Parent (N2)));
15935 end if;
15936
15937 -- A selected component may denote a static constant that has been
15938 -- folded. If the static constant is global to the generic, capture
15939 -- its value. Otherwise the folding will happen in any instantiation.
15940
15941 elsif Nkind (Parent (N)) = N_Selected_Component
15942 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
15943 then
15944 if Present (Entity (Original_Node (Parent (N2))))
15945 and then Is_Global (Entity (Original_Node (Parent (N2))))
15946 then
15947 Rewrite (Parent (N), New_Copy (Parent (N2)));
15948 Set_Analyzed (Parent (N), False);
15949 end if;
15950
15951 -- A selected component may be transformed into a parameterless
15952 -- function call. If the called entity is global, rewrite the node
15953 -- appropriately, i.e. as an extended name for the global entity.
15954
15955 elsif Nkind (Parent (N)) = N_Selected_Component
15956 and then Nkind (Parent (N2)) = N_Function_Call
15957 and then N = Selector_Name (Parent (N))
15958 then
15959 if No (Parameter_Associations (Parent (N2))) then
15960 if Is_Global (Entity (Name (Parent (N2)))) then
15961 Change_Selected_Component_To_Expanded_Name (Parent (N));
15962 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15963 Set_Global_Type (Parent (N), Name (Parent (N2)));
15964 Save_Entity_Descendants (N);
15965
15966 else
15967 Set_Is_Prefixed_Call (Parent (N));
15968 Set_Associated_Node (N, Empty);
15969 Set_Etype (N, Empty);
15970 end if;
15971
15972 -- In Ada 2005, X.F may be a call to a primitive operation,
15973 -- rewritten as F (X). This rewriting will be done again in an
15974 -- instance, so keep the original node. Global entities will be
15975 -- captured as for other constructs. Indicate that this must
15976 -- resolve as a call, to prevent accidental overloading in the
15977 -- instance, if both a component and a primitive operation appear
15978 -- as candidates.
15979
15980 else
15981 Set_Is_Prefixed_Call (Parent (N));
15982 end if;
15983
15984 -- Entity is local. Reset in generic unit, so that node is resolved
15985 -- anew at the point of instantiation.
15986
15987 else
15988 Set_Associated_Node (N, Empty);
15989 Set_Etype (N, Empty);
15990 end if;
15991 end Reset_Entity;
15992
15993 -----------------------------
15994 -- Save_Entity_Descendants --
15995 -----------------------------
15996
15997 procedure Save_Entity_Descendants (N : Node_Id) is
15998 begin
15999 case Nkind (N) is
16000 when N_Binary_Op =>
16001 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
16002 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16003
16004 when N_Unary_Op =>
16005 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16006
16007 when N_Expanded_Name
16008 | N_Selected_Component
16009 =>
16010 Save_Global_Descendant (Union_Id (Prefix (N)));
16011 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16012
16013 when N_Character_Literal
16014 | N_Identifier
16015 | N_Operator_Symbol
16016 =>
16017 null;
16018
16019 when others =>
16020 raise Program_Error;
16021 end case;
16022 end Save_Entity_Descendants;
16023
16024 --------------------------
16025 -- Save_Global_Defaults --
16026 --------------------------
16027
16028 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16029 Loc : constant Source_Ptr := Sloc (N1);
16030 Assoc2 : constant List_Id := Generic_Associations (N2);
16031 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16032 Assoc1 : List_Id;
16033 Act1 : Node_Id;
16034 Act2 : Node_Id;
16035 Def : Node_Id;
16036 Ndec : Node_Id;
16037 Subp : Entity_Id;
16038 Actual : Entity_Id;
16039
16040 begin
16041 Assoc1 := Generic_Associations (N1);
16042
16043 if Present (Assoc1) then
16044 Act1 := First (Assoc1);
16045 else
16046 Act1 := Empty;
16047 Set_Generic_Associations (N1, New_List);
16048 Assoc1 := Generic_Associations (N1);
16049 end if;
16050
16051 if Present (Assoc2) then
16052 Act2 := First (Assoc2);
16053 else
16054 return;
16055 end if;
16056
16057 while Present (Act1) and then Present (Act2) loop
16058 Next (Act1);
16059 Next (Act2);
16060 end loop;
16061
16062 -- Find the associations added for default subprograms
16063
16064 if Present (Act2) then
16065 while Nkind (Act2) /= N_Generic_Association
16066 or else No (Entity (Selector_Name (Act2)))
16067 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16068 loop
16069 Next (Act2);
16070 end loop;
16071
16072 -- Add a similar association if the default is global. The
16073 -- renaming declaration for the actual has been analyzed, and
16074 -- its alias is the program it renames. Link the actual in the
16075 -- original generic tree with the node in the analyzed tree.
16076
16077 while Present (Act2) loop
16078 Subp := Entity (Selector_Name (Act2));
16079 Def := Explicit_Generic_Actual_Parameter (Act2);
16080
16081 -- Following test is defence against rubbish errors
16082
16083 if No (Alias (Subp)) then
16084 return;
16085 end if;
16086
16087 -- Retrieve the resolved actual from the renaming declaration
16088 -- created for the instantiated formal.
16089
16090 Actual := Entity (Name (Parent (Parent (Subp))));
16091 Set_Entity (Def, Actual);
16092 Set_Etype (Def, Etype (Actual));
16093
16094 if Is_Global (Actual) then
16095 Ndec :=
16096 Make_Generic_Association (Loc,
16097 Selector_Name =>
16098 New_Occurrence_Of (Subp, Loc),
16099 Explicit_Generic_Actual_Parameter =>
16100 New_Occurrence_Of (Actual, Loc));
16101
16102 Set_Associated_Node
16103 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16104
16105 Append (Ndec, Assoc1);
16106
16107 -- If there are other defaults, add a dummy association in case
16108 -- there are other defaulted formals with the same name.
16109
16110 elsif Present (Next (Act2)) then
16111 Ndec :=
16112 Make_Generic_Association (Loc,
16113 Selector_Name =>
16114 New_Occurrence_Of (Subp, Loc),
16115 Explicit_Generic_Actual_Parameter => Empty);
16116
16117 Append (Ndec, Assoc1);
16118 end if;
16119
16120 Next (Act2);
16121 end loop;
16122 end if;
16123
16124 if Nkind (Name (N1)) = N_Identifier
16125 and then Is_Child_Unit (Gen_Id)
16126 and then Is_Global (Gen_Id)
16127 and then Is_Generic_Unit (Scope (Gen_Id))
16128 and then In_Open_Scopes (Scope (Gen_Id))
16129 then
16130 -- This is an instantiation of a child unit within a sibling, so
16131 -- that the generic parent is in scope. An eventual instance must
16132 -- occur within the scope of an instance of the parent. Make name
16133 -- in instance into an expanded name, to preserve the identifier
16134 -- of the parent, so it can be resolved subsequently.
16135
16136 Rewrite (Name (N2),
16137 Make_Expanded_Name (Loc,
16138 Chars => Chars (Gen_Id),
16139 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16140 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16141 Set_Entity (Name (N2), Gen_Id);
16142
16143 Rewrite (Name (N1),
16144 Make_Expanded_Name (Loc,
16145 Chars => Chars (Gen_Id),
16146 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16147 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16148
16149 Set_Associated_Node (Name (N1), Name (N2));
16150 Set_Associated_Node (Prefix (Name (N1)), Empty);
16151 Set_Associated_Node
16152 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16153 Set_Etype (Name (N1), Etype (Gen_Id));
16154 end if;
16155 end Save_Global_Defaults;
16156
16157 ----------------------------
16158 -- Save_Global_Descendant --
16159 ----------------------------
16160
16161 procedure Save_Global_Descendant (D : Union_Id) is
16162 N1 : Node_Id;
16163
16164 begin
16165 if D in Node_Range then
16166 if D = Union_Id (Empty) then
16167 null;
16168
16169 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16170 Save_References (Node_Id (D));
16171 end if;
16172
16173 elsif D in List_Range then
16174 pragma Assert (D /= Union_Id (No_List));
16175 -- Because No_List = Empty, which is in Node_Range above
16176
16177 if Is_Empty_List (List_Id (D)) then
16178 null;
16179
16180 else
16181 N1 := First (List_Id (D));
16182 while Present (N1) loop
16183 Save_References (N1);
16184 Next (N1);
16185 end loop;
16186 end if;
16187
16188 -- Element list or other non-node field, nothing to do
16189
16190 else
16191 null;
16192 end if;
16193 end Save_Global_Descendant;
16194
16195 ---------------------
16196 -- Save_References --
16197 ---------------------
16198
16199 -- This is the recursive procedure that does the work once the enclosing
16200 -- generic scope has been established. We have to treat specially a
16201 -- number of node rewritings that are required by semantic processing
16202 -- and which change the kind of nodes in the generic copy: typically
16203 -- constant-folding, replacing an operator node by a string literal, or
16204 -- a selected component by an expanded name. In each of those cases, the
16205 -- transformation is propagated to the generic unit.
16206
16207 procedure Save_References (N : Node_Id) is
16208 Loc : constant Source_Ptr := Sloc (N);
16209
16210 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16211 -- Determine whether arbitrary node Nod requires delayed capture of
16212 -- global references within its aspect specifications.
16213
16214 procedure Save_References_In_Aggregate (N : Node_Id);
16215 -- Save all global references in [extension] aggregate node N
16216
16217 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16218 -- Save all global references in a character literal or operator
16219 -- symbol denoted by N.
16220
16221 procedure Save_References_In_Descendants (N : Node_Id);
16222 -- Save all global references in all descendants of node N
16223
16224 procedure Save_References_In_Identifier (N : Node_Id);
16225 -- Save all global references in identifier node N
16226
16227 procedure Save_References_In_Operator (N : Node_Id);
16228 -- Save all global references in operator node N
16229
16230 procedure Save_References_In_Pragma (Prag : Node_Id);
16231 -- Save all global references found within the expression of pragma
16232 -- Prag.
16233
16234 ---------------------------
16235 -- Requires_Delayed_Save --
16236 ---------------------------
16237
16238 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16239 begin
16240 -- Generic packages and subprograms require delayed capture of
16241 -- global references within their aspects due to the timing of
16242 -- annotation analysis.
16243
16244 if Nkind (Nod) in N_Generic_Package_Declaration
16245 | N_Generic_Subprogram_Declaration
16246 | N_Package_Body
16247 | N_Package_Body_Stub
16248 | N_Subprogram_Body
16249 | N_Subprogram_Body_Stub
16250 then
16251 -- Since the capture of global references is done on the
16252 -- unanalyzed generic template, there is no information around
16253 -- to infer the context. Use the Associated_Entity linkages to
16254 -- peek into the analyzed generic copy and determine what the
16255 -- template corresponds to.
16256
16257 if Nod = Templ then
16258 return
16259 Is_Generic_Declaration_Or_Body
16260 (Unit_Declaration_Node
16261 (Associated_Entity (Defining_Entity (Nod))));
16262
16263 -- Otherwise the generic unit being processed is not the top
16264 -- level template. It is safe to capture of global references
16265 -- within the generic unit because at this point the top level
16266 -- copy is fully analyzed.
16267
16268 else
16269 return False;
16270 end if;
16271
16272 -- Otherwise capture the global references without interference
16273
16274 else
16275 return False;
16276 end if;
16277 end Requires_Delayed_Save;
16278
16279 ----------------------------------
16280 -- Save_References_In_Aggregate --
16281 ----------------------------------
16282
16283 procedure Save_References_In_Aggregate (N : Node_Id) is
16284 Nam : Node_Id;
16285 Qual : Node_Id := Empty;
16286 Typ : Entity_Id := Empty;
16287
16288 use Atree.Unchecked_Access;
16289 -- This code section is part of implementing an untyped tree
16290 -- traversal, so it needs direct access to node fields.
16291
16292 begin
16293 N2 := Get_Associated_Node (N);
16294
16295 if Present (N2) then
16296 Typ := Etype (N2);
16297
16298 -- In an instance within a generic, use the name of the actual
16299 -- and not the original generic parameter. If the actual is
16300 -- global in the current generic it must be preserved for its
16301 -- instantiation.
16302
16303 if Nkind (Parent (Typ)) = N_Subtype_Declaration
16304 and then Present (Generic_Parent_Type (Parent (Typ)))
16305 then
16306 Typ := Base_Type (Typ);
16307 Set_Etype (N2, Typ);
16308 end if;
16309 end if;
16310
16311 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16312 Set_Associated_Node (N, Empty);
16313
16314 -- If the aggregate is an actual in a call, it has been
16315 -- resolved in the current context, to some local type. The
16316 -- enclosing call may have been disambiguated by the aggregate,
16317 -- and this disambiguation might fail at instantiation time
16318 -- because the type to which the aggregate did resolve is not
16319 -- preserved. In order to preserve some of this information,
16320 -- wrap the aggregate in a qualified expression, using the id
16321 -- of its type. For further disambiguation we qualify the type
16322 -- name with its scope (if visible and not hidden by a local
16323 -- homograph) because both id's will have corresponding
16324 -- entities in an instance. This resolves most of the problems
16325 -- with missing type information on aggregates in instances.
16326
16327 if Present (N2)
16328 and then Nkind (N2) = Nkind (N)
16329 and then Nkind (Parent (N2)) in N_Subprogram_Call
16330 and then Present (Typ)
16331 and then Comes_From_Source (Typ)
16332 then
16333 Nam := Make_Identifier (Loc, Chars (Typ));
16334
16335 if Is_Immediately_Visible (Scope (Typ))
16336 and then
16337 (not In_Open_Scopes (Scope (Typ))
16338 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16339 then
16340 Nam :=
16341 Make_Selected_Component (Loc,
16342 Prefix =>
16343 Make_Identifier (Loc, Chars (Scope (Typ))),
16344 Selector_Name => Nam);
16345 end if;
16346
16347 Qual :=
16348 Make_Qualified_Expression (Loc,
16349 Subtype_Mark => Nam,
16350 Expression => Relocate_Node (N));
16351 end if;
16352 end if;
16353
16354 Save_Global_Descendant (Field1 (N));
16355 Save_Global_Descendant (Field2 (N));
16356 Save_Global_Descendant (Field3 (N));
16357 Save_Global_Descendant (Field5 (N));
16358
16359 if Present (Qual) then
16360 Rewrite (N, Qual);
16361 end if;
16362 end Save_References_In_Aggregate;
16363
16364 ----------------------------------------------
16365 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16366 ----------------------------------------------
16367
16368 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16369 begin
16370 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16371 Reset_Entity (N);
16372
16373 elsif Nkind (N) = N_Operator_Symbol
16374 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16375 then
16376 Change_Operator_Symbol_To_String_Literal (N);
16377 end if;
16378 end Save_References_In_Char_Lit_Or_Op_Symbol;
16379
16380 ------------------------------------
16381 -- Save_References_In_Descendants --
16382 ------------------------------------
16383
16384 procedure Save_References_In_Descendants (N : Node_Id) is
16385 use Atree.Unchecked_Access;
16386 -- This code section is part of implementing an untyped tree
16387 -- traversal, so it needs direct access to node fields.
16388
16389 begin
16390 Save_Global_Descendant (Field1 (N));
16391 Save_Global_Descendant (Field2 (N));
16392 Save_Global_Descendant (Field3 (N));
16393 Save_Global_Descendant (Field4 (N));
16394 Save_Global_Descendant (Field5 (N));
16395 end Save_References_In_Descendants;
16396
16397 -----------------------------------
16398 -- Save_References_In_Identifier --
16399 -----------------------------------
16400
16401 procedure Save_References_In_Identifier (N : Node_Id) is
16402 begin
16403 -- The node did not undergo a transformation
16404
16405 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16406 -- If this is a discriminant reference, always save it.
16407 -- It is used in the instance to find the corresponding
16408 -- discriminant positionally rather than by name.
16409
16410 Set_Original_Discriminant
16411 (N, Original_Discriminant (Get_Associated_Node (N)));
16412
16413 Reset_Entity (N);
16414
16415 -- The analysis of the generic copy transformed the identifier
16416 -- into another construct. Propagate the changes to the template.
16417
16418 else
16419 N2 := Get_Associated_Node (N);
16420
16421 -- The identifier denotes a call to a parameterless function.
16422 -- Mark the node as resolved when the function is external.
16423
16424 if Nkind (N2) = N_Function_Call then
16425 E := Entity (Name (N2));
16426
16427 if Present (E) and then Is_Global (E) then
16428 Set_Etype (N, Etype (N2));
16429 else
16430 Set_Associated_Node (N, Empty);
16431 Set_Etype (N, Empty);
16432 end if;
16433
16434 -- The identifier denotes a named number that was constant
16435 -- folded. Preserve the original name for ASIS and undo the
16436 -- constant folding which will be repeated in the instance.
16437 -- Is this still needed???
16438
16439 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16440 and then Is_Entity_Name (Original_Node (N2))
16441 then
16442 Set_Associated_Node (N, Original_Node (N2));
16443 Reset_Entity (N);
16444
16445 -- The identifier resolved to a string literal. Propagate this
16446 -- information to the generic template.
16447
16448 elsif Nkind (N2) = N_String_Literal then
16449 Rewrite (N, New_Copy (N2));
16450
16451 -- The identifier is rewritten as a dereference if it is the
16452 -- prefix of an implicit dereference. Preserve the original
16453 -- tree as the analysis of the instance will expand the node
16454 -- again, but preserve the resolved entity if it is global.
16455
16456 elsif Nkind (N2) = N_Explicit_Dereference then
16457 if Is_Entity_Name (Prefix (N2))
16458 and then Present (Entity (Prefix (N2)))
16459 and then Is_Global (Entity (Prefix (N2)))
16460 then
16461 Set_Associated_Node (N, Prefix (N2));
16462
16463 elsif Nkind (Prefix (N2)) = N_Function_Call
16464 and then Present (Entity (Name (Prefix (N2))))
16465 and then Is_Global (Entity (Name (Prefix (N2))))
16466 then
16467 Rewrite (N,
16468 Make_Explicit_Dereference (Loc,
16469 Prefix =>
16470 Make_Function_Call (Loc,
16471 Name =>
16472 New_Occurrence_Of
16473 (Entity (Name (Prefix (N2))), Loc))));
16474
16475 else
16476 Set_Associated_Node (N, Empty);
16477 Set_Etype (N, Empty);
16478 end if;
16479
16480 -- The subtype mark of a nominally unconstrained object is
16481 -- rewritten as a subtype indication using the bounds of the
16482 -- expression. Recover the original subtype mark.
16483
16484 elsif Nkind (N2) = N_Subtype_Indication
16485 and then Is_Entity_Name (Original_Node (N2))
16486 then
16487 Set_Associated_Node (N, Original_Node (N2));
16488 Reset_Entity (N);
16489 end if;
16490 end if;
16491 end Save_References_In_Identifier;
16492
16493 ---------------------------------
16494 -- Save_References_In_Operator --
16495 ---------------------------------
16496
16497 procedure Save_References_In_Operator (N : Node_Id) is
16498 begin
16499 -- The node did not undergo a transformation
16500
16501 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16502 if Nkind (N) = N_Op_Concat then
16503 Set_Is_Component_Left_Opnd (N,
16504 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16505
16506 Set_Is_Component_Right_Opnd (N,
16507 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16508 end if;
16509
16510 Reset_Entity (N);
16511
16512 -- The analysis of the generic copy transformed the operator into
16513 -- some other construct. Propagate the changes to the template if
16514 -- applicable.
16515
16516 else
16517 N2 := Get_Associated_Node (N);
16518
16519 -- The operator resoved to a function call
16520
16521 if Nkind (N2) = N_Function_Call then
16522
16523 -- Add explicit qualifications in the generic template for
16524 -- all operands of universal type. This aids resolution by
16525 -- preserving the actual type of a literal or an attribute
16526 -- that yields a universal result.
16527
16528 Qualify_Universal_Operands (N, N2);
16529
16530 E := Entity (Name (N2));
16531
16532 if Present (E) and then Is_Global (E) then
16533 Set_Etype (N, Etype (N2));
16534 else
16535 Set_Associated_Node (N, Empty);
16536 Set_Etype (N, Empty);
16537 end if;
16538
16539 -- The operator was folded into a literal
16540
16541 elsif Nkind (N2) in N_Integer_Literal
16542 | N_Real_Literal
16543 | N_String_Literal
16544 then
16545 if Present (Original_Node (N2))
16546 and then Nkind (Original_Node (N2)) = Nkind (N)
16547 then
16548 -- Operation was constant-folded. Whenever possible,
16549 -- recover semantic information from unfolded node.
16550 -- This was initially done for ASIS but is apparently
16551 -- needed also for e.g. compiling a-nbnbin.adb.
16552
16553 Set_Associated_Node (N, Original_Node (N2));
16554
16555 if Nkind (N) = N_Op_Concat then
16556 Set_Is_Component_Left_Opnd (N,
16557 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16558 Set_Is_Component_Right_Opnd (N,
16559 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16560 end if;
16561
16562 Reset_Entity (N);
16563
16564 -- Propagate the constant folding back to the template
16565
16566 else
16567 Rewrite (N, New_Copy (N2));
16568 Set_Analyzed (N, False);
16569 end if;
16570
16571 -- The operator was folded into an enumeration literal. Retain
16572 -- the entity to avoid spurious ambiguities if it is overloaded
16573 -- at the point of instantiation or inlining.
16574
16575 elsif Nkind (N2) = N_Identifier
16576 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16577 then
16578 Rewrite (N, New_Copy (N2));
16579 Set_Analyzed (N, False);
16580 end if;
16581 end if;
16582
16583 -- Complete the operands check if node has not been constant
16584 -- folded.
16585
16586 if Nkind (N) in N_Op then
16587 Save_Entity_Descendants (N);
16588 end if;
16589 end Save_References_In_Operator;
16590
16591 -------------------------------
16592 -- Save_References_In_Pragma --
16593 -------------------------------
16594
16595 procedure Save_References_In_Pragma (Prag : Node_Id) is
16596 Context : Node_Id;
16597 Do_Save : Boolean := True;
16598
16599 use Atree.Unchecked_Access;
16600 -- This code section is part of implementing an untyped tree
16601 -- traversal, so it needs direct access to node fields.
16602
16603 begin
16604 -- Do not save global references in pragmas generated from aspects
16605 -- because the pragmas will be regenerated at instantiation time.
16606
16607 if From_Aspect_Specification (Prag) then
16608 Do_Save := False;
16609
16610 -- The capture of global references within contract-related source
16611 -- pragmas associated with generic packages, subprograms or their
16612 -- respective bodies must be delayed due to timing of annotation
16613 -- analysis. Global references are still captured in routine
16614 -- Save_Global_References_In_Contract.
16615
16616 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16617 if Is_Package_Contract_Annotation (Prag) then
16618 Context := Find_Related_Package_Or_Body (Prag);
16619 else
16620 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16621 Context := Find_Related_Declaration_Or_Body (Prag);
16622 end if;
16623
16624 -- The use of Original_Node accounts for the case when the
16625 -- related context is generic template.
16626
16627 if Requires_Delayed_Save (Original_Node (Context)) then
16628 Do_Save := False;
16629 end if;
16630 end if;
16631
16632 -- For all other cases, save all global references within the
16633 -- descendants, but skip the following semantic fields:
16634
16635 -- Field1 - Next_Pragma
16636 -- Field3 - Corresponding_Aspect
16637 -- Field5 - Next_Rep_Item
16638
16639 if Do_Save then
16640 Save_Global_Descendant (Field2 (Prag));
16641 Save_Global_Descendant (Field4 (Prag));
16642 end if;
16643 end Save_References_In_Pragma;
16644
16645 -- Start of processing for Save_References
16646
16647 begin
16648 if N = Empty then
16649 null;
16650
16651 -- Aggregates
16652
16653 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16654 Save_References_In_Aggregate (N);
16655
16656 -- Character literals, operator symbols
16657
16658 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16659 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16660
16661 -- Defining identifiers
16662
16663 elsif Nkind (N) in N_Entity then
16664 null;
16665
16666 -- Identifiers
16667
16668 elsif Nkind (N) = N_Identifier then
16669 Save_References_In_Identifier (N);
16670
16671 -- Operators
16672
16673 elsif Nkind (N) in N_Op then
16674 Save_References_In_Operator (N);
16675
16676 -- Pragmas
16677
16678 elsif Nkind (N) = N_Pragma then
16679 Save_References_In_Pragma (N);
16680
16681 else
16682 Save_References_In_Descendants (N);
16683 end if;
16684
16685 -- Save all global references found within the aspect specifications
16686 -- of the related node.
16687
16688 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16689
16690 -- The capture of global references within aspects associated with
16691 -- generic packages, subprograms or their bodies must be delayed
16692 -- due to timing of annotation analysis. Global references are
16693 -- still captured in routine Save_Global_References_In_Contract.
16694
16695 if Requires_Delayed_Save (N) then
16696 null;
16697
16698 -- Otherwise save all global references within the aspects
16699
16700 else
16701 Save_Global_References_In_Aspects (N);
16702 end if;
16703 end if;
16704 end Save_References;
16705
16706 -- Start of processing for Save_Global_References
16707
16708 begin
16709 Gen_Scope := Current_Scope;
16710
16711 -- If the generic unit is a child unit, references to entities in the
16712 -- parent are treated as local, because they will be resolved anew in
16713 -- the context of the instance of the parent.
16714
16715 while Is_Child_Unit (Gen_Scope)
16716 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16717 loop
16718 Gen_Scope := Scope (Gen_Scope);
16719 end loop;
16720
16721 Save_References (Templ);
16722 end Save_Global_References;
16723
16724 ---------------------------------------
16725 -- Save_Global_References_In_Aspects --
16726 ---------------------------------------
16727
16728 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16729 Asp : Node_Id;
16730 Expr : Node_Id;
16731
16732 begin
16733 Asp := First (Aspect_Specifications (N));
16734 while Present (Asp) loop
16735 Expr := Expression (Asp);
16736
16737 if Present (Expr) then
16738 Save_Global_References (Expr);
16739 end if;
16740
16741 Next (Asp);
16742 end loop;
16743 end Save_Global_References_In_Aspects;
16744
16745 ------------------------------------------
16746 -- Set_Copied_Sloc_For_Inherited_Pragma --
16747 ------------------------------------------
16748
16749 procedure Set_Copied_Sloc_For_Inherited_Pragma
16750 (N : Node_Id;
16751 E : Entity_Id)
16752 is
16753 begin
16754 Create_Instantiation_Source (N, E,
16755 Inlined_Body => False,
16756 Inherited_Pragma => True,
16757 Factor => S_Adjustment);
16758 end Set_Copied_Sloc_For_Inherited_Pragma;
16759
16760 --------------------------------------
16761 -- Set_Copied_Sloc_For_Inlined_Body --
16762 --------------------------------------
16763
16764 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16765 begin
16766 Create_Instantiation_Source (N, E,
16767 Inlined_Body => True,
16768 Inherited_Pragma => False,
16769 Factor => S_Adjustment);
16770 end Set_Copied_Sloc_For_Inlined_Body;
16771
16772 ---------------------
16773 -- Set_Instance_Of --
16774 ---------------------
16775
16776 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16777 begin
16778 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16779 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16780 Generic_Renamings.Increment_Last;
16781 end Set_Instance_Of;
16782
16783 --------------------
16784 -- Set_Next_Assoc --
16785 --------------------
16786
16787 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16788 begin
16789 Generic_Renamings.Table (E).Next_In_HTable := Next;
16790 end Set_Next_Assoc;
16791
16792 -------------------
16793 -- Start_Generic --
16794 -------------------
16795
16796 procedure Start_Generic is
16797 begin
16798 -- ??? More things could be factored out in this routine.
16799 -- Should probably be done at a later stage.
16800
16801 Generic_Flags.Append (Inside_A_Generic);
16802 Inside_A_Generic := True;
16803
16804 Expander_Mode_Save_And_Set (False);
16805 end Start_Generic;
16806
16807 ----------------------
16808 -- Set_Instance_Env --
16809 ----------------------
16810
16811 -- WARNING: This routine manages SPARK regions
16812
16813 procedure Set_Instance_Env
16814 (Gen_Unit : Entity_Id;
16815 Act_Unit : Entity_Id)
16816 is
16817 Saved_AE : constant Boolean := Assertions_Enabled;
16818 Saved_CPL : constant Node_Id := Check_Policy_List;
16819 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16820 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16821 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16822
16823 begin
16824 -- Regardless of the current mode, predefined units are analyzed in the
16825 -- most current Ada mode, and earlier version Ada checks do not apply
16826 -- to predefined units. Nothing needs to be done for non-internal units.
16827 -- These are always analyzed in the current mode.
16828
16829 if In_Internal_Unit (Gen_Unit) then
16830
16831 -- The following call resets all configuration attributes to default
16832 -- or the xxx_Config versions of the attributes when the current sem
16833 -- unit is the main unit. At the same time, internal units must also
16834 -- inherit certain configuration attributes from their context. It
16835 -- is unclear what these two sets are.
16836
16837 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16838
16839 -- Reinstall relevant configuration attributes of the context
16840
16841 Assertions_Enabled := Saved_AE;
16842 Check_Policy_List := Saved_CPL;
16843 Dynamic_Elaboration_Checks := Saved_DEC;
16844
16845 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16846 end if;
16847
16848 Current_Instantiated_Parent :=
16849 (Gen_Id => Gen_Unit,
16850 Act_Id => Act_Unit,
16851 Next_In_HTable => Assoc_Null);
16852 end Set_Instance_Env;
16853
16854 -----------------
16855 -- Switch_View --
16856 -----------------
16857
16858 procedure Switch_View (T : Entity_Id) is
16859 BT : constant Entity_Id := Base_Type (T);
16860 Priv_Elmt : Elmt_Id := No_Elmt;
16861 Priv_Sub : Entity_Id;
16862
16863 begin
16864 -- T may be private but its base type may have been exchanged through
16865 -- some other occurrence, in which case there is nothing to switch
16866 -- besides T itself. Note that a private dependent subtype of a private
16867 -- type might not have been switched even if the base type has been,
16868 -- because of the last branch of Check_Private_View (see comment there).
16869
16870 if not Is_Private_Type (BT) then
16871 Prepend_Elmt (Full_View (T), Exchanged_Views);
16872 Exchange_Declarations (T);
16873 return;
16874 end if;
16875
16876 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16877
16878 if Present (Full_View (BT)) then
16879 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16880 Exchange_Declarations (BT);
16881 end if;
16882
16883 while Present (Priv_Elmt) loop
16884 Priv_Sub := Node (Priv_Elmt);
16885
16886 if Present (Full_View (Priv_Sub)) then
16887 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16888 Exchange_Declarations (Priv_Sub);
16889 end if;
16890
16891 Next_Elmt (Priv_Elmt);
16892 end loop;
16893 end Switch_View;
16894
16895 -----------------
16896 -- True_Parent --
16897 -----------------
16898
16899 function True_Parent (N : Node_Id) return Node_Id is
16900 begin
16901 if Nkind (Parent (N)) = N_Subunit then
16902 return Parent (Corresponding_Stub (Parent (N)));
16903 else
16904 return Parent (N);
16905 end if;
16906 end True_Parent;
16907
16908 -----------------------------
16909 -- Valid_Default_Attribute --
16910 -----------------------------
16911
16912 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16913 Attr_Id : constant Attribute_Id :=
16914 Get_Attribute_Id (Attribute_Name (Def));
16915 T : constant Entity_Id := Entity (Prefix (Def));
16916 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16917 F : Entity_Id;
16918 Num_F : Nat;
16919 OK : Boolean;
16920
16921 begin
16922 if No (T) or else T = Any_Id then
16923 return;
16924 end if;
16925
16926 Num_F := 0;
16927 F := First_Formal (Nam);
16928 while Present (F) loop
16929 Num_F := Num_F + 1;
16930 Next_Formal (F);
16931 end loop;
16932
16933 case Attr_Id is
16934 when Attribute_Adjacent
16935 | Attribute_Ceiling
16936 | Attribute_Copy_Sign
16937 | Attribute_Floor
16938 | Attribute_Fraction
16939 | Attribute_Machine
16940 | Attribute_Model
16941 | Attribute_Remainder
16942 | Attribute_Rounding
16943 | Attribute_Unbiased_Rounding
16944 =>
16945 OK := Is_Fun
16946 and then Num_F = 1
16947 and then Is_Floating_Point_Type (T);
16948
16949 when Attribute_Image
16950 | Attribute_Pred
16951 | Attribute_Succ
16952 | Attribute_Value
16953 | Attribute_Wide_Image
16954 | Attribute_Wide_Value
16955 =>
16956 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16957
16958 when Attribute_Max
16959 | Attribute_Min
16960 =>
16961 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16962
16963 when Attribute_Input =>
16964 OK := (Is_Fun and then Num_F = 1);
16965
16966 when Attribute_Output
16967 | Attribute_Put_Image
16968 | Attribute_Read
16969 | Attribute_Write
16970 =>
16971 OK := not Is_Fun and then Num_F = 2;
16972
16973 when others =>
16974 OK := False;
16975 end case;
16976
16977 if not OK then
16978 Error_Msg_N
16979 ("attribute reference has wrong profile for subprogram", Def);
16980 end if;
16981 end Valid_Default_Attribute;
16982
16983 end Sem_Ch12;