[Ada] Do not instantiate generic bodies outside of main unit in GNATprove
[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 S : Entity_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 S := Current_Scope;
2106 while Present (S) loop
2107 if Ekind (S) in E_Block
2108 | E_Function
2109 | E_Loop
2110 | E_Procedure
2111 then
2112 Needs_Freezing := False;
2113 exit;
2114 end if;
2115
2116 S := Scope (S);
2117 end loop;
2118
2119 if Needs_Freezing then
2120 Check_Generic_Parent;
2121
2122 -- If the actual is a renaming of a proper
2123 -- instance of the formal package, indicate
2124 -- that it is the instance that must be frozen.
2125
2126 if Nkind (Parent (Actual)) =
2127 N_Package_Renaming_Declaration
2128 then
2129 Set_Has_Delayed_Freeze
2130 (Renamed_Entity (Actual));
2131 Append_Elmt
2132 (Renamed_Entity (Actual),
2133 Actuals_To_Freeze);
2134 else
2135 Set_Has_Delayed_Freeze (Actual);
2136 Append_Elmt (Actual, Actuals_To_Freeze);
2137 end if;
2138 end if;
2139 end if;
2140 end Explicit_Freeze_Check;
2141 end if;
2142
2143 -- For use type and use package appearing in the generic part,
2144 -- we have already copied them, so we can just move them where
2145 -- they belong (we mustn't recopy them since this would mess up
2146 -- the Sloc values).
2147
2148 when N_Use_Package_Clause
2149 | N_Use_Type_Clause
2150 =>
2151 if Nkind (Original_Node (I_Node)) =
2152 N_Formal_Package_Declaration
2153 then
2154 Append (New_Copy_Tree (Formal), Assoc_List);
2155 else
2156 Remove (Formal);
2157 Append (Formal, Assoc_List);
2158 end if;
2159
2160 when others =>
2161 raise Program_Error;
2162 end case;
2163
2164 Formal := Saved_Formal;
2165 Next_Non_Pragma (Analyzed_Formal);
2166 end loop;
2167
2168 if Num_Actuals > Num_Matched then
2169 Error_Msg_Sloc := Sloc (Gen_Unit);
2170
2171 if Present (Selector_Name (Actual)) then
2172 Error_Msg_NE
2173 ("unmatched actual &", Actual, Selector_Name (Actual));
2174 Error_Msg_NE
2175 ("\in instantiation of & declared#", Actual, Gen_Unit);
2176 else
2177 Error_Msg_NE
2178 ("unmatched actual in instantiation of & declared#",
2179 Actual, Gen_Unit);
2180 end if;
2181 end if;
2182
2183 elsif Present (Actuals) then
2184 Error_Msg_N
2185 ("too many actuals in generic instantiation", Instantiation_Node);
2186 end if;
2187
2188 -- An instantiation freezes all generic actuals. The only exceptions
2189 -- to this are incomplete types and subprograms which are not fully
2190 -- defined at the point of instantiation.
2191
2192 declare
2193 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2194 begin
2195 while Present (Elmt) loop
2196 Freeze_Before (I_Node, Node (Elmt));
2197 Next_Elmt (Elmt);
2198 end loop;
2199 end;
2200
2201 -- If there are default subprograms, normalize the tree by adding
2202 -- explicit associations for them. This is required if the instance
2203 -- appears within a generic.
2204
2205 if not Is_Empty_List (Default_Actuals) then
2206 declare
2207 Default : Node_Id;
2208
2209 begin
2210 Default := First (Default_Actuals);
2211 while Present (Default) loop
2212 Mark_Rewrite_Insertion (Default);
2213 Next (Default);
2214 end loop;
2215
2216 if No (Actuals) then
2217 Set_Generic_Associations (I_Node, Default_Actuals);
2218 else
2219 Append_List_To (Actuals, Default_Actuals);
2220 end if;
2221 end;
2222 end if;
2223
2224 -- If this is a formal package, normalize the parameter list by adding
2225 -- explicit box associations for the formals that are covered by an
2226 -- Others_Choice.
2227
2228 if not Is_Empty_List (Default_Formals) then
2229 Append_List (Default_Formals, Formals);
2230 end if;
2231
2232 return Assoc_List;
2233 end Analyze_Associations;
2234
2235 -------------------------------
2236 -- Analyze_Formal_Array_Type --
2237 -------------------------------
2238
2239 procedure Analyze_Formal_Array_Type
2240 (T : in out Entity_Id;
2241 Def : Node_Id)
2242 is
2243 DSS : Node_Id;
2244
2245 begin
2246 -- Treated like a non-generic array declaration, with additional
2247 -- semantic checks.
2248
2249 Enter_Name (T);
2250
2251 if Nkind (Def) = N_Constrained_Array_Definition then
2252 DSS := First (Discrete_Subtype_Definitions (Def));
2253 while Present (DSS) loop
2254 if Nkind (DSS) in N_Subtype_Indication
2255 | N_Range
2256 | N_Attribute_Reference
2257 then
2258 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2259 end if;
2260
2261 Next (DSS);
2262 end loop;
2263 end if;
2264
2265 Array_Type_Declaration (T, Def);
2266 Set_Is_Generic_Type (Base_Type (T));
2267
2268 if Ekind (Component_Type (T)) = E_Incomplete_Type
2269 and then No (Full_View (Component_Type (T)))
2270 then
2271 Error_Msg_N ("premature usage of incomplete type", Def);
2272
2273 -- Check that range constraint is not allowed on the component type
2274 -- of a generic formal array type (AARM 12.5.3(3))
2275
2276 elsif Is_Internal (Component_Type (T))
2277 and then Present (Subtype_Indication (Component_Definition (Def)))
2278 and then Nkind (Original_Node
2279 (Subtype_Indication (Component_Definition (Def)))) =
2280 N_Subtype_Indication
2281 then
2282 Error_Msg_N
2283 ("in a formal, a subtype indication can only be "
2284 & "a subtype mark (RM 12.5.3(3))",
2285 Subtype_Indication (Component_Definition (Def)));
2286 end if;
2287
2288 end Analyze_Formal_Array_Type;
2289
2290 ---------------------------------------------
2291 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2292 ---------------------------------------------
2293
2294 -- As for other generic types, we create a valid type representation with
2295 -- legal but arbitrary attributes, whose values are never considered
2296 -- static. For all scalar types we introduce an anonymous base type, with
2297 -- the same attributes. We choose the corresponding integer type to be
2298 -- Standard_Integer.
2299 -- Here and in other similar routines, the Sloc of the generated internal
2300 -- type must be the same as the sloc of the defining identifier of the
2301 -- formal type declaration, to provide proper source navigation.
2302
2303 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2304 (T : Entity_Id;
2305 Def : Node_Id)
2306 is
2307 Loc : constant Source_Ptr := Sloc (Def);
2308
2309 Base : constant Entity_Id :=
2310 New_Internal_Entity
2311 (E_Decimal_Fixed_Point_Type,
2312 Current_Scope,
2313 Sloc (Defining_Identifier (Parent (Def))), 'G');
2314
2315 Int_Base : constant Entity_Id := Standard_Integer;
2316 Delta_Val : constant Ureal := Ureal_1;
2317 Digs_Val : constant Uint := Uint_6;
2318
2319 function Make_Dummy_Bound return Node_Id;
2320 -- Return a properly typed universal real literal to use as a bound
2321
2322 ----------------------
2323 -- Make_Dummy_Bound --
2324 ----------------------
2325
2326 function Make_Dummy_Bound return Node_Id is
2327 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2328 begin
2329 Set_Etype (Bound, Universal_Real);
2330 return Bound;
2331 end Make_Dummy_Bound;
2332
2333 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2334
2335 begin
2336 Enter_Name (T);
2337
2338 Set_Etype (Base, Base);
2339 Set_Size_Info (Base, Int_Base);
2340 Set_RM_Size (Base, RM_Size (Int_Base));
2341 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2342 Set_Digits_Value (Base, Digs_Val);
2343 Set_Delta_Value (Base, Delta_Val);
2344 Set_Small_Value (Base, Delta_Val);
2345 Set_Scalar_Range (Base,
2346 Make_Range (Loc,
2347 Low_Bound => Make_Dummy_Bound,
2348 High_Bound => Make_Dummy_Bound));
2349
2350 Set_Is_Generic_Type (Base);
2351 Set_Parent (Base, Parent (Def));
2352
2353 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2354 Set_Etype (T, Base);
2355 Set_Size_Info (T, Int_Base);
2356 Set_RM_Size (T, RM_Size (Int_Base));
2357 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2358 Set_Digits_Value (T, Digs_Val);
2359 Set_Delta_Value (T, Delta_Val);
2360 Set_Small_Value (T, Delta_Val);
2361 Set_Scalar_Range (T, Scalar_Range (Base));
2362 Set_Is_Constrained (T);
2363
2364 Check_Restriction (No_Fixed_Point, Def);
2365 end Analyze_Formal_Decimal_Fixed_Point_Type;
2366
2367 -------------------------------------------
2368 -- Analyze_Formal_Derived_Interface_Type --
2369 -------------------------------------------
2370
2371 procedure Analyze_Formal_Derived_Interface_Type
2372 (N : Node_Id;
2373 T : Entity_Id;
2374 Def : Node_Id)
2375 is
2376 Loc : constant Source_Ptr := Sloc (Def);
2377
2378 begin
2379 -- Rewrite as a type declaration of a derived type. This ensures that
2380 -- the interface list and primitive operations are properly captured.
2381
2382 Rewrite (N,
2383 Make_Full_Type_Declaration (Loc,
2384 Defining_Identifier => T,
2385 Type_Definition => Def));
2386 Analyze (N);
2387 Set_Is_Generic_Type (T);
2388 end Analyze_Formal_Derived_Interface_Type;
2389
2390 ---------------------------------
2391 -- Analyze_Formal_Derived_Type --
2392 ---------------------------------
2393
2394 procedure Analyze_Formal_Derived_Type
2395 (N : Node_Id;
2396 T : Entity_Id;
2397 Def : Node_Id)
2398 is
2399 Loc : constant Source_Ptr := Sloc (Def);
2400 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2401 New_N : Node_Id;
2402
2403 begin
2404 Set_Is_Generic_Type (T);
2405
2406 if Private_Present (Def) then
2407 New_N :=
2408 Make_Private_Extension_Declaration (Loc,
2409 Defining_Identifier => T,
2410 Discriminant_Specifications => Discriminant_Specifications (N),
2411 Unknown_Discriminants_Present => Unk_Disc,
2412 Subtype_Indication => Subtype_Mark (Def),
2413 Interface_List => Interface_List (Def));
2414
2415 Set_Abstract_Present (New_N, Abstract_Present (Def));
2416 Set_Limited_Present (New_N, Limited_Present (Def));
2417 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2418
2419 else
2420 New_N :=
2421 Make_Full_Type_Declaration (Loc,
2422 Defining_Identifier => T,
2423 Discriminant_Specifications =>
2424 Discriminant_Specifications (Parent (T)),
2425 Type_Definition =>
2426 Make_Derived_Type_Definition (Loc,
2427 Subtype_Indication => Subtype_Mark (Def)));
2428
2429 Set_Abstract_Present
2430 (Type_Definition (New_N), Abstract_Present (Def));
2431 Set_Limited_Present
2432 (Type_Definition (New_N), Limited_Present (Def));
2433 end if;
2434
2435 Rewrite (N, New_N);
2436 Analyze (N);
2437
2438 if Unk_Disc then
2439 if not Is_Composite_Type (T) then
2440 Error_Msg_N
2441 ("unknown discriminants not allowed for elementary types", N);
2442 else
2443 Set_Has_Unknown_Discriminants (T);
2444 Set_Is_Constrained (T, False);
2445 end if;
2446 end if;
2447
2448 -- If the parent type has a known size, so does the formal, which makes
2449 -- legal representation clauses that involve the formal.
2450
2451 Set_Size_Known_At_Compile_Time
2452 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2453 end Analyze_Formal_Derived_Type;
2454
2455 ----------------------------------
2456 -- Analyze_Formal_Discrete_Type --
2457 ----------------------------------
2458
2459 -- The operations defined for a discrete types are those of an enumeration
2460 -- type. The size is set to an arbitrary value, for use in analyzing the
2461 -- generic unit.
2462
2463 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2464 Loc : constant Source_Ptr := Sloc (Def);
2465 Lo : Node_Id;
2466 Hi : Node_Id;
2467
2468 Base : constant Entity_Id :=
2469 New_Internal_Entity
2470 (E_Floating_Point_Type, Current_Scope,
2471 Sloc (Defining_Identifier (Parent (Def))), 'G');
2472
2473 begin
2474 Enter_Name (T);
2475 Set_Ekind (T, E_Enumeration_Subtype);
2476 Set_Etype (T, Base);
2477 Init_Size (T, 8);
2478 Init_Alignment (T);
2479 Set_Is_Generic_Type (T);
2480 Set_Is_Constrained (T);
2481
2482 -- For semantic analysis, the bounds of the type must be set to some
2483 -- non-static value. The simplest is to create attribute nodes for those
2484 -- bounds, that refer to the type itself. These bounds are never
2485 -- analyzed but serve as place-holders.
2486
2487 Lo :=
2488 Make_Attribute_Reference (Loc,
2489 Attribute_Name => Name_First,
2490 Prefix => New_Occurrence_Of (T, Loc));
2491 Set_Etype (Lo, T);
2492
2493 Hi :=
2494 Make_Attribute_Reference (Loc,
2495 Attribute_Name => Name_Last,
2496 Prefix => New_Occurrence_Of (T, Loc));
2497 Set_Etype (Hi, T);
2498
2499 Set_Scalar_Range (T,
2500 Make_Range (Loc,
2501 Low_Bound => Lo,
2502 High_Bound => Hi));
2503
2504 Set_Ekind (Base, E_Enumeration_Type);
2505 Set_Etype (Base, Base);
2506 Init_Size (Base, 8);
2507 Init_Alignment (Base);
2508 Set_Is_Generic_Type (Base);
2509 Set_Scalar_Range (Base, Scalar_Range (T));
2510 Set_Parent (Base, Parent (Def));
2511 end Analyze_Formal_Discrete_Type;
2512
2513 ----------------------------------
2514 -- Analyze_Formal_Floating_Type --
2515 ---------------------------------
2516
2517 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2518 Base : constant Entity_Id :=
2519 New_Internal_Entity
2520 (E_Floating_Point_Type, Current_Scope,
2521 Sloc (Defining_Identifier (Parent (Def))), 'G');
2522
2523 begin
2524 -- The various semantic attributes are taken from the predefined type
2525 -- Float, just so that all of them are initialized. Their values are
2526 -- never used because no constant folding or expansion takes place in
2527 -- the generic itself.
2528
2529 Enter_Name (T);
2530 Set_Ekind (T, E_Floating_Point_Subtype);
2531 Set_Etype (T, Base);
2532 Set_Size_Info (T, (Standard_Float));
2533 Set_RM_Size (T, RM_Size (Standard_Float));
2534 Set_Digits_Value (T, Digits_Value (Standard_Float));
2535 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2536 Set_Is_Constrained (T);
2537
2538 Set_Is_Generic_Type (Base);
2539 Set_Etype (Base, Base);
2540 Set_Size_Info (Base, (Standard_Float));
2541 Set_RM_Size (Base, RM_Size (Standard_Float));
2542 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2543 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2544 Set_Parent (Base, Parent (Def));
2545
2546 Check_Restriction (No_Floating_Point, Def);
2547 end Analyze_Formal_Floating_Type;
2548
2549 -----------------------------------
2550 -- Analyze_Formal_Interface_Type;--
2551 -----------------------------------
2552
2553 procedure Analyze_Formal_Interface_Type
2554 (N : Node_Id;
2555 T : Entity_Id;
2556 Def : Node_Id)
2557 is
2558 Loc : constant Source_Ptr := Sloc (N);
2559 New_N : Node_Id;
2560
2561 begin
2562 New_N :=
2563 Make_Full_Type_Declaration (Loc,
2564 Defining_Identifier => T,
2565 Type_Definition => Def);
2566
2567 Rewrite (N, New_N);
2568 Analyze (N);
2569 Set_Is_Generic_Type (T);
2570 end Analyze_Formal_Interface_Type;
2571
2572 ---------------------------------
2573 -- Analyze_Formal_Modular_Type --
2574 ---------------------------------
2575
2576 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2577 begin
2578 -- Apart from their entity kind, generic modular types are treated like
2579 -- signed integer types, and have the same attributes.
2580
2581 Analyze_Formal_Signed_Integer_Type (T, Def);
2582 Set_Ekind (T, E_Modular_Integer_Subtype);
2583 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2584
2585 end Analyze_Formal_Modular_Type;
2586
2587 ---------------------------------------
2588 -- Analyze_Formal_Object_Declaration --
2589 ---------------------------------------
2590
2591 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2592 E : constant Node_Id := Default_Expression (N);
2593 Id : constant Node_Id := Defining_Identifier (N);
2594 K : Entity_Kind;
2595 T : Node_Id;
2596
2597 begin
2598 Enter_Name (Id);
2599
2600 -- Determine the mode of the formal object
2601
2602 if Out_Present (N) then
2603 K := E_Generic_In_Out_Parameter;
2604
2605 if not In_Present (N) then
2606 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2607 end if;
2608
2609 else
2610 K := E_Generic_In_Parameter;
2611 end if;
2612
2613 if Present (Subtype_Mark (N)) then
2614 Find_Type (Subtype_Mark (N));
2615 T := Entity (Subtype_Mark (N));
2616
2617 -- Verify that there is no redundant null exclusion
2618
2619 if Null_Exclusion_Present (N) then
2620 if not Is_Access_Type (T) then
2621 Error_Msg_N
2622 ("null exclusion can only apply to an access type", N);
2623
2624 elsif Can_Never_Be_Null (T) then
2625 Error_Msg_NE
2626 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2627 end if;
2628 end if;
2629
2630 -- Ada 2005 (AI-423): Formal object with an access definition
2631
2632 else
2633 Check_Access_Definition (N);
2634 T := Access_Definition
2635 (Related_Nod => N,
2636 N => Access_Definition (N));
2637 end if;
2638
2639 if Ekind (T) = E_Incomplete_Type then
2640 declare
2641 Error_Node : Node_Id;
2642
2643 begin
2644 if Present (Subtype_Mark (N)) then
2645 Error_Node := Subtype_Mark (N);
2646 else
2647 Check_Access_Definition (N);
2648 Error_Node := Access_Definition (N);
2649 end if;
2650
2651 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2652 end;
2653 end if;
2654
2655 if K = E_Generic_In_Parameter then
2656
2657 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2658
2659 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2660 Error_Msg_N
2661 ("generic formal of mode IN must not be of limited type", N);
2662 Explain_Limited_Type (T, N);
2663 end if;
2664
2665 if Is_Abstract_Type (T) then
2666 Error_Msg_N
2667 ("generic formal of mode IN must not be of abstract type", N);
2668 end if;
2669
2670 if Present (E) then
2671 Preanalyze_Spec_Expression (E, T);
2672
2673 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2674 Error_Msg_N
2675 ("initialization not allowed for limited types", E);
2676 Explain_Limited_Type (T, E);
2677 end if;
2678 end if;
2679
2680 Set_Ekind (Id, K);
2681 Set_Etype (Id, T);
2682
2683 -- Case of generic IN OUT parameter
2684
2685 else
2686 -- If the formal has an unconstrained type, construct its actual
2687 -- subtype, as is done for subprogram formals. In this fashion, all
2688 -- its uses can refer to specific bounds.
2689
2690 Set_Ekind (Id, K);
2691 Set_Etype (Id, T);
2692
2693 if (Is_Array_Type (T) and then not Is_Constrained (T))
2694 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2695 then
2696 declare
2697 Non_Freezing_Ref : constant Node_Id :=
2698 New_Occurrence_Of (Id, Sloc (Id));
2699 Decl : Node_Id;
2700
2701 begin
2702 -- Make sure the actual subtype doesn't generate bogus freezing
2703
2704 Set_Must_Not_Freeze (Non_Freezing_Ref);
2705 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2706 Insert_Before_And_Analyze (N, Decl);
2707 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2708 end;
2709 else
2710 Set_Actual_Subtype (Id, T);
2711 end if;
2712
2713 if Present (E) then
2714 Error_Msg_N
2715 ("initialization not allowed for `IN OUT` formals", N);
2716 end if;
2717 end if;
2718
2719 if Has_Aspects (N) then
2720 Analyze_Aspect_Specifications (N, Id);
2721 end if;
2722 end Analyze_Formal_Object_Declaration;
2723
2724 ----------------------------------------------
2725 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2726 ----------------------------------------------
2727
2728 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2729 (T : Entity_Id;
2730 Def : Node_Id)
2731 is
2732 Loc : constant Source_Ptr := Sloc (Def);
2733 Base : constant Entity_Id :=
2734 New_Internal_Entity
2735 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2736 Sloc (Defining_Identifier (Parent (Def))), 'G');
2737
2738 begin
2739 -- The semantic attributes are set for completeness only, their values
2740 -- will never be used, since all properties of the type are non-static.
2741
2742 Enter_Name (T);
2743 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2744 Set_Etype (T, Base);
2745 Set_Size_Info (T, Standard_Integer);
2746 Set_RM_Size (T, RM_Size (Standard_Integer));
2747 Set_Small_Value (T, Ureal_1);
2748 Set_Delta_Value (T, Ureal_1);
2749 Set_Scalar_Range (T,
2750 Make_Range (Loc,
2751 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2752 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2753 Set_Is_Constrained (T);
2754
2755 Set_Is_Generic_Type (Base);
2756 Set_Etype (Base, Base);
2757 Set_Size_Info (Base, Standard_Integer);
2758 Set_RM_Size (Base, RM_Size (Standard_Integer));
2759 Set_Small_Value (Base, Ureal_1);
2760 Set_Delta_Value (Base, Ureal_1);
2761 Set_Scalar_Range (Base, Scalar_Range (T));
2762 Set_Parent (Base, Parent (Def));
2763
2764 Check_Restriction (No_Fixed_Point, Def);
2765 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2766
2767 ----------------------------------------
2768 -- Analyze_Formal_Package_Declaration --
2769 ----------------------------------------
2770
2771 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2772 Gen_Id : constant Node_Id := Name (N);
2773 Loc : constant Source_Ptr := Sloc (N);
2774 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2775 Formal : Entity_Id;
2776 Gen_Decl : Node_Id;
2777 Gen_Unit : Entity_Id;
2778 Renaming : Node_Id;
2779
2780 Vis_Prims_List : Elist_Id := No_Elist;
2781 -- List of primitives made temporarily visible in the instantiation
2782 -- to match the visibility of the formal type.
2783
2784 function Build_Local_Package return Node_Id;
2785 -- The formal package is rewritten so that its parameters are replaced
2786 -- with corresponding declarations. For parameters with bona fide
2787 -- associations these declarations are created by Analyze_Associations
2788 -- as for a regular instantiation. For boxed parameters, we preserve
2789 -- the formal declarations and analyze them, in order to introduce
2790 -- entities of the right kind in the environment of the formal.
2791
2792 -------------------------
2793 -- Build_Local_Package --
2794 -------------------------
2795
2796 function Build_Local_Package return Node_Id is
2797 Decls : List_Id;
2798 Pack_Decl : Node_Id;
2799
2800 begin
2801 -- Within the formal, the name of the generic package is a renaming
2802 -- of the formal (as for a regular instantiation).
2803
2804 Pack_Decl :=
2805 Make_Package_Declaration (Loc,
2806 Specification =>
2807 Copy_Generic_Node
2808 (Specification (Original_Node (Gen_Decl)),
2809 Empty, Instantiating => True));
2810
2811 Renaming :=
2812 Make_Package_Renaming_Declaration (Loc,
2813 Defining_Unit_Name =>
2814 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2815 Name => New_Occurrence_Of (Formal, Loc));
2816
2817 if Nkind (Gen_Id) = N_Identifier
2818 and then Chars (Gen_Id) = Chars (Pack_Id)
2819 then
2820 Error_Msg_NE
2821 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2822 end if;
2823
2824 -- If the formal is declared with a box, or with an others choice,
2825 -- create corresponding declarations for all entities in the formal
2826 -- part, so that names with the proper types are available in the
2827 -- specification of the formal package.
2828
2829 -- On the other hand, if there are no associations, then all the
2830 -- formals must have defaults, and this will be checked by the
2831 -- call to Analyze_Associations.
2832
2833 if Box_Present (N)
2834 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2835 then
2836 declare
2837 Formal_Decl : Node_Id;
2838
2839 begin
2840 -- TBA : for a formal package, need to recurse ???
2841
2842 Decls := New_List;
2843 Formal_Decl :=
2844 First
2845 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2846 while Present (Formal_Decl) loop
2847 Append_To
2848 (Decls,
2849 Copy_Generic_Node
2850 (Formal_Decl, Empty, Instantiating => True));
2851 Next (Formal_Decl);
2852 end loop;
2853 end;
2854
2855 -- If generic associations are present, use Analyze_Associations to
2856 -- create the proper renaming declarations.
2857
2858 else
2859 declare
2860 Act_Tree : constant Node_Id :=
2861 Copy_Generic_Node
2862 (Original_Node (Gen_Decl), Empty,
2863 Instantiating => True);
2864
2865 begin
2866 Generic_Renamings.Set_Last (0);
2867 Generic_Renamings_HTable.Reset;
2868 Instantiation_Node := N;
2869
2870 Decls :=
2871 Analyze_Associations
2872 (I_Node => Original_Node (N),
2873 Formals => Generic_Formal_Declarations (Act_Tree),
2874 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2875
2876 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2877 end;
2878 end if;
2879
2880 Append (Renaming, To => Decls);
2881
2882 -- Add generated declarations ahead of local declarations in
2883 -- the package.
2884
2885 if No (Visible_Declarations (Specification (Pack_Decl))) then
2886 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2887 else
2888 Insert_List_Before
2889 (First (Visible_Declarations (Specification (Pack_Decl))),
2890 Decls);
2891 end if;
2892
2893 return Pack_Decl;
2894 end Build_Local_Package;
2895
2896 -- Local variables
2897
2898 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2899 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2900
2901 Associations : Boolean := True;
2902 New_N : Node_Id;
2903 Parent_Installed : Boolean := False;
2904 Parent_Instance : Entity_Id;
2905 Renaming_In_Par : Entity_Id;
2906
2907 -- Start of processing for Analyze_Formal_Package_Declaration
2908
2909 begin
2910 Check_Text_IO_Special_Unit (Gen_Id);
2911
2912 Init_Env;
2913 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2914 Gen_Unit := Entity (Gen_Id);
2915
2916 -- Check for a formal package that is a package renaming
2917
2918 if Present (Renamed_Object (Gen_Unit)) then
2919
2920 -- Indicate that unit is used, before replacing it with renamed
2921 -- entity for use below.
2922
2923 if In_Extended_Main_Source_Unit (N) then
2924 Set_Is_Instantiated (Gen_Unit);
2925 Generate_Reference (Gen_Unit, N);
2926 end if;
2927
2928 Gen_Unit := Renamed_Object (Gen_Unit);
2929 end if;
2930
2931 if Ekind (Gen_Unit) /= E_Generic_Package then
2932 Error_Msg_N ("expect generic package name", Gen_Id);
2933 Restore_Env;
2934 goto Leave;
2935
2936 elsif Gen_Unit = Current_Scope then
2937 Error_Msg_N
2938 ("generic package cannot be used as a formal package of itself",
2939 Gen_Id);
2940 Restore_Env;
2941 goto Leave;
2942
2943 elsif In_Open_Scopes (Gen_Unit) then
2944 if Is_Compilation_Unit (Gen_Unit)
2945 and then Is_Child_Unit (Current_Scope)
2946 then
2947 -- Special-case the error when the formal is a parent, and
2948 -- continue analysis to minimize cascaded errors.
2949
2950 Error_Msg_N
2951 ("generic parent cannot be used as formal package of a child "
2952 & "unit", Gen_Id);
2953
2954 else
2955 Error_Msg_N
2956 ("generic package cannot be used as a formal package within "
2957 & "itself", Gen_Id);
2958 Restore_Env;
2959 goto Leave;
2960 end if;
2961 end if;
2962
2963 -- Check that name of formal package does not hide name of generic,
2964 -- or its leading prefix. This check must be done separately because
2965 -- the name of the generic has already been analyzed.
2966
2967 declare
2968 Gen_Name : Entity_Id;
2969
2970 begin
2971 Gen_Name := Gen_Id;
2972 while Nkind (Gen_Name) = N_Expanded_Name loop
2973 Gen_Name := Prefix (Gen_Name);
2974 end loop;
2975
2976 if Chars (Gen_Name) = Chars (Pack_Id) then
2977 Error_Msg_NE
2978 ("& is hidden within declaration of formal package",
2979 Gen_Id, Gen_Name);
2980 end if;
2981 end;
2982
2983 if Box_Present (N)
2984 or else No (Generic_Associations (N))
2985 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2986 then
2987 Associations := False;
2988 end if;
2989
2990 -- If there are no generic associations, the generic parameters appear
2991 -- as local entities and are instantiated like them. We copy the generic
2992 -- package declaration as if it were an instantiation, and analyze it
2993 -- like a regular package, except that we treat the formals as
2994 -- additional visible components.
2995
2996 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2997
2998 if In_Extended_Main_Source_Unit (N) then
2999 Set_Is_Instantiated (Gen_Unit);
3000 Generate_Reference (Gen_Unit, N);
3001 end if;
3002
3003 Formal := New_Copy (Pack_Id);
3004 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3005
3006 -- Make local generic without formals. The formals will be replaced with
3007 -- internal declarations.
3008
3009 begin
3010 New_N := Build_Local_Package;
3011
3012 -- If there are errors in the parameter list, Analyze_Associations
3013 -- raises Instantiation_Error. Patch the declaration to prevent further
3014 -- exception propagation.
3015
3016 exception
3017 when Instantiation_Error =>
3018 Enter_Name (Formal);
3019 Set_Ekind (Formal, E_Variable);
3020 Set_Etype (Formal, Any_Type);
3021 Restore_Hidden_Primitives (Vis_Prims_List);
3022
3023 if Parent_Installed then
3024 Remove_Parent;
3025 end if;
3026
3027 goto Leave;
3028 end;
3029
3030 Rewrite (N, New_N);
3031 Set_Defining_Unit_Name (Specification (New_N), Formal);
3032 Set_Generic_Parent (Specification (N), Gen_Unit);
3033 Set_Instance_Env (Gen_Unit, Formal);
3034 Set_Is_Generic_Instance (Formal);
3035
3036 Enter_Name (Formal);
3037 Set_Ekind (Formal, E_Package);
3038 Set_Etype (Formal, Standard_Void_Type);
3039 Set_Inner_Instances (Formal, New_Elmt_List);
3040
3041 -- It is unclear that any aspects can apply to a formal package
3042 -- declaration, given that they look like a hidden conformance
3043 -- requirement on the corresponding actual. However, Abstract_State
3044 -- must be treated specially because it generates declarations that
3045 -- must appear before other declarations in the specification and
3046 -- must be analyzed at once.
3047
3048 if Present (Aspect_Specifications (Gen_Decl)) then
3049 if No (Aspect_Specifications (N)) then
3050 Set_Aspect_Specifications (N, New_List);
3051 Set_Has_Aspects (N);
3052 end if;
3053
3054 declare
3055 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3056 New_A : Node_Id;
3057
3058 begin
3059 while Present (ASN) loop
3060 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3061 New_A :=
3062 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3063 Set_Entity (New_A, Formal);
3064 Set_Analyzed (New_A, False);
3065 Append (New_A, Aspect_Specifications (N));
3066 Analyze_Aspect_Specifications (N, Formal);
3067 exit;
3068 end if;
3069
3070 Next (ASN);
3071 end loop;
3072 end;
3073 end if;
3074
3075 Push_Scope (Formal);
3076
3077 -- Manually set the SPARK_Mode from the context because the package
3078 -- declaration is never analyzed.
3079
3080 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3081 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3082 Set_SPARK_Pragma_Inherited (Formal);
3083 Set_SPARK_Aux_Pragma_Inherited (Formal);
3084
3085 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3086
3087 -- Similarly, we have to make the name of the formal visible in the
3088 -- parent instance, to resolve properly fully qualified names that
3089 -- may appear in the generic unit. The parent instance has been
3090 -- placed on the scope stack ahead of the current scope.
3091
3092 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3093
3094 Renaming_In_Par :=
3095 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3096 Set_Ekind (Renaming_In_Par, E_Package);
3097 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3098 Set_Scope (Renaming_In_Par, Parent_Instance);
3099 Set_Parent (Renaming_In_Par, Parent (Formal));
3100 Set_Renamed_Object (Renaming_In_Par, Formal);
3101 Append_Entity (Renaming_In_Par, Parent_Instance);
3102 end if;
3103
3104 -- A formal package declaration behaves as a package instantiation with
3105 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3106 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3107 -- all SPARK_Mode pragmas within the generic_package_name.
3108
3109 if SPARK_Mode /= On then
3110 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3111
3112 -- Mark the formal spec in case the body is instantiated at a later
3113 -- pass. This preserves the original context in effect for the body.
3114
3115 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3116 end if;
3117
3118 Analyze (Specification (N));
3119
3120 -- The formals for which associations are provided are not visible
3121 -- outside of the formal package. The others are still declared by a
3122 -- formal parameter declaration.
3123
3124 -- If there are no associations, the only local entity to hide is the
3125 -- generated package renaming itself.
3126
3127 declare
3128 E : Entity_Id;
3129
3130 begin
3131 E := First_Entity (Formal);
3132 while Present (E) loop
3133 if Associations and then not Is_Generic_Formal (E) then
3134 Set_Is_Hidden (E);
3135 end if;
3136
3137 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3138 Set_Is_Hidden (E);
3139 exit;
3140 end if;
3141
3142 Next_Entity (E);
3143 end loop;
3144 end;
3145
3146 End_Package_Scope (Formal);
3147 Restore_Hidden_Primitives (Vis_Prims_List);
3148
3149 if Parent_Installed then
3150 Remove_Parent;
3151 end if;
3152
3153 Restore_Env;
3154
3155 -- Inside the generic unit, the formal package is a regular package, but
3156 -- no body is needed for it. Note that after instantiation, the defining
3157 -- unit name we need is in the new tree and not in the original (see
3158 -- Package_Instantiation). A generic formal package is an instance, and
3159 -- can be used as an actual for an inner instance.
3160
3161 Set_Has_Completion (Formal, True);
3162
3163 -- Add semantic information to the original defining identifier.
3164
3165 Set_Ekind (Pack_Id, E_Package);
3166 Set_Etype (Pack_Id, Standard_Void_Type);
3167 Set_Scope (Pack_Id, Scope (Formal));
3168 Set_Has_Completion (Pack_Id, True);
3169
3170 <<Leave>>
3171 if Has_Aspects (N) then
3172 -- Unclear that any other aspects may appear here, snalyze them
3173 -- for completion, given that the grammar allows their appearance.
3174
3175 Analyze_Aspect_Specifications (N, Pack_Id);
3176 end if;
3177
3178 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3179 end Analyze_Formal_Package_Declaration;
3180
3181 ---------------------------------
3182 -- Analyze_Formal_Private_Type --
3183 ---------------------------------
3184
3185 procedure Analyze_Formal_Private_Type
3186 (N : Node_Id;
3187 T : Entity_Id;
3188 Def : Node_Id)
3189 is
3190 begin
3191 New_Private_Type (N, T, Def);
3192
3193 -- Set the size to an arbitrary but legal value
3194
3195 Set_Size_Info (T, Standard_Integer);
3196 Set_RM_Size (T, RM_Size (Standard_Integer));
3197 end Analyze_Formal_Private_Type;
3198
3199 ------------------------------------
3200 -- Analyze_Formal_Incomplete_Type --
3201 ------------------------------------
3202
3203 procedure Analyze_Formal_Incomplete_Type
3204 (T : Entity_Id;
3205 Def : Node_Id)
3206 is
3207 begin
3208 Enter_Name (T);
3209 Set_Ekind (T, E_Incomplete_Type);
3210 Set_Etype (T, T);
3211 Set_Private_Dependents (T, New_Elmt_List);
3212
3213 if Tagged_Present (Def) then
3214 Set_Is_Tagged_Type (T);
3215 Make_Class_Wide_Type (T);
3216 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3217 end if;
3218 end Analyze_Formal_Incomplete_Type;
3219
3220 ----------------------------------------
3221 -- Analyze_Formal_Signed_Integer_Type --
3222 ----------------------------------------
3223
3224 procedure Analyze_Formal_Signed_Integer_Type
3225 (T : Entity_Id;
3226 Def : Node_Id)
3227 is
3228 Base : constant Entity_Id :=
3229 New_Internal_Entity
3230 (E_Signed_Integer_Type,
3231 Current_Scope,
3232 Sloc (Defining_Identifier (Parent (Def))), 'G');
3233
3234 begin
3235 Enter_Name (T);
3236
3237 Set_Ekind (T, E_Signed_Integer_Subtype);
3238 Set_Etype (T, Base);
3239 Set_Size_Info (T, Standard_Integer);
3240 Set_RM_Size (T, RM_Size (Standard_Integer));
3241 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3242 Set_Is_Constrained (T);
3243
3244 Set_Is_Generic_Type (Base);
3245 Set_Size_Info (Base, Standard_Integer);
3246 Set_RM_Size (Base, RM_Size (Standard_Integer));
3247 Set_Etype (Base, Base);
3248 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3249 Set_Parent (Base, Parent (Def));
3250 end Analyze_Formal_Signed_Integer_Type;
3251
3252 -------------------------------------------
3253 -- Analyze_Formal_Subprogram_Declaration --
3254 -------------------------------------------
3255
3256 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3257 Spec : constant Node_Id := Specification (N);
3258 Def : constant Node_Id := Default_Name (N);
3259 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3260 Subp : Entity_Id;
3261
3262 begin
3263 if Nam = Error then
3264 return;
3265 end if;
3266
3267 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3268 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3269 goto Leave;
3270 end if;
3271
3272 Analyze_Subprogram_Declaration (N);
3273 Set_Is_Formal_Subprogram (Nam);
3274 Set_Has_Completion (Nam);
3275
3276 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3277 Set_Is_Abstract_Subprogram (Nam);
3278
3279 Set_Is_Dispatching_Operation (Nam);
3280
3281 -- A formal abstract procedure cannot have a null default
3282 -- (RM 12.6(4.1/2)).
3283
3284 if Nkind (Spec) = N_Procedure_Specification
3285 and then Null_Present (Spec)
3286 then
3287 Error_Msg_N
3288 ("a formal abstract subprogram cannot default to null", Spec);
3289 end if;
3290
3291 declare
3292 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3293 begin
3294 if No (Ctrl_Type) then
3295 Error_Msg_N
3296 ("abstract formal subprogram must have a controlling type",
3297 N);
3298
3299 elsif Ada_Version >= Ada_2012
3300 and then Is_Incomplete_Type (Ctrl_Type)
3301 then
3302 Error_Msg_NE
3303 ("controlling type of abstract formal subprogram cannot "
3304 & "be incomplete type", N, Ctrl_Type);
3305
3306 else
3307 Check_Controlling_Formals (Ctrl_Type, Nam);
3308 end if;
3309 end;
3310 end if;
3311
3312 -- Default name is resolved at the point of instantiation
3313
3314 if Box_Present (N) then
3315 null;
3316
3317 -- Else default is bound at the point of generic declaration
3318
3319 elsif Present (Def) then
3320 if Nkind (Def) = N_Operator_Symbol then
3321 Find_Direct_Name (Def);
3322
3323 elsif Nkind (Def) /= N_Attribute_Reference then
3324 Analyze (Def);
3325
3326 else
3327 -- For an attribute reference, analyze the prefix and verify
3328 -- that it has the proper profile for the subprogram.
3329
3330 Analyze (Prefix (Def));
3331 Valid_Default_Attribute (Nam, Def);
3332 goto Leave;
3333 end if;
3334
3335 -- Default name may be overloaded, in which case the interpretation
3336 -- with the correct profile must be selected, as for a renaming.
3337 -- If the definition is an indexed component, it must denote a
3338 -- member of an entry family. If it is a selected component, it
3339 -- can be a protected operation.
3340
3341 if Etype (Def) = Any_Type then
3342 goto Leave;
3343
3344 elsif Nkind (Def) = N_Selected_Component then
3345 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3346 Error_Msg_N ("expect valid subprogram name as default", Def);
3347 end if;
3348
3349 elsif Nkind (Def) = N_Indexed_Component then
3350 if Is_Entity_Name (Prefix (Def)) then
3351 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3352 Error_Msg_N ("expect valid subprogram name as default", Def);
3353 end if;
3354
3355 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3356 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3357 E_Entry_Family
3358 then
3359 Error_Msg_N ("expect valid subprogram name as default", Def);
3360 end if;
3361
3362 else
3363 Error_Msg_N ("expect valid subprogram name as default", Def);
3364 goto Leave;
3365 end if;
3366
3367 elsif Nkind (Def) = N_Character_Literal then
3368
3369 -- Needs some type checks: subprogram should be parameterless???
3370
3371 Resolve (Def, (Etype (Nam)));
3372
3373 elsif not Is_Entity_Name (Def)
3374 or else not Is_Overloadable (Entity (Def))
3375 then
3376 Error_Msg_N ("expect valid subprogram name as default", Def);
3377 goto Leave;
3378
3379 elsif not Is_Overloaded (Def) then
3380 Subp := Entity (Def);
3381
3382 if Subp = Nam then
3383 Error_Msg_N ("premature usage of formal subprogram", Def);
3384
3385 elsif not Entity_Matches_Spec (Subp, Nam) then
3386 Error_Msg_N ("no visible entity matches specification", Def);
3387 end if;
3388
3389 -- More than one interpretation, so disambiguate as for a renaming
3390
3391 else
3392 declare
3393 I : Interp_Index;
3394 I1 : Interp_Index := 0;
3395 It : Interp;
3396 It1 : Interp;
3397
3398 begin
3399 Subp := Any_Id;
3400 Get_First_Interp (Def, I, It);
3401 while Present (It.Nam) loop
3402 if Entity_Matches_Spec (It.Nam, Nam) then
3403 if Subp /= Any_Id then
3404 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3405
3406 if It1 = No_Interp then
3407 Error_Msg_N ("ambiguous default subprogram", Def);
3408 else
3409 Subp := It1.Nam;
3410 end if;
3411
3412 exit;
3413
3414 else
3415 I1 := I;
3416 Subp := It.Nam;
3417 end if;
3418 end if;
3419
3420 Get_Next_Interp (I, It);
3421 end loop;
3422 end;
3423
3424 if Subp /= Any_Id then
3425
3426 -- Subprogram found, generate reference to it
3427
3428 Set_Entity (Def, Subp);
3429 Generate_Reference (Subp, Def);
3430
3431 if Subp = Nam then
3432 Error_Msg_N ("premature usage of formal subprogram", Def);
3433
3434 elsif Ekind (Subp) /= E_Operator then
3435 Check_Mode_Conformant (Subp, Nam);
3436 end if;
3437
3438 else
3439 Error_Msg_N ("no visible subprogram matches specification", N);
3440 end if;
3441 end if;
3442 end if;
3443
3444 <<Leave>>
3445 if Has_Aspects (N) then
3446 Analyze_Aspect_Specifications (N, Nam);
3447 end if;
3448
3449 end Analyze_Formal_Subprogram_Declaration;
3450
3451 -------------------------------------
3452 -- Analyze_Formal_Type_Declaration --
3453 -------------------------------------
3454
3455 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3456 Def : constant Node_Id := Formal_Type_Definition (N);
3457 T : Entity_Id;
3458
3459 begin
3460 T := Defining_Identifier (N);
3461
3462 if Present (Discriminant_Specifications (N))
3463 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3464 then
3465 Error_Msg_N
3466 ("discriminants not allowed for this formal type", T);
3467 end if;
3468
3469 -- Enter the new name, and branch to specific routine
3470
3471 case Nkind (Def) is
3472 when N_Formal_Private_Type_Definition =>
3473 Analyze_Formal_Private_Type (N, T, Def);
3474
3475 when N_Formal_Derived_Type_Definition =>
3476 Analyze_Formal_Derived_Type (N, T, Def);
3477
3478 when N_Formal_Incomplete_Type_Definition =>
3479 Analyze_Formal_Incomplete_Type (T, Def);
3480
3481 when N_Formal_Discrete_Type_Definition =>
3482 Analyze_Formal_Discrete_Type (T, Def);
3483
3484 when N_Formal_Signed_Integer_Type_Definition =>
3485 Analyze_Formal_Signed_Integer_Type (T, Def);
3486
3487 when N_Formal_Modular_Type_Definition =>
3488 Analyze_Formal_Modular_Type (T, Def);
3489
3490 when N_Formal_Floating_Point_Definition =>
3491 Analyze_Formal_Floating_Type (T, Def);
3492
3493 when N_Formal_Ordinary_Fixed_Point_Definition =>
3494 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3495
3496 when N_Formal_Decimal_Fixed_Point_Definition =>
3497 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3498
3499 when N_Array_Type_Definition =>
3500 Analyze_Formal_Array_Type (T, Def);
3501
3502 when N_Access_Function_Definition
3503 | N_Access_Procedure_Definition
3504 | N_Access_To_Object_Definition
3505 =>
3506 Analyze_Generic_Access_Type (T, Def);
3507
3508 -- Ada 2005: a interface declaration is encoded as an abstract
3509 -- record declaration or a abstract type derivation.
3510
3511 when N_Record_Definition =>
3512 Analyze_Formal_Interface_Type (N, T, Def);
3513
3514 when N_Derived_Type_Definition =>
3515 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3516
3517 when N_Error =>
3518 null;
3519
3520 when others =>
3521 raise Program_Error;
3522 end case;
3523
3524 -- A formal type declaration declares a type and its first
3525 -- subtype.
3526
3527 Set_Is_Generic_Type (T);
3528 Set_Is_First_Subtype (T);
3529
3530 if Has_Aspects (N) then
3531 Analyze_Aspect_Specifications (N, T);
3532 end if;
3533 end Analyze_Formal_Type_Declaration;
3534
3535 ------------------------------------
3536 -- Analyze_Function_Instantiation --
3537 ------------------------------------
3538
3539 procedure Analyze_Function_Instantiation (N : Node_Id) is
3540 begin
3541 Analyze_Subprogram_Instantiation (N, E_Function);
3542 end Analyze_Function_Instantiation;
3543
3544 ---------------------------------
3545 -- Analyze_Generic_Access_Type --
3546 ---------------------------------
3547
3548 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3549 begin
3550 Enter_Name (T);
3551
3552 if Nkind (Def) = N_Access_To_Object_Definition then
3553 Access_Type_Declaration (T, Def);
3554
3555 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3556 and then No (Full_View (Designated_Type (T)))
3557 and then not Is_Generic_Type (Designated_Type (T))
3558 then
3559 Error_Msg_N ("premature usage of incomplete type", Def);
3560
3561 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3562 Error_Msg_N
3563 ("only a subtype mark is allowed in a formal", Def);
3564 end if;
3565
3566 else
3567 Access_Subprogram_Declaration (T, Def);
3568 end if;
3569 end Analyze_Generic_Access_Type;
3570
3571 ---------------------------------
3572 -- Analyze_Generic_Formal_Part --
3573 ---------------------------------
3574
3575 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3576 Gen_Parm_Decl : Node_Id;
3577
3578 begin
3579 -- The generic formals are processed in the scope of the generic unit,
3580 -- where they are immediately visible. The scope is installed by the
3581 -- caller.
3582
3583 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3584 while Present (Gen_Parm_Decl) loop
3585 Analyze (Gen_Parm_Decl);
3586 Next (Gen_Parm_Decl);
3587 end loop;
3588
3589 Generate_Reference_To_Generic_Formals (Current_Scope);
3590
3591 -- For Ada 2020, some formal parameters can carry aspects, which must
3592 -- be name-resolved at the end of the list of formal parameters (which
3593 -- has the semantics of a declaration list).
3594
3595 Analyze_Contracts (Generic_Formal_Declarations (N));
3596 end Analyze_Generic_Formal_Part;
3597
3598 ------------------------------------------
3599 -- Analyze_Generic_Package_Declaration --
3600 ------------------------------------------
3601
3602 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3603 Decls : constant List_Id := Visible_Declarations (Specification (N));
3604 Loc : constant Source_Ptr := Sloc (N);
3605
3606 Decl : Node_Id;
3607 Id : Entity_Id;
3608 New_N : Node_Id;
3609 Renaming : Node_Id;
3610 Save_Parent : Node_Id;
3611
3612 begin
3613 -- A generic may grant access to its private enclosing context depending
3614 -- on the placement of its corresponding body. From elaboration point of
3615 -- view, the flow of execution may enter this private context, and then
3616 -- reach an external unit, thus producing a dependency on that external
3617 -- unit. For such a path to be properly discovered and encoded in the
3618 -- ALI file of the main unit, let the ABE mechanism process the body of
3619 -- the main unit, and encode all relevant invocation constructs and the
3620 -- relations between them.
3621
3622 Mark_Save_Invocation_Graph_Of_Body;
3623
3624 -- We introduce a renaming of the enclosing package, to have a usable
3625 -- entity as the prefix of an expanded name for a local entity of the
3626 -- form Par.P.Q, where P is the generic package. This is because a local
3627 -- entity named P may hide it, so that the usual visibility rules in
3628 -- the instance will not resolve properly.
3629
3630 Renaming :=
3631 Make_Package_Renaming_Declaration (Loc,
3632 Defining_Unit_Name =>
3633 Make_Defining_Identifier (Loc,
3634 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3635 Name =>
3636 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3637
3638 -- The declaration is inserted before other declarations, but before
3639 -- pragmas that may be library-unit pragmas and must appear before other
3640 -- declarations. The pragma Compile_Time_Error is not in this class, and
3641 -- may contain an expression that includes such a qualified name, so the
3642 -- renaming declaration must appear before it.
3643
3644 -- Are there other pragmas that require this special handling ???
3645
3646 if Present (Decls) then
3647 Decl := First (Decls);
3648 while Present (Decl)
3649 and then Nkind (Decl) = N_Pragma
3650 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3651 loop
3652 Next (Decl);
3653 end loop;
3654
3655 if Present (Decl) then
3656 Insert_Before (Decl, Renaming);
3657 else
3658 Append (Renaming, Visible_Declarations (Specification (N)));
3659 end if;
3660
3661 else
3662 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3663 end if;
3664
3665 -- Create copy of generic unit, and save for instantiation. If the unit
3666 -- is a child unit, do not copy the specifications for the parent, which
3667 -- are not part of the generic tree.
3668
3669 Save_Parent := Parent_Spec (N);
3670 Set_Parent_Spec (N, Empty);
3671
3672 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3673 Set_Parent_Spec (New_N, Save_Parent);
3674 Rewrite (N, New_N);
3675
3676 -- Once the contents of the generic copy and the template are swapped,
3677 -- do the same for their respective aspect specifications.
3678
3679 Exchange_Aspects (N, New_N);
3680
3681 -- Collect all contract-related source pragmas found within the template
3682 -- and attach them to the contract of the package spec. This contract is
3683 -- used in the capture of global references within annotations.
3684
3685 Create_Generic_Contract (N);
3686
3687 Id := Defining_Entity (N);
3688 Generate_Definition (Id);
3689
3690 -- Expansion is not applied to generic units
3691
3692 Start_Generic;
3693
3694 Enter_Name (Id);
3695 Set_Ekind (Id, E_Generic_Package);
3696 Set_Etype (Id, Standard_Void_Type);
3697
3698 -- Set SPARK_Mode from context
3699
3700 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3701 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3702 Set_SPARK_Pragma_Inherited (Id);
3703 Set_SPARK_Aux_Pragma_Inherited (Id);
3704
3705 -- Preserve relevant elaboration-related attributes of the context which
3706 -- are no longer available or very expensive to recompute once analysis,
3707 -- resolution, and expansion are over.
3708
3709 Mark_Elaboration_Attributes
3710 (N_Id => Id,
3711 Checks => True,
3712 Warnings => True);
3713
3714 -- Analyze aspects now, so that generated pragmas appear in the
3715 -- declarations before building and analyzing the generic copy.
3716
3717 if Has_Aspects (N) then
3718 Analyze_Aspect_Specifications (N, Id);
3719 end if;
3720
3721 Push_Scope (Id);
3722 Enter_Generic_Scope (Id);
3723 Set_Inner_Instances (Id, New_Elmt_List);
3724
3725 Set_Categorization_From_Pragmas (N);
3726 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3727
3728 -- Link the declaration of the generic homonym in the generic copy to
3729 -- the package it renames, so that it is always resolved properly.
3730
3731 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3732 Set_Entity (Associated_Node (Name (Renaming)), Id);
3733
3734 -- For a library unit, we have reconstructed the entity for the unit,
3735 -- and must reset it in the library tables.
3736
3737 if Nkind (Parent (N)) = N_Compilation_Unit then
3738 Set_Cunit_Entity (Current_Sem_Unit, Id);
3739 end if;
3740
3741 Analyze_Generic_Formal_Part (N);
3742
3743 -- After processing the generic formals, analysis proceeds as for a
3744 -- non-generic package.
3745
3746 Analyze (Specification (N));
3747
3748 Validate_Categorization_Dependency (N, Id);
3749
3750 End_Generic;
3751
3752 End_Package_Scope (Id);
3753 Exit_Generic_Scope (Id);
3754
3755 -- If the generic appears within a package unit, the body of that unit
3756 -- has to be present for instantiation and inlining.
3757
3758 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3759 Set_Body_Needed_For_Inlining
3760 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3761 end if;
3762
3763 if Nkind (Parent (N)) /= N_Compilation_Unit then
3764 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3765 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3766 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3767
3768 else
3769 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3770 Validate_RT_RAT_Component (N);
3771
3772 -- If this is a spec without a body, check that generic parameters
3773 -- are referenced.
3774
3775 if not Body_Required (Parent (N)) then
3776 Check_References (Id);
3777 end if;
3778 end if;
3779
3780 -- If there is a specified storage pool in the context, create an
3781 -- aspect on the package declaration, so that it is used in any
3782 -- instance that does not override it.
3783
3784 if Present (Default_Pool) then
3785 declare
3786 ASN : Node_Id;
3787
3788 begin
3789 ASN :=
3790 Make_Aspect_Specification (Loc,
3791 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3792 Expression => New_Copy (Default_Pool));
3793
3794 if No (Aspect_Specifications (Specification (N))) then
3795 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3796 else
3797 Append (ASN, Aspect_Specifications (Specification (N)));
3798 end if;
3799 end;
3800 end if;
3801 end Analyze_Generic_Package_Declaration;
3802
3803 --------------------------------------------
3804 -- Analyze_Generic_Subprogram_Declaration --
3805 --------------------------------------------
3806
3807 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3808 Formals : List_Id;
3809 Id : Entity_Id;
3810 New_N : Node_Id;
3811 Result_Type : Entity_Id;
3812 Save_Parent : Node_Id;
3813 Spec : Node_Id;
3814 Typ : Entity_Id;
3815
3816 begin
3817 -- A generic may grant access to its private enclosing context depending
3818 -- on the placement of its corresponding body. From elaboration point of
3819 -- view, the flow of execution may enter this private context, and then
3820 -- reach an external unit, thus producing a dependency on that external
3821 -- unit. For such a path to be properly discovered and encoded in the
3822 -- ALI file of the main unit, let the ABE mechanism process the body of
3823 -- the main unit, and encode all relevant invocation constructs and the
3824 -- relations between them.
3825
3826 Mark_Save_Invocation_Graph_Of_Body;
3827
3828 -- Create copy of generic unit, and save for instantiation. If the unit
3829 -- is a child unit, do not copy the specifications for the parent, which
3830 -- are not part of the generic tree.
3831
3832 Save_Parent := Parent_Spec (N);
3833 Set_Parent_Spec (N, Empty);
3834
3835 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3836 Set_Parent_Spec (New_N, Save_Parent);
3837 Rewrite (N, New_N);
3838
3839 -- Once the contents of the generic copy and the template are swapped,
3840 -- do the same for their respective aspect specifications.
3841
3842 Exchange_Aspects (N, New_N);
3843
3844 -- Collect all contract-related source pragmas found within the template
3845 -- and attach them to the contract of the subprogram spec. This contract
3846 -- is used in the capture of global references within annotations.
3847
3848 Create_Generic_Contract (N);
3849
3850 Spec := Specification (N);
3851 Id := Defining_Entity (Spec);
3852 Generate_Definition (Id);
3853
3854 if Nkind (Id) = N_Defining_Operator_Symbol then
3855 Error_Msg_N
3856 ("operator symbol not allowed for generic subprogram", Id);
3857 end if;
3858
3859 Start_Generic;
3860
3861 Enter_Name (Id);
3862 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3863
3864 Push_Scope (Id);
3865 Enter_Generic_Scope (Id);
3866 Set_Inner_Instances (Id, New_Elmt_List);
3867 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3868
3869 Analyze_Generic_Formal_Part (N);
3870
3871 if Nkind (Spec) = N_Function_Specification then
3872 Set_Ekind (Id, E_Generic_Function);
3873 else
3874 Set_Ekind (Id, E_Generic_Procedure);
3875 end if;
3876
3877 -- Set SPARK_Mode from context
3878
3879 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3880 Set_SPARK_Pragma_Inherited (Id);
3881
3882 -- Preserve relevant elaboration-related attributes of the context which
3883 -- are no longer available or very expensive to recompute once analysis,
3884 -- resolution, and expansion are over.
3885
3886 Mark_Elaboration_Attributes
3887 (N_Id => Id,
3888 Checks => True,
3889 Warnings => True);
3890
3891 Formals := Parameter_Specifications (Spec);
3892
3893 if Present (Formals) then
3894 Process_Formals (Formals, Spec);
3895 end if;
3896
3897 if Nkind (Spec) = N_Function_Specification then
3898 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3899 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3900 Set_Etype (Id, Result_Type);
3901
3902 -- Check restriction imposed by AI05-073: a generic function
3903 -- cannot return an abstract type or an access to such.
3904
3905 -- This is a binding interpretation should it apply to earlier
3906 -- versions of Ada as well as Ada 2012???
3907
3908 if Is_Abstract_Type (Designated_Type (Result_Type))
3909 and then Ada_Version >= Ada_2012
3910 then
3911 Error_Msg_N
3912 ("generic function cannot have an access result "
3913 & "that designates an abstract type", Spec);
3914 end if;
3915
3916 else
3917 Find_Type (Result_Definition (Spec));
3918 Typ := Entity (Result_Definition (Spec));
3919
3920 if Is_Abstract_Type (Typ)
3921 and then Ada_Version >= Ada_2012
3922 then
3923 Error_Msg_N
3924 ("generic function cannot have abstract result type", Spec);
3925 end if;
3926
3927 -- If a null exclusion is imposed on the result type, then create
3928 -- a null-excluding itype (an access subtype) and use it as the
3929 -- function's Etype.
3930
3931 if Is_Access_Type (Typ)
3932 and then Null_Exclusion_Present (Spec)
3933 then
3934 Set_Etype (Id,
3935 Create_Null_Excluding_Itype
3936 (T => Typ,
3937 Related_Nod => Spec,
3938 Scope_Id => Defining_Unit_Name (Spec)));
3939 else
3940 Set_Etype (Id, Typ);
3941 end if;
3942 end if;
3943
3944 else
3945 Set_Etype (Id, Standard_Void_Type);
3946 end if;
3947
3948 -- Analyze the aspects of the generic copy to ensure that all generated
3949 -- pragmas (if any) perform their semantic effects.
3950
3951 if Has_Aspects (N) then
3952 Analyze_Aspect_Specifications (N, Id);
3953 end if;
3954
3955 -- For a library unit, we have reconstructed the entity for the unit,
3956 -- and must reset it in the library tables. We also make sure that
3957 -- Body_Required is set properly in the original compilation unit node.
3958
3959 if Nkind (Parent (N)) = N_Compilation_Unit then
3960 Set_Cunit_Entity (Current_Sem_Unit, Id);
3961 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3962 end if;
3963
3964 -- If the generic appears within a package unit, the body of that unit
3965 -- has to be present for instantiation and inlining.
3966
3967 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3968 and then Unit_Requires_Body (Id)
3969 then
3970 Set_Body_Needed_For_Inlining
3971 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3972 end if;
3973
3974 Set_Categorization_From_Pragmas (N);
3975 Validate_Categorization_Dependency (N, Id);
3976
3977 -- Capture all global references that occur within the profile of the
3978 -- generic subprogram. Aspects are not part of this processing because
3979 -- they must be delayed. If processed now, Save_Global_References will
3980 -- destroy the Associated_Node links and prevent the capture of global
3981 -- references when the contract of the generic subprogram is analyzed.
3982
3983 Save_Global_References (Original_Node (N));
3984
3985 End_Generic;
3986 End_Scope;
3987 Exit_Generic_Scope (Id);
3988 Generate_Reference_To_Formals (Id);
3989
3990 List_Inherited_Pre_Post_Aspects (Id);
3991 end Analyze_Generic_Subprogram_Declaration;
3992
3993 -----------------------------------
3994 -- Analyze_Package_Instantiation --
3995 -----------------------------------
3996
3997 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3998 -- must be replaced by gotos which jump to the end of the routine in order
3999 -- to restore the Ghost and SPARK modes.
4000
4001 procedure Analyze_Package_Instantiation (N : Node_Id) is
4002 Has_Inline_Always : Boolean := False;
4003 -- Set if the generic unit contains any subprograms with Inline_Always.
4004 -- Only relevant when back-end inlining is not enabled.
4005
4006 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4007 -- Return True if inlining is active and Gen_Unit contains inlined
4008 -- subprograms. In this case, we may either instantiate the body when
4009 -- front-end inlining is enabled, or add a pending instantiation when
4010 -- back-end inlining is enabled. In the former case, this may cause
4011 -- superfluous instantiations, but in either case we need to perform
4012 -- the instantiation of the body in the context of the instance and
4013 -- not in that of the point of inlining.
4014
4015 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4016 -- Return True if Gen_Unit needs to have its body instantiated in the
4017 -- context of N. This in particular excludes generic contexts.
4018
4019 -----------------------
4020 -- Might_Inline_Subp --
4021 -----------------------
4022
4023 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4024 E : Entity_Id;
4025
4026 begin
4027 if Inline_Processing_Required then
4028 -- No need to recompute the answer if we know it is positive
4029 -- and back-end inlining is enabled.
4030
4031 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4032 return True;
4033 end if;
4034
4035 E := First_Entity (Gen_Unit);
4036 while Present (E) loop
4037 if Is_Subprogram (E) and then Is_Inlined (E) then
4038 -- Remember if there are any subprograms with Inline_Always
4039
4040 if Has_Pragma_Inline_Always (E) then
4041 Has_Inline_Always := True;
4042 end if;
4043
4044 Set_Is_Inlined (Gen_Unit);
4045 return True;
4046 end if;
4047
4048 Next_Entity (E);
4049 end loop;
4050 end if;
4051
4052 return False;
4053 end Might_Inline_Subp;
4054
4055 -------------------------------
4056 -- Needs_Body_Instantiated --
4057 -------------------------------
4058
4059 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4060 begin
4061 -- No need to instantiate bodies in generic units
4062
4063 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4064 return False;
4065 end if;
4066
4067 -- If the instantiation is in the main unit, then the body is needed
4068
4069 if Is_In_Main_Unit (N) then
4070 return True;
4071 end if;
4072
4073 -- In GNATprove mode, never instantiate bodies outside of the main
4074 -- unit, as it does not use frontend/backend inlining in the way that
4075 -- GNAT does, so does not benefit from such instantiations. On the
4076 -- contrary, such instantiations may bring artificial constraints,
4077 -- as for example such bodies may require preprocessing.
4078
4079 if GNATprove_Mode then
4080 return False;
4081 end if;
4082
4083 -- If not, then again no need to instantiate bodies in generic units
4084
4085 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4086 return False;
4087 end if;
4088
4089 -- Here we have a special handling for back-end inlining: if inline
4090 -- processing is required, then we unconditionally want to have the
4091 -- body instantiated. The reason is that Might_Inline_Subp does not
4092 -- catch all the cases (as it does not recurse into nested packages)
4093 -- so this avoids the need to patch things up afterwards. Moreover,
4094 -- these instantiations are only performed on demand when back-end
4095 -- inlining is enabled, so this causes very little extra work.
4096
4097 if Inline_Processing_Required and then Back_End_Inlining then
4098 return True;
4099 end if;
4100
4101 -- We want to have the bodies instantiated in non-main units if
4102 -- they might contribute inlined subprograms.
4103
4104 return Might_Inline_Subp (Gen_Unit);
4105 end Needs_Body_Instantiated;
4106
4107 -- Local declarations
4108
4109 Gen_Id : constant Node_Id := Name (N);
4110 Inst_Id : constant Entity_Id := Defining_Entity (N);
4111 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4112 Loc : constant Source_Ptr := Sloc (N);
4113
4114 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4115 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4116 Saved_ISMP : constant Boolean :=
4117 Ignore_SPARK_Mode_Pragmas_In_Instance;
4118 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4119 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4120 -- Save the Ghost and SPARK mode-related data to restore on exit
4121
4122 Saved_Style_Check : constant Boolean := Style_Check;
4123 -- Save style check mode for restore on exit
4124
4125 Act_Decl : Node_Id;
4126 Act_Decl_Name : Node_Id;
4127 Act_Decl_Id : Entity_Id;
4128 Act_Spec : Node_Id;
4129 Act_Tree : Node_Id;
4130 Env_Installed : Boolean := False;
4131 Gen_Decl : Node_Id;
4132 Gen_Spec : Node_Id;
4133 Gen_Unit : Entity_Id;
4134 Inline_Now : Boolean := False;
4135 Needs_Body : Boolean;
4136 Parent_Installed : Boolean := False;
4137 Renaming_List : List_Id;
4138 Unit_Renaming : Node_Id;
4139
4140 Vis_Prims_List : Elist_Id := No_Elist;
4141 -- List of primitives made temporarily visible in the instantiation
4142 -- to match the visibility of the formal type
4143
4144 -- Start of processing for Analyze_Package_Instantiation
4145
4146 begin
4147 -- Preserve relevant elaboration-related attributes of the context which
4148 -- are no longer available or very expensive to recompute once analysis,
4149 -- resolution, and expansion are over.
4150
4151 Mark_Elaboration_Attributes
4152 (N_Id => N,
4153 Checks => True,
4154 Level => True,
4155 Modes => True,
4156 Warnings => True);
4157
4158 -- Very first thing: check for Text_IO special unit in case we are
4159 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4160
4161 Check_Text_IO_Special_Unit (Name (N));
4162
4163 -- Make node global for error reporting
4164
4165 Instantiation_Node := N;
4166
4167 -- Case of instantiation of a generic package
4168
4169 if Nkind (N) = N_Package_Instantiation then
4170 Act_Decl_Id := New_Copy (Defining_Entity (N));
4171 Set_Comes_From_Source (Act_Decl_Id, True);
4172
4173 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4174 Act_Decl_Name :=
4175 Make_Defining_Program_Unit_Name (Loc,
4176 Name =>
4177 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4178 Defining_Identifier => Act_Decl_Id);
4179 else
4180 Act_Decl_Name := Act_Decl_Id;
4181 end if;
4182
4183 -- Case of instantiation of a formal package
4184
4185 else
4186 Act_Decl_Id := Defining_Identifier (N);
4187 Act_Decl_Name := Act_Decl_Id;
4188 end if;
4189
4190 Generate_Definition (Act_Decl_Id);
4191 Set_Ekind (Act_Decl_Id, E_Package);
4192
4193 -- Initialize list of incomplete actuals before analysis
4194
4195 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4196
4197 Preanalyze_Actuals (N, Act_Decl_Id);
4198
4199 -- Turn off style checking in instances. If the check is enabled on the
4200 -- generic unit, a warning in an instance would just be noise. If not
4201 -- enabled on the generic, then a warning in an instance is just wrong.
4202 -- This must be done after analyzing the actuals, which do come from
4203 -- source and are subject to style checking.
4204
4205 Style_Check := False;
4206
4207 Init_Env;
4208 Env_Installed := True;
4209
4210 -- Reset renaming map for formal types. The mapping is established
4211 -- when analyzing the generic associations, but some mappings are
4212 -- inherited from formal packages of parent units, and these are
4213 -- constructed when the parents are installed.
4214
4215 Generic_Renamings.Set_Last (0);
4216 Generic_Renamings_HTable.Reset;
4217
4218 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4219 Gen_Unit := Entity (Gen_Id);
4220
4221 -- A package instantiation is Ghost when it is subject to pragma Ghost
4222 -- or the generic template is Ghost. Set the mode now to ensure that
4223 -- any nodes generated during analysis and expansion are marked as
4224 -- Ghost.
4225
4226 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4227
4228 -- Verify that it is the name of a generic package
4229
4230 -- A visibility glitch: if the instance is a child unit and the generic
4231 -- is the generic unit of a parent instance (i.e. both the parent and
4232 -- the child units are instances of the same package) the name now
4233 -- denotes the renaming within the parent, not the intended generic
4234 -- unit. See if there is a homonym that is the desired generic. The
4235 -- renaming declaration must be visible inside the instance of the
4236 -- child, but not when analyzing the name in the instantiation itself.
4237
4238 if Ekind (Gen_Unit) = E_Package
4239 and then Present (Renamed_Entity (Gen_Unit))
4240 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4241 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4242 and then Present (Homonym (Gen_Unit))
4243 then
4244 Gen_Unit := Homonym (Gen_Unit);
4245 end if;
4246
4247 if Etype (Gen_Unit) = Any_Type then
4248 Restore_Env;
4249 goto Leave;
4250
4251 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4252
4253 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4254
4255 if From_Limited_With (Gen_Unit) then
4256 Error_Msg_N
4257 ("cannot instantiate a limited withed package", Gen_Id);
4258 else
4259 Error_Msg_NE
4260 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4261 end if;
4262
4263 Restore_Env;
4264 goto Leave;
4265 end if;
4266
4267 if In_Extended_Main_Source_Unit (N) then
4268 Set_Is_Instantiated (Gen_Unit);
4269 Generate_Reference (Gen_Unit, N);
4270
4271 if Present (Renamed_Object (Gen_Unit)) then
4272 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4273 Generate_Reference (Renamed_Object (Gen_Unit), N);
4274 end if;
4275 end if;
4276
4277 if Nkind (Gen_Id) = N_Identifier
4278 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4279 then
4280 Error_Msg_NE
4281 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4282
4283 elsif Nkind (Gen_Id) = N_Expanded_Name
4284 and then Is_Child_Unit (Gen_Unit)
4285 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4286 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4287 then
4288 Error_Msg_N
4289 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4290 end if;
4291
4292 Set_Entity (Gen_Id, Gen_Unit);
4293
4294 -- If generic is a renaming, get original generic unit
4295
4296 if Present (Renamed_Object (Gen_Unit))
4297 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4298 then
4299 Gen_Unit := Renamed_Object (Gen_Unit);
4300 end if;
4301
4302 -- Verify that there are no circular instantiations
4303
4304 if In_Open_Scopes (Gen_Unit) then
4305 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4306 Restore_Env;
4307 goto Leave;
4308
4309 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4310 Error_Msg_Node_2 := Current_Scope;
4311 Error_Msg_NE
4312 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4313 Circularity_Detected := True;
4314 Restore_Env;
4315 goto Leave;
4316
4317 else
4318 Set_Ekind (Inst_Id, E_Package);
4319 Set_Scope (Inst_Id, Current_Scope);
4320
4321 -- If the context of the instance is subject to SPARK_Mode "off" or
4322 -- the annotation is altogether missing, set the global flag which
4323 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4324 -- the instance.
4325
4326 if SPARK_Mode /= On then
4327 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4328
4329 -- Mark the instance spec in case the body is instantiated at a
4330 -- later pass. This preserves the original context in effect for
4331 -- the body.
4332
4333 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4334 end if;
4335
4336 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4337 Gen_Spec := Specification (Gen_Decl);
4338
4339 -- Initialize renamings map, for error checking, and the list that
4340 -- holds private entities whose views have changed between generic
4341 -- definition and instantiation. If this is the instance created to
4342 -- validate an actual package, the instantiation environment is that
4343 -- of the enclosing instance.
4344
4345 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4346
4347 -- Copy original generic tree, to produce text for instantiation
4348
4349 Act_Tree :=
4350 Copy_Generic_Node
4351 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4352
4353 Act_Spec := Specification (Act_Tree);
4354
4355 -- If this is the instance created to validate an actual package,
4356 -- only the formals matter, do not examine the package spec itself.
4357
4358 if Is_Actual_Pack then
4359 Set_Visible_Declarations (Act_Spec, New_List);
4360 Set_Private_Declarations (Act_Spec, New_List);
4361 end if;
4362
4363 Renaming_List :=
4364 Analyze_Associations
4365 (I_Node => N,
4366 Formals => Generic_Formal_Declarations (Act_Tree),
4367 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4368
4369 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4370
4371 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4372 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4373 Set_Is_Generic_Instance (Act_Decl_Id);
4374 Set_Generic_Parent (Act_Spec, Gen_Unit);
4375
4376 -- References to the generic in its own declaration or its body are
4377 -- references to the instance. Add a renaming declaration for the
4378 -- generic unit itself. This declaration, as well as the renaming
4379 -- declarations for the generic formals, must remain private to the
4380 -- unit: the formals, because this is the language semantics, and
4381 -- the unit because its use is an artifact of the implementation.
4382
4383 Unit_Renaming :=
4384 Make_Package_Renaming_Declaration (Loc,
4385 Defining_Unit_Name =>
4386 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4387 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4388
4389 Append (Unit_Renaming, Renaming_List);
4390
4391 -- The renaming declarations are the first local declarations of the
4392 -- new unit.
4393
4394 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4395 Insert_List_Before
4396 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4397 else
4398 Set_Visible_Declarations (Act_Spec, Renaming_List);
4399 end if;
4400
4401 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4402
4403 -- Propagate the aspect specifications from the package declaration
4404 -- template to the instantiated version of the package declaration.
4405
4406 if Has_Aspects (Act_Tree) then
4407 Set_Aspect_Specifications (Act_Decl,
4408 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4409 end if;
4410
4411 -- The generic may have a generated Default_Storage_Pool aspect,
4412 -- set at the point of generic declaration. If the instance has
4413 -- that aspect, it overrides the one inherited from the generic.
4414
4415 if Has_Aspects (Gen_Spec) then
4416 if No (Aspect_Specifications (N)) then
4417 Set_Aspect_Specifications (N,
4418 (New_Copy_List_Tree
4419 (Aspect_Specifications (Gen_Spec))));
4420
4421 else
4422 declare
4423 Inherited_Aspects : constant List_Id :=
4424 New_Copy_List_Tree
4425 (Aspect_Specifications (Gen_Spec));
4426
4427 ASN1 : Node_Id;
4428 ASN2 : Node_Id;
4429 Pool_Present : Boolean := False;
4430
4431 begin
4432 ASN1 := First (Aspect_Specifications (N));
4433 while Present (ASN1) loop
4434 if Chars (Identifier (ASN1)) =
4435 Name_Default_Storage_Pool
4436 then
4437 Pool_Present := True;
4438 exit;
4439 end if;
4440
4441 Next (ASN1);
4442 end loop;
4443
4444 if Pool_Present then
4445
4446 -- If generic carries a default storage pool, remove it
4447 -- in favor of the instance one.
4448
4449 ASN2 := First (Inherited_Aspects);
4450 while Present (ASN2) loop
4451 if Chars (Identifier (ASN2)) =
4452 Name_Default_Storage_Pool
4453 then
4454 Remove (ASN2);
4455 exit;
4456 end if;
4457
4458 Next (ASN2);
4459 end loop;
4460 end if;
4461
4462 Prepend_List_To
4463 (Aspect_Specifications (N), Inherited_Aspects);
4464 end;
4465 end if;
4466 end if;
4467
4468 -- Save the instantiation node for a subsequent instantiation of the
4469 -- body if there is one and it needs to be instantiated here.
4470
4471 -- We instantiate the body only if we are generating code, or if we
4472 -- are generating cross-reference information, or for GNATprove use.
4473
4474 declare
4475 Enclosing_Body_Present : Boolean := False;
4476 -- If the generic unit is not a compilation unit, then a body may
4477 -- be present in its parent even if none is required. We create a
4478 -- tentative pending instantiation for the body, which will be
4479 -- discarded if none is actually present.
4480
4481 Scop : Entity_Id;
4482
4483 begin
4484 if Scope (Gen_Unit) /= Standard_Standard
4485 and then not Is_Child_Unit (Gen_Unit)
4486 then
4487 Scop := Scope (Gen_Unit);
4488 while Present (Scop) and then Scop /= Standard_Standard loop
4489 if Unit_Requires_Body (Scop) then
4490 Enclosing_Body_Present := True;
4491 exit;
4492
4493 elsif In_Open_Scopes (Scop)
4494 and then In_Package_Body (Scop)
4495 then
4496 Enclosing_Body_Present := True;
4497 exit;
4498 end if;
4499
4500 exit when Is_Compilation_Unit (Scop);
4501 Scop := Scope (Scop);
4502 end loop;
4503 end if;
4504
4505 -- If front-end inlining is enabled or there are any subprograms
4506 -- marked with Inline_Always, and this is a unit for which code
4507 -- will be generated, we instantiate the body at once.
4508
4509 -- This is done if the instance is not the main unit, and if the
4510 -- generic is not a child unit of another generic, to avoid scope
4511 -- problems and the reinstallation of parent instances.
4512
4513 if Expander_Active
4514 and then (not Is_Child_Unit (Gen_Unit)
4515 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4516 and then Might_Inline_Subp (Gen_Unit)
4517 and then not Is_Actual_Pack
4518 then
4519 if not Back_End_Inlining
4520 and then (Front_End_Inlining or else Has_Inline_Always)
4521 and then (Is_In_Main_Unit (N)
4522 or else In_Main_Context (Current_Scope))
4523 and then Nkind (Parent (N)) /= N_Compilation_Unit
4524 then
4525 Inline_Now := True;
4526
4527 -- In configurable_run_time mode we force the inlining of
4528 -- predefined subprograms marked Inline_Always, to minimize
4529 -- the use of the run-time library.
4530
4531 elsif In_Predefined_Unit (Gen_Decl)
4532 and then Configurable_Run_Time_Mode
4533 and then Nkind (Parent (N)) /= N_Compilation_Unit
4534 then
4535 Inline_Now := True;
4536 end if;
4537
4538 -- If the current scope is itself an instance within a child
4539 -- unit, there will be duplications in the scope stack, and the
4540 -- unstacking mechanism in Inline_Instance_Body will fail.
4541 -- This loses some rare cases of optimization, and might be
4542 -- improved some day, if we can find a proper abstraction for
4543 -- "the complete compilation context" that can be saved and
4544 -- restored. ???
4545
4546 if Is_Generic_Instance (Current_Scope) then
4547 declare
4548 Curr_Unit : constant Entity_Id :=
4549 Cunit_Entity (Current_Sem_Unit);
4550 begin
4551 if Curr_Unit /= Current_Scope
4552 and then Is_Child_Unit (Curr_Unit)
4553 then
4554 Inline_Now := False;
4555 end if;
4556 end;
4557 end if;
4558 end if;
4559
4560 Needs_Body :=
4561 (Unit_Requires_Body (Gen_Unit)
4562 or else Enclosing_Body_Present
4563 or else Present (Corresponding_Body (Gen_Decl)))
4564 and then Needs_Body_Instantiated (Gen_Unit)
4565 and then not Is_Actual_Pack
4566 and then not Inline_Now
4567 and then (Operating_Mode = Generate_Code
4568 or else (Operating_Mode = Check_Semantics
4569 and then GNATprove_Mode));
4570
4571 -- If front-end inlining is enabled or there are any subprograms
4572 -- marked with Inline_Always, do not instantiate body when within
4573 -- a generic context.
4574
4575 if not Back_End_Inlining
4576 and then (Front_End_Inlining or else Has_Inline_Always)
4577 and then not Expander_Active
4578 then
4579 Needs_Body := False;
4580 end if;
4581
4582 -- If the current context is generic, and the package being
4583 -- instantiated is declared within a formal package, there is no
4584 -- body to instantiate until the enclosing generic is instantiated
4585 -- and there is an actual for the formal package. If the formal
4586 -- package has parameters, we build a regular package instance for
4587 -- it, that precedes the original formal package declaration.
4588
4589 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4590 declare
4591 Decl : constant Node_Id :=
4592 Original_Node
4593 (Unit_Declaration_Node (Scope (Gen_Unit)));
4594 begin
4595 if Nkind (Decl) = N_Formal_Package_Declaration
4596 or else (Nkind (Decl) = N_Package_Declaration
4597 and then Is_List_Member (Decl)
4598 and then Present (Next (Decl))
4599 and then
4600 Nkind (Next (Decl)) =
4601 N_Formal_Package_Declaration)
4602 then
4603 Needs_Body := False;
4604 end if;
4605 end;
4606 end if;
4607 end;
4608
4609 -- For RCI unit calling stubs, we omit the instance body if the
4610 -- instance is the RCI library unit itself.
4611
4612 -- However there is a special case for nested instances: in this case
4613 -- we do generate the instance body, as it might be required, e.g.
4614 -- because it provides stream attributes for some type used in the
4615 -- profile of a remote subprogram. This is consistent with 12.3(12),
4616 -- which indicates that the instance body occurs at the place of the
4617 -- instantiation, and thus is part of the RCI declaration, which is
4618 -- present on all client partitions (this is E.2.3(18)).
4619
4620 -- Note that AI12-0002 may make it illegal at some point to have
4621 -- stream attributes defined in an RCI unit, in which case this
4622 -- special case will become unnecessary. In the meantime, there
4623 -- is known application code in production that depends on this
4624 -- being possible, so we definitely cannot eliminate the body in
4625 -- the case of nested instances for the time being.
4626
4627 -- When we generate a nested instance body, calling stubs for any
4628 -- relevant subprogram will be inserted immediately after the
4629 -- subprogram declarations, and will take precedence over the
4630 -- subsequent (original) body. (The stub and original body will be
4631 -- complete homographs, but this is permitted in an instance).
4632 -- (Could we do better and remove the original body???)
4633
4634 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4635 and then Comes_From_Source (N)
4636 and then Nkind (Parent (N)) = N_Compilation_Unit
4637 then
4638 Needs_Body := False;
4639 end if;
4640
4641 if Needs_Body then
4642 -- Indicate that the enclosing scopes contain an instantiation,
4643 -- and that cleanup actions should be delayed until after the
4644 -- instance body is expanded.
4645
4646 Check_Forward_Instantiation (Gen_Decl);
4647 if Nkind (N) = N_Package_Instantiation then
4648 declare
4649 Enclosing_Master : Entity_Id;
4650
4651 begin
4652 -- Loop to search enclosing masters
4653
4654 Enclosing_Master := Current_Scope;
4655 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4656 if Ekind (Enclosing_Master) = E_Package then
4657 if Is_Compilation_Unit (Enclosing_Master) then
4658 if In_Package_Body (Enclosing_Master) then
4659 Set_Delay_Subprogram_Descriptors
4660 (Body_Entity (Enclosing_Master));
4661 else
4662 Set_Delay_Subprogram_Descriptors
4663 (Enclosing_Master);
4664 end if;
4665
4666 exit Scope_Loop;
4667
4668 else
4669 Enclosing_Master := Scope (Enclosing_Master);
4670 end if;
4671
4672 elsif Is_Generic_Unit (Enclosing_Master)
4673 or else Ekind (Enclosing_Master) = E_Void
4674 then
4675 -- Cleanup actions will eventually be performed on the
4676 -- enclosing subprogram or package instance, if any.
4677 -- Enclosing scope is void in the formal part of a
4678 -- generic subprogram.
4679
4680 exit Scope_Loop;
4681
4682 else
4683 if Ekind (Enclosing_Master) = E_Entry
4684 and then
4685 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4686 then
4687 if not Expander_Active then
4688 exit Scope_Loop;
4689 else
4690 Enclosing_Master :=
4691 Protected_Body_Subprogram (Enclosing_Master);
4692 end if;
4693 end if;
4694
4695 Set_Delay_Cleanups (Enclosing_Master);
4696
4697 while Ekind (Enclosing_Master) = E_Block loop
4698 Enclosing_Master := Scope (Enclosing_Master);
4699 end loop;
4700
4701 if Is_Subprogram (Enclosing_Master) then
4702 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4703
4704 elsif Is_Task_Type (Enclosing_Master) then
4705 declare
4706 TBP : constant Node_Id :=
4707 Get_Task_Body_Procedure
4708 (Enclosing_Master);
4709 begin
4710 if Present (TBP) then
4711 Set_Delay_Subprogram_Descriptors (TBP);
4712 Set_Delay_Cleanups (TBP);
4713 end if;
4714 end;
4715 end if;
4716
4717 exit Scope_Loop;
4718 end if;
4719 end loop Scope_Loop;
4720 end;
4721
4722 -- Make entry in table
4723
4724 Add_Pending_Instantiation (N, Act_Decl);
4725 end if;
4726 end if;
4727
4728 Set_Categorization_From_Pragmas (Act_Decl);
4729
4730 if Parent_Installed then
4731 Hide_Current_Scope;
4732 end if;
4733
4734 Set_Instance_Spec (N, Act_Decl);
4735
4736 -- If not a compilation unit, insert the package declaration before
4737 -- the original instantiation node.
4738
4739 if Nkind (Parent (N)) /= N_Compilation_Unit then
4740 Mark_Rewrite_Insertion (Act_Decl);
4741 Insert_Before (N, Act_Decl);
4742
4743 if Has_Aspects (N) then
4744 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4745
4746 -- The pragma created for a Default_Storage_Pool aspect must
4747 -- appear ahead of the declarations in the instance spec.
4748 -- Analysis has placed it after the instance node, so remove
4749 -- it and reinsert it properly now.
4750
4751 declare
4752 ASN : constant Node_Id := First (Aspect_Specifications (N));
4753 A_Name : constant Name_Id := Chars (Identifier (ASN));
4754 Decl : Node_Id;
4755
4756 begin
4757 if A_Name = Name_Default_Storage_Pool then
4758 if No (Visible_Declarations (Act_Spec)) then
4759 Set_Visible_Declarations (Act_Spec, New_List);
4760 end if;
4761
4762 Decl := Next (N);
4763 while Present (Decl) loop
4764 if Nkind (Decl) = N_Pragma then
4765 Remove (Decl);
4766 Prepend (Decl, Visible_Declarations (Act_Spec));
4767 exit;
4768 end if;
4769
4770 Next (Decl);
4771 end loop;
4772 end if;
4773 end;
4774 end if;
4775
4776 Analyze (Act_Decl);
4777
4778 -- For an instantiation that is a compilation unit, place
4779 -- declaration on current node so context is complete for analysis
4780 -- (including nested instantiations). If this is the main unit,
4781 -- the declaration eventually replaces the instantiation node.
4782 -- If the instance body is created later, it replaces the
4783 -- instance node, and the declaration is attached to it
4784 -- (see Build_Instance_Compilation_Unit_Nodes).
4785
4786 else
4787 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4788
4789 -- The entity for the current unit is the newly created one,
4790 -- and all semantic information is attached to it.
4791
4792 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4793
4794 -- If this is the main unit, replace the main entity as well
4795
4796 if Current_Sem_Unit = Main_Unit then
4797 Main_Unit_Entity := Act_Decl_Id;
4798 end if;
4799 end if;
4800
4801 Set_Unit (Parent (N), Act_Decl);
4802 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4803 Set_Package_Instantiation (Act_Decl_Id, N);
4804
4805 -- Process aspect specifications of the instance node, if any, to
4806 -- take into account categorization pragmas before analyzing the
4807 -- instance.
4808
4809 if Has_Aspects (N) then
4810 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4811 end if;
4812
4813 Analyze (Act_Decl);
4814 Set_Unit (Parent (N), N);
4815 Set_Body_Required (Parent (N), False);
4816
4817 -- We never need elaboration checks on instantiations, since by
4818 -- definition, the body instantiation is elaborated at the same
4819 -- time as the spec instantiation.
4820
4821 if Legacy_Elaboration_Checks then
4822 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4823 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4824 end if;
4825 end if;
4826
4827 if Legacy_Elaboration_Checks then
4828 Check_Elab_Instantiation (N);
4829 end if;
4830
4831 -- Save the scenario for later examination by the ABE Processing
4832 -- phase.
4833
4834 Record_Elaboration_Scenario (N);
4835
4836 -- The instantiation results in a guaranteed ABE
4837
4838 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4839 -- Do not instantiate the corresponding body because gigi cannot
4840 -- handle certain types of premature instantiations.
4841
4842 Remove_Dead_Instance (N);
4843
4844 -- Create completing bodies for all subprogram declarations since
4845 -- their real bodies will not be instantiated.
4846
4847 Provide_Completing_Bodies (Instance_Spec (N));
4848 end if;
4849
4850 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4851
4852 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4853 First_Private_Entity (Act_Decl_Id));
4854
4855 -- If the instantiation will receive a body, the unit will be
4856 -- transformed into a package body, and receive its own elaboration
4857 -- entity. Otherwise, the nature of the unit is now a package
4858 -- declaration.
4859
4860 if Nkind (Parent (N)) = N_Compilation_Unit
4861 and then not Needs_Body
4862 then
4863 Rewrite (N, Act_Decl);
4864 end if;
4865
4866 if Present (Corresponding_Body (Gen_Decl))
4867 or else Unit_Requires_Body (Gen_Unit)
4868 then
4869 Set_Has_Completion (Act_Decl_Id);
4870 end if;
4871
4872 Check_Formal_Packages (Act_Decl_Id);
4873
4874 Restore_Hidden_Primitives (Vis_Prims_List);
4875 Restore_Private_Views (Act_Decl_Id);
4876
4877 Inherit_Context (Gen_Decl, N);
4878
4879 if Parent_Installed then
4880 Remove_Parent;
4881 end if;
4882
4883 Restore_Env;
4884 Env_Installed := False;
4885 end if;
4886
4887 Validate_Categorization_Dependency (N, Act_Decl_Id);
4888
4889 -- There used to be a check here to prevent instantiations in local
4890 -- contexts if the No_Local_Allocators restriction was active. This
4891 -- check was removed by a binding interpretation in AI-95-00130/07,
4892 -- but we retain the code for documentation purposes.
4893
4894 -- if Ekind (Act_Decl_Id) /= E_Void
4895 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4896 -- then
4897 -- Check_Restriction (No_Local_Allocators, N);
4898 -- end if;
4899
4900 if Inline_Now then
4901 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4902 end if;
4903
4904 -- Check that if N is an instantiation of System.Dim_Float_IO or
4905 -- System.Dim_Integer_IO, the formal type has a dimension system.
4906
4907 if Nkind (N) = N_Package_Instantiation
4908 and then Is_Dim_IO_Package_Instantiation (N)
4909 then
4910 declare
4911 Assoc : constant Node_Id := First (Generic_Associations (N));
4912 begin
4913 if not Has_Dimension_System
4914 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4915 then
4916 Error_Msg_N ("type with a dimension system expected", Assoc);
4917 end if;
4918 end;
4919 end if;
4920
4921 <<Leave>>
4922 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4923 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4924 end if;
4925
4926 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4927 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4928 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4929 Style_Check := Saved_Style_Check;
4930
4931 exception
4932 when Instantiation_Error =>
4933 if Parent_Installed then
4934 Remove_Parent;
4935 end if;
4936
4937 if Env_Installed then
4938 Restore_Env;
4939 end if;
4940
4941 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4942 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4943 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4944 Style_Check := Saved_Style_Check;
4945 end Analyze_Package_Instantiation;
4946
4947 --------------------------
4948 -- Inline_Instance_Body --
4949 --------------------------
4950
4951 -- WARNING: This routine manages SPARK regions. Return statements must be
4952 -- replaced by gotos which jump to the end of the routine and restore the
4953 -- SPARK mode.
4954
4955 procedure Inline_Instance_Body
4956 (N : Node_Id;
4957 Gen_Unit : Entity_Id;
4958 Act_Decl : Node_Id)
4959 is
4960 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4961
4962 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4963 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4964 Gen_Comp : constant Entity_Id :=
4965 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4966
4967 Scope_Stack_Depth : constant Pos :=
4968 Scope_Stack.Last - Scope_Stack.First + 1;
4969
4970 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4971 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4972 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4973
4974 Curr_Scope : Entity_Id := Empty;
4975 List : Elist_Id := No_Elist; -- init to avoid warning
4976 N_Instances : Nat := 0;
4977 Num_Inner : Nat := 0;
4978 Num_Scopes : Nat := 0;
4979 Removed : Boolean := False;
4980 S : Entity_Id;
4981 Vis : Boolean;
4982
4983 begin
4984 -- Case of generic unit defined in another unit. We must remove the
4985 -- complete context of the current unit to install that of the generic.
4986
4987 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4988
4989 -- Add some comments for the following two loops ???
4990
4991 S := Current_Scope;
4992 while Present (S) and then S /= Standard_Standard loop
4993 loop
4994 Num_Scopes := Num_Scopes + 1;
4995
4996 Use_Clauses (Num_Scopes) :=
4997 (Scope_Stack.Table
4998 (Scope_Stack.Last - Num_Scopes + 1).
4999 First_Use_Clause);
5000 End_Use_Clauses (Use_Clauses (Num_Scopes));
5001
5002 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5003 or else Scope_Stack.Table
5004 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5005 end loop;
5006
5007 exit when Is_Generic_Instance (S)
5008 and then (In_Package_Body (S)
5009 or else Ekind (S) = E_Procedure
5010 or else Ekind (S) = E_Function);
5011 S := Scope (S);
5012 end loop;
5013
5014 Vis := Is_Immediately_Visible (Gen_Comp);
5015
5016 -- Find and save all enclosing instances
5017
5018 S := Current_Scope;
5019
5020 while Present (S)
5021 and then S /= Standard_Standard
5022 loop
5023 if Is_Generic_Instance (S) then
5024 N_Instances := N_Instances + 1;
5025 Instances (N_Instances) := S;
5026
5027 exit when In_Package_Body (S);
5028 end if;
5029
5030 S := Scope (S);
5031 end loop;
5032
5033 -- Remove context of current compilation unit, unless we are within a
5034 -- nested package instantiation, in which case the context has been
5035 -- removed previously.
5036
5037 -- If current scope is the body of a child unit, remove context of
5038 -- spec as well. If an enclosing scope is an instance body, the
5039 -- context has already been removed, but the entities in the body
5040 -- must be made invisible as well.
5041
5042 S := Current_Scope;
5043 while Present (S) and then S /= Standard_Standard loop
5044 if Is_Generic_Instance (S)
5045 and then (In_Package_Body (S)
5046 or else Ekind (S) in E_Procedure | E_Function)
5047 then
5048 -- We still have to remove the entities of the enclosing
5049 -- instance from direct visibility.
5050
5051 declare
5052 E : Entity_Id;
5053 begin
5054 E := First_Entity (S);
5055 while Present (E) loop
5056 Set_Is_Immediately_Visible (E, False);
5057 Next_Entity (E);
5058 end loop;
5059 end;
5060
5061 exit;
5062 end if;
5063
5064 if S = Curr_Unit
5065 or else (Ekind (Curr_Unit) = E_Package_Body
5066 and then S = Spec_Entity (Curr_Unit))
5067 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5068 and then S = Corresponding_Spec
5069 (Unit_Declaration_Node (Curr_Unit)))
5070 then
5071 Removed := True;
5072
5073 -- Remove entities in current scopes from visibility, so that
5074 -- instance body is compiled in a clean environment.
5075
5076 List := Save_Scope_Stack (Handle_Use => False);
5077
5078 if Is_Child_Unit (S) then
5079
5080 -- Remove child unit from stack, as well as inner scopes.
5081 -- Removing the context of a child unit removes parent units
5082 -- as well.
5083
5084 while Current_Scope /= S loop
5085 Num_Inner := Num_Inner + 1;
5086 Inner_Scopes (Num_Inner) := Current_Scope;
5087 Pop_Scope;
5088 end loop;
5089
5090 Pop_Scope;
5091 Remove_Context (Curr_Comp);
5092 Curr_Scope := S;
5093
5094 else
5095 Remove_Context (Curr_Comp);
5096 end if;
5097
5098 if Ekind (Curr_Unit) = E_Package_Body then
5099 Remove_Context (Library_Unit (Curr_Comp));
5100 end if;
5101 end if;
5102
5103 S := Scope (S);
5104 end loop;
5105
5106 pragma Assert (Num_Inner < Num_Scopes);
5107
5108 Push_Scope (Standard_Standard);
5109 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5110
5111 -- The inlined package body is analyzed with the configuration state
5112 -- of the context prior to the scope manipulations performed above.
5113
5114 -- ??? shouldn't this also use the warning state of the context prior
5115 -- to the scope manipulations?
5116
5117 Instantiate_Package_Body
5118 (Body_Info =>
5119 ((Act_Decl => Act_Decl,
5120 Config_Switches => Config_Attrs,
5121 Current_Sem_Unit => Current_Sem_Unit,
5122 Expander_Status => Expander_Active,
5123 Inst_Node => N,
5124 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5125 Scope_Suppress => Scope_Suppress,
5126 Warnings => Save_Warnings)),
5127 Inlined_Body => True);
5128
5129 Pop_Scope;
5130
5131 -- Restore context
5132
5133 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5134
5135 -- Reset Generic_Instance flag so that use clauses can be installed
5136 -- in the proper order. (See Use_One_Package for effect of enclosing
5137 -- instances on processing of use clauses).
5138
5139 for J in 1 .. N_Instances loop
5140 Set_Is_Generic_Instance (Instances (J), False);
5141 end loop;
5142
5143 if Removed then
5144 Install_Context (Curr_Comp, Chain => False);
5145
5146 if Present (Curr_Scope)
5147 and then Is_Child_Unit (Curr_Scope)
5148 then
5149 Push_Scope (Curr_Scope);
5150 Set_Is_Immediately_Visible (Curr_Scope);
5151
5152 -- Finally, restore inner scopes as well
5153
5154 for J in reverse 1 .. Num_Inner loop
5155 Push_Scope (Inner_Scopes (J));
5156 end loop;
5157 end if;
5158
5159 Restore_Scope_Stack (List, Handle_Use => False);
5160
5161 if Present (Curr_Scope)
5162 and then
5163 (In_Private_Part (Curr_Scope)
5164 or else In_Package_Body (Curr_Scope))
5165 then
5166 -- Install private declaration of ancestor units, which are
5167 -- currently available. Restore_Scope_Stack and Install_Context
5168 -- only install the visible part of parents.
5169
5170 declare
5171 Par : Entity_Id;
5172 begin
5173 Par := Scope (Curr_Scope);
5174 while (Present (Par)) and then Par /= Standard_Standard loop
5175 Install_Private_Declarations (Par);
5176 Par := Scope (Par);
5177 end loop;
5178 end;
5179 end if;
5180 end if;
5181
5182 -- Restore use clauses. For a child unit, use clauses in the parents
5183 -- are restored when installing the context, so only those in inner
5184 -- scopes (and those local to the child unit itself) need to be
5185 -- installed explicitly.
5186
5187 if Is_Child_Unit (Curr_Unit) and then Removed then
5188 for J in reverse 1 .. Num_Inner + 1 loop
5189 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5190 Use_Clauses (J);
5191 Install_Use_Clauses (Use_Clauses (J));
5192 end loop;
5193
5194 else
5195 for J in reverse 1 .. Num_Scopes loop
5196 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5197 Use_Clauses (J);
5198 Install_Use_Clauses (Use_Clauses (J));
5199 end loop;
5200 end if;
5201
5202 -- Restore status of instances. If one of them is a body, make its
5203 -- local entities visible again.
5204
5205 declare
5206 E : Entity_Id;
5207 Inst : Entity_Id;
5208
5209 begin
5210 for J in 1 .. N_Instances loop
5211 Inst := Instances (J);
5212 Set_Is_Generic_Instance (Inst, True);
5213
5214 if In_Package_Body (Inst)
5215 or else Ekind (S) in E_Procedure | E_Function
5216 then
5217 E := First_Entity (Instances (J));
5218 while Present (E) loop
5219 Set_Is_Immediately_Visible (E);
5220 Next_Entity (E);
5221 end loop;
5222 end if;
5223 end loop;
5224 end;
5225
5226 -- If generic unit is in current unit, current context is correct. Note
5227 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5228 -- enclosing scopes were removed.
5229
5230 else
5231 Instantiate_Package_Body
5232 (Body_Info =>
5233 ((Act_Decl => Act_Decl,
5234 Config_Switches => Save_Config_Switches,
5235 Current_Sem_Unit => Current_Sem_Unit,
5236 Expander_Status => Expander_Active,
5237 Inst_Node => N,
5238 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5239 Scope_Suppress => Scope_Suppress,
5240 Warnings => Save_Warnings)),
5241 Inlined_Body => True);
5242 end if;
5243 end Inline_Instance_Body;
5244
5245 -------------------------------------
5246 -- Analyze_Procedure_Instantiation --
5247 -------------------------------------
5248
5249 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5250 begin
5251 Analyze_Subprogram_Instantiation (N, E_Procedure);
5252 end Analyze_Procedure_Instantiation;
5253
5254 -----------------------------------
5255 -- Need_Subprogram_Instance_Body --
5256 -----------------------------------
5257
5258 function Need_Subprogram_Instance_Body
5259 (N : Node_Id;
5260 Subp : Entity_Id) return Boolean
5261 is
5262 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5263 -- Return True if E is an inlined subprogram, an inlined renaming or a
5264 -- subprogram nested in an inlined subprogram. The inlining machinery
5265 -- totally disregards nested subprograms since it considers that they
5266 -- will always be compiled if the parent is (see Inline.Is_Nested).
5267
5268 ------------------------------------
5269 -- Is_Inlined_Or_Child_Of_Inlined --
5270 ------------------------------------
5271
5272 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5273 Scop : Entity_Id;
5274
5275 begin
5276 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5277 return True;
5278 end if;
5279
5280 Scop := Scope (E);
5281 while Scop /= Standard_Standard loop
5282 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5283 return True;
5284 end if;
5285
5286 Scop := Scope (Scop);
5287 end loop;
5288
5289 return False;
5290 end Is_Inlined_Or_Child_Of_Inlined;
5291
5292 begin
5293 -- Must be in the main unit or inlined (or child of inlined)
5294
5295 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5296
5297 -- Must be generating code or analyzing code in GNATprove mode
5298
5299 and then (Operating_Mode = Generate_Code
5300 or else (Operating_Mode = Check_Semantics
5301 and then GNATprove_Mode))
5302
5303 -- The body is needed when generating code (full expansion) and in
5304 -- in GNATprove mode (special expansion) for formal verification of
5305 -- the body itself.
5306
5307 and then (Expander_Active or GNATprove_Mode)
5308
5309 -- No point in inlining if ABE is inevitable
5310
5311 and then not Is_Known_Guaranteed_ABE (N)
5312
5313 -- Or if subprogram is eliminated
5314
5315 and then not Is_Eliminated (Subp)
5316 then
5317 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5318 return True;
5319
5320 -- Here if not inlined, or we ignore the inlining
5321
5322 else
5323 return False;
5324 end if;
5325 end Need_Subprogram_Instance_Body;
5326
5327 --------------------------------------
5328 -- Analyze_Subprogram_Instantiation --
5329 --------------------------------------
5330
5331 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5332 -- must be replaced by gotos which jump to the end of the routine in order
5333 -- to restore the Ghost and SPARK modes.
5334
5335 procedure Analyze_Subprogram_Instantiation
5336 (N : Node_Id;
5337 K : Entity_Kind)
5338 is
5339 Errs : constant Nat := Serious_Errors_Detected;
5340 Gen_Id : constant Node_Id := Name (N);
5341 Inst_Id : constant Entity_Id := Defining_Entity (N);
5342 Anon_Id : constant Entity_Id :=
5343 Make_Defining_Identifier (Sloc (Inst_Id),
5344 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5345 Loc : constant Source_Ptr := Sloc (N);
5346
5347 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5348 Act_Decl : Node_Id;
5349 Act_Spec : Node_Id;
5350 Act_Tree : Node_Id;
5351
5352 Env_Installed : Boolean := False;
5353 Gen_Unit : Entity_Id;
5354 Gen_Decl : Node_Id;
5355 Pack_Id : Entity_Id;
5356 Parent_Installed : Boolean := False;
5357
5358 Renaming_List : List_Id;
5359 -- The list of declarations that link formals and actuals of the
5360 -- instance. These are subtype declarations for formal types, and
5361 -- renaming declarations for other formals. The subprogram declaration
5362 -- for the instance is then appended to the list, and the last item on
5363 -- the list is the renaming declaration for the instance.
5364
5365 procedure Analyze_Instance_And_Renamings;
5366 -- The instance must be analyzed in a context that includes the mappings
5367 -- of generic parameters into actuals. We create a package declaration
5368 -- for this purpose, and a subprogram with an internal name within the
5369 -- package. The subprogram instance is simply an alias for the internal
5370 -- subprogram, declared in the current scope.
5371
5372 procedure Build_Subprogram_Renaming;
5373 -- If the subprogram is recursive, there are occurrences of the name of
5374 -- the generic within the body, which must resolve to the current
5375 -- instance. We add a renaming declaration after the declaration, which
5376 -- is available in the instance body, as well as in the analysis of
5377 -- aspects that appear in the generic. This renaming declaration is
5378 -- inserted after the instance declaration which it renames.
5379
5380 ------------------------------------
5381 -- Analyze_Instance_And_Renamings --
5382 ------------------------------------
5383
5384 procedure Analyze_Instance_And_Renamings is
5385 Def_Ent : constant Entity_Id := Defining_Entity (N);
5386 Pack_Decl : Node_Id;
5387
5388 begin
5389 if Nkind (Parent (N)) = N_Compilation_Unit then
5390
5391 -- For the case of a compilation unit, the container package has
5392 -- the same name as the instantiation, to insure that the binder
5393 -- calls the elaboration procedure with the right name. Copy the
5394 -- entity of the instance, which may have compilation level flags
5395 -- (e.g. Is_Child_Unit) set.
5396
5397 Pack_Id := New_Copy (Def_Ent);
5398
5399 else
5400 -- Otherwise we use the name of the instantiation concatenated
5401 -- with its source position to ensure uniqueness if there are
5402 -- several instantiations with the same name.
5403
5404 Pack_Id :=
5405 Make_Defining_Identifier (Loc,
5406 Chars => New_External_Name
5407 (Related_Id => Chars (Def_Ent),
5408 Suffix => "GP",
5409 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5410 end if;
5411
5412 Pack_Decl :=
5413 Make_Package_Declaration (Loc,
5414 Specification => Make_Package_Specification (Loc,
5415 Defining_Unit_Name => Pack_Id,
5416 Visible_Declarations => Renaming_List,
5417 End_Label => Empty));
5418
5419 Set_Instance_Spec (N, Pack_Decl);
5420 Set_Is_Generic_Instance (Pack_Id);
5421 Set_Debug_Info_Needed (Pack_Id);
5422
5423 -- Case of not a compilation unit
5424
5425 if Nkind (Parent (N)) /= N_Compilation_Unit then
5426 Mark_Rewrite_Insertion (Pack_Decl);
5427 Insert_Before (N, Pack_Decl);
5428 Set_Has_Completion (Pack_Id);
5429
5430 -- Case of an instantiation that is a compilation unit
5431
5432 -- Place declaration on current node so context is complete for
5433 -- analysis (including nested instantiations), and for use in a
5434 -- context_clause (see Analyze_With_Clause).
5435
5436 else
5437 Set_Unit (Parent (N), Pack_Decl);
5438 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5439 end if;
5440
5441 Analyze (Pack_Decl);
5442 Check_Formal_Packages (Pack_Id);
5443
5444 -- Body of the enclosing package is supplied when instantiating the
5445 -- subprogram body, after semantic analysis is completed.
5446
5447 if Nkind (Parent (N)) = N_Compilation_Unit then
5448
5449 -- Remove package itself from visibility, so it does not
5450 -- conflict with subprogram.
5451
5452 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5453
5454 -- Set name and scope of internal subprogram so that the proper
5455 -- external name will be generated. The proper scope is the scope
5456 -- of the wrapper package. We need to generate debugging info for
5457 -- the internal subprogram, so set flag accordingly.
5458
5459 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5460 Set_Scope (Anon_Id, Scope (Pack_Id));
5461
5462 -- Mark wrapper package as referenced, to avoid spurious warnings
5463 -- if the instantiation appears in various with_ clauses of
5464 -- subunits of the main unit.
5465
5466 Set_Referenced (Pack_Id);
5467 end if;
5468
5469 Set_Is_Generic_Instance (Anon_Id);
5470 Set_Debug_Info_Needed (Anon_Id);
5471 Act_Decl_Id := New_Copy (Anon_Id);
5472
5473 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5474 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5475 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5476
5477 -- Subprogram instance comes from source only if generic does
5478
5479 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5480
5481 -- If the instance is a child unit, mark the Id accordingly. Mark
5482 -- the anonymous entity as well, which is the real subprogram and
5483 -- which is used when the instance appears in a context clause.
5484 -- Similarly, propagate the Is_Eliminated flag to handle properly
5485 -- nested eliminated subprograms.
5486
5487 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5488 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5489 New_Overloaded_Entity (Act_Decl_Id);
5490 Check_Eliminated (Act_Decl_Id);
5491 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5492
5493 if Nkind (Parent (N)) = N_Compilation_Unit then
5494
5495 -- In compilation unit case, kill elaboration checks on the
5496 -- instantiation, since they are never needed - the body is
5497 -- instantiated at the same point as the spec.
5498
5499 if Legacy_Elaboration_Checks then
5500 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5501 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5502 end if;
5503
5504 Set_Is_Compilation_Unit (Anon_Id);
5505 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5506 end if;
5507
5508 -- The instance is not a freezing point for the new subprogram.
5509 -- The anonymous subprogram may have a freeze node, created for
5510 -- some delayed aspects. This freeze node must not be inherited
5511 -- by the visible subprogram entity.
5512
5513 Set_Is_Frozen (Act_Decl_Id, False);
5514 Set_Freeze_Node (Act_Decl_Id, Empty);
5515
5516 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5517 Valid_Operator_Definition (Act_Decl_Id);
5518 end if;
5519
5520 Set_Alias (Act_Decl_Id, Anon_Id);
5521 Set_Has_Completion (Act_Decl_Id);
5522 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5523
5524 if Nkind (Parent (N)) = N_Compilation_Unit then
5525 Set_Body_Required (Parent (N), False);
5526 end if;
5527 end Analyze_Instance_And_Renamings;
5528
5529 -------------------------------
5530 -- Build_Subprogram_Renaming --
5531 -------------------------------
5532
5533 procedure Build_Subprogram_Renaming is
5534 Renaming_Decl : Node_Id;
5535 Unit_Renaming : Node_Id;
5536
5537 begin
5538 Unit_Renaming :=
5539 Make_Subprogram_Renaming_Declaration (Loc,
5540 Specification =>
5541 Copy_Generic_Node
5542 (Specification (Original_Node (Gen_Decl)),
5543 Empty,
5544 Instantiating => True),
5545 Name => New_Occurrence_Of (Anon_Id, Loc));
5546
5547 -- The generic may be a child unit. The renaming needs an identifier
5548 -- with the proper name.
5549
5550 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5551 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5552
5553 -- If there is a formal subprogram with the same name as the unit
5554 -- itself, do not add this renaming declaration, to prevent
5555 -- ambiguities when there is a call with that name in the body.
5556 -- This is a partial and ugly fix for one ACATS test. ???
5557
5558 Renaming_Decl := First (Renaming_List);
5559 while Present (Renaming_Decl) loop
5560 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5561 and then
5562 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5563 then
5564 exit;
5565 end if;
5566
5567 Next (Renaming_Decl);
5568 end loop;
5569
5570 if No (Renaming_Decl) then
5571 Append (Unit_Renaming, Renaming_List);
5572 end if;
5573 end Build_Subprogram_Renaming;
5574
5575 -- Local variables
5576
5577 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5578 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5579 Saved_ISMP : constant Boolean :=
5580 Ignore_SPARK_Mode_Pragmas_In_Instance;
5581 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5582 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5583 -- Save the Ghost and SPARK mode-related data to restore on exit
5584
5585 Vis_Prims_List : Elist_Id := No_Elist;
5586 -- List of primitives made temporarily visible in the instantiation
5587 -- to match the visibility of the formal type
5588
5589 -- Start of processing for Analyze_Subprogram_Instantiation
5590
5591 begin
5592 -- Preserve relevant elaboration-related attributes of the context which
5593 -- are no longer available or very expensive to recompute once analysis,
5594 -- resolution, and expansion are over.
5595
5596 Mark_Elaboration_Attributes
5597 (N_Id => N,
5598 Checks => True,
5599 Level => True,
5600 Modes => True,
5601 Warnings => True);
5602
5603 -- Very first thing: check for special Text_IO unit in case we are
5604 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5605 -- such an instantiation is bogus (these are packages, not subprograms),
5606 -- but we get a better error message if we do this.
5607
5608 Check_Text_IO_Special_Unit (Gen_Id);
5609
5610 -- Make node global for error reporting
5611
5612 Instantiation_Node := N;
5613
5614 -- For package instantiations we turn off style checks, because they
5615 -- will have been emitted in the generic. For subprogram instantiations
5616 -- we want to apply at least the check on overriding indicators so we
5617 -- do not modify the style check status.
5618
5619 -- The renaming declarations for the actuals do not come from source and
5620 -- will not generate spurious warnings.
5621
5622 Preanalyze_Actuals (N);
5623
5624 Init_Env;
5625 Env_Installed := True;
5626 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5627 Gen_Unit := Entity (Gen_Id);
5628
5629 -- A subprogram instantiation is Ghost when it is subject to pragma
5630 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5631 -- that any nodes generated during analysis and expansion are marked as
5632 -- Ghost.
5633
5634 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5635
5636 Generate_Reference (Gen_Unit, Gen_Id);
5637
5638 if Nkind (Gen_Id) = N_Identifier
5639 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5640 then
5641 Error_Msg_NE
5642 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5643 end if;
5644
5645 if Etype (Gen_Unit) = Any_Type then
5646 Restore_Env;
5647 goto Leave;
5648 end if;
5649
5650 -- Verify that it is a generic subprogram of the right kind, and that
5651 -- it does not lead to a circular instantiation.
5652
5653 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5654 Error_Msg_NE
5655 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5656
5657 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5658 Error_Msg_NE
5659 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5660
5661 elsif In_Open_Scopes (Gen_Unit) then
5662 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5663
5664 else
5665 Set_Ekind (Inst_Id, K);
5666 Set_Scope (Inst_Id, Current_Scope);
5667
5668 Set_Entity (Gen_Id, Gen_Unit);
5669 Set_Is_Instantiated (Gen_Unit);
5670
5671 if In_Extended_Main_Source_Unit (N) then
5672 Generate_Reference (Gen_Unit, N);
5673 end if;
5674
5675 -- If renaming, get original unit
5676
5677 if Present (Renamed_Object (Gen_Unit))
5678 and then Is_Generic_Subprogram (Renamed_Object (Gen_Unit))
5679 then
5680 Gen_Unit := Renamed_Object (Gen_Unit);
5681 Set_Is_Instantiated (Gen_Unit);
5682 Generate_Reference (Gen_Unit, N);
5683 end if;
5684
5685 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5686 Error_Msg_Node_2 := Current_Scope;
5687 Error_Msg_NE
5688 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5689 Circularity_Detected := True;
5690 Restore_Hidden_Primitives (Vis_Prims_List);
5691 goto Leave;
5692 end if;
5693
5694 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5695
5696 -- Initialize renamings map, for error checking
5697
5698 Generic_Renamings.Set_Last (0);
5699 Generic_Renamings_HTable.Reset;
5700
5701 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5702
5703 -- Copy original generic tree, to produce text for instantiation
5704
5705 Act_Tree :=
5706 Copy_Generic_Node
5707 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5708
5709 -- Inherit overriding indicator from instance node
5710
5711 Act_Spec := Specification (Act_Tree);
5712 Set_Must_Override (Act_Spec, Must_Override (N));
5713 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5714
5715 Renaming_List :=
5716 Analyze_Associations
5717 (I_Node => N,
5718 Formals => Generic_Formal_Declarations (Act_Tree),
5719 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5720
5721 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5722
5723 -- The subprogram itself cannot contain a nested instance, so the
5724 -- current parent is left empty.
5725
5726 Set_Instance_Env (Gen_Unit, Empty);
5727
5728 -- Build the subprogram declaration, which does not appear in the
5729 -- generic template, and give it a sloc consistent with that of the
5730 -- template.
5731
5732 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5733 Set_Generic_Parent (Act_Spec, Gen_Unit);
5734 Act_Decl :=
5735 Make_Subprogram_Declaration (Sloc (Act_Spec),
5736 Specification => Act_Spec);
5737
5738 -- The aspects have been copied previously, but they have to be
5739 -- linked explicitly to the new subprogram declaration. Explicit
5740 -- pre/postconditions on the instance are analyzed below, in a
5741 -- separate step.
5742
5743 Move_Aspects (Act_Tree, To => Act_Decl);
5744 Set_Categorization_From_Pragmas (Act_Decl);
5745
5746 if Parent_Installed then
5747 Hide_Current_Scope;
5748 end if;
5749
5750 Append (Act_Decl, Renaming_List);
5751
5752 -- Contract-related source pragmas that follow a generic subprogram
5753 -- must be instantiated explicitly because they are not part of the
5754 -- subprogram template.
5755
5756 Instantiate_Subprogram_Contract
5757 (Original_Node (Gen_Decl), Renaming_List);
5758
5759 Build_Subprogram_Renaming;
5760
5761 -- If the context of the instance is subject to SPARK_Mode "off" or
5762 -- the annotation is altogether missing, set the global flag which
5763 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5764 -- the instance. This should be done prior to analyzing the instance.
5765
5766 if SPARK_Mode /= On then
5767 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5768 end if;
5769
5770 -- If the context of an instance is not subject to SPARK_Mode "off",
5771 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5772 -- the latter should be the one applicable to the instance.
5773
5774 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5775 and then Saved_SM /= Off
5776 and then Present (SPARK_Pragma (Gen_Unit))
5777 then
5778 Set_SPARK_Mode (Gen_Unit);
5779 end if;
5780
5781 Analyze_Instance_And_Renamings;
5782
5783 -- Restore SPARK_Mode from the context after analysis of the package
5784 -- declaration, so that the SPARK_Mode on the generic spec does not
5785 -- apply to the pending instance for the instance body.
5786
5787 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5788 and then Saved_SM /= Off
5789 and then Present (SPARK_Pragma (Gen_Unit))
5790 then
5791 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5792 end if;
5793
5794 -- If the generic is marked Import (Intrinsic), then so is the
5795 -- instance. This indicates that there is no body to instantiate. If
5796 -- generic is marked inline, so it the instance, and the anonymous
5797 -- subprogram it renames. If inlined, or else if inlining is enabled
5798 -- for the compilation, we generate the instance body even if it is
5799 -- not within the main unit.
5800
5801 if Is_Intrinsic_Subprogram (Gen_Unit) then
5802 Set_Is_Intrinsic_Subprogram (Anon_Id);
5803 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5804
5805 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5806 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5807 end if;
5808 end if;
5809
5810 -- Inherit convention from generic unit. Intrinsic convention, as for
5811 -- an instance of unchecked conversion, is not inherited because an
5812 -- explicit Ada instance has been created.
5813
5814 if Has_Convention_Pragma (Gen_Unit)
5815 and then Convention (Gen_Unit) /= Convention_Intrinsic
5816 then
5817 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5818 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5819 end if;
5820
5821 Generate_Definition (Act_Decl_Id);
5822
5823 -- Inherit all inlining-related flags which apply to the generic in
5824 -- the subprogram and its declaration.
5825
5826 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5827 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5828
5829 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5830 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5831
5832 Set_Has_Pragma_Inline_Always
5833 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5834 Set_Has_Pragma_Inline_Always
5835 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5836
5837 Set_Has_Pragma_No_Inline
5838 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5839 Set_Has_Pragma_No_Inline
5840 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5841
5842 -- Propagate No_Return if pragma applied to generic unit. This must
5843 -- be done explicitly because pragma does not appear in generic
5844 -- declaration (unlike the aspect case).
5845
5846 if No_Return (Gen_Unit) then
5847 Set_No_Return (Act_Decl_Id);
5848 Set_No_Return (Anon_Id);
5849 end if;
5850
5851 -- Mark both the instance spec and the anonymous package in case the
5852 -- body is instantiated at a later pass. This preserves the original
5853 -- context in effect for the body.
5854
5855 if SPARK_Mode /= On then
5856 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5857 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5858 end if;
5859
5860 if Legacy_Elaboration_Checks
5861 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5862 then
5863 Check_Elab_Instantiation (N);
5864 end if;
5865
5866 -- Save the scenario for later examination by the ABE Processing
5867 -- phase.
5868
5869 Record_Elaboration_Scenario (N);
5870
5871 -- The instantiation results in a guaranteed ABE. Create a completing
5872 -- body for the subprogram declaration because the real body will not
5873 -- be instantiated.
5874
5875 if Is_Known_Guaranteed_ABE (N) then
5876 Provide_Completing_Bodies (Instance_Spec (N));
5877 end if;
5878
5879 if Is_Dispatching_Operation (Act_Decl_Id)
5880 and then Ada_Version >= Ada_2005
5881 then
5882 declare
5883 Formal : Entity_Id;
5884
5885 begin
5886 Formal := First_Formal (Act_Decl_Id);
5887 while Present (Formal) loop
5888 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5889 and then Is_Controlling_Formal (Formal)
5890 and then not Can_Never_Be_Null (Formal)
5891 then
5892 Error_Msg_NE
5893 ("access parameter& is controlling,", N, Formal);
5894 Error_Msg_NE
5895 ("\corresponding parameter of & must be explicitly "
5896 & "null-excluding", N, Gen_Id);
5897 end if;
5898
5899 Next_Formal (Formal);
5900 end loop;
5901 end;
5902 end if;
5903
5904 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5905
5906 Validate_Categorization_Dependency (N, Act_Decl_Id);
5907
5908 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5909 Inherit_Context (Gen_Decl, N);
5910
5911 Restore_Private_Views (Pack_Id, False);
5912
5913 -- If the context requires a full instantiation, mark node for
5914 -- subsequent construction of the body.
5915
5916 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5917 Check_Forward_Instantiation (Gen_Decl);
5918
5919 -- The wrapper package is always delayed, because it does not
5920 -- constitute a freeze point, but to insure that the freeze node
5921 -- is placed properly, it is created directly when instantiating
5922 -- the body (otherwise the freeze node might appear to early for
5923 -- nested instantiations).
5924
5925 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5926 Rewrite (N, Unit (Parent (N)));
5927 Set_Unit (Parent (N), N);
5928 end if;
5929
5930 -- Replace instance node for library-level instantiations of
5931 -- intrinsic subprograms.
5932
5933 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5934 Rewrite (N, Unit (Parent (N)));
5935 Set_Unit (Parent (N), N);
5936 end if;
5937
5938 if Parent_Installed then
5939 Remove_Parent;
5940 end if;
5941
5942 Restore_Hidden_Primitives (Vis_Prims_List);
5943 Restore_Env;
5944 Env_Installed := False;
5945 Generic_Renamings.Set_Last (0);
5946 Generic_Renamings_HTable.Reset;
5947 end if;
5948
5949 <<Leave>>
5950 -- Analyze aspects in declaration if no errors appear in the instance.
5951
5952 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5953 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5954 end if;
5955
5956 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5957 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5958 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5959
5960 exception
5961 when Instantiation_Error =>
5962 if Parent_Installed then
5963 Remove_Parent;
5964 end if;
5965
5966 if Env_Installed then
5967 Restore_Env;
5968 end if;
5969
5970 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5971 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5972 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5973 end Analyze_Subprogram_Instantiation;
5974
5975 -------------------------
5976 -- Get_Associated_Node --
5977 -------------------------
5978
5979 function Get_Associated_Node (N : Node_Id) return Node_Id is
5980 Assoc : Node_Id;
5981
5982 begin
5983 Assoc := Associated_Node (N);
5984
5985 if Nkind (Assoc) /= Nkind (N) then
5986 return Assoc;
5987
5988 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
5989 return Assoc;
5990
5991 else
5992 -- If the node is part of an inner generic, it may itself have been
5993 -- remapped into a further generic copy. Associated_Node is otherwise
5994 -- used for the entity of the node, and will be of a different node
5995 -- kind, or else N has been rewritten as a literal or function call.
5996
5997 while Present (Associated_Node (Assoc))
5998 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5999 loop
6000 Assoc := Associated_Node (Assoc);
6001 end loop;
6002
6003 -- Follow an additional link in case the final node was rewritten.
6004 -- This can only happen with nested generic units.
6005
6006 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6007 and then Present (Associated_Node (Assoc))
6008 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6009 | N_Explicit_Dereference
6010 | N_Integer_Literal
6011 | N_Real_Literal
6012 | N_String_Literal
6013 then
6014 Assoc := Associated_Node (Assoc);
6015 end if;
6016
6017 -- An additional special case: an unconstrained type in an object
6018 -- declaration may have been rewritten as a local subtype constrained
6019 -- by the expression in the declaration. We need to recover the
6020 -- original entity, which may be global.
6021
6022 if Present (Original_Node (Assoc))
6023 and then Nkind (Parent (N)) = N_Object_Declaration
6024 then
6025 Assoc := Original_Node (Assoc);
6026 end if;
6027
6028 return Assoc;
6029 end if;
6030 end Get_Associated_Node;
6031
6032 ----------------------------
6033 -- Build_Function_Wrapper --
6034 ----------------------------
6035
6036 function Build_Function_Wrapper
6037 (Formal_Subp : Entity_Id;
6038 Actual_Subp : Entity_Id) return Node_Id
6039 is
6040 Loc : constant Source_Ptr := Sloc (Current_Scope);
6041 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6042 Actuals : List_Id;
6043 Decl : Node_Id;
6044 Func_Name : Node_Id;
6045 Func : Entity_Id;
6046 Parm_Type : Node_Id;
6047 Profile : List_Id := New_List;
6048 Spec : Node_Id;
6049 Act_F : Entity_Id;
6050 Form_F : Entity_Id;
6051 New_F : Entity_Id;
6052
6053 begin
6054 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
6055
6056 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6057 Set_Ekind (Func, E_Function);
6058 Set_Is_Generic_Actual_Subprogram (Func);
6059
6060 Actuals := New_List;
6061 Profile := New_List;
6062
6063 Act_F := First_Formal (Actual_Subp);
6064 Form_F := First_Formal (Formal_Subp);
6065 while Present (Form_F) loop
6066
6067 -- Create new formal for profile of wrapper, and add a reference
6068 -- to it in the list of actuals for the enclosing call. The name
6069 -- must be that of the formal in the formal subprogram, because
6070 -- calls to it in the generic body may use named associations.
6071
6072 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6073
6074 Parm_Type :=
6075 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
6076
6077 Append_To (Profile,
6078 Make_Parameter_Specification (Loc,
6079 Defining_Identifier => New_F,
6080 Parameter_Type => Parm_Type));
6081
6082 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
6083 Next_Formal (Form_F);
6084
6085 if Present (Act_F) then
6086 Next_Formal (Act_F);
6087 end if;
6088 end loop;
6089
6090 Spec :=
6091 Make_Function_Specification (Loc,
6092 Defining_Unit_Name => Func,
6093 Parameter_Specifications => Profile,
6094 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6095
6096 Decl :=
6097 Make_Expression_Function (Loc,
6098 Specification => Spec,
6099 Expression =>
6100 Make_Function_Call (Loc,
6101 Name => Func_Name,
6102 Parameter_Associations => Actuals));
6103
6104 return Decl;
6105 end Build_Function_Wrapper;
6106
6107 ----------------------------
6108 -- Build_Operator_Wrapper --
6109 ----------------------------
6110
6111 function Build_Operator_Wrapper
6112 (Formal_Subp : Entity_Id;
6113 Actual_Subp : Entity_Id) return Node_Id
6114 is
6115 Loc : constant Source_Ptr := Sloc (Current_Scope);
6116 Ret_Type : constant Entity_Id :=
6117 Get_Instance_Of (Etype (Formal_Subp));
6118 Op_Type : constant Entity_Id :=
6119 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6120 Is_Binary : constant Boolean :=
6121 Present (Next_Formal (First_Formal (Formal_Subp)));
6122
6123 Decl : Node_Id;
6124 Expr : Node_Id := Empty;
6125 F1, F2 : Entity_Id;
6126 Func : Entity_Id;
6127 Op_Name : Name_Id;
6128 Spec : Node_Id;
6129 L, R : Node_Id;
6130
6131 begin
6132 Op_Name := Chars (Actual_Subp);
6133
6134 -- Create entities for wrapper function and its formals
6135
6136 F1 := Make_Temporary (Loc, 'A');
6137 F2 := Make_Temporary (Loc, 'B');
6138 L := New_Occurrence_Of (F1, Loc);
6139 R := New_Occurrence_Of (F2, Loc);
6140
6141 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6142 Set_Ekind (Func, E_Function);
6143 Set_Is_Generic_Actual_Subprogram (Func);
6144
6145 Spec :=
6146 Make_Function_Specification (Loc,
6147 Defining_Unit_Name => Func,
6148 Parameter_Specifications => New_List (
6149 Make_Parameter_Specification (Loc,
6150 Defining_Identifier => F1,
6151 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6152 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6153
6154 if Is_Binary then
6155 Append_To (Parameter_Specifications (Spec),
6156 Make_Parameter_Specification (Loc,
6157 Defining_Identifier => F2,
6158 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6159 end if;
6160
6161 -- Build expression as a function call, or as an operator node
6162 -- that corresponds to the name of the actual, starting with
6163 -- binary operators.
6164
6165 if Op_Name not in Any_Operator_Name then
6166 Expr :=
6167 Make_Function_Call (Loc,
6168 Name =>
6169 New_Occurrence_Of (Actual_Subp, Loc),
6170 Parameter_Associations => New_List (L));
6171
6172 if Is_Binary then
6173 Append_To (Parameter_Associations (Expr), R);
6174 end if;
6175
6176 -- Binary operators
6177
6178 elsif Is_Binary then
6179 if Op_Name = Name_Op_And then
6180 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6181 elsif Op_Name = Name_Op_Or then
6182 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6183 elsif Op_Name = Name_Op_Xor then
6184 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6185 elsif Op_Name = Name_Op_Eq then
6186 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6187 elsif Op_Name = Name_Op_Ne then
6188 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6189 elsif Op_Name = Name_Op_Le then
6190 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6191 elsif Op_Name = Name_Op_Gt then
6192 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6193 elsif Op_Name = Name_Op_Ge then
6194 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6195 elsif Op_Name = Name_Op_Lt then
6196 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6197 elsif Op_Name = Name_Op_Add then
6198 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6199 elsif Op_Name = Name_Op_Subtract then
6200 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6201 elsif Op_Name = Name_Op_Concat then
6202 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6203 elsif Op_Name = Name_Op_Multiply then
6204 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6205 elsif Op_Name = Name_Op_Divide then
6206 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6207 elsif Op_Name = Name_Op_Mod then
6208 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6209 elsif Op_Name = Name_Op_Rem then
6210 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6211 elsif Op_Name = Name_Op_Expon then
6212 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6213 end if;
6214
6215 -- Unary operators
6216
6217 else
6218 if Op_Name = Name_Op_Add then
6219 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6220 elsif Op_Name = Name_Op_Subtract then
6221 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6222 elsif Op_Name = Name_Op_Abs then
6223 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6224 elsif Op_Name = Name_Op_Not then
6225 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6226 end if;
6227 end if;
6228
6229 Decl :=
6230 Make_Expression_Function (Loc,
6231 Specification => Spec,
6232 Expression => Expr);
6233
6234 return Decl;
6235 end Build_Operator_Wrapper;
6236
6237 -----------------------------------
6238 -- Build_Subprogram_Decl_Wrapper --
6239 -----------------------------------
6240
6241 function Build_Subprogram_Decl_Wrapper
6242 (Formal_Subp : Entity_Id) return Node_Id
6243 is
6244 Loc : constant Source_Ptr := Sloc (Current_Scope);
6245 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6246 Decl : Node_Id;
6247 Subp : Entity_Id;
6248 Parm_Spec : Node_Id;
6249 Profile : List_Id := New_List;
6250 Spec : Node_Id;
6251 Form_F : Entity_Id;
6252 New_F : Entity_Id;
6253
6254 begin
6255
6256 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6257 Set_Ekind (Subp, Ekind (Formal_Subp));
6258 Set_Is_Generic_Actual_Subprogram (Subp);
6259
6260 Profile := Parameter_Specifications (
6261 New_Copy_Tree
6262 (Specification (Unit_Declaration_Node (Formal_Subp))));
6263
6264 Form_F := First_Formal (Formal_Subp);
6265 Parm_Spec := First (Profile);
6266
6267 -- Create new entities for the formals. Reset entities so that
6268 -- parameter types are properly resolved when wrapper declaration
6269 -- is analyzed.
6270
6271 while Present (Parm_Spec) loop
6272 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6273 Set_Defining_Identifier (Parm_Spec, New_F);
6274 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6275 Next (Parm_Spec);
6276 Next_Formal (Form_F);
6277 end loop;
6278
6279 if Ret_Type = Standard_Void_Type then
6280 Spec :=
6281 Make_Procedure_Specification (Loc,
6282 Defining_Unit_Name => Subp,
6283 Parameter_Specifications => Profile);
6284 else
6285 Spec :=
6286 Make_Function_Specification (Loc,
6287 Defining_Unit_Name => Subp,
6288 Parameter_Specifications => Profile,
6289 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6290 end if;
6291
6292 Decl :=
6293 Make_Subprogram_Declaration (Loc, Specification => Spec);
6294
6295 return Decl;
6296 end Build_Subprogram_Decl_Wrapper;
6297
6298 -----------------------------------
6299 -- Build_Subprogram_Body_Wrapper --
6300 -----------------------------------
6301
6302 function Build_Subprogram_Body_Wrapper
6303 (Formal_Subp : Entity_Id;
6304 Actual_Name : Node_Id) return Node_Id
6305 is
6306 Loc : constant Source_Ptr := Sloc (Current_Scope);
6307 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6308 Spec_Node : constant Node_Id :=
6309 Specification
6310 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6311 Act : Node_Id;
6312 Actuals : List_Id;
6313 Body_Node : Node_Id;
6314 Stmt : Node_Id;
6315 begin
6316 Actuals := New_List;
6317 Act := First (Parameter_Specifications (Spec_Node));
6318
6319 while Present (Act) loop
6320 Append_To (Actuals,
6321 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6322 Next (Act);
6323 end loop;
6324
6325 if Ret_Type = Standard_Void_Type then
6326 Stmt := Make_Procedure_Call_Statement (Loc,
6327 Name => Actual_Name,
6328 Parameter_Associations => Actuals);
6329
6330 else
6331 Stmt := Make_Simple_Return_Statement (Loc,
6332 Expression =>
6333 Make_Function_Call (Loc,
6334 Name => Actual_Name,
6335 Parameter_Associations => Actuals));
6336 end if;
6337
6338 Body_Node := Make_Subprogram_Body (Loc,
6339 Specification => Spec_Node,
6340 Declarations => New_List,
6341 Handled_Statement_Sequence =>
6342 Make_Handled_Sequence_Of_Statements (Loc,
6343 Statements => New_List (Stmt)));
6344
6345 return Body_Node;
6346 end Build_Subprogram_Body_Wrapper;
6347
6348 -------------------------------------------
6349 -- Build_Instance_Compilation_Unit_Nodes --
6350 -------------------------------------------
6351
6352 procedure Build_Instance_Compilation_Unit_Nodes
6353 (N : Node_Id;
6354 Act_Body : Node_Id;
6355 Act_Decl : Node_Id)
6356 is
6357 Decl_Cunit : Node_Id;
6358 Body_Cunit : Node_Id;
6359 Citem : Node_Id;
6360 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6361 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6362
6363 begin
6364 -- A new compilation unit node is built for the instance declaration
6365
6366 Decl_Cunit :=
6367 Make_Compilation_Unit (Sloc (N),
6368 Context_Items => Empty_List,
6369 Unit => Act_Decl,
6370 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6371
6372 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6373
6374 -- The new compilation unit is linked to its body, but both share the
6375 -- same file, so we do not set Body_Required on the new unit so as not
6376 -- to create a spurious dependency on a non-existent body in the ali.
6377 -- This simplifies CodePeer unit traversal.
6378
6379 -- We use the original instantiation compilation unit as the resulting
6380 -- compilation unit of the instance, since this is the main unit.
6381
6382 Rewrite (N, Act_Body);
6383
6384 -- Propagate the aspect specifications from the package body template to
6385 -- the instantiated version of the package body.
6386
6387 if Has_Aspects (Act_Body) then
6388 Set_Aspect_Specifications
6389 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6390 end if;
6391
6392 Body_Cunit := Parent (N);
6393
6394 -- The two compilation unit nodes are linked by the Library_Unit field
6395
6396 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6397 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6398
6399 -- Preserve the private nature of the package if needed
6400
6401 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6402
6403 -- If the instance is not the main unit, its context, categorization
6404 -- and elaboration entity are not relevant to the compilation.
6405
6406 if Body_Cunit /= Cunit (Main_Unit) then
6407 Make_Instance_Unit (Body_Cunit, In_Main => False);
6408 return;
6409 end if;
6410
6411 -- The context clause items on the instantiation, which are now attached
6412 -- to the body compilation unit (since the body overwrote the original
6413 -- instantiation node), semantically belong on the spec, so copy them
6414 -- there. It's harmless to leave them on the body as well. In fact one
6415 -- could argue that they belong in both places.
6416
6417 Citem := First (Context_Items (Body_Cunit));
6418 while Present (Citem) loop
6419 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6420 Next (Citem);
6421 end loop;
6422
6423 -- Propagate categorization flags on packages, so that they appear in
6424 -- the ali file for the spec of the unit.
6425
6426 if Ekind (New_Main) = E_Package then
6427 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6428 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6429 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6430 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6431 Set_Is_Remote_Call_Interface
6432 (Old_Main, Is_Remote_Call_Interface (New_Main));
6433 end if;
6434
6435 -- Make entry in Units table, so that binder can generate call to
6436 -- elaboration procedure for body, if any.
6437
6438 Make_Instance_Unit (Body_Cunit, In_Main => True);
6439 Main_Unit_Entity := New_Main;
6440 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6441
6442 -- Build elaboration entity, since the instance may certainly generate
6443 -- elaboration code requiring a flag for protection.
6444
6445 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6446 end Build_Instance_Compilation_Unit_Nodes;
6447
6448 -----------------------------
6449 -- Check_Access_Definition --
6450 -----------------------------
6451
6452 procedure Check_Access_Definition (N : Node_Id) is
6453 begin
6454 pragma Assert
6455 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6456 null;
6457 end Check_Access_Definition;
6458
6459 -----------------------------------
6460 -- Check_Formal_Package_Instance --
6461 -----------------------------------
6462
6463 -- If the formal has specific parameters, they must match those of the
6464 -- actual. Both of them are instances, and the renaming declarations for
6465 -- their formal parameters appear in the same order in both. The analyzed
6466 -- formal has been analyzed in the context of the current instance.
6467
6468 procedure Check_Formal_Package_Instance
6469 (Formal_Pack : Entity_Id;
6470 Actual_Pack : Entity_Id)
6471 is
6472 E1 : Entity_Id := First_Entity (Actual_Pack);
6473 E2 : Entity_Id := First_Entity (Formal_Pack);
6474 Prev_E1 : Entity_Id;
6475
6476 Expr1 : Node_Id;
6477 Expr2 : Node_Id;
6478
6479 procedure Check_Mismatch (B : Boolean);
6480 -- Common error routine for mismatch between the parameters of the
6481 -- actual instance and those of the formal package.
6482
6483 function Is_Defaulted (Param : Entity_Id) return Boolean;
6484 -- If the formal package has partly box-initialized formals, skip
6485 -- conformance check for these formals. Previously the code assumed
6486 -- that box initialization for a formal package applied to all its
6487 -- formal parameters.
6488
6489 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6490 -- The formal may come from a nested formal package, and the actual may
6491 -- have been constant-folded. To determine whether the two denote the
6492 -- same entity we may have to traverse several definitions to recover
6493 -- the ultimate entity that they refer to.
6494
6495 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6496 -- The formal and the actual must be identical, but if both are
6497 -- given by attributes they end up renaming different generated bodies,
6498 -- and we must verify that the attributes themselves match.
6499
6500 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6501 -- Similarly, if the formal comes from a nested formal package, the
6502 -- actual may designate the formal through multiple renamings, which
6503 -- have to be followed to determine the original variable in question.
6504
6505 --------------------
6506 -- Check_Mismatch --
6507 --------------------
6508
6509 procedure Check_Mismatch (B : Boolean) is
6510 -- A Formal_Type_Declaration for a derived private type is rewritten
6511 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6512 -- which is why we examine the original node.
6513
6514 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6515
6516 begin
6517 if Kind = N_Formal_Type_Declaration then
6518 return;
6519
6520 elsif Kind in N_Formal_Object_Declaration
6521 | N_Formal_Package_Declaration
6522 | N_Formal_Subprogram_Declaration
6523 then
6524 null;
6525
6526 -- Ada 2012: If both formal and actual are incomplete types they
6527 -- are conformant.
6528
6529 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6530 null;
6531
6532 elsif B then
6533 Error_Msg_NE
6534 ("actual for & in actual instance does not match formal",
6535 Parent (Actual_Pack), E1);
6536 end if;
6537 end Check_Mismatch;
6538
6539 ------------------
6540 -- Is_Defaulted --
6541 ------------------
6542
6543 function Is_Defaulted (Param : Entity_Id) return Boolean is
6544 Assoc : Node_Id;
6545
6546 begin
6547 Assoc :=
6548 First (Generic_Associations (Parent
6549 (Associated_Formal_Package (Actual_Pack))));
6550
6551 while Present (Assoc) loop
6552 if Nkind (Assoc) = N_Others_Choice then
6553 return True;
6554
6555 elsif Nkind (Assoc) = N_Generic_Association
6556 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6557 then
6558 return Box_Present (Assoc);
6559 end if;
6560
6561 Next (Assoc);
6562 end loop;
6563
6564 return False;
6565 end Is_Defaulted;
6566
6567 --------------------------------
6568 -- Same_Instantiated_Constant --
6569 --------------------------------
6570
6571 function Same_Instantiated_Constant
6572 (E1, E2 : Entity_Id) return Boolean
6573 is
6574 Ent : Entity_Id;
6575
6576 begin
6577 Ent := E2;
6578 while Present (Ent) loop
6579 if E1 = Ent then
6580 return True;
6581
6582 elsif Ekind (Ent) /= E_Constant then
6583 return False;
6584
6585 elsif Is_Entity_Name (Constant_Value (Ent)) then
6586 if Entity (Constant_Value (Ent)) = E1 then
6587 return True;
6588 else
6589 Ent := Entity (Constant_Value (Ent));
6590 end if;
6591
6592 -- The actual may be a constant that has been folded. Recover
6593 -- original name.
6594
6595 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6596 Ent := Entity (Original_Node (Constant_Value (Ent)));
6597
6598 else
6599 return False;
6600 end if;
6601 end loop;
6602
6603 return False;
6604 end Same_Instantiated_Constant;
6605
6606 --------------------------------
6607 -- Same_Instantiated_Function --
6608 --------------------------------
6609
6610 function Same_Instantiated_Function
6611 (E1, E2 : Entity_Id) return Boolean
6612 is
6613 U1, U2 : Node_Id;
6614 begin
6615 if Alias (E1) = Alias (E2) then
6616 return True;
6617
6618 elsif Present (Alias (E2)) then
6619 U1 := Original_Node (Unit_Declaration_Node (E1));
6620 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6621
6622 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6623 and then Nkind (Name (U1)) = N_Attribute_Reference
6624
6625 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6626 and then Nkind (Name (U2)) = N_Attribute_Reference
6627
6628 and then
6629 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6630 else
6631 return False;
6632 end if;
6633 end Same_Instantiated_Function;
6634
6635 --------------------------------
6636 -- Same_Instantiated_Variable --
6637 --------------------------------
6638
6639 function Same_Instantiated_Variable
6640 (E1, E2 : Entity_Id) return Boolean
6641 is
6642 function Original_Entity (E : Entity_Id) return Entity_Id;
6643 -- Follow chain of renamings to the ultimate ancestor
6644
6645 ---------------------
6646 -- Original_Entity --
6647 ---------------------
6648
6649 function Original_Entity (E : Entity_Id) return Entity_Id is
6650 Orig : Entity_Id;
6651
6652 begin
6653 Orig := E;
6654 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6655 and then Present (Renamed_Object (Orig))
6656 and then Is_Entity_Name (Renamed_Object (Orig))
6657 loop
6658 Orig := Entity (Renamed_Object (Orig));
6659 end loop;
6660
6661 return Orig;
6662 end Original_Entity;
6663
6664 -- Start of processing for Same_Instantiated_Variable
6665
6666 begin
6667 return Ekind (E1) = Ekind (E2)
6668 and then Original_Entity (E1) = Original_Entity (E2);
6669 end Same_Instantiated_Variable;
6670
6671 -- Start of processing for Check_Formal_Package_Instance
6672
6673 begin
6674 Prev_E1 := E1;
6675 while Present (E1) and then Present (E2) loop
6676 exit when Ekind (E1) = E_Package
6677 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6678
6679 -- If the formal is the renaming of the formal package, this
6680 -- is the end of its formal part, which may occur before the
6681 -- end of the formal part in the actual in the presence of
6682 -- defaulted parameters in the formal package.
6683
6684 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6685 and then Renamed_Entity (E2) = Scope (E2);
6686
6687 -- The analysis of the actual may generate additional internal
6688 -- entities. If the formal is defaulted, there is no corresponding
6689 -- analysis and the internal entities must be skipped, until we
6690 -- find corresponding entities again.
6691
6692 if Comes_From_Source (E2)
6693 and then not Comes_From_Source (E1)
6694 and then Chars (E1) /= Chars (E2)
6695 then
6696 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6697 Next_Entity (E1);
6698 end loop;
6699 end if;
6700
6701 if No (E1) then
6702 return;
6703
6704 -- Entities may be declared without full declaration, such as
6705 -- itypes and predefined operators (concatenation for arrays, eg).
6706 -- Skip it and keep the formal entity to find a later match for it.
6707
6708 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6709 E1 := Prev_E1;
6710 goto Next_E;
6711
6712 -- If the formal entity comes from a formal declaration, it was
6713 -- defaulted in the formal package, and no check is needed on it.
6714
6715 elsif Nkind (Original_Node (Parent (E2))) in
6716 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6717 then
6718 -- If the formal is a tagged type the corresponding class-wide
6719 -- type has been generated as well, and it must be skipped.
6720
6721 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6722 Next_Entity (E2);
6723 end if;
6724
6725 goto Next_E;
6726
6727 -- Ditto for defaulted formal subprograms.
6728
6729 elsif Is_Overloadable (E1)
6730 and then Nkind (Unit_Declaration_Node (E2)) in
6731 N_Formal_Subprogram_Declaration
6732 then
6733 goto Next_E;
6734
6735 elsif Is_Defaulted (E1) then
6736 goto Next_E;
6737
6738 elsif Is_Type (E1) then
6739
6740 -- Subtypes must statically match. E1, E2 are the local entities
6741 -- that are subtypes of the actuals. Itypes generated for other
6742 -- parameters need not be checked, the check will be performed
6743 -- on the parameters themselves.
6744
6745 -- If E2 is a formal type declaration, it is a defaulted parameter
6746 -- and needs no checking.
6747
6748 if not Is_Itype (E1) and then not Is_Itype (E2) then
6749 Check_Mismatch
6750 (not Is_Type (E2)
6751 or else Etype (E1) /= Etype (E2)
6752 or else not Subtypes_Statically_Match (E1, E2));
6753 end if;
6754
6755 elsif Ekind (E1) = E_Constant then
6756
6757 -- IN parameters must denote the same static value, or the same
6758 -- constant, or the literal null.
6759
6760 Expr1 := Expression (Parent (E1));
6761
6762 if Ekind (E2) /= E_Constant then
6763 Check_Mismatch (True);
6764 goto Next_E;
6765 else
6766 Expr2 := Expression (Parent (E2));
6767 end if;
6768
6769 if Is_OK_Static_Expression (Expr1) then
6770 if not Is_OK_Static_Expression (Expr2) then
6771 Check_Mismatch (True);
6772
6773 elsif Is_Discrete_Type (Etype (E1)) then
6774 declare
6775 V1 : constant Uint := Expr_Value (Expr1);
6776 V2 : constant Uint := Expr_Value (Expr2);
6777 begin
6778 Check_Mismatch (V1 /= V2);
6779 end;
6780
6781 elsif Is_Real_Type (Etype (E1)) then
6782 declare
6783 V1 : constant Ureal := Expr_Value_R (Expr1);
6784 V2 : constant Ureal := Expr_Value_R (Expr2);
6785 begin
6786 Check_Mismatch (V1 /= V2);
6787 end;
6788
6789 elsif Is_String_Type (Etype (E1))
6790 and then Nkind (Expr1) = N_String_Literal
6791 then
6792 if Nkind (Expr2) /= N_String_Literal then
6793 Check_Mismatch (True);
6794 else
6795 Check_Mismatch
6796 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6797 end if;
6798 end if;
6799
6800 elsif Is_Entity_Name (Expr1) then
6801 if Is_Entity_Name (Expr2) then
6802 if Entity (Expr1) = Entity (Expr2) then
6803 null;
6804 else
6805 Check_Mismatch
6806 (not Same_Instantiated_Constant
6807 (Entity (Expr1), Entity (Expr2)));
6808 end if;
6809
6810 else
6811 Check_Mismatch (True);
6812 end if;
6813
6814 elsif Is_Entity_Name (Original_Node (Expr1))
6815 and then Is_Entity_Name (Expr2)
6816 and then Same_Instantiated_Constant
6817 (Entity (Original_Node (Expr1)), Entity (Expr2))
6818 then
6819 null;
6820
6821 elsif Nkind (Expr1) = N_Null then
6822 Check_Mismatch (Nkind (Expr1) /= N_Null);
6823
6824 else
6825 Check_Mismatch (True);
6826 end if;
6827
6828 elsif Ekind (E1) = E_Variable then
6829 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6830
6831 elsif Ekind (E1) = E_Package then
6832 Check_Mismatch
6833 (Ekind (E1) /= Ekind (E2)
6834 or else (Present (Renamed_Object (E2))
6835 and then Renamed_Object (E1) /=
6836 Renamed_Object (E2)));
6837
6838 elsif Is_Overloadable (E1) then
6839 -- Verify that the actual subprograms match. Note that actuals
6840 -- that are attributes are rewritten as subprograms. If the
6841 -- subprogram in the formal package is defaulted, no check is
6842 -- needed. Note that this can only happen in Ada 2005 when the
6843 -- formal package can be partially parameterized.
6844
6845 if Nkind (Unit_Declaration_Node (E1)) =
6846 N_Subprogram_Renaming_Declaration
6847 and then From_Default (Unit_Declaration_Node (E1))
6848 then
6849 null;
6850
6851 -- If the formal package has an "others" box association that
6852 -- covers this formal, there is no need for a check either.
6853
6854 elsif Nkind (Unit_Declaration_Node (E2)) in
6855 N_Formal_Subprogram_Declaration
6856 and then Box_Present (Unit_Declaration_Node (E2))
6857 then
6858 null;
6859
6860 -- No check needed if subprogram is a defaulted null procedure
6861
6862 elsif No (Alias (E2))
6863 and then Ekind (E2) = E_Procedure
6864 and then
6865 Null_Present (Specification (Unit_Declaration_Node (E2)))
6866 then
6867 null;
6868
6869 -- Otherwise the actual in the formal and the actual in the
6870 -- instantiation of the formal must match, up to renamings.
6871
6872 else
6873 Check_Mismatch
6874 (Ekind (E2) /= Ekind (E1)
6875 or else not Same_Instantiated_Function (E1, E2));
6876 end if;
6877
6878 else
6879 raise Program_Error;
6880 end if;
6881
6882 <<Next_E>>
6883 Prev_E1 := E1;
6884 Next_Entity (E1);
6885 Next_Entity (E2);
6886 end loop;
6887 end Check_Formal_Package_Instance;
6888
6889 ---------------------------
6890 -- Check_Formal_Packages --
6891 ---------------------------
6892
6893 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6894 E : Entity_Id;
6895 Formal_P : Entity_Id;
6896 Formal_Decl : Node_Id;
6897 begin
6898 -- Iterate through the declarations in the instance, looking for package
6899 -- renaming declarations that denote instances of formal packages. Stop
6900 -- when we find the renaming of the current package itself. The
6901 -- declaration for a formal package without a box is followed by an
6902 -- internal entity that repeats the instantiation.
6903
6904 E := First_Entity (P_Id);
6905 while Present (E) loop
6906 if Ekind (E) = E_Package then
6907 if Renamed_Object (E) = P_Id then
6908 exit;
6909
6910 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6911 null;
6912
6913 else
6914 Formal_Decl := Parent (Associated_Formal_Package (E));
6915
6916 -- Nothing to check if the formal has a box or an others_clause
6917 -- (necessarily with a box), or no associations altogether
6918
6919 if Box_Present (Formal_Decl)
6920 or else No (Generic_Associations (Formal_Decl))
6921 then
6922 null;
6923
6924 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6925 N_Others_Choice
6926 then
6927 -- The internal validating package was generated but formal
6928 -- and instance are known to be compatible.
6929
6930 Formal_P := Next_Entity (E);
6931 Remove (Unit_Declaration_Node (Formal_P));
6932
6933 else
6934 Formal_P := Next_Entity (E);
6935
6936 -- If the instance is within an enclosing instance body
6937 -- there is no need to verify the legality of current formal
6938 -- packages because they were legal in the generic body.
6939 -- This optimization may be applicable elsewhere, and it
6940 -- also removes spurious errors that may arise with
6941 -- on-the-fly inlining and confusion between private and
6942 -- full views.
6943
6944 if not In_Instance_Body then
6945 Check_Formal_Package_Instance (Formal_P, E);
6946 end if;
6947
6948 -- Restore the visibility of formals of the formal instance
6949 -- that are not defaulted, and are hidden within the current
6950 -- generic. These formals may be visible within an enclosing
6951 -- generic.
6952
6953 declare
6954 Elmt : Elmt_Id;
6955 begin
6956 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6957 while Present (Elmt) loop
6958 Set_Is_Hidden (Node (Elmt), False);
6959 Next_Elmt (Elmt);
6960 end loop;
6961 end;
6962
6963 -- After checking, remove the internal validating package.
6964 -- It is only needed for semantic checks, and as it may
6965 -- contain generic formal declarations it should not reach
6966 -- gigi.
6967
6968 Remove (Unit_Declaration_Node (Formal_P));
6969 end if;
6970 end if;
6971 end if;
6972
6973 Next_Entity (E);
6974 end loop;
6975 end Check_Formal_Packages;
6976
6977 ---------------------------------
6978 -- Check_Forward_Instantiation --
6979 ---------------------------------
6980
6981 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6982 S : Entity_Id;
6983 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6984
6985 begin
6986 -- The instantiation appears before the generic body if we are in the
6987 -- scope of the unit containing the generic, either in its spec or in
6988 -- the package body, and before the generic body.
6989
6990 if Ekind (Gen_Comp) = E_Package_Body then
6991 Gen_Comp := Spec_Entity (Gen_Comp);
6992 end if;
6993
6994 if In_Open_Scopes (Gen_Comp)
6995 and then No (Corresponding_Body (Decl))
6996 then
6997 S := Current_Scope;
6998
6999 while Present (S)
7000 and then not Is_Compilation_Unit (S)
7001 and then not Is_Child_Unit (S)
7002 loop
7003 if Ekind (S) = E_Package then
7004 Set_Has_Forward_Instantiation (S);
7005 end if;
7006
7007 S := Scope (S);
7008 end loop;
7009 end if;
7010 end Check_Forward_Instantiation;
7011
7012 ---------------------------
7013 -- Check_Generic_Actuals --
7014 ---------------------------
7015
7016 -- The visibility of the actuals may be different between the point of
7017 -- generic instantiation and the instantiation of the body.
7018
7019 procedure Check_Generic_Actuals
7020 (Instance : Entity_Id;
7021 Is_Formal_Box : Boolean)
7022 is
7023 E : Entity_Id;
7024 Astype : Entity_Id;
7025
7026 begin
7027 E := First_Entity (Instance);
7028 while Present (E) loop
7029 if Is_Type (E)
7030 and then Nkind (Parent (E)) = N_Subtype_Declaration
7031 and then Scope (Etype (E)) /= Instance
7032 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7033 then
7034 -- Restore the proper view of the actual from the information
7035 -- saved earlier by Instantiate_Type.
7036
7037 Check_Private_View (Subtype_Indication (Parent (E)));
7038
7039 -- If the actual is itself the formal of a parent instance,
7040 -- then also restore the proper view of its actual and so on.
7041 -- That's necessary for nested instantiations of the form
7042
7043 -- generic
7044 -- type Component is private;
7045 -- type Array_Type is array (Positive range <>) of Component;
7046 -- procedure Proc;
7047
7048 -- when the outermost actuals have inconsistent views, because
7049 -- the Component_Type of Array_Type of the inner instantiations
7050 -- is the actual of Component of the outermost one and not that
7051 -- of the corresponding inner instantiations.
7052
7053 Astype := Ancestor_Subtype (E);
7054 while Present (Astype)
7055 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7056 and then Present (Generic_Parent_Type (Parent (Astype)))
7057 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7058 loop
7059 Check_Private_View (Subtype_Indication (Parent (Astype)));
7060 Astype := Ancestor_Subtype (Astype);
7061 end loop;
7062
7063 Set_Is_Generic_Actual_Type (E);
7064
7065 if Is_Private_Type (E) and then Present (Full_View (E)) then
7066 Set_Is_Generic_Actual_Type (Full_View (E));
7067 end if;
7068
7069 Set_Is_Hidden (E, False);
7070 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7071
7072 -- We constructed the generic actual type as a subtype of the
7073 -- supplied type. This means that it normally would not inherit
7074 -- subtype specific attributes of the actual, which is wrong for
7075 -- the generic case.
7076
7077 Astype := Ancestor_Subtype (E);
7078
7079 if No (Astype) then
7080
7081 -- This can happen when E is an itype that is the full view of
7082 -- a private type completed, e.g. with a constrained array. In
7083 -- that case, use the first subtype, which will carry size
7084 -- information. The base type itself is unconstrained and will
7085 -- not carry it.
7086
7087 Astype := First_Subtype (E);
7088 end if;
7089
7090 Set_Size_Info (E, (Astype));
7091 Set_RM_Size (E, RM_Size (Astype));
7092 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7093
7094 if Is_Discrete_Or_Fixed_Point_Type (E) then
7095 Set_RM_Size (E, RM_Size (Astype));
7096 end if;
7097
7098 elsif Ekind (E) = E_Package then
7099
7100 -- If this is the renaming for the current instance, we're done.
7101 -- Otherwise it is a formal package. If the corresponding formal
7102 -- was declared with a box, the (instantiations of the) generic
7103 -- formal part are also visible. Otherwise, ignore the entity
7104 -- created to validate the actuals.
7105
7106 if Renamed_Object (E) = Instance then
7107 exit;
7108
7109 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7110 null;
7111
7112 -- The visibility of a formal of an enclosing generic is already
7113 -- correct.
7114
7115 elsif Denotes_Formal_Package (E) then
7116 null;
7117
7118 elsif Present (Associated_Formal_Package (E))
7119 and then not Is_Generic_Formal (E)
7120 then
7121 if Box_Present (Parent (Associated_Formal_Package (E))) then
7122 Check_Generic_Actuals (Renamed_Object (E), True);
7123
7124 else
7125 Check_Generic_Actuals (Renamed_Object (E), False);
7126 end if;
7127
7128 Set_Is_Hidden (E, False);
7129 end if;
7130
7131 -- If this is a subprogram instance (in a wrapper package) the
7132 -- actual is fully visible.
7133
7134 elsif Is_Wrapper_Package (Instance) then
7135 Set_Is_Hidden (E, False);
7136
7137 -- If the formal package is declared with a box, or if the formal
7138 -- parameter is defaulted, it is visible in the body.
7139
7140 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7141 Set_Is_Hidden (E, False);
7142 end if;
7143
7144 if Ekind (E) = E_Constant then
7145
7146 -- If the type of the actual is a private type declared in the
7147 -- enclosing scope of the generic unit, the body of the generic
7148 -- sees the full view of the type (because it has to appear in
7149 -- the corresponding package body). If the type is private now,
7150 -- exchange views to restore the proper visiblity in the instance.
7151
7152 declare
7153 Typ : constant Entity_Id := Base_Type (Etype (E));
7154 -- The type of the actual
7155
7156 Gen_Id : Entity_Id;
7157 -- The generic unit
7158
7159 Parent_Scope : Entity_Id;
7160 -- The enclosing scope of the generic unit
7161
7162 begin
7163 if Is_Wrapper_Package (Instance) then
7164 Gen_Id :=
7165 Generic_Parent
7166 (Specification
7167 (Unit_Declaration_Node
7168 (Related_Instance (Instance))));
7169 else
7170 Gen_Id :=
7171 Generic_Parent (Package_Specification (Instance));
7172 end if;
7173
7174 Parent_Scope := Scope (Gen_Id);
7175
7176 -- The exchange is only needed if the generic is defined
7177 -- within a package which is not a common ancestor of the
7178 -- scope of the instance, and is not already in scope.
7179
7180 if Is_Private_Type (Typ)
7181 and then Scope (Typ) = Parent_Scope
7182 and then Scope (Instance) /= Parent_Scope
7183 and then Ekind (Parent_Scope) = E_Package
7184 and then not Is_Child_Unit (Gen_Id)
7185 then
7186 Switch_View (Typ);
7187
7188 -- If the type of the entity is a subtype, it may also have
7189 -- to be made visible, together with the base type of its
7190 -- full view, after exchange.
7191
7192 if Is_Private_Type (Etype (E)) then
7193 Switch_View (Etype (E));
7194 Switch_View (Base_Type (Etype (E)));
7195 end if;
7196 end if;
7197 end;
7198 end if;
7199
7200 Next_Entity (E);
7201 end loop;
7202 end Check_Generic_Actuals;
7203
7204 ------------------------------
7205 -- Check_Generic_Child_Unit --
7206 ------------------------------
7207
7208 procedure Check_Generic_Child_Unit
7209 (Gen_Id : Node_Id;
7210 Parent_Installed : in out Boolean)
7211 is
7212 Loc : constant Source_Ptr := Sloc (Gen_Id);
7213 Gen_Par : Entity_Id := Empty;
7214 E : Entity_Id;
7215 Inst_Par : Entity_Id;
7216 S : Node_Id;
7217
7218 function Find_Generic_Child
7219 (Scop : Entity_Id;
7220 Id : Node_Id) return Entity_Id;
7221 -- Search generic parent for possible child unit with the given name
7222
7223 function In_Enclosing_Instance return Boolean;
7224 -- Within an instance of the parent, the child unit may be denoted by
7225 -- a simple name, or an abbreviated expanded name. Examine enclosing
7226 -- scopes to locate a possible parent instantiation.
7227
7228 ------------------------
7229 -- Find_Generic_Child --
7230 ------------------------
7231
7232 function Find_Generic_Child
7233 (Scop : Entity_Id;
7234 Id : Node_Id) return Entity_Id
7235 is
7236 E : Entity_Id;
7237
7238 begin
7239 -- If entity of name is already set, instance has already been
7240 -- resolved, e.g. in an enclosing instantiation.
7241
7242 if Present (Entity (Id)) then
7243 if Scope (Entity (Id)) = Scop then
7244 return Entity (Id);
7245 else
7246 return Empty;
7247 end if;
7248
7249 else
7250 E := First_Entity (Scop);
7251 while Present (E) loop
7252 if Chars (E) = Chars (Id)
7253 and then Is_Child_Unit (E)
7254 then
7255 if Is_Child_Unit (E)
7256 and then not Is_Visible_Lib_Unit (E)
7257 then
7258 Error_Msg_NE
7259 ("generic child unit& is not visible", Gen_Id, E);
7260 end if;
7261
7262 Set_Entity (Id, E);
7263 return E;
7264 end if;
7265
7266 Next_Entity (E);
7267 end loop;
7268
7269 return Empty;
7270 end if;
7271 end Find_Generic_Child;
7272
7273 ---------------------------
7274 -- In_Enclosing_Instance --
7275 ---------------------------
7276
7277 function In_Enclosing_Instance return Boolean is
7278 Enclosing_Instance : Node_Id;
7279 Instance_Decl : Node_Id;
7280
7281 begin
7282 -- We do not inline any call that contains instantiations, except
7283 -- for instantiations of Unchecked_Conversion, so if we are within
7284 -- an inlined body the current instance does not require parents.
7285
7286 if In_Inlined_Body then
7287 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7288 return False;
7289 end if;
7290
7291 -- Loop to check enclosing scopes
7292
7293 Enclosing_Instance := Current_Scope;
7294 while Present (Enclosing_Instance) loop
7295 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7296
7297 if Ekind (Enclosing_Instance) = E_Package
7298 and then Is_Generic_Instance (Enclosing_Instance)
7299 and then Present
7300 (Generic_Parent (Specification (Instance_Decl)))
7301 then
7302 -- Check whether the generic we are looking for is a child of
7303 -- this instance.
7304
7305 E := Find_Generic_Child
7306 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7307 exit when Present (E);
7308
7309 else
7310 E := Empty;
7311 end if;
7312
7313 Enclosing_Instance := Scope (Enclosing_Instance);
7314 end loop;
7315
7316 if No (E) then
7317
7318 -- Not a child unit
7319
7320 Analyze (Gen_Id);
7321 return False;
7322
7323 else
7324 Rewrite (Gen_Id,
7325 Make_Expanded_Name (Loc,
7326 Chars => Chars (E),
7327 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7328 Selector_Name => New_Occurrence_Of (E, Loc)));
7329
7330 Set_Entity (Gen_Id, E);
7331 Set_Etype (Gen_Id, Etype (E));
7332 Parent_Installed := False; -- Already in scope.
7333 return True;
7334 end if;
7335 end In_Enclosing_Instance;
7336
7337 -- Start of processing for Check_Generic_Child_Unit
7338
7339 begin
7340 -- If the name of the generic is given by a selected component, it may
7341 -- be the name of a generic child unit, and the prefix is the name of an
7342 -- instance of the parent, in which case the child unit must be visible.
7343 -- If this instance is not in scope, it must be placed there and removed
7344 -- after instantiation, because what is being instantiated is not the
7345 -- original child, but the corresponding child present in the instance
7346 -- of the parent.
7347
7348 -- If the child is instantiated within the parent, it can be given by
7349 -- a simple name. In this case the instance is already in scope, but
7350 -- the child generic must be recovered from the generic parent as well.
7351
7352 if Nkind (Gen_Id) = N_Selected_Component then
7353 S := Selector_Name (Gen_Id);
7354 Analyze (Prefix (Gen_Id));
7355 Inst_Par := Entity (Prefix (Gen_Id));
7356
7357 if Ekind (Inst_Par) = E_Package
7358 and then Present (Renamed_Object (Inst_Par))
7359 then
7360 Inst_Par := Renamed_Object (Inst_Par);
7361 end if;
7362
7363 if Ekind (Inst_Par) = E_Package then
7364 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7365 Gen_Par := Generic_Parent (Parent (Inst_Par));
7366
7367 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7368 and then
7369 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7370 then
7371 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7372 end if;
7373
7374 elsif Ekind (Inst_Par) = E_Generic_Package
7375 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7376 then
7377 -- A formal package may be a real child package, and not the
7378 -- implicit instance within a parent. In this case the child is
7379 -- not visible and has to be retrieved explicitly as well.
7380
7381 Gen_Par := Inst_Par;
7382 end if;
7383
7384 if Present (Gen_Par) then
7385
7386 -- The prefix denotes an instantiation. The entity itself may be a
7387 -- nested generic, or a child unit.
7388
7389 E := Find_Generic_Child (Gen_Par, S);
7390
7391 if Present (E) then
7392 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7393 Set_Entity (Gen_Id, E);
7394 Set_Etype (Gen_Id, Etype (E));
7395 Set_Entity (S, E);
7396 Set_Etype (S, Etype (E));
7397
7398 -- Indicate that this is a reference to the parent
7399
7400 if In_Extended_Main_Source_Unit (Gen_Id) then
7401 Set_Is_Instantiated (Inst_Par);
7402 end if;
7403
7404 -- A common mistake is to replicate the naming scheme of a
7405 -- hierarchy by instantiating a generic child directly, rather
7406 -- than the implicit child in a parent instance:
7407
7408 -- generic .. package Gpar is ..
7409 -- generic .. package Gpar.Child is ..
7410 -- package Par is new Gpar ();
7411
7412 -- with Gpar.Child;
7413 -- package Par.Child is new Gpar.Child ();
7414 -- rather than Par.Child
7415
7416 -- In this case the instantiation is within Par, which is an
7417 -- instance, but Gpar does not denote Par because we are not IN
7418 -- the instance of Gpar, so this is illegal. The test below
7419 -- recognizes this particular case.
7420
7421 if Is_Child_Unit (E)
7422 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7423 and then (not In_Instance
7424 or else Nkind (Parent (Parent (Gen_Id))) =
7425 N_Compilation_Unit)
7426 then
7427 Error_Msg_N
7428 ("prefix of generic child unit must be instance of parent",
7429 Gen_Id);
7430 end if;
7431
7432 if not In_Open_Scopes (Inst_Par)
7433 and then Nkind (Parent (Gen_Id)) not in
7434 N_Generic_Renaming_Declaration
7435 then
7436 Install_Parent (Inst_Par);
7437 Parent_Installed := True;
7438
7439 elsif In_Open_Scopes (Inst_Par) then
7440
7441 -- If the parent is already installed, install the actuals
7442 -- for its formal packages. This is necessary when the child
7443 -- instance is a child of the parent instance: in this case,
7444 -- the parent is placed on the scope stack but the formal
7445 -- packages are not made visible.
7446
7447 Install_Formal_Packages (Inst_Par);
7448 end if;
7449
7450 else
7451 -- If the generic parent does not contain an entity that
7452 -- corresponds to the selector, the instance doesn't either.
7453 -- Analyzing the node will yield the appropriate error message.
7454 -- If the entity is not a child unit, then it is an inner
7455 -- generic in the parent.
7456
7457 Analyze (Gen_Id);
7458 end if;
7459
7460 else
7461 Analyze (Gen_Id);
7462
7463 if Is_Child_Unit (Entity (Gen_Id))
7464 and then
7465 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7466 and then not In_Open_Scopes (Inst_Par)
7467 then
7468 Install_Parent (Inst_Par);
7469 Parent_Installed := True;
7470
7471 -- The generic unit may be the renaming of the implicit child
7472 -- present in an instance. In that case the parent instance is
7473 -- obtained from the name of the renamed entity.
7474
7475 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7476 and then Present (Renamed_Entity (Entity (Gen_Id)))
7477 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7478 then
7479 declare
7480 Renamed_Package : constant Node_Id :=
7481 Name (Parent (Entity (Gen_Id)));
7482 begin
7483 if Nkind (Renamed_Package) = N_Expanded_Name then
7484 Inst_Par := Entity (Prefix (Renamed_Package));
7485 Install_Parent (Inst_Par);
7486 Parent_Installed := True;
7487 end if;
7488 end;
7489 end if;
7490 end if;
7491
7492 elsif Nkind (Gen_Id) = N_Expanded_Name then
7493
7494 -- Entity already present, analyze prefix, whose meaning may be an
7495 -- instance in the current context. If it is an instance of a
7496 -- relative within another, the proper parent may still have to be
7497 -- installed, if they are not of the same generation.
7498
7499 Analyze (Prefix (Gen_Id));
7500
7501 -- Prevent cascaded errors
7502
7503 if Etype (Prefix (Gen_Id)) = Any_Type then
7504 return;
7505 end if;
7506
7507 -- In the unlikely case that a local declaration hides the name of
7508 -- the parent package, locate it on the homonym chain. If the context
7509 -- is an instance of the parent, the renaming entity is flagged as
7510 -- such.
7511
7512 Inst_Par := Entity (Prefix (Gen_Id));
7513 while Present (Inst_Par)
7514 and then not Is_Package_Or_Generic_Package (Inst_Par)
7515 loop
7516 Inst_Par := Homonym (Inst_Par);
7517 end loop;
7518
7519 pragma Assert (Present (Inst_Par));
7520 Set_Entity (Prefix (Gen_Id), Inst_Par);
7521
7522 if In_Enclosing_Instance then
7523 null;
7524
7525 elsif Present (Entity (Gen_Id))
7526 and then No (Renamed_Entity (Entity (Gen_Id)))
7527 and then Is_Child_Unit (Entity (Gen_Id))
7528 and then not In_Open_Scopes (Inst_Par)
7529 then
7530 Install_Parent (Inst_Par);
7531 Parent_Installed := True;
7532
7533 -- Handle renaming of generic child unit
7534
7535 elsif Present (Entity (Gen_Id))
7536 and then Present (Renamed_Entity (Entity (Gen_Id)))
7537 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7538 then
7539 declare
7540 E : Entity_Id;
7541 Ren_Decl : Node_Id;
7542
7543 begin
7544 -- The entity of the renamed generic child unit does not
7545 -- have any reference to the instantiated parent. In order to
7546 -- locate it we traverse the scope containing the renaming
7547 -- declaration; the instance of the parent is available in
7548 -- the prefix of the renaming declaration. For example:
7549
7550 -- package A is
7551 -- package Inst_Par is new ...
7552 -- generic package Ren_Child renames Ins_Par.Child;
7553 -- end;
7554
7555 -- with A;
7556 -- package B is
7557 -- package Inst_Child is new A.Ren_Child;
7558 -- end;
7559
7560 E := First_Entity (Entity (Prefix (Gen_Id)));
7561 while Present (E) loop
7562 if Present (Renamed_Entity (E))
7563 and then
7564 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7565 then
7566 Ren_Decl := Parent (E);
7567 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7568
7569 if not In_Open_Scopes (Inst_Par) then
7570 Install_Parent (Inst_Par);
7571 Parent_Installed := True;
7572 end if;
7573
7574 exit;
7575 end if;
7576
7577 E := Next_Entity (E);
7578 end loop;
7579 end;
7580 end if;
7581
7582 elsif In_Enclosing_Instance then
7583
7584 -- The child unit is found in some enclosing scope
7585
7586 null;
7587
7588 else
7589 Analyze (Gen_Id);
7590
7591 -- If this is the renaming of the implicit child in a parent
7592 -- instance, recover the parent name and install it.
7593
7594 if Is_Entity_Name (Gen_Id) then
7595 E := Entity (Gen_Id);
7596
7597 if Is_Generic_Unit (E)
7598 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7599 and then Is_Child_Unit (Renamed_Object (E))
7600 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7601 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7602 then
7603 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7604 Inst_Par := Entity (Prefix (Gen_Id));
7605
7606 if not In_Open_Scopes (Inst_Par) then
7607 Install_Parent (Inst_Par);
7608 Parent_Installed := True;
7609 end if;
7610
7611 -- If it is a child unit of a non-generic parent, it may be
7612 -- use-visible and given by a direct name. Install parent as
7613 -- for other cases.
7614
7615 elsif Is_Generic_Unit (E)
7616 and then Is_Child_Unit (E)
7617 and then
7618 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7619 and then not Is_Generic_Unit (Scope (E))
7620 then
7621 if not In_Open_Scopes (Scope (E)) then
7622 Install_Parent (Scope (E));
7623 Parent_Installed := True;
7624 end if;
7625 end if;
7626 end if;
7627 end if;
7628 end Check_Generic_Child_Unit;
7629
7630 -----------------------------
7631 -- Check_Hidden_Child_Unit --
7632 -----------------------------
7633
7634 procedure Check_Hidden_Child_Unit
7635 (N : Node_Id;
7636 Gen_Unit : Entity_Id;
7637 Act_Decl_Id : Entity_Id)
7638 is
7639 Gen_Id : constant Node_Id := Name (N);
7640
7641 begin
7642 if Is_Child_Unit (Gen_Unit)
7643 and then Is_Child_Unit (Act_Decl_Id)
7644 and then Nkind (Gen_Id) = N_Expanded_Name
7645 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7646 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7647 then
7648 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7649 Error_Msg_NE
7650 ("generic unit & is implicitly declared in &",
7651 Defining_Unit_Name (N), Gen_Unit);
7652 Error_Msg_N ("\instance must have different name",
7653 Defining_Unit_Name (N));
7654 end if;
7655 end Check_Hidden_Child_Unit;
7656
7657 ------------------------
7658 -- Check_Private_View --
7659 ------------------------
7660
7661 procedure Check_Private_View (N : Node_Id) is
7662 T : constant Entity_Id := Etype (N);
7663 BT : Entity_Id;
7664
7665 begin
7666 -- Exchange views if the type was not private in the generic but is
7667 -- private at the point of instantiation. Do not exchange views if
7668 -- the scope of the type is in scope. This can happen if both generic
7669 -- and instance are sibling units, or if type is defined in a parent.
7670 -- In this case the visibility of the type will be correct for all
7671 -- semantic checks.
7672
7673 if Present (T) then
7674 BT := Base_Type (T);
7675
7676 if Is_Private_Type (T)
7677 and then not Has_Private_View (N)
7678 and then Present (Full_View (T))
7679 and then not In_Open_Scopes (Scope (T))
7680 then
7681 -- In the generic, the full declaration was visible
7682
7683 Switch_View (T);
7684
7685 elsif Has_Private_View (N)
7686 and then not Is_Private_Type (T)
7687 and then not Has_Been_Exchanged (T)
7688 and then (not In_Open_Scopes (Scope (T))
7689 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7690 then
7691 -- In the generic, only the private declaration was visible
7692
7693 -- If the type appears in a subtype declaration, the subtype in
7694 -- instance must have a view compatible with that of its parent,
7695 -- which must be exchanged (see corresponding code in Restore_
7696 -- Private_Views) so we make an exception to the open scope rule.
7697
7698 Prepend_Elmt (T, Exchanged_Views);
7699 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7700
7701 -- Finally, a non-private subtype may have a private base type, which
7702 -- must be exchanged for consistency. This can happen when a package
7703 -- body is instantiated, when the scope stack is empty but in fact
7704 -- the subtype and the base type are declared in an enclosing scope.
7705
7706 -- Note that in this case we introduce an inconsistency in the view
7707 -- set, because we switch the base type BT, but there could be some
7708 -- private dependent subtypes of BT which remain unswitched. Such
7709 -- subtypes might need to be switched at a later point (see specific
7710 -- provision for that case in Switch_View).
7711
7712 elsif not Is_Private_Type (T)
7713 and then not Has_Private_View (N)
7714 and then Is_Private_Type (BT)
7715 and then Present (Full_View (BT))
7716 and then not Is_Generic_Type (BT)
7717 and then not In_Open_Scopes (BT)
7718 then
7719 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7720 Exchange_Declarations (BT);
7721 end if;
7722 end if;
7723 end Check_Private_View;
7724
7725 -----------------------------
7726 -- Check_Hidden_Primitives --
7727 -----------------------------
7728
7729 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7730 Actual : Node_Id;
7731 Gen_T : Entity_Id;
7732 Result : Elist_Id := No_Elist;
7733
7734 begin
7735 if No (Assoc_List) then
7736 return No_Elist;
7737 end if;
7738
7739 -- Traverse the list of associations between formals and actuals
7740 -- searching for renamings of tagged types
7741
7742 Actual := First (Assoc_List);
7743 while Present (Actual) loop
7744 if Nkind (Actual) = N_Subtype_Declaration then
7745 Gen_T := Generic_Parent_Type (Actual);
7746
7747 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7748
7749 -- Traverse the list of primitives of the actual types
7750 -- searching for hidden primitives that are visible in the
7751 -- corresponding generic formal; leave them visible and
7752 -- append them to Result to restore their decoration later.
7753
7754 Install_Hidden_Primitives
7755 (Prims_List => Result,
7756 Gen_T => Gen_T,
7757 Act_T => Entity (Subtype_Indication (Actual)));
7758 end if;
7759 end if;
7760
7761 Next (Actual);
7762 end loop;
7763
7764 return Result;
7765 end Check_Hidden_Primitives;
7766
7767 --------------------------
7768 -- Contains_Instance_Of --
7769 --------------------------
7770
7771 function Contains_Instance_Of
7772 (Inner : Entity_Id;
7773 Outer : Entity_Id;
7774 N : Node_Id) return Boolean
7775 is
7776 Elmt : Elmt_Id;
7777 Scop : Entity_Id;
7778
7779 begin
7780 Scop := Outer;
7781
7782 -- Verify that there are no circular instantiations. We check whether
7783 -- the unit contains an instance of the current scope or some enclosing
7784 -- scope (in case one of the instances appears in a subunit). Longer
7785 -- circularities involving subunits might seem too pathological to
7786 -- consider, but they were not too pathological for the authors of
7787 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7788 -- enclosing generic scopes as containing an instance.
7789
7790 loop
7791 -- Within a generic subprogram body, the scope is not generic, to
7792 -- allow for recursive subprograms. Use the declaration to determine
7793 -- whether this is a generic unit.
7794
7795 if Ekind (Scop) = E_Generic_Package
7796 or else (Is_Subprogram (Scop)
7797 and then Nkind (Unit_Declaration_Node (Scop)) =
7798 N_Generic_Subprogram_Declaration)
7799 then
7800 Elmt := First_Elmt (Inner_Instances (Inner));
7801
7802 while Present (Elmt) loop
7803 if Node (Elmt) = Scop then
7804 Error_Msg_Node_2 := Inner;
7805 Error_Msg_NE
7806 ("circular Instantiation: & instantiated within &!",
7807 N, Scop);
7808 return True;
7809
7810 elsif Node (Elmt) = Inner then
7811 return True;
7812
7813 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7814 Error_Msg_Node_2 := Inner;
7815 Error_Msg_NE
7816 ("circular Instantiation: & instantiated within &!",
7817 N, Node (Elmt));
7818 return True;
7819 end if;
7820
7821 Next_Elmt (Elmt);
7822 end loop;
7823
7824 -- Indicate that Inner is being instantiated within Scop
7825
7826 Append_Elmt (Inner, Inner_Instances (Scop));
7827 end if;
7828
7829 if Scop = Standard_Standard then
7830 exit;
7831 else
7832 Scop := Scope (Scop);
7833 end if;
7834 end loop;
7835
7836 return False;
7837 end Contains_Instance_Of;
7838
7839 -----------------------
7840 -- Copy_Generic_Node --
7841 -----------------------
7842
7843 function Copy_Generic_Node
7844 (N : Node_Id;
7845 Parent_Id : Node_Id;
7846 Instantiating : Boolean) return Node_Id
7847 is
7848 Ent : Entity_Id;
7849 New_N : Node_Id;
7850
7851 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7852 -- Check the given value of one of the Fields referenced by the current
7853 -- node to determine whether to copy it recursively. The field may hold
7854 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7855 -- Char) in which case it need not be copied.
7856
7857 procedure Copy_Descendants;
7858 -- Common utility for various nodes
7859
7860 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7861 -- Make copy of element list
7862
7863 function Copy_Generic_List
7864 (L : List_Id;
7865 Parent_Id : Node_Id) return List_Id;
7866 -- Apply Copy_Node recursively to the members of a node list
7867
7868 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7869 -- True if an identifier is part of the defining program unit name of
7870 -- a child unit.
7871 -- Consider removing this subprogram now that ASIS no longer uses it.
7872
7873 ----------------------
7874 -- Copy_Descendants --
7875 ----------------------
7876
7877 procedure Copy_Descendants is
7878 use Atree.Unchecked_Access;
7879 -- This code section is part of the implementation of an untyped
7880 -- tree traversal, so it needs direct access to node fields.
7881
7882 begin
7883 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7884 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7885 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7886 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7887 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7888 end Copy_Descendants;
7889
7890 -----------------------------
7891 -- Copy_Generic_Descendant --
7892 -----------------------------
7893
7894 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7895 begin
7896 if D = Union_Id (Empty) then
7897 return D;
7898
7899 elsif D in Node_Range then
7900 return Union_Id
7901 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7902
7903 elsif D in List_Range then
7904 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7905
7906 elsif D in Elist_Range then
7907 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7908
7909 -- Nothing else is copyable (e.g. Uint values), return as is
7910
7911 else
7912 return D;
7913 end if;
7914 end Copy_Generic_Descendant;
7915
7916 ------------------------
7917 -- Copy_Generic_Elist --
7918 ------------------------
7919
7920 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7921 M : Elmt_Id;
7922 L : Elist_Id;
7923
7924 begin
7925 if Present (E) then
7926 L := New_Elmt_List;
7927 M := First_Elmt (E);
7928 while Present (M) loop
7929 Append_Elmt
7930 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7931 Next_Elmt (M);
7932 end loop;
7933
7934 return L;
7935
7936 else
7937 return No_Elist;
7938 end if;
7939 end Copy_Generic_Elist;
7940
7941 -----------------------
7942 -- Copy_Generic_List --
7943 -----------------------
7944
7945 function Copy_Generic_List
7946 (L : List_Id;
7947 Parent_Id : Node_Id) return List_Id
7948 is
7949 N : Node_Id;
7950 New_L : List_Id;
7951
7952 begin
7953 if Present (L) then
7954 New_L := New_List;
7955 Set_Parent (New_L, Parent_Id);
7956
7957 N := First (L);
7958 while Present (N) loop
7959 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7960 Next (N);
7961 end loop;
7962
7963 return New_L;
7964
7965 else
7966 return No_List;
7967 end if;
7968 end Copy_Generic_List;
7969
7970 ---------------------------
7971 -- In_Defining_Unit_Name --
7972 ---------------------------
7973
7974 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7975 begin
7976 return
7977 Present (Parent (Nam))
7978 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7979 or else
7980 (Nkind (Parent (Nam)) = N_Expanded_Name
7981 and then In_Defining_Unit_Name (Parent (Nam))));
7982 end In_Defining_Unit_Name;
7983
7984 -- Start of processing for Copy_Generic_Node
7985
7986 begin
7987 if N = Empty then
7988 return N;
7989 end if;
7990
7991 New_N := New_Copy (N);
7992
7993 -- Copy aspects if present
7994
7995 if Has_Aspects (N) then
7996 Set_Has_Aspects (New_N, False);
7997 Set_Aspect_Specifications
7998 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7999 end if;
8000
8001 -- If we are instantiating, we want to adjust the sloc based on the
8002 -- current S_Adjustment. However, if this is the root node of a subunit,
8003 -- we need to defer that adjustment to below (see "elsif Instantiating
8004 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8005 -- computed the adjustment.
8006
8007 if Instantiating
8008 and then not (Nkind (N) in N_Proper_Body
8009 and then Was_Originally_Stub (N))
8010 then
8011 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8012 end if;
8013
8014 if not Is_List_Member (N) then
8015 Set_Parent (New_N, Parent_Id);
8016 end if;
8017
8018 -- Special casing for identifiers and other entity names and operators
8019
8020 if Nkind (New_N) in N_Character_Literal
8021 | N_Expanded_Name
8022 | N_Identifier
8023 | N_Operator_Symbol
8024 | N_Op
8025 then
8026 if not Instantiating then
8027
8028 -- Link both nodes in order to assign subsequently the entity of
8029 -- the copy to the original node, in case this is a global
8030 -- reference.
8031
8032 Set_Associated_Node (N, New_N);
8033
8034 -- If we are within an instantiation, this is a nested generic
8035 -- that has already been analyzed at the point of definition.
8036 -- We must preserve references that were global to the enclosing
8037 -- parent at that point. Other occurrences, whether global or
8038 -- local to the current generic, must be resolved anew, so we
8039 -- reset the entity in the generic copy. A global reference has a
8040 -- smaller depth than the parent, or else the same depth in case
8041 -- both are distinct compilation units.
8042
8043 -- A child unit is implicitly declared within the enclosing parent
8044 -- but is in fact global to it, and must be preserved.
8045
8046 -- It is also possible for Current_Instantiated_Parent to be
8047 -- defined, and for this not to be a nested generic, namely if
8048 -- the unit is loaded through Rtsfind. In that case, the entity of
8049 -- New_N is only a link to the associated node, and not a defining
8050 -- occurrence.
8051
8052 -- The entities for parent units in the defining_program_unit of a
8053 -- generic child unit are established when the context of the unit
8054 -- is first analyzed, before the generic copy is made. They are
8055 -- preserved in the copy for use in e.g. ASIS queries.
8056
8057 Ent := Entity (New_N);
8058
8059 if No (Current_Instantiated_Parent.Gen_Id) then
8060 if No (Ent)
8061 or else Nkind (Ent) /= N_Defining_Identifier
8062 or else not In_Defining_Unit_Name (N)
8063 then
8064 Set_Associated_Node (New_N, Empty);
8065 end if;
8066
8067 elsif No (Ent)
8068 or else Nkind (Ent) not in N_Entity
8069 or else No (Scope (Ent))
8070 or else
8071 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8072 and then not Is_Child_Unit (Ent))
8073 or else
8074 (Scope_Depth (Scope (Ent)) >
8075 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8076 and then
8077 Get_Source_Unit (Ent) =
8078 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8079 then
8080 Set_Associated_Node (New_N, Empty);
8081 end if;
8082
8083 -- Case of instantiating identifier or some other name or operator
8084
8085 else
8086 -- If the associated node is still defined, the entity in it
8087 -- is global, and must be copied to the instance. If this copy
8088 -- is being made for a body to inline, it is applied to an
8089 -- instantiated tree, and the entity is already present and
8090 -- must be also preserved.
8091
8092 declare
8093 Assoc : constant Node_Id := Get_Associated_Node (N);
8094
8095 begin
8096 if Present (Assoc) then
8097 if Nkind (Assoc) = Nkind (N) then
8098 Set_Entity (New_N, Entity (Assoc));
8099 Check_Private_View (N);
8100
8101 -- Here we deal with a very peculiar case for which the
8102 -- Has_Private_View mechanism is not sufficient, because
8103 -- the reference to the type is implicit in the tree,
8104 -- that is to say, it's not referenced from a node but
8105 -- only from another type, namely through Component_Type.
8106
8107 -- package P is
8108
8109 -- type Pt is private;
8110
8111 -- generic
8112 -- type Ft is array (Positive range <>) of Pt;
8113 -- package G is
8114 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8115 -- end G;
8116
8117 -- private
8118 -- type Pt is new Boolean;
8119 -- end P;
8120
8121 -- package body P is
8122 -- package body G is
8123 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8124 -- begin
8125 -- if (F1 < F2) /= Lt then
8126 -- null;
8127 -- end if;
8128 -- end Check;
8129 -- end G;
8130 -- end P;
8131
8132 -- type Arr is array (Positive range <>) of P.Pt;
8133
8134 -- package Inst is new P.G (Arr);
8135
8136 -- Pt is a global type for the generic package G and it
8137 -- is not referenced in its body, but only as component
8138 -- type of Ft, which is a local type. This means that no
8139 -- references to Pt or Ft are seen during the copy of the
8140 -- body, the only reference to Pt being seen is when the
8141 -- actuals are checked by Check_Generic_Actuals, but Pt
8142 -- is still private at this point. In the end, the views
8143 -- of Pt are not switched in the body and, therefore, the
8144 -- array comparison is rejected because the component is
8145 -- still private.
8146
8147 -- Adding e.g. a dummy variable of type Pt in the body is
8148 -- sufficient to make everything work, so we generate an
8149 -- artificial reference to Pt on the fly and thus force
8150 -- the switching of views on the grounds that, if the
8151 -- comparison was accepted during the semantic analysis
8152 -- of the generic, this means that the component cannot
8153 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8154
8155 if Nkind (Assoc) in N_Op_Compare
8156 and then Present (Etype (Left_Opnd (Assoc)))
8157 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8158 and then Present (Etype (Right_Opnd (Assoc)))
8159 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8160 then
8161 declare
8162 Ltyp : constant Entity_Id :=
8163 Etype (Left_Opnd (Assoc));
8164 Rtyp : constant Entity_Id :=
8165 Etype (Right_Opnd (Assoc));
8166 begin
8167 if Is_Private_Type (Component_Type (Ltyp)) then
8168 Check_Private_View
8169 (New_Occurrence_Of (Component_Type (Ltyp),
8170 Sloc (N)));
8171 end if;
8172 if Is_Private_Type (Component_Type (Rtyp)) then
8173 Check_Private_View
8174 (New_Occurrence_Of (Component_Type (Rtyp),
8175 Sloc (N)));
8176 end if;
8177 end;
8178
8179 -- Here is a similar case, for the Designated_Type of an
8180 -- access type that is present as target type in a type
8181 -- conversion from another access type. In this case, if
8182 -- the base types of the designated types are different
8183 -- and the conversion was accepted during the semantic
8184 -- analysis of the generic, this means that the target
8185 -- type cannot have been private (see Valid_Conversion).
8186
8187 elsif Nkind (Assoc) = N_Identifier
8188 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8189 and then Subtype_Mark (Parent (Assoc)) = Assoc
8190 and then Present (Etype (Assoc))
8191 and then Is_Access_Type (Etype (Assoc))
8192 and then Present (Etype (Expression (Parent (Assoc))))
8193 and then
8194 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8195 then
8196 declare
8197 Targ_Desig : constant Entity_Id :=
8198 Designated_Type (Etype (Assoc));
8199 Expr_Desig : constant Entity_Id :=
8200 Designated_Type
8201 (Etype (Expression (Parent (Assoc))));
8202 begin
8203 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8204 and then Is_Private_Type (Targ_Desig)
8205 then
8206 Check_Private_View
8207 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8208 end if;
8209 end;
8210 end if;
8211
8212 -- The node is a reference to a global type and acts as the
8213 -- subtype mark of a qualified expression created in order
8214 -- to aid resolution of accidental overloading in instances.
8215 -- Since N is a reference to a type, the Associated_Node of
8216 -- N denotes an entity rather than another identifier. See
8217 -- Qualify_Universal_Operands for details.
8218
8219 elsif Nkind (N) = N_Identifier
8220 and then Nkind (Parent (N)) = N_Qualified_Expression
8221 and then Subtype_Mark (Parent (N)) = N
8222 and then Is_Qualified_Universal_Literal (Parent (N))
8223 then
8224 Set_Entity (New_N, Assoc);
8225
8226 -- The name in the call may be a selected component if the
8227 -- call has not been analyzed yet, as may be the case for
8228 -- pre/post conditions in a generic unit.
8229
8230 elsif Nkind (Assoc) = N_Function_Call
8231 and then Is_Entity_Name (Name (Assoc))
8232 then
8233 Set_Entity (New_N, Entity (Name (Assoc)));
8234
8235 elsif Nkind (Assoc) in N_Entity
8236 and then Expander_Active
8237 then
8238 -- Inlining case: we are copying a tree that contains
8239 -- global entities, which are preserved in the copy to be
8240 -- used for subsequent inlining.
8241
8242 null;
8243
8244 else
8245 Set_Entity (New_N, Empty);
8246 end if;
8247 end if;
8248 end;
8249 end if;
8250
8251 -- For expanded name, we must copy the Prefix and Selector_Name
8252
8253 if Nkind (N) = N_Expanded_Name then
8254 Set_Prefix
8255 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8256
8257 Set_Selector_Name (New_N,
8258 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8259
8260 -- For operators, copy the operands
8261
8262 elsif Nkind (N) in N_Op then
8263 if Nkind (N) in N_Binary_Op then
8264 Set_Left_Opnd (New_N,
8265 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8266 end if;
8267
8268 Set_Right_Opnd (New_N,
8269 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8270 end if;
8271
8272 -- Establish a link between an entity from the generic template and the
8273 -- corresponding entity in the generic copy to be analyzed.
8274
8275 elsif Nkind (N) in N_Entity then
8276 if not Instantiating then
8277 Set_Associated_Entity (N, New_N);
8278 end if;
8279
8280 -- Clear any existing link the copy may inherit from the replicated
8281 -- generic template entity.
8282
8283 Set_Associated_Entity (New_N, Empty);
8284
8285 -- Special casing for stubs
8286
8287 elsif Nkind (N) in N_Body_Stub then
8288
8289 -- In any case, we must copy the specification or defining
8290 -- identifier as appropriate.
8291
8292 if Nkind (N) = N_Subprogram_Body_Stub then
8293 Set_Specification (New_N,
8294 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8295
8296 else
8297 Set_Defining_Identifier (New_N,
8298 Copy_Generic_Node
8299 (Defining_Identifier (N), New_N, Instantiating));
8300 end if;
8301
8302 -- If we are not instantiating, then this is where we load and
8303 -- analyze subunits, i.e. at the point where the stub occurs. A
8304 -- more permissive system might defer this analysis to the point
8305 -- of instantiation, but this seems too complicated for now.
8306
8307 if not Instantiating then
8308 declare
8309 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8310 Subunit : Node_Id;
8311 Unum : Unit_Number_Type;
8312 New_Body : Node_Id;
8313
8314 begin
8315 -- Make sure that, if it is a subunit of the main unit that is
8316 -- preprocessed and if -gnateG is specified, the preprocessed
8317 -- file will be written.
8318
8319 Lib.Analysing_Subunit_Of_Main :=
8320 Lib.In_Extended_Main_Source_Unit (N);
8321 Unum :=
8322 Load_Unit
8323 (Load_Name => Subunit_Name,
8324 Required => False,
8325 Subunit => True,
8326 Error_Node => N);
8327 Lib.Analysing_Subunit_Of_Main := False;
8328
8329 -- If the proper body is not found, a warning message will be
8330 -- emitted when analyzing the stub, or later at the point of
8331 -- instantiation. Here we just leave the stub as is.
8332
8333 if Unum = No_Unit then
8334 Subunits_Missing := True;
8335 goto Subunit_Not_Found;
8336 end if;
8337
8338 Subunit := Cunit (Unum);
8339
8340 if Nkind (Unit (Subunit)) /= N_Subunit then
8341 Error_Msg_N
8342 ("found child unit instead of expected SEPARATE subunit",
8343 Subunit);
8344 Error_Msg_Sloc := Sloc (N);
8345 Error_Msg_N ("\to complete stub #", Subunit);
8346 goto Subunit_Not_Found;
8347 end if;
8348
8349 -- We must create a generic copy of the subunit, in order to
8350 -- perform semantic analysis on it, and we must replace the
8351 -- stub in the original generic unit with the subunit, in order
8352 -- to preserve non-local references within.
8353
8354 -- Only the proper body needs to be copied. Library_Unit and
8355 -- context clause are simply inherited by the generic copy.
8356 -- Note that the copy (which may be recursive if there are
8357 -- nested subunits) must be done first, before attaching it to
8358 -- the enclosing generic.
8359
8360 New_Body :=
8361 Copy_Generic_Node
8362 (Proper_Body (Unit (Subunit)),
8363 Empty, Instantiating => False);
8364
8365 -- Now place the original proper body in the original generic
8366 -- unit. This is a body, not a compilation unit.
8367
8368 Rewrite (N, Proper_Body (Unit (Subunit)));
8369 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8370 Set_Was_Originally_Stub (N);
8371
8372 -- Finally replace the body of the subunit with its copy, and
8373 -- make this new subunit into the library unit of the generic
8374 -- copy, which does not have stubs any longer.
8375
8376 Set_Proper_Body (Unit (Subunit), New_Body);
8377 Set_Library_Unit (New_N, Subunit);
8378 Inherit_Context (Unit (Subunit), N);
8379 end;
8380
8381 -- If we are instantiating, this must be an error case, since
8382 -- otherwise we would have replaced the stub node by the proper body
8383 -- that corresponds. So just ignore it in the copy (i.e. we have
8384 -- copied it, and that is good enough).
8385
8386 else
8387 null;
8388 end if;
8389
8390 <<Subunit_Not_Found>> null;
8391
8392 -- If the node is a compilation unit, it is the subunit of a stub, which
8393 -- has been loaded already (see code below). In this case, the library
8394 -- unit field of N points to the parent unit (which is a compilation
8395 -- unit) and need not (and cannot) be copied.
8396
8397 -- When the proper body of the stub is analyzed, the library_unit link
8398 -- is used to establish the proper context (see sem_ch10).
8399
8400 -- The other fields of a compilation unit are copied as usual
8401
8402 elsif Nkind (N) = N_Compilation_Unit then
8403
8404 -- This code can only be executed when not instantiating, because in
8405 -- the copy made for an instantiation, the compilation unit node has
8406 -- disappeared at the point that a stub is replaced by its proper
8407 -- body.
8408
8409 pragma Assert (not Instantiating);
8410
8411 Set_Context_Items (New_N,
8412 Copy_Generic_List (Context_Items (N), New_N));
8413
8414 Set_Unit (New_N,
8415 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8416
8417 Set_First_Inlined_Subprogram (New_N,
8418 Copy_Generic_Node
8419 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8420
8421 Set_Aux_Decls_Node
8422 (New_N,
8423 Copy_Generic_Node
8424 (Aux_Decls_Node (N), New_N, Instantiating => False));
8425
8426 -- For an assignment node, the assignment is known to be semantically
8427 -- legal if we are instantiating the template. This avoids incorrect
8428 -- diagnostics in generated code.
8429
8430 elsif Nkind (N) = N_Assignment_Statement then
8431
8432 -- Copy name and expression fields in usual manner
8433
8434 Set_Name (New_N,
8435 Copy_Generic_Node (Name (N), New_N, Instantiating));
8436
8437 Set_Expression (New_N,
8438 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8439
8440 if Instantiating then
8441 Set_Assignment_OK (Name (New_N), True);
8442 end if;
8443
8444 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8445 if not Instantiating then
8446 Set_Associated_Node (N, New_N);
8447
8448 else
8449 if Present (Get_Associated_Node (N))
8450 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8451 then
8452 -- In the generic the aggregate has some composite type. If at
8453 -- the point of instantiation the type has a private view,
8454 -- install the full view (and that of its ancestors, if any).
8455
8456 declare
8457 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8458 Rt : Entity_Id;
8459
8460 begin
8461 if Present (T) and then Is_Private_Type (T) then
8462 Switch_View (T);
8463 end if;
8464
8465 if Present (T)
8466 and then Is_Tagged_Type (T)
8467 and then Is_Derived_Type (T)
8468 then
8469 Rt := Root_Type (T);
8470
8471 loop
8472 T := Etype (T);
8473
8474 if Is_Private_Type (T) then
8475 Switch_View (T);
8476 end if;
8477
8478 exit when T = Rt;
8479 end loop;
8480 end if;
8481 end;
8482 end if;
8483 end if;
8484
8485 -- Do not copy the associated node, which points to the generic copy
8486 -- of the aggregate.
8487
8488 declare
8489 use Atree.Unchecked_Access;
8490 -- This code section is part of the implementation of an untyped
8491 -- tree traversal, so it needs direct access to node fields.
8492
8493 begin
8494 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8495 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8496 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8497 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8498 end;
8499
8500 -- Allocators do not have an identifier denoting the access type, so we
8501 -- must locate it through the expression to check whether the views are
8502 -- consistent.
8503
8504 elsif Nkind (N) = N_Allocator
8505 and then Nkind (Expression (N)) = N_Qualified_Expression
8506 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8507 and then Instantiating
8508 then
8509 declare
8510 T : constant Node_Id :=
8511 Get_Associated_Node (Subtype_Mark (Expression (N)));
8512 Acc_T : Entity_Id;
8513
8514 begin
8515 if Present (T) then
8516
8517 -- Retrieve the allocator node in the generic copy
8518
8519 Acc_T := Etype (Parent (Parent (T)));
8520
8521 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8522 Switch_View (Acc_T);
8523 end if;
8524 end if;
8525
8526 Copy_Descendants;
8527 end;
8528
8529 -- For a proper body, we must catch the case of a proper body that
8530 -- replaces a stub. This represents the point at which a separate
8531 -- compilation unit, and hence template file, may be referenced, so we
8532 -- must make a new source instantiation entry for the template of the
8533 -- subunit, and ensure that all nodes in the subunit are adjusted using
8534 -- this new source instantiation entry.
8535
8536 elsif Nkind (N) in N_Proper_Body then
8537 declare
8538 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8539 begin
8540 if Instantiating and then Was_Originally_Stub (N) then
8541 Create_Instantiation_Source
8542 (Instantiation_Node,
8543 Defining_Entity (N),
8544 S_Adjustment);
8545
8546 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8547 end if;
8548
8549 -- Now copy the fields of the proper body, using the new
8550 -- adjustment factor if one was needed as per test above.
8551
8552 Copy_Descendants;
8553
8554 -- Restore the original adjustment factor
8555
8556 S_Adjustment := Save_Adjustment;
8557 end;
8558
8559 elsif Nkind (N) = N_Pragma and then Instantiating then
8560
8561 -- Do not copy Comment or Ident pragmas their content is relevant to
8562 -- the generic unit, not to the instantiating unit.
8563
8564 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8565 New_N := Make_Null_Statement (Sloc (N));
8566
8567 -- Do not copy pragmas generated from aspects because the pragmas do
8568 -- not carry any semantic information, plus they will be regenerated
8569 -- in the instance.
8570
8571 -- However, generating C we need to copy them since postconditions
8572 -- are inlined by the front end, and the front-end inlining machinery
8573 -- relies on this routine to perform inlining.
8574
8575 elsif From_Aspect_Specification (N)
8576 and then not Modify_Tree_For_C
8577 then
8578 New_N := Make_Null_Statement (Sloc (N));
8579
8580 else
8581 Copy_Descendants;
8582 end if;
8583
8584 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8585
8586 -- No descendant fields need traversing
8587
8588 null;
8589
8590 elsif Nkind (N) = N_String_Literal
8591 and then Present (Etype (N))
8592 and then Instantiating
8593 then
8594 -- If the string is declared in an outer scope, the string_literal
8595 -- subtype created for it may have the wrong scope. Force reanalysis
8596 -- of the constant to generate a new itype in the proper context.
8597
8598 Set_Etype (New_N, Empty);
8599 Set_Analyzed (New_N, False);
8600
8601 -- For the remaining nodes, copy their descendants recursively
8602
8603 else
8604 Copy_Descendants;
8605
8606 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8607 Set_Generic_Parent (Specification (New_N), N);
8608
8609 -- Should preserve Corresponding_Spec??? (12.3(14))
8610 end if;
8611 end if;
8612
8613 -- Propagate dimensions if present, so that they are reflected in the
8614 -- instance.
8615
8616 if Nkind (N) in N_Has_Etype
8617 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8618 and then Present (Etype (N))
8619 and then Is_Floating_Point_Type (Etype (N))
8620 and then Has_Dimension_System (Etype (N))
8621 then
8622 Copy_Dimensions (N, New_N);
8623 end if;
8624
8625 return New_N;
8626 end Copy_Generic_Node;
8627
8628 ----------------------------
8629 -- Denotes_Formal_Package --
8630 ----------------------------
8631
8632 function Denotes_Formal_Package
8633 (Pack : Entity_Id;
8634 On_Exit : Boolean := False;
8635 Instance : Entity_Id := Empty) return Boolean
8636 is
8637 Par : Entity_Id;
8638 Scop : constant Entity_Id := Scope (Pack);
8639 E : Entity_Id;
8640
8641 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8642 -- The package in question may be an actual for a previous formal
8643 -- package P of the current instance, so examine its actuals as well.
8644 -- This must be recursive over other formal packages.
8645
8646 ----------------------------------
8647 -- Is_Actual_Of_Previous_Formal --
8648 ----------------------------------
8649
8650 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8651 E1 : Entity_Id;
8652
8653 begin
8654 E1 := First_Entity (P);
8655 while Present (E1) and then E1 /= Instance loop
8656 if Ekind (E1) = E_Package
8657 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8658 then
8659 if Renamed_Object (E1) = Pack then
8660 return True;
8661
8662 elsif E1 = P or else Renamed_Object (E1) = P then
8663 return False;
8664
8665 elsif Is_Actual_Of_Previous_Formal (E1) then
8666 return True;
8667 end if;
8668 end if;
8669
8670 Next_Entity (E1);
8671 end loop;
8672
8673 return False;
8674 end Is_Actual_Of_Previous_Formal;
8675
8676 -- Start of processing for Denotes_Formal_Package
8677
8678 begin
8679 if On_Exit then
8680 Par :=
8681 Instance_Envs.Table
8682 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8683 else
8684 Par := Current_Instantiated_Parent.Act_Id;
8685 end if;
8686
8687 if Ekind (Scop) = E_Generic_Package
8688 or else Nkind (Unit_Declaration_Node (Scop)) =
8689 N_Generic_Subprogram_Declaration
8690 then
8691 return True;
8692
8693 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8694 N_Formal_Package_Declaration
8695 then
8696 return True;
8697
8698 elsif No (Par) then
8699 return False;
8700
8701 else
8702 -- Check whether this package is associated with a formal package of
8703 -- the enclosing instantiation. Iterate over the list of renamings.
8704
8705 E := First_Entity (Par);
8706 while Present (E) loop
8707 if Ekind (E) /= E_Package
8708 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8709 then
8710 null;
8711
8712 elsif Renamed_Object (E) = Par then
8713 return False;
8714
8715 elsif Renamed_Object (E) = Pack then
8716 return True;
8717
8718 elsif Is_Actual_Of_Previous_Formal (E) then
8719 return True;
8720
8721 end if;
8722
8723 Next_Entity (E);
8724 end loop;
8725
8726 return False;
8727 end if;
8728 end Denotes_Formal_Package;
8729
8730 -----------------
8731 -- End_Generic --
8732 -----------------
8733
8734 procedure End_Generic is
8735 begin
8736 -- ??? More things could be factored out in this routine. Should
8737 -- probably be done at a later stage.
8738
8739 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8740 Generic_Flags.Decrement_Last;
8741
8742 Expander_Mode_Restore;
8743 end End_Generic;
8744
8745 -------------
8746 -- Earlier --
8747 -------------
8748
8749 function Earlier (N1, N2 : Node_Id) return Boolean is
8750 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8751 -- Find distance from given node to enclosing compilation unit
8752
8753 ----------------
8754 -- Find_Depth --
8755 ----------------
8756
8757 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8758 begin
8759 while Present (P)
8760 and then Nkind (P) /= N_Compilation_Unit
8761 loop
8762 P := True_Parent (P);
8763 D := D + 1;
8764 end loop;
8765 end Find_Depth;
8766
8767 -- Local declarations
8768
8769 D1 : Integer := 0;
8770 D2 : Integer := 0;
8771 P1 : Node_Id := N1;
8772 P2 : Node_Id := N2;
8773 T1 : Source_Ptr;
8774 T2 : Source_Ptr;
8775
8776 -- Start of processing for Earlier
8777
8778 begin
8779 Find_Depth (P1, D1);
8780 Find_Depth (P2, D2);
8781
8782 if P1 /= P2 then
8783 return False;
8784 else
8785 P1 := N1;
8786 P2 := N2;
8787 end if;
8788
8789 while D1 > D2 loop
8790 P1 := True_Parent (P1);
8791 D1 := D1 - 1;
8792 end loop;
8793
8794 while D2 > D1 loop
8795 P2 := True_Parent (P2);
8796 D2 := D2 - 1;
8797 end loop;
8798
8799 -- At this point P1 and P2 are at the same distance from the root.
8800 -- We examine their parents until we find a common declarative list.
8801 -- If we reach the root, N1 and N2 do not descend from the same
8802 -- declarative list (e.g. one is nested in the declarative part and
8803 -- the other is in a block in the statement part) and the earlier
8804 -- one is already frozen.
8805
8806 while not Is_List_Member (P1)
8807 or else not Is_List_Member (P2)
8808 or else List_Containing (P1) /= List_Containing (P2)
8809 loop
8810 P1 := True_Parent (P1);
8811 P2 := True_Parent (P2);
8812
8813 if Nkind (Parent (P1)) = N_Subunit then
8814 P1 := Corresponding_Stub (Parent (P1));
8815 end if;
8816
8817 if Nkind (Parent (P2)) = N_Subunit then
8818 P2 := Corresponding_Stub (Parent (P2));
8819 end if;
8820
8821 if P1 = P2 then
8822 return False;
8823 end if;
8824 end loop;
8825
8826 -- Expanded code usually shares the source location of the original
8827 -- construct it was generated for. This however may not necessarily
8828 -- reflect the true location of the code within the tree.
8829
8830 -- Before comparing the slocs of the two nodes, make sure that we are
8831 -- working with correct source locations. Assume that P1 is to the left
8832 -- of P2. If either one does not come from source, traverse the common
8833 -- list heading towards the other node and locate the first source
8834 -- statement.
8835
8836 -- P1 P2
8837 -- ----+===+===+--------------+===+===+----
8838 -- expanded code expanded code
8839
8840 if not Comes_From_Source (P1) then
8841 while Present (P1) loop
8842
8843 -- Neither P2 nor a source statement were located during the
8844 -- search. If we reach the end of the list, then P1 does not
8845 -- occur earlier than P2.
8846
8847 -- ---->
8848 -- start --- P2 ----- P1 --- end
8849
8850 if No (Next (P1)) then
8851 return False;
8852
8853 -- We encounter P2 while going to the right of the list. This
8854 -- means that P1 does indeed appear earlier.
8855
8856 -- ---->
8857 -- start --- P1 ===== P2 --- end
8858 -- expanded code in between
8859
8860 elsif P1 = P2 then
8861 return True;
8862
8863 -- No need to look any further since we have located a source
8864 -- statement.
8865
8866 elsif Comes_From_Source (P1) then
8867 exit;
8868 end if;
8869
8870 -- Keep going right
8871
8872 Next (P1);
8873 end loop;
8874 end if;
8875
8876 if not Comes_From_Source (P2) then
8877 while Present (P2) loop
8878
8879 -- Neither P1 nor a source statement were located during the
8880 -- search. If we reach the start of the list, then P1 does not
8881 -- occur earlier than P2.
8882
8883 -- <----
8884 -- start --- P2 --- P1 --- end
8885
8886 if No (Prev (P2)) then
8887 return False;
8888
8889 -- We encounter P1 while going to the left of the list. This
8890 -- means that P1 does indeed appear earlier.
8891
8892 -- <----
8893 -- start --- P1 ===== P2 --- end
8894 -- expanded code in between
8895
8896 elsif P2 = P1 then
8897 return True;
8898
8899 -- No need to look any further since we have located a source
8900 -- statement.
8901
8902 elsif Comes_From_Source (P2) then
8903 exit;
8904 end if;
8905
8906 -- Keep going left
8907
8908 Prev (P2);
8909 end loop;
8910 end if;
8911
8912 -- At this point either both nodes came from source or we approximated
8913 -- their source locations through neighboring source statements.
8914
8915 T1 := Top_Level_Location (Sloc (P1));
8916 T2 := Top_Level_Location (Sloc (P2));
8917
8918 -- When two nodes come from the same instance, they have identical top
8919 -- level locations. To determine proper relation within the tree, check
8920 -- their locations within the template.
8921
8922 if T1 = T2 then
8923 return Sloc (P1) < Sloc (P2);
8924
8925 -- The two nodes either come from unrelated instances or do not come
8926 -- from instantiated code at all.
8927
8928 else
8929 return T1 < T2;
8930 end if;
8931 end Earlier;
8932
8933 ----------------------
8934 -- Find_Actual_Type --
8935 ----------------------
8936
8937 function Find_Actual_Type
8938 (Typ : Entity_Id;
8939 Gen_Type : Entity_Id) return Entity_Id
8940 is
8941 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8942 T : Entity_Id;
8943
8944 begin
8945 -- Special processing only applies to child units
8946
8947 if not Is_Child_Unit (Gen_Scope) then
8948 return Get_Instance_Of (Typ);
8949
8950 -- If designated or component type is itself a formal of the child unit,
8951 -- its instance is available.
8952
8953 elsif Scope (Typ) = Gen_Scope then
8954 return Get_Instance_Of (Typ);
8955
8956 -- If the array or access type is not declared in the parent unit,
8957 -- no special processing needed.
8958
8959 elsif not Is_Generic_Type (Typ)
8960 and then Scope (Gen_Scope) /= Scope (Typ)
8961 then
8962 return Get_Instance_Of (Typ);
8963
8964 -- Otherwise, retrieve designated or component type by visibility
8965
8966 else
8967 T := Current_Entity (Typ);
8968 while Present (T) loop
8969 if In_Open_Scopes (Scope (T)) then
8970 return T;
8971 elsif Is_Generic_Actual_Type (T) then
8972 return T;
8973 end if;
8974
8975 T := Homonym (T);
8976 end loop;
8977
8978 return Typ;
8979 end if;
8980 end Find_Actual_Type;
8981
8982 ----------------------------
8983 -- Freeze_Subprogram_Body --
8984 ----------------------------
8985
8986 procedure Freeze_Subprogram_Body
8987 (Inst_Node : Node_Id;
8988 Gen_Body : Node_Id;
8989 Pack_Id : Entity_Id)
8990 is
8991 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8992 Par : constant Entity_Id := Scope (Gen_Unit);
8993 Enc_G : Entity_Id;
8994 Enc_G_F : Node_Id;
8995 Enc_I : Node_Id;
8996 F_Node : Node_Id;
8997
8998 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8999 -- Find innermost package body that encloses the given node, and which
9000 -- is not a compilation unit. Freeze nodes for the instance, or for its
9001 -- enclosing body, may be inserted after the enclosing_body of the
9002 -- generic unit. Used to determine proper placement of freeze node for
9003 -- both package and subprogram instances.
9004
9005 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9006 -- Find entity for given package body, and locate or create a freeze
9007 -- node for it.
9008
9009 ----------------------------
9010 -- Enclosing_Package_Body --
9011 ----------------------------
9012
9013 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9014 P : Node_Id;
9015
9016 begin
9017 P := Parent (N);
9018 while Present (P)
9019 and then Nkind (Parent (P)) /= N_Compilation_Unit
9020 loop
9021 if Nkind (P) = N_Package_Body then
9022 if Nkind (Parent (P)) = N_Subunit then
9023 return Corresponding_Stub (Parent (P));
9024 else
9025 return P;
9026 end if;
9027 end if;
9028
9029 P := True_Parent (P);
9030 end loop;
9031
9032 return Empty;
9033 end Enclosing_Package_Body;
9034
9035 -------------------------
9036 -- Package_Freeze_Node --
9037 -------------------------
9038
9039 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9040 Id : Entity_Id;
9041
9042 begin
9043 if Nkind (B) = N_Package_Body then
9044 Id := Corresponding_Spec (B);
9045 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9046 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9047 end if;
9048
9049 Ensure_Freeze_Node (Id);
9050 return Freeze_Node (Id);
9051 end Package_Freeze_Node;
9052
9053 -- Start of processing for Freeze_Subprogram_Body
9054
9055 begin
9056 -- If the instance and the generic body appear within the same unit, and
9057 -- the instance precedes the generic, the freeze node for the instance
9058 -- must appear after that of the generic. If the generic is nested
9059 -- within another instance I2, then current instance must be frozen
9060 -- after I2. In both cases, the freeze nodes are those of enclosing
9061 -- packages. Otherwise, the freeze node is placed at the end of the
9062 -- current declarative part.
9063
9064 Enc_G := Enclosing_Package_Body (Gen_Body);
9065 Enc_I := Enclosing_Package_Body (Inst_Node);
9066 Ensure_Freeze_Node (Pack_Id);
9067 F_Node := Freeze_Node (Pack_Id);
9068
9069 if Is_Generic_Instance (Par)
9070 and then Present (Freeze_Node (Par))
9071 and then In_Same_Declarative_Part
9072 (Parent (Freeze_Node (Par)), Inst_Node)
9073 then
9074 -- The parent was a premature instantiation. Insert freeze node at
9075 -- the end the current declarative part.
9076
9077 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
9078 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9079
9080 -- Handle the following case:
9081 --
9082 -- package Parent_Inst is new ...
9083 -- Parent_Inst []
9084 --
9085 -- procedure P ... -- this body freezes Parent_Inst
9086 --
9087 -- package Inst is new ...
9088 --
9089 -- In this particular scenario, the freeze node for Inst must be
9090 -- inserted in the same manner as that of Parent_Inst - before the
9091 -- next source body or at the end of the declarative list (body not
9092 -- available). If body P did not exist and Parent_Inst was frozen
9093 -- after Inst, either by a body following Inst or at the end of the
9094 -- declarative region, the freeze node for Inst must be inserted
9095 -- after that of Parent_Inst. This relation is established by
9096 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9097
9098 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
9099 List_Containing (Inst_Node)
9100 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
9101 then
9102 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9103
9104 else
9105 Insert_After (Freeze_Node (Par), F_Node);
9106 end if;
9107
9108 -- The body enclosing the instance should be frozen after the body that
9109 -- includes the generic, because the body of the instance may make
9110 -- references to entities therein. If the two are not in the same
9111 -- declarative part, or if the one enclosing the instance is frozen
9112 -- already, freeze the instance at the end of the current declarative
9113 -- part.
9114
9115 elsif Is_Generic_Instance (Par)
9116 and then Present (Freeze_Node (Par))
9117 and then Present (Enc_I)
9118 then
9119 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I) then
9120 -- The enclosing package may contain several instances. Rather
9121 -- than computing the earliest point at which to insert its freeze
9122 -- node, we place it at the end of the declarative part of the
9123 -- parent of the generic.
9124
9125 Insert_Freeze_Node_For_Instance
9126 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
9127 end if;
9128
9129 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9130
9131 elsif Present (Enc_G)
9132 and then Present (Enc_I)
9133 and then Enc_G /= Enc_I
9134 and then Earlier (Inst_Node, Gen_Body)
9135 then
9136 -- Freeze package that encloses instance, and place node after the
9137 -- package that encloses generic. If enclosing package is already
9138 -- frozen we have to assume it is at the proper place. This may be a
9139 -- potential ABE that requires dynamic checking. Do not add a freeze
9140 -- node if the package that encloses the generic is inside the body
9141 -- that encloses the instance, because the freeze node would be in
9142 -- the wrong scope. Additional contortions needed if the bodies are
9143 -- within a subunit.
9144
9145 declare
9146 Enclosing_Body : Node_Id;
9147
9148 begin
9149 if Nkind (Enc_I) = N_Package_Body_Stub then
9150 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
9151 else
9152 Enclosing_Body := Enc_I;
9153 end if;
9154
9155 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9156 Insert_Freeze_Node_For_Instance
9157 (Enc_G, Package_Freeze_Node (Enc_I));
9158 end if;
9159 end;
9160
9161 -- Freeze enclosing subunit before instance
9162
9163 Enc_G_F := Package_Freeze_Node (Enc_G);
9164
9165 if not Is_List_Member (Enc_G_F) then
9166 Insert_After (Enc_G, Enc_G_F);
9167 end if;
9168
9169 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9170
9171 else
9172 -- If none of the above, insert freeze node at the end of the current
9173 -- declarative part.
9174
9175 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9176 end if;
9177 end Freeze_Subprogram_Body;
9178
9179 ----------------
9180 -- Get_Gen_Id --
9181 ----------------
9182
9183 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9184 begin
9185 return Generic_Renamings.Table (E).Gen_Id;
9186 end Get_Gen_Id;
9187
9188 ---------------------
9189 -- Get_Instance_Of --
9190 ---------------------
9191
9192 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9193 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9194
9195 begin
9196 if Res /= Assoc_Null then
9197 return Generic_Renamings.Table (Res).Act_Id;
9198
9199 else
9200 -- On exit, entity is not instantiated: not a generic parameter, or
9201 -- else parameter of an inner generic unit.
9202
9203 return A;
9204 end if;
9205 end Get_Instance_Of;
9206
9207 ---------------------------------
9208 -- Get_Unit_Instantiation_Node --
9209 ---------------------------------
9210
9211 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9212 Decl : Node_Id := Unit_Declaration_Node (A);
9213 Inst : Node_Id;
9214
9215 begin
9216 -- If the Package_Instantiation attribute has been set on the package
9217 -- entity, then use it directly when it (or its Original_Node) refers
9218 -- to an N_Package_Instantiation node. In principle it should be
9219 -- possible to have this field set in all cases, which should be
9220 -- investigated, and would allow this function to be significantly
9221 -- simplified. ???
9222
9223 Inst := Package_Instantiation (A);
9224
9225 if Present (Inst) then
9226 if Nkind (Inst) = N_Package_Instantiation then
9227 return Inst;
9228
9229 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9230 return Original_Node (Inst);
9231 end if;
9232 end if;
9233
9234 -- If the instantiation is a compilation unit that does not need body
9235 -- then the instantiation node has been rewritten as a package
9236 -- declaration for the instance, and we return the original node.
9237
9238 -- If it is a compilation unit and the instance node has not been
9239 -- rewritten, then it is still the unit of the compilation. Finally, if
9240 -- a body is present, this is a parent of the main unit whose body has
9241 -- been compiled for inlining purposes, and the instantiation node has
9242 -- been rewritten with the instance body.
9243
9244 -- Otherwise the instantiation node appears after the declaration. If
9245 -- the entity is a formal package, the declaration may have been
9246 -- rewritten as a generic declaration (in the case of a formal with box)
9247 -- or left as a formal package declaration if it has actuals, and is
9248 -- found with a forward search.
9249
9250 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9251 if Nkind (Decl) = N_Package_Declaration
9252 and then Present (Corresponding_Body (Decl))
9253 then
9254 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9255 end if;
9256
9257 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9258 return Original_Node (Decl);
9259 else
9260 return Unit (Parent (Decl));
9261 end if;
9262
9263 elsif Nkind (Decl) = N_Package_Declaration
9264 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9265 then
9266 return Original_Node (Decl);
9267
9268 else
9269 Inst := Next (Decl);
9270 while Nkind (Inst) not in N_Formal_Package_Declaration
9271 | N_Function_Instantiation
9272 | N_Package_Instantiation
9273 | N_Procedure_Instantiation
9274 loop
9275 Next (Inst);
9276 end loop;
9277
9278 return Inst;
9279 end if;
9280 end Get_Unit_Instantiation_Node;
9281
9282 ------------------------
9283 -- Has_Been_Exchanged --
9284 ------------------------
9285
9286 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9287 Next : Elmt_Id;
9288
9289 begin
9290 Next := First_Elmt (Exchanged_Views);
9291 while Present (Next) loop
9292 if Full_View (Node (Next)) = E then
9293 return True;
9294 end if;
9295
9296 Next_Elmt (Next);
9297 end loop;
9298
9299 return False;
9300 end Has_Been_Exchanged;
9301
9302 -------------------
9303 -- Has_Contracts --
9304 -------------------
9305
9306 function Has_Contracts (Decl : Node_Id) return Boolean is
9307 A_List : constant List_Id := Aspect_Specifications (Decl);
9308 A_Spec : Node_Id;
9309 A_Id : Aspect_Id;
9310 begin
9311 if No (A_List) then
9312 return False;
9313 else
9314 A_Spec := First (A_List);
9315 while Present (A_Spec) loop
9316 A_Id := Get_Aspect_Id (A_Spec);
9317 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9318 return True;
9319 end if;
9320
9321 Next (A_Spec);
9322 end loop;
9323
9324 return False;
9325 end if;
9326 end Has_Contracts;
9327
9328 ----------
9329 -- Hash --
9330 ----------
9331
9332 function Hash (F : Entity_Id) return HTable_Range is
9333 begin
9334 return HTable_Range (F mod HTable_Size);
9335 end Hash;
9336
9337 ------------------------
9338 -- Hide_Current_Scope --
9339 ------------------------
9340
9341 procedure Hide_Current_Scope is
9342 C : constant Entity_Id := Current_Scope;
9343 E : Entity_Id;
9344
9345 begin
9346 Set_Is_Hidden_Open_Scope (C);
9347
9348 E := First_Entity (C);
9349 while Present (E) loop
9350 if Is_Immediately_Visible (E) then
9351 Set_Is_Immediately_Visible (E, False);
9352 Append_Elmt (E, Hidden_Entities);
9353 end if;
9354
9355 Next_Entity (E);
9356 end loop;
9357
9358 -- Make the scope name invisible as well. This is necessary, but might
9359 -- conflict with calls to Rtsfind later on, in case the scope is a
9360 -- predefined one. There is no clean solution to this problem, so for
9361 -- now we depend on the user not redefining Standard itself in one of
9362 -- the parent units.
9363
9364 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9365 Set_Is_Immediately_Visible (C, False);
9366 Append_Elmt (C, Hidden_Entities);
9367 end if;
9368
9369 end Hide_Current_Scope;
9370
9371 --------------
9372 -- Init_Env --
9373 --------------
9374
9375 procedure Init_Env is
9376 Saved : Instance_Env;
9377
9378 begin
9379 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9380 Saved.Exchanged_Views := Exchanged_Views;
9381 Saved.Hidden_Entities := Hidden_Entities;
9382 Saved.Current_Sem_Unit := Current_Sem_Unit;
9383 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9384 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9385
9386 -- Save configuration switches. These may be reset if the unit is a
9387 -- predefined unit, and the current mode is not Ada 2005.
9388
9389 Saved.Switches := Save_Config_Switches;
9390
9391 Instance_Envs.Append (Saved);
9392
9393 Exchanged_Views := New_Elmt_List;
9394 Hidden_Entities := New_Elmt_List;
9395
9396 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9397 -- this is set properly in Set_Instance_Env.
9398
9399 Current_Instantiated_Parent :=
9400 (Current_Scope, Current_Scope, Assoc_Null);
9401 end Init_Env;
9402
9403 ---------------------
9404 -- In_Main_Context --
9405 ---------------------
9406
9407 function In_Main_Context (E : Entity_Id) return Boolean is
9408 Context : List_Id;
9409 Clause : Node_Id;
9410 Nam : Node_Id;
9411
9412 begin
9413 if not Is_Compilation_Unit (E)
9414 or else Ekind (E) /= E_Package
9415 or else In_Private_Part (E)
9416 then
9417 return False;
9418 end if;
9419
9420 Context := Context_Items (Cunit (Main_Unit));
9421
9422 Clause := First (Context);
9423 while Present (Clause) loop
9424 if Nkind (Clause) = N_With_Clause then
9425 Nam := Name (Clause);
9426
9427 -- If the current scope is part of the context of the main unit,
9428 -- analysis of the corresponding with_clause is not complete, and
9429 -- the entity is not set. We use the Chars field directly, which
9430 -- might produce false positives in rare cases, but guarantees
9431 -- that we produce all the instance bodies we will need.
9432
9433 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9434 or else (Nkind (Nam) = N_Selected_Component
9435 and then Chars (Selector_Name (Nam)) = Chars (E))
9436 then
9437 return True;
9438 end if;
9439 end if;
9440
9441 Next (Clause);
9442 end loop;
9443
9444 return False;
9445 end In_Main_Context;
9446
9447 ---------------------
9448 -- Inherit_Context --
9449 ---------------------
9450
9451 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9452 Current_Context : List_Id;
9453 Current_Unit : Node_Id;
9454 Item : Node_Id;
9455 New_I : Node_Id;
9456
9457 Clause : Node_Id;
9458 OK : Boolean;
9459 Lib_Unit : Node_Id;
9460
9461 begin
9462 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9463
9464 -- The inherited context is attached to the enclosing compilation
9465 -- unit. This is either the main unit, or the declaration for the
9466 -- main unit (in case the instantiation appears within the package
9467 -- declaration and the main unit is its body).
9468
9469 Current_Unit := Parent (Inst);
9470 while Present (Current_Unit)
9471 and then Nkind (Current_Unit) /= N_Compilation_Unit
9472 loop
9473 Current_Unit := Parent (Current_Unit);
9474 end loop;
9475
9476 Current_Context := Context_Items (Current_Unit);
9477
9478 Item := First (Context_Items (Parent (Gen_Decl)));
9479 while Present (Item) loop
9480 if Nkind (Item) = N_With_Clause then
9481 Lib_Unit := Library_Unit (Item);
9482
9483 -- Take care to prevent direct cyclic with's
9484
9485 if Lib_Unit /= Current_Unit then
9486
9487 -- Do not add a unit if it is already in the context
9488
9489 Clause := First (Current_Context);
9490 OK := True;
9491 while Present (Clause) loop
9492 if Nkind (Clause) = N_With_Clause
9493 and then Library_Unit (Clause) = Lib_Unit
9494 then
9495 OK := False;
9496 exit;
9497 end if;
9498
9499 Next (Clause);
9500 end loop;
9501
9502 if OK then
9503 New_I := New_Copy (Item);
9504 Set_Implicit_With (New_I);
9505
9506 Append (New_I, Current_Context);
9507 end if;
9508 end if;
9509 end if;
9510
9511 Next (Item);
9512 end loop;
9513 end if;
9514 end Inherit_Context;
9515
9516 ----------------
9517 -- Initialize --
9518 ----------------
9519
9520 procedure Initialize is
9521 begin
9522 Generic_Renamings.Init;
9523 Instance_Envs.Init;
9524 Generic_Flags.Init;
9525 Generic_Renamings_HTable.Reset;
9526 Circularity_Detected := False;
9527 Exchanged_Views := No_Elist;
9528 Hidden_Entities := No_Elist;
9529 end Initialize;
9530
9531 -------------------------------------
9532 -- Insert_Freeze_Node_For_Instance --
9533 -------------------------------------
9534
9535 procedure Insert_Freeze_Node_For_Instance
9536 (N : Node_Id;
9537 F_Node : Node_Id)
9538 is
9539 Decl : Node_Id;
9540 Decls : List_Id;
9541 Inst : Entity_Id;
9542 Par_N : Node_Id;
9543
9544 function Enclosing_Body (N : Node_Id) return Node_Id;
9545 -- Find enclosing package or subprogram body, if any. Freeze node may
9546 -- be placed at end of current declarative list if previous instance
9547 -- and current one have different enclosing bodies.
9548
9549 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9550 -- Find the local instance, if any, that declares the generic that is
9551 -- being instantiated. If present, the freeze node for this instance
9552 -- must follow the freeze node for the previous instance.
9553
9554 --------------------
9555 -- Enclosing_Body --
9556 --------------------
9557
9558 function Enclosing_Body (N : Node_Id) return Node_Id is
9559 P : Node_Id;
9560
9561 begin
9562 P := Parent (N);
9563 while Present (P)
9564 and then Nkind (Parent (P)) /= N_Compilation_Unit
9565 loop
9566 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9567 if Nkind (Parent (P)) = N_Subunit then
9568 return Corresponding_Stub (Parent (P));
9569 else
9570 return P;
9571 end if;
9572 end if;
9573
9574 P := True_Parent (P);
9575 end loop;
9576
9577 return Empty;
9578 end Enclosing_Body;
9579
9580 -----------------------
9581 -- Previous_Instance --
9582 -----------------------
9583
9584 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9585 S : Entity_Id;
9586
9587 begin
9588 S := Scope (Gen);
9589 while Present (S) and then S /= Standard_Standard loop
9590 if Is_Generic_Instance (S)
9591 and then In_Same_Source_Unit (S, N)
9592 then
9593 return S;
9594 end if;
9595
9596 S := Scope (S);
9597 end loop;
9598
9599 return Empty;
9600 end Previous_Instance;
9601
9602 -- Start of processing for Insert_Freeze_Node_For_Instance
9603
9604 begin
9605 if not Is_List_Member (F_Node) then
9606 Decl := N;
9607 Decls := List_Containing (N);
9608 Inst := Entity (F_Node);
9609 Par_N := Parent (Decls);
9610
9611 -- When processing a subprogram instantiation, utilize the actual
9612 -- subprogram instantiation rather than its package wrapper as it
9613 -- carries all the context information.
9614
9615 if Is_Wrapper_Package (Inst) then
9616 Inst := Related_Instance (Inst);
9617 end if;
9618
9619 -- If this is a package instance, check whether the generic is
9620 -- declared in a previous instance and the current instance is
9621 -- not within the previous one.
9622
9623 if Present (Generic_Parent (Parent (Inst)))
9624 and then Is_In_Main_Unit (N)
9625 then
9626 declare
9627 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9628 Par_I : constant Entity_Id :=
9629 Previous_Instance
9630 (Generic_Parent (Parent (Inst)));
9631 Scop : Entity_Id;
9632
9633 begin
9634 if Present (Par_I)
9635 and then Earlier (N, Freeze_Node (Par_I))
9636 then
9637 Scop := Scope (Inst);
9638
9639 -- If the current instance is within the one that contains
9640 -- the generic, the freeze node for the current one must
9641 -- appear in the current declarative part. Ditto, if the
9642 -- current instance is within another package instance or
9643 -- within a body that does not enclose the current instance.
9644 -- In these three cases the freeze node of the previous
9645 -- instance is not relevant.
9646
9647 while Present (Scop) and then Scop /= Standard_Standard loop
9648 exit when Scop = Par_I
9649 or else
9650 (Is_Generic_Instance (Scop)
9651 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9652 Scop := Scope (Scop);
9653 end loop;
9654
9655 -- Previous instance encloses current instance
9656
9657 if Scop = Par_I then
9658 null;
9659
9660 -- If the next node is a source body we must freeze in
9661 -- the current scope as well.
9662
9663 elsif Present (Next (N))
9664 and then Nkind (Next (N)) in N_Subprogram_Body
9665 | N_Package_Body
9666 and then Comes_From_Source (Next (N))
9667 then
9668 null;
9669
9670 -- Current instance is within an unrelated instance
9671
9672 elsif Is_Generic_Instance (Scop) then
9673 null;
9674
9675 -- Current instance is within an unrelated body
9676
9677 elsif Present (Enclosing_N)
9678 and then Enclosing_N /= Enclosing_Body (Par_I)
9679 then
9680 null;
9681
9682 else
9683 Insert_After (Freeze_Node (Par_I), F_Node);
9684 return;
9685 end if;
9686 end if;
9687 end;
9688 end if;
9689
9690 -- When the instantiation occurs in a package declaration, append the
9691 -- freeze node to the private declarations (if any).
9692
9693 if Nkind (Par_N) = N_Package_Specification
9694 and then Decls = Visible_Declarations (Par_N)
9695 and then Present (Private_Declarations (Par_N))
9696 and then not Is_Empty_List (Private_Declarations (Par_N))
9697 then
9698 Decls := Private_Declarations (Par_N);
9699 Decl := First (Decls);
9700 end if;
9701
9702 -- Determine the proper freeze point of a package instantiation. We
9703 -- adhere to the general rule of a package or subprogram body causing
9704 -- freezing of anything before it in the same declarative region. In
9705 -- this case, the proper freeze point of a package instantiation is
9706 -- before the first source body which follows, or before a stub. This
9707 -- ensures that entities coming from the instance are already frozen
9708 -- and usable in source bodies.
9709
9710 if Nkind (Par_N) /= N_Package_Declaration
9711 and then Ekind (Inst) = E_Package
9712 and then Is_Generic_Instance (Inst)
9713 and then
9714 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9715 then
9716 while Present (Decl) loop
9717 if (Nkind (Decl) in N_Unit_Body
9718 or else
9719 Nkind (Decl) in N_Body_Stub)
9720 and then Comes_From_Source (Decl)
9721 then
9722 Insert_Before (Decl, F_Node);
9723 return;
9724 end if;
9725
9726 Next (Decl);
9727 end loop;
9728 end if;
9729
9730 -- In a package declaration, or if no previous body, insert at end
9731 -- of list.
9732
9733 Set_Sloc (F_Node, Sloc (Last (Decls)));
9734 Insert_After (Last (Decls), F_Node);
9735 end if;
9736 end Insert_Freeze_Node_For_Instance;
9737
9738 ------------------
9739 -- Install_Body --
9740 ------------------
9741
9742 procedure Install_Body
9743 (Act_Body : Node_Id;
9744 N : Node_Id;
9745 Gen_Body : Node_Id;
9746 Gen_Decl : Node_Id)
9747 is
9748 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9749 -- Check if the generic definition and the instantiation come from
9750 -- a common scope, in which case the instance must be frozen after
9751 -- the generic body.
9752
9753 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9754 -- If the instance is nested inside a generic unit, the Sloc of the
9755 -- instance indicates the place of the original definition, not the
9756 -- point of the current enclosing instance. Pending a better usage of
9757 -- Slocs to indicate instantiation places, we determine the place of
9758 -- origin of a node by finding the maximum sloc of any ancestor node.
9759 -- Why is this not equivalent to Top_Level_Location ???
9760
9761 -------------------
9762 -- In_Same_Scope --
9763 -------------------
9764
9765 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9766 Act_Scop : Entity_Id := Scope (Act_Id);
9767 Gen_Scop : Entity_Id := Scope (Gen_Id);
9768
9769 begin
9770 while Act_Scop /= Standard_Standard
9771 and then Gen_Scop /= Standard_Standard
9772 loop
9773 if Act_Scop = Gen_Scop then
9774 return True;
9775 end if;
9776
9777 Act_Scop := Scope (Act_Scop);
9778 Gen_Scop := Scope (Gen_Scop);
9779 end loop;
9780
9781 return False;
9782 end In_Same_Scope;
9783
9784 ---------------
9785 -- True_Sloc --
9786 ---------------
9787
9788 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9789 N1 : Node_Id;
9790 Res : Source_Ptr;
9791
9792 begin
9793 Res := Sloc (N);
9794 N1 := N;
9795 while Present (N1) and then N1 /= Act_Unit loop
9796 if Sloc (N1) > Res then
9797 Res := Sloc (N1);
9798 end if;
9799
9800 N1 := Parent (N1);
9801 end loop;
9802
9803 return Res;
9804 end True_Sloc;
9805
9806 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9807 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9808 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9809 Par : constant Entity_Id := Scope (Gen_Id);
9810 Gen_Unit : constant Node_Id :=
9811 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9812
9813 Body_Unit : Node_Id;
9814 F_Node : Node_Id;
9815 Must_Delay : Boolean;
9816 Orig_Body : Node_Id := Gen_Body;
9817
9818 -- Start of processing for Install_Body
9819
9820 begin
9821 -- Handle first the case of an instance with incomplete actual types.
9822 -- The instance body cannot be placed after the declaration because
9823 -- full views have not been seen yet. Any use of the non-limited views
9824 -- in the instance body requires the presence of a regular with_clause
9825 -- in the enclosing unit, and will fail if this with_clause is missing.
9826 -- We place the instance body at the beginning of the enclosing body,
9827 -- which is the unit being compiled. The freeze node for the instance
9828 -- is then placed after the instance body.
9829
9830 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9831 and then Expander_Active
9832 and then Ekind (Scope (Act_Id)) = E_Package
9833 then
9834 declare
9835 Scop : constant Entity_Id := Scope (Act_Id);
9836 Body_Id : constant Node_Id :=
9837 Corresponding_Body (Unit_Declaration_Node (Scop));
9838
9839 begin
9840 Ensure_Freeze_Node (Act_Id);
9841 F_Node := Freeze_Node (Act_Id);
9842 if Present (Body_Id) then
9843 Set_Is_Frozen (Act_Id, False);
9844 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9845 if Is_List_Member (F_Node) then
9846 Remove (F_Node);
9847 end if;
9848
9849 Insert_After (Act_Body, F_Node);
9850 end if;
9851 end;
9852 return;
9853 end if;
9854
9855 -- If the body is a subunit, the freeze point is the corresponding stub
9856 -- in the current compilation, not the subunit itself.
9857
9858 if Nkind (Parent (Gen_Body)) = N_Subunit then
9859 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9860 else
9861 Orig_Body := Gen_Body;
9862 end if;
9863
9864 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9865
9866 -- If the instantiation and the generic definition appear in the same
9867 -- package declaration, this is an early instantiation. If they appear
9868 -- in the same declarative part, it is an early instantiation only if
9869 -- the generic body appears textually later, and the generic body is
9870 -- also in the main unit.
9871
9872 -- If instance is nested within a subprogram, and the generic body
9873 -- is not, the instance is delayed because the enclosing body is. If
9874 -- instance and body are within the same scope, or the same subprogram
9875 -- body, indicate explicitly that the instance is delayed.
9876
9877 Must_Delay :=
9878 (Gen_Unit = Act_Unit
9879 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9880 | N_Package_Declaration
9881 or else (Gen_Unit = Body_Unit
9882 and then True_Sloc (N, Act_Unit) <
9883 Sloc (Orig_Body)))
9884 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9885 and then In_Same_Scope (Gen_Id, Act_Id));
9886
9887 -- If this is an early instantiation, the freeze node is placed after
9888 -- the generic body. Otherwise, if the generic appears in an instance,
9889 -- we cannot freeze the current instance until the outer one is frozen.
9890 -- This is only relevant if the current instance is nested within some
9891 -- inner scope not itself within the outer instance. If this scope is
9892 -- a package body in the same declarative part as the outer instance,
9893 -- then that body needs to be frozen after the outer instance. Finally,
9894 -- if no delay is needed, we place the freeze node at the end of the
9895 -- current declarative part.
9896
9897 if Expander_Active
9898 and then (No (Freeze_Node (Act_Id))
9899 or else not Is_List_Member (Freeze_Node (Act_Id)))
9900 then
9901 Ensure_Freeze_Node (Act_Id);
9902 F_Node := Freeze_Node (Act_Id);
9903
9904 if Must_Delay then
9905 Insert_After (Orig_Body, F_Node);
9906
9907 elsif Is_Generic_Instance (Par)
9908 and then Present (Freeze_Node (Par))
9909 and then Scope (Act_Id) /= Par
9910 then
9911 -- Freeze instance of inner generic after instance of enclosing
9912 -- generic.
9913
9914 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9915
9916 -- Handle the following case:
9917
9918 -- package Parent_Inst is new ...
9919 -- Parent_Inst []
9920
9921 -- procedure P ... -- this body freezes Parent_Inst
9922
9923 -- package Inst is new ...
9924
9925 -- In this particular scenario, the freeze node for Inst must
9926 -- be inserted in the same manner as that of Parent_Inst,
9927 -- before the next source body or at the end of the declarative
9928 -- list (body not available). If body P did not exist and
9929 -- Parent_Inst was frozen after Inst, either by a body
9930 -- following Inst or at the end of the declarative region,
9931 -- the freeze node for Inst must be inserted after that of
9932 -- Parent_Inst. This relation is established by comparing
9933 -- the Slocs of Parent_Inst freeze node and Inst.
9934 -- We examine the parents of the enclosing lists to handle
9935 -- the case where the parent instance is in the visible part
9936 -- of a package declaration, and the inner instance is in
9937 -- the corresponding private part.
9938
9939 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9940 = Parent (List_Containing (N))
9941 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9942 then
9943 Insert_Freeze_Node_For_Instance (N, F_Node);
9944 else
9945 Insert_After (Freeze_Node (Par), F_Node);
9946 end if;
9947
9948 -- Freeze package enclosing instance of inner generic after
9949 -- instance of enclosing generic.
9950
9951 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9952 and then In_Same_Declarative_Part
9953 (Parent (Freeze_Node (Par)), Parent (N))
9954 then
9955 declare
9956 Enclosing : Entity_Id;
9957
9958 begin
9959 Enclosing := Corresponding_Spec (Parent (N));
9960
9961 if No (Enclosing) then
9962 Enclosing := Defining_Entity (Parent (N));
9963 end if;
9964
9965 Insert_Freeze_Node_For_Instance (N, F_Node);
9966 Ensure_Freeze_Node (Enclosing);
9967
9968 if not Is_List_Member (Freeze_Node (Enclosing)) then
9969
9970 -- The enclosing context is a subunit, insert the freeze
9971 -- node after the stub.
9972
9973 if Nkind (Parent (Parent (N))) = N_Subunit then
9974 Insert_Freeze_Node_For_Instance
9975 (Corresponding_Stub (Parent (Parent (N))),
9976 Freeze_Node (Enclosing));
9977
9978 -- The enclosing context is a package with a stub body
9979 -- which has already been replaced by the real body.
9980 -- Insert the freeze node after the actual body.
9981
9982 elsif Ekind (Enclosing) = E_Package
9983 and then Present (Body_Entity (Enclosing))
9984 and then Was_Originally_Stub
9985 (Parent (Body_Entity (Enclosing)))
9986 then
9987 Insert_Freeze_Node_For_Instance
9988 (Parent (Body_Entity (Enclosing)),
9989 Freeze_Node (Enclosing));
9990
9991 -- The parent instance has been frozen before the body of
9992 -- the enclosing package, insert the freeze node after
9993 -- the body.
9994
9995 elsif List_Containing (Freeze_Node (Par)) =
9996 List_Containing (Parent (N))
9997 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9998 then
9999 Insert_Freeze_Node_For_Instance
10000 (Parent (N), Freeze_Node (Enclosing));
10001
10002 else
10003 Insert_After
10004 (Freeze_Node (Par), Freeze_Node (Enclosing));
10005 end if;
10006 end if;
10007 end;
10008
10009 else
10010 Insert_Freeze_Node_For_Instance (N, F_Node);
10011 end if;
10012
10013 else
10014 Insert_Freeze_Node_For_Instance (N, F_Node);
10015 end if;
10016 end if;
10017
10018 Set_Is_Frozen (Act_Id);
10019 Insert_Before (N, Act_Body);
10020 Mark_Rewrite_Insertion (Act_Body);
10021 end Install_Body;
10022
10023 -----------------------------
10024 -- Install_Formal_Packages --
10025 -----------------------------
10026
10027 procedure Install_Formal_Packages (Par : Entity_Id) is
10028 E : Entity_Id;
10029 Gen : Entity_Id;
10030 Gen_E : Entity_Id := Empty;
10031
10032 begin
10033 E := First_Entity (Par);
10034
10035 -- If we are installing an instance parent, locate the formal packages
10036 -- of its generic parent.
10037
10038 if Is_Generic_Instance (Par) then
10039 Gen := Generic_Parent (Package_Specification (Par));
10040 Gen_E := First_Entity (Gen);
10041 end if;
10042
10043 while Present (E) loop
10044 if Ekind (E) = E_Package
10045 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10046 then
10047 -- If this is the renaming for the parent instance, done
10048
10049 if Renamed_Object (E) = Par then
10050 exit;
10051
10052 -- The visibility of a formal of an enclosing generic is already
10053 -- correct.
10054
10055 elsif Denotes_Formal_Package (E) then
10056 null;
10057
10058 elsif Present (Associated_Formal_Package (E)) then
10059 Check_Generic_Actuals (Renamed_Object (E), True);
10060 Set_Is_Hidden (E, False);
10061
10062 -- Find formal package in generic unit that corresponds to
10063 -- (instance of) formal package in instance.
10064
10065 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10066 Next_Entity (Gen_E);
10067 end loop;
10068
10069 if Present (Gen_E) then
10070 Map_Formal_Package_Entities (Gen_E, E);
10071 end if;
10072 end if;
10073 end if;
10074
10075 Next_Entity (E);
10076
10077 if Present (Gen_E) then
10078 Next_Entity (Gen_E);
10079 end if;
10080 end loop;
10081 end Install_Formal_Packages;
10082
10083 --------------------
10084 -- Install_Parent --
10085 --------------------
10086
10087 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10088 Ancestors : constant Elist_Id := New_Elmt_List;
10089 S : constant Entity_Id := Current_Scope;
10090 Inst_Par : Entity_Id;
10091 First_Par : Entity_Id;
10092 Inst_Node : Node_Id;
10093 Gen_Par : Entity_Id;
10094 First_Gen : Entity_Id;
10095 Elmt : Elmt_Id;
10096
10097 procedure Install_Noninstance_Specs (Par : Entity_Id);
10098 -- Install the scopes of noninstance parent units ending with Par
10099
10100 procedure Install_Spec (Par : Entity_Id);
10101 -- The child unit is within the declarative part of the parent, so the
10102 -- declarations within the parent are immediately visible.
10103
10104 -------------------------------
10105 -- Install_Noninstance_Specs --
10106 -------------------------------
10107
10108 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10109 begin
10110 if Present (Par)
10111 and then Par /= Standard_Standard
10112 and then not In_Open_Scopes (Par)
10113 then
10114 Install_Noninstance_Specs (Scope (Par));
10115 Install_Spec (Par);
10116 end if;
10117 end Install_Noninstance_Specs;
10118
10119 ------------------
10120 -- Install_Spec --
10121 ------------------
10122
10123 procedure Install_Spec (Par : Entity_Id) is
10124 Spec : constant Node_Id := Package_Specification (Par);
10125
10126 begin
10127 -- If this parent of the child instance is a top-level unit,
10128 -- then record the unit and its visibility for later resetting in
10129 -- Remove_Parent. We exclude units that are generic instances, as we
10130 -- only want to record this information for the ultimate top-level
10131 -- noninstance parent (is that always correct???).
10132
10133 if Scope (Par) = Standard_Standard
10134 and then not Is_Generic_Instance (Par)
10135 then
10136 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10137 Instance_Parent_Unit := Par;
10138 end if;
10139
10140 -- Open the parent scope and make it and its declarations visible.
10141 -- If this point is not within a body, then only the visible
10142 -- declarations should be made visible, and installation of the
10143 -- private declarations is deferred until the appropriate point
10144 -- within analysis of the spec being instantiated (see the handling
10145 -- of parent visibility in Analyze_Package_Specification). This is
10146 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10147 -- private view problems that occur when compiling instantiations of
10148 -- a generic child of that package (Generic_Dispatching_Constructor).
10149 -- If the instance freezes a tagged type, inlinings of operations
10150 -- from Ada.Tags may need the full view of type Tag. If inlining took
10151 -- proper account of establishing visibility of inlined subprograms'
10152 -- parents then it should be possible to remove this
10153 -- special check. ???
10154
10155 Push_Scope (Par);
10156 Set_Is_Immediately_Visible (Par);
10157 Install_Visible_Declarations (Par);
10158 Set_Use (Visible_Declarations (Spec));
10159
10160 if In_Body or else Is_RTU (Par, Ada_Tags) then
10161 Install_Private_Declarations (Par);
10162 Set_Use (Private_Declarations (Spec));
10163 end if;
10164 end Install_Spec;
10165
10166 -- Start of processing for Install_Parent
10167
10168 begin
10169 -- We need to install the parent instance to compile the instantiation
10170 -- of the child, but the child instance must appear in the current
10171 -- scope. Given that we cannot place the parent above the current scope
10172 -- in the scope stack, we duplicate the current scope and unstack both
10173 -- after the instantiation is complete.
10174
10175 -- If the parent is itself the instantiation of a child unit, we must
10176 -- also stack the instantiation of its parent, and so on. Each such
10177 -- ancestor is the prefix of the name in a prior instantiation.
10178
10179 -- If this is a nested instance, the parent unit itself resolves to
10180 -- a renaming of the parent instance, whose declaration we need.
10181
10182 -- Finally, the parent may be a generic (not an instance) when the
10183 -- child unit appears as a formal package.
10184
10185 Inst_Par := P;
10186
10187 if Present (Renamed_Entity (Inst_Par)) then
10188 Inst_Par := Renamed_Entity (Inst_Par);
10189 end if;
10190
10191 First_Par := Inst_Par;
10192
10193 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10194
10195 First_Gen := Gen_Par;
10196
10197 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10198
10199 -- Load grandparent instance as well
10200
10201 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10202
10203 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10204 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10205
10206 if Present (Renamed_Entity (Inst_Par)) then
10207 Inst_Par := Renamed_Entity (Inst_Par);
10208 end if;
10209
10210 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10211
10212 if Present (Gen_Par) then
10213 Prepend_Elmt (Inst_Par, Ancestors);
10214
10215 else
10216 -- Parent is not the name of an instantiation
10217
10218 Install_Noninstance_Specs (Inst_Par);
10219 exit;
10220 end if;
10221
10222 else
10223 -- Previous error
10224
10225 exit;
10226 end if;
10227 end loop;
10228
10229 if Present (First_Gen) then
10230 Append_Elmt (First_Par, Ancestors);
10231 else
10232 Install_Noninstance_Specs (First_Par);
10233 end if;
10234
10235 if not Is_Empty_Elmt_List (Ancestors) then
10236 Elmt := First_Elmt (Ancestors);
10237 while Present (Elmt) loop
10238 Install_Spec (Node (Elmt));
10239 Install_Formal_Packages (Node (Elmt));
10240 Next_Elmt (Elmt);
10241 end loop;
10242 end if;
10243
10244 if not In_Body then
10245 Push_Scope (S);
10246 end if;
10247 end Install_Parent;
10248
10249 -------------------------------
10250 -- Install_Hidden_Primitives --
10251 -------------------------------
10252
10253 procedure Install_Hidden_Primitives
10254 (Prims_List : in out Elist_Id;
10255 Gen_T : Entity_Id;
10256 Act_T : Entity_Id)
10257 is
10258 Elmt : Elmt_Id;
10259 List : Elist_Id := No_Elist;
10260 Prim_G_Elmt : Elmt_Id;
10261 Prim_A_Elmt : Elmt_Id;
10262 Prim_G : Node_Id;
10263 Prim_A : Node_Id;
10264
10265 begin
10266 -- No action needed in case of serious errors because we cannot trust
10267 -- in the order of primitives
10268
10269 if Serious_Errors_Detected > 0 then
10270 return;
10271
10272 -- No action possible if we don't have available the list of primitive
10273 -- operations
10274
10275 elsif No (Gen_T)
10276 or else not Is_Record_Type (Gen_T)
10277 or else not Is_Tagged_Type (Gen_T)
10278 or else not Is_Record_Type (Act_T)
10279 or else not Is_Tagged_Type (Act_T)
10280 then
10281 return;
10282
10283 -- There is no need to handle interface types since their primitives
10284 -- cannot be hidden
10285
10286 elsif Is_Interface (Gen_T) then
10287 return;
10288 end if;
10289
10290 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10291
10292 if not Is_Class_Wide_Type (Act_T) then
10293 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10294 else
10295 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10296 end if;
10297
10298 loop
10299 -- Skip predefined primitives in the generic formal
10300
10301 while Present (Prim_G_Elmt)
10302 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10303 loop
10304 Next_Elmt (Prim_G_Elmt);
10305 end loop;
10306
10307 -- Skip predefined primitives in the generic actual
10308
10309 while Present (Prim_A_Elmt)
10310 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10311 loop
10312 Next_Elmt (Prim_A_Elmt);
10313 end loop;
10314
10315 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10316
10317 Prim_G := Node (Prim_G_Elmt);
10318 Prim_A := Node (Prim_A_Elmt);
10319
10320 -- There is no need to handle interface primitives because their
10321 -- primitives are not hidden
10322
10323 exit when Present (Interface_Alias (Prim_G));
10324
10325 -- Here we install one hidden primitive
10326
10327 if Chars (Prim_G) /= Chars (Prim_A)
10328 and then Has_Suffix (Prim_A, 'P')
10329 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10330 then
10331 Set_Chars (Prim_A, Chars (Prim_G));
10332 Append_New_Elmt (Prim_A, To => List);
10333 end if;
10334
10335 Next_Elmt (Prim_A_Elmt);
10336 Next_Elmt (Prim_G_Elmt);
10337 end loop;
10338
10339 -- Append the elements to the list of temporarily visible primitives
10340 -- avoiding duplicates.
10341
10342 if Present (List) then
10343 if No (Prims_List) then
10344 Prims_List := New_Elmt_List;
10345 end if;
10346
10347 Elmt := First_Elmt (List);
10348 while Present (Elmt) loop
10349 Append_Unique_Elmt (Node (Elmt), Prims_List);
10350 Next_Elmt (Elmt);
10351 end loop;
10352 end if;
10353 end Install_Hidden_Primitives;
10354
10355 -------------------------------
10356 -- Restore_Hidden_Primitives --
10357 -------------------------------
10358
10359 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10360 Prim_Elmt : Elmt_Id;
10361 Prim : Node_Id;
10362
10363 begin
10364 if Prims_List /= No_Elist then
10365 Prim_Elmt := First_Elmt (Prims_List);
10366 while Present (Prim_Elmt) loop
10367 Prim := Node (Prim_Elmt);
10368 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10369 Next_Elmt (Prim_Elmt);
10370 end loop;
10371
10372 Prims_List := No_Elist;
10373 end if;
10374 end Restore_Hidden_Primitives;
10375
10376 --------------------------------
10377 -- Instantiate_Formal_Package --
10378 --------------------------------
10379
10380 function Instantiate_Formal_Package
10381 (Formal : Node_Id;
10382 Actual : Node_Id;
10383 Analyzed_Formal : Node_Id) return List_Id
10384 is
10385 Loc : constant Source_Ptr := Sloc (Actual);
10386 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10387 Actual_Pack : Entity_Id;
10388 Formal_Pack : Entity_Id;
10389 Gen_Parent : Entity_Id;
10390 Decls : List_Id;
10391 Nod : Node_Id;
10392 Parent_Spec : Node_Id;
10393
10394 procedure Find_Matching_Actual
10395 (F : Node_Id;
10396 Act : in out Entity_Id);
10397 -- We need to associate each formal entity in the formal package with
10398 -- the corresponding entity in the actual package. The actual package
10399 -- has been analyzed and possibly expanded, and as a result there is
10400 -- no one-to-one correspondence between the two lists (for example,
10401 -- the actual may include subtypes, itypes, and inherited primitive
10402 -- operations, interspersed among the renaming declarations for the
10403 -- actuals). We retrieve the corresponding actual by name because each
10404 -- actual has the same name as the formal, and they do appear in the
10405 -- same order.
10406
10407 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10408 -- Retrieve entity of defining entity of generic formal parameter.
10409 -- Only the declarations of formals need to be considered when
10410 -- linking them to actuals, but the declarative list may include
10411 -- internal entities generated during analysis, and those are ignored.
10412
10413 procedure Match_Formal_Entity
10414 (Formal_Node : Node_Id;
10415 Formal_Ent : Entity_Id;
10416 Actual_Ent : Entity_Id);
10417 -- Associates the formal entity with the actual. In the case where
10418 -- Formal_Ent is a formal package, this procedure iterates through all
10419 -- of its formals and enters associations between the actuals occurring
10420 -- in the formal package's corresponding actual package (given by
10421 -- Actual_Ent) and the formal package's formal parameters. This
10422 -- procedure recurses if any of the parameters is itself a package.
10423
10424 function Is_Instance_Of
10425 (Act_Spec : Entity_Id;
10426 Gen_Anc : Entity_Id) return Boolean;
10427 -- The actual can be an instantiation of a generic within another
10428 -- instance, in which case there is no direct link from it to the
10429 -- original generic ancestor. In that case, we recognize that the
10430 -- ultimate ancestor is the same by examining names and scopes.
10431
10432 procedure Process_Nested_Formal (Formal : Entity_Id);
10433 -- If the current formal is declared with a box, its own formals are
10434 -- visible in the instance, as they were in the generic, and their
10435 -- Hidden flag must be reset. If some of these formals are themselves
10436 -- packages declared with a box, the processing must be recursive.
10437
10438 --------------------------
10439 -- Find_Matching_Actual --
10440 --------------------------
10441
10442 procedure Find_Matching_Actual
10443 (F : Node_Id;
10444 Act : in out Entity_Id)
10445 is
10446 Formal_Ent : Entity_Id;
10447
10448 begin
10449 case Nkind (Original_Node (F)) is
10450 when N_Formal_Object_Declaration
10451 | N_Formal_Type_Declaration
10452 =>
10453 Formal_Ent := Defining_Identifier (F);
10454
10455 while Present (Act)
10456 and then Chars (Act) /= Chars (Formal_Ent)
10457 loop
10458 Next_Entity (Act);
10459 end loop;
10460
10461 when N_Formal_Package_Declaration
10462 | N_Formal_Subprogram_Declaration
10463 | N_Generic_Package_Declaration
10464 | N_Package_Declaration
10465 =>
10466 Formal_Ent := Defining_Entity (F);
10467
10468 while Present (Act)
10469 and then Chars (Act) /= Chars (Formal_Ent)
10470 loop
10471 Next_Entity (Act);
10472 end loop;
10473
10474 when others =>
10475 raise Program_Error;
10476 end case;
10477 end Find_Matching_Actual;
10478
10479 -------------------------
10480 -- Match_Formal_Entity --
10481 -------------------------
10482
10483 procedure Match_Formal_Entity
10484 (Formal_Node : Node_Id;
10485 Formal_Ent : Entity_Id;
10486 Actual_Ent : Entity_Id)
10487 is
10488 Act_Pkg : Entity_Id;
10489
10490 begin
10491 Set_Instance_Of (Formal_Ent, Actual_Ent);
10492
10493 if Ekind (Actual_Ent) = E_Package then
10494
10495 -- Record associations for each parameter
10496
10497 Act_Pkg := Actual_Ent;
10498
10499 declare
10500 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10501 F_Ent : Entity_Id;
10502 F_Node : Node_Id;
10503
10504 Gen_Decl : Node_Id;
10505 Formals : List_Id;
10506 Actual : Entity_Id;
10507
10508 begin
10509 -- Retrieve the actual given in the formal package declaration
10510
10511 Actual := Entity (Name (Original_Node (Formal_Node)));
10512
10513 -- The actual in the formal package declaration may be a
10514 -- renamed generic package, in which case we want to retrieve
10515 -- the original generic in order to traverse its formal part.
10516
10517 if Present (Renamed_Entity (Actual)) then
10518 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10519 else
10520 Gen_Decl := Unit_Declaration_Node (Actual);
10521 end if;
10522
10523 Formals := Generic_Formal_Declarations (Gen_Decl);
10524
10525 if Present (Formals) then
10526 F_Node := First_Non_Pragma (Formals);
10527 else
10528 F_Node := Empty;
10529 end if;
10530
10531 while Present (A_Ent)
10532 and then Present (F_Node)
10533 and then A_Ent /= First_Private_Entity (Act_Pkg)
10534 loop
10535 F_Ent := Get_Formal_Entity (F_Node);
10536
10537 if Present (F_Ent) then
10538
10539 -- This is a formal of the original package. Record
10540 -- association and recurse.
10541
10542 Find_Matching_Actual (F_Node, A_Ent);
10543 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10544 Next_Entity (A_Ent);
10545 end if;
10546
10547 Next_Non_Pragma (F_Node);
10548 end loop;
10549 end;
10550 end if;
10551 end Match_Formal_Entity;
10552
10553 -----------------------
10554 -- Get_Formal_Entity --
10555 -----------------------
10556
10557 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10558 Kind : constant Node_Kind := Nkind (Original_Node (N));
10559 begin
10560 case Kind is
10561 when N_Formal_Object_Declaration =>
10562 return Defining_Identifier (N);
10563
10564 when N_Formal_Type_Declaration =>
10565 return Defining_Identifier (N);
10566
10567 when N_Formal_Subprogram_Declaration =>
10568 return Defining_Unit_Name (Specification (N));
10569
10570 when N_Formal_Package_Declaration =>
10571 return Defining_Identifier (Original_Node (N));
10572
10573 when N_Generic_Package_Declaration =>
10574 return Defining_Identifier (Original_Node (N));
10575
10576 -- All other declarations are introduced by semantic analysis and
10577 -- have no match in the actual.
10578
10579 when others =>
10580 return Empty;
10581 end case;
10582 end Get_Formal_Entity;
10583
10584 --------------------
10585 -- Is_Instance_Of --
10586 --------------------
10587
10588 function Is_Instance_Of
10589 (Act_Spec : Entity_Id;
10590 Gen_Anc : Entity_Id) return Boolean
10591 is
10592 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10593
10594 begin
10595 if No (Gen_Par) then
10596 return False;
10597
10598 -- Simplest case: the generic parent of the actual is the formal
10599
10600 elsif Gen_Par = Gen_Anc then
10601 return True;
10602
10603 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10604 return False;
10605
10606 -- The actual may be obtained through several instantiations. Its
10607 -- scope must itself be an instance of a generic declared in the
10608 -- same scope as the formal. Any other case is detected above.
10609
10610 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10611 return False;
10612
10613 else
10614 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10615 end if;
10616 end Is_Instance_Of;
10617
10618 ---------------------------
10619 -- Process_Nested_Formal --
10620 ---------------------------
10621
10622 procedure Process_Nested_Formal (Formal : Entity_Id) is
10623 Ent : Entity_Id;
10624
10625 begin
10626 if Present (Associated_Formal_Package (Formal))
10627 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10628 then
10629 Ent := First_Entity (Formal);
10630 while Present (Ent) loop
10631 Set_Is_Hidden (Ent, False);
10632 Set_Is_Visible_Formal (Ent);
10633 Set_Is_Potentially_Use_Visible
10634 (Ent, Is_Potentially_Use_Visible (Formal));
10635
10636 if Ekind (Ent) = E_Package then
10637 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10638 Process_Nested_Formal (Ent);
10639 end if;
10640
10641 Next_Entity (Ent);
10642 end loop;
10643 end if;
10644 end Process_Nested_Formal;
10645
10646 -- Start of processing for Instantiate_Formal_Package
10647
10648 begin
10649 Analyze (Actual);
10650
10651 -- The actual must be a package instance, or else a current instance
10652 -- such as a parent generic within the body of a generic child.
10653
10654 if not Is_Entity_Name (Actual)
10655 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10656 then
10657 Error_Msg_N
10658 ("expect package instance to instantiate formal", Actual);
10659 Abandon_Instantiation (Actual);
10660 raise Program_Error;
10661
10662 else
10663 Actual_Pack := Entity (Actual);
10664 Set_Is_Instantiated (Actual_Pack);
10665
10666 -- The actual may be a renamed package, or an outer generic formal
10667 -- package whose instantiation is converted into a renaming.
10668
10669 if Present (Renamed_Object (Actual_Pack)) then
10670 Actual_Pack := Renamed_Object (Actual_Pack);
10671 end if;
10672
10673 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10674 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10675 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10676 else
10677 Gen_Parent :=
10678 Generic_Parent (Specification (Analyzed_Formal));
10679 Formal_Pack :=
10680 Defining_Unit_Name (Specification (Analyzed_Formal));
10681 end if;
10682
10683 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10684 Parent_Spec := Package_Specification (Actual_Pack);
10685 else
10686 Parent_Spec := Parent (Actual_Pack);
10687 end if;
10688
10689 if Gen_Parent = Any_Id then
10690 Error_Msg_N
10691 ("previous error in declaration of formal package", Actual);
10692 Abandon_Instantiation (Actual);
10693
10694 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10695 null;
10696
10697 -- If this is the current instance of an enclosing generic, that unit
10698 -- is the generic package we need.
10699
10700 elsif In_Open_Scopes (Actual_Pack)
10701 and then Ekind (Actual_Pack) = E_Generic_Package
10702 then
10703 null;
10704
10705 else
10706 Error_Msg_NE
10707 ("actual parameter must be instance of&", Actual, Gen_Parent);
10708 Abandon_Instantiation (Actual);
10709 end if;
10710
10711 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10712 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10713
10714 Nod :=
10715 Make_Package_Renaming_Declaration (Loc,
10716 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10717 Name => New_Occurrence_Of (Actual_Pack, Loc));
10718
10719 Set_Associated_Formal_Package
10720 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10721 Decls := New_List (Nod);
10722
10723 -- If the formal F has a box, then the generic declarations are
10724 -- visible in the generic G. In an instance of G, the corresponding
10725 -- entities in the actual for F (which are the actuals for the
10726 -- instantiation of the generic that F denotes) must also be made
10727 -- visible for analysis of the current instance. On exit from the
10728 -- current instance, those entities are made private again. If the
10729 -- actual is currently in use, these entities are also use-visible.
10730
10731 -- The loop through the actual entities also steps through the formal
10732 -- entities and enters associations from formals to actuals into the
10733 -- renaming map. This is necessary to properly handle checking of
10734 -- actual parameter associations for later formals that depend on
10735 -- actuals declared in the formal package.
10736
10737 -- In Ada 2005, partial parameterization requires that we make
10738 -- visible the actuals corresponding to formals that were defaulted
10739 -- in the formal package. There formals are identified because they
10740 -- remain formal generics within the formal package, rather than
10741 -- being renamings of the actuals supplied.
10742
10743 declare
10744 Gen_Decl : constant Node_Id :=
10745 Unit_Declaration_Node (Gen_Parent);
10746 Formals : constant List_Id :=
10747 Generic_Formal_Declarations (Gen_Decl);
10748
10749 Actual_Ent : Entity_Id;
10750 Actual_Of_Formal : Node_Id;
10751 Formal_Node : Node_Id;
10752 Formal_Ent : Entity_Id;
10753
10754 begin
10755 if Present (Formals) then
10756 Formal_Node := First_Non_Pragma (Formals);
10757 else
10758 Formal_Node := Empty;
10759 end if;
10760
10761 Actual_Ent := First_Entity (Actual_Pack);
10762 Actual_Of_Formal :=
10763 First (Visible_Declarations (Specification (Analyzed_Formal)));
10764 while Present (Actual_Ent)
10765 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10766 loop
10767 if Present (Formal_Node) then
10768 Formal_Ent := Get_Formal_Entity (Formal_Node);
10769
10770 if Present (Formal_Ent) then
10771 Find_Matching_Actual (Formal_Node, Actual_Ent);
10772 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10773
10774 -- We iterate at the same time over the actuals of the
10775 -- local package created for the formal, to determine
10776 -- which one of the formals of the original generic were
10777 -- defaulted in the formal. The corresponding actual
10778 -- entities are visible in the enclosing instance.
10779
10780 if Box_Present (Formal)
10781 or else
10782 (Present (Actual_Of_Formal)
10783 and then
10784 Is_Generic_Formal
10785 (Get_Formal_Entity (Actual_Of_Formal)))
10786 then
10787 Set_Is_Hidden (Actual_Ent, False);
10788 Set_Is_Visible_Formal (Actual_Ent);
10789 Set_Is_Potentially_Use_Visible
10790 (Actual_Ent, In_Use (Actual_Pack));
10791
10792 if Ekind (Actual_Ent) = E_Package then
10793 Process_Nested_Formal (Actual_Ent);
10794 end if;
10795
10796 else
10797 if not Is_Hidden (Actual_Ent) then
10798 Append_Elmt (Actual_Ent, Hidden_Formals);
10799 end if;
10800
10801 Set_Is_Hidden (Actual_Ent);
10802 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10803 end if;
10804 end if;
10805
10806 Next_Non_Pragma (Formal_Node);
10807 Next (Actual_Of_Formal);
10808
10809 else
10810 -- No further formals to match, but the generic part may
10811 -- contain inherited operation that are not hidden in the
10812 -- enclosing instance.
10813
10814 Next_Entity (Actual_Ent);
10815 end if;
10816 end loop;
10817
10818 -- Inherited subprograms generated by formal derived types are
10819 -- also visible if the types are.
10820
10821 Actual_Ent := First_Entity (Actual_Pack);
10822 while Present (Actual_Ent)
10823 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10824 loop
10825 if Is_Overloadable (Actual_Ent)
10826 and then
10827 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10828 and then
10829 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10830 then
10831 Set_Is_Hidden (Actual_Ent, False);
10832 Set_Is_Potentially_Use_Visible
10833 (Actual_Ent, In_Use (Actual_Pack));
10834 end if;
10835
10836 Next_Entity (Actual_Ent);
10837 end loop;
10838
10839 -- No conformance to check if the generic has no formal parameters
10840 -- and the formal package has no generic associations.
10841
10842 if Is_Empty_List (Formals)
10843 and then
10844 (Box_Present (Formal)
10845 or else No (Generic_Associations (Formal)))
10846 then
10847 return Decls;
10848 end if;
10849 end;
10850
10851 -- If the formal is not declared with a box, reanalyze it as an
10852 -- abbreviated instantiation, to verify the matching rules of 12.7.
10853 -- The actual checks are performed after the generic associations
10854 -- have been analyzed, to guarantee the same visibility for this
10855 -- instantiation and for the actuals.
10856
10857 -- In Ada 2005, the generic associations for the formal can include
10858 -- defaulted parameters. These are ignored during check. This
10859 -- internal instantiation is removed from the tree after conformance
10860 -- checking, because it contains formal declarations for those
10861 -- defaulted parameters, and those should not reach the back-end.
10862
10863 if not Box_Present (Formal) then
10864 declare
10865 I_Pack : constant Entity_Id :=
10866 Make_Temporary (Sloc (Actual), 'P');
10867
10868 begin
10869 Set_Is_Internal (I_Pack);
10870 Set_Ekind (I_Pack, E_Package);
10871 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10872
10873 Append_To (Decls,
10874 Make_Package_Instantiation (Sloc (Actual),
10875 Defining_Unit_Name => I_Pack,
10876 Name =>
10877 New_Occurrence_Of
10878 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10879 Generic_Associations => Generic_Associations (Formal)));
10880 end;
10881 end if;
10882
10883 return Decls;
10884 end if;
10885 end Instantiate_Formal_Package;
10886
10887 -----------------------------------
10888 -- Instantiate_Formal_Subprogram --
10889 -----------------------------------
10890
10891 function Instantiate_Formal_Subprogram
10892 (Formal : Node_Id;
10893 Actual : Node_Id;
10894 Analyzed_Formal : Node_Id) return Node_Id
10895 is
10896 Analyzed_S : constant Entity_Id :=
10897 Defining_Unit_Name (Specification (Analyzed_Formal));
10898 Formal_Sub : constant Entity_Id :=
10899 Defining_Unit_Name (Specification (Formal));
10900
10901 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10902 -- If the generic is a child unit, the parent has been installed on the
10903 -- scope stack, but a default subprogram cannot resolve to something
10904 -- on the parent because that parent is not really part of the visible
10905 -- context (it is there to resolve explicit local entities). If the
10906 -- default has resolved in this way, we remove the entity from immediate
10907 -- visibility and analyze the node again to emit an error message or
10908 -- find another visible candidate.
10909
10910 procedure Valid_Actual_Subprogram (Act : Node_Id);
10911 -- Perform legality check and raise exception on failure
10912
10913 -----------------------
10914 -- From_Parent_Scope --
10915 -----------------------
10916
10917 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10918 Gen_Scope : Node_Id;
10919
10920 begin
10921 Gen_Scope := Scope (Analyzed_S);
10922 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10923 if Scope (Subp) = Scope (Gen_Scope) then
10924 return True;
10925 end if;
10926
10927 Gen_Scope := Scope (Gen_Scope);
10928 end loop;
10929
10930 return False;
10931 end From_Parent_Scope;
10932
10933 -----------------------------
10934 -- Valid_Actual_Subprogram --
10935 -----------------------------
10936
10937 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10938 Act_E : Entity_Id;
10939
10940 begin
10941 if Is_Entity_Name (Act) then
10942 Act_E := Entity (Act);
10943
10944 elsif Nkind (Act) = N_Selected_Component
10945 and then Is_Entity_Name (Selector_Name (Act))
10946 then
10947 Act_E := Entity (Selector_Name (Act));
10948
10949 else
10950 Act_E := Empty;
10951 end if;
10952
10953 if (Present (Act_E) and then Is_Overloadable (Act_E))
10954 or else Nkind (Act) in N_Attribute_Reference
10955 | N_Indexed_Component
10956 | N_Character_Literal
10957 | N_Explicit_Dereference
10958 then
10959 return;
10960 end if;
10961
10962 Error_Msg_NE
10963 ("expect subprogram or entry name in instantiation of &",
10964 Instantiation_Node, Formal_Sub);
10965 Abandon_Instantiation (Instantiation_Node);
10966 end Valid_Actual_Subprogram;
10967
10968 -- Local variables
10969
10970 Decl_Node : Node_Id;
10971 Loc : Source_Ptr;
10972 Nam : Node_Id;
10973 New_Spec : Node_Id;
10974 New_Subp : Entity_Id;
10975
10976 -- Start of processing for Instantiate_Formal_Subprogram
10977
10978 begin
10979 New_Spec := New_Copy_Tree (Specification (Formal));
10980
10981 -- The tree copy has created the proper instantiation sloc for the
10982 -- new specification. Use this location for all other constructed
10983 -- declarations.
10984
10985 Loc := Sloc (Defining_Unit_Name (New_Spec));
10986
10987 -- Create new entity for the actual (New_Copy_Tree does not), and
10988 -- indicate that it is an actual.
10989
10990 -- If the actual is not an entity (i.e. an attribute reference)
10991 -- and the formal includes aspect specifications for contracts,
10992 -- we create an internal name for the renaming declaration. The
10993 -- constructed wrapper contains a call to the entity in the renaming.
10994 -- This is an expansion activity, as is the wrapper creation.
10995
10996 if Ada_Version >= Ada_2020
10997 and then Has_Contracts (Analyzed_Formal)
10998 and then not Is_Entity_Name (Actual)
10999 and then Expander_Active
11000 then
11001 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11002 Set_Defining_Unit_Name (New_Spec, New_Subp);
11003 else
11004 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11005 end if;
11006
11007 Set_Ekind (New_Subp, Ekind (Analyzed_S));
11008 Set_Is_Generic_Actual_Subprogram (New_Subp);
11009 Set_Defining_Unit_Name (New_Spec, New_Subp);
11010
11011 -- Create new entities for the each of the formals in the specification
11012 -- of the renaming declaration built for the actual.
11013
11014 if Present (Parameter_Specifications (New_Spec)) then
11015 declare
11016 F : Node_Id;
11017 F_Id : Entity_Id;
11018
11019 begin
11020 F := First (Parameter_Specifications (New_Spec));
11021 while Present (F) loop
11022 F_Id := Defining_Identifier (F);
11023
11024 Set_Defining_Identifier (F,
11025 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11026 Next (F);
11027 end loop;
11028 end;
11029 end if;
11030
11031 -- Find entity of actual. If the actual is an attribute reference, it
11032 -- cannot be resolved here (its formal is missing) but is handled
11033 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11034 -- fully resolved subsequently, when the renaming declaration for the
11035 -- formal is analyzed. If it is an explicit dereference, resolve the
11036 -- prefix but not the actual itself, to prevent interpretation as call.
11037
11038 if Present (Actual) then
11039 Loc := Sloc (Actual);
11040 Set_Sloc (New_Spec, Loc);
11041
11042 if Nkind (Actual) = N_Operator_Symbol then
11043 Find_Direct_Name (Actual);
11044
11045 elsif Nkind (Actual) = N_Explicit_Dereference then
11046 Analyze (Prefix (Actual));
11047
11048 elsif Nkind (Actual) /= N_Attribute_Reference then
11049 Analyze (Actual);
11050 end if;
11051
11052 Valid_Actual_Subprogram (Actual);
11053 Nam := Actual;
11054
11055 elsif Present (Default_Name (Formal)) then
11056 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11057 | N_Selected_Component
11058 | N_Indexed_Component
11059 | N_Character_Literal
11060 and then Present (Entity (Default_Name (Formal)))
11061 then
11062 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11063 else
11064 Nam := New_Copy (Default_Name (Formal));
11065 Set_Sloc (Nam, Loc);
11066 end if;
11067
11068 elsif Box_Present (Formal) then
11069
11070 -- Actual is resolved at the point of instantiation. Create an
11071 -- identifier or operator with the same name as the formal.
11072
11073 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11074 Nam :=
11075 Make_Operator_Symbol (Loc,
11076 Chars => Chars (Formal_Sub),
11077 Strval => No_String);
11078 else
11079 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11080 end if;
11081
11082 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11083 and then Null_Present (Specification (Formal))
11084 then
11085 -- Generate null body for procedure, for use in the instance
11086
11087 Decl_Node :=
11088 Make_Subprogram_Body (Loc,
11089 Specification => New_Spec,
11090 Declarations => New_List,
11091 Handled_Statement_Sequence =>
11092 Make_Handled_Sequence_Of_Statements (Loc,
11093 Statements => New_List (Make_Null_Statement (Loc))));
11094
11095 -- RM 12.6 (16 2/2): The procedure has convention Intrinsic
11096
11097 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11098
11099 -- Eliminate the calls to it when optimization is enabled
11100
11101 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11102 return Decl_Node;
11103
11104 else
11105 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11106 Error_Msg_NE
11107 ("missing actual&", Instantiation_Node, Formal_Sub);
11108 Error_Msg_NE
11109 ("\in instantiation of & declared#",
11110 Instantiation_Node, Scope (Analyzed_S));
11111 Abandon_Instantiation (Instantiation_Node);
11112 end if;
11113
11114 Decl_Node :=
11115 Make_Subprogram_Renaming_Declaration (Loc,
11116 Specification => New_Spec,
11117 Name => Nam);
11118
11119 -- If we do not have an actual and the formal specified <> then set to
11120 -- get proper default.
11121
11122 if No (Actual) and then Box_Present (Formal) then
11123 Set_From_Default (Decl_Node);
11124 end if;
11125
11126 -- Gather possible interpretations for the actual before analyzing the
11127 -- instance. If overloaded, it will be resolved when analyzing the
11128 -- renaming declaration.
11129
11130 if Box_Present (Formal) and then No (Actual) then
11131 Analyze (Nam);
11132
11133 if Is_Child_Unit (Scope (Analyzed_S))
11134 and then Present (Entity (Nam))
11135 then
11136 if not Is_Overloaded (Nam) then
11137 if From_Parent_Scope (Entity (Nam)) then
11138 Set_Is_Immediately_Visible (Entity (Nam), False);
11139 Set_Entity (Nam, Empty);
11140 Set_Etype (Nam, Empty);
11141
11142 Analyze (Nam);
11143 Set_Is_Immediately_Visible (Entity (Nam));
11144 end if;
11145
11146 else
11147 declare
11148 I : Interp_Index;
11149 It : Interp;
11150
11151 begin
11152 Get_First_Interp (Nam, I, It);
11153 while Present (It.Nam) loop
11154 if From_Parent_Scope (It.Nam) then
11155 Remove_Interp (I);
11156 end if;
11157
11158 Get_Next_Interp (I, It);
11159 end loop;
11160 end;
11161 end if;
11162 end if;
11163 end if;
11164
11165 -- The generic instantiation freezes the actual. This can only be done
11166 -- once the actual is resolved, in the analysis of the renaming
11167 -- declaration. To make the formal subprogram entity available, we set
11168 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11169 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11170 -- of formal abstract subprograms.
11171
11172 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11173
11174 -- We cannot analyze the renaming declaration, and thus find the actual,
11175 -- until all the actuals are assembled in the instance. For subsequent
11176 -- checks of other actuals, indicate the node that will hold the
11177 -- instance of this formal.
11178
11179 Set_Instance_Of (Analyzed_S, Nam);
11180
11181 if Nkind (Actual) = N_Selected_Component
11182 and then Is_Task_Type (Etype (Prefix (Actual)))
11183 and then not Is_Frozen (Etype (Prefix (Actual)))
11184 then
11185 -- The renaming declaration will create a body, which must appear
11186 -- outside of the instantiation, We move the renaming declaration
11187 -- out of the instance, and create an additional renaming inside,
11188 -- to prevent freezing anomalies.
11189
11190 declare
11191 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11192
11193 begin
11194 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11195 Insert_Before (Instantiation_Node, Decl_Node);
11196 Analyze (Decl_Node);
11197
11198 -- Now create renaming within the instance
11199
11200 Decl_Node :=
11201 Make_Subprogram_Renaming_Declaration (Loc,
11202 Specification => New_Copy_Tree (New_Spec),
11203 Name => New_Occurrence_Of (Anon_Id, Loc));
11204
11205 Set_Defining_Unit_Name (Specification (Decl_Node),
11206 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11207 end;
11208 end if;
11209
11210 return Decl_Node;
11211 end Instantiate_Formal_Subprogram;
11212
11213 ------------------------
11214 -- Instantiate_Object --
11215 ------------------------
11216
11217 function Instantiate_Object
11218 (Formal : Node_Id;
11219 Actual : Node_Id;
11220 Analyzed_Formal : Node_Id) return List_Id
11221 is
11222 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11223 A_Gen_Obj : constant Entity_Id :=
11224 Defining_Identifier (Analyzed_Formal);
11225 Acc_Def : Node_Id := Empty;
11226 Act_Assoc : constant Node_Id := Parent (Actual);
11227 Actual_Decl : Node_Id := Empty;
11228 Decl_Node : Node_Id;
11229 Def : Node_Id;
11230 Ftyp : Entity_Id;
11231 List : constant List_Id := New_List;
11232 Loc : constant Source_Ptr := Sloc (Actual);
11233 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11234 Subt_Decl : Node_Id := Empty;
11235 Subt_Mark : Node_Id := Empty;
11236
11237 -- Start of processing for Instantiate_Object
11238
11239 begin
11240 -- Formal may be an anonymous access
11241
11242 if Present (Subtype_Mark (Formal)) then
11243 Subt_Mark := Subtype_Mark (Formal);
11244 else
11245 Check_Access_Definition (Formal);
11246 Acc_Def := Access_Definition (Formal);
11247 end if;
11248
11249 -- Sloc for error message on missing actual
11250
11251 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11252
11253 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11254 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11255 end if;
11256
11257 Set_Parent (List, Parent (Actual));
11258
11259 -- OUT present
11260
11261 if Out_Present (Formal) then
11262
11263 -- An IN OUT generic actual must be a name. The instantiation is a
11264 -- renaming declaration. The actual is the name being renamed. We
11265 -- use the actual directly, rather than a copy, because it is not
11266 -- used further in the list of actuals, and because a copy or a use
11267 -- of relocate_node is incorrect if the instance is nested within a
11268 -- generic. In order to simplify e.g. ASIS queries, the
11269 -- Generic_Parent field links the declaration to the generic
11270 -- association.
11271
11272 if No (Actual) then
11273 Error_Msg_NE
11274 ("missing actual &",
11275 Instantiation_Node, Gen_Obj);
11276 Error_Msg_NE
11277 ("\in instantiation of & declared#",
11278 Instantiation_Node, Scope (A_Gen_Obj));
11279 Abandon_Instantiation (Instantiation_Node);
11280 end if;
11281
11282 if Present (Subt_Mark) then
11283 Decl_Node :=
11284 Make_Object_Renaming_Declaration (Loc,
11285 Defining_Identifier => New_Copy (Gen_Obj),
11286 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11287 Name => Actual);
11288
11289 else pragma Assert (Present (Acc_Def));
11290 Decl_Node :=
11291 Make_Object_Renaming_Declaration (Loc,
11292 Defining_Identifier => New_Copy (Gen_Obj),
11293 Access_Definition => New_Copy_Tree (Acc_Def),
11294 Name => Actual);
11295 end if;
11296
11297 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11298
11299 -- The analysis of the actual may produce Insert_Action nodes, so
11300 -- the declaration must have a context in which to attach them.
11301
11302 Append (Decl_Node, List);
11303 Analyze (Actual);
11304
11305 -- Return if the analysis of the actual reported some error
11306
11307 if Etype (Actual) = Any_Type then
11308 return List;
11309 end if;
11310
11311 -- This check is performed here because Analyze_Object_Renaming will
11312 -- not check it when Comes_From_Source is False. Note though that the
11313 -- check for the actual being the name of an object will be performed
11314 -- in Analyze_Object_Renaming.
11315
11316 if Is_Object_Reference (Actual)
11317 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11318 then
11319 Error_Msg_N
11320 ("illegal discriminant-dependent component for in out parameter",
11321 Actual);
11322 end if;
11323
11324 -- The actual has to be resolved in order to check that it is a
11325 -- variable (due to cases such as F (1), where F returns access to
11326 -- an array, and for overloaded prefixes).
11327
11328 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11329
11330 -- If the type of the formal is not itself a formal, and the current
11331 -- unit is a child unit, the formal type must be declared in a
11332 -- parent, and must be retrieved by visibility.
11333
11334 if Ftyp = Orig_Ftyp
11335 and then Is_Generic_Unit (Scope (Ftyp))
11336 and then Is_Child_Unit (Scope (A_Gen_Obj))
11337 then
11338 declare
11339 Temp : constant Node_Id :=
11340 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11341 begin
11342 Set_Entity (Temp, Empty);
11343 Find_Type (Temp);
11344 Ftyp := Entity (Temp);
11345 end;
11346 end if;
11347
11348 if Is_Private_Type (Ftyp)
11349 and then not Is_Private_Type (Etype (Actual))
11350 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11351 or else Base_Type (Etype (Actual)) = Ftyp)
11352 then
11353 -- If the actual has the type of the full view of the formal, or
11354 -- else a non-private subtype of the formal, then the visibility
11355 -- of the formal type has changed. Add to the actuals a subtype
11356 -- declaration that will force the exchange of views in the body
11357 -- of the instance as well.
11358
11359 Subt_Decl :=
11360 Make_Subtype_Declaration (Loc,
11361 Defining_Identifier => Make_Temporary (Loc, 'P'),
11362 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11363
11364 Prepend (Subt_Decl, List);
11365
11366 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11367 Exchange_Declarations (Ftyp);
11368 end if;
11369
11370 Resolve (Actual, Ftyp);
11371
11372 if not Denotes_Variable (Actual) then
11373 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11374
11375 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11376
11377 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11378 -- the type of the actual shall resolve to a specific anonymous
11379 -- access type.
11380
11381 if Ada_Version < Ada_2005
11382 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11383 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11384 then
11385 Error_Msg_NE
11386 ("type of actual does not match type of&", Actual, Gen_Obj);
11387 end if;
11388 end if;
11389
11390 Note_Possible_Modification (Actual, Sure => True);
11391
11392 -- Check for instantiation with atomic/volatile/VFA object actual for
11393 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11394
11395 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11396 Error_Msg_NE
11397 ("cannot instantiate nonatomic formal & of mode in out",
11398 Actual, Gen_Obj);
11399 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11400
11401 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11402 then
11403 Error_Msg_NE
11404 ("cannot instantiate nonvolatile formal & of mode in out",
11405 Actual, Gen_Obj);
11406 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11407
11408 elsif Is_Volatile_Full_Access_Object (Actual)
11409 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11410 then
11411 Error_Msg_NE
11412 ("cannot instantiate nonfull access formal & of mode in out",
11413 Actual, Gen_Obj);
11414 Error_Msg_N
11415 ("\with full access object actual (RM C.6(12))", Actual);
11416 end if;
11417
11418 -- Check for instantiation on nonatomic subcomponent of a full access
11419 -- object in Ada 2020 (RM C.6 (12)).
11420
11421 if Ada_Version >= Ada_2020
11422 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11423 and then not Is_Atomic_Object (Actual)
11424 then
11425 Error_Msg_NE
11426 ("cannot instantiate formal & of mode in out with actual",
11427 Actual, Gen_Obj);
11428 Error_Msg_N
11429 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11430 Actual);
11431 end if;
11432
11433 -- Check actual/formal compatibility with respect to the four
11434 -- volatility refinement aspects.
11435
11436 declare
11437 Actual_Obj : Entity_Id;
11438 N : Node_Id := Actual;
11439 begin
11440 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11441 -- pointer dereference like component selection.
11442 loop
11443 if Is_Entity_Name (N) then
11444 Actual_Obj := Entity (N);
11445 exit;
11446 end if;
11447
11448 case Nkind (N) is
11449 when N_Indexed_Component
11450 | N_Selected_Component
11451 | N_Slice
11452 | N_Explicit_Dereference
11453 =>
11454 N := Prefix (N);
11455
11456 when N_Type_Conversion =>
11457 N := Expression (N);
11458
11459 when others =>
11460 Actual_Obj := Etype (N);
11461 exit;
11462 end case;
11463 end loop;
11464
11465 Check_Volatility_Compatibility
11466 (Actual_Obj, A_Gen_Obj, "actual object",
11467 "its corresponding formal object of mode in out",
11468 Srcpos_Bearer => Actual);
11469 end;
11470
11471 -- Formal in-parameter
11472
11473 else
11474 -- The instantiation of a generic formal in-parameter is constant
11475 -- declaration. The actual is the expression for that declaration.
11476 -- Its type is a full copy of the type of the formal. This may be
11477 -- an access to subprogram, for which we need to generate entities
11478 -- for the formals in the new signature.
11479
11480 if Present (Actual) then
11481 if Present (Subt_Mark) then
11482 Def := New_Copy_Tree (Subt_Mark);
11483 else
11484 pragma Assert (Present (Acc_Def));
11485 Def := New_Copy_Tree (Acc_Def);
11486 end if;
11487
11488 Decl_Node :=
11489 Make_Object_Declaration (Loc,
11490 Defining_Identifier => New_Copy (Gen_Obj),
11491 Constant_Present => True,
11492 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11493 Object_Definition => Def,
11494 Expression => Actual);
11495
11496 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11497
11498 -- A generic formal object of a tagged type is defined to be
11499 -- aliased so the new constant must also be treated as aliased.
11500
11501 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11502 Set_Aliased_Present (Decl_Node);
11503 end if;
11504
11505 Append (Decl_Node, List);
11506
11507 -- No need to repeat (pre-)analysis of some expression nodes
11508 -- already handled in Preanalyze_Actuals.
11509
11510 if Nkind (Actual) /= N_Allocator then
11511 Analyze (Actual);
11512
11513 -- Return if the analysis of the actual reported some error
11514
11515 if Etype (Actual) = Any_Type then
11516 return List;
11517 end if;
11518 end if;
11519
11520 declare
11521 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11522 Typ : Entity_Id;
11523
11524 begin
11525 Typ := Get_Instance_Of (Formal_Type);
11526
11527 -- If the actual appears in the current or an enclosing scope,
11528 -- use its type directly. This is relevant if it has an actual
11529 -- subtype that is distinct from its nominal one. This cannot
11530 -- be done in general because the type of the actual may
11531 -- depend on other actuals, and only be fully determined when
11532 -- the enclosing instance is analyzed.
11533
11534 if Present (Etype (Actual))
11535 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11536 then
11537 Freeze_Before (Instantiation_Node, Etype (Actual));
11538 else
11539 Freeze_Before (Instantiation_Node, Typ);
11540 end if;
11541
11542 -- If the actual is an aggregate, perform name resolution on
11543 -- its components (the analysis of an aggregate does not do it)
11544 -- to capture local names that may be hidden if the generic is
11545 -- a child unit.
11546
11547 if Nkind (Actual) = N_Aggregate then
11548 Preanalyze_And_Resolve (Actual, Typ);
11549 end if;
11550
11551 if Is_Limited_Type (Typ)
11552 and then not OK_For_Limited_Init (Typ, Actual)
11553 then
11554 Error_Msg_N
11555 ("initialization not allowed for limited types", Actual);
11556 Explain_Limited_Type (Typ, Actual);
11557 end if;
11558 end;
11559
11560 elsif Present (Default_Expression (Formal)) then
11561
11562 -- Use default to construct declaration
11563
11564 if Present (Subt_Mark) then
11565 Def := New_Copy (Subt_Mark);
11566 else
11567 pragma Assert (Present (Acc_Def));
11568 Def := New_Copy_Tree (Acc_Def);
11569 end if;
11570
11571 Decl_Node :=
11572 Make_Object_Declaration (Sloc (Formal),
11573 Defining_Identifier => New_Copy (Gen_Obj),
11574 Constant_Present => True,
11575 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11576 Object_Definition => Def,
11577 Expression => New_Copy_Tree
11578 (Default_Expression (Formal)));
11579
11580 Set_Corresponding_Generic_Association
11581 (Decl_Node, Expression (Decl_Node));
11582
11583 Append (Decl_Node, List);
11584 Set_Analyzed (Expression (Decl_Node), False);
11585
11586 else
11587 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11588 Error_Msg_NE ("\in instantiation of & declared#",
11589 Instantiation_Node, Scope (A_Gen_Obj));
11590
11591 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11592
11593 -- Create dummy constant declaration so that instance can be
11594 -- analyzed, to minimize cascaded visibility errors.
11595
11596 if Present (Subt_Mark) then
11597 Def := Subt_Mark;
11598 else pragma Assert (Present (Acc_Def));
11599 Def := Acc_Def;
11600 end if;
11601
11602 Decl_Node :=
11603 Make_Object_Declaration (Loc,
11604 Defining_Identifier => New_Copy (Gen_Obj),
11605 Constant_Present => True,
11606 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11607 Object_Definition => New_Copy (Def),
11608 Expression =>
11609 Make_Attribute_Reference (Sloc (Gen_Obj),
11610 Attribute_Name => Name_First,
11611 Prefix => New_Copy (Def)));
11612
11613 Append (Decl_Node, List);
11614
11615 else
11616 Abandon_Instantiation (Instantiation_Node);
11617 end if;
11618 end if;
11619 end if;
11620
11621 if Nkind (Actual) in N_Has_Entity then
11622 Actual_Decl := Parent (Entity (Actual));
11623 end if;
11624
11625 -- Ada 2005 (AI-423) refined by AI12-0287:
11626 -- For an object_renaming_declaration with a null_exclusion or an
11627 -- access_definition that has a null_exclusion, the subtype of the
11628 -- object_name shall exclude null. In addition, if the
11629 -- object_renaming_declaration occurs within the body of a generic unit
11630 -- G or within the body of a generic unit declared within the
11631 -- declarative region of generic unit G, then:
11632 -- * if the object_name statically denotes a generic formal object of
11633 -- mode in out of G, then the declaration of that object shall have a
11634 -- null_exclusion;
11635 -- * if the object_name statically denotes a call of a generic formal
11636 -- function of G, then the declaration of the result of that function
11637 -- shall have a null_exclusion.
11638
11639 if Ada_Version >= Ada_2005
11640 and then Present (Actual_Decl)
11641 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11642 | N_Object_Declaration
11643 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11644 and then not Has_Null_Exclusion (Actual_Decl)
11645 and then Has_Null_Exclusion (Analyzed_Formal)
11646 and then Ekind (Defining_Identifier (Analyzed_Formal))
11647 = E_Generic_In_Out_Parameter
11648 and then ((In_Generic_Scope (Entity (Actual))
11649 and then In_Package_Body (Scope (Entity (Actual))))
11650 or else not Can_Never_Be_Null (Etype (Actual)))
11651 then
11652 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11653 Error_Msg_N
11654 ("actual must exclude null to match generic formal#", Actual);
11655 end if;
11656
11657 -- An effectively volatile object cannot be used as an actual in a
11658 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11659 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11660 -- legality rule, and also verifies that the actual is an object.
11661
11662 if SPARK_Mode = On
11663 and then Present (Actual)
11664 and then Is_Object_Reference (Actual)
11665 and then Is_Effectively_Volatile_Object (Actual)
11666 and then not Is_Effectively_Volatile (A_Gen_Obj)
11667 then
11668 Error_Msg_N
11669 ("volatile object cannot act as actual in generic instantiation",
11670 Actual);
11671 end if;
11672
11673 return List;
11674 end Instantiate_Object;
11675
11676 ------------------------------
11677 -- Instantiate_Package_Body --
11678 ------------------------------
11679
11680 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11681 -- must be replaced by gotos which jump to the end of the routine in order
11682 -- to restore the Ghost and SPARK modes.
11683
11684 procedure Instantiate_Package_Body
11685 (Body_Info : Pending_Body_Info;
11686 Inlined_Body : Boolean := False;
11687 Body_Optional : Boolean := False)
11688 is
11689 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11690 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11691 Act_Spec : constant Node_Id := Specification (Act_Decl);
11692 Ctx_Parents : Elist_Id := No_Elist;
11693 Ctx_Top : Int := 0;
11694 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11695 Gen_Id : constant Node_Id := Name (Inst_Node);
11696 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11697 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11698 Loc : constant Source_Ptr := Sloc (Inst_Node);
11699
11700 procedure Check_Initialized_Types;
11701 -- In a generic package body, an entity of a generic private type may
11702 -- appear uninitialized. This is suspicious, unless the actual is a
11703 -- fully initialized type.
11704
11705 procedure Install_Parents_Of_Generic_Context
11706 (Inst_Scope : Entity_Id;
11707 Ctx_Parents : out Elist_Id);
11708 -- Inst_Scope is the scope where the instance appears within; when it
11709 -- appears within a generic child package G, this routine collects and
11710 -- installs the enclosing packages of G in the scopes stack; installed
11711 -- packages are returned in Ctx_Parents.
11712
11713 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11714 -- Reverse effect after instantiation is complete
11715
11716 -----------------------------
11717 -- Check_Initialized_Types --
11718 -----------------------------
11719
11720 procedure Check_Initialized_Types is
11721 Decl : Node_Id;
11722 Formal : Entity_Id;
11723 Actual : Entity_Id;
11724 Uninit_Var : Entity_Id;
11725
11726 begin
11727 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11728 while Present (Decl) loop
11729 Uninit_Var := Empty;
11730
11731 if Nkind (Decl) = N_Private_Extension_Declaration then
11732 Uninit_Var := Uninitialized_Variable (Decl);
11733
11734 elsif Nkind (Decl) = N_Formal_Type_Declaration
11735 and then Nkind (Formal_Type_Definition (Decl)) =
11736 N_Formal_Private_Type_Definition
11737 then
11738 Uninit_Var :=
11739 Uninitialized_Variable (Formal_Type_Definition (Decl));
11740 end if;
11741
11742 if Present (Uninit_Var) then
11743 Formal := Defining_Identifier (Decl);
11744 Actual := First_Entity (Act_Decl_Id);
11745
11746 -- For each formal there is a subtype declaration that renames
11747 -- the actual and has the same name as the formal. Locate the
11748 -- formal for warning message about uninitialized variables
11749 -- in the generic, for which the actual type should be a fully
11750 -- initialized type.
11751
11752 while Present (Actual) loop
11753 exit when Ekind (Actual) = E_Package
11754 and then Present (Renamed_Object (Actual));
11755
11756 if Chars (Actual) = Chars (Formal)
11757 and then not Is_Scalar_Type (Actual)
11758 and then not Is_Fully_Initialized_Type (Actual)
11759 and then Warn_On_No_Value_Assigned
11760 then
11761 Error_Msg_Node_2 := Formal;
11762 Error_Msg_NE
11763 ("generic unit has uninitialized variable& of "
11764 & "formal private type &?v?", Actual, Uninit_Var);
11765 Error_Msg_NE
11766 ("actual type for& should be fully initialized type?v?",
11767 Actual, Formal);
11768 exit;
11769 end if;
11770
11771 Next_Entity (Actual);
11772 end loop;
11773 end if;
11774
11775 Next (Decl);
11776 end loop;
11777 end Check_Initialized_Types;
11778
11779 ----------------------------------------
11780 -- Install_Parents_Of_Generic_Context --
11781 ----------------------------------------
11782
11783 procedure Install_Parents_Of_Generic_Context
11784 (Inst_Scope : Entity_Id;
11785 Ctx_Parents : out Elist_Id)
11786 is
11787 Elmt : Elmt_Id;
11788 S : Entity_Id;
11789
11790 begin
11791 Ctx_Parents := New_Elmt_List;
11792
11793 -- Collect context parents (ie. parents where the instantiation
11794 -- appears within).
11795
11796 S := Inst_Scope;
11797 while S /= Standard_Standard loop
11798 Prepend_Elmt (S, Ctx_Parents);
11799 S := Scope (S);
11800 end loop;
11801
11802 -- Install enclosing parents
11803
11804 Elmt := First_Elmt (Ctx_Parents);
11805 while Present (Elmt) loop
11806 Push_Scope (Node (Elmt));
11807 Set_Is_Immediately_Visible (Node (Elmt));
11808 Next_Elmt (Elmt);
11809 end loop;
11810 end Install_Parents_Of_Generic_Context;
11811
11812 ---------------------------------------
11813 -- Remove_Parents_Of_Generic_Context --
11814 ---------------------------------------
11815
11816 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11817 Elmt : Elmt_Id;
11818
11819 begin
11820 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11821
11822 Elmt := Last_Elmt (Ctx_Parents);
11823 while Present (Elmt) loop
11824 pragma Assert (Current_Scope = Node (Elmt));
11825 Set_Is_Immediately_Visible (Current_Scope, False);
11826 Pop_Scope;
11827
11828 Remove_Last_Elmt (Ctx_Parents);
11829 Elmt := Last_Elmt (Ctx_Parents);
11830 end loop;
11831 end Remove_Parents_Of_Generic_Context;
11832
11833 -- Local variables
11834
11835 -- The following constants capture the context prior to instantiating
11836 -- the package body.
11837
11838 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11839 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11840 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11841 Saved_ISMP : constant Boolean :=
11842 Ignore_SPARK_Mode_Pragmas_In_Instance;
11843 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11844 Local_Suppress_Stack_Top;
11845 Saved_SC : constant Boolean := Style_Check;
11846 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11847 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11848 Saved_SS : constant Suppress_Record := Scope_Suppress;
11849 Saved_Warn : constant Warning_Record := Save_Warnings;
11850
11851 Act_Body : Node_Id;
11852 Act_Body_Id : Entity_Id;
11853 Act_Body_Name : Node_Id;
11854 Gen_Body : Node_Id;
11855 Gen_Body_Id : Node_Id;
11856 Par_Ent : Entity_Id := Empty;
11857 Par_Installed : Boolean := False;
11858 Par_Vis : Boolean := False;
11859
11860 Scope_Check_Id : Entity_Id;
11861 Scope_Check_Last : Nat;
11862 -- Value of Current_Scope before calls to Install_Parents; used to check
11863 -- that scopes are correctly removed after instantiation.
11864
11865 Vis_Prims_List : Elist_Id := No_Elist;
11866 -- List of primitives made temporarily visible in the instantiation
11867 -- to match the visibility of the formal type.
11868
11869 -- Start of processing for Instantiate_Package_Body
11870
11871 begin
11872 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11873
11874 -- The instance body may already have been processed, as the parent of
11875 -- another instance that is inlined (Load_Parent_Of_Generic).
11876
11877 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11878 return;
11879 end if;
11880
11881 -- The package being instantiated may be subject to pragma Ghost. Set
11882 -- the mode now to ensure that any nodes generated during instantiation
11883 -- are properly marked as Ghost.
11884
11885 Set_Ghost_Mode (Act_Decl_Id);
11886
11887 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11888
11889 -- Re-establish the state of information on which checks are suppressed.
11890 -- This information was set in Body_Info at the point of instantiation,
11891 -- and now we restore it so that the instance is compiled using the
11892 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11893
11894 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11895 Scope_Suppress := Body_Info.Scope_Suppress;
11896
11897 Restore_Config_Switches (Body_Info.Config_Switches);
11898 Restore_Warnings (Body_Info.Warnings);
11899
11900 if No (Gen_Body_Id) then
11901
11902 -- Do not look for parent of generic body if none is required.
11903 -- This may happen when the routine is called as part of the
11904 -- Pending_Instantiations processing, when nested instances
11905 -- may precede the one generated from the main unit.
11906
11907 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11908 and then Body_Optional
11909 then
11910 goto Leave;
11911 else
11912 Load_Parent_Of_Generic
11913 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11914
11915 -- Surprisingly enough, loading the body of the parent can cause
11916 -- the body to be instantiated and the double instantiation needs
11917 -- to be prevented in order to avoid giving bogus semantic errors.
11918
11919 -- This case can occur because of the Collect_Previous_Instances
11920 -- machinery of Load_Parent_Of_Generic, which will instantiate
11921 -- bodies that are deemed to be ahead of the body of the parent
11922 -- in the compilation unit. But the relative position of these
11923 -- bodies is computed using the mere comparison of their Sloc.
11924
11925 -- Now suppose that you have two generic packages G and H, with
11926 -- G containing a mere instantiation of H:
11927
11928 -- generic
11929 -- package H is
11930
11931 -- generic
11932 -- package Nested_G is
11933 -- ...
11934 -- end Nested_G;
11935
11936 -- end H;
11937
11938 -- with H;
11939
11940 -- generic
11941 -- package G is
11942
11943 -- package My_H is new H;
11944
11945 -- end G;
11946
11947 -- and a third package Q instantiating G and Nested_G:
11948
11949 -- with G;
11950
11951 -- package Q is
11952
11953 -- package My_G is new G;
11954
11955 -- package My_Nested_G is new My_G.My_H.Nested_G;
11956
11957 -- end Q;
11958
11959 -- The body to be instantiated is that of My_Nested_G and its
11960 -- parent is the instance My_G.My_H. This latter instantiation
11961 -- is done when My_G is analyzed, i.e. after the declarations
11962 -- of My_G and My_Nested_G have been parsed; as a result, the
11963 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11964
11965 -- Therefore loading the body of My_G.My_H will cause the body
11966 -- of My_Nested_G to be instantiated because it is deemed to be
11967 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11968 -- will again be invoked on My_G.My_H, but this time with the
11969 -- Collect_Previous_Instances machinery disabled, so there is
11970 -- no endless mutual recursion and things are done in order.
11971
11972 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11973 goto Leave;
11974 end if;
11975
11976 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11977 end if;
11978 end if;
11979
11980 -- Establish global variable for sloc adjustment and for error recovery
11981 -- In the case of an instance body for an instantiation with actuals
11982 -- from a limited view, the instance body is placed at the beginning
11983 -- of the enclosing package body: use the body entity as the source
11984 -- location for nodes of the instance body.
11985
11986 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11987 declare
11988 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11989 Body_Id : constant Node_Id :=
11990 Corresponding_Body (Unit_Declaration_Node (Scop));
11991
11992 begin
11993 Instantiation_Node := Body_Id;
11994 end;
11995 else
11996 Instantiation_Node := Inst_Node;
11997 end if;
11998
11999 if Present (Gen_Body_Id) then
12000 Save_Env (Gen_Unit, Act_Decl_Id);
12001 Style_Check := False;
12002
12003 -- If the context of the instance is subject to SPARK_Mode "off", the
12004 -- annotation is missing, or the body is instantiated at a later pass
12005 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12006 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12007 -- instance.
12008
12009 if SPARK_Mode /= On
12010 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12011 then
12012 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12013 end if;
12014
12015 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12016 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12017
12018 Create_Instantiation_Source
12019 (Inst_Node, Gen_Body_Id, S_Adjustment);
12020
12021 Act_Body :=
12022 Copy_Generic_Node
12023 (Original_Node (Gen_Body), Empty, Instantiating => True);
12024
12025 -- Create proper (possibly qualified) defining name for the body, to
12026 -- correspond to the one in the spec.
12027
12028 Act_Body_Id :=
12029 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12030 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12031
12032 -- Some attributes of spec entity are not inherited by body entity
12033
12034 Set_Handler_Records (Act_Body_Id, No_List);
12035
12036 if Nkind (Defining_Unit_Name (Act_Spec)) =
12037 N_Defining_Program_Unit_Name
12038 then
12039 Act_Body_Name :=
12040 Make_Defining_Program_Unit_Name (Loc,
12041 Name =>
12042 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12043 Defining_Identifier => Act_Body_Id);
12044 else
12045 Act_Body_Name := Act_Body_Id;
12046 end if;
12047
12048 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12049
12050 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12051 Check_Generic_Actuals (Act_Decl_Id, False);
12052 Check_Initialized_Types;
12053
12054 -- Install primitives hidden at the point of the instantiation but
12055 -- visible when processing the generic formals
12056
12057 declare
12058 E : Entity_Id;
12059
12060 begin
12061 E := First_Entity (Act_Decl_Id);
12062 while Present (E) loop
12063 if Is_Type (E)
12064 and then not Is_Itype (E)
12065 and then Is_Generic_Actual_Type (E)
12066 and then Is_Tagged_Type (E)
12067 then
12068 Install_Hidden_Primitives
12069 (Prims_List => Vis_Prims_List,
12070 Gen_T => Generic_Parent_Type (Parent (E)),
12071 Act_T => E);
12072 end if;
12073
12074 Next_Entity (E);
12075 end loop;
12076 end;
12077
12078 Scope_Check_Id := Current_Scope;
12079 Scope_Check_Last := Scope_Stack.Last;
12080
12081 -- If the instantiation appears within a generic child some actual
12082 -- parameter may be the current instance of the enclosing generic
12083 -- parent.
12084
12085 declare
12086 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12087
12088 begin
12089 if Is_Child_Unit (Inst_Scope)
12090 and then Ekind (Inst_Scope) = E_Generic_Package
12091 and then Present (Generic_Associations (Inst_Node))
12092 then
12093 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12094
12095 -- Hide them from visibility; required to avoid conflicts
12096 -- installing the parent instance.
12097
12098 if Present (Ctx_Parents) then
12099 Push_Scope (Standard_Standard);
12100 Ctx_Top := Scope_Stack.Last;
12101 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12102 end if;
12103 end if;
12104 end;
12105
12106 -- If it is a child unit, make the parent instance (which is an
12107 -- instance of the parent of the generic) visible. The parent
12108 -- instance is the prefix of the name of the generic unit.
12109
12110 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12111 and then Nkind (Gen_Id) = N_Expanded_Name
12112 then
12113 Par_Ent := Entity (Prefix (Gen_Id));
12114 Par_Vis := Is_Immediately_Visible (Par_Ent);
12115 Install_Parent (Par_Ent, In_Body => True);
12116 Par_Installed := True;
12117
12118 elsif Is_Child_Unit (Gen_Unit) then
12119 Par_Ent := Scope (Gen_Unit);
12120 Par_Vis := Is_Immediately_Visible (Par_Ent);
12121 Install_Parent (Par_Ent, In_Body => True);
12122 Par_Installed := True;
12123 end if;
12124
12125 -- If the instantiation is a library unit, and this is the main unit,
12126 -- then build the resulting compilation unit nodes for the instance.
12127 -- If this is a compilation unit but it is not the main unit, then it
12128 -- is the body of a unit in the context, that is being compiled
12129 -- because it is encloses some inlined unit or another generic unit
12130 -- being instantiated. In that case, this body is not part of the
12131 -- current compilation, and is not attached to the tree, but its
12132 -- parent must be set for analysis.
12133
12134 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12135
12136 -- Replace instance node with body of instance, and create new
12137 -- node for corresponding instance declaration.
12138
12139 Build_Instance_Compilation_Unit_Nodes
12140 (Inst_Node, Act_Body, Act_Decl);
12141
12142 -- If the instantiation appears within a generic child package
12143 -- enable visibility of current instance of enclosing generic
12144 -- parents.
12145
12146 if Present (Ctx_Parents) then
12147 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12148 Analyze (Inst_Node);
12149 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12150 else
12151 Analyze (Inst_Node);
12152 end if;
12153
12154 if Parent (Inst_Node) = Cunit (Main_Unit) then
12155
12156 -- If the instance is a child unit itself, then set the scope
12157 -- of the expanded body to be the parent of the instantiation
12158 -- (ensuring that the fully qualified name will be generated
12159 -- for the elaboration subprogram).
12160
12161 if Nkind (Defining_Unit_Name (Act_Spec)) =
12162 N_Defining_Program_Unit_Name
12163 then
12164 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12165 end if;
12166 end if;
12167
12168 -- Case where instantiation is not a library unit
12169
12170 else
12171 -- If this is an early instantiation, i.e. appears textually
12172 -- before the corresponding body and must be elaborated first,
12173 -- indicate that the body instance is to be delayed.
12174
12175 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
12176
12177 -- If the instantiation appears within a generic child package
12178 -- enable visibility of current instance of enclosing generic
12179 -- parents.
12180
12181 if Present (Ctx_Parents) then
12182 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12183 Analyze (Act_Body);
12184 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12185 else
12186 Analyze (Act_Body);
12187 end if;
12188 end if;
12189
12190 Inherit_Context (Gen_Body, Inst_Node);
12191
12192 if Par_Installed then
12193 Remove_Parent (In_Body => True);
12194
12195 -- Restore the previous visibility of the parent
12196
12197 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12198 end if;
12199
12200 -- Remove the parent instances if they have been placed on the scope
12201 -- stack to compile the body.
12202
12203 if Present (Ctx_Parents) then
12204 pragma Assert (Scope_Stack.Last = Ctx_Top
12205 and then Current_Scope = Standard_Standard);
12206 Pop_Scope;
12207
12208 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12209 end if;
12210
12211 pragma Assert (Current_Scope = Scope_Check_Id);
12212 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12213
12214 Restore_Hidden_Primitives (Vis_Prims_List);
12215
12216 -- Restore the private views that were made visible when the body of
12217 -- the instantiation was created. Note that, in the case where one of
12218 -- these private views is declared in the parent, there is a nesting
12219 -- issue with the calls to Install_Parent and Remove_Parent made in
12220 -- between above with In_Body set to True, because these calls also
12221 -- want to swap and restore this private view respectively. In this
12222 -- case, the call to Install_Parent does nothing, but the call to
12223 -- Remove_Parent does restore the private view, thus undercutting the
12224 -- call to Restore_Private_Views. That's OK under the condition that
12225 -- the two mechanisms swap exactly the same entities, in particular
12226 -- the private entities dependent on the primary private entities.
12227
12228 Restore_Private_Views (Act_Decl_Id);
12229
12230 -- Remove the current unit from visibility if this is an instance
12231 -- that is not elaborated on the fly for inlining purposes.
12232
12233 if not Inlined_Body then
12234 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12235 end if;
12236
12237 Restore_Env;
12238
12239 -- If we have no body, and the unit requires a body, then complain. This
12240 -- complaint is suppressed if we have detected other errors (since a
12241 -- common reason for missing the body is that it had errors).
12242 -- In CodePeer mode, a warning has been emitted already, no need for
12243 -- further messages.
12244
12245 elsif Unit_Requires_Body (Gen_Unit)
12246 and then not Body_Optional
12247 then
12248 if CodePeer_Mode then
12249 null;
12250
12251 elsif Serious_Errors_Detected = 0 then
12252 Error_Msg_NE
12253 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12254
12255 -- Don't attempt to perform any cleanup actions if some other error
12256 -- was already detected, since this can cause blowups.
12257
12258 else
12259 goto Leave;
12260 end if;
12261
12262 -- Case of package that does not need a body
12263
12264 else
12265 -- If the instantiation of the declaration is a library unit, rewrite
12266 -- the original package instantiation as a package declaration in the
12267 -- compilation unit node.
12268
12269 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12270 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12271 Rewrite (Inst_Node, Act_Decl);
12272
12273 -- Generate elaboration entity, in case spec has elaboration code.
12274 -- This cannot be done when the instance is analyzed, because it
12275 -- is not known yet whether the body exists.
12276
12277 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12278 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12279
12280 -- If the instantiation is not a library unit, then append the
12281 -- declaration to the list of implicitly generated entities, unless
12282 -- it is already a list member which means that it was already
12283 -- processed
12284
12285 elsif not Is_List_Member (Act_Decl) then
12286 Mark_Rewrite_Insertion (Act_Decl);
12287 Insert_Before (Inst_Node, Act_Decl);
12288 end if;
12289 end if;
12290
12291 <<Leave>>
12292
12293 -- Restore the context that was in effect prior to instantiating the
12294 -- package body.
12295
12296 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12297 Local_Suppress_Stack_Top := Saved_LSST;
12298 Scope_Suppress := Saved_SS;
12299 Style_Check := Saved_SC;
12300
12301 Expander_Mode_Restore;
12302 Restore_Config_Switches (Saved_CS);
12303 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12304 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12305 Restore_Warnings (Saved_Warn);
12306 end Instantiate_Package_Body;
12307
12308 ---------------------------------
12309 -- Instantiate_Subprogram_Body --
12310 ---------------------------------
12311
12312 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12313 -- must be replaced by gotos which jump to the end of the routine in order
12314 -- to restore the Ghost and SPARK modes.
12315
12316 procedure Instantiate_Subprogram_Body
12317 (Body_Info : Pending_Body_Info;
12318 Body_Optional : Boolean := False)
12319 is
12320 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12321 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12322 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12323 Gen_Id : constant Node_Id := Name (Inst_Node);
12324 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12325 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12326 Loc : constant Source_Ptr := Sloc (Inst_Node);
12327 Pack_Id : constant Entity_Id :=
12328 Defining_Unit_Name (Parent (Act_Decl));
12329
12330 -- The following constants capture the context prior to instantiating
12331 -- the subprogram body.
12332
12333 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12334 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12335 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12336 Saved_ISMP : constant Boolean :=
12337 Ignore_SPARK_Mode_Pragmas_In_Instance;
12338 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12339 Local_Suppress_Stack_Top;
12340 Saved_SC : constant Boolean := Style_Check;
12341 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12342 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12343 Saved_SS : constant Suppress_Record := Scope_Suppress;
12344 Saved_Warn : constant Warning_Record := Save_Warnings;
12345
12346 Act_Body : Node_Id;
12347 Act_Body_Id : Entity_Id;
12348 Gen_Body : Node_Id;
12349 Gen_Body_Id : Node_Id;
12350 Pack_Body : Node_Id;
12351 Par_Ent : Entity_Id := Empty;
12352 Par_Installed : Boolean := False;
12353 Par_Vis : Boolean := False;
12354 Ret_Expr : Node_Id;
12355
12356 begin
12357 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12358
12359 -- Subprogram body may have been created already because of an inline
12360 -- pragma, or because of multiple elaborations of the enclosing package
12361 -- when several instances of the subprogram appear in the main unit.
12362
12363 if Present (Corresponding_Body (Act_Decl)) then
12364 return;
12365 end if;
12366
12367 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12368 -- the mode now to ensure that any nodes generated during instantiation
12369 -- are properly marked as Ghost.
12370
12371 Set_Ghost_Mode (Act_Decl_Id);
12372
12373 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12374
12375 -- Re-establish the state of information on which checks are suppressed.
12376 -- This information was set in Body_Info at the point of instantiation,
12377 -- and now we restore it so that the instance is compiled using the
12378 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12379
12380 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12381 Scope_Suppress := Body_Info.Scope_Suppress;
12382
12383 Restore_Config_Switches (Body_Info.Config_Switches);
12384 Restore_Warnings (Body_Info.Warnings);
12385
12386 if No (Gen_Body_Id) then
12387
12388 -- For imported generic subprogram, no body to compile, complete
12389 -- the spec entity appropriately.
12390
12391 if Is_Imported (Gen_Unit) then
12392 Set_Is_Imported (Act_Decl_Id);
12393 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12394 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12395 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12396 Set_Has_Completion (Act_Decl_Id);
12397 goto Leave;
12398
12399 -- For other cases, compile the body
12400
12401 else
12402 Load_Parent_Of_Generic
12403 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12404 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12405 end if;
12406 end if;
12407
12408 Instantiation_Node := Inst_Node;
12409
12410 if Present (Gen_Body_Id) then
12411 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12412
12413 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12414
12415 -- Either body is not present, or context is non-expanding, as
12416 -- when compiling a subunit. Mark the instance as completed, and
12417 -- diagnose a missing body when needed.
12418
12419 if Expander_Active
12420 and then Operating_Mode = Generate_Code
12421 then
12422 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12423 end if;
12424
12425 Set_Has_Completion (Act_Decl_Id);
12426 goto Leave;
12427 end if;
12428
12429 Save_Env (Gen_Unit, Act_Decl_Id);
12430 Style_Check := False;
12431
12432 -- If the context of the instance is subject to SPARK_Mode "off", the
12433 -- annotation is missing, or the body is instantiated at a later pass
12434 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12435 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12436 -- instance.
12437
12438 if SPARK_Mode /= On
12439 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12440 then
12441 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12442 end if;
12443
12444 -- If the context of an instance is not subject to SPARK_Mode "off",
12445 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12446 -- the latter should be the one applicable to the instance.
12447
12448 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12449 and then SPARK_Mode /= Off
12450 and then Present (SPARK_Pragma (Gen_Body_Id))
12451 then
12452 Set_SPARK_Mode (Gen_Body_Id);
12453 end if;
12454
12455 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12456 Create_Instantiation_Source
12457 (Inst_Node,
12458 Gen_Body_Id,
12459 S_Adjustment);
12460
12461 Act_Body :=
12462 Copy_Generic_Node
12463 (Original_Node (Gen_Body), Empty, Instantiating => True);
12464
12465 -- Create proper defining name for the body, to correspond to the one
12466 -- in the spec.
12467
12468 Act_Body_Id :=
12469 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12470
12471 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12472 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12473
12474 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12475 Set_Has_Completion (Act_Decl_Id);
12476 Check_Generic_Actuals (Pack_Id, False);
12477
12478 -- Generate a reference to link the visible subprogram instance to
12479 -- the generic body, which for navigation purposes is the only
12480 -- available source for the instance.
12481
12482 Generate_Reference
12483 (Related_Instance (Pack_Id),
12484 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12485
12486 -- If it is a child unit, make the parent instance (which is an
12487 -- instance of the parent of the generic) visible. The parent
12488 -- instance is the prefix of the name of the generic unit.
12489
12490 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12491 and then Nkind (Gen_Id) = N_Expanded_Name
12492 then
12493 Par_Ent := Entity (Prefix (Gen_Id));
12494 Par_Vis := Is_Immediately_Visible (Par_Ent);
12495 Install_Parent (Par_Ent, In_Body => True);
12496 Par_Installed := True;
12497
12498 elsif Is_Child_Unit (Gen_Unit) then
12499 Par_Ent := Scope (Gen_Unit);
12500 Par_Vis := Is_Immediately_Visible (Par_Ent);
12501 Install_Parent (Par_Ent, In_Body => True);
12502 Par_Installed := True;
12503 end if;
12504
12505 -- Subprogram body is placed in the body of wrapper package,
12506 -- whose spec contains the subprogram declaration as well as
12507 -- the renaming declarations for the generic parameters.
12508
12509 Pack_Body :=
12510 Make_Package_Body (Loc,
12511 Defining_Unit_Name => New_Copy (Pack_Id),
12512 Declarations => New_List (Act_Body));
12513
12514 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12515
12516 -- If the instantiation is a library unit, then build resulting
12517 -- compilation unit nodes for the instance. The declaration of
12518 -- the enclosing package is the grandparent of the subprogram
12519 -- declaration. First replace the instantiation node as the unit
12520 -- of the corresponding compilation.
12521
12522 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12523 if Parent (Inst_Node) = Cunit (Main_Unit) then
12524 Set_Unit (Parent (Inst_Node), Inst_Node);
12525 Build_Instance_Compilation_Unit_Nodes
12526 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12527 Analyze (Inst_Node);
12528 else
12529 Set_Parent (Pack_Body, Parent (Inst_Node));
12530 Analyze (Pack_Body);
12531 end if;
12532
12533 else
12534 Insert_Before (Inst_Node, Pack_Body);
12535 Mark_Rewrite_Insertion (Pack_Body);
12536 Analyze (Pack_Body);
12537
12538 if Expander_Active then
12539 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
12540 end if;
12541 end if;
12542
12543 Inherit_Context (Gen_Body, Inst_Node);
12544
12545 Restore_Private_Views (Pack_Id, False);
12546
12547 if Par_Installed then
12548 Remove_Parent (In_Body => True);
12549
12550 -- Restore the previous visibility of the parent
12551
12552 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12553 end if;
12554
12555 Restore_Env;
12556
12557 -- Body not found. Error was emitted already. If there were no previous
12558 -- errors, this may be an instance whose scope is a premature instance.
12559 -- In that case we must insure that the (legal) program does raise
12560 -- program error if executed. We generate a subprogram body for this
12561 -- purpose. See DEC ac30vso.
12562
12563 -- Should not reference proprietary DEC tests in comments ???
12564
12565 elsif Serious_Errors_Detected = 0
12566 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12567 then
12568 if Body_Optional then
12569 goto Leave;
12570
12571 elsif Ekind (Act_Decl_Id) = E_Procedure then
12572 Act_Body :=
12573 Make_Subprogram_Body (Loc,
12574 Specification =>
12575 Make_Procedure_Specification (Loc,
12576 Defining_Unit_Name =>
12577 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12578 Parameter_Specifications =>
12579 New_Copy_List
12580 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12581
12582 Declarations => Empty_List,
12583 Handled_Statement_Sequence =>
12584 Make_Handled_Sequence_Of_Statements (Loc,
12585 Statements => New_List (
12586 Make_Raise_Program_Error (Loc,
12587 Reason => PE_Access_Before_Elaboration))));
12588
12589 else
12590 Ret_Expr :=
12591 Make_Raise_Program_Error (Loc,
12592 Reason => PE_Access_Before_Elaboration);
12593
12594 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12595 Set_Analyzed (Ret_Expr);
12596
12597 Act_Body :=
12598 Make_Subprogram_Body (Loc,
12599 Specification =>
12600 Make_Function_Specification (Loc,
12601 Defining_Unit_Name =>
12602 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12603 Parameter_Specifications =>
12604 New_Copy_List
12605 (Parameter_Specifications (Parent (Act_Decl_Id))),
12606 Result_Definition =>
12607 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12608
12609 Declarations => Empty_List,
12610 Handled_Statement_Sequence =>
12611 Make_Handled_Sequence_Of_Statements (Loc,
12612 Statements => New_List (
12613 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12614 end if;
12615
12616 Pack_Body :=
12617 Make_Package_Body (Loc,
12618 Defining_Unit_Name => New_Copy (Pack_Id),
12619 Declarations => New_List (Act_Body));
12620
12621 Insert_After (Inst_Node, Pack_Body);
12622 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12623 Analyze (Pack_Body);
12624 end if;
12625
12626 <<Leave>>
12627
12628 -- Restore the context that was in effect prior to instantiating the
12629 -- subprogram body.
12630
12631 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12632 Local_Suppress_Stack_Top := Saved_LSST;
12633 Scope_Suppress := Saved_SS;
12634 Style_Check := Saved_SC;
12635
12636 Expander_Mode_Restore;
12637 Restore_Config_Switches (Saved_CS);
12638 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12639 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12640 Restore_Warnings (Saved_Warn);
12641 end Instantiate_Subprogram_Body;
12642
12643 ----------------------
12644 -- Instantiate_Type --
12645 ----------------------
12646
12647 function Instantiate_Type
12648 (Formal : Node_Id;
12649 Actual : Node_Id;
12650 Analyzed_Formal : Node_Id;
12651 Actual_Decls : List_Id) return List_Id
12652 is
12653 A_Gen_T : constant Entity_Id :=
12654 Defining_Identifier (Analyzed_Formal);
12655 Def : constant Node_Id := Formal_Type_Definition (Formal);
12656 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12657 Act_T : Entity_Id;
12658 Ancestor : Entity_Id := Empty;
12659 Decl_Node : Node_Id;
12660 Decl_Nodes : List_Id;
12661 Loc : Source_Ptr;
12662 Subt : Entity_Id;
12663
12664 procedure Check_Shared_Variable_Control_Aspects;
12665 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12666 -- that may be specified for a formal type are obeyed by the actual.
12667
12668 procedure Diagnose_Predicated_Actual;
12669 -- There are a number of constructs in which a discrete type with
12670 -- predicates is illegal, e.g. as an index in an array type declaration.
12671 -- If a generic type is used is such a construct in a generic package
12672 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12673 -- of the generic contract that the actual cannot have predicates.
12674
12675 procedure Validate_Array_Type_Instance;
12676 procedure Validate_Access_Subprogram_Instance;
12677 procedure Validate_Access_Type_Instance;
12678 procedure Validate_Derived_Type_Instance;
12679 procedure Validate_Derived_Interface_Type_Instance;
12680 procedure Validate_Discriminated_Formal_Type;
12681 procedure Validate_Interface_Type_Instance;
12682 procedure Validate_Private_Type_Instance;
12683 procedure Validate_Incomplete_Type_Instance;
12684 -- These procedures perform validation tests for the named case.
12685 -- Validate_Discriminated_Formal_Type is shared by formal private
12686 -- types and Ada 2012 formal incomplete types.
12687
12688 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12689 -- Check that base types are the same and that the subtypes match
12690 -- statically. Used in several of the above.
12691
12692 --------------------------------------------
12693 -- Check_Shared_Variable_Control_Aspects --
12694 --------------------------------------------
12695
12696 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12697 -- that may be specified for the formal are obeyed by the actual.
12698 -- If the formal is a derived type the aspect specifications must match.
12699 -- NOTE: AI12-0282 implies that matching of aspects is required between
12700 -- formal and actual in all cases, but this is too restrictive.
12701 -- In particular it violates a language design rule: a limited private
12702 -- indefinite formal can be matched by any actual. The current code
12703 -- reflects an older and more permissive version of RM C.6 (12/5).
12704
12705 procedure Check_Shared_Variable_Control_Aspects is
12706 begin
12707 if Ada_Version >= Ada_2020 then
12708 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12709 Error_Msg_NE
12710 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12711
12712 elsif Is_Derived_Type (A_Gen_T)
12713 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12714 then
12715 Error_Msg_NE
12716 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12717 end if;
12718
12719 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12720 Error_Msg_NE
12721 ("actual for& must have Volatile aspect",
12722 Actual, A_Gen_T);
12723
12724 elsif Is_Derived_Type (A_Gen_T)
12725 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12726 then
12727 Error_Msg_NE
12728 ("actual for& has different Volatile aspect",
12729 Actual, A_Gen_T);
12730 end if;
12731
12732 -- We assume that an array type whose atomic component type
12733 -- is Atomic is equivalent to an array type with the explicit
12734 -- aspect Has_Atomic_Components. This is a reasonable inference
12735 -- from the intent of AI12-0282, and makes it legal to use an
12736 -- actual that does not have the identical aspect as the formal.
12737 -- Ditto for volatile components.
12738
12739 declare
12740 Actual_Atomic_Comp : constant Boolean :=
12741 Has_Atomic_Components (Act_T)
12742 or else (Is_Array_Type (Act_T)
12743 and then Is_Atomic (Component_Type (Act_T)));
12744 begin
12745 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12746 Error_Msg_NE
12747 ("formal and actual for& must agree on atomic components",
12748 Actual, A_Gen_T);
12749 end if;
12750 end;
12751
12752 declare
12753 Actual_Volatile_Comp : constant Boolean :=
12754 Has_Volatile_Components (Act_T)
12755 or else (Is_Array_Type (Act_T)
12756 and then Is_Volatile (Component_Type (Act_T)));
12757 begin
12758 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12759 then
12760 Error_Msg_NE
12761 ("actual for& must have volatile components",
12762 Actual, A_Gen_T);
12763 end if;
12764 end;
12765
12766 -- The following two aspects do not require exact matching,
12767 -- but only one-way agreement. See RM C.6.
12768
12769 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12770 then
12771 Error_Msg_NE
12772 ("actual for& must have Independent aspect specified",
12773 Actual, A_Gen_T);
12774 end if;
12775
12776 if Has_Independent_Components (A_Gen_T)
12777 and then not Has_Independent_Components (Act_T)
12778 then
12779 Error_Msg_NE
12780 ("actual for& must have Independent_Components specified",
12781 Actual, A_Gen_T);
12782 end if;
12783
12784 -- Check actual/formal compatibility with respect to the four
12785 -- volatility refinement aspects.
12786
12787 Check_Volatility_Compatibility
12788 (Act_T, A_Gen_T,
12789 "actual type", "its corresponding formal type",
12790 Srcpos_Bearer => Act_T);
12791 end if;
12792 end Check_Shared_Variable_Control_Aspects;
12793
12794 ---------------------------------
12795 -- Diagnose_Predicated_Actual --
12796 ---------------------------------
12797
12798 procedure Diagnose_Predicated_Actual is
12799 begin
12800 if No_Predicate_On_Actual (A_Gen_T)
12801 and then Has_Predicates (Act_T)
12802 then
12803 Error_Msg_NE
12804 ("actual for& cannot be a type with predicate",
12805 Instantiation_Node, A_Gen_T);
12806
12807 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12808 and then Has_Predicates (Act_T)
12809 and then not Has_Static_Predicate_Aspect (Act_T)
12810 then
12811 Error_Msg_NE
12812 ("actual for& cannot be a type with a dynamic predicate",
12813 Instantiation_Node, A_Gen_T);
12814 end if;
12815 end Diagnose_Predicated_Actual;
12816
12817 --------------------
12818 -- Subtypes_Match --
12819 --------------------
12820
12821 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12822 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12823
12824 begin
12825 -- Some detailed comments would be useful here ???
12826
12827 return ((Base_Type (T) = Act_T
12828 or else Base_Type (T) = Base_Type (Act_T))
12829 and then Subtypes_Statically_Match (T, Act_T))
12830
12831 or else (Is_Class_Wide_Type (Gen_T)
12832 and then Is_Class_Wide_Type (Act_T)
12833 and then Subtypes_Match
12834 (Get_Instance_Of (Root_Type (Gen_T)),
12835 Root_Type (Act_T)))
12836
12837 or else
12838 (Ekind (Gen_T) in E_Anonymous_Access_Subprogram_Type
12839 | E_Anonymous_Access_Type
12840 and then Ekind (Act_T) = Ekind (Gen_T)
12841 and then Subtypes_Statically_Match
12842 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12843 end Subtypes_Match;
12844
12845 -----------------------------------------
12846 -- Validate_Access_Subprogram_Instance --
12847 -----------------------------------------
12848
12849 procedure Validate_Access_Subprogram_Instance is
12850 begin
12851 if not Is_Access_Type (Act_T)
12852 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12853 then
12854 Error_Msg_NE
12855 ("expect access type in instantiation of &", Actual, Gen_T);
12856 Abandon_Instantiation (Actual);
12857 end if;
12858
12859 -- According to AI05-288, actuals for access_to_subprograms must be
12860 -- subtype conformant with the generic formal. Previous to AI05-288
12861 -- only mode conformance was required.
12862
12863 -- This is a binding interpretation that applies to previous versions
12864 -- of the language, no need to maintain previous weaker checks.
12865
12866 Check_Subtype_Conformant
12867 (Designated_Type (Act_T),
12868 Designated_Type (A_Gen_T),
12869 Actual,
12870 Get_Inst => True);
12871
12872 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12873 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12874 Error_Msg_NE
12875 ("protected access type not allowed for formal &",
12876 Actual, Gen_T);
12877 end if;
12878
12879 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12880 Error_Msg_NE
12881 ("expect protected access type for formal &",
12882 Actual, Gen_T);
12883 end if;
12884
12885 -- If the formal has a specified convention (which in most cases
12886 -- will be StdCall) verify that the actual has the same convention.
12887
12888 if Has_Convention_Pragma (A_Gen_T)
12889 and then Convention (A_Gen_T) /= Convention (Act_T)
12890 then
12891 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12892 Error_Msg_NE
12893 ("actual for formal & must have convention %", Actual, Gen_T);
12894 end if;
12895
12896 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12897 Error_Msg_NE
12898 ("non null exclusion of actual and formal & do not match",
12899 Actual, Gen_T);
12900 end if;
12901 end Validate_Access_Subprogram_Instance;
12902
12903 -----------------------------------
12904 -- Validate_Access_Type_Instance --
12905 -----------------------------------
12906
12907 procedure Validate_Access_Type_Instance is
12908 Desig_Type : constant Entity_Id :=
12909 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12910 Desig_Act : Entity_Id;
12911
12912 begin
12913 if not Is_Access_Type (Act_T) then
12914 Error_Msg_NE
12915 ("expect access type in instantiation of &", Actual, Gen_T);
12916 Abandon_Instantiation (Actual);
12917 end if;
12918
12919 if Is_Access_Constant (A_Gen_T) then
12920 if not Is_Access_Constant (Act_T) then
12921 Error_Msg_N
12922 ("actual type must be access-to-constant type", Actual);
12923 Abandon_Instantiation (Actual);
12924 end if;
12925 else
12926 if Is_Access_Constant (Act_T) then
12927 Error_Msg_N
12928 ("actual type must be access-to-variable type", Actual);
12929 Abandon_Instantiation (Actual);
12930
12931 elsif Ekind (A_Gen_T) = E_General_Access_Type
12932 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12933 then
12934 Error_Msg_N -- CODEFIX
12935 ("actual must be general access type!", Actual);
12936 Error_Msg_NE -- CODEFIX
12937 ("add ALL to }!", Actual, Act_T);
12938 Abandon_Instantiation (Actual);
12939 end if;
12940 end if;
12941
12942 -- The designated subtypes, that is to say the subtypes introduced
12943 -- by an access type declaration (and not by a subtype declaration)
12944 -- must match.
12945
12946 Desig_Act := Designated_Type (Base_Type (Act_T));
12947
12948 -- The designated type may have been introduced through a limited_
12949 -- with clause, in which case retrieve the non-limited view. This
12950 -- applies to incomplete types as well as to class-wide types.
12951
12952 if From_Limited_With (Desig_Act) then
12953 Desig_Act := Available_View (Desig_Act);
12954 end if;
12955
12956 if not Subtypes_Match (Desig_Type, Desig_Act) then
12957 Error_Msg_NE
12958 ("designated type of actual does not match that of formal &",
12959 Actual, Gen_T);
12960
12961 if not Predicates_Match (Desig_Type, Desig_Act) then
12962 Error_Msg_N ("\predicates do not match", Actual);
12963 end if;
12964
12965 Abandon_Instantiation (Actual);
12966
12967 elsif Is_Access_Type (Designated_Type (Act_T))
12968 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12969 /=
12970 Is_Constrained (Designated_Type (Desig_Type))
12971 then
12972 Error_Msg_NE
12973 ("designated type of actual does not match that of formal &",
12974 Actual, Gen_T);
12975
12976 if not Predicates_Match (Desig_Type, Desig_Act) then
12977 Error_Msg_N ("\predicates do not match", Actual);
12978 end if;
12979
12980 Abandon_Instantiation (Actual);
12981 end if;
12982
12983 -- Ada 2005: null-exclusion indicators of the two types must agree
12984
12985 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12986 Error_Msg_NE
12987 ("non null exclusion of actual and formal & do not match",
12988 Actual, Gen_T);
12989 end if;
12990 end Validate_Access_Type_Instance;
12991
12992 ----------------------------------
12993 -- Validate_Array_Type_Instance --
12994 ----------------------------------
12995
12996 procedure Validate_Array_Type_Instance is
12997 I1 : Node_Id;
12998 I2 : Node_Id;
12999 T2 : Entity_Id;
13000
13001 function Formal_Dimensions return Nat;
13002 -- Count number of dimensions in array type formal
13003
13004 -----------------------
13005 -- Formal_Dimensions --
13006 -----------------------
13007
13008 function Formal_Dimensions return Nat is
13009 Num : Nat := 0;
13010 Index : Node_Id;
13011
13012 begin
13013 if Nkind (Def) = N_Constrained_Array_Definition then
13014 Index := First (Discrete_Subtype_Definitions (Def));
13015 else
13016 Index := First (Subtype_Marks (Def));
13017 end if;
13018
13019 while Present (Index) loop
13020 Num := Num + 1;
13021 Next_Index (Index);
13022 end loop;
13023
13024 return Num;
13025 end Formal_Dimensions;
13026
13027 -- Start of processing for Validate_Array_Type_Instance
13028
13029 begin
13030 if not Is_Array_Type (Act_T) then
13031 Error_Msg_NE
13032 ("expect array type in instantiation of &", Actual, Gen_T);
13033 Abandon_Instantiation (Actual);
13034
13035 elsif Nkind (Def) = N_Constrained_Array_Definition then
13036 if not (Is_Constrained (Act_T)) then
13037 Error_Msg_NE
13038 ("expect constrained array in instantiation of &",
13039 Actual, Gen_T);
13040 Abandon_Instantiation (Actual);
13041 end if;
13042
13043 else
13044 if Is_Constrained (Act_T) then
13045 Error_Msg_NE
13046 ("expect unconstrained array in instantiation of &",
13047 Actual, Gen_T);
13048 Abandon_Instantiation (Actual);
13049 end if;
13050 end if;
13051
13052 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13053 Error_Msg_NE
13054 ("dimensions of actual do not match formal &", Actual, Gen_T);
13055 Abandon_Instantiation (Actual);
13056 end if;
13057
13058 I1 := First_Index (A_Gen_T);
13059 I2 := First_Index (Act_T);
13060 for J in 1 .. Formal_Dimensions loop
13061
13062 -- If the indexes of the actual were given by a subtype_mark,
13063 -- the index was transformed into a range attribute. Retrieve
13064 -- the original type mark for checking.
13065
13066 if Is_Entity_Name (Original_Node (I2)) then
13067 T2 := Entity (Original_Node (I2));
13068 else
13069 T2 := Etype (I2);
13070 end if;
13071
13072 if not Subtypes_Match
13073 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13074 then
13075 Error_Msg_NE
13076 ("index types of actual do not match those of formal &",
13077 Actual, Gen_T);
13078 Abandon_Instantiation (Actual);
13079 end if;
13080
13081 Next_Index (I1);
13082 Next_Index (I2);
13083 end loop;
13084
13085 -- Check matching subtypes. Note that there are complex visibility
13086 -- issues when the generic is a child unit and some aspect of the
13087 -- generic type is declared in a parent unit of the generic. We do
13088 -- the test to handle this special case only after a direct check
13089 -- for static matching has failed. The case where both the component
13090 -- type and the array type are separate formals, and the component
13091 -- type is a private view may also require special checking in
13092 -- Subtypes_Match. Finally, we assume that a child instance where
13093 -- the component type comes from a formal of a parent instance is
13094 -- correct because the generic was correct. A more precise check
13095 -- seems too complex to install???
13096
13097 if Subtypes_Match
13098 (Component_Type (A_Gen_T), Component_Type (Act_T))
13099 or else
13100 Subtypes_Match
13101 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13102 Component_Type (Act_T))
13103 or else
13104 (not Inside_A_Generic
13105 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13106 then
13107 null;
13108 else
13109 Error_Msg_NE
13110 ("component subtype of actual does not match that of formal &",
13111 Actual, Gen_T);
13112 Abandon_Instantiation (Actual);
13113 end if;
13114
13115 if Has_Aliased_Components (A_Gen_T)
13116 and then not Has_Aliased_Components (Act_T)
13117 then
13118 Error_Msg_NE
13119 ("actual must have aliased components to match formal type &",
13120 Actual, Gen_T);
13121 end if;
13122 end Validate_Array_Type_Instance;
13123
13124 -----------------------------------------------
13125 -- Validate_Derived_Interface_Type_Instance --
13126 -----------------------------------------------
13127
13128 procedure Validate_Derived_Interface_Type_Instance is
13129 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13130 Elmt : Elmt_Id;
13131
13132 begin
13133 -- First apply interface instance checks
13134
13135 Validate_Interface_Type_Instance;
13136
13137 -- Verify that immediate parent interface is an ancestor of
13138 -- the actual.
13139
13140 if Present (Par)
13141 and then not Interface_Present_In_Ancestor (Act_T, Par)
13142 then
13143 Error_Msg_NE
13144 ("interface actual must include progenitor&", Actual, Par);
13145 end if;
13146
13147 -- Now verify that the actual includes all other ancestors of
13148 -- the formal.
13149
13150 Elmt := First_Elmt (Interfaces (A_Gen_T));
13151 while Present (Elmt) loop
13152 if not Interface_Present_In_Ancestor
13153 (Act_T, Get_Instance_Of (Node (Elmt)))
13154 then
13155 Error_Msg_NE
13156 ("interface actual must include progenitor&",
13157 Actual, Node (Elmt));
13158 end if;
13159
13160 Next_Elmt (Elmt);
13161 end loop;
13162 end Validate_Derived_Interface_Type_Instance;
13163
13164 ------------------------------------
13165 -- Validate_Derived_Type_Instance --
13166 ------------------------------------
13167
13168 procedure Validate_Derived_Type_Instance is
13169 Actual_Discr : Entity_Id;
13170 Ancestor_Discr : Entity_Id;
13171
13172 begin
13173 -- Verify that the actual includes the progenitors of the formal,
13174 -- if any. The formal may depend on previous formals and their
13175 -- instance, so we must examine instance of interfaces if present.
13176 -- The actual may be an extension of an interface, in which case
13177 -- it does not appear in the interface list, so this must be
13178 -- checked separately.
13179
13180 if Present (Interface_List (Def)) then
13181 if not Has_Interfaces (Act_T) then
13182 Error_Msg_NE
13183 ("actual must implement all interfaces of formal&",
13184 Actual, A_Gen_T);
13185
13186 else
13187 declare
13188 Act_Iface_List : Elist_Id;
13189 Iface : Node_Id;
13190 Iface_Ent : Entity_Id;
13191
13192 function Instance_Exists (I : Entity_Id) return Boolean;
13193 -- If the interface entity is declared in a generic unit,
13194 -- this can only be legal if we are within an instantiation
13195 -- of a child of that generic. There is currently no
13196 -- mechanism to relate an interface declared within a
13197 -- generic to the corresponding interface in an instance,
13198 -- so we traverse the list of interfaces of the actual,
13199 -- looking for a name match.
13200
13201 ---------------------
13202 -- Instance_Exists --
13203 ---------------------
13204
13205 function Instance_Exists (I : Entity_Id) return Boolean is
13206 Iface_Elmt : Elmt_Id;
13207
13208 begin
13209 Iface_Elmt := First_Elmt (Act_Iface_List);
13210 while Present (Iface_Elmt) loop
13211 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13212 and then Chars (Node (Iface_Elmt)) = Chars (I)
13213 then
13214 return True;
13215 end if;
13216
13217 Next_Elmt (Iface_Elmt);
13218 end loop;
13219
13220 return False;
13221 end Instance_Exists;
13222
13223 begin
13224 Iface := First (Abstract_Interface_List (A_Gen_T));
13225 Collect_Interfaces (Act_T, Act_Iface_List);
13226
13227 while Present (Iface) loop
13228 Iface_Ent := Get_Instance_Of (Entity (Iface));
13229
13230 if Is_Ancestor (Iface_Ent, Act_T)
13231 or else Is_Progenitor (Iface_Ent, Act_T)
13232 then
13233 null;
13234
13235 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13236 and then Instance_Exists (Iface_Ent)
13237 then
13238 null;
13239
13240 else
13241 Error_Msg_Name_1 := Chars (Act_T);
13242 Error_Msg_NE
13243 ("Actual% must implement interface&",
13244 Actual, Etype (Iface));
13245 end if;
13246
13247 Next (Iface);
13248 end loop;
13249 end;
13250 end if;
13251 end if;
13252
13253 -- If the parent type in the generic declaration is itself a previous
13254 -- formal type, then it is local to the generic and absent from the
13255 -- analyzed generic definition. In that case the ancestor is the
13256 -- instance of the formal (which must have been instantiated
13257 -- previously), unless the ancestor is itself a formal derived type.
13258 -- In this latter case (which is the subject of Corrigendum 8652/0038
13259 -- (AI-202) the ancestor of the formals is the ancestor of its
13260 -- parent. Otherwise, the analyzed generic carries the parent type.
13261 -- If the parent type is defined in a previous formal package, then
13262 -- the scope of that formal package is that of the generic type
13263 -- itself, and it has already been mapped into the corresponding type
13264 -- in the actual package.
13265
13266 -- Common case: parent type defined outside of the generic
13267
13268 if Is_Entity_Name (Subtype_Mark (Def))
13269 and then Present (Entity (Subtype_Mark (Def)))
13270 then
13271 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13272
13273 -- Check whether parent is defined in a previous formal package
13274
13275 elsif
13276 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13277 then
13278 Ancestor :=
13279 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13280
13281 -- The type may be a local derivation, or a type extension of a
13282 -- previous formal, or of a formal of a parent package.
13283
13284 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13285 or else
13286 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13287 then
13288 -- Check whether the parent is another derived formal type in the
13289 -- same generic unit.
13290
13291 if Etype (A_Gen_T) /= A_Gen_T
13292 and then Is_Generic_Type (Etype (A_Gen_T))
13293 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13294 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13295 then
13296 -- Locate ancestor of parent from the subtype declaration
13297 -- created for the actual.
13298
13299 declare
13300 Decl : Node_Id;
13301
13302 begin
13303 Decl := First (Actual_Decls);
13304 while Present (Decl) loop
13305 if Nkind (Decl) = N_Subtype_Declaration
13306 and then Chars (Defining_Identifier (Decl)) =
13307 Chars (Etype (A_Gen_T))
13308 then
13309 Ancestor := Generic_Parent_Type (Decl);
13310 exit;
13311 else
13312 Next (Decl);
13313 end if;
13314 end loop;
13315 end;
13316
13317 pragma Assert (Present (Ancestor));
13318
13319 -- The ancestor itself may be a previous formal that has been
13320 -- instantiated.
13321
13322 Ancestor := Get_Instance_Of (Ancestor);
13323
13324 else
13325 Ancestor :=
13326 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13327 end if;
13328
13329 -- Check whether parent is a previous formal of the current generic
13330
13331 elsif Is_Derived_Type (A_Gen_T)
13332 and then Is_Generic_Type (Etype (A_Gen_T))
13333 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13334 then
13335 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13336
13337 -- An unusual case: the actual is a type declared in a parent unit,
13338 -- but is not a formal type so there is no instance_of for it.
13339 -- Retrieve it by analyzing the record extension.
13340
13341 elsif Is_Child_Unit (Scope (A_Gen_T))
13342 and then In_Open_Scopes (Scope (Act_T))
13343 and then Is_Generic_Instance (Scope (Act_T))
13344 then
13345 Analyze (Subtype_Mark (Def));
13346 Ancestor := Entity (Subtype_Mark (Def));
13347
13348 else
13349 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13350 end if;
13351
13352 -- If the formal derived type has pragma Preelaborable_Initialization
13353 -- then the actual type must have preelaborable initialization.
13354
13355 if Known_To_Have_Preelab_Init (A_Gen_T)
13356 and then not Has_Preelaborable_Initialization (Act_T)
13357 then
13358 Error_Msg_NE
13359 ("actual for & must have preelaborable initialization",
13360 Actual, Gen_T);
13361 end if;
13362
13363 -- Ada 2005 (AI-251)
13364
13365 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13366 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13367 Error_Msg_NE
13368 ("(Ada 2005) expected type implementing & in instantiation",
13369 Actual, Ancestor);
13370 end if;
13371
13372 -- Finally verify that the (instance of) the ancestor is an ancestor
13373 -- of the actual.
13374
13375 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13376 Error_Msg_NE
13377 ("expect type derived from & in instantiation",
13378 Actual, First_Subtype (Ancestor));
13379 Abandon_Instantiation (Actual);
13380 end if;
13381
13382 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13383 -- that the formal type declaration has been rewritten as a private
13384 -- extension.
13385
13386 if Ada_Version >= Ada_2005
13387 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13388 and then Synchronized_Present (Parent (A_Gen_T))
13389 then
13390 -- The actual must be a synchronized tagged type
13391
13392 if not Is_Tagged_Type (Act_T) then
13393 Error_Msg_N
13394 ("actual of synchronized type must be tagged", Actual);
13395 Abandon_Instantiation (Actual);
13396
13397 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13398 and then Nkind (Type_Definition (Parent (Act_T))) =
13399 N_Derived_Type_Definition
13400 and then not Synchronized_Present
13401 (Type_Definition (Parent (Act_T)))
13402 then
13403 Error_Msg_N
13404 ("actual of synchronized type must be synchronized", Actual);
13405 Abandon_Instantiation (Actual);
13406 end if;
13407 end if;
13408
13409 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13410 -- removes the second instance of the phrase "or allow pass by copy".
13411
13412 -- For Ada 2020, the aspect may be specified explicitly for the
13413 -- formal regardless of whether an ancestor obeys it.
13414
13415 if Is_Atomic (Act_T)
13416 and then not Is_Atomic (Ancestor)
13417 and then not Is_Atomic (A_Gen_T)
13418 then
13419 Error_Msg_N
13420 ("cannot have atomic actual type for non-atomic formal type",
13421 Actual);
13422
13423 elsif Is_Volatile (Act_T)
13424 and then not Is_Volatile (Ancestor)
13425 and then not Is_Volatile (A_Gen_T)
13426 then
13427 Error_Msg_N
13428 ("cannot have volatile actual type for non-volatile formal type",
13429 Actual);
13430 end if;
13431
13432 -- It should not be necessary to check for unknown discriminants on
13433 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13434 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13435 -- needs fixing. ???
13436
13437 if Is_Definite_Subtype (A_Gen_T)
13438 and then not Unknown_Discriminants_Present (Formal)
13439 and then not Is_Definite_Subtype (Act_T)
13440 then
13441 Error_Msg_N ("actual subtype must be constrained", Actual);
13442 Abandon_Instantiation (Actual);
13443 end if;
13444
13445 if not Unknown_Discriminants_Present (Formal) then
13446 if Is_Constrained (Ancestor) then
13447 if not Is_Constrained (Act_T) then
13448 Error_Msg_N ("actual subtype must be constrained", Actual);
13449 Abandon_Instantiation (Actual);
13450 end if;
13451
13452 -- Ancestor is unconstrained, Check if generic formal and actual
13453 -- agree on constrainedness. The check only applies to array types
13454 -- and discriminated types.
13455
13456 elsif Is_Constrained (Act_T) then
13457 if Ekind (Ancestor) = E_Access_Type
13458 or else (not Is_Constrained (A_Gen_T)
13459 and then Is_Composite_Type (A_Gen_T))
13460 then
13461 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13462 Abandon_Instantiation (Actual);
13463 end if;
13464
13465 -- A class-wide type is only allowed if the formal has unknown
13466 -- discriminants.
13467
13468 elsif Is_Class_Wide_Type (Act_T)
13469 and then not Has_Unknown_Discriminants (Ancestor)
13470 then
13471 Error_Msg_NE
13472 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13473 Abandon_Instantiation (Actual);
13474
13475 -- Otherwise, the formal and actual must have the same number
13476 -- of discriminants and each discriminant of the actual must
13477 -- correspond to a discriminant of the formal.
13478
13479 elsif Has_Discriminants (Act_T)
13480 and then not Has_Unknown_Discriminants (Act_T)
13481 and then Has_Discriminants (Ancestor)
13482 then
13483 Actual_Discr := First_Discriminant (Act_T);
13484 Ancestor_Discr := First_Discriminant (Ancestor);
13485 while Present (Actual_Discr)
13486 and then Present (Ancestor_Discr)
13487 loop
13488 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13489 No (Corresponding_Discriminant (Actual_Discr))
13490 then
13491 Error_Msg_NE
13492 ("discriminant & does not correspond "
13493 & "to ancestor discriminant", Actual, Actual_Discr);
13494 Abandon_Instantiation (Actual);
13495 end if;
13496
13497 Next_Discriminant (Actual_Discr);
13498 Next_Discriminant (Ancestor_Discr);
13499 end loop;
13500
13501 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13502 Error_Msg_NE
13503 ("actual for & must have same number of discriminants",
13504 Actual, Gen_T);
13505 Abandon_Instantiation (Actual);
13506 end if;
13507
13508 -- This case should be caught by the earlier check for
13509 -- constrainedness, but the check here is added for completeness.
13510
13511 elsif Has_Discriminants (Act_T)
13512 and then not Has_Unknown_Discriminants (Act_T)
13513 then
13514 Error_Msg_NE
13515 ("actual for & must not have discriminants", Actual, Gen_T);
13516 Abandon_Instantiation (Actual);
13517
13518 elsif Has_Discriminants (Ancestor) then
13519 Error_Msg_NE
13520 ("actual for & must have known discriminants", Actual, Gen_T);
13521 Abandon_Instantiation (Actual);
13522 end if;
13523
13524 if not Subtypes_Statically_Compatible
13525 (Act_T, Ancestor, Formal_Derived_Matching => True)
13526 then
13527 Error_Msg_NE
13528 ("actual for & must be statically compatible with ancestor",
13529 Actual, Gen_T);
13530
13531 if not Predicates_Compatible (Act_T, Ancestor) then
13532 Error_Msg_N
13533 ("\predicate on actual is not compatible with ancestor",
13534 Actual);
13535 end if;
13536
13537 Abandon_Instantiation (Actual);
13538 end if;
13539 end if;
13540
13541 -- If the formal and actual types are abstract, check that there
13542 -- are no abstract primitives of the actual type that correspond to
13543 -- nonabstract primitives of the formal type (second sentence of
13544 -- RM95 3.9.3(9)).
13545
13546 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13547 Check_Abstract_Primitives : declare
13548 Gen_Prims : constant Elist_Id :=
13549 Primitive_Operations (A_Gen_T);
13550 Gen_Elmt : Elmt_Id;
13551 Gen_Subp : Entity_Id;
13552 Anc_Subp : Entity_Id;
13553 Anc_Formal : Entity_Id;
13554 Anc_F_Type : Entity_Id;
13555
13556 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13557 Act_Elmt : Elmt_Id;
13558 Act_Subp : Entity_Id;
13559 Act_Formal : Entity_Id;
13560 Act_F_Type : Entity_Id;
13561
13562 Subprograms_Correspond : Boolean;
13563
13564 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13565 -- Returns true if T2 is derived directly or indirectly from
13566 -- T1, including derivations from interfaces. T1 and T2 are
13567 -- required to be specific tagged base types.
13568
13569 ------------------------
13570 -- Is_Tagged_Ancestor --
13571 ------------------------
13572
13573 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13574 is
13575 Intfc_Elmt : Elmt_Id;
13576
13577 begin
13578 -- The predicate is satisfied if the types are the same
13579
13580 if T1 = T2 then
13581 return True;
13582
13583 -- If we've reached the top of the derivation chain then
13584 -- we know that T1 is not an ancestor of T2.
13585
13586 elsif Etype (T2) = T2 then
13587 return False;
13588
13589 -- Proceed to check T2's immediate parent
13590
13591 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13592 return True;
13593
13594 -- Finally, check to see if T1 is an ancestor of any of T2's
13595 -- progenitors.
13596
13597 else
13598 Intfc_Elmt := First_Elmt (Interfaces (T2));
13599 while Present (Intfc_Elmt) loop
13600 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13601 return True;
13602 end if;
13603
13604 Next_Elmt (Intfc_Elmt);
13605 end loop;
13606 end if;
13607
13608 return False;
13609 end Is_Tagged_Ancestor;
13610
13611 -- Start of processing for Check_Abstract_Primitives
13612
13613 begin
13614 -- Loop over all of the formal derived type's primitives
13615
13616 Gen_Elmt := First_Elmt (Gen_Prims);
13617 while Present (Gen_Elmt) loop
13618 Gen_Subp := Node (Gen_Elmt);
13619
13620 -- If the primitive of the formal is not abstract, then
13621 -- determine whether there is a corresponding primitive of
13622 -- the actual type that's abstract.
13623
13624 if not Is_Abstract_Subprogram (Gen_Subp) then
13625 Act_Elmt := First_Elmt (Act_Prims);
13626 while Present (Act_Elmt) loop
13627 Act_Subp := Node (Act_Elmt);
13628
13629 -- If we find an abstract primitive of the actual,
13630 -- then we need to test whether it corresponds to the
13631 -- subprogram from which the generic formal primitive
13632 -- is inherited.
13633
13634 if Is_Abstract_Subprogram (Act_Subp) then
13635 Anc_Subp := Alias (Gen_Subp);
13636
13637 -- Test whether we have a corresponding primitive
13638 -- by comparing names, kinds, formal types, and
13639 -- result types.
13640
13641 if Chars (Anc_Subp) = Chars (Act_Subp)
13642 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13643 then
13644 Anc_Formal := First_Formal (Anc_Subp);
13645 Act_Formal := First_Formal (Act_Subp);
13646 while Present (Anc_Formal)
13647 and then Present (Act_Formal)
13648 loop
13649 Anc_F_Type := Etype (Anc_Formal);
13650 Act_F_Type := Etype (Act_Formal);
13651
13652 if Ekind (Anc_F_Type) =
13653 E_Anonymous_Access_Type
13654 then
13655 Anc_F_Type := Designated_Type (Anc_F_Type);
13656
13657 if Ekind (Act_F_Type) =
13658 E_Anonymous_Access_Type
13659 then
13660 Act_F_Type :=
13661 Designated_Type (Act_F_Type);
13662 else
13663 exit;
13664 end if;
13665
13666 elsif
13667 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13668 then
13669 exit;
13670 end if;
13671
13672 Anc_F_Type := Base_Type (Anc_F_Type);
13673 Act_F_Type := Base_Type (Act_F_Type);
13674
13675 -- If the formal is controlling, then the
13676 -- the type of the actual primitive's formal
13677 -- must be derived directly or indirectly
13678 -- from the type of the ancestor primitive's
13679 -- formal.
13680
13681 if Is_Controlling_Formal (Anc_Formal) then
13682 if not Is_Tagged_Ancestor
13683 (Anc_F_Type, Act_F_Type)
13684 then
13685 exit;
13686 end if;
13687
13688 -- Otherwise the types of the formals must
13689 -- be the same.
13690
13691 elsif Anc_F_Type /= Act_F_Type then
13692 exit;
13693 end if;
13694
13695 Next_Entity (Anc_Formal);
13696 Next_Entity (Act_Formal);
13697 end loop;
13698
13699 -- If we traversed through all of the formals
13700 -- then so far the subprograms correspond, so
13701 -- now check that any result types correspond.
13702
13703 if No (Anc_Formal) and then No (Act_Formal) then
13704 Subprograms_Correspond := True;
13705
13706 if Ekind (Act_Subp) = E_Function then
13707 Anc_F_Type := Etype (Anc_Subp);
13708 Act_F_Type := Etype (Act_Subp);
13709
13710 if Ekind (Anc_F_Type) =
13711 E_Anonymous_Access_Type
13712 then
13713 Anc_F_Type :=
13714 Designated_Type (Anc_F_Type);
13715
13716 if Ekind (Act_F_Type) =
13717 E_Anonymous_Access_Type
13718 then
13719 Act_F_Type :=
13720 Designated_Type (Act_F_Type);
13721 else
13722 Subprograms_Correspond := False;
13723 end if;
13724
13725 elsif
13726 Ekind (Act_F_Type)
13727 = E_Anonymous_Access_Type
13728 then
13729 Subprograms_Correspond := False;
13730 end if;
13731
13732 Anc_F_Type := Base_Type (Anc_F_Type);
13733 Act_F_Type := Base_Type (Act_F_Type);
13734
13735 -- Now either the result types must be
13736 -- the same or, if the result type is
13737 -- controlling, the result type of the
13738 -- actual primitive must descend from the
13739 -- result type of the ancestor primitive.
13740
13741 if Subprograms_Correspond
13742 and then Anc_F_Type /= Act_F_Type
13743 and then
13744 Has_Controlling_Result (Anc_Subp)
13745 and then not Is_Tagged_Ancestor
13746 (Anc_F_Type, Act_F_Type)
13747 then
13748 Subprograms_Correspond := False;
13749 end if;
13750 end if;
13751
13752 -- Found a matching subprogram belonging to
13753 -- formal ancestor type, so actual subprogram
13754 -- corresponds and this violates 3.9.3(9).
13755
13756 if Subprograms_Correspond then
13757 Error_Msg_NE
13758 ("abstract subprogram & overrides "
13759 & "nonabstract subprogram of ancestor",
13760 Actual, Act_Subp);
13761 end if;
13762 end if;
13763 end if;
13764 end if;
13765
13766 Next_Elmt (Act_Elmt);
13767 end loop;
13768 end if;
13769
13770 Next_Elmt (Gen_Elmt);
13771 end loop;
13772 end Check_Abstract_Primitives;
13773 end if;
13774
13775 -- Verify that limitedness matches. If parent is a limited
13776 -- interface then the generic formal is not unless declared
13777 -- explicitly so. If not declared limited, the actual cannot be
13778 -- limited (see AI05-0087).
13779
13780 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13781 if not In_Instance then
13782 Error_Msg_NE
13783 ("actual for non-limited & cannot be a limited type",
13784 Actual, Gen_T);
13785 Explain_Limited_Type (Act_T, Actual);
13786 Abandon_Instantiation (Actual);
13787 end if;
13788 end if;
13789
13790 -- Check for AI12-0036
13791
13792 declare
13793 Formal_Is_Private_Extension : constant Boolean :=
13794 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13795
13796 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13797
13798 begin
13799 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13800 if not In_Instance then
13801 if Actual_Is_Tagged then
13802 Error_Msg_NE
13803 ("actual for & cannot be a tagged type", Actual, Gen_T);
13804 else
13805 Error_Msg_NE
13806 ("actual for & must be a tagged type", Actual, Gen_T);
13807 end if;
13808
13809 Abandon_Instantiation (Actual);
13810 end if;
13811 end if;
13812 end;
13813 end Validate_Derived_Type_Instance;
13814
13815 ----------------------------------------
13816 -- Validate_Discriminated_Formal_Type --
13817 ----------------------------------------
13818
13819 procedure Validate_Discriminated_Formal_Type is
13820 Formal_Discr : Entity_Id;
13821 Actual_Discr : Entity_Id;
13822 Formal_Subt : Entity_Id;
13823
13824 begin
13825 if Has_Discriminants (A_Gen_T) then
13826 if not Has_Discriminants (Act_T) then
13827 Error_Msg_NE
13828 ("actual for & must have discriminants", Actual, Gen_T);
13829 Abandon_Instantiation (Actual);
13830
13831 elsif Is_Constrained (Act_T) then
13832 Error_Msg_NE
13833 ("actual for & must be unconstrained", Actual, Gen_T);
13834 Abandon_Instantiation (Actual);
13835
13836 else
13837 Formal_Discr := First_Discriminant (A_Gen_T);
13838 Actual_Discr := First_Discriminant (Act_T);
13839 while Formal_Discr /= Empty loop
13840 if Actual_Discr = Empty then
13841 Error_Msg_NE
13842 ("discriminants on actual do not match formal",
13843 Actual, Gen_T);
13844 Abandon_Instantiation (Actual);
13845 end if;
13846
13847 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13848
13849 -- Access discriminants match if designated types do
13850
13851 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13852 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13853 E_Anonymous_Access_Type
13854 and then
13855 Get_Instance_Of
13856 (Designated_Type (Base_Type (Formal_Subt))) =
13857 Designated_Type (Base_Type (Etype (Actual_Discr)))
13858 then
13859 null;
13860
13861 elsif Base_Type (Formal_Subt) /=
13862 Base_Type (Etype (Actual_Discr))
13863 then
13864 Error_Msg_NE
13865 ("types of actual discriminants must match formal",
13866 Actual, Gen_T);
13867 Abandon_Instantiation (Actual);
13868
13869 elsif not Subtypes_Statically_Match
13870 (Formal_Subt, Etype (Actual_Discr))
13871 and then Ada_Version >= Ada_95
13872 then
13873 Error_Msg_NE
13874 ("subtypes of actual discriminants must match formal",
13875 Actual, Gen_T);
13876 Abandon_Instantiation (Actual);
13877 end if;
13878
13879 Next_Discriminant (Formal_Discr);
13880 Next_Discriminant (Actual_Discr);
13881 end loop;
13882
13883 if Actual_Discr /= Empty then
13884 Error_Msg_NE
13885 ("discriminants on actual do not match formal",
13886 Actual, Gen_T);
13887 Abandon_Instantiation (Actual);
13888 end if;
13889 end if;
13890 end if;
13891 end Validate_Discriminated_Formal_Type;
13892
13893 ---------------------------------------
13894 -- Validate_Incomplete_Type_Instance --
13895 ---------------------------------------
13896
13897 procedure Validate_Incomplete_Type_Instance is
13898 begin
13899 if not Is_Tagged_Type (Act_T)
13900 and then Is_Tagged_Type (A_Gen_T)
13901 then
13902 Error_Msg_NE
13903 ("actual for & must be a tagged type", Actual, Gen_T);
13904 end if;
13905
13906 Validate_Discriminated_Formal_Type;
13907 end Validate_Incomplete_Type_Instance;
13908
13909 --------------------------------------
13910 -- Validate_Interface_Type_Instance --
13911 --------------------------------------
13912
13913 procedure Validate_Interface_Type_Instance is
13914 begin
13915 if not Is_Interface (Act_T) then
13916 Error_Msg_NE
13917 ("actual for formal interface type must be an interface",
13918 Actual, Gen_T);
13919
13920 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13921 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13922 or else Is_Protected_Interface (A_Gen_T) /=
13923 Is_Protected_Interface (Act_T)
13924 or else Is_Synchronized_Interface (A_Gen_T) /=
13925 Is_Synchronized_Interface (Act_T)
13926 then
13927 Error_Msg_NE
13928 ("actual for interface& does not match (RM 12.5.5(4))",
13929 Actual, Gen_T);
13930 end if;
13931 end Validate_Interface_Type_Instance;
13932
13933 ------------------------------------
13934 -- Validate_Private_Type_Instance --
13935 ------------------------------------
13936
13937 procedure Validate_Private_Type_Instance is
13938 begin
13939 if Is_Limited_Type (Act_T)
13940 and then not Is_Limited_Type (A_Gen_T)
13941 then
13942 if In_Instance then
13943 null;
13944 else
13945 Error_Msg_NE
13946 ("actual for non-limited & cannot be a limited type", Actual,
13947 Gen_T);
13948 Explain_Limited_Type (Act_T, Actual);
13949 Abandon_Instantiation (Actual);
13950 end if;
13951
13952 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13953 and then not Has_Preelaborable_Initialization (Act_T)
13954 then
13955 Error_Msg_NE
13956 ("actual for & must have preelaborable initialization", Actual,
13957 Gen_T);
13958
13959 elsif not Is_Definite_Subtype (Act_T)
13960 and then Is_Definite_Subtype (A_Gen_T)
13961 and then Ada_Version >= Ada_95
13962 then
13963 Error_Msg_NE
13964 ("actual for & must be a definite subtype", Actual, Gen_T);
13965
13966 elsif not Is_Tagged_Type (Act_T)
13967 and then Is_Tagged_Type (A_Gen_T)
13968 then
13969 Error_Msg_NE
13970 ("actual for & must be a tagged type", Actual, Gen_T);
13971 end if;
13972
13973 Validate_Discriminated_Formal_Type;
13974 Ancestor := Gen_T;
13975 end Validate_Private_Type_Instance;
13976
13977 -- Start of processing for Instantiate_Type
13978
13979 begin
13980 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13981 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13982 return New_List (Error);
13983
13984 elsif not Is_Entity_Name (Actual)
13985 or else not Is_Type (Entity (Actual))
13986 then
13987 Error_Msg_NE
13988 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13989 Abandon_Instantiation (Actual);
13990
13991 else
13992 Act_T := Entity (Actual);
13993
13994 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13995 -- as a generic actual parameter if the corresponding formal type
13996 -- does not have a known_discriminant_part, or is a formal derived
13997 -- type that is an Unchecked_Union type.
13998
13999 if Is_Unchecked_Union (Base_Type (Act_T)) then
14000 if not Has_Discriminants (A_Gen_T)
14001 or else (Is_Derived_Type (A_Gen_T)
14002 and then Is_Unchecked_Union (A_Gen_T))
14003 then
14004 null;
14005 else
14006 Error_Msg_N ("unchecked union cannot be the actual for a "
14007 & "discriminated formal type", Act_T);
14008
14009 end if;
14010 end if;
14011
14012 -- Deal with fixed/floating restrictions
14013
14014 if Is_Floating_Point_Type (Act_T) then
14015 Check_Restriction (No_Floating_Point, Actual);
14016 elsif Is_Fixed_Point_Type (Act_T) then
14017 Check_Restriction (No_Fixed_Point, Actual);
14018 end if;
14019
14020 -- Deal with error of using incomplete type as generic actual.
14021 -- This includes limited views of a type, even if the non-limited
14022 -- view may be available.
14023
14024 if Ekind (Act_T) = E_Incomplete_Type
14025 or else (Is_Class_Wide_Type (Act_T)
14026 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14027 then
14028 -- If the formal is an incomplete type, the actual can be
14029 -- incomplete as well.
14030
14031 if Ekind (A_Gen_T) = E_Incomplete_Type then
14032 null;
14033
14034 elsif Is_Class_Wide_Type (Act_T)
14035 or else No (Full_View (Act_T))
14036 then
14037 Error_Msg_N ("premature use of incomplete type", Actual);
14038 Abandon_Instantiation (Actual);
14039 else
14040 Act_T := Full_View (Act_T);
14041 Set_Entity (Actual, Act_T);
14042
14043 if Has_Private_Component (Act_T) then
14044 Error_Msg_N
14045 ("premature use of type with private component", Actual);
14046 end if;
14047 end if;
14048
14049 -- Deal with error of premature use of private type as generic actual
14050
14051 elsif Is_Private_Type (Act_T)
14052 and then Is_Private_Type (Base_Type (Act_T))
14053 and then not Is_Generic_Type (Act_T)
14054 and then not Is_Derived_Type (Act_T)
14055 and then No (Full_View (Root_Type (Act_T)))
14056 then
14057 -- If the formal is an incomplete type, the actual can be
14058 -- private or incomplete as well.
14059
14060 if Ekind (A_Gen_T) = E_Incomplete_Type then
14061 null;
14062 else
14063 Error_Msg_N ("premature use of private type", Actual);
14064 end if;
14065
14066 elsif Has_Private_Component (Act_T) then
14067 Error_Msg_N
14068 ("premature use of type with private component", Actual);
14069 end if;
14070
14071 Set_Instance_Of (A_Gen_T, Act_T);
14072
14073 -- If the type is generic, the class-wide type may also be used
14074
14075 if Is_Tagged_Type (A_Gen_T)
14076 and then Is_Tagged_Type (Act_T)
14077 and then not Is_Class_Wide_Type (A_Gen_T)
14078 then
14079 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14080 Class_Wide_Type (Act_T));
14081 end if;
14082
14083 if not Is_Abstract_Type (A_Gen_T)
14084 and then Is_Abstract_Type (Act_T)
14085 then
14086 Error_Msg_N
14087 ("actual of non-abstract formal cannot be abstract", Actual);
14088 end if;
14089
14090 -- A generic scalar type is a first subtype for which we generate
14091 -- an anonymous base type. Indicate that the instance of this base
14092 -- is the base type of the actual.
14093
14094 if Is_Scalar_Type (A_Gen_T) then
14095 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14096 end if;
14097 end if;
14098
14099 Check_Shared_Variable_Control_Aspects;
14100
14101 if Error_Posted (Act_T) then
14102 null;
14103 else
14104 case Nkind (Def) is
14105 when N_Formal_Private_Type_Definition =>
14106 Validate_Private_Type_Instance;
14107
14108 when N_Formal_Incomplete_Type_Definition =>
14109 Validate_Incomplete_Type_Instance;
14110
14111 when N_Formal_Derived_Type_Definition =>
14112 Validate_Derived_Type_Instance;
14113
14114 when N_Formal_Discrete_Type_Definition =>
14115 if not Is_Discrete_Type (Act_T) then
14116 Error_Msg_NE
14117 ("expect discrete type in instantiation of&",
14118 Actual, Gen_T);
14119 Abandon_Instantiation (Actual);
14120 end if;
14121
14122 Diagnose_Predicated_Actual;
14123
14124 when N_Formal_Signed_Integer_Type_Definition =>
14125 if not Is_Signed_Integer_Type (Act_T) then
14126 Error_Msg_NE
14127 ("expect signed integer type in instantiation of&",
14128 Actual, Gen_T);
14129 Abandon_Instantiation (Actual);
14130 end if;
14131
14132 Diagnose_Predicated_Actual;
14133
14134 when N_Formal_Modular_Type_Definition =>
14135 if not Is_Modular_Integer_Type (Act_T) then
14136 Error_Msg_NE
14137 ("expect modular type in instantiation of &",
14138 Actual, Gen_T);
14139 Abandon_Instantiation (Actual);
14140 end if;
14141
14142 Diagnose_Predicated_Actual;
14143
14144 when N_Formal_Floating_Point_Definition =>
14145 if not Is_Floating_Point_Type (Act_T) then
14146 Error_Msg_NE
14147 ("expect float type in instantiation of &", Actual, Gen_T);
14148 Abandon_Instantiation (Actual);
14149 end if;
14150
14151 when N_Formal_Ordinary_Fixed_Point_Definition =>
14152 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14153 Error_Msg_NE
14154 ("expect ordinary fixed point type in instantiation of &",
14155 Actual, Gen_T);
14156 Abandon_Instantiation (Actual);
14157 end if;
14158
14159 when N_Formal_Decimal_Fixed_Point_Definition =>
14160 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14161 Error_Msg_NE
14162 ("expect decimal type in instantiation of &",
14163 Actual, Gen_T);
14164 Abandon_Instantiation (Actual);
14165 end if;
14166
14167 when N_Array_Type_Definition =>
14168 Validate_Array_Type_Instance;
14169
14170 when N_Access_To_Object_Definition =>
14171 Validate_Access_Type_Instance;
14172
14173 when N_Access_Function_Definition
14174 | N_Access_Procedure_Definition
14175 =>
14176 Validate_Access_Subprogram_Instance;
14177
14178 when N_Record_Definition =>
14179 Validate_Interface_Type_Instance;
14180
14181 when N_Derived_Type_Definition =>
14182 Validate_Derived_Interface_Type_Instance;
14183
14184 when others =>
14185 raise Program_Error;
14186 end case;
14187 end if;
14188
14189 Subt := New_Copy (Gen_T);
14190
14191 -- Use adjusted sloc of subtype name as the location for other nodes in
14192 -- the subtype declaration.
14193
14194 Loc := Sloc (Subt);
14195
14196 Decl_Node :=
14197 Make_Subtype_Declaration (Loc,
14198 Defining_Identifier => Subt,
14199 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14200
14201 -- Record whether the actual is private at this point, so that
14202 -- Check_Generic_Actuals can restore its proper view before the
14203 -- semantic analysis of the instance.
14204
14205 if Is_Private_Type (Act_T) then
14206 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14207 end if;
14208
14209 -- In Ada 2012 the actual may be a limited view. Indicate that
14210 -- the local subtype must be treated as such.
14211
14212 if From_Limited_With (Act_T) then
14213 Set_Ekind (Subt, E_Incomplete_Subtype);
14214 Set_From_Limited_With (Subt);
14215 end if;
14216
14217 Decl_Nodes := New_List (Decl_Node);
14218
14219 -- Flag actual derived types so their elaboration produces the
14220 -- appropriate renamings for the primitive operations of the ancestor.
14221 -- Flag actual for formal private types as well, to determine whether
14222 -- operations in the private part may override inherited operations.
14223 -- If the formal has an interface list, the ancestor is not the
14224 -- parent, but the analyzed formal that includes the interface
14225 -- operations of all its progenitors.
14226
14227 -- Same treatment for formal private types, so we can check whether the
14228 -- type is tagged limited when validating derivations in the private
14229 -- part. (See AI05-096).
14230
14231 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14232 if Present (Interface_List (Def)) then
14233 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14234 else
14235 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14236 end if;
14237
14238 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14239 | N_Formal_Incomplete_Type_Definition
14240 then
14241 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14242 end if;
14243
14244 -- If the actual is a synchronized type that implements an interface,
14245 -- the primitive operations are attached to the corresponding record,
14246 -- and we have to treat it as an additional generic actual, so that its
14247 -- primitive operations become visible in the instance. The task or
14248 -- protected type itself does not carry primitive operations.
14249
14250 if Is_Concurrent_Type (Act_T)
14251 and then Is_Tagged_Type (Act_T)
14252 and then Present (Corresponding_Record_Type (Act_T))
14253 and then Present (Ancestor)
14254 and then Is_Interface (Ancestor)
14255 then
14256 declare
14257 Corr_Rec : constant Entity_Id :=
14258 Corresponding_Record_Type (Act_T);
14259 New_Corr : Entity_Id;
14260 Corr_Decl : Node_Id;
14261
14262 begin
14263 New_Corr := Make_Temporary (Loc, 'S');
14264 Corr_Decl :=
14265 Make_Subtype_Declaration (Loc,
14266 Defining_Identifier => New_Corr,
14267 Subtype_Indication =>
14268 New_Occurrence_Of (Corr_Rec, Loc));
14269 Append_To (Decl_Nodes, Corr_Decl);
14270
14271 if Ekind (Act_T) = E_Task_Type then
14272 Set_Ekind (Subt, E_Task_Subtype);
14273 else
14274 Set_Ekind (Subt, E_Protected_Subtype);
14275 end if;
14276
14277 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14278 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14279 Set_Generic_Parent_Type (Decl_Node, Empty);
14280 end;
14281 end if;
14282
14283 -- For a floating-point type, capture dimension info if any, because
14284 -- the generated subtype declaration does not come from source and
14285 -- will not process dimensions.
14286
14287 if Is_Floating_Point_Type (Act_T) then
14288 Copy_Dimensions (Act_T, Subt);
14289 end if;
14290
14291 return Decl_Nodes;
14292 end Instantiate_Type;
14293
14294 ---------------------
14295 -- Is_In_Main_Unit --
14296 ---------------------
14297
14298 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14299 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14300 Current_Unit : Node_Id;
14301
14302 begin
14303 if Unum = Main_Unit then
14304 return True;
14305
14306 -- If the current unit is a subunit then it is either the main unit or
14307 -- is being compiled as part of the main unit.
14308
14309 elsif Nkind (N) = N_Compilation_Unit then
14310 return Nkind (Unit (N)) = N_Subunit;
14311 end if;
14312
14313 Current_Unit := Parent (N);
14314 while Present (Current_Unit)
14315 and then Nkind (Current_Unit) /= N_Compilation_Unit
14316 loop
14317 Current_Unit := Parent (Current_Unit);
14318 end loop;
14319
14320 -- The instantiation node is in the main unit, or else the current node
14321 -- (perhaps as the result of nested instantiations) is in the main unit,
14322 -- or in the declaration of the main unit, which in this last case must
14323 -- be a body.
14324
14325 return
14326 Current_Unit = Cunit (Main_Unit)
14327 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14328 or else (Present (Current_Unit)
14329 and then Present (Library_Unit (Current_Unit))
14330 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14331 end Is_In_Main_Unit;
14332
14333 ----------------------------
14334 -- Load_Parent_Of_Generic --
14335 ----------------------------
14336
14337 procedure Load_Parent_Of_Generic
14338 (N : Node_Id;
14339 Spec : Node_Id;
14340 Body_Optional : Boolean := False)
14341 is
14342 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14343 Saved_Style_Check : constant Boolean := Style_Check;
14344 Saved_Warnings : constant Warning_Record := Save_Warnings;
14345 True_Parent : Node_Id;
14346 Inst_Node : Node_Id;
14347 OK : Boolean;
14348 Previous_Instances : constant Elist_Id := New_Elmt_List;
14349
14350 procedure Collect_Previous_Instances (Decls : List_Id);
14351 -- Collect all instantiations in the given list of declarations, that
14352 -- precede the generic that we need to load. If the bodies of these
14353 -- instantiations are available, we must analyze them, to ensure that
14354 -- the public symbols generated are the same when the unit is compiled
14355 -- to generate code, and when it is compiled in the context of a unit
14356 -- that needs a particular nested instance. This process is applied to
14357 -- both package and subprogram instances.
14358
14359 --------------------------------
14360 -- Collect_Previous_Instances --
14361 --------------------------------
14362
14363 procedure Collect_Previous_Instances (Decls : List_Id) is
14364 Decl : Node_Id;
14365
14366 begin
14367 Decl := First (Decls);
14368 while Present (Decl) loop
14369 if Sloc (Decl) >= Sloc (Inst_Node) then
14370 return;
14371
14372 -- If Decl is an instantiation, then record it as requiring
14373 -- instantiation of the corresponding body, except if it is an
14374 -- abbreviated instantiation generated internally for conformance
14375 -- checking purposes only for the case of a formal package
14376 -- declared without a box (see Instantiate_Formal_Package). Such
14377 -- an instantiation does not generate any code (the actual code
14378 -- comes from actual) and thus does not need to be analyzed here.
14379 -- If the instantiation appears with a generic package body it is
14380 -- not analyzed here either.
14381
14382 elsif Nkind (Decl) = N_Package_Instantiation
14383 and then not Is_Internal (Defining_Entity (Decl))
14384 then
14385 Append_Elmt (Decl, Previous_Instances);
14386
14387 -- For a subprogram instantiation, omit instantiations intrinsic
14388 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14389
14390 elsif Nkind (Decl) in N_Function_Instantiation
14391 | N_Procedure_Instantiation
14392 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14393 then
14394 Append_Elmt (Decl, Previous_Instances);
14395
14396 elsif Nkind (Decl) = N_Package_Declaration then
14397 Collect_Previous_Instances
14398 (Visible_Declarations (Specification (Decl)));
14399 Collect_Previous_Instances
14400 (Private_Declarations (Specification (Decl)));
14401
14402 -- Previous non-generic bodies may contain instances as well
14403
14404 elsif Nkind (Decl) = N_Package_Body
14405 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14406 then
14407 Collect_Previous_Instances (Declarations (Decl));
14408
14409 elsif Nkind (Decl) = N_Subprogram_Body
14410 and then not Acts_As_Spec (Decl)
14411 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14412 then
14413 Collect_Previous_Instances (Declarations (Decl));
14414 end if;
14415
14416 Next (Decl);
14417 end loop;
14418 end Collect_Previous_Instances;
14419
14420 -- Start of processing for Load_Parent_Of_Generic
14421
14422 begin
14423 if not In_Same_Source_Unit (N, Spec)
14424 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14425 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14426 and then not Is_In_Main_Unit (Spec))
14427 then
14428 -- Find body of parent of spec, and analyze it. A special case arises
14429 -- when the parent is an instantiation, that is to say when we are
14430 -- currently instantiating a nested generic. In that case, there is
14431 -- no separate file for the body of the enclosing instance. Instead,
14432 -- the enclosing body must be instantiated as if it were a pending
14433 -- instantiation, in order to produce the body for the nested generic
14434 -- we require now. Note that in that case the generic may be defined
14435 -- in a package body, the instance defined in the same package body,
14436 -- and the original enclosing body may not be in the main unit.
14437
14438 Inst_Node := Empty;
14439
14440 True_Parent := Parent (Spec);
14441 while Present (True_Parent)
14442 and then Nkind (True_Parent) /= N_Compilation_Unit
14443 loop
14444 if Nkind (True_Parent) = N_Package_Declaration
14445 and then
14446 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14447 then
14448 -- Parent is a compilation unit that is an instantiation, and
14449 -- instantiation node has been replaced with package decl.
14450
14451 Inst_Node := Original_Node (True_Parent);
14452 exit;
14453
14454 elsif Nkind (True_Parent) = N_Package_Declaration
14455 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14456 and then
14457 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14458 then
14459 -- Parent is a compilation unit that is an instantiation, but
14460 -- instantiation node has not been replaced with package decl.
14461
14462 Inst_Node := Unit (Parent (True_Parent));
14463 exit;
14464
14465 elsif Nkind (True_Parent) = N_Package_Declaration
14466 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14467 and then Present (Generic_Parent (Specification (True_Parent)))
14468 then
14469 -- Parent is an instantiation within another specification.
14470 -- Declaration for instance has been inserted before original
14471 -- instantiation node. A direct link would be preferable?
14472
14473 Inst_Node := Next (True_Parent);
14474 while Present (Inst_Node)
14475 and then Nkind (Inst_Node) /= N_Package_Instantiation
14476 loop
14477 Next (Inst_Node);
14478 end loop;
14479
14480 -- If the instance appears within a generic, and the generic
14481 -- unit is defined within a formal package of the enclosing
14482 -- generic, there is no generic body available, and none
14483 -- needed. A more precise test should be used ???
14484
14485 if No (Inst_Node) then
14486 return;
14487 end if;
14488
14489 exit;
14490
14491 -- If an ancestor of the generic comes from a formal package
14492 -- there is no source for the ancestor body. This is detected
14493 -- by examining the scope of the ancestor and its declaration.
14494 -- The body, if any is needed, will be available when the
14495 -- current unit (containing a formal package) is instantiated.
14496
14497 elsif Nkind (True_Parent) = N_Package_Specification
14498 and then Present (Generic_Parent (True_Parent))
14499 and then Nkind
14500 (Original_Node (Unit_Declaration_Node
14501 (Scope (Generic_Parent (True_Parent)))))
14502 = N_Formal_Package_Declaration
14503 then
14504 return;
14505
14506 else
14507 True_Parent := Parent (True_Parent);
14508 end if;
14509 end loop;
14510
14511 -- Case where we are currently instantiating a nested generic
14512
14513 if Present (Inst_Node) then
14514 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14515
14516 -- Instantiation node and declaration of instantiated package
14517 -- were exchanged when only the declaration was needed.
14518 -- Restore instantiation node before proceeding with body.
14519
14520 Set_Unit (Parent (True_Parent), Inst_Node);
14521 end if;
14522
14523 -- Now complete instantiation of enclosing body, if it appears in
14524 -- some other unit. If it appears in the current unit, the body
14525 -- will have been instantiated already.
14526
14527 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14528
14529 -- We need to determine the expander mode to instantiate the
14530 -- enclosing body. Because the generic body we need may use
14531 -- global entities declared in the enclosing package (including
14532 -- aggregates) it is in general necessary to compile this body
14533 -- with expansion enabled, except if we are within a generic
14534 -- package, in which case the usual generic rule applies.
14535
14536 declare
14537 Exp_Status : Boolean := True;
14538 Scop : Entity_Id;
14539
14540 begin
14541 -- Loop through scopes looking for generic package
14542
14543 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14544 while Present (Scop)
14545 and then Scop /= Standard_Standard
14546 loop
14547 if Ekind (Scop) = E_Generic_Package then
14548 Exp_Status := False;
14549 exit;
14550 end if;
14551
14552 Scop := Scope (Scop);
14553 end loop;
14554
14555 -- Collect previous instantiations in the unit that contains
14556 -- the desired generic.
14557
14558 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14559 and then not Body_Optional
14560 then
14561 declare
14562 Decl : Elmt_Id;
14563 Info : Pending_Body_Info;
14564 Par : Node_Id;
14565
14566 begin
14567 Par := Parent (Inst_Node);
14568 while Present (Par) loop
14569 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14570 Par := Parent (Par);
14571 end loop;
14572
14573 pragma Assert (Present (Par));
14574
14575 if Nkind (Par) = N_Package_Body then
14576 Collect_Previous_Instances (Declarations (Par));
14577
14578 elsif Nkind (Par) = N_Package_Declaration then
14579 Collect_Previous_Instances
14580 (Visible_Declarations (Specification (Par)));
14581 Collect_Previous_Instances
14582 (Private_Declarations (Specification (Par)));
14583
14584 else
14585 -- Enclosing unit is a subprogram body. In this
14586 -- case all instance bodies are processed in order
14587 -- and there is no need to collect them separately.
14588
14589 null;
14590 end if;
14591
14592 Decl := First_Elmt (Previous_Instances);
14593 while Present (Decl) loop
14594 Info :=
14595 (Act_Decl =>
14596 Instance_Spec (Node (Decl)),
14597 Config_Switches => Save_Config_Switches,
14598 Current_Sem_Unit =>
14599 Get_Code_Unit (Sloc (Node (Decl))),
14600 Expander_Status => Exp_Status,
14601 Inst_Node => Node (Decl),
14602 Local_Suppress_Stack_Top =>
14603 Local_Suppress_Stack_Top,
14604 Scope_Suppress => Scope_Suppress,
14605 Warnings => Save_Warnings);
14606
14607 -- Package instance
14608
14609 if Nkind (Node (Decl)) = N_Package_Instantiation
14610 then
14611 Instantiate_Package_Body
14612 (Info, Body_Optional => True);
14613
14614 -- Subprogram instance
14615
14616 else
14617 -- The instance_spec is in the wrapper package,
14618 -- usually followed by its local renaming
14619 -- declaration. See Build_Subprogram_Renaming
14620 -- for details. If the instance carries aspects,
14621 -- these result in the corresponding pragmas,
14622 -- inserted after the subprogram declaration.
14623 -- They must be skipped as well when retrieving
14624 -- the desired spec. Some of them may have been
14625 -- rewritten as null statements.
14626 -- A direct link would be more robust ???
14627
14628 declare
14629 Decl : Node_Id :=
14630 (Last (Visible_Declarations
14631 (Specification (Info.Act_Decl))));
14632 begin
14633 while Nkind (Decl) in
14634 N_Null_Statement |
14635 N_Pragma |
14636 N_Subprogram_Renaming_Declaration
14637 loop
14638 Decl := Prev (Decl);
14639 end loop;
14640
14641 Info.Act_Decl := Decl;
14642 end;
14643
14644 Instantiate_Subprogram_Body
14645 (Info, Body_Optional => True);
14646 end if;
14647
14648 Next_Elmt (Decl);
14649 end loop;
14650 end;
14651 end if;
14652
14653 Instantiate_Package_Body
14654 (Body_Info =>
14655 ((Act_Decl => True_Parent,
14656 Config_Switches => Save_Config_Switches,
14657 Current_Sem_Unit =>
14658 Get_Code_Unit (Sloc (Inst_Node)),
14659 Expander_Status => Exp_Status,
14660 Inst_Node => Inst_Node,
14661 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14662 Scope_Suppress => Scope_Suppress,
14663 Warnings => Save_Warnings)),
14664 Body_Optional => Body_Optional);
14665 end;
14666 end if;
14667
14668 -- Case where we are not instantiating a nested generic
14669
14670 else
14671 Opt.Style_Check := False;
14672 Expander_Mode_Save_And_Set (True);
14673 Load_Needed_Body (Comp_Unit, OK);
14674 Opt.Style_Check := Saved_Style_Check;
14675 Restore_Warnings (Saved_Warnings);
14676 Expander_Mode_Restore;
14677
14678 if not OK
14679 and then Unit_Requires_Body (Defining_Entity (Spec))
14680 and then not Body_Optional
14681 then
14682 declare
14683 Bname : constant Unit_Name_Type :=
14684 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14685
14686 begin
14687 -- In CodePeer mode, the missing body may make the analysis
14688 -- incomplete, but we do not treat it as fatal.
14689
14690 if CodePeer_Mode then
14691 return;
14692
14693 else
14694 Error_Msg_Unit_1 := Bname;
14695 Error_Msg_N ("this instantiation requires$!", N);
14696 Error_Msg_File_1 :=
14697 Get_File_Name (Bname, Subunit => False);
14698 Error_Msg_N ("\but file{ was not found!", N);
14699 raise Unrecoverable_Error;
14700 end if;
14701 end;
14702 end if;
14703 end if;
14704 end if;
14705
14706 -- If loading parent of the generic caused an instantiation circularity,
14707 -- we abandon compilation at this point, because otherwise in some cases
14708 -- we get into trouble with infinite recursions after this point.
14709
14710 if Circularity_Detected then
14711 raise Unrecoverable_Error;
14712 end if;
14713 end Load_Parent_Of_Generic;
14714
14715 ---------------------------------
14716 -- Map_Formal_Package_Entities --
14717 ---------------------------------
14718
14719 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14720 E1 : Entity_Id;
14721 E2 : Entity_Id;
14722
14723 begin
14724 Set_Instance_Of (Form, Act);
14725
14726 -- Traverse formal and actual package to map the corresponding entities.
14727 -- We skip over internal entities that may be generated during semantic
14728 -- analysis, and find the matching entities by name, given that they
14729 -- must appear in the same order.
14730
14731 E1 := First_Entity (Form);
14732 E2 := First_Entity (Act);
14733 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14734 -- Could this test be a single condition??? Seems like it could, and
14735 -- isn't FPE (Form) a constant anyway???
14736
14737 if not Is_Internal (E1)
14738 and then Present (Parent (E1))
14739 and then not Is_Class_Wide_Type (E1)
14740 and then not Is_Internal_Name (Chars (E1))
14741 then
14742 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14743 Next_Entity (E2);
14744 end loop;
14745
14746 if No (E2) then
14747 exit;
14748 else
14749 Set_Instance_Of (E1, E2);
14750
14751 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14752 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14753 end if;
14754
14755 if Is_Constrained (E1) then
14756 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14757 end if;
14758
14759 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14760 Map_Formal_Package_Entities (E1, E2);
14761 end if;
14762 end if;
14763 end if;
14764
14765 Next_Entity (E1);
14766 end loop;
14767 end Map_Formal_Package_Entities;
14768
14769 -----------------------
14770 -- Move_Freeze_Nodes --
14771 -----------------------
14772
14773 procedure Move_Freeze_Nodes
14774 (Out_Of : Entity_Id;
14775 After : Node_Id;
14776 L : List_Id)
14777 is
14778 Decl : Node_Id;
14779 Next_Decl : Node_Id;
14780 Next_Node : Node_Id := After;
14781 Spec : Node_Id;
14782
14783 function Is_Outer_Type (T : Entity_Id) return Boolean;
14784 -- Check whether entity is declared in a scope external to that of the
14785 -- generic unit.
14786
14787 -------------------
14788 -- Is_Outer_Type --
14789 -------------------
14790
14791 function Is_Outer_Type (T : Entity_Id) return Boolean is
14792 Scop : Entity_Id := Scope (T);
14793
14794 begin
14795 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14796 return True;
14797
14798 else
14799 while Scop /= Standard_Standard loop
14800 if Scop = Out_Of then
14801 return False;
14802 else
14803 Scop := Scope (Scop);
14804 end if;
14805 end loop;
14806
14807 return True;
14808 end if;
14809 end Is_Outer_Type;
14810
14811 -- Start of processing for Move_Freeze_Nodes
14812
14813 begin
14814 if No (L) then
14815 return;
14816 end if;
14817
14818 -- First remove the freeze nodes that may appear before all other
14819 -- declarations.
14820
14821 Decl := First (L);
14822 while Present (Decl)
14823 and then Nkind (Decl) = N_Freeze_Entity
14824 and then Is_Outer_Type (Entity (Decl))
14825 loop
14826 Decl := Remove_Head (L);
14827 Insert_After (Next_Node, Decl);
14828 Set_Analyzed (Decl, False);
14829 Next_Node := Decl;
14830 Decl := First (L);
14831 end loop;
14832
14833 -- Next scan the list of declarations and remove each freeze node that
14834 -- appears ahead of the current node.
14835
14836 while Present (Decl) loop
14837 while Present (Next (Decl))
14838 and then Nkind (Next (Decl)) = N_Freeze_Entity
14839 and then Is_Outer_Type (Entity (Next (Decl)))
14840 loop
14841 Next_Decl := Remove_Next (Decl);
14842 Insert_After (Next_Node, Next_Decl);
14843 Set_Analyzed (Next_Decl, False);
14844 Next_Node := Next_Decl;
14845 end loop;
14846
14847 -- If the declaration is a nested package or concurrent type, then
14848 -- recurse. Nested generic packages will have been processed from the
14849 -- inside out.
14850
14851 case Nkind (Decl) is
14852 when N_Package_Declaration =>
14853 Spec := Specification (Decl);
14854
14855 when N_Task_Type_Declaration =>
14856 Spec := Task_Definition (Decl);
14857
14858 when N_Protected_Type_Declaration =>
14859 Spec := Protected_Definition (Decl);
14860
14861 when others =>
14862 Spec := Empty;
14863 end case;
14864
14865 if Present (Spec) then
14866 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14867 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14868 end if;
14869
14870 Next (Decl);
14871 end loop;
14872 end Move_Freeze_Nodes;
14873
14874 ----------------
14875 -- Next_Assoc --
14876 ----------------
14877
14878 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14879 begin
14880 return Generic_Renamings.Table (E).Next_In_HTable;
14881 end Next_Assoc;
14882
14883 ------------------------
14884 -- Preanalyze_Actuals --
14885 ------------------------
14886
14887 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14888 procedure Perform_Appropriate_Analysis (N : Node_Id);
14889 -- Determine if the actuals we are analyzing come from a generic
14890 -- instantiation that is a library unit and dispatch accordingly.
14891
14892 ----------------------------------
14893 -- Perform_Appropriate_Analysis --
14894 ----------------------------------
14895
14896 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14897 begin
14898 -- When we have a library instantiation we cannot allow any expansion
14899 -- to occur, since there may be no place to put it. Instead, in that
14900 -- case we perform a preanalysis of the actual.
14901
14902 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14903 Preanalyze (N);
14904 else
14905 Analyze (N);
14906 end if;
14907 end Perform_Appropriate_Analysis;
14908
14909 -- Local variables
14910
14911 Errs : constant Nat := Serious_Errors_Detected;
14912
14913 Assoc : Node_Id;
14914 Act : Node_Id;
14915
14916 Cur : Entity_Id := Empty;
14917 -- Current homograph of the instance name
14918
14919 Vis : Boolean := False;
14920 -- Saved visibility status of the current homograph
14921
14922 -- Start of processing for Preanalyze_Actuals
14923
14924 begin
14925 Assoc := First (Generic_Associations (N));
14926
14927 -- If the instance is a child unit, its name may hide an outer homonym,
14928 -- so make it invisible to perform name resolution on the actuals.
14929
14930 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14931 and then Present
14932 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14933 then
14934 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14935
14936 if Is_Compilation_Unit (Cur) then
14937 Vis := Is_Immediately_Visible (Cur);
14938 Set_Is_Immediately_Visible (Cur, False);
14939 else
14940 Cur := Empty;
14941 end if;
14942 end if;
14943
14944 while Present (Assoc) loop
14945 if Nkind (Assoc) /= N_Others_Choice then
14946 Act := Explicit_Generic_Actual_Parameter (Assoc);
14947
14948 -- Within a nested instantiation, a defaulted actual is an empty
14949 -- association, so nothing to analyze. If the subprogram actual
14950 -- is an attribute, analyze prefix only, because actual is not a
14951 -- complete attribute reference.
14952
14953 -- If actual is an allocator, analyze expression only. The full
14954 -- analysis can generate code, and if instance is a compilation
14955 -- unit we have to wait until the package instance is installed
14956 -- to have a proper place to insert this code.
14957
14958 -- String literals may be operators, but at this point we do not
14959 -- know whether the actual is a formal subprogram or a string.
14960
14961 if No (Act) then
14962 null;
14963
14964 elsif Nkind (Act) = N_Attribute_Reference then
14965 Perform_Appropriate_Analysis (Prefix (Act));
14966
14967 elsif Nkind (Act) = N_Explicit_Dereference then
14968 Perform_Appropriate_Analysis (Prefix (Act));
14969
14970 elsif Nkind (Act) = N_Allocator then
14971 declare
14972 Expr : constant Node_Id := Expression (Act);
14973
14974 begin
14975 if Nkind (Expr) = N_Subtype_Indication then
14976 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14977
14978 -- Analyze separately each discriminant constraint, when
14979 -- given with a named association.
14980
14981 declare
14982 Constr : Node_Id;
14983
14984 begin
14985 Constr := First (Constraints (Constraint (Expr)));
14986 while Present (Constr) loop
14987 if Nkind (Constr) = N_Discriminant_Association then
14988 Perform_Appropriate_Analysis
14989 (Expression (Constr));
14990 else
14991 Perform_Appropriate_Analysis (Constr);
14992 end if;
14993
14994 Next (Constr);
14995 end loop;
14996 end;
14997
14998 else
14999 Perform_Appropriate_Analysis (Expr);
15000 end if;
15001 end;
15002
15003 elsif Nkind (Act) /= N_Operator_Symbol then
15004 Perform_Appropriate_Analysis (Act);
15005
15006 -- Within a package instance, mark actuals that are limited
15007 -- views, so their use can be moved to the body of the
15008 -- enclosing unit.
15009
15010 if Is_Entity_Name (Act)
15011 and then Is_Type (Entity (Act))
15012 and then From_Limited_With (Entity (Act))
15013 and then Present (Inst)
15014 then
15015 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15016 end if;
15017 end if;
15018
15019 if Errs /= Serious_Errors_Detected then
15020
15021 -- Do a minimal analysis of the generic, to prevent spurious
15022 -- warnings complaining about the generic being unreferenced,
15023 -- before abandoning the instantiation.
15024
15025 Perform_Appropriate_Analysis (Name (N));
15026
15027 if Is_Entity_Name (Name (N))
15028 and then Etype (Name (N)) /= Any_Type
15029 then
15030 Generate_Reference (Entity (Name (N)), Name (N));
15031 Set_Is_Instantiated (Entity (Name (N)));
15032 end if;
15033
15034 if Present (Cur) then
15035
15036 -- For the case of a child instance hiding an outer homonym,
15037 -- provide additional warning which might explain the error.
15038
15039 Set_Is_Immediately_Visible (Cur, Vis);
15040 Error_Msg_NE
15041 ("& hides outer unit with the same name??",
15042 N, Defining_Unit_Name (N));
15043 end if;
15044
15045 Abandon_Instantiation (Act);
15046 end if;
15047 end if;
15048
15049 Next (Assoc);
15050 end loop;
15051
15052 if Present (Cur) then
15053 Set_Is_Immediately_Visible (Cur, Vis);
15054 end if;
15055 end Preanalyze_Actuals;
15056
15057 -------------------------------
15058 -- Provide_Completing_Bodies --
15059 -------------------------------
15060
15061 procedure Provide_Completing_Bodies (N : Node_Id) is
15062 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15063 -- Generate the completing body for subprogram declaration Subp_Decl
15064
15065 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15066 -- Generating completing bodies for all subprograms found in declarative
15067 -- list Decls.
15068
15069 ---------------------------
15070 -- Build_Completing_Body --
15071 ---------------------------
15072
15073 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15074 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15075 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15076 Spec : Node_Id;
15077
15078 begin
15079 -- Nothing to do if the subprogram already has a completing body
15080
15081 if Present (Corresponding_Body (Subp_Decl)) then
15082 return;
15083
15084 -- Mark the function as having a valid return statement even though
15085 -- the body contains a single raise statement.
15086
15087 elsif Ekind (Subp_Id) = E_Function then
15088 Set_Return_Present (Subp_Id);
15089 end if;
15090
15091 -- Clone the specification to obtain new entities and reset the only
15092 -- semantic field.
15093
15094 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15095 Set_Generic_Parent (Spec, Empty);
15096
15097 -- Generate:
15098 -- function Func ... return ... is
15099 -- <or>
15100 -- procedure Proc ... is
15101 -- begin
15102 -- raise Program_Error with "access before elaboration";
15103 -- edn Proc;
15104
15105 Insert_After_And_Analyze (Subp_Decl,
15106 Make_Subprogram_Body (Loc,
15107 Specification => Spec,
15108 Declarations => New_List,
15109 Handled_Statement_Sequence =>
15110 Make_Handled_Sequence_Of_Statements (Loc,
15111 Statements => New_List (
15112 Make_Raise_Program_Error (Loc,
15113 Reason => PE_Access_Before_Elaboration)))));
15114 end Build_Completing_Body;
15115
15116 ----------------------------------
15117 -- Provide_Completing_Bodies_In --
15118 ----------------------------------
15119
15120 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15121 Decl : Node_Id;
15122
15123 begin
15124 if Present (Decls) then
15125 Decl := First (Decls);
15126 while Present (Decl) loop
15127 Provide_Completing_Bodies (Decl);
15128 Next (Decl);
15129 end loop;
15130 end if;
15131 end Provide_Completing_Bodies_In;
15132
15133 -- Local variables
15134
15135 Spec : Node_Id;
15136
15137 -- Start of processing for Provide_Completing_Bodies
15138
15139 begin
15140 if Nkind (N) = N_Package_Declaration then
15141 Spec := Specification (N);
15142
15143 Push_Scope (Defining_Entity (N));
15144 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15145 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15146 Pop_Scope;
15147
15148 elsif Nkind (N) = N_Subprogram_Declaration then
15149 Build_Completing_Body (N);
15150 end if;
15151 end Provide_Completing_Bodies;
15152
15153 -------------------
15154 -- Remove_Parent --
15155 -------------------
15156
15157 procedure Remove_Parent (In_Body : Boolean := False) is
15158 S : Entity_Id := Current_Scope;
15159 -- S is the scope containing the instantiation just completed. The scope
15160 -- stack contains the parent instances of the instantiation, followed by
15161 -- the original S.
15162
15163 Cur_P : Entity_Id;
15164 E : Entity_Id;
15165 P : Entity_Id;
15166 Hidden : Elmt_Id;
15167
15168 begin
15169 -- After child instantiation is complete, remove from scope stack the
15170 -- extra copy of the current scope, and then remove parent instances.
15171
15172 if not In_Body then
15173 Pop_Scope;
15174
15175 while Current_Scope /= S loop
15176 P := Current_Scope;
15177 End_Package_Scope (Current_Scope);
15178
15179 if In_Open_Scopes (P) then
15180 E := First_Entity (P);
15181 while Present (E) loop
15182 Set_Is_Immediately_Visible (E, True);
15183 Next_Entity (E);
15184 end loop;
15185
15186 -- If instantiation is declared in a block, it is the enclosing
15187 -- scope that might be a parent instance. Note that only one
15188 -- block can be involved, because the parent instances have
15189 -- been installed within it.
15190
15191 if Ekind (P) = E_Block then
15192 Cur_P := Scope (P);
15193 else
15194 Cur_P := P;
15195 end if;
15196
15197 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15198 -- We are within an instance of some sibling. Retain
15199 -- visibility of parent, for proper subsequent cleanup, and
15200 -- reinstall private declarations as well.
15201
15202 Set_In_Private_Part (P);
15203 Install_Private_Declarations (P);
15204 end if;
15205
15206 -- If the ultimate parent is a top-level unit recorded in
15207 -- Instance_Parent_Unit, then reset its visibility to what it was
15208 -- before instantiation. (It's not clear what the purpose is of
15209 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15210 -- present before the ultimate parent test was added.???)
15211
15212 elsif not In_Open_Scopes (Scope (P))
15213 or else (P = Instance_Parent_Unit
15214 and then not Parent_Unit_Visible)
15215 then
15216 Set_Is_Immediately_Visible (P, False);
15217
15218 -- If the current scope is itself an instantiation of a generic
15219 -- nested within P, and we are in the private part of body of this
15220 -- instantiation, restore the full views of P, that were removed
15221 -- in End_Package_Scope above. This obscure case can occur when a
15222 -- subunit of a generic contains an instance of a child unit of
15223 -- its generic parent unit.
15224
15225 elsif S = Current_Scope and then Is_Generic_Instance (S) then
15226 declare
15227 Par : constant Entity_Id :=
15228 Generic_Parent (Package_Specification (S));
15229 begin
15230 if Present (Par)
15231 and then P = Scope (Par)
15232 and then (In_Package_Body (S) or else In_Private_Part (S))
15233 then
15234 Set_In_Private_Part (P);
15235 Install_Private_Declarations (P);
15236 end if;
15237 end;
15238 end if;
15239 end loop;
15240
15241 -- Reset visibility of entities in the enclosing scope
15242
15243 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15244
15245 Hidden := First_Elmt (Hidden_Entities);
15246 while Present (Hidden) loop
15247 Set_Is_Immediately_Visible (Node (Hidden), True);
15248 Next_Elmt (Hidden);
15249 end loop;
15250
15251 else
15252 -- Each body is analyzed separately, and there is no context that
15253 -- needs preserving from one body instance to the next, so remove all
15254 -- parent scopes that have been installed.
15255
15256 while Present (S) loop
15257 End_Package_Scope (S);
15258 Set_Is_Immediately_Visible (S, False);
15259 S := Current_Scope;
15260 exit when S = Standard_Standard;
15261 end loop;
15262 end if;
15263 end Remove_Parent;
15264
15265 -----------------
15266 -- Restore_Env --
15267 -----------------
15268
15269 procedure Restore_Env is
15270 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15271
15272 begin
15273 if No (Current_Instantiated_Parent.Act_Id) then
15274 -- Restore environment after subprogram inlining
15275
15276 Restore_Private_Views (Empty);
15277 end if;
15278
15279 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15280 Exchanged_Views := Saved.Exchanged_Views;
15281 Hidden_Entities := Saved.Hidden_Entities;
15282 Current_Sem_Unit := Saved.Current_Sem_Unit;
15283 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15284 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15285
15286 Restore_Config_Switches (Saved.Switches);
15287
15288 Instance_Envs.Decrement_Last;
15289 end Restore_Env;
15290
15291 ---------------------------
15292 -- Restore_Private_Views --
15293 ---------------------------
15294
15295 procedure Restore_Private_Views
15296 (Pack_Id : Entity_Id;
15297 Is_Package : Boolean := True)
15298 is
15299 M : Elmt_Id;
15300 E : Entity_Id;
15301 Typ : Entity_Id;
15302 Dep_Elmt : Elmt_Id;
15303 Dep_Typ : Node_Id;
15304
15305 procedure Restore_Nested_Formal (Formal : Entity_Id);
15306 -- Hide the generic formals of formal packages declared with box which
15307 -- were reachable in the current instantiation.
15308
15309 ---------------------------
15310 -- Restore_Nested_Formal --
15311 ---------------------------
15312
15313 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15314 Ent : Entity_Id;
15315
15316 begin
15317 if Present (Renamed_Object (Formal))
15318 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
15319 then
15320 return;
15321
15322 elsif Present (Associated_Formal_Package (Formal)) then
15323 Ent := First_Entity (Formal);
15324 while Present (Ent) loop
15325 exit when Ekind (Ent) = E_Package
15326 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15327
15328 Set_Is_Hidden (Ent);
15329 Set_Is_Potentially_Use_Visible (Ent, False);
15330
15331 -- If package, then recurse
15332
15333 if Ekind (Ent) = E_Package then
15334 Restore_Nested_Formal (Ent);
15335 end if;
15336
15337 Next_Entity (Ent);
15338 end loop;
15339 end if;
15340 end Restore_Nested_Formal;
15341
15342 -- Start of processing for Restore_Private_Views
15343
15344 begin
15345 M := First_Elmt (Exchanged_Views);
15346 while Present (M) loop
15347 Typ := Node (M);
15348
15349 -- Subtypes of types whose views have been exchanged, and that are
15350 -- defined within the instance, were not on the Private_Dependents
15351 -- list on entry to the instance, so they have to be exchanged
15352 -- explicitly now, in order to remain consistent with the view of the
15353 -- parent type.
15354
15355 if Ekind (Typ) in E_Private_Type
15356 | E_Limited_Private_Type
15357 | E_Record_Type_With_Private
15358 then
15359 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15360 while Present (Dep_Elmt) loop
15361 Dep_Typ := Node (Dep_Elmt);
15362
15363 if Scope (Dep_Typ) = Pack_Id
15364 and then Present (Full_View (Dep_Typ))
15365 then
15366 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15367 Exchange_Declarations (Dep_Typ);
15368 end if;
15369
15370 Next_Elmt (Dep_Elmt);
15371 end loop;
15372 end if;
15373
15374 Exchange_Declarations (Node (M));
15375 Next_Elmt (M);
15376 end loop;
15377
15378 if No (Pack_Id) then
15379 return;
15380 end if;
15381
15382 -- Make the generic formal parameters private, and make the formal types
15383 -- into subtypes of the actuals again.
15384
15385 E := First_Entity (Pack_Id);
15386 while Present (E) loop
15387 Set_Is_Hidden (E, True);
15388
15389 if Is_Type (E)
15390 and then Nkind (Parent (E)) = N_Subtype_Declaration
15391 then
15392 -- If the actual for E is itself a generic actual type from
15393 -- an enclosing instance, E is still a generic actual type
15394 -- outside of the current instance. This matter when resolving
15395 -- an overloaded call that may be ambiguous in the enclosing
15396 -- instance, when two of its actuals coincide.
15397
15398 if Is_Entity_Name (Subtype_Indication (Parent (E)))
15399 and then Is_Generic_Actual_Type
15400 (Entity (Subtype_Indication (Parent (E))))
15401 then
15402 null;
15403 else
15404 Set_Is_Generic_Actual_Type (E, False);
15405
15406 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15407 -- flag also on the Full_View if the type is private, since it
15408 -- was set also on this Full_View. However, this flag is relied
15409 -- upon by Covers to spot "types exported from instantiations"
15410 -- which are implicit Full_Views built for instantiations made
15411 -- on private types and we get type mismatches if we do it when
15412 -- the block exchanging the declarations below triggers ???
15413
15414 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15415 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15416 -- end if;
15417 end if;
15418
15419 -- An unusual case of aliasing: the actual may also be directly
15420 -- visible in the generic, and be private there, while it is fully
15421 -- visible in the context of the instance. The internal subtype
15422 -- is private in the instance but has full visibility like its
15423 -- parent in the enclosing scope. This enforces the invariant that
15424 -- the privacy status of all private dependents of a type coincide
15425 -- with that of the parent type. This can only happen when a
15426 -- generic child unit is instantiated within a sibling.
15427
15428 if Is_Private_Type (E)
15429 and then not Is_Private_Type (Etype (E))
15430 then
15431 Exchange_Declarations (E);
15432 end if;
15433
15434 elsif Ekind (E) = E_Package then
15435
15436 -- The end of the renaming list is the renaming of the generic
15437 -- package itself. If the instance is a subprogram, all entities
15438 -- in the corresponding package are renamings. If this entity is
15439 -- a formal package, make its own formals private as well. The
15440 -- actual in this case is itself the renaming of an instantiation.
15441 -- If the entity is not a package renaming, it is the entity
15442 -- created to validate formal package actuals: ignore it.
15443
15444 -- If the actual is itself a formal package for the enclosing
15445 -- generic, or the actual for such a formal package, it remains
15446 -- visible on exit from the instance, and therefore nothing needs
15447 -- to be done either, except to keep it accessible.
15448
15449 if Is_Package and then Renamed_Object (E) = Pack_Id then
15450 exit;
15451
15452 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15453 null;
15454
15455 elsif
15456 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
15457 then
15458 Set_Is_Hidden (E, False);
15459
15460 else
15461 declare
15462 Act_P : constant Entity_Id := Renamed_Object (E);
15463 Id : Entity_Id;
15464
15465 begin
15466 Id := First_Entity (Act_P);
15467 while Present (Id)
15468 and then Id /= First_Private_Entity (Act_P)
15469 loop
15470 exit when Ekind (Id) = E_Package
15471 and then Renamed_Object (Id) = Act_P;
15472
15473 Set_Is_Hidden (Id, True);
15474 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15475
15476 if Ekind (Id) = E_Package then
15477 Restore_Nested_Formal (Id);
15478 end if;
15479
15480 Next_Entity (Id);
15481 end loop;
15482 end;
15483 end if;
15484 end if;
15485
15486 Next_Entity (E);
15487 end loop;
15488 end Restore_Private_Views;
15489
15490 --------------
15491 -- Save_Env --
15492 --------------
15493
15494 procedure Save_Env
15495 (Gen_Unit : Entity_Id;
15496 Act_Unit : Entity_Id)
15497 is
15498 begin
15499 Init_Env;
15500 Set_Instance_Env (Gen_Unit, Act_Unit);
15501 end Save_Env;
15502
15503 ----------------------------
15504 -- Save_Global_References --
15505 ----------------------------
15506
15507 procedure Save_Global_References (Templ : Node_Id) is
15508
15509 -- ??? it is horrible to use global variables in highly recursive code
15510
15511 E : Entity_Id;
15512 -- The entity of the current associated node
15513
15514 Gen_Scope : Entity_Id;
15515 -- The scope of the generic for which references are being saved
15516
15517 N2 : Node_Id;
15518 -- The current associated node
15519
15520 function Is_Global (E : Entity_Id) return Boolean;
15521 -- Check whether entity is defined outside of generic unit. Examine the
15522 -- scope of an entity, and the scope of the scope, etc, until we find
15523 -- either Standard, in which case the entity is global, or the generic
15524 -- unit itself, which indicates that the entity is local. If the entity
15525 -- is the generic unit itself, as in the case of a recursive call, or
15526 -- the enclosing generic unit, if different from the current scope, then
15527 -- it is local as well, because it will be replaced at the point of
15528 -- instantiation. On the other hand, if it is a reference to a child
15529 -- unit of a common ancestor, which appears in an instantiation, it is
15530 -- global because it is used to denote a specific compilation unit at
15531 -- the time the instantiations will be analyzed.
15532
15533 procedure Qualify_Universal_Operands
15534 (Op : Node_Id;
15535 Func_Call : Node_Id);
15536 -- Op denotes a binary or unary operator in generic template Templ. Node
15537 -- Func_Call is the function call alternative of the operator within the
15538 -- the analyzed copy of the template. Change each operand which yields a
15539 -- universal type by wrapping it into a qualified expression
15540 --
15541 -- Actual_Typ'(Operand)
15542 --
15543 -- where Actual_Typ is the type of corresponding actual parameter of
15544 -- Operand in Func_Call.
15545
15546 procedure Reset_Entity (N : Node_Id);
15547 -- Save semantic information on global entity so that it is not resolved
15548 -- again at instantiation time.
15549
15550 procedure Save_Entity_Descendants (N : Node_Id);
15551 -- Apply Save_Global_References to the two syntactic descendants of
15552 -- non-terminal nodes that carry an Associated_Node and are processed
15553 -- through Reset_Entity. Once the global entity (if any) has been
15554 -- captured together with its type, only two syntactic descendants need
15555 -- to be traversed to complete the processing of the tree rooted at N.
15556 -- This applies to Selected_Components, Expanded_Names, and to Operator
15557 -- nodes. N can also be a character literal, identifier, or operator
15558 -- symbol node, but the call has no effect in these cases.
15559
15560 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15561 -- Default actuals in nested instances must be handled specially
15562 -- because there is no link to them from the original tree. When an
15563 -- actual subprogram is given by a default, we add an explicit generic
15564 -- association for it in the instantiation node. When we save the
15565 -- global references on the name of the instance, we recover the list
15566 -- of generic associations, and add an explicit one to the original
15567 -- generic tree, through which a global actual can be preserved.
15568 -- Similarly, if a child unit is instantiated within a sibling, in the
15569 -- context of the parent, we must preserve the identifier of the parent
15570 -- so that it can be properly resolved in a subsequent instantiation.
15571
15572 procedure Save_Global_Descendant (D : Union_Id);
15573 -- Apply Save_References recursively to the descendants of node D
15574
15575 procedure Save_References (N : Node_Id);
15576 -- This is the recursive procedure that does the work, once the
15577 -- enclosing generic scope has been established.
15578
15579 ---------------
15580 -- Is_Global --
15581 ---------------
15582
15583 function Is_Global (E : Entity_Id) return Boolean is
15584 Se : Entity_Id;
15585
15586 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15587 -- Determine whether the parent node of a reference to a child unit
15588 -- denotes an instantiation or a formal package, in which case the
15589 -- reference to the child unit is global, even if it appears within
15590 -- the current scope (e.g. when the instance appears within the body
15591 -- of an ancestor).
15592
15593 ----------------------
15594 -- Is_Instance_Node --
15595 ----------------------
15596
15597 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15598 begin
15599 return Nkind (Decl) in N_Generic_Instantiation
15600 or else
15601 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15602 end Is_Instance_Node;
15603
15604 -- Start of processing for Is_Global
15605
15606 begin
15607 if E = Gen_Scope then
15608 return False;
15609
15610 elsif E = Standard_Standard then
15611 return True;
15612
15613 elsif Is_Child_Unit (E)
15614 and then (Is_Instance_Node (Parent (N2))
15615 or else (Nkind (Parent (N2)) = N_Expanded_Name
15616 and then N2 = Selector_Name (Parent (N2))
15617 and then
15618 Is_Instance_Node (Parent (Parent (N2)))))
15619 then
15620 return True;
15621
15622 else
15623 Se := Scope (E);
15624 while Se /= Gen_Scope loop
15625 if Se = Standard_Standard then
15626 return True;
15627 else
15628 Se := Scope (Se);
15629 end if;
15630 end loop;
15631
15632 return False;
15633 end if;
15634 end Is_Global;
15635
15636 --------------------------------
15637 -- Qualify_Universal_Operands --
15638 --------------------------------
15639
15640 procedure Qualify_Universal_Operands
15641 (Op : Node_Id;
15642 Func_Call : Node_Id)
15643 is
15644 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15645 -- Rewrite operand Opnd as a qualified expression of the form
15646 --
15647 -- Actual_Typ'(Opnd)
15648 --
15649 -- where Actual is the corresponding actual parameter of Opnd in
15650 -- function call Func_Call.
15651
15652 function Qualify_Type
15653 (Loc : Source_Ptr;
15654 Typ : Entity_Id) return Node_Id;
15655 -- Qualify type Typ by creating a selected component of the form
15656 --
15657 -- Scope_Of_Typ.Typ
15658
15659 ---------------------
15660 -- Qualify_Operand --
15661 ---------------------
15662
15663 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15664 Loc : constant Source_Ptr := Sloc (Opnd);
15665 Typ : constant Entity_Id := Etype (Actual);
15666 Mark : Node_Id;
15667 Qual : Node_Id;
15668
15669 begin
15670 -- Qualify the operand when it is of a universal type. Note that
15671 -- the template is unanalyzed and it is not possible to directly
15672 -- query the type. This transformation is not done when the type
15673 -- of the actual is internally generated because the type will be
15674 -- regenerated in the instance.
15675
15676 if Yields_Universal_Type (Opnd)
15677 and then Comes_From_Source (Typ)
15678 and then not Is_Hidden (Typ)
15679 then
15680 -- The type of the actual may be a global reference. Save this
15681 -- information by creating a reference to it.
15682
15683 if Is_Global (Typ) then
15684 Mark := New_Occurrence_Of (Typ, Loc);
15685
15686 -- Otherwise rely on resolution to find the proper type within
15687 -- the instance.
15688
15689 else
15690 Mark := Qualify_Type (Loc, Typ);
15691 end if;
15692
15693 Qual :=
15694 Make_Qualified_Expression (Loc,
15695 Subtype_Mark => Mark,
15696 Expression => Relocate_Node (Opnd));
15697
15698 -- Mark the qualification to distinguish it from other source
15699 -- constructs and signal the instantiation mechanism that this
15700 -- node requires special processing. See Copy_Generic_Node for
15701 -- details.
15702
15703 Set_Is_Qualified_Universal_Literal (Qual);
15704
15705 Rewrite (Opnd, Qual);
15706 end if;
15707 end Qualify_Operand;
15708
15709 ------------------
15710 -- Qualify_Type --
15711 ------------------
15712
15713 function Qualify_Type
15714 (Loc : Source_Ptr;
15715 Typ : Entity_Id) return Node_Id
15716 is
15717 Scop : constant Entity_Id := Scope (Typ);
15718 Result : Node_Id;
15719
15720 begin
15721 Result := Make_Identifier (Loc, Chars (Typ));
15722
15723 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15724 Result :=
15725 Make_Selected_Component (Loc,
15726 Prefix => Make_Identifier (Loc, Chars (Scop)),
15727 Selector_Name => Result);
15728 end if;
15729
15730 return Result;
15731 end Qualify_Type;
15732
15733 -- Local variables
15734
15735 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15736
15737 -- Start of processing for Qualify_Universal_Operands
15738
15739 begin
15740 if Nkind (Op) in N_Binary_Op then
15741 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15742 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15743
15744 elsif Nkind (Op) in N_Unary_Op then
15745 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15746 end if;
15747 end Qualify_Universal_Operands;
15748
15749 ------------------
15750 -- Reset_Entity --
15751 ------------------
15752
15753 procedure Reset_Entity (N : Node_Id) is
15754 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15755 -- If the type of N2 is global to the generic unit, save the type in
15756 -- the generic node. Just as we perform name capture for explicit
15757 -- references within the generic, we must capture the global types
15758 -- of local entities because they may participate in resolution in
15759 -- the instance.
15760
15761 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15762 -- Find the ultimate ancestor of the current unit. If it is not a
15763 -- generic unit, then the name of the current unit in the prefix of
15764 -- an expanded name must be replaced with its generic homonym to
15765 -- ensure that it will be properly resolved in an instance.
15766
15767 ---------------------
15768 -- Set_Global_Type --
15769 ---------------------
15770
15771 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15772 Typ : constant Entity_Id := Etype (N2);
15773
15774 begin
15775 Set_Etype (N, Typ);
15776
15777 -- If the entity of N is not the associated node, this is a
15778 -- nested generic and it has an associated node as well, whose
15779 -- type is already the full view (see below). Indicate that the
15780 -- original node has a private view.
15781
15782 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15783 Set_Has_Private_View (N);
15784 end if;
15785
15786 -- If not a private type, nothing else to do
15787
15788 if not Is_Private_Type (Typ) then
15789 null;
15790
15791 -- If it is a derivation of a private type in a context where no
15792 -- full view is needed, nothing to do either.
15793
15794 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15795 null;
15796
15797 -- Otherwise mark the type for flipping and use the full view when
15798 -- available.
15799
15800 else
15801 Set_Has_Private_View (N);
15802
15803 if Present (Full_View (Typ)) then
15804 Set_Etype (N2, Full_View (Typ));
15805 end if;
15806 end if;
15807
15808 if Is_Floating_Point_Type (Typ)
15809 and then Has_Dimension_System (Typ)
15810 then
15811 Copy_Dimensions (N2, N);
15812 end if;
15813 end Set_Global_Type;
15814
15815 ------------------
15816 -- Top_Ancestor --
15817 ------------------
15818
15819 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15820 Par : Entity_Id;
15821
15822 begin
15823 Par := E;
15824 while Is_Child_Unit (Par) loop
15825 Par := Scope (Par);
15826 end loop;
15827
15828 return Par;
15829 end Top_Ancestor;
15830
15831 -- Start of processing for Reset_Entity
15832
15833 begin
15834 N2 := Get_Associated_Node (N);
15835 E := Entity (N2);
15836
15837 if Present (E) then
15838
15839 -- If the node is an entry call to an entry in an enclosing task,
15840 -- it is rewritten as a selected component. No global entity to
15841 -- preserve in this case, since the expansion will be redone in
15842 -- the instance.
15843
15844 if Nkind (E) not in N_Entity then
15845 Set_Associated_Node (N, Empty);
15846 Set_Etype (N, Empty);
15847 return;
15848 end if;
15849
15850 -- If the entity is an itype created as a subtype of an access
15851 -- type with a null exclusion restore source entity for proper
15852 -- visibility. The itype will be created anew in the instance.
15853
15854 if Is_Itype (E)
15855 and then Ekind (E) = E_Access_Subtype
15856 and then Is_Entity_Name (N)
15857 and then Chars (Etype (E)) = Chars (N)
15858 then
15859 E := Etype (E);
15860 Set_Entity (N2, E);
15861 Set_Etype (N2, E);
15862 end if;
15863
15864 if Is_Global (E) then
15865 Set_Global_Type (N, N2);
15866
15867 elsif Nkind (N) = N_Op_Concat
15868 and then Is_Generic_Type (Etype (N2))
15869 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15870 or else
15871 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15872 and then Is_Intrinsic_Subprogram (E)
15873 then
15874 null;
15875
15876 -- Entity is local. Mark generic node as unresolved. Note that now
15877 -- it does not have an entity.
15878
15879 else
15880 Set_Associated_Node (N, Empty);
15881 Set_Etype (N, Empty);
15882 end if;
15883
15884 if Nkind (Parent (N)) in N_Generic_Instantiation
15885 and then N = Name (Parent (N))
15886 then
15887 Save_Global_Defaults (Parent (N), Parent (N2));
15888 end if;
15889
15890 elsif Nkind (Parent (N)) = N_Selected_Component
15891 and then Nkind (Parent (N2)) = N_Expanded_Name
15892 then
15893 -- In case of previous errors, the tree might be malformed
15894
15895 if No (Entity (Parent (N2))) then
15896 null;
15897
15898 elsif Is_Global (Entity (Parent (N2))) then
15899 Change_Selected_Component_To_Expanded_Name (Parent (N));
15900 Set_Associated_Node (Parent (N), Parent (N2));
15901 Set_Global_Type (Parent (N), Parent (N2));
15902 Save_Entity_Descendants (N);
15903
15904 -- If this is a reference to the current generic entity, replace
15905 -- by the name of the generic homonym of the current package. This
15906 -- is because in an instantiation Par.P.Q will not resolve to the
15907 -- name of the instance, whose enclosing scope is not necessarily
15908 -- Par. We use the generic homonym rather that the name of the
15909 -- generic itself because it may be hidden by a local declaration.
15910
15911 elsif In_Open_Scopes (Entity (Parent (N2)))
15912 and then not
15913 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15914 then
15915 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15916 Rewrite (Parent (N),
15917 Make_Identifier (Sloc (N),
15918 Chars =>
15919 Chars (Generic_Homonym (Entity (Parent (N2))))));
15920 else
15921 Rewrite (Parent (N),
15922 Make_Identifier (Sloc (N),
15923 Chars => Chars (Selector_Name (Parent (N2)))));
15924 end if;
15925 end if;
15926
15927 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15928 and then Parent (N) = Name (Parent (Parent (N)))
15929 then
15930 Save_Global_Defaults
15931 (Parent (Parent (N)), Parent (Parent (N2)));
15932 end if;
15933
15934 -- A selected component may denote a static constant that has been
15935 -- folded. If the static constant is global to the generic, capture
15936 -- its value. Otherwise the folding will happen in any instantiation.
15937
15938 elsif Nkind (Parent (N)) = N_Selected_Component
15939 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
15940 then
15941 if Present (Entity (Original_Node (Parent (N2))))
15942 and then Is_Global (Entity (Original_Node (Parent (N2))))
15943 then
15944 Rewrite (Parent (N), New_Copy (Parent (N2)));
15945 Set_Analyzed (Parent (N), False);
15946 end if;
15947
15948 -- A selected component may be transformed into a parameterless
15949 -- function call. If the called entity is global, rewrite the node
15950 -- appropriately, i.e. as an extended name for the global entity.
15951
15952 elsif Nkind (Parent (N)) = N_Selected_Component
15953 and then Nkind (Parent (N2)) = N_Function_Call
15954 and then N = Selector_Name (Parent (N))
15955 then
15956 if No (Parameter_Associations (Parent (N2))) then
15957 if Is_Global (Entity (Name (Parent (N2)))) then
15958 Change_Selected_Component_To_Expanded_Name (Parent (N));
15959 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15960 Set_Global_Type (Parent (N), Name (Parent (N2)));
15961 Save_Entity_Descendants (N);
15962
15963 else
15964 Set_Is_Prefixed_Call (Parent (N));
15965 Set_Associated_Node (N, Empty);
15966 Set_Etype (N, Empty);
15967 end if;
15968
15969 -- In Ada 2005, X.F may be a call to a primitive operation,
15970 -- rewritten as F (X). This rewriting will be done again in an
15971 -- instance, so keep the original node. Global entities will be
15972 -- captured as for other constructs. Indicate that this must
15973 -- resolve as a call, to prevent accidental overloading in the
15974 -- instance, if both a component and a primitive operation appear
15975 -- as candidates.
15976
15977 else
15978 Set_Is_Prefixed_Call (Parent (N));
15979 end if;
15980
15981 -- Entity is local. Reset in generic unit, so that node is resolved
15982 -- anew at the point of instantiation.
15983
15984 else
15985 Set_Associated_Node (N, Empty);
15986 Set_Etype (N, Empty);
15987 end if;
15988 end Reset_Entity;
15989
15990 -----------------------------
15991 -- Save_Entity_Descendants --
15992 -----------------------------
15993
15994 procedure Save_Entity_Descendants (N : Node_Id) is
15995 begin
15996 case Nkind (N) is
15997 when N_Binary_Op =>
15998 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15999 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16000
16001 when N_Unary_Op =>
16002 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16003
16004 when N_Expanded_Name
16005 | N_Selected_Component
16006 =>
16007 Save_Global_Descendant (Union_Id (Prefix (N)));
16008 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16009
16010 when N_Character_Literal
16011 | N_Identifier
16012 | N_Operator_Symbol
16013 =>
16014 null;
16015
16016 when others =>
16017 raise Program_Error;
16018 end case;
16019 end Save_Entity_Descendants;
16020
16021 --------------------------
16022 -- Save_Global_Defaults --
16023 --------------------------
16024
16025 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16026 Loc : constant Source_Ptr := Sloc (N1);
16027 Assoc2 : constant List_Id := Generic_Associations (N2);
16028 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16029 Assoc1 : List_Id;
16030 Act1 : Node_Id;
16031 Act2 : Node_Id;
16032 Def : Node_Id;
16033 Ndec : Node_Id;
16034 Subp : Entity_Id;
16035 Actual : Entity_Id;
16036
16037 begin
16038 Assoc1 := Generic_Associations (N1);
16039
16040 if Present (Assoc1) then
16041 Act1 := First (Assoc1);
16042 else
16043 Act1 := Empty;
16044 Set_Generic_Associations (N1, New_List);
16045 Assoc1 := Generic_Associations (N1);
16046 end if;
16047
16048 if Present (Assoc2) then
16049 Act2 := First (Assoc2);
16050 else
16051 return;
16052 end if;
16053
16054 while Present (Act1) and then Present (Act2) loop
16055 Next (Act1);
16056 Next (Act2);
16057 end loop;
16058
16059 -- Find the associations added for default subprograms
16060
16061 if Present (Act2) then
16062 while Nkind (Act2) /= N_Generic_Association
16063 or else No (Entity (Selector_Name (Act2)))
16064 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16065 loop
16066 Next (Act2);
16067 end loop;
16068
16069 -- Add a similar association if the default is global. The
16070 -- renaming declaration for the actual has been analyzed, and
16071 -- its alias is the program it renames. Link the actual in the
16072 -- original generic tree with the node in the analyzed tree.
16073
16074 while Present (Act2) loop
16075 Subp := Entity (Selector_Name (Act2));
16076 Def := Explicit_Generic_Actual_Parameter (Act2);
16077
16078 -- Following test is defence against rubbish errors
16079
16080 if No (Alias (Subp)) then
16081 return;
16082 end if;
16083
16084 -- Retrieve the resolved actual from the renaming declaration
16085 -- created for the instantiated formal.
16086
16087 Actual := Entity (Name (Parent (Parent (Subp))));
16088 Set_Entity (Def, Actual);
16089 Set_Etype (Def, Etype (Actual));
16090
16091 if Is_Global (Actual) then
16092 Ndec :=
16093 Make_Generic_Association (Loc,
16094 Selector_Name =>
16095 New_Occurrence_Of (Subp, Loc),
16096 Explicit_Generic_Actual_Parameter =>
16097 New_Occurrence_Of (Actual, Loc));
16098
16099 Set_Associated_Node
16100 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16101
16102 Append (Ndec, Assoc1);
16103
16104 -- If there are other defaults, add a dummy association in case
16105 -- there are other defaulted formals with the same name.
16106
16107 elsif Present (Next (Act2)) then
16108 Ndec :=
16109 Make_Generic_Association (Loc,
16110 Selector_Name =>
16111 New_Occurrence_Of (Subp, Loc),
16112 Explicit_Generic_Actual_Parameter => Empty);
16113
16114 Append (Ndec, Assoc1);
16115 end if;
16116
16117 Next (Act2);
16118 end loop;
16119 end if;
16120
16121 if Nkind (Name (N1)) = N_Identifier
16122 and then Is_Child_Unit (Gen_Id)
16123 and then Is_Global (Gen_Id)
16124 and then Is_Generic_Unit (Scope (Gen_Id))
16125 and then In_Open_Scopes (Scope (Gen_Id))
16126 then
16127 -- This is an instantiation of a child unit within a sibling, so
16128 -- that the generic parent is in scope. An eventual instance must
16129 -- occur within the scope of an instance of the parent. Make name
16130 -- in instance into an expanded name, to preserve the identifier
16131 -- of the parent, so it can be resolved subsequently.
16132
16133 Rewrite (Name (N2),
16134 Make_Expanded_Name (Loc,
16135 Chars => Chars (Gen_Id),
16136 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16137 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16138 Set_Entity (Name (N2), Gen_Id);
16139
16140 Rewrite (Name (N1),
16141 Make_Expanded_Name (Loc,
16142 Chars => Chars (Gen_Id),
16143 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16144 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16145
16146 Set_Associated_Node (Name (N1), Name (N2));
16147 Set_Associated_Node (Prefix (Name (N1)), Empty);
16148 Set_Associated_Node
16149 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16150 Set_Etype (Name (N1), Etype (Gen_Id));
16151 end if;
16152 end Save_Global_Defaults;
16153
16154 ----------------------------
16155 -- Save_Global_Descendant --
16156 ----------------------------
16157
16158 procedure Save_Global_Descendant (D : Union_Id) is
16159 N1 : Node_Id;
16160
16161 begin
16162 if D in Node_Range then
16163 if D = Union_Id (Empty) then
16164 null;
16165
16166 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16167 Save_References (Node_Id (D));
16168 end if;
16169
16170 elsif D in List_Range then
16171 pragma Assert (D /= Union_Id (No_List));
16172 -- Because No_List = Empty, which is in Node_Range above
16173
16174 if Is_Empty_List (List_Id (D)) then
16175 null;
16176
16177 else
16178 N1 := First (List_Id (D));
16179 while Present (N1) loop
16180 Save_References (N1);
16181 Next (N1);
16182 end loop;
16183 end if;
16184
16185 -- Element list or other non-node field, nothing to do
16186
16187 else
16188 null;
16189 end if;
16190 end Save_Global_Descendant;
16191
16192 ---------------------
16193 -- Save_References --
16194 ---------------------
16195
16196 -- This is the recursive procedure that does the work once the enclosing
16197 -- generic scope has been established. We have to treat specially a
16198 -- number of node rewritings that are required by semantic processing
16199 -- and which change the kind of nodes in the generic copy: typically
16200 -- constant-folding, replacing an operator node by a string literal, or
16201 -- a selected component by an expanded name. In each of those cases, the
16202 -- transformation is propagated to the generic unit.
16203
16204 procedure Save_References (N : Node_Id) is
16205 Loc : constant Source_Ptr := Sloc (N);
16206
16207 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16208 -- Determine whether arbitrary node Nod requires delayed capture of
16209 -- global references within its aspect specifications.
16210
16211 procedure Save_References_In_Aggregate (N : Node_Id);
16212 -- Save all global references in [extension] aggregate node N
16213
16214 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16215 -- Save all global references in a character literal or operator
16216 -- symbol denoted by N.
16217
16218 procedure Save_References_In_Descendants (N : Node_Id);
16219 -- Save all global references in all descendants of node N
16220
16221 procedure Save_References_In_Identifier (N : Node_Id);
16222 -- Save all global references in identifier node N
16223
16224 procedure Save_References_In_Operator (N : Node_Id);
16225 -- Save all global references in operator node N
16226
16227 procedure Save_References_In_Pragma (Prag : Node_Id);
16228 -- Save all global references found within the expression of pragma
16229 -- Prag.
16230
16231 ---------------------------
16232 -- Requires_Delayed_Save --
16233 ---------------------------
16234
16235 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16236 begin
16237 -- Generic packages and subprograms require delayed capture of
16238 -- global references within their aspects due to the timing of
16239 -- annotation analysis.
16240
16241 if Nkind (Nod) in N_Generic_Package_Declaration
16242 | N_Generic_Subprogram_Declaration
16243 | N_Package_Body
16244 | N_Package_Body_Stub
16245 | N_Subprogram_Body
16246 | N_Subprogram_Body_Stub
16247 then
16248 -- Since the capture of global references is done on the
16249 -- unanalyzed generic template, there is no information around
16250 -- to infer the context. Use the Associated_Entity linkages to
16251 -- peek into the analyzed generic copy and determine what the
16252 -- template corresponds to.
16253
16254 if Nod = Templ then
16255 return
16256 Is_Generic_Declaration_Or_Body
16257 (Unit_Declaration_Node
16258 (Associated_Entity (Defining_Entity (Nod))));
16259
16260 -- Otherwise the generic unit being processed is not the top
16261 -- level template. It is safe to capture of global references
16262 -- within the generic unit because at this point the top level
16263 -- copy is fully analyzed.
16264
16265 else
16266 return False;
16267 end if;
16268
16269 -- Otherwise capture the global references without interference
16270
16271 else
16272 return False;
16273 end if;
16274 end Requires_Delayed_Save;
16275
16276 ----------------------------------
16277 -- Save_References_In_Aggregate --
16278 ----------------------------------
16279
16280 procedure Save_References_In_Aggregate (N : Node_Id) is
16281 Nam : Node_Id;
16282 Qual : Node_Id := Empty;
16283 Typ : Entity_Id := Empty;
16284
16285 use Atree.Unchecked_Access;
16286 -- This code section is part of implementing an untyped tree
16287 -- traversal, so it needs direct access to node fields.
16288
16289 begin
16290 N2 := Get_Associated_Node (N);
16291
16292 if Present (N2) then
16293 Typ := Etype (N2);
16294
16295 -- In an instance within a generic, use the name of the actual
16296 -- and not the original generic parameter. If the actual is
16297 -- global in the current generic it must be preserved for its
16298 -- instantiation.
16299
16300 if Nkind (Parent (Typ)) = N_Subtype_Declaration
16301 and then Present (Generic_Parent_Type (Parent (Typ)))
16302 then
16303 Typ := Base_Type (Typ);
16304 Set_Etype (N2, Typ);
16305 end if;
16306 end if;
16307
16308 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16309 Set_Associated_Node (N, Empty);
16310
16311 -- If the aggregate is an actual in a call, it has been
16312 -- resolved in the current context, to some local type. The
16313 -- enclosing call may have been disambiguated by the aggregate,
16314 -- and this disambiguation might fail at instantiation time
16315 -- because the type to which the aggregate did resolve is not
16316 -- preserved. In order to preserve some of this information,
16317 -- wrap the aggregate in a qualified expression, using the id
16318 -- of its type. For further disambiguation we qualify the type
16319 -- name with its scope (if visible and not hidden by a local
16320 -- homograph) because both id's will have corresponding
16321 -- entities in an instance. This resolves most of the problems
16322 -- with missing type information on aggregates in instances.
16323
16324 if Present (N2)
16325 and then Nkind (N2) = Nkind (N)
16326 and then Nkind (Parent (N2)) in N_Subprogram_Call
16327 and then Present (Typ)
16328 and then Comes_From_Source (Typ)
16329 then
16330 Nam := Make_Identifier (Loc, Chars (Typ));
16331
16332 if Is_Immediately_Visible (Scope (Typ))
16333 and then
16334 (not In_Open_Scopes (Scope (Typ))
16335 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16336 then
16337 Nam :=
16338 Make_Selected_Component (Loc,
16339 Prefix =>
16340 Make_Identifier (Loc, Chars (Scope (Typ))),
16341 Selector_Name => Nam);
16342 end if;
16343
16344 Qual :=
16345 Make_Qualified_Expression (Loc,
16346 Subtype_Mark => Nam,
16347 Expression => Relocate_Node (N));
16348 end if;
16349 end if;
16350
16351 Save_Global_Descendant (Field1 (N));
16352 Save_Global_Descendant (Field2 (N));
16353 Save_Global_Descendant (Field3 (N));
16354 Save_Global_Descendant (Field5 (N));
16355
16356 if Present (Qual) then
16357 Rewrite (N, Qual);
16358 end if;
16359 end Save_References_In_Aggregate;
16360
16361 ----------------------------------------------
16362 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16363 ----------------------------------------------
16364
16365 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16366 begin
16367 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16368 Reset_Entity (N);
16369
16370 elsif Nkind (N) = N_Operator_Symbol
16371 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16372 then
16373 Change_Operator_Symbol_To_String_Literal (N);
16374 end if;
16375 end Save_References_In_Char_Lit_Or_Op_Symbol;
16376
16377 ------------------------------------
16378 -- Save_References_In_Descendants --
16379 ------------------------------------
16380
16381 procedure Save_References_In_Descendants (N : Node_Id) is
16382 use Atree.Unchecked_Access;
16383 -- This code section is part of implementing an untyped tree
16384 -- traversal, so it needs direct access to node fields.
16385
16386 begin
16387 Save_Global_Descendant (Field1 (N));
16388 Save_Global_Descendant (Field2 (N));
16389 Save_Global_Descendant (Field3 (N));
16390 Save_Global_Descendant (Field4 (N));
16391 Save_Global_Descendant (Field5 (N));
16392 end Save_References_In_Descendants;
16393
16394 -----------------------------------
16395 -- Save_References_In_Identifier --
16396 -----------------------------------
16397
16398 procedure Save_References_In_Identifier (N : Node_Id) is
16399 begin
16400 -- The node did not undergo a transformation
16401
16402 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16403 -- If this is a discriminant reference, always save it.
16404 -- It is used in the instance to find the corresponding
16405 -- discriminant positionally rather than by name.
16406
16407 Set_Original_Discriminant
16408 (N, Original_Discriminant (Get_Associated_Node (N)));
16409
16410 Reset_Entity (N);
16411
16412 -- The analysis of the generic copy transformed the identifier
16413 -- into another construct. Propagate the changes to the template.
16414
16415 else
16416 N2 := Get_Associated_Node (N);
16417
16418 -- The identifier denotes a call to a parameterless function.
16419 -- Mark the node as resolved when the function is external.
16420
16421 if Nkind (N2) = N_Function_Call then
16422 E := Entity (Name (N2));
16423
16424 if Present (E) and then Is_Global (E) then
16425 Set_Etype (N, Etype (N2));
16426 else
16427 Set_Associated_Node (N, Empty);
16428 Set_Etype (N, Empty);
16429 end if;
16430
16431 -- The identifier denotes a named number that was constant
16432 -- folded. Preserve the original name for ASIS and undo the
16433 -- constant folding which will be repeated in the instance.
16434 -- Is this still needed???
16435
16436 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16437 and then Is_Entity_Name (Original_Node (N2))
16438 then
16439 Set_Associated_Node (N, Original_Node (N2));
16440 Reset_Entity (N);
16441
16442 -- The identifier resolved to a string literal. Propagate this
16443 -- information to the generic template.
16444
16445 elsif Nkind (N2) = N_String_Literal then
16446 Rewrite (N, New_Copy (N2));
16447
16448 -- The identifier is rewritten as a dereference if it is the
16449 -- prefix of an implicit dereference. Preserve the original
16450 -- tree as the analysis of the instance will expand the node
16451 -- again, but preserve the resolved entity if it is global.
16452
16453 elsif Nkind (N2) = N_Explicit_Dereference then
16454 if Is_Entity_Name (Prefix (N2))
16455 and then Present (Entity (Prefix (N2)))
16456 and then Is_Global (Entity (Prefix (N2)))
16457 then
16458 Set_Associated_Node (N, Prefix (N2));
16459
16460 elsif Nkind (Prefix (N2)) = N_Function_Call
16461 and then Present (Entity (Name (Prefix (N2))))
16462 and then Is_Global (Entity (Name (Prefix (N2))))
16463 then
16464 Rewrite (N,
16465 Make_Explicit_Dereference (Loc,
16466 Prefix =>
16467 Make_Function_Call (Loc,
16468 Name =>
16469 New_Occurrence_Of
16470 (Entity (Name (Prefix (N2))), Loc))));
16471
16472 else
16473 Set_Associated_Node (N, Empty);
16474 Set_Etype (N, Empty);
16475 end if;
16476
16477 -- The subtype mark of a nominally unconstrained object is
16478 -- rewritten as a subtype indication using the bounds of the
16479 -- expression. Recover the original subtype mark.
16480
16481 elsif Nkind (N2) = N_Subtype_Indication
16482 and then Is_Entity_Name (Original_Node (N2))
16483 then
16484 Set_Associated_Node (N, Original_Node (N2));
16485 Reset_Entity (N);
16486 end if;
16487 end if;
16488 end Save_References_In_Identifier;
16489
16490 ---------------------------------
16491 -- Save_References_In_Operator --
16492 ---------------------------------
16493
16494 procedure Save_References_In_Operator (N : Node_Id) is
16495 begin
16496 -- The node did not undergo a transformation
16497
16498 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16499 if Nkind (N) = N_Op_Concat then
16500 Set_Is_Component_Left_Opnd (N,
16501 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16502
16503 Set_Is_Component_Right_Opnd (N,
16504 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16505 end if;
16506
16507 Reset_Entity (N);
16508
16509 -- The analysis of the generic copy transformed the operator into
16510 -- some other construct. Propagate the changes to the template if
16511 -- applicable.
16512
16513 else
16514 N2 := Get_Associated_Node (N);
16515
16516 -- The operator resoved to a function call
16517
16518 if Nkind (N2) = N_Function_Call then
16519
16520 -- Add explicit qualifications in the generic template for
16521 -- all operands of universal type. This aids resolution by
16522 -- preserving the actual type of a literal or an attribute
16523 -- that yields a universal result.
16524
16525 Qualify_Universal_Operands (N, N2);
16526
16527 E := Entity (Name (N2));
16528
16529 if Present (E) and then Is_Global (E) then
16530 Set_Etype (N, Etype (N2));
16531 else
16532 Set_Associated_Node (N, Empty);
16533 Set_Etype (N, Empty);
16534 end if;
16535
16536 -- The operator was folded into a literal
16537
16538 elsif Nkind (N2) in N_Integer_Literal
16539 | N_Real_Literal
16540 | N_String_Literal
16541 then
16542 if Present (Original_Node (N2))
16543 and then Nkind (Original_Node (N2)) = Nkind (N)
16544 then
16545 -- Operation was constant-folded. Whenever possible,
16546 -- recover semantic information from unfolded node.
16547 -- This was initially done for ASIS but is apparently
16548 -- needed also for e.g. compiling a-nbnbin.adb.
16549
16550 Set_Associated_Node (N, Original_Node (N2));
16551
16552 if Nkind (N) = N_Op_Concat then
16553 Set_Is_Component_Left_Opnd (N,
16554 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16555 Set_Is_Component_Right_Opnd (N,
16556 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16557 end if;
16558
16559 Reset_Entity (N);
16560
16561 -- Propagate the constant folding back to the template
16562
16563 else
16564 Rewrite (N, New_Copy (N2));
16565 Set_Analyzed (N, False);
16566 end if;
16567
16568 -- The operator was folded into an enumeration literal. Retain
16569 -- the entity to avoid spurious ambiguities if it is overloaded
16570 -- at the point of instantiation or inlining.
16571
16572 elsif Nkind (N2) = N_Identifier
16573 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16574 then
16575 Rewrite (N, New_Copy (N2));
16576 Set_Analyzed (N, False);
16577 end if;
16578 end if;
16579
16580 -- Complete the operands check if node has not been constant
16581 -- folded.
16582
16583 if Nkind (N) in N_Op then
16584 Save_Entity_Descendants (N);
16585 end if;
16586 end Save_References_In_Operator;
16587
16588 -------------------------------
16589 -- Save_References_In_Pragma --
16590 -------------------------------
16591
16592 procedure Save_References_In_Pragma (Prag : Node_Id) is
16593 Context : Node_Id;
16594 Do_Save : Boolean := True;
16595
16596 use Atree.Unchecked_Access;
16597 -- This code section is part of implementing an untyped tree
16598 -- traversal, so it needs direct access to node fields.
16599
16600 begin
16601 -- Do not save global references in pragmas generated from aspects
16602 -- because the pragmas will be regenerated at instantiation time.
16603
16604 if From_Aspect_Specification (Prag) then
16605 Do_Save := False;
16606
16607 -- The capture of global references within contract-related source
16608 -- pragmas associated with generic packages, subprograms or their
16609 -- respective bodies must be delayed due to timing of annotation
16610 -- analysis. Global references are still captured in routine
16611 -- Save_Global_References_In_Contract.
16612
16613 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16614 if Is_Package_Contract_Annotation (Prag) then
16615 Context := Find_Related_Package_Or_Body (Prag);
16616 else
16617 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16618 Context := Find_Related_Declaration_Or_Body (Prag);
16619 end if;
16620
16621 -- The use of Original_Node accounts for the case when the
16622 -- related context is generic template.
16623
16624 if Requires_Delayed_Save (Original_Node (Context)) then
16625 Do_Save := False;
16626 end if;
16627 end if;
16628
16629 -- For all other cases, save all global references within the
16630 -- descendants, but skip the following semantic fields:
16631
16632 -- Field1 - Next_Pragma
16633 -- Field3 - Corresponding_Aspect
16634 -- Field5 - Next_Rep_Item
16635
16636 if Do_Save then
16637 Save_Global_Descendant (Field2 (Prag));
16638 Save_Global_Descendant (Field4 (Prag));
16639 end if;
16640 end Save_References_In_Pragma;
16641
16642 -- Start of processing for Save_References
16643
16644 begin
16645 if N = Empty then
16646 null;
16647
16648 -- Aggregates
16649
16650 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16651 Save_References_In_Aggregate (N);
16652
16653 -- Character literals, operator symbols
16654
16655 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16656 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16657
16658 -- Defining identifiers
16659
16660 elsif Nkind (N) in N_Entity then
16661 null;
16662
16663 -- Identifiers
16664
16665 elsif Nkind (N) = N_Identifier then
16666 Save_References_In_Identifier (N);
16667
16668 -- Operators
16669
16670 elsif Nkind (N) in N_Op then
16671 Save_References_In_Operator (N);
16672
16673 -- Pragmas
16674
16675 elsif Nkind (N) = N_Pragma then
16676 Save_References_In_Pragma (N);
16677
16678 else
16679 Save_References_In_Descendants (N);
16680 end if;
16681
16682 -- Save all global references found within the aspect specifications
16683 -- of the related node.
16684
16685 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16686
16687 -- The capture of global references within aspects associated with
16688 -- generic packages, subprograms or their bodies must be delayed
16689 -- due to timing of annotation analysis. Global references are
16690 -- still captured in routine Save_Global_References_In_Contract.
16691
16692 if Requires_Delayed_Save (N) then
16693 null;
16694
16695 -- Otherwise save all global references within the aspects
16696
16697 else
16698 Save_Global_References_In_Aspects (N);
16699 end if;
16700 end if;
16701 end Save_References;
16702
16703 -- Start of processing for Save_Global_References
16704
16705 begin
16706 Gen_Scope := Current_Scope;
16707
16708 -- If the generic unit is a child unit, references to entities in the
16709 -- parent are treated as local, because they will be resolved anew in
16710 -- the context of the instance of the parent.
16711
16712 while Is_Child_Unit (Gen_Scope)
16713 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16714 loop
16715 Gen_Scope := Scope (Gen_Scope);
16716 end loop;
16717
16718 Save_References (Templ);
16719 end Save_Global_References;
16720
16721 ---------------------------------------
16722 -- Save_Global_References_In_Aspects --
16723 ---------------------------------------
16724
16725 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16726 Asp : Node_Id;
16727 Expr : Node_Id;
16728
16729 begin
16730 Asp := First (Aspect_Specifications (N));
16731 while Present (Asp) loop
16732 Expr := Expression (Asp);
16733
16734 if Present (Expr) then
16735 Save_Global_References (Expr);
16736 end if;
16737
16738 Next (Asp);
16739 end loop;
16740 end Save_Global_References_In_Aspects;
16741
16742 ------------------------------------------
16743 -- Set_Copied_Sloc_For_Inherited_Pragma --
16744 ------------------------------------------
16745
16746 procedure Set_Copied_Sloc_For_Inherited_Pragma
16747 (N : Node_Id;
16748 E : Entity_Id)
16749 is
16750 begin
16751 Create_Instantiation_Source (N, E,
16752 Inlined_Body => False,
16753 Inherited_Pragma => True,
16754 Factor => S_Adjustment);
16755 end Set_Copied_Sloc_For_Inherited_Pragma;
16756
16757 --------------------------------------
16758 -- Set_Copied_Sloc_For_Inlined_Body --
16759 --------------------------------------
16760
16761 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16762 begin
16763 Create_Instantiation_Source (N, E,
16764 Inlined_Body => True,
16765 Inherited_Pragma => False,
16766 Factor => S_Adjustment);
16767 end Set_Copied_Sloc_For_Inlined_Body;
16768
16769 ---------------------
16770 -- Set_Instance_Of --
16771 ---------------------
16772
16773 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16774 begin
16775 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16776 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16777 Generic_Renamings.Increment_Last;
16778 end Set_Instance_Of;
16779
16780 --------------------
16781 -- Set_Next_Assoc --
16782 --------------------
16783
16784 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16785 begin
16786 Generic_Renamings.Table (E).Next_In_HTable := Next;
16787 end Set_Next_Assoc;
16788
16789 -------------------
16790 -- Start_Generic --
16791 -------------------
16792
16793 procedure Start_Generic is
16794 begin
16795 -- ??? More things could be factored out in this routine.
16796 -- Should probably be done at a later stage.
16797
16798 Generic_Flags.Append (Inside_A_Generic);
16799 Inside_A_Generic := True;
16800
16801 Expander_Mode_Save_And_Set (False);
16802 end Start_Generic;
16803
16804 ----------------------
16805 -- Set_Instance_Env --
16806 ----------------------
16807
16808 -- WARNING: This routine manages SPARK regions
16809
16810 procedure Set_Instance_Env
16811 (Gen_Unit : Entity_Id;
16812 Act_Unit : Entity_Id)
16813 is
16814 Saved_AE : constant Boolean := Assertions_Enabled;
16815 Saved_CPL : constant Node_Id := Check_Policy_List;
16816 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16817 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16818 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16819
16820 begin
16821 -- Regardless of the current mode, predefined units are analyzed in the
16822 -- most current Ada mode, and earlier version Ada checks do not apply
16823 -- to predefined units. Nothing needs to be done for non-internal units.
16824 -- These are always analyzed in the current mode.
16825
16826 if In_Internal_Unit (Gen_Unit) then
16827
16828 -- The following call resets all configuration attributes to default
16829 -- or the xxx_Config versions of the attributes when the current sem
16830 -- unit is the main unit. At the same time, internal units must also
16831 -- inherit certain configuration attributes from their context. It
16832 -- is unclear what these two sets are.
16833
16834 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16835
16836 -- Reinstall relevant configuration attributes of the context
16837
16838 Assertions_Enabled := Saved_AE;
16839 Check_Policy_List := Saved_CPL;
16840 Dynamic_Elaboration_Checks := Saved_DEC;
16841
16842 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16843 end if;
16844
16845 Current_Instantiated_Parent :=
16846 (Gen_Id => Gen_Unit,
16847 Act_Id => Act_Unit,
16848 Next_In_HTable => Assoc_Null);
16849 end Set_Instance_Env;
16850
16851 -----------------
16852 -- Switch_View --
16853 -----------------
16854
16855 procedure Switch_View (T : Entity_Id) is
16856 BT : constant Entity_Id := Base_Type (T);
16857 Priv_Elmt : Elmt_Id := No_Elmt;
16858 Priv_Sub : Entity_Id;
16859
16860 begin
16861 -- T may be private but its base type may have been exchanged through
16862 -- some other occurrence, in which case there is nothing to switch
16863 -- besides T itself. Note that a private dependent subtype of a private
16864 -- type might not have been switched even if the base type has been,
16865 -- because of the last branch of Check_Private_View (see comment there).
16866
16867 if not Is_Private_Type (BT) then
16868 Prepend_Elmt (Full_View (T), Exchanged_Views);
16869 Exchange_Declarations (T);
16870 return;
16871 end if;
16872
16873 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16874
16875 if Present (Full_View (BT)) then
16876 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16877 Exchange_Declarations (BT);
16878 end if;
16879
16880 while Present (Priv_Elmt) loop
16881 Priv_Sub := Node (Priv_Elmt);
16882
16883 if Present (Full_View (Priv_Sub)) then
16884 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16885 Exchange_Declarations (Priv_Sub);
16886 end if;
16887
16888 Next_Elmt (Priv_Elmt);
16889 end loop;
16890 end Switch_View;
16891
16892 -----------------
16893 -- True_Parent --
16894 -----------------
16895
16896 function True_Parent (N : Node_Id) return Node_Id is
16897 begin
16898 if Nkind (Parent (N)) = N_Subunit then
16899 return Parent (Corresponding_Stub (Parent (N)));
16900 else
16901 return Parent (N);
16902 end if;
16903 end True_Parent;
16904
16905 -----------------------------
16906 -- Valid_Default_Attribute --
16907 -----------------------------
16908
16909 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16910 Attr_Id : constant Attribute_Id :=
16911 Get_Attribute_Id (Attribute_Name (Def));
16912 T : constant Entity_Id := Entity (Prefix (Def));
16913 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16914 F : Entity_Id;
16915 Num_F : Nat;
16916 OK : Boolean;
16917
16918 begin
16919 if No (T) or else T = Any_Id then
16920 return;
16921 end if;
16922
16923 Num_F := 0;
16924 F := First_Formal (Nam);
16925 while Present (F) loop
16926 Num_F := Num_F + 1;
16927 Next_Formal (F);
16928 end loop;
16929
16930 case Attr_Id is
16931 when Attribute_Adjacent
16932 | Attribute_Ceiling
16933 | Attribute_Copy_Sign
16934 | Attribute_Floor
16935 | Attribute_Fraction
16936 | Attribute_Machine
16937 | Attribute_Model
16938 | Attribute_Remainder
16939 | Attribute_Rounding
16940 | Attribute_Unbiased_Rounding
16941 =>
16942 OK := Is_Fun
16943 and then Num_F = 1
16944 and then Is_Floating_Point_Type (T);
16945
16946 when Attribute_Image
16947 | Attribute_Pred
16948 | Attribute_Succ
16949 | Attribute_Value
16950 | Attribute_Wide_Image
16951 | Attribute_Wide_Value
16952 =>
16953 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16954
16955 when Attribute_Max
16956 | Attribute_Min
16957 =>
16958 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16959
16960 when Attribute_Input =>
16961 OK := (Is_Fun and then Num_F = 1);
16962
16963 when Attribute_Output
16964 | Attribute_Put_Image
16965 | Attribute_Read
16966 | Attribute_Write
16967 =>
16968 OK := not Is_Fun and then Num_F = 2;
16969
16970 when others =>
16971 OK := False;
16972 end case;
16973
16974 if not OK then
16975 Error_Msg_N
16976 ("attribute reference has wrong profile for subprogram", Def);
16977 end if;
16978 end Valid_Default_Attribute;
16979
16980 end Sem_Ch12;