[multiple changes]
[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-2014, 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 Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Disp; use Exp_Disp;
33 with Exp_Util; use Exp_Util;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
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 -- This should really be reset on encountering a new main unit, but in
245 -- practice we are not using multiple main units so it is not critical.
246
247 --------------------------------------------------
248 -- Formal packages and partial parameterization --
249 --------------------------------------------------
250
251 -- When compiling a generic, a formal package is a local instantiation. If
252 -- declared with a box, its generic formals are visible in the enclosing
253 -- generic. If declared with a partial list of actuals, those actuals that
254 -- are defaulted (covered by an Others clause, or given an explicit box
255 -- initialization) are also visible in the enclosing generic, while those
256 -- that have a corresponding actual are not.
257
258 -- In our source model of instantiation, the same visibility must be
259 -- present in the spec and body of an instance: the names of the formals
260 -- that are defaulted must be made visible within the instance, and made
261 -- invisible (hidden) after the instantiation is complete, so that they
262 -- are not accessible outside of the instance.
263
264 -- In a generic, a formal package is treated like a special instantiation.
265 -- Our Ada 95 compiler handled formals with and without box in different
266 -- ways. With partial parameterization, we use a single model for both.
267 -- We create a package declaration that consists of the specification of
268 -- the generic package, and a set of declarations that map the actuals
269 -- into local renamings, just as we do for bona fide instantiations. For
270 -- defaulted parameters and formals with a box, we copy directly the
271 -- declarations of the formal into this local package. The result is a
272 -- a package whose visible declarations may include generic formals. This
273 -- package is only used for type checking and visibility analysis, and
274 -- never reaches the back-end, so it can freely violate the placement
275 -- rules for generic formal declarations.
276
277 -- The list of declarations (renamings and copies of formals) is built
278 -- by Analyze_Associations, just as for regular instantiations.
279
280 -- At the point of instantiation, conformance checking must be applied only
281 -- to those parameters that were specified in the formal. We perform this
282 -- checking by creating another internal instantiation, this one including
283 -- only the renamings and the formals (the rest of the package spec is not
284 -- relevant to conformance checking). We can then traverse two lists: the
285 -- list of actuals in the instance that corresponds to the formal package,
286 -- and the list of actuals produced for this bogus instantiation. We apply
287 -- the conformance rules to those actuals that are not defaulted (i.e.
288 -- which still appear as generic formals.
289
290 -- When we compile an instance body we must make the right parameters
291 -- visible again. The predicate Is_Generic_Formal indicates which of the
292 -- formals should have its Is_Hidden flag reset.
293
294 -----------------------
295 -- Local subprograms --
296 -----------------------
297
298 procedure Abandon_Instantiation (N : Node_Id);
299 pragma No_Return (Abandon_Instantiation);
300 -- Posts an error message "instantiation abandoned" at the indicated node
301 -- and then raises the exception Instantiation_Error to do it.
302
303 procedure Analyze_Formal_Array_Type
304 (T : in out Entity_Id;
305 Def : Node_Id);
306 -- A formal array type is treated like an array type declaration, and
307 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
308 -- in-out, because in the case of an anonymous type the entity is
309 -- actually created in the procedure.
310
311 -- The following procedures treat other kinds of formal parameters
312
313 procedure Analyze_Formal_Derived_Interface_Type
314 (N : Node_Id;
315 T : Entity_Id;
316 Def : Node_Id);
317
318 procedure Analyze_Formal_Derived_Type
319 (N : Node_Id;
320 T : Entity_Id;
321 Def : Node_Id);
322
323 procedure Analyze_Formal_Interface_Type
324 (N : Node_Id;
325 T : Entity_Id;
326 Def : Node_Id);
327
328 -- The following subprograms create abbreviated declarations for formal
329 -- scalar types. We introduce an anonymous base of the proper class for
330 -- each of them, and define the formals as constrained first subtypes of
331 -- their bases. The bounds are expressions that are non-static in the
332 -- generic.
333
334 procedure Analyze_Formal_Decimal_Fixed_Point_Type
335 (T : Entity_Id; Def : Node_Id);
336 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
337 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
339 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
340 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
341 (T : Entity_Id; Def : Node_Id);
342
343 procedure Analyze_Formal_Private_Type
344 (N : Node_Id;
345 T : Entity_Id;
346 Def : Node_Id);
347 -- Creates a new private type, which does not require completion
348
349 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
350 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
351
352 procedure Analyze_Generic_Formal_Part (N : Node_Id);
353 -- Analyze generic formal part
354
355 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
356 -- Create a new access type with the given designated type
357
358 function Analyze_Associations
359 (I_Node : Node_Id;
360 Formals : List_Id;
361 F_Copy : List_Id) return List_Id;
362 -- At instantiation time, build the list of associations between formals
363 -- and actuals. Each association becomes a renaming declaration for the
364 -- formal entity. F_Copy is the analyzed list of formals in the generic
365 -- copy. It is used to apply legality checks to the actuals. I_Node is the
366 -- instantiation node itself.
367
368 procedure Analyze_Subprogram_Instantiation
369 (N : Node_Id;
370 K : Entity_Kind);
371
372 procedure Build_Instance_Compilation_Unit_Nodes
373 (N : Node_Id;
374 Act_Body : Node_Id;
375 Act_Decl : Node_Id);
376 -- This procedure is used in the case where the generic instance of a
377 -- subprogram body or package body is a library unit. In this case, the
378 -- original library unit node for the generic instantiation must be
379 -- replaced by the resulting generic body, and a link made to a new
380 -- compilation unit node for the generic declaration. The argument N is
381 -- the original generic instantiation. Act_Body and Act_Decl are the body
382 -- and declaration of the instance (either package body and declaration
383 -- nodes or subprogram body and declaration nodes depending on the case).
384 -- On return, the node N has been rewritten with the actual body.
385
386 procedure Check_Access_Definition (N : Node_Id);
387 -- Subsidiary routine to null exclusion processing. Perform an assertion
388 -- check on Ada version and the presence of an access definition in N.
389
390 procedure Check_Formal_Packages (P_Id : Entity_Id);
391 -- Apply the following to all formal packages in generic associations
392
393 procedure Check_Formal_Package_Instance
394 (Formal_Pack : Entity_Id;
395 Actual_Pack : Entity_Id);
396 -- Verify that the actuals of the actual instance match the actuals of
397 -- the template for a formal package that is not declared with a box.
398
399 procedure Check_Forward_Instantiation (Decl : Node_Id);
400 -- If the generic is a local entity and the corresponding body has not
401 -- been seen yet, flag enclosing packages to indicate that it will be
402 -- elaborated after the generic body. Subprograms declared in the same
403 -- package cannot be inlined by the front-end because front-end inlining
404 -- requires a strict linear order of elaboration.
405
406 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
407 -- Check if some association between formals and actuals requires to make
408 -- visible primitives of a tagged type, and make those primitives visible.
409 -- Return the list of primitives whose visibility is modified (to restore
410 -- their visibility later through Restore_Hidden_Primitives). If no
411 -- candidate is found then return No_Elist.
412
413 procedure Check_Hidden_Child_Unit
414 (N : Node_Id;
415 Gen_Unit : Entity_Id;
416 Act_Decl_Id : Entity_Id);
417 -- If the generic unit is an implicit child instance within a parent
418 -- instance, we need to make an explicit test that it is not hidden by
419 -- a child instance of the same name and parent.
420
421 procedure Check_Generic_Actuals
422 (Instance : Entity_Id;
423 Is_Formal_Box : Boolean);
424 -- Similar to previous one. Check the actuals in the instantiation,
425 -- whose views can change between the point of instantiation and the point
426 -- of instantiation of the body. In addition, mark the generic renamings
427 -- as generic actuals, so that they are not compatible with other actuals.
428 -- Recurse on an actual that is a formal package whose declaration has
429 -- a box.
430
431 function Contains_Instance_Of
432 (Inner : Entity_Id;
433 Outer : Entity_Id;
434 N : Node_Id) return Boolean;
435 -- Inner is instantiated within the generic Outer. Check whether Inner
436 -- directly or indirectly contains an instance of Outer or of one of its
437 -- parents, in the case of a subunit. Each generic unit holds a list of
438 -- the entities instantiated within (at any depth). This procedure
439 -- determines whether the set of such lists contains a cycle, i.e. an
440 -- illegal circular instantiation.
441
442 function Denotes_Formal_Package
443 (Pack : Entity_Id;
444 On_Exit : Boolean := False;
445 Instance : Entity_Id := Empty) return Boolean;
446 -- Returns True if E is a formal package of an enclosing generic, or
447 -- the actual for such a formal in an enclosing instantiation. If such
448 -- a package is used as a formal in an nested generic, or as an actual
449 -- in a nested instantiation, the visibility of ITS formals should not
450 -- be modified. When called from within Restore_Private_Views, the flag
451 -- On_Exit is true, to indicate that the search for a possible enclosing
452 -- instance should ignore the current one. In that case Instance denotes
453 -- the declaration for which this is an actual. This declaration may be
454 -- an instantiation in the source, or the internal instantiation that
455 -- corresponds to the actual for a formal package.
456
457 function Earlier (N1, N2 : Node_Id) return Boolean;
458 -- Yields True if N1 and N2 appear in the same compilation unit,
459 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
460 -- traversal of the tree for the unit. Used to determine the placement
461 -- of freeze nodes for instance bodies that may depend on other instances.
462
463 function Find_Actual_Type
464 (Typ : Entity_Id;
465 Gen_Type : Entity_Id) return Entity_Id;
466 -- When validating the actual types of a child instance, check whether
467 -- the formal is a formal type of the parent unit, and retrieve the current
468 -- actual for it. Typ is the entity in the analyzed formal type declaration
469 -- (component or index type of an array type, or designated type of an
470 -- access formal) and Gen_Type is the enclosing analyzed formal array
471 -- or access type. The desired actual may be a formal of a parent, or may
472 -- be declared in a formal package of a parent. In both cases it is a
473 -- generic actual type because it appears within a visible instance.
474 -- Finally, it may be declared in a parent unit without being a formal
475 -- of that unit, in which case it must be retrieved by visibility.
476 -- Ambiguities may still arise if two homonyms are declared in two formal
477 -- packages, and the prefix of the formal type may be needed to resolve
478 -- the ambiguity in the instance ???
479
480 function In_Same_Declarative_Part
481 (F_Node : Node_Id;
482 Inst : Node_Id) return Boolean;
483 -- True if the instantiation Inst and the given freeze_node F_Node appear
484 -- within the same declarative part, ignoring subunits, but with no inter-
485 -- vening subprograms or concurrent units. Used to find the proper plave
486 -- for the freeze node of an instance, when the generic is declared in a
487 -- previous instance. If predicate is true, the freeze node of the instance
488 -- can be placed after the freeze node of the previous instance, Otherwise
489 -- it has to be placed at the end of the current declarative part.
490
491 function In_Main_Context (E : Entity_Id) return Boolean;
492 -- Check whether an instantiation is in the context of the main unit.
493 -- Used to determine whether its body should be elaborated to allow
494 -- front-end inlining.
495
496 procedure Set_Instance_Env
497 (Gen_Unit : Entity_Id;
498 Act_Unit : Entity_Id);
499 -- Save current instance on saved environment, to be used to determine
500 -- the global status of entities in nested instances. Part of Save_Env.
501 -- called after verifying that the generic unit is legal for the instance,
502 -- The procedure also examines whether the generic unit is a predefined
503 -- unit, in order to set configuration switches accordingly. As a result
504 -- the procedure must be called after analyzing and freezing the actuals.
505
506 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
507 -- Associate analyzed generic parameter with corresponding
508 -- instance. Used for semantic checks at instantiation time.
509
510 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
511 -- Traverse the Exchanged_Views list to see if a type was private
512 -- and has already been flipped during this phase of instantiation.
513
514 procedure Hide_Current_Scope;
515 -- When instantiating a generic child unit, the parent context must be
516 -- present, but the instance and all entities that may be generated
517 -- must be inserted in the current scope. We leave the current scope
518 -- on the stack, but make its entities invisible to avoid visibility
519 -- problems. This is reversed at the end of the instantiation. This is
520 -- not done for the instantiation of the bodies, which only require the
521 -- instances of the generic parents to be in scope.
522
523 procedure Install_Body
524 (Act_Body : Node_Id;
525 N : Node_Id;
526 Gen_Body : Node_Id;
527 Gen_Decl : Node_Id);
528 -- If the instantiation happens textually before the body of the generic,
529 -- the instantiation of the body must be analyzed after the generic body,
530 -- and not at the point of instantiation. Such early instantiations can
531 -- happen if the generic and the instance appear in a package declaration
532 -- because the generic body can only appear in the corresponding package
533 -- body. Early instantiations can also appear if generic, instance and
534 -- body are all in the declarative part of a subprogram or entry. Entities
535 -- of packages that are early instantiations are delayed, and their freeze
536 -- node appears after the generic body.
537
538 procedure Insert_Freeze_Node_For_Instance
539 (N : Node_Id;
540 F_Node : Node_Id);
541 -- N denotes a package or a subprogram instantiation and F_Node is the
542 -- associated freeze node. Insert the freeze node before the first source
543 -- body which follows immediately after N. If no such body is found, the
544 -- freeze node is inserted at the end of the declarative region which
545 -- contains N.
546
547 procedure Freeze_Subprogram_Body
548 (Inst_Node : Node_Id;
549 Gen_Body : Node_Id;
550 Pack_Id : Entity_Id);
551 -- The generic body may appear textually after the instance, including
552 -- in the proper body of a stub, or within a different package instance.
553 -- Given that the instance can only be elaborated after the generic, we
554 -- place freeze_nodes for the instance and/or for packages that may enclose
555 -- the instance and the generic, so that the back-end can establish the
556 -- proper order of elaboration.
557
558 procedure Init_Env;
559 -- Establish environment for subsequent instantiation. Separated from
560 -- Save_Env because data-structures for visibility handling must be
561 -- initialized before call to Check_Generic_Child_Unit.
562
563 procedure Install_Formal_Packages (Par : Entity_Id);
564 -- Install the visible part of any formal of the parent that is a formal
565 -- package. Note that for the case of a formal package with a box, this
566 -- includes the formal part of the formal package (12.7(10/2)).
567
568 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
569 -- When compiling an instance of a child unit the parent (which is
570 -- itself an instance) is an enclosing scope that must be made
571 -- immediately visible. This procedure is also used to install the non-
572 -- generic parent of a generic child unit when compiling its body, so
573 -- that full views of types in the parent are made visible.
574
575 procedure Remove_Parent (In_Body : Boolean := False);
576 -- Reverse effect after instantiation of child is complete
577
578 procedure Install_Hidden_Primitives
579 (Prims_List : in out Elist_Id;
580 Gen_T : Entity_Id;
581 Act_T : Entity_Id);
582 -- Remove suffix 'P' from hidden primitives of Act_T to match the
583 -- visibility of primitives of Gen_T. The list of primitives to which
584 -- the suffix is removed is added to Prims_List to restore them later.
585
586 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
587 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
588 -- set to No_Elist.
589
590 procedure Inline_Instance_Body
591 (N : Node_Id;
592 Gen_Unit : Entity_Id;
593 Act_Decl : Node_Id);
594 -- If front-end inlining is requested, instantiate the package body,
595 -- and preserve the visibility of its compilation unit, to insure
596 -- that successive instantiations succeed.
597
598 -- The functions Instantiate_XXX perform various legality checks and build
599 -- the declarations for instantiated generic parameters. In all of these
600 -- Formal is the entity in the generic unit, Actual is the entity of
601 -- expression in the generic associations, and Analyzed_Formal is the
602 -- formal in the generic copy, which contains the semantic information to
603 -- be used to validate the actual.
604
605 function Instantiate_Object
606 (Formal : Node_Id;
607 Actual : Node_Id;
608 Analyzed_Formal : Node_Id) return List_Id;
609
610 function Instantiate_Type
611 (Formal : Node_Id;
612 Actual : Node_Id;
613 Analyzed_Formal : Node_Id;
614 Actual_Decls : List_Id) return List_Id;
615
616 function Instantiate_Formal_Subprogram
617 (Formal : Node_Id;
618 Actual : Node_Id;
619 Analyzed_Formal : Node_Id) return Node_Id;
620
621 function Instantiate_Formal_Package
622 (Formal : Node_Id;
623 Actual : Node_Id;
624 Analyzed_Formal : Node_Id) return List_Id;
625 -- If the formal package is declared with a box, special visibility rules
626 -- apply to its formals: they are in the visible part of the package. This
627 -- is true in the declarative region of the formal package, that is to say
628 -- in the enclosing generic or instantiation. For an instantiation, the
629 -- parameters of the formal package are made visible in an explicit step.
630 -- Furthermore, if the actual has a visible USE clause, these formals must
631 -- be made potentially use-visible as well. On exit from the enclosing
632 -- instantiation, the reverse must be done.
633
634 -- For a formal package declared without a box, there are conformance rules
635 -- that apply to the actuals in the generic declaration and the actuals of
636 -- the actual package in the enclosing instantiation. The simplest way to
637 -- apply these rules is to repeat the instantiation of the formal package
638 -- in the context of the enclosing instance, and compare the generic
639 -- associations of this instantiation with those of the actual package.
640 -- This internal instantiation only needs to contain the renamings of the
641 -- formals: the visible and private declarations themselves need not be
642 -- created.
643
644 -- In Ada 2005, the formal package may be only partially parameterized.
645 -- In that case the visibility step must make visible those actuals whose
646 -- corresponding formals were given with a box. A final complication
647 -- involves inherited operations from formal derived types, which must
648 -- be visible if the type is.
649
650 function Is_In_Main_Unit (N : Node_Id) return Boolean;
651 -- Test if given node is in the main unit
652
653 procedure Load_Parent_Of_Generic
654 (N : Node_Id;
655 Spec : Node_Id;
656 Body_Optional : Boolean := False);
657 -- If the generic appears in a separate non-generic library unit, load the
658 -- corresponding body to retrieve the body of the generic. N is the node
659 -- for the generic instantiation, Spec is the generic package declaration.
660 --
661 -- Body_Optional is a flag that indicates that the body is being loaded to
662 -- ensure that temporaries are generated consistently when there are other
663 -- instances in the current declarative part that precede the one being
664 -- loaded. In that case a missing body is acceptable.
665
666 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
667 -- Add the context clause of the unit containing a generic unit to a
668 -- compilation unit that is, or contains, an instantiation.
669
670 function Get_Associated_Node (N : Node_Id) return Node_Id;
671 -- In order to propagate semantic information back from the analyzed copy
672 -- to the original generic, we maintain links between selected nodes in the
673 -- generic and their corresponding copies. At the end of generic analysis,
674 -- the routine Save_Global_References traverses the generic tree, examines
675 -- the semantic information, and preserves the links to those nodes that
676 -- contain global information. At instantiation, the information from the
677 -- associated node is placed on the new copy, so that name resolution is
678 -- not repeated.
679 --
680 -- Three kinds of source nodes have associated nodes:
681 --
682 -- a) those that can reference (denote) entities, that is identifiers,
683 -- character literals, expanded_names, operator symbols, operators,
684 -- and attribute reference nodes. These nodes have an Entity field
685 -- and are the set of nodes that are in N_Has_Entity.
686 --
687 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
688 --
689 -- c) selected components (N_Selected_Component)
690 --
691 -- For the first class, the associated node preserves the entity if it is
692 -- global. If the generic contains nested instantiations, the associated
693 -- node itself has been recopied, and a chain of them must be followed.
694 --
695 -- For aggregates, the associated node allows retrieval of the type, which
696 -- may otherwise not appear in the generic. The view of this type may be
697 -- different between generic and instantiation, and the full view can be
698 -- installed before the instantiation is analyzed. For aggregates of type
699 -- extensions, the same view exchange may have to be performed for some of
700 -- the ancestor types, if their view is private at the point of
701 -- instantiation.
702 --
703 -- Nodes that are selected components in the parse tree may be rewritten
704 -- as expanded names after resolution, and must be treated as potential
705 -- entity holders, which is why they also have an Associated_Node.
706 --
707 -- Nodes that do not come from source, such as freeze nodes, do not appear
708 -- in the generic tree, and need not have an associated node.
709 --
710 -- The associated node is stored in the Associated_Node field. Note that
711 -- this field overlaps Entity, which is fine, because the whole point is
712 -- that we don't need or want the normal Entity field in this situation.
713
714 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
715 -- Within the generic part, entities in the formal package are
716 -- visible. To validate subsequent type declarations, indicate
717 -- the correspondence between the entities in the analyzed formal,
718 -- and the entities in the actual package. There are three packages
719 -- involved in the instantiation of a formal package: the parent
720 -- generic P1 which appears in the generic declaration, the fake
721 -- instantiation P2 which appears in the analyzed generic, and whose
722 -- visible entities may be used in subsequent formals, and the actual
723 -- P3 in the instance. To validate subsequent formals, me indicate
724 -- that the entities in P2 are mapped into those of P3. The mapping of
725 -- entities has to be done recursively for nested packages.
726
727 procedure Move_Freeze_Nodes
728 (Out_Of : Entity_Id;
729 After : Node_Id;
730 L : List_Id);
731 -- Freeze nodes can be generated in the analysis of a generic unit, but
732 -- will not be seen by the back-end. It is necessary to move those nodes
733 -- to the enclosing scope if they freeze an outer entity. We place them
734 -- at the end of the enclosing generic package, which is semantically
735 -- neutral.
736
737 procedure Preanalyze_Actuals (N : Node_Id);
738 -- Analyze actuals to perform name resolution. Full resolution is done
739 -- later, when the expected types are known, but names have to be captured
740 -- before installing parents of generics, that are not visible for the
741 -- actuals themselves.
742
743 function True_Parent (N : Node_Id) return Node_Id;
744 -- For a subunit, return parent of corresponding stub, else return
745 -- parent of node.
746
747 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
748 -- Verify that an attribute that appears as the default for a formal
749 -- subprogram is a function or procedure with the correct profile.
750
751 -------------------------------------------
752 -- Data Structures for Generic Renamings --
753 -------------------------------------------
754
755 -- The map Generic_Renamings associates generic entities with their
756 -- corresponding actuals. Currently used to validate type instances. It
757 -- will eventually be used for all generic parameters to eliminate the
758 -- need for overload resolution in the instance.
759
760 type Assoc_Ptr is new Int;
761
762 Assoc_Null : constant Assoc_Ptr := -1;
763
764 type Assoc is record
765 Gen_Id : Entity_Id;
766 Act_Id : Entity_Id;
767 Next_In_HTable : Assoc_Ptr;
768 end record;
769
770 package Generic_Renamings is new Table.Table
771 (Table_Component_Type => Assoc,
772 Table_Index_Type => Assoc_Ptr,
773 Table_Low_Bound => 0,
774 Table_Initial => 10,
775 Table_Increment => 100,
776 Table_Name => "Generic_Renamings");
777
778 -- Variable to hold enclosing instantiation. When the environment is
779 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
780
781 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
782
783 -- Hash table for associations
784
785 HTable_Size : constant := 37;
786 type HTable_Range is range 0 .. HTable_Size - 1;
787
788 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
789 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
790 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
791 function Hash (F : Entity_Id) return HTable_Range;
792
793 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
794 Header_Num => HTable_Range,
795 Element => Assoc,
796 Elmt_Ptr => Assoc_Ptr,
797 Null_Ptr => Assoc_Null,
798 Set_Next => Set_Next_Assoc,
799 Next => Next_Assoc,
800 Key => Entity_Id,
801 Get_Key => Get_Gen_Id,
802 Hash => Hash,
803 Equal => "=");
804
805 Exchanged_Views : Elist_Id;
806 -- This list holds the private views that have been exchanged during
807 -- instantiation to restore the visibility of the generic declaration.
808 -- (see comments above). After instantiation, the current visibility is
809 -- reestablished by means of a traversal of this list.
810
811 Hidden_Entities : Elist_Id;
812 -- This list holds the entities of the current scope that are removed
813 -- from immediate visibility when instantiating a child unit. Their
814 -- visibility is restored in Remove_Parent.
815
816 -- Because instantiations can be recursive, the following must be saved
817 -- on entry and restored on exit from an instantiation (spec or body).
818 -- This is done by the two procedures Save_Env and Restore_Env. For
819 -- package and subprogram instantiations (but not for the body instances)
820 -- the action of Save_Env is done in two steps: Init_Env is called before
821 -- Check_Generic_Child_Unit, because setting the parent instances requires
822 -- that the visibility data structures be properly initialized. Once the
823 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
824
825 Parent_Unit_Visible : Boolean := False;
826 -- Parent_Unit_Visible is used when the generic is a child unit, and
827 -- indicates whether the ultimate parent of the generic is visible in the
828 -- instantiation environment. It is used to reset the visibility of the
829 -- parent at the end of the instantiation (see Remove_Parent).
830
831 Instance_Parent_Unit : Entity_Id := Empty;
832 -- This records the ultimate parent unit of an instance of a generic
833 -- child unit and is used in conjunction with Parent_Unit_Visible to
834 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
835
836 type Instance_Env is record
837 Instantiated_Parent : Assoc;
838 Exchanged_Views : Elist_Id;
839 Hidden_Entities : Elist_Id;
840 Current_Sem_Unit : Unit_Number_Type;
841 Parent_Unit_Visible : Boolean := False;
842 Instance_Parent_Unit : Entity_Id := Empty;
843 Switches : Config_Switches_Type;
844 end record;
845
846 package Instance_Envs is new Table.Table (
847 Table_Component_Type => Instance_Env,
848 Table_Index_Type => Int,
849 Table_Low_Bound => 0,
850 Table_Initial => 32,
851 Table_Increment => 100,
852 Table_Name => "Instance_Envs");
853
854 procedure Restore_Private_Views
855 (Pack_Id : Entity_Id;
856 Is_Package : Boolean := True);
857 -- Restore the private views of external types, and unmark the generic
858 -- renamings of actuals, so that they become compatible subtypes again.
859 -- For subprograms, Pack_Id is the package constructed to hold the
860 -- renamings.
861
862 procedure Switch_View (T : Entity_Id);
863 -- Switch the partial and full views of a type and its private
864 -- dependents (i.e. its subtypes and derived types).
865
866 ------------------------------------
867 -- Structures for Error Reporting --
868 ------------------------------------
869
870 Instantiation_Node : Node_Id;
871 -- Used by subprograms that validate instantiation of formal parameters
872 -- where there might be no actual on which to place the error message.
873 -- Also used to locate the instantiation node for generic subunits.
874
875 Instantiation_Error : exception;
876 -- When there is a semantic error in the generic parameter matching,
877 -- there is no point in continuing the instantiation, because the
878 -- number of cascaded errors is unpredictable. This exception aborts
879 -- the instantiation process altogether.
880
881 S_Adjustment : Sloc_Adjustment;
882 -- Offset created for each node in an instantiation, in order to keep
883 -- track of the source position of the instantiation in each of its nodes.
884 -- A subsequent semantic error or warning on a construct of the instance
885 -- points to both places: the original generic node, and the point of
886 -- instantiation. See Sinput and Sinput.L for additional details.
887
888 ------------------------------------------------------------
889 -- Data structure for keeping track when inside a Generic --
890 ------------------------------------------------------------
891
892 -- The following table is used to save values of the Inside_A_Generic
893 -- flag (see spec of Sem) when they are saved by Start_Generic.
894
895 package Generic_Flags is new Table.Table (
896 Table_Component_Type => Boolean,
897 Table_Index_Type => Int,
898 Table_Low_Bound => 0,
899 Table_Initial => 32,
900 Table_Increment => 200,
901 Table_Name => "Generic_Flags");
902
903 ---------------------------
904 -- Abandon_Instantiation --
905 ---------------------------
906
907 procedure Abandon_Instantiation (N : Node_Id) is
908 begin
909 Error_Msg_N ("\instantiation abandoned!", N);
910 raise Instantiation_Error;
911 end Abandon_Instantiation;
912
913 --------------------------
914 -- Analyze_Associations --
915 --------------------------
916
917 function Analyze_Associations
918 (I_Node : Node_Id;
919 Formals : List_Id;
920 F_Copy : List_Id) return List_Id
921 is
922 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
923 Assoc : constant List_Id := New_List;
924 Default_Actuals : constant Elist_Id := New_Elmt_List;
925 Gen_Unit : constant Entity_Id :=
926 Defining_Entity (Parent (F_Copy));
927
928 Actuals : List_Id;
929 Actual : Node_Id;
930 Analyzed_Formal : Node_Id;
931 First_Named : Node_Id := Empty;
932 Formal : Node_Id;
933 Match : Node_Id;
934 Named : Node_Id;
935 Saved_Formal : Node_Id;
936
937 Default_Formals : constant List_Id := New_List;
938 -- If an Others_Choice is present, some of the formals may be defaulted.
939 -- To simplify the treatment of visibility in an instance, we introduce
940 -- individual defaults for each such formal. These defaults are
941 -- appended to the list of associations and replace the Others_Choice.
942
943 Found_Assoc : Node_Id;
944 -- Association for the current formal being match. Empty if there are
945 -- no remaining actuals, or if there is no named association with the
946 -- name of the formal.
947
948 Is_Named_Assoc : Boolean;
949 Num_Matched : Int := 0;
950 Num_Actuals : Int := 0;
951
952 Others_Present : Boolean := False;
953 Others_Choice : Node_Id := Empty;
954 -- In Ada 2005, indicates partial parameterization of a formal
955 -- package. As usual an other association must be last in the list.
956
957 function Build_Wrapper
958 (Formal : Entity_Id;
959 Actual : Entity_Id := Empty) return Node_Id;
960 -- In GNATProve mode, create a wrapper function for actuals that are
961 -- operators, in order to propagate their contract to the renaming
962 -- declarations generated for them. If the actual is absent, this is
963 -- a formal with a default, and the name of the operator is that of the
964 -- formal.
965
966 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
967 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
968 -- cannot have a named association for it. AI05-0025 extends this rule
969 -- to formals of formal packages by AI05-0025, and it also applies to
970 -- box-initialized formals.
971
972 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
973 -- Determine whether the parameter types and the return type of Subp
974 -- are fully defined at the point of instantiation.
975
976 function Matching_Actual
977 (F : Entity_Id;
978 A_F : Entity_Id) return Node_Id;
979 -- Find actual that corresponds to a given a formal parameter. If the
980 -- actuals are positional, return the next one, if any. If the actuals
981 -- are named, scan the parameter associations to find the right one.
982 -- A_F is the corresponding entity in the analyzed generic,which is
983 -- placed on the selector name for ASIS use.
984 --
985 -- In Ada 2005, a named association may be given with a box, in which
986 -- case Matching_Actual sets Found_Assoc to the generic association,
987 -- but return Empty for the actual itself. In this case the code below
988 -- creates a corresponding declaration for the formal.
989
990 function Partial_Parameterization return Boolean;
991 -- Ada 2005: if no match is found for a given formal, check if the
992 -- association for it includes a box, or whether the associations
993 -- include an Others clause.
994
995 procedure Process_Default (F : Entity_Id);
996 -- Add a copy of the declaration of generic formal F to the list of
997 -- associations, and add an explicit box association for F if there
998 -- is none yet, and the default comes from an Others_Choice.
999
1000 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1001 -- Determine whether Subp renames one of the subprograms defined in the
1002 -- generated package Standard.
1003
1004 procedure Set_Analyzed_Formal;
1005 -- Find the node in the generic copy that corresponds to a given formal.
1006 -- The semantic information on this node is used to perform legality
1007 -- checks on the actuals. Because semantic analysis can introduce some
1008 -- anonymous entities or modify the declaration node itself, the
1009 -- correspondence between the two lists is not one-one. In addition to
1010 -- anonymous types, the presence a formal equality will introduce an
1011 -- implicit declaration for the corresponding inequality.
1012
1013 -------------------
1014 -- Build_Wrapper --
1015 -------------------
1016
1017 function Build_Wrapper
1018 (Formal : Entity_Id;
1019 Actual : Entity_Id := Empty) return Node_Id
1020 is
1021 Loc : constant Source_Ptr := Sloc (I_Node);
1022 Typ : constant Entity_Id := Etype (Formal);
1023 Is_Binary : constant Boolean :=
1024 Present (Next_Formal (First_Formal (Formal)));
1025
1026 Decl : Node_Id;
1027 Expr : Node_Id;
1028 F1, F2 : Entity_Id;
1029 Func : Entity_Id;
1030 Op_Name : Name_Id;
1031 Spec : Node_Id;
1032
1033 L, R : Node_Id;
1034
1035 begin
1036 if No (Actual) then
1037 Op_Name := Chars (Formal);
1038 else
1039 Op_Name := Chars (Actual);
1040 end if;
1041
1042 -- Create entities for wrapper function and its formals
1043
1044 F1 := Make_Temporary (Loc, 'A');
1045 F2 := Make_Temporary (Loc, 'B');
1046 L := New_Occurrence_Of (F1, Loc);
1047 R := New_Occurrence_Of (F2, Loc);
1048
1049 Func := Make_Defining_Identifier (Loc, Chars (Formal));
1050 Set_Ekind (Func, E_Function);
1051 Set_Is_Generic_Actual_Subprogram (Func);
1052
1053 Spec :=
1054 Make_Function_Specification (Loc,
1055 Defining_Unit_Name => Func,
1056 Parameter_Specifications => New_List (
1057 Make_Parameter_Specification (Loc,
1058 Defining_Identifier => F1,
1059 Parameter_Type =>
1060 Make_Identifier (Loc,
1061 Chars => Chars (Etype (First_Formal (Formal)))))),
1062 Result_Definition => Make_Identifier (Loc, Chars (Typ)));
1063
1064 if Is_Binary then
1065 Append_To (Parameter_Specifications (Spec),
1066 Make_Parameter_Specification (Loc,
1067 Defining_Identifier => F2,
1068 Parameter_Type =>
1069 Make_Identifier (Loc,
1070 Chars (Etype (Next_Formal (First_Formal (Formal)))))));
1071 end if;
1072
1073 -- Build expression as a function call, or as an operator node
1074 -- that corresponds to the name of the actual, starting with binary
1075 -- operators.
1076
1077 if Present (Actual) and then Op_Name not in Any_Operator_Name then
1078 Expr :=
1079 Make_Function_Call (Loc,
1080 Name =>
1081 New_Occurrence_Of (Entity (Actual), Loc),
1082 Parameter_Associations => New_List (L));
1083
1084 if Is_Binary then
1085 Append_To (Parameter_Associations (Expr), R);
1086 end if;
1087
1088 -- Binary operators
1089
1090 elsif Is_Binary then
1091 if Op_Name = Name_Op_And then
1092 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
1093 elsif Op_Name = Name_Op_Or then
1094 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
1095 elsif Op_Name = Name_Op_Xor then
1096 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
1097 elsif Op_Name = Name_Op_Eq then
1098 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
1099 elsif Op_Name = Name_Op_Ne then
1100 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
1101 elsif Op_Name = Name_Op_Le then
1102 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
1103 elsif Op_Name = Name_Op_Gt then
1104 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
1105 elsif Op_Name = Name_Op_Ge then
1106 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
1107 elsif Op_Name = Name_Op_Lt then
1108 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
1109 elsif Op_Name = Name_Op_Add then
1110 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
1111 elsif Op_Name = Name_Op_Subtract then
1112 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
1113 elsif Op_Name = Name_Op_Concat then
1114 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
1115 elsif Op_Name = Name_Op_Multiply then
1116 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
1117 elsif Op_Name = Name_Op_Divide then
1118 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
1119 elsif Op_Name = Name_Op_Mod then
1120 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
1121 elsif Op_Name = Name_Op_Rem then
1122 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
1123 elsif Op_Name = Name_Op_Expon then
1124 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
1125 end if;
1126
1127 -- Unary operators
1128
1129 else
1130 if Op_Name = Name_Op_Add then
1131 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
1132 elsif Op_Name = Name_Op_Subtract then
1133 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
1134 elsif Op_Name = Name_Op_Abs then
1135 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
1136 elsif Op_Name = Name_Op_Not then
1137 Expr := Make_Op_Not (Loc, Right_Opnd => L);
1138 end if;
1139 end if;
1140
1141 -- Propagate visible entity to operator node, either from a
1142 -- given actual or from a default.
1143
1144 if Is_Entity_Name (Actual) and then Nkind (Expr) in N_Op then
1145 Set_Entity (Expr, Entity (Actual));
1146 end if;
1147
1148 Decl :=
1149 Make_Expression_Function (Loc,
1150 Specification => Spec,
1151 Expression => Expr);
1152
1153 return Decl;
1154 end Build_Wrapper;
1155
1156 ----------------------------------------
1157 -- Check_Overloaded_Formal_Subprogram --
1158 ----------------------------------------
1159
1160 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1161 Temp_Formal : Entity_Id;
1162
1163 begin
1164 Temp_Formal := First (Formals);
1165 while Present (Temp_Formal) loop
1166 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1167 and then Temp_Formal /= Formal
1168 and then
1169 Chars (Defining_Unit_Name (Specification (Formal))) =
1170 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1171 then
1172 if Present (Found_Assoc) then
1173 Error_Msg_N
1174 ("named association not allowed for overloaded formal",
1175 Found_Assoc);
1176
1177 else
1178 Error_Msg_N
1179 ("named association not allowed for overloaded formal",
1180 Others_Choice);
1181 end if;
1182
1183 Abandon_Instantiation (Instantiation_Node);
1184 end if;
1185
1186 Next (Temp_Formal);
1187 end loop;
1188 end Check_Overloaded_Formal_Subprogram;
1189
1190 -------------------------------
1191 -- Has_Fully_Defined_Profile --
1192 -------------------------------
1193
1194 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1195 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1196 -- Determine whethet type Typ is fully defined
1197
1198 ---------------------------
1199 -- Is_Fully_Defined_Type --
1200 ---------------------------
1201
1202 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1203 begin
1204 -- A private type without a full view is not fully defined
1205
1206 if Is_Private_Type (Typ)
1207 and then No (Full_View (Typ))
1208 then
1209 return False;
1210
1211 -- An incomplete type is never fully defined
1212
1213 elsif Is_Incomplete_Type (Typ) then
1214 return False;
1215
1216 -- All other types are fully defined
1217
1218 else
1219 return True;
1220 end if;
1221 end Is_Fully_Defined_Type;
1222
1223 -- Local declarations
1224
1225 Param : Entity_Id;
1226
1227 -- Start of processing for Has_Fully_Defined_Profile
1228
1229 begin
1230 -- Check the parameters
1231
1232 Param := First_Formal (Subp);
1233 while Present (Param) loop
1234 if not Is_Fully_Defined_Type (Etype (Param)) then
1235 return False;
1236 end if;
1237
1238 Next_Formal (Param);
1239 end loop;
1240
1241 -- Check the return type
1242
1243 return Is_Fully_Defined_Type (Etype (Subp));
1244 end Has_Fully_Defined_Profile;
1245
1246 ---------------------
1247 -- Matching_Actual --
1248 ---------------------
1249
1250 function Matching_Actual
1251 (F : Entity_Id;
1252 A_F : Entity_Id) return Node_Id
1253 is
1254 Prev : Node_Id;
1255 Act : Node_Id;
1256
1257 begin
1258 Is_Named_Assoc := False;
1259
1260 -- End of list of purely positional parameters
1261
1262 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1263 Found_Assoc := Empty;
1264 Act := Empty;
1265
1266 -- Case of positional parameter corresponding to current formal
1267
1268 elsif No (Selector_Name (Actual)) then
1269 Found_Assoc := Actual;
1270 Act := Explicit_Generic_Actual_Parameter (Actual);
1271 Num_Matched := Num_Matched + 1;
1272 Next (Actual);
1273
1274 -- Otherwise scan list of named actuals to find the one with the
1275 -- desired name. All remaining actuals have explicit names.
1276
1277 else
1278 Is_Named_Assoc := True;
1279 Found_Assoc := Empty;
1280 Act := Empty;
1281 Prev := Empty;
1282
1283 while Present (Actual) loop
1284 if Chars (Selector_Name (Actual)) = Chars (F) then
1285 Set_Entity (Selector_Name (Actual), A_F);
1286 Set_Etype (Selector_Name (Actual), Etype (A_F));
1287 Generate_Reference (A_F, Selector_Name (Actual));
1288 Found_Assoc := Actual;
1289 Act := Explicit_Generic_Actual_Parameter (Actual);
1290 Num_Matched := Num_Matched + 1;
1291 exit;
1292 end if;
1293
1294 Prev := Actual;
1295 Next (Actual);
1296 end loop;
1297
1298 -- Reset for subsequent searches. In most cases the named
1299 -- associations are in order. If they are not, we reorder them
1300 -- to avoid scanning twice the same actual. This is not just a
1301 -- question of efficiency: there may be multiple defaults with
1302 -- boxes that have the same name. In a nested instantiation we
1303 -- insert actuals for those defaults, and cannot rely on their
1304 -- names to disambiguate them.
1305
1306 if Actual = First_Named then
1307 Next (First_Named);
1308
1309 elsif Present (Actual) then
1310 Insert_Before (First_Named, Remove_Next (Prev));
1311 end if;
1312
1313 Actual := First_Named;
1314 end if;
1315
1316 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1317 Set_Used_As_Generic_Actual (Entity (Act));
1318 end if;
1319
1320 return Act;
1321 end Matching_Actual;
1322
1323 ------------------------------
1324 -- Partial_Parameterization --
1325 ------------------------------
1326
1327 function Partial_Parameterization return Boolean is
1328 begin
1329 return Others_Present
1330 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1331 end Partial_Parameterization;
1332
1333 ---------------------
1334 -- Process_Default --
1335 ---------------------
1336
1337 procedure Process_Default (F : Entity_Id) is
1338 Loc : constant Source_Ptr := Sloc (I_Node);
1339 F_Id : constant Entity_Id := Defining_Entity (F);
1340 Decl : Node_Id;
1341 Default : Node_Id;
1342 Id : Entity_Id;
1343
1344 begin
1345 -- Append copy of formal declaration to associations, and create new
1346 -- defining identifier for it.
1347
1348 Decl := New_Copy_Tree (F);
1349 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1350
1351 if Nkind (F) in N_Formal_Subprogram_Declaration then
1352 Set_Defining_Unit_Name (Specification (Decl), Id);
1353
1354 else
1355 Set_Defining_Identifier (Decl, Id);
1356 end if;
1357
1358 Append (Decl, Assoc);
1359
1360 if No (Found_Assoc) then
1361 Default :=
1362 Make_Generic_Association (Loc,
1363 Selector_Name => New_Occurrence_Of (Id, Loc),
1364 Explicit_Generic_Actual_Parameter => Empty);
1365 Set_Box_Present (Default);
1366 Append (Default, Default_Formals);
1367 end if;
1368 end Process_Default;
1369
1370 ---------------------------------
1371 -- Renames_Standard_Subprogram --
1372 ---------------------------------
1373
1374 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1375 Id : Entity_Id;
1376
1377 begin
1378 Id := Alias (Subp);
1379 while Present (Id) loop
1380 if Scope (Id) = Standard_Standard then
1381 return True;
1382 end if;
1383
1384 Id := Alias (Id);
1385 end loop;
1386
1387 return False;
1388 end Renames_Standard_Subprogram;
1389
1390 -------------------------
1391 -- Set_Analyzed_Formal --
1392 -------------------------
1393
1394 procedure Set_Analyzed_Formal is
1395 Kind : Node_Kind;
1396
1397 begin
1398 while Present (Analyzed_Formal) loop
1399 Kind := Nkind (Analyzed_Formal);
1400
1401 case Nkind (Formal) is
1402
1403 when N_Formal_Subprogram_Declaration =>
1404 exit when Kind in N_Formal_Subprogram_Declaration
1405 and then
1406 Chars
1407 (Defining_Unit_Name (Specification (Formal))) =
1408 Chars
1409 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1410
1411 when N_Formal_Package_Declaration =>
1412 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1413 N_Generic_Package_Declaration,
1414 N_Package_Declaration);
1415
1416 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1417
1418 when others =>
1419
1420 -- Skip freeze nodes, and nodes inserted to replace
1421 -- unrecognized pragmas.
1422
1423 exit when
1424 Kind not in N_Formal_Subprogram_Declaration
1425 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1426 N_Freeze_Entity,
1427 N_Null_Statement,
1428 N_Itype_Reference)
1429 and then Chars (Defining_Identifier (Formal)) =
1430 Chars (Defining_Identifier (Analyzed_Formal));
1431 end case;
1432
1433 Next (Analyzed_Formal);
1434 end loop;
1435 end Set_Analyzed_Formal;
1436
1437 -- Start of processing for Analyze_Associations
1438
1439 begin
1440 Actuals := Generic_Associations (I_Node);
1441
1442 if Present (Actuals) then
1443
1444 -- Check for an Others choice, indicating a partial parameterization
1445 -- for a formal package.
1446
1447 Actual := First (Actuals);
1448 while Present (Actual) loop
1449 if Nkind (Actual) = N_Others_Choice then
1450 Others_Present := True;
1451 Others_Choice := Actual;
1452
1453 if Present (Next (Actual)) then
1454 Error_Msg_N ("others must be last association", Actual);
1455 end if;
1456
1457 -- This subprogram is used both for formal packages and for
1458 -- instantiations. For the latter, associations must all be
1459 -- explicit.
1460
1461 if Nkind (I_Node) /= N_Formal_Package_Declaration
1462 and then Comes_From_Source (I_Node)
1463 then
1464 Error_Msg_N
1465 ("others association not allowed in an instance",
1466 Actual);
1467 end if;
1468
1469 -- In any case, nothing to do after the others association
1470
1471 exit;
1472
1473 elsif Box_Present (Actual)
1474 and then Comes_From_Source (I_Node)
1475 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1476 then
1477 Error_Msg_N
1478 ("box association not allowed in an instance", Actual);
1479 end if;
1480
1481 Next (Actual);
1482 end loop;
1483
1484 -- If named associations are present, save first named association
1485 -- (it may of course be Empty) to facilitate subsequent name search.
1486
1487 First_Named := First (Actuals);
1488 while Present (First_Named)
1489 and then Nkind (First_Named) /= N_Others_Choice
1490 and then No (Selector_Name (First_Named))
1491 loop
1492 Num_Actuals := Num_Actuals + 1;
1493 Next (First_Named);
1494 end loop;
1495 end if;
1496
1497 Named := First_Named;
1498 while Present (Named) loop
1499 if Nkind (Named) /= N_Others_Choice
1500 and then No (Selector_Name (Named))
1501 then
1502 Error_Msg_N ("invalid positional actual after named one", Named);
1503 Abandon_Instantiation (Named);
1504 end if;
1505
1506 -- A named association may lack an actual parameter, if it was
1507 -- introduced for a default subprogram that turns out to be local
1508 -- to the outer instantiation.
1509
1510 if Nkind (Named) /= N_Others_Choice
1511 and then Present (Explicit_Generic_Actual_Parameter (Named))
1512 then
1513 Num_Actuals := Num_Actuals + 1;
1514 end if;
1515
1516 Next (Named);
1517 end loop;
1518
1519 if Present (Formals) then
1520 Formal := First_Non_Pragma (Formals);
1521 Analyzed_Formal := First_Non_Pragma (F_Copy);
1522
1523 if Present (Actuals) then
1524 Actual := First (Actuals);
1525
1526 -- All formals should have default values
1527
1528 else
1529 Actual := Empty;
1530 end if;
1531
1532 while Present (Formal) loop
1533 Set_Analyzed_Formal;
1534 Saved_Formal := Next_Non_Pragma (Formal);
1535
1536 case Nkind (Formal) is
1537 when N_Formal_Object_Declaration =>
1538 Match :=
1539 Matching_Actual (
1540 Defining_Identifier (Formal),
1541 Defining_Identifier (Analyzed_Formal));
1542
1543 if No (Match) and then Partial_Parameterization then
1544 Process_Default (Formal);
1545 else
1546 Append_List
1547 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1548 Assoc);
1549 end if;
1550
1551 when N_Formal_Type_Declaration =>
1552 Match :=
1553 Matching_Actual (
1554 Defining_Identifier (Formal),
1555 Defining_Identifier (Analyzed_Formal));
1556
1557 if No (Match) then
1558 if Partial_Parameterization then
1559 Process_Default (Formal);
1560
1561 else
1562 Error_Msg_Sloc := Sloc (Gen_Unit);
1563 Error_Msg_NE
1564 ("missing actual&",
1565 Instantiation_Node,
1566 Defining_Identifier (Formal));
1567 Error_Msg_NE ("\in instantiation of & declared#",
1568 Instantiation_Node, Gen_Unit);
1569 Abandon_Instantiation (Instantiation_Node);
1570 end if;
1571
1572 else
1573 Analyze (Match);
1574 Append_List
1575 (Instantiate_Type
1576 (Formal, Match, Analyzed_Formal, Assoc),
1577 Assoc);
1578
1579 -- An instantiation is a freeze point for the actuals,
1580 -- unless this is a rewritten formal package, or the
1581 -- formal is an Ada 2012 formal incomplete type.
1582
1583 if Nkind (I_Node) = N_Formal_Package_Declaration
1584 or else
1585 (Ada_Version >= Ada_2012
1586 and then
1587 Ekind (Defining_Identifier (Analyzed_Formal)) =
1588 E_Incomplete_Type)
1589 then
1590 null;
1591
1592 else
1593 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1594 end if;
1595 end if;
1596
1597 -- A remote access-to-class-wide type is not a legal actual
1598 -- for a generic formal of an access type (E.2.2(17/2)).
1599 -- In GNAT an exception to this rule is introduced when
1600 -- the formal is marked as remote using implementation
1601 -- defined aspect/pragma Remote_Access_Type. In that case
1602 -- the actual must be remote as well.
1603
1604 -- If the current instantiation is the construction of a
1605 -- local copy for a formal package the actuals may be
1606 -- defaulted, and there is no matching actual to check.
1607
1608 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1609 and then
1610 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1611 N_Access_To_Object_Definition
1612 and then Present (Match)
1613 then
1614 declare
1615 Formal_Ent : constant Entity_Id :=
1616 Defining_Identifier (Analyzed_Formal);
1617 begin
1618 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1619 = Is_Remote_Types (Formal_Ent)
1620 then
1621 -- Remoteness of formal and actual match
1622
1623 null;
1624
1625 elsif Is_Remote_Types (Formal_Ent) then
1626
1627 -- Remote formal, non-remote actual
1628
1629 Error_Msg_NE
1630 ("actual for& must be remote", Match, Formal_Ent);
1631
1632 else
1633 -- Non-remote formal, remote actual
1634
1635 Error_Msg_NE
1636 ("actual for& may not be remote",
1637 Match, Formal_Ent);
1638 end if;
1639 end;
1640 end if;
1641
1642 when N_Formal_Subprogram_Declaration =>
1643 Match :=
1644 Matching_Actual
1645 (Defining_Unit_Name (Specification (Formal)),
1646 Defining_Unit_Name (Specification (Analyzed_Formal)));
1647
1648 -- If the formal subprogram has the same name as another
1649 -- formal subprogram of the generic, then a named
1650 -- association is illegal (12.3(9)). Exclude named
1651 -- associations that are generated for a nested instance.
1652
1653 if Present (Match)
1654 and then Is_Named_Assoc
1655 and then Comes_From_Source (Found_Assoc)
1656 then
1657 Check_Overloaded_Formal_Subprogram (Formal);
1658 end if;
1659
1660 -- If there is no corresponding actual, this may be case
1661 -- of partial parameterization, or else the formal has a
1662 -- default or a box.
1663
1664 if No (Match) and then Partial_Parameterization then
1665 Process_Default (Formal);
1666
1667 if Nkind (I_Node) = N_Formal_Package_Declaration then
1668 Check_Overloaded_Formal_Subprogram (Formal);
1669 end if;
1670
1671 else
1672 if GNATprove_Mode
1673 and then
1674 Present
1675 (Containing_Package_With_Ext_Axioms
1676 (Defining_Entity (Analyzed_Formal)))
1677 and then Ekind (Defining_Entity (Analyzed_Formal)) =
1678 E_Function
1679 then
1680 -- If actual is an entity (function or operator),
1681 -- build wrapper for it.
1682
1683 if Present (Match) then
1684 if Nkind (Match) = N_Operator_Symbol then
1685
1686 -- If the name is a default, find its visible
1687 -- entity at the point of instantiation.
1688
1689 if Is_Entity_Name (Match)
1690 and then No (Entity (Match))
1691 then
1692 Find_Direct_Name (Match);
1693 end if;
1694
1695 Append_To
1696 (Assoc,
1697 Build_Wrapper
1698 (Defining_Entity (Analyzed_Formal), Match));
1699
1700 else
1701 Append_To (Assoc,
1702 Instantiate_Formal_Subprogram
1703 (Formal, Match, Analyzed_Formal));
1704 end if;
1705
1706 -- Ditto if formal is an operator with a default.
1707
1708 elsif Box_Present (Formal)
1709 and then Nkind (Defining_Entity (Analyzed_Formal)) =
1710 N_Defining_Operator_Symbol
1711 then
1712 Append_To (Assoc,
1713 Build_Wrapper
1714 (Defining_Entity (Analyzed_Formal)));
1715
1716 -- Otherwise create renaming declaration.
1717
1718 else
1719 Append_To (Assoc,
1720 Instantiate_Formal_Subprogram
1721 (Formal, Match, Analyzed_Formal));
1722 end if;
1723
1724 else
1725 Append_To (Assoc,
1726 Instantiate_Formal_Subprogram
1727 (Formal, Match, Analyzed_Formal));
1728 end if;
1729
1730 -- An instantiation is a freeze point for the actuals,
1731 -- unless this is a rewritten formal package.
1732
1733 if Nkind (I_Node) /= N_Formal_Package_Declaration
1734 and then Nkind (Match) = N_Identifier
1735 and then Is_Subprogram (Entity (Match))
1736
1737 -- The actual subprogram may rename a routine defined
1738 -- in Standard. Avoid freezing such renamings because
1739 -- subprograms coming from Standard cannot be frozen.
1740
1741 and then
1742 not Renames_Standard_Subprogram (Entity (Match))
1743
1744 -- If the actual subprogram comes from a different
1745 -- unit, it is already frozen, either by a body in
1746 -- that unit or by the end of the declarative part
1747 -- of the unit. This check avoids the freezing of
1748 -- subprograms defined in Standard which are used
1749 -- as generic actuals.
1750
1751 and then In_Same_Code_Unit (Entity (Match), I_Node)
1752 and then Has_Fully_Defined_Profile (Entity (Match))
1753 then
1754 -- Mark the subprogram as having a delayed freeze
1755 -- since this may be an out-of-order action.
1756
1757 Set_Has_Delayed_Freeze (Entity (Match));
1758 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1759 end if;
1760 end if;
1761
1762 -- If this is a nested generic, preserve default for later
1763 -- instantiations.
1764
1765 if No (Match) and then Box_Present (Formal) then
1766 Append_Elmt
1767 (Defining_Unit_Name (Specification (Last (Assoc))),
1768 Default_Actuals);
1769 end if;
1770
1771 when N_Formal_Package_Declaration =>
1772 Match :=
1773 Matching_Actual (
1774 Defining_Identifier (Formal),
1775 Defining_Identifier (Original_Node (Analyzed_Formal)));
1776
1777 if No (Match) then
1778 if Partial_Parameterization then
1779 Process_Default (Formal);
1780
1781 else
1782 Error_Msg_Sloc := Sloc (Gen_Unit);
1783 Error_Msg_NE
1784 ("missing actual&",
1785 Instantiation_Node, Defining_Identifier (Formal));
1786 Error_Msg_NE ("\in instantiation of & declared#",
1787 Instantiation_Node, Gen_Unit);
1788
1789 Abandon_Instantiation (Instantiation_Node);
1790 end if;
1791
1792 else
1793 Analyze (Match);
1794 Append_List
1795 (Instantiate_Formal_Package
1796 (Formal, Match, Analyzed_Formal),
1797 Assoc);
1798 end if;
1799
1800 -- For use type and use package appearing in the generic part,
1801 -- we have already copied them, so we can just move them where
1802 -- they belong (we mustn't recopy them since this would mess up
1803 -- the Sloc values).
1804
1805 when N_Use_Package_Clause |
1806 N_Use_Type_Clause =>
1807 if Nkind (Original_Node (I_Node)) =
1808 N_Formal_Package_Declaration
1809 then
1810 Append (New_Copy_Tree (Formal), Assoc);
1811 else
1812 Remove (Formal);
1813 Append (Formal, Assoc);
1814 end if;
1815
1816 when others =>
1817 raise Program_Error;
1818
1819 end case;
1820
1821 Formal := Saved_Formal;
1822 Next_Non_Pragma (Analyzed_Formal);
1823 end loop;
1824
1825 if Num_Actuals > Num_Matched then
1826 Error_Msg_Sloc := Sloc (Gen_Unit);
1827
1828 if Present (Selector_Name (Actual)) then
1829 Error_Msg_NE
1830 ("unmatched actual&",
1831 Actual, Selector_Name (Actual));
1832 Error_Msg_NE ("\in instantiation of& declared#",
1833 Actual, Gen_Unit);
1834 else
1835 Error_Msg_NE
1836 ("unmatched actual in instantiation of& declared#",
1837 Actual, Gen_Unit);
1838 end if;
1839 end if;
1840
1841 elsif Present (Actuals) then
1842 Error_Msg_N
1843 ("too many actuals in generic instantiation", Instantiation_Node);
1844 end if;
1845
1846 -- An instantiation freezes all generic actuals. The only exceptions
1847 -- to this are incomplete types and subprograms which are not fully
1848 -- defined at the point of instantiation.
1849
1850 declare
1851 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1852 begin
1853 while Present (Elmt) loop
1854 Freeze_Before (I_Node, Node (Elmt));
1855 Next_Elmt (Elmt);
1856 end loop;
1857 end;
1858
1859 -- If there are default subprograms, normalize the tree by adding
1860 -- explicit associations for them. This is required if the instance
1861 -- appears within a generic.
1862
1863 declare
1864 Elmt : Elmt_Id;
1865 Subp : Entity_Id;
1866 New_D : Node_Id;
1867
1868 begin
1869 Elmt := First_Elmt (Default_Actuals);
1870 while Present (Elmt) loop
1871 if No (Actuals) then
1872 Actuals := New_List;
1873 Set_Generic_Associations (I_Node, Actuals);
1874 end if;
1875
1876 Subp := Node (Elmt);
1877 New_D :=
1878 Make_Generic_Association (Sloc (Subp),
1879 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1880 Explicit_Generic_Actual_Parameter =>
1881 New_Occurrence_Of (Subp, Sloc (Subp)));
1882 Mark_Rewrite_Insertion (New_D);
1883 Append_To (Actuals, New_D);
1884 Next_Elmt (Elmt);
1885 end loop;
1886 end;
1887
1888 -- If this is a formal package, normalize the parameter list by adding
1889 -- explicit box associations for the formals that are covered by an
1890 -- Others_Choice.
1891
1892 if not Is_Empty_List (Default_Formals) then
1893 Append_List (Default_Formals, Formals);
1894 end if;
1895
1896 return Assoc;
1897 end Analyze_Associations;
1898
1899 -------------------------------
1900 -- Analyze_Formal_Array_Type --
1901 -------------------------------
1902
1903 procedure Analyze_Formal_Array_Type
1904 (T : in out Entity_Id;
1905 Def : Node_Id)
1906 is
1907 DSS : Node_Id;
1908
1909 begin
1910 -- Treated like a non-generic array declaration, with additional
1911 -- semantic checks.
1912
1913 Enter_Name (T);
1914
1915 if Nkind (Def) = N_Constrained_Array_Definition then
1916 DSS := First (Discrete_Subtype_Definitions (Def));
1917 while Present (DSS) loop
1918 if Nkind_In (DSS, N_Subtype_Indication,
1919 N_Range,
1920 N_Attribute_Reference)
1921 then
1922 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1923 end if;
1924
1925 Next (DSS);
1926 end loop;
1927 end if;
1928
1929 Array_Type_Declaration (T, Def);
1930 Set_Is_Generic_Type (Base_Type (T));
1931
1932 if Ekind (Component_Type (T)) = E_Incomplete_Type
1933 and then No (Full_View (Component_Type (T)))
1934 then
1935 Error_Msg_N ("premature usage of incomplete type", Def);
1936
1937 -- Check that range constraint is not allowed on the component type
1938 -- of a generic formal array type (AARM 12.5.3(3))
1939
1940 elsif Is_Internal (Component_Type (T))
1941 and then Present (Subtype_Indication (Component_Definition (Def)))
1942 and then Nkind (Original_Node
1943 (Subtype_Indication (Component_Definition (Def)))) =
1944 N_Subtype_Indication
1945 then
1946 Error_Msg_N
1947 ("in a formal, a subtype indication can only be "
1948 & "a subtype mark (RM 12.5.3(3))",
1949 Subtype_Indication (Component_Definition (Def)));
1950 end if;
1951
1952 end Analyze_Formal_Array_Type;
1953
1954 ---------------------------------------------
1955 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1956 ---------------------------------------------
1957
1958 -- As for other generic types, we create a valid type representation with
1959 -- legal but arbitrary attributes, whose values are never considered
1960 -- static. For all scalar types we introduce an anonymous base type, with
1961 -- the same attributes. We choose the corresponding integer type to be
1962 -- Standard_Integer.
1963 -- Here and in other similar routines, the Sloc of the generated internal
1964 -- type must be the same as the sloc of the defining identifier of the
1965 -- formal type declaration, to provide proper source navigation.
1966
1967 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1968 (T : Entity_Id;
1969 Def : Node_Id)
1970 is
1971 Loc : constant Source_Ptr := Sloc (Def);
1972
1973 Base : constant Entity_Id :=
1974 New_Internal_Entity
1975 (E_Decimal_Fixed_Point_Type,
1976 Current_Scope,
1977 Sloc (Defining_Identifier (Parent (Def))), 'G');
1978
1979 Int_Base : constant Entity_Id := Standard_Integer;
1980 Delta_Val : constant Ureal := Ureal_1;
1981 Digs_Val : constant Uint := Uint_6;
1982
1983 function Make_Dummy_Bound return Node_Id;
1984 -- Return a properly typed universal real literal to use as a bound
1985
1986 ----------------------
1987 -- Make_Dummy_Bound --
1988 ----------------------
1989
1990 function Make_Dummy_Bound return Node_Id is
1991 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1992 begin
1993 Set_Etype (Bound, Universal_Real);
1994 return Bound;
1995 end Make_Dummy_Bound;
1996
1997 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1998
1999 begin
2000 Enter_Name (T);
2001
2002 Set_Etype (Base, Base);
2003 Set_Size_Info (Base, Int_Base);
2004 Set_RM_Size (Base, RM_Size (Int_Base));
2005 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2006 Set_Digits_Value (Base, Digs_Val);
2007 Set_Delta_Value (Base, Delta_Val);
2008 Set_Small_Value (Base, Delta_Val);
2009 Set_Scalar_Range (Base,
2010 Make_Range (Loc,
2011 Low_Bound => Make_Dummy_Bound,
2012 High_Bound => Make_Dummy_Bound));
2013
2014 Set_Is_Generic_Type (Base);
2015 Set_Parent (Base, Parent (Def));
2016
2017 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2018 Set_Etype (T, Base);
2019 Set_Size_Info (T, Int_Base);
2020 Set_RM_Size (T, RM_Size (Int_Base));
2021 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2022 Set_Digits_Value (T, Digs_Val);
2023 Set_Delta_Value (T, Delta_Val);
2024 Set_Small_Value (T, Delta_Val);
2025 Set_Scalar_Range (T, Scalar_Range (Base));
2026 Set_Is_Constrained (T);
2027
2028 Check_Restriction (No_Fixed_Point, Def);
2029 end Analyze_Formal_Decimal_Fixed_Point_Type;
2030
2031 -------------------------------------------
2032 -- Analyze_Formal_Derived_Interface_Type --
2033 -------------------------------------------
2034
2035 procedure Analyze_Formal_Derived_Interface_Type
2036 (N : Node_Id;
2037 T : Entity_Id;
2038 Def : Node_Id)
2039 is
2040 Loc : constant Source_Ptr := Sloc (Def);
2041
2042 begin
2043 -- Rewrite as a type declaration of a derived type. This ensures that
2044 -- the interface list and primitive operations are properly captured.
2045
2046 Rewrite (N,
2047 Make_Full_Type_Declaration (Loc,
2048 Defining_Identifier => T,
2049 Type_Definition => Def));
2050 Analyze (N);
2051 Set_Is_Generic_Type (T);
2052 end Analyze_Formal_Derived_Interface_Type;
2053
2054 ---------------------------------
2055 -- Analyze_Formal_Derived_Type --
2056 ---------------------------------
2057
2058 procedure Analyze_Formal_Derived_Type
2059 (N : Node_Id;
2060 T : Entity_Id;
2061 Def : Node_Id)
2062 is
2063 Loc : constant Source_Ptr := Sloc (Def);
2064 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2065 New_N : Node_Id;
2066
2067 begin
2068 Set_Is_Generic_Type (T);
2069
2070 if Private_Present (Def) then
2071 New_N :=
2072 Make_Private_Extension_Declaration (Loc,
2073 Defining_Identifier => T,
2074 Discriminant_Specifications => Discriminant_Specifications (N),
2075 Unknown_Discriminants_Present => Unk_Disc,
2076 Subtype_Indication => Subtype_Mark (Def),
2077 Interface_List => Interface_List (Def));
2078
2079 Set_Abstract_Present (New_N, Abstract_Present (Def));
2080 Set_Limited_Present (New_N, Limited_Present (Def));
2081 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2082
2083 else
2084 New_N :=
2085 Make_Full_Type_Declaration (Loc,
2086 Defining_Identifier => T,
2087 Discriminant_Specifications =>
2088 Discriminant_Specifications (Parent (T)),
2089 Type_Definition =>
2090 Make_Derived_Type_Definition (Loc,
2091 Subtype_Indication => Subtype_Mark (Def)));
2092
2093 Set_Abstract_Present
2094 (Type_Definition (New_N), Abstract_Present (Def));
2095 Set_Limited_Present
2096 (Type_Definition (New_N), Limited_Present (Def));
2097 end if;
2098
2099 Rewrite (N, New_N);
2100 Analyze (N);
2101
2102 if Unk_Disc then
2103 if not Is_Composite_Type (T) then
2104 Error_Msg_N
2105 ("unknown discriminants not allowed for elementary types", N);
2106 else
2107 Set_Has_Unknown_Discriminants (T);
2108 Set_Is_Constrained (T, False);
2109 end if;
2110 end if;
2111
2112 -- If the parent type has a known size, so does the formal, which makes
2113 -- legal representation clauses that involve the formal.
2114
2115 Set_Size_Known_At_Compile_Time
2116 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2117 end Analyze_Formal_Derived_Type;
2118
2119 ----------------------------------
2120 -- Analyze_Formal_Discrete_Type --
2121 ----------------------------------
2122
2123 -- The operations defined for a discrete types are those of an enumeration
2124 -- type. The size is set to an arbitrary value, for use in analyzing the
2125 -- generic unit.
2126
2127 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2128 Loc : constant Source_Ptr := Sloc (Def);
2129 Lo : Node_Id;
2130 Hi : Node_Id;
2131
2132 Base : constant Entity_Id :=
2133 New_Internal_Entity
2134 (E_Floating_Point_Type, Current_Scope,
2135 Sloc (Defining_Identifier (Parent (Def))), 'G');
2136
2137 begin
2138 Enter_Name (T);
2139 Set_Ekind (T, E_Enumeration_Subtype);
2140 Set_Etype (T, Base);
2141 Init_Size (T, 8);
2142 Init_Alignment (T);
2143 Set_Is_Generic_Type (T);
2144 Set_Is_Constrained (T);
2145
2146 -- For semantic analysis, the bounds of the type must be set to some
2147 -- non-static value. The simplest is to create attribute nodes for those
2148 -- bounds, that refer to the type itself. These bounds are never
2149 -- analyzed but serve as place-holders.
2150
2151 Lo :=
2152 Make_Attribute_Reference (Loc,
2153 Attribute_Name => Name_First,
2154 Prefix => New_Occurrence_Of (T, Loc));
2155 Set_Etype (Lo, T);
2156
2157 Hi :=
2158 Make_Attribute_Reference (Loc,
2159 Attribute_Name => Name_Last,
2160 Prefix => New_Occurrence_Of (T, Loc));
2161 Set_Etype (Hi, T);
2162
2163 Set_Scalar_Range (T,
2164 Make_Range (Loc,
2165 Low_Bound => Lo,
2166 High_Bound => Hi));
2167
2168 Set_Ekind (Base, E_Enumeration_Type);
2169 Set_Etype (Base, Base);
2170 Init_Size (Base, 8);
2171 Init_Alignment (Base);
2172 Set_Is_Generic_Type (Base);
2173 Set_Scalar_Range (Base, Scalar_Range (T));
2174 Set_Parent (Base, Parent (Def));
2175 end Analyze_Formal_Discrete_Type;
2176
2177 ----------------------------------
2178 -- Analyze_Formal_Floating_Type --
2179 ---------------------------------
2180
2181 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2182 Base : constant Entity_Id :=
2183 New_Internal_Entity
2184 (E_Floating_Point_Type, Current_Scope,
2185 Sloc (Defining_Identifier (Parent (Def))), 'G');
2186
2187 begin
2188 -- The various semantic attributes are taken from the predefined type
2189 -- Float, just so that all of them are initialized. Their values are
2190 -- never used because no constant folding or expansion takes place in
2191 -- the generic itself.
2192
2193 Enter_Name (T);
2194 Set_Ekind (T, E_Floating_Point_Subtype);
2195 Set_Etype (T, Base);
2196 Set_Size_Info (T, (Standard_Float));
2197 Set_RM_Size (T, RM_Size (Standard_Float));
2198 Set_Digits_Value (T, Digits_Value (Standard_Float));
2199 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2200 Set_Is_Constrained (T);
2201
2202 Set_Is_Generic_Type (Base);
2203 Set_Etype (Base, Base);
2204 Set_Size_Info (Base, (Standard_Float));
2205 Set_RM_Size (Base, RM_Size (Standard_Float));
2206 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2207 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2208 Set_Parent (Base, Parent (Def));
2209
2210 Check_Restriction (No_Floating_Point, Def);
2211 end Analyze_Formal_Floating_Type;
2212
2213 -----------------------------------
2214 -- Analyze_Formal_Interface_Type;--
2215 -----------------------------------
2216
2217 procedure Analyze_Formal_Interface_Type
2218 (N : Node_Id;
2219 T : Entity_Id;
2220 Def : Node_Id)
2221 is
2222 Loc : constant Source_Ptr := Sloc (N);
2223 New_N : Node_Id;
2224
2225 begin
2226 New_N :=
2227 Make_Full_Type_Declaration (Loc,
2228 Defining_Identifier => T,
2229 Type_Definition => Def);
2230
2231 Rewrite (N, New_N);
2232 Analyze (N);
2233 Set_Is_Generic_Type (T);
2234 end Analyze_Formal_Interface_Type;
2235
2236 ---------------------------------
2237 -- Analyze_Formal_Modular_Type --
2238 ---------------------------------
2239
2240 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2241 begin
2242 -- Apart from their entity kind, generic modular types are treated like
2243 -- signed integer types, and have the same attributes.
2244
2245 Analyze_Formal_Signed_Integer_Type (T, Def);
2246 Set_Ekind (T, E_Modular_Integer_Subtype);
2247 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2248
2249 end Analyze_Formal_Modular_Type;
2250
2251 ---------------------------------------
2252 -- Analyze_Formal_Object_Declaration --
2253 ---------------------------------------
2254
2255 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2256 E : constant Node_Id := Default_Expression (N);
2257 Id : constant Node_Id := Defining_Identifier (N);
2258 K : Entity_Kind;
2259 T : Node_Id;
2260
2261 begin
2262 Enter_Name (Id);
2263
2264 -- Determine the mode of the formal object
2265
2266 if Out_Present (N) then
2267 K := E_Generic_In_Out_Parameter;
2268
2269 if not In_Present (N) then
2270 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2271 end if;
2272
2273 else
2274 K := E_Generic_In_Parameter;
2275 end if;
2276
2277 if Present (Subtype_Mark (N)) then
2278 Find_Type (Subtype_Mark (N));
2279 T := Entity (Subtype_Mark (N));
2280
2281 -- Verify that there is no redundant null exclusion
2282
2283 if Null_Exclusion_Present (N) then
2284 if not Is_Access_Type (T) then
2285 Error_Msg_N
2286 ("null exclusion can only apply to an access type", N);
2287
2288 elsif Can_Never_Be_Null (T) then
2289 Error_Msg_NE
2290 ("`NOT NULL` not allowed (& already excludes null)",
2291 N, T);
2292 end if;
2293 end if;
2294
2295 -- Ada 2005 (AI-423): Formal object with an access definition
2296
2297 else
2298 Check_Access_Definition (N);
2299 T := Access_Definition
2300 (Related_Nod => N,
2301 N => Access_Definition (N));
2302 end if;
2303
2304 if Ekind (T) = E_Incomplete_Type then
2305 declare
2306 Error_Node : Node_Id;
2307
2308 begin
2309 if Present (Subtype_Mark (N)) then
2310 Error_Node := Subtype_Mark (N);
2311 else
2312 Check_Access_Definition (N);
2313 Error_Node := Access_Definition (N);
2314 end if;
2315
2316 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2317 end;
2318 end if;
2319
2320 if K = E_Generic_In_Parameter then
2321
2322 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2323
2324 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2325 Error_Msg_N
2326 ("generic formal of mode IN must not be of limited type", N);
2327 Explain_Limited_Type (T, N);
2328 end if;
2329
2330 if Is_Abstract_Type (T) then
2331 Error_Msg_N
2332 ("generic formal of mode IN must not be of abstract type", N);
2333 end if;
2334
2335 if Present (E) then
2336 Preanalyze_Spec_Expression (E, T);
2337
2338 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2339 Error_Msg_N
2340 ("initialization not allowed for limited types", E);
2341 Explain_Limited_Type (T, E);
2342 end if;
2343 end if;
2344
2345 Set_Ekind (Id, K);
2346 Set_Etype (Id, T);
2347
2348 -- Case of generic IN OUT parameter
2349
2350 else
2351 -- If the formal has an unconstrained type, construct its actual
2352 -- subtype, as is done for subprogram formals. In this fashion, all
2353 -- its uses can refer to specific bounds.
2354
2355 Set_Ekind (Id, K);
2356 Set_Etype (Id, T);
2357
2358 if (Is_Array_Type (T)
2359 and then not Is_Constrained (T))
2360 or else
2361 (Ekind (T) = E_Record_Type
2362 and then Has_Discriminants (T))
2363 then
2364 declare
2365 Non_Freezing_Ref : constant Node_Id :=
2366 New_Occurrence_Of (Id, Sloc (Id));
2367 Decl : Node_Id;
2368
2369 begin
2370 -- Make sure the actual subtype doesn't generate bogus freezing
2371
2372 Set_Must_Not_Freeze (Non_Freezing_Ref);
2373 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2374 Insert_Before_And_Analyze (N, Decl);
2375 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2376 end;
2377 else
2378 Set_Actual_Subtype (Id, T);
2379 end if;
2380
2381 if Present (E) then
2382 Error_Msg_N
2383 ("initialization not allowed for `IN OUT` formals", N);
2384 end if;
2385 end if;
2386
2387 if Has_Aspects (N) then
2388 Analyze_Aspect_Specifications (N, Id);
2389 end if;
2390 end Analyze_Formal_Object_Declaration;
2391
2392 ----------------------------------------------
2393 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2394 ----------------------------------------------
2395
2396 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2397 (T : Entity_Id;
2398 Def : Node_Id)
2399 is
2400 Loc : constant Source_Ptr := Sloc (Def);
2401 Base : constant Entity_Id :=
2402 New_Internal_Entity
2403 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2404 Sloc (Defining_Identifier (Parent (Def))), 'G');
2405
2406 begin
2407 -- The semantic attributes are set for completeness only, their values
2408 -- will never be used, since all properties of the type are non-static.
2409
2410 Enter_Name (T);
2411 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2412 Set_Etype (T, Base);
2413 Set_Size_Info (T, Standard_Integer);
2414 Set_RM_Size (T, RM_Size (Standard_Integer));
2415 Set_Small_Value (T, Ureal_1);
2416 Set_Delta_Value (T, Ureal_1);
2417 Set_Scalar_Range (T,
2418 Make_Range (Loc,
2419 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2420 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2421 Set_Is_Constrained (T);
2422
2423 Set_Is_Generic_Type (Base);
2424 Set_Etype (Base, Base);
2425 Set_Size_Info (Base, Standard_Integer);
2426 Set_RM_Size (Base, RM_Size (Standard_Integer));
2427 Set_Small_Value (Base, Ureal_1);
2428 Set_Delta_Value (Base, Ureal_1);
2429 Set_Scalar_Range (Base, Scalar_Range (T));
2430 Set_Parent (Base, Parent (Def));
2431
2432 Check_Restriction (No_Fixed_Point, Def);
2433 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2434
2435 ----------------------------------------
2436 -- Analyze_Formal_Package_Declaration --
2437 ----------------------------------------
2438
2439 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2440 Loc : constant Source_Ptr := Sloc (N);
2441 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2442 Formal : Entity_Id;
2443 Gen_Id : constant Node_Id := Name (N);
2444 Gen_Decl : Node_Id;
2445 Gen_Unit : Entity_Id;
2446 New_N : Node_Id;
2447 Parent_Installed : Boolean := False;
2448 Renaming : Node_Id;
2449 Parent_Instance : Entity_Id;
2450 Renaming_In_Par : Entity_Id;
2451 Associations : Boolean := True;
2452
2453 Vis_Prims_List : Elist_Id := No_Elist;
2454 -- List of primitives made temporarily visible in the instantiation
2455 -- to match the visibility of the formal type
2456
2457 function Build_Local_Package return Node_Id;
2458 -- The formal package is rewritten so that its parameters are replaced
2459 -- with corresponding declarations. For parameters with bona fide
2460 -- associations these declarations are created by Analyze_Associations
2461 -- as for a regular instantiation. For boxed parameters, we preserve
2462 -- the formal declarations and analyze them, in order to introduce
2463 -- entities of the right kind in the environment of the formal.
2464
2465 -------------------------
2466 -- Build_Local_Package --
2467 -------------------------
2468
2469 function Build_Local_Package return Node_Id is
2470 Decls : List_Id;
2471 Pack_Decl : Node_Id;
2472
2473 begin
2474 -- Within the formal, the name of the generic package is a renaming
2475 -- of the formal (as for a regular instantiation).
2476
2477 Pack_Decl :=
2478 Make_Package_Declaration (Loc,
2479 Specification =>
2480 Copy_Generic_Node
2481 (Specification (Original_Node (Gen_Decl)),
2482 Empty, Instantiating => True));
2483
2484 Renaming := Make_Package_Renaming_Declaration (Loc,
2485 Defining_Unit_Name =>
2486 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2487 Name => New_Occurrence_Of (Formal, Loc));
2488
2489 if Nkind (Gen_Id) = N_Identifier
2490 and then Chars (Gen_Id) = Chars (Pack_Id)
2491 then
2492 Error_Msg_NE
2493 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2494 end if;
2495
2496 -- If the formal is declared with a box, or with an others choice,
2497 -- create corresponding declarations for all entities in the formal
2498 -- part, so that names with the proper types are available in the
2499 -- specification of the formal package.
2500
2501 -- On the other hand, if there are no associations, then all the
2502 -- formals must have defaults, and this will be checked by the
2503 -- call to Analyze_Associations.
2504
2505 if Box_Present (N)
2506 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2507 then
2508 declare
2509 Formal_Decl : Node_Id;
2510
2511 begin
2512 -- TBA : for a formal package, need to recurse ???
2513
2514 Decls := New_List;
2515 Formal_Decl :=
2516 First
2517 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2518 while Present (Formal_Decl) loop
2519 Append_To
2520 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2521 Next (Formal_Decl);
2522 end loop;
2523 end;
2524
2525 -- If generic associations are present, use Analyze_Associations to
2526 -- create the proper renaming declarations.
2527
2528 else
2529 declare
2530 Act_Tree : constant Node_Id :=
2531 Copy_Generic_Node
2532 (Original_Node (Gen_Decl), Empty,
2533 Instantiating => True);
2534
2535 begin
2536 Generic_Renamings.Set_Last (0);
2537 Generic_Renamings_HTable.Reset;
2538 Instantiation_Node := N;
2539
2540 Decls :=
2541 Analyze_Associations
2542 (I_Node => Original_Node (N),
2543 Formals => Generic_Formal_Declarations (Act_Tree),
2544 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2545
2546 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2547 end;
2548 end if;
2549
2550 Append (Renaming, To => Decls);
2551
2552 -- Add generated declarations ahead of local declarations in
2553 -- the package.
2554
2555 if No (Visible_Declarations (Specification (Pack_Decl))) then
2556 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2557 else
2558 Insert_List_Before
2559 (First (Visible_Declarations (Specification (Pack_Decl))),
2560 Decls);
2561 end if;
2562
2563 return Pack_Decl;
2564 end Build_Local_Package;
2565
2566 -- Start of processing for Analyze_Formal_Package_Declaration
2567
2568 begin
2569 Check_Text_IO_Special_Unit (Gen_Id);
2570
2571 Init_Env;
2572 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2573 Gen_Unit := Entity (Gen_Id);
2574
2575 -- Check for a formal package that is a package renaming
2576
2577 if Present (Renamed_Object (Gen_Unit)) then
2578
2579 -- Indicate that unit is used, before replacing it with renamed
2580 -- entity for use below.
2581
2582 if In_Extended_Main_Source_Unit (N) then
2583 Set_Is_Instantiated (Gen_Unit);
2584 Generate_Reference (Gen_Unit, N);
2585 end if;
2586
2587 Gen_Unit := Renamed_Object (Gen_Unit);
2588 end if;
2589
2590 if Ekind (Gen_Unit) /= E_Generic_Package then
2591 Error_Msg_N ("expect generic package name", Gen_Id);
2592 Restore_Env;
2593 goto Leave;
2594
2595 elsif Gen_Unit = Current_Scope then
2596 Error_Msg_N
2597 ("generic package cannot be used as a formal package of itself",
2598 Gen_Id);
2599 Restore_Env;
2600 goto Leave;
2601
2602 elsif In_Open_Scopes (Gen_Unit) then
2603 if Is_Compilation_Unit (Gen_Unit)
2604 and then Is_Child_Unit (Current_Scope)
2605 then
2606 -- Special-case the error when the formal is a parent, and
2607 -- continue analysis to minimize cascaded errors.
2608
2609 Error_Msg_N
2610 ("generic parent cannot be used as formal package "
2611 & "of a child unit",
2612 Gen_Id);
2613
2614 else
2615 Error_Msg_N
2616 ("generic package cannot be used as a formal package "
2617 & "within itself",
2618 Gen_Id);
2619 Restore_Env;
2620 goto Leave;
2621 end if;
2622 end if;
2623
2624 -- Check that name of formal package does not hide name of generic,
2625 -- or its leading prefix. This check must be done separately because
2626 -- the name of the generic has already been analyzed.
2627
2628 declare
2629 Gen_Name : Entity_Id;
2630
2631 begin
2632 Gen_Name := Gen_Id;
2633 while Nkind (Gen_Name) = N_Expanded_Name loop
2634 Gen_Name := Prefix (Gen_Name);
2635 end loop;
2636
2637 if Chars (Gen_Name) = Chars (Pack_Id) then
2638 Error_Msg_NE
2639 ("& is hidden within declaration of formal package",
2640 Gen_Id, Gen_Name);
2641 end if;
2642 end;
2643
2644 if Box_Present (N)
2645 or else No (Generic_Associations (N))
2646 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2647 then
2648 Associations := False;
2649 end if;
2650
2651 -- If there are no generic associations, the generic parameters appear
2652 -- as local entities and are instantiated like them. We copy the generic
2653 -- package declaration as if it were an instantiation, and analyze it
2654 -- like a regular package, except that we treat the formals as
2655 -- additional visible components.
2656
2657 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2658
2659 if In_Extended_Main_Source_Unit (N) then
2660 Set_Is_Instantiated (Gen_Unit);
2661 Generate_Reference (Gen_Unit, N);
2662 end if;
2663
2664 Formal := New_Copy (Pack_Id);
2665 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2666
2667 begin
2668 -- Make local generic without formals. The formals will be replaced
2669 -- with internal declarations.
2670
2671 New_N := Build_Local_Package;
2672
2673 -- If there are errors in the parameter list, Analyze_Associations
2674 -- raises Instantiation_Error. Patch the declaration to prevent
2675 -- further exception propagation.
2676
2677 exception
2678 when Instantiation_Error =>
2679
2680 Enter_Name (Formal);
2681 Set_Ekind (Formal, E_Variable);
2682 Set_Etype (Formal, Any_Type);
2683 Restore_Hidden_Primitives (Vis_Prims_List);
2684
2685 if Parent_Installed then
2686 Remove_Parent;
2687 end if;
2688
2689 goto Leave;
2690 end;
2691
2692 Rewrite (N, New_N);
2693 Set_Defining_Unit_Name (Specification (New_N), Formal);
2694 Set_Generic_Parent (Specification (N), Gen_Unit);
2695 Set_Instance_Env (Gen_Unit, Formal);
2696 Set_Is_Generic_Instance (Formal);
2697
2698 Enter_Name (Formal);
2699 Set_Ekind (Formal, E_Package);
2700 Set_Etype (Formal, Standard_Void_Type);
2701 Set_Inner_Instances (Formal, New_Elmt_List);
2702 Push_Scope (Formal);
2703
2704 if Is_Child_Unit (Gen_Unit)
2705 and then Parent_Installed
2706 then
2707 -- Similarly, we have to make the name of the formal visible in the
2708 -- parent instance, to resolve properly fully qualified names that
2709 -- may appear in the generic unit. The parent instance has been
2710 -- placed on the scope stack ahead of the current scope.
2711
2712 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2713
2714 Renaming_In_Par :=
2715 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2716 Set_Ekind (Renaming_In_Par, E_Package);
2717 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2718 Set_Scope (Renaming_In_Par, Parent_Instance);
2719 Set_Parent (Renaming_In_Par, Parent (Formal));
2720 Set_Renamed_Object (Renaming_In_Par, Formal);
2721 Append_Entity (Renaming_In_Par, Parent_Instance);
2722 end if;
2723
2724 Analyze (Specification (N));
2725
2726 -- The formals for which associations are provided are not visible
2727 -- outside of the formal package. The others are still declared by a
2728 -- formal parameter declaration.
2729
2730 -- If there are no associations, the only local entity to hide is the
2731 -- generated package renaming itself.
2732
2733 declare
2734 E : Entity_Id;
2735
2736 begin
2737 E := First_Entity (Formal);
2738 while Present (E) loop
2739 if Associations
2740 and then not Is_Generic_Formal (E)
2741 then
2742 Set_Is_Hidden (E);
2743 end if;
2744
2745 if Ekind (E) = E_Package
2746 and then Renamed_Entity (E) = Formal
2747 then
2748 Set_Is_Hidden (E);
2749 exit;
2750 end if;
2751
2752 Next_Entity (E);
2753 end loop;
2754 end;
2755
2756 End_Package_Scope (Formal);
2757 Restore_Hidden_Primitives (Vis_Prims_List);
2758
2759 if Parent_Installed then
2760 Remove_Parent;
2761 end if;
2762
2763 Restore_Env;
2764
2765 -- Inside the generic unit, the formal package is a regular package, but
2766 -- no body is needed for it. Note that after instantiation, the defining
2767 -- unit name we need is in the new tree and not in the original (see
2768 -- Package_Instantiation). A generic formal package is an instance, and
2769 -- can be used as an actual for an inner instance.
2770
2771 Set_Has_Completion (Formal, True);
2772
2773 -- Add semantic information to the original defining identifier.
2774 -- for ASIS use.
2775
2776 Set_Ekind (Pack_Id, E_Package);
2777 Set_Etype (Pack_Id, Standard_Void_Type);
2778 Set_Scope (Pack_Id, Scope (Formal));
2779 Set_Has_Completion (Pack_Id, True);
2780
2781 <<Leave>>
2782 if Has_Aspects (N) then
2783 Analyze_Aspect_Specifications (N, Pack_Id);
2784 end if;
2785 end Analyze_Formal_Package_Declaration;
2786
2787 ---------------------------------
2788 -- Analyze_Formal_Private_Type --
2789 ---------------------------------
2790
2791 procedure Analyze_Formal_Private_Type
2792 (N : Node_Id;
2793 T : Entity_Id;
2794 Def : Node_Id)
2795 is
2796 begin
2797 New_Private_Type (N, T, Def);
2798
2799 -- Set the size to an arbitrary but legal value
2800
2801 Set_Size_Info (T, Standard_Integer);
2802 Set_RM_Size (T, RM_Size (Standard_Integer));
2803 end Analyze_Formal_Private_Type;
2804
2805 ------------------------------------
2806 -- Analyze_Formal_Incomplete_Type --
2807 ------------------------------------
2808
2809 procedure Analyze_Formal_Incomplete_Type
2810 (T : Entity_Id;
2811 Def : Node_Id)
2812 is
2813 begin
2814 Enter_Name (T);
2815 Set_Ekind (T, E_Incomplete_Type);
2816 Set_Etype (T, T);
2817 Set_Private_Dependents (T, New_Elmt_List);
2818
2819 if Tagged_Present (Def) then
2820 Set_Is_Tagged_Type (T);
2821 Make_Class_Wide_Type (T);
2822 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2823 end if;
2824 end Analyze_Formal_Incomplete_Type;
2825
2826 ----------------------------------------
2827 -- Analyze_Formal_Signed_Integer_Type --
2828 ----------------------------------------
2829
2830 procedure Analyze_Formal_Signed_Integer_Type
2831 (T : Entity_Id;
2832 Def : Node_Id)
2833 is
2834 Base : constant Entity_Id :=
2835 New_Internal_Entity
2836 (E_Signed_Integer_Type,
2837 Current_Scope,
2838 Sloc (Defining_Identifier (Parent (Def))), 'G');
2839
2840 begin
2841 Enter_Name (T);
2842
2843 Set_Ekind (T, E_Signed_Integer_Subtype);
2844 Set_Etype (T, Base);
2845 Set_Size_Info (T, Standard_Integer);
2846 Set_RM_Size (T, RM_Size (Standard_Integer));
2847 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2848 Set_Is_Constrained (T);
2849
2850 Set_Is_Generic_Type (Base);
2851 Set_Size_Info (Base, Standard_Integer);
2852 Set_RM_Size (Base, RM_Size (Standard_Integer));
2853 Set_Etype (Base, Base);
2854 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2855 Set_Parent (Base, Parent (Def));
2856 end Analyze_Formal_Signed_Integer_Type;
2857
2858 -------------------------------------------
2859 -- Analyze_Formal_Subprogram_Declaration --
2860 -------------------------------------------
2861
2862 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2863 Spec : constant Node_Id := Specification (N);
2864 Def : constant Node_Id := Default_Name (N);
2865 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2866 Subp : Entity_Id;
2867
2868 begin
2869 if Nam = Error then
2870 return;
2871 end if;
2872
2873 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2874 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2875 goto Leave;
2876 end if;
2877
2878 Analyze_Subprogram_Declaration (N);
2879 Set_Is_Formal_Subprogram (Nam);
2880 Set_Has_Completion (Nam);
2881
2882 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2883 Set_Is_Abstract_Subprogram (Nam);
2884 Set_Is_Dispatching_Operation (Nam);
2885
2886 declare
2887 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2888 begin
2889 if No (Ctrl_Type) then
2890 Error_Msg_N
2891 ("abstract formal subprogram must have a controlling type",
2892 N);
2893
2894 elsif Ada_Version >= Ada_2012
2895 and then Is_Incomplete_Type (Ctrl_Type)
2896 then
2897 Error_Msg_NE
2898 ("controlling type of abstract formal subprogram cannot " &
2899 "be incomplete type", N, Ctrl_Type);
2900
2901 else
2902 Check_Controlling_Formals (Ctrl_Type, Nam);
2903 end if;
2904 end;
2905 end if;
2906
2907 -- Default name is resolved at the point of instantiation
2908
2909 if Box_Present (N) then
2910 null;
2911
2912 -- Else default is bound at the point of generic declaration
2913
2914 elsif Present (Def) then
2915 if Nkind (Def) = N_Operator_Symbol then
2916 Find_Direct_Name (Def);
2917
2918 elsif Nkind (Def) /= N_Attribute_Reference then
2919 Analyze (Def);
2920
2921 else
2922 -- For an attribute reference, analyze the prefix and verify
2923 -- that it has the proper profile for the subprogram.
2924
2925 Analyze (Prefix (Def));
2926 Valid_Default_Attribute (Nam, Def);
2927 goto Leave;
2928 end if;
2929
2930 -- Default name may be overloaded, in which case the interpretation
2931 -- with the correct profile must be selected, as for a renaming.
2932 -- If the definition is an indexed component, it must denote a
2933 -- member of an entry family. If it is a selected component, it
2934 -- can be a protected operation.
2935
2936 if Etype (Def) = Any_Type then
2937 goto Leave;
2938
2939 elsif Nkind (Def) = N_Selected_Component then
2940 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2941 Error_Msg_N ("expect valid subprogram name as default", Def);
2942 end if;
2943
2944 elsif Nkind (Def) = N_Indexed_Component then
2945 if Is_Entity_Name (Prefix (Def)) then
2946 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2947 Error_Msg_N ("expect valid subprogram name as default", Def);
2948 end if;
2949
2950 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2951 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2952 E_Entry_Family
2953 then
2954 Error_Msg_N ("expect valid subprogram name as default", Def);
2955 end if;
2956
2957 else
2958 Error_Msg_N ("expect valid subprogram name as default", Def);
2959 goto Leave;
2960 end if;
2961
2962 elsif Nkind (Def) = N_Character_Literal then
2963
2964 -- Needs some type checks: subprogram should be parameterless???
2965
2966 Resolve (Def, (Etype (Nam)));
2967
2968 elsif not Is_Entity_Name (Def)
2969 or else not Is_Overloadable (Entity (Def))
2970 then
2971 Error_Msg_N ("expect valid subprogram name as default", Def);
2972 goto Leave;
2973
2974 elsif not Is_Overloaded (Def) then
2975 Subp := Entity (Def);
2976
2977 if Subp = Nam then
2978 Error_Msg_N ("premature usage of formal subprogram", Def);
2979
2980 elsif not Entity_Matches_Spec (Subp, Nam) then
2981 Error_Msg_N ("no visible entity matches specification", Def);
2982 end if;
2983
2984 -- More than one interpretation, so disambiguate as for a renaming
2985
2986 else
2987 declare
2988 I : Interp_Index;
2989 I1 : Interp_Index := 0;
2990 It : Interp;
2991 It1 : Interp;
2992
2993 begin
2994 Subp := Any_Id;
2995 Get_First_Interp (Def, I, It);
2996 while Present (It.Nam) loop
2997 if Entity_Matches_Spec (It.Nam, Nam) then
2998 if Subp /= Any_Id then
2999 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3000
3001 if It1 = No_Interp then
3002 Error_Msg_N ("ambiguous default subprogram", Def);
3003 else
3004 Subp := It1.Nam;
3005 end if;
3006
3007 exit;
3008
3009 else
3010 I1 := I;
3011 Subp := It.Nam;
3012 end if;
3013 end if;
3014
3015 Get_Next_Interp (I, It);
3016 end loop;
3017 end;
3018
3019 if Subp /= Any_Id then
3020
3021 -- Subprogram found, generate reference to it
3022
3023 Set_Entity (Def, Subp);
3024 Generate_Reference (Subp, Def);
3025
3026 if Subp = Nam then
3027 Error_Msg_N ("premature usage of formal subprogram", Def);
3028
3029 elsif Ekind (Subp) /= E_Operator then
3030 Check_Mode_Conformant (Subp, Nam);
3031 end if;
3032
3033 else
3034 Error_Msg_N ("no visible subprogram matches specification", N);
3035 end if;
3036 end if;
3037 end if;
3038
3039 <<Leave>>
3040 if Has_Aspects (N) then
3041 Analyze_Aspect_Specifications (N, Nam);
3042 end if;
3043
3044 end Analyze_Formal_Subprogram_Declaration;
3045
3046 -------------------------------------
3047 -- Analyze_Formal_Type_Declaration --
3048 -------------------------------------
3049
3050 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3051 Def : constant Node_Id := Formal_Type_Definition (N);
3052 T : Entity_Id;
3053
3054 begin
3055 T := Defining_Identifier (N);
3056
3057 if Present (Discriminant_Specifications (N))
3058 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3059 then
3060 Error_Msg_N
3061 ("discriminants not allowed for this formal type", T);
3062 end if;
3063
3064 -- Enter the new name, and branch to specific routine
3065
3066 case Nkind (Def) is
3067 when N_Formal_Private_Type_Definition =>
3068 Analyze_Formal_Private_Type (N, T, Def);
3069
3070 when N_Formal_Derived_Type_Definition =>
3071 Analyze_Formal_Derived_Type (N, T, Def);
3072
3073 when N_Formal_Incomplete_Type_Definition =>
3074 Analyze_Formal_Incomplete_Type (T, Def);
3075
3076 when N_Formal_Discrete_Type_Definition =>
3077 Analyze_Formal_Discrete_Type (T, Def);
3078
3079 when N_Formal_Signed_Integer_Type_Definition =>
3080 Analyze_Formal_Signed_Integer_Type (T, Def);
3081
3082 when N_Formal_Modular_Type_Definition =>
3083 Analyze_Formal_Modular_Type (T, Def);
3084
3085 when N_Formal_Floating_Point_Definition =>
3086 Analyze_Formal_Floating_Type (T, Def);
3087
3088 when N_Formal_Ordinary_Fixed_Point_Definition =>
3089 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3090
3091 when N_Formal_Decimal_Fixed_Point_Definition =>
3092 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3093
3094 when N_Array_Type_Definition =>
3095 Analyze_Formal_Array_Type (T, Def);
3096
3097 when N_Access_To_Object_Definition |
3098 N_Access_Function_Definition |
3099 N_Access_Procedure_Definition =>
3100 Analyze_Generic_Access_Type (T, Def);
3101
3102 -- Ada 2005: a interface declaration is encoded as an abstract
3103 -- record declaration or a abstract type derivation.
3104
3105 when N_Record_Definition =>
3106 Analyze_Formal_Interface_Type (N, T, Def);
3107
3108 when N_Derived_Type_Definition =>
3109 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3110
3111 when N_Error =>
3112 null;
3113
3114 when others =>
3115 raise Program_Error;
3116
3117 end case;
3118
3119 Set_Is_Generic_Type (T);
3120
3121 if Has_Aspects (N) then
3122 Analyze_Aspect_Specifications (N, T);
3123 end if;
3124 end Analyze_Formal_Type_Declaration;
3125
3126 ------------------------------------
3127 -- Analyze_Function_Instantiation --
3128 ------------------------------------
3129
3130 procedure Analyze_Function_Instantiation (N : Node_Id) is
3131 begin
3132 Analyze_Subprogram_Instantiation (N, E_Function);
3133 end Analyze_Function_Instantiation;
3134
3135 ---------------------------------
3136 -- Analyze_Generic_Access_Type --
3137 ---------------------------------
3138
3139 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3140 begin
3141 Enter_Name (T);
3142
3143 if Nkind (Def) = N_Access_To_Object_Definition then
3144 Access_Type_Declaration (T, Def);
3145
3146 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3147 and then No (Full_View (Designated_Type (T)))
3148 and then not Is_Generic_Type (Designated_Type (T))
3149 then
3150 Error_Msg_N ("premature usage of incomplete type", Def);
3151
3152 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3153 Error_Msg_N
3154 ("only a subtype mark is allowed in a formal", Def);
3155 end if;
3156
3157 else
3158 Access_Subprogram_Declaration (T, Def);
3159 end if;
3160 end Analyze_Generic_Access_Type;
3161
3162 ---------------------------------
3163 -- Analyze_Generic_Formal_Part --
3164 ---------------------------------
3165
3166 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3167 Gen_Parm_Decl : Node_Id;
3168
3169 begin
3170 -- The generic formals are processed in the scope of the generic unit,
3171 -- where they are immediately visible. The scope is installed by the
3172 -- caller.
3173
3174 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3175
3176 while Present (Gen_Parm_Decl) loop
3177 Analyze (Gen_Parm_Decl);
3178 Next (Gen_Parm_Decl);
3179 end loop;
3180
3181 Generate_Reference_To_Generic_Formals (Current_Scope);
3182 end Analyze_Generic_Formal_Part;
3183
3184 ------------------------------------------
3185 -- Analyze_Generic_Package_Declaration --
3186 ------------------------------------------
3187
3188 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3189 Loc : constant Source_Ptr := Sloc (N);
3190 Id : Entity_Id;
3191 New_N : Node_Id;
3192 Save_Parent : Node_Id;
3193 Renaming : Node_Id;
3194 Decls : constant List_Id :=
3195 Visible_Declarations (Specification (N));
3196 Decl : Node_Id;
3197
3198 begin
3199 Check_SPARK_05_Restriction ("generic is not allowed", N);
3200
3201 -- We introduce a renaming of the enclosing package, to have a usable
3202 -- entity as the prefix of an expanded name for a local entity of the
3203 -- form Par.P.Q, where P is the generic package. This is because a local
3204 -- entity named P may hide it, so that the usual visibility rules in
3205 -- the instance will not resolve properly.
3206
3207 Renaming :=
3208 Make_Package_Renaming_Declaration (Loc,
3209 Defining_Unit_Name =>
3210 Make_Defining_Identifier (Loc,
3211 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3212 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
3213
3214 if Present (Decls) then
3215 Decl := First (Decls);
3216 while Present (Decl)
3217 and then Nkind (Decl) = N_Pragma
3218 loop
3219 Next (Decl);
3220 end loop;
3221
3222 if Present (Decl) then
3223 Insert_Before (Decl, Renaming);
3224 else
3225 Append (Renaming, Visible_Declarations (Specification (N)));
3226 end if;
3227
3228 else
3229 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3230 end if;
3231
3232 -- Create copy of generic unit, and save for instantiation. If the unit
3233 -- is a child unit, do not copy the specifications for the parent, which
3234 -- are not part of the generic tree.
3235
3236 Save_Parent := Parent_Spec (N);
3237 Set_Parent_Spec (N, Empty);
3238
3239 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3240 Set_Parent_Spec (New_N, Save_Parent);
3241 Rewrite (N, New_N);
3242
3243 -- Once the contents of the generic copy and the template are swapped,
3244 -- do the same for their respective aspect specifications.
3245
3246 Exchange_Aspects (N, New_N);
3247 Id := Defining_Entity (N);
3248 Generate_Definition (Id);
3249
3250 -- Expansion is not applied to generic units
3251
3252 Start_Generic;
3253
3254 Enter_Name (Id);
3255 Set_Ekind (Id, E_Generic_Package);
3256 Set_Etype (Id, Standard_Void_Type);
3257 Set_Contract (Id, Make_Contract (Sloc (Id)));
3258
3259 -- Analyze aspects now, so that generated pragmas appear in the
3260 -- declarations before building and analyzing the generic copy.
3261
3262 if Has_Aspects (N) then
3263 Analyze_Aspect_Specifications (N, Id);
3264 end if;
3265
3266 Push_Scope (Id);
3267 Enter_Generic_Scope (Id);
3268 Set_Inner_Instances (Id, New_Elmt_List);
3269
3270 Set_Categorization_From_Pragmas (N);
3271 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3272
3273 -- Link the declaration of the generic homonym in the generic copy to
3274 -- the package it renames, so that it is always resolved properly.
3275
3276 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3277 Set_Entity (Associated_Node (Name (Renaming)), Id);
3278
3279 -- For a library unit, we have reconstructed the entity for the unit,
3280 -- and must reset it in the library tables.
3281
3282 if Nkind (Parent (N)) = N_Compilation_Unit then
3283 Set_Cunit_Entity (Current_Sem_Unit, Id);
3284 end if;
3285
3286 Analyze_Generic_Formal_Part (N);
3287
3288 -- After processing the generic formals, analysis proceeds as for a
3289 -- non-generic package.
3290
3291 Analyze (Specification (N));
3292
3293 Validate_Categorization_Dependency (N, Id);
3294
3295 End_Generic;
3296
3297 End_Package_Scope (Id);
3298 Exit_Generic_Scope (Id);
3299
3300 if Nkind (Parent (N)) /= N_Compilation_Unit then
3301 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3302 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3303 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3304
3305 else
3306 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3307 Validate_RT_RAT_Component (N);
3308
3309 -- If this is a spec without a body, check that generic parameters
3310 -- are referenced.
3311
3312 if not Body_Required (Parent (N)) then
3313 Check_References (Id);
3314 end if;
3315 end if;
3316 end Analyze_Generic_Package_Declaration;
3317
3318 --------------------------------------------
3319 -- Analyze_Generic_Subprogram_Declaration --
3320 --------------------------------------------
3321
3322 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3323 Spec : Node_Id;
3324 Id : Entity_Id;
3325 Formals : List_Id;
3326 New_N : Node_Id;
3327 Result_Type : Entity_Id;
3328 Save_Parent : Node_Id;
3329 Typ : Entity_Id;
3330
3331 begin
3332 Check_SPARK_05_Restriction ("generic is not allowed", N);
3333
3334 -- Create copy of generic unit, and save for instantiation. If the unit
3335 -- is a child unit, do not copy the specifications for the parent, which
3336 -- are not part of the generic tree.
3337
3338 Save_Parent := Parent_Spec (N);
3339 Set_Parent_Spec (N, Empty);
3340
3341 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3342 Set_Parent_Spec (New_N, Save_Parent);
3343 Rewrite (N, New_N);
3344
3345 -- Once the contents of the generic copy and the template are swapped,
3346 -- do the same for their respective aspect specifications.
3347
3348 Exchange_Aspects (N, New_N);
3349
3350 Spec := Specification (N);
3351 Id := Defining_Entity (Spec);
3352 Generate_Definition (Id);
3353 Set_Contract (Id, Make_Contract (Sloc (Id)));
3354
3355 if Nkind (Id) = N_Defining_Operator_Symbol then
3356 Error_Msg_N
3357 ("operator symbol not allowed for generic subprogram", Id);
3358 end if;
3359
3360 Start_Generic;
3361
3362 Enter_Name (Id);
3363 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3364
3365 -- Analyze the aspects of the generic copy to ensure that all generated
3366 -- pragmas (if any) perform their semantic effects.
3367
3368 if Has_Aspects (N) then
3369 Analyze_Aspect_Specifications (N, Id);
3370 end if;
3371
3372 Push_Scope (Id);
3373 Enter_Generic_Scope (Id);
3374 Set_Inner_Instances (Id, New_Elmt_List);
3375 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3376
3377 Analyze_Generic_Formal_Part (N);
3378
3379 Formals := Parameter_Specifications (Spec);
3380
3381 if Present (Formals) then
3382 Process_Formals (Formals, Spec);
3383 end if;
3384
3385 if Nkind (Spec) = N_Function_Specification then
3386 Set_Ekind (Id, E_Generic_Function);
3387
3388 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3389 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3390 Set_Etype (Id, Result_Type);
3391
3392 -- Check restriction imposed by AI05-073: a generic function
3393 -- cannot return an abstract type or an access to such.
3394
3395 -- This is a binding interpretation should it apply to earlier
3396 -- versions of Ada as well as Ada 2012???
3397
3398 if Is_Abstract_Type (Designated_Type (Result_Type))
3399 and then Ada_Version >= Ada_2012
3400 then
3401 Error_Msg_N ("generic function cannot have an access result"
3402 & " that designates an abstract type", Spec);
3403 end if;
3404
3405 else
3406 Find_Type (Result_Definition (Spec));
3407 Typ := Entity (Result_Definition (Spec));
3408
3409 if Is_Abstract_Type (Typ)
3410 and then Ada_Version >= Ada_2012
3411 then
3412 Error_Msg_N
3413 ("generic function cannot have abstract result type", Spec);
3414 end if;
3415
3416 -- If a null exclusion is imposed on the result type, then create
3417 -- a null-excluding itype (an access subtype) and use it as the
3418 -- function's Etype.
3419
3420 if Is_Access_Type (Typ)
3421 and then Null_Exclusion_Present (Spec)
3422 then
3423 Set_Etype (Id,
3424 Create_Null_Excluding_Itype
3425 (T => Typ,
3426 Related_Nod => Spec,
3427 Scope_Id => Defining_Unit_Name (Spec)));
3428 else
3429 Set_Etype (Id, Typ);
3430 end if;
3431 end if;
3432
3433 else
3434 Set_Ekind (Id, E_Generic_Procedure);
3435 Set_Etype (Id, Standard_Void_Type);
3436 end if;
3437
3438 -- For a library unit, we have reconstructed the entity for the unit,
3439 -- and must reset it in the library tables. We also make sure that
3440 -- Body_Required is set properly in the original compilation unit node.
3441
3442 if Nkind (Parent (N)) = N_Compilation_Unit then
3443 Set_Cunit_Entity (Current_Sem_Unit, Id);
3444 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3445 end if;
3446
3447 Set_Categorization_From_Pragmas (N);
3448 Validate_Categorization_Dependency (N, Id);
3449
3450 Save_Global_References (Original_Node (N));
3451
3452 -- For ASIS purposes, convert any postcondition, precondition pragmas
3453 -- into aspects, if N is not a compilation unit by itself, in order to
3454 -- enable the analysis of expressions inside the corresponding PPC
3455 -- pragmas.
3456
3457 if ASIS_Mode and then Is_List_Member (N) then
3458 Make_Aspect_For_PPC_In_Gen_Sub_Decl (N);
3459 end if;
3460
3461 End_Generic;
3462 End_Scope;
3463 Exit_Generic_Scope (Id);
3464 Generate_Reference_To_Formals (Id);
3465
3466 List_Inherited_Pre_Post_Aspects (Id);
3467 end Analyze_Generic_Subprogram_Declaration;
3468
3469 -----------------------------------
3470 -- Analyze_Package_Instantiation --
3471 -----------------------------------
3472
3473 procedure Analyze_Package_Instantiation (N : Node_Id) is
3474 Loc : constant Source_Ptr := Sloc (N);
3475 Gen_Id : constant Node_Id := Name (N);
3476
3477 Act_Decl : Node_Id;
3478 Act_Decl_Name : Node_Id;
3479 Act_Decl_Id : Entity_Id;
3480 Act_Spec : Node_Id;
3481 Act_Tree : Node_Id;
3482
3483 Gen_Decl : Node_Id;
3484 Gen_Unit : Entity_Id;
3485
3486 Is_Actual_Pack : constant Boolean :=
3487 Is_Internal (Defining_Entity (N));
3488
3489 Env_Installed : Boolean := False;
3490 Parent_Installed : Boolean := False;
3491 Renaming_List : List_Id;
3492 Unit_Renaming : Node_Id;
3493 Needs_Body : Boolean;
3494 Inline_Now : Boolean := False;
3495
3496 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3497 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3498
3499 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3500 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3501 -- Save the SPARK_Mode-related data for restore on exit
3502
3503 Save_Style_Check : constant Boolean := Style_Check;
3504 -- Save style check mode for restore on exit
3505
3506 procedure Delay_Descriptors (E : Entity_Id);
3507 -- Delay generation of subprogram descriptors for given entity
3508
3509 function Might_Inline_Subp return Boolean;
3510 -- If inlining is active and the generic contains inlined subprograms,
3511 -- we instantiate the body. This may cause superfluous instantiations,
3512 -- but it is simpler than detecting the need for the body at the point
3513 -- of inlining, when the context of the instance is not available.
3514
3515 function Must_Inline_Subp return Boolean;
3516 -- If inlining is active and the generic contains inlined subprograms,
3517 -- return True if some of the inlined subprograms must be inlined by
3518 -- the frontend.
3519
3520 -----------------------
3521 -- Delay_Descriptors --
3522 -----------------------
3523
3524 procedure Delay_Descriptors (E : Entity_Id) is
3525 begin
3526 if not Delay_Subprogram_Descriptors (E) then
3527 Set_Delay_Subprogram_Descriptors (E);
3528 Pending_Descriptor.Append (E);
3529 end if;
3530 end Delay_Descriptors;
3531
3532 -----------------------
3533 -- Might_Inline_Subp --
3534 -----------------------
3535
3536 function Might_Inline_Subp return Boolean is
3537 E : Entity_Id;
3538
3539 begin
3540 if not Inline_Processing_Required then
3541 return False;
3542
3543 else
3544 E := First_Entity (Gen_Unit);
3545 while Present (E) loop
3546 if Is_Subprogram (E) and then Is_Inlined (E) then
3547 return True;
3548 end if;
3549
3550 Next_Entity (E);
3551 end loop;
3552 end if;
3553
3554 return False;
3555 end Might_Inline_Subp;
3556
3557 ----------------------
3558 -- Must_Inline_Subp --
3559 ----------------------
3560
3561 function Must_Inline_Subp return Boolean is
3562 E : Entity_Id;
3563
3564 begin
3565 if not Inline_Processing_Required then
3566 return False;
3567
3568 else
3569 E := First_Entity (Gen_Unit);
3570 while Present (E) loop
3571 if Is_Subprogram (E)
3572 and then Is_Inlined (E)
3573 and then Must_Inline (E)
3574 then
3575 return True;
3576 end if;
3577
3578 Next_Entity (E);
3579 end loop;
3580 end if;
3581
3582 return False;
3583 end Must_Inline_Subp;
3584
3585 -- Local declarations
3586
3587 Vis_Prims_List : Elist_Id := No_Elist;
3588 -- List of primitives made temporarily visible in the instantiation
3589 -- to match the visibility of the formal type
3590
3591 -- Start of processing for Analyze_Package_Instantiation
3592
3593 begin
3594 Check_SPARK_05_Restriction ("generic is not allowed", N);
3595
3596 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3597 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3598
3599 Check_Text_IO_Special_Unit (Name (N));
3600
3601 -- Make node global for error reporting
3602
3603 Instantiation_Node := N;
3604
3605 -- Turn off style checking in instances. If the check is enabled on the
3606 -- generic unit, a warning in an instance would just be noise. If not
3607 -- enabled on the generic, then a warning in an instance is just wrong.
3608
3609 Style_Check := False;
3610
3611 -- Case of instantiation of a generic package
3612
3613 if Nkind (N) = N_Package_Instantiation then
3614 Act_Decl_Id := New_Copy (Defining_Entity (N));
3615 Set_Comes_From_Source (Act_Decl_Id, True);
3616
3617 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3618 Act_Decl_Name :=
3619 Make_Defining_Program_Unit_Name (Loc,
3620 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
3621 Defining_Identifier => Act_Decl_Id);
3622 else
3623 Act_Decl_Name := Act_Decl_Id;
3624 end if;
3625
3626 -- Case of instantiation of a formal package
3627
3628 else
3629 Act_Decl_Id := Defining_Identifier (N);
3630 Act_Decl_Name := Act_Decl_Id;
3631 end if;
3632
3633 Generate_Definition (Act_Decl_Id);
3634 Preanalyze_Actuals (N);
3635
3636 Init_Env;
3637 Env_Installed := True;
3638
3639 -- Reset renaming map for formal types. The mapping is established
3640 -- when analyzing the generic associations, but some mappings are
3641 -- inherited from formal packages of parent units, and these are
3642 -- constructed when the parents are installed.
3643
3644 Generic_Renamings.Set_Last (0);
3645 Generic_Renamings_HTable.Reset;
3646
3647 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3648 Gen_Unit := Entity (Gen_Id);
3649
3650 -- Verify that it is the name of a generic package
3651
3652 -- A visibility glitch: if the instance is a child unit and the generic
3653 -- is the generic unit of a parent instance (i.e. both the parent and
3654 -- the child units are instances of the same package) the name now
3655 -- denotes the renaming within the parent, not the intended generic
3656 -- unit. See if there is a homonym that is the desired generic. The
3657 -- renaming declaration must be visible inside the instance of the
3658 -- child, but not when analyzing the name in the instantiation itself.
3659
3660 if Ekind (Gen_Unit) = E_Package
3661 and then Present (Renamed_Entity (Gen_Unit))
3662 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3663 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3664 and then Present (Homonym (Gen_Unit))
3665 then
3666 Gen_Unit := Homonym (Gen_Unit);
3667 end if;
3668
3669 if Etype (Gen_Unit) = Any_Type then
3670 Restore_Env;
3671 goto Leave;
3672
3673 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3674
3675 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3676
3677 if From_Limited_With (Gen_Unit) then
3678 Error_Msg_N
3679 ("cannot instantiate a limited withed package", Gen_Id);
3680 else
3681 Error_Msg_NE
3682 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3683 end if;
3684
3685 Restore_Env;
3686 goto Leave;
3687 end if;
3688
3689 if In_Extended_Main_Source_Unit (N) then
3690 Set_Is_Instantiated (Gen_Unit);
3691 Generate_Reference (Gen_Unit, N);
3692
3693 if Present (Renamed_Object (Gen_Unit)) then
3694 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3695 Generate_Reference (Renamed_Object (Gen_Unit), N);
3696 end if;
3697 end if;
3698
3699 if Nkind (Gen_Id) = N_Identifier
3700 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3701 then
3702 Error_Msg_NE
3703 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3704
3705 elsif Nkind (Gen_Id) = N_Expanded_Name
3706 and then Is_Child_Unit (Gen_Unit)
3707 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3708 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3709 then
3710 Error_Msg_N
3711 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3712 end if;
3713
3714 Set_Entity (Gen_Id, Gen_Unit);
3715
3716 -- If generic is a renaming, get original generic unit
3717
3718 if Present (Renamed_Object (Gen_Unit))
3719 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3720 then
3721 Gen_Unit := Renamed_Object (Gen_Unit);
3722 end if;
3723
3724 -- Verify that there are no circular instantiations
3725
3726 if In_Open_Scopes (Gen_Unit) then
3727 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3728 Restore_Env;
3729 goto Leave;
3730
3731 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3732 Error_Msg_Node_2 := Current_Scope;
3733 Error_Msg_NE
3734 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3735 Circularity_Detected := True;
3736 Restore_Env;
3737 goto Leave;
3738
3739 else
3740 -- If the context of the instance is subject to SPARK_Mode "off",
3741 -- set the global flag which signals Analyze_Pragma to ignore all
3742 -- SPARK_Mode pragmas within the instance.
3743
3744 if SPARK_Mode = Off then
3745 Ignore_Pragma_SPARK_Mode := True;
3746 end if;
3747
3748 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3749
3750 -- Initialize renamings map, for error checking, and the list that
3751 -- holds private entities whose views have changed between generic
3752 -- definition and instantiation. If this is the instance created to
3753 -- validate an actual package, the instantiation environment is that
3754 -- of the enclosing instance.
3755
3756 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3757
3758 -- Copy original generic tree, to produce text for instantiation
3759
3760 Act_Tree :=
3761 Copy_Generic_Node
3762 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3763
3764 Act_Spec := Specification (Act_Tree);
3765
3766 -- If this is the instance created to validate an actual package,
3767 -- only the formals matter, do not examine the package spec itself.
3768
3769 if Is_Actual_Pack then
3770 Set_Visible_Declarations (Act_Spec, New_List);
3771 Set_Private_Declarations (Act_Spec, New_List);
3772 end if;
3773
3774 Renaming_List :=
3775 Analyze_Associations
3776 (I_Node => N,
3777 Formals => Generic_Formal_Declarations (Act_Tree),
3778 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3779
3780 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3781
3782 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3783 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3784 Set_Is_Generic_Instance (Act_Decl_Id);
3785 Set_Generic_Parent (Act_Spec, Gen_Unit);
3786
3787 -- References to the generic in its own declaration or its body are
3788 -- references to the instance. Add a renaming declaration for the
3789 -- generic unit itself. This declaration, as well as the renaming
3790 -- declarations for the generic formals, must remain private to the
3791 -- unit: the formals, because this is the language semantics, and
3792 -- the unit because its use is an artifact of the implementation.
3793
3794 Unit_Renaming :=
3795 Make_Package_Renaming_Declaration (Loc,
3796 Defining_Unit_Name =>
3797 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3798 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3799
3800 Append (Unit_Renaming, Renaming_List);
3801
3802 -- The renaming declarations are the first local declarations of the
3803 -- new unit.
3804
3805 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3806 Insert_List_Before
3807 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3808 else
3809 Set_Visible_Declarations (Act_Spec, Renaming_List);
3810 end if;
3811
3812 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3813
3814 -- Propagate the aspect specifications from the package declaration
3815 -- template to the instantiated version of the package declaration.
3816
3817 if Has_Aspects (Act_Tree) then
3818 Set_Aspect_Specifications (Act_Decl,
3819 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3820 end if;
3821
3822 -- Save the instantiation node, for subsequent instantiation of the
3823 -- body, if there is one and we are generating code for the current
3824 -- unit. Mark unit as having a body (avoids premature error message).
3825
3826 -- We instantiate the body if we are generating code, if we are
3827 -- generating cross-reference information, or if we are building
3828 -- trees for ASIS use or GNATprove use.
3829
3830 declare
3831 Enclosing_Body_Present : Boolean := False;
3832 -- If the generic unit is not a compilation unit, then a body may
3833 -- be present in its parent even if none is required. We create a
3834 -- tentative pending instantiation for the body, which will be
3835 -- discarded if none is actually present.
3836
3837 Scop : Entity_Id;
3838
3839 begin
3840 if Scope (Gen_Unit) /= Standard_Standard
3841 and then not Is_Child_Unit (Gen_Unit)
3842 then
3843 Scop := Scope (Gen_Unit);
3844
3845 while Present (Scop)
3846 and then Scop /= Standard_Standard
3847 loop
3848 if Unit_Requires_Body (Scop) then
3849 Enclosing_Body_Present := True;
3850 exit;
3851
3852 elsif In_Open_Scopes (Scop)
3853 and then In_Package_Body (Scop)
3854 then
3855 Enclosing_Body_Present := True;
3856 exit;
3857 end if;
3858
3859 exit when Is_Compilation_Unit (Scop);
3860 Scop := Scope (Scop);
3861 end loop;
3862 end if;
3863
3864 -- If front-end inlining is enabled, and this is a unit for which
3865 -- code will be generated, we instantiate the body at once.
3866
3867 -- This is done if the instance is not the main unit, and if the
3868 -- generic is not a child unit of another generic, to avoid scope
3869 -- problems and the reinstallation of parent instances.
3870
3871 if Expander_Active
3872 and then (not Is_Child_Unit (Gen_Unit)
3873 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3874 and then Might_Inline_Subp
3875 and then not Is_Actual_Pack
3876 then
3877 if not Back_End_Inlining
3878 and then Front_End_Inlining
3879 and then (Is_In_Main_Unit (N)
3880 or else In_Main_Context (Current_Scope))
3881 and then Nkind (Parent (N)) /= N_Compilation_Unit
3882 then
3883 Inline_Now := True;
3884
3885 elsif Back_End_Inlining
3886 and then Must_Inline_Subp
3887 and then (Is_In_Main_Unit (N)
3888 or else In_Main_Context (Current_Scope))
3889 and then Nkind (Parent (N)) /= N_Compilation_Unit
3890 then
3891 Inline_Now := True;
3892
3893 -- In configurable_run_time mode we force the inlining of
3894 -- predefined subprograms marked Inline_Always, to minimize
3895 -- the use of the run-time library.
3896
3897 elsif Is_Predefined_File_Name
3898 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3899 and then Configurable_Run_Time_Mode
3900 and then Nkind (Parent (N)) /= N_Compilation_Unit
3901 then
3902 Inline_Now := True;
3903 end if;
3904
3905 -- If the current scope is itself an instance within a child
3906 -- unit, there will be duplications in the scope stack, and the
3907 -- unstacking mechanism in Inline_Instance_Body will fail.
3908 -- This loses some rare cases of optimization, and might be
3909 -- improved some day, if we can find a proper abstraction for
3910 -- "the complete compilation context" that can be saved and
3911 -- restored. ???
3912
3913 if Is_Generic_Instance (Current_Scope) then
3914 declare
3915 Curr_Unit : constant Entity_Id :=
3916 Cunit_Entity (Current_Sem_Unit);
3917 begin
3918 if Curr_Unit /= Current_Scope
3919 and then Is_Child_Unit (Curr_Unit)
3920 then
3921 Inline_Now := False;
3922 end if;
3923 end;
3924 end if;
3925 end if;
3926
3927 Needs_Body :=
3928 (Unit_Requires_Body (Gen_Unit)
3929 or else Enclosing_Body_Present
3930 or else Present (Corresponding_Body (Gen_Decl)))
3931 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3932 and then not Is_Actual_Pack
3933 and then not Inline_Now
3934 and then (Operating_Mode = Generate_Code
3935
3936 -- Need comment for this check ???
3937
3938 or else (Operating_Mode = Check_Semantics
3939 and then (ASIS_Mode or GNATprove_Mode)));
3940
3941 -- If front_end_inlining is enabled, do not instantiate body if
3942 -- within a generic context.
3943
3944 if (Front_End_Inlining and then not Expander_Active)
3945 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3946 then
3947 Needs_Body := False;
3948 end if;
3949
3950 -- If the current context is generic, and the package being
3951 -- instantiated is declared within a formal package, there is no
3952 -- body to instantiate until the enclosing generic is instantiated
3953 -- and there is an actual for the formal package. If the formal
3954 -- package has parameters, we build a regular package instance for
3955 -- it, that precedes the original formal package declaration.
3956
3957 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3958 declare
3959 Decl : constant Node_Id :=
3960 Original_Node
3961 (Unit_Declaration_Node (Scope (Gen_Unit)));
3962 begin
3963 if Nkind (Decl) = N_Formal_Package_Declaration
3964 or else (Nkind (Decl) = N_Package_Declaration
3965 and then Is_List_Member (Decl)
3966 and then Present (Next (Decl))
3967 and then
3968 Nkind (Next (Decl)) =
3969 N_Formal_Package_Declaration)
3970 then
3971 Needs_Body := False;
3972 end if;
3973 end;
3974 end if;
3975 end;
3976
3977 -- For RCI unit calling stubs, we omit the instance body if the
3978 -- instance is the RCI library unit itself.
3979
3980 -- However there is a special case for nested instances: in this case
3981 -- we do generate the instance body, as it might be required, e.g.
3982 -- because it provides stream attributes for some type used in the
3983 -- profile of a remote subprogram. This is consistent with 12.3(12),
3984 -- which indicates that the instance body occurs at the place of the
3985 -- instantiation, and thus is part of the RCI declaration, which is
3986 -- present on all client partitions (this is E.2.3(18)).
3987
3988 -- Note that AI12-0002 may make it illegal at some point to have
3989 -- stream attributes defined in an RCI unit, in which case this
3990 -- special case will become unnecessary. In the meantime, there
3991 -- is known application code in production that depends on this
3992 -- being possible, so we definitely cannot eliminate the body in
3993 -- the case of nested instances for the time being.
3994
3995 -- When we generate a nested instance body, calling stubs for any
3996 -- relevant subprogram will be be inserted immediately after the
3997 -- subprogram declarations, and will take precedence over the
3998 -- subsequent (original) body. (The stub and original body will be
3999 -- complete homographs, but this is permitted in an instance).
4000 -- (Could we do better and remove the original body???)
4001
4002 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4003 and then Comes_From_Source (N)
4004 and then Nkind (Parent (N)) = N_Compilation_Unit
4005 then
4006 Needs_Body := False;
4007 end if;
4008
4009 if Needs_Body then
4010
4011 -- Here is a defence against a ludicrous number of instantiations
4012 -- caused by a circular set of instantiation attempts.
4013
4014 if Pending_Instantiations.Last > Maximum_Instantiations then
4015 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4016 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4017 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4018 raise Unrecoverable_Error;
4019 end if;
4020
4021 -- Indicate that the enclosing scopes contain an instantiation,
4022 -- and that cleanup actions should be delayed until after the
4023 -- instance body is expanded.
4024
4025 Check_Forward_Instantiation (Gen_Decl);
4026 if Nkind (N) = N_Package_Instantiation then
4027 declare
4028 Enclosing_Master : Entity_Id;
4029
4030 begin
4031 -- Loop to search enclosing masters
4032
4033 Enclosing_Master := Current_Scope;
4034 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4035 if Ekind (Enclosing_Master) = E_Package then
4036 if Is_Compilation_Unit (Enclosing_Master) then
4037 if In_Package_Body (Enclosing_Master) then
4038 Delay_Descriptors
4039 (Body_Entity (Enclosing_Master));
4040 else
4041 Delay_Descriptors
4042 (Enclosing_Master);
4043 end if;
4044
4045 exit Scope_Loop;
4046
4047 else
4048 Enclosing_Master := Scope (Enclosing_Master);
4049 end if;
4050
4051 elsif Is_Generic_Unit (Enclosing_Master)
4052 or else Ekind (Enclosing_Master) = E_Void
4053 then
4054 -- Cleanup actions will eventually be performed on the
4055 -- enclosing subprogram or package instance, if any.
4056 -- Enclosing scope is void in the formal part of a
4057 -- generic subprogram.
4058
4059 exit Scope_Loop;
4060
4061 else
4062 if Ekind (Enclosing_Master) = E_Entry
4063 and then
4064 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4065 then
4066 if not Expander_Active then
4067 exit Scope_Loop;
4068 else
4069 Enclosing_Master :=
4070 Protected_Body_Subprogram (Enclosing_Master);
4071 end if;
4072 end if;
4073
4074 Set_Delay_Cleanups (Enclosing_Master);
4075
4076 while Ekind (Enclosing_Master) = E_Block loop
4077 Enclosing_Master := Scope (Enclosing_Master);
4078 end loop;
4079
4080 if Is_Subprogram (Enclosing_Master) then
4081 Delay_Descriptors (Enclosing_Master);
4082
4083 elsif Is_Task_Type (Enclosing_Master) then
4084 declare
4085 TBP : constant Node_Id :=
4086 Get_Task_Body_Procedure
4087 (Enclosing_Master);
4088 begin
4089 if Present (TBP) then
4090 Delay_Descriptors (TBP);
4091 Set_Delay_Cleanups (TBP);
4092 end if;
4093 end;
4094 end if;
4095
4096 exit Scope_Loop;
4097 end if;
4098 end loop Scope_Loop;
4099 end;
4100
4101 -- Make entry in table
4102
4103 Pending_Instantiations.Append
4104 ((Inst_Node => N,
4105 Act_Decl => Act_Decl,
4106 Expander_Status => Expander_Active,
4107 Current_Sem_Unit => Current_Sem_Unit,
4108 Scope_Suppress => Scope_Suppress,
4109 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4110 Version => Ada_Version,
4111 Version_Pragma => Ada_Version_Pragma,
4112 Warnings => Save_Warnings,
4113 SPARK_Mode => SPARK_Mode,
4114 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4115 end if;
4116 end if;
4117
4118 Set_Categorization_From_Pragmas (Act_Decl);
4119
4120 if Parent_Installed then
4121 Hide_Current_Scope;
4122 end if;
4123
4124 Set_Instance_Spec (N, Act_Decl);
4125
4126 -- If not a compilation unit, insert the package declaration before
4127 -- the original instantiation node.
4128
4129 if Nkind (Parent (N)) /= N_Compilation_Unit then
4130 Mark_Rewrite_Insertion (Act_Decl);
4131 Insert_Before (N, Act_Decl);
4132 Analyze (Act_Decl);
4133
4134 -- For an instantiation that is a compilation unit, place
4135 -- declaration on current node so context is complete for analysis
4136 -- (including nested instantiations). If this is the main unit,
4137 -- the declaration eventually replaces the instantiation node.
4138 -- If the instance body is created later, it replaces the
4139 -- instance node, and the declaration is attached to it
4140 -- (see Build_Instance_Compilation_Unit_Nodes).
4141
4142 else
4143 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4144
4145 -- The entity for the current unit is the newly created one,
4146 -- and all semantic information is attached to it.
4147
4148 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4149
4150 -- If this is the main unit, replace the main entity as well
4151
4152 if Current_Sem_Unit = Main_Unit then
4153 Main_Unit_Entity := Act_Decl_Id;
4154 end if;
4155 end if;
4156
4157 Set_Unit (Parent (N), Act_Decl);
4158 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4159 Set_Package_Instantiation (Act_Decl_Id, N);
4160
4161 -- Process aspect specifications of the instance node, if any, to
4162 -- take into account categorization pragmas before analyzing the
4163 -- instance.
4164
4165 if Has_Aspects (N) then
4166 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4167 end if;
4168
4169 Analyze (Act_Decl);
4170 Set_Unit (Parent (N), N);
4171 Set_Body_Required (Parent (N), False);
4172
4173 -- We never need elaboration checks on instantiations, since by
4174 -- definition, the body instantiation is elaborated at the same
4175 -- time as the spec instantiation.
4176
4177 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4178 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4179 end if;
4180
4181 Check_Elab_Instantiation (N);
4182
4183 if ABE_Is_Certain (N) and then Needs_Body then
4184 Pending_Instantiations.Decrement_Last;
4185 end if;
4186
4187 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4188
4189 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4190 First_Private_Entity (Act_Decl_Id));
4191
4192 -- If the instantiation will receive a body, the unit will be
4193 -- transformed into a package body, and receive its own elaboration
4194 -- entity. Otherwise, the nature of the unit is now a package
4195 -- declaration.
4196
4197 if Nkind (Parent (N)) = N_Compilation_Unit
4198 and then not Needs_Body
4199 then
4200 Rewrite (N, Act_Decl);
4201 end if;
4202
4203 if Present (Corresponding_Body (Gen_Decl))
4204 or else Unit_Requires_Body (Gen_Unit)
4205 then
4206 Set_Has_Completion (Act_Decl_Id);
4207 end if;
4208
4209 Check_Formal_Packages (Act_Decl_Id);
4210
4211 Restore_Hidden_Primitives (Vis_Prims_List);
4212 Restore_Private_Views (Act_Decl_Id);
4213
4214 Inherit_Context (Gen_Decl, N);
4215
4216 if Parent_Installed then
4217 Remove_Parent;
4218 end if;
4219
4220 Restore_Env;
4221 Env_Installed := False;
4222 end if;
4223
4224 Validate_Categorization_Dependency (N, Act_Decl_Id);
4225
4226 -- There used to be a check here to prevent instantiations in local
4227 -- contexts if the No_Local_Allocators restriction was active. This
4228 -- check was removed by a binding interpretation in AI-95-00130/07,
4229 -- but we retain the code for documentation purposes.
4230
4231 -- if Ekind (Act_Decl_Id) /= E_Void
4232 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4233 -- then
4234 -- Check_Restriction (No_Local_Allocators, N);
4235 -- end if;
4236
4237 if Inline_Now then
4238 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4239 end if;
4240
4241 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4242 -- be used as defining identifiers for a formal package and for the
4243 -- corresponding expanded package.
4244
4245 if Nkind (N) = N_Formal_Package_Declaration then
4246 Act_Decl_Id := New_Copy (Defining_Entity (N));
4247 Set_Comes_From_Source (Act_Decl_Id, True);
4248 Set_Is_Generic_Instance (Act_Decl_Id, False);
4249 Set_Defining_Identifier (N, Act_Decl_Id);
4250 end if;
4251
4252 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4253 SPARK_Mode := Save_SM;
4254 SPARK_Mode_Pragma := Save_SMP;
4255 Style_Check := Save_Style_Check;
4256
4257 -- Check that if N is an instantiation of System.Dim_Float_IO or
4258 -- System.Dim_Integer_IO, the formal type has a dimension system.
4259
4260 if Nkind (N) = N_Package_Instantiation
4261 and then Is_Dim_IO_Package_Instantiation (N)
4262 then
4263 declare
4264 Assoc : constant Node_Id := First (Generic_Associations (N));
4265 begin
4266 if not Has_Dimension_System
4267 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4268 then
4269 Error_Msg_N ("type with a dimension system expected", Assoc);
4270 end if;
4271 end;
4272 end if;
4273
4274 <<Leave>>
4275 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4276 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4277 end if;
4278
4279 exception
4280 when Instantiation_Error =>
4281 if Parent_Installed then
4282 Remove_Parent;
4283 end if;
4284
4285 if Env_Installed then
4286 Restore_Env;
4287 end if;
4288
4289 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4290 SPARK_Mode := Save_SM;
4291 SPARK_Mode_Pragma := Save_SMP;
4292 Style_Check := Save_Style_Check;
4293 end Analyze_Package_Instantiation;
4294
4295 --------------------------
4296 -- Inline_Instance_Body --
4297 --------------------------
4298
4299 procedure Inline_Instance_Body
4300 (N : Node_Id;
4301 Gen_Unit : Entity_Id;
4302 Act_Decl : Node_Id)
4303 is
4304 Vis : Boolean;
4305 Gen_Comp : constant Entity_Id :=
4306 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4307 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4308 Curr_Scope : Entity_Id := Empty;
4309 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4310 Removed : Boolean := False;
4311 Num_Scopes : Int := 0;
4312
4313 Scope_Stack_Depth : constant Int :=
4314 Scope_Stack.Last - Scope_Stack.First + 1;
4315
4316 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4317 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4318 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4319 List : Elist_Id;
4320 Num_Inner : Int := 0;
4321 N_Instances : Int := 0;
4322 S : Entity_Id;
4323
4324 begin
4325 -- Case of generic unit defined in another unit. We must remove the
4326 -- complete context of the current unit to install that of the generic.
4327
4328 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4329
4330 -- Add some comments for the following two loops ???
4331
4332 S := Current_Scope;
4333 while Present (S) and then S /= Standard_Standard loop
4334 loop
4335 Num_Scopes := Num_Scopes + 1;
4336
4337 Use_Clauses (Num_Scopes) :=
4338 (Scope_Stack.Table
4339 (Scope_Stack.Last - Num_Scopes + 1).
4340 First_Use_Clause);
4341 End_Use_Clauses (Use_Clauses (Num_Scopes));
4342
4343 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4344 or else Scope_Stack.Table
4345 (Scope_Stack.Last - Num_Scopes).Entity
4346 = Scope (S);
4347 end loop;
4348
4349 exit when Is_Generic_Instance (S)
4350 and then (In_Package_Body (S)
4351 or else Ekind (S) = E_Procedure
4352 or else Ekind (S) = E_Function);
4353 S := Scope (S);
4354 end loop;
4355
4356 Vis := Is_Immediately_Visible (Gen_Comp);
4357
4358 -- Find and save all enclosing instances
4359
4360 S := Current_Scope;
4361
4362 while Present (S)
4363 and then S /= Standard_Standard
4364 loop
4365 if Is_Generic_Instance (S) then
4366 N_Instances := N_Instances + 1;
4367 Instances (N_Instances) := S;
4368
4369 exit when In_Package_Body (S);
4370 end if;
4371
4372 S := Scope (S);
4373 end loop;
4374
4375 -- Remove context of current compilation unit, unless we are within a
4376 -- nested package instantiation, in which case the context has been
4377 -- removed previously.
4378
4379 -- If current scope is the body of a child unit, remove context of
4380 -- spec as well. If an enclosing scope is an instance body, the
4381 -- context has already been removed, but the entities in the body
4382 -- must be made invisible as well.
4383
4384 S := Current_Scope;
4385
4386 while Present (S)
4387 and then S /= Standard_Standard
4388 loop
4389 if Is_Generic_Instance (S)
4390 and then (In_Package_Body (S)
4391 or else Ekind (S) = E_Procedure
4392 or else Ekind (S) = E_Function)
4393 then
4394 -- We still have to remove the entities of the enclosing
4395 -- instance from direct visibility.
4396
4397 declare
4398 E : Entity_Id;
4399 begin
4400 E := First_Entity (S);
4401 while Present (E) loop
4402 Set_Is_Immediately_Visible (E, False);
4403 Next_Entity (E);
4404 end loop;
4405 end;
4406
4407 exit;
4408 end if;
4409
4410 if S = Curr_Unit
4411 or else (Ekind (Curr_Unit) = E_Package_Body
4412 and then S = Spec_Entity (Curr_Unit))
4413 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4414 and then S =
4415 Corresponding_Spec
4416 (Unit_Declaration_Node (Curr_Unit)))
4417 then
4418 Removed := True;
4419
4420 -- Remove entities in current scopes from visibility, so that
4421 -- instance body is compiled in a clean environment.
4422
4423 List := Save_Scope_Stack (Handle_Use => False);
4424
4425 if Is_Child_Unit (S) then
4426
4427 -- Remove child unit from stack, as well as inner scopes.
4428 -- Removing the context of a child unit removes parent units
4429 -- as well.
4430
4431 while Current_Scope /= S loop
4432 Num_Inner := Num_Inner + 1;
4433 Inner_Scopes (Num_Inner) := Current_Scope;
4434 Pop_Scope;
4435 end loop;
4436
4437 Pop_Scope;
4438 Remove_Context (Curr_Comp);
4439 Curr_Scope := S;
4440
4441 else
4442 Remove_Context (Curr_Comp);
4443 end if;
4444
4445 if Ekind (Curr_Unit) = E_Package_Body then
4446 Remove_Context (Library_Unit (Curr_Comp));
4447 end if;
4448 end if;
4449
4450 S := Scope (S);
4451 end loop;
4452 pragma Assert (Num_Inner < Num_Scopes);
4453
4454 Push_Scope (Standard_Standard);
4455 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4456 Instantiate_Package_Body
4457 (Body_Info =>
4458 ((Inst_Node => N,
4459 Act_Decl => Act_Decl,
4460 Expander_Status => Expander_Active,
4461 Current_Sem_Unit => Current_Sem_Unit,
4462 Scope_Suppress => Scope_Suppress,
4463 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4464 Version => Ada_Version,
4465 Version_Pragma => Ada_Version_Pragma,
4466 Warnings => Save_Warnings,
4467 SPARK_Mode => SPARK_Mode,
4468 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4469 Inlined_Body => True);
4470
4471 Pop_Scope;
4472
4473 -- Restore context
4474
4475 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4476
4477 -- Reset Generic_Instance flag so that use clauses can be installed
4478 -- in the proper order. (See Use_One_Package for effect of enclosing
4479 -- instances on processing of use clauses).
4480
4481 for J in 1 .. N_Instances loop
4482 Set_Is_Generic_Instance (Instances (J), False);
4483 end loop;
4484
4485 if Removed then
4486 Install_Context (Curr_Comp);
4487
4488 if Present (Curr_Scope)
4489 and then Is_Child_Unit (Curr_Scope)
4490 then
4491 Push_Scope (Curr_Scope);
4492 Set_Is_Immediately_Visible (Curr_Scope);
4493
4494 -- Finally, restore inner scopes as well
4495
4496 for J in reverse 1 .. Num_Inner loop
4497 Push_Scope (Inner_Scopes (J));
4498 end loop;
4499 end if;
4500
4501 Restore_Scope_Stack (List, Handle_Use => False);
4502
4503 if Present (Curr_Scope)
4504 and then
4505 (In_Private_Part (Curr_Scope)
4506 or else In_Package_Body (Curr_Scope))
4507 then
4508 -- Install private declaration of ancestor units, which are
4509 -- currently available. Restore_Scope_Stack and Install_Context
4510 -- only install the visible part of parents.
4511
4512 declare
4513 Par : Entity_Id;
4514 begin
4515 Par := Scope (Curr_Scope);
4516 while (Present (Par))
4517 and then Par /= Standard_Standard
4518 loop
4519 Install_Private_Declarations (Par);
4520 Par := Scope (Par);
4521 end loop;
4522 end;
4523 end if;
4524 end if;
4525
4526 -- Restore use clauses. For a child unit, use clauses in the parents
4527 -- are restored when installing the context, so only those in inner
4528 -- scopes (and those local to the child unit itself) need to be
4529 -- installed explicitly.
4530
4531 if Is_Child_Unit (Curr_Unit)
4532 and then Removed
4533 then
4534 for J in reverse 1 .. Num_Inner + 1 loop
4535 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4536 Use_Clauses (J);
4537 Install_Use_Clauses (Use_Clauses (J));
4538 end loop;
4539
4540 else
4541 for J in reverse 1 .. Num_Scopes loop
4542 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4543 Use_Clauses (J);
4544 Install_Use_Clauses (Use_Clauses (J));
4545 end loop;
4546 end if;
4547
4548 -- Restore status of instances. If one of them is a body, make its
4549 -- local entities visible again.
4550
4551 declare
4552 E : Entity_Id;
4553 Inst : Entity_Id;
4554
4555 begin
4556 for J in 1 .. N_Instances loop
4557 Inst := Instances (J);
4558 Set_Is_Generic_Instance (Inst, True);
4559
4560 if In_Package_Body (Inst)
4561 or else Ekind (S) = E_Procedure
4562 or else Ekind (S) = E_Function
4563 then
4564 E := First_Entity (Instances (J));
4565 while Present (E) loop
4566 Set_Is_Immediately_Visible (E);
4567 Next_Entity (E);
4568 end loop;
4569 end if;
4570 end loop;
4571 end;
4572
4573 -- If generic unit is in current unit, current context is correct
4574
4575 else
4576 Instantiate_Package_Body
4577 (Body_Info =>
4578 ((Inst_Node => N,
4579 Act_Decl => Act_Decl,
4580 Expander_Status => Expander_Active,
4581 Current_Sem_Unit => Current_Sem_Unit,
4582 Scope_Suppress => Scope_Suppress,
4583 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4584 Version => Ada_Version,
4585 Version_Pragma => Ada_Version_Pragma,
4586 Warnings => Save_Warnings,
4587 SPARK_Mode => SPARK_Mode,
4588 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4589 Inlined_Body => True);
4590 end if;
4591 end Inline_Instance_Body;
4592
4593 -------------------------------------
4594 -- Analyze_Procedure_Instantiation --
4595 -------------------------------------
4596
4597 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4598 begin
4599 Analyze_Subprogram_Instantiation (N, E_Procedure);
4600 end Analyze_Procedure_Instantiation;
4601
4602 -----------------------------------
4603 -- Need_Subprogram_Instance_Body --
4604 -----------------------------------
4605
4606 function Need_Subprogram_Instance_Body
4607 (N : Node_Id;
4608 Subp : Entity_Id) return Boolean
4609 is
4610 begin
4611 -- Must be inlined (or inlined renaming)
4612
4613 if (Is_In_Main_Unit (N)
4614 or else Is_Inlined (Subp)
4615 or else Is_Inlined (Alias (Subp)))
4616
4617 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4618
4619 and then (Operating_Mode = Generate_Code
4620 or else (Operating_Mode = Check_Semantics
4621 and then (ASIS_Mode or GNATprove_Mode)))
4622
4623 -- The body is needed when generating code (full expansion), in ASIS
4624 -- mode for other tools, and in GNATprove mode (special expansion) for
4625 -- formal verification of the body itself.
4626
4627 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4628
4629 -- No point in inlining if ABE is inevitable
4630
4631 and then not ABE_Is_Certain (N)
4632
4633 -- Or if subprogram is eliminated
4634
4635 and then not Is_Eliminated (Subp)
4636 then
4637 Pending_Instantiations.Append
4638 ((Inst_Node => N,
4639 Act_Decl => Unit_Declaration_Node (Subp),
4640 Expander_Status => Expander_Active,
4641 Current_Sem_Unit => Current_Sem_Unit,
4642 Scope_Suppress => Scope_Suppress,
4643 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4644 Version => Ada_Version,
4645 Version_Pragma => Ada_Version_Pragma,
4646 Warnings => Save_Warnings,
4647 SPARK_Mode => SPARK_Mode,
4648 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4649 return True;
4650
4651 -- Here if not inlined, or we ignore the inlining
4652
4653 else
4654 return False;
4655 end if;
4656 end Need_Subprogram_Instance_Body;
4657
4658 --------------------------------------
4659 -- Analyze_Subprogram_Instantiation --
4660 --------------------------------------
4661
4662 procedure Analyze_Subprogram_Instantiation
4663 (N : Node_Id;
4664 K : Entity_Kind)
4665 is
4666 Loc : constant Source_Ptr := Sloc (N);
4667 Gen_Id : constant Node_Id := Name (N);
4668
4669 Anon_Id : constant Entity_Id :=
4670 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4671 Chars => New_External_Name
4672 (Chars (Defining_Entity (N)), 'R'));
4673
4674 Act_Decl_Id : Entity_Id;
4675 Act_Decl : Node_Id;
4676 Act_Spec : Node_Id;
4677 Act_Tree : Node_Id;
4678
4679 Env_Installed : Boolean := False;
4680 Gen_Unit : Entity_Id;
4681 Gen_Decl : Node_Id;
4682 Pack_Id : Entity_Id;
4683 Parent_Installed : Boolean := False;
4684 Renaming_List : List_Id;
4685
4686 procedure Analyze_Instance_And_Renamings;
4687 -- The instance must be analyzed in a context that includes the mappings
4688 -- of generic parameters into actuals. We create a package declaration
4689 -- for this purpose, and a subprogram with an internal name within the
4690 -- package. The subprogram instance is simply an alias for the internal
4691 -- subprogram, declared in the current scope.
4692
4693 ------------------------------------
4694 -- Analyze_Instance_And_Renamings --
4695 ------------------------------------
4696
4697 procedure Analyze_Instance_And_Renamings is
4698 Def_Ent : constant Entity_Id := Defining_Entity (N);
4699 Pack_Decl : Node_Id;
4700
4701 begin
4702 if Nkind (Parent (N)) = N_Compilation_Unit then
4703
4704 -- For the case of a compilation unit, the container package has
4705 -- the same name as the instantiation, to insure that the binder
4706 -- calls the elaboration procedure with the right name. Copy the
4707 -- entity of the instance, which may have compilation level flags
4708 -- (e.g. Is_Child_Unit) set.
4709
4710 Pack_Id := New_Copy (Def_Ent);
4711
4712 else
4713 -- Otherwise we use the name of the instantiation concatenated
4714 -- with its source position to ensure uniqueness if there are
4715 -- several instantiations with the same name.
4716
4717 Pack_Id :=
4718 Make_Defining_Identifier (Loc,
4719 Chars => New_External_Name
4720 (Related_Id => Chars (Def_Ent),
4721 Suffix => "GP",
4722 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4723 end if;
4724
4725 Pack_Decl := Make_Package_Declaration (Loc,
4726 Specification => Make_Package_Specification (Loc,
4727 Defining_Unit_Name => Pack_Id,
4728 Visible_Declarations => Renaming_List,
4729 End_Label => Empty));
4730
4731 Set_Instance_Spec (N, Pack_Decl);
4732 Set_Is_Generic_Instance (Pack_Id);
4733 Set_Debug_Info_Needed (Pack_Id);
4734
4735 -- Case of not a compilation unit
4736
4737 if Nkind (Parent (N)) /= N_Compilation_Unit then
4738 Mark_Rewrite_Insertion (Pack_Decl);
4739 Insert_Before (N, Pack_Decl);
4740 Set_Has_Completion (Pack_Id);
4741
4742 -- Case of an instantiation that is a compilation unit
4743
4744 -- Place declaration on current node so context is complete for
4745 -- analysis (including nested instantiations), and for use in a
4746 -- context_clause (see Analyze_With_Clause).
4747
4748 else
4749 Set_Unit (Parent (N), Pack_Decl);
4750 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4751 end if;
4752
4753 Analyze (Pack_Decl);
4754 Check_Formal_Packages (Pack_Id);
4755 Set_Is_Generic_Instance (Pack_Id, False);
4756
4757 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4758 -- above???
4759
4760 -- Body of the enclosing package is supplied when instantiating the
4761 -- subprogram body, after semantic analysis is completed.
4762
4763 if Nkind (Parent (N)) = N_Compilation_Unit then
4764
4765 -- Remove package itself from visibility, so it does not
4766 -- conflict with subprogram.
4767
4768 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4769
4770 -- Set name and scope of internal subprogram so that the proper
4771 -- external name will be generated. The proper scope is the scope
4772 -- of the wrapper package. We need to generate debugging info for
4773 -- the internal subprogram, so set flag accordingly.
4774
4775 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4776 Set_Scope (Anon_Id, Scope (Pack_Id));
4777
4778 -- Mark wrapper package as referenced, to avoid spurious warnings
4779 -- if the instantiation appears in various with_ clauses of
4780 -- subunits of the main unit.
4781
4782 Set_Referenced (Pack_Id);
4783 end if;
4784
4785 Set_Is_Generic_Instance (Anon_Id);
4786 Set_Debug_Info_Needed (Anon_Id);
4787 Act_Decl_Id := New_Copy (Anon_Id);
4788
4789 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4790 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4791 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4792 Set_Comes_From_Source (Act_Decl_Id, True);
4793
4794 -- The signature may involve types that are not frozen yet, but the
4795 -- subprogram will be frozen at the point the wrapper package is
4796 -- frozen, so it does not need its own freeze node. In fact, if one
4797 -- is created, it might conflict with the freezing actions from the
4798 -- wrapper package.
4799
4800 Set_Has_Delayed_Freeze (Anon_Id, False);
4801
4802 -- If the instance is a child unit, mark the Id accordingly. Mark
4803 -- the anonymous entity as well, which is the real subprogram and
4804 -- which is used when the instance appears in a context clause.
4805 -- Similarly, propagate the Is_Eliminated flag to handle properly
4806 -- nested eliminated subprograms.
4807
4808 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4809 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4810 New_Overloaded_Entity (Act_Decl_Id);
4811 Check_Eliminated (Act_Decl_Id);
4812 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4813
4814 -- In compilation unit case, kill elaboration checks on the
4815 -- instantiation, since they are never needed -- the body is
4816 -- instantiated at the same point as the spec.
4817
4818 if Nkind (Parent (N)) = N_Compilation_Unit then
4819 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4820 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4821 Set_Is_Compilation_Unit (Anon_Id);
4822
4823 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4824 end if;
4825
4826 -- The instance is not a freezing point for the new subprogram
4827
4828 Set_Is_Frozen (Act_Decl_Id, False);
4829
4830 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4831 Valid_Operator_Definition (Act_Decl_Id);
4832 end if;
4833
4834 Set_Alias (Act_Decl_Id, Anon_Id);
4835 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4836 Set_Has_Completion (Act_Decl_Id);
4837 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4838
4839 if Nkind (Parent (N)) = N_Compilation_Unit then
4840 Set_Body_Required (Parent (N), False);
4841 end if;
4842 end Analyze_Instance_And_Renamings;
4843
4844 -- Local variables
4845
4846 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
4847 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
4848
4849 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4850 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4851 -- Save the SPARK_Mode-related data for restore on exit
4852
4853 Vis_Prims_List : Elist_Id := No_Elist;
4854 -- List of primitives made temporarily visible in the instantiation
4855 -- to match the visibility of the formal type
4856
4857 -- Start of processing for Analyze_Subprogram_Instantiation
4858
4859 begin
4860 Check_SPARK_05_Restriction ("generic is not allowed", N);
4861
4862 -- Very first thing: check for special Text_IO unit in case we are
4863 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
4864 -- such an instantiation is bogus (these are packages, not subprograms),
4865 -- but we get a better error message if we do this.
4866
4867 Check_Text_IO_Special_Unit (Gen_Id);
4868
4869 -- Make node global for error reporting
4870
4871 Instantiation_Node := N;
4872
4873 -- For package instantiations we turn off style checks, because they
4874 -- will have been emitted in the generic. For subprogram instantiations
4875 -- we want to apply at least the check on overriding indicators so we
4876 -- do not modify the style check status.
4877
4878 -- The renaming declarations for the actuals do not come from source and
4879 -- will not generate spurious warnings.
4880
4881 Preanalyze_Actuals (N);
4882
4883 Init_Env;
4884 Env_Installed := True;
4885 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4886 Gen_Unit := Entity (Gen_Id);
4887
4888 Generate_Reference (Gen_Unit, Gen_Id);
4889
4890 if Nkind (Gen_Id) = N_Identifier
4891 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4892 then
4893 Error_Msg_NE
4894 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4895 end if;
4896
4897 if Etype (Gen_Unit) = Any_Type then
4898 Restore_Env;
4899 return;
4900 end if;
4901
4902 -- Verify that it is a generic subprogram of the right kind, and that
4903 -- it does not lead to a circular instantiation.
4904
4905 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
4906 Error_Msg_NE
4907 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
4908
4909 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
4910 Error_Msg_NE
4911 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
4912
4913 elsif In_Open_Scopes (Gen_Unit) then
4914 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4915
4916 else
4917 -- If the context of the instance is subject to SPARK_Mode "off",
4918 -- set the global flag which signals Analyze_Pragma to ignore all
4919 -- SPARK_Mode pragmas within the instance.
4920
4921 if SPARK_Mode = Off then
4922 Ignore_Pragma_SPARK_Mode := True;
4923 end if;
4924
4925 Set_Entity (Gen_Id, Gen_Unit);
4926 Set_Is_Instantiated (Gen_Unit);
4927
4928 if In_Extended_Main_Source_Unit (N) then
4929 Generate_Reference (Gen_Unit, N);
4930 end if;
4931
4932 -- If renaming, get original unit
4933
4934 if Present (Renamed_Object (Gen_Unit))
4935 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4936 or else
4937 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4938 then
4939 Gen_Unit := Renamed_Object (Gen_Unit);
4940 Set_Is_Instantiated (Gen_Unit);
4941 Generate_Reference (Gen_Unit, N);
4942 end if;
4943
4944 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4945 Error_Msg_Node_2 := Current_Scope;
4946 Error_Msg_NE
4947 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4948 Circularity_Detected := True;
4949 Restore_Hidden_Primitives (Vis_Prims_List);
4950 goto Leave;
4951 end if;
4952
4953 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4954
4955 -- Initialize renamings map, for error checking
4956
4957 Generic_Renamings.Set_Last (0);
4958 Generic_Renamings_HTable.Reset;
4959
4960 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4961
4962 -- Copy original generic tree, to produce text for instantiation
4963
4964 Act_Tree :=
4965 Copy_Generic_Node
4966 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4967
4968 -- Inherit overriding indicator from instance node
4969
4970 Act_Spec := Specification (Act_Tree);
4971 Set_Must_Override (Act_Spec, Must_Override (N));
4972 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4973
4974 Renaming_List :=
4975 Analyze_Associations
4976 (I_Node => N,
4977 Formals => Generic_Formal_Declarations (Act_Tree),
4978 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4979
4980 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4981
4982 -- The subprogram itself cannot contain a nested instance, so the
4983 -- current parent is left empty.
4984
4985 Set_Instance_Env (Gen_Unit, Empty);
4986
4987 -- Build the subprogram declaration, which does not appear in the
4988 -- generic template, and give it a sloc consistent with that of the
4989 -- template.
4990
4991 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4992 Set_Generic_Parent (Act_Spec, Gen_Unit);
4993 Act_Decl :=
4994 Make_Subprogram_Declaration (Sloc (Act_Spec),
4995 Specification => Act_Spec);
4996
4997 -- The aspects have been copied previously, but they have to be
4998 -- linked explicitly to the new subprogram declaration. Explicit
4999 -- pre/postconditions on the instance are analyzed below, in a
5000 -- separate step.
5001
5002 Move_Aspects (Act_Tree, To => Act_Decl);
5003 Set_Categorization_From_Pragmas (Act_Decl);
5004
5005 if Parent_Installed then
5006 Hide_Current_Scope;
5007 end if;
5008
5009 Append (Act_Decl, Renaming_List);
5010 Analyze_Instance_And_Renamings;
5011
5012 -- If the generic is marked Import (Intrinsic), then so is the
5013 -- instance. This indicates that there is no body to instantiate. If
5014 -- generic is marked inline, so it the instance, and the anonymous
5015 -- subprogram it renames. If inlined, or else if inlining is enabled
5016 -- for the compilation, we generate the instance body even if it is
5017 -- not within the main unit.
5018
5019 if Is_Intrinsic_Subprogram (Gen_Unit) then
5020 Set_Is_Intrinsic_Subprogram (Anon_Id);
5021 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5022
5023 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5024 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5025 end if;
5026 end if;
5027
5028 -- Inherit convention from generic unit. Intrinsic convention, as for
5029 -- an instance of unchecked conversion, is not inherited because an
5030 -- explicit Ada instance has been created.
5031
5032 if Has_Convention_Pragma (Gen_Unit)
5033 and then Convention (Gen_Unit) /= Convention_Intrinsic
5034 then
5035 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5036 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5037 end if;
5038
5039 Generate_Definition (Act_Decl_Id);
5040 -- Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id)));
5041 -- ??? needed?
5042 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
5043
5044 -- Inherit all inlining-related flags which apply to the generic in
5045 -- the subprogram and its declaration.
5046
5047 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5048 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5049
5050 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5051 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5052
5053 Set_Has_Pragma_Inline_Always
5054 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5055 Set_Has_Pragma_Inline_Always
5056 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5057
5058 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5059 Check_Elab_Instantiation (N);
5060 end if;
5061
5062 if Is_Dispatching_Operation (Act_Decl_Id)
5063 and then Ada_Version >= Ada_2005
5064 then
5065 declare
5066 Formal : Entity_Id;
5067
5068 begin
5069 Formal := First_Formal (Act_Decl_Id);
5070 while Present (Formal) loop
5071 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5072 and then Is_Controlling_Formal (Formal)
5073 and then not Can_Never_Be_Null (Formal)
5074 then
5075 Error_Msg_NE ("access parameter& is controlling,",
5076 N, Formal);
5077 Error_Msg_NE
5078 ("\corresponding parameter of & must be"
5079 & " explicitly null-excluding", N, Gen_Id);
5080 end if;
5081
5082 Next_Formal (Formal);
5083 end loop;
5084 end;
5085 end if;
5086
5087 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5088
5089 Validate_Categorization_Dependency (N, Act_Decl_Id);
5090
5091 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5092 Inherit_Context (Gen_Decl, N);
5093
5094 Restore_Private_Views (Pack_Id, False);
5095
5096 -- If the context requires a full instantiation, mark node for
5097 -- subsequent construction of the body.
5098
5099 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5100 Check_Forward_Instantiation (Gen_Decl);
5101
5102 -- The wrapper package is always delayed, because it does not
5103 -- constitute a freeze point, but to insure that the freeze
5104 -- node is placed properly, it is created directly when
5105 -- instantiating the body (otherwise the freeze node might
5106 -- appear to early for nested instantiations).
5107
5108 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5109
5110 -- For ASIS purposes, indicate that the wrapper package has
5111 -- replaced the instantiation node.
5112
5113 Rewrite (N, Unit (Parent (N)));
5114 Set_Unit (Parent (N), N);
5115 end if;
5116
5117 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5118
5119 -- Replace instance node for library-level instantiations of
5120 -- intrinsic subprograms, for ASIS use.
5121
5122 Rewrite (N, Unit (Parent (N)));
5123 Set_Unit (Parent (N), N);
5124 end if;
5125
5126 if Parent_Installed then
5127 Remove_Parent;
5128 end if;
5129
5130 Restore_Hidden_Primitives (Vis_Prims_List);
5131 Restore_Env;
5132 Env_Installed := False;
5133 Generic_Renamings.Set_Last (0);
5134 Generic_Renamings_HTable.Reset;
5135
5136 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5137 SPARK_Mode := Save_SM;
5138 SPARK_Mode_Pragma := Save_SMP;
5139 end if;
5140
5141 <<Leave>>
5142 if Has_Aspects (N) then
5143 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5144 end if;
5145
5146 exception
5147 when Instantiation_Error =>
5148 if Parent_Installed then
5149 Remove_Parent;
5150 end if;
5151
5152 if Env_Installed then
5153 Restore_Env;
5154 end if;
5155
5156 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5157 SPARK_Mode := Save_SM;
5158 SPARK_Mode_Pragma := Save_SMP;
5159 end Analyze_Subprogram_Instantiation;
5160
5161 -------------------------
5162 -- Get_Associated_Node --
5163 -------------------------
5164
5165 function Get_Associated_Node (N : Node_Id) return Node_Id is
5166 Assoc : Node_Id;
5167
5168 begin
5169 Assoc := Associated_Node (N);
5170
5171 if Nkind (Assoc) /= Nkind (N) then
5172 return Assoc;
5173
5174 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5175 return Assoc;
5176
5177 else
5178 -- If the node is part of an inner generic, it may itself have been
5179 -- remapped into a further generic copy. Associated_Node is otherwise
5180 -- used for the entity of the node, and will be of a different node
5181 -- kind, or else N has been rewritten as a literal or function call.
5182
5183 while Present (Associated_Node (Assoc))
5184 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5185 loop
5186 Assoc := Associated_Node (Assoc);
5187 end loop;
5188
5189 -- Follow and additional link in case the final node was rewritten.
5190 -- This can only happen with nested generic units.
5191
5192 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5193 and then Present (Associated_Node (Assoc))
5194 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5195 N_Explicit_Dereference,
5196 N_Integer_Literal,
5197 N_Real_Literal,
5198 N_String_Literal))
5199 then
5200 Assoc := Associated_Node (Assoc);
5201 end if;
5202
5203 -- An additional special case: an unconstrained type in an object
5204 -- declaration may have been rewritten as a local subtype constrained
5205 -- by the expression in the declaration. We need to recover the
5206 -- original entity which may be global.
5207
5208 if Present (Original_Node (Assoc))
5209 and then Nkind (Parent (N)) = N_Object_Declaration
5210 then
5211 Assoc := Original_Node (Assoc);
5212 end if;
5213
5214 return Assoc;
5215 end if;
5216 end Get_Associated_Node;
5217
5218 -------------------------------------------
5219 -- Build_Instance_Compilation_Unit_Nodes --
5220 -------------------------------------------
5221
5222 procedure Build_Instance_Compilation_Unit_Nodes
5223 (N : Node_Id;
5224 Act_Body : Node_Id;
5225 Act_Decl : Node_Id)
5226 is
5227 Decl_Cunit : Node_Id;
5228 Body_Cunit : Node_Id;
5229 Citem : Node_Id;
5230 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5231 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5232
5233 begin
5234 -- A new compilation unit node is built for the instance declaration
5235
5236 Decl_Cunit :=
5237 Make_Compilation_Unit (Sloc (N),
5238 Context_Items => Empty_List,
5239 Unit => Act_Decl,
5240 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5241
5242 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5243
5244 -- The new compilation unit is linked to its body, but both share the
5245 -- same file, so we do not set Body_Required on the new unit so as not
5246 -- to create a spurious dependency on a non-existent body in the ali.
5247 -- This simplifies CodePeer unit traversal.
5248
5249 -- We use the original instantiation compilation unit as the resulting
5250 -- compilation unit of the instance, since this is the main unit.
5251
5252 Rewrite (N, Act_Body);
5253
5254 -- Propagate the aspect specifications from the package body template to
5255 -- the instantiated version of the package body.
5256
5257 if Has_Aspects (Act_Body) then
5258 Set_Aspect_Specifications
5259 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5260 end if;
5261
5262 Body_Cunit := Parent (N);
5263
5264 -- The two compilation unit nodes are linked by the Library_Unit field
5265
5266 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5267 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5268
5269 -- Preserve the private nature of the package if needed
5270
5271 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5272
5273 -- If the instance is not the main unit, its context, categorization
5274 -- and elaboration entity are not relevant to the compilation.
5275
5276 if Body_Cunit /= Cunit (Main_Unit) then
5277 Make_Instance_Unit (Body_Cunit, In_Main => False);
5278 return;
5279 end if;
5280
5281 -- The context clause items on the instantiation, which are now attached
5282 -- to the body compilation unit (since the body overwrote the original
5283 -- instantiation node), semantically belong on the spec, so copy them
5284 -- there. It's harmless to leave them on the body as well. In fact one
5285 -- could argue that they belong in both places.
5286
5287 Citem := First (Context_Items (Body_Cunit));
5288 while Present (Citem) loop
5289 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5290 Next (Citem);
5291 end loop;
5292
5293 -- Propagate categorization flags on packages, so that they appear in
5294 -- the ali file for the spec of the unit.
5295
5296 if Ekind (New_Main) = E_Package then
5297 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5298 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5299 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5300 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5301 Set_Is_Remote_Call_Interface
5302 (Old_Main, Is_Remote_Call_Interface (New_Main));
5303 end if;
5304
5305 -- Make entry in Units table, so that binder can generate call to
5306 -- elaboration procedure for body, if any.
5307
5308 Make_Instance_Unit (Body_Cunit, In_Main => True);
5309 Main_Unit_Entity := New_Main;
5310 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5311
5312 -- Build elaboration entity, since the instance may certainly generate
5313 -- elaboration code requiring a flag for protection.
5314
5315 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5316 end Build_Instance_Compilation_Unit_Nodes;
5317
5318 -----------------------------
5319 -- Check_Access_Definition --
5320 -----------------------------
5321
5322 procedure Check_Access_Definition (N : Node_Id) is
5323 begin
5324 pragma Assert
5325 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5326 null;
5327 end Check_Access_Definition;
5328
5329 -----------------------------------
5330 -- Check_Formal_Package_Instance --
5331 -----------------------------------
5332
5333 -- If the formal has specific parameters, they must match those of the
5334 -- actual. Both of them are instances, and the renaming declarations for
5335 -- their formal parameters appear in the same order in both. The analyzed
5336 -- formal has been analyzed in the context of the current instance.
5337
5338 procedure Check_Formal_Package_Instance
5339 (Formal_Pack : Entity_Id;
5340 Actual_Pack : Entity_Id)
5341 is
5342 E1 : Entity_Id := First_Entity (Actual_Pack);
5343 E2 : Entity_Id := First_Entity (Formal_Pack);
5344
5345 Expr1 : Node_Id;
5346 Expr2 : Node_Id;
5347
5348 procedure Check_Mismatch (B : Boolean);
5349 -- Common error routine for mismatch between the parameters of the
5350 -- actual instance and those of the formal package.
5351
5352 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5353 -- The formal may come from a nested formal package, and the actual may
5354 -- have been constant-folded. To determine whether the two denote the
5355 -- same entity we may have to traverse several definitions to recover
5356 -- the ultimate entity that they refer to.
5357
5358 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5359 -- Similarly, if the formal comes from a nested formal package, the
5360 -- actual may designate the formal through multiple renamings, which
5361 -- have to be followed to determine the original variable in question.
5362
5363 --------------------
5364 -- Check_Mismatch --
5365 --------------------
5366
5367 procedure Check_Mismatch (B : Boolean) is
5368 Kind : constant Node_Kind := Nkind (Parent (E2));
5369
5370 begin
5371 if Kind = N_Formal_Type_Declaration then
5372 return;
5373
5374 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5375 N_Formal_Package_Declaration)
5376 or else Kind in N_Formal_Subprogram_Declaration
5377 then
5378 null;
5379
5380 elsif B then
5381 Error_Msg_NE
5382 ("actual for & in actual instance does not match formal",
5383 Parent (Actual_Pack), E1);
5384 end if;
5385 end Check_Mismatch;
5386
5387 --------------------------------
5388 -- Same_Instantiated_Constant --
5389 --------------------------------
5390
5391 function Same_Instantiated_Constant
5392 (E1, E2 : Entity_Id) return Boolean
5393 is
5394 Ent : Entity_Id;
5395
5396 begin
5397 Ent := E2;
5398 while Present (Ent) loop
5399 if E1 = Ent then
5400 return True;
5401
5402 elsif Ekind (Ent) /= E_Constant then
5403 return False;
5404
5405 elsif Is_Entity_Name (Constant_Value (Ent)) then
5406 if Entity (Constant_Value (Ent)) = E1 then
5407 return True;
5408 else
5409 Ent := Entity (Constant_Value (Ent));
5410 end if;
5411
5412 -- The actual may be a constant that has been folded. Recover
5413 -- original name.
5414
5415 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5416 Ent := Entity (Original_Node (Constant_Value (Ent)));
5417 else
5418 return False;
5419 end if;
5420 end loop;
5421
5422 return False;
5423 end Same_Instantiated_Constant;
5424
5425 --------------------------------
5426 -- Same_Instantiated_Variable --
5427 --------------------------------
5428
5429 function Same_Instantiated_Variable
5430 (E1, E2 : Entity_Id) return Boolean
5431 is
5432 function Original_Entity (E : Entity_Id) return Entity_Id;
5433 -- Follow chain of renamings to the ultimate ancestor
5434
5435 ---------------------
5436 -- Original_Entity --
5437 ---------------------
5438
5439 function Original_Entity (E : Entity_Id) return Entity_Id is
5440 Orig : Entity_Id;
5441
5442 begin
5443 Orig := E;
5444 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5445 and then Present (Renamed_Object (Orig))
5446 and then Is_Entity_Name (Renamed_Object (Orig))
5447 loop
5448 Orig := Entity (Renamed_Object (Orig));
5449 end loop;
5450
5451 return Orig;
5452 end Original_Entity;
5453
5454 -- Start of processing for Same_Instantiated_Variable
5455
5456 begin
5457 return Ekind (E1) = Ekind (E2)
5458 and then Original_Entity (E1) = Original_Entity (E2);
5459 end Same_Instantiated_Variable;
5460
5461 -- Start of processing for Check_Formal_Package_Instance
5462
5463 begin
5464 while Present (E1)
5465 and then Present (E2)
5466 loop
5467 exit when Ekind (E1) = E_Package
5468 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5469
5470 -- If the formal is the renaming of the formal package, this
5471 -- is the end of its formal part, which may occur before the
5472 -- end of the formal part in the actual in the presence of
5473 -- defaulted parameters in the formal package.
5474
5475 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5476 and then Renamed_Entity (E2) = Scope (E2);
5477
5478 -- The analysis of the actual may generate additional internal
5479 -- entities. If the formal is defaulted, there is no corresponding
5480 -- analysis and the internal entities must be skipped, until we
5481 -- find corresponding entities again.
5482
5483 if Comes_From_Source (E2)
5484 and then not Comes_From_Source (E1)
5485 and then Chars (E1) /= Chars (E2)
5486 then
5487 while Present (E1)
5488 and then Chars (E1) /= Chars (E2)
5489 loop
5490 Next_Entity (E1);
5491 end loop;
5492 end if;
5493
5494 if No (E1) then
5495 return;
5496
5497 -- If the formal entity comes from a formal declaration, it was
5498 -- defaulted in the formal package, and no check is needed on it.
5499
5500 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5501 goto Next_E;
5502
5503 -- Ditto for defaulted formal subprograms.
5504
5505 elsif Is_Overloadable (E1)
5506 and then Nkind (Unit_Declaration_Node (E2)) in
5507 N_Formal_Subprogram_Declaration
5508 then
5509 goto Next_E;
5510
5511 elsif Is_Type (E1) then
5512
5513 -- Subtypes must statically match. E1, E2 are the local entities
5514 -- that are subtypes of the actuals. Itypes generated for other
5515 -- parameters need not be checked, the check will be performed
5516 -- on the parameters themselves.
5517
5518 -- If E2 is a formal type declaration, it is a defaulted parameter
5519 -- and needs no checking.
5520
5521 if not Is_Itype (E1)
5522 and then not Is_Itype (E2)
5523 then
5524 Check_Mismatch
5525 (not Is_Type (E2)
5526 or else Etype (E1) /= Etype (E2)
5527 or else not Subtypes_Statically_Match (E1, E2));
5528 end if;
5529
5530 elsif Ekind (E1) = E_Constant then
5531
5532 -- IN parameters must denote the same static value, or the same
5533 -- constant, or the literal null.
5534
5535 Expr1 := Expression (Parent (E1));
5536
5537 if Ekind (E2) /= E_Constant then
5538 Check_Mismatch (True);
5539 goto Next_E;
5540 else
5541 Expr2 := Expression (Parent (E2));
5542 end if;
5543
5544 if Is_OK_Static_Expression (Expr1) then
5545 if not Is_OK_Static_Expression (Expr2) then
5546 Check_Mismatch (True);
5547
5548 elsif Is_Discrete_Type (Etype (E1)) then
5549 declare
5550 V1 : constant Uint := Expr_Value (Expr1);
5551 V2 : constant Uint := Expr_Value (Expr2);
5552 begin
5553 Check_Mismatch (V1 /= V2);
5554 end;
5555
5556 elsif Is_Real_Type (Etype (E1)) then
5557 declare
5558 V1 : constant Ureal := Expr_Value_R (Expr1);
5559 V2 : constant Ureal := Expr_Value_R (Expr2);
5560 begin
5561 Check_Mismatch (V1 /= V2);
5562 end;
5563
5564 elsif Is_String_Type (Etype (E1))
5565 and then Nkind (Expr1) = N_String_Literal
5566 then
5567 if Nkind (Expr2) /= N_String_Literal then
5568 Check_Mismatch (True);
5569 else
5570 Check_Mismatch
5571 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5572 end if;
5573 end if;
5574
5575 elsif Is_Entity_Name (Expr1) then
5576 if Is_Entity_Name (Expr2) then
5577 if Entity (Expr1) = Entity (Expr2) then
5578 null;
5579 else
5580 Check_Mismatch
5581 (not Same_Instantiated_Constant
5582 (Entity (Expr1), Entity (Expr2)));
5583 end if;
5584 else
5585 Check_Mismatch (True);
5586 end if;
5587
5588 elsif Is_Entity_Name (Original_Node (Expr1))
5589 and then Is_Entity_Name (Expr2)
5590 and then
5591 Same_Instantiated_Constant
5592 (Entity (Original_Node (Expr1)), Entity (Expr2))
5593 then
5594 null;
5595
5596 elsif Nkind (Expr1) = N_Null then
5597 Check_Mismatch (Nkind (Expr1) /= N_Null);
5598
5599 else
5600 Check_Mismatch (True);
5601 end if;
5602
5603 elsif Ekind (E1) = E_Variable then
5604 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5605
5606 elsif Ekind (E1) = E_Package then
5607 Check_Mismatch
5608 (Ekind (E1) /= Ekind (E2)
5609 or else Renamed_Object (E1) /= Renamed_Object (E2));
5610
5611 elsif Is_Overloadable (E1) then
5612
5613 -- Verify that the actual subprograms match. Note that actuals
5614 -- that are attributes are rewritten as subprograms. If the
5615 -- subprogram in the formal package is defaulted, no check is
5616 -- needed. Note that this can only happen in Ada 2005 when the
5617 -- formal package can be partially parameterized.
5618
5619 if Nkind (Unit_Declaration_Node (E1)) =
5620 N_Subprogram_Renaming_Declaration
5621 and then From_Default (Unit_Declaration_Node (E1))
5622 then
5623 null;
5624
5625 -- If the formal package has an "others" box association that
5626 -- covers this formal, there is no need for a check either.
5627
5628 elsif Nkind (Unit_Declaration_Node (E2)) in
5629 N_Formal_Subprogram_Declaration
5630 and then Box_Present (Unit_Declaration_Node (E2))
5631 then
5632 null;
5633
5634 -- No check needed if subprogram is a defaulted null procedure
5635
5636 elsif No (Alias (E2))
5637 and then Ekind (E2) = E_Procedure
5638 and then
5639 Null_Present (Specification (Unit_Declaration_Node (E2)))
5640 then
5641 null;
5642
5643 -- Otherwise the actual in the formal and the actual in the
5644 -- instantiation of the formal must match, up to renamings.
5645
5646 else
5647 Check_Mismatch
5648 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5649 end if;
5650
5651 else
5652 raise Program_Error;
5653 end if;
5654
5655 <<Next_E>>
5656 Next_Entity (E1);
5657 Next_Entity (E2);
5658 end loop;
5659 end Check_Formal_Package_Instance;
5660
5661 ---------------------------
5662 -- Check_Formal_Packages --
5663 ---------------------------
5664
5665 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5666 E : Entity_Id;
5667 Formal_P : Entity_Id;
5668
5669 begin
5670 -- Iterate through the declarations in the instance, looking for package
5671 -- renaming declarations that denote instances of formal packages. Stop
5672 -- when we find the renaming of the current package itself. The
5673 -- declaration for a formal package without a box is followed by an
5674 -- internal entity that repeats the instantiation.
5675
5676 E := First_Entity (P_Id);
5677 while Present (E) loop
5678 if Ekind (E) = E_Package then
5679 if Renamed_Object (E) = P_Id then
5680 exit;
5681
5682 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5683 null;
5684
5685 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5686 Formal_P := Next_Entity (E);
5687 Check_Formal_Package_Instance (Formal_P, E);
5688
5689 -- After checking, remove the internal validating package. It
5690 -- is only needed for semantic checks, and as it may contain
5691 -- generic formal declarations it should not reach gigi.
5692
5693 Remove (Unit_Declaration_Node (Formal_P));
5694 end if;
5695 end if;
5696
5697 Next_Entity (E);
5698 end loop;
5699 end Check_Formal_Packages;
5700
5701 ---------------------------------
5702 -- Check_Forward_Instantiation --
5703 ---------------------------------
5704
5705 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5706 S : Entity_Id;
5707 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5708
5709 begin
5710 -- The instantiation appears before the generic body if we are in the
5711 -- scope of the unit containing the generic, either in its spec or in
5712 -- the package body, and before the generic body.
5713
5714 if Ekind (Gen_Comp) = E_Package_Body then
5715 Gen_Comp := Spec_Entity (Gen_Comp);
5716 end if;
5717
5718 if In_Open_Scopes (Gen_Comp)
5719 and then No (Corresponding_Body (Decl))
5720 then
5721 S := Current_Scope;
5722
5723 while Present (S)
5724 and then not Is_Compilation_Unit (S)
5725 and then not Is_Child_Unit (S)
5726 loop
5727 if Ekind (S) = E_Package then
5728 Set_Has_Forward_Instantiation (S);
5729 end if;
5730
5731 S := Scope (S);
5732 end loop;
5733 end if;
5734 end Check_Forward_Instantiation;
5735
5736 ---------------------------
5737 -- Check_Generic_Actuals --
5738 ---------------------------
5739
5740 -- The visibility of the actuals may be different between the point of
5741 -- generic instantiation and the instantiation of the body.
5742
5743 procedure Check_Generic_Actuals
5744 (Instance : Entity_Id;
5745 Is_Formal_Box : Boolean)
5746 is
5747 E : Entity_Id;
5748 Astype : Entity_Id;
5749
5750 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5751 -- For a formal that is an array type, the component type is often a
5752 -- previous formal in the same unit. The privacy status of the component
5753 -- type will have been examined earlier in the traversal of the
5754 -- corresponding actuals, and this status should not be modified for
5755 -- the array (sub)type itself. However, if the base type of the array
5756 -- (sub)type is private, its full view must be restored in the body to
5757 -- be consistent with subsequent index subtypes, etc.
5758 --
5759 -- To detect this case we have to rescan the list of formals, which is
5760 -- usually short enough to ignore the resulting inefficiency.
5761
5762 -----------------------------
5763 -- Denotes_Previous_Actual --
5764 -----------------------------
5765
5766 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5767 Prev : Entity_Id;
5768
5769 begin
5770 Prev := First_Entity (Instance);
5771 while Present (Prev) loop
5772 if Is_Type (Prev)
5773 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5774 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5775 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5776 then
5777 return True;
5778
5779 elsif Prev = E then
5780 return False;
5781
5782 else
5783 Next_Entity (Prev);
5784 end if;
5785 end loop;
5786
5787 return False;
5788 end Denotes_Previous_Actual;
5789
5790 -- Start of processing for Check_Generic_Actuals
5791
5792 begin
5793 E := First_Entity (Instance);
5794 while Present (E) loop
5795 if Is_Type (E)
5796 and then Nkind (Parent (E)) = N_Subtype_Declaration
5797 and then Scope (Etype (E)) /= Instance
5798 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5799 then
5800 if Is_Array_Type (E)
5801 and then not Is_Private_Type (Etype (E))
5802 and then Denotes_Previous_Actual (Component_Type (E))
5803 then
5804 null;
5805 else
5806 Check_Private_View (Subtype_Indication (Parent (E)));
5807 end if;
5808
5809 Set_Is_Generic_Actual_Type (E, True);
5810 Set_Is_Hidden (E, False);
5811 Set_Is_Potentially_Use_Visible (E,
5812 In_Use (Instance));
5813
5814 -- We constructed the generic actual type as a subtype of the
5815 -- supplied type. This means that it normally would not inherit
5816 -- subtype specific attributes of the actual, which is wrong for
5817 -- the generic case.
5818
5819 Astype := Ancestor_Subtype (E);
5820
5821 if No (Astype) then
5822
5823 -- This can happen when E is an itype that is the full view of
5824 -- a private type completed, e.g. with a constrained array. In
5825 -- that case, use the first subtype, which will carry size
5826 -- information. The base type itself is unconstrained and will
5827 -- not carry it.
5828
5829 Astype := First_Subtype (E);
5830 end if;
5831
5832 Set_Size_Info (E, (Astype));
5833 Set_RM_Size (E, RM_Size (Astype));
5834 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5835
5836 if Is_Discrete_Or_Fixed_Point_Type (E) then
5837 Set_RM_Size (E, RM_Size (Astype));
5838
5839 -- In nested instances, the base type of an access actual may
5840 -- itself be private, and need to be exchanged.
5841
5842 elsif Is_Access_Type (E)
5843 and then Is_Private_Type (Etype (E))
5844 then
5845 Check_Private_View
5846 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5847 end if;
5848
5849 elsif Ekind (E) = E_Package then
5850
5851 -- If this is the renaming for the current instance, we're done.
5852 -- Otherwise it is a formal package. If the corresponding formal
5853 -- was declared with a box, the (instantiations of the) generic
5854 -- formal part are also visible. Otherwise, ignore the entity
5855 -- created to validate the actuals.
5856
5857 if Renamed_Object (E) = Instance then
5858 exit;
5859
5860 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5861 null;
5862
5863 -- The visibility of a formal of an enclosing generic is already
5864 -- correct.
5865
5866 elsif Denotes_Formal_Package (E) then
5867 null;
5868
5869 elsif Present (Associated_Formal_Package (E))
5870 and then not Is_Generic_Formal (E)
5871 then
5872 if Box_Present (Parent (Associated_Formal_Package (E))) then
5873 Check_Generic_Actuals (Renamed_Object (E), True);
5874
5875 else
5876 Check_Generic_Actuals (Renamed_Object (E), False);
5877 end if;
5878
5879 Set_Is_Hidden (E, False);
5880 end if;
5881
5882 -- If this is a subprogram instance (in a wrapper package) the
5883 -- actual is fully visible.
5884
5885 elsif Is_Wrapper_Package (Instance) then
5886 Set_Is_Hidden (E, False);
5887
5888 -- If the formal package is declared with a box, or if the formal
5889 -- parameter is defaulted, it is visible in the body.
5890
5891 elsif Is_Formal_Box
5892 or else Is_Visible_Formal (E)
5893 then
5894 Set_Is_Hidden (E, False);
5895 end if;
5896
5897 if Ekind (E) = E_Constant then
5898
5899 -- If the type of the actual is a private type declared in the
5900 -- enclosing scope of the generic unit, the body of the generic
5901 -- sees the full view of the type (because it has to appear in
5902 -- the corresponding package body). If the type is private now,
5903 -- exchange views to restore the proper visiblity in the instance.
5904
5905 declare
5906 Typ : constant Entity_Id := Base_Type (Etype (E));
5907 -- The type of the actual
5908
5909 Gen_Id : Entity_Id;
5910 -- The generic unit
5911
5912 Parent_Scope : Entity_Id;
5913 -- The enclosing scope of the generic unit
5914
5915 begin
5916 if Is_Wrapper_Package (Instance) then
5917 Gen_Id :=
5918 Generic_Parent
5919 (Specification
5920 (Unit_Declaration_Node
5921 (Related_Instance (Instance))));
5922 else
5923 Gen_Id :=
5924 Generic_Parent (Package_Specification (Instance));
5925 end if;
5926
5927 Parent_Scope := Scope (Gen_Id);
5928
5929 -- The exchange is only needed if the generic is defined
5930 -- within a package which is not a common ancestor of the
5931 -- scope of the instance, and is not already in scope.
5932
5933 if Is_Private_Type (Typ)
5934 and then Scope (Typ) = Parent_Scope
5935 and then Scope (Instance) /= Parent_Scope
5936 and then Ekind (Parent_Scope) = E_Package
5937 and then not Is_Child_Unit (Gen_Id)
5938 then
5939 Switch_View (Typ);
5940
5941 -- If the type of the entity is a subtype, it may also have
5942 -- to be made visible, together with the base type of its
5943 -- full view, after exchange.
5944
5945 if Is_Private_Type (Etype (E)) then
5946 Switch_View (Etype (E));
5947 Switch_View (Base_Type (Etype (E)));
5948 end if;
5949 end if;
5950 end;
5951 end if;
5952
5953 Next_Entity (E);
5954 end loop;
5955 end Check_Generic_Actuals;
5956
5957 ------------------------------
5958 -- Check_Generic_Child_Unit --
5959 ------------------------------
5960
5961 procedure Check_Generic_Child_Unit
5962 (Gen_Id : Node_Id;
5963 Parent_Installed : in out Boolean)
5964 is
5965 Loc : constant Source_Ptr := Sloc (Gen_Id);
5966 Gen_Par : Entity_Id := Empty;
5967 E : Entity_Id;
5968 Inst_Par : Entity_Id;
5969 S : Node_Id;
5970
5971 function Find_Generic_Child
5972 (Scop : Entity_Id;
5973 Id : Node_Id) return Entity_Id;
5974 -- Search generic parent for possible child unit with the given name
5975
5976 function In_Enclosing_Instance return Boolean;
5977 -- Within an instance of the parent, the child unit may be denoted by
5978 -- a simple name, or an abbreviated expanded name. Examine enclosing
5979 -- scopes to locate a possible parent instantiation.
5980
5981 ------------------------
5982 -- Find_Generic_Child --
5983 ------------------------
5984
5985 function Find_Generic_Child
5986 (Scop : Entity_Id;
5987 Id : Node_Id) return Entity_Id
5988 is
5989 E : Entity_Id;
5990
5991 begin
5992 -- If entity of name is already set, instance has already been
5993 -- resolved, e.g. in an enclosing instantiation.
5994
5995 if Present (Entity (Id)) then
5996 if Scope (Entity (Id)) = Scop then
5997 return Entity (Id);
5998 else
5999 return Empty;
6000 end if;
6001
6002 else
6003 E := First_Entity (Scop);
6004 while Present (E) loop
6005 if Chars (E) = Chars (Id)
6006 and then Is_Child_Unit (E)
6007 then
6008 if Is_Child_Unit (E)
6009 and then not Is_Visible_Lib_Unit (E)
6010 then
6011 Error_Msg_NE
6012 ("generic child unit& is not visible", Gen_Id, E);
6013 end if;
6014
6015 Set_Entity (Id, E);
6016 return E;
6017 end if;
6018
6019 Next_Entity (E);
6020 end loop;
6021
6022 return Empty;
6023 end if;
6024 end Find_Generic_Child;
6025
6026 ---------------------------
6027 -- In_Enclosing_Instance --
6028 ---------------------------
6029
6030 function In_Enclosing_Instance return Boolean is
6031 Enclosing_Instance : Node_Id;
6032 Instance_Decl : Node_Id;
6033
6034 begin
6035 -- We do not inline any call that contains instantiations, except
6036 -- for instantiations of Unchecked_Conversion, so if we are within
6037 -- an inlined body the current instance does not require parents.
6038
6039 if In_Inlined_Body then
6040 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6041 return False;
6042 end if;
6043
6044 -- Loop to check enclosing scopes
6045
6046 Enclosing_Instance := Current_Scope;
6047 while Present (Enclosing_Instance) loop
6048 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6049
6050 if Ekind (Enclosing_Instance) = E_Package
6051 and then Is_Generic_Instance (Enclosing_Instance)
6052 and then Present
6053 (Generic_Parent (Specification (Instance_Decl)))
6054 then
6055 -- Check whether the generic we are looking for is a child of
6056 -- this instance.
6057
6058 E := Find_Generic_Child
6059 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6060 exit when Present (E);
6061
6062 else
6063 E := Empty;
6064 end if;
6065
6066 Enclosing_Instance := Scope (Enclosing_Instance);
6067 end loop;
6068
6069 if No (E) then
6070
6071 -- Not a child unit
6072
6073 Analyze (Gen_Id);
6074 return False;
6075
6076 else
6077 Rewrite (Gen_Id,
6078 Make_Expanded_Name (Loc,
6079 Chars => Chars (E),
6080 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6081 Selector_Name => New_Occurrence_Of (E, Loc)));
6082
6083 Set_Entity (Gen_Id, E);
6084 Set_Etype (Gen_Id, Etype (E));
6085 Parent_Installed := False; -- Already in scope.
6086 return True;
6087 end if;
6088 end In_Enclosing_Instance;
6089
6090 -- Start of processing for Check_Generic_Child_Unit
6091
6092 begin
6093 -- If the name of the generic is given by a selected component, it may
6094 -- be the name of a generic child unit, and the prefix is the name of an
6095 -- instance of the parent, in which case the child unit must be visible.
6096 -- If this instance is not in scope, it must be placed there and removed
6097 -- after instantiation, because what is being instantiated is not the
6098 -- original child, but the corresponding child present in the instance
6099 -- of the parent.
6100
6101 -- If the child is instantiated within the parent, it can be given by
6102 -- a simple name. In this case the instance is already in scope, but
6103 -- the child generic must be recovered from the generic parent as well.
6104
6105 if Nkind (Gen_Id) = N_Selected_Component then
6106 S := Selector_Name (Gen_Id);
6107 Analyze (Prefix (Gen_Id));
6108 Inst_Par := Entity (Prefix (Gen_Id));
6109
6110 if Ekind (Inst_Par) = E_Package
6111 and then Present (Renamed_Object (Inst_Par))
6112 then
6113 Inst_Par := Renamed_Object (Inst_Par);
6114 end if;
6115
6116 if Ekind (Inst_Par) = E_Package then
6117 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6118 Gen_Par := Generic_Parent (Parent (Inst_Par));
6119
6120 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6121 and then
6122 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6123 then
6124 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6125 end if;
6126
6127 elsif Ekind (Inst_Par) = E_Generic_Package
6128 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6129 then
6130 -- A formal package may be a real child package, and not the
6131 -- implicit instance within a parent. In this case the child is
6132 -- not visible and has to be retrieved explicitly as well.
6133
6134 Gen_Par := Inst_Par;
6135 end if;
6136
6137 if Present (Gen_Par) then
6138
6139 -- The prefix denotes an instantiation. The entity itself may be a
6140 -- nested generic, or a child unit.
6141
6142 E := Find_Generic_Child (Gen_Par, S);
6143
6144 if Present (E) then
6145 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6146 Set_Entity (Gen_Id, E);
6147 Set_Etype (Gen_Id, Etype (E));
6148 Set_Entity (S, E);
6149 Set_Etype (S, Etype (E));
6150
6151 -- Indicate that this is a reference to the parent
6152
6153 if In_Extended_Main_Source_Unit (Gen_Id) then
6154 Set_Is_Instantiated (Inst_Par);
6155 end if;
6156
6157 -- A common mistake is to replicate the naming scheme of a
6158 -- hierarchy by instantiating a generic child directly, rather
6159 -- than the implicit child in a parent instance:
6160
6161 -- generic .. package Gpar is ..
6162 -- generic .. package Gpar.Child is ..
6163 -- package Par is new Gpar ();
6164
6165 -- with Gpar.Child;
6166 -- package Par.Child is new Gpar.Child ();
6167 -- rather than Par.Child
6168
6169 -- In this case the instantiation is within Par, which is an
6170 -- instance, but Gpar does not denote Par because we are not IN
6171 -- the instance of Gpar, so this is illegal. The test below
6172 -- recognizes this particular case.
6173
6174 if Is_Child_Unit (E)
6175 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6176 and then (not In_Instance
6177 or else Nkind (Parent (Parent (Gen_Id))) =
6178 N_Compilation_Unit)
6179 then
6180 Error_Msg_N
6181 ("prefix of generic child unit must be instance of parent",
6182 Gen_Id);
6183 end if;
6184
6185 if not In_Open_Scopes (Inst_Par)
6186 and then Nkind (Parent (Gen_Id)) not in
6187 N_Generic_Renaming_Declaration
6188 then
6189 Install_Parent (Inst_Par);
6190 Parent_Installed := True;
6191
6192 elsif In_Open_Scopes (Inst_Par) then
6193
6194 -- If the parent is already installed, install the actuals
6195 -- for its formal packages. This is necessary when the child
6196 -- instance is a child of the parent instance: in this case,
6197 -- the parent is placed on the scope stack but the formal
6198 -- packages are not made visible.
6199
6200 Install_Formal_Packages (Inst_Par);
6201 end if;
6202
6203 else
6204 -- If the generic parent does not contain an entity that
6205 -- corresponds to the selector, the instance doesn't either.
6206 -- Analyzing the node will yield the appropriate error message.
6207 -- If the entity is not a child unit, then it is an inner
6208 -- generic in the parent.
6209
6210 Analyze (Gen_Id);
6211 end if;
6212
6213 else
6214 Analyze (Gen_Id);
6215
6216 if Is_Child_Unit (Entity (Gen_Id))
6217 and then
6218 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6219 and then not In_Open_Scopes (Inst_Par)
6220 then
6221 Install_Parent (Inst_Par);
6222 Parent_Installed := True;
6223
6224 -- The generic unit may be the renaming of the implicit child
6225 -- present in an instance. In that case the parent instance is
6226 -- obtained from the name of the renamed entity.
6227
6228 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6229 and then Present (Renamed_Entity (Entity (Gen_Id)))
6230 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6231 then
6232 declare
6233 Renamed_Package : constant Node_Id :=
6234 Name (Parent (Entity (Gen_Id)));
6235 begin
6236 if Nkind (Renamed_Package) = N_Expanded_Name then
6237 Inst_Par := Entity (Prefix (Renamed_Package));
6238 Install_Parent (Inst_Par);
6239 Parent_Installed := True;
6240 end if;
6241 end;
6242 end if;
6243 end if;
6244
6245 elsif Nkind (Gen_Id) = N_Expanded_Name then
6246
6247 -- Entity already present, analyze prefix, whose meaning may be
6248 -- an instance in the current context. If it is an instance of
6249 -- a relative within another, the proper parent may still have
6250 -- to be installed, if they are not of the same generation.
6251
6252 Analyze (Prefix (Gen_Id));
6253
6254 -- In the unlikely case that a local declaration hides the name
6255 -- of the parent package, locate it on the homonym chain. If the
6256 -- context is an instance of the parent, the renaming entity is
6257 -- flagged as such.
6258
6259 Inst_Par := Entity (Prefix (Gen_Id));
6260 while Present (Inst_Par)
6261 and then not Is_Package_Or_Generic_Package (Inst_Par)
6262 loop
6263 Inst_Par := Homonym (Inst_Par);
6264 end loop;
6265
6266 pragma Assert (Present (Inst_Par));
6267 Set_Entity (Prefix (Gen_Id), Inst_Par);
6268
6269 if In_Enclosing_Instance then
6270 null;
6271
6272 elsif Present (Entity (Gen_Id))
6273 and then Is_Child_Unit (Entity (Gen_Id))
6274 and then not In_Open_Scopes (Inst_Par)
6275 then
6276 Install_Parent (Inst_Par);
6277 Parent_Installed := True;
6278 end if;
6279
6280 elsif In_Enclosing_Instance then
6281
6282 -- The child unit is found in some enclosing scope
6283
6284 null;
6285
6286 else
6287 Analyze (Gen_Id);
6288
6289 -- If this is the renaming of the implicit child in a parent
6290 -- instance, recover the parent name and install it.
6291
6292 if Is_Entity_Name (Gen_Id) then
6293 E := Entity (Gen_Id);
6294
6295 if Is_Generic_Unit (E)
6296 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6297 and then Is_Child_Unit (Renamed_Object (E))
6298 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6299 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6300 then
6301 Rewrite (Gen_Id,
6302 New_Copy_Tree (Name (Parent (E))));
6303 Inst_Par := Entity (Prefix (Gen_Id));
6304
6305 if not In_Open_Scopes (Inst_Par) then
6306 Install_Parent (Inst_Par);
6307 Parent_Installed := True;
6308 end if;
6309
6310 -- If it is a child unit of a non-generic parent, it may be
6311 -- use-visible and given by a direct name. Install parent as
6312 -- for other cases.
6313
6314 elsif Is_Generic_Unit (E)
6315 and then Is_Child_Unit (E)
6316 and then
6317 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6318 and then not Is_Generic_Unit (Scope (E))
6319 then
6320 if not In_Open_Scopes (Scope (E)) then
6321 Install_Parent (Scope (E));
6322 Parent_Installed := True;
6323 end if;
6324 end if;
6325 end if;
6326 end if;
6327 end Check_Generic_Child_Unit;
6328
6329 -----------------------------
6330 -- Check_Hidden_Child_Unit --
6331 -----------------------------
6332
6333 procedure Check_Hidden_Child_Unit
6334 (N : Node_Id;
6335 Gen_Unit : Entity_Id;
6336 Act_Decl_Id : Entity_Id)
6337 is
6338 Gen_Id : constant Node_Id := Name (N);
6339
6340 begin
6341 if Is_Child_Unit (Gen_Unit)
6342 and then Is_Child_Unit (Act_Decl_Id)
6343 and then Nkind (Gen_Id) = N_Expanded_Name
6344 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6345 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6346 then
6347 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6348 Error_Msg_NE
6349 ("generic unit & is implicitly declared in &",
6350 Defining_Unit_Name (N), Gen_Unit);
6351 Error_Msg_N ("\instance must have different name",
6352 Defining_Unit_Name (N));
6353 end if;
6354 end Check_Hidden_Child_Unit;
6355
6356 ------------------------
6357 -- Check_Private_View --
6358 ------------------------
6359
6360 procedure Check_Private_View (N : Node_Id) is
6361 T : constant Entity_Id := Etype (N);
6362 BT : Entity_Id;
6363
6364 begin
6365 -- Exchange views if the type was not private in the generic but is
6366 -- private at the point of instantiation. Do not exchange views if
6367 -- the scope of the type is in scope. This can happen if both generic
6368 -- and instance are sibling units, or if type is defined in a parent.
6369 -- In this case the visibility of the type will be correct for all
6370 -- semantic checks.
6371
6372 if Present (T) then
6373 BT := Base_Type (T);
6374
6375 if Is_Private_Type (T)
6376 and then not Has_Private_View (N)
6377 and then Present (Full_View (T))
6378 and then not In_Open_Scopes (Scope (T))
6379 then
6380 -- In the generic, the full type was visible. Save the private
6381 -- entity, for subsequent exchange.
6382
6383 Switch_View (T);
6384
6385 elsif Has_Private_View (N)
6386 and then not Is_Private_Type (T)
6387 and then not Has_Been_Exchanged (T)
6388 and then Etype (Get_Associated_Node (N)) /= T
6389 then
6390 -- Only the private declaration was visible in the generic. If
6391 -- the type appears in a subtype declaration, the subtype in the
6392 -- instance must have a view compatible with that of its parent,
6393 -- which must be exchanged (see corresponding code in Restore_
6394 -- Private_Views). Otherwise, if the type is defined in a parent
6395 -- unit, leave full visibility within instance, which is safe.
6396
6397 if In_Open_Scopes (Scope (Base_Type (T)))
6398 and then not Is_Private_Type (Base_Type (T))
6399 and then Comes_From_Source (Base_Type (T))
6400 then
6401 null;
6402
6403 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6404 or else not In_Private_Part (Scope (Base_Type (T)))
6405 then
6406 Prepend_Elmt (T, Exchanged_Views);
6407 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6408 end if;
6409
6410 -- For composite types with inconsistent representation exchange
6411 -- component types accordingly.
6412
6413 elsif Is_Access_Type (T)
6414 and then Is_Private_Type (Designated_Type (T))
6415 and then not Has_Private_View (N)
6416 and then Present (Full_View (Designated_Type (T)))
6417 then
6418 Switch_View (Designated_Type (T));
6419
6420 elsif Is_Array_Type (T) then
6421 if Is_Private_Type (Component_Type (T))
6422 and then not Has_Private_View (N)
6423 and then Present (Full_View (Component_Type (T)))
6424 then
6425 Switch_View (Component_Type (T));
6426 end if;
6427
6428 -- The normal exchange mechanism relies on the setting of a
6429 -- flag on the reference in the generic. However, an additional
6430 -- mechanism is needed for types that are not explicitly
6431 -- mentioned in the generic, but may be needed in expanded code
6432 -- in the instance. This includes component types of arrays and
6433 -- designated types of access types. This processing must also
6434 -- include the index types of arrays which we take care of here.
6435
6436 declare
6437 Indx : Node_Id;
6438 Typ : Entity_Id;
6439
6440 begin
6441 Indx := First_Index (T);
6442 while Present (Indx) loop
6443 Typ := Base_Type (Etype (Indx));
6444
6445 if Is_Private_Type (Typ)
6446 and then Present (Full_View (Typ))
6447 then
6448 Switch_View (Typ);
6449 end if;
6450
6451 Next_Index (Indx);
6452 end loop;
6453 end;
6454
6455 elsif Is_Private_Type (T)
6456 and then Present (Full_View (T))
6457 and then Is_Array_Type (Full_View (T))
6458 and then Is_Private_Type (Component_Type (Full_View (T)))
6459 then
6460 Switch_View (T);
6461
6462 -- Finally, a non-private subtype may have a private base type, which
6463 -- must be exchanged for consistency. This can happen when a package
6464 -- body is instantiated, when the scope stack is empty but in fact
6465 -- the subtype and the base type are declared in an enclosing scope.
6466
6467 -- Note that in this case we introduce an inconsistency in the view
6468 -- set, because we switch the base type BT, but there could be some
6469 -- private dependent subtypes of BT which remain unswitched. Such
6470 -- subtypes might need to be switched at a later point (see specific
6471 -- provision for that case in Switch_View).
6472
6473 elsif not Is_Private_Type (T)
6474 and then not Has_Private_View (N)
6475 and then Is_Private_Type (BT)
6476 and then Present (Full_View (BT))
6477 and then not Is_Generic_Type (BT)
6478 and then not In_Open_Scopes (BT)
6479 then
6480 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6481 Exchange_Declarations (BT);
6482 end if;
6483 end if;
6484 end Check_Private_View;
6485
6486 -----------------------------
6487 -- Check_Hidden_Primitives --
6488 -----------------------------
6489
6490 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6491 Actual : Node_Id;
6492 Gen_T : Entity_Id;
6493 Result : Elist_Id := No_Elist;
6494
6495 begin
6496 if No (Assoc_List) then
6497 return No_Elist;
6498 end if;
6499
6500 -- Traverse the list of associations between formals and actuals
6501 -- searching for renamings of tagged types
6502
6503 Actual := First (Assoc_List);
6504 while Present (Actual) loop
6505 if Nkind (Actual) = N_Subtype_Declaration then
6506 Gen_T := Generic_Parent_Type (Actual);
6507
6508 if Present (Gen_T)
6509 and then Is_Tagged_Type (Gen_T)
6510 then
6511 -- Traverse the list of primitives of the actual types
6512 -- searching for hidden primitives that are visible in the
6513 -- corresponding generic formal; leave them visible and
6514 -- append them to Result to restore their decoration later.
6515
6516 Install_Hidden_Primitives
6517 (Prims_List => Result,
6518 Gen_T => Gen_T,
6519 Act_T => Entity (Subtype_Indication (Actual)));
6520 end if;
6521 end if;
6522
6523 Next (Actual);
6524 end loop;
6525
6526 return Result;
6527 end Check_Hidden_Primitives;
6528
6529 --------------------------
6530 -- Contains_Instance_Of --
6531 --------------------------
6532
6533 function Contains_Instance_Of
6534 (Inner : Entity_Id;
6535 Outer : Entity_Id;
6536 N : Node_Id) return Boolean
6537 is
6538 Elmt : Elmt_Id;
6539 Scop : Entity_Id;
6540
6541 begin
6542 Scop := Outer;
6543
6544 -- Verify that there are no circular instantiations. We check whether
6545 -- the unit contains an instance of the current scope or some enclosing
6546 -- scope (in case one of the instances appears in a subunit). Longer
6547 -- circularities involving subunits might seem too pathological to
6548 -- consider, but they were not too pathological for the authors of
6549 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6550 -- enclosing generic scopes as containing an instance.
6551
6552 loop
6553 -- Within a generic subprogram body, the scope is not generic, to
6554 -- allow for recursive subprograms. Use the declaration to determine
6555 -- whether this is a generic unit.
6556
6557 if Ekind (Scop) = E_Generic_Package
6558 or else (Is_Subprogram (Scop)
6559 and then Nkind (Unit_Declaration_Node (Scop)) =
6560 N_Generic_Subprogram_Declaration)
6561 then
6562 Elmt := First_Elmt (Inner_Instances (Inner));
6563
6564 while Present (Elmt) loop
6565 if Node (Elmt) = Scop then
6566 Error_Msg_Node_2 := Inner;
6567 Error_Msg_NE
6568 ("circular Instantiation: & instantiated within &!",
6569 N, Scop);
6570 return True;
6571
6572 elsif Node (Elmt) = Inner then
6573 return True;
6574
6575 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6576 Error_Msg_Node_2 := Inner;
6577 Error_Msg_NE
6578 ("circular Instantiation: & instantiated within &!",
6579 N, Node (Elmt));
6580 return True;
6581 end if;
6582
6583 Next_Elmt (Elmt);
6584 end loop;
6585
6586 -- Indicate that Inner is being instantiated within Scop
6587
6588 Append_Elmt (Inner, Inner_Instances (Scop));
6589 end if;
6590
6591 if Scop = Standard_Standard then
6592 exit;
6593 else
6594 Scop := Scope (Scop);
6595 end if;
6596 end loop;
6597
6598 return False;
6599 end Contains_Instance_Of;
6600
6601 -----------------------
6602 -- Copy_Generic_Node --
6603 -----------------------
6604
6605 function Copy_Generic_Node
6606 (N : Node_Id;
6607 Parent_Id : Node_Id;
6608 Instantiating : Boolean) return Node_Id
6609 is
6610 Ent : Entity_Id;
6611 New_N : Node_Id;
6612
6613 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6614 -- Check the given value of one of the Fields referenced by the current
6615 -- node to determine whether to copy it recursively. The field may hold
6616 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6617 -- Char) in which case it need not be copied.
6618
6619 procedure Copy_Descendants;
6620 -- Common utility for various nodes
6621
6622 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6623 -- Make copy of element list
6624
6625 function Copy_Generic_List
6626 (L : List_Id;
6627 Parent_Id : Node_Id) return List_Id;
6628 -- Apply Copy_Node recursively to the members of a node list
6629
6630 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6631 -- True if an identifier is part of the defining program unit name of
6632 -- a child unit. The entity of such an identifier must be kept (for
6633 -- ASIS use) even though as the name of an enclosing generic it would
6634 -- otherwise not be preserved in the generic tree.
6635
6636 ----------------------
6637 -- Copy_Descendants --
6638 ----------------------
6639
6640 procedure Copy_Descendants is
6641
6642 use Atree.Unchecked_Access;
6643 -- This code section is part of the implementation of an untyped
6644 -- tree traversal, so it needs direct access to node fields.
6645
6646 begin
6647 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6648 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6649 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6650 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6651 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6652 end Copy_Descendants;
6653
6654 -----------------------------
6655 -- Copy_Generic_Descendant --
6656 -----------------------------
6657
6658 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6659 begin
6660 if D = Union_Id (Empty) then
6661 return D;
6662
6663 elsif D in Node_Range then
6664 return Union_Id
6665 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6666
6667 elsif D in List_Range then
6668 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6669
6670 elsif D in Elist_Range then
6671 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6672
6673 -- Nothing else is copyable (e.g. Uint values), return as is
6674
6675 else
6676 return D;
6677 end if;
6678 end Copy_Generic_Descendant;
6679
6680 ------------------------
6681 -- Copy_Generic_Elist --
6682 ------------------------
6683
6684 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6685 M : Elmt_Id;
6686 L : Elist_Id;
6687
6688 begin
6689 if Present (E) then
6690 L := New_Elmt_List;
6691 M := First_Elmt (E);
6692 while Present (M) loop
6693 Append_Elmt
6694 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6695 Next_Elmt (M);
6696 end loop;
6697
6698 return L;
6699
6700 else
6701 return No_Elist;
6702 end if;
6703 end Copy_Generic_Elist;
6704
6705 -----------------------
6706 -- Copy_Generic_List --
6707 -----------------------
6708
6709 function Copy_Generic_List
6710 (L : List_Id;
6711 Parent_Id : Node_Id) return List_Id
6712 is
6713 N : Node_Id;
6714 New_L : List_Id;
6715
6716 begin
6717 if Present (L) then
6718 New_L := New_List;
6719 Set_Parent (New_L, Parent_Id);
6720
6721 N := First (L);
6722 while Present (N) loop
6723 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6724 Next (N);
6725 end loop;
6726
6727 return New_L;
6728
6729 else
6730 return No_List;
6731 end if;
6732 end Copy_Generic_List;
6733
6734 ---------------------------
6735 -- In_Defining_Unit_Name --
6736 ---------------------------
6737
6738 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6739 begin
6740 return Present (Parent (Nam))
6741 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6742 or else
6743 (Nkind (Parent (Nam)) = N_Expanded_Name
6744 and then In_Defining_Unit_Name (Parent (Nam))));
6745 end In_Defining_Unit_Name;
6746
6747 -- Start of processing for Copy_Generic_Node
6748
6749 begin
6750 if N = Empty then
6751 return N;
6752 end if;
6753
6754 New_N := New_Copy (N);
6755
6756 -- Copy aspects if present
6757
6758 if Has_Aspects (N) then
6759 Set_Has_Aspects (New_N, False);
6760 Set_Aspect_Specifications
6761 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6762 end if;
6763
6764 if Instantiating then
6765 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6766 end if;
6767
6768 if not Is_List_Member (N) then
6769 Set_Parent (New_N, Parent_Id);
6770 end if;
6771
6772 -- If defining identifier, then all fields have been copied already
6773
6774 if Nkind (New_N) in N_Entity then
6775 null;
6776
6777 -- Special casing for identifiers and other entity names and operators
6778
6779 elsif Nkind_In (New_N, N_Identifier,
6780 N_Character_Literal,
6781 N_Expanded_Name,
6782 N_Operator_Symbol)
6783 or else Nkind (New_N) in N_Op
6784 then
6785 if not Instantiating then
6786
6787 -- Link both nodes in order to assign subsequently the entity of
6788 -- the copy to the original node, in case this is a global
6789 -- reference.
6790
6791 Set_Associated_Node (N, New_N);
6792
6793 -- If we are within an instantiation, this is a nested generic
6794 -- that has already been analyzed at the point of definition.
6795 -- We must preserve references that were global to the enclosing
6796 -- parent at that point. Other occurrences, whether global or
6797 -- local to the current generic, must be resolved anew, so we
6798 -- reset the entity in the generic copy. A global reference has a
6799 -- smaller depth than the parent, or else the same depth in case
6800 -- both are distinct compilation units.
6801
6802 -- A child unit is implicitly declared within the enclosing parent
6803 -- but is in fact global to it, and must be preserved.
6804
6805 -- It is also possible for Current_Instantiated_Parent to be
6806 -- defined, and for this not to be a nested generic, namely if
6807 -- the unit is loaded through Rtsfind. In that case, the entity of
6808 -- New_N is only a link to the associated node, and not a defining
6809 -- occurrence.
6810
6811 -- The entities for parent units in the defining_program_unit of a
6812 -- generic child unit are established when the context of the unit
6813 -- is first analyzed, before the generic copy is made. They are
6814 -- preserved in the copy for use in ASIS queries.
6815
6816 Ent := Entity (New_N);
6817
6818 if No (Current_Instantiated_Parent.Gen_Id) then
6819 if No (Ent)
6820 or else Nkind (Ent) /= N_Defining_Identifier
6821 or else not In_Defining_Unit_Name (N)
6822 then
6823 Set_Associated_Node (New_N, Empty);
6824 end if;
6825
6826 elsif No (Ent)
6827 or else
6828 not Nkind_In (Ent, N_Defining_Identifier,
6829 N_Defining_Character_Literal,
6830 N_Defining_Operator_Symbol)
6831 or else No (Scope (Ent))
6832 or else
6833 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6834 and then not Is_Child_Unit (Ent))
6835 or else
6836 (Scope_Depth (Scope (Ent)) >
6837 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6838 and then
6839 Get_Source_Unit (Ent) =
6840 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6841 then
6842 Set_Associated_Node (New_N, Empty);
6843 end if;
6844
6845 -- Case of instantiating identifier or some other name or operator
6846
6847 else
6848 -- If the associated node is still defined, the entity in it
6849 -- is global, and must be copied to the instance. If this copy
6850 -- is being made for a body to inline, it is applied to an
6851 -- instantiated tree, and the entity is already present and
6852 -- must be also preserved.
6853
6854 declare
6855 Assoc : constant Node_Id := Get_Associated_Node (N);
6856
6857 begin
6858 if Present (Assoc) then
6859 if Nkind (Assoc) = Nkind (N) then
6860 Set_Entity (New_N, Entity (Assoc));
6861 Check_Private_View (N);
6862
6863 -- The name in the call may be a selected component if the
6864 -- call has not been analyzed yet, as may be the case for
6865 -- pre/post conditions in a generic unit.
6866
6867 elsif Nkind (Assoc) = N_Function_Call
6868 and then Is_Entity_Name (Name (Assoc))
6869 then
6870 Set_Entity (New_N, Entity (Name (Assoc)));
6871
6872 elsif Nkind_In (Assoc, N_Defining_Identifier,
6873 N_Defining_Character_Literal,
6874 N_Defining_Operator_Symbol)
6875 and then Expander_Active
6876 then
6877 -- Inlining case: we are copying a tree that contains
6878 -- global entities, which are preserved in the copy to be
6879 -- used for subsequent inlining.
6880
6881 null;
6882
6883 else
6884 Set_Entity (New_N, Empty);
6885 end if;
6886 end if;
6887 end;
6888 end if;
6889
6890 -- For expanded name, we must copy the Prefix and Selector_Name
6891
6892 if Nkind (N) = N_Expanded_Name then
6893 Set_Prefix
6894 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
6895
6896 Set_Selector_Name (New_N,
6897 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
6898
6899 -- For operators, we must copy the right operand
6900
6901 elsif Nkind (N) in N_Op then
6902 Set_Right_Opnd (New_N,
6903 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
6904
6905 -- And for binary operators, the left operand as well
6906
6907 if Nkind (N) in N_Binary_Op then
6908 Set_Left_Opnd (New_N,
6909 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
6910 end if;
6911 end if;
6912
6913 -- Special casing for stubs
6914
6915 elsif Nkind (N) in N_Body_Stub then
6916
6917 -- In any case, we must copy the specification or defining
6918 -- identifier as appropriate.
6919
6920 if Nkind (N) = N_Subprogram_Body_Stub then
6921 Set_Specification (New_N,
6922 Copy_Generic_Node (Specification (N), New_N, Instantiating));
6923
6924 else
6925 Set_Defining_Identifier (New_N,
6926 Copy_Generic_Node
6927 (Defining_Identifier (N), New_N, Instantiating));
6928 end if;
6929
6930 -- If we are not instantiating, then this is where we load and
6931 -- analyze subunits, i.e. at the point where the stub occurs. A
6932 -- more permissive system might defer this analysis to the point
6933 -- of instantiation, but this seems too complicated for now.
6934
6935 if not Instantiating then
6936 declare
6937 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
6938 Subunit : Node_Id;
6939 Unum : Unit_Number_Type;
6940 New_Body : Node_Id;
6941
6942 begin
6943 -- Make sure that, if it is a subunit of the main unit that is
6944 -- preprocessed and if -gnateG is specified, the preprocessed
6945 -- file will be written.
6946
6947 Lib.Analysing_Subunit_Of_Main :=
6948 Lib.In_Extended_Main_Source_Unit (N);
6949 Unum :=
6950 Load_Unit
6951 (Load_Name => Subunit_Name,
6952 Required => False,
6953 Subunit => True,
6954 Error_Node => N);
6955 Lib.Analysing_Subunit_Of_Main := False;
6956
6957 -- If the proper body is not found, a warning message will be
6958 -- emitted when analyzing the stub, or later at the point of
6959 -- instantiation. Here we just leave the stub as is.
6960
6961 if Unum = No_Unit then
6962 Subunits_Missing := True;
6963 goto Subunit_Not_Found;
6964 end if;
6965
6966 Subunit := Cunit (Unum);
6967
6968 if Nkind (Unit (Subunit)) /= N_Subunit then
6969 Error_Msg_N
6970 ("found child unit instead of expected SEPARATE subunit",
6971 Subunit);
6972 Error_Msg_Sloc := Sloc (N);
6973 Error_Msg_N ("\to complete stub #", Subunit);
6974 goto Subunit_Not_Found;
6975 end if;
6976
6977 -- We must create a generic copy of the subunit, in order to
6978 -- perform semantic analysis on it, and we must replace the
6979 -- stub in the original generic unit with the subunit, in order
6980 -- to preserve non-local references within.
6981
6982 -- Only the proper body needs to be copied. Library_Unit and
6983 -- context clause are simply inherited by the generic copy.
6984 -- Note that the copy (which may be recursive if there are
6985 -- nested subunits) must be done first, before attaching it to
6986 -- the enclosing generic.
6987
6988 New_Body :=
6989 Copy_Generic_Node
6990 (Proper_Body (Unit (Subunit)),
6991 Empty, Instantiating => False);
6992
6993 -- Now place the original proper body in the original generic
6994 -- unit. This is a body, not a compilation unit.
6995
6996 Rewrite (N, Proper_Body (Unit (Subunit)));
6997 Set_Is_Compilation_Unit (Defining_Entity (N), False);
6998 Set_Was_Originally_Stub (N);
6999
7000 -- Finally replace the body of the subunit with its copy, and
7001 -- make this new subunit into the library unit of the generic
7002 -- copy, which does not have stubs any longer.
7003
7004 Set_Proper_Body (Unit (Subunit), New_Body);
7005 Set_Library_Unit (New_N, Subunit);
7006 Inherit_Context (Unit (Subunit), N);
7007 end;
7008
7009 -- If we are instantiating, this must be an error case, since
7010 -- otherwise we would have replaced the stub node by the proper body
7011 -- that corresponds. So just ignore it in the copy (i.e. we have
7012 -- copied it, and that is good enough).
7013
7014 else
7015 null;
7016 end if;
7017
7018 <<Subunit_Not_Found>> null;
7019
7020 -- If the node is a compilation unit, it is the subunit of a stub, which
7021 -- has been loaded already (see code below). In this case, the library
7022 -- unit field of N points to the parent unit (which is a compilation
7023 -- unit) and need not (and cannot) be copied.
7024
7025 -- When the proper body of the stub is analyzed, the library_unit link
7026 -- is used to establish the proper context (see sem_ch10).
7027
7028 -- The other fields of a compilation unit are copied as usual
7029
7030 elsif Nkind (N) = N_Compilation_Unit then
7031
7032 -- This code can only be executed when not instantiating, because in
7033 -- the copy made for an instantiation, the compilation unit node has
7034 -- disappeared at the point that a stub is replaced by its proper
7035 -- body.
7036
7037 pragma Assert (not Instantiating);
7038
7039 Set_Context_Items (New_N,
7040 Copy_Generic_List (Context_Items (N), New_N));
7041
7042 Set_Unit (New_N,
7043 Copy_Generic_Node (Unit (N), New_N, False));
7044
7045 Set_First_Inlined_Subprogram (New_N,
7046 Copy_Generic_Node
7047 (First_Inlined_Subprogram (N), New_N, False));
7048
7049 Set_Aux_Decls_Node (New_N,
7050 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7051
7052 -- For an assignment node, the assignment is known to be semantically
7053 -- legal if we are instantiating the template. This avoids incorrect
7054 -- diagnostics in generated code.
7055
7056 elsif Nkind (N) = N_Assignment_Statement then
7057
7058 -- Copy name and expression fields in usual manner
7059
7060 Set_Name (New_N,
7061 Copy_Generic_Node (Name (N), New_N, Instantiating));
7062
7063 Set_Expression (New_N,
7064 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7065
7066 if Instantiating then
7067 Set_Assignment_OK (Name (New_N), True);
7068 end if;
7069
7070 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7071 if not Instantiating then
7072 Set_Associated_Node (N, New_N);
7073
7074 else
7075 if Present (Get_Associated_Node (N))
7076 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7077 then
7078 -- In the generic the aggregate has some composite type. If at
7079 -- the point of instantiation the type has a private view,
7080 -- install the full view (and that of its ancestors, if any).
7081
7082 declare
7083 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7084 Rt : Entity_Id;
7085
7086 begin
7087 if Present (T)
7088 and then Is_Private_Type (T)
7089 then
7090 Switch_View (T);
7091 end if;
7092
7093 if Present (T)
7094 and then Is_Tagged_Type (T)
7095 and then Is_Derived_Type (T)
7096 then
7097 Rt := Root_Type (T);
7098
7099 loop
7100 T := Etype (T);
7101
7102 if Is_Private_Type (T) then
7103 Switch_View (T);
7104 end if;
7105
7106 exit when T = Rt;
7107 end loop;
7108 end if;
7109 end;
7110 end if;
7111 end if;
7112
7113 -- Do not copy the associated node, which points to the generic copy
7114 -- of the aggregate.
7115
7116 declare
7117 use Atree.Unchecked_Access;
7118 -- This code section is part of the implementation of an untyped
7119 -- tree traversal, so it needs direct access to node fields.
7120
7121 begin
7122 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7123 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7124 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7125 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7126 end;
7127
7128 -- Allocators do not have an identifier denoting the access type, so we
7129 -- must locate it through the expression to check whether the views are
7130 -- consistent.
7131
7132 elsif Nkind (N) = N_Allocator
7133 and then Nkind (Expression (N)) = N_Qualified_Expression
7134 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7135 and then Instantiating
7136 then
7137 declare
7138 T : constant Node_Id :=
7139 Get_Associated_Node (Subtype_Mark (Expression (N)));
7140 Acc_T : Entity_Id;
7141
7142 begin
7143 if Present (T) then
7144
7145 -- Retrieve the allocator node in the generic copy
7146
7147 Acc_T := Etype (Parent (Parent (T)));
7148 if Present (Acc_T)
7149 and then Is_Private_Type (Acc_T)
7150 then
7151 Switch_View (Acc_T);
7152 end if;
7153 end if;
7154
7155 Copy_Descendants;
7156 end;
7157
7158 -- For a proper body, we must catch the case of a proper body that
7159 -- replaces a stub. This represents the point at which a separate
7160 -- compilation unit, and hence template file, may be referenced, so we
7161 -- must make a new source instantiation entry for the template of the
7162 -- subunit, and ensure that all nodes in the subunit are adjusted using
7163 -- this new source instantiation entry.
7164
7165 elsif Nkind (N) in N_Proper_Body then
7166 declare
7167 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7168
7169 begin
7170 if Instantiating and then Was_Originally_Stub (N) then
7171 Create_Instantiation_Source
7172 (Instantiation_Node,
7173 Defining_Entity (N),
7174 False,
7175 S_Adjustment);
7176 end if;
7177
7178 -- Now copy the fields of the proper body, using the new
7179 -- adjustment factor if one was needed as per test above.
7180
7181 Copy_Descendants;
7182
7183 -- Restore the original adjustment factor in case changed
7184
7185 S_Adjustment := Save_Adjustment;
7186 end;
7187
7188 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
7189 -- generic unit, not to the instantiating unit.
7190
7191 elsif Nkind (N) = N_Pragma and then Instantiating then
7192 declare
7193 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
7194 begin
7195 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
7196 New_N := Make_Null_Statement (Sloc (N));
7197 else
7198 Copy_Descendants;
7199 end if;
7200 end;
7201
7202 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7203
7204 -- No descendant fields need traversing
7205
7206 null;
7207
7208 elsif Nkind (N) = N_String_Literal
7209 and then Present (Etype (N))
7210 and then Instantiating
7211 then
7212 -- If the string is declared in an outer scope, the string_literal
7213 -- subtype created for it may have the wrong scope. We force the
7214 -- reanalysis of the constant to generate a new itype in the proper
7215 -- context.
7216
7217 Set_Etype (New_N, Empty);
7218 Set_Analyzed (New_N, False);
7219
7220 -- For the remaining nodes, copy their descendants recursively
7221
7222 else
7223 Copy_Descendants;
7224
7225 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7226 Set_Generic_Parent (Specification (New_N), N);
7227
7228 -- Should preserve Corresponding_Spec??? (12.3(14))
7229 end if;
7230 end if;
7231
7232 return New_N;
7233 end Copy_Generic_Node;
7234
7235 ----------------------------
7236 -- Denotes_Formal_Package --
7237 ----------------------------
7238
7239 function Denotes_Formal_Package
7240 (Pack : Entity_Id;
7241 On_Exit : Boolean := False;
7242 Instance : Entity_Id := Empty) return Boolean
7243 is
7244 Par : Entity_Id;
7245 Scop : constant Entity_Id := Scope (Pack);
7246 E : Entity_Id;
7247
7248 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7249 -- The package in question may be an actual for a previous formal
7250 -- package P of the current instance, so examine its actuals as well.
7251 -- This must be recursive over other formal packages.
7252
7253 ----------------------------------
7254 -- Is_Actual_Of_Previous_Formal --
7255 ----------------------------------
7256
7257 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7258 E1 : Entity_Id;
7259
7260 begin
7261 E1 := First_Entity (P);
7262 while Present (E1) and then E1 /= Instance loop
7263 if Ekind (E1) = E_Package
7264 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7265 then
7266 if Renamed_Object (E1) = Pack then
7267 return True;
7268
7269 elsif E1 = P or else Renamed_Object (E1) = P then
7270 return False;
7271
7272 elsif Is_Actual_Of_Previous_Formal (E1) then
7273 return True;
7274 end if;
7275 end if;
7276
7277 Next_Entity (E1);
7278 end loop;
7279
7280 return False;
7281 end Is_Actual_Of_Previous_Formal;
7282
7283 -- Start of processing for Denotes_Formal_Package
7284
7285 begin
7286 if On_Exit then
7287 Par :=
7288 Instance_Envs.Table
7289 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7290 else
7291 Par := Current_Instantiated_Parent.Act_Id;
7292 end if;
7293
7294 if Ekind (Scop) = E_Generic_Package
7295 or else Nkind (Unit_Declaration_Node (Scop)) =
7296 N_Generic_Subprogram_Declaration
7297 then
7298 return True;
7299
7300 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7301 N_Formal_Package_Declaration
7302 then
7303 return True;
7304
7305 elsif No (Par) then
7306 return False;
7307
7308 else
7309 -- Check whether this package is associated with a formal package of
7310 -- the enclosing instantiation. Iterate over the list of renamings.
7311
7312 E := First_Entity (Par);
7313 while Present (E) loop
7314 if Ekind (E) /= E_Package
7315 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7316 then
7317 null;
7318
7319 elsif Renamed_Object (E) = Par then
7320 return False;
7321
7322 elsif Renamed_Object (E) = Pack then
7323 return True;
7324
7325 elsif Is_Actual_Of_Previous_Formal (E) then
7326 return True;
7327
7328 end if;
7329
7330 Next_Entity (E);
7331 end loop;
7332
7333 return False;
7334 end if;
7335 end Denotes_Formal_Package;
7336
7337 -----------------
7338 -- End_Generic --
7339 -----------------
7340
7341 procedure End_Generic is
7342 begin
7343 -- ??? More things could be factored out in this routine. Should
7344 -- probably be done at a later stage.
7345
7346 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7347 Generic_Flags.Decrement_Last;
7348
7349 Expander_Mode_Restore;
7350 end End_Generic;
7351
7352 -------------
7353 -- Earlier --
7354 -------------
7355
7356 function Earlier (N1, N2 : Node_Id) return Boolean is
7357 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7358 -- Find distance from given node to enclosing compilation unit
7359
7360 ----------------
7361 -- Find_Depth --
7362 ----------------
7363
7364 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7365 begin
7366 while Present (P)
7367 and then Nkind (P) /= N_Compilation_Unit
7368 loop
7369 P := True_Parent (P);
7370 D := D + 1;
7371 end loop;
7372 end Find_Depth;
7373
7374 -- Local declarations
7375
7376 D1 : Integer := 0;
7377 D2 : Integer := 0;
7378 P1 : Node_Id := N1;
7379 P2 : Node_Id := N2;
7380 T1 : Source_Ptr;
7381 T2 : Source_Ptr;
7382
7383 -- Start of processing for Earlier
7384
7385 begin
7386 Find_Depth (P1, D1);
7387 Find_Depth (P2, D2);
7388
7389 if P1 /= P2 then
7390 return False;
7391 else
7392 P1 := N1;
7393 P2 := N2;
7394 end if;
7395
7396 while D1 > D2 loop
7397 P1 := True_Parent (P1);
7398 D1 := D1 - 1;
7399 end loop;
7400
7401 while D2 > D1 loop
7402 P2 := True_Parent (P2);
7403 D2 := D2 - 1;
7404 end loop;
7405
7406 -- At this point P1 and P2 are at the same distance from the root.
7407 -- We examine their parents until we find a common declarative list.
7408 -- If we reach the root, N1 and N2 do not descend from the same
7409 -- declarative list (e.g. one is nested in the declarative part and
7410 -- the other is in a block in the statement part) and the earlier
7411 -- one is already frozen.
7412
7413 while not Is_List_Member (P1)
7414 or else not Is_List_Member (P2)
7415 or else List_Containing (P1) /= List_Containing (P2)
7416 loop
7417 P1 := True_Parent (P1);
7418 P2 := True_Parent (P2);
7419
7420 if Nkind (Parent (P1)) = N_Subunit then
7421 P1 := Corresponding_Stub (Parent (P1));
7422 end if;
7423
7424 if Nkind (Parent (P2)) = N_Subunit then
7425 P2 := Corresponding_Stub (Parent (P2));
7426 end if;
7427
7428 if P1 = P2 then
7429 return False;
7430 end if;
7431 end loop;
7432
7433 -- Expanded code usually shares the source location of the original
7434 -- construct it was generated for. This however may not necessarely
7435 -- reflect the true location of the code within the tree.
7436
7437 -- Before comparing the slocs of the two nodes, make sure that we are
7438 -- working with correct source locations. Assume that P1 is to the left
7439 -- of P2. If either one does not come from source, traverse the common
7440 -- list heading towards the other node and locate the first source
7441 -- statement.
7442
7443 -- P1 P2
7444 -- ----+===+===+--------------+===+===+----
7445 -- expanded code expanded code
7446
7447 if not Comes_From_Source (P1) then
7448 while Present (P1) loop
7449
7450 -- Neither P2 nor a source statement were located during the
7451 -- search. If we reach the end of the list, then P1 does not
7452 -- occur earlier than P2.
7453
7454 -- ---->
7455 -- start --- P2 ----- P1 --- end
7456
7457 if No (Next (P1)) then
7458 return False;
7459
7460 -- We encounter P2 while going to the right of the list. This
7461 -- means that P1 does indeed appear earlier.
7462
7463 -- ---->
7464 -- start --- P1 ===== P2 --- end
7465 -- expanded code in between
7466
7467 elsif P1 = P2 then
7468 return True;
7469
7470 -- No need to look any further since we have located a source
7471 -- statement.
7472
7473 elsif Comes_From_Source (P1) then
7474 exit;
7475 end if;
7476
7477 -- Keep going right
7478
7479 Next (P1);
7480 end loop;
7481 end if;
7482
7483 if not Comes_From_Source (P2) then
7484 while Present (P2) loop
7485
7486 -- Neither P1 nor a source statement were located during the
7487 -- search. If we reach the start of the list, then P1 does not
7488 -- occur earlier than P2.
7489
7490 -- <----
7491 -- start --- P2 --- P1 --- end
7492
7493 if No (Prev (P2)) then
7494 return False;
7495
7496 -- We encounter P1 while going to the left of the list. This
7497 -- means that P1 does indeed appear earlier.
7498
7499 -- <----
7500 -- start --- P1 ===== P2 --- end
7501 -- expanded code in between
7502
7503 elsif P2 = P1 then
7504 return True;
7505
7506 -- No need to look any further since we have located a source
7507 -- statement.
7508
7509 elsif Comes_From_Source (P2) then
7510 exit;
7511 end if;
7512
7513 -- Keep going left
7514
7515 Prev (P2);
7516 end loop;
7517 end if;
7518
7519 -- At this point either both nodes came from source or we approximated
7520 -- their source locations through neighbouring source statements.
7521
7522 T1 := Top_Level_Location (Sloc (P1));
7523 T2 := Top_Level_Location (Sloc (P2));
7524
7525 -- When two nodes come from the same instance, they have identical top
7526 -- level locations. To determine proper relation within the tree, check
7527 -- their locations within the template.
7528
7529 if T1 = T2 then
7530 return Sloc (P1) < Sloc (P2);
7531
7532 -- The two nodes either come from unrelated instances or do not come
7533 -- from instantiated code at all.
7534
7535 else
7536 return T1 < T2;
7537 end if;
7538 end Earlier;
7539
7540 ----------------------
7541 -- Find_Actual_Type --
7542 ----------------------
7543
7544 function Find_Actual_Type
7545 (Typ : Entity_Id;
7546 Gen_Type : Entity_Id) return Entity_Id
7547 is
7548 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7549 T : Entity_Id;
7550
7551 begin
7552 -- Special processing only applies to child units
7553
7554 if not Is_Child_Unit (Gen_Scope) then
7555 return Get_Instance_Of (Typ);
7556
7557 -- If designated or component type is itself a formal of the child unit,
7558 -- its instance is available.
7559
7560 elsif Scope (Typ) = Gen_Scope then
7561 return Get_Instance_Of (Typ);
7562
7563 -- If the array or access type is not declared in the parent unit,
7564 -- no special processing needed.
7565
7566 elsif not Is_Generic_Type (Typ)
7567 and then Scope (Gen_Scope) /= Scope (Typ)
7568 then
7569 return Get_Instance_Of (Typ);
7570
7571 -- Otherwise, retrieve designated or component type by visibility
7572
7573 else
7574 T := Current_Entity (Typ);
7575 while Present (T) loop
7576 if In_Open_Scopes (Scope (T)) then
7577 return T;
7578
7579 elsif Is_Generic_Actual_Type (T) then
7580 return T;
7581 end if;
7582
7583 T := Homonym (T);
7584 end loop;
7585
7586 return Typ;
7587 end if;
7588 end Find_Actual_Type;
7589
7590 ----------------------------
7591 -- Freeze_Subprogram_Body --
7592 ----------------------------
7593
7594 procedure Freeze_Subprogram_Body
7595 (Inst_Node : Node_Id;
7596 Gen_Body : Node_Id;
7597 Pack_Id : Entity_Id)
7598 is
7599 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7600 Par : constant Entity_Id := Scope (Gen_Unit);
7601 E_G_Id : Entity_Id;
7602 Enc_G : Entity_Id;
7603 Enc_I : Node_Id;
7604 F_Node : Node_Id;
7605
7606 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7607 -- Find innermost package body that encloses the given node, and which
7608 -- is not a compilation unit. Freeze nodes for the instance, or for its
7609 -- enclosing body, may be inserted after the enclosing_body of the
7610 -- generic unit. Used to determine proper placement of freeze node for
7611 -- both package and subprogram instances.
7612
7613 function Package_Freeze_Node (B : Node_Id) return Node_Id;
7614 -- Find entity for given package body, and locate or create a freeze
7615 -- node for it.
7616
7617 ----------------------------
7618 -- Enclosing_Package_Body --
7619 ----------------------------
7620
7621 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
7622 P : Node_Id;
7623
7624 begin
7625 P := Parent (N);
7626 while Present (P)
7627 and then Nkind (Parent (P)) /= N_Compilation_Unit
7628 loop
7629 if Nkind (P) = N_Package_Body then
7630 if Nkind (Parent (P)) = N_Subunit then
7631 return Corresponding_Stub (Parent (P));
7632 else
7633 return P;
7634 end if;
7635 end if;
7636
7637 P := True_Parent (P);
7638 end loop;
7639
7640 return Empty;
7641 end Enclosing_Package_Body;
7642
7643 -------------------------
7644 -- Package_Freeze_Node --
7645 -------------------------
7646
7647 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7648 Id : Entity_Id;
7649
7650 begin
7651 if Nkind (B) = N_Package_Body then
7652 Id := Corresponding_Spec (B);
7653 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7654 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7655 end if;
7656
7657 Ensure_Freeze_Node (Id);
7658 return Freeze_Node (Id);
7659 end Package_Freeze_Node;
7660
7661 -- Start of processing of Freeze_Subprogram_Body
7662
7663 begin
7664 -- If the instance and the generic body appear within the same unit, and
7665 -- the instance precedes the generic, the freeze node for the instance
7666 -- must appear after that of the generic. If the generic is nested
7667 -- within another instance I2, then current instance must be frozen
7668 -- after I2. In both cases, the freeze nodes are those of enclosing
7669 -- packages. Otherwise, the freeze node is placed at the end of the
7670 -- current declarative part.
7671
7672 Enc_G := Enclosing_Package_Body (Gen_Body);
7673 Enc_I := Enclosing_Package_Body (Inst_Node);
7674 Ensure_Freeze_Node (Pack_Id);
7675 F_Node := Freeze_Node (Pack_Id);
7676
7677 if Is_Generic_Instance (Par)
7678 and then Present (Freeze_Node (Par))
7679 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7680 then
7681 -- The parent was a premature instantiation. Insert freeze node at
7682 -- the end the current declarative part.
7683
7684 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7685 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7686
7687 -- Handle the following case:
7688 --
7689 -- package Parent_Inst is new ...
7690 -- Parent_Inst []
7691 --
7692 -- procedure P ... -- this body freezes Parent_Inst
7693 --
7694 -- package Inst is new ...
7695 --
7696 -- In this particular scenario, the freeze node for Inst must be
7697 -- inserted in the same manner as that of Parent_Inst - before the
7698 -- next source body or at the end of the declarative list (body not
7699 -- available). If body P did not exist and Parent_Inst was frozen
7700 -- after Inst, either by a body following Inst or at the end of the
7701 -- declarative region, the freeze node for Inst must be inserted
7702 -- after that of Parent_Inst. This relation is established by
7703 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7704
7705 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7706 List_Containing (Inst_Node)
7707 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7708 then
7709 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7710
7711 else
7712 Insert_After (Freeze_Node (Par), F_Node);
7713 end if;
7714
7715 -- The body enclosing the instance should be frozen after the body that
7716 -- includes the generic, because the body of the instance may make
7717 -- references to entities therein. If the two are not in the same
7718 -- declarative part, or if the one enclosing the instance is frozen
7719 -- already, freeze the instance at the end of the current declarative
7720 -- part.
7721
7722 elsif Is_Generic_Instance (Par)
7723 and then Present (Freeze_Node (Par))
7724 and then Present (Enc_I)
7725 then
7726 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7727 or else
7728 (Nkind (Enc_I) = N_Package_Body
7729 and then
7730 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7731 then
7732 -- The enclosing package may contain several instances. Rather
7733 -- than computing the earliest point at which to insert its freeze
7734 -- node, we place it at the end of the declarative part of the
7735 -- parent of the generic.
7736
7737 Insert_Freeze_Node_For_Instance
7738 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7739 end if;
7740
7741 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7742
7743 elsif Present (Enc_G)
7744 and then Present (Enc_I)
7745 and then Enc_G /= Enc_I
7746 and then Earlier (Inst_Node, Gen_Body)
7747 then
7748 if Nkind (Enc_G) = N_Package_Body then
7749 E_G_Id := Corresponding_Spec (Enc_G);
7750 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7751 E_G_Id :=
7752 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7753 end if;
7754
7755 -- Freeze package that encloses instance, and place node after the
7756 -- package that encloses generic. If enclosing package is already
7757 -- frozen we have to assume it is at the proper place. This may be a
7758 -- potential ABE that requires dynamic checking. Do not add a freeze
7759 -- node if the package that encloses the generic is inside the body
7760 -- that encloses the instance, because the freeze node would be in
7761 -- the wrong scope. Additional contortions needed if the bodies are
7762 -- within a subunit.
7763
7764 declare
7765 Enclosing_Body : Node_Id;
7766
7767 begin
7768 if Nkind (Enc_I) = N_Package_Body_Stub then
7769 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7770 else
7771 Enclosing_Body := Enc_I;
7772 end if;
7773
7774 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7775 Insert_Freeze_Node_For_Instance
7776 (Enc_G, Package_Freeze_Node (Enc_I));
7777 end if;
7778 end;
7779
7780 -- Freeze enclosing subunit before instance
7781
7782 Ensure_Freeze_Node (E_G_Id);
7783
7784 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7785 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7786 end if;
7787
7788 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7789
7790 else
7791 -- If none of the above, insert freeze node at the end of the current
7792 -- declarative part.
7793
7794 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7795 end if;
7796 end Freeze_Subprogram_Body;
7797
7798 ----------------
7799 -- Get_Gen_Id --
7800 ----------------
7801
7802 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7803 begin
7804 return Generic_Renamings.Table (E).Gen_Id;
7805 end Get_Gen_Id;
7806
7807 ---------------------
7808 -- Get_Instance_Of --
7809 ---------------------
7810
7811 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7812 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7813
7814 begin
7815 if Res /= Assoc_Null then
7816 return Generic_Renamings.Table (Res).Act_Id;
7817 else
7818 -- On exit, entity is not instantiated: not a generic parameter, or
7819 -- else parameter of an inner generic unit.
7820
7821 return A;
7822 end if;
7823 end Get_Instance_Of;
7824
7825 ------------------------------------
7826 -- Get_Package_Instantiation_Node --
7827 ------------------------------------
7828
7829 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7830 Decl : Node_Id := Unit_Declaration_Node (A);
7831 Inst : Node_Id;
7832
7833 begin
7834 -- If the Package_Instantiation attribute has been set on the package
7835 -- entity, then use it directly when it (or its Original_Node) refers
7836 -- to an N_Package_Instantiation node. In principle it should be
7837 -- possible to have this field set in all cases, which should be
7838 -- investigated, and would allow this function to be significantly
7839 -- simplified. ???
7840
7841 Inst := Package_Instantiation (A);
7842
7843 if Present (Inst) then
7844 if Nkind (Inst) = N_Package_Instantiation then
7845 return Inst;
7846
7847 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7848 return Original_Node (Inst);
7849 end if;
7850 end if;
7851
7852 -- If the instantiation is a compilation unit that does not need body
7853 -- then the instantiation node has been rewritten as a package
7854 -- declaration for the instance, and we return the original node.
7855
7856 -- If it is a compilation unit and the instance node has not been
7857 -- rewritten, then it is still the unit of the compilation. Finally, if
7858 -- a body is present, this is a parent of the main unit whose body has
7859 -- been compiled for inlining purposes, and the instantiation node has
7860 -- been rewritten with the instance body.
7861
7862 -- Otherwise the instantiation node appears after the declaration. If
7863 -- the entity is a formal package, the declaration may have been
7864 -- rewritten as a generic declaration (in the case of a formal with box)
7865 -- or left as a formal package declaration if it has actuals, and is
7866 -- found with a forward search.
7867
7868 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7869 if Nkind (Decl) = N_Package_Declaration
7870 and then Present (Corresponding_Body (Decl))
7871 then
7872 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7873 end if;
7874
7875 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
7876 return Original_Node (Decl);
7877 else
7878 return Unit (Parent (Decl));
7879 end if;
7880
7881 elsif Nkind (Decl) = N_Package_Declaration
7882 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
7883 then
7884 return Original_Node (Decl);
7885
7886 else
7887 Inst := Next (Decl);
7888 while not Nkind_In (Inst, N_Package_Instantiation,
7889 N_Formal_Package_Declaration)
7890 loop
7891 Next (Inst);
7892 end loop;
7893
7894 return Inst;
7895 end if;
7896 end Get_Package_Instantiation_Node;
7897
7898 ------------------------
7899 -- Has_Been_Exchanged --
7900 ------------------------
7901
7902 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
7903 Next : Elmt_Id;
7904
7905 begin
7906 Next := First_Elmt (Exchanged_Views);
7907 while Present (Next) loop
7908 if Full_View (Node (Next)) = E then
7909 return True;
7910 end if;
7911
7912 Next_Elmt (Next);
7913 end loop;
7914
7915 return False;
7916 end Has_Been_Exchanged;
7917
7918 ----------
7919 -- Hash --
7920 ----------
7921
7922 function Hash (F : Entity_Id) return HTable_Range is
7923 begin
7924 return HTable_Range (F mod HTable_Size);
7925 end Hash;
7926
7927 ------------------------
7928 -- Hide_Current_Scope --
7929 ------------------------
7930
7931 procedure Hide_Current_Scope is
7932 C : constant Entity_Id := Current_Scope;
7933 E : Entity_Id;
7934
7935 begin
7936 Set_Is_Hidden_Open_Scope (C);
7937
7938 E := First_Entity (C);
7939 while Present (E) loop
7940 if Is_Immediately_Visible (E) then
7941 Set_Is_Immediately_Visible (E, False);
7942 Append_Elmt (E, Hidden_Entities);
7943 end if;
7944
7945 Next_Entity (E);
7946 end loop;
7947
7948 -- Make the scope name invisible as well. This is necessary, but might
7949 -- conflict with calls to Rtsfind later on, in case the scope is a
7950 -- predefined one. There is no clean solution to this problem, so for
7951 -- now we depend on the user not redefining Standard itself in one of
7952 -- the parent units.
7953
7954 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
7955 Set_Is_Immediately_Visible (C, False);
7956 Append_Elmt (C, Hidden_Entities);
7957 end if;
7958
7959 end Hide_Current_Scope;
7960
7961 --------------
7962 -- Init_Env --
7963 --------------
7964
7965 procedure Init_Env is
7966 Saved : Instance_Env;
7967
7968 begin
7969 Saved.Instantiated_Parent := Current_Instantiated_Parent;
7970 Saved.Exchanged_Views := Exchanged_Views;
7971 Saved.Hidden_Entities := Hidden_Entities;
7972 Saved.Current_Sem_Unit := Current_Sem_Unit;
7973 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
7974 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
7975
7976 -- Save configuration switches. These may be reset if the unit is a
7977 -- predefined unit, and the current mode is not Ada 2005.
7978
7979 Save_Opt_Config_Switches (Saved.Switches);
7980
7981 Instance_Envs.Append (Saved);
7982
7983 Exchanged_Views := New_Elmt_List;
7984 Hidden_Entities := New_Elmt_List;
7985
7986 -- Make dummy entry for Instantiated parent. If generic unit is legal,
7987 -- this is set properly in Set_Instance_Env.
7988
7989 Current_Instantiated_Parent :=
7990 (Current_Scope, Current_Scope, Assoc_Null);
7991 end Init_Env;
7992
7993 ------------------------------
7994 -- In_Same_Declarative_Part --
7995 ------------------------------
7996
7997 function In_Same_Declarative_Part
7998 (F_Node : Node_Id;
7999 Inst : Node_Id) return Boolean
8000 is
8001 Decls : constant Node_Id := Parent (F_Node);
8002 Nod : Node_Id := Parent (Inst);
8003
8004 begin
8005 while Present (Nod) loop
8006 if Nod = Decls then
8007 return True;
8008
8009 elsif Nkind_In (Nod, N_Subprogram_Body,
8010 N_Package_Body,
8011 N_Package_Declaration,
8012 N_Task_Body,
8013 N_Protected_Body,
8014 N_Block_Statement)
8015 then
8016 return False;
8017
8018 elsif Nkind (Nod) = N_Subunit then
8019 Nod := Corresponding_Stub (Nod);
8020
8021 elsif Nkind (Nod) = N_Compilation_Unit then
8022 return False;
8023
8024 else
8025 Nod := Parent (Nod);
8026 end if;
8027 end loop;
8028
8029 return False;
8030 end In_Same_Declarative_Part;
8031
8032 ---------------------
8033 -- In_Main_Context --
8034 ---------------------
8035
8036 function In_Main_Context (E : Entity_Id) return Boolean is
8037 Context : List_Id;
8038 Clause : Node_Id;
8039 Nam : Node_Id;
8040
8041 begin
8042 if not Is_Compilation_Unit (E)
8043 or else Ekind (E) /= E_Package
8044 or else In_Private_Part (E)
8045 then
8046 return False;
8047 end if;
8048
8049 Context := Context_Items (Cunit (Main_Unit));
8050
8051 Clause := First (Context);
8052 while Present (Clause) loop
8053 if Nkind (Clause) = N_With_Clause then
8054 Nam := Name (Clause);
8055
8056 -- If the current scope is part of the context of the main unit,
8057 -- analysis of the corresponding with_clause is not complete, and
8058 -- the entity is not set. We use the Chars field directly, which
8059 -- might produce false positives in rare cases, but guarantees
8060 -- that we produce all the instance bodies we will need.
8061
8062 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8063 or else (Nkind (Nam) = N_Selected_Component
8064 and then Chars (Selector_Name (Nam)) = Chars (E))
8065 then
8066 return True;
8067 end if;
8068 end if;
8069
8070 Next (Clause);
8071 end loop;
8072
8073 return False;
8074 end In_Main_Context;
8075
8076 ---------------------
8077 -- Inherit_Context --
8078 ---------------------
8079
8080 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8081 Current_Context : List_Id;
8082 Current_Unit : Node_Id;
8083 Item : Node_Id;
8084 New_I : Node_Id;
8085
8086 Clause : Node_Id;
8087 OK : Boolean;
8088 Lib_Unit : Node_Id;
8089
8090 begin
8091 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8092
8093 -- The inherited context is attached to the enclosing compilation
8094 -- unit. This is either the main unit, or the declaration for the
8095 -- main unit (in case the instantiation appears within the package
8096 -- declaration and the main unit is its body).
8097
8098 Current_Unit := Parent (Inst);
8099 while Present (Current_Unit)
8100 and then Nkind (Current_Unit) /= N_Compilation_Unit
8101 loop
8102 Current_Unit := Parent (Current_Unit);
8103 end loop;
8104
8105 Current_Context := Context_Items (Current_Unit);
8106
8107 Item := First (Context_Items (Parent (Gen_Decl)));
8108 while Present (Item) loop
8109 if Nkind (Item) = N_With_Clause then
8110 Lib_Unit := Library_Unit (Item);
8111
8112 -- Take care to prevent direct cyclic with's
8113
8114 if Lib_Unit /= Current_Unit then
8115
8116 -- Do not add a unit if it is already in the context
8117
8118 Clause := First (Current_Context);
8119 OK := True;
8120 while Present (Clause) loop
8121 if Nkind (Clause) = N_With_Clause and then
8122 Library_Unit (Clause) = Lib_Unit
8123 then
8124 OK := False;
8125 exit;
8126 end if;
8127
8128 Next (Clause);
8129 end loop;
8130
8131 if OK then
8132 New_I := New_Copy (Item);
8133 Set_Implicit_With (New_I, True);
8134 Set_Implicit_With_From_Instantiation (New_I, True);
8135 Append (New_I, Current_Context);
8136 end if;
8137 end if;
8138 end if;
8139
8140 Next (Item);
8141 end loop;
8142 end if;
8143 end Inherit_Context;
8144
8145 ----------------
8146 -- Initialize --
8147 ----------------
8148
8149 procedure Initialize is
8150 begin
8151 Generic_Renamings.Init;
8152 Instance_Envs.Init;
8153 Generic_Flags.Init;
8154 Generic_Renamings_HTable.Reset;
8155 Circularity_Detected := False;
8156 Exchanged_Views := No_Elist;
8157 Hidden_Entities := No_Elist;
8158 end Initialize;
8159
8160 -------------------------------------
8161 -- Insert_Freeze_Node_For_Instance --
8162 -------------------------------------
8163
8164 procedure Insert_Freeze_Node_For_Instance
8165 (N : Node_Id;
8166 F_Node : Node_Id)
8167 is
8168 Decl : Node_Id;
8169 Decls : List_Id;
8170 Inst : Entity_Id;
8171 Par_N : Node_Id;
8172
8173 function Enclosing_Body (N : Node_Id) return Node_Id;
8174 -- Find enclosing package or subprogram body, if any. Freeze node may
8175 -- be placed at end of current declarative list if previous instance
8176 -- and current one have different enclosing bodies.
8177
8178 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8179 -- Find the local instance, if any, that declares the generic that is
8180 -- being instantiated. If present, the freeze node for this instance
8181 -- must follow the freeze node for the previous instance.
8182
8183 --------------------
8184 -- Enclosing_Body --
8185 --------------------
8186
8187 function Enclosing_Body (N : Node_Id) return Node_Id is
8188 P : Node_Id;
8189
8190 begin
8191 P := Parent (N);
8192 while Present (P)
8193 and then Nkind (Parent (P)) /= N_Compilation_Unit
8194 loop
8195 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8196 if Nkind (Parent (P)) = N_Subunit then
8197 return Corresponding_Stub (Parent (P));
8198 else
8199 return P;
8200 end if;
8201 end if;
8202
8203 P := True_Parent (P);
8204 end loop;
8205
8206 return Empty;
8207 end Enclosing_Body;
8208
8209 -----------------------
8210 -- Previous_Instance --
8211 -----------------------
8212
8213 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8214 S : Entity_Id;
8215
8216 begin
8217 S := Scope (Gen);
8218 while Present (S)
8219 and then S /= Standard_Standard
8220 loop
8221 if Is_Generic_Instance (S)
8222 and then In_Same_Source_Unit (S, N)
8223 then
8224 return S;
8225 end if;
8226
8227 S := Scope (S);
8228 end loop;
8229
8230 return Empty;
8231 end Previous_Instance;
8232
8233 -- Start of processing for Insert_Freeze_Node_For_Instance
8234
8235 begin
8236 if not Is_List_Member (F_Node) then
8237 Decl := N;
8238 Decls := List_Containing (N);
8239 Inst := Entity (F_Node);
8240 Par_N := Parent (Decls);
8241
8242 -- When processing a subprogram instantiation, utilize the actual
8243 -- subprogram instantiation rather than its package wrapper as it
8244 -- carries all the context information.
8245
8246 if Is_Wrapper_Package (Inst) then
8247 Inst := Related_Instance (Inst);
8248 end if;
8249
8250 -- If this is a package instance, check whether the generic is
8251 -- declared in a previous instance and the current instance is
8252 -- not within the previous one.
8253
8254 if Present (Generic_Parent (Parent (Inst)))
8255 and then Is_In_Main_Unit (N)
8256 then
8257 declare
8258 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8259 Par_I : constant Entity_Id :=
8260 Previous_Instance
8261 (Generic_Parent (Parent (Inst)));
8262 Scop : Entity_Id;
8263
8264 begin
8265 if Present (Par_I)
8266 and then Earlier (N, Freeze_Node (Par_I))
8267 then
8268 Scop := Scope (Inst);
8269
8270 -- If the current instance is within the one that contains
8271 -- the generic, the freeze node for the current one must
8272 -- appear in the current declarative part. Ditto, if the
8273 -- current instance is within another package instance or
8274 -- within a body that does not enclose the current instance.
8275 -- In these three cases the freeze node of the previous
8276 -- instance is not relevant.
8277
8278 while Present (Scop)
8279 and then Scop /= Standard_Standard
8280 loop
8281 exit when Scop = Par_I
8282 or else
8283 (Is_Generic_Instance (Scop)
8284 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8285 Scop := Scope (Scop);
8286 end loop;
8287
8288 -- Previous instance encloses current instance
8289
8290 if Scop = Par_I then
8291 null;
8292
8293 -- If the next node is a source body we must freeze in
8294 -- the current scope as well.
8295
8296 elsif Present (Next (N))
8297 and then Nkind_In (Next (N),
8298 N_Subprogram_Body, N_Package_Body)
8299 and then Comes_From_Source (Next (N))
8300 then
8301 null;
8302
8303 -- Current instance is within an unrelated instance
8304
8305 elsif Is_Generic_Instance (Scop) then
8306 null;
8307
8308 -- Current instance is within an unrelated body
8309
8310 elsif Present (Enclosing_N)
8311 and then Enclosing_N /= Enclosing_Body (Par_I)
8312 then
8313 null;
8314
8315 else
8316 Insert_After (Freeze_Node (Par_I), F_Node);
8317 return;
8318 end if;
8319 end if;
8320 end;
8321 end if;
8322
8323 -- When the instantiation occurs in a package declaration, append the
8324 -- freeze node to the private declarations (if any).
8325
8326 if Nkind (Par_N) = N_Package_Specification
8327 and then Decls = Visible_Declarations (Par_N)
8328 and then Present (Private_Declarations (Par_N))
8329 and then not Is_Empty_List (Private_Declarations (Par_N))
8330 then
8331 Decls := Private_Declarations (Par_N);
8332 Decl := First (Decls);
8333 end if;
8334
8335 -- Determine the proper freeze point of a package instantiation. We
8336 -- adhere to the general rule of a package or subprogram body causing
8337 -- freezing of anything before it in the same declarative region. In
8338 -- this case, the proper freeze point of a package instantiation is
8339 -- before the first source body which follows, or before a stub. This
8340 -- ensures that entities coming from the instance are already frozen
8341 -- and usable in source bodies.
8342
8343 if Nkind (Par_N) /= N_Package_Declaration
8344 and then Ekind (Inst) = E_Package
8345 and then Is_Generic_Instance (Inst)
8346 and then
8347 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8348 then
8349 while Present (Decl) loop
8350 if (Nkind (Decl) in N_Unit_Body
8351 or else
8352 Nkind (Decl) in N_Body_Stub)
8353 and then Comes_From_Source (Decl)
8354 then
8355 Insert_Before (Decl, F_Node);
8356 return;
8357 end if;
8358
8359 Next (Decl);
8360 end loop;
8361 end if;
8362
8363 -- In a package declaration, or if no previous body, insert at end
8364 -- of list.
8365
8366 Set_Sloc (F_Node, Sloc (Last (Decls)));
8367 Insert_After (Last (Decls), F_Node);
8368 end if;
8369 end Insert_Freeze_Node_For_Instance;
8370
8371 ------------------
8372 -- Install_Body --
8373 ------------------
8374
8375 procedure Install_Body
8376 (Act_Body : Node_Id;
8377 N : Node_Id;
8378 Gen_Body : Node_Id;
8379 Gen_Decl : Node_Id)
8380 is
8381 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8382 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8383 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8384 Par : constant Entity_Id := Scope (Gen_Id);
8385 Gen_Unit : constant Node_Id :=
8386 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8387 Orig_Body : Node_Id := Gen_Body;
8388 F_Node : Node_Id;
8389 Body_Unit : Node_Id;
8390
8391 Must_Delay : Boolean;
8392
8393 function In_Same_Enclosing_Subp return Boolean;
8394 -- Check whether instance and generic body are within same subprogram.
8395
8396 function True_Sloc (N : Node_Id) return Source_Ptr;
8397 -- If the instance is nested inside a generic unit, the Sloc of the
8398 -- instance indicates the place of the original definition, not the
8399 -- point of the current enclosing instance. Pending a better usage of
8400 -- Slocs to indicate instantiation places, we determine the place of
8401 -- origin of a node by finding the maximum sloc of any ancestor node.
8402 -- Why is this not equivalent to Top_Level_Location ???
8403
8404 ----------------------------
8405 -- In_Same_Enclosing_Subp --
8406 ----------------------------
8407
8408 function In_Same_Enclosing_Subp return Boolean is
8409 Scop : Entity_Id;
8410 Subp : Entity_Id;
8411
8412 begin
8413 Scop := Scope (Act_Id);
8414 while Scop /= Standard_Standard
8415 and then not Is_Overloadable (Scop)
8416 loop
8417 Scop := Scope (Scop);
8418 end loop;
8419
8420 if Scop = Standard_Standard then
8421 return False;
8422 else
8423 Subp := Scop;
8424 end if;
8425
8426 Scop := Scope (Gen_Id);
8427 while Scop /= Standard_Standard loop
8428 if Scop = Subp then
8429 return True;
8430 else
8431 Scop := Scope (Scop);
8432 end if;
8433 end loop;
8434
8435 return False;
8436 end In_Same_Enclosing_Subp;
8437
8438 ---------------
8439 -- True_Sloc --
8440 ---------------
8441
8442 function True_Sloc (N : Node_Id) return Source_Ptr is
8443 Res : Source_Ptr;
8444 N1 : Node_Id;
8445
8446 begin
8447 Res := Sloc (N);
8448 N1 := N;
8449 while Present (N1) and then N1 /= Act_Unit loop
8450 if Sloc (N1) > Res then
8451 Res := Sloc (N1);
8452 end if;
8453
8454 N1 := Parent (N1);
8455 end loop;
8456
8457 return Res;
8458 end True_Sloc;
8459
8460 -- Start of processing for Install_Body
8461
8462 begin
8463 -- If the body is a subunit, the freeze point is the corresponding stub
8464 -- in the current compilation, not the subunit itself.
8465
8466 if Nkind (Parent (Gen_Body)) = N_Subunit then
8467 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8468 else
8469 Orig_Body := Gen_Body;
8470 end if;
8471
8472 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8473
8474 -- If the instantiation and the generic definition appear in the same
8475 -- package declaration, this is an early instantiation. If they appear
8476 -- in the same declarative part, it is an early instantiation only if
8477 -- the generic body appears textually later, and the generic body is
8478 -- also in the main unit.
8479
8480 -- If instance is nested within a subprogram, and the generic body
8481 -- is not, the instance is delayed because the enclosing body is. If
8482 -- instance and body are within the same scope, or the same subprogram
8483 -- body, indicate explicitly that the instance is delayed.
8484
8485 Must_Delay :=
8486 (Gen_Unit = Act_Unit
8487 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8488 N_Generic_Package_Declaration)
8489 or else (Gen_Unit = Body_Unit
8490 and then True_Sloc (N) < Sloc (Orig_Body)))
8491 and then Is_In_Main_Unit (Gen_Unit)
8492 and then (Scope (Act_Id) = Scope (Gen_Id)
8493 or else In_Same_Enclosing_Subp));
8494
8495 -- If this is an early instantiation, the freeze node is placed after
8496 -- the generic body. Otherwise, if the generic appears in an instance,
8497 -- we cannot freeze the current instance until the outer one is frozen.
8498 -- This is only relevant if the current instance is nested within some
8499 -- inner scope not itself within the outer instance. If this scope is
8500 -- a package body in the same declarative part as the outer instance,
8501 -- then that body needs to be frozen after the outer instance. Finally,
8502 -- if no delay is needed, we place the freeze node at the end of the
8503 -- current declarative part.
8504
8505 if Expander_Active then
8506 Ensure_Freeze_Node (Act_Id);
8507 F_Node := Freeze_Node (Act_Id);
8508
8509 if Must_Delay then
8510 Insert_After (Orig_Body, F_Node);
8511
8512 elsif Is_Generic_Instance (Par)
8513 and then Present (Freeze_Node (Par))
8514 and then Scope (Act_Id) /= Par
8515 then
8516 -- Freeze instance of inner generic after instance of enclosing
8517 -- generic.
8518
8519 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8520
8521 -- Handle the following case:
8522
8523 -- package Parent_Inst is new ...
8524 -- Parent_Inst []
8525
8526 -- procedure P ... -- this body freezes Parent_Inst
8527
8528 -- package Inst is new ...
8529
8530 -- In this particular scenario, the freeze node for Inst must
8531 -- be inserted in the same manner as that of Parent_Inst,
8532 -- before the next source body or at the end of the declarative
8533 -- list (body not available). If body P did not exist and
8534 -- Parent_Inst was frozen after Inst, either by a body
8535 -- following Inst or at the end of the declarative region,
8536 -- the freeze node for Inst must be inserted after that of
8537 -- Parent_Inst. This relation is established by comparing
8538 -- the Slocs of Parent_Inst freeze node and Inst.
8539
8540 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8541 List_Containing (N)
8542 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8543 then
8544 Insert_Freeze_Node_For_Instance (N, F_Node);
8545 else
8546 Insert_After (Freeze_Node (Par), F_Node);
8547 end if;
8548
8549 -- Freeze package enclosing instance of inner generic after
8550 -- instance of enclosing generic.
8551
8552 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8553 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8554 then
8555 declare
8556 Enclosing : Entity_Id;
8557
8558 begin
8559 Enclosing := Corresponding_Spec (Parent (N));
8560
8561 if No (Enclosing) then
8562 Enclosing := Defining_Entity (Parent (N));
8563 end if;
8564
8565 Insert_Freeze_Node_For_Instance (N, F_Node);
8566 Ensure_Freeze_Node (Enclosing);
8567
8568 if not Is_List_Member (Freeze_Node (Enclosing)) then
8569
8570 -- The enclosing context is a subunit, insert the freeze
8571 -- node after the stub.
8572
8573 if Nkind (Parent (Parent (N))) = N_Subunit then
8574 Insert_Freeze_Node_For_Instance
8575 (Corresponding_Stub (Parent (Parent (N))),
8576 Freeze_Node (Enclosing));
8577
8578 -- The enclosing context is a package with a stub body
8579 -- which has already been replaced by the real body.
8580 -- Insert the freeze node after the actual body.
8581
8582 elsif Ekind (Enclosing) = E_Package
8583 and then Present (Body_Entity (Enclosing))
8584 and then Was_Originally_Stub
8585 (Parent (Body_Entity (Enclosing)))
8586 then
8587 Insert_Freeze_Node_For_Instance
8588 (Parent (Body_Entity (Enclosing)),
8589 Freeze_Node (Enclosing));
8590
8591 -- The parent instance has been frozen before the body of
8592 -- the enclosing package, insert the freeze node after
8593 -- the body.
8594
8595 elsif List_Containing (Freeze_Node (Par)) =
8596 List_Containing (Parent (N))
8597 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8598 then
8599 Insert_Freeze_Node_For_Instance
8600 (Parent (N), Freeze_Node (Enclosing));
8601
8602 else
8603 Insert_After
8604 (Freeze_Node (Par), Freeze_Node (Enclosing));
8605 end if;
8606 end if;
8607 end;
8608
8609 else
8610 Insert_Freeze_Node_For_Instance (N, F_Node);
8611 end if;
8612
8613 else
8614 Insert_Freeze_Node_For_Instance (N, F_Node);
8615 end if;
8616 end if;
8617
8618 Set_Is_Frozen (Act_Id);
8619 Insert_Before (N, Act_Body);
8620 Mark_Rewrite_Insertion (Act_Body);
8621 end Install_Body;
8622
8623 -----------------------------
8624 -- Install_Formal_Packages --
8625 -----------------------------
8626
8627 procedure Install_Formal_Packages (Par : Entity_Id) is
8628 E : Entity_Id;
8629 Gen : Entity_Id;
8630 Gen_E : Entity_Id := Empty;
8631
8632 begin
8633 E := First_Entity (Par);
8634
8635 -- If we are installing an instance parent, locate the formal packages
8636 -- of its generic parent.
8637
8638 if Is_Generic_Instance (Par) then
8639 Gen := Generic_Parent (Package_Specification (Par));
8640 Gen_E := First_Entity (Gen);
8641 end if;
8642
8643 while Present (E) loop
8644 if Ekind (E) = E_Package
8645 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
8646 then
8647 -- If this is the renaming for the parent instance, done
8648
8649 if Renamed_Object (E) = Par then
8650 exit;
8651
8652 -- The visibility of a formal of an enclosing generic is already
8653 -- correct.
8654
8655 elsif Denotes_Formal_Package (E) then
8656 null;
8657
8658 elsif Present (Associated_Formal_Package (E)) then
8659 Check_Generic_Actuals (Renamed_Object (E), True);
8660 Set_Is_Hidden (E, False);
8661
8662 -- Find formal package in generic unit that corresponds to
8663 -- (instance of) formal package in instance.
8664
8665 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
8666 Next_Entity (Gen_E);
8667 end loop;
8668
8669 if Present (Gen_E) then
8670 Map_Formal_Package_Entities (Gen_E, E);
8671 end if;
8672 end if;
8673 end if;
8674
8675 Next_Entity (E);
8676 if Present (Gen_E) then
8677 Next_Entity (Gen_E);
8678 end if;
8679 end loop;
8680 end Install_Formal_Packages;
8681
8682 --------------------
8683 -- Install_Parent --
8684 --------------------
8685
8686 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
8687 Ancestors : constant Elist_Id := New_Elmt_List;
8688 S : constant Entity_Id := Current_Scope;
8689 Inst_Par : Entity_Id;
8690 First_Par : Entity_Id;
8691 Inst_Node : Node_Id;
8692 Gen_Par : Entity_Id;
8693 First_Gen : Entity_Id;
8694 Elmt : Elmt_Id;
8695
8696 procedure Install_Noninstance_Specs (Par : Entity_Id);
8697 -- Install the scopes of noninstance parent units ending with Par
8698
8699 procedure Install_Spec (Par : Entity_Id);
8700 -- The child unit is within the declarative part of the parent, so the
8701 -- declarations within the parent are immediately visible.
8702
8703 -------------------------------
8704 -- Install_Noninstance_Specs --
8705 -------------------------------
8706
8707 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8708 begin
8709 if Present (Par)
8710 and then Par /= Standard_Standard
8711 and then not In_Open_Scopes (Par)
8712 then
8713 Install_Noninstance_Specs (Scope (Par));
8714 Install_Spec (Par);
8715 end if;
8716 end Install_Noninstance_Specs;
8717
8718 ------------------
8719 -- Install_Spec --
8720 ------------------
8721
8722 procedure Install_Spec (Par : Entity_Id) is
8723 Spec : constant Node_Id := Package_Specification (Par);
8724
8725 begin
8726 -- If this parent of the child instance is a top-level unit,
8727 -- then record the unit and its visibility for later resetting in
8728 -- Remove_Parent. We exclude units that are generic instances, as we
8729 -- only want to record this information for the ultimate top-level
8730 -- noninstance parent (is that always correct???).
8731
8732 if Scope (Par) = Standard_Standard
8733 and then not Is_Generic_Instance (Par)
8734 then
8735 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8736 Instance_Parent_Unit := Par;
8737 end if;
8738
8739 -- Open the parent scope and make it and its declarations visible.
8740 -- If this point is not within a body, then only the visible
8741 -- declarations should be made visible, and installation of the
8742 -- private declarations is deferred until the appropriate point
8743 -- within analysis of the spec being instantiated (see the handling
8744 -- of parent visibility in Analyze_Package_Specification). This is
8745 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8746 -- private view problems that occur when compiling instantiations of
8747 -- a generic child of that package (Generic_Dispatching_Constructor).
8748 -- If the instance freezes a tagged type, inlinings of operations
8749 -- from Ada.Tags may need the full view of type Tag. If inlining took
8750 -- proper account of establishing visibility of inlined subprograms'
8751 -- parents then it should be possible to remove this
8752 -- special check. ???
8753
8754 Push_Scope (Par);
8755 Set_Is_Immediately_Visible (Par);
8756 Install_Visible_Declarations (Par);
8757 Set_Use (Visible_Declarations (Spec));
8758
8759 if In_Body or else Is_RTU (Par, Ada_Tags) then
8760 Install_Private_Declarations (Par);
8761 Set_Use (Private_Declarations (Spec));
8762 end if;
8763 end Install_Spec;
8764
8765 -- Start of processing for Install_Parent
8766
8767 begin
8768 -- We need to install the parent instance to compile the instantiation
8769 -- of the child, but the child instance must appear in the current
8770 -- scope. Given that we cannot place the parent above the current scope
8771 -- in the scope stack, we duplicate the current scope and unstack both
8772 -- after the instantiation is complete.
8773
8774 -- If the parent is itself the instantiation of a child unit, we must
8775 -- also stack the instantiation of its parent, and so on. Each such
8776 -- ancestor is the prefix of the name in a prior instantiation.
8777
8778 -- If this is a nested instance, the parent unit itself resolves to
8779 -- a renaming of the parent instance, whose declaration we need.
8780
8781 -- Finally, the parent may be a generic (not an instance) when the
8782 -- child unit appears as a formal package.
8783
8784 Inst_Par := P;
8785
8786 if Present (Renamed_Entity (Inst_Par)) then
8787 Inst_Par := Renamed_Entity (Inst_Par);
8788 end if;
8789
8790 First_Par := Inst_Par;
8791
8792 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8793
8794 First_Gen := Gen_Par;
8795
8796 while Present (Gen_Par)
8797 and then Is_Child_Unit (Gen_Par)
8798 loop
8799 -- Load grandparent instance as well
8800
8801 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8802
8803 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8804 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8805
8806 if Present (Renamed_Entity (Inst_Par)) then
8807 Inst_Par := Renamed_Entity (Inst_Par);
8808 end if;
8809
8810 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8811
8812 if Present (Gen_Par) then
8813 Prepend_Elmt (Inst_Par, Ancestors);
8814
8815 else
8816 -- Parent is not the name of an instantiation
8817
8818 Install_Noninstance_Specs (Inst_Par);
8819 exit;
8820 end if;
8821
8822 else
8823 -- Previous error
8824
8825 exit;
8826 end if;
8827 end loop;
8828
8829 if Present (First_Gen) then
8830 Append_Elmt (First_Par, Ancestors);
8831 else
8832 Install_Noninstance_Specs (First_Par);
8833 end if;
8834
8835 if not Is_Empty_Elmt_List (Ancestors) then
8836 Elmt := First_Elmt (Ancestors);
8837 while Present (Elmt) loop
8838 Install_Spec (Node (Elmt));
8839 Install_Formal_Packages (Node (Elmt));
8840 Next_Elmt (Elmt);
8841 end loop;
8842 end if;
8843
8844 if not In_Body then
8845 Push_Scope (S);
8846 end if;
8847 end Install_Parent;
8848
8849 -------------------------------
8850 -- Install_Hidden_Primitives --
8851 -------------------------------
8852
8853 procedure Install_Hidden_Primitives
8854 (Prims_List : in out Elist_Id;
8855 Gen_T : Entity_Id;
8856 Act_T : Entity_Id)
8857 is
8858 Elmt : Elmt_Id;
8859 List : Elist_Id := No_Elist;
8860 Prim_G_Elmt : Elmt_Id;
8861 Prim_A_Elmt : Elmt_Id;
8862 Prim_G : Node_Id;
8863 Prim_A : Node_Id;
8864
8865 begin
8866 -- No action needed in case of serious errors because we cannot trust
8867 -- in the order of primitives
8868
8869 if Serious_Errors_Detected > 0 then
8870 return;
8871
8872 -- No action possible if we don't have available the list of primitive
8873 -- operations
8874
8875 elsif No (Gen_T)
8876 or else not Is_Record_Type (Gen_T)
8877 or else not Is_Tagged_Type (Gen_T)
8878 or else not Is_Record_Type (Act_T)
8879 or else not Is_Tagged_Type (Act_T)
8880 then
8881 return;
8882
8883 -- There is no need to handle interface types since their primitives
8884 -- cannot be hidden
8885
8886 elsif Is_Interface (Gen_T) then
8887 return;
8888 end if;
8889
8890 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
8891
8892 if not Is_Class_Wide_Type (Act_T) then
8893 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
8894 else
8895 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
8896 end if;
8897
8898 loop
8899 -- Skip predefined primitives in the generic formal
8900
8901 while Present (Prim_G_Elmt)
8902 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
8903 loop
8904 Next_Elmt (Prim_G_Elmt);
8905 end loop;
8906
8907 -- Skip predefined primitives in the generic actual
8908
8909 while Present (Prim_A_Elmt)
8910 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
8911 loop
8912 Next_Elmt (Prim_A_Elmt);
8913 end loop;
8914
8915 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
8916
8917 Prim_G := Node (Prim_G_Elmt);
8918 Prim_A := Node (Prim_A_Elmt);
8919
8920 -- There is no need to handle interface primitives because their
8921 -- primitives are not hidden
8922
8923 exit when Present (Interface_Alias (Prim_G));
8924
8925 -- Here we install one hidden primitive
8926
8927 if Chars (Prim_G) /= Chars (Prim_A)
8928 and then Has_Suffix (Prim_A, 'P')
8929 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
8930 then
8931 Set_Chars (Prim_A, Chars (Prim_G));
8932 Append_New_Elmt (Prim_A, To => List);
8933 end if;
8934
8935 Next_Elmt (Prim_A_Elmt);
8936 Next_Elmt (Prim_G_Elmt);
8937 end loop;
8938
8939 -- Append the elements to the list of temporarily visible primitives
8940 -- avoiding duplicates.
8941
8942 if Present (List) then
8943 if No (Prims_List) then
8944 Prims_List := New_Elmt_List;
8945 end if;
8946
8947 Elmt := First_Elmt (List);
8948 while Present (Elmt) loop
8949 Append_Unique_Elmt (Node (Elmt), Prims_List);
8950 Next_Elmt (Elmt);
8951 end loop;
8952 end if;
8953 end Install_Hidden_Primitives;
8954
8955 -------------------------------
8956 -- Restore_Hidden_Primitives --
8957 -------------------------------
8958
8959 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
8960 Prim_Elmt : Elmt_Id;
8961 Prim : Node_Id;
8962
8963 begin
8964 if Prims_List /= No_Elist then
8965 Prim_Elmt := First_Elmt (Prims_List);
8966 while Present (Prim_Elmt) loop
8967 Prim := Node (Prim_Elmt);
8968 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
8969 Next_Elmt (Prim_Elmt);
8970 end loop;
8971
8972 Prims_List := No_Elist;
8973 end if;
8974 end Restore_Hidden_Primitives;
8975
8976 --------------------------------
8977 -- Instantiate_Formal_Package --
8978 --------------------------------
8979
8980 function Instantiate_Formal_Package
8981 (Formal : Node_Id;
8982 Actual : Node_Id;
8983 Analyzed_Formal : Node_Id) return List_Id
8984 is
8985 Loc : constant Source_Ptr := Sloc (Actual);
8986 Actual_Pack : Entity_Id;
8987 Formal_Pack : Entity_Id;
8988 Gen_Parent : Entity_Id;
8989 Decls : List_Id;
8990 Nod : Node_Id;
8991 Parent_Spec : Node_Id;
8992
8993 procedure Find_Matching_Actual
8994 (F : Node_Id;
8995 Act : in out Entity_Id);
8996 -- We need to associate each formal entity in the formal package with
8997 -- the corresponding entity in the actual package. The actual package
8998 -- has been analyzed and possibly expanded, and as a result there is
8999 -- no one-to-one correspondence between the two lists (for example,
9000 -- the actual may include subtypes, itypes, and inherited primitive
9001 -- operations, interspersed among the renaming declarations for the
9002 -- actuals) . We retrieve the corresponding actual by name because each
9003 -- actual has the same name as the formal, and they do appear in the
9004 -- same order.
9005
9006 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9007 -- Retrieve entity of defining entity of generic formal parameter.
9008 -- Only the declarations of formals need to be considered when
9009 -- linking them to actuals, but the declarative list may include
9010 -- internal entities generated during analysis, and those are ignored.
9011
9012 procedure Match_Formal_Entity
9013 (Formal_Node : Node_Id;
9014 Formal_Ent : Entity_Id;
9015 Actual_Ent : Entity_Id);
9016 -- Associates the formal entity with the actual. In the case where
9017 -- Formal_Ent is a formal package, this procedure iterates through all
9018 -- of its formals and enters associations between the actuals occurring
9019 -- in the formal package's corresponding actual package (given by
9020 -- Actual_Ent) and the formal package's formal parameters. This
9021 -- procedure recurses if any of the parameters is itself a package.
9022
9023 function Is_Instance_Of
9024 (Act_Spec : Entity_Id;
9025 Gen_Anc : Entity_Id) return Boolean;
9026 -- The actual can be an instantiation of a generic within another
9027 -- instance, in which case there is no direct link from it to the
9028 -- original generic ancestor. In that case, we recognize that the
9029 -- ultimate ancestor is the same by examining names and scopes.
9030
9031 procedure Process_Nested_Formal (Formal : Entity_Id);
9032 -- If the current formal is declared with a box, its own formals are
9033 -- visible in the instance, as they were in the generic, and their
9034 -- Hidden flag must be reset. If some of these formals are themselves
9035 -- packages declared with a box, the processing must be recursive.
9036
9037 --------------------------
9038 -- Find_Matching_Actual --
9039 --------------------------
9040
9041 procedure Find_Matching_Actual
9042 (F : Node_Id;
9043 Act : in out Entity_Id)
9044 is
9045 Formal_Ent : Entity_Id;
9046
9047 begin
9048 case Nkind (Original_Node (F)) is
9049 when N_Formal_Object_Declaration |
9050 N_Formal_Type_Declaration =>
9051 Formal_Ent := Defining_Identifier (F);
9052
9053 while Chars (Act) /= Chars (Formal_Ent) loop
9054 Next_Entity (Act);
9055 end loop;
9056
9057 when N_Formal_Subprogram_Declaration |
9058 N_Formal_Package_Declaration |
9059 N_Package_Declaration |
9060 N_Generic_Package_Declaration =>
9061 Formal_Ent := Defining_Entity (F);
9062
9063 while Chars (Act) /= Chars (Formal_Ent) loop
9064 Next_Entity (Act);
9065 end loop;
9066
9067 when others =>
9068 raise Program_Error;
9069 end case;
9070 end Find_Matching_Actual;
9071
9072 -------------------------
9073 -- Match_Formal_Entity --
9074 -------------------------
9075
9076 procedure Match_Formal_Entity
9077 (Formal_Node : Node_Id;
9078 Formal_Ent : Entity_Id;
9079 Actual_Ent : Entity_Id)
9080 is
9081 Act_Pkg : Entity_Id;
9082
9083 begin
9084 Set_Instance_Of (Formal_Ent, Actual_Ent);
9085
9086 if Ekind (Actual_Ent) = E_Package then
9087
9088 -- Record associations for each parameter
9089
9090 Act_Pkg := Actual_Ent;
9091
9092 declare
9093 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9094 F_Ent : Entity_Id;
9095 F_Node : Node_Id;
9096
9097 Gen_Decl : Node_Id;
9098 Formals : List_Id;
9099 Actual : Entity_Id;
9100
9101 begin
9102 -- Retrieve the actual given in the formal package declaration
9103
9104 Actual := Entity (Name (Original_Node (Formal_Node)));
9105
9106 -- The actual in the formal package declaration may be a
9107 -- renamed generic package, in which case we want to retrieve
9108 -- the original generic in order to traverse its formal part.
9109
9110 if Present (Renamed_Entity (Actual)) then
9111 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9112 else
9113 Gen_Decl := Unit_Declaration_Node (Actual);
9114 end if;
9115
9116 Formals := Generic_Formal_Declarations (Gen_Decl);
9117
9118 if Present (Formals) then
9119 F_Node := First_Non_Pragma (Formals);
9120 else
9121 F_Node := Empty;
9122 end if;
9123
9124 while Present (A_Ent)
9125 and then Present (F_Node)
9126 and then A_Ent /= First_Private_Entity (Act_Pkg)
9127 loop
9128 F_Ent := Get_Formal_Entity (F_Node);
9129
9130 if Present (F_Ent) then
9131
9132 -- This is a formal of the original package. Record
9133 -- association and recurse.
9134
9135 Find_Matching_Actual (F_Node, A_Ent);
9136 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9137 Next_Entity (A_Ent);
9138 end if;
9139
9140 Next_Non_Pragma (F_Node);
9141 end loop;
9142 end;
9143 end if;
9144 end Match_Formal_Entity;
9145
9146 -----------------------
9147 -- Get_Formal_Entity --
9148 -----------------------
9149
9150 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9151 Kind : constant Node_Kind := Nkind (Original_Node (N));
9152 begin
9153 case Kind is
9154 when N_Formal_Object_Declaration =>
9155 return Defining_Identifier (N);
9156
9157 when N_Formal_Type_Declaration =>
9158 return Defining_Identifier (N);
9159
9160 when N_Formal_Subprogram_Declaration =>
9161 return Defining_Unit_Name (Specification (N));
9162
9163 when N_Formal_Package_Declaration =>
9164 return Defining_Identifier (Original_Node (N));
9165
9166 when N_Generic_Package_Declaration =>
9167 return Defining_Identifier (Original_Node (N));
9168
9169 -- All other declarations are introduced by semantic analysis and
9170 -- have no match in the actual.
9171
9172 when others =>
9173 return Empty;
9174 end case;
9175 end Get_Formal_Entity;
9176
9177 --------------------
9178 -- Is_Instance_Of --
9179 --------------------
9180
9181 function Is_Instance_Of
9182 (Act_Spec : Entity_Id;
9183 Gen_Anc : Entity_Id) return Boolean
9184 is
9185 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9186
9187 begin
9188 if No (Gen_Par) then
9189 return False;
9190
9191 -- Simplest case: the generic parent of the actual is the formal
9192
9193 elsif Gen_Par = Gen_Anc then
9194 return True;
9195
9196 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9197 return False;
9198
9199 -- The actual may be obtained through several instantiations. Its
9200 -- scope must itself be an instance of a generic declared in the
9201 -- same scope as the formal. Any other case is detected above.
9202
9203 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9204 return False;
9205
9206 else
9207 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9208 end if;
9209 end Is_Instance_Of;
9210
9211 ---------------------------
9212 -- Process_Nested_Formal --
9213 ---------------------------
9214
9215 procedure Process_Nested_Formal (Formal : Entity_Id) is
9216 Ent : Entity_Id;
9217
9218 begin
9219 if Present (Associated_Formal_Package (Formal))
9220 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9221 then
9222 Ent := First_Entity (Formal);
9223 while Present (Ent) loop
9224 Set_Is_Hidden (Ent, False);
9225 Set_Is_Visible_Formal (Ent);
9226 Set_Is_Potentially_Use_Visible
9227 (Ent, Is_Potentially_Use_Visible (Formal));
9228
9229 if Ekind (Ent) = E_Package then
9230 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9231 Process_Nested_Formal (Ent);
9232 end if;
9233
9234 Next_Entity (Ent);
9235 end loop;
9236 end if;
9237 end Process_Nested_Formal;
9238
9239 -- Start of processing for Instantiate_Formal_Package
9240
9241 begin
9242 Analyze (Actual);
9243
9244 if not Is_Entity_Name (Actual)
9245 or else Ekind (Entity (Actual)) /= E_Package
9246 then
9247 Error_Msg_N
9248 ("expect package instance to instantiate formal", Actual);
9249 Abandon_Instantiation (Actual);
9250 raise Program_Error;
9251
9252 else
9253 Actual_Pack := Entity (Actual);
9254 Set_Is_Instantiated (Actual_Pack);
9255
9256 -- The actual may be a renamed package, or an outer generic formal
9257 -- package whose instantiation is converted into a renaming.
9258
9259 if Present (Renamed_Object (Actual_Pack)) then
9260 Actual_Pack := Renamed_Object (Actual_Pack);
9261 end if;
9262
9263 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9264 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9265 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9266 else
9267 Gen_Parent :=
9268 Generic_Parent (Specification (Analyzed_Formal));
9269 Formal_Pack :=
9270 Defining_Unit_Name (Specification (Analyzed_Formal));
9271 end if;
9272
9273 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9274 Parent_Spec := Package_Specification (Actual_Pack);
9275 else
9276 Parent_Spec := Parent (Actual_Pack);
9277 end if;
9278
9279 if Gen_Parent = Any_Id then
9280 Error_Msg_N
9281 ("previous error in declaration of formal package", Actual);
9282 Abandon_Instantiation (Actual);
9283
9284 elsif
9285 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9286 then
9287 null;
9288
9289 else
9290 Error_Msg_NE
9291 ("actual parameter must be instance of&", Actual, Gen_Parent);
9292 Abandon_Instantiation (Actual);
9293 end if;
9294
9295 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9296 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9297
9298 Nod :=
9299 Make_Package_Renaming_Declaration (Loc,
9300 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9301 Name => New_Occurrence_Of (Actual_Pack, Loc));
9302
9303 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
9304 Defining_Identifier (Formal));
9305 Decls := New_List (Nod);
9306
9307 -- If the formal F has a box, then the generic declarations are
9308 -- visible in the generic G. In an instance of G, the corresponding
9309 -- entities in the actual for F (which are the actuals for the
9310 -- instantiation of the generic that F denotes) must also be made
9311 -- visible for analysis of the current instance. On exit from the
9312 -- current instance, those entities are made private again. If the
9313 -- actual is currently in use, these entities are also use-visible.
9314
9315 -- The loop through the actual entities also steps through the formal
9316 -- entities and enters associations from formals to actuals into the
9317 -- renaming map. This is necessary to properly handle checking of
9318 -- actual parameter associations for later formals that depend on
9319 -- actuals declared in the formal package.
9320
9321 -- In Ada 2005, partial parameterization requires that we make
9322 -- visible the actuals corresponding to formals that were defaulted
9323 -- in the formal package. There formals are identified because they
9324 -- remain formal generics within the formal package, rather than
9325 -- being renamings of the actuals supplied.
9326
9327 declare
9328 Gen_Decl : constant Node_Id :=
9329 Unit_Declaration_Node (Gen_Parent);
9330 Formals : constant List_Id :=
9331 Generic_Formal_Declarations (Gen_Decl);
9332
9333 Actual_Ent : Entity_Id;
9334 Actual_Of_Formal : Node_Id;
9335 Formal_Node : Node_Id;
9336 Formal_Ent : Entity_Id;
9337
9338 begin
9339 if Present (Formals) then
9340 Formal_Node := First_Non_Pragma (Formals);
9341 else
9342 Formal_Node := Empty;
9343 end if;
9344
9345 Actual_Ent := First_Entity (Actual_Pack);
9346 Actual_Of_Formal :=
9347 First (Visible_Declarations (Specification (Analyzed_Formal)));
9348 while Present (Actual_Ent)
9349 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9350 loop
9351 if Present (Formal_Node) then
9352 Formal_Ent := Get_Formal_Entity (Formal_Node);
9353
9354 if Present (Formal_Ent) then
9355 Find_Matching_Actual (Formal_Node, Actual_Ent);
9356 Match_Formal_Entity
9357 (Formal_Node, Formal_Ent, Actual_Ent);
9358
9359 -- We iterate at the same time over the actuals of the
9360 -- local package created for the formal, to determine
9361 -- which one of the formals of the original generic were
9362 -- defaulted in the formal. The corresponding actual
9363 -- entities are visible in the enclosing instance.
9364
9365 if Box_Present (Formal)
9366 or else
9367 (Present (Actual_Of_Formal)
9368 and then
9369 Is_Generic_Formal
9370 (Get_Formal_Entity (Actual_Of_Formal)))
9371 then
9372 Set_Is_Hidden (Actual_Ent, False);
9373 Set_Is_Visible_Formal (Actual_Ent);
9374 Set_Is_Potentially_Use_Visible
9375 (Actual_Ent, In_Use (Actual_Pack));
9376
9377 if Ekind (Actual_Ent) = E_Package then
9378 Process_Nested_Formal (Actual_Ent);
9379 end if;
9380
9381 else
9382 Set_Is_Hidden (Actual_Ent);
9383 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9384 end if;
9385 end if;
9386
9387 Next_Non_Pragma (Formal_Node);
9388 Next (Actual_Of_Formal);
9389
9390 else
9391 -- No further formals to match, but the generic part may
9392 -- contain inherited operation that are not hidden in the
9393 -- enclosing instance.
9394
9395 Next_Entity (Actual_Ent);
9396 end if;
9397 end loop;
9398
9399 -- Inherited subprograms generated by formal derived types are
9400 -- also visible if the types are.
9401
9402 Actual_Ent := First_Entity (Actual_Pack);
9403 while Present (Actual_Ent)
9404 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9405 loop
9406 if Is_Overloadable (Actual_Ent)
9407 and then
9408 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9409 and then
9410 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9411 then
9412 Set_Is_Hidden (Actual_Ent, False);
9413 Set_Is_Potentially_Use_Visible
9414 (Actual_Ent, In_Use (Actual_Pack));
9415 end if;
9416
9417 Next_Entity (Actual_Ent);
9418 end loop;
9419 end;
9420
9421 -- If the formal is not declared with a box, reanalyze it as an
9422 -- abbreviated instantiation, to verify the matching rules of 12.7.
9423 -- The actual checks are performed after the generic associations
9424 -- have been analyzed, to guarantee the same visibility for this
9425 -- instantiation and for the actuals.
9426
9427 -- In Ada 2005, the generic associations for the formal can include
9428 -- defaulted parameters. These are ignored during check. This
9429 -- internal instantiation is removed from the tree after conformance
9430 -- checking, because it contains formal declarations for those
9431 -- defaulted parameters, and those should not reach the back-end.
9432
9433 if not Box_Present (Formal) then
9434 declare
9435 I_Pack : constant Entity_Id :=
9436 Make_Temporary (Sloc (Actual), 'P');
9437
9438 begin
9439 Set_Is_Internal (I_Pack);
9440
9441 Append_To (Decls,
9442 Make_Package_Instantiation (Sloc (Actual),
9443 Defining_Unit_Name => I_Pack,
9444 Name =>
9445 New_Occurrence_Of
9446 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9447 Generic_Associations =>
9448 Generic_Associations (Formal)));
9449 end;
9450 end if;
9451
9452 return Decls;
9453 end if;
9454 end Instantiate_Formal_Package;
9455
9456 -----------------------------------
9457 -- Instantiate_Formal_Subprogram --
9458 -----------------------------------
9459
9460 function Instantiate_Formal_Subprogram
9461 (Formal : Node_Id;
9462 Actual : Node_Id;
9463 Analyzed_Formal : Node_Id) return Node_Id
9464 is
9465 Analyzed_S : constant Entity_Id :=
9466 Defining_Unit_Name (Specification (Analyzed_Formal));
9467 Formal_Sub : constant Entity_Id :=
9468 Defining_Unit_Name (Specification (Formal));
9469
9470 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9471 -- If the generic is a child unit, the parent has been installed on the
9472 -- scope stack, but a default subprogram cannot resolve to something
9473 -- on the parent because that parent is not really part of the visible
9474 -- context (it is there to resolve explicit local entities). If the
9475 -- default has resolved in this way, we remove the entity from immediate
9476 -- visibility and analyze the node again to emit an error message or
9477 -- find another visible candidate.
9478
9479 procedure Valid_Actual_Subprogram (Act : Node_Id);
9480 -- Perform legality check and raise exception on failure
9481
9482 -----------------------
9483 -- From_Parent_Scope --
9484 -----------------------
9485
9486 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9487 Gen_Scope : Node_Id;
9488
9489 begin
9490 Gen_Scope := Scope (Analyzed_S);
9491 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9492 if Scope (Subp) = Scope (Gen_Scope) then
9493 return True;
9494 end if;
9495
9496 Gen_Scope := Scope (Gen_Scope);
9497 end loop;
9498
9499 return False;
9500 end From_Parent_Scope;
9501
9502 -----------------------------
9503 -- Valid_Actual_Subprogram --
9504 -----------------------------
9505
9506 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9507 Act_E : Entity_Id;
9508
9509 begin
9510 if Is_Entity_Name (Act) then
9511 Act_E := Entity (Act);
9512
9513 elsif Nkind (Act) = N_Selected_Component
9514 and then Is_Entity_Name (Selector_Name (Act))
9515 then
9516 Act_E := Entity (Selector_Name (Act));
9517
9518 else
9519 Act_E := Empty;
9520 end if;
9521
9522 if (Present (Act_E) and then Is_Overloadable (Act_E))
9523 or else Nkind_In (Act, N_Attribute_Reference,
9524 N_Indexed_Component,
9525 N_Character_Literal,
9526 N_Explicit_Dereference)
9527 then
9528 return;
9529 end if;
9530
9531 Error_Msg_NE
9532 ("expect subprogram or entry name in instantiation of&",
9533 Instantiation_Node, Formal_Sub);
9534 Abandon_Instantiation (Instantiation_Node);
9535 end Valid_Actual_Subprogram;
9536
9537 -- Local variables
9538
9539 Decl_Node : Node_Id;
9540 Loc : Source_Ptr;
9541 Nam : Node_Id;
9542 New_Spec : Node_Id;
9543
9544 -- Start of processing for Instantiate_Formal_Subprogram
9545
9546 begin
9547 New_Spec := New_Copy_Tree (Specification (Formal));
9548
9549 -- The tree copy has created the proper instantiation sloc for the
9550 -- new specification. Use this location for all other constructed
9551 -- declarations.
9552
9553 Loc := Sloc (Defining_Unit_Name (New_Spec));
9554
9555 -- Create new entity for the actual (New_Copy_Tree does not)
9556
9557 Set_Defining_Unit_Name
9558 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9559
9560 -- Create new entities for the each of the formals in the specification
9561 -- of the renaming declaration built for the actual.
9562
9563 if Present (Parameter_Specifications (New_Spec)) then
9564 declare
9565 F : Node_Id;
9566 F_Id : Entity_Id;
9567
9568 begin
9569 F := First (Parameter_Specifications (New_Spec));
9570 while Present (F) loop
9571 F_Id := Defining_Identifier (F);
9572
9573 Set_Defining_Identifier (F,
9574 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
9575 Next (F);
9576 end loop;
9577 end;
9578 end if;
9579
9580 -- Find entity of actual. If the actual is an attribute reference, it
9581 -- cannot be resolved here (its formal is missing) but is handled
9582 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9583 -- fully resolved subsequently, when the renaming declaration for the
9584 -- formal is analyzed. If it is an explicit dereference, resolve the
9585 -- prefix but not the actual itself, to prevent interpretation as call.
9586
9587 if Present (Actual) then
9588 Loc := Sloc (Actual);
9589 Set_Sloc (New_Spec, Loc);
9590
9591 if Nkind (Actual) = N_Operator_Symbol then
9592 Find_Direct_Name (Actual);
9593
9594 elsif Nkind (Actual) = N_Explicit_Dereference then
9595 Analyze (Prefix (Actual));
9596
9597 elsif Nkind (Actual) /= N_Attribute_Reference then
9598 Analyze (Actual);
9599 end if;
9600
9601 Valid_Actual_Subprogram (Actual);
9602 Nam := Actual;
9603
9604 elsif Present (Default_Name (Formal)) then
9605 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9606 N_Selected_Component,
9607 N_Indexed_Component,
9608 N_Character_Literal)
9609 and then Present (Entity (Default_Name (Formal)))
9610 then
9611 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9612 else
9613 Nam := New_Copy (Default_Name (Formal));
9614 Set_Sloc (Nam, Loc);
9615 end if;
9616
9617 elsif Box_Present (Formal) then
9618
9619 -- Actual is resolved at the point of instantiation. Create an
9620 -- identifier or operator with the same name as the formal.
9621
9622 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
9623 Nam :=
9624 Make_Operator_Symbol (Loc,
9625 Chars => Chars (Formal_Sub),
9626 Strval => No_String);
9627 else
9628 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
9629 end if;
9630
9631 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
9632 and then Null_Present (Specification (Formal))
9633 then
9634 -- Generate null body for procedure, for use in the instance
9635
9636 Decl_Node :=
9637 Make_Subprogram_Body (Loc,
9638 Specification => New_Spec,
9639 Declarations => New_List,
9640 Handled_Statement_Sequence =>
9641 Make_Handled_Sequence_Of_Statements (Loc,
9642 Statements => New_List (Make_Null_Statement (Loc))));
9643
9644 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
9645 return Decl_Node;
9646
9647 else
9648 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
9649 Error_Msg_NE
9650 ("missing actual&", Instantiation_Node, Formal_Sub);
9651 Error_Msg_NE
9652 ("\in instantiation of & declared#",
9653 Instantiation_Node, Scope (Analyzed_S));
9654 Abandon_Instantiation (Instantiation_Node);
9655 end if;
9656
9657 Decl_Node :=
9658 Make_Subprogram_Renaming_Declaration (Loc,
9659 Specification => New_Spec,
9660 Name => Nam);
9661
9662 -- If we do not have an actual and the formal specified <> then set to
9663 -- get proper default.
9664
9665 if No (Actual) and then Box_Present (Formal) then
9666 Set_From_Default (Decl_Node);
9667 end if;
9668
9669 -- Gather possible interpretations for the actual before analyzing the
9670 -- instance. If overloaded, it will be resolved when analyzing the
9671 -- renaming declaration.
9672
9673 if Box_Present (Formal) and then No (Actual) then
9674 Analyze (Nam);
9675
9676 if Is_Child_Unit (Scope (Analyzed_S))
9677 and then Present (Entity (Nam))
9678 then
9679 if not Is_Overloaded (Nam) then
9680 if From_Parent_Scope (Entity (Nam)) then
9681 Set_Is_Immediately_Visible (Entity (Nam), False);
9682 Set_Entity (Nam, Empty);
9683 Set_Etype (Nam, Empty);
9684
9685 Analyze (Nam);
9686 Set_Is_Immediately_Visible (Entity (Nam));
9687 end if;
9688
9689 else
9690 declare
9691 I : Interp_Index;
9692 It : Interp;
9693
9694 begin
9695 Get_First_Interp (Nam, I, It);
9696 while Present (It.Nam) loop
9697 if From_Parent_Scope (It.Nam) then
9698 Remove_Interp (I);
9699 end if;
9700
9701 Get_Next_Interp (I, It);
9702 end loop;
9703 end;
9704 end if;
9705 end if;
9706 end if;
9707
9708 -- The generic instantiation freezes the actual. This can only be done
9709 -- once the actual is resolved, in the analysis of the renaming
9710 -- declaration. To make the formal subprogram entity available, we set
9711 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9712 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9713 -- of formal abstract subprograms.
9714
9715 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9716
9717 -- We cannot analyze the renaming declaration, and thus find the actual,
9718 -- until all the actuals are assembled in the instance. For subsequent
9719 -- checks of other actuals, indicate the node that will hold the
9720 -- instance of this formal.
9721
9722 Set_Instance_Of (Analyzed_S, Nam);
9723
9724 if Nkind (Actual) = N_Selected_Component
9725 and then Is_Task_Type (Etype (Prefix (Actual)))
9726 and then not Is_Frozen (Etype (Prefix (Actual)))
9727 then
9728 -- The renaming declaration will create a body, which must appear
9729 -- outside of the instantiation, We move the renaming declaration
9730 -- out of the instance, and create an additional renaming inside,
9731 -- to prevent freezing anomalies.
9732
9733 declare
9734 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9735
9736 begin
9737 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9738 Insert_Before (Instantiation_Node, Decl_Node);
9739 Analyze (Decl_Node);
9740
9741 -- Now create renaming within the instance
9742
9743 Decl_Node :=
9744 Make_Subprogram_Renaming_Declaration (Loc,
9745 Specification => New_Copy_Tree (New_Spec),
9746 Name => New_Occurrence_Of (Anon_Id, Loc));
9747
9748 Set_Defining_Unit_Name (Specification (Decl_Node),
9749 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9750 end;
9751 end if;
9752
9753 return Decl_Node;
9754 end Instantiate_Formal_Subprogram;
9755
9756 ------------------------
9757 -- Instantiate_Object --
9758 ------------------------
9759
9760 function Instantiate_Object
9761 (Formal : Node_Id;
9762 Actual : Node_Id;
9763 Analyzed_Formal : Node_Id) return List_Id
9764 is
9765 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9766 A_Gen_Obj : constant Entity_Id :=
9767 Defining_Identifier (Analyzed_Formal);
9768 Acc_Def : Node_Id := Empty;
9769 Act_Assoc : constant Node_Id := Parent (Actual);
9770 Actual_Decl : Node_Id := Empty;
9771 Decl_Node : Node_Id;
9772 Def : Node_Id;
9773 Ftyp : Entity_Id;
9774 List : constant List_Id := New_List;
9775 Loc : constant Source_Ptr := Sloc (Actual);
9776 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9777 Subt_Decl : Node_Id := Empty;
9778 Subt_Mark : Node_Id := Empty;
9779
9780 begin
9781 if Present (Subtype_Mark (Formal)) then
9782 Subt_Mark := Subtype_Mark (Formal);
9783 else
9784 Check_Access_Definition (Formal);
9785 Acc_Def := Access_Definition (Formal);
9786 end if;
9787
9788 -- Sloc for error message on missing actual
9789
9790 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9791
9792 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9793 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9794 end if;
9795
9796 Set_Parent (List, Parent (Actual));
9797
9798 -- OUT present
9799
9800 if Out_Present (Formal) then
9801
9802 -- An IN OUT generic actual must be a name. The instantiation is a
9803 -- renaming declaration. The actual is the name being renamed. We
9804 -- use the actual directly, rather than a copy, because it is not
9805 -- used further in the list of actuals, and because a copy or a use
9806 -- of relocate_node is incorrect if the instance is nested within a
9807 -- generic. In order to simplify ASIS searches, the Generic_Parent
9808 -- field links the declaration to the generic association.
9809
9810 if No (Actual) then
9811 Error_Msg_NE
9812 ("missing actual&",
9813 Instantiation_Node, Gen_Obj);
9814 Error_Msg_NE
9815 ("\in instantiation of & declared#",
9816 Instantiation_Node, Scope (A_Gen_Obj));
9817 Abandon_Instantiation (Instantiation_Node);
9818 end if;
9819
9820 if Present (Subt_Mark) then
9821 Decl_Node :=
9822 Make_Object_Renaming_Declaration (Loc,
9823 Defining_Identifier => New_Copy (Gen_Obj),
9824 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9825 Name => Actual);
9826
9827 else pragma Assert (Present (Acc_Def));
9828 Decl_Node :=
9829 Make_Object_Renaming_Declaration (Loc,
9830 Defining_Identifier => New_Copy (Gen_Obj),
9831 Access_Definition => New_Copy_Tree (Acc_Def),
9832 Name => Actual);
9833 end if;
9834
9835 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9836
9837 -- The analysis of the actual may produce Insert_Action nodes, so
9838 -- the declaration must have a context in which to attach them.
9839
9840 Append (Decl_Node, List);
9841 Analyze (Actual);
9842
9843 -- Return if the analysis of the actual reported some error
9844
9845 if Etype (Actual) = Any_Type then
9846 return List;
9847 end if;
9848
9849 -- This check is performed here because Analyze_Object_Renaming will
9850 -- not check it when Comes_From_Source is False. Note though that the
9851 -- check for the actual being the name of an object will be performed
9852 -- in Analyze_Object_Renaming.
9853
9854 if Is_Object_Reference (Actual)
9855 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
9856 then
9857 Error_Msg_N
9858 ("illegal discriminant-dependent component for in out parameter",
9859 Actual);
9860 end if;
9861
9862 -- The actual has to be resolved in order to check that it is a
9863 -- variable (due to cases such as F (1), where F returns access to
9864 -- an array, and for overloaded prefixes).
9865
9866 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
9867
9868 -- If the type of the formal is not itself a formal, and the current
9869 -- unit is a child unit, the formal type must be declared in a
9870 -- parent, and must be retrieved by visibility.
9871
9872 if Ftyp = Orig_Ftyp
9873 and then Is_Generic_Unit (Scope (Ftyp))
9874 and then Is_Child_Unit (Scope (A_Gen_Obj))
9875 then
9876 declare
9877 Temp : constant Node_Id :=
9878 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
9879 begin
9880 Set_Entity (Temp, Empty);
9881 Find_Type (Temp);
9882 Ftyp := Entity (Temp);
9883 end;
9884 end if;
9885
9886 if Is_Private_Type (Ftyp)
9887 and then not Is_Private_Type (Etype (Actual))
9888 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
9889 or else Base_Type (Etype (Actual)) = Ftyp)
9890 then
9891 -- If the actual has the type of the full view of the formal, or
9892 -- else a non-private subtype of the formal, then the visibility
9893 -- of the formal type has changed. Add to the actuals a subtype
9894 -- declaration that will force the exchange of views in the body
9895 -- of the instance as well.
9896
9897 Subt_Decl :=
9898 Make_Subtype_Declaration (Loc,
9899 Defining_Identifier => Make_Temporary (Loc, 'P'),
9900 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
9901
9902 Prepend (Subt_Decl, List);
9903
9904 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
9905 Exchange_Declarations (Ftyp);
9906 end if;
9907
9908 Resolve (Actual, Ftyp);
9909
9910 if not Denotes_Variable (Actual) then
9911 Error_Msg_NE
9912 ("actual for& must be a variable", Actual, Gen_Obj);
9913
9914 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
9915
9916 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
9917 -- the type of the actual shall resolve to a specific anonymous
9918 -- access type.
9919
9920 if Ada_Version < Ada_2005
9921 or else
9922 Ekind (Base_Type (Ftyp)) /=
9923 E_Anonymous_Access_Type
9924 or else
9925 Ekind (Base_Type (Etype (Actual))) /=
9926 E_Anonymous_Access_Type
9927 then
9928 Error_Msg_NE ("type of actual does not match type of&",
9929 Actual, Gen_Obj);
9930 end if;
9931 end if;
9932
9933 Note_Possible_Modification (Actual, Sure => True);
9934
9935 -- Check for instantiation of atomic/volatile actual for
9936 -- non-atomic/volatile formal (RM C.6 (12)).
9937
9938 if Is_Atomic_Object (Actual)
9939 and then not Is_Atomic (Orig_Ftyp)
9940 then
9941 Error_Msg_N
9942 ("cannot instantiate non-atomic formal object " &
9943 "with atomic actual", Actual);
9944
9945 elsif Is_Volatile_Object (Actual)
9946 and then not Is_Volatile (Orig_Ftyp)
9947 then
9948 Error_Msg_N
9949 ("cannot instantiate non-volatile formal object " &
9950 "with volatile actual", Actual);
9951 end if;
9952
9953 -- Formal in-parameter
9954
9955 else
9956 -- The instantiation of a generic formal in-parameter is constant
9957 -- declaration. The actual is the expression for that declaration.
9958
9959 if Present (Actual) then
9960 if Present (Subt_Mark) then
9961 Def := Subt_Mark;
9962 else pragma Assert (Present (Acc_Def));
9963 Def := Acc_Def;
9964 end if;
9965
9966 Decl_Node :=
9967 Make_Object_Declaration (Loc,
9968 Defining_Identifier => New_Copy (Gen_Obj),
9969 Constant_Present => True,
9970 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9971 Object_Definition => New_Copy_Tree (Def),
9972 Expression => Actual);
9973
9974 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9975
9976 -- A generic formal object of a tagged type is defined to be
9977 -- aliased so the new constant must also be treated as aliased.
9978
9979 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
9980 Set_Aliased_Present (Decl_Node);
9981 end if;
9982
9983 Append (Decl_Node, List);
9984
9985 -- No need to repeat (pre-)analysis of some expression nodes
9986 -- already handled in Preanalyze_Actuals.
9987
9988 if Nkind (Actual) /= N_Allocator then
9989 Analyze (Actual);
9990
9991 -- Return if the analysis of the actual reported some error
9992
9993 if Etype (Actual) = Any_Type then
9994 return List;
9995 end if;
9996 end if;
9997
9998 declare
9999 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10000 Typ : Entity_Id;
10001
10002 begin
10003 Typ := Get_Instance_Of (Formal_Type);
10004
10005 Freeze_Before (Instantiation_Node, Typ);
10006
10007 -- If the actual is an aggregate, perform name resolution on
10008 -- its components (the analysis of an aggregate does not do it)
10009 -- to capture local names that may be hidden if the generic is
10010 -- a child unit.
10011
10012 if Nkind (Actual) = N_Aggregate then
10013 Preanalyze_And_Resolve (Actual, Typ);
10014 end if;
10015
10016 if Is_Limited_Type (Typ)
10017 and then not OK_For_Limited_Init (Typ, Actual)
10018 then
10019 Error_Msg_N
10020 ("initialization not allowed for limited types", Actual);
10021 Explain_Limited_Type (Typ, Actual);
10022 end if;
10023 end;
10024
10025 elsif Present (Default_Expression (Formal)) then
10026
10027 -- Use default to construct declaration
10028
10029 if Present (Subt_Mark) then
10030 Def := Subt_Mark;
10031 else pragma Assert (Present (Acc_Def));
10032 Def := Acc_Def;
10033 end if;
10034
10035 Decl_Node :=
10036 Make_Object_Declaration (Sloc (Formal),
10037 Defining_Identifier => New_Copy (Gen_Obj),
10038 Constant_Present => True,
10039 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10040 Object_Definition => New_Copy (Def),
10041 Expression => New_Copy_Tree
10042 (Default_Expression (Formal)));
10043
10044 Append (Decl_Node, List);
10045 Set_Analyzed (Expression (Decl_Node), False);
10046
10047 else
10048 Error_Msg_NE
10049 ("missing actual&",
10050 Instantiation_Node, Gen_Obj);
10051 Error_Msg_NE ("\in instantiation of & declared#",
10052 Instantiation_Node, Scope (A_Gen_Obj));
10053
10054 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10055
10056 -- Create dummy constant declaration so that instance can be
10057 -- analyzed, to minimize cascaded visibility errors.
10058
10059 if Present (Subt_Mark) then
10060 Def := Subt_Mark;
10061 else pragma Assert (Present (Acc_Def));
10062 Def := Acc_Def;
10063 end if;
10064
10065 Decl_Node :=
10066 Make_Object_Declaration (Loc,
10067 Defining_Identifier => New_Copy (Gen_Obj),
10068 Constant_Present => True,
10069 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10070 Object_Definition => New_Copy (Def),
10071 Expression =>
10072 Make_Attribute_Reference (Sloc (Gen_Obj),
10073 Attribute_Name => Name_First,
10074 Prefix => New_Copy (Def)));
10075
10076 Append (Decl_Node, List);
10077
10078 else
10079 Abandon_Instantiation (Instantiation_Node);
10080 end if;
10081 end if;
10082 end if;
10083
10084 if Nkind (Actual) in N_Has_Entity then
10085 Actual_Decl := Parent (Entity (Actual));
10086 end if;
10087
10088 -- Ada 2005 (AI-423): For a formal object declaration with a null
10089 -- exclusion or an access definition that has a null exclusion: If the
10090 -- actual matching the formal object declaration denotes a generic
10091 -- formal object of another generic unit G, and the instantiation
10092 -- containing the actual occurs within the body of G or within the body
10093 -- of a generic unit declared within the declarative region of G, then
10094 -- the declaration of the formal object of G must have a null exclusion.
10095 -- Otherwise, the subtype of the actual matching the formal object
10096 -- declaration shall exclude null.
10097
10098 if Ada_Version >= Ada_2005
10099 and then Present (Actual_Decl)
10100 and then
10101 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10102 N_Object_Declaration)
10103 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10104 and then not Has_Null_Exclusion (Actual_Decl)
10105 and then Has_Null_Exclusion (Analyzed_Formal)
10106 then
10107 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10108 Error_Msg_N
10109 ("actual must exclude null to match generic formal#", Actual);
10110 end if;
10111
10112 -- An effectively volatile object cannot be used as an actual in
10113 -- a generic instance. The following check is only relevant when
10114 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10115
10116 if SPARK_Mode = On
10117 and then Present (Actual)
10118 and then Is_Effectively_Volatile_Object (Actual)
10119 then
10120 Error_Msg_N
10121 ("volatile object cannot act as actual in generic instantiation "
10122 & "(SPARK RM 7.1.3(8))", Actual);
10123 end if;
10124
10125 return List;
10126 end Instantiate_Object;
10127
10128 ------------------------------
10129 -- Instantiate_Package_Body --
10130 ------------------------------
10131
10132 procedure Instantiate_Package_Body
10133 (Body_Info : Pending_Body_Info;
10134 Inlined_Body : Boolean := False;
10135 Body_Optional : Boolean := False)
10136 is
10137 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10138 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10139 Loc : constant Source_Ptr := Sloc (Inst_Node);
10140
10141 Gen_Id : constant Node_Id := Name (Inst_Node);
10142 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10143 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10144 Act_Spec : constant Node_Id := Specification (Act_Decl);
10145 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10146
10147 Act_Body_Name : Node_Id;
10148 Gen_Body : Node_Id;
10149 Gen_Body_Id : Node_Id;
10150 Act_Body : Node_Id;
10151 Act_Body_Id : Entity_Id;
10152
10153 Parent_Installed : Boolean := False;
10154 Save_Style_Check : constant Boolean := Style_Check;
10155
10156 Par_Ent : Entity_Id := Empty;
10157 Par_Vis : Boolean := False;
10158
10159 Vis_Prims_List : Elist_Id := No_Elist;
10160 -- List of primitives made temporarily visible in the instantiation
10161 -- to match the visibility of the formal type
10162
10163 procedure Check_Initialized_Types;
10164 -- In a generic package body, an entity of a generic private type may
10165 -- appear uninitialized. This is suspicious, unless the actual is a
10166 -- fully initialized type.
10167
10168 -----------------------------
10169 -- Check_Initialized_Types --
10170 -----------------------------
10171
10172 procedure Check_Initialized_Types is
10173 Decl : Node_Id;
10174 Formal : Entity_Id;
10175 Actual : Entity_Id;
10176 Uninit_Var : Entity_Id;
10177
10178 begin
10179 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10180 while Present (Decl) loop
10181 Uninit_Var := Empty;
10182
10183 if Nkind (Decl) = N_Private_Extension_Declaration then
10184 Uninit_Var := Uninitialized_Variable (Decl);
10185
10186 elsif Nkind (Decl) = N_Formal_Type_Declaration
10187 and then Nkind (Formal_Type_Definition (Decl)) =
10188 N_Formal_Private_Type_Definition
10189 then
10190 Uninit_Var :=
10191 Uninitialized_Variable (Formal_Type_Definition (Decl));
10192 end if;
10193
10194 if Present (Uninit_Var) then
10195 Formal := Defining_Identifier (Decl);
10196 Actual := First_Entity (Act_Decl_Id);
10197
10198 -- For each formal there is a subtype declaration that renames
10199 -- the actual and has the same name as the formal. Locate the
10200 -- formal for warning message about uninitialized variables
10201 -- in the generic, for which the actual type should be a fully
10202 -- initialized type.
10203
10204 while Present (Actual) loop
10205 exit when Ekind (Actual) = E_Package
10206 and then Present (Renamed_Object (Actual));
10207
10208 if Chars (Actual) = Chars (Formal)
10209 and then not Is_Scalar_Type (Actual)
10210 and then not Is_Fully_Initialized_Type (Actual)
10211 and then Warn_On_No_Value_Assigned
10212 then
10213 Error_Msg_Node_2 := Formal;
10214 Error_Msg_NE
10215 ("generic unit has uninitialized variable& of "
10216 & "formal private type &?v?", Actual, Uninit_Var);
10217 Error_Msg_NE
10218 ("actual type for& should be fully initialized type?v?",
10219 Actual, Formal);
10220 exit;
10221 end if;
10222
10223 Next_Entity (Actual);
10224 end loop;
10225 end if;
10226
10227 Next (Decl);
10228 end loop;
10229 end Check_Initialized_Types;
10230
10231 -- Start of processing for Instantiate_Package_Body
10232
10233 begin
10234 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10235
10236 -- The instance body may already have been processed, as the parent of
10237 -- another instance that is inlined (Load_Parent_Of_Generic).
10238
10239 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10240 return;
10241 end if;
10242
10243 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10244
10245 -- Re-establish the state of information on which checks are suppressed.
10246 -- This information was set in Body_Info at the point of instantiation,
10247 -- and now we restore it so that the instance is compiled using the
10248 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10249
10250 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10251 Scope_Suppress := Body_Info.Scope_Suppress;
10252 Opt.Ada_Version := Body_Info.Version;
10253 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10254 Restore_Warnings (Body_Info.Warnings);
10255 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10256 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10257
10258 if No (Gen_Body_Id) then
10259
10260 -- Do not look for parent of generic body if none is required.
10261 -- This may happen when the routine is called as part of the
10262 -- Pending_Instantiations processing, when nested instances
10263 -- may precede the one generated from the main unit.
10264
10265 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10266 and then Body_Optional
10267 then
10268 return;
10269 else
10270 Load_Parent_Of_Generic
10271 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10272 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10273 end if;
10274 end if;
10275
10276 -- Establish global variable for sloc adjustment and for error recovery
10277
10278 Instantiation_Node := Inst_Node;
10279
10280 if Present (Gen_Body_Id) then
10281 Save_Env (Gen_Unit, Act_Decl_Id);
10282 Style_Check := False;
10283 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10284
10285 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10286
10287 Create_Instantiation_Source
10288 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10289
10290 Act_Body :=
10291 Copy_Generic_Node
10292 (Original_Node (Gen_Body), Empty, Instantiating => True);
10293
10294 -- Build new name (possibly qualified) for body declaration
10295
10296 Act_Body_Id := New_Copy (Act_Decl_Id);
10297
10298 -- Some attributes of spec entity are not inherited by body entity
10299
10300 Set_Handler_Records (Act_Body_Id, No_List);
10301
10302 if Nkind (Defining_Unit_Name (Act_Spec)) =
10303 N_Defining_Program_Unit_Name
10304 then
10305 Act_Body_Name :=
10306 Make_Defining_Program_Unit_Name (Loc,
10307 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10308 Defining_Identifier => Act_Body_Id);
10309 else
10310 Act_Body_Name := Act_Body_Id;
10311 end if;
10312
10313 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10314
10315 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10316 Check_Generic_Actuals (Act_Decl_Id, False);
10317 Check_Initialized_Types;
10318
10319 -- Install primitives hidden at the point of the instantiation but
10320 -- visible when processing the generic formals
10321
10322 declare
10323 E : Entity_Id;
10324
10325 begin
10326 E := First_Entity (Act_Decl_Id);
10327 while Present (E) loop
10328 if Is_Type (E)
10329 and then Is_Generic_Actual_Type (E)
10330 and then Is_Tagged_Type (E)
10331 then
10332 Install_Hidden_Primitives
10333 (Prims_List => Vis_Prims_List,
10334 Gen_T => Generic_Parent_Type (Parent (E)),
10335 Act_T => E);
10336 end if;
10337
10338 Next_Entity (E);
10339 end loop;
10340 end;
10341
10342 -- If it is a child unit, make the parent instance (which is an
10343 -- instance of the parent of the generic) visible. The parent
10344 -- instance is the prefix of the name of the generic unit.
10345
10346 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10347 and then Nkind (Gen_Id) = N_Expanded_Name
10348 then
10349 Par_Ent := Entity (Prefix (Gen_Id));
10350 Par_Vis := Is_Immediately_Visible (Par_Ent);
10351 Install_Parent (Par_Ent, In_Body => True);
10352 Parent_Installed := True;
10353
10354 elsif Is_Child_Unit (Gen_Unit) then
10355 Par_Ent := Scope (Gen_Unit);
10356 Par_Vis := Is_Immediately_Visible (Par_Ent);
10357 Install_Parent (Par_Ent, In_Body => True);
10358 Parent_Installed := True;
10359 end if;
10360
10361 -- If the instantiation is a library unit, and this is the main unit,
10362 -- then build the resulting compilation unit nodes for the instance.
10363 -- If this is a compilation unit but it is not the main unit, then it
10364 -- is the body of a unit in the context, that is being compiled
10365 -- because it is encloses some inlined unit or another generic unit
10366 -- being instantiated. In that case, this body is not part of the
10367 -- current compilation, and is not attached to the tree, but its
10368 -- parent must be set for analysis.
10369
10370 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10371
10372 -- Replace instance node with body of instance, and create new
10373 -- node for corresponding instance declaration.
10374
10375 Build_Instance_Compilation_Unit_Nodes
10376 (Inst_Node, Act_Body, Act_Decl);
10377 Analyze (Inst_Node);
10378
10379 if Parent (Inst_Node) = Cunit (Main_Unit) then
10380
10381 -- If the instance is a child unit itself, then set the scope
10382 -- of the expanded body to be the parent of the instantiation
10383 -- (ensuring that the fully qualified name will be generated
10384 -- for the elaboration subprogram).
10385
10386 if Nkind (Defining_Unit_Name (Act_Spec)) =
10387 N_Defining_Program_Unit_Name
10388 then
10389 Set_Scope
10390 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10391 end if;
10392 end if;
10393
10394 -- Case where instantiation is not a library unit
10395
10396 else
10397 -- If this is an early instantiation, i.e. appears textually
10398 -- before the corresponding body and must be elaborated first,
10399 -- indicate that the body instance is to be delayed.
10400
10401 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10402
10403 -- Now analyze the body. We turn off all checks if this is an
10404 -- internal unit, since there is no reason to have checks on for
10405 -- any predefined run-time library code. All such code is designed
10406 -- to be compiled with checks off.
10407
10408 -- Note that we do NOT apply this criterion to children of GNAT
10409 -- The latter units must suppress checks explicitly if needed.
10410
10411 if Is_Predefined_File_Name
10412 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10413 then
10414 Analyze (Act_Body, Suppress => All_Checks);
10415 else
10416 Analyze (Act_Body);
10417 end if;
10418 end if;
10419
10420 Inherit_Context (Gen_Body, Inst_Node);
10421
10422 -- Remove the parent instances if they have been placed on the scope
10423 -- stack to compile the body.
10424
10425 if Parent_Installed then
10426 Remove_Parent (In_Body => True);
10427
10428 -- Restore the previous visibility of the parent
10429
10430 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10431 end if;
10432
10433 Restore_Hidden_Primitives (Vis_Prims_List);
10434 Restore_Private_Views (Act_Decl_Id);
10435
10436 -- Remove the current unit from visibility if this is an instance
10437 -- that is not elaborated on the fly for inlining purposes.
10438
10439 if not Inlined_Body then
10440 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10441 end if;
10442
10443 Restore_Env;
10444 Style_Check := Save_Style_Check;
10445
10446 -- If we have no body, and the unit requires a body, then complain. This
10447 -- complaint is suppressed if we have detected other errors (since a
10448 -- common reason for missing the body is that it had errors).
10449 -- In CodePeer mode, a warning has been emitted already, no need for
10450 -- further messages.
10451
10452 elsif Unit_Requires_Body (Gen_Unit)
10453 and then not Body_Optional
10454 then
10455 if CodePeer_Mode then
10456 null;
10457
10458 elsif Serious_Errors_Detected = 0 then
10459 Error_Msg_NE
10460 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10461
10462 -- Don't attempt to perform any cleanup actions if some other error
10463 -- was already detected, since this can cause blowups.
10464
10465 else
10466 return;
10467 end if;
10468
10469 -- Case of package that does not need a body
10470
10471 else
10472 -- If the instantiation of the declaration is a library unit, rewrite
10473 -- the original package instantiation as a package declaration in the
10474 -- compilation unit node.
10475
10476 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10477 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10478 Rewrite (Inst_Node, Act_Decl);
10479
10480 -- Generate elaboration entity, in case spec has elaboration code.
10481 -- This cannot be done when the instance is analyzed, because it
10482 -- is not known yet whether the body exists.
10483
10484 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10485 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10486
10487 -- If the instantiation is not a library unit, then append the
10488 -- declaration to the list of implicitly generated entities, unless
10489 -- it is already a list member which means that it was already
10490 -- processed
10491
10492 elsif not Is_List_Member (Act_Decl) then
10493 Mark_Rewrite_Insertion (Act_Decl);
10494 Insert_Before (Inst_Node, Act_Decl);
10495 end if;
10496 end if;
10497
10498 Expander_Mode_Restore;
10499 end Instantiate_Package_Body;
10500
10501 ---------------------------------
10502 -- Instantiate_Subprogram_Body --
10503 ---------------------------------
10504
10505 procedure Instantiate_Subprogram_Body
10506 (Body_Info : Pending_Body_Info;
10507 Body_Optional : Boolean := False)
10508 is
10509 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10510 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10511 Loc : constant Source_Ptr := Sloc (Inst_Node);
10512 Gen_Id : constant Node_Id := Name (Inst_Node);
10513 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10514 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10515 Anon_Id : constant Entity_Id :=
10516 Defining_Unit_Name (Specification (Act_Decl));
10517 Pack_Id : constant Entity_Id :=
10518 Defining_Unit_Name (Parent (Act_Decl));
10519 Decls : List_Id;
10520 Gen_Body : Node_Id;
10521 Gen_Body_Id : Node_Id;
10522 Act_Body : Node_Id;
10523 Pack_Body : Node_Id;
10524 Prev_Formal : Entity_Id;
10525 Ret_Expr : Node_Id;
10526 Unit_Renaming : Node_Id;
10527
10528 Parent_Installed : Boolean := False;
10529
10530 Saved_Style_Check : constant Boolean := Style_Check;
10531 Saved_Warnings : constant Warning_Record := Save_Warnings;
10532
10533 Par_Ent : Entity_Id := Empty;
10534 Par_Vis : Boolean := False;
10535
10536 begin
10537 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10538
10539 -- Subprogram body may have been created already because of an inline
10540 -- pragma, or because of multiple elaborations of the enclosing package
10541 -- when several instances of the subprogram appear in the main unit.
10542
10543 if Present (Corresponding_Body (Act_Decl)) then
10544 return;
10545 end if;
10546
10547 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10548
10549 -- Re-establish the state of information on which checks are suppressed.
10550 -- This information was set in Body_Info at the point of instantiation,
10551 -- and now we restore it so that the instance is compiled using the
10552 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10553
10554 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10555 Scope_Suppress := Body_Info.Scope_Suppress;
10556 Opt.Ada_Version := Body_Info.Version;
10557 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10558 Restore_Warnings (Body_Info.Warnings);
10559 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10560 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10561
10562 if No (Gen_Body_Id) then
10563
10564 -- For imported generic subprogram, no body to compile, complete
10565 -- the spec entity appropriately.
10566
10567 if Is_Imported (Gen_Unit) then
10568 Set_Is_Imported (Anon_Id);
10569 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10570 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
10571 Set_Convention (Anon_Id, Convention (Gen_Unit));
10572 Set_Has_Completion (Anon_Id);
10573 return;
10574
10575 -- For other cases, compile the body
10576
10577 else
10578 Load_Parent_Of_Generic
10579 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10580 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10581 end if;
10582 end if;
10583
10584 Instantiation_Node := Inst_Node;
10585
10586 if Present (Gen_Body_Id) then
10587 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10588
10589 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
10590
10591 -- Either body is not present, or context is non-expanding, as
10592 -- when compiling a subunit. Mark the instance as completed, and
10593 -- diagnose a missing body when needed.
10594
10595 if Expander_Active
10596 and then Operating_Mode = Generate_Code
10597 then
10598 Error_Msg_N
10599 ("missing proper body for instantiation", Gen_Body);
10600 end if;
10601
10602 Set_Has_Completion (Anon_Id);
10603 return;
10604 end if;
10605
10606 Save_Env (Gen_Unit, Anon_Id);
10607 Style_Check := False;
10608 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10609 Create_Instantiation_Source
10610 (Inst_Node,
10611 Gen_Body_Id,
10612 False,
10613 S_Adjustment);
10614
10615 Act_Body :=
10616 Copy_Generic_Node
10617 (Original_Node (Gen_Body), Empty, Instantiating => True);
10618
10619 -- Create proper defining name for the body, to correspond to
10620 -- the one in the spec.
10621
10622 Set_Defining_Unit_Name (Specification (Act_Body),
10623 Make_Defining_Identifier
10624 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
10625 Set_Corresponding_Spec (Act_Body, Anon_Id);
10626 Set_Has_Completion (Anon_Id);
10627 Check_Generic_Actuals (Pack_Id, False);
10628
10629 -- Generate a reference to link the visible subprogram instance to
10630 -- the generic body, which for navigation purposes is the only
10631 -- available source for the instance.
10632
10633 Generate_Reference
10634 (Related_Instance (Pack_Id),
10635 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
10636
10637 -- If it is a child unit, make the parent instance (which is an
10638 -- instance of the parent of the generic) visible. The parent
10639 -- instance is the prefix of the name of the generic unit.
10640
10641 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10642 and then Nkind (Gen_Id) = N_Expanded_Name
10643 then
10644 Par_Ent := Entity (Prefix (Gen_Id));
10645 Par_Vis := Is_Immediately_Visible (Par_Ent);
10646 Install_Parent (Par_Ent, In_Body => True);
10647 Parent_Installed := True;
10648
10649 elsif Is_Child_Unit (Gen_Unit) then
10650 Par_Ent := Scope (Gen_Unit);
10651 Par_Vis := Is_Immediately_Visible (Par_Ent);
10652 Install_Parent (Par_Ent, In_Body => True);
10653 Parent_Installed := True;
10654 end if;
10655
10656 -- Inside its body, a reference to the generic unit is a reference
10657 -- to the instance. The corresponding renaming is the first
10658 -- declaration in the body.
10659
10660 Unit_Renaming :=
10661 Make_Subprogram_Renaming_Declaration (Loc,
10662 Specification =>
10663 Copy_Generic_Node (
10664 Specification (Original_Node (Gen_Body)),
10665 Empty,
10666 Instantiating => True),
10667 Name => New_Occurrence_Of (Anon_Id, Loc));
10668
10669 -- If there is a formal subprogram with the same name as the unit
10670 -- itself, do not add this renaming declaration. This is a temporary
10671 -- fix for one ACVC test. ???
10672
10673 Prev_Formal := First_Entity (Pack_Id);
10674 while Present (Prev_Formal) loop
10675 if Chars (Prev_Formal) = Chars (Gen_Unit)
10676 and then Is_Overloadable (Prev_Formal)
10677 then
10678 exit;
10679 end if;
10680
10681 Next_Entity (Prev_Formal);
10682 end loop;
10683
10684 if Present (Prev_Formal) then
10685 Decls := New_List (Act_Body);
10686 else
10687 Decls := New_List (Unit_Renaming, Act_Body);
10688 end if;
10689
10690 -- The subprogram body is placed in the body of a dummy package body,
10691 -- whose spec contains the subprogram declaration as well as the
10692 -- renaming declarations for the generic parameters.
10693
10694 Pack_Body := Make_Package_Body (Loc,
10695 Defining_Unit_Name => New_Copy (Pack_Id),
10696 Declarations => Decls);
10697
10698 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10699
10700 -- If the instantiation is a library unit, then build resulting
10701 -- compilation unit nodes for the instance. The declaration of
10702 -- the enclosing package is the grandparent of the subprogram
10703 -- declaration. First replace the instantiation node as the unit
10704 -- of the corresponding compilation.
10705
10706 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10707 if Parent (Inst_Node) = Cunit (Main_Unit) then
10708 Set_Unit (Parent (Inst_Node), Inst_Node);
10709 Build_Instance_Compilation_Unit_Nodes
10710 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
10711 Analyze (Inst_Node);
10712 else
10713 Set_Parent (Pack_Body, Parent (Inst_Node));
10714 Analyze (Pack_Body);
10715 end if;
10716
10717 else
10718 Insert_Before (Inst_Node, Pack_Body);
10719 Mark_Rewrite_Insertion (Pack_Body);
10720 Analyze (Pack_Body);
10721
10722 if Expander_Active then
10723 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
10724 end if;
10725 end if;
10726
10727 Inherit_Context (Gen_Body, Inst_Node);
10728
10729 Restore_Private_Views (Pack_Id, False);
10730
10731 if Parent_Installed then
10732 Remove_Parent (In_Body => True);
10733
10734 -- Restore the previous visibility of the parent
10735
10736 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10737 end if;
10738
10739 Restore_Env;
10740 Style_Check := Saved_Style_Check;
10741 Restore_Warnings (Saved_Warnings);
10742
10743 -- Body not found. Error was emitted already. If there were no previous
10744 -- errors, this may be an instance whose scope is a premature instance.
10745 -- In that case we must insure that the (legal) program does raise
10746 -- program error if executed. We generate a subprogram body for this
10747 -- purpose. See DEC ac30vso.
10748
10749 -- Should not reference proprietary DEC tests in comments ???
10750
10751 elsif Serious_Errors_Detected = 0
10752 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
10753 then
10754 if Body_Optional then
10755 return;
10756
10757 elsif Ekind (Anon_Id) = E_Procedure then
10758 Act_Body :=
10759 Make_Subprogram_Body (Loc,
10760 Specification =>
10761 Make_Procedure_Specification (Loc,
10762 Defining_Unit_Name =>
10763 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10764 Parameter_Specifications =>
10765 New_Copy_List
10766 (Parameter_Specifications (Parent (Anon_Id)))),
10767
10768 Declarations => Empty_List,
10769 Handled_Statement_Sequence =>
10770 Make_Handled_Sequence_Of_Statements (Loc,
10771 Statements =>
10772 New_List (
10773 Make_Raise_Program_Error (Loc,
10774 Reason =>
10775 PE_Access_Before_Elaboration))));
10776
10777 else
10778 Ret_Expr :=
10779 Make_Raise_Program_Error (Loc,
10780 Reason => PE_Access_Before_Elaboration);
10781
10782 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
10783 Set_Analyzed (Ret_Expr);
10784
10785 Act_Body :=
10786 Make_Subprogram_Body (Loc,
10787 Specification =>
10788 Make_Function_Specification (Loc,
10789 Defining_Unit_Name =>
10790 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10791 Parameter_Specifications =>
10792 New_Copy_List
10793 (Parameter_Specifications (Parent (Anon_Id))),
10794 Result_Definition =>
10795 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10796
10797 Declarations => Empty_List,
10798 Handled_Statement_Sequence =>
10799 Make_Handled_Sequence_Of_Statements (Loc,
10800 Statements =>
10801 New_List
10802 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10803 end if;
10804
10805 Pack_Body := Make_Package_Body (Loc,
10806 Defining_Unit_Name => New_Copy (Pack_Id),
10807 Declarations => New_List (Act_Body));
10808
10809 Insert_After (Inst_Node, Pack_Body);
10810 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10811 Analyze (Pack_Body);
10812 end if;
10813
10814 Expander_Mode_Restore;
10815 end Instantiate_Subprogram_Body;
10816
10817 ----------------------
10818 -- Instantiate_Type --
10819 ----------------------
10820
10821 function Instantiate_Type
10822 (Formal : Node_Id;
10823 Actual : Node_Id;
10824 Analyzed_Formal : Node_Id;
10825 Actual_Decls : List_Id) return List_Id
10826 is
10827 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10828 A_Gen_T : constant Entity_Id :=
10829 Defining_Identifier (Analyzed_Formal);
10830 Ancestor : Entity_Id := Empty;
10831 Def : constant Node_Id := Formal_Type_Definition (Formal);
10832 Act_T : Entity_Id;
10833 Decl_Node : Node_Id;
10834 Decl_Nodes : List_Id;
10835 Loc : Source_Ptr;
10836 Subt : Entity_Id;
10837
10838 procedure Diagnose_Predicated_Actual;
10839 -- There are a number of constructs in which a discrete type with
10840 -- predicates is illegal, e.g. as an index in an array type declaration.
10841 -- If a generic type is used is such a construct in a generic package
10842 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
10843 -- of the generic contract that the actual cannot have predicates.
10844
10845 procedure Validate_Array_Type_Instance;
10846 procedure Validate_Access_Subprogram_Instance;
10847 procedure Validate_Access_Type_Instance;
10848 procedure Validate_Derived_Type_Instance;
10849 procedure Validate_Derived_Interface_Type_Instance;
10850 procedure Validate_Discriminated_Formal_Type;
10851 procedure Validate_Interface_Type_Instance;
10852 procedure Validate_Private_Type_Instance;
10853 procedure Validate_Incomplete_Type_Instance;
10854 -- These procedures perform validation tests for the named case.
10855 -- Validate_Discriminated_Formal_Type is shared by formal private
10856 -- types and Ada 2012 formal incomplete types.
10857
10858 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
10859 -- Check that base types are the same and that the subtypes match
10860 -- statically. Used in several of the above.
10861
10862 ---------------------------------
10863 -- Diagnose_Predicated_Actual --
10864 ---------------------------------
10865
10866 procedure Diagnose_Predicated_Actual is
10867 begin
10868 if No_Predicate_On_Actual (A_Gen_T)
10869 and then Has_Predicates (Act_T)
10870 then
10871 Error_Msg_NE
10872 ("actual for& cannot be a type with predicate",
10873 Instantiation_Node, A_Gen_T);
10874
10875 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
10876 and then Has_Predicates (Act_T)
10877 and then not Has_Static_Predicate_Aspect (Act_T)
10878 then
10879 Error_Msg_NE
10880 ("actual for& cannot be a type with a dynamic predicate",
10881 Instantiation_Node, A_Gen_T);
10882 end if;
10883 end Diagnose_Predicated_Actual;
10884
10885 --------------------
10886 -- Subtypes_Match --
10887 --------------------
10888
10889 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
10890 T : constant Entity_Id := Get_Instance_Of (Gen_T);
10891
10892 begin
10893 -- Some detailed comments would be useful here ???
10894
10895 return ((Base_Type (T) = Act_T
10896 or else Base_Type (T) = Base_Type (Act_T))
10897 and then Subtypes_Statically_Match (T, Act_T))
10898
10899 or else (Is_Class_Wide_Type (Gen_T)
10900 and then Is_Class_Wide_Type (Act_T)
10901 and then Subtypes_Match
10902 (Get_Instance_Of (Root_Type (Gen_T)),
10903 Root_Type (Act_T)))
10904
10905 or else
10906 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
10907 E_Anonymous_Access_Type)
10908 and then Ekind (Act_T) = Ekind (Gen_T)
10909 and then Subtypes_Statically_Match
10910 (Designated_Type (Gen_T), Designated_Type (Act_T)));
10911 end Subtypes_Match;
10912
10913 -----------------------------------------
10914 -- Validate_Access_Subprogram_Instance --
10915 -----------------------------------------
10916
10917 procedure Validate_Access_Subprogram_Instance is
10918 begin
10919 if not Is_Access_Type (Act_T)
10920 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
10921 then
10922 Error_Msg_NE
10923 ("expect access type in instantiation of &", Actual, Gen_T);
10924 Abandon_Instantiation (Actual);
10925 end if;
10926
10927 -- According to AI05-288, actuals for access_to_subprograms must be
10928 -- subtype conformant with the generic formal. Previous to AI05-288
10929 -- only mode conformance was required.
10930
10931 -- This is a binding interpretation that applies to previous versions
10932 -- of the language, no need to maintain previous weaker checks.
10933
10934 Check_Subtype_Conformant
10935 (Designated_Type (Act_T),
10936 Designated_Type (A_Gen_T),
10937 Actual,
10938 Get_Inst => True);
10939
10940 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
10941 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
10942 Error_Msg_NE
10943 ("protected access type not allowed for formal &",
10944 Actual, Gen_T);
10945 end if;
10946
10947 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
10948 Error_Msg_NE
10949 ("expect protected access type for formal &",
10950 Actual, Gen_T);
10951 end if;
10952 end Validate_Access_Subprogram_Instance;
10953
10954 -----------------------------------
10955 -- Validate_Access_Type_Instance --
10956 -----------------------------------
10957
10958 procedure Validate_Access_Type_Instance is
10959 Desig_Type : constant Entity_Id :=
10960 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
10961 Desig_Act : Entity_Id;
10962
10963 begin
10964 if not Is_Access_Type (Act_T) then
10965 Error_Msg_NE
10966 ("expect access type in instantiation of &", Actual, Gen_T);
10967 Abandon_Instantiation (Actual);
10968 end if;
10969
10970 if Is_Access_Constant (A_Gen_T) then
10971 if not Is_Access_Constant (Act_T) then
10972 Error_Msg_N
10973 ("actual type must be access-to-constant type", Actual);
10974 Abandon_Instantiation (Actual);
10975 end if;
10976 else
10977 if Is_Access_Constant (Act_T) then
10978 Error_Msg_N
10979 ("actual type must be access-to-variable type", Actual);
10980 Abandon_Instantiation (Actual);
10981
10982 elsif Ekind (A_Gen_T) = E_General_Access_Type
10983 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
10984 then
10985 Error_Msg_N -- CODEFIX
10986 ("actual must be general access type!", Actual);
10987 Error_Msg_NE -- CODEFIX
10988 ("add ALL to }!", Actual, Act_T);
10989 Abandon_Instantiation (Actual);
10990 end if;
10991 end if;
10992
10993 -- The designated subtypes, that is to say the subtypes introduced
10994 -- by an access type declaration (and not by a subtype declaration)
10995 -- must match.
10996
10997 Desig_Act := Designated_Type (Base_Type (Act_T));
10998
10999 -- The designated type may have been introduced through a limited_
11000 -- with clause, in which case retrieve the non-limited view. This
11001 -- applies to incomplete types as well as to class-wide types.
11002
11003 if From_Limited_With (Desig_Act) then
11004 Desig_Act := Available_View (Desig_Act);
11005 end if;
11006
11007 if not Subtypes_Match (Desig_Type, Desig_Act) then
11008 Error_Msg_NE
11009 ("designated type of actual does not match that of formal &",
11010 Actual, Gen_T);
11011
11012 if not Predicates_Match (Desig_Type, Desig_Act) then
11013 Error_Msg_N ("\predicates do not match", Actual);
11014 end if;
11015
11016 Abandon_Instantiation (Actual);
11017
11018 elsif Is_Access_Type (Designated_Type (Act_T))
11019 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11020 /=
11021 Is_Constrained (Designated_Type (Desig_Type))
11022 then
11023 Error_Msg_NE
11024 ("designated type of actual does not match that of formal &",
11025 Actual, Gen_T);
11026
11027 if not Predicates_Match (Desig_Type, Desig_Act) then
11028 Error_Msg_N ("\predicates do not match", Actual);
11029 end if;
11030
11031 Abandon_Instantiation (Actual);
11032 end if;
11033
11034 -- Ada 2005: null-exclusion indicators of the two types must agree
11035
11036 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11037 Error_Msg_NE
11038 ("non null exclusion of actual and formal & do not match",
11039 Actual, Gen_T);
11040 end if;
11041 end Validate_Access_Type_Instance;
11042
11043 ----------------------------------
11044 -- Validate_Array_Type_Instance --
11045 ----------------------------------
11046
11047 procedure Validate_Array_Type_Instance is
11048 I1 : Node_Id;
11049 I2 : Node_Id;
11050 T2 : Entity_Id;
11051
11052 function Formal_Dimensions return Int;
11053 -- Count number of dimensions in array type formal
11054
11055 -----------------------
11056 -- Formal_Dimensions --
11057 -----------------------
11058
11059 function Formal_Dimensions return Int is
11060 Num : Int := 0;
11061 Index : Node_Id;
11062
11063 begin
11064 if Nkind (Def) = N_Constrained_Array_Definition then
11065 Index := First (Discrete_Subtype_Definitions (Def));
11066 else
11067 Index := First (Subtype_Marks (Def));
11068 end if;
11069
11070 while Present (Index) loop
11071 Num := Num + 1;
11072 Next_Index (Index);
11073 end loop;
11074
11075 return Num;
11076 end Formal_Dimensions;
11077
11078 -- Start of processing for Validate_Array_Type_Instance
11079
11080 begin
11081 if not Is_Array_Type (Act_T) then
11082 Error_Msg_NE
11083 ("expect array type in instantiation of &", Actual, Gen_T);
11084 Abandon_Instantiation (Actual);
11085
11086 elsif Nkind (Def) = N_Constrained_Array_Definition then
11087 if not (Is_Constrained (Act_T)) then
11088 Error_Msg_NE
11089 ("expect constrained array in instantiation of &",
11090 Actual, Gen_T);
11091 Abandon_Instantiation (Actual);
11092 end if;
11093
11094 else
11095 if Is_Constrained (Act_T) then
11096 Error_Msg_NE
11097 ("expect unconstrained array in instantiation of &",
11098 Actual, Gen_T);
11099 Abandon_Instantiation (Actual);
11100 end if;
11101 end if;
11102
11103 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11104 Error_Msg_NE
11105 ("dimensions of actual do not match formal &", Actual, Gen_T);
11106 Abandon_Instantiation (Actual);
11107 end if;
11108
11109 I1 := First_Index (A_Gen_T);
11110 I2 := First_Index (Act_T);
11111 for J in 1 .. Formal_Dimensions loop
11112
11113 -- If the indexes of the actual were given by a subtype_mark,
11114 -- the index was transformed into a range attribute. Retrieve
11115 -- the original type mark for checking.
11116
11117 if Is_Entity_Name (Original_Node (I2)) then
11118 T2 := Entity (Original_Node (I2));
11119 else
11120 T2 := Etype (I2);
11121 end if;
11122
11123 if not Subtypes_Match
11124 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11125 then
11126 Error_Msg_NE
11127 ("index types of actual do not match those of formal &",
11128 Actual, Gen_T);
11129 Abandon_Instantiation (Actual);
11130 end if;
11131
11132 Next_Index (I1);
11133 Next_Index (I2);
11134 end loop;
11135
11136 -- Check matching subtypes. Note that there are complex visibility
11137 -- issues when the generic is a child unit and some aspect of the
11138 -- generic type is declared in a parent unit of the generic. We do
11139 -- the test to handle this special case only after a direct check
11140 -- for static matching has failed. The case where both the component
11141 -- type and the array type are separate formals, and the component
11142 -- type is a private view may also require special checking in
11143 -- Subtypes_Match.
11144
11145 if Subtypes_Match
11146 (Component_Type (A_Gen_T), Component_Type (Act_T))
11147 or else Subtypes_Match
11148 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11149 Component_Type (Act_T))
11150 then
11151 null;
11152 else
11153 Error_Msg_NE
11154 ("component subtype of actual does not match that of formal &",
11155 Actual, Gen_T);
11156 Abandon_Instantiation (Actual);
11157 end if;
11158
11159 if Has_Aliased_Components (A_Gen_T)
11160 and then not Has_Aliased_Components (Act_T)
11161 then
11162 Error_Msg_NE
11163 ("actual must have aliased components to match formal type &",
11164 Actual, Gen_T);
11165 end if;
11166 end Validate_Array_Type_Instance;
11167
11168 -----------------------------------------------
11169 -- Validate_Derived_Interface_Type_Instance --
11170 -----------------------------------------------
11171
11172 procedure Validate_Derived_Interface_Type_Instance is
11173 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11174 Elmt : Elmt_Id;
11175
11176 begin
11177 -- First apply interface instance checks
11178
11179 Validate_Interface_Type_Instance;
11180
11181 -- Verify that immediate parent interface is an ancestor of
11182 -- the actual.
11183
11184 if Present (Par)
11185 and then not Interface_Present_In_Ancestor (Act_T, Par)
11186 then
11187 Error_Msg_NE
11188 ("interface actual must include progenitor&", Actual, Par);
11189 end if;
11190
11191 -- Now verify that the actual includes all other ancestors of
11192 -- the formal.
11193
11194 Elmt := First_Elmt (Interfaces (A_Gen_T));
11195 while Present (Elmt) loop
11196 if not Interface_Present_In_Ancestor
11197 (Act_T, Get_Instance_Of (Node (Elmt)))
11198 then
11199 Error_Msg_NE
11200 ("interface actual must include progenitor&",
11201 Actual, Node (Elmt));
11202 end if;
11203
11204 Next_Elmt (Elmt);
11205 end loop;
11206 end Validate_Derived_Interface_Type_Instance;
11207
11208 ------------------------------------
11209 -- Validate_Derived_Type_Instance --
11210 ------------------------------------
11211
11212 procedure Validate_Derived_Type_Instance is
11213 Actual_Discr : Entity_Id;
11214 Ancestor_Discr : Entity_Id;
11215
11216 begin
11217 -- If the parent type in the generic declaration is itself a previous
11218 -- formal type, then it is local to the generic and absent from the
11219 -- analyzed generic definition. In that case the ancestor is the
11220 -- instance of the formal (which must have been instantiated
11221 -- previously), unless the ancestor is itself a formal derived type.
11222 -- In this latter case (which is the subject of Corrigendum 8652/0038
11223 -- (AI-202) the ancestor of the formals is the ancestor of its
11224 -- parent. Otherwise, the analyzed generic carries the parent type.
11225 -- If the parent type is defined in a previous formal package, then
11226 -- the scope of that formal package is that of the generic type
11227 -- itself, and it has already been mapped into the corresponding type
11228 -- in the actual package.
11229
11230 -- Common case: parent type defined outside of the generic
11231
11232 if Is_Entity_Name (Subtype_Mark (Def))
11233 and then Present (Entity (Subtype_Mark (Def)))
11234 then
11235 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11236
11237 -- Check whether parent is defined in a previous formal package
11238
11239 elsif
11240 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11241 then
11242 Ancestor :=
11243 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11244
11245 -- The type may be a local derivation, or a type extension of a
11246 -- previous formal, or of a formal of a parent package.
11247
11248 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11249 or else
11250 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11251 then
11252 -- Check whether the parent is another derived formal type in the
11253 -- same generic unit.
11254
11255 if Etype (A_Gen_T) /= A_Gen_T
11256 and then Is_Generic_Type (Etype (A_Gen_T))
11257 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11258 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11259 then
11260 -- Locate ancestor of parent from the subtype declaration
11261 -- created for the actual.
11262
11263 declare
11264 Decl : Node_Id;
11265
11266 begin
11267 Decl := First (Actual_Decls);
11268 while Present (Decl) loop
11269 if Nkind (Decl) = N_Subtype_Declaration
11270 and then Chars (Defining_Identifier (Decl)) =
11271 Chars (Etype (A_Gen_T))
11272 then
11273 Ancestor := Generic_Parent_Type (Decl);
11274 exit;
11275 else
11276 Next (Decl);
11277 end if;
11278 end loop;
11279 end;
11280
11281 pragma Assert (Present (Ancestor));
11282
11283 -- The ancestor itself may be a previous formal that has been
11284 -- instantiated.
11285
11286 Ancestor := Get_Instance_Of (Ancestor);
11287
11288 else
11289 Ancestor :=
11290 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11291 end if;
11292
11293 -- An unusual case: the actual is a type declared in a parent unit,
11294 -- but is not a formal type so there is no instance_of for it.
11295 -- Retrieve it by analyzing the record extension.
11296
11297 elsif Is_Child_Unit (Scope (A_Gen_T))
11298 and then In_Open_Scopes (Scope (Act_T))
11299 and then Is_Generic_Instance (Scope (Act_T))
11300 then
11301 Analyze (Subtype_Mark (Def));
11302 Ancestor := Entity (Subtype_Mark (Def));
11303
11304 else
11305 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11306 end if;
11307
11308 -- If the formal derived type has pragma Preelaborable_Initialization
11309 -- then the actual type must have preelaborable initialization.
11310
11311 if Known_To_Have_Preelab_Init (A_Gen_T)
11312 and then not Has_Preelaborable_Initialization (Act_T)
11313 then
11314 Error_Msg_NE
11315 ("actual for & must have preelaborable initialization",
11316 Actual, Gen_T);
11317 end if;
11318
11319 -- Ada 2005 (AI-251)
11320
11321 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11322 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11323 Error_Msg_NE
11324 ("(Ada 2005) expected type implementing & in instantiation",
11325 Actual, Ancestor);
11326 end if;
11327
11328 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11329 Error_Msg_NE
11330 ("expect type derived from & in instantiation",
11331 Actual, First_Subtype (Ancestor));
11332 Abandon_Instantiation (Actual);
11333 end if;
11334
11335 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11336 -- that the formal type declaration has been rewritten as a private
11337 -- extension.
11338
11339 if Ada_Version >= Ada_2005
11340 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11341 and then Synchronized_Present (Parent (A_Gen_T))
11342 then
11343 -- The actual must be a synchronized tagged type
11344
11345 if not Is_Tagged_Type (Act_T) then
11346 Error_Msg_N
11347 ("actual of synchronized type must be tagged", Actual);
11348 Abandon_Instantiation (Actual);
11349
11350 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11351 and then Nkind (Type_Definition (Parent (Act_T))) =
11352 N_Derived_Type_Definition
11353 and then not Synchronized_Present (Type_Definition
11354 (Parent (Act_T)))
11355 then
11356 Error_Msg_N
11357 ("actual of synchronized type must be synchronized", Actual);
11358 Abandon_Instantiation (Actual);
11359 end if;
11360 end if;
11361
11362 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11363 -- removes the second instance of the phrase "or allow pass by copy".
11364
11365 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11366 Error_Msg_N
11367 ("cannot have atomic actual type for non-atomic formal type",
11368 Actual);
11369
11370 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11371 Error_Msg_N
11372 ("cannot have volatile actual type for non-volatile formal type",
11373 Actual);
11374 end if;
11375
11376 -- It should not be necessary to check for unknown discriminants on
11377 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11378 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11379 -- needs fixing. ???
11380
11381 if not Is_Indefinite_Subtype (A_Gen_T)
11382 and then not Unknown_Discriminants_Present (Formal)
11383 and then Is_Indefinite_Subtype (Act_T)
11384 then
11385 Error_Msg_N
11386 ("actual subtype must be constrained", Actual);
11387 Abandon_Instantiation (Actual);
11388 end if;
11389
11390 if not Unknown_Discriminants_Present (Formal) then
11391 if Is_Constrained (Ancestor) then
11392 if not Is_Constrained (Act_T) then
11393 Error_Msg_N
11394 ("actual subtype must be constrained", Actual);
11395 Abandon_Instantiation (Actual);
11396 end if;
11397
11398 -- Ancestor is unconstrained, Check if generic formal and actual
11399 -- agree on constrainedness. The check only applies to array types
11400 -- and discriminated types.
11401
11402 elsif Is_Constrained (Act_T) then
11403 if Ekind (Ancestor) = E_Access_Type
11404 or else
11405 (not Is_Constrained (A_Gen_T)
11406 and then Is_Composite_Type (A_Gen_T))
11407 then
11408 Error_Msg_N
11409 ("actual subtype must be unconstrained", Actual);
11410 Abandon_Instantiation (Actual);
11411 end if;
11412
11413 -- A class-wide type is only allowed if the formal has unknown
11414 -- discriminants.
11415
11416 elsif Is_Class_Wide_Type (Act_T)
11417 and then not Has_Unknown_Discriminants (Ancestor)
11418 then
11419 Error_Msg_NE
11420 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11421 Abandon_Instantiation (Actual);
11422
11423 -- Otherwise, the formal and actual must have the same number
11424 -- of discriminants and each discriminant of the actual must
11425 -- correspond to a discriminant of the formal.
11426
11427 elsif Has_Discriminants (Act_T)
11428 and then not Has_Unknown_Discriminants (Act_T)
11429 and then Has_Discriminants (Ancestor)
11430 then
11431 Actual_Discr := First_Discriminant (Act_T);
11432 Ancestor_Discr := First_Discriminant (Ancestor);
11433 while Present (Actual_Discr)
11434 and then Present (Ancestor_Discr)
11435 loop
11436 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11437 No (Corresponding_Discriminant (Actual_Discr))
11438 then
11439 Error_Msg_NE
11440 ("discriminant & does not correspond " &
11441 "to ancestor discriminant", Actual, Actual_Discr);
11442 Abandon_Instantiation (Actual);
11443 end if;
11444
11445 Next_Discriminant (Actual_Discr);
11446 Next_Discriminant (Ancestor_Discr);
11447 end loop;
11448
11449 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11450 Error_Msg_NE
11451 ("actual for & must have same number of discriminants",
11452 Actual, Gen_T);
11453 Abandon_Instantiation (Actual);
11454 end if;
11455
11456 -- This case should be caught by the earlier check for
11457 -- constrainedness, but the check here is added for completeness.
11458
11459 elsif Has_Discriminants (Act_T)
11460 and then not Has_Unknown_Discriminants (Act_T)
11461 then
11462 Error_Msg_NE
11463 ("actual for & must not have discriminants", Actual, Gen_T);
11464 Abandon_Instantiation (Actual);
11465
11466 elsif Has_Discriminants (Ancestor) then
11467 Error_Msg_NE
11468 ("actual for & must have known discriminants", Actual, Gen_T);
11469 Abandon_Instantiation (Actual);
11470 end if;
11471
11472 if not Subtypes_Statically_Compatible
11473 (Act_T, Ancestor, Formal_Derived_Matching => True)
11474 then
11475 Error_Msg_N
11476 ("constraint on actual is incompatible with formal", Actual);
11477 Abandon_Instantiation (Actual);
11478 end if;
11479 end if;
11480
11481 -- If the formal and actual types are abstract, check that there
11482 -- are no abstract primitives of the actual type that correspond to
11483 -- nonabstract primitives of the formal type (second sentence of
11484 -- RM95-3.9.3(9)).
11485
11486 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11487 Check_Abstract_Primitives : declare
11488 Gen_Prims : constant Elist_Id :=
11489 Primitive_Operations (A_Gen_T);
11490 Gen_Elmt : Elmt_Id;
11491 Gen_Subp : Entity_Id;
11492 Anc_Subp : Entity_Id;
11493 Anc_Formal : Entity_Id;
11494 Anc_F_Type : Entity_Id;
11495
11496 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11497 Act_Elmt : Elmt_Id;
11498 Act_Subp : Entity_Id;
11499 Act_Formal : Entity_Id;
11500 Act_F_Type : Entity_Id;
11501
11502 Subprograms_Correspond : Boolean;
11503
11504 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11505 -- Returns true if T2 is derived directly or indirectly from
11506 -- T1, including derivations from interfaces. T1 and T2 are
11507 -- required to be specific tagged base types.
11508
11509 ------------------------
11510 -- Is_Tagged_Ancestor --
11511 ------------------------
11512
11513 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11514 is
11515 Intfc_Elmt : Elmt_Id;
11516
11517 begin
11518 -- The predicate is satisfied if the types are the same
11519
11520 if T1 = T2 then
11521 return True;
11522
11523 -- If we've reached the top of the derivation chain then
11524 -- we know that T1 is not an ancestor of T2.
11525
11526 elsif Etype (T2) = T2 then
11527 return False;
11528
11529 -- Proceed to check T2's immediate parent
11530
11531 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11532 return True;
11533
11534 -- Finally, check to see if T1 is an ancestor of any of T2's
11535 -- progenitors.
11536
11537 else
11538 Intfc_Elmt := First_Elmt (Interfaces (T2));
11539 while Present (Intfc_Elmt) loop
11540 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11541 return True;
11542 end if;
11543
11544 Next_Elmt (Intfc_Elmt);
11545 end loop;
11546 end if;
11547
11548 return False;
11549 end Is_Tagged_Ancestor;
11550
11551 -- Start of processing for Check_Abstract_Primitives
11552
11553 begin
11554 -- Loop over all of the formal derived type's primitives
11555
11556 Gen_Elmt := First_Elmt (Gen_Prims);
11557 while Present (Gen_Elmt) loop
11558 Gen_Subp := Node (Gen_Elmt);
11559
11560 -- If the primitive of the formal is not abstract, then
11561 -- determine whether there is a corresponding primitive of
11562 -- the actual type that's abstract.
11563
11564 if not Is_Abstract_Subprogram (Gen_Subp) then
11565 Act_Elmt := First_Elmt (Act_Prims);
11566 while Present (Act_Elmt) loop
11567 Act_Subp := Node (Act_Elmt);
11568
11569 -- If we find an abstract primitive of the actual,
11570 -- then we need to test whether it corresponds to the
11571 -- subprogram from which the generic formal primitive
11572 -- is inherited.
11573
11574 if Is_Abstract_Subprogram (Act_Subp) then
11575 Anc_Subp := Alias (Gen_Subp);
11576
11577 -- Test whether we have a corresponding primitive
11578 -- by comparing names, kinds, formal types, and
11579 -- result types.
11580
11581 if Chars (Anc_Subp) = Chars (Act_Subp)
11582 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11583 then
11584 Anc_Formal := First_Formal (Anc_Subp);
11585 Act_Formal := First_Formal (Act_Subp);
11586 while Present (Anc_Formal)
11587 and then Present (Act_Formal)
11588 loop
11589 Anc_F_Type := Etype (Anc_Formal);
11590 Act_F_Type := Etype (Act_Formal);
11591
11592 if Ekind (Anc_F_Type)
11593 = E_Anonymous_Access_Type
11594 then
11595 Anc_F_Type := Designated_Type (Anc_F_Type);
11596
11597 if Ekind (Act_F_Type)
11598 = E_Anonymous_Access_Type
11599 then
11600 Act_F_Type :=
11601 Designated_Type (Act_F_Type);
11602 else
11603 exit;
11604 end if;
11605
11606 elsif
11607 Ekind (Act_F_Type) = E_Anonymous_Access_Type
11608 then
11609 exit;
11610 end if;
11611
11612 Anc_F_Type := Base_Type (Anc_F_Type);
11613 Act_F_Type := Base_Type (Act_F_Type);
11614
11615 -- If the formal is controlling, then the
11616 -- the type of the actual primitive's formal
11617 -- must be derived directly or indirectly
11618 -- from the type of the ancestor primitive's
11619 -- formal.
11620
11621 if Is_Controlling_Formal (Anc_Formal) then
11622 if not Is_Tagged_Ancestor
11623 (Anc_F_Type, Act_F_Type)
11624 then
11625 exit;
11626 end if;
11627
11628 -- Otherwise the types of the formals must
11629 -- be the same.
11630
11631 elsif Anc_F_Type /= Act_F_Type then
11632 exit;
11633 end if;
11634
11635 Next_Entity (Anc_Formal);
11636 Next_Entity (Act_Formal);
11637 end loop;
11638
11639 -- If we traversed through all of the formals
11640 -- then so far the subprograms correspond, so
11641 -- now check that any result types correspond.
11642
11643 if No (Anc_Formal) and then No (Act_Formal) then
11644 Subprograms_Correspond := True;
11645
11646 if Ekind (Act_Subp) = E_Function then
11647 Anc_F_Type := Etype (Anc_Subp);
11648 Act_F_Type := Etype (Act_Subp);
11649
11650 if Ekind (Anc_F_Type)
11651 = E_Anonymous_Access_Type
11652 then
11653 Anc_F_Type :=
11654 Designated_Type (Anc_F_Type);
11655
11656 if Ekind (Act_F_Type)
11657 = E_Anonymous_Access_Type
11658 then
11659 Act_F_Type :=
11660 Designated_Type (Act_F_Type);
11661 else
11662 Subprograms_Correspond := False;
11663 end if;
11664
11665 elsif
11666 Ekind (Act_F_Type)
11667 = E_Anonymous_Access_Type
11668 then
11669 Subprograms_Correspond := False;
11670 end if;
11671
11672 Anc_F_Type := Base_Type (Anc_F_Type);
11673 Act_F_Type := Base_Type (Act_F_Type);
11674
11675 -- Now either the result types must be
11676 -- the same or, if the result type is
11677 -- controlling, the result type of the
11678 -- actual primitive must descend from the
11679 -- result type of the ancestor primitive.
11680
11681 if Subprograms_Correspond
11682 and then Anc_F_Type /= Act_F_Type
11683 and then
11684 Has_Controlling_Result (Anc_Subp)
11685 and then
11686 not Is_Tagged_Ancestor
11687 (Anc_F_Type, Act_F_Type)
11688 then
11689 Subprograms_Correspond := False;
11690 end if;
11691 end if;
11692
11693 -- Found a matching subprogram belonging to
11694 -- formal ancestor type, so actual subprogram
11695 -- corresponds and this violates 3.9.3(9).
11696
11697 if Subprograms_Correspond then
11698 Error_Msg_NE
11699 ("abstract subprogram & overrides " &
11700 "nonabstract subprogram of ancestor",
11701 Actual,
11702 Act_Subp);
11703 end if;
11704 end if;
11705 end if;
11706 end if;
11707
11708 Next_Elmt (Act_Elmt);
11709 end loop;
11710 end if;
11711
11712 Next_Elmt (Gen_Elmt);
11713 end loop;
11714 end Check_Abstract_Primitives;
11715 end if;
11716
11717 -- Verify that limitedness matches. If parent is a limited
11718 -- interface then the generic formal is not unless declared
11719 -- explicitly so. If not declared limited, the actual cannot be
11720 -- limited (see AI05-0087).
11721
11722 -- Even though this AI is a binding interpretation, we enable the
11723 -- check only in Ada 2012 mode, because this improper construct
11724 -- shows up in user code and in existing B-tests.
11725
11726 if Is_Limited_Type (Act_T)
11727 and then not Is_Limited_Type (A_Gen_T)
11728 and then Ada_Version >= Ada_2012
11729 then
11730 if In_Instance then
11731 null;
11732 else
11733 Error_Msg_NE
11734 ("actual for non-limited & cannot be a limited type", Actual,
11735 Gen_T);
11736 Explain_Limited_Type (Act_T, Actual);
11737 Abandon_Instantiation (Actual);
11738 end if;
11739 end if;
11740 end Validate_Derived_Type_Instance;
11741
11742 ----------------------------------------
11743 -- Validate_Discriminated_Formal_Type --
11744 ----------------------------------------
11745
11746 procedure Validate_Discriminated_Formal_Type is
11747 Formal_Discr : Entity_Id;
11748 Actual_Discr : Entity_Id;
11749 Formal_Subt : Entity_Id;
11750
11751 begin
11752 if Has_Discriminants (A_Gen_T) then
11753 if not Has_Discriminants (Act_T) then
11754 Error_Msg_NE
11755 ("actual for & must have discriminants", Actual, Gen_T);
11756 Abandon_Instantiation (Actual);
11757
11758 elsif Is_Constrained (Act_T) then
11759 Error_Msg_NE
11760 ("actual for & must be unconstrained", Actual, Gen_T);
11761 Abandon_Instantiation (Actual);
11762
11763 else
11764 Formal_Discr := First_Discriminant (A_Gen_T);
11765 Actual_Discr := First_Discriminant (Act_T);
11766 while Formal_Discr /= Empty loop
11767 if Actual_Discr = Empty then
11768 Error_Msg_NE
11769 ("discriminants on actual do not match formal",
11770 Actual, Gen_T);
11771 Abandon_Instantiation (Actual);
11772 end if;
11773
11774 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
11775
11776 -- Access discriminants match if designated types do
11777
11778 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
11779 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
11780 E_Anonymous_Access_Type
11781 and then
11782 Get_Instance_Of
11783 (Designated_Type (Base_Type (Formal_Subt))) =
11784 Designated_Type (Base_Type (Etype (Actual_Discr)))
11785 then
11786 null;
11787
11788 elsif Base_Type (Formal_Subt) /=
11789 Base_Type (Etype (Actual_Discr))
11790 then
11791 Error_Msg_NE
11792 ("types of actual discriminants must match formal",
11793 Actual, Gen_T);
11794 Abandon_Instantiation (Actual);
11795
11796 elsif not Subtypes_Statically_Match
11797 (Formal_Subt, Etype (Actual_Discr))
11798 and then Ada_Version >= Ada_95
11799 then
11800 Error_Msg_NE
11801 ("subtypes of actual discriminants must match formal",
11802 Actual, Gen_T);
11803 Abandon_Instantiation (Actual);
11804 end if;
11805
11806 Next_Discriminant (Formal_Discr);
11807 Next_Discriminant (Actual_Discr);
11808 end loop;
11809
11810 if Actual_Discr /= Empty then
11811 Error_Msg_NE
11812 ("discriminants on actual do not match formal",
11813 Actual, Gen_T);
11814 Abandon_Instantiation (Actual);
11815 end if;
11816 end if;
11817 end if;
11818 end Validate_Discriminated_Formal_Type;
11819
11820 ---------------------------------------
11821 -- Validate_Incomplete_Type_Instance --
11822 ---------------------------------------
11823
11824 procedure Validate_Incomplete_Type_Instance is
11825 begin
11826 if not Is_Tagged_Type (Act_T)
11827 and then Is_Tagged_Type (A_Gen_T)
11828 then
11829 Error_Msg_NE
11830 ("actual for & must be a tagged type", Actual, Gen_T);
11831 end if;
11832
11833 Validate_Discriminated_Formal_Type;
11834 end Validate_Incomplete_Type_Instance;
11835
11836 --------------------------------------
11837 -- Validate_Interface_Type_Instance --
11838 --------------------------------------
11839
11840 procedure Validate_Interface_Type_Instance is
11841 begin
11842 if not Is_Interface (Act_T) then
11843 Error_Msg_NE
11844 ("actual for formal interface type must be an interface",
11845 Actual, Gen_T);
11846
11847 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
11848 or else
11849 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
11850 or else
11851 Is_Protected_Interface (A_Gen_T) /=
11852 Is_Protected_Interface (Act_T)
11853 or else
11854 Is_Synchronized_Interface (A_Gen_T) /=
11855 Is_Synchronized_Interface (Act_T)
11856 then
11857 Error_Msg_NE
11858 ("actual for interface& does not match (RM 12.5.5(4))",
11859 Actual, Gen_T);
11860 end if;
11861 end Validate_Interface_Type_Instance;
11862
11863 ------------------------------------
11864 -- Validate_Private_Type_Instance --
11865 ------------------------------------
11866
11867 procedure Validate_Private_Type_Instance is
11868 begin
11869 if Is_Limited_Type (Act_T)
11870 and then not Is_Limited_Type (A_Gen_T)
11871 then
11872 if In_Instance then
11873 null;
11874 else
11875 Error_Msg_NE
11876 ("actual for non-limited & cannot be a limited type", Actual,
11877 Gen_T);
11878 Explain_Limited_Type (Act_T, Actual);
11879 Abandon_Instantiation (Actual);
11880 end if;
11881
11882 elsif Known_To_Have_Preelab_Init (A_Gen_T)
11883 and then not Has_Preelaborable_Initialization (Act_T)
11884 then
11885 Error_Msg_NE
11886 ("actual for & must have preelaborable initialization", Actual,
11887 Gen_T);
11888
11889 elsif Is_Indefinite_Subtype (Act_T)
11890 and then not Is_Indefinite_Subtype (A_Gen_T)
11891 and then Ada_Version >= Ada_95
11892 then
11893 Error_Msg_NE
11894 ("actual for & must be a definite subtype", Actual, Gen_T);
11895
11896 elsif not Is_Tagged_Type (Act_T)
11897 and then Is_Tagged_Type (A_Gen_T)
11898 then
11899 Error_Msg_NE
11900 ("actual for & must be a tagged type", Actual, Gen_T);
11901 end if;
11902
11903 Validate_Discriminated_Formal_Type;
11904 Ancestor := Gen_T;
11905 end Validate_Private_Type_Instance;
11906
11907 -- Start of processing for Instantiate_Type
11908
11909 begin
11910 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
11911 Error_Msg_N ("duplicate instantiation of generic type", Actual);
11912 return New_List (Error);
11913
11914 elsif not Is_Entity_Name (Actual)
11915 or else not Is_Type (Entity (Actual))
11916 then
11917 Error_Msg_NE
11918 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
11919 Abandon_Instantiation (Actual);
11920
11921 else
11922 Act_T := Entity (Actual);
11923
11924 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
11925 -- as a generic actual parameter if the corresponding formal type
11926 -- does not have a known_discriminant_part, or is a formal derived
11927 -- type that is an Unchecked_Union type.
11928
11929 if Is_Unchecked_Union (Base_Type (Act_T)) then
11930 if not Has_Discriminants (A_Gen_T)
11931 or else
11932 (Is_Derived_Type (A_Gen_T)
11933 and then
11934 Is_Unchecked_Union (A_Gen_T))
11935 then
11936 null;
11937 else
11938 Error_Msg_N ("unchecked union cannot be the actual for a" &
11939 " discriminated formal type", Act_T);
11940
11941 end if;
11942 end if;
11943
11944 -- Deal with fixed/floating restrictions
11945
11946 if Is_Floating_Point_Type (Act_T) then
11947 Check_Restriction (No_Floating_Point, Actual);
11948 elsif Is_Fixed_Point_Type (Act_T) then
11949 Check_Restriction (No_Fixed_Point, Actual);
11950 end if;
11951
11952 -- Deal with error of using incomplete type as generic actual.
11953 -- This includes limited views of a type, even if the non-limited
11954 -- view may be available.
11955
11956 if Ekind (Act_T) = E_Incomplete_Type
11957 or else (Is_Class_Wide_Type (Act_T)
11958 and then
11959 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
11960 then
11961 -- If the formal is an incomplete type, the actual can be
11962 -- incomplete as well.
11963
11964 if Ekind (A_Gen_T) = E_Incomplete_Type then
11965 null;
11966
11967 elsif Is_Class_Wide_Type (Act_T)
11968 or else No (Full_View (Act_T))
11969 then
11970 Error_Msg_N ("premature use of incomplete type", Actual);
11971 Abandon_Instantiation (Actual);
11972 else
11973 Act_T := Full_View (Act_T);
11974 Set_Entity (Actual, Act_T);
11975
11976 if Has_Private_Component (Act_T) then
11977 Error_Msg_N
11978 ("premature use of type with private component", Actual);
11979 end if;
11980 end if;
11981
11982 -- Deal with error of premature use of private type as generic actual
11983
11984 elsif Is_Private_Type (Act_T)
11985 and then Is_Private_Type (Base_Type (Act_T))
11986 and then not Is_Generic_Type (Act_T)
11987 and then not Is_Derived_Type (Act_T)
11988 and then No (Full_View (Root_Type (Act_T)))
11989 then
11990 -- If the formal is an incomplete type, the actual can be
11991 -- private or incomplete as well.
11992
11993 if Ekind (A_Gen_T) = E_Incomplete_Type then
11994 null;
11995 else
11996 Error_Msg_N ("premature use of private type", Actual);
11997 end if;
11998
11999 elsif Has_Private_Component (Act_T) then
12000 Error_Msg_N
12001 ("premature use of type with private component", Actual);
12002 end if;
12003
12004 Set_Instance_Of (A_Gen_T, Act_T);
12005
12006 -- If the type is generic, the class-wide type may also be used
12007
12008 if Is_Tagged_Type (A_Gen_T)
12009 and then Is_Tagged_Type (Act_T)
12010 and then not Is_Class_Wide_Type (A_Gen_T)
12011 then
12012 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12013 Class_Wide_Type (Act_T));
12014 end if;
12015
12016 if not Is_Abstract_Type (A_Gen_T)
12017 and then Is_Abstract_Type (Act_T)
12018 then
12019 Error_Msg_N
12020 ("actual of non-abstract formal cannot be abstract", Actual);
12021 end if;
12022
12023 -- A generic scalar type is a first subtype for which we generate
12024 -- an anonymous base type. Indicate that the instance of this base
12025 -- is the base type of the actual.
12026
12027 if Is_Scalar_Type (A_Gen_T) then
12028 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12029 end if;
12030 end if;
12031
12032 if Error_Posted (Act_T) then
12033 null;
12034 else
12035 case Nkind (Def) is
12036 when N_Formal_Private_Type_Definition =>
12037 Validate_Private_Type_Instance;
12038
12039 when N_Formal_Incomplete_Type_Definition =>
12040 Validate_Incomplete_Type_Instance;
12041
12042 when N_Formal_Derived_Type_Definition =>
12043 Validate_Derived_Type_Instance;
12044
12045 when N_Formal_Discrete_Type_Definition =>
12046 if not Is_Discrete_Type (Act_T) then
12047 Error_Msg_NE
12048 ("expect discrete type in instantiation of&",
12049 Actual, Gen_T);
12050 Abandon_Instantiation (Actual);
12051 end if;
12052
12053 Diagnose_Predicated_Actual;
12054
12055 when N_Formal_Signed_Integer_Type_Definition =>
12056 if not Is_Signed_Integer_Type (Act_T) then
12057 Error_Msg_NE
12058 ("expect signed integer type in instantiation of&",
12059 Actual, Gen_T);
12060 Abandon_Instantiation (Actual);
12061 end if;
12062
12063 Diagnose_Predicated_Actual;
12064
12065 when N_Formal_Modular_Type_Definition =>
12066 if not Is_Modular_Integer_Type (Act_T) then
12067 Error_Msg_NE
12068 ("expect modular type in instantiation of &",
12069 Actual, Gen_T);
12070 Abandon_Instantiation (Actual);
12071 end if;
12072
12073 Diagnose_Predicated_Actual;
12074
12075 when N_Formal_Floating_Point_Definition =>
12076 if not Is_Floating_Point_Type (Act_T) then
12077 Error_Msg_NE
12078 ("expect float type in instantiation of &", Actual, Gen_T);
12079 Abandon_Instantiation (Actual);
12080 end if;
12081
12082 when N_Formal_Ordinary_Fixed_Point_Definition =>
12083 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12084 Error_Msg_NE
12085 ("expect ordinary fixed point type in instantiation of &",
12086 Actual, Gen_T);
12087 Abandon_Instantiation (Actual);
12088 end if;
12089
12090 when N_Formal_Decimal_Fixed_Point_Definition =>
12091 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12092 Error_Msg_NE
12093 ("expect decimal type in instantiation of &",
12094 Actual, Gen_T);
12095 Abandon_Instantiation (Actual);
12096 end if;
12097
12098 when N_Array_Type_Definition =>
12099 Validate_Array_Type_Instance;
12100
12101 when N_Access_To_Object_Definition =>
12102 Validate_Access_Type_Instance;
12103
12104 when N_Access_Function_Definition |
12105 N_Access_Procedure_Definition =>
12106 Validate_Access_Subprogram_Instance;
12107
12108 when N_Record_Definition =>
12109 Validate_Interface_Type_Instance;
12110
12111 when N_Derived_Type_Definition =>
12112 Validate_Derived_Interface_Type_Instance;
12113
12114 when others =>
12115 raise Program_Error;
12116
12117 end case;
12118 end if;
12119
12120 Subt := New_Copy (Gen_T);
12121
12122 -- Use adjusted sloc of subtype name as the location for other nodes in
12123 -- the subtype declaration.
12124
12125 Loc := Sloc (Subt);
12126
12127 Decl_Node :=
12128 Make_Subtype_Declaration (Loc,
12129 Defining_Identifier => Subt,
12130 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12131
12132 if Is_Private_Type (Act_T) then
12133 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12134
12135 elsif Is_Access_Type (Act_T)
12136 and then Is_Private_Type (Designated_Type (Act_T))
12137 then
12138 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12139 end if;
12140
12141 Decl_Nodes := New_List (Decl_Node);
12142
12143 -- Flag actual derived types so their elaboration produces the
12144 -- appropriate renamings for the primitive operations of the ancestor.
12145 -- Flag actual for formal private types as well, to determine whether
12146 -- operations in the private part may override inherited operations.
12147 -- If the formal has an interface list, the ancestor is not the
12148 -- parent, but the analyzed formal that includes the interface
12149 -- operations of all its progenitors.
12150
12151 -- Same treatment for formal private types, so we can check whether the
12152 -- type is tagged limited when validating derivations in the private
12153 -- part. (See AI05-096).
12154
12155 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12156 if Present (Interface_List (Def)) then
12157 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12158 else
12159 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12160 end if;
12161
12162 elsif Nkind_In (Def,
12163 N_Formal_Private_Type_Definition,
12164 N_Formal_Incomplete_Type_Definition)
12165 then
12166 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12167 end if;
12168
12169 -- If the actual is a synchronized type that implements an interface,
12170 -- the primitive operations are attached to the corresponding record,
12171 -- and we have to treat it as an additional generic actual, so that its
12172 -- primitive operations become visible in the instance. The task or
12173 -- protected type itself does not carry primitive operations.
12174
12175 if Is_Concurrent_Type (Act_T)
12176 and then Is_Tagged_Type (Act_T)
12177 and then Present (Corresponding_Record_Type (Act_T))
12178 and then Present (Ancestor)
12179 and then Is_Interface (Ancestor)
12180 then
12181 declare
12182 Corr_Rec : constant Entity_Id :=
12183 Corresponding_Record_Type (Act_T);
12184 New_Corr : Entity_Id;
12185 Corr_Decl : Node_Id;
12186
12187 begin
12188 New_Corr := Make_Temporary (Loc, 'S');
12189 Corr_Decl :=
12190 Make_Subtype_Declaration (Loc,
12191 Defining_Identifier => New_Corr,
12192 Subtype_Indication =>
12193 New_Occurrence_Of (Corr_Rec, Loc));
12194 Append_To (Decl_Nodes, Corr_Decl);
12195
12196 if Ekind (Act_T) = E_Task_Type then
12197 Set_Ekind (Subt, E_Task_Subtype);
12198 else
12199 Set_Ekind (Subt, E_Protected_Subtype);
12200 end if;
12201
12202 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12203 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12204 Set_Generic_Parent_Type (Decl_Node, Empty);
12205 end;
12206 end if;
12207
12208 return Decl_Nodes;
12209 end Instantiate_Type;
12210
12211 ---------------------
12212 -- Is_In_Main_Unit --
12213 ---------------------
12214
12215 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12216 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12217 Current_Unit : Node_Id;
12218
12219 begin
12220 if Unum = Main_Unit then
12221 return True;
12222
12223 -- If the current unit is a subunit then it is either the main unit or
12224 -- is being compiled as part of the main unit.
12225
12226 elsif Nkind (N) = N_Compilation_Unit then
12227 return Nkind (Unit (N)) = N_Subunit;
12228 end if;
12229
12230 Current_Unit := Parent (N);
12231 while Present (Current_Unit)
12232 and then Nkind (Current_Unit) /= N_Compilation_Unit
12233 loop
12234 Current_Unit := Parent (Current_Unit);
12235 end loop;
12236
12237 -- The instantiation node is in the main unit, or else the current node
12238 -- (perhaps as the result of nested instantiations) is in the main unit,
12239 -- or in the declaration of the main unit, which in this last case must
12240 -- be a body.
12241
12242 return Unum = Main_Unit
12243 or else Current_Unit = Cunit (Main_Unit)
12244 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12245 or else (Present (Library_Unit (Current_Unit))
12246 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12247 end Is_In_Main_Unit;
12248
12249 ----------------------------
12250 -- Load_Parent_Of_Generic --
12251 ----------------------------
12252
12253 procedure Load_Parent_Of_Generic
12254 (N : Node_Id;
12255 Spec : Node_Id;
12256 Body_Optional : Boolean := False)
12257 is
12258 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12259 Saved_Style_Check : constant Boolean := Style_Check;
12260 Saved_Warnings : constant Warning_Record := Save_Warnings;
12261 True_Parent : Node_Id;
12262 Inst_Node : Node_Id;
12263 OK : Boolean;
12264 Previous_Instances : constant Elist_Id := New_Elmt_List;
12265
12266 procedure Collect_Previous_Instances (Decls : List_Id);
12267 -- Collect all instantiations in the given list of declarations, that
12268 -- precede the generic that we need to load. If the bodies of these
12269 -- instantiations are available, we must analyze them, to ensure that
12270 -- the public symbols generated are the same when the unit is compiled
12271 -- to generate code, and when it is compiled in the context of a unit
12272 -- that needs a particular nested instance. This process is applied to
12273 -- both package and subprogram instances.
12274
12275 --------------------------------
12276 -- Collect_Previous_Instances --
12277 --------------------------------
12278
12279 procedure Collect_Previous_Instances (Decls : List_Id) is
12280 Decl : Node_Id;
12281
12282 begin
12283 Decl := First (Decls);
12284 while Present (Decl) loop
12285 if Sloc (Decl) >= Sloc (Inst_Node) then
12286 return;
12287
12288 -- If Decl is an instantiation, then record it as requiring
12289 -- instantiation of the corresponding body, except if it is an
12290 -- abbreviated instantiation generated internally for conformance
12291 -- checking purposes only for the case of a formal package
12292 -- declared without a box (see Instantiate_Formal_Package). Such
12293 -- an instantiation does not generate any code (the actual code
12294 -- comes from actual) and thus does not need to be analyzed here.
12295 -- If the instantiation appears with a generic package body it is
12296 -- not analyzed here either.
12297
12298 elsif Nkind (Decl) = N_Package_Instantiation
12299 and then not Is_Internal (Defining_Entity (Decl))
12300 then
12301 Append_Elmt (Decl, Previous_Instances);
12302
12303 -- For a subprogram instantiation, omit instantiations intrinsic
12304 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12305
12306 elsif Nkind_In (Decl, N_Function_Instantiation,
12307 N_Procedure_Instantiation)
12308 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12309 then
12310 Append_Elmt (Decl, Previous_Instances);
12311
12312 elsif Nkind (Decl) = N_Package_Declaration then
12313 Collect_Previous_Instances
12314 (Visible_Declarations (Specification (Decl)));
12315 Collect_Previous_Instances
12316 (Private_Declarations (Specification (Decl)));
12317
12318 -- Previous non-generic bodies may contain instances as well
12319
12320 elsif Nkind (Decl) = N_Package_Body
12321 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12322 then
12323 Collect_Previous_Instances (Declarations (Decl));
12324
12325 elsif Nkind (Decl) = N_Subprogram_Body
12326 and then not Acts_As_Spec (Decl)
12327 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12328 then
12329 Collect_Previous_Instances (Declarations (Decl));
12330 end if;
12331
12332 Next (Decl);
12333 end loop;
12334 end Collect_Previous_Instances;
12335
12336 -- Start of processing for Load_Parent_Of_Generic
12337
12338 begin
12339 if not In_Same_Source_Unit (N, Spec)
12340 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12341 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12342 and then not Is_In_Main_Unit (Spec))
12343 then
12344 -- Find body of parent of spec, and analyze it. A special case arises
12345 -- when the parent is an instantiation, that is to say when we are
12346 -- currently instantiating a nested generic. In that case, there is
12347 -- no separate file for the body of the enclosing instance. Instead,
12348 -- the enclosing body must be instantiated as if it were a pending
12349 -- instantiation, in order to produce the body for the nested generic
12350 -- we require now. Note that in that case the generic may be defined
12351 -- in a package body, the instance defined in the same package body,
12352 -- and the original enclosing body may not be in the main unit.
12353
12354 Inst_Node := Empty;
12355
12356 True_Parent := Parent (Spec);
12357 while Present (True_Parent)
12358 and then Nkind (True_Parent) /= N_Compilation_Unit
12359 loop
12360 if Nkind (True_Parent) = N_Package_Declaration
12361 and then
12362 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12363 then
12364 -- Parent is a compilation unit that is an instantiation.
12365 -- Instantiation node has been replaced with package decl.
12366
12367 Inst_Node := Original_Node (True_Parent);
12368 exit;
12369
12370 elsif Nkind (True_Parent) = N_Package_Declaration
12371 and then Present (Generic_Parent (Specification (True_Parent)))
12372 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12373 then
12374 -- Parent is an instantiation within another specification.
12375 -- Declaration for instance has been inserted before original
12376 -- instantiation node. A direct link would be preferable?
12377
12378 Inst_Node := Next (True_Parent);
12379 while Present (Inst_Node)
12380 and then Nkind (Inst_Node) /= N_Package_Instantiation
12381 loop
12382 Next (Inst_Node);
12383 end loop;
12384
12385 -- If the instance appears within a generic, and the generic
12386 -- unit is defined within a formal package of the enclosing
12387 -- generic, there is no generic body available, and none
12388 -- needed. A more precise test should be used ???
12389
12390 if No (Inst_Node) then
12391 return;
12392 end if;
12393
12394 exit;
12395
12396 else
12397 True_Parent := Parent (True_Parent);
12398 end if;
12399 end loop;
12400
12401 -- Case where we are currently instantiating a nested generic
12402
12403 if Present (Inst_Node) then
12404 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12405
12406 -- Instantiation node and declaration of instantiated package
12407 -- were exchanged when only the declaration was needed.
12408 -- Restore instantiation node before proceeding with body.
12409
12410 Set_Unit (Parent (True_Parent), Inst_Node);
12411 end if;
12412
12413 -- Now complete instantiation of enclosing body, if it appears in
12414 -- some other unit. If it appears in the current unit, the body
12415 -- will have been instantiated already.
12416
12417 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12418
12419 -- We need to determine the expander mode to instantiate the
12420 -- enclosing body. Because the generic body we need may use
12421 -- global entities declared in the enclosing package (including
12422 -- aggregates) it is in general necessary to compile this body
12423 -- with expansion enabled, except if we are within a generic
12424 -- package, in which case the usual generic rule applies.
12425
12426 declare
12427 Exp_Status : Boolean := True;
12428 Scop : Entity_Id;
12429
12430 begin
12431 -- Loop through scopes looking for generic package
12432
12433 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12434 while Present (Scop)
12435 and then Scop /= Standard_Standard
12436 loop
12437 if Ekind (Scop) = E_Generic_Package then
12438 Exp_Status := False;
12439 exit;
12440 end if;
12441
12442 Scop := Scope (Scop);
12443 end loop;
12444
12445 -- Collect previous instantiations in the unit that contains
12446 -- the desired generic.
12447
12448 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12449 and then not Body_Optional
12450 then
12451 declare
12452 Decl : Elmt_Id;
12453 Info : Pending_Body_Info;
12454 Par : Node_Id;
12455
12456 begin
12457 Par := Parent (Inst_Node);
12458 while Present (Par) loop
12459 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12460 Par := Parent (Par);
12461 end loop;
12462
12463 pragma Assert (Present (Par));
12464
12465 if Nkind (Par) = N_Package_Body then
12466 Collect_Previous_Instances (Declarations (Par));
12467
12468 elsif Nkind (Par) = N_Package_Declaration then
12469 Collect_Previous_Instances
12470 (Visible_Declarations (Specification (Par)));
12471 Collect_Previous_Instances
12472 (Private_Declarations (Specification (Par)));
12473
12474 else
12475 -- Enclosing unit is a subprogram body. In this
12476 -- case all instance bodies are processed in order
12477 -- and there is no need to collect them separately.
12478
12479 null;
12480 end if;
12481
12482 Decl := First_Elmt (Previous_Instances);
12483 while Present (Decl) loop
12484 Info :=
12485 (Inst_Node => Node (Decl),
12486 Act_Decl =>
12487 Instance_Spec (Node (Decl)),
12488 Expander_Status => Exp_Status,
12489 Current_Sem_Unit =>
12490 Get_Code_Unit (Sloc (Node (Decl))),
12491 Scope_Suppress => Scope_Suppress,
12492 Local_Suppress_Stack_Top =>
12493 Local_Suppress_Stack_Top,
12494 Version => Ada_Version,
12495 Version_Pragma => Ada_Version_Pragma,
12496 Warnings => Save_Warnings,
12497 SPARK_Mode => SPARK_Mode,
12498 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12499
12500 -- Package instance
12501
12502 if
12503 Nkind (Node (Decl)) = N_Package_Instantiation
12504 then
12505 Instantiate_Package_Body
12506 (Info, Body_Optional => True);
12507
12508 -- Subprogram instance
12509
12510 else
12511 -- The instance_spec is the wrapper package,
12512 -- and the subprogram declaration is the last
12513 -- declaration in the wrapper.
12514
12515 Info.Act_Decl :=
12516 Last
12517 (Visible_Declarations
12518 (Specification (Info.Act_Decl)));
12519
12520 Instantiate_Subprogram_Body
12521 (Info, Body_Optional => True);
12522 end if;
12523
12524 Next_Elmt (Decl);
12525 end loop;
12526 end;
12527 end if;
12528
12529 Instantiate_Package_Body
12530 (Body_Info =>
12531 ((Inst_Node => Inst_Node,
12532 Act_Decl => True_Parent,
12533 Expander_Status => Exp_Status,
12534 Current_Sem_Unit => Get_Code_Unit
12535 (Sloc (Inst_Node)),
12536 Scope_Suppress => Scope_Suppress,
12537 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12538 Version => Ada_Version,
12539 Version_Pragma => Ada_Version_Pragma,
12540 Warnings => Save_Warnings,
12541 SPARK_Mode => SPARK_Mode,
12542 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12543 Body_Optional => Body_Optional);
12544 end;
12545 end if;
12546
12547 -- Case where we are not instantiating a nested generic
12548
12549 else
12550 Opt.Style_Check := False;
12551 Expander_Mode_Save_And_Set (True);
12552 Load_Needed_Body (Comp_Unit, OK);
12553 Opt.Style_Check := Saved_Style_Check;
12554 Restore_Warnings (Saved_Warnings);
12555 Expander_Mode_Restore;
12556
12557 if not OK
12558 and then Unit_Requires_Body (Defining_Entity (Spec))
12559 and then not Body_Optional
12560 then
12561 declare
12562 Bname : constant Unit_Name_Type :=
12563 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12564
12565 begin
12566 -- In CodePeer mode, the missing body may make the analysis
12567 -- incomplete, but we do not treat it as fatal.
12568
12569 if CodePeer_Mode then
12570 return;
12571
12572 else
12573 Error_Msg_Unit_1 := Bname;
12574 Error_Msg_N ("this instantiation requires$!", N);
12575 Error_Msg_File_1 :=
12576 Get_File_Name (Bname, Subunit => False);
12577 Error_Msg_N ("\but file{ was not found!", N);
12578 raise Unrecoverable_Error;
12579 end if;
12580 end;
12581 end if;
12582 end if;
12583 end if;
12584
12585 -- If loading parent of the generic caused an instantiation circularity,
12586 -- we abandon compilation at this point, because otherwise in some cases
12587 -- we get into trouble with infinite recursions after this point.
12588
12589 if Circularity_Detected then
12590 raise Unrecoverable_Error;
12591 end if;
12592 end Load_Parent_Of_Generic;
12593
12594 ---------------------------------
12595 -- Map_Formal_Package_Entities --
12596 ---------------------------------
12597
12598 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
12599 E1 : Entity_Id;
12600 E2 : Entity_Id;
12601
12602 begin
12603 Set_Instance_Of (Form, Act);
12604
12605 -- Traverse formal and actual package to map the corresponding entities.
12606 -- We skip over internal entities that may be generated during semantic
12607 -- analysis, and find the matching entities by name, given that they
12608 -- must appear in the same order.
12609
12610 E1 := First_Entity (Form);
12611 E2 := First_Entity (Act);
12612 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
12613 -- Could this test be a single condition??? Seems like it could, and
12614 -- isn't FPE (Form) a constant anyway???
12615
12616 if not Is_Internal (E1)
12617 and then Present (Parent (E1))
12618 and then not Is_Class_Wide_Type (E1)
12619 and then not Is_Internal_Name (Chars (E1))
12620 then
12621 while Present (E2) and then Chars (E2) /= Chars (E1) loop
12622 Next_Entity (E2);
12623 end loop;
12624
12625 if No (E2) then
12626 exit;
12627 else
12628 Set_Instance_Of (E1, E2);
12629
12630 if Is_Type (E1) and then Is_Tagged_Type (E2) then
12631 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
12632 end if;
12633
12634 if Is_Constrained (E1) then
12635 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
12636 end if;
12637
12638 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
12639 Map_Formal_Package_Entities (E1, E2);
12640 end if;
12641 end if;
12642 end if;
12643
12644 Next_Entity (E1);
12645 end loop;
12646 end Map_Formal_Package_Entities;
12647
12648 -----------------------
12649 -- Move_Freeze_Nodes --
12650 -----------------------
12651
12652 procedure Move_Freeze_Nodes
12653 (Out_Of : Entity_Id;
12654 After : Node_Id;
12655 L : List_Id)
12656 is
12657 Decl : Node_Id;
12658 Next_Decl : Node_Id;
12659 Next_Node : Node_Id := After;
12660 Spec : Node_Id;
12661
12662 function Is_Outer_Type (T : Entity_Id) return Boolean;
12663 -- Check whether entity is declared in a scope external to that of the
12664 -- generic unit.
12665
12666 -------------------
12667 -- Is_Outer_Type --
12668 -------------------
12669
12670 function Is_Outer_Type (T : Entity_Id) return Boolean is
12671 Scop : Entity_Id := Scope (T);
12672
12673 begin
12674 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
12675 return True;
12676
12677 else
12678 while Scop /= Standard_Standard loop
12679 if Scop = Out_Of then
12680 return False;
12681 else
12682 Scop := Scope (Scop);
12683 end if;
12684 end loop;
12685
12686 return True;
12687 end if;
12688 end Is_Outer_Type;
12689
12690 -- Start of processing for Move_Freeze_Nodes
12691
12692 begin
12693 if No (L) then
12694 return;
12695 end if;
12696
12697 -- First remove the freeze nodes that may appear before all other
12698 -- declarations.
12699
12700 Decl := First (L);
12701 while Present (Decl)
12702 and then Nkind (Decl) = N_Freeze_Entity
12703 and then Is_Outer_Type (Entity (Decl))
12704 loop
12705 Decl := Remove_Head (L);
12706 Insert_After (Next_Node, Decl);
12707 Set_Analyzed (Decl, False);
12708 Next_Node := Decl;
12709 Decl := First (L);
12710 end loop;
12711
12712 -- Next scan the list of declarations and remove each freeze node that
12713 -- appears ahead of the current node.
12714
12715 while Present (Decl) loop
12716 while Present (Next (Decl))
12717 and then Nkind (Next (Decl)) = N_Freeze_Entity
12718 and then Is_Outer_Type (Entity (Next (Decl)))
12719 loop
12720 Next_Decl := Remove_Next (Decl);
12721 Insert_After (Next_Node, Next_Decl);
12722 Set_Analyzed (Next_Decl, False);
12723 Next_Node := Next_Decl;
12724 end loop;
12725
12726 -- If the declaration is a nested package or concurrent type, then
12727 -- recurse. Nested generic packages will have been processed from the
12728 -- inside out.
12729
12730 case Nkind (Decl) is
12731 when N_Package_Declaration =>
12732 Spec := Specification (Decl);
12733
12734 when N_Task_Type_Declaration =>
12735 Spec := Task_Definition (Decl);
12736
12737 when N_Protected_Type_Declaration =>
12738 Spec := Protected_Definition (Decl);
12739
12740 when others =>
12741 Spec := Empty;
12742 end case;
12743
12744 if Present (Spec) then
12745 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
12746 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
12747 end if;
12748
12749 Next (Decl);
12750 end loop;
12751 end Move_Freeze_Nodes;
12752
12753 ----------------
12754 -- Next_Assoc --
12755 ----------------
12756
12757 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
12758 begin
12759 return Generic_Renamings.Table (E).Next_In_HTable;
12760 end Next_Assoc;
12761
12762 ------------------------
12763 -- Preanalyze_Actuals --
12764 ------------------------
12765
12766 procedure Preanalyze_Actuals (N : Node_Id) is
12767 Assoc : Node_Id;
12768 Act : Node_Id;
12769 Errs : constant Int := Serious_Errors_Detected;
12770
12771 Cur : Entity_Id := Empty;
12772 -- Current homograph of the instance name
12773
12774 Vis : Boolean;
12775 -- Saved visibility status of the current homograph
12776
12777 begin
12778 Assoc := First (Generic_Associations (N));
12779
12780 -- If the instance is a child unit, its name may hide an outer homonym,
12781 -- so make it invisible to perform name resolution on the actuals.
12782
12783 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
12784 and then Present
12785 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
12786 then
12787 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
12788
12789 if Is_Compilation_Unit (Cur) then
12790 Vis := Is_Immediately_Visible (Cur);
12791 Set_Is_Immediately_Visible (Cur, False);
12792 else
12793 Cur := Empty;
12794 end if;
12795 end if;
12796
12797 while Present (Assoc) loop
12798 if Nkind (Assoc) /= N_Others_Choice then
12799 Act := Explicit_Generic_Actual_Parameter (Assoc);
12800
12801 -- Within a nested instantiation, a defaulted actual is an empty
12802 -- association, so nothing to analyze. If the subprogram actual
12803 -- is an attribute, analyze prefix only, because actual is not a
12804 -- complete attribute reference.
12805
12806 -- If actual is an allocator, analyze expression only. The full
12807 -- analysis can generate code, and if instance is a compilation
12808 -- unit we have to wait until the package instance is installed
12809 -- to have a proper place to insert this code.
12810
12811 -- String literals may be operators, but at this point we do not
12812 -- know whether the actual is a formal subprogram or a string.
12813
12814 if No (Act) then
12815 null;
12816
12817 elsif Nkind (Act) = N_Attribute_Reference then
12818 Analyze (Prefix (Act));
12819
12820 elsif Nkind (Act) = N_Explicit_Dereference then
12821 Analyze (Prefix (Act));
12822
12823 elsif Nkind (Act) = N_Allocator then
12824 declare
12825 Expr : constant Node_Id := Expression (Act);
12826
12827 begin
12828 if Nkind (Expr) = N_Subtype_Indication then
12829 Analyze (Subtype_Mark (Expr));
12830
12831 -- Analyze separately each discriminant constraint, when
12832 -- given with a named association.
12833
12834 declare
12835 Constr : Node_Id;
12836
12837 begin
12838 Constr := First (Constraints (Constraint (Expr)));
12839 while Present (Constr) loop
12840 if Nkind (Constr) = N_Discriminant_Association then
12841 Analyze (Expression (Constr));
12842 else
12843 Analyze (Constr);
12844 end if;
12845
12846 Next (Constr);
12847 end loop;
12848 end;
12849
12850 else
12851 Analyze (Expr);
12852 end if;
12853 end;
12854
12855 elsif Nkind (Act) /= N_Operator_Symbol then
12856 Analyze (Act);
12857 end if;
12858
12859 -- Ensure that a ghost subprogram does not act as generic actual
12860
12861 if Is_Entity_Name (Act)
12862 and then Is_Ghost_Subprogram (Entity (Act))
12863 then
12864 Error_Msg_N
12865 ("ghost subprogram & cannot act as generic actual", Act);
12866 Abandon_Instantiation (Act);
12867
12868 elsif Errs /= Serious_Errors_Detected then
12869
12870 -- Do a minimal analysis of the generic, to prevent spurious
12871 -- warnings complaining about the generic being unreferenced,
12872 -- before abandoning the instantiation.
12873
12874 Analyze (Name (N));
12875
12876 if Is_Entity_Name (Name (N))
12877 and then Etype (Name (N)) /= Any_Type
12878 then
12879 Generate_Reference (Entity (Name (N)), Name (N));
12880 Set_Is_Instantiated (Entity (Name (N)));
12881 end if;
12882
12883 if Present (Cur) then
12884
12885 -- For the case of a child instance hiding an outer homonym,
12886 -- provide additional warning which might explain the error.
12887
12888 Set_Is_Immediately_Visible (Cur, Vis);
12889 Error_Msg_NE ("& hides outer unit with the same name??",
12890 N, Defining_Unit_Name (N));
12891 end if;
12892
12893 Abandon_Instantiation (Act);
12894 end if;
12895 end if;
12896
12897 Next (Assoc);
12898 end loop;
12899
12900 if Present (Cur) then
12901 Set_Is_Immediately_Visible (Cur, Vis);
12902 end if;
12903 end Preanalyze_Actuals;
12904
12905 -------------------
12906 -- Remove_Parent --
12907 -------------------
12908
12909 procedure Remove_Parent (In_Body : Boolean := False) is
12910 S : Entity_Id := Current_Scope;
12911 -- S is the scope containing the instantiation just completed. The scope
12912 -- stack contains the parent instances of the instantiation, followed by
12913 -- the original S.
12914
12915 Cur_P : Entity_Id;
12916 E : Entity_Id;
12917 P : Entity_Id;
12918 Hidden : Elmt_Id;
12919
12920 begin
12921 -- After child instantiation is complete, remove from scope stack the
12922 -- extra copy of the current scope, and then remove parent instances.
12923
12924 if not In_Body then
12925 Pop_Scope;
12926
12927 while Current_Scope /= S loop
12928 P := Current_Scope;
12929 End_Package_Scope (Current_Scope);
12930
12931 if In_Open_Scopes (P) then
12932 E := First_Entity (P);
12933 while Present (E) loop
12934 Set_Is_Immediately_Visible (E, True);
12935 Next_Entity (E);
12936 end loop;
12937
12938 -- If instantiation is declared in a block, it is the enclosing
12939 -- scope that might be a parent instance. Note that only one
12940 -- block can be involved, because the parent instances have
12941 -- been installed within it.
12942
12943 if Ekind (P) = E_Block then
12944 Cur_P := Scope (P);
12945 else
12946 Cur_P := P;
12947 end if;
12948
12949 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
12950 -- We are within an instance of some sibling. Retain
12951 -- visibility of parent, for proper subsequent cleanup, and
12952 -- reinstall private declarations as well.
12953
12954 Set_In_Private_Part (P);
12955 Install_Private_Declarations (P);
12956 end if;
12957
12958 -- If the ultimate parent is a top-level unit recorded in
12959 -- Instance_Parent_Unit, then reset its visibility to what it was
12960 -- before instantiation. (It's not clear what the purpose is of
12961 -- testing whether Scope (P) is In_Open_Scopes, but that test was
12962 -- present before the ultimate parent test was added.???)
12963
12964 elsif not In_Open_Scopes (Scope (P))
12965 or else (P = Instance_Parent_Unit
12966 and then not Parent_Unit_Visible)
12967 then
12968 Set_Is_Immediately_Visible (P, False);
12969
12970 -- If the current scope is itself an instantiation of a generic
12971 -- nested within P, and we are in the private part of body of this
12972 -- instantiation, restore the full views of P, that were removed
12973 -- in End_Package_Scope above. This obscure case can occur when a
12974 -- subunit of a generic contains an instance of a child unit of
12975 -- its generic parent unit.
12976
12977 elsif S = Current_Scope and then Is_Generic_Instance (S) then
12978 declare
12979 Par : constant Entity_Id :=
12980 Generic_Parent (Package_Specification (S));
12981 begin
12982 if Present (Par)
12983 and then P = Scope (Par)
12984 and then (In_Package_Body (S) or else In_Private_Part (S))
12985 then
12986 Set_In_Private_Part (P);
12987 Install_Private_Declarations (P);
12988 end if;
12989 end;
12990 end if;
12991 end loop;
12992
12993 -- Reset visibility of entities in the enclosing scope
12994
12995 Set_Is_Hidden_Open_Scope (Current_Scope, False);
12996
12997 Hidden := First_Elmt (Hidden_Entities);
12998 while Present (Hidden) loop
12999 Set_Is_Immediately_Visible (Node (Hidden), True);
13000 Next_Elmt (Hidden);
13001 end loop;
13002
13003 else
13004 -- Each body is analyzed separately, and there is no context that
13005 -- needs preserving from one body instance to the next, so remove all
13006 -- parent scopes that have been installed.
13007
13008 while Present (S) loop
13009 End_Package_Scope (S);
13010 Set_Is_Immediately_Visible (S, False);
13011 S := Current_Scope;
13012 exit when S = Standard_Standard;
13013 end loop;
13014 end if;
13015 end Remove_Parent;
13016
13017 -----------------
13018 -- Restore_Env --
13019 -----------------
13020
13021 procedure Restore_Env is
13022 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13023
13024 begin
13025 if No (Current_Instantiated_Parent.Act_Id) then
13026 -- Restore environment after subprogram inlining
13027
13028 Restore_Private_Views (Empty);
13029 end if;
13030
13031 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13032 Exchanged_Views := Saved.Exchanged_Views;
13033 Hidden_Entities := Saved.Hidden_Entities;
13034 Current_Sem_Unit := Saved.Current_Sem_Unit;
13035 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13036 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13037
13038 Restore_Opt_Config_Switches (Saved.Switches);
13039
13040 Instance_Envs.Decrement_Last;
13041 end Restore_Env;
13042
13043 ---------------------------
13044 -- Restore_Private_Views --
13045 ---------------------------
13046
13047 procedure Restore_Private_Views
13048 (Pack_Id : Entity_Id;
13049 Is_Package : Boolean := True)
13050 is
13051 M : Elmt_Id;
13052 E : Entity_Id;
13053 Typ : Entity_Id;
13054 Dep_Elmt : Elmt_Id;
13055 Dep_Typ : Node_Id;
13056
13057 procedure Restore_Nested_Formal (Formal : Entity_Id);
13058 -- Hide the generic formals of formal packages declared with box which
13059 -- were reachable in the current instantiation.
13060
13061 ---------------------------
13062 -- Restore_Nested_Formal --
13063 ---------------------------
13064
13065 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13066 Ent : Entity_Id;
13067
13068 begin
13069 if Present (Renamed_Object (Formal))
13070 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13071 then
13072 return;
13073
13074 elsif Present (Associated_Formal_Package (Formal)) then
13075 Ent := First_Entity (Formal);
13076 while Present (Ent) loop
13077 exit when Ekind (Ent) = E_Package
13078 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13079
13080 Set_Is_Hidden (Ent);
13081 Set_Is_Potentially_Use_Visible (Ent, False);
13082
13083 -- If package, then recurse
13084
13085 if Ekind (Ent) = E_Package then
13086 Restore_Nested_Formal (Ent);
13087 end if;
13088
13089 Next_Entity (Ent);
13090 end loop;
13091 end if;
13092 end Restore_Nested_Formal;
13093
13094 -- Start of processing for Restore_Private_Views
13095
13096 begin
13097 M := First_Elmt (Exchanged_Views);
13098 while Present (M) loop
13099 Typ := Node (M);
13100
13101 -- Subtypes of types whose views have been exchanged, and that are
13102 -- defined within the instance, were not on the Private_Dependents
13103 -- list on entry to the instance, so they have to be exchanged
13104 -- explicitly now, in order to remain consistent with the view of the
13105 -- parent type.
13106
13107 if Ekind_In (Typ, E_Private_Type,
13108 E_Limited_Private_Type,
13109 E_Record_Type_With_Private)
13110 then
13111 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13112 while Present (Dep_Elmt) loop
13113 Dep_Typ := Node (Dep_Elmt);
13114
13115 if Scope (Dep_Typ) = Pack_Id
13116 and then Present (Full_View (Dep_Typ))
13117 then
13118 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13119 Exchange_Declarations (Dep_Typ);
13120 end if;
13121
13122 Next_Elmt (Dep_Elmt);
13123 end loop;
13124 end if;
13125
13126 Exchange_Declarations (Node (M));
13127 Next_Elmt (M);
13128 end loop;
13129
13130 if No (Pack_Id) then
13131 return;
13132 end if;
13133
13134 -- Make the generic formal parameters private, and make the formal types
13135 -- into subtypes of the actuals again.
13136
13137 E := First_Entity (Pack_Id);
13138 while Present (E) loop
13139 Set_Is_Hidden (E, True);
13140
13141 if Is_Type (E)
13142 and then Nkind (Parent (E)) = N_Subtype_Declaration
13143 then
13144 -- If the actual for E is itself a generic actual type from
13145 -- an enclosing instance, E is still a generic actual type
13146 -- outside of the current instance. This matter when resolving
13147 -- an overloaded call that may be ambiguous in the enclosing
13148 -- instance, when two of its actuals coincide.
13149
13150 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13151 and then Is_Generic_Actual_Type
13152 (Entity (Subtype_Indication (Parent (E))))
13153 then
13154 null;
13155 else
13156 Set_Is_Generic_Actual_Type (E, False);
13157 end if;
13158
13159 -- An unusual case of aliasing: the actual may also be directly
13160 -- visible in the generic, and be private there, while it is fully
13161 -- visible in the context of the instance. The internal subtype
13162 -- is private in the instance but has full visibility like its
13163 -- parent in the enclosing scope. This enforces the invariant that
13164 -- the privacy status of all private dependents of a type coincide
13165 -- with that of the parent type. This can only happen when a
13166 -- generic child unit is instantiated within a sibling.
13167
13168 if Is_Private_Type (E)
13169 and then not Is_Private_Type (Etype (E))
13170 then
13171 Exchange_Declarations (E);
13172 end if;
13173
13174 elsif Ekind (E) = E_Package then
13175
13176 -- The end of the renaming list is the renaming of the generic
13177 -- package itself. If the instance is a subprogram, all entities
13178 -- in the corresponding package are renamings. If this entity is
13179 -- a formal package, make its own formals private as well. The
13180 -- actual in this case is itself the renaming of an instantiation.
13181 -- If the entity is not a package renaming, it is the entity
13182 -- created to validate formal package actuals: ignore it.
13183
13184 -- If the actual is itself a formal package for the enclosing
13185 -- generic, or the actual for such a formal package, it remains
13186 -- visible on exit from the instance, and therefore nothing needs
13187 -- to be done either, except to keep it accessible.
13188
13189 if Is_Package and then Renamed_Object (E) = Pack_Id then
13190 exit;
13191
13192 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13193 null;
13194
13195 elsif
13196 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13197 then
13198 Set_Is_Hidden (E, False);
13199
13200 else
13201 declare
13202 Act_P : constant Entity_Id := Renamed_Object (E);
13203 Id : Entity_Id;
13204
13205 begin
13206 Id := First_Entity (Act_P);
13207 while Present (Id)
13208 and then Id /= First_Private_Entity (Act_P)
13209 loop
13210 exit when Ekind (Id) = E_Package
13211 and then Renamed_Object (Id) = Act_P;
13212
13213 Set_Is_Hidden (Id, True);
13214 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13215
13216 if Ekind (Id) = E_Package then
13217 Restore_Nested_Formal (Id);
13218 end if;
13219
13220 Next_Entity (Id);
13221 end loop;
13222 end;
13223 end if;
13224 end if;
13225
13226 Next_Entity (E);
13227 end loop;
13228 end Restore_Private_Views;
13229
13230 --------------
13231 -- Save_Env --
13232 --------------
13233
13234 procedure Save_Env
13235 (Gen_Unit : Entity_Id;
13236 Act_Unit : Entity_Id)
13237 is
13238 begin
13239 Init_Env;
13240 Set_Instance_Env (Gen_Unit, Act_Unit);
13241 end Save_Env;
13242
13243 ----------------------------
13244 -- Save_Global_References --
13245 ----------------------------
13246
13247 procedure Save_Global_References (N : Node_Id) is
13248 Gen_Scope : Entity_Id;
13249 E : Entity_Id;
13250 N2 : Node_Id;
13251
13252 function Is_Global (E : Entity_Id) return Boolean;
13253 -- Check whether entity is defined outside of generic unit. Examine the
13254 -- scope of an entity, and the scope of the scope, etc, until we find
13255 -- either Standard, in which case the entity is global, or the generic
13256 -- unit itself, which indicates that the entity is local. If the entity
13257 -- is the generic unit itself, as in the case of a recursive call, or
13258 -- the enclosing generic unit, if different from the current scope, then
13259 -- it is local as well, because it will be replaced at the point of
13260 -- instantiation. On the other hand, if it is a reference to a child
13261 -- unit of a common ancestor, which appears in an instantiation, it is
13262 -- global because it is used to denote a specific compilation unit at
13263 -- the time the instantiations will be analyzed.
13264
13265 procedure Reset_Entity (N : Node_Id);
13266 -- Save semantic information on global entity so that it is not resolved
13267 -- again at instantiation time.
13268
13269 procedure Save_Entity_Descendants (N : Node_Id);
13270 -- Apply Save_Global_References to the two syntactic descendants of
13271 -- non-terminal nodes that carry an Associated_Node and are processed
13272 -- through Reset_Entity. Once the global entity (if any) has been
13273 -- captured together with its type, only two syntactic descendants need
13274 -- to be traversed to complete the processing of the tree rooted at N.
13275 -- This applies to Selected_Components, Expanded_Names, and to Operator
13276 -- nodes. N can also be a character literal, identifier, or operator
13277 -- symbol node, but the call has no effect in these cases.
13278
13279 procedure Save_Global_Defaults (N1, N2 : Node_Id);
13280 -- Default actuals in nested instances must be handled specially
13281 -- because there is no link to them from the original tree. When an
13282 -- actual subprogram is given by a default, we add an explicit generic
13283 -- association for it in the instantiation node. When we save the
13284 -- global references on the name of the instance, we recover the list
13285 -- of generic associations, and add an explicit one to the original
13286 -- generic tree, through which a global actual can be preserved.
13287 -- Similarly, if a child unit is instantiated within a sibling, in the
13288 -- context of the parent, we must preserve the identifier of the parent
13289 -- so that it can be properly resolved in a subsequent instantiation.
13290
13291 procedure Save_Global_Descendant (D : Union_Id);
13292 -- Apply Save_Global_References recursively to the descendents of the
13293 -- current node.
13294
13295 procedure Save_References (N : Node_Id);
13296 -- This is the recursive procedure that does the work, once the
13297 -- enclosing generic scope has been established.
13298
13299 ---------------
13300 -- Is_Global --
13301 ---------------
13302
13303 function Is_Global (E : Entity_Id) return Boolean is
13304 Se : Entity_Id;
13305
13306 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13307 -- Determine whether the parent node of a reference to a child unit
13308 -- denotes an instantiation or a formal package, in which case the
13309 -- reference to the child unit is global, even if it appears within
13310 -- the current scope (e.g. when the instance appears within the body
13311 -- of an ancestor).
13312
13313 ----------------------
13314 -- Is_Instance_Node --
13315 ----------------------
13316
13317 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13318 begin
13319 return Nkind (Decl) in N_Generic_Instantiation
13320 or else
13321 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13322 end Is_Instance_Node;
13323
13324 -- Start of processing for Is_Global
13325
13326 begin
13327 if E = Gen_Scope then
13328 return False;
13329
13330 elsif E = Standard_Standard then
13331 return True;
13332
13333 elsif Is_Child_Unit (E)
13334 and then (Is_Instance_Node (Parent (N2))
13335 or else (Nkind (Parent (N2)) = N_Expanded_Name
13336 and then N2 = Selector_Name (Parent (N2))
13337 and then
13338 Is_Instance_Node (Parent (Parent (N2)))))
13339 then
13340 return True;
13341
13342 else
13343 Se := Scope (E);
13344 while Se /= Gen_Scope loop
13345 if Se = Standard_Standard then
13346 return True;
13347 else
13348 Se := Scope (Se);
13349 end if;
13350 end loop;
13351
13352 return False;
13353 end if;
13354 end Is_Global;
13355
13356 ------------------
13357 -- Reset_Entity --
13358 ------------------
13359
13360 procedure Reset_Entity (N : Node_Id) is
13361
13362 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13363 -- If the type of N2 is global to the generic unit, save the type in
13364 -- the generic node. Just as we perform name capture for explicit
13365 -- references within the generic, we must capture the global types
13366 -- of local entities because they may participate in resolution in
13367 -- the instance.
13368
13369 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13370 -- Find the ultimate ancestor of the current unit. If it is not a
13371 -- generic unit, then the name of the current unit in the prefix of
13372 -- an expanded name must be replaced with its generic homonym to
13373 -- ensure that it will be properly resolved in an instance.
13374
13375 ---------------------
13376 -- Set_Global_Type --
13377 ---------------------
13378
13379 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13380 Typ : constant Entity_Id := Etype (N2);
13381
13382 begin
13383 Set_Etype (N, Typ);
13384
13385 if Entity (N) /= N2
13386 and then Has_Private_View (Entity (N))
13387 then
13388 -- If the entity of N is not the associated node, this is a
13389 -- nested generic and it has an associated node as well, whose
13390 -- type is already the full view (see below). Indicate that the
13391 -- original node has a private view.
13392
13393 Set_Has_Private_View (N);
13394 end if;
13395
13396 -- If not a private type, nothing else to do
13397
13398 if not Is_Private_Type (Typ) then
13399 if Is_Array_Type (Typ)
13400 and then Is_Private_Type (Component_Type (Typ))
13401 then
13402 Set_Has_Private_View (N);
13403 end if;
13404
13405 -- If it is a derivation of a private type in a context where no
13406 -- full view is needed, nothing to do either.
13407
13408 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13409 null;
13410
13411 -- Otherwise mark the type for flipping and use the full view when
13412 -- available.
13413
13414 else
13415 Set_Has_Private_View (N);
13416
13417 if Present (Full_View (Typ)) then
13418 Set_Etype (N2, Full_View (Typ));
13419 end if;
13420 end if;
13421 end Set_Global_Type;
13422
13423 ------------------
13424 -- Top_Ancestor --
13425 ------------------
13426
13427 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13428 Par : Entity_Id;
13429
13430 begin
13431 Par := E;
13432 while Is_Child_Unit (Par) loop
13433 Par := Scope (Par);
13434 end loop;
13435
13436 return Par;
13437 end Top_Ancestor;
13438
13439 -- Start of processing for Reset_Entity
13440
13441 begin
13442 N2 := Get_Associated_Node (N);
13443 E := Entity (N2);
13444
13445 if Present (E) then
13446
13447 -- If the node is an entry call to an entry in an enclosing task,
13448 -- it is rewritten as a selected component. No global entity to
13449 -- preserve in this case, since the expansion will be redone in
13450 -- the instance.
13451
13452 if not Nkind_In (E, N_Defining_Identifier,
13453 N_Defining_Character_Literal,
13454 N_Defining_Operator_Symbol)
13455 then
13456 Set_Associated_Node (N, Empty);
13457 Set_Etype (N, Empty);
13458 return;
13459 end if;
13460
13461 -- If the entity is an itype created as a subtype of an access
13462 -- type with a null exclusion restore source entity for proper
13463 -- visibility. The itype will be created anew in the instance.
13464
13465 if Is_Itype (E)
13466 and then Ekind (E) = E_Access_Subtype
13467 and then Is_Entity_Name (N)
13468 and then Chars (Etype (E)) = Chars (N)
13469 then
13470 E := Etype (E);
13471 Set_Entity (N2, E);
13472 Set_Etype (N2, E);
13473 end if;
13474
13475 if Is_Global (E) then
13476
13477 -- If the entity is a package renaming that is the prefix of
13478 -- an expanded name, it has been rewritten as the renamed
13479 -- package, which is necessary semantically but complicates
13480 -- ASIS tree traversal, so we recover the original entity to
13481 -- expose the renaming. Take into account that the context may
13482 -- be a nested generic, that the original node may itself have
13483 -- an associated node that had better be an entity, and that
13484 -- the current node is still a selected component.
13485
13486 if Ekind (E) = E_Package
13487 and then Nkind (N) = N_Selected_Component
13488 and then Nkind (Parent (N)) = N_Expanded_Name
13489 and then Present (Original_Node (N2))
13490 and then Is_Entity_Name (Original_Node (N2))
13491 and then Present (Entity (Original_Node (N2)))
13492 then
13493 if Is_Global (Entity (Original_Node (N2))) then
13494 N2 := Original_Node (N2);
13495 Set_Associated_Node (N, N2);
13496 Set_Global_Type (N, N2);
13497
13498 else
13499 -- Renaming is local, and will be resolved in instance
13500
13501 Set_Associated_Node (N, Empty);
13502 Set_Etype (N, Empty);
13503 end if;
13504
13505 else
13506 Set_Global_Type (N, N2);
13507 end if;
13508
13509 elsif Nkind (N) = N_Op_Concat
13510 and then Is_Generic_Type (Etype (N2))
13511 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13512 or else
13513 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13514 and then Is_Intrinsic_Subprogram (E)
13515 then
13516 null;
13517
13518 else
13519 -- Entity is local. Mark generic node as unresolved.
13520 -- Note that now it does not have an entity.
13521
13522 Set_Associated_Node (N, Empty);
13523 Set_Etype (N, Empty);
13524 end if;
13525
13526 if Nkind (Parent (N)) in N_Generic_Instantiation
13527 and then N = Name (Parent (N))
13528 then
13529 Save_Global_Defaults (Parent (N), Parent (N2));
13530 end if;
13531
13532 elsif Nkind (Parent (N)) = N_Selected_Component
13533 and then Nkind (Parent (N2)) = N_Expanded_Name
13534 then
13535 if Is_Global (Entity (Parent (N2))) then
13536 Change_Selected_Component_To_Expanded_Name (Parent (N));
13537 Set_Associated_Node (Parent (N), Parent (N2));
13538 Set_Global_Type (Parent (N), Parent (N2));
13539 Save_Entity_Descendants (N);
13540
13541 -- If this is a reference to the current generic entity, replace
13542 -- by the name of the generic homonym of the current package. This
13543 -- is because in an instantiation Par.P.Q will not resolve to the
13544 -- name of the instance, whose enclosing scope is not necessarily
13545 -- Par. We use the generic homonym rather that the name of the
13546 -- generic itself because it may be hidden by a local declaration.
13547
13548 elsif In_Open_Scopes (Entity (Parent (N2)))
13549 and then not
13550 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13551 then
13552 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13553 Rewrite (Parent (N),
13554 Make_Identifier (Sloc (N),
13555 Chars =>
13556 Chars (Generic_Homonym (Entity (Parent (N2))))));
13557 else
13558 Rewrite (Parent (N),
13559 Make_Identifier (Sloc (N),
13560 Chars => Chars (Selector_Name (Parent (N2)))));
13561 end if;
13562 end if;
13563
13564 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13565 and then Parent (N) = Name (Parent (Parent (N)))
13566 then
13567 Save_Global_Defaults
13568 (Parent (Parent (N)), Parent (Parent ((N2))));
13569 end if;
13570
13571 -- A selected component may denote a static constant that has been
13572 -- folded. If the static constant is global to the generic, capture
13573 -- its value. Otherwise the folding will happen in any instantiation.
13574
13575 elsif Nkind (Parent (N)) = N_Selected_Component
13576 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13577 then
13578 if Present (Entity (Original_Node (Parent (N2))))
13579 and then Is_Global (Entity (Original_Node (Parent (N2))))
13580 then
13581 Rewrite (Parent (N), New_Copy (Parent (N2)));
13582 Set_Analyzed (Parent (N), False);
13583
13584 else
13585 null;
13586 end if;
13587
13588 -- A selected component may be transformed into a parameterless
13589 -- function call. If the called entity is global, rewrite the node
13590 -- appropriately, i.e. as an extended name for the global entity.
13591
13592 elsif Nkind (Parent (N)) = N_Selected_Component
13593 and then Nkind (Parent (N2)) = N_Function_Call
13594 and then N = Selector_Name (Parent (N))
13595 then
13596 if No (Parameter_Associations (Parent (N2))) then
13597 if Is_Global (Entity (Name (Parent (N2)))) then
13598 Change_Selected_Component_To_Expanded_Name (Parent (N));
13599 Set_Associated_Node (Parent (N), Name (Parent (N2)));
13600 Set_Global_Type (Parent (N), Name (Parent (N2)));
13601 Save_Entity_Descendants (N);
13602
13603 else
13604 Set_Is_Prefixed_Call (Parent (N));
13605 Set_Associated_Node (N, Empty);
13606 Set_Etype (N, Empty);
13607 end if;
13608
13609 -- In Ada 2005, X.F may be a call to a primitive operation,
13610 -- rewritten as F (X). This rewriting will be done again in an
13611 -- instance, so keep the original node. Global entities will be
13612 -- captured as for other constructs. Indicate that this must
13613 -- resolve as a call, to prevent accidental overloading in the
13614 -- instance, if both a component and a primitive operation appear
13615 -- as candidates.
13616
13617 else
13618 Set_Is_Prefixed_Call (Parent (N));
13619 end if;
13620
13621 -- Entity is local. Reset in generic unit, so that node is resolved
13622 -- anew at the point of instantiation.
13623
13624 else
13625 Set_Associated_Node (N, Empty);
13626 Set_Etype (N, Empty);
13627 end if;
13628 end Reset_Entity;
13629
13630 -----------------------------
13631 -- Save_Entity_Descendants --
13632 -----------------------------
13633
13634 procedure Save_Entity_Descendants (N : Node_Id) is
13635 begin
13636 case Nkind (N) is
13637 when N_Binary_Op =>
13638 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
13639 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13640
13641 when N_Unary_Op =>
13642 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13643
13644 when N_Expanded_Name | N_Selected_Component =>
13645 Save_Global_Descendant (Union_Id (Prefix (N)));
13646 Save_Global_Descendant (Union_Id (Selector_Name (N)));
13647
13648 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
13649 null;
13650
13651 when others =>
13652 raise Program_Error;
13653 end case;
13654 end Save_Entity_Descendants;
13655
13656 --------------------------
13657 -- Save_Global_Defaults --
13658 --------------------------
13659
13660 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
13661 Loc : constant Source_Ptr := Sloc (N1);
13662 Assoc2 : constant List_Id := Generic_Associations (N2);
13663 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
13664 Assoc1 : List_Id;
13665 Act1 : Node_Id;
13666 Act2 : Node_Id;
13667 Def : Node_Id;
13668 Ndec : Node_Id;
13669 Subp : Entity_Id;
13670 Actual : Entity_Id;
13671
13672 begin
13673 Assoc1 := Generic_Associations (N1);
13674
13675 if Present (Assoc1) then
13676 Act1 := First (Assoc1);
13677 else
13678 Act1 := Empty;
13679 Set_Generic_Associations (N1, New_List);
13680 Assoc1 := Generic_Associations (N1);
13681 end if;
13682
13683 if Present (Assoc2) then
13684 Act2 := First (Assoc2);
13685 else
13686 return;
13687 end if;
13688
13689 while Present (Act1) and then Present (Act2) loop
13690 Next (Act1);
13691 Next (Act2);
13692 end loop;
13693
13694 -- Find the associations added for default subprograms
13695
13696 if Present (Act2) then
13697 while Nkind (Act2) /= N_Generic_Association
13698 or else No (Entity (Selector_Name (Act2)))
13699 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
13700 loop
13701 Next (Act2);
13702 end loop;
13703
13704 -- Add a similar association if the default is global. The
13705 -- renaming declaration for the actual has been analyzed, and
13706 -- its alias is the program it renames. Link the actual in the
13707 -- original generic tree with the node in the analyzed tree.
13708
13709 while Present (Act2) loop
13710 Subp := Entity (Selector_Name (Act2));
13711 Def := Explicit_Generic_Actual_Parameter (Act2);
13712
13713 -- Following test is defence against rubbish errors
13714
13715 if No (Alias (Subp)) then
13716 return;
13717 end if;
13718
13719 -- Retrieve the resolved actual from the renaming declaration
13720 -- created for the instantiated formal.
13721
13722 Actual := Entity (Name (Parent (Parent (Subp))));
13723 Set_Entity (Def, Actual);
13724 Set_Etype (Def, Etype (Actual));
13725
13726 if Is_Global (Actual) then
13727 Ndec :=
13728 Make_Generic_Association (Loc,
13729 Selector_Name => New_Occurrence_Of (Subp, Loc),
13730 Explicit_Generic_Actual_Parameter =>
13731 New_Occurrence_Of (Actual, Loc));
13732
13733 Set_Associated_Node
13734 (Explicit_Generic_Actual_Parameter (Ndec), Def);
13735
13736 Append (Ndec, Assoc1);
13737
13738 -- If there are other defaults, add a dummy association in case
13739 -- there are other defaulted formals with the same name.
13740
13741 elsif Present (Next (Act2)) then
13742 Ndec :=
13743 Make_Generic_Association (Loc,
13744 Selector_Name => New_Occurrence_Of (Subp, Loc),
13745 Explicit_Generic_Actual_Parameter => Empty);
13746
13747 Append (Ndec, Assoc1);
13748 end if;
13749
13750 Next (Act2);
13751 end loop;
13752 end if;
13753
13754 if Nkind (Name (N1)) = N_Identifier
13755 and then Is_Child_Unit (Gen_Id)
13756 and then Is_Global (Gen_Id)
13757 and then Is_Generic_Unit (Scope (Gen_Id))
13758 and then In_Open_Scopes (Scope (Gen_Id))
13759 then
13760 -- This is an instantiation of a child unit within a sibling, so
13761 -- that the generic parent is in scope. An eventual instance must
13762 -- occur within the scope of an instance of the parent. Make name
13763 -- in instance into an expanded name, to preserve the identifier
13764 -- of the parent, so it can be resolved subsequently.
13765
13766 Rewrite (Name (N2),
13767 Make_Expanded_Name (Loc,
13768 Chars => Chars (Gen_Id),
13769 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13770 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13771 Set_Entity (Name (N2), Gen_Id);
13772
13773 Rewrite (Name (N1),
13774 Make_Expanded_Name (Loc,
13775 Chars => Chars (Gen_Id),
13776 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13777 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13778
13779 Set_Associated_Node (Name (N1), Name (N2));
13780 Set_Associated_Node (Prefix (Name (N1)), Empty);
13781 Set_Associated_Node
13782 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
13783 Set_Etype (Name (N1), Etype (Gen_Id));
13784 end if;
13785
13786 end Save_Global_Defaults;
13787
13788 ----------------------------
13789 -- Save_Global_Descendant --
13790 ----------------------------
13791
13792 procedure Save_Global_Descendant (D : Union_Id) is
13793 N1 : Node_Id;
13794
13795 begin
13796 if D in Node_Range then
13797 if D = Union_Id (Empty) then
13798 null;
13799
13800 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
13801 Save_References (Node_Id (D));
13802 end if;
13803
13804 elsif D in List_Range then
13805 if D = Union_Id (No_List)
13806 or else Is_Empty_List (List_Id (D))
13807 then
13808 null;
13809
13810 else
13811 N1 := First (List_Id (D));
13812 while Present (N1) loop
13813 Save_References (N1);
13814 Next (N1);
13815 end loop;
13816 end if;
13817
13818 -- Element list or other non-node field, nothing to do
13819
13820 else
13821 null;
13822 end if;
13823 end Save_Global_Descendant;
13824
13825 ---------------------
13826 -- Save_References --
13827 ---------------------
13828
13829 -- This is the recursive procedure that does the work once the enclosing
13830 -- generic scope has been established. We have to treat specially a
13831 -- number of node rewritings that are required by semantic processing
13832 -- and which change the kind of nodes in the generic copy: typically
13833 -- constant-folding, replacing an operator node by a string literal, or
13834 -- a selected component by an expanded name. In each of those cases, the
13835 -- transformation is propagated to the generic unit.
13836
13837 procedure Save_References (N : Node_Id) is
13838 Loc : constant Source_Ptr := Sloc (N);
13839
13840 begin
13841 if N = Empty then
13842 null;
13843
13844 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
13845 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13846 Reset_Entity (N);
13847
13848 elsif Nkind (N) = N_Operator_Symbol
13849 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
13850 then
13851 Change_Operator_Symbol_To_String_Literal (N);
13852 end if;
13853
13854 elsif Nkind (N) in N_Op then
13855 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13856 if Nkind (N) = N_Op_Concat then
13857 Set_Is_Component_Left_Opnd (N,
13858 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13859
13860 Set_Is_Component_Right_Opnd (N,
13861 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13862 end if;
13863
13864 Reset_Entity (N);
13865
13866 else
13867 -- Node may be transformed into call to a user-defined operator
13868
13869 N2 := Get_Associated_Node (N);
13870
13871 if Nkind (N2) = N_Function_Call then
13872 E := Entity (Name (N2));
13873
13874 if Present (E)
13875 and then Is_Global (E)
13876 then
13877 Set_Etype (N, Etype (N2));
13878 else
13879 Set_Associated_Node (N, Empty);
13880 Set_Etype (N, Empty);
13881 end if;
13882
13883 elsif Nkind_In (N2, N_Integer_Literal,
13884 N_Real_Literal,
13885 N_String_Literal)
13886 then
13887 if Present (Original_Node (N2))
13888 and then Nkind (Original_Node (N2)) = Nkind (N)
13889 then
13890
13891 -- Operation was constant-folded. Whenever possible,
13892 -- recover semantic information from unfolded node,
13893 -- for ASIS use.
13894
13895 Set_Associated_Node (N, Original_Node (N2));
13896
13897 if Nkind (N) = N_Op_Concat then
13898 Set_Is_Component_Left_Opnd (N,
13899 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13900 Set_Is_Component_Right_Opnd (N,
13901 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13902 end if;
13903
13904 Reset_Entity (N);
13905
13906 else
13907 -- If original node is already modified, propagate
13908 -- constant-folding to template.
13909
13910 Rewrite (N, New_Copy (N2));
13911 Set_Analyzed (N, False);
13912 end if;
13913
13914 elsif Nkind (N2) = N_Identifier
13915 and then Ekind (Entity (N2)) = E_Enumeration_Literal
13916 then
13917 -- Same if call was folded into a literal, but in this case
13918 -- retain the entity to avoid spurious ambiguities if it is
13919 -- overloaded at the point of instantiation or inlining.
13920
13921 Rewrite (N, New_Copy (N2));
13922 Set_Analyzed (N, False);
13923 end if;
13924 end if;
13925
13926 -- Complete operands check if node has not been constant-folded
13927
13928 if Nkind (N) in N_Op then
13929 Save_Entity_Descendants (N);
13930 end if;
13931
13932 elsif Nkind (N) = N_Identifier then
13933 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13934
13935 -- If this is a discriminant reference, always save it. It is
13936 -- used in the instance to find the corresponding discriminant
13937 -- positionally rather than by name.
13938
13939 Set_Original_Discriminant
13940 (N, Original_Discriminant (Get_Associated_Node (N)));
13941 Reset_Entity (N);
13942
13943 else
13944 N2 := Get_Associated_Node (N);
13945
13946 if Nkind (N2) = N_Function_Call then
13947 E := Entity (Name (N2));
13948
13949 -- Name resolves to a call to parameterless function. If
13950 -- original entity is global, mark node as resolved.
13951
13952 if Present (E)
13953 and then Is_Global (E)
13954 then
13955 Set_Etype (N, Etype (N2));
13956 else
13957 Set_Associated_Node (N, Empty);
13958 Set_Etype (N, Empty);
13959 end if;
13960
13961 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
13962 and then Is_Entity_Name (Original_Node (N2))
13963 then
13964 -- Name resolves to named number that is constant-folded,
13965 -- We must preserve the original name for ASIS use, and
13966 -- undo the constant-folding, which will be repeated in
13967 -- each instance.
13968
13969 Set_Associated_Node (N, Original_Node (N2));
13970 Reset_Entity (N);
13971
13972 elsif Nkind (N2) = N_String_Literal then
13973
13974 -- Name resolves to string literal. Perform the same
13975 -- replacement in generic.
13976
13977 Rewrite (N, New_Copy (N2));
13978
13979 elsif Nkind (N2) = N_Explicit_Dereference then
13980
13981 -- An identifier is rewritten as a dereference if it is the
13982 -- prefix in an implicit dereference (call or attribute).
13983 -- The analysis of an instantiation will expand the node
13984 -- again, so we preserve the original tree but link it to
13985 -- the resolved entity in case it is global.
13986
13987 if Is_Entity_Name (Prefix (N2))
13988 and then Present (Entity (Prefix (N2)))
13989 and then Is_Global (Entity (Prefix (N2)))
13990 then
13991 Set_Associated_Node (N, Prefix (N2));
13992
13993 elsif Nkind (Prefix (N2)) = N_Function_Call
13994 and then Is_Global (Entity (Name (Prefix (N2))))
13995 then
13996 Rewrite (N,
13997 Make_Explicit_Dereference (Loc,
13998 Prefix => Make_Function_Call (Loc,
13999 Name =>
14000 New_Occurrence_Of (Entity (Name (Prefix (N2))),
14001 Loc))));
14002
14003 else
14004 Set_Associated_Node (N, Empty);
14005 Set_Etype (N, Empty);
14006 end if;
14007
14008 -- The subtype mark of a nominally unconstrained object is
14009 -- rewritten as a subtype indication using the bounds of the
14010 -- expression. Recover the original subtype mark.
14011
14012 elsif Nkind (N2) = N_Subtype_Indication
14013 and then Is_Entity_Name (Original_Node (N2))
14014 then
14015 Set_Associated_Node (N, Original_Node (N2));
14016 Reset_Entity (N);
14017
14018 else
14019 null;
14020 end if;
14021 end if;
14022
14023 elsif Nkind (N) in N_Entity then
14024 null;
14025
14026 else
14027 declare
14028 Qual : Node_Id := Empty;
14029 Typ : Entity_Id := Empty;
14030 Nam : Node_Id;
14031
14032 use Atree.Unchecked_Access;
14033 -- This code section is part of implementing an untyped tree
14034 -- traversal, so it needs direct access to node fields.
14035
14036 begin
14037 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14038 N2 := Get_Associated_Node (N);
14039
14040 if No (N2) then
14041 Typ := Empty;
14042 else
14043 Typ := Etype (N2);
14044
14045 -- In an instance within a generic, use the name of the
14046 -- actual and not the original generic parameter. If the
14047 -- actual is global in the current generic it must be
14048 -- preserved for its instantiation.
14049
14050 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14051 and then
14052 Present (Generic_Parent_Type (Parent (Typ)))
14053 then
14054 Typ := Base_Type (Typ);
14055 Set_Etype (N2, Typ);
14056 end if;
14057 end if;
14058
14059 if No (N2)
14060 or else No (Typ)
14061 or else not Is_Global (Typ)
14062 then
14063 Set_Associated_Node (N, Empty);
14064
14065 -- If the aggregate is an actual in a call, it has been
14066 -- resolved in the current context, to some local type.
14067 -- The enclosing call may have been disambiguated by the
14068 -- aggregate, and this disambiguation might fail at
14069 -- instantiation time because the type to which the
14070 -- aggregate did resolve is not preserved. In order to
14071 -- preserve some of this information, we wrap the
14072 -- aggregate in a qualified expression, using the id of
14073 -- its type. For further disambiguation we qualify the
14074 -- type name with its scope (if visible) because both
14075 -- id's will have corresponding entities in an instance.
14076 -- This resolves most of the problems with missing type
14077 -- information on aggregates in instances.
14078
14079 if Nkind (N2) = Nkind (N)
14080 and then Nkind (Parent (N2)) in N_Subprogram_Call
14081 and then Comes_From_Source (Typ)
14082 then
14083 if Is_Immediately_Visible (Scope (Typ)) then
14084 Nam := Make_Selected_Component (Loc,
14085 Prefix =>
14086 Make_Identifier (Loc, Chars (Scope (Typ))),
14087 Selector_Name =>
14088 Make_Identifier (Loc, Chars (Typ)));
14089 else
14090 Nam := Make_Identifier (Loc, Chars (Typ));
14091 end if;
14092
14093 Qual :=
14094 Make_Qualified_Expression (Loc,
14095 Subtype_Mark => Nam,
14096 Expression => Relocate_Node (N));
14097 end if;
14098 end if;
14099
14100 Save_Global_Descendant (Field1 (N));
14101 Save_Global_Descendant (Field2 (N));
14102 Save_Global_Descendant (Field3 (N));
14103 Save_Global_Descendant (Field5 (N));
14104
14105 if Present (Qual) then
14106 Rewrite (N, Qual);
14107 end if;
14108
14109 -- All other cases than aggregates
14110
14111 else
14112 Save_Global_Descendant (Field1 (N));
14113 Save_Global_Descendant (Field2 (N));
14114 Save_Global_Descendant (Field3 (N));
14115 Save_Global_Descendant (Field4 (N));
14116 Save_Global_Descendant (Field5 (N));
14117 end if;
14118 end;
14119 end if;
14120
14121 -- If a node has aspects, references within their expressions must
14122 -- be saved separately, given they are not directly in the tree.
14123
14124 if Has_Aspects (N) then
14125 declare
14126 Aspect : Node_Id;
14127
14128 begin
14129 Aspect := First (Aspect_Specifications (N));
14130 while Present (Aspect) loop
14131 if Present (Expression (Aspect)) then
14132 Save_Global_References (Expression (Aspect));
14133 end if;
14134
14135 Next (Aspect);
14136 end loop;
14137 end;
14138 end if;
14139 end Save_References;
14140
14141 -- Start of processing for Save_Global_References
14142
14143 begin
14144 Gen_Scope := Current_Scope;
14145
14146 -- If the generic unit is a child unit, references to entities in the
14147 -- parent are treated as local, because they will be resolved anew in
14148 -- the context of the instance of the parent.
14149
14150 while Is_Child_Unit (Gen_Scope)
14151 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14152 loop
14153 Gen_Scope := Scope (Gen_Scope);
14154 end loop;
14155
14156 Save_References (N);
14157 end Save_Global_References;
14158
14159 --------------------------------------
14160 -- Set_Copied_Sloc_For_Inlined_Body --
14161 --------------------------------------
14162
14163 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14164 begin
14165 Create_Instantiation_Source (N, E, True, S_Adjustment);
14166 end Set_Copied_Sloc_For_Inlined_Body;
14167
14168 ---------------------
14169 -- Set_Instance_Of --
14170 ---------------------
14171
14172 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14173 begin
14174 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14175 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14176 Generic_Renamings.Increment_Last;
14177 end Set_Instance_Of;
14178
14179 --------------------
14180 -- Set_Next_Assoc --
14181 --------------------
14182
14183 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14184 begin
14185 Generic_Renamings.Table (E).Next_In_HTable := Next;
14186 end Set_Next_Assoc;
14187
14188 -------------------
14189 -- Start_Generic --
14190 -------------------
14191
14192 procedure Start_Generic is
14193 begin
14194 -- ??? More things could be factored out in this routine.
14195 -- Should probably be done at a later stage.
14196
14197 Generic_Flags.Append (Inside_A_Generic);
14198 Inside_A_Generic := True;
14199
14200 Expander_Mode_Save_And_Set (False);
14201 end Start_Generic;
14202
14203 ----------------------
14204 -- Set_Instance_Env --
14205 ----------------------
14206
14207 procedure Set_Instance_Env
14208 (Gen_Unit : Entity_Id;
14209 Act_Unit : Entity_Id)
14210 is
14211 Assertion_Status : constant Boolean := Assertions_Enabled;
14212 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14213 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14214
14215 begin
14216 -- Regardless of the current mode, predefined units are analyzed in the
14217 -- most current Ada mode, and earlier version Ada checks do not apply
14218 -- to predefined units. Nothing needs to be done for non-internal units.
14219 -- These are always analyzed in the current mode.
14220
14221 if Is_Internal_File_Name
14222 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14223 Renamings_Included => True)
14224 then
14225 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14226
14227 -- In Ada2012 we may want to enable assertions in an instance of a
14228 -- predefined unit, in which case we need to preserve the current
14229 -- setting for the Assertions_Enabled flag. This will become more
14230 -- critical when pre/postconditions are added to predefined units,
14231 -- as is already the case for some numeric libraries.
14232
14233 if Ada_Version >= Ada_2012 then
14234 Assertions_Enabled := Assertion_Status;
14235 end if;
14236
14237 -- SPARK_Mode for an instance is the one applicable at the point of
14238 -- instantiation.
14239
14240 SPARK_Mode := Save_SPARK_Mode;
14241 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14242 end if;
14243
14244 Current_Instantiated_Parent :=
14245 (Gen_Id => Gen_Unit,
14246 Act_Id => Act_Unit,
14247 Next_In_HTable => Assoc_Null);
14248 end Set_Instance_Env;
14249
14250 -----------------
14251 -- Switch_View --
14252 -----------------
14253
14254 procedure Switch_View (T : Entity_Id) is
14255 BT : constant Entity_Id := Base_Type (T);
14256 Priv_Elmt : Elmt_Id := No_Elmt;
14257 Priv_Sub : Entity_Id;
14258
14259 begin
14260 -- T may be private but its base type may have been exchanged through
14261 -- some other occurrence, in which case there is nothing to switch
14262 -- besides T itself. Note that a private dependent subtype of a private
14263 -- type might not have been switched even if the base type has been,
14264 -- because of the last branch of Check_Private_View (see comment there).
14265
14266 if not Is_Private_Type (BT) then
14267 Prepend_Elmt (Full_View (T), Exchanged_Views);
14268 Exchange_Declarations (T);
14269 return;
14270 end if;
14271
14272 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14273
14274 if Present (Full_View (BT)) then
14275 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14276 Exchange_Declarations (BT);
14277 end if;
14278
14279 while Present (Priv_Elmt) loop
14280 Priv_Sub := (Node (Priv_Elmt));
14281
14282 -- We avoid flipping the subtype if the Etype of its full view is
14283 -- private because this would result in a malformed subtype. This
14284 -- occurs when the Etype of the subtype full view is the full view of
14285 -- the base type (and since the base types were just switched, the
14286 -- subtype is pointing to the wrong view). This is currently the case
14287 -- for tagged record types, access types (maybe more?) and needs to
14288 -- be resolved. ???
14289
14290 if Present (Full_View (Priv_Sub))
14291 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14292 then
14293 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14294 Exchange_Declarations (Priv_Sub);
14295 end if;
14296
14297 Next_Elmt (Priv_Elmt);
14298 end loop;
14299 end Switch_View;
14300
14301 -----------------
14302 -- True_Parent --
14303 -----------------
14304
14305 function True_Parent (N : Node_Id) return Node_Id is
14306 begin
14307 if Nkind (Parent (N)) = N_Subunit then
14308 return Parent (Corresponding_Stub (Parent (N)));
14309 else
14310 return Parent (N);
14311 end if;
14312 end True_Parent;
14313
14314 -----------------------------
14315 -- Valid_Default_Attribute --
14316 -----------------------------
14317
14318 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
14319 Attr_Id : constant Attribute_Id :=
14320 Get_Attribute_Id (Attribute_Name (Def));
14321 T : constant Entity_Id := Entity (Prefix (Def));
14322 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
14323 F : Entity_Id;
14324 Num_F : Int;
14325 OK : Boolean;
14326
14327 begin
14328 if No (T)
14329 or else T = Any_Id
14330 then
14331 return;
14332 end if;
14333
14334 Num_F := 0;
14335 F := First_Formal (Nam);
14336 while Present (F) loop
14337 Num_F := Num_F + 1;
14338 Next_Formal (F);
14339 end loop;
14340
14341 case Attr_Id is
14342 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
14343 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
14344 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
14345 Attribute_Unbiased_Rounding =>
14346 OK := Is_Fun
14347 and then Num_F = 1
14348 and then Is_Floating_Point_Type (T);
14349
14350 when Attribute_Image | Attribute_Pred | Attribute_Succ |
14351 Attribute_Value | Attribute_Wide_Image |
14352 Attribute_Wide_Value =>
14353 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
14354
14355 when Attribute_Max | Attribute_Min =>
14356 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
14357
14358 when Attribute_Input =>
14359 OK := (Is_Fun and then Num_F = 1);
14360
14361 when Attribute_Output | Attribute_Read | Attribute_Write =>
14362 OK := (not Is_Fun and then Num_F = 2);
14363
14364 when others =>
14365 OK := False;
14366 end case;
14367
14368 if not OK then
14369 Error_Msg_N ("attribute reference has wrong profile for subprogram",
14370 Def);
14371 end if;
14372 end Valid_Default_Attribute;
14373
14374 end Sem_Ch12;