sem_aggr.adb, [...]: Minor reformatting.
[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-2013, 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 Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
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 parametrization --
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 parametrization, 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 parametrization of a formal
955 -- package. As usual an other association must be last in the list.
956
957 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
958 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
959 -- cannot have a named association for it. AI05-0025 extends this rule
960 -- to formals of formal packages by AI05-0025, and it also applies to
961 -- box-initialized formals.
962
963 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
964 -- Determine whether the parameter types and the return type of Subp
965 -- are fully defined at the point of instantiation.
966
967 function Matching_Actual
968 (F : Entity_Id;
969 A_F : Entity_Id) return Node_Id;
970 -- Find actual that corresponds to a given a formal parameter. If the
971 -- actuals are positional, return the next one, if any. If the actuals
972 -- are named, scan the parameter associations to find the right one.
973 -- A_F is the corresponding entity in the analyzed generic,which is
974 -- placed on the selector name for ASIS use.
975 --
976 -- In Ada 2005, a named association may be given with a box, in which
977 -- case Matching_Actual sets Found_Assoc to the generic association,
978 -- but return Empty for the actual itself. In this case the code below
979 -- creates a corresponding declaration for the formal.
980
981 function Partial_Parametrization return Boolean;
982 -- Ada 2005: if no match is found for a given formal, check if the
983 -- association for it includes a box, or whether the associations
984 -- include an Others clause.
985
986 procedure Process_Default (F : Entity_Id);
987 -- Add a copy of the declaration of generic formal F to the list of
988 -- associations, and add an explicit box association for F if there
989 -- is none yet, and the default comes from an Others_Choice.
990
991 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
992 -- Determine whether Subp renames one of the subprograms defined in the
993 -- generated package Standard.
994
995 procedure Set_Analyzed_Formal;
996 -- Find the node in the generic copy that corresponds to a given formal.
997 -- The semantic information on this node is used to perform legality
998 -- checks on the actuals. Because semantic analysis can introduce some
999 -- anonymous entities or modify the declaration node itself, the
1000 -- correspondence between the two lists is not one-one. In addition to
1001 -- anonymous types, the presence a formal equality will introduce an
1002 -- implicit declaration for the corresponding inequality.
1003
1004 ----------------------------------------
1005 -- Check_Overloaded_Formal_Subprogram --
1006 ----------------------------------------
1007
1008 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1009 Temp_Formal : Entity_Id;
1010
1011 begin
1012 Temp_Formal := First (Formals);
1013 while Present (Temp_Formal) loop
1014 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1015 and then Temp_Formal /= Formal
1016 and then
1017 Chars (Defining_Unit_Name (Specification (Formal))) =
1018 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1019 then
1020 if Present (Found_Assoc) then
1021 Error_Msg_N
1022 ("named association not allowed for overloaded formal",
1023 Found_Assoc);
1024
1025 else
1026 Error_Msg_N
1027 ("named association not allowed for overloaded formal",
1028 Others_Choice);
1029 end if;
1030
1031 Abandon_Instantiation (Instantiation_Node);
1032 end if;
1033
1034 Next (Temp_Formal);
1035 end loop;
1036 end Check_Overloaded_Formal_Subprogram;
1037
1038 -------------------------------
1039 -- Has_Fully_Defined_Profile --
1040 -------------------------------
1041
1042 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1043 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1044 -- Determine whethet type Typ is fully defined
1045
1046 ---------------------------
1047 -- Is_Fully_Defined_Type --
1048 ---------------------------
1049
1050 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1051 begin
1052 -- A private type without a full view is not fully defined
1053
1054 if Is_Private_Type (Typ)
1055 and then No (Full_View (Typ))
1056 then
1057 return False;
1058
1059 -- An incomplete type is never fully defined
1060
1061 elsif Is_Incomplete_Type (Typ) then
1062 return False;
1063
1064 -- All other types are fully defined
1065
1066 else
1067 return True;
1068 end if;
1069 end Is_Fully_Defined_Type;
1070
1071 -- Local declarations
1072
1073 Param : Entity_Id;
1074
1075 -- Start of processing for Has_Fully_Defined_Profile
1076
1077 begin
1078 -- Check the parameters
1079
1080 Param := First_Formal (Subp);
1081 while Present (Param) loop
1082 if not Is_Fully_Defined_Type (Etype (Param)) then
1083 return False;
1084 end if;
1085
1086 Next_Formal (Param);
1087 end loop;
1088
1089 -- Check the return type
1090
1091 return Is_Fully_Defined_Type (Etype (Subp));
1092 end Has_Fully_Defined_Profile;
1093
1094 ---------------------
1095 -- Matching_Actual --
1096 ---------------------
1097
1098 function Matching_Actual
1099 (F : Entity_Id;
1100 A_F : Entity_Id) return Node_Id
1101 is
1102 Prev : Node_Id;
1103 Act : Node_Id;
1104
1105 begin
1106 Is_Named_Assoc := False;
1107
1108 -- End of list of purely positional parameters
1109
1110 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1111 Found_Assoc := Empty;
1112 Act := Empty;
1113
1114 -- Case of positional parameter corresponding to current formal
1115
1116 elsif No (Selector_Name (Actual)) then
1117 Found_Assoc := Actual;
1118 Act := Explicit_Generic_Actual_Parameter (Actual);
1119 Num_Matched := Num_Matched + 1;
1120 Next (Actual);
1121
1122 -- Otherwise scan list of named actuals to find the one with the
1123 -- desired name. All remaining actuals have explicit names.
1124
1125 else
1126 Is_Named_Assoc := True;
1127 Found_Assoc := Empty;
1128 Act := Empty;
1129 Prev := Empty;
1130
1131 while Present (Actual) loop
1132 if Chars (Selector_Name (Actual)) = Chars (F) then
1133 Set_Entity (Selector_Name (Actual), A_F);
1134 Set_Etype (Selector_Name (Actual), Etype (A_F));
1135 Generate_Reference (A_F, Selector_Name (Actual));
1136 Found_Assoc := Actual;
1137 Act := Explicit_Generic_Actual_Parameter (Actual);
1138 Num_Matched := Num_Matched + 1;
1139 exit;
1140 end if;
1141
1142 Prev := Actual;
1143 Next (Actual);
1144 end loop;
1145
1146 -- Reset for subsequent searches. In most cases the named
1147 -- associations are in order. If they are not, we reorder them
1148 -- to avoid scanning twice the same actual. This is not just a
1149 -- question of efficiency: there may be multiple defaults with
1150 -- boxes that have the same name. In a nested instantiation we
1151 -- insert actuals for those defaults, and cannot rely on their
1152 -- names to disambiguate them.
1153
1154 if Actual = First_Named then
1155 Next (First_Named);
1156
1157 elsif Present (Actual) then
1158 Insert_Before (First_Named, Remove_Next (Prev));
1159 end if;
1160
1161 Actual := First_Named;
1162 end if;
1163
1164 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1165 Set_Used_As_Generic_Actual (Entity (Act));
1166 end if;
1167
1168 return Act;
1169 end Matching_Actual;
1170
1171 -----------------------------
1172 -- Partial_Parametrization --
1173 -----------------------------
1174
1175 function Partial_Parametrization return Boolean is
1176 begin
1177 return Others_Present
1178 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1179 end Partial_Parametrization;
1180
1181 ---------------------
1182 -- Process_Default --
1183 ---------------------
1184
1185 procedure Process_Default (F : Entity_Id) is
1186 Loc : constant Source_Ptr := Sloc (I_Node);
1187 F_Id : constant Entity_Id := Defining_Entity (F);
1188 Decl : Node_Id;
1189 Default : Node_Id;
1190 Id : Entity_Id;
1191
1192 begin
1193 -- Append copy of formal declaration to associations, and create new
1194 -- defining identifier for it.
1195
1196 Decl := New_Copy_Tree (F);
1197 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1198
1199 if Nkind (F) in N_Formal_Subprogram_Declaration then
1200 Set_Defining_Unit_Name (Specification (Decl), Id);
1201
1202 else
1203 Set_Defining_Identifier (Decl, Id);
1204 end if;
1205
1206 Append (Decl, Assoc);
1207
1208 if No (Found_Assoc) then
1209 Default :=
1210 Make_Generic_Association (Loc,
1211 Selector_Name => New_Occurrence_Of (Id, Loc),
1212 Explicit_Generic_Actual_Parameter => Empty);
1213 Set_Box_Present (Default);
1214 Append (Default, Default_Formals);
1215 end if;
1216 end Process_Default;
1217
1218 ---------------------------------
1219 -- Renames_Standard_Subprogram --
1220 ---------------------------------
1221
1222 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1223 Id : Entity_Id;
1224
1225 begin
1226 Id := Alias (Subp);
1227 while Present (Id) loop
1228 if Scope (Id) = Standard_Standard then
1229 return True;
1230 end if;
1231
1232 Id := Alias (Id);
1233 end loop;
1234
1235 return False;
1236 end Renames_Standard_Subprogram;
1237
1238 -------------------------
1239 -- Set_Analyzed_Formal --
1240 -------------------------
1241
1242 procedure Set_Analyzed_Formal is
1243 Kind : Node_Kind;
1244
1245 begin
1246 while Present (Analyzed_Formal) loop
1247 Kind := Nkind (Analyzed_Formal);
1248
1249 case Nkind (Formal) is
1250
1251 when N_Formal_Subprogram_Declaration =>
1252 exit when Kind in N_Formal_Subprogram_Declaration
1253 and then
1254 Chars
1255 (Defining_Unit_Name (Specification (Formal))) =
1256 Chars
1257 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1258
1259 when N_Formal_Package_Declaration =>
1260 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1261 N_Generic_Package_Declaration,
1262 N_Package_Declaration);
1263
1264 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1265
1266 when others =>
1267
1268 -- Skip freeze nodes, and nodes inserted to replace
1269 -- unrecognized pragmas.
1270
1271 exit when
1272 Kind not in N_Formal_Subprogram_Declaration
1273 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1274 N_Freeze_Entity,
1275 N_Null_Statement,
1276 N_Itype_Reference)
1277 and then Chars (Defining_Identifier (Formal)) =
1278 Chars (Defining_Identifier (Analyzed_Formal));
1279 end case;
1280
1281 Next (Analyzed_Formal);
1282 end loop;
1283 end Set_Analyzed_Formal;
1284
1285 -- Start of processing for Analyze_Associations
1286
1287 begin
1288 Actuals := Generic_Associations (I_Node);
1289
1290 if Present (Actuals) then
1291
1292 -- Check for an Others choice, indicating a partial parametrization
1293 -- for a formal package.
1294
1295 Actual := First (Actuals);
1296 while Present (Actual) loop
1297 if Nkind (Actual) = N_Others_Choice then
1298 Others_Present := True;
1299 Others_Choice := Actual;
1300
1301 if Present (Next (Actual)) then
1302 Error_Msg_N ("others must be last association", Actual);
1303 end if;
1304
1305 -- This subprogram is used both for formal packages and for
1306 -- instantiations. For the latter, associations must all be
1307 -- explicit.
1308
1309 if Nkind (I_Node) /= N_Formal_Package_Declaration
1310 and then Comes_From_Source (I_Node)
1311 then
1312 Error_Msg_N
1313 ("others association not allowed in an instance",
1314 Actual);
1315 end if;
1316
1317 -- In any case, nothing to do after the others association
1318
1319 exit;
1320
1321 elsif Box_Present (Actual)
1322 and then Comes_From_Source (I_Node)
1323 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1324 then
1325 Error_Msg_N
1326 ("box association not allowed in an instance", Actual);
1327 end if;
1328
1329 Next (Actual);
1330 end loop;
1331
1332 -- If named associations are present, save first named association
1333 -- (it may of course be Empty) to facilitate subsequent name search.
1334
1335 First_Named := First (Actuals);
1336 while Present (First_Named)
1337 and then Nkind (First_Named) /= N_Others_Choice
1338 and then No (Selector_Name (First_Named))
1339 loop
1340 Num_Actuals := Num_Actuals + 1;
1341 Next (First_Named);
1342 end loop;
1343 end if;
1344
1345 Named := First_Named;
1346 while Present (Named) loop
1347 if Nkind (Named) /= N_Others_Choice
1348 and then No (Selector_Name (Named))
1349 then
1350 Error_Msg_N ("invalid positional actual after named one", Named);
1351 Abandon_Instantiation (Named);
1352 end if;
1353
1354 -- A named association may lack an actual parameter, if it was
1355 -- introduced for a default subprogram that turns out to be local
1356 -- to the outer instantiation.
1357
1358 if Nkind (Named) /= N_Others_Choice
1359 and then Present (Explicit_Generic_Actual_Parameter (Named))
1360 then
1361 Num_Actuals := Num_Actuals + 1;
1362 end if;
1363
1364 Next (Named);
1365 end loop;
1366
1367 if Present (Formals) then
1368 Formal := First_Non_Pragma (Formals);
1369 Analyzed_Formal := First_Non_Pragma (F_Copy);
1370
1371 if Present (Actuals) then
1372 Actual := First (Actuals);
1373
1374 -- All formals should have default values
1375
1376 else
1377 Actual := Empty;
1378 end if;
1379
1380 while Present (Formal) loop
1381 Set_Analyzed_Formal;
1382 Saved_Formal := Next_Non_Pragma (Formal);
1383
1384 case Nkind (Formal) is
1385 when N_Formal_Object_Declaration =>
1386 Match :=
1387 Matching_Actual (
1388 Defining_Identifier (Formal),
1389 Defining_Identifier (Analyzed_Formal));
1390
1391 if No (Match) and then Partial_Parametrization then
1392 Process_Default (Formal);
1393 else
1394 Append_List
1395 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1396 Assoc);
1397 end if;
1398
1399 when N_Formal_Type_Declaration =>
1400 Match :=
1401 Matching_Actual (
1402 Defining_Identifier (Formal),
1403 Defining_Identifier (Analyzed_Formal));
1404
1405 if No (Match) then
1406 if Partial_Parametrization then
1407 Process_Default (Formal);
1408
1409 else
1410 Error_Msg_Sloc := Sloc (Gen_Unit);
1411 Error_Msg_NE
1412 ("missing actual&",
1413 Instantiation_Node,
1414 Defining_Identifier (Formal));
1415 Error_Msg_NE ("\in instantiation of & declared#",
1416 Instantiation_Node, Gen_Unit);
1417 Abandon_Instantiation (Instantiation_Node);
1418 end if;
1419
1420 else
1421 Analyze (Match);
1422 Append_List
1423 (Instantiate_Type
1424 (Formal, Match, Analyzed_Formal, Assoc),
1425 Assoc);
1426
1427 -- An instantiation is a freeze point for the actuals,
1428 -- unless this is a rewritten formal package, or the
1429 -- formal is an Ada 2012 formal incomplete type.
1430
1431 if Nkind (I_Node) = N_Formal_Package_Declaration
1432 or else
1433 (Ada_Version >= Ada_2012
1434 and then
1435 Ekind (Defining_Identifier (Analyzed_Formal)) =
1436 E_Incomplete_Type)
1437 then
1438 null;
1439
1440 else
1441 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1442 end if;
1443 end if;
1444
1445 -- A remote access-to-class-wide type is not a legal actual
1446 -- for a generic formal of an access type (E.2.2(17/2)).
1447 -- In GNAT an exception to this rule is introduced when
1448 -- the formal is marked as remote using implementation
1449 -- defined aspect/pragma Remote_Access_Type. In that case
1450 -- the actual must be remote as well.
1451
1452 -- If the current instantiation is the construction of a
1453 -- local copy for a formal package the actuals may be
1454 -- defaulted, and there is no matching actual to check.
1455
1456 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1457 and then
1458 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1459 N_Access_To_Object_Definition
1460 and then Present (Match)
1461 then
1462 declare
1463 Formal_Ent : constant Entity_Id :=
1464 Defining_Identifier (Analyzed_Formal);
1465 begin
1466 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1467 = Is_Remote_Types (Formal_Ent)
1468 then
1469 -- Remoteness of formal and actual match
1470
1471 null;
1472
1473 elsif Is_Remote_Types (Formal_Ent) then
1474
1475 -- Remote formal, non-remote actual
1476
1477 Error_Msg_NE
1478 ("actual for& must be remote", Match, Formal_Ent);
1479
1480 else
1481 -- Non-remote formal, remote actual
1482
1483 Error_Msg_NE
1484 ("actual for& may not be remote",
1485 Match, Formal_Ent);
1486 end if;
1487 end;
1488 end if;
1489
1490 when N_Formal_Subprogram_Declaration =>
1491 Match :=
1492 Matching_Actual
1493 (Defining_Unit_Name (Specification (Formal)),
1494 Defining_Unit_Name (Specification (Analyzed_Formal)));
1495
1496 -- If the formal subprogram has the same name as another
1497 -- formal subprogram of the generic, then a named
1498 -- association is illegal (12.3(9)). Exclude named
1499 -- associations that are generated for a nested instance.
1500
1501 if Present (Match)
1502 and then Is_Named_Assoc
1503 and then Comes_From_Source (Found_Assoc)
1504 then
1505 Check_Overloaded_Formal_Subprogram (Formal);
1506 end if;
1507
1508 -- If there is no corresponding actual, this may be case of
1509 -- partial parametrization, or else the formal has a default
1510 -- or a box.
1511
1512 if No (Match) and then Partial_Parametrization then
1513 Process_Default (Formal);
1514
1515 if Nkind (I_Node) = N_Formal_Package_Declaration then
1516 Check_Overloaded_Formal_Subprogram (Formal);
1517 end if;
1518
1519 else
1520 Append_To (Assoc,
1521 Instantiate_Formal_Subprogram
1522 (Formal, Match, Analyzed_Formal));
1523
1524 -- An instantiation is a freeze point for the actuals,
1525 -- unless this is a rewritten formal package.
1526
1527 if Nkind (I_Node) /= N_Formal_Package_Declaration
1528 and then Nkind (Match) = N_Identifier
1529 and then Is_Subprogram (Entity (Match))
1530
1531 -- The actual subprogram may rename a routine defined
1532 -- in Standard. Avoid freezing such renamings because
1533 -- subprograms coming from Standard cannot be frozen.
1534
1535 and then
1536 not Renames_Standard_Subprogram (Entity (Match))
1537
1538 -- If the actual subprogram comes from a different
1539 -- unit, it is already frozen, either by a body in
1540 -- that unit or by the end of the declarative part
1541 -- of the unit. This check avoids the freezing of
1542 -- subprograms defined in Standard which are used
1543 -- as generic actuals.
1544
1545 and then In_Same_Code_Unit (Entity (Match), I_Node)
1546 and then Has_Fully_Defined_Profile (Entity (Match))
1547 then
1548 -- Mark the subprogram as having a delayed freeze
1549 -- since this may be an out-of-order action.
1550
1551 Set_Has_Delayed_Freeze (Entity (Match));
1552 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1553 end if;
1554 end if;
1555
1556 -- If this is a nested generic, preserve default for later
1557 -- instantiations.
1558
1559 if No (Match)
1560 and then Box_Present (Formal)
1561 then
1562 Append_Elmt
1563 (Defining_Unit_Name (Specification (Last (Assoc))),
1564 Default_Actuals);
1565 end if;
1566
1567 when N_Formal_Package_Declaration =>
1568 Match :=
1569 Matching_Actual (
1570 Defining_Identifier (Formal),
1571 Defining_Identifier (Original_Node (Analyzed_Formal)));
1572
1573 if No (Match) then
1574 if Partial_Parametrization then
1575 Process_Default (Formal);
1576
1577 else
1578 Error_Msg_Sloc := Sloc (Gen_Unit);
1579 Error_Msg_NE
1580 ("missing actual&",
1581 Instantiation_Node, Defining_Identifier (Formal));
1582 Error_Msg_NE ("\in instantiation of & declared#",
1583 Instantiation_Node, Gen_Unit);
1584
1585 Abandon_Instantiation (Instantiation_Node);
1586 end if;
1587
1588 else
1589 Analyze (Match);
1590 Append_List
1591 (Instantiate_Formal_Package
1592 (Formal, Match, Analyzed_Formal),
1593 Assoc);
1594 end if;
1595
1596 -- For use type and use package appearing in the generic part,
1597 -- we have already copied them, so we can just move them where
1598 -- they belong (we mustn't recopy them since this would mess up
1599 -- the Sloc values).
1600
1601 when N_Use_Package_Clause |
1602 N_Use_Type_Clause =>
1603 if Nkind (Original_Node (I_Node)) =
1604 N_Formal_Package_Declaration
1605 then
1606 Append (New_Copy_Tree (Formal), Assoc);
1607 else
1608 Remove (Formal);
1609 Append (Formal, Assoc);
1610 end if;
1611
1612 when others =>
1613 raise Program_Error;
1614
1615 end case;
1616
1617 Formal := Saved_Formal;
1618 Next_Non_Pragma (Analyzed_Formal);
1619 end loop;
1620
1621 if Num_Actuals > Num_Matched then
1622 Error_Msg_Sloc := Sloc (Gen_Unit);
1623
1624 if Present (Selector_Name (Actual)) then
1625 Error_Msg_NE
1626 ("unmatched actual&",
1627 Actual, Selector_Name (Actual));
1628 Error_Msg_NE ("\in instantiation of& declared#",
1629 Actual, Gen_Unit);
1630 else
1631 Error_Msg_NE
1632 ("unmatched actual in instantiation of& declared#",
1633 Actual, Gen_Unit);
1634 end if;
1635 end if;
1636
1637 elsif Present (Actuals) then
1638 Error_Msg_N
1639 ("too many actuals in generic instantiation", Instantiation_Node);
1640 end if;
1641
1642 -- An instantiation freezes all generic actuals. The only exceptions
1643 -- to this are incomplete types and subprograms which are not fully
1644 -- defined at the point of instantiation.
1645
1646 declare
1647 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1648 begin
1649 while Present (Elmt) loop
1650 Freeze_Before (I_Node, Node (Elmt));
1651 Next_Elmt (Elmt);
1652 end loop;
1653 end;
1654
1655 -- If there are default subprograms, normalize the tree by adding
1656 -- explicit associations for them. This is required if the instance
1657 -- appears within a generic.
1658
1659 declare
1660 Elmt : Elmt_Id;
1661 Subp : Entity_Id;
1662 New_D : Node_Id;
1663
1664 begin
1665 Elmt := First_Elmt (Default_Actuals);
1666 while Present (Elmt) loop
1667 if No (Actuals) then
1668 Actuals := New_List;
1669 Set_Generic_Associations (I_Node, Actuals);
1670 end if;
1671
1672 Subp := Node (Elmt);
1673 New_D :=
1674 Make_Generic_Association (Sloc (Subp),
1675 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1676 Explicit_Generic_Actual_Parameter =>
1677 New_Occurrence_Of (Subp, Sloc (Subp)));
1678 Mark_Rewrite_Insertion (New_D);
1679 Append_To (Actuals, New_D);
1680 Next_Elmt (Elmt);
1681 end loop;
1682 end;
1683
1684 -- If this is a formal package, normalize the parameter list by adding
1685 -- explicit box associations for the formals that are covered by an
1686 -- Others_Choice.
1687
1688 if not Is_Empty_List (Default_Formals) then
1689 Append_List (Default_Formals, Formals);
1690 end if;
1691
1692 return Assoc;
1693 end Analyze_Associations;
1694
1695 -------------------------------
1696 -- Analyze_Formal_Array_Type --
1697 -------------------------------
1698
1699 procedure Analyze_Formal_Array_Type
1700 (T : in out Entity_Id;
1701 Def : Node_Id)
1702 is
1703 DSS : Node_Id;
1704
1705 begin
1706 -- Treated like a non-generic array declaration, with additional
1707 -- semantic checks.
1708
1709 Enter_Name (T);
1710
1711 if Nkind (Def) = N_Constrained_Array_Definition then
1712 DSS := First (Discrete_Subtype_Definitions (Def));
1713 while Present (DSS) loop
1714 if Nkind_In (DSS, N_Subtype_Indication,
1715 N_Range,
1716 N_Attribute_Reference)
1717 then
1718 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1719 end if;
1720
1721 Next (DSS);
1722 end loop;
1723 end if;
1724
1725 Array_Type_Declaration (T, Def);
1726 Set_Is_Generic_Type (Base_Type (T));
1727
1728 if Ekind (Component_Type (T)) = E_Incomplete_Type
1729 and then No (Full_View (Component_Type (T)))
1730 then
1731 Error_Msg_N ("premature usage of incomplete type", Def);
1732
1733 -- Check that range constraint is not allowed on the component type
1734 -- of a generic formal array type (AARM 12.5.3(3))
1735
1736 elsif Is_Internal (Component_Type (T))
1737 and then Present (Subtype_Indication (Component_Definition (Def)))
1738 and then Nkind (Original_Node
1739 (Subtype_Indication (Component_Definition (Def)))) =
1740 N_Subtype_Indication
1741 then
1742 Error_Msg_N
1743 ("in a formal, a subtype indication can only be "
1744 & "a subtype mark (RM 12.5.3(3))",
1745 Subtype_Indication (Component_Definition (Def)));
1746 end if;
1747
1748 end Analyze_Formal_Array_Type;
1749
1750 ---------------------------------------------
1751 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1752 ---------------------------------------------
1753
1754 -- As for other generic types, we create a valid type representation with
1755 -- legal but arbitrary attributes, whose values are never considered
1756 -- static. For all scalar types we introduce an anonymous base type, with
1757 -- the same attributes. We choose the corresponding integer type to be
1758 -- Standard_Integer.
1759 -- Here and in other similar routines, the Sloc of the generated internal
1760 -- type must be the same as the sloc of the defining identifier of the
1761 -- formal type declaration, to provide proper source navigation.
1762
1763 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1764 (T : Entity_Id;
1765 Def : Node_Id)
1766 is
1767 Loc : constant Source_Ptr := Sloc (Def);
1768
1769 Base : constant Entity_Id :=
1770 New_Internal_Entity
1771 (E_Decimal_Fixed_Point_Type,
1772 Current_Scope,
1773 Sloc (Defining_Identifier (Parent (Def))), 'G');
1774
1775 Int_Base : constant Entity_Id := Standard_Integer;
1776 Delta_Val : constant Ureal := Ureal_1;
1777 Digs_Val : constant Uint := Uint_6;
1778
1779 begin
1780 Enter_Name (T);
1781
1782 Set_Etype (Base, Base);
1783 Set_Size_Info (Base, Int_Base);
1784 Set_RM_Size (Base, RM_Size (Int_Base));
1785 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1786 Set_Digits_Value (Base, Digs_Val);
1787 Set_Delta_Value (Base, Delta_Val);
1788 Set_Small_Value (Base, Delta_Val);
1789 Set_Scalar_Range (Base,
1790 Make_Range (Loc,
1791 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1792 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1793
1794 Set_Is_Generic_Type (Base);
1795 Set_Parent (Base, Parent (Def));
1796
1797 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1798 Set_Etype (T, Base);
1799 Set_Size_Info (T, Int_Base);
1800 Set_RM_Size (T, RM_Size (Int_Base));
1801 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1802 Set_Digits_Value (T, Digs_Val);
1803 Set_Delta_Value (T, Delta_Val);
1804 Set_Small_Value (T, Delta_Val);
1805 Set_Scalar_Range (T, Scalar_Range (Base));
1806 Set_Is_Constrained (T);
1807
1808 Check_Restriction (No_Fixed_Point, Def);
1809 end Analyze_Formal_Decimal_Fixed_Point_Type;
1810
1811 -------------------------------------------
1812 -- Analyze_Formal_Derived_Interface_Type --
1813 -------------------------------------------
1814
1815 procedure Analyze_Formal_Derived_Interface_Type
1816 (N : Node_Id;
1817 T : Entity_Id;
1818 Def : Node_Id)
1819 is
1820 Loc : constant Source_Ptr := Sloc (Def);
1821
1822 begin
1823 -- Rewrite as a type declaration of a derived type. This ensures that
1824 -- the interface list and primitive operations are properly captured.
1825
1826 Rewrite (N,
1827 Make_Full_Type_Declaration (Loc,
1828 Defining_Identifier => T,
1829 Type_Definition => Def));
1830 Analyze (N);
1831 Set_Is_Generic_Type (T);
1832 end Analyze_Formal_Derived_Interface_Type;
1833
1834 ---------------------------------
1835 -- Analyze_Formal_Derived_Type --
1836 ---------------------------------
1837
1838 procedure Analyze_Formal_Derived_Type
1839 (N : Node_Id;
1840 T : Entity_Id;
1841 Def : Node_Id)
1842 is
1843 Loc : constant Source_Ptr := Sloc (Def);
1844 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1845 New_N : Node_Id;
1846
1847 begin
1848 Set_Is_Generic_Type (T);
1849
1850 if Private_Present (Def) then
1851 New_N :=
1852 Make_Private_Extension_Declaration (Loc,
1853 Defining_Identifier => T,
1854 Discriminant_Specifications => Discriminant_Specifications (N),
1855 Unknown_Discriminants_Present => Unk_Disc,
1856 Subtype_Indication => Subtype_Mark (Def),
1857 Interface_List => Interface_List (Def));
1858
1859 Set_Abstract_Present (New_N, Abstract_Present (Def));
1860 Set_Limited_Present (New_N, Limited_Present (Def));
1861 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
1862
1863 else
1864 New_N :=
1865 Make_Full_Type_Declaration (Loc,
1866 Defining_Identifier => T,
1867 Discriminant_Specifications =>
1868 Discriminant_Specifications (Parent (T)),
1869 Type_Definition =>
1870 Make_Derived_Type_Definition (Loc,
1871 Subtype_Indication => Subtype_Mark (Def)));
1872
1873 Set_Abstract_Present
1874 (Type_Definition (New_N), Abstract_Present (Def));
1875 Set_Limited_Present
1876 (Type_Definition (New_N), Limited_Present (Def));
1877 end if;
1878
1879 Rewrite (N, New_N);
1880 Analyze (N);
1881
1882 if Unk_Disc then
1883 if not Is_Composite_Type (T) then
1884 Error_Msg_N
1885 ("unknown discriminants not allowed for elementary types", N);
1886 else
1887 Set_Has_Unknown_Discriminants (T);
1888 Set_Is_Constrained (T, False);
1889 end if;
1890 end if;
1891
1892 -- If the parent type has a known size, so does the formal, which makes
1893 -- legal representation clauses that involve the formal.
1894
1895 Set_Size_Known_At_Compile_Time
1896 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1897 end Analyze_Formal_Derived_Type;
1898
1899 ----------------------------------
1900 -- Analyze_Formal_Discrete_Type --
1901 ----------------------------------
1902
1903 -- The operations defined for a discrete types are those of an enumeration
1904 -- type. The size is set to an arbitrary value, for use in analyzing the
1905 -- generic unit.
1906
1907 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1908 Loc : constant Source_Ptr := Sloc (Def);
1909 Lo : Node_Id;
1910 Hi : Node_Id;
1911
1912 Base : constant Entity_Id :=
1913 New_Internal_Entity
1914 (E_Floating_Point_Type, Current_Scope,
1915 Sloc (Defining_Identifier (Parent (Def))), 'G');
1916
1917 begin
1918 Enter_Name (T);
1919 Set_Ekind (T, E_Enumeration_Subtype);
1920 Set_Etype (T, Base);
1921 Init_Size (T, 8);
1922 Init_Alignment (T);
1923 Set_Is_Generic_Type (T);
1924 Set_Is_Constrained (T);
1925
1926 -- For semantic analysis, the bounds of the type must be set to some
1927 -- non-static value. The simplest is to create attribute nodes for those
1928 -- bounds, that refer to the type itself. These bounds are never
1929 -- analyzed but serve as place-holders.
1930
1931 Lo :=
1932 Make_Attribute_Reference (Loc,
1933 Attribute_Name => Name_First,
1934 Prefix => New_Reference_To (T, Loc));
1935 Set_Etype (Lo, T);
1936
1937 Hi :=
1938 Make_Attribute_Reference (Loc,
1939 Attribute_Name => Name_Last,
1940 Prefix => New_Reference_To (T, Loc));
1941 Set_Etype (Hi, T);
1942
1943 Set_Scalar_Range (T,
1944 Make_Range (Loc,
1945 Low_Bound => Lo,
1946 High_Bound => Hi));
1947
1948 Set_Ekind (Base, E_Enumeration_Type);
1949 Set_Etype (Base, Base);
1950 Init_Size (Base, 8);
1951 Init_Alignment (Base);
1952 Set_Is_Generic_Type (Base);
1953 Set_Scalar_Range (Base, Scalar_Range (T));
1954 Set_Parent (Base, Parent (Def));
1955 end Analyze_Formal_Discrete_Type;
1956
1957 ----------------------------------
1958 -- Analyze_Formal_Floating_Type --
1959 ---------------------------------
1960
1961 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1962 Base : constant Entity_Id :=
1963 New_Internal_Entity
1964 (E_Floating_Point_Type, Current_Scope,
1965 Sloc (Defining_Identifier (Parent (Def))), 'G');
1966
1967 begin
1968 -- The various semantic attributes are taken from the predefined type
1969 -- Float, just so that all of them are initialized. Their values are
1970 -- never used because no constant folding or expansion takes place in
1971 -- the generic itself.
1972
1973 Enter_Name (T);
1974 Set_Ekind (T, E_Floating_Point_Subtype);
1975 Set_Etype (T, Base);
1976 Set_Size_Info (T, (Standard_Float));
1977 Set_RM_Size (T, RM_Size (Standard_Float));
1978 Set_Digits_Value (T, Digits_Value (Standard_Float));
1979 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1980 Set_Is_Constrained (T);
1981
1982 Set_Is_Generic_Type (Base);
1983 Set_Etype (Base, Base);
1984 Set_Size_Info (Base, (Standard_Float));
1985 Set_RM_Size (Base, RM_Size (Standard_Float));
1986 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1987 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1988 Set_Parent (Base, Parent (Def));
1989
1990 Check_Restriction (No_Floating_Point, Def);
1991 end Analyze_Formal_Floating_Type;
1992
1993 -----------------------------------
1994 -- Analyze_Formal_Interface_Type;--
1995 -----------------------------------
1996
1997 procedure Analyze_Formal_Interface_Type
1998 (N : Node_Id;
1999 T : Entity_Id;
2000 Def : Node_Id)
2001 is
2002 Loc : constant Source_Ptr := Sloc (N);
2003 New_N : Node_Id;
2004
2005 begin
2006 New_N :=
2007 Make_Full_Type_Declaration (Loc,
2008 Defining_Identifier => T,
2009 Type_Definition => Def);
2010
2011 Rewrite (N, New_N);
2012 Analyze (N);
2013 Set_Is_Generic_Type (T);
2014 end Analyze_Formal_Interface_Type;
2015
2016 ---------------------------------
2017 -- Analyze_Formal_Modular_Type --
2018 ---------------------------------
2019
2020 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2021 begin
2022 -- Apart from their entity kind, generic modular types are treated like
2023 -- signed integer types, and have the same attributes.
2024
2025 Analyze_Formal_Signed_Integer_Type (T, Def);
2026 Set_Ekind (T, E_Modular_Integer_Subtype);
2027 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2028
2029 end Analyze_Formal_Modular_Type;
2030
2031 ---------------------------------------
2032 -- Analyze_Formal_Object_Declaration --
2033 ---------------------------------------
2034
2035 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2036 E : constant Node_Id := Default_Expression (N);
2037 Id : constant Node_Id := Defining_Identifier (N);
2038 K : Entity_Kind;
2039 T : Node_Id;
2040
2041 begin
2042 Enter_Name (Id);
2043
2044 -- Determine the mode of the formal object
2045
2046 if Out_Present (N) then
2047 K := E_Generic_In_Out_Parameter;
2048
2049 if not In_Present (N) then
2050 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2051 end if;
2052
2053 else
2054 K := E_Generic_In_Parameter;
2055 end if;
2056
2057 if Present (Subtype_Mark (N)) then
2058 Find_Type (Subtype_Mark (N));
2059 T := Entity (Subtype_Mark (N));
2060
2061 -- Verify that there is no redundant null exclusion
2062
2063 if Null_Exclusion_Present (N) then
2064 if not Is_Access_Type (T) then
2065 Error_Msg_N
2066 ("null exclusion can only apply to an access type", N);
2067
2068 elsif Can_Never_Be_Null (T) then
2069 Error_Msg_NE
2070 ("`NOT NULL` not allowed (& already excludes null)",
2071 N, T);
2072 end if;
2073 end if;
2074
2075 -- Ada 2005 (AI-423): Formal object with an access definition
2076
2077 else
2078 Check_Access_Definition (N);
2079 T := Access_Definition
2080 (Related_Nod => N,
2081 N => Access_Definition (N));
2082 end if;
2083
2084 if Ekind (T) = E_Incomplete_Type then
2085 declare
2086 Error_Node : Node_Id;
2087
2088 begin
2089 if Present (Subtype_Mark (N)) then
2090 Error_Node := Subtype_Mark (N);
2091 else
2092 Check_Access_Definition (N);
2093 Error_Node := Access_Definition (N);
2094 end if;
2095
2096 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2097 end;
2098 end if;
2099
2100 if K = E_Generic_In_Parameter then
2101
2102 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2103
2104 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2105 Error_Msg_N
2106 ("generic formal of mode IN must not be of limited type", N);
2107 Explain_Limited_Type (T, N);
2108 end if;
2109
2110 if Is_Abstract_Type (T) then
2111 Error_Msg_N
2112 ("generic formal of mode IN must not be of abstract type", N);
2113 end if;
2114
2115 if Present (E) then
2116 Preanalyze_Spec_Expression (E, T);
2117
2118 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2119 Error_Msg_N
2120 ("initialization not allowed for limited types", E);
2121 Explain_Limited_Type (T, E);
2122 end if;
2123 end if;
2124
2125 Set_Ekind (Id, K);
2126 Set_Etype (Id, T);
2127
2128 -- Case of generic IN OUT parameter
2129
2130 else
2131 -- If the formal has an unconstrained type, construct its actual
2132 -- subtype, as is done for subprogram formals. In this fashion, all
2133 -- its uses can refer to specific bounds.
2134
2135 Set_Ekind (Id, K);
2136 Set_Etype (Id, T);
2137
2138 if (Is_Array_Type (T)
2139 and then not Is_Constrained (T))
2140 or else
2141 (Ekind (T) = E_Record_Type
2142 and then Has_Discriminants (T))
2143 then
2144 declare
2145 Non_Freezing_Ref : constant Node_Id :=
2146 New_Reference_To (Id, Sloc (Id));
2147 Decl : Node_Id;
2148
2149 begin
2150 -- Make sure the actual subtype doesn't generate bogus freezing
2151
2152 Set_Must_Not_Freeze (Non_Freezing_Ref);
2153 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2154 Insert_Before_And_Analyze (N, Decl);
2155 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2156 end;
2157 else
2158 Set_Actual_Subtype (Id, T);
2159 end if;
2160
2161 if Present (E) then
2162 Error_Msg_N
2163 ("initialization not allowed for `IN OUT` formals", N);
2164 end if;
2165 end if;
2166
2167 if Has_Aspects (N) then
2168 Analyze_Aspect_Specifications (N, Id);
2169 end if;
2170 end Analyze_Formal_Object_Declaration;
2171
2172 ----------------------------------------------
2173 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2174 ----------------------------------------------
2175
2176 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2177 (T : Entity_Id;
2178 Def : Node_Id)
2179 is
2180 Loc : constant Source_Ptr := Sloc (Def);
2181 Base : constant Entity_Id :=
2182 New_Internal_Entity
2183 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2184 Sloc (Defining_Identifier (Parent (Def))), 'G');
2185
2186 begin
2187 -- The semantic attributes are set for completeness only, their values
2188 -- will never be used, since all properties of the type are non-static.
2189
2190 Enter_Name (T);
2191 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2192 Set_Etype (T, Base);
2193 Set_Size_Info (T, Standard_Integer);
2194 Set_RM_Size (T, RM_Size (Standard_Integer));
2195 Set_Small_Value (T, Ureal_1);
2196 Set_Delta_Value (T, Ureal_1);
2197 Set_Scalar_Range (T,
2198 Make_Range (Loc,
2199 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2200 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2201 Set_Is_Constrained (T);
2202
2203 Set_Is_Generic_Type (Base);
2204 Set_Etype (Base, Base);
2205 Set_Size_Info (Base, Standard_Integer);
2206 Set_RM_Size (Base, RM_Size (Standard_Integer));
2207 Set_Small_Value (Base, Ureal_1);
2208 Set_Delta_Value (Base, Ureal_1);
2209 Set_Scalar_Range (Base, Scalar_Range (T));
2210 Set_Parent (Base, Parent (Def));
2211
2212 Check_Restriction (No_Fixed_Point, Def);
2213 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2214
2215 ----------------------------------------
2216 -- Analyze_Formal_Package_Declaration --
2217 ----------------------------------------
2218
2219 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2220 Loc : constant Source_Ptr := Sloc (N);
2221 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2222 Formal : Entity_Id;
2223 Gen_Id : constant Node_Id := Name (N);
2224 Gen_Decl : Node_Id;
2225 Gen_Unit : Entity_Id;
2226 New_N : Node_Id;
2227 Parent_Installed : Boolean := False;
2228 Renaming : Node_Id;
2229 Parent_Instance : Entity_Id;
2230 Renaming_In_Par : Entity_Id;
2231 Associations : Boolean := True;
2232
2233 Vis_Prims_List : Elist_Id := No_Elist;
2234 -- List of primitives made temporarily visible in the instantiation
2235 -- to match the visibility of the formal type
2236
2237 function Build_Local_Package return Node_Id;
2238 -- The formal package is rewritten so that its parameters are replaced
2239 -- with corresponding declarations. For parameters with bona fide
2240 -- associations these declarations are created by Analyze_Associations
2241 -- as for a regular instantiation. For boxed parameters, we preserve
2242 -- the formal declarations and analyze them, in order to introduce
2243 -- entities of the right kind in the environment of the formal.
2244
2245 -------------------------
2246 -- Build_Local_Package --
2247 -------------------------
2248
2249 function Build_Local_Package return Node_Id is
2250 Decls : List_Id;
2251 Pack_Decl : Node_Id;
2252
2253 begin
2254 -- Within the formal, the name of the generic package is a renaming
2255 -- of the formal (as for a regular instantiation).
2256
2257 Pack_Decl :=
2258 Make_Package_Declaration (Loc,
2259 Specification =>
2260 Copy_Generic_Node
2261 (Specification (Original_Node (Gen_Decl)),
2262 Empty, Instantiating => True));
2263
2264 Renaming := Make_Package_Renaming_Declaration (Loc,
2265 Defining_Unit_Name =>
2266 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2267 Name => New_Occurrence_Of (Formal, Loc));
2268
2269 if Nkind (Gen_Id) = N_Identifier
2270 and then Chars (Gen_Id) = Chars (Pack_Id)
2271 then
2272 Error_Msg_NE
2273 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2274 end if;
2275
2276 -- If the formal is declared with a box, or with an others choice,
2277 -- create corresponding declarations for all entities in the formal
2278 -- part, so that names with the proper types are available in the
2279 -- specification of the formal package.
2280
2281 -- On the other hand, if there are no associations, then all the
2282 -- formals must have defaults, and this will be checked by the
2283 -- call to Analyze_Associations.
2284
2285 if Box_Present (N)
2286 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2287 then
2288 declare
2289 Formal_Decl : Node_Id;
2290
2291 begin
2292 -- TBA : for a formal package, need to recurse ???
2293
2294 Decls := New_List;
2295 Formal_Decl :=
2296 First
2297 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2298 while Present (Formal_Decl) loop
2299 Append_To
2300 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2301 Next (Formal_Decl);
2302 end loop;
2303 end;
2304
2305 -- If generic associations are present, use Analyze_Associations to
2306 -- create the proper renaming declarations.
2307
2308 else
2309 declare
2310 Act_Tree : constant Node_Id :=
2311 Copy_Generic_Node
2312 (Original_Node (Gen_Decl), Empty,
2313 Instantiating => True);
2314
2315 begin
2316 Generic_Renamings.Set_Last (0);
2317 Generic_Renamings_HTable.Reset;
2318 Instantiation_Node := N;
2319
2320 Decls :=
2321 Analyze_Associations
2322 (I_Node => Original_Node (N),
2323 Formals => Generic_Formal_Declarations (Act_Tree),
2324 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2325
2326 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2327 end;
2328 end if;
2329
2330 Append (Renaming, To => Decls);
2331
2332 -- Add generated declarations ahead of local declarations in
2333 -- the package.
2334
2335 if No (Visible_Declarations (Specification (Pack_Decl))) then
2336 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2337 else
2338 Insert_List_Before
2339 (First (Visible_Declarations (Specification (Pack_Decl))),
2340 Decls);
2341 end if;
2342
2343 return Pack_Decl;
2344 end Build_Local_Package;
2345
2346 -- Start of processing for Analyze_Formal_Package_Declaration
2347
2348 begin
2349 Text_IO_Kludge (Gen_Id);
2350
2351 Init_Env;
2352 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2353 Gen_Unit := Entity (Gen_Id);
2354
2355 -- Check for a formal package that is a package renaming
2356
2357 if Present (Renamed_Object (Gen_Unit)) then
2358
2359 -- Indicate that unit is used, before replacing it with renamed
2360 -- entity for use below.
2361
2362 if In_Extended_Main_Source_Unit (N) then
2363 Set_Is_Instantiated (Gen_Unit);
2364 Generate_Reference (Gen_Unit, N);
2365 end if;
2366
2367 Gen_Unit := Renamed_Object (Gen_Unit);
2368 end if;
2369
2370 if Ekind (Gen_Unit) /= E_Generic_Package then
2371 Error_Msg_N ("expect generic package name", Gen_Id);
2372 Restore_Env;
2373 goto Leave;
2374
2375 elsif Gen_Unit = Current_Scope then
2376 Error_Msg_N
2377 ("generic package cannot be used as a formal package of itself",
2378 Gen_Id);
2379 Restore_Env;
2380 goto Leave;
2381
2382 elsif In_Open_Scopes (Gen_Unit) then
2383 if Is_Compilation_Unit (Gen_Unit)
2384 and then Is_Child_Unit (Current_Scope)
2385 then
2386 -- Special-case the error when the formal is a parent, and
2387 -- continue analysis to minimize cascaded errors.
2388
2389 Error_Msg_N
2390 ("generic parent cannot be used as formal package "
2391 & "of a child unit",
2392 Gen_Id);
2393
2394 else
2395 Error_Msg_N
2396 ("generic package cannot be used as a formal package "
2397 & "within itself",
2398 Gen_Id);
2399 Restore_Env;
2400 goto Leave;
2401 end if;
2402 end if;
2403
2404 -- Check that name of formal package does not hide name of generic,
2405 -- or its leading prefix. This check must be done separately because
2406 -- the name of the generic has already been analyzed.
2407
2408 declare
2409 Gen_Name : Entity_Id;
2410
2411 begin
2412 Gen_Name := Gen_Id;
2413 while Nkind (Gen_Name) = N_Expanded_Name loop
2414 Gen_Name := Prefix (Gen_Name);
2415 end loop;
2416
2417 if Chars (Gen_Name) = Chars (Pack_Id) then
2418 Error_Msg_NE
2419 ("& is hidden within declaration of formal package",
2420 Gen_Id, Gen_Name);
2421 end if;
2422 end;
2423
2424 if Box_Present (N)
2425 or else No (Generic_Associations (N))
2426 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2427 then
2428 Associations := False;
2429 end if;
2430
2431 -- If there are no generic associations, the generic parameters appear
2432 -- as local entities and are instantiated like them. We copy the generic
2433 -- package declaration as if it were an instantiation, and analyze it
2434 -- like a regular package, except that we treat the formals as
2435 -- additional visible components.
2436
2437 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2438
2439 if In_Extended_Main_Source_Unit (N) then
2440 Set_Is_Instantiated (Gen_Unit);
2441 Generate_Reference (Gen_Unit, N);
2442 end if;
2443
2444 Formal := New_Copy (Pack_Id);
2445 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2446
2447 begin
2448 -- Make local generic without formals. The formals will be replaced
2449 -- with internal declarations.
2450
2451 New_N := Build_Local_Package;
2452
2453 -- If there are errors in the parameter list, Analyze_Associations
2454 -- raises Instantiation_Error. Patch the declaration to prevent
2455 -- further exception propagation.
2456
2457 exception
2458 when Instantiation_Error =>
2459
2460 Enter_Name (Formal);
2461 Set_Ekind (Formal, E_Variable);
2462 Set_Etype (Formal, Any_Type);
2463 Restore_Hidden_Primitives (Vis_Prims_List);
2464
2465 if Parent_Installed then
2466 Remove_Parent;
2467 end if;
2468
2469 goto Leave;
2470 end;
2471
2472 Rewrite (N, New_N);
2473 Set_Defining_Unit_Name (Specification (New_N), Formal);
2474 Set_Generic_Parent (Specification (N), Gen_Unit);
2475 Set_Instance_Env (Gen_Unit, Formal);
2476 Set_Is_Generic_Instance (Formal);
2477
2478 Enter_Name (Formal);
2479 Set_Ekind (Formal, E_Package);
2480 Set_Etype (Formal, Standard_Void_Type);
2481 Set_Inner_Instances (Formal, New_Elmt_List);
2482 Push_Scope (Formal);
2483
2484 if Is_Child_Unit (Gen_Unit)
2485 and then Parent_Installed
2486 then
2487 -- Similarly, we have to make the name of the formal visible in the
2488 -- parent instance, to resolve properly fully qualified names that
2489 -- may appear in the generic unit. The parent instance has been
2490 -- placed on the scope stack ahead of the current scope.
2491
2492 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2493
2494 Renaming_In_Par :=
2495 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2496 Set_Ekind (Renaming_In_Par, E_Package);
2497 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2498 Set_Scope (Renaming_In_Par, Parent_Instance);
2499 Set_Parent (Renaming_In_Par, Parent (Formal));
2500 Set_Renamed_Object (Renaming_In_Par, Formal);
2501 Append_Entity (Renaming_In_Par, Parent_Instance);
2502 end if;
2503
2504 Analyze (Specification (N));
2505
2506 -- The formals for which associations are provided are not visible
2507 -- outside of the formal package. The others are still declared by a
2508 -- formal parameter declaration.
2509
2510 -- If there are no associations, the only local entity to hide is the
2511 -- generated package renaming itself.
2512
2513 declare
2514 E : Entity_Id;
2515
2516 begin
2517 E := First_Entity (Formal);
2518 while Present (E) loop
2519 if Associations
2520 and then not Is_Generic_Formal (E)
2521 then
2522 Set_Is_Hidden (E);
2523 end if;
2524
2525 if Ekind (E) = E_Package
2526 and then Renamed_Entity (E) = Formal
2527 then
2528 Set_Is_Hidden (E);
2529 exit;
2530 end if;
2531
2532 Next_Entity (E);
2533 end loop;
2534 end;
2535
2536 End_Package_Scope (Formal);
2537 Restore_Hidden_Primitives (Vis_Prims_List);
2538
2539 if Parent_Installed then
2540 Remove_Parent;
2541 end if;
2542
2543 Restore_Env;
2544
2545 -- Inside the generic unit, the formal package is a regular package, but
2546 -- no body is needed for it. Note that after instantiation, the defining
2547 -- unit name we need is in the new tree and not in the original (see
2548 -- Package_Instantiation). A generic formal package is an instance, and
2549 -- can be used as an actual for an inner instance.
2550
2551 Set_Has_Completion (Formal, True);
2552
2553 -- Add semantic information to the original defining identifier.
2554 -- for ASIS use.
2555
2556 Set_Ekind (Pack_Id, E_Package);
2557 Set_Etype (Pack_Id, Standard_Void_Type);
2558 Set_Scope (Pack_Id, Scope (Formal));
2559 Set_Has_Completion (Pack_Id, True);
2560
2561 <<Leave>>
2562 if Has_Aspects (N) then
2563 Analyze_Aspect_Specifications (N, Pack_Id);
2564 end if;
2565 end Analyze_Formal_Package_Declaration;
2566
2567 ---------------------------------
2568 -- Analyze_Formal_Private_Type --
2569 ---------------------------------
2570
2571 procedure Analyze_Formal_Private_Type
2572 (N : Node_Id;
2573 T : Entity_Id;
2574 Def : Node_Id)
2575 is
2576 begin
2577 New_Private_Type (N, T, Def);
2578
2579 -- Set the size to an arbitrary but legal value
2580
2581 Set_Size_Info (T, Standard_Integer);
2582 Set_RM_Size (T, RM_Size (Standard_Integer));
2583 end Analyze_Formal_Private_Type;
2584
2585 ------------------------------------
2586 -- Analyze_Formal_Incomplete_Type --
2587 ------------------------------------
2588
2589 procedure Analyze_Formal_Incomplete_Type
2590 (T : Entity_Id;
2591 Def : Node_Id)
2592 is
2593 begin
2594 Enter_Name (T);
2595 Set_Ekind (T, E_Incomplete_Type);
2596 Set_Etype (T, T);
2597 Set_Private_Dependents (T, New_Elmt_List);
2598
2599 if Tagged_Present (Def) then
2600 Set_Is_Tagged_Type (T);
2601 Make_Class_Wide_Type (T);
2602 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2603 end if;
2604 end Analyze_Formal_Incomplete_Type;
2605
2606 ----------------------------------------
2607 -- Analyze_Formal_Signed_Integer_Type --
2608 ----------------------------------------
2609
2610 procedure Analyze_Formal_Signed_Integer_Type
2611 (T : Entity_Id;
2612 Def : Node_Id)
2613 is
2614 Base : constant Entity_Id :=
2615 New_Internal_Entity
2616 (E_Signed_Integer_Type,
2617 Current_Scope,
2618 Sloc (Defining_Identifier (Parent (Def))), 'G');
2619
2620 begin
2621 Enter_Name (T);
2622
2623 Set_Ekind (T, E_Signed_Integer_Subtype);
2624 Set_Etype (T, Base);
2625 Set_Size_Info (T, Standard_Integer);
2626 Set_RM_Size (T, RM_Size (Standard_Integer));
2627 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2628 Set_Is_Constrained (T);
2629
2630 Set_Is_Generic_Type (Base);
2631 Set_Size_Info (Base, Standard_Integer);
2632 Set_RM_Size (Base, RM_Size (Standard_Integer));
2633 Set_Etype (Base, Base);
2634 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2635 Set_Parent (Base, Parent (Def));
2636 end Analyze_Formal_Signed_Integer_Type;
2637
2638 -------------------------------------------
2639 -- Analyze_Formal_Subprogram_Declaration --
2640 -------------------------------------------
2641
2642 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2643 Spec : constant Node_Id := Specification (N);
2644 Def : constant Node_Id := Default_Name (N);
2645 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2646 Subp : Entity_Id;
2647
2648 begin
2649 if Nam = Error then
2650 return;
2651 end if;
2652
2653 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2654 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2655 goto Leave;
2656 end if;
2657
2658 Analyze_Subprogram_Declaration (N);
2659 Set_Is_Formal_Subprogram (Nam);
2660 Set_Has_Completion (Nam);
2661
2662 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2663 Set_Is_Abstract_Subprogram (Nam);
2664 Set_Is_Dispatching_Operation (Nam);
2665
2666 declare
2667 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2668 begin
2669 if No (Ctrl_Type) then
2670 Error_Msg_N
2671 ("abstract formal subprogram must have a controlling type",
2672 N);
2673
2674 elsif Ada_Version >= Ada_2012
2675 and then Is_Incomplete_Type (Ctrl_Type)
2676 then
2677 Error_Msg_NE
2678 ("controlling type of abstract formal subprogram cannot " &
2679 "be incomplete type", N, Ctrl_Type);
2680
2681 else
2682 Check_Controlling_Formals (Ctrl_Type, Nam);
2683 end if;
2684 end;
2685 end if;
2686
2687 -- Default name is resolved at the point of instantiation
2688
2689 if Box_Present (N) then
2690 null;
2691
2692 -- Else default is bound at the point of generic declaration
2693
2694 elsif Present (Def) then
2695 if Nkind (Def) = N_Operator_Symbol then
2696 Find_Direct_Name (Def);
2697
2698 elsif Nkind (Def) /= N_Attribute_Reference then
2699 Analyze (Def);
2700
2701 else
2702 -- For an attribute reference, analyze the prefix and verify
2703 -- that it has the proper profile for the subprogram.
2704
2705 Analyze (Prefix (Def));
2706 Valid_Default_Attribute (Nam, Def);
2707 goto Leave;
2708 end if;
2709
2710 -- Default name may be overloaded, in which case the interpretation
2711 -- with the correct profile must be selected, as for a renaming.
2712 -- If the definition is an indexed component, it must denote a
2713 -- member of an entry family. If it is a selected component, it
2714 -- can be a protected operation.
2715
2716 if Etype (Def) = Any_Type then
2717 goto Leave;
2718
2719 elsif Nkind (Def) = N_Selected_Component then
2720 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2721 Error_Msg_N ("expect valid subprogram name as default", Def);
2722 end if;
2723
2724 elsif Nkind (Def) = N_Indexed_Component then
2725 if Is_Entity_Name (Prefix (Def)) then
2726 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2727 Error_Msg_N ("expect valid subprogram name as default", Def);
2728 end if;
2729
2730 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2731 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2732 E_Entry_Family
2733 then
2734 Error_Msg_N ("expect valid subprogram name as default", Def);
2735 end if;
2736
2737 else
2738 Error_Msg_N ("expect valid subprogram name as default", Def);
2739 goto Leave;
2740 end if;
2741
2742 elsif Nkind (Def) = N_Character_Literal then
2743
2744 -- Needs some type checks: subprogram should be parameterless???
2745
2746 Resolve (Def, (Etype (Nam)));
2747
2748 elsif not Is_Entity_Name (Def)
2749 or else not Is_Overloadable (Entity (Def))
2750 then
2751 Error_Msg_N ("expect valid subprogram name as default", Def);
2752 goto Leave;
2753
2754 elsif not Is_Overloaded (Def) then
2755 Subp := Entity (Def);
2756
2757 if Subp = Nam then
2758 Error_Msg_N ("premature usage of formal subprogram", Def);
2759
2760 elsif not Entity_Matches_Spec (Subp, Nam) then
2761 Error_Msg_N ("no visible entity matches specification", Def);
2762 end if;
2763
2764 -- More than one interpretation, so disambiguate as for a renaming
2765
2766 else
2767 declare
2768 I : Interp_Index;
2769 I1 : Interp_Index := 0;
2770 It : Interp;
2771 It1 : Interp;
2772
2773 begin
2774 Subp := Any_Id;
2775 Get_First_Interp (Def, I, It);
2776 while Present (It.Nam) loop
2777 if Entity_Matches_Spec (It.Nam, Nam) then
2778 if Subp /= Any_Id then
2779 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2780
2781 if It1 = No_Interp then
2782 Error_Msg_N ("ambiguous default subprogram", Def);
2783 else
2784 Subp := It1.Nam;
2785 end if;
2786
2787 exit;
2788
2789 else
2790 I1 := I;
2791 Subp := It.Nam;
2792 end if;
2793 end if;
2794
2795 Get_Next_Interp (I, It);
2796 end loop;
2797 end;
2798
2799 if Subp /= Any_Id then
2800
2801 -- Subprogram found, generate reference to it
2802
2803 Set_Entity (Def, Subp);
2804 Generate_Reference (Subp, Def);
2805
2806 if Subp = Nam then
2807 Error_Msg_N ("premature usage of formal subprogram", Def);
2808
2809 elsif Ekind (Subp) /= E_Operator then
2810 Check_Mode_Conformant (Subp, Nam);
2811 end if;
2812
2813 else
2814 Error_Msg_N ("no visible subprogram matches specification", N);
2815 end if;
2816 end if;
2817 end if;
2818
2819 <<Leave>>
2820 if Has_Aspects (N) then
2821 Analyze_Aspect_Specifications (N, Nam);
2822 end if;
2823
2824 end Analyze_Formal_Subprogram_Declaration;
2825
2826 -------------------------------------
2827 -- Analyze_Formal_Type_Declaration --
2828 -------------------------------------
2829
2830 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2831 Def : constant Node_Id := Formal_Type_Definition (N);
2832 T : Entity_Id;
2833
2834 begin
2835 T := Defining_Identifier (N);
2836
2837 if Present (Discriminant_Specifications (N))
2838 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2839 then
2840 Error_Msg_N
2841 ("discriminants not allowed for this formal type", T);
2842 end if;
2843
2844 -- Enter the new name, and branch to specific routine
2845
2846 case Nkind (Def) is
2847 when N_Formal_Private_Type_Definition =>
2848 Analyze_Formal_Private_Type (N, T, Def);
2849
2850 when N_Formal_Derived_Type_Definition =>
2851 Analyze_Formal_Derived_Type (N, T, Def);
2852
2853 when N_Formal_Incomplete_Type_Definition =>
2854 Analyze_Formal_Incomplete_Type (T, Def);
2855
2856 when N_Formal_Discrete_Type_Definition =>
2857 Analyze_Formal_Discrete_Type (T, Def);
2858
2859 when N_Formal_Signed_Integer_Type_Definition =>
2860 Analyze_Formal_Signed_Integer_Type (T, Def);
2861
2862 when N_Formal_Modular_Type_Definition =>
2863 Analyze_Formal_Modular_Type (T, Def);
2864
2865 when N_Formal_Floating_Point_Definition =>
2866 Analyze_Formal_Floating_Type (T, Def);
2867
2868 when N_Formal_Ordinary_Fixed_Point_Definition =>
2869 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2870
2871 when N_Formal_Decimal_Fixed_Point_Definition =>
2872 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2873
2874 when N_Array_Type_Definition =>
2875 Analyze_Formal_Array_Type (T, Def);
2876
2877 when N_Access_To_Object_Definition |
2878 N_Access_Function_Definition |
2879 N_Access_Procedure_Definition =>
2880 Analyze_Generic_Access_Type (T, Def);
2881
2882 -- Ada 2005: a interface declaration is encoded as an abstract
2883 -- record declaration or a abstract type derivation.
2884
2885 when N_Record_Definition =>
2886 Analyze_Formal_Interface_Type (N, T, Def);
2887
2888 when N_Derived_Type_Definition =>
2889 Analyze_Formal_Derived_Interface_Type (N, T, Def);
2890
2891 when N_Error =>
2892 null;
2893
2894 when others =>
2895 raise Program_Error;
2896
2897 end case;
2898
2899 Set_Is_Generic_Type (T);
2900
2901 if Has_Aspects (N) then
2902 Analyze_Aspect_Specifications (N, T);
2903 end if;
2904 end Analyze_Formal_Type_Declaration;
2905
2906 ------------------------------------
2907 -- Analyze_Function_Instantiation --
2908 ------------------------------------
2909
2910 procedure Analyze_Function_Instantiation (N : Node_Id) is
2911 begin
2912 Analyze_Subprogram_Instantiation (N, E_Function);
2913 end Analyze_Function_Instantiation;
2914
2915 ---------------------------------
2916 -- Analyze_Generic_Access_Type --
2917 ---------------------------------
2918
2919 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2920 begin
2921 Enter_Name (T);
2922
2923 if Nkind (Def) = N_Access_To_Object_Definition then
2924 Access_Type_Declaration (T, Def);
2925
2926 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2927 and then No (Full_View (Designated_Type (T)))
2928 and then not Is_Generic_Type (Designated_Type (T))
2929 then
2930 Error_Msg_N ("premature usage of incomplete type", Def);
2931
2932 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
2933 Error_Msg_N
2934 ("only a subtype mark is allowed in a formal", Def);
2935 end if;
2936
2937 else
2938 Access_Subprogram_Declaration (T, Def);
2939 end if;
2940 end Analyze_Generic_Access_Type;
2941
2942 ---------------------------------
2943 -- Analyze_Generic_Formal_Part --
2944 ---------------------------------
2945
2946 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2947 Gen_Parm_Decl : Node_Id;
2948
2949 begin
2950 -- The generic formals are processed in the scope of the generic unit,
2951 -- where they are immediately visible. The scope is installed by the
2952 -- caller.
2953
2954 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2955
2956 while Present (Gen_Parm_Decl) loop
2957 Analyze (Gen_Parm_Decl);
2958 Next (Gen_Parm_Decl);
2959 end loop;
2960
2961 Generate_Reference_To_Generic_Formals (Current_Scope);
2962 end Analyze_Generic_Formal_Part;
2963
2964 ------------------------------------------
2965 -- Analyze_Generic_Package_Declaration --
2966 ------------------------------------------
2967
2968 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2969 Loc : constant Source_Ptr := Sloc (N);
2970 Id : Entity_Id;
2971 New_N : Node_Id;
2972 Save_Parent : Node_Id;
2973 Renaming : Node_Id;
2974 Decls : constant List_Id :=
2975 Visible_Declarations (Specification (N));
2976 Decl : Node_Id;
2977
2978 begin
2979 Check_SPARK_Restriction ("generic is not allowed", N);
2980
2981 -- We introduce a renaming of the enclosing package, to have a usable
2982 -- entity as the prefix of an expanded name for a local entity of the
2983 -- form Par.P.Q, where P is the generic package. This is because a local
2984 -- entity named P may hide it, so that the usual visibility rules in
2985 -- the instance will not resolve properly.
2986
2987 Renaming :=
2988 Make_Package_Renaming_Declaration (Loc,
2989 Defining_Unit_Name =>
2990 Make_Defining_Identifier (Loc,
2991 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2992 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2993
2994 if Present (Decls) then
2995 Decl := First (Decls);
2996 while Present (Decl)
2997 and then Nkind (Decl) = N_Pragma
2998 loop
2999 Next (Decl);
3000 end loop;
3001
3002 if Present (Decl) then
3003 Insert_Before (Decl, Renaming);
3004 else
3005 Append (Renaming, Visible_Declarations (Specification (N)));
3006 end if;
3007
3008 else
3009 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3010 end if;
3011
3012 -- Create copy of generic unit, and save for instantiation. If the unit
3013 -- is a child unit, do not copy the specifications for the parent, which
3014 -- are not part of the generic tree.
3015
3016 Save_Parent := Parent_Spec (N);
3017 Set_Parent_Spec (N, Empty);
3018
3019 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3020 Set_Parent_Spec (New_N, Save_Parent);
3021 Rewrite (N, New_N);
3022 Id := Defining_Entity (N);
3023 Generate_Definition (Id);
3024
3025 -- Expansion is not applied to generic units
3026
3027 Start_Generic;
3028
3029 Enter_Name (Id);
3030 Set_Ekind (Id, E_Generic_Package);
3031 Set_Etype (Id, Standard_Void_Type);
3032 Set_Contract (Id, Make_Contract (Sloc (Id)));
3033
3034 -- Analyze aspects now, so that generated pragmas appear in the
3035 -- declarations before building and analyzing the generic copy.
3036
3037 if Has_Aspects (N) then
3038 Analyze_Aspect_Specifications (N, Id);
3039 end if;
3040
3041 Push_Scope (Id);
3042 Enter_Generic_Scope (Id);
3043 Set_Inner_Instances (Id, New_Elmt_List);
3044
3045 Set_Categorization_From_Pragmas (N);
3046 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3047
3048 -- Link the declaration of the generic homonym in the generic copy to
3049 -- the package it renames, so that it is always resolved properly.
3050
3051 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3052 Set_Entity (Associated_Node (Name (Renaming)), Id);
3053
3054 -- For a library unit, we have reconstructed the entity for the unit,
3055 -- and must reset it in the library tables.
3056
3057 if Nkind (Parent (N)) = N_Compilation_Unit then
3058 Set_Cunit_Entity (Current_Sem_Unit, Id);
3059 end if;
3060
3061 Analyze_Generic_Formal_Part (N);
3062
3063 -- After processing the generic formals, analysis proceeds as for a
3064 -- non-generic package.
3065
3066 Analyze (Specification (N));
3067
3068 Validate_Categorization_Dependency (N, Id);
3069
3070 End_Generic;
3071
3072 End_Package_Scope (Id);
3073 Exit_Generic_Scope (Id);
3074
3075 if Nkind (Parent (N)) /= N_Compilation_Unit then
3076 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3077 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3078 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3079
3080 else
3081 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3082 Validate_RT_RAT_Component (N);
3083
3084 -- If this is a spec without a body, check that generic parameters
3085 -- are referenced.
3086
3087 if not Body_Required (Parent (N)) then
3088 Check_References (Id);
3089 end if;
3090 end if;
3091
3092 end Analyze_Generic_Package_Declaration;
3093
3094 --------------------------------------------
3095 -- Analyze_Generic_Subprogram_Declaration --
3096 --------------------------------------------
3097
3098 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3099 Spec : Node_Id;
3100 Id : Entity_Id;
3101 Formals : List_Id;
3102 New_N : Node_Id;
3103 Result_Type : Entity_Id;
3104 Save_Parent : Node_Id;
3105 Typ : Entity_Id;
3106
3107 begin
3108 Check_SPARK_Restriction ("generic is not allowed", N);
3109
3110 -- Create copy of generic unit, and save for instantiation. If the unit
3111 -- is a child unit, do not copy the specifications for the parent, which
3112 -- are not part of the generic tree.
3113
3114 Save_Parent := Parent_Spec (N);
3115 Set_Parent_Spec (N, Empty);
3116
3117 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3118 Set_Parent_Spec (New_N, Save_Parent);
3119 Rewrite (N, New_N);
3120
3121 -- The aspect specifications are not attached to the tree, and must
3122 -- be copied and attached to the generic copy explicitly.
3123
3124 if Present (Aspect_Specifications (New_N)) then
3125 declare
3126 Aspects : constant List_Id := Aspect_Specifications (N);
3127 begin
3128 Set_Has_Aspects (N, False);
3129 Move_Aspects (New_N, To => N);
3130 Set_Has_Aspects (Original_Node (N), False);
3131 Set_Aspect_Specifications (Original_Node (N), Aspects);
3132 end;
3133 end if;
3134
3135 Spec := Specification (N);
3136 Id := Defining_Entity (Spec);
3137 Generate_Definition (Id);
3138 Set_Contract (Id, Make_Contract (Sloc (Id)));
3139
3140 if Nkind (Id) = N_Defining_Operator_Symbol then
3141 Error_Msg_N
3142 ("operator symbol not allowed for generic subprogram", Id);
3143 end if;
3144
3145 Start_Generic;
3146
3147 Enter_Name (Id);
3148
3149 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3150 Push_Scope (Id);
3151 Enter_Generic_Scope (Id);
3152 Set_Inner_Instances (Id, New_Elmt_List);
3153 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3154
3155 Analyze_Generic_Formal_Part (N);
3156
3157 Formals := Parameter_Specifications (Spec);
3158
3159 if Present (Formals) then
3160 Process_Formals (Formals, Spec);
3161 end if;
3162
3163 if Nkind (Spec) = N_Function_Specification then
3164 Set_Ekind (Id, E_Generic_Function);
3165
3166 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3167 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3168 Set_Etype (Id, Result_Type);
3169
3170 -- Check restriction imposed by AI05-073: a generic function
3171 -- cannot return an abstract type or an access to such.
3172
3173 -- This is a binding interpretation should it apply to earlier
3174 -- versions of Ada as well as Ada 2012???
3175
3176 if Is_Abstract_Type (Designated_Type (Result_Type))
3177 and then Ada_Version >= Ada_2012
3178 then
3179 Error_Msg_N ("generic function cannot have an access result"
3180 & " that designates an abstract type", Spec);
3181 end if;
3182
3183 else
3184 Find_Type (Result_Definition (Spec));
3185 Typ := Entity (Result_Definition (Spec));
3186
3187 if Is_Abstract_Type (Typ)
3188 and then Ada_Version >= Ada_2012
3189 then
3190 Error_Msg_N
3191 ("generic function cannot have abstract result type", Spec);
3192 end if;
3193
3194 -- If a null exclusion is imposed on the result type, then create
3195 -- a null-excluding itype (an access subtype) and use it as the
3196 -- function's Etype.
3197
3198 if Is_Access_Type (Typ)
3199 and then Null_Exclusion_Present (Spec)
3200 then
3201 Set_Etype (Id,
3202 Create_Null_Excluding_Itype
3203 (T => Typ,
3204 Related_Nod => Spec,
3205 Scope_Id => Defining_Unit_Name (Spec)));
3206 else
3207 Set_Etype (Id, Typ);
3208 end if;
3209 end if;
3210
3211 else
3212 Set_Ekind (Id, E_Generic_Procedure);
3213 Set_Etype (Id, Standard_Void_Type);
3214 end if;
3215
3216 -- For a library unit, we have reconstructed the entity for the unit,
3217 -- and must reset it in the library tables. We also make sure that
3218 -- Body_Required is set properly in the original compilation unit node.
3219
3220 if Nkind (Parent (N)) = N_Compilation_Unit then
3221 Set_Cunit_Entity (Current_Sem_Unit, Id);
3222 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3223 end if;
3224
3225 Set_Categorization_From_Pragmas (N);
3226 Validate_Categorization_Dependency (N, Id);
3227
3228 Save_Global_References (Original_Node (N));
3229
3230 -- For ASIS purposes, convert any postcondition, precondition pragmas
3231 -- into aspects, if N is not a compilation unit by itself, in order to
3232 -- enable the analysis of expressions inside the corresponding PPC
3233 -- pragmas.
3234
3235 if ASIS_Mode and then Is_List_Member (N) then
3236 Make_Aspect_For_PPC_In_Gen_Sub_Decl (N);
3237 end if;
3238
3239 -- To capture global references, analyze the expressions of aspects,
3240 -- and propagate information to original tree. Note that in this case
3241 -- analysis of attributes is not delayed until the freeze point.
3242
3243 -- It seems very hard to recreate the proper visibility of the generic
3244 -- subprogram at a later point because the analysis of an aspect may
3245 -- create pragmas after the generic copies have been made ???
3246
3247 if Has_Aspects (N) then
3248 declare
3249 Aspect : Node_Id;
3250
3251 begin
3252 Aspect := First (Aspect_Specifications (N));
3253 while Present (Aspect) loop
3254 if Get_Aspect_Id (Aspect) /= Aspect_Warnings then
3255 Analyze (Expression (Aspect));
3256 end if;
3257
3258 Next (Aspect);
3259 end loop;
3260
3261 Aspect := First (Aspect_Specifications (Original_Node (N)));
3262 while Present (Aspect) loop
3263 Save_Global_References (Expression (Aspect));
3264 Next (Aspect);
3265 end loop;
3266 end;
3267 end if;
3268
3269 End_Generic;
3270 End_Scope;
3271 Exit_Generic_Scope (Id);
3272 Generate_Reference_To_Formals (Id);
3273
3274 List_Inherited_Pre_Post_Aspects (Id);
3275 end Analyze_Generic_Subprogram_Declaration;
3276
3277 -----------------------------------
3278 -- Analyze_Package_Instantiation --
3279 -----------------------------------
3280
3281 procedure Analyze_Package_Instantiation (N : Node_Id) is
3282 Loc : constant Source_Ptr := Sloc (N);
3283 Gen_Id : constant Node_Id := Name (N);
3284
3285 Act_Decl : Node_Id;
3286 Act_Decl_Name : Node_Id;
3287 Act_Decl_Id : Entity_Id;
3288 Act_Spec : Node_Id;
3289 Act_Tree : Node_Id;
3290
3291 Gen_Decl : Node_Id;
3292 Gen_Unit : Entity_Id;
3293
3294 Is_Actual_Pack : constant Boolean :=
3295 Is_Internal (Defining_Entity (N));
3296
3297 Env_Installed : Boolean := False;
3298 Parent_Installed : Boolean := False;
3299 Renaming_List : List_Id;
3300 Unit_Renaming : Node_Id;
3301 Needs_Body : Boolean;
3302 Inline_Now : Boolean := False;
3303
3304 Save_Style_Check : constant Boolean := Style_Check;
3305 -- Save style check mode for restore on exit
3306
3307 procedure Delay_Descriptors (E : Entity_Id);
3308 -- Delay generation of subprogram descriptors for given entity
3309
3310 function Might_Inline_Subp return Boolean;
3311 -- If inlining is active and the generic contains inlined subprograms,
3312 -- we instantiate the body. This may cause superfluous instantiations,
3313 -- but it is simpler than detecting the need for the body at the point
3314 -- of inlining, when the context of the instance is not available.
3315
3316 function Must_Inline_Subp return Boolean;
3317 -- If inlining is active and the generic contains inlined subprograms,
3318 -- return True if some of the inlined subprograms must be inlined by
3319 -- the frontend.
3320
3321 -----------------------
3322 -- Delay_Descriptors --
3323 -----------------------
3324
3325 procedure Delay_Descriptors (E : Entity_Id) is
3326 begin
3327 if not Delay_Subprogram_Descriptors (E) then
3328 Set_Delay_Subprogram_Descriptors (E);
3329 Pending_Descriptor.Append (E);
3330 end if;
3331 end Delay_Descriptors;
3332
3333 -----------------------
3334 -- Might_Inline_Subp --
3335 -----------------------
3336
3337 function Might_Inline_Subp return Boolean is
3338 E : Entity_Id;
3339
3340 begin
3341 if not Inline_Processing_Required then
3342 return False;
3343
3344 else
3345 E := First_Entity (Gen_Unit);
3346 while Present (E) loop
3347 if Is_Subprogram (E)
3348 and then Is_Inlined (E)
3349 then
3350 return True;
3351 end if;
3352
3353 Next_Entity (E);
3354 end loop;
3355 end if;
3356
3357 return False;
3358 end Might_Inline_Subp;
3359
3360 ----------------------
3361 -- Must_Inline_Subp --
3362 ----------------------
3363
3364 function Must_Inline_Subp return Boolean is
3365 E : Entity_Id;
3366
3367 begin
3368 if not Inline_Processing_Required then
3369 return False;
3370
3371 else
3372 E := First_Entity (Gen_Unit);
3373 while Present (E) loop
3374 if Is_Subprogram (E)
3375 and then Is_Inlined (E)
3376 and then Must_Inline (E)
3377 then
3378 return True;
3379 end if;
3380
3381 Next_Entity (E);
3382 end loop;
3383 end if;
3384
3385 return False;
3386 end Must_Inline_Subp;
3387
3388 -- Local declarations
3389
3390 Vis_Prims_List : Elist_Id := No_Elist;
3391 -- List of primitives made temporarily visible in the instantiation
3392 -- to match the visibility of the formal type
3393
3394 -- Start of processing for Analyze_Package_Instantiation
3395
3396 begin
3397 Check_SPARK_Restriction ("generic is not allowed", N);
3398
3399 -- Very first thing: apply the special kludge for Text_IO processing
3400 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3401
3402 Text_IO_Kludge (Name (N));
3403
3404 -- Make node global for error reporting
3405
3406 Instantiation_Node := N;
3407
3408 -- Turn off style checking in instances. If the check is enabled on the
3409 -- generic unit, a warning in an instance would just be noise. If not
3410 -- enabled on the generic, then a warning in an instance is just wrong.
3411
3412 Style_Check := False;
3413
3414 -- Case of instantiation of a generic package
3415
3416 if Nkind (N) = N_Package_Instantiation then
3417 Act_Decl_Id := New_Copy (Defining_Entity (N));
3418 Set_Comes_From_Source (Act_Decl_Id, True);
3419
3420 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3421 Act_Decl_Name :=
3422 Make_Defining_Program_Unit_Name (Loc,
3423 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
3424 Defining_Identifier => Act_Decl_Id);
3425 else
3426 Act_Decl_Name := Act_Decl_Id;
3427 end if;
3428
3429 -- Case of instantiation of a formal package
3430
3431 else
3432 Act_Decl_Id := Defining_Identifier (N);
3433 Act_Decl_Name := Act_Decl_Id;
3434 end if;
3435
3436 Generate_Definition (Act_Decl_Id);
3437 Preanalyze_Actuals (N);
3438
3439 Init_Env;
3440 Env_Installed := True;
3441
3442 -- Reset renaming map for formal types. The mapping is established
3443 -- when analyzing the generic associations, but some mappings are
3444 -- inherited from formal packages of parent units, and these are
3445 -- constructed when the parents are installed.
3446
3447 Generic_Renamings.Set_Last (0);
3448 Generic_Renamings_HTable.Reset;
3449
3450 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3451 Gen_Unit := Entity (Gen_Id);
3452
3453 -- Verify that it is the name of a generic package
3454
3455 -- A visibility glitch: if the instance is a child unit and the generic
3456 -- is the generic unit of a parent instance (i.e. both the parent and
3457 -- the child units are instances of the same package) the name now
3458 -- denotes the renaming within the parent, not the intended generic
3459 -- unit. See if there is a homonym that is the desired generic. The
3460 -- renaming declaration must be visible inside the instance of the
3461 -- child, but not when analyzing the name in the instantiation itself.
3462
3463 if Ekind (Gen_Unit) = E_Package
3464 and then Present (Renamed_Entity (Gen_Unit))
3465 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3466 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3467 and then Present (Homonym (Gen_Unit))
3468 then
3469 Gen_Unit := Homonym (Gen_Unit);
3470 end if;
3471
3472 if Etype (Gen_Unit) = Any_Type then
3473 Restore_Env;
3474 goto Leave;
3475
3476 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3477
3478 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3479
3480 if From_Limited_With (Gen_Unit) then
3481 Error_Msg_N
3482 ("cannot instantiate a limited withed package", Gen_Id);
3483 else
3484 Error_Msg_NE
3485 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3486 end if;
3487
3488 Restore_Env;
3489 goto Leave;
3490 end if;
3491
3492 if In_Extended_Main_Source_Unit (N) then
3493 Set_Is_Instantiated (Gen_Unit);
3494 Generate_Reference (Gen_Unit, N);
3495
3496 if Present (Renamed_Object (Gen_Unit)) then
3497 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3498 Generate_Reference (Renamed_Object (Gen_Unit), N);
3499 end if;
3500 end if;
3501
3502 if Nkind (Gen_Id) = N_Identifier
3503 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3504 then
3505 Error_Msg_NE
3506 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3507
3508 elsif Nkind (Gen_Id) = N_Expanded_Name
3509 and then Is_Child_Unit (Gen_Unit)
3510 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3511 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3512 then
3513 Error_Msg_N
3514 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3515 end if;
3516
3517 Set_Entity (Gen_Id, Gen_Unit);
3518
3519 -- If generic is a renaming, get original generic unit
3520
3521 if Present (Renamed_Object (Gen_Unit))
3522 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3523 then
3524 Gen_Unit := Renamed_Object (Gen_Unit);
3525 end if;
3526
3527 -- Verify that there are no circular instantiations
3528
3529 if In_Open_Scopes (Gen_Unit) then
3530 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3531 Restore_Env;
3532 goto Leave;
3533
3534 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3535 Error_Msg_Node_2 := Current_Scope;
3536 Error_Msg_NE
3537 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3538 Circularity_Detected := True;
3539 Restore_Env;
3540 goto Leave;
3541
3542 else
3543 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3544
3545 -- Initialize renamings map, for error checking, and the list that
3546 -- holds private entities whose views have changed between generic
3547 -- definition and instantiation. If this is the instance created to
3548 -- validate an actual package, the instantiation environment is that
3549 -- of the enclosing instance.
3550
3551 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3552
3553 -- Copy original generic tree, to produce text for instantiation
3554
3555 Act_Tree :=
3556 Copy_Generic_Node
3557 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3558
3559 Act_Spec := Specification (Act_Tree);
3560
3561 -- If this is the instance created to validate an actual package,
3562 -- only the formals matter, do not examine the package spec itself.
3563
3564 if Is_Actual_Pack then
3565 Set_Visible_Declarations (Act_Spec, New_List);
3566 Set_Private_Declarations (Act_Spec, New_List);
3567 end if;
3568
3569 Renaming_List :=
3570 Analyze_Associations
3571 (I_Node => N,
3572 Formals => Generic_Formal_Declarations (Act_Tree),
3573 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3574
3575 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3576
3577 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3578 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3579 Set_Is_Generic_Instance (Act_Decl_Id);
3580
3581 Set_Generic_Parent (Act_Spec, Gen_Unit);
3582
3583 -- References to the generic in its own declaration or its body are
3584 -- references to the instance. Add a renaming declaration for the
3585 -- generic unit itself. This declaration, as well as the renaming
3586 -- declarations for the generic formals, must remain private to the
3587 -- unit: the formals, because this is the language semantics, and
3588 -- the unit because its use is an artifact of the implementation.
3589
3590 Unit_Renaming :=
3591 Make_Package_Renaming_Declaration (Loc,
3592 Defining_Unit_Name =>
3593 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3594 Name => New_Reference_To (Act_Decl_Id, Loc));
3595
3596 Append (Unit_Renaming, Renaming_List);
3597
3598 -- The renaming declarations are the first local declarations of the
3599 -- new unit.
3600
3601 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3602 Insert_List_Before
3603 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3604 else
3605 Set_Visible_Declarations (Act_Spec, Renaming_List);
3606 end if;
3607
3608 Act_Decl :=
3609 Make_Package_Declaration (Loc,
3610 Specification => Act_Spec);
3611
3612 -- Save the instantiation node, for subsequent instantiation of the
3613 -- body, if there is one and we are generating code for the current
3614 -- unit. Mark unit as having a body (avoids premature error message).
3615
3616 -- We instantiate the body if we are generating code, if we are
3617 -- generating cross-reference information, or if we are building
3618 -- trees for ASIS use or GNATprove use.
3619
3620 declare
3621 Enclosing_Body_Present : Boolean := False;
3622 -- If the generic unit is not a compilation unit, then a body may
3623 -- be present in its parent even if none is required. We create a
3624 -- tentative pending instantiation for the body, which will be
3625 -- discarded if none is actually present.
3626
3627 Scop : Entity_Id;
3628
3629 begin
3630 if Scope (Gen_Unit) /= Standard_Standard
3631 and then not Is_Child_Unit (Gen_Unit)
3632 then
3633 Scop := Scope (Gen_Unit);
3634
3635 while Present (Scop)
3636 and then Scop /= Standard_Standard
3637 loop
3638 if Unit_Requires_Body (Scop) then
3639 Enclosing_Body_Present := True;
3640 exit;
3641
3642 elsif In_Open_Scopes (Scop)
3643 and then In_Package_Body (Scop)
3644 then
3645 Enclosing_Body_Present := True;
3646 exit;
3647 end if;
3648
3649 exit when Is_Compilation_Unit (Scop);
3650 Scop := Scope (Scop);
3651 end loop;
3652 end if;
3653
3654 -- If front-end inlining is enabled, and this is a unit for which
3655 -- code will be generated, we instantiate the body at once.
3656
3657 -- This is done if the instance is not the main unit, and if the
3658 -- generic is not a child unit of another generic, to avoid scope
3659 -- problems and the reinstallation of parent instances.
3660
3661 if Expander_Active
3662 and then (not Is_Child_Unit (Gen_Unit)
3663 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3664 and then Might_Inline_Subp
3665 and then not Is_Actual_Pack
3666 then
3667 if not Debug_Flag_Dot_K
3668 and then Front_End_Inlining
3669 and then (Is_In_Main_Unit (N)
3670 or else In_Main_Context (Current_Scope))
3671 and then Nkind (Parent (N)) /= N_Compilation_Unit
3672 then
3673 Inline_Now := True;
3674
3675 elsif Debug_Flag_Dot_K
3676 and then Must_Inline_Subp
3677 and then (Is_In_Main_Unit (N)
3678 or else In_Main_Context (Current_Scope))
3679 and then Nkind (Parent (N)) /= N_Compilation_Unit
3680 then
3681 Inline_Now := True;
3682
3683 -- In configurable_run_time mode we force the inlining of
3684 -- predefined subprograms marked Inline_Always, to minimize
3685 -- the use of the run-time library.
3686
3687 elsif Is_Predefined_File_Name
3688 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3689 and then Configurable_Run_Time_Mode
3690 and then Nkind (Parent (N)) /= N_Compilation_Unit
3691 then
3692 Inline_Now := True;
3693 end if;
3694
3695 -- If the current scope is itself an instance within a child
3696 -- unit, there will be duplications in the scope stack, and the
3697 -- unstacking mechanism in Inline_Instance_Body will fail.
3698 -- This loses some rare cases of optimization, and might be
3699 -- improved some day, if we can find a proper abstraction for
3700 -- "the complete compilation context" that can be saved and
3701 -- restored. ???
3702
3703 if Is_Generic_Instance (Current_Scope) then
3704 declare
3705 Curr_Unit : constant Entity_Id :=
3706 Cunit_Entity (Current_Sem_Unit);
3707 begin
3708 if Curr_Unit /= Current_Scope
3709 and then Is_Child_Unit (Curr_Unit)
3710 then
3711 Inline_Now := False;
3712 end if;
3713 end;
3714 end if;
3715 end if;
3716
3717 Needs_Body :=
3718 (Unit_Requires_Body (Gen_Unit)
3719 or else Enclosing_Body_Present
3720 or else Present (Corresponding_Body (Gen_Decl)))
3721 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3722 and then not Is_Actual_Pack
3723 and then not Inline_Now
3724 and then (Operating_Mode = Generate_Code
3725 or else (Operating_Mode = Check_Semantics
3726 and then (ASIS_Mode or GNATprove_Mode)));
3727
3728 -- If front_end_inlining is enabled, do not instantiate body if
3729 -- within a generic context.
3730
3731 if (Front_End_Inlining and then not Expander_Active)
3732 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3733 then
3734 Needs_Body := False;
3735 end if;
3736
3737 -- If the current context is generic, and the package being
3738 -- instantiated is declared within a formal package, there is no
3739 -- body to instantiate until the enclosing generic is instantiated
3740 -- and there is an actual for the formal package. If the formal
3741 -- package has parameters, we build a regular package instance for
3742 -- it, that precedes the original formal package declaration.
3743
3744 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3745 declare
3746 Decl : constant Node_Id :=
3747 Original_Node
3748 (Unit_Declaration_Node (Scope (Gen_Unit)));
3749 begin
3750 if Nkind (Decl) = N_Formal_Package_Declaration
3751 or else (Nkind (Decl) = N_Package_Declaration
3752 and then Is_List_Member (Decl)
3753 and then Present (Next (Decl))
3754 and then
3755 Nkind (Next (Decl)) =
3756 N_Formal_Package_Declaration)
3757 then
3758 Needs_Body := False;
3759 end if;
3760 end;
3761 end if;
3762 end;
3763
3764 -- For RCI unit calling stubs, we omit the instance body if the
3765 -- instance is the RCI library unit itself.
3766
3767 -- However there is a special case for nested instances: in this case
3768 -- we do generate the instance body, as it might be required, e.g.
3769 -- because it provides stream attributes for some type used in the
3770 -- profile of a remote subprogram. This is consistent with 12.3(12),
3771 -- which indicates that the instance body occurs at the place of the
3772 -- instantiation, and thus is part of the RCI declaration, which is
3773 -- present on all client partitions (this is E.2.3(18)).
3774
3775 -- Note that AI12-0002 may make it illegal at some point to have
3776 -- stream attributes defined in an RCI unit, in which case this
3777 -- special case will become unnecessary. In the meantime, there
3778 -- is known application code in production that depends on this
3779 -- being possible, so we definitely cannot eliminate the body in
3780 -- the case of nested instances for the time being.
3781
3782 -- When we generate a nested instance body, calling stubs for any
3783 -- relevant subprogram will be be inserted immediately after the
3784 -- subprogram declarations, and will take precedence over the
3785 -- subsequent (original) body. (The stub and original body will be
3786 -- complete homographs, but this is permitted in an instance).
3787 -- (Could we do better and remove the original body???)
3788
3789 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
3790 and then Comes_From_Source (N)
3791 and then Nkind (Parent (N)) = N_Compilation_Unit
3792 then
3793 Needs_Body := False;
3794 end if;
3795
3796 if Needs_Body then
3797
3798 -- Here is a defence against a ludicrous number of instantiations
3799 -- caused by a circular set of instantiation attempts.
3800
3801 if Pending_Instantiations.Last > Maximum_Instantiations then
3802 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
3803 Error_Msg_N ("too many instantiations, exceeds max of^", N);
3804 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
3805 raise Unrecoverable_Error;
3806 end if;
3807
3808 -- Indicate that the enclosing scopes contain an instantiation,
3809 -- and that cleanup actions should be delayed until after the
3810 -- instance body is expanded.
3811
3812 Check_Forward_Instantiation (Gen_Decl);
3813 if Nkind (N) = N_Package_Instantiation then
3814 declare
3815 Enclosing_Master : Entity_Id;
3816
3817 begin
3818 -- Loop to search enclosing masters
3819
3820 Enclosing_Master := Current_Scope;
3821 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
3822 if Ekind (Enclosing_Master) = E_Package then
3823 if Is_Compilation_Unit (Enclosing_Master) then
3824 if In_Package_Body (Enclosing_Master) then
3825 Delay_Descriptors
3826 (Body_Entity (Enclosing_Master));
3827 else
3828 Delay_Descriptors
3829 (Enclosing_Master);
3830 end if;
3831
3832 exit Scope_Loop;
3833
3834 else
3835 Enclosing_Master := Scope (Enclosing_Master);
3836 end if;
3837
3838 elsif Is_Generic_Unit (Enclosing_Master)
3839 or else Ekind (Enclosing_Master) = E_Void
3840 then
3841 -- Cleanup actions will eventually be performed on the
3842 -- enclosing subprogram or package instance, if any.
3843 -- Enclosing scope is void in the formal part of a
3844 -- generic subprogram.
3845
3846 exit Scope_Loop;
3847
3848 else
3849 if Ekind (Enclosing_Master) = E_Entry
3850 and then
3851 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
3852 then
3853 if not Expander_Active then
3854 exit Scope_Loop;
3855 else
3856 Enclosing_Master :=
3857 Protected_Body_Subprogram (Enclosing_Master);
3858 end if;
3859 end if;
3860
3861 Set_Delay_Cleanups (Enclosing_Master);
3862
3863 while Ekind (Enclosing_Master) = E_Block loop
3864 Enclosing_Master := Scope (Enclosing_Master);
3865 end loop;
3866
3867 if Is_Subprogram (Enclosing_Master) then
3868 Delay_Descriptors (Enclosing_Master);
3869
3870 elsif Is_Task_Type (Enclosing_Master) then
3871 declare
3872 TBP : constant Node_Id :=
3873 Get_Task_Body_Procedure
3874 (Enclosing_Master);
3875 begin
3876 if Present (TBP) then
3877 Delay_Descriptors (TBP);
3878 Set_Delay_Cleanups (TBP);
3879 end if;
3880 end;
3881 end if;
3882
3883 exit Scope_Loop;
3884 end if;
3885 end loop Scope_Loop;
3886 end;
3887
3888 -- Make entry in table
3889
3890 Pending_Instantiations.Append
3891 ((Inst_Node => N,
3892 Act_Decl => Act_Decl,
3893 Expander_Status => Expander_Active,
3894 Current_Sem_Unit => Current_Sem_Unit,
3895 Scope_Suppress => Scope_Suppress,
3896 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
3897 Version => Ada_Version,
3898 Version_Pragma => Ada_Version_Pragma,
3899 Warnings => Save_Warnings));
3900 end if;
3901 end if;
3902
3903 Set_Categorization_From_Pragmas (Act_Decl);
3904
3905 if Parent_Installed then
3906 Hide_Current_Scope;
3907 end if;
3908
3909 Set_Instance_Spec (N, Act_Decl);
3910
3911 -- If not a compilation unit, insert the package declaration before
3912 -- the original instantiation node.
3913
3914 if Nkind (Parent (N)) /= N_Compilation_Unit then
3915 Mark_Rewrite_Insertion (Act_Decl);
3916 Insert_Before (N, Act_Decl);
3917 Analyze (Act_Decl);
3918
3919 -- For an instantiation that is a compilation unit, place
3920 -- declaration on current node so context is complete for analysis
3921 -- (including nested instantiations). If this is the main unit,
3922 -- the declaration eventually replaces the instantiation node.
3923 -- If the instance body is created later, it replaces the
3924 -- instance node, and the declaration is attached to it
3925 -- (see Build_Instance_Compilation_Unit_Nodes).
3926
3927 else
3928 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
3929
3930 -- The entity for the current unit is the newly created one,
3931 -- and all semantic information is attached to it.
3932
3933 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
3934
3935 -- If this is the main unit, replace the main entity as well
3936
3937 if Current_Sem_Unit = Main_Unit then
3938 Main_Unit_Entity := Act_Decl_Id;
3939 end if;
3940 end if;
3941
3942 Set_Unit (Parent (N), Act_Decl);
3943 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3944 Set_Package_Instantiation (Act_Decl_Id, N);
3945
3946 -- Process aspect specifications of the instance node, if any, to
3947 -- take into account categorization pragmas before analyzing the
3948 -- instance.
3949
3950 if Has_Aspects (N) then
3951 Analyze_Aspect_Specifications (N, Act_Decl_Id);
3952 end if;
3953
3954 Analyze (Act_Decl);
3955 Set_Unit (Parent (N), N);
3956 Set_Body_Required (Parent (N), False);
3957
3958 -- We never need elaboration checks on instantiations, since by
3959 -- definition, the body instantiation is elaborated at the same
3960 -- time as the spec instantiation.
3961
3962 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3963 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3964 end if;
3965
3966 Check_Elab_Instantiation (N);
3967
3968 if ABE_Is_Certain (N) and then Needs_Body then
3969 Pending_Instantiations.Decrement_Last;
3970 end if;
3971
3972 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3973
3974 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
3975 First_Private_Entity (Act_Decl_Id));
3976
3977 -- If the instantiation will receive a body, the unit will be
3978 -- transformed into a package body, and receive its own elaboration
3979 -- entity. Otherwise, the nature of the unit is now a package
3980 -- declaration.
3981
3982 if Nkind (Parent (N)) = N_Compilation_Unit
3983 and then not Needs_Body
3984 then
3985 Rewrite (N, Act_Decl);
3986 end if;
3987
3988 if Present (Corresponding_Body (Gen_Decl))
3989 or else Unit_Requires_Body (Gen_Unit)
3990 then
3991 Set_Has_Completion (Act_Decl_Id);
3992 end if;
3993
3994 Check_Formal_Packages (Act_Decl_Id);
3995
3996 Restore_Hidden_Primitives (Vis_Prims_List);
3997 Restore_Private_Views (Act_Decl_Id);
3998
3999 Inherit_Context (Gen_Decl, N);
4000
4001 if Parent_Installed then
4002 Remove_Parent;
4003 end if;
4004
4005 Restore_Env;
4006 Env_Installed := False;
4007 end if;
4008
4009 Validate_Categorization_Dependency (N, Act_Decl_Id);
4010
4011 -- There used to be a check here to prevent instantiations in local
4012 -- contexts if the No_Local_Allocators restriction was active. This
4013 -- check was removed by a binding interpretation in AI-95-00130/07,
4014 -- but we retain the code for documentation purposes.
4015
4016 -- if Ekind (Act_Decl_Id) /= E_Void
4017 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4018 -- then
4019 -- Check_Restriction (No_Local_Allocators, N);
4020 -- end if;
4021
4022 if Inline_Now then
4023 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4024 end if;
4025
4026 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4027 -- be used as defining identifiers for a formal package and for the
4028 -- corresponding expanded package.
4029
4030 if Nkind (N) = N_Formal_Package_Declaration then
4031 Act_Decl_Id := New_Copy (Defining_Entity (N));
4032 Set_Comes_From_Source (Act_Decl_Id, True);
4033 Set_Is_Generic_Instance (Act_Decl_Id, False);
4034 Set_Defining_Identifier (N, Act_Decl_Id);
4035 end if;
4036
4037 Style_Check := Save_Style_Check;
4038
4039 -- Check that if N is an instantiation of System.Dim_Float_IO or
4040 -- System.Dim_Integer_IO, the formal type has a dimension system.
4041
4042 if Nkind (N) = N_Package_Instantiation
4043 and then Is_Dim_IO_Package_Instantiation (N)
4044 then
4045 declare
4046 Assoc : constant Node_Id := First (Generic_Associations (N));
4047 begin
4048 if not Has_Dimension_System
4049 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4050 then
4051 Error_Msg_N ("type with a dimension system expected", Assoc);
4052 end if;
4053 end;
4054 end if;
4055
4056 <<Leave>>
4057 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4058 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4059 end if;
4060
4061 exception
4062 when Instantiation_Error =>
4063 if Parent_Installed then
4064 Remove_Parent;
4065 end if;
4066
4067 if Env_Installed then
4068 Restore_Env;
4069 end if;
4070
4071 Style_Check := Save_Style_Check;
4072 end Analyze_Package_Instantiation;
4073
4074 --------------------------
4075 -- Inline_Instance_Body --
4076 --------------------------
4077
4078 procedure Inline_Instance_Body
4079 (N : Node_Id;
4080 Gen_Unit : Entity_Id;
4081 Act_Decl : Node_Id)
4082 is
4083 Vis : Boolean;
4084 Gen_Comp : constant Entity_Id :=
4085 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4086 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4087 Curr_Scope : Entity_Id := Empty;
4088 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4089 Removed : Boolean := False;
4090 Num_Scopes : Int := 0;
4091
4092 Scope_Stack_Depth : constant Int :=
4093 Scope_Stack.Last - Scope_Stack.First + 1;
4094
4095 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4096 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4097 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4098 List : Elist_Id;
4099 Num_Inner : Int := 0;
4100 N_Instances : Int := 0;
4101 S : Entity_Id;
4102
4103 begin
4104 -- Case of generic unit defined in another unit. We must remove the
4105 -- complete context of the current unit to install that of the generic.
4106
4107 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4108
4109 -- Add some comments for the following two loops ???
4110
4111 S := Current_Scope;
4112 while Present (S) and then S /= Standard_Standard loop
4113 loop
4114 Num_Scopes := Num_Scopes + 1;
4115
4116 Use_Clauses (Num_Scopes) :=
4117 (Scope_Stack.Table
4118 (Scope_Stack.Last - Num_Scopes + 1).
4119 First_Use_Clause);
4120 End_Use_Clauses (Use_Clauses (Num_Scopes));
4121
4122 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4123 or else Scope_Stack.Table
4124 (Scope_Stack.Last - Num_Scopes).Entity
4125 = Scope (S);
4126 end loop;
4127
4128 exit when Is_Generic_Instance (S)
4129 and then (In_Package_Body (S)
4130 or else Ekind (S) = E_Procedure
4131 or else Ekind (S) = E_Function);
4132 S := Scope (S);
4133 end loop;
4134
4135 Vis := Is_Immediately_Visible (Gen_Comp);
4136
4137 -- Find and save all enclosing instances
4138
4139 S := Current_Scope;
4140
4141 while Present (S)
4142 and then S /= Standard_Standard
4143 loop
4144 if Is_Generic_Instance (S) then
4145 N_Instances := N_Instances + 1;
4146 Instances (N_Instances) := S;
4147
4148 exit when In_Package_Body (S);
4149 end if;
4150
4151 S := Scope (S);
4152 end loop;
4153
4154 -- Remove context of current compilation unit, unless we are within a
4155 -- nested package instantiation, in which case the context has been
4156 -- removed previously.
4157
4158 -- If current scope is the body of a child unit, remove context of
4159 -- spec as well. If an enclosing scope is an instance body, the
4160 -- context has already been removed, but the entities in the body
4161 -- must be made invisible as well.
4162
4163 S := Current_Scope;
4164
4165 while Present (S)
4166 and then S /= Standard_Standard
4167 loop
4168 if Is_Generic_Instance (S)
4169 and then (In_Package_Body (S)
4170 or else Ekind (S) = E_Procedure
4171 or else Ekind (S) = E_Function)
4172 then
4173 -- We still have to remove the entities of the enclosing
4174 -- instance from direct visibility.
4175
4176 declare
4177 E : Entity_Id;
4178 begin
4179 E := First_Entity (S);
4180 while Present (E) loop
4181 Set_Is_Immediately_Visible (E, False);
4182 Next_Entity (E);
4183 end loop;
4184 end;
4185
4186 exit;
4187 end if;
4188
4189 if S = Curr_Unit
4190 or else (Ekind (Curr_Unit) = E_Package_Body
4191 and then S = Spec_Entity (Curr_Unit))
4192 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4193 and then S =
4194 Corresponding_Spec
4195 (Unit_Declaration_Node (Curr_Unit)))
4196 then
4197 Removed := True;
4198
4199 -- Remove entities in current scopes from visibility, so that
4200 -- instance body is compiled in a clean environment.
4201
4202 List := Save_Scope_Stack (Handle_Use => False);
4203
4204 if Is_Child_Unit (S) then
4205
4206 -- Remove child unit from stack, as well as inner scopes.
4207 -- Removing the context of a child unit removes parent units
4208 -- as well.
4209
4210 while Current_Scope /= S loop
4211 Num_Inner := Num_Inner + 1;
4212 Inner_Scopes (Num_Inner) := Current_Scope;
4213 Pop_Scope;
4214 end loop;
4215
4216 Pop_Scope;
4217 Remove_Context (Curr_Comp);
4218 Curr_Scope := S;
4219
4220 else
4221 Remove_Context (Curr_Comp);
4222 end if;
4223
4224 if Ekind (Curr_Unit) = E_Package_Body then
4225 Remove_Context (Library_Unit (Curr_Comp));
4226 end if;
4227 end if;
4228
4229 S := Scope (S);
4230 end loop;
4231 pragma Assert (Num_Inner < Num_Scopes);
4232
4233 Push_Scope (Standard_Standard);
4234 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4235 Instantiate_Package_Body
4236 (Body_Info =>
4237 ((Inst_Node => N,
4238 Act_Decl => Act_Decl,
4239 Expander_Status => Expander_Active,
4240 Current_Sem_Unit => Current_Sem_Unit,
4241 Scope_Suppress => Scope_Suppress,
4242 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4243 Version => Ada_Version,
4244 Version_Pragma => Ada_Version_Pragma,
4245 Warnings => Save_Warnings)),
4246 Inlined_Body => True);
4247
4248 Pop_Scope;
4249
4250 -- Restore context
4251
4252 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4253
4254 -- Reset Generic_Instance flag so that use clauses can be installed
4255 -- in the proper order. (See Use_One_Package for effect of enclosing
4256 -- instances on processing of use clauses).
4257
4258 for J in 1 .. N_Instances loop
4259 Set_Is_Generic_Instance (Instances (J), False);
4260 end loop;
4261
4262 if Removed then
4263 Install_Context (Curr_Comp);
4264
4265 if Present (Curr_Scope)
4266 and then Is_Child_Unit (Curr_Scope)
4267 then
4268 Push_Scope (Curr_Scope);
4269 Set_Is_Immediately_Visible (Curr_Scope);
4270
4271 -- Finally, restore inner scopes as well
4272
4273 for J in reverse 1 .. Num_Inner loop
4274 Push_Scope (Inner_Scopes (J));
4275 end loop;
4276 end if;
4277
4278 Restore_Scope_Stack (List, Handle_Use => False);
4279
4280 if Present (Curr_Scope)
4281 and then
4282 (In_Private_Part (Curr_Scope)
4283 or else In_Package_Body (Curr_Scope))
4284 then
4285 -- Install private declaration of ancestor units, which are
4286 -- currently available. Restore_Scope_Stack and Install_Context
4287 -- only install the visible part of parents.
4288
4289 declare
4290 Par : Entity_Id;
4291 begin
4292 Par := Scope (Curr_Scope);
4293 while (Present (Par))
4294 and then Par /= Standard_Standard
4295 loop
4296 Install_Private_Declarations (Par);
4297 Par := Scope (Par);
4298 end loop;
4299 end;
4300 end if;
4301 end if;
4302
4303 -- Restore use clauses. For a child unit, use clauses in the parents
4304 -- are restored when installing the context, so only those in inner
4305 -- scopes (and those local to the child unit itself) need to be
4306 -- installed explicitly.
4307
4308 if Is_Child_Unit (Curr_Unit)
4309 and then Removed
4310 then
4311 for J in reverse 1 .. Num_Inner + 1 loop
4312 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4313 Use_Clauses (J);
4314 Install_Use_Clauses (Use_Clauses (J));
4315 end loop;
4316
4317 else
4318 for J in reverse 1 .. Num_Scopes loop
4319 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4320 Use_Clauses (J);
4321 Install_Use_Clauses (Use_Clauses (J));
4322 end loop;
4323 end if;
4324
4325 -- Restore status of instances. If one of them is a body, make its
4326 -- local entities visible again.
4327
4328 declare
4329 E : Entity_Id;
4330 Inst : Entity_Id;
4331
4332 begin
4333 for J in 1 .. N_Instances loop
4334 Inst := Instances (J);
4335 Set_Is_Generic_Instance (Inst, True);
4336
4337 if In_Package_Body (Inst)
4338 or else Ekind (S) = E_Procedure
4339 or else Ekind (S) = E_Function
4340 then
4341 E := First_Entity (Instances (J));
4342 while Present (E) loop
4343 Set_Is_Immediately_Visible (E);
4344 Next_Entity (E);
4345 end loop;
4346 end if;
4347 end loop;
4348 end;
4349
4350 -- If generic unit is in current unit, current context is correct
4351
4352 else
4353 Instantiate_Package_Body
4354 (Body_Info =>
4355 ((Inst_Node => N,
4356 Act_Decl => Act_Decl,
4357 Expander_Status => Expander_Active,
4358 Current_Sem_Unit => Current_Sem_Unit,
4359 Scope_Suppress => Scope_Suppress,
4360 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4361 Version => Ada_Version,
4362 Version_Pragma => Ada_Version_Pragma,
4363 Warnings => Save_Warnings)),
4364 Inlined_Body => True);
4365 end if;
4366 end Inline_Instance_Body;
4367
4368 -------------------------------------
4369 -- Analyze_Procedure_Instantiation --
4370 -------------------------------------
4371
4372 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4373 begin
4374 Analyze_Subprogram_Instantiation (N, E_Procedure);
4375 end Analyze_Procedure_Instantiation;
4376
4377 -----------------------------------
4378 -- Need_Subprogram_Instance_Body --
4379 -----------------------------------
4380
4381 function Need_Subprogram_Instance_Body
4382 (N : Node_Id;
4383 Subp : Entity_Id) return Boolean
4384 is
4385 begin
4386 -- Must be inlined (or inlined renaming)
4387
4388 if (Is_In_Main_Unit (N)
4389 or else Is_Inlined (Subp)
4390 or else Is_Inlined (Alias (Subp)))
4391
4392 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4393
4394 and then (Operating_Mode = Generate_Code
4395 or else (Operating_Mode = Check_Semantics
4396 and then (ASIS_Mode or GNATprove_Mode)))
4397
4398 -- The body is needed when generating code (full expansion), in ASIS
4399 -- mode for other tools, and in GNATprove mode (special expansion) for
4400 -- formal verification of the body itself.
4401
4402 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4403
4404 -- No point in inlining if ABE is inevitable
4405
4406 and then not ABE_Is_Certain (N)
4407
4408 -- Or if subprogram is eliminated
4409
4410 and then not Is_Eliminated (Subp)
4411 then
4412 Pending_Instantiations.Append
4413 ((Inst_Node => N,
4414 Act_Decl => Unit_Declaration_Node (Subp),
4415 Expander_Status => Expander_Active,
4416 Current_Sem_Unit => Current_Sem_Unit,
4417 Scope_Suppress => Scope_Suppress,
4418 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4419 Version => Ada_Version,
4420 Version_Pragma => Ada_Version_Pragma,
4421 Warnings => Save_Warnings));
4422 return True;
4423
4424 -- Here if not inlined, or we ignore the inlining
4425
4426 else
4427 return False;
4428 end if;
4429 end Need_Subprogram_Instance_Body;
4430
4431 --------------------------------------
4432 -- Analyze_Subprogram_Instantiation --
4433 --------------------------------------
4434
4435 procedure Analyze_Subprogram_Instantiation
4436 (N : Node_Id;
4437 K : Entity_Kind)
4438 is
4439 Loc : constant Source_Ptr := Sloc (N);
4440 Gen_Id : constant Node_Id := Name (N);
4441
4442 Anon_Id : constant Entity_Id :=
4443 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4444 Chars => New_External_Name
4445 (Chars (Defining_Entity (N)), 'R'));
4446
4447 Act_Decl_Id : Entity_Id;
4448 Act_Decl : Node_Id;
4449 Act_Spec : Node_Id;
4450 Act_Tree : Node_Id;
4451
4452 Env_Installed : Boolean := False;
4453 Gen_Unit : Entity_Id;
4454 Gen_Decl : Node_Id;
4455 Pack_Id : Entity_Id;
4456 Parent_Installed : Boolean := False;
4457 Renaming_List : List_Id;
4458
4459 procedure Analyze_Instance_And_Renamings;
4460 -- The instance must be analyzed in a context that includes the mappings
4461 -- of generic parameters into actuals. We create a package declaration
4462 -- for this purpose, and a subprogram with an internal name within the
4463 -- package. The subprogram instance is simply an alias for the internal
4464 -- subprogram, declared in the current scope.
4465
4466 ------------------------------------
4467 -- Analyze_Instance_And_Renamings --
4468 ------------------------------------
4469
4470 procedure Analyze_Instance_And_Renamings is
4471 Def_Ent : constant Entity_Id := Defining_Entity (N);
4472 Pack_Decl : Node_Id;
4473
4474 begin
4475 if Nkind (Parent (N)) = N_Compilation_Unit then
4476
4477 -- For the case of a compilation unit, the container package has
4478 -- the same name as the instantiation, to insure that the binder
4479 -- calls the elaboration procedure with the right name. Copy the
4480 -- entity of the instance, which may have compilation level flags
4481 -- (e.g. Is_Child_Unit) set.
4482
4483 Pack_Id := New_Copy (Def_Ent);
4484
4485 else
4486 -- Otherwise we use the name of the instantiation concatenated
4487 -- with its source position to ensure uniqueness if there are
4488 -- several instantiations with the same name.
4489
4490 Pack_Id :=
4491 Make_Defining_Identifier (Loc,
4492 Chars => New_External_Name
4493 (Related_Id => Chars (Def_Ent),
4494 Suffix => "GP",
4495 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4496 end if;
4497
4498 Pack_Decl := Make_Package_Declaration (Loc,
4499 Specification => Make_Package_Specification (Loc,
4500 Defining_Unit_Name => Pack_Id,
4501 Visible_Declarations => Renaming_List,
4502 End_Label => Empty));
4503
4504 Set_Instance_Spec (N, Pack_Decl);
4505 Set_Is_Generic_Instance (Pack_Id);
4506 Set_Debug_Info_Needed (Pack_Id);
4507
4508 -- Case of not a compilation unit
4509
4510 if Nkind (Parent (N)) /= N_Compilation_Unit then
4511 Mark_Rewrite_Insertion (Pack_Decl);
4512 Insert_Before (N, Pack_Decl);
4513 Set_Has_Completion (Pack_Id);
4514
4515 -- Case of an instantiation that is a compilation unit
4516
4517 -- Place declaration on current node so context is complete for
4518 -- analysis (including nested instantiations), and for use in a
4519 -- context_clause (see Analyze_With_Clause).
4520
4521 else
4522 Set_Unit (Parent (N), Pack_Decl);
4523 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4524 end if;
4525
4526 Analyze (Pack_Decl);
4527 Check_Formal_Packages (Pack_Id);
4528 Set_Is_Generic_Instance (Pack_Id, False);
4529
4530 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4531 -- above???
4532
4533 -- Body of the enclosing package is supplied when instantiating the
4534 -- subprogram body, after semantic analysis is completed.
4535
4536 if Nkind (Parent (N)) = N_Compilation_Unit then
4537
4538 -- Remove package itself from visibility, so it does not
4539 -- conflict with subprogram.
4540
4541 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4542
4543 -- Set name and scope of internal subprogram so that the proper
4544 -- external name will be generated. The proper scope is the scope
4545 -- of the wrapper package. We need to generate debugging info for
4546 -- the internal subprogram, so set flag accordingly.
4547
4548 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4549 Set_Scope (Anon_Id, Scope (Pack_Id));
4550
4551 -- Mark wrapper package as referenced, to avoid spurious warnings
4552 -- if the instantiation appears in various with_ clauses of
4553 -- subunits of the main unit.
4554
4555 Set_Referenced (Pack_Id);
4556 end if;
4557
4558 Set_Is_Generic_Instance (Anon_Id);
4559 Set_Debug_Info_Needed (Anon_Id);
4560 Act_Decl_Id := New_Copy (Anon_Id);
4561
4562 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4563 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4564 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4565 Set_Comes_From_Source (Act_Decl_Id, True);
4566
4567 -- The signature may involve types that are not frozen yet, but the
4568 -- subprogram will be frozen at the point the wrapper package is
4569 -- frozen, so it does not need its own freeze node. In fact, if one
4570 -- is created, it might conflict with the freezing actions from the
4571 -- wrapper package.
4572
4573 Set_Has_Delayed_Freeze (Anon_Id, False);
4574
4575 -- If the instance is a child unit, mark the Id accordingly. Mark
4576 -- the anonymous entity as well, which is the real subprogram and
4577 -- which is used when the instance appears in a context clause.
4578 -- Similarly, propagate the Is_Eliminated flag to handle properly
4579 -- nested eliminated subprograms.
4580
4581 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4582 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4583 New_Overloaded_Entity (Act_Decl_Id);
4584 Check_Eliminated (Act_Decl_Id);
4585 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4586
4587 -- In compilation unit case, kill elaboration checks on the
4588 -- instantiation, since they are never needed -- the body is
4589 -- instantiated at the same point as the spec.
4590
4591 if Nkind (Parent (N)) = N_Compilation_Unit then
4592 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4593 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4594 Set_Is_Compilation_Unit (Anon_Id);
4595
4596 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4597 end if;
4598
4599 -- The instance is not a freezing point for the new subprogram
4600
4601 Set_Is_Frozen (Act_Decl_Id, False);
4602
4603 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4604 Valid_Operator_Definition (Act_Decl_Id);
4605 end if;
4606
4607 Set_Alias (Act_Decl_Id, Anon_Id);
4608 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4609 Set_Has_Completion (Act_Decl_Id);
4610 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4611
4612 if Nkind (Parent (N)) = N_Compilation_Unit then
4613 Set_Body_Required (Parent (N), False);
4614 end if;
4615 end Analyze_Instance_And_Renamings;
4616
4617 -- Local variables
4618
4619 Vis_Prims_List : Elist_Id := No_Elist;
4620 -- List of primitives made temporarily visible in the instantiation
4621 -- to match the visibility of the formal type
4622
4623 -- Start of processing for Analyze_Subprogram_Instantiation
4624
4625 begin
4626 Check_SPARK_Restriction ("generic is not allowed", N);
4627
4628 -- Very first thing: apply the special kludge for Text_IO processing
4629 -- in case we are instantiating one of the children of [Wide_]Text_IO.
4630 -- Of course such an instantiation is bogus (these are packages, not
4631 -- subprograms), but we get a better error message if we do this.
4632
4633 Text_IO_Kludge (Gen_Id);
4634
4635 -- Make node global for error reporting
4636
4637 Instantiation_Node := N;
4638
4639 -- For package instantiations we turn off style checks, because they
4640 -- will have been emitted in the generic. For subprogram instantiations
4641 -- we want to apply at least the check on overriding indicators so we
4642 -- do not modify the style check status.
4643
4644 -- The renaming declarations for the actuals do not come from source and
4645 -- will not generate spurious warnings.
4646
4647 Preanalyze_Actuals (N);
4648
4649 Init_Env;
4650 Env_Installed := True;
4651 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4652 Gen_Unit := Entity (Gen_Id);
4653
4654 Generate_Reference (Gen_Unit, Gen_Id);
4655
4656 if Nkind (Gen_Id) = N_Identifier
4657 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4658 then
4659 Error_Msg_NE
4660 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4661 end if;
4662
4663 if Etype (Gen_Unit) = Any_Type then
4664 Restore_Env;
4665 return;
4666 end if;
4667
4668 -- Verify that it is a generic subprogram of the right kind, and that
4669 -- it does not lead to a circular instantiation.
4670
4671 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
4672 Error_Msg_NE
4673 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
4674
4675 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
4676 Error_Msg_NE
4677 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
4678
4679 elsif In_Open_Scopes (Gen_Unit) then
4680 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4681
4682 else
4683 Set_Entity (Gen_Id, Gen_Unit);
4684 Set_Is_Instantiated (Gen_Unit);
4685
4686 if In_Extended_Main_Source_Unit (N) then
4687 Generate_Reference (Gen_Unit, N);
4688 end if;
4689
4690 -- If renaming, get original unit
4691
4692 if Present (Renamed_Object (Gen_Unit))
4693 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4694 or else
4695 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4696 then
4697 Gen_Unit := Renamed_Object (Gen_Unit);
4698 Set_Is_Instantiated (Gen_Unit);
4699 Generate_Reference (Gen_Unit, N);
4700 end if;
4701
4702 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4703 Error_Msg_Node_2 := Current_Scope;
4704 Error_Msg_NE
4705 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4706 Circularity_Detected := True;
4707 Restore_Hidden_Primitives (Vis_Prims_List);
4708 goto Leave;
4709 end if;
4710
4711 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4712
4713 -- Initialize renamings map, for error checking
4714
4715 Generic_Renamings.Set_Last (0);
4716 Generic_Renamings_HTable.Reset;
4717
4718 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4719
4720 -- Copy original generic tree, to produce text for instantiation
4721
4722 Act_Tree :=
4723 Copy_Generic_Node
4724 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4725
4726 -- Inherit overriding indicator from instance node
4727
4728 Act_Spec := Specification (Act_Tree);
4729 Set_Must_Override (Act_Spec, Must_Override (N));
4730 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4731
4732 Renaming_List :=
4733 Analyze_Associations
4734 (I_Node => N,
4735 Formals => Generic_Formal_Declarations (Act_Tree),
4736 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4737
4738 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4739
4740 -- The subprogram itself cannot contain a nested instance, so the
4741 -- current parent is left empty.
4742
4743 Set_Instance_Env (Gen_Unit, Empty);
4744
4745 -- Build the subprogram declaration, which does not appear in the
4746 -- generic template, and give it a sloc consistent with that of the
4747 -- template.
4748
4749 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4750 Set_Generic_Parent (Act_Spec, Gen_Unit);
4751 Act_Decl :=
4752 Make_Subprogram_Declaration (Sloc (Act_Spec),
4753 Specification => Act_Spec);
4754
4755 -- The aspects have been copied previously, but they have to be
4756 -- linked explicitly to the new subprogram declaration. Explicit
4757 -- pre/postconditions on the instance are analyzed below, in a
4758 -- separate step.
4759
4760 Move_Aspects (Act_Tree, To => Act_Decl);
4761 Set_Categorization_From_Pragmas (Act_Decl);
4762
4763 if Parent_Installed then
4764 Hide_Current_Scope;
4765 end if;
4766
4767 Append (Act_Decl, Renaming_List);
4768 Analyze_Instance_And_Renamings;
4769
4770 -- If the generic is marked Import (Intrinsic), then so is the
4771 -- instance. This indicates that there is no body to instantiate. If
4772 -- generic is marked inline, so it the instance, and the anonymous
4773 -- subprogram it renames. If inlined, or else if inlining is enabled
4774 -- for the compilation, we generate the instance body even if it is
4775 -- not within the main unit.
4776
4777 if Is_Intrinsic_Subprogram (Gen_Unit) then
4778 Set_Is_Intrinsic_Subprogram (Anon_Id);
4779 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
4780
4781 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
4782 Validate_Unchecked_Conversion (N, Act_Decl_Id);
4783 end if;
4784 end if;
4785
4786 -- Inherit convention from generic unit. Intrinsic convention, as for
4787 -- an instance of unchecked conversion, is not inherited because an
4788 -- explicit Ada instance has been created.
4789
4790 if Has_Convention_Pragma (Gen_Unit)
4791 and then Convention (Gen_Unit) /= Convention_Intrinsic
4792 then
4793 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
4794 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
4795 end if;
4796
4797 Generate_Definition (Act_Decl_Id);
4798 -- Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id)));
4799 -- ??? needed?
4800 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
4801
4802 -- Inherit all inlining-related flags which apply to the generic in
4803 -- the subprogram and its declaration.
4804
4805 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
4806 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
4807
4808 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
4809 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
4810
4811 Set_Has_Pragma_Inline_Always
4812 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
4813 Set_Has_Pragma_Inline_Always
4814 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
4815
4816 if not Is_Intrinsic_Subprogram (Gen_Unit) then
4817 Check_Elab_Instantiation (N);
4818 end if;
4819
4820 if Is_Dispatching_Operation (Act_Decl_Id)
4821 and then Ada_Version >= Ada_2005
4822 then
4823 declare
4824 Formal : Entity_Id;
4825
4826 begin
4827 Formal := First_Formal (Act_Decl_Id);
4828 while Present (Formal) loop
4829 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
4830 and then Is_Controlling_Formal (Formal)
4831 and then not Can_Never_Be_Null (Formal)
4832 then
4833 Error_Msg_NE ("access parameter& is controlling,",
4834 N, Formal);
4835 Error_Msg_NE
4836 ("\corresponding parameter of & must be"
4837 & " explicitly null-excluding", N, Gen_Id);
4838 end if;
4839
4840 Next_Formal (Formal);
4841 end loop;
4842 end;
4843 end if;
4844
4845 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4846
4847 Validate_Categorization_Dependency (N, Act_Decl_Id);
4848
4849 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
4850 Inherit_Context (Gen_Decl, N);
4851
4852 Restore_Private_Views (Pack_Id, False);
4853
4854 -- If the context requires a full instantiation, mark node for
4855 -- subsequent construction of the body.
4856
4857 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
4858 Check_Forward_Instantiation (Gen_Decl);
4859
4860 -- The wrapper package is always delayed, because it does not
4861 -- constitute a freeze point, but to insure that the freeze
4862 -- node is placed properly, it is created directly when
4863 -- instantiating the body (otherwise the freeze node might
4864 -- appear to early for nested instantiations).
4865
4866 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4867
4868 -- For ASIS purposes, indicate that the wrapper package has
4869 -- replaced the instantiation node.
4870
4871 Rewrite (N, Unit (Parent (N)));
4872 Set_Unit (Parent (N), N);
4873 end if;
4874
4875 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4876
4877 -- Replace instance node for library-level instantiations of
4878 -- intrinsic subprograms, for ASIS use.
4879
4880 Rewrite (N, Unit (Parent (N)));
4881 Set_Unit (Parent (N), N);
4882 end if;
4883
4884 if Parent_Installed then
4885 Remove_Parent;
4886 end if;
4887
4888 Restore_Hidden_Primitives (Vis_Prims_List);
4889 Restore_Env;
4890 Env_Installed := False;
4891 Generic_Renamings.Set_Last (0);
4892 Generic_Renamings_HTable.Reset;
4893 end if;
4894
4895 <<Leave>>
4896 if Has_Aspects (N) then
4897 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4898 end if;
4899
4900 exception
4901 when Instantiation_Error =>
4902 if Parent_Installed then
4903 Remove_Parent;
4904 end if;
4905
4906 if Env_Installed then
4907 Restore_Env;
4908 end if;
4909 end Analyze_Subprogram_Instantiation;
4910
4911 -------------------------
4912 -- Get_Associated_Node --
4913 -------------------------
4914
4915 function Get_Associated_Node (N : Node_Id) return Node_Id is
4916 Assoc : Node_Id;
4917
4918 begin
4919 Assoc := Associated_Node (N);
4920
4921 if Nkind (Assoc) /= Nkind (N) then
4922 return Assoc;
4923
4924 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
4925 return Assoc;
4926
4927 else
4928 -- If the node is part of an inner generic, it may itself have been
4929 -- remapped into a further generic copy. Associated_Node is otherwise
4930 -- used for the entity of the node, and will be of a different node
4931 -- kind, or else N has been rewritten as a literal or function call.
4932
4933 while Present (Associated_Node (Assoc))
4934 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
4935 loop
4936 Assoc := Associated_Node (Assoc);
4937 end loop;
4938
4939 -- Follow and additional link in case the final node was rewritten.
4940 -- This can only happen with nested generic units.
4941
4942 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
4943 and then Present (Associated_Node (Assoc))
4944 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
4945 N_Explicit_Dereference,
4946 N_Integer_Literal,
4947 N_Real_Literal,
4948 N_String_Literal))
4949 then
4950 Assoc := Associated_Node (Assoc);
4951 end if;
4952
4953 -- An additional special case: an unconstrained type in an object
4954 -- declaration may have been rewritten as a local subtype constrained
4955 -- by the expression in the declaration. We need to recover the
4956 -- original entity which may be global.
4957
4958 if Present (Original_Node (Assoc))
4959 and then Nkind (Parent (N)) = N_Object_Declaration
4960 then
4961 Assoc := Original_Node (Assoc);
4962 end if;
4963
4964 return Assoc;
4965 end if;
4966 end Get_Associated_Node;
4967
4968 -------------------------------------------
4969 -- Build_Instance_Compilation_Unit_Nodes --
4970 -------------------------------------------
4971
4972 procedure Build_Instance_Compilation_Unit_Nodes
4973 (N : Node_Id;
4974 Act_Body : Node_Id;
4975 Act_Decl : Node_Id)
4976 is
4977 Decl_Cunit : Node_Id;
4978 Body_Cunit : Node_Id;
4979 Citem : Node_Id;
4980 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
4981 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
4982
4983 begin
4984 -- A new compilation unit node is built for the instance declaration
4985
4986 Decl_Cunit :=
4987 Make_Compilation_Unit (Sloc (N),
4988 Context_Items => Empty_List,
4989 Unit => Act_Decl,
4990 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
4991
4992 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4993
4994 -- The new compilation unit is linked to its body, but both share the
4995 -- same file, so we do not set Body_Required on the new unit so as not
4996 -- to create a spurious dependency on a non-existent body in the ali.
4997 -- This simplifies CodePeer unit traversal.
4998
4999 -- We use the original instantiation compilation unit as the resulting
5000 -- compilation unit of the instance, since this is the main unit.
5001
5002 Rewrite (N, Act_Body);
5003 Body_Cunit := Parent (N);
5004
5005 -- The two compilation unit nodes are linked by the Library_Unit field
5006
5007 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5008 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5009
5010 -- Preserve the private nature of the package if needed
5011
5012 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5013
5014 -- If the instance is not the main unit, its context, categorization
5015 -- and elaboration entity are not relevant to the compilation.
5016
5017 if Body_Cunit /= Cunit (Main_Unit) then
5018 Make_Instance_Unit (Body_Cunit, In_Main => False);
5019 return;
5020 end if;
5021
5022 -- The context clause items on the instantiation, which are now attached
5023 -- to the body compilation unit (since the body overwrote the original
5024 -- instantiation node), semantically belong on the spec, so copy them
5025 -- there. It's harmless to leave them on the body as well. In fact one
5026 -- could argue that they belong in both places.
5027
5028 Citem := First (Context_Items (Body_Cunit));
5029 while Present (Citem) loop
5030 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5031 Next (Citem);
5032 end loop;
5033
5034 -- Propagate categorization flags on packages, so that they appear in
5035 -- the ali file for the spec of the unit.
5036
5037 if Ekind (New_Main) = E_Package then
5038 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5039 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5040 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5041 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5042 Set_Is_Remote_Call_Interface
5043 (Old_Main, Is_Remote_Call_Interface (New_Main));
5044 end if;
5045
5046 -- Make entry in Units table, so that binder can generate call to
5047 -- elaboration procedure for body, if any.
5048
5049 Make_Instance_Unit (Body_Cunit, In_Main => True);
5050 Main_Unit_Entity := New_Main;
5051 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5052
5053 -- Build elaboration entity, since the instance may certainly generate
5054 -- elaboration code requiring a flag for protection.
5055
5056 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5057 end Build_Instance_Compilation_Unit_Nodes;
5058
5059 -----------------------------
5060 -- Check_Access_Definition --
5061 -----------------------------
5062
5063 procedure Check_Access_Definition (N : Node_Id) is
5064 begin
5065 pragma Assert
5066 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5067 null;
5068 end Check_Access_Definition;
5069
5070 -----------------------------------
5071 -- Check_Formal_Package_Instance --
5072 -----------------------------------
5073
5074 -- If the formal has specific parameters, they must match those of the
5075 -- actual. Both of them are instances, and the renaming declarations for
5076 -- their formal parameters appear in the same order in both. The analyzed
5077 -- formal has been analyzed in the context of the current instance.
5078
5079 procedure Check_Formal_Package_Instance
5080 (Formal_Pack : Entity_Id;
5081 Actual_Pack : Entity_Id)
5082 is
5083 E1 : Entity_Id := First_Entity (Actual_Pack);
5084 E2 : Entity_Id := First_Entity (Formal_Pack);
5085
5086 Expr1 : Node_Id;
5087 Expr2 : Node_Id;
5088
5089 procedure Check_Mismatch (B : Boolean);
5090 -- Common error routine for mismatch between the parameters of the
5091 -- actual instance and those of the formal package.
5092
5093 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5094 -- The formal may come from a nested formal package, and the actual may
5095 -- have been constant-folded. To determine whether the two denote the
5096 -- same entity we may have to traverse several definitions to recover
5097 -- the ultimate entity that they refer to.
5098
5099 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5100 -- Similarly, if the formal comes from a nested formal package, the
5101 -- actual may designate the formal through multiple renamings, which
5102 -- have to be followed to determine the original variable in question.
5103
5104 --------------------
5105 -- Check_Mismatch --
5106 --------------------
5107
5108 procedure Check_Mismatch (B : Boolean) is
5109 Kind : constant Node_Kind := Nkind (Parent (E2));
5110
5111 begin
5112 if Kind = N_Formal_Type_Declaration then
5113 return;
5114
5115 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5116 N_Formal_Package_Declaration)
5117 or else Kind in N_Formal_Subprogram_Declaration
5118 then
5119 null;
5120
5121 elsif B then
5122 Error_Msg_NE
5123 ("actual for & in actual instance does not match formal",
5124 Parent (Actual_Pack), E1);
5125 end if;
5126 end Check_Mismatch;
5127
5128 --------------------------------
5129 -- Same_Instantiated_Constant --
5130 --------------------------------
5131
5132 function Same_Instantiated_Constant
5133 (E1, E2 : Entity_Id) return Boolean
5134 is
5135 Ent : Entity_Id;
5136
5137 begin
5138 Ent := E2;
5139 while Present (Ent) loop
5140 if E1 = Ent then
5141 return True;
5142
5143 elsif Ekind (Ent) /= E_Constant then
5144 return False;
5145
5146 elsif Is_Entity_Name (Constant_Value (Ent)) then
5147 if Entity (Constant_Value (Ent)) = E1 then
5148 return True;
5149 else
5150 Ent := Entity (Constant_Value (Ent));
5151 end if;
5152
5153 -- The actual may be a constant that has been folded. Recover
5154 -- original name.
5155
5156 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5157 Ent := Entity (Original_Node (Constant_Value (Ent)));
5158 else
5159 return False;
5160 end if;
5161 end loop;
5162
5163 return False;
5164 end Same_Instantiated_Constant;
5165
5166 --------------------------------
5167 -- Same_Instantiated_Variable --
5168 --------------------------------
5169
5170 function Same_Instantiated_Variable
5171 (E1, E2 : Entity_Id) return Boolean
5172 is
5173 function Original_Entity (E : Entity_Id) return Entity_Id;
5174 -- Follow chain of renamings to the ultimate ancestor
5175
5176 ---------------------
5177 -- Original_Entity --
5178 ---------------------
5179
5180 function Original_Entity (E : Entity_Id) return Entity_Id is
5181 Orig : Entity_Id;
5182
5183 begin
5184 Orig := E;
5185 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5186 and then Present (Renamed_Object (Orig))
5187 and then Is_Entity_Name (Renamed_Object (Orig))
5188 loop
5189 Orig := Entity (Renamed_Object (Orig));
5190 end loop;
5191
5192 return Orig;
5193 end Original_Entity;
5194
5195 -- Start of processing for Same_Instantiated_Variable
5196
5197 begin
5198 return Ekind (E1) = Ekind (E2)
5199 and then Original_Entity (E1) = Original_Entity (E2);
5200 end Same_Instantiated_Variable;
5201
5202 -- Start of processing for Check_Formal_Package_Instance
5203
5204 begin
5205 while Present (E1)
5206 and then Present (E2)
5207 loop
5208 exit when Ekind (E1) = E_Package
5209 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5210
5211 -- If the formal is the renaming of the formal package, this
5212 -- is the end of its formal part, which may occur before the
5213 -- end of the formal part in the actual in the presence of
5214 -- defaulted parameters in the formal package.
5215
5216 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5217 and then Renamed_Entity (E2) = Scope (E2);
5218
5219 -- The analysis of the actual may generate additional internal
5220 -- entities. If the formal is defaulted, there is no corresponding
5221 -- analysis and the internal entities must be skipped, until we
5222 -- find corresponding entities again.
5223
5224 if Comes_From_Source (E2)
5225 and then not Comes_From_Source (E1)
5226 and then Chars (E1) /= Chars (E2)
5227 then
5228 while Present (E1)
5229 and then Chars (E1) /= Chars (E2)
5230 loop
5231 Next_Entity (E1);
5232 end loop;
5233 end if;
5234
5235 if No (E1) then
5236 return;
5237
5238 -- If the formal entity comes from a formal declaration, it was
5239 -- defaulted in the formal package, and no check is needed on it.
5240
5241 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5242 goto Next_E;
5243
5244 -- Ditto for defaulted formal subprograms.
5245
5246 elsif Is_Overloadable (E1)
5247 and then Nkind (Unit_Declaration_Node (E2)) in
5248 N_Formal_Subprogram_Declaration
5249 then
5250 goto Next_E;
5251
5252 elsif Is_Type (E1) then
5253
5254 -- Subtypes must statically match. E1, E2 are the local entities
5255 -- that are subtypes of the actuals. Itypes generated for other
5256 -- parameters need not be checked, the check will be performed
5257 -- on the parameters themselves.
5258
5259 -- If E2 is a formal type declaration, it is a defaulted parameter
5260 -- and needs no checking.
5261
5262 if not Is_Itype (E1)
5263 and then not Is_Itype (E2)
5264 then
5265 Check_Mismatch
5266 (not Is_Type (E2)
5267 or else Etype (E1) /= Etype (E2)
5268 or else not Subtypes_Statically_Match (E1, E2));
5269 end if;
5270
5271 elsif Ekind (E1) = E_Constant then
5272
5273 -- IN parameters must denote the same static value, or the same
5274 -- constant, or the literal null.
5275
5276 Expr1 := Expression (Parent (E1));
5277
5278 if Ekind (E2) /= E_Constant then
5279 Check_Mismatch (True);
5280 goto Next_E;
5281 else
5282 Expr2 := Expression (Parent (E2));
5283 end if;
5284
5285 if Is_Static_Expression (Expr1) then
5286
5287 if not Is_Static_Expression (Expr2) then
5288 Check_Mismatch (True);
5289
5290 elsif Is_Discrete_Type (Etype (E1)) then
5291 declare
5292 V1 : constant Uint := Expr_Value (Expr1);
5293 V2 : constant Uint := Expr_Value (Expr2);
5294 begin
5295 Check_Mismatch (V1 /= V2);
5296 end;
5297
5298 elsif Is_Real_Type (Etype (E1)) then
5299 declare
5300 V1 : constant Ureal := Expr_Value_R (Expr1);
5301 V2 : constant Ureal := Expr_Value_R (Expr2);
5302 begin
5303 Check_Mismatch (V1 /= V2);
5304 end;
5305
5306 elsif Is_String_Type (Etype (E1))
5307 and then Nkind (Expr1) = N_String_Literal
5308 then
5309 if Nkind (Expr2) /= N_String_Literal then
5310 Check_Mismatch (True);
5311 else
5312 Check_Mismatch
5313 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5314 end if;
5315 end if;
5316
5317 elsif Is_Entity_Name (Expr1) then
5318 if Is_Entity_Name (Expr2) then
5319 if Entity (Expr1) = Entity (Expr2) then
5320 null;
5321 else
5322 Check_Mismatch
5323 (not Same_Instantiated_Constant
5324 (Entity (Expr1), Entity (Expr2)));
5325 end if;
5326 else
5327 Check_Mismatch (True);
5328 end if;
5329
5330 elsif Is_Entity_Name (Original_Node (Expr1))
5331 and then Is_Entity_Name (Expr2)
5332 and then
5333 Same_Instantiated_Constant
5334 (Entity (Original_Node (Expr1)), Entity (Expr2))
5335 then
5336 null;
5337
5338 elsif Nkind (Expr1) = N_Null then
5339 Check_Mismatch (Nkind (Expr1) /= N_Null);
5340
5341 else
5342 Check_Mismatch (True);
5343 end if;
5344
5345 elsif Ekind (E1) = E_Variable then
5346 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5347
5348 elsif Ekind (E1) = E_Package then
5349 Check_Mismatch
5350 (Ekind (E1) /= Ekind (E2)
5351 or else Renamed_Object (E1) /= Renamed_Object (E2));
5352
5353 elsif Is_Overloadable (E1) then
5354
5355 -- Verify that the actual subprograms match. Note that actuals
5356 -- that are attributes are rewritten as subprograms. If the
5357 -- subprogram in the formal package is defaulted, no check is
5358 -- needed. Note that this can only happen in Ada 2005 when the
5359 -- formal package can be partially parameterized.
5360
5361 if Nkind (Unit_Declaration_Node (E1)) =
5362 N_Subprogram_Renaming_Declaration
5363 and then From_Default (Unit_Declaration_Node (E1))
5364 then
5365 null;
5366
5367 -- If the formal package has an "others" box association that
5368 -- covers this formal, there is no need for a check either.
5369
5370 elsif Nkind (Unit_Declaration_Node (E2)) in
5371 N_Formal_Subprogram_Declaration
5372 and then Box_Present (Unit_Declaration_Node (E2))
5373 then
5374 null;
5375
5376 -- No check needed if subprogram is a defaulted null procedure
5377
5378 elsif No (Alias (E2))
5379 and then Ekind (E2) = E_Procedure
5380 and then
5381 Null_Present (Specification (Unit_Declaration_Node (E2)))
5382 then
5383 null;
5384
5385 -- Otherwise the actual in the formal and the actual in the
5386 -- instantiation of the formal must match, up to renamings.
5387
5388 else
5389 Check_Mismatch
5390 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5391 end if;
5392
5393 else
5394 raise Program_Error;
5395 end if;
5396
5397 <<Next_E>>
5398 Next_Entity (E1);
5399 Next_Entity (E2);
5400 end loop;
5401 end Check_Formal_Package_Instance;
5402
5403 ---------------------------
5404 -- Check_Formal_Packages --
5405 ---------------------------
5406
5407 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5408 E : Entity_Id;
5409 Formal_P : Entity_Id;
5410
5411 begin
5412 -- Iterate through the declarations in the instance, looking for package
5413 -- renaming declarations that denote instances of formal packages. Stop
5414 -- when we find the renaming of the current package itself. The
5415 -- declaration for a formal package without a box is followed by an
5416 -- internal entity that repeats the instantiation.
5417
5418 E := First_Entity (P_Id);
5419 while Present (E) loop
5420 if Ekind (E) = E_Package then
5421 if Renamed_Object (E) = P_Id then
5422 exit;
5423
5424 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5425 null;
5426
5427 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5428 Formal_P := Next_Entity (E);
5429 Check_Formal_Package_Instance (Formal_P, E);
5430
5431 -- After checking, remove the internal validating package. It
5432 -- is only needed for semantic checks, and as it may contain
5433 -- generic formal declarations it should not reach gigi.
5434
5435 Remove (Unit_Declaration_Node (Formal_P));
5436 end if;
5437 end if;
5438
5439 Next_Entity (E);
5440 end loop;
5441 end Check_Formal_Packages;
5442
5443 ---------------------------------
5444 -- Check_Forward_Instantiation --
5445 ---------------------------------
5446
5447 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5448 S : Entity_Id;
5449 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5450
5451 begin
5452 -- The instantiation appears before the generic body if we are in the
5453 -- scope of the unit containing the generic, either in its spec or in
5454 -- the package body, and before the generic body.
5455
5456 if Ekind (Gen_Comp) = E_Package_Body then
5457 Gen_Comp := Spec_Entity (Gen_Comp);
5458 end if;
5459
5460 if In_Open_Scopes (Gen_Comp)
5461 and then No (Corresponding_Body (Decl))
5462 then
5463 S := Current_Scope;
5464
5465 while Present (S)
5466 and then not Is_Compilation_Unit (S)
5467 and then not Is_Child_Unit (S)
5468 loop
5469 if Ekind (S) = E_Package then
5470 Set_Has_Forward_Instantiation (S);
5471 end if;
5472
5473 S := Scope (S);
5474 end loop;
5475 end if;
5476 end Check_Forward_Instantiation;
5477
5478 ---------------------------
5479 -- Check_Generic_Actuals --
5480 ---------------------------
5481
5482 -- The visibility of the actuals may be different between the point of
5483 -- generic instantiation and the instantiation of the body.
5484
5485 procedure Check_Generic_Actuals
5486 (Instance : Entity_Id;
5487 Is_Formal_Box : Boolean)
5488 is
5489 E : Entity_Id;
5490 Astype : Entity_Id;
5491
5492 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5493 -- For a formal that is an array type, the component type is often a
5494 -- previous formal in the same unit. The privacy status of the component
5495 -- type will have been examined earlier in the traversal of the
5496 -- corresponding actuals, and this status should not be modified for
5497 -- the array (sub)type itself. However, if the base type of the array
5498 -- (sub)type is private, its full view must be restored in the body to
5499 -- be consistent with subsequent index subtypes, etc.
5500 --
5501 -- To detect this case we have to rescan the list of formals, which is
5502 -- usually short enough to ignore the resulting inefficiency.
5503
5504 -----------------------------
5505 -- Denotes_Previous_Actual --
5506 -----------------------------
5507
5508 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5509 Prev : Entity_Id;
5510
5511 begin
5512 Prev := First_Entity (Instance);
5513 while Present (Prev) loop
5514 if Is_Type (Prev)
5515 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5516 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5517 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5518 then
5519 return True;
5520
5521 elsif Prev = E then
5522 return False;
5523
5524 else
5525 Next_Entity (Prev);
5526 end if;
5527 end loop;
5528
5529 return False;
5530 end Denotes_Previous_Actual;
5531
5532 -- Start of processing for Check_Generic_Actuals
5533
5534 begin
5535 E := First_Entity (Instance);
5536 while Present (E) loop
5537 if Is_Type (E)
5538 and then Nkind (Parent (E)) = N_Subtype_Declaration
5539 and then Scope (Etype (E)) /= Instance
5540 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5541 then
5542 if Is_Array_Type (E)
5543 and then not Is_Private_Type (Etype (E))
5544 and then Denotes_Previous_Actual (Component_Type (E))
5545 then
5546 null;
5547 else
5548 Check_Private_View (Subtype_Indication (Parent (E)));
5549 end if;
5550
5551 Set_Is_Generic_Actual_Type (E, True);
5552 Set_Is_Hidden (E, False);
5553 Set_Is_Potentially_Use_Visible (E,
5554 In_Use (Instance));
5555
5556 -- We constructed the generic actual type as a subtype of the
5557 -- supplied type. This means that it normally would not inherit
5558 -- subtype specific attributes of the actual, which is wrong for
5559 -- the generic case.
5560
5561 Astype := Ancestor_Subtype (E);
5562
5563 if No (Astype) then
5564
5565 -- This can happen when E is an itype that is the full view of
5566 -- a private type completed, e.g. with a constrained array. In
5567 -- that case, use the first subtype, which will carry size
5568 -- information. The base type itself is unconstrained and will
5569 -- not carry it.
5570
5571 Astype := First_Subtype (E);
5572 end if;
5573
5574 Set_Size_Info (E, (Astype));
5575 Set_RM_Size (E, RM_Size (Astype));
5576 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5577
5578 if Is_Discrete_Or_Fixed_Point_Type (E) then
5579 Set_RM_Size (E, RM_Size (Astype));
5580
5581 -- In nested instances, the base type of an access actual may
5582 -- itself be private, and need to be exchanged.
5583
5584 elsif Is_Access_Type (E)
5585 and then Is_Private_Type (Etype (E))
5586 then
5587 Check_Private_View
5588 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5589 end if;
5590
5591 elsif Ekind (E) = E_Package then
5592
5593 -- If this is the renaming for the current instance, we're done.
5594 -- Otherwise it is a formal package. If the corresponding formal
5595 -- was declared with a box, the (instantiations of the) generic
5596 -- formal part are also visible. Otherwise, ignore the entity
5597 -- created to validate the actuals.
5598
5599 if Renamed_Object (E) = Instance then
5600 exit;
5601
5602 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5603 null;
5604
5605 -- The visibility of a formal of an enclosing generic is already
5606 -- correct.
5607
5608 elsif Denotes_Formal_Package (E) then
5609 null;
5610
5611 elsif Present (Associated_Formal_Package (E))
5612 and then not Is_Generic_Formal (E)
5613 then
5614 if Box_Present (Parent (Associated_Formal_Package (E))) then
5615 Check_Generic_Actuals (Renamed_Object (E), True);
5616
5617 else
5618 Check_Generic_Actuals (Renamed_Object (E), False);
5619 end if;
5620
5621 Set_Is_Hidden (E, False);
5622 end if;
5623
5624 -- If this is a subprogram instance (in a wrapper package) the
5625 -- actual is fully visible.
5626
5627 elsif Is_Wrapper_Package (Instance) then
5628 Set_Is_Hidden (E, False);
5629
5630 -- If the formal package is declared with a box, or if the formal
5631 -- parameter is defaulted, it is visible in the body.
5632
5633 elsif Is_Formal_Box
5634 or else Is_Visible_Formal (E)
5635 then
5636 Set_Is_Hidden (E, False);
5637 end if;
5638
5639 if Ekind (E) = E_Constant then
5640
5641 -- If the type of the actual is a private type declared in the
5642 -- enclosing scope of the generic unit, the body of the generic
5643 -- sees the full view of the type (because it has to appear in
5644 -- the corresponding package body). If the type is private now,
5645 -- exchange views to restore the proper visiblity in the instance.
5646
5647 declare
5648 Typ : constant Entity_Id := Base_Type (Etype (E));
5649 -- The type of the actual
5650
5651 Gen_Id : Entity_Id;
5652 -- The generic unit
5653
5654 Parent_Scope : Entity_Id;
5655 -- The enclosing scope of the generic unit
5656
5657 begin
5658 if Is_Wrapper_Package (Instance) then
5659 Gen_Id :=
5660 Generic_Parent
5661 (Specification
5662 (Unit_Declaration_Node
5663 (Related_Instance (Instance))));
5664 else
5665 Gen_Id :=
5666 Generic_Parent (Package_Specification (Instance));
5667 end if;
5668
5669 Parent_Scope := Scope (Gen_Id);
5670
5671 -- The exchange is only needed if the generic is defined
5672 -- within a package which is not a common ancestor of the
5673 -- scope of the instance, and is not already in scope.
5674
5675 if Is_Private_Type (Typ)
5676 and then Scope (Typ) = Parent_Scope
5677 and then Scope (Instance) /= Parent_Scope
5678 and then Ekind (Parent_Scope) = E_Package
5679 and then not Is_Child_Unit (Gen_Id)
5680 then
5681 Switch_View (Typ);
5682
5683 -- If the type of the entity is a subtype, it may also have
5684 -- to be made visible, together with the base type of its
5685 -- full view, after exchange.
5686
5687 if Is_Private_Type (Etype (E)) then
5688 Switch_View (Etype (E));
5689 Switch_View (Base_Type (Etype (E)));
5690 end if;
5691 end if;
5692 end;
5693 end if;
5694
5695 Next_Entity (E);
5696 end loop;
5697 end Check_Generic_Actuals;
5698
5699 ------------------------------
5700 -- Check_Generic_Child_Unit --
5701 ------------------------------
5702
5703 procedure Check_Generic_Child_Unit
5704 (Gen_Id : Node_Id;
5705 Parent_Installed : in out Boolean)
5706 is
5707 Loc : constant Source_Ptr := Sloc (Gen_Id);
5708 Gen_Par : Entity_Id := Empty;
5709 E : Entity_Id;
5710 Inst_Par : Entity_Id;
5711 S : Node_Id;
5712
5713 function Find_Generic_Child
5714 (Scop : Entity_Id;
5715 Id : Node_Id) return Entity_Id;
5716 -- Search generic parent for possible child unit with the given name
5717
5718 function In_Enclosing_Instance return Boolean;
5719 -- Within an instance of the parent, the child unit may be denoted by
5720 -- a simple name, or an abbreviated expanded name. Examine enclosing
5721 -- scopes to locate a possible parent instantiation.
5722
5723 ------------------------
5724 -- Find_Generic_Child --
5725 ------------------------
5726
5727 function Find_Generic_Child
5728 (Scop : Entity_Id;
5729 Id : Node_Id) return Entity_Id
5730 is
5731 E : Entity_Id;
5732
5733 begin
5734 -- If entity of name is already set, instance has already been
5735 -- resolved, e.g. in an enclosing instantiation.
5736
5737 if Present (Entity (Id)) then
5738 if Scope (Entity (Id)) = Scop then
5739 return Entity (Id);
5740 else
5741 return Empty;
5742 end if;
5743
5744 else
5745 E := First_Entity (Scop);
5746 while Present (E) loop
5747 if Chars (E) = Chars (Id)
5748 and then Is_Child_Unit (E)
5749 then
5750 if Is_Child_Unit (E)
5751 and then not Is_Visible_Lib_Unit (E)
5752 then
5753 Error_Msg_NE
5754 ("generic child unit& is not visible", Gen_Id, E);
5755 end if;
5756
5757 Set_Entity (Id, E);
5758 return E;
5759 end if;
5760
5761 Next_Entity (E);
5762 end loop;
5763
5764 return Empty;
5765 end if;
5766 end Find_Generic_Child;
5767
5768 ---------------------------
5769 -- In_Enclosing_Instance --
5770 ---------------------------
5771
5772 function In_Enclosing_Instance return Boolean is
5773 Enclosing_Instance : Node_Id;
5774 Instance_Decl : Node_Id;
5775
5776 begin
5777 -- We do not inline any call that contains instantiations, except
5778 -- for instantiations of Unchecked_Conversion, so if we are within
5779 -- an inlined body the current instance does not require parents.
5780
5781 if In_Inlined_Body then
5782 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
5783 return False;
5784 end if;
5785
5786 -- Loop to check enclosing scopes
5787
5788 Enclosing_Instance := Current_Scope;
5789 while Present (Enclosing_Instance) loop
5790 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
5791
5792 if Ekind (Enclosing_Instance) = E_Package
5793 and then Is_Generic_Instance (Enclosing_Instance)
5794 and then Present
5795 (Generic_Parent (Specification (Instance_Decl)))
5796 then
5797 -- Check whether the generic we are looking for is a child of
5798 -- this instance.
5799
5800 E := Find_Generic_Child
5801 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
5802 exit when Present (E);
5803
5804 else
5805 E := Empty;
5806 end if;
5807
5808 Enclosing_Instance := Scope (Enclosing_Instance);
5809 end loop;
5810
5811 if No (E) then
5812
5813 -- Not a child unit
5814
5815 Analyze (Gen_Id);
5816 return False;
5817
5818 else
5819 Rewrite (Gen_Id,
5820 Make_Expanded_Name (Loc,
5821 Chars => Chars (E),
5822 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
5823 Selector_Name => New_Occurrence_Of (E, Loc)));
5824
5825 Set_Entity (Gen_Id, E);
5826 Set_Etype (Gen_Id, Etype (E));
5827 Parent_Installed := False; -- Already in scope.
5828 return True;
5829 end if;
5830 end In_Enclosing_Instance;
5831
5832 -- Start of processing for Check_Generic_Child_Unit
5833
5834 begin
5835 -- If the name of the generic is given by a selected component, it may
5836 -- be the name of a generic child unit, and the prefix is the name of an
5837 -- instance of the parent, in which case the child unit must be visible.
5838 -- If this instance is not in scope, it must be placed there and removed
5839 -- after instantiation, because what is being instantiated is not the
5840 -- original child, but the corresponding child present in the instance
5841 -- of the parent.
5842
5843 -- If the child is instantiated within the parent, it can be given by
5844 -- a simple name. In this case the instance is already in scope, but
5845 -- the child generic must be recovered from the generic parent as well.
5846
5847 if Nkind (Gen_Id) = N_Selected_Component then
5848 S := Selector_Name (Gen_Id);
5849 Analyze (Prefix (Gen_Id));
5850 Inst_Par := Entity (Prefix (Gen_Id));
5851
5852 if Ekind (Inst_Par) = E_Package
5853 and then Present (Renamed_Object (Inst_Par))
5854 then
5855 Inst_Par := Renamed_Object (Inst_Par);
5856 end if;
5857
5858 if Ekind (Inst_Par) = E_Package then
5859 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
5860 Gen_Par := Generic_Parent (Parent (Inst_Par));
5861
5862 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
5863 and then
5864 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
5865 then
5866 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
5867 end if;
5868
5869 elsif Ekind (Inst_Par) = E_Generic_Package
5870 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
5871 then
5872 -- A formal package may be a real child package, and not the
5873 -- implicit instance within a parent. In this case the child is
5874 -- not visible and has to be retrieved explicitly as well.
5875
5876 Gen_Par := Inst_Par;
5877 end if;
5878
5879 if Present (Gen_Par) then
5880
5881 -- The prefix denotes an instantiation. The entity itself may be a
5882 -- nested generic, or a child unit.
5883
5884 E := Find_Generic_Child (Gen_Par, S);
5885
5886 if Present (E) then
5887 Change_Selected_Component_To_Expanded_Name (Gen_Id);
5888 Set_Entity (Gen_Id, E);
5889 Set_Etype (Gen_Id, Etype (E));
5890 Set_Entity (S, E);
5891 Set_Etype (S, Etype (E));
5892
5893 -- Indicate that this is a reference to the parent
5894
5895 if In_Extended_Main_Source_Unit (Gen_Id) then
5896 Set_Is_Instantiated (Inst_Par);
5897 end if;
5898
5899 -- A common mistake is to replicate the naming scheme of a
5900 -- hierarchy by instantiating a generic child directly, rather
5901 -- than the implicit child in a parent instance:
5902
5903 -- generic .. package Gpar is ..
5904 -- generic .. package Gpar.Child is ..
5905 -- package Par is new Gpar ();
5906
5907 -- with Gpar.Child;
5908 -- package Par.Child is new Gpar.Child ();
5909 -- rather than Par.Child
5910
5911 -- In this case the instantiation is within Par, which is an
5912 -- instance, but Gpar does not denote Par because we are not IN
5913 -- the instance of Gpar, so this is illegal. The test below
5914 -- recognizes this particular case.
5915
5916 if Is_Child_Unit (E)
5917 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
5918 and then (not In_Instance
5919 or else Nkind (Parent (Parent (Gen_Id))) =
5920 N_Compilation_Unit)
5921 then
5922 Error_Msg_N
5923 ("prefix of generic child unit must be instance of parent",
5924 Gen_Id);
5925 end if;
5926
5927 if not In_Open_Scopes (Inst_Par)
5928 and then Nkind (Parent (Gen_Id)) not in
5929 N_Generic_Renaming_Declaration
5930 then
5931 Install_Parent (Inst_Par);
5932 Parent_Installed := True;
5933
5934 elsif In_Open_Scopes (Inst_Par) then
5935
5936 -- If the parent is already installed, install the actuals
5937 -- for its formal packages. This is necessary when the child
5938 -- instance is a child of the parent instance: in this case,
5939 -- the parent is placed on the scope stack but the formal
5940 -- packages are not made visible.
5941
5942 Install_Formal_Packages (Inst_Par);
5943 end if;
5944
5945 else
5946 -- If the generic parent does not contain an entity that
5947 -- corresponds to the selector, the instance doesn't either.
5948 -- Analyzing the node will yield the appropriate error message.
5949 -- If the entity is not a child unit, then it is an inner
5950 -- generic in the parent.
5951
5952 Analyze (Gen_Id);
5953 end if;
5954
5955 else
5956 Analyze (Gen_Id);
5957
5958 if Is_Child_Unit (Entity (Gen_Id))
5959 and then
5960 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5961 and then not In_Open_Scopes (Inst_Par)
5962 then
5963 Install_Parent (Inst_Par);
5964 Parent_Installed := True;
5965
5966 -- The generic unit may be the renaming of the implicit child
5967 -- present in an instance. In that case the parent instance is
5968 -- obtained from the name of the renamed entity.
5969
5970 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
5971 and then Present (Renamed_Entity (Entity (Gen_Id)))
5972 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
5973 then
5974 declare
5975 Renamed_Package : constant Node_Id :=
5976 Name (Parent (Entity (Gen_Id)));
5977 begin
5978 if Nkind (Renamed_Package) = N_Expanded_Name then
5979 Inst_Par := Entity (Prefix (Renamed_Package));
5980 Install_Parent (Inst_Par);
5981 Parent_Installed := True;
5982 end if;
5983 end;
5984 end if;
5985 end if;
5986
5987 elsif Nkind (Gen_Id) = N_Expanded_Name then
5988
5989 -- Entity already present, analyze prefix, whose meaning may be
5990 -- an instance in the current context. If it is an instance of
5991 -- a relative within another, the proper parent may still have
5992 -- to be installed, if they are not of the same generation.
5993
5994 Analyze (Prefix (Gen_Id));
5995
5996 -- In the unlikely case that a local declaration hides the name
5997 -- of the parent package, locate it on the homonym chain. If the
5998 -- context is an instance of the parent, the renaming entity is
5999 -- flagged as such.
6000
6001 Inst_Par := Entity (Prefix (Gen_Id));
6002 while Present (Inst_Par)
6003 and then not Is_Package_Or_Generic_Package (Inst_Par)
6004 loop
6005 Inst_Par := Homonym (Inst_Par);
6006 end loop;
6007
6008 pragma Assert (Present (Inst_Par));
6009 Set_Entity (Prefix (Gen_Id), Inst_Par);
6010
6011 if In_Enclosing_Instance then
6012 null;
6013
6014 elsif Present (Entity (Gen_Id))
6015 and then Is_Child_Unit (Entity (Gen_Id))
6016 and then not In_Open_Scopes (Inst_Par)
6017 then
6018 Install_Parent (Inst_Par);
6019 Parent_Installed := True;
6020 end if;
6021
6022 elsif In_Enclosing_Instance then
6023
6024 -- The child unit is found in some enclosing scope
6025
6026 null;
6027
6028 else
6029 Analyze (Gen_Id);
6030
6031 -- If this is the renaming of the implicit child in a parent
6032 -- instance, recover the parent name and install it.
6033
6034 if Is_Entity_Name (Gen_Id) then
6035 E := Entity (Gen_Id);
6036
6037 if Is_Generic_Unit (E)
6038 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6039 and then Is_Child_Unit (Renamed_Object (E))
6040 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6041 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6042 then
6043 Rewrite (Gen_Id,
6044 New_Copy_Tree (Name (Parent (E))));
6045 Inst_Par := Entity (Prefix (Gen_Id));
6046
6047 if not In_Open_Scopes (Inst_Par) then
6048 Install_Parent (Inst_Par);
6049 Parent_Installed := True;
6050 end if;
6051
6052 -- If it is a child unit of a non-generic parent, it may be
6053 -- use-visible and given by a direct name. Install parent as
6054 -- for other cases.
6055
6056 elsif Is_Generic_Unit (E)
6057 and then Is_Child_Unit (E)
6058 and then
6059 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6060 and then not Is_Generic_Unit (Scope (E))
6061 then
6062 if not In_Open_Scopes (Scope (E)) then
6063 Install_Parent (Scope (E));
6064 Parent_Installed := True;
6065 end if;
6066 end if;
6067 end if;
6068 end if;
6069 end Check_Generic_Child_Unit;
6070
6071 -----------------------------
6072 -- Check_Hidden_Child_Unit --
6073 -----------------------------
6074
6075 procedure Check_Hidden_Child_Unit
6076 (N : Node_Id;
6077 Gen_Unit : Entity_Id;
6078 Act_Decl_Id : Entity_Id)
6079 is
6080 Gen_Id : constant Node_Id := Name (N);
6081
6082 begin
6083 if Is_Child_Unit (Gen_Unit)
6084 and then Is_Child_Unit (Act_Decl_Id)
6085 and then Nkind (Gen_Id) = N_Expanded_Name
6086 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6087 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6088 then
6089 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6090 Error_Msg_NE
6091 ("generic unit & is implicitly declared in &",
6092 Defining_Unit_Name (N), Gen_Unit);
6093 Error_Msg_N ("\instance must have different name",
6094 Defining_Unit_Name (N));
6095 end if;
6096 end Check_Hidden_Child_Unit;
6097
6098 ------------------------
6099 -- Check_Private_View --
6100 ------------------------
6101
6102 procedure Check_Private_View (N : Node_Id) is
6103 T : constant Entity_Id := Etype (N);
6104 BT : Entity_Id;
6105
6106 begin
6107 -- Exchange views if the type was not private in the generic but is
6108 -- private at the point of instantiation. Do not exchange views if
6109 -- the scope of the type is in scope. This can happen if both generic
6110 -- and instance are sibling units, or if type is defined in a parent.
6111 -- In this case the visibility of the type will be correct for all
6112 -- semantic checks.
6113
6114 if Present (T) then
6115 BT := Base_Type (T);
6116
6117 if Is_Private_Type (T)
6118 and then not Has_Private_View (N)
6119 and then Present (Full_View (T))
6120 and then not In_Open_Scopes (Scope (T))
6121 then
6122 -- In the generic, the full type was visible. Save the private
6123 -- entity, for subsequent exchange.
6124
6125 Switch_View (T);
6126
6127 elsif Has_Private_View (N)
6128 and then not Is_Private_Type (T)
6129 and then not Has_Been_Exchanged (T)
6130 and then Etype (Get_Associated_Node (N)) /= T
6131 then
6132 -- Only the private declaration was visible in the generic. If
6133 -- the type appears in a subtype declaration, the subtype in the
6134 -- instance must have a view compatible with that of its parent,
6135 -- which must be exchanged (see corresponding code in Restore_
6136 -- Private_Views). Otherwise, if the type is defined in a parent
6137 -- unit, leave full visibility within instance, which is safe.
6138
6139 if In_Open_Scopes (Scope (Base_Type (T)))
6140 and then not Is_Private_Type (Base_Type (T))
6141 and then Comes_From_Source (Base_Type (T))
6142 then
6143 null;
6144
6145 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6146 or else not In_Private_Part (Scope (Base_Type (T)))
6147 then
6148 Prepend_Elmt (T, Exchanged_Views);
6149 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6150 end if;
6151
6152 -- For composite types with inconsistent representation exchange
6153 -- component types accordingly.
6154
6155 elsif Is_Access_Type (T)
6156 and then Is_Private_Type (Designated_Type (T))
6157 and then not Has_Private_View (N)
6158 and then Present (Full_View (Designated_Type (T)))
6159 then
6160 Switch_View (Designated_Type (T));
6161
6162 elsif Is_Array_Type (T) then
6163 if Is_Private_Type (Component_Type (T))
6164 and then not Has_Private_View (N)
6165 and then Present (Full_View (Component_Type (T)))
6166 then
6167 Switch_View (Component_Type (T));
6168 end if;
6169
6170 -- The normal exchange mechanism relies on the setting of a
6171 -- flag on the reference in the generic. However, an additional
6172 -- mechanism is needed for types that are not explicitly
6173 -- mentioned in the generic, but may be needed in expanded code
6174 -- in the instance. This includes component types of arrays and
6175 -- designated types of access types. This processing must also
6176 -- include the index types of arrays which we take care of here.
6177
6178 declare
6179 Indx : Node_Id;
6180 Typ : Entity_Id;
6181
6182 begin
6183 Indx := First_Index (T);
6184 while Present (Indx) loop
6185 Typ := Base_Type (Etype (Indx));
6186
6187 if Is_Private_Type (Typ)
6188 and then Present (Full_View (Typ))
6189 then
6190 Switch_View (Typ);
6191 end if;
6192
6193 Next_Index (Indx);
6194 end loop;
6195 end;
6196
6197 elsif Is_Private_Type (T)
6198 and then Present (Full_View (T))
6199 and then Is_Array_Type (Full_View (T))
6200 and then Is_Private_Type (Component_Type (Full_View (T)))
6201 then
6202 Switch_View (T);
6203
6204 -- Finally, a non-private subtype may have a private base type, which
6205 -- must be exchanged for consistency. This can happen when a package
6206 -- body is instantiated, when the scope stack is empty but in fact
6207 -- the subtype and the base type are declared in an enclosing scope.
6208
6209 -- Note that in this case we introduce an inconsistency in the view
6210 -- set, because we switch the base type BT, but there could be some
6211 -- private dependent subtypes of BT which remain unswitched. Such
6212 -- subtypes might need to be switched at a later point (see specific
6213 -- provision for that case in Switch_View).
6214
6215 elsif not Is_Private_Type (T)
6216 and then not Has_Private_View (N)
6217 and then Is_Private_Type (BT)
6218 and then Present (Full_View (BT))
6219 and then not Is_Generic_Type (BT)
6220 and then not In_Open_Scopes (BT)
6221 then
6222 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6223 Exchange_Declarations (BT);
6224 end if;
6225 end if;
6226 end Check_Private_View;
6227
6228 -----------------------------
6229 -- Check_Hidden_Primitives --
6230 -----------------------------
6231
6232 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6233 Actual : Node_Id;
6234 Gen_T : Entity_Id;
6235 Result : Elist_Id := No_Elist;
6236
6237 begin
6238 if No (Assoc_List) then
6239 return No_Elist;
6240 end if;
6241
6242 -- Traverse the list of associations between formals and actuals
6243 -- searching for renamings of tagged types
6244
6245 Actual := First (Assoc_List);
6246 while Present (Actual) loop
6247 if Nkind (Actual) = N_Subtype_Declaration then
6248 Gen_T := Generic_Parent_Type (Actual);
6249
6250 if Present (Gen_T)
6251 and then Is_Tagged_Type (Gen_T)
6252 then
6253 -- Traverse the list of primitives of the actual types
6254 -- searching for hidden primitives that are visible in the
6255 -- corresponding generic formal; leave them visible and
6256 -- append them to Result to restore their decoration later.
6257
6258 Install_Hidden_Primitives
6259 (Prims_List => Result,
6260 Gen_T => Gen_T,
6261 Act_T => Entity (Subtype_Indication (Actual)));
6262 end if;
6263 end if;
6264
6265 Next (Actual);
6266 end loop;
6267
6268 return Result;
6269 end Check_Hidden_Primitives;
6270
6271 --------------------------
6272 -- Contains_Instance_Of --
6273 --------------------------
6274
6275 function Contains_Instance_Of
6276 (Inner : Entity_Id;
6277 Outer : Entity_Id;
6278 N : Node_Id) return Boolean
6279 is
6280 Elmt : Elmt_Id;
6281 Scop : Entity_Id;
6282
6283 begin
6284 Scop := Outer;
6285
6286 -- Verify that there are no circular instantiations. We check whether
6287 -- the unit contains an instance of the current scope or some enclosing
6288 -- scope (in case one of the instances appears in a subunit). Longer
6289 -- circularities involving subunits might seem too pathological to
6290 -- consider, but they were not too pathological for the authors of
6291 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6292 -- enclosing generic scopes as containing an instance.
6293
6294 loop
6295 -- Within a generic subprogram body, the scope is not generic, to
6296 -- allow for recursive subprograms. Use the declaration to determine
6297 -- whether this is a generic unit.
6298
6299 if Ekind (Scop) = E_Generic_Package
6300 or else (Is_Subprogram (Scop)
6301 and then Nkind (Unit_Declaration_Node (Scop)) =
6302 N_Generic_Subprogram_Declaration)
6303 then
6304 Elmt := First_Elmt (Inner_Instances (Inner));
6305
6306 while Present (Elmt) loop
6307 if Node (Elmt) = Scop then
6308 Error_Msg_Node_2 := Inner;
6309 Error_Msg_NE
6310 ("circular Instantiation: & instantiated within &!",
6311 N, Scop);
6312 return True;
6313
6314 elsif Node (Elmt) = Inner then
6315 return True;
6316
6317 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6318 Error_Msg_Node_2 := Inner;
6319 Error_Msg_NE
6320 ("circular Instantiation: & instantiated within &!",
6321 N, Node (Elmt));
6322 return True;
6323 end if;
6324
6325 Next_Elmt (Elmt);
6326 end loop;
6327
6328 -- Indicate that Inner is being instantiated within Scop
6329
6330 Append_Elmt (Inner, Inner_Instances (Scop));
6331 end if;
6332
6333 if Scop = Standard_Standard then
6334 exit;
6335 else
6336 Scop := Scope (Scop);
6337 end if;
6338 end loop;
6339
6340 return False;
6341 end Contains_Instance_Of;
6342
6343 -----------------------
6344 -- Copy_Generic_Node --
6345 -----------------------
6346
6347 function Copy_Generic_Node
6348 (N : Node_Id;
6349 Parent_Id : Node_Id;
6350 Instantiating : Boolean) return Node_Id
6351 is
6352 Ent : Entity_Id;
6353 New_N : Node_Id;
6354
6355 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6356 -- Check the given value of one of the Fields referenced by the current
6357 -- node to determine whether to copy it recursively. The field may hold
6358 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6359 -- Char) in which case it need not be copied.
6360
6361 procedure Copy_Descendants;
6362 -- Common utility for various nodes
6363
6364 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6365 -- Make copy of element list
6366
6367 function Copy_Generic_List
6368 (L : List_Id;
6369 Parent_Id : Node_Id) return List_Id;
6370 -- Apply Copy_Node recursively to the members of a node list
6371
6372 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6373 -- True if an identifier is part of the defining program unit name of
6374 -- a child unit. The entity of such an identifier must be kept (for
6375 -- ASIS use) even though as the name of an enclosing generic it would
6376 -- otherwise not be preserved in the generic tree.
6377
6378 ----------------------
6379 -- Copy_Descendants --
6380 ----------------------
6381
6382 procedure Copy_Descendants is
6383
6384 use Atree.Unchecked_Access;
6385 -- This code section is part of the implementation of an untyped
6386 -- tree traversal, so it needs direct access to node fields.
6387
6388 begin
6389 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6390 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6391 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6392 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6393 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6394 end Copy_Descendants;
6395
6396 -----------------------------
6397 -- Copy_Generic_Descendant --
6398 -----------------------------
6399
6400 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6401 begin
6402 if D = Union_Id (Empty) then
6403 return D;
6404
6405 elsif D in Node_Range then
6406 return Union_Id
6407 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6408
6409 elsif D in List_Range then
6410 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6411
6412 elsif D in Elist_Range then
6413 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6414
6415 -- Nothing else is copyable (e.g. Uint values), return as is
6416
6417 else
6418 return D;
6419 end if;
6420 end Copy_Generic_Descendant;
6421
6422 ------------------------
6423 -- Copy_Generic_Elist --
6424 ------------------------
6425
6426 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6427 M : Elmt_Id;
6428 L : Elist_Id;
6429
6430 begin
6431 if Present (E) then
6432 L := New_Elmt_List;
6433 M := First_Elmt (E);
6434 while Present (M) loop
6435 Append_Elmt
6436 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6437 Next_Elmt (M);
6438 end loop;
6439
6440 return L;
6441
6442 else
6443 return No_Elist;
6444 end if;
6445 end Copy_Generic_Elist;
6446
6447 -----------------------
6448 -- Copy_Generic_List --
6449 -----------------------
6450
6451 function Copy_Generic_List
6452 (L : List_Id;
6453 Parent_Id : Node_Id) return List_Id
6454 is
6455 N : Node_Id;
6456 New_L : List_Id;
6457
6458 begin
6459 if Present (L) then
6460 New_L := New_List;
6461 Set_Parent (New_L, Parent_Id);
6462
6463 N := First (L);
6464 while Present (N) loop
6465 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6466 Next (N);
6467 end loop;
6468
6469 return New_L;
6470
6471 else
6472 return No_List;
6473 end if;
6474 end Copy_Generic_List;
6475
6476 ---------------------------
6477 -- In_Defining_Unit_Name --
6478 ---------------------------
6479
6480 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6481 begin
6482 return Present (Parent (Nam))
6483 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6484 or else
6485 (Nkind (Parent (Nam)) = N_Expanded_Name
6486 and then In_Defining_Unit_Name (Parent (Nam))));
6487 end In_Defining_Unit_Name;
6488
6489 -- Start of processing for Copy_Generic_Node
6490
6491 begin
6492 if N = Empty then
6493 return N;
6494 end if;
6495
6496 New_N := New_Copy (N);
6497
6498 -- Copy aspects if present
6499
6500 if Has_Aspects (N) then
6501 Set_Has_Aspects (New_N, False);
6502 Set_Aspect_Specifications
6503 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6504 end if;
6505
6506 if Instantiating then
6507 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6508 end if;
6509
6510 if not Is_List_Member (N) then
6511 Set_Parent (New_N, Parent_Id);
6512 end if;
6513
6514 -- If defining identifier, then all fields have been copied already
6515
6516 if Nkind (New_N) in N_Entity then
6517 null;
6518
6519 -- Special casing for identifiers and other entity names and operators
6520
6521 elsif Nkind_In (New_N, N_Identifier,
6522 N_Character_Literal,
6523 N_Expanded_Name,
6524 N_Operator_Symbol)
6525 or else Nkind (New_N) in N_Op
6526 then
6527 if not Instantiating then
6528
6529 -- Link both nodes in order to assign subsequently the entity of
6530 -- the copy to the original node, in case this is a global
6531 -- reference.
6532
6533 Set_Associated_Node (N, New_N);
6534
6535 -- If we are within an instantiation, this is a nested generic
6536 -- that has already been analyzed at the point of definition.
6537 -- We must preserve references that were global to the enclosing
6538 -- parent at that point. Other occurrences, whether global or
6539 -- local to the current generic, must be resolved anew, so we
6540 -- reset the entity in the generic copy. A global reference has a
6541 -- smaller depth than the parent, or else the same depth in case
6542 -- both are distinct compilation units.
6543
6544 -- A child unit is implicitly declared within the enclosing parent
6545 -- but is in fact global to it, and must be preserved.
6546
6547 -- It is also possible for Current_Instantiated_Parent to be
6548 -- defined, and for this not to be a nested generic, namely if
6549 -- the unit is loaded through Rtsfind. In that case, the entity of
6550 -- New_N is only a link to the associated node, and not a defining
6551 -- occurrence.
6552
6553 -- The entities for parent units in the defining_program_unit of a
6554 -- generic child unit are established when the context of the unit
6555 -- is first analyzed, before the generic copy is made. They are
6556 -- preserved in the copy for use in ASIS queries.
6557
6558 Ent := Entity (New_N);
6559
6560 if No (Current_Instantiated_Parent.Gen_Id) then
6561 if No (Ent)
6562 or else Nkind (Ent) /= N_Defining_Identifier
6563 or else not In_Defining_Unit_Name (N)
6564 then
6565 Set_Associated_Node (New_N, Empty);
6566 end if;
6567
6568 elsif No (Ent)
6569 or else
6570 not Nkind_In (Ent, N_Defining_Identifier,
6571 N_Defining_Character_Literal,
6572 N_Defining_Operator_Symbol)
6573 or else No (Scope (Ent))
6574 or else
6575 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6576 and then not Is_Child_Unit (Ent))
6577 or else
6578 (Scope_Depth (Scope (Ent)) >
6579 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6580 and then
6581 Get_Source_Unit (Ent) =
6582 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6583 then
6584 Set_Associated_Node (New_N, Empty);
6585 end if;
6586
6587 -- Case of instantiating identifier or some other name or operator
6588
6589 else
6590 -- If the associated node is still defined, the entity in it
6591 -- is global, and must be copied to the instance. If this copy
6592 -- is being made for a body to inline, it is applied to an
6593 -- instantiated tree, and the entity is already present and
6594 -- must be also preserved.
6595
6596 declare
6597 Assoc : constant Node_Id := Get_Associated_Node (N);
6598
6599 begin
6600 if Present (Assoc) then
6601 if Nkind (Assoc) = Nkind (N) then
6602 Set_Entity (New_N, Entity (Assoc));
6603 Check_Private_View (N);
6604
6605 -- The name in the call may be a selected component if the
6606 -- call has not been analyzed yet, as may be the case for
6607 -- pre/post conditions in a generic unit.
6608
6609 elsif Nkind (Assoc) = N_Function_Call
6610 and then Is_Entity_Name (Name (Assoc))
6611 then
6612 Set_Entity (New_N, Entity (Name (Assoc)));
6613
6614 elsif Nkind_In (Assoc, N_Defining_Identifier,
6615 N_Defining_Character_Literal,
6616 N_Defining_Operator_Symbol)
6617 and then Expander_Active
6618 then
6619 -- Inlining case: we are copying a tree that contains
6620 -- global entities, which are preserved in the copy to be
6621 -- used for subsequent inlining.
6622
6623 null;
6624
6625 else
6626 Set_Entity (New_N, Empty);
6627 end if;
6628 end if;
6629 end;
6630 end if;
6631
6632 -- For expanded name, we must copy the Prefix and Selector_Name
6633
6634 if Nkind (N) = N_Expanded_Name then
6635 Set_Prefix
6636 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
6637
6638 Set_Selector_Name (New_N,
6639 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
6640
6641 -- For operators, we must copy the right operand
6642
6643 elsif Nkind (N) in N_Op then
6644 Set_Right_Opnd (New_N,
6645 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
6646
6647 -- And for binary operators, the left operand as well
6648
6649 if Nkind (N) in N_Binary_Op then
6650 Set_Left_Opnd (New_N,
6651 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
6652 end if;
6653 end if;
6654
6655 -- Special casing for stubs
6656
6657 elsif Nkind (N) in N_Body_Stub then
6658
6659 -- In any case, we must copy the specification or defining
6660 -- identifier as appropriate.
6661
6662 if Nkind (N) = N_Subprogram_Body_Stub then
6663 Set_Specification (New_N,
6664 Copy_Generic_Node (Specification (N), New_N, Instantiating));
6665
6666 else
6667 Set_Defining_Identifier (New_N,
6668 Copy_Generic_Node
6669 (Defining_Identifier (N), New_N, Instantiating));
6670 end if;
6671
6672 -- If we are not instantiating, then this is where we load and
6673 -- analyze subunits, i.e. at the point where the stub occurs. A
6674 -- more permissive system might defer this analysis to the point
6675 -- of instantiation, but this seems too complicated for now.
6676
6677 if not Instantiating then
6678 declare
6679 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
6680 Subunit : Node_Id;
6681 Unum : Unit_Number_Type;
6682 New_Body : Node_Id;
6683
6684 begin
6685 -- Make sure that, if it is a subunit of the main unit that is
6686 -- preprocessed and if -gnateG is specified, the preprocessed
6687 -- file will be written.
6688
6689 Lib.Analysing_Subunit_Of_Main :=
6690 Lib.In_Extended_Main_Source_Unit (N);
6691 Unum :=
6692 Load_Unit
6693 (Load_Name => Subunit_Name,
6694 Required => False,
6695 Subunit => True,
6696 Error_Node => N);
6697 Lib.Analysing_Subunit_Of_Main := False;
6698
6699 -- If the proper body is not found, a warning message will be
6700 -- emitted when analyzing the stub, or later at the point of
6701 -- instantiation. Here we just leave the stub as is.
6702
6703 if Unum = No_Unit then
6704 Subunits_Missing := True;
6705 goto Subunit_Not_Found;
6706 end if;
6707
6708 Subunit := Cunit (Unum);
6709
6710 if Nkind (Unit (Subunit)) /= N_Subunit then
6711 Error_Msg_N
6712 ("found child unit instead of expected SEPARATE subunit",
6713 Subunit);
6714 Error_Msg_Sloc := Sloc (N);
6715 Error_Msg_N ("\to complete stub #", Subunit);
6716 goto Subunit_Not_Found;
6717 end if;
6718
6719 -- We must create a generic copy of the subunit, in order to
6720 -- perform semantic analysis on it, and we must replace the
6721 -- stub in the original generic unit with the subunit, in order
6722 -- to preserve non-local references within.
6723
6724 -- Only the proper body needs to be copied. Library_Unit and
6725 -- context clause are simply inherited by the generic copy.
6726 -- Note that the copy (which may be recursive if there are
6727 -- nested subunits) must be done first, before attaching it to
6728 -- the enclosing generic.
6729
6730 New_Body :=
6731 Copy_Generic_Node
6732 (Proper_Body (Unit (Subunit)),
6733 Empty, Instantiating => False);
6734
6735 -- Now place the original proper body in the original generic
6736 -- unit. This is a body, not a compilation unit.
6737
6738 Rewrite (N, Proper_Body (Unit (Subunit)));
6739 Set_Is_Compilation_Unit (Defining_Entity (N), False);
6740 Set_Was_Originally_Stub (N);
6741
6742 -- Finally replace the body of the subunit with its copy, and
6743 -- make this new subunit into the library unit of the generic
6744 -- copy, which does not have stubs any longer.
6745
6746 Set_Proper_Body (Unit (Subunit), New_Body);
6747 Set_Library_Unit (New_N, Subunit);
6748 Inherit_Context (Unit (Subunit), N);
6749 end;
6750
6751 -- If we are instantiating, this must be an error case, since
6752 -- otherwise we would have replaced the stub node by the proper body
6753 -- that corresponds. So just ignore it in the copy (i.e. we have
6754 -- copied it, and that is good enough).
6755
6756 else
6757 null;
6758 end if;
6759
6760 <<Subunit_Not_Found>> null;
6761
6762 -- If the node is a compilation unit, it is the subunit of a stub, which
6763 -- has been loaded already (see code below). In this case, the library
6764 -- unit field of N points to the parent unit (which is a compilation
6765 -- unit) and need not (and cannot!) be copied.
6766
6767 -- When the proper body of the stub is analyzed, the library_unit link
6768 -- is used to establish the proper context (see sem_ch10).
6769
6770 -- The other fields of a compilation unit are copied as usual
6771
6772 elsif Nkind (N) = N_Compilation_Unit then
6773
6774 -- This code can only be executed when not instantiating, because in
6775 -- the copy made for an instantiation, the compilation unit node has
6776 -- disappeared at the point that a stub is replaced by its proper
6777 -- body.
6778
6779 pragma Assert (not Instantiating);
6780
6781 Set_Context_Items (New_N,
6782 Copy_Generic_List (Context_Items (N), New_N));
6783
6784 Set_Unit (New_N,
6785 Copy_Generic_Node (Unit (N), New_N, False));
6786
6787 Set_First_Inlined_Subprogram (New_N,
6788 Copy_Generic_Node
6789 (First_Inlined_Subprogram (N), New_N, False));
6790
6791 Set_Aux_Decls_Node (New_N,
6792 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
6793
6794 -- For an assignment node, the assignment is known to be semantically
6795 -- legal if we are instantiating the template. This avoids incorrect
6796 -- diagnostics in generated code.
6797
6798 elsif Nkind (N) = N_Assignment_Statement then
6799
6800 -- Copy name and expression fields in usual manner
6801
6802 Set_Name (New_N,
6803 Copy_Generic_Node (Name (N), New_N, Instantiating));
6804
6805 Set_Expression (New_N,
6806 Copy_Generic_Node (Expression (N), New_N, Instantiating));
6807
6808 if Instantiating then
6809 Set_Assignment_OK (Name (New_N), True);
6810 end if;
6811
6812 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
6813 if not Instantiating then
6814 Set_Associated_Node (N, New_N);
6815
6816 else
6817 if Present (Get_Associated_Node (N))
6818 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
6819 then
6820 -- In the generic the aggregate has some composite type. If at
6821 -- the point of instantiation the type has a private view,
6822 -- install the full view (and that of its ancestors, if any).
6823
6824 declare
6825 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
6826 Rt : Entity_Id;
6827
6828 begin
6829 if Present (T)
6830 and then Is_Private_Type (T)
6831 then
6832 Switch_View (T);
6833 end if;
6834
6835 if Present (T)
6836 and then Is_Tagged_Type (T)
6837 and then Is_Derived_Type (T)
6838 then
6839 Rt := Root_Type (T);
6840
6841 loop
6842 T := Etype (T);
6843
6844 if Is_Private_Type (T) then
6845 Switch_View (T);
6846 end if;
6847
6848 exit when T = Rt;
6849 end loop;
6850 end if;
6851 end;
6852 end if;
6853 end if;
6854
6855 -- Do not copy the associated node, which points to the generic copy
6856 -- of the aggregate.
6857
6858 declare
6859 use Atree.Unchecked_Access;
6860 -- This code section is part of the implementation of an untyped
6861 -- tree traversal, so it needs direct access to node fields.
6862
6863 begin
6864 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6865 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6866 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6867 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6868 end;
6869
6870 -- Allocators do not have an identifier denoting the access type, so we
6871 -- must locate it through the expression to check whether the views are
6872 -- consistent.
6873
6874 elsif Nkind (N) = N_Allocator
6875 and then Nkind (Expression (N)) = N_Qualified_Expression
6876 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
6877 and then Instantiating
6878 then
6879 declare
6880 T : constant Node_Id :=
6881 Get_Associated_Node (Subtype_Mark (Expression (N)));
6882 Acc_T : Entity_Id;
6883
6884 begin
6885 if Present (T) then
6886
6887 -- Retrieve the allocator node in the generic copy
6888
6889 Acc_T := Etype (Parent (Parent (T)));
6890 if Present (Acc_T)
6891 and then Is_Private_Type (Acc_T)
6892 then
6893 Switch_View (Acc_T);
6894 end if;
6895 end if;
6896
6897 Copy_Descendants;
6898 end;
6899
6900 -- For a proper body, we must catch the case of a proper body that
6901 -- replaces a stub. This represents the point at which a separate
6902 -- compilation unit, and hence template file, may be referenced, so we
6903 -- must make a new source instantiation entry for the template of the
6904 -- subunit, and ensure that all nodes in the subunit are adjusted using
6905 -- this new source instantiation entry.
6906
6907 elsif Nkind (N) in N_Proper_Body then
6908 declare
6909 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
6910
6911 begin
6912 if Instantiating and then Was_Originally_Stub (N) then
6913 Create_Instantiation_Source
6914 (Instantiation_Node,
6915 Defining_Entity (N),
6916 False,
6917 S_Adjustment);
6918 end if;
6919
6920 -- Now copy the fields of the proper body, using the new
6921 -- adjustment factor if one was needed as per test above.
6922
6923 Copy_Descendants;
6924
6925 -- Restore the original adjustment factor in case changed
6926
6927 S_Adjustment := Save_Adjustment;
6928 end;
6929
6930 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
6931 -- generic unit, not to the instantiating unit.
6932
6933 elsif Nkind (N) = N_Pragma and then Instantiating then
6934 declare
6935 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
6936 begin
6937 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
6938 New_N := Make_Null_Statement (Sloc (N));
6939 else
6940 Copy_Descendants;
6941 end if;
6942 end;
6943
6944 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
6945
6946 -- No descendant fields need traversing
6947
6948 null;
6949
6950 elsif Nkind (N) = N_String_Literal
6951 and then Present (Etype (N))
6952 and then Instantiating
6953 then
6954 -- If the string is declared in an outer scope, the string_literal
6955 -- subtype created for it may have the wrong scope. We force the
6956 -- reanalysis of the constant to generate a new itype in the proper
6957 -- context.
6958
6959 Set_Etype (New_N, Empty);
6960 Set_Analyzed (New_N, False);
6961
6962 -- For the remaining nodes, copy their descendants recursively
6963
6964 else
6965 Copy_Descendants;
6966
6967 if Instantiating and then Nkind (N) = N_Subprogram_Body then
6968 Set_Generic_Parent (Specification (New_N), N);
6969
6970 -- Should preserve Corresponding_Spec??? (12.3(14))
6971 end if;
6972 end if;
6973
6974 return New_N;
6975 end Copy_Generic_Node;
6976
6977 ----------------------------
6978 -- Denotes_Formal_Package --
6979 ----------------------------
6980
6981 function Denotes_Formal_Package
6982 (Pack : Entity_Id;
6983 On_Exit : Boolean := False;
6984 Instance : Entity_Id := Empty) return Boolean
6985 is
6986 Par : Entity_Id;
6987 Scop : constant Entity_Id := Scope (Pack);
6988 E : Entity_Id;
6989
6990 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
6991 -- The package in question may be an actual for a previous formal
6992 -- package P of the current instance, so examine its actuals as well.
6993 -- This must be recursive over other formal packages.
6994
6995 ----------------------------------
6996 -- Is_Actual_Of_Previous_Formal --
6997 ----------------------------------
6998
6999 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7000 E1 : Entity_Id;
7001
7002 begin
7003 E1 := First_Entity (P);
7004 while Present (E1) and then E1 /= Instance loop
7005 if Ekind (E1) = E_Package
7006 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7007 then
7008 if Renamed_Object (E1) = Pack then
7009 return True;
7010
7011 elsif E1 = P or else Renamed_Object (E1) = P then
7012 return False;
7013
7014 elsif Is_Actual_Of_Previous_Formal (E1) then
7015 return True;
7016 end if;
7017 end if;
7018
7019 Next_Entity (E1);
7020 end loop;
7021
7022 return False;
7023 end Is_Actual_Of_Previous_Formal;
7024
7025 -- Start of processing for Denotes_Formal_Package
7026
7027 begin
7028 if On_Exit then
7029 Par :=
7030 Instance_Envs.Table
7031 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7032 else
7033 Par := Current_Instantiated_Parent.Act_Id;
7034 end if;
7035
7036 if Ekind (Scop) = E_Generic_Package
7037 or else Nkind (Unit_Declaration_Node (Scop)) =
7038 N_Generic_Subprogram_Declaration
7039 then
7040 return True;
7041
7042 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7043 N_Formal_Package_Declaration
7044 then
7045 return True;
7046
7047 elsif No (Par) then
7048 return False;
7049
7050 else
7051 -- Check whether this package is associated with a formal package of
7052 -- the enclosing instantiation. Iterate over the list of renamings.
7053
7054 E := First_Entity (Par);
7055 while Present (E) loop
7056 if Ekind (E) /= E_Package
7057 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7058 then
7059 null;
7060
7061 elsif Renamed_Object (E) = Par then
7062 return False;
7063
7064 elsif Renamed_Object (E) = Pack then
7065 return True;
7066
7067 elsif Is_Actual_Of_Previous_Formal (E) then
7068 return True;
7069
7070 end if;
7071
7072 Next_Entity (E);
7073 end loop;
7074
7075 return False;
7076 end if;
7077 end Denotes_Formal_Package;
7078
7079 -----------------
7080 -- End_Generic --
7081 -----------------
7082
7083 procedure End_Generic is
7084 begin
7085 -- ??? More things could be factored out in this routine. Should
7086 -- probably be done at a later stage.
7087
7088 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7089 Generic_Flags.Decrement_Last;
7090
7091 Expander_Mode_Restore;
7092 end End_Generic;
7093
7094 -------------
7095 -- Earlier --
7096 -------------
7097
7098 function Earlier (N1, N2 : Node_Id) return Boolean is
7099 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7100 -- Find distance from given node to enclosing compilation unit
7101
7102 ----------------
7103 -- Find_Depth --
7104 ----------------
7105
7106 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7107 begin
7108 while Present (P)
7109 and then Nkind (P) /= N_Compilation_Unit
7110 loop
7111 P := True_Parent (P);
7112 D := D + 1;
7113 end loop;
7114 end Find_Depth;
7115
7116 -- Local declarations
7117
7118 D1 : Integer := 0;
7119 D2 : Integer := 0;
7120 P1 : Node_Id := N1;
7121 P2 : Node_Id := N2;
7122 T1 : Source_Ptr;
7123 T2 : Source_Ptr;
7124
7125 -- Start of processing for Earlier
7126
7127 begin
7128 Find_Depth (P1, D1);
7129 Find_Depth (P2, D2);
7130
7131 if P1 /= P2 then
7132 return False;
7133 else
7134 P1 := N1;
7135 P2 := N2;
7136 end if;
7137
7138 while D1 > D2 loop
7139 P1 := True_Parent (P1);
7140 D1 := D1 - 1;
7141 end loop;
7142
7143 while D2 > D1 loop
7144 P2 := True_Parent (P2);
7145 D2 := D2 - 1;
7146 end loop;
7147
7148 -- At this point P1 and P2 are at the same distance from the root.
7149 -- We examine their parents until we find a common declarative list.
7150 -- If we reach the root, N1 and N2 do not descend from the same
7151 -- declarative list (e.g. one is nested in the declarative part and
7152 -- the other is in a block in the statement part) and the earlier
7153 -- one is already frozen.
7154
7155 while not Is_List_Member (P1)
7156 or else not Is_List_Member (P2)
7157 or else List_Containing (P1) /= List_Containing (P2)
7158 loop
7159 P1 := True_Parent (P1);
7160 P2 := True_Parent (P2);
7161
7162 if Nkind (Parent (P1)) = N_Subunit then
7163 P1 := Corresponding_Stub (Parent (P1));
7164 end if;
7165
7166 if Nkind (Parent (P2)) = N_Subunit then
7167 P2 := Corresponding_Stub (Parent (P2));
7168 end if;
7169
7170 if P1 = P2 then
7171 return False;
7172 end if;
7173 end loop;
7174
7175 -- Expanded code usually shares the source location of the original
7176 -- construct it was generated for. This however may not necessarely
7177 -- reflect the true location of the code within the tree.
7178
7179 -- Before comparing the slocs of the two nodes, make sure that we are
7180 -- working with correct source locations. Assume that P1 is to the left
7181 -- of P2. If either one does not come from source, traverse the common
7182 -- list heading towards the other node and locate the first source
7183 -- statement.
7184
7185 -- P1 P2
7186 -- ----+===+===+--------------+===+===+----
7187 -- expanded code expanded code
7188
7189 if not Comes_From_Source (P1) then
7190 while Present (P1) loop
7191
7192 -- Neither P2 nor a source statement were located during the
7193 -- search. If we reach the end of the list, then P1 does not
7194 -- occur earlier than P2.
7195
7196 -- ---->
7197 -- start --- P2 ----- P1 --- end
7198
7199 if No (Next (P1)) then
7200 return False;
7201
7202 -- We encounter P2 while going to the right of the list. This
7203 -- means that P1 does indeed appear earlier.
7204
7205 -- ---->
7206 -- start --- P1 ===== P2 --- end
7207 -- expanded code in between
7208
7209 elsif P1 = P2 then
7210 return True;
7211
7212 -- No need to look any further since we have located a source
7213 -- statement.
7214
7215 elsif Comes_From_Source (P1) then
7216 exit;
7217 end if;
7218
7219 -- Keep going right
7220
7221 Next (P1);
7222 end loop;
7223 end if;
7224
7225 if not Comes_From_Source (P2) then
7226 while Present (P2) loop
7227
7228 -- Neither P1 nor a source statement were located during the
7229 -- search. If we reach the start of the list, then P1 does not
7230 -- occur earlier than P2.
7231
7232 -- <----
7233 -- start --- P2 --- P1 --- end
7234
7235 if No (Prev (P2)) then
7236 return False;
7237
7238 -- We encounter P1 while going to the left of the list. This
7239 -- means that P1 does indeed appear earlier.
7240
7241 -- <----
7242 -- start --- P1 ===== P2 --- end
7243 -- expanded code in between
7244
7245 elsif P2 = P1 then
7246 return True;
7247
7248 -- No need to look any further since we have located a source
7249 -- statement.
7250
7251 elsif Comes_From_Source (P2) then
7252 exit;
7253 end if;
7254
7255 -- Keep going left
7256
7257 Prev (P2);
7258 end loop;
7259 end if;
7260
7261 -- At this point either both nodes came from source or we approximated
7262 -- their source locations through neighbouring source statements.
7263
7264 T1 := Top_Level_Location (Sloc (P1));
7265 T2 := Top_Level_Location (Sloc (P2));
7266
7267 -- When two nodes come from the same instance, they have identical top
7268 -- level locations. To determine proper relation within the tree, check
7269 -- their locations within the template.
7270
7271 if T1 = T2 then
7272 return Sloc (P1) < Sloc (P2);
7273
7274 -- The two nodes either come from unrelated instances or do not come
7275 -- from instantiated code at all.
7276
7277 else
7278 return T1 < T2;
7279 end if;
7280 end Earlier;
7281
7282 ----------------------
7283 -- Find_Actual_Type --
7284 ----------------------
7285
7286 function Find_Actual_Type
7287 (Typ : Entity_Id;
7288 Gen_Type : Entity_Id) return Entity_Id
7289 is
7290 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7291 T : Entity_Id;
7292
7293 begin
7294 -- Special processing only applies to child units
7295
7296 if not Is_Child_Unit (Gen_Scope) then
7297 return Get_Instance_Of (Typ);
7298
7299 -- If designated or component type is itself a formal of the child unit,
7300 -- its instance is available.
7301
7302 elsif Scope (Typ) = Gen_Scope then
7303 return Get_Instance_Of (Typ);
7304
7305 -- If the array or access type is not declared in the parent unit,
7306 -- no special processing needed.
7307
7308 elsif not Is_Generic_Type (Typ)
7309 and then Scope (Gen_Scope) /= Scope (Typ)
7310 then
7311 return Get_Instance_Of (Typ);
7312
7313 -- Otherwise, retrieve designated or component type by visibility
7314
7315 else
7316 T := Current_Entity (Typ);
7317 while Present (T) loop
7318 if In_Open_Scopes (Scope (T)) then
7319 return T;
7320
7321 elsif Is_Generic_Actual_Type (T) then
7322 return T;
7323 end if;
7324
7325 T := Homonym (T);
7326 end loop;
7327
7328 return Typ;
7329 end if;
7330 end Find_Actual_Type;
7331
7332 ----------------------------
7333 -- Freeze_Subprogram_Body --
7334 ----------------------------
7335
7336 procedure Freeze_Subprogram_Body
7337 (Inst_Node : Node_Id;
7338 Gen_Body : Node_Id;
7339 Pack_Id : Entity_Id)
7340 is
7341 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7342 Par : constant Entity_Id := Scope (Gen_Unit);
7343 E_G_Id : Entity_Id;
7344 Enc_G : Entity_Id;
7345 Enc_I : Node_Id;
7346 F_Node : Node_Id;
7347
7348 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7349 -- Find innermost package body that encloses the given node, and which
7350 -- is not a compilation unit. Freeze nodes for the instance, or for its
7351 -- enclosing body, may be inserted after the enclosing_body of the
7352 -- generic unit. Used to determine proper placement of freeze node for
7353 -- both package and subprogram instances.
7354
7355 function Package_Freeze_Node (B : Node_Id) return Node_Id;
7356 -- Find entity for given package body, and locate or create a freeze
7357 -- node for it.
7358
7359 ----------------------------
7360 -- Enclosing_Package_Body --
7361 ----------------------------
7362
7363 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
7364 P : Node_Id;
7365
7366 begin
7367 P := Parent (N);
7368 while Present (P)
7369 and then Nkind (Parent (P)) /= N_Compilation_Unit
7370 loop
7371 if Nkind (P) = N_Package_Body then
7372 if Nkind (Parent (P)) = N_Subunit then
7373 return Corresponding_Stub (Parent (P));
7374 else
7375 return P;
7376 end if;
7377 end if;
7378
7379 P := True_Parent (P);
7380 end loop;
7381
7382 return Empty;
7383 end Enclosing_Package_Body;
7384
7385 -------------------------
7386 -- Package_Freeze_Node --
7387 -------------------------
7388
7389 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7390 Id : Entity_Id;
7391
7392 begin
7393 if Nkind (B) = N_Package_Body then
7394 Id := Corresponding_Spec (B);
7395 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7396 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7397 end if;
7398
7399 Ensure_Freeze_Node (Id);
7400 return Freeze_Node (Id);
7401 end Package_Freeze_Node;
7402
7403 -- Start of processing of Freeze_Subprogram_Body
7404
7405 begin
7406 -- If the instance and the generic body appear within the same unit, and
7407 -- the instance precedes the generic, the freeze node for the instance
7408 -- must appear after that of the generic. If the generic is nested
7409 -- within another instance I2, then current instance must be frozen
7410 -- after I2. In both cases, the freeze nodes are those of enclosing
7411 -- packages. Otherwise, the freeze node is placed at the end of the
7412 -- current declarative part.
7413
7414 Enc_G := Enclosing_Package_Body (Gen_Body);
7415 Enc_I := Enclosing_Package_Body (Inst_Node);
7416 Ensure_Freeze_Node (Pack_Id);
7417 F_Node := Freeze_Node (Pack_Id);
7418
7419 if Is_Generic_Instance (Par)
7420 and then Present (Freeze_Node (Par))
7421 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7422 then
7423 -- The parent was a premature instantiation. Insert freeze node at
7424 -- the end the current declarative part.
7425
7426 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7427 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7428
7429 -- Handle the following case:
7430 --
7431 -- package Parent_Inst is new ...
7432 -- Parent_Inst []
7433 --
7434 -- procedure P ... -- this body freezes Parent_Inst
7435 --
7436 -- package Inst is new ...
7437 --
7438 -- In this particular scenario, the freeze node for Inst must be
7439 -- inserted in the same manner as that of Parent_Inst - before the
7440 -- next source body or at the end of the declarative list (body not
7441 -- available). If body P did not exist and Parent_Inst was frozen
7442 -- after Inst, either by a body following Inst or at the end of the
7443 -- declarative region, the freeze node for Inst must be inserted
7444 -- after that of Parent_Inst. This relation is established by
7445 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7446
7447 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7448 List_Containing (Inst_Node)
7449 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7450 then
7451 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7452
7453 else
7454 Insert_After (Freeze_Node (Par), F_Node);
7455 end if;
7456
7457 -- The body enclosing the instance should be frozen after the body that
7458 -- includes the generic, because the body of the instance may make
7459 -- references to entities therein. If the two are not in the same
7460 -- declarative part, or if the one enclosing the instance is frozen
7461 -- already, freeze the instance at the end of the current declarative
7462 -- part.
7463
7464 elsif Is_Generic_Instance (Par)
7465 and then Present (Freeze_Node (Par))
7466 and then Present (Enc_I)
7467 then
7468 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7469 or else
7470 (Nkind (Enc_I) = N_Package_Body
7471 and then
7472 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7473 then
7474 -- The enclosing package may contain several instances. Rather
7475 -- than computing the earliest point at which to insert its freeze
7476 -- node, we place it at the end of the declarative part of the
7477 -- parent of the generic.
7478
7479 Insert_Freeze_Node_For_Instance
7480 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7481 end if;
7482
7483 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7484
7485 elsif Present (Enc_G)
7486 and then Present (Enc_I)
7487 and then Enc_G /= Enc_I
7488 and then Earlier (Inst_Node, Gen_Body)
7489 then
7490 if Nkind (Enc_G) = N_Package_Body then
7491 E_G_Id := Corresponding_Spec (Enc_G);
7492 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7493 E_G_Id :=
7494 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7495 end if;
7496
7497 -- Freeze package that encloses instance, and place node after the
7498 -- package that encloses generic. If enclosing package is already
7499 -- frozen we have to assume it is at the proper place. This may be a
7500 -- potential ABE that requires dynamic checking. Do not add a freeze
7501 -- node if the package that encloses the generic is inside the body
7502 -- that encloses the instance, because the freeze node would be in
7503 -- the wrong scope. Additional contortions needed if the bodies are
7504 -- within a subunit.
7505
7506 declare
7507 Enclosing_Body : Node_Id;
7508
7509 begin
7510 if Nkind (Enc_I) = N_Package_Body_Stub then
7511 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7512 else
7513 Enclosing_Body := Enc_I;
7514 end if;
7515
7516 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7517 Insert_Freeze_Node_For_Instance
7518 (Enc_G, Package_Freeze_Node (Enc_I));
7519 end if;
7520 end;
7521
7522 -- Freeze enclosing subunit before instance
7523
7524 Ensure_Freeze_Node (E_G_Id);
7525
7526 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7527 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7528 end if;
7529
7530 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7531
7532 else
7533 -- If none of the above, insert freeze node at the end of the current
7534 -- declarative part.
7535
7536 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7537 end if;
7538 end Freeze_Subprogram_Body;
7539
7540 ----------------
7541 -- Get_Gen_Id --
7542 ----------------
7543
7544 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7545 begin
7546 return Generic_Renamings.Table (E).Gen_Id;
7547 end Get_Gen_Id;
7548
7549 ---------------------
7550 -- Get_Instance_Of --
7551 ---------------------
7552
7553 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7554 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7555
7556 begin
7557 if Res /= Assoc_Null then
7558 return Generic_Renamings.Table (Res).Act_Id;
7559 else
7560 -- On exit, entity is not instantiated: not a generic parameter, or
7561 -- else parameter of an inner generic unit.
7562
7563 return A;
7564 end if;
7565 end Get_Instance_Of;
7566
7567 ------------------------------------
7568 -- Get_Package_Instantiation_Node --
7569 ------------------------------------
7570
7571 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7572 Decl : Node_Id := Unit_Declaration_Node (A);
7573 Inst : Node_Id;
7574
7575 begin
7576 -- If the Package_Instantiation attribute has been set on the package
7577 -- entity, then use it directly when it (or its Original_Node) refers
7578 -- to an N_Package_Instantiation node. In principle it should be
7579 -- possible to have this field set in all cases, which should be
7580 -- investigated, and would allow this function to be significantly
7581 -- simplified. ???
7582
7583 Inst := Package_Instantiation (A);
7584
7585 if Present (Inst) then
7586 if Nkind (Inst) = N_Package_Instantiation then
7587 return Inst;
7588
7589 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7590 return Original_Node (Inst);
7591 end if;
7592 end if;
7593
7594 -- If the instantiation is a compilation unit that does not need body
7595 -- then the instantiation node has been rewritten as a package
7596 -- declaration for the instance, and we return the original node.
7597
7598 -- If it is a compilation unit and the instance node has not been
7599 -- rewritten, then it is still the unit of the compilation. Finally, if
7600 -- a body is present, this is a parent of the main unit whose body has
7601 -- been compiled for inlining purposes, and the instantiation node has
7602 -- been rewritten with the instance body.
7603
7604 -- Otherwise the instantiation node appears after the declaration. If
7605 -- the entity is a formal package, the declaration may have been
7606 -- rewritten as a generic declaration (in the case of a formal with box)
7607 -- or left as a formal package declaration if it has actuals, and is
7608 -- found with a forward search.
7609
7610 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7611 if Nkind (Decl) = N_Package_Declaration
7612 and then Present (Corresponding_Body (Decl))
7613 then
7614 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7615 end if;
7616
7617 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
7618 return Original_Node (Decl);
7619 else
7620 return Unit (Parent (Decl));
7621 end if;
7622
7623 elsif Nkind (Decl) = N_Package_Declaration
7624 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
7625 then
7626 return Original_Node (Decl);
7627
7628 else
7629 Inst := Next (Decl);
7630 while not Nkind_In (Inst, N_Package_Instantiation,
7631 N_Formal_Package_Declaration)
7632 loop
7633 Next (Inst);
7634 end loop;
7635
7636 return Inst;
7637 end if;
7638 end Get_Package_Instantiation_Node;
7639
7640 ------------------------
7641 -- Has_Been_Exchanged --
7642 ------------------------
7643
7644 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
7645 Next : Elmt_Id;
7646
7647 begin
7648 Next := First_Elmt (Exchanged_Views);
7649 while Present (Next) loop
7650 if Full_View (Node (Next)) = E then
7651 return True;
7652 end if;
7653
7654 Next_Elmt (Next);
7655 end loop;
7656
7657 return False;
7658 end Has_Been_Exchanged;
7659
7660 ----------
7661 -- Hash --
7662 ----------
7663
7664 function Hash (F : Entity_Id) return HTable_Range is
7665 begin
7666 return HTable_Range (F mod HTable_Size);
7667 end Hash;
7668
7669 ------------------------
7670 -- Hide_Current_Scope --
7671 ------------------------
7672
7673 procedure Hide_Current_Scope is
7674 C : constant Entity_Id := Current_Scope;
7675 E : Entity_Id;
7676
7677 begin
7678 Set_Is_Hidden_Open_Scope (C);
7679
7680 E := First_Entity (C);
7681 while Present (E) loop
7682 if Is_Immediately_Visible (E) then
7683 Set_Is_Immediately_Visible (E, False);
7684 Append_Elmt (E, Hidden_Entities);
7685 end if;
7686
7687 Next_Entity (E);
7688 end loop;
7689
7690 -- Make the scope name invisible as well. This is necessary, but might
7691 -- conflict with calls to Rtsfind later on, in case the scope is a
7692 -- predefined one. There is no clean solution to this problem, so for
7693 -- now we depend on the user not redefining Standard itself in one of
7694 -- the parent units.
7695
7696 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
7697 Set_Is_Immediately_Visible (C, False);
7698 Append_Elmt (C, Hidden_Entities);
7699 end if;
7700
7701 end Hide_Current_Scope;
7702
7703 --------------
7704 -- Init_Env --
7705 --------------
7706
7707 procedure Init_Env is
7708 Saved : Instance_Env;
7709
7710 begin
7711 Saved.Instantiated_Parent := Current_Instantiated_Parent;
7712 Saved.Exchanged_Views := Exchanged_Views;
7713 Saved.Hidden_Entities := Hidden_Entities;
7714 Saved.Current_Sem_Unit := Current_Sem_Unit;
7715 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
7716 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
7717
7718 -- Save configuration switches. These may be reset if the unit is a
7719 -- predefined unit, and the current mode is not Ada 2005.
7720
7721 Save_Opt_Config_Switches (Saved.Switches);
7722
7723 Instance_Envs.Append (Saved);
7724
7725 Exchanged_Views := New_Elmt_List;
7726 Hidden_Entities := New_Elmt_List;
7727
7728 -- Make dummy entry for Instantiated parent. If generic unit is legal,
7729 -- this is set properly in Set_Instance_Env.
7730
7731 Current_Instantiated_Parent :=
7732 (Current_Scope, Current_Scope, Assoc_Null);
7733 end Init_Env;
7734
7735 ------------------------------
7736 -- In_Same_Declarative_Part --
7737 ------------------------------
7738
7739 function In_Same_Declarative_Part
7740 (F_Node : Node_Id;
7741 Inst : Node_Id) return Boolean
7742 is
7743 Decls : constant Node_Id := Parent (F_Node);
7744 Nod : Node_Id := Parent (Inst);
7745
7746 begin
7747 while Present (Nod) loop
7748 if Nod = Decls then
7749 return True;
7750
7751 elsif Nkind_In (Nod, N_Subprogram_Body,
7752 N_Package_Body,
7753 N_Package_Declaration,
7754 N_Task_Body,
7755 N_Protected_Body,
7756 N_Block_Statement)
7757 then
7758 return False;
7759
7760 elsif Nkind (Nod) = N_Subunit then
7761 Nod := Corresponding_Stub (Nod);
7762
7763 elsif Nkind (Nod) = N_Compilation_Unit then
7764 return False;
7765
7766 else
7767 Nod := Parent (Nod);
7768 end if;
7769 end loop;
7770
7771 return False;
7772 end In_Same_Declarative_Part;
7773
7774 ---------------------
7775 -- In_Main_Context --
7776 ---------------------
7777
7778 function In_Main_Context (E : Entity_Id) return Boolean is
7779 Context : List_Id;
7780 Clause : Node_Id;
7781 Nam : Node_Id;
7782
7783 begin
7784 if not Is_Compilation_Unit (E)
7785 or else Ekind (E) /= E_Package
7786 or else In_Private_Part (E)
7787 then
7788 return False;
7789 end if;
7790
7791 Context := Context_Items (Cunit (Main_Unit));
7792
7793 Clause := First (Context);
7794 while Present (Clause) loop
7795 if Nkind (Clause) = N_With_Clause then
7796 Nam := Name (Clause);
7797
7798 -- If the current scope is part of the context of the main unit,
7799 -- analysis of the corresponding with_clause is not complete, and
7800 -- the entity is not set. We use the Chars field directly, which
7801 -- might produce false positives in rare cases, but guarantees
7802 -- that we produce all the instance bodies we will need.
7803
7804 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
7805 or else (Nkind (Nam) = N_Selected_Component
7806 and then Chars (Selector_Name (Nam)) = Chars (E))
7807 then
7808 return True;
7809 end if;
7810 end if;
7811
7812 Next (Clause);
7813 end loop;
7814
7815 return False;
7816 end In_Main_Context;
7817
7818 ---------------------
7819 -- Inherit_Context --
7820 ---------------------
7821
7822 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
7823 Current_Context : List_Id;
7824 Current_Unit : Node_Id;
7825 Item : Node_Id;
7826 New_I : Node_Id;
7827
7828 Clause : Node_Id;
7829 OK : Boolean;
7830 Lib_Unit : Node_Id;
7831
7832 begin
7833 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
7834
7835 -- The inherited context is attached to the enclosing compilation
7836 -- unit. This is either the main unit, or the declaration for the
7837 -- main unit (in case the instantiation appears within the package
7838 -- declaration and the main unit is its body).
7839
7840 Current_Unit := Parent (Inst);
7841 while Present (Current_Unit)
7842 and then Nkind (Current_Unit) /= N_Compilation_Unit
7843 loop
7844 Current_Unit := Parent (Current_Unit);
7845 end loop;
7846
7847 Current_Context := Context_Items (Current_Unit);
7848
7849 Item := First (Context_Items (Parent (Gen_Decl)));
7850 while Present (Item) loop
7851 if Nkind (Item) = N_With_Clause then
7852 Lib_Unit := Library_Unit (Item);
7853
7854 -- Take care to prevent direct cyclic with's
7855
7856 if Lib_Unit /= Current_Unit then
7857
7858 -- Do not add a unit if it is already in the context
7859
7860 Clause := First (Current_Context);
7861 OK := True;
7862 while Present (Clause) loop
7863 if Nkind (Clause) = N_With_Clause and then
7864 Library_Unit (Clause) = Lib_Unit
7865 then
7866 OK := False;
7867 exit;
7868 end if;
7869
7870 Next (Clause);
7871 end loop;
7872
7873 if OK then
7874 New_I := New_Copy (Item);
7875 Set_Implicit_With (New_I, True);
7876 Set_Implicit_With_From_Instantiation (New_I, True);
7877 Append (New_I, Current_Context);
7878 end if;
7879 end if;
7880 end if;
7881
7882 Next (Item);
7883 end loop;
7884 end if;
7885 end Inherit_Context;
7886
7887 ----------------
7888 -- Initialize --
7889 ----------------
7890
7891 procedure Initialize is
7892 begin
7893 Generic_Renamings.Init;
7894 Instance_Envs.Init;
7895 Generic_Flags.Init;
7896 Generic_Renamings_HTable.Reset;
7897 Circularity_Detected := False;
7898 Exchanged_Views := No_Elist;
7899 Hidden_Entities := No_Elist;
7900 end Initialize;
7901
7902 -------------------------------------
7903 -- Insert_Freeze_Node_For_Instance --
7904 -------------------------------------
7905
7906 procedure Insert_Freeze_Node_For_Instance
7907 (N : Node_Id;
7908 F_Node : Node_Id)
7909 is
7910 Decl : Node_Id;
7911 Decls : List_Id;
7912 Inst : Entity_Id;
7913 Par_N : Node_Id;
7914
7915 function Enclosing_Body (N : Node_Id) return Node_Id;
7916 -- Find enclosing package or subprogram body, if any. Freeze node may
7917 -- be placed at end of current declarative list if previous instance
7918 -- and current one have different enclosing bodies.
7919
7920 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
7921 -- Find the local instance, if any, that declares the generic that is
7922 -- being instantiated. If present, the freeze node for this instance
7923 -- must follow the freeze node for the previous instance.
7924
7925 --------------------
7926 -- Enclosing_Body --
7927 --------------------
7928
7929 function Enclosing_Body (N : Node_Id) return Node_Id is
7930 P : Node_Id;
7931
7932 begin
7933 P := Parent (N);
7934 while Present (P)
7935 and then Nkind (Parent (P)) /= N_Compilation_Unit
7936 loop
7937 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
7938 if Nkind (Parent (P)) = N_Subunit then
7939 return Corresponding_Stub (Parent (P));
7940 else
7941 return P;
7942 end if;
7943 end if;
7944
7945 P := True_Parent (P);
7946 end loop;
7947
7948 return Empty;
7949 end Enclosing_Body;
7950
7951 -----------------------
7952 -- Previous_Instance --
7953 -----------------------
7954
7955 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
7956 S : Entity_Id;
7957
7958 begin
7959 S := Scope (Gen);
7960 while Present (S)
7961 and then S /= Standard_Standard
7962 loop
7963 if Is_Generic_Instance (S)
7964 and then In_Same_Source_Unit (S, N)
7965 then
7966 return S;
7967 end if;
7968
7969 S := Scope (S);
7970 end loop;
7971
7972 return Empty;
7973 end Previous_Instance;
7974
7975 -- Start of processing for Insert_Freeze_Node_For_Instance
7976
7977 begin
7978 if not Is_List_Member (F_Node) then
7979 Decl := N;
7980 Decls := List_Containing (N);
7981 Inst := Entity (F_Node);
7982 Par_N := Parent (Decls);
7983
7984 -- When processing a subprogram instantiation, utilize the actual
7985 -- subprogram instantiation rather than its package wrapper as it
7986 -- carries all the context information.
7987
7988 if Is_Wrapper_Package (Inst) then
7989 Inst := Related_Instance (Inst);
7990 end if;
7991
7992 -- If this is a package instance, check whether the generic is
7993 -- declared in a previous instance and the current instance is
7994 -- not within the previous one.
7995
7996 if Present (Generic_Parent (Parent (Inst)))
7997 and then Is_In_Main_Unit (N)
7998 then
7999 declare
8000 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8001 Par_I : constant Entity_Id :=
8002 Previous_Instance
8003 (Generic_Parent (Parent (Inst)));
8004 Scop : Entity_Id;
8005
8006 begin
8007 if Present (Par_I)
8008 and then Earlier (N, Freeze_Node (Par_I))
8009 then
8010 Scop := Scope (Inst);
8011
8012 -- If the current instance is within the one that contains
8013 -- the generic, the freeze node for the current one must
8014 -- appear in the current declarative part. Ditto, if the
8015 -- current instance is within another package instance or
8016 -- within a body that does not enclose the current instance.
8017 -- In these three cases the freeze node of the previous
8018 -- instance is not relevant.
8019
8020 while Present (Scop)
8021 and then Scop /= Standard_Standard
8022 loop
8023 exit when Scop = Par_I
8024 or else
8025 (Is_Generic_Instance (Scop)
8026 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8027 Scop := Scope (Scop);
8028 end loop;
8029
8030 -- Previous instance encloses current instance
8031
8032 if Scop = Par_I then
8033 null;
8034
8035 -- If the next node is a source body we must freeze in
8036 -- the current scope as well.
8037
8038 elsif Present (Next (N))
8039 and then Nkind_In (Next (N),
8040 N_Subprogram_Body, N_Package_Body)
8041 and then Comes_From_Source (Next (N))
8042 then
8043 null;
8044
8045 -- Current instance is within an unrelated instance
8046
8047 elsif Is_Generic_Instance (Scop) then
8048 null;
8049
8050 -- Current instance is within an unrelated body
8051
8052 elsif Present (Enclosing_N)
8053 and then Enclosing_N /= Enclosing_Body (Par_I)
8054 then
8055 null;
8056
8057 else
8058 Insert_After (Freeze_Node (Par_I), F_Node);
8059 return;
8060 end if;
8061 end if;
8062 end;
8063 end if;
8064
8065 -- When the instantiation occurs in a package declaration, append the
8066 -- freeze node to the private declarations (if any).
8067
8068 if Nkind (Par_N) = N_Package_Specification
8069 and then Decls = Visible_Declarations (Par_N)
8070 and then Present (Private_Declarations (Par_N))
8071 and then not Is_Empty_List (Private_Declarations (Par_N))
8072 then
8073 Decls := Private_Declarations (Par_N);
8074 Decl := First (Decls);
8075 end if;
8076
8077 -- Determine the proper freeze point of a package instantiation. We
8078 -- adhere to the general rule of a package or subprogram body causing
8079 -- freezing of anything before it in the same declarative region. In
8080 -- this case, the proper freeze point of a package instantiation is
8081 -- before the first source body which follows, or before a stub. This
8082 -- ensures that entities coming from the instance are already frozen
8083 -- and usable in source bodies.
8084
8085 if Nkind (Par_N) /= N_Package_Declaration
8086 and then Ekind (Inst) = E_Package
8087 and then Is_Generic_Instance (Inst)
8088 and then
8089 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8090 then
8091 while Present (Decl) loop
8092 if (Nkind (Decl) in N_Unit_Body
8093 or else
8094 Nkind (Decl) in N_Body_Stub)
8095 and then Comes_From_Source (Decl)
8096 then
8097 Insert_Before (Decl, F_Node);
8098 return;
8099 end if;
8100
8101 Next (Decl);
8102 end loop;
8103 end if;
8104
8105 -- In a package declaration, or if no previous body, insert at end
8106 -- of list.
8107
8108 Set_Sloc (F_Node, Sloc (Last (Decls)));
8109 Insert_After (Last (Decls), F_Node);
8110 end if;
8111 end Insert_Freeze_Node_For_Instance;
8112
8113 ------------------
8114 -- Install_Body --
8115 ------------------
8116
8117 procedure Install_Body
8118 (Act_Body : Node_Id;
8119 N : Node_Id;
8120 Gen_Body : Node_Id;
8121 Gen_Decl : Node_Id)
8122 is
8123 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8124 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8125 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8126 Par : constant Entity_Id := Scope (Gen_Id);
8127 Gen_Unit : constant Node_Id :=
8128 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8129 Orig_Body : Node_Id := Gen_Body;
8130 F_Node : Node_Id;
8131 Body_Unit : Node_Id;
8132
8133 Must_Delay : Boolean;
8134
8135 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
8136 -- Find subprogram (if any) that encloses instance and/or generic body
8137
8138 function True_Sloc (N : Node_Id) return Source_Ptr;
8139 -- If the instance is nested inside a generic unit, the Sloc of the
8140 -- instance indicates the place of the original definition, not the
8141 -- point of the current enclosing instance. Pending a better usage of
8142 -- Slocs to indicate instantiation places, we determine the place of
8143 -- origin of a node by finding the maximum sloc of any ancestor node.
8144 -- Why is this not equivalent to Top_Level_Location ???
8145
8146 --------------------
8147 -- Enclosing_Subp --
8148 --------------------
8149
8150 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
8151 Scop : Entity_Id;
8152
8153 begin
8154 Scop := Scope (Id);
8155 while Scop /= Standard_Standard
8156 and then not Is_Overloadable (Scop)
8157 loop
8158 Scop := Scope (Scop);
8159 end loop;
8160
8161 return Scop;
8162 end Enclosing_Subp;
8163
8164 ---------------
8165 -- True_Sloc --
8166 ---------------
8167
8168 function True_Sloc (N : Node_Id) return Source_Ptr is
8169 Res : Source_Ptr;
8170 N1 : Node_Id;
8171
8172 begin
8173 Res := Sloc (N);
8174 N1 := N;
8175 while Present (N1) and then N1 /= Act_Unit loop
8176 if Sloc (N1) > Res then
8177 Res := Sloc (N1);
8178 end if;
8179
8180 N1 := Parent (N1);
8181 end loop;
8182
8183 return Res;
8184 end True_Sloc;
8185
8186 -- Start of processing for Install_Body
8187
8188 begin
8189 -- If the body is a subunit, the freeze point is the corresponding stub
8190 -- in the current compilation, not the subunit itself.
8191
8192 if Nkind (Parent (Gen_Body)) = N_Subunit then
8193 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8194 else
8195 Orig_Body := Gen_Body;
8196 end if;
8197
8198 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8199
8200 -- If the instantiation and the generic definition appear in the same
8201 -- package declaration, this is an early instantiation. If they appear
8202 -- in the same declarative part, it is an early instantiation only if
8203 -- the generic body appears textually later, and the generic body is
8204 -- also in the main unit.
8205
8206 -- If instance is nested within a subprogram, and the generic body is
8207 -- not, the instance is delayed because the enclosing body is. If
8208 -- instance and body are within the same scope, or the same sub-
8209 -- program body, indicate explicitly that the instance is delayed.
8210
8211 Must_Delay :=
8212 (Gen_Unit = Act_Unit
8213 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8214 N_Generic_Package_Declaration)
8215 or else (Gen_Unit = Body_Unit
8216 and then True_Sloc (N) < Sloc (Orig_Body)))
8217 and then Is_In_Main_Unit (Gen_Unit)
8218 and then (Scope (Act_Id) = Scope (Gen_Id)
8219 or else
8220 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
8221
8222 -- If this is an early instantiation, the freeze node is placed after
8223 -- the generic body. Otherwise, if the generic appears in an instance,
8224 -- we cannot freeze the current instance until the outer one is frozen.
8225 -- This is only relevant if the current instance is nested within some
8226 -- inner scope not itself within the outer instance. If this scope is
8227 -- a package body in the same declarative part as the outer instance,
8228 -- then that body needs to be frozen after the outer instance. Finally,
8229 -- if no delay is needed, we place the freeze node at the end of the
8230 -- current declarative part.
8231
8232 if Expander_Active then
8233 Ensure_Freeze_Node (Act_Id);
8234 F_Node := Freeze_Node (Act_Id);
8235
8236 if Must_Delay then
8237 Insert_After (Orig_Body, F_Node);
8238
8239 elsif Is_Generic_Instance (Par)
8240 and then Present (Freeze_Node (Par))
8241 and then Scope (Act_Id) /= Par
8242 then
8243 -- Freeze instance of inner generic after instance of enclosing
8244 -- generic.
8245
8246 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8247
8248 -- Handle the following case:
8249
8250 -- package Parent_Inst is new ...
8251 -- Parent_Inst []
8252
8253 -- procedure P ... -- this body freezes Parent_Inst
8254
8255 -- package Inst is new ...
8256
8257 -- In this particular scenario, the freeze node for Inst must
8258 -- be inserted in the same manner as that of Parent_Inst -
8259 -- before the next source body or at the end of the declarative
8260 -- list (body not available). If body P did not exist and
8261 -- Parent_Inst was frozen after Inst, either by a body
8262 -- following Inst or at the end of the declarative region, the
8263 -- freeze node for Inst must be inserted after that of
8264 -- Parent_Inst. This relation is established by comparing the
8265 -- Slocs of Parent_Inst freeze node and Inst.
8266
8267 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8268 List_Containing (N)
8269 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8270 then
8271 Insert_Freeze_Node_For_Instance (N, F_Node);
8272 else
8273 Insert_After (Freeze_Node (Par), F_Node);
8274 end if;
8275
8276 -- Freeze package enclosing instance of inner generic after
8277 -- instance of enclosing generic.
8278
8279 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8280 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8281 then
8282 declare
8283 Enclosing : Entity_Id;
8284
8285 begin
8286 Enclosing := Corresponding_Spec (Parent (N));
8287
8288 if No (Enclosing) then
8289 Enclosing := Defining_Entity (Parent (N));
8290 end if;
8291
8292 Insert_Freeze_Node_For_Instance (N, F_Node);
8293 Ensure_Freeze_Node (Enclosing);
8294
8295 if not Is_List_Member (Freeze_Node (Enclosing)) then
8296
8297 -- The enclosing context is a subunit, insert the freeze
8298 -- node after the stub.
8299
8300 if Nkind (Parent (Parent (N))) = N_Subunit then
8301 Insert_Freeze_Node_For_Instance
8302 (Corresponding_Stub (Parent (Parent (N))),
8303 Freeze_Node (Enclosing));
8304
8305 -- The enclosing context is a package with a stub body
8306 -- which has already been replaced by the real body.
8307 -- Insert the freeze node after the actual body.
8308
8309 elsif Ekind (Enclosing) = E_Package
8310 and then Present (Body_Entity (Enclosing))
8311 and then Was_Originally_Stub
8312 (Parent (Body_Entity (Enclosing)))
8313 then
8314 Insert_Freeze_Node_For_Instance
8315 (Parent (Body_Entity (Enclosing)),
8316 Freeze_Node (Enclosing));
8317
8318 -- The parent instance has been frozen before the body of
8319 -- the enclosing package, insert the freeze node after
8320 -- the body.
8321
8322 elsif List_Containing (Freeze_Node (Par)) =
8323 List_Containing (Parent (N))
8324 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8325 then
8326 Insert_Freeze_Node_For_Instance
8327 (Parent (N), Freeze_Node (Enclosing));
8328
8329 else
8330 Insert_After
8331 (Freeze_Node (Par), Freeze_Node (Enclosing));
8332 end if;
8333 end if;
8334 end;
8335
8336 else
8337 Insert_Freeze_Node_For_Instance (N, F_Node);
8338 end if;
8339
8340 else
8341 Insert_Freeze_Node_For_Instance (N, F_Node);
8342 end if;
8343 end if;
8344
8345 Set_Is_Frozen (Act_Id);
8346 Insert_Before (N, Act_Body);
8347 Mark_Rewrite_Insertion (Act_Body);
8348 end Install_Body;
8349
8350 -----------------------------
8351 -- Install_Formal_Packages --
8352 -----------------------------
8353
8354 procedure Install_Formal_Packages (Par : Entity_Id) is
8355 E : Entity_Id;
8356 Gen : Entity_Id;
8357 Gen_E : Entity_Id := Empty;
8358
8359 begin
8360 E := First_Entity (Par);
8361
8362 -- If we are installing an instance parent, locate the formal packages
8363 -- of its generic parent.
8364
8365 if Is_Generic_Instance (Par) then
8366 Gen := Generic_Parent (Package_Specification (Par));
8367 Gen_E := First_Entity (Gen);
8368 end if;
8369
8370 while Present (E) loop
8371 if Ekind (E) = E_Package
8372 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
8373 then
8374 -- If this is the renaming for the parent instance, done
8375
8376 if Renamed_Object (E) = Par then
8377 exit;
8378
8379 -- The visibility of a formal of an enclosing generic is already
8380 -- correct.
8381
8382 elsif Denotes_Formal_Package (E) then
8383 null;
8384
8385 elsif Present (Associated_Formal_Package (E)) then
8386 Check_Generic_Actuals (Renamed_Object (E), True);
8387 Set_Is_Hidden (E, False);
8388
8389 -- Find formal package in generic unit that corresponds to
8390 -- (instance of) formal package in instance.
8391
8392 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
8393 Next_Entity (Gen_E);
8394 end loop;
8395
8396 if Present (Gen_E) then
8397 Map_Formal_Package_Entities (Gen_E, E);
8398 end if;
8399 end if;
8400 end if;
8401
8402 Next_Entity (E);
8403 if Present (Gen_E) then
8404 Next_Entity (Gen_E);
8405 end if;
8406 end loop;
8407 end Install_Formal_Packages;
8408
8409 --------------------
8410 -- Install_Parent --
8411 --------------------
8412
8413 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
8414 Ancestors : constant Elist_Id := New_Elmt_List;
8415 S : constant Entity_Id := Current_Scope;
8416 Inst_Par : Entity_Id;
8417 First_Par : Entity_Id;
8418 Inst_Node : Node_Id;
8419 Gen_Par : Entity_Id;
8420 First_Gen : Entity_Id;
8421 Elmt : Elmt_Id;
8422
8423 procedure Install_Noninstance_Specs (Par : Entity_Id);
8424 -- Install the scopes of noninstance parent units ending with Par
8425
8426 procedure Install_Spec (Par : Entity_Id);
8427 -- The child unit is within the declarative part of the parent, so the
8428 -- declarations within the parent are immediately visible.
8429
8430 -------------------------------
8431 -- Install_Noninstance_Specs --
8432 -------------------------------
8433
8434 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8435 begin
8436 if Present (Par)
8437 and then Par /= Standard_Standard
8438 and then not In_Open_Scopes (Par)
8439 then
8440 Install_Noninstance_Specs (Scope (Par));
8441 Install_Spec (Par);
8442 end if;
8443 end Install_Noninstance_Specs;
8444
8445 ------------------
8446 -- Install_Spec --
8447 ------------------
8448
8449 procedure Install_Spec (Par : Entity_Id) is
8450 Spec : constant Node_Id := Package_Specification (Par);
8451
8452 begin
8453 -- If this parent of the child instance is a top-level unit,
8454 -- then record the unit and its visibility for later resetting in
8455 -- Remove_Parent. We exclude units that are generic instances, as we
8456 -- only want to record this information for the ultimate top-level
8457 -- noninstance parent (is that always correct???).
8458
8459 if Scope (Par) = Standard_Standard
8460 and then not Is_Generic_Instance (Par)
8461 then
8462 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8463 Instance_Parent_Unit := Par;
8464 end if;
8465
8466 -- Open the parent scope and make it and its declarations visible.
8467 -- If this point is not within a body, then only the visible
8468 -- declarations should be made visible, and installation of the
8469 -- private declarations is deferred until the appropriate point
8470 -- within analysis of the spec being instantiated (see the handling
8471 -- of parent visibility in Analyze_Package_Specification). This is
8472 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8473 -- private view problems that occur when compiling instantiations of
8474 -- a generic child of that package (Generic_Dispatching_Constructor).
8475 -- If the instance freezes a tagged type, inlinings of operations
8476 -- from Ada.Tags may need the full view of type Tag. If inlining took
8477 -- proper account of establishing visibility of inlined subprograms'
8478 -- parents then it should be possible to remove this
8479 -- special check. ???
8480
8481 Push_Scope (Par);
8482 Set_Is_Immediately_Visible (Par);
8483 Install_Visible_Declarations (Par);
8484 Set_Use (Visible_Declarations (Spec));
8485
8486 if In_Body or else Is_RTU (Par, Ada_Tags) then
8487 Install_Private_Declarations (Par);
8488 Set_Use (Private_Declarations (Spec));
8489 end if;
8490 end Install_Spec;
8491
8492 -- Start of processing for Install_Parent
8493
8494 begin
8495 -- We need to install the parent instance to compile the instantiation
8496 -- of the child, but the child instance must appear in the current
8497 -- scope. Given that we cannot place the parent above the current scope
8498 -- in the scope stack, we duplicate the current scope and unstack both
8499 -- after the instantiation is complete.
8500
8501 -- If the parent is itself the instantiation of a child unit, we must
8502 -- also stack the instantiation of its parent, and so on. Each such
8503 -- ancestor is the prefix of the name in a prior instantiation.
8504
8505 -- If this is a nested instance, the parent unit itself resolves to
8506 -- a renaming of the parent instance, whose declaration we need.
8507
8508 -- Finally, the parent may be a generic (not an instance) when the
8509 -- child unit appears as a formal package.
8510
8511 Inst_Par := P;
8512
8513 if Present (Renamed_Entity (Inst_Par)) then
8514 Inst_Par := Renamed_Entity (Inst_Par);
8515 end if;
8516
8517 First_Par := Inst_Par;
8518
8519 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8520
8521 First_Gen := Gen_Par;
8522
8523 while Present (Gen_Par)
8524 and then Is_Child_Unit (Gen_Par)
8525 loop
8526 -- Load grandparent instance as well
8527
8528 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8529
8530 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8531 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8532
8533 if Present (Renamed_Entity (Inst_Par)) then
8534 Inst_Par := Renamed_Entity (Inst_Par);
8535 end if;
8536
8537 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8538
8539 if Present (Gen_Par) then
8540 Prepend_Elmt (Inst_Par, Ancestors);
8541
8542 else
8543 -- Parent is not the name of an instantiation
8544
8545 Install_Noninstance_Specs (Inst_Par);
8546 exit;
8547 end if;
8548
8549 else
8550 -- Previous error
8551
8552 exit;
8553 end if;
8554 end loop;
8555
8556 if Present (First_Gen) then
8557 Append_Elmt (First_Par, Ancestors);
8558 else
8559 Install_Noninstance_Specs (First_Par);
8560 end if;
8561
8562 if not Is_Empty_Elmt_List (Ancestors) then
8563 Elmt := First_Elmt (Ancestors);
8564 while Present (Elmt) loop
8565 Install_Spec (Node (Elmt));
8566 Install_Formal_Packages (Node (Elmt));
8567 Next_Elmt (Elmt);
8568 end loop;
8569 end if;
8570
8571 if not In_Body then
8572 Push_Scope (S);
8573 end if;
8574 end Install_Parent;
8575
8576 -------------------------------
8577 -- Install_Hidden_Primitives --
8578 -------------------------------
8579
8580 procedure Install_Hidden_Primitives
8581 (Prims_List : in out Elist_Id;
8582 Gen_T : Entity_Id;
8583 Act_T : Entity_Id)
8584 is
8585 Elmt : Elmt_Id;
8586 List : Elist_Id := No_Elist;
8587 Prim_G_Elmt : Elmt_Id;
8588 Prim_A_Elmt : Elmt_Id;
8589 Prim_G : Node_Id;
8590 Prim_A : Node_Id;
8591
8592 begin
8593 -- No action needed in case of serious errors because we cannot trust
8594 -- in the order of primitives
8595
8596 if Serious_Errors_Detected > 0 then
8597 return;
8598
8599 -- No action possible if we don't have available the list of primitive
8600 -- operations
8601
8602 elsif No (Gen_T)
8603 or else not Is_Record_Type (Gen_T)
8604 or else not Is_Tagged_Type (Gen_T)
8605 or else not Is_Record_Type (Act_T)
8606 or else not Is_Tagged_Type (Act_T)
8607 then
8608 return;
8609
8610 -- There is no need to handle interface types since their primitives
8611 -- cannot be hidden
8612
8613 elsif Is_Interface (Gen_T) then
8614 return;
8615 end if;
8616
8617 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
8618
8619 if not Is_Class_Wide_Type (Act_T) then
8620 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
8621 else
8622 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
8623 end if;
8624
8625 loop
8626 -- Skip predefined primitives in the generic formal
8627
8628 while Present (Prim_G_Elmt)
8629 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
8630 loop
8631 Next_Elmt (Prim_G_Elmt);
8632 end loop;
8633
8634 -- Skip predefined primitives in the generic actual
8635
8636 while Present (Prim_A_Elmt)
8637 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
8638 loop
8639 Next_Elmt (Prim_A_Elmt);
8640 end loop;
8641
8642 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
8643
8644 Prim_G := Node (Prim_G_Elmt);
8645 Prim_A := Node (Prim_A_Elmt);
8646
8647 -- There is no need to handle interface primitives because their
8648 -- primitives are not hidden
8649
8650 exit when Present (Interface_Alias (Prim_G));
8651
8652 -- Here we install one hidden primitive
8653
8654 if Chars (Prim_G) /= Chars (Prim_A)
8655 and then Has_Suffix (Prim_A, 'P')
8656 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
8657 then
8658 Set_Chars (Prim_A, Chars (Prim_G));
8659
8660 if List = No_Elist then
8661 List := New_Elmt_List;
8662 end if;
8663
8664 Append_Elmt (Prim_A, List);
8665 end if;
8666
8667 Next_Elmt (Prim_A_Elmt);
8668 Next_Elmt (Prim_G_Elmt);
8669 end loop;
8670
8671 -- Append the elements to the list of temporarily visible primitives
8672 -- avoiding duplicates.
8673
8674 if Present (List) then
8675 if No (Prims_List) then
8676 Prims_List := New_Elmt_List;
8677 end if;
8678
8679 Elmt := First_Elmt (List);
8680 while Present (Elmt) loop
8681 Append_Unique_Elmt (Node (Elmt), Prims_List);
8682 Next_Elmt (Elmt);
8683 end loop;
8684 end if;
8685 end Install_Hidden_Primitives;
8686
8687 -------------------------------
8688 -- Restore_Hidden_Primitives --
8689 -------------------------------
8690
8691 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
8692 Prim_Elmt : Elmt_Id;
8693 Prim : Node_Id;
8694
8695 begin
8696 if Prims_List /= No_Elist then
8697 Prim_Elmt := First_Elmt (Prims_List);
8698 while Present (Prim_Elmt) loop
8699 Prim := Node (Prim_Elmt);
8700 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
8701 Next_Elmt (Prim_Elmt);
8702 end loop;
8703
8704 Prims_List := No_Elist;
8705 end if;
8706 end Restore_Hidden_Primitives;
8707
8708 --------------------------------
8709 -- Instantiate_Formal_Package --
8710 --------------------------------
8711
8712 function Instantiate_Formal_Package
8713 (Formal : Node_Id;
8714 Actual : Node_Id;
8715 Analyzed_Formal : Node_Id) return List_Id
8716 is
8717 Loc : constant Source_Ptr := Sloc (Actual);
8718 Actual_Pack : Entity_Id;
8719 Formal_Pack : Entity_Id;
8720 Gen_Parent : Entity_Id;
8721 Decls : List_Id;
8722 Nod : Node_Id;
8723 Parent_Spec : Node_Id;
8724
8725 procedure Find_Matching_Actual
8726 (F : Node_Id;
8727 Act : in out Entity_Id);
8728 -- We need to associate each formal entity in the formal package with
8729 -- the corresponding entity in the actual package. The actual package
8730 -- has been analyzed and possibly expanded, and as a result there is
8731 -- no one-to-one correspondence between the two lists (for example,
8732 -- the actual may include subtypes, itypes, and inherited primitive
8733 -- operations, interspersed among the renaming declarations for the
8734 -- actuals) . We retrieve the corresponding actual by name because each
8735 -- actual has the same name as the formal, and they do appear in the
8736 -- same order.
8737
8738 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
8739 -- Retrieve entity of defining entity of generic formal parameter.
8740 -- Only the declarations of formals need to be considered when
8741 -- linking them to actuals, but the declarative list may include
8742 -- internal entities generated during analysis, and those are ignored.
8743
8744 procedure Match_Formal_Entity
8745 (Formal_Node : Node_Id;
8746 Formal_Ent : Entity_Id;
8747 Actual_Ent : Entity_Id);
8748 -- Associates the formal entity with the actual. In the case where
8749 -- Formal_Ent is a formal package, this procedure iterates through all
8750 -- of its formals and enters associations between the actuals occurring
8751 -- in the formal package's corresponding actual package (given by
8752 -- Actual_Ent) and the formal package's formal parameters. This
8753 -- procedure recurses if any of the parameters is itself a package.
8754
8755 function Is_Instance_Of
8756 (Act_Spec : Entity_Id;
8757 Gen_Anc : Entity_Id) return Boolean;
8758 -- The actual can be an instantiation of a generic within another
8759 -- instance, in which case there is no direct link from it to the
8760 -- original generic ancestor. In that case, we recognize that the
8761 -- ultimate ancestor is the same by examining names and scopes.
8762
8763 procedure Process_Nested_Formal (Formal : Entity_Id);
8764 -- If the current formal is declared with a box, its own formals are
8765 -- visible in the instance, as they were in the generic, and their
8766 -- Hidden flag must be reset. If some of these formals are themselves
8767 -- packages declared with a box, the processing must be recursive.
8768
8769 --------------------------
8770 -- Find_Matching_Actual --
8771 --------------------------
8772
8773 procedure Find_Matching_Actual
8774 (F : Node_Id;
8775 Act : in out Entity_Id)
8776 is
8777 Formal_Ent : Entity_Id;
8778
8779 begin
8780 case Nkind (Original_Node (F)) is
8781 when N_Formal_Object_Declaration |
8782 N_Formal_Type_Declaration =>
8783 Formal_Ent := Defining_Identifier (F);
8784
8785 while Chars (Act) /= Chars (Formal_Ent) loop
8786 Next_Entity (Act);
8787 end loop;
8788
8789 when N_Formal_Subprogram_Declaration |
8790 N_Formal_Package_Declaration |
8791 N_Package_Declaration |
8792 N_Generic_Package_Declaration =>
8793 Formal_Ent := Defining_Entity (F);
8794
8795 while Chars (Act) /= Chars (Formal_Ent) loop
8796 Next_Entity (Act);
8797 end loop;
8798
8799 when others =>
8800 raise Program_Error;
8801 end case;
8802 end Find_Matching_Actual;
8803
8804 -------------------------
8805 -- Match_Formal_Entity --
8806 -------------------------
8807
8808 procedure Match_Formal_Entity
8809 (Formal_Node : Node_Id;
8810 Formal_Ent : Entity_Id;
8811 Actual_Ent : Entity_Id)
8812 is
8813 Act_Pkg : Entity_Id;
8814
8815 begin
8816 Set_Instance_Of (Formal_Ent, Actual_Ent);
8817
8818 if Ekind (Actual_Ent) = E_Package then
8819
8820 -- Record associations for each parameter
8821
8822 Act_Pkg := Actual_Ent;
8823
8824 declare
8825 A_Ent : Entity_Id := First_Entity (Act_Pkg);
8826 F_Ent : Entity_Id;
8827 F_Node : Node_Id;
8828
8829 Gen_Decl : Node_Id;
8830 Formals : List_Id;
8831 Actual : Entity_Id;
8832
8833 begin
8834 -- Retrieve the actual given in the formal package declaration
8835
8836 Actual := Entity (Name (Original_Node (Formal_Node)));
8837
8838 -- The actual in the formal package declaration may be a
8839 -- renamed generic package, in which case we want to retrieve
8840 -- the original generic in order to traverse its formal part.
8841
8842 if Present (Renamed_Entity (Actual)) then
8843 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
8844 else
8845 Gen_Decl := Unit_Declaration_Node (Actual);
8846 end if;
8847
8848 Formals := Generic_Formal_Declarations (Gen_Decl);
8849
8850 if Present (Formals) then
8851 F_Node := First_Non_Pragma (Formals);
8852 else
8853 F_Node := Empty;
8854 end if;
8855
8856 while Present (A_Ent)
8857 and then Present (F_Node)
8858 and then A_Ent /= First_Private_Entity (Act_Pkg)
8859 loop
8860 F_Ent := Get_Formal_Entity (F_Node);
8861
8862 if Present (F_Ent) then
8863
8864 -- This is a formal of the original package. Record
8865 -- association and recurse.
8866
8867 Find_Matching_Actual (F_Node, A_Ent);
8868 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
8869 Next_Entity (A_Ent);
8870 end if;
8871
8872 Next_Non_Pragma (F_Node);
8873 end loop;
8874 end;
8875 end if;
8876 end Match_Formal_Entity;
8877
8878 -----------------------
8879 -- Get_Formal_Entity --
8880 -----------------------
8881
8882 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
8883 Kind : constant Node_Kind := Nkind (Original_Node (N));
8884 begin
8885 case Kind is
8886 when N_Formal_Object_Declaration =>
8887 return Defining_Identifier (N);
8888
8889 when N_Formal_Type_Declaration =>
8890 return Defining_Identifier (N);
8891
8892 when N_Formal_Subprogram_Declaration =>
8893 return Defining_Unit_Name (Specification (N));
8894
8895 when N_Formal_Package_Declaration =>
8896 return Defining_Identifier (Original_Node (N));
8897
8898 when N_Generic_Package_Declaration =>
8899 return Defining_Identifier (Original_Node (N));
8900
8901 -- All other declarations are introduced by semantic analysis and
8902 -- have no match in the actual.
8903
8904 when others =>
8905 return Empty;
8906 end case;
8907 end Get_Formal_Entity;
8908
8909 --------------------
8910 -- Is_Instance_Of --
8911 --------------------
8912
8913 function Is_Instance_Of
8914 (Act_Spec : Entity_Id;
8915 Gen_Anc : Entity_Id) return Boolean
8916 is
8917 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
8918
8919 begin
8920 if No (Gen_Par) then
8921 return False;
8922
8923 -- Simplest case: the generic parent of the actual is the formal
8924
8925 elsif Gen_Par = Gen_Anc then
8926 return True;
8927
8928 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
8929 return False;
8930
8931 -- The actual may be obtained through several instantiations. Its
8932 -- scope must itself be an instance of a generic declared in the
8933 -- same scope as the formal. Any other case is detected above.
8934
8935 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
8936 return False;
8937
8938 else
8939 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
8940 end if;
8941 end Is_Instance_Of;
8942
8943 ---------------------------
8944 -- Process_Nested_Formal --
8945 ---------------------------
8946
8947 procedure Process_Nested_Formal (Formal : Entity_Id) is
8948 Ent : Entity_Id;
8949
8950 begin
8951 if Present (Associated_Formal_Package (Formal))
8952 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
8953 then
8954 Ent := First_Entity (Formal);
8955 while Present (Ent) loop
8956 Set_Is_Hidden (Ent, False);
8957 Set_Is_Visible_Formal (Ent);
8958 Set_Is_Potentially_Use_Visible
8959 (Ent, Is_Potentially_Use_Visible (Formal));
8960
8961 if Ekind (Ent) = E_Package then
8962 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
8963 Process_Nested_Formal (Ent);
8964 end if;
8965
8966 Next_Entity (Ent);
8967 end loop;
8968 end if;
8969 end Process_Nested_Formal;
8970
8971 -- Start of processing for Instantiate_Formal_Package
8972
8973 begin
8974 Analyze (Actual);
8975
8976 if not Is_Entity_Name (Actual)
8977 or else Ekind (Entity (Actual)) /= E_Package
8978 then
8979 Error_Msg_N
8980 ("expect package instance to instantiate formal", Actual);
8981 Abandon_Instantiation (Actual);
8982 raise Program_Error;
8983
8984 else
8985 Actual_Pack := Entity (Actual);
8986 Set_Is_Instantiated (Actual_Pack);
8987
8988 -- The actual may be a renamed package, or an outer generic formal
8989 -- package whose instantiation is converted into a renaming.
8990
8991 if Present (Renamed_Object (Actual_Pack)) then
8992 Actual_Pack := Renamed_Object (Actual_Pack);
8993 end if;
8994
8995 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
8996 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
8997 Formal_Pack := Defining_Identifier (Analyzed_Formal);
8998 else
8999 Gen_Parent :=
9000 Generic_Parent (Specification (Analyzed_Formal));
9001 Formal_Pack :=
9002 Defining_Unit_Name (Specification (Analyzed_Formal));
9003 end if;
9004
9005 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9006 Parent_Spec := Package_Specification (Actual_Pack);
9007 else
9008 Parent_Spec := Parent (Actual_Pack);
9009 end if;
9010
9011 if Gen_Parent = Any_Id then
9012 Error_Msg_N
9013 ("previous error in declaration of formal package", Actual);
9014 Abandon_Instantiation (Actual);
9015
9016 elsif
9017 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9018 then
9019 null;
9020
9021 else
9022 Error_Msg_NE
9023 ("actual parameter must be instance of&", Actual, Gen_Parent);
9024 Abandon_Instantiation (Actual);
9025 end if;
9026
9027 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9028 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9029
9030 Nod :=
9031 Make_Package_Renaming_Declaration (Loc,
9032 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9033 Name => New_Reference_To (Actual_Pack, Loc));
9034
9035 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
9036 Defining_Identifier (Formal));
9037 Decls := New_List (Nod);
9038
9039 -- If the formal F has a box, then the generic declarations are
9040 -- visible in the generic G. In an instance of G, the corresponding
9041 -- entities in the actual for F (which are the actuals for the
9042 -- instantiation of the generic that F denotes) must also be made
9043 -- visible for analysis of the current instance. On exit from the
9044 -- current instance, those entities are made private again. If the
9045 -- actual is currently in use, these entities are also use-visible.
9046
9047 -- The loop through the actual entities also steps through the formal
9048 -- entities and enters associations from formals to actuals into the
9049 -- renaming map. This is necessary to properly handle checking of
9050 -- actual parameter associations for later formals that depend on
9051 -- actuals declared in the formal package.
9052
9053 -- In Ada 2005, partial parametrization requires that we make visible
9054 -- the actuals corresponding to formals that were defaulted in the
9055 -- formal package. There formals are identified because they remain
9056 -- formal generics within the formal package, rather than being
9057 -- renamings of the actuals supplied.
9058
9059 declare
9060 Gen_Decl : constant Node_Id :=
9061 Unit_Declaration_Node (Gen_Parent);
9062 Formals : constant List_Id :=
9063 Generic_Formal_Declarations (Gen_Decl);
9064
9065 Actual_Ent : Entity_Id;
9066 Actual_Of_Formal : Node_Id;
9067 Formal_Node : Node_Id;
9068 Formal_Ent : Entity_Id;
9069
9070 begin
9071 if Present (Formals) then
9072 Formal_Node := First_Non_Pragma (Formals);
9073 else
9074 Formal_Node := Empty;
9075 end if;
9076
9077 Actual_Ent := First_Entity (Actual_Pack);
9078 Actual_Of_Formal :=
9079 First (Visible_Declarations (Specification (Analyzed_Formal)));
9080 while Present (Actual_Ent)
9081 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9082 loop
9083 if Present (Formal_Node) then
9084 Formal_Ent := Get_Formal_Entity (Formal_Node);
9085
9086 if Present (Formal_Ent) then
9087 Find_Matching_Actual (Formal_Node, Actual_Ent);
9088 Match_Formal_Entity
9089 (Formal_Node, Formal_Ent, Actual_Ent);
9090
9091 -- We iterate at the same time over the actuals of the
9092 -- local package created for the formal, to determine
9093 -- which one of the formals of the original generic were
9094 -- defaulted in the formal. The corresponding actual
9095 -- entities are visible in the enclosing instance.
9096
9097 if Box_Present (Formal)
9098 or else
9099 (Present (Actual_Of_Formal)
9100 and then
9101 Is_Generic_Formal
9102 (Get_Formal_Entity (Actual_Of_Formal)))
9103 then
9104 Set_Is_Hidden (Actual_Ent, False);
9105 Set_Is_Visible_Formal (Actual_Ent);
9106 Set_Is_Potentially_Use_Visible
9107 (Actual_Ent, In_Use (Actual_Pack));
9108
9109 if Ekind (Actual_Ent) = E_Package then
9110 Process_Nested_Formal (Actual_Ent);
9111 end if;
9112
9113 else
9114 Set_Is_Hidden (Actual_Ent);
9115 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9116 end if;
9117 end if;
9118
9119 Next_Non_Pragma (Formal_Node);
9120 Next (Actual_Of_Formal);
9121
9122 else
9123 -- No further formals to match, but the generic part may
9124 -- contain inherited operation that are not hidden in the
9125 -- enclosing instance.
9126
9127 Next_Entity (Actual_Ent);
9128 end if;
9129 end loop;
9130
9131 -- Inherited subprograms generated by formal derived types are
9132 -- also visible if the types are.
9133
9134 Actual_Ent := First_Entity (Actual_Pack);
9135 while Present (Actual_Ent)
9136 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9137 loop
9138 if Is_Overloadable (Actual_Ent)
9139 and then
9140 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9141 and then
9142 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9143 then
9144 Set_Is_Hidden (Actual_Ent, False);
9145 Set_Is_Potentially_Use_Visible
9146 (Actual_Ent, In_Use (Actual_Pack));
9147 end if;
9148
9149 Next_Entity (Actual_Ent);
9150 end loop;
9151 end;
9152
9153 -- If the formal is not declared with a box, reanalyze it as an
9154 -- abbreviated instantiation, to verify the matching rules of 12.7.
9155 -- The actual checks are performed after the generic associations
9156 -- have been analyzed, to guarantee the same visibility for this
9157 -- instantiation and for the actuals.
9158
9159 -- In Ada 2005, the generic associations for the formal can include
9160 -- defaulted parameters. These are ignored during check. This
9161 -- internal instantiation is removed from the tree after conformance
9162 -- checking, because it contains formal declarations for those
9163 -- defaulted parameters, and those should not reach the back-end.
9164
9165 if not Box_Present (Formal) then
9166 declare
9167 I_Pack : constant Entity_Id :=
9168 Make_Temporary (Sloc (Actual), 'P');
9169
9170 begin
9171 Set_Is_Internal (I_Pack);
9172
9173 Append_To (Decls,
9174 Make_Package_Instantiation (Sloc (Actual),
9175 Defining_Unit_Name => I_Pack,
9176 Name =>
9177 New_Occurrence_Of
9178 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9179 Generic_Associations =>
9180 Generic_Associations (Formal)));
9181 end;
9182 end if;
9183
9184 return Decls;
9185 end if;
9186 end Instantiate_Formal_Package;
9187
9188 -----------------------------------
9189 -- Instantiate_Formal_Subprogram --
9190 -----------------------------------
9191
9192 function Instantiate_Formal_Subprogram
9193 (Formal : Node_Id;
9194 Actual : Node_Id;
9195 Analyzed_Formal : Node_Id) return Node_Id
9196 is
9197 Loc : Source_Ptr;
9198 Formal_Sub : constant Entity_Id :=
9199 Defining_Unit_Name (Specification (Formal));
9200 Analyzed_S : constant Entity_Id :=
9201 Defining_Unit_Name (Specification (Analyzed_Formal));
9202 Decl_Node : Node_Id;
9203 Nam : Node_Id;
9204 New_Spec : Node_Id;
9205
9206 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9207 -- If the generic is a child unit, the parent has been installed on the
9208 -- scope stack, but a default subprogram cannot resolve to something
9209 -- on the parent because that parent is not really part of the visible
9210 -- context (it is there to resolve explicit local entities). If the
9211 -- default has resolved in this way, we remove the entity from immediate
9212 -- visibility and analyze the node again to emit an error message or
9213 -- find another visible candidate.
9214
9215 procedure Valid_Actual_Subprogram (Act : Node_Id);
9216 -- Perform legality check and raise exception on failure
9217
9218 -----------------------
9219 -- From_Parent_Scope --
9220 -----------------------
9221
9222 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9223 Gen_Scope : Node_Id;
9224
9225 begin
9226 Gen_Scope := Scope (Analyzed_S);
9227 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9228 if Scope (Subp) = Scope (Gen_Scope) then
9229 return True;
9230 end if;
9231
9232 Gen_Scope := Scope (Gen_Scope);
9233 end loop;
9234
9235 return False;
9236 end From_Parent_Scope;
9237
9238 -----------------------------
9239 -- Valid_Actual_Subprogram --
9240 -----------------------------
9241
9242 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9243 Act_E : Entity_Id;
9244
9245 begin
9246 if Is_Entity_Name (Act) then
9247 Act_E := Entity (Act);
9248
9249 elsif Nkind (Act) = N_Selected_Component
9250 and then Is_Entity_Name (Selector_Name (Act))
9251 then
9252 Act_E := Entity (Selector_Name (Act));
9253
9254 else
9255 Act_E := Empty;
9256 end if;
9257
9258 if (Present (Act_E) and then Is_Overloadable (Act_E))
9259 or else Nkind_In (Act, N_Attribute_Reference,
9260 N_Indexed_Component,
9261 N_Character_Literal,
9262 N_Explicit_Dereference)
9263 then
9264 return;
9265 end if;
9266
9267 Error_Msg_NE
9268 ("expect subprogram or entry name in instantiation of&",
9269 Instantiation_Node, Formal_Sub);
9270 Abandon_Instantiation (Instantiation_Node);
9271
9272 end Valid_Actual_Subprogram;
9273
9274 -- Start of processing for Instantiate_Formal_Subprogram
9275
9276 begin
9277 New_Spec := New_Copy_Tree (Specification (Formal));
9278
9279 -- The tree copy has created the proper instantiation sloc for the
9280 -- new specification. Use this location for all other constructed
9281 -- declarations.
9282
9283 Loc := Sloc (Defining_Unit_Name (New_Spec));
9284
9285 -- Create new entity for the actual (New_Copy_Tree does not)
9286
9287 Set_Defining_Unit_Name
9288 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9289
9290 -- Create new entities for the each of the formals in the
9291 -- specification of the renaming declaration built for the actual.
9292
9293 if Present (Parameter_Specifications (New_Spec)) then
9294 declare
9295 F : Node_Id;
9296 begin
9297 F := First (Parameter_Specifications (New_Spec));
9298 while Present (F) loop
9299 Set_Defining_Identifier (F,
9300 Make_Defining_Identifier (Sloc (F),
9301 Chars => Chars (Defining_Identifier (F))));
9302 Next (F);
9303 end loop;
9304 end;
9305 end if;
9306
9307 -- Find entity of actual. If the actual is an attribute reference, it
9308 -- cannot be resolved here (its formal is missing) but is handled
9309 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9310 -- fully resolved subsequently, when the renaming declaration for the
9311 -- formal is analyzed. If it is an explicit dereference, resolve the
9312 -- prefix but not the actual itself, to prevent interpretation as call.
9313
9314 if Present (Actual) then
9315 Loc := Sloc (Actual);
9316 Set_Sloc (New_Spec, Loc);
9317
9318 if Nkind (Actual) = N_Operator_Symbol then
9319 Find_Direct_Name (Actual);
9320
9321 elsif Nkind (Actual) = N_Explicit_Dereference then
9322 Analyze (Prefix (Actual));
9323
9324 elsif Nkind (Actual) /= N_Attribute_Reference then
9325 Analyze (Actual);
9326 end if;
9327
9328 Valid_Actual_Subprogram (Actual);
9329 Nam := Actual;
9330
9331 elsif Present (Default_Name (Formal)) then
9332 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9333 N_Selected_Component,
9334 N_Indexed_Component,
9335 N_Character_Literal)
9336 and then Present (Entity (Default_Name (Formal)))
9337 then
9338 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9339 else
9340 Nam := New_Copy (Default_Name (Formal));
9341 Set_Sloc (Nam, Loc);
9342 end if;
9343
9344 elsif Box_Present (Formal) then
9345
9346 -- Actual is resolved at the point of instantiation. Create an
9347 -- identifier or operator with the same name as the formal.
9348
9349 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
9350 Nam := Make_Operator_Symbol (Loc,
9351 Chars => Chars (Formal_Sub),
9352 Strval => No_String);
9353 else
9354 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
9355 end if;
9356
9357 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
9358 and then Null_Present (Specification (Formal))
9359 then
9360 -- Generate null body for procedure, for use in the instance
9361
9362 Decl_Node :=
9363 Make_Subprogram_Body (Loc,
9364 Specification => New_Spec,
9365 Declarations => New_List,
9366 Handled_Statement_Sequence =>
9367 Make_Handled_Sequence_Of_Statements (Loc,
9368 Statements => New_List (Make_Null_Statement (Loc))));
9369
9370 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
9371 return Decl_Node;
9372
9373 else
9374 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
9375 Error_Msg_NE
9376 ("missing actual&", Instantiation_Node, Formal_Sub);
9377 Error_Msg_NE
9378 ("\in instantiation of & declared#",
9379 Instantiation_Node, Scope (Analyzed_S));
9380 Abandon_Instantiation (Instantiation_Node);
9381 end if;
9382
9383 Decl_Node :=
9384 Make_Subprogram_Renaming_Declaration (Loc,
9385 Specification => New_Spec,
9386 Name => Nam);
9387
9388 -- If we do not have an actual and the formal specified <> then set to
9389 -- get proper default.
9390
9391 if No (Actual) and then Box_Present (Formal) then
9392 Set_From_Default (Decl_Node);
9393 end if;
9394
9395 -- Gather possible interpretations for the actual before analyzing the
9396 -- instance. If overloaded, it will be resolved when analyzing the
9397 -- renaming declaration.
9398
9399 if Box_Present (Formal)
9400 and then No (Actual)
9401 then
9402 Analyze (Nam);
9403
9404 if Is_Child_Unit (Scope (Analyzed_S))
9405 and then Present (Entity (Nam))
9406 then
9407 if not Is_Overloaded (Nam) then
9408 if From_Parent_Scope (Entity (Nam)) then
9409 Set_Is_Immediately_Visible (Entity (Nam), False);
9410 Set_Entity (Nam, Empty);
9411 Set_Etype (Nam, Empty);
9412
9413 Analyze (Nam);
9414 Set_Is_Immediately_Visible (Entity (Nam));
9415 end if;
9416
9417 else
9418 declare
9419 I : Interp_Index;
9420 It : Interp;
9421
9422 begin
9423 Get_First_Interp (Nam, I, It);
9424 while Present (It.Nam) loop
9425 if From_Parent_Scope (It.Nam) then
9426 Remove_Interp (I);
9427 end if;
9428
9429 Get_Next_Interp (I, It);
9430 end loop;
9431 end;
9432 end if;
9433 end if;
9434 end if;
9435
9436 -- The generic instantiation freezes the actual. This can only be done
9437 -- once the actual is resolved, in the analysis of the renaming
9438 -- declaration. To make the formal subprogram entity available, we set
9439 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9440 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9441 -- of formal abstract subprograms.
9442
9443 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9444
9445 -- We cannot analyze the renaming declaration, and thus find the actual,
9446 -- until all the actuals are assembled in the instance. For subsequent
9447 -- checks of other actuals, indicate the node that will hold the
9448 -- instance of this formal.
9449
9450 Set_Instance_Of (Analyzed_S, Nam);
9451
9452 if Nkind (Actual) = N_Selected_Component
9453 and then Is_Task_Type (Etype (Prefix (Actual)))
9454 and then not Is_Frozen (Etype (Prefix (Actual)))
9455 then
9456 -- The renaming declaration will create a body, which must appear
9457 -- outside of the instantiation, We move the renaming declaration
9458 -- out of the instance, and create an additional renaming inside,
9459 -- to prevent freezing anomalies.
9460
9461 declare
9462 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9463
9464 begin
9465 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9466 Insert_Before (Instantiation_Node, Decl_Node);
9467 Analyze (Decl_Node);
9468
9469 -- Now create renaming within the instance
9470
9471 Decl_Node :=
9472 Make_Subprogram_Renaming_Declaration (Loc,
9473 Specification => New_Copy_Tree (New_Spec),
9474 Name => New_Occurrence_Of (Anon_Id, Loc));
9475
9476 Set_Defining_Unit_Name (Specification (Decl_Node),
9477 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9478 end;
9479 end if;
9480
9481 return Decl_Node;
9482 end Instantiate_Formal_Subprogram;
9483
9484 ------------------------
9485 -- Instantiate_Object --
9486 ------------------------
9487
9488 function Instantiate_Object
9489 (Formal : Node_Id;
9490 Actual : Node_Id;
9491 Analyzed_Formal : Node_Id) return List_Id
9492 is
9493 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9494 A_Gen_Obj : constant Entity_Id :=
9495 Defining_Identifier (Analyzed_Formal);
9496 Acc_Def : Node_Id := Empty;
9497 Act_Assoc : constant Node_Id := Parent (Actual);
9498 Actual_Decl : Node_Id := Empty;
9499 Decl_Node : Node_Id;
9500 Def : Node_Id;
9501 Ftyp : Entity_Id;
9502 List : constant List_Id := New_List;
9503 Loc : constant Source_Ptr := Sloc (Actual);
9504 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9505 Subt_Decl : Node_Id := Empty;
9506 Subt_Mark : Node_Id := Empty;
9507
9508 begin
9509 if Present (Subtype_Mark (Formal)) then
9510 Subt_Mark := Subtype_Mark (Formal);
9511 else
9512 Check_Access_Definition (Formal);
9513 Acc_Def := Access_Definition (Formal);
9514 end if;
9515
9516 -- Sloc for error message on missing actual
9517
9518 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9519
9520 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9521 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9522 end if;
9523
9524 Set_Parent (List, Parent (Actual));
9525
9526 -- OUT present
9527
9528 if Out_Present (Formal) then
9529
9530 -- An IN OUT generic actual must be a name. The instantiation is a
9531 -- renaming declaration. The actual is the name being renamed. We
9532 -- use the actual directly, rather than a copy, because it is not
9533 -- used further in the list of actuals, and because a copy or a use
9534 -- of relocate_node is incorrect if the instance is nested within a
9535 -- generic. In order to simplify ASIS searches, the Generic_Parent
9536 -- field links the declaration to the generic association.
9537
9538 if No (Actual) then
9539 Error_Msg_NE
9540 ("missing actual&",
9541 Instantiation_Node, Gen_Obj);
9542 Error_Msg_NE
9543 ("\in instantiation of & declared#",
9544 Instantiation_Node, Scope (A_Gen_Obj));
9545 Abandon_Instantiation (Instantiation_Node);
9546 end if;
9547
9548 if Present (Subt_Mark) then
9549 Decl_Node :=
9550 Make_Object_Renaming_Declaration (Loc,
9551 Defining_Identifier => New_Copy (Gen_Obj),
9552 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9553 Name => Actual);
9554
9555 else pragma Assert (Present (Acc_Def));
9556 Decl_Node :=
9557 Make_Object_Renaming_Declaration (Loc,
9558 Defining_Identifier => New_Copy (Gen_Obj),
9559 Access_Definition => New_Copy_Tree (Acc_Def),
9560 Name => Actual);
9561 end if;
9562
9563 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9564
9565 -- The analysis of the actual may produce Insert_Action nodes, so
9566 -- the declaration must have a context in which to attach them.
9567
9568 Append (Decl_Node, List);
9569 Analyze (Actual);
9570
9571 -- Return if the analysis of the actual reported some error
9572
9573 if Etype (Actual) = Any_Type then
9574 return List;
9575 end if;
9576
9577 -- This check is performed here because Analyze_Object_Renaming will
9578 -- not check it when Comes_From_Source is False. Note though that the
9579 -- check for the actual being the name of an object will be performed
9580 -- in Analyze_Object_Renaming.
9581
9582 if Is_Object_Reference (Actual)
9583 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
9584 then
9585 Error_Msg_N
9586 ("illegal discriminant-dependent component for in out parameter",
9587 Actual);
9588 end if;
9589
9590 -- The actual has to be resolved in order to check that it is a
9591 -- variable (due to cases such as F (1), where F returns access to
9592 -- an array, and for overloaded prefixes).
9593
9594 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
9595
9596 -- If the type of the formal is not itself a formal, and the current
9597 -- unit is a child unit, the formal type must be declared in a
9598 -- parent, and must be retrieved by visibility.
9599
9600 if Ftyp = Orig_Ftyp
9601 and then Is_Generic_Unit (Scope (Ftyp))
9602 and then Is_Child_Unit (Scope (A_Gen_Obj))
9603 then
9604 declare
9605 Temp : constant Node_Id :=
9606 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
9607 begin
9608 Set_Entity (Temp, Empty);
9609 Find_Type (Temp);
9610 Ftyp := Entity (Temp);
9611 end;
9612 end if;
9613
9614 if Is_Private_Type (Ftyp)
9615 and then not Is_Private_Type (Etype (Actual))
9616 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
9617 or else Base_Type (Etype (Actual)) = Ftyp)
9618 then
9619 -- If the actual has the type of the full view of the formal, or
9620 -- else a non-private subtype of the formal, then the visibility
9621 -- of the formal type has changed. Add to the actuals a subtype
9622 -- declaration that will force the exchange of views in the body
9623 -- of the instance as well.
9624
9625 Subt_Decl :=
9626 Make_Subtype_Declaration (Loc,
9627 Defining_Identifier => Make_Temporary (Loc, 'P'),
9628 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
9629
9630 Prepend (Subt_Decl, List);
9631
9632 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
9633 Exchange_Declarations (Ftyp);
9634 end if;
9635
9636 Resolve (Actual, Ftyp);
9637
9638 if not Denotes_Variable (Actual) then
9639 Error_Msg_NE
9640 ("actual for& must be a variable", Actual, Gen_Obj);
9641
9642 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
9643
9644 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
9645 -- the type of the actual shall resolve to a specific anonymous
9646 -- access type.
9647
9648 if Ada_Version < Ada_2005
9649 or else
9650 Ekind (Base_Type (Ftyp)) /=
9651 E_Anonymous_Access_Type
9652 or else
9653 Ekind (Base_Type (Etype (Actual))) /=
9654 E_Anonymous_Access_Type
9655 then
9656 Error_Msg_NE ("type of actual does not match type of&",
9657 Actual, Gen_Obj);
9658 end if;
9659 end if;
9660
9661 Note_Possible_Modification (Actual, Sure => True);
9662
9663 -- Check for instantiation of atomic/volatile actual for
9664 -- non-atomic/volatile formal (RM C.6 (12)).
9665
9666 if Is_Atomic_Object (Actual)
9667 and then not Is_Atomic (Orig_Ftyp)
9668 then
9669 Error_Msg_N
9670 ("cannot instantiate non-atomic formal object " &
9671 "with atomic actual", Actual);
9672
9673 elsif Is_Volatile_Object (Actual)
9674 and then not Is_Volatile (Orig_Ftyp)
9675 then
9676 Error_Msg_N
9677 ("cannot instantiate non-volatile formal object " &
9678 "with volatile actual", Actual);
9679 end if;
9680
9681 -- Formal in-parameter
9682
9683 else
9684 -- The instantiation of a generic formal in-parameter is constant
9685 -- declaration. The actual is the expression for that declaration.
9686
9687 if Present (Actual) then
9688 if Present (Subt_Mark) then
9689 Def := Subt_Mark;
9690 else pragma Assert (Present (Acc_Def));
9691 Def := Acc_Def;
9692 end if;
9693
9694 Decl_Node :=
9695 Make_Object_Declaration (Loc,
9696 Defining_Identifier => New_Copy (Gen_Obj),
9697 Constant_Present => True,
9698 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9699 Object_Definition => New_Copy_Tree (Def),
9700 Expression => Actual);
9701
9702 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9703
9704 -- A generic formal object of a tagged type is defined to be
9705 -- aliased so the new constant must also be treated as aliased.
9706
9707 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
9708 Set_Aliased_Present (Decl_Node);
9709 end if;
9710
9711 Append (Decl_Node, List);
9712
9713 -- No need to repeat (pre-)analysis of some expression nodes
9714 -- already handled in Preanalyze_Actuals.
9715
9716 if Nkind (Actual) /= N_Allocator then
9717 Analyze (Actual);
9718
9719 -- Return if the analysis of the actual reported some error
9720
9721 if Etype (Actual) = Any_Type then
9722 return List;
9723 end if;
9724 end if;
9725
9726 declare
9727 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
9728 Typ : Entity_Id;
9729
9730 begin
9731 Typ := Get_Instance_Of (Formal_Type);
9732
9733 Freeze_Before (Instantiation_Node, Typ);
9734
9735 -- If the actual is an aggregate, perform name resolution on
9736 -- its components (the analysis of an aggregate does not do it)
9737 -- to capture local names that may be hidden if the generic is
9738 -- a child unit.
9739
9740 if Nkind (Actual) = N_Aggregate then
9741 Preanalyze_And_Resolve (Actual, Typ);
9742 end if;
9743
9744 if Is_Limited_Type (Typ)
9745 and then not OK_For_Limited_Init (Typ, Actual)
9746 then
9747 Error_Msg_N
9748 ("initialization not allowed for limited types", Actual);
9749 Explain_Limited_Type (Typ, Actual);
9750 end if;
9751 end;
9752
9753 elsif Present (Default_Expression (Formal)) then
9754
9755 -- Use default to construct declaration
9756
9757 if Present (Subt_Mark) then
9758 Def := Subt_Mark;
9759 else pragma Assert (Present (Acc_Def));
9760 Def := Acc_Def;
9761 end if;
9762
9763 Decl_Node :=
9764 Make_Object_Declaration (Sloc (Formal),
9765 Defining_Identifier => New_Copy (Gen_Obj),
9766 Constant_Present => True,
9767 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9768 Object_Definition => New_Copy (Def),
9769 Expression => New_Copy_Tree
9770 (Default_Expression (Formal)));
9771
9772 Append (Decl_Node, List);
9773 Set_Analyzed (Expression (Decl_Node), False);
9774
9775 else
9776 Error_Msg_NE
9777 ("missing actual&",
9778 Instantiation_Node, Gen_Obj);
9779 Error_Msg_NE ("\in instantiation of & declared#",
9780 Instantiation_Node, Scope (A_Gen_Obj));
9781
9782 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
9783
9784 -- Create dummy constant declaration so that instance can be
9785 -- analyzed, to minimize cascaded visibility errors.
9786
9787 if Present (Subt_Mark) then
9788 Def := Subt_Mark;
9789 else pragma Assert (Present (Acc_Def));
9790 Def := Acc_Def;
9791 end if;
9792
9793 Decl_Node :=
9794 Make_Object_Declaration (Loc,
9795 Defining_Identifier => New_Copy (Gen_Obj),
9796 Constant_Present => True,
9797 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9798 Object_Definition => New_Copy (Def),
9799 Expression =>
9800 Make_Attribute_Reference (Sloc (Gen_Obj),
9801 Attribute_Name => Name_First,
9802 Prefix => New_Copy (Def)));
9803
9804 Append (Decl_Node, List);
9805
9806 else
9807 Abandon_Instantiation (Instantiation_Node);
9808 end if;
9809 end if;
9810 end if;
9811
9812 if Nkind (Actual) in N_Has_Entity then
9813 Actual_Decl := Parent (Entity (Actual));
9814 end if;
9815
9816 -- Ada 2005 (AI-423): For a formal object declaration with a null
9817 -- exclusion or an access definition that has a null exclusion: If the
9818 -- actual matching the formal object declaration denotes a generic
9819 -- formal object of another generic unit G, and the instantiation
9820 -- containing the actual occurs within the body of G or within the body
9821 -- of a generic unit declared within the declarative region of G, then
9822 -- the declaration of the formal object of G must have a null exclusion.
9823 -- Otherwise, the subtype of the actual matching the formal object
9824 -- declaration shall exclude null.
9825
9826 if Ada_Version >= Ada_2005
9827 and then Present (Actual_Decl)
9828 and then
9829 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
9830 N_Object_Declaration)
9831 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
9832 and then not Has_Null_Exclusion (Actual_Decl)
9833 and then Has_Null_Exclusion (Analyzed_Formal)
9834 then
9835 Error_Msg_Sloc := Sloc (Analyzed_Formal);
9836 Error_Msg_N
9837 ("actual must exclude null to match generic formal#", Actual);
9838 end if;
9839
9840 return List;
9841 end Instantiate_Object;
9842
9843 ------------------------------
9844 -- Instantiate_Package_Body --
9845 ------------------------------
9846
9847 procedure Instantiate_Package_Body
9848 (Body_Info : Pending_Body_Info;
9849 Inlined_Body : Boolean := False;
9850 Body_Optional : Boolean := False)
9851 is
9852 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
9853 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
9854 Loc : constant Source_Ptr := Sloc (Inst_Node);
9855
9856 Gen_Id : constant Node_Id := Name (Inst_Node);
9857 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
9858 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
9859 Act_Spec : constant Node_Id := Specification (Act_Decl);
9860 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
9861
9862 Act_Body_Name : Node_Id;
9863 Gen_Body : Node_Id;
9864 Gen_Body_Id : Node_Id;
9865 Act_Body : Node_Id;
9866 Act_Body_Id : Entity_Id;
9867
9868 Parent_Installed : Boolean := False;
9869 Save_Style_Check : constant Boolean := Style_Check;
9870
9871 Par_Ent : Entity_Id := Empty;
9872 Par_Vis : Boolean := False;
9873
9874 Vis_Prims_List : Elist_Id := No_Elist;
9875 -- List of primitives made temporarily visible in the instantiation
9876 -- to match the visibility of the formal type
9877
9878 begin
9879 Gen_Body_Id := Corresponding_Body (Gen_Decl);
9880
9881 -- The instance body may already have been processed, as the parent of
9882 -- another instance that is inlined (Load_Parent_Of_Generic).
9883
9884 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
9885 return;
9886 end if;
9887
9888 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
9889
9890 -- Re-establish the state of information on which checks are suppressed.
9891 -- This information was set in Body_Info at the point of instantiation,
9892 -- and now we restore it so that the instance is compiled using the
9893 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
9894
9895 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
9896 Scope_Suppress := Body_Info.Scope_Suppress;
9897 Opt.Ada_Version := Body_Info.Version;
9898 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
9899 Restore_Warnings (Body_Info.Warnings);
9900
9901 if No (Gen_Body_Id) then
9902 Load_Parent_Of_Generic
9903 (Inst_Node, Specification (Gen_Decl), Body_Optional);
9904 Gen_Body_Id := Corresponding_Body (Gen_Decl);
9905 end if;
9906
9907 -- Establish global variable for sloc adjustment and for error recovery
9908
9909 Instantiation_Node := Inst_Node;
9910
9911 if Present (Gen_Body_Id) then
9912 Save_Env (Gen_Unit, Act_Decl_Id);
9913 Style_Check := False;
9914 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
9915
9916 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
9917
9918 Create_Instantiation_Source
9919 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
9920
9921 Act_Body :=
9922 Copy_Generic_Node
9923 (Original_Node (Gen_Body), Empty, Instantiating => True);
9924
9925 -- Build new name (possibly qualified) for body declaration
9926
9927 Act_Body_Id := New_Copy (Act_Decl_Id);
9928
9929 -- Some attributes of spec entity are not inherited by body entity
9930
9931 Set_Handler_Records (Act_Body_Id, No_List);
9932
9933 if Nkind (Defining_Unit_Name (Act_Spec)) =
9934 N_Defining_Program_Unit_Name
9935 then
9936 Act_Body_Name :=
9937 Make_Defining_Program_Unit_Name (Loc,
9938 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
9939 Defining_Identifier => Act_Body_Id);
9940 else
9941 Act_Body_Name := Act_Body_Id;
9942 end if;
9943
9944 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
9945
9946 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
9947 Check_Generic_Actuals (Act_Decl_Id, False);
9948
9949 -- Install primitives hidden at the point of the instantiation but
9950 -- visible when processing the generic formals
9951
9952 declare
9953 E : Entity_Id;
9954
9955 begin
9956 E := First_Entity (Act_Decl_Id);
9957 while Present (E) loop
9958 if Is_Type (E)
9959 and then Is_Generic_Actual_Type (E)
9960 and then Is_Tagged_Type (E)
9961 then
9962 Install_Hidden_Primitives
9963 (Prims_List => Vis_Prims_List,
9964 Gen_T => Generic_Parent_Type (Parent (E)),
9965 Act_T => E);
9966 end if;
9967
9968 Next_Entity (E);
9969 end loop;
9970 end;
9971
9972 -- If it is a child unit, make the parent instance (which is an
9973 -- instance of the parent of the generic) visible. The parent
9974 -- instance is the prefix of the name of the generic unit.
9975
9976 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
9977 and then Nkind (Gen_Id) = N_Expanded_Name
9978 then
9979 Par_Ent := Entity (Prefix (Gen_Id));
9980 Par_Vis := Is_Immediately_Visible (Par_Ent);
9981 Install_Parent (Par_Ent, In_Body => True);
9982 Parent_Installed := True;
9983
9984 elsif Is_Child_Unit (Gen_Unit) then
9985 Par_Ent := Scope (Gen_Unit);
9986 Par_Vis := Is_Immediately_Visible (Par_Ent);
9987 Install_Parent (Par_Ent, In_Body => True);
9988 Parent_Installed := True;
9989 end if;
9990
9991 -- If the instantiation is a library unit, and this is the main unit,
9992 -- then build the resulting compilation unit nodes for the instance.
9993 -- If this is a compilation unit but it is not the main unit, then it
9994 -- is the body of a unit in the context, that is being compiled
9995 -- because it is encloses some inlined unit or another generic unit
9996 -- being instantiated. In that case, this body is not part of the
9997 -- current compilation, and is not attached to the tree, but its
9998 -- parent must be set for analysis.
9999
10000 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10001
10002 -- Replace instance node with body of instance, and create new
10003 -- node for corresponding instance declaration.
10004
10005 Build_Instance_Compilation_Unit_Nodes
10006 (Inst_Node, Act_Body, Act_Decl);
10007 Analyze (Inst_Node);
10008
10009 if Parent (Inst_Node) = Cunit (Main_Unit) then
10010
10011 -- If the instance is a child unit itself, then set the scope
10012 -- of the expanded body to be the parent of the instantiation
10013 -- (ensuring that the fully qualified name will be generated
10014 -- for the elaboration subprogram).
10015
10016 if Nkind (Defining_Unit_Name (Act_Spec)) =
10017 N_Defining_Program_Unit_Name
10018 then
10019 Set_Scope
10020 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10021 end if;
10022 end if;
10023
10024 -- Case where instantiation is not a library unit
10025
10026 else
10027 -- If this is an early instantiation, i.e. appears textually
10028 -- before the corresponding body and must be elaborated first,
10029 -- indicate that the body instance is to be delayed.
10030
10031 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10032
10033 -- Now analyze the body. We turn off all checks if this is an
10034 -- internal unit, since there is no reason to have checks on for
10035 -- any predefined run-time library code. All such code is designed
10036 -- to be compiled with checks off.
10037
10038 -- Note that we do NOT apply this criterion to children of GNAT
10039 -- (or on VMS, children of DEC). The latter units must suppress
10040 -- checks explicitly if this is needed.
10041
10042 if Is_Predefined_File_Name
10043 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10044 then
10045 Analyze (Act_Body, Suppress => All_Checks);
10046 else
10047 Analyze (Act_Body);
10048 end if;
10049 end if;
10050
10051 Inherit_Context (Gen_Body, Inst_Node);
10052
10053 -- Remove the parent instances if they have been placed on the scope
10054 -- stack to compile the body.
10055
10056 if Parent_Installed then
10057 Remove_Parent (In_Body => True);
10058
10059 -- Restore the previous visibility of the parent
10060
10061 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10062 end if;
10063
10064 Restore_Hidden_Primitives (Vis_Prims_List);
10065 Restore_Private_Views (Act_Decl_Id);
10066
10067 -- Remove the current unit from visibility if this is an instance
10068 -- that is not elaborated on the fly for inlining purposes.
10069
10070 if not Inlined_Body then
10071 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10072 end if;
10073
10074 Restore_Env;
10075 Style_Check := Save_Style_Check;
10076
10077 -- If we have no body, and the unit requires a body, then complain. This
10078 -- complaint is suppressed if we have detected other errors (since a
10079 -- common reason for missing the body is that it had errors).
10080 -- In CodePeer mode, a warning has been emitted already, no need for
10081 -- further messages.
10082
10083 elsif Unit_Requires_Body (Gen_Unit)
10084 and then not Body_Optional
10085 then
10086 if CodePeer_Mode then
10087 null;
10088
10089 elsif Serious_Errors_Detected = 0 then
10090 Error_Msg_NE
10091 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10092
10093 -- Don't attempt to perform any cleanup actions if some other error
10094 -- was already detected, since this can cause blowups.
10095
10096 else
10097 return;
10098 end if;
10099
10100 -- Case of package that does not need a body
10101
10102 else
10103 -- If the instantiation of the declaration is a library unit, rewrite
10104 -- the original package instantiation as a package declaration in the
10105 -- compilation unit node.
10106
10107 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10108 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10109 Rewrite (Inst_Node, Act_Decl);
10110
10111 -- Generate elaboration entity, in case spec has elaboration code.
10112 -- This cannot be done when the instance is analyzed, because it
10113 -- is not known yet whether the body exists.
10114
10115 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10116 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10117
10118 -- If the instantiation is not a library unit, then append the
10119 -- declaration to the list of implicitly generated entities, unless
10120 -- it is already a list member which means that it was already
10121 -- processed
10122
10123 elsif not Is_List_Member (Act_Decl) then
10124 Mark_Rewrite_Insertion (Act_Decl);
10125 Insert_Before (Inst_Node, Act_Decl);
10126 end if;
10127 end if;
10128
10129 Expander_Mode_Restore;
10130 end Instantiate_Package_Body;
10131
10132 ---------------------------------
10133 -- Instantiate_Subprogram_Body --
10134 ---------------------------------
10135
10136 procedure Instantiate_Subprogram_Body
10137 (Body_Info : Pending_Body_Info;
10138 Body_Optional : Boolean := False)
10139 is
10140 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10141 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10142 Loc : constant Source_Ptr := Sloc (Inst_Node);
10143 Gen_Id : constant Node_Id := Name (Inst_Node);
10144 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10145 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10146 Anon_Id : constant Entity_Id :=
10147 Defining_Unit_Name (Specification (Act_Decl));
10148 Pack_Id : constant Entity_Id :=
10149 Defining_Unit_Name (Parent (Act_Decl));
10150 Decls : List_Id;
10151 Gen_Body : Node_Id;
10152 Gen_Body_Id : Node_Id;
10153 Act_Body : Node_Id;
10154 Pack_Body : Node_Id;
10155 Prev_Formal : Entity_Id;
10156 Ret_Expr : Node_Id;
10157 Unit_Renaming : Node_Id;
10158
10159 Parent_Installed : Boolean := False;
10160
10161 Saved_Style_Check : constant Boolean := Style_Check;
10162 Saved_Warnings : constant Warning_Record := Save_Warnings;
10163
10164 Par_Ent : Entity_Id := Empty;
10165 Par_Vis : Boolean := False;
10166
10167 begin
10168 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10169
10170 -- Subprogram body may have been created already because of an inline
10171 -- pragma, or because of multiple elaborations of the enclosing package
10172 -- when several instances of the subprogram appear in the main unit.
10173
10174 if Present (Corresponding_Body (Act_Decl)) then
10175 return;
10176 end if;
10177
10178 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10179
10180 -- Re-establish the state of information on which checks are suppressed.
10181 -- This information was set in Body_Info at the point of instantiation,
10182 -- and now we restore it so that the instance is compiled using the
10183 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10184
10185 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10186 Scope_Suppress := Body_Info.Scope_Suppress;
10187 Opt.Ada_Version := Body_Info.Version;
10188 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10189 Restore_Warnings (Body_Info.Warnings);
10190
10191 if No (Gen_Body_Id) then
10192
10193 -- For imported generic subprogram, no body to compile, complete
10194 -- the spec entity appropriately.
10195
10196 if Is_Imported (Gen_Unit) then
10197 Set_Is_Imported (Anon_Id);
10198 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10199 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
10200 Set_Convention (Anon_Id, Convention (Gen_Unit));
10201 Set_Has_Completion (Anon_Id);
10202 return;
10203
10204 -- For other cases, compile the body
10205
10206 else
10207 Load_Parent_Of_Generic
10208 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10209 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10210 end if;
10211 end if;
10212
10213 Instantiation_Node := Inst_Node;
10214
10215 if Present (Gen_Body_Id) then
10216 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10217
10218 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
10219
10220 -- Either body is not present, or context is non-expanding, as
10221 -- when compiling a subunit. Mark the instance as completed, and
10222 -- diagnose a missing body when needed.
10223
10224 if Expander_Active
10225 and then Operating_Mode = Generate_Code
10226 then
10227 Error_Msg_N
10228 ("missing proper body for instantiation", Gen_Body);
10229 end if;
10230
10231 Set_Has_Completion (Anon_Id);
10232 return;
10233 end if;
10234
10235 Save_Env (Gen_Unit, Anon_Id);
10236 Style_Check := False;
10237 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10238 Create_Instantiation_Source
10239 (Inst_Node,
10240 Gen_Body_Id,
10241 False,
10242 S_Adjustment);
10243
10244 Act_Body :=
10245 Copy_Generic_Node
10246 (Original_Node (Gen_Body), Empty, Instantiating => True);
10247
10248 -- Create proper defining name for the body, to correspond to
10249 -- the one in the spec.
10250
10251 Set_Defining_Unit_Name (Specification (Act_Body),
10252 Make_Defining_Identifier
10253 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
10254 Set_Corresponding_Spec (Act_Body, Anon_Id);
10255 Set_Has_Completion (Anon_Id);
10256 Check_Generic_Actuals (Pack_Id, False);
10257
10258 -- Generate a reference to link the visible subprogram instance to
10259 -- the generic body, which for navigation purposes is the only
10260 -- available source for the instance.
10261
10262 Generate_Reference
10263 (Related_Instance (Pack_Id),
10264 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
10265
10266 -- If it is a child unit, make the parent instance (which is an
10267 -- instance of the parent of the generic) visible. The parent
10268 -- instance is the prefix of the name of the generic unit.
10269
10270 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10271 and then Nkind (Gen_Id) = N_Expanded_Name
10272 then
10273 Par_Ent := Entity (Prefix (Gen_Id));
10274 Par_Vis := Is_Immediately_Visible (Par_Ent);
10275 Install_Parent (Par_Ent, In_Body => True);
10276 Parent_Installed := True;
10277
10278 elsif Is_Child_Unit (Gen_Unit) then
10279 Par_Ent := Scope (Gen_Unit);
10280 Par_Vis := Is_Immediately_Visible (Par_Ent);
10281 Install_Parent (Par_Ent, In_Body => True);
10282 Parent_Installed := True;
10283 end if;
10284
10285 -- Inside its body, a reference to the generic unit is a reference
10286 -- to the instance. The corresponding renaming is the first
10287 -- declaration in the body.
10288
10289 Unit_Renaming :=
10290 Make_Subprogram_Renaming_Declaration (Loc,
10291 Specification =>
10292 Copy_Generic_Node (
10293 Specification (Original_Node (Gen_Body)),
10294 Empty,
10295 Instantiating => True),
10296 Name => New_Occurrence_Of (Anon_Id, Loc));
10297
10298 -- If there is a formal subprogram with the same name as the unit
10299 -- itself, do not add this renaming declaration. This is a temporary
10300 -- fix for one ACVC test. ???
10301
10302 Prev_Formal := First_Entity (Pack_Id);
10303 while Present (Prev_Formal) loop
10304 if Chars (Prev_Formal) = Chars (Gen_Unit)
10305 and then Is_Overloadable (Prev_Formal)
10306 then
10307 exit;
10308 end if;
10309
10310 Next_Entity (Prev_Formal);
10311 end loop;
10312
10313 if Present (Prev_Formal) then
10314 Decls := New_List (Act_Body);
10315 else
10316 Decls := New_List (Unit_Renaming, Act_Body);
10317 end if;
10318
10319 -- The subprogram body is placed in the body of a dummy package body,
10320 -- whose spec contains the subprogram declaration as well as the
10321 -- renaming declarations for the generic parameters.
10322
10323 Pack_Body := Make_Package_Body (Loc,
10324 Defining_Unit_Name => New_Copy (Pack_Id),
10325 Declarations => Decls);
10326
10327 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10328
10329 -- If the instantiation is a library unit, then build resulting
10330 -- compilation unit nodes for the instance. The declaration of
10331 -- the enclosing package is the grandparent of the subprogram
10332 -- declaration. First replace the instantiation node as the unit
10333 -- of the corresponding compilation.
10334
10335 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10336 if Parent (Inst_Node) = Cunit (Main_Unit) then
10337 Set_Unit (Parent (Inst_Node), Inst_Node);
10338 Build_Instance_Compilation_Unit_Nodes
10339 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
10340 Analyze (Inst_Node);
10341 else
10342 Set_Parent (Pack_Body, Parent (Inst_Node));
10343 Analyze (Pack_Body);
10344 end if;
10345
10346 else
10347 Insert_Before (Inst_Node, Pack_Body);
10348 Mark_Rewrite_Insertion (Pack_Body);
10349 Analyze (Pack_Body);
10350
10351 if Expander_Active then
10352 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
10353 end if;
10354 end if;
10355
10356 Inherit_Context (Gen_Body, Inst_Node);
10357
10358 Restore_Private_Views (Pack_Id, False);
10359
10360 if Parent_Installed then
10361 Remove_Parent (In_Body => True);
10362
10363 -- Restore the previous visibility of the parent
10364
10365 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10366 end if;
10367
10368 Restore_Env;
10369 Style_Check := Saved_Style_Check;
10370 Restore_Warnings (Saved_Warnings);
10371
10372 -- Body not found. Error was emitted already. If there were no previous
10373 -- errors, this may be an instance whose scope is a premature instance.
10374 -- In that case we must insure that the (legal) program does raise
10375 -- program error if executed. We generate a subprogram body for this
10376 -- purpose. See DEC ac30vso.
10377
10378 -- Should not reference proprietary DEC tests in comments ???
10379
10380 elsif Serious_Errors_Detected = 0
10381 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
10382 then
10383 if Body_Optional then
10384 return;
10385
10386 elsif Ekind (Anon_Id) = E_Procedure then
10387 Act_Body :=
10388 Make_Subprogram_Body (Loc,
10389 Specification =>
10390 Make_Procedure_Specification (Loc,
10391 Defining_Unit_Name =>
10392 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10393 Parameter_Specifications =>
10394 New_Copy_List
10395 (Parameter_Specifications (Parent (Anon_Id)))),
10396
10397 Declarations => Empty_List,
10398 Handled_Statement_Sequence =>
10399 Make_Handled_Sequence_Of_Statements (Loc,
10400 Statements =>
10401 New_List (
10402 Make_Raise_Program_Error (Loc,
10403 Reason =>
10404 PE_Access_Before_Elaboration))));
10405
10406 else
10407 Ret_Expr :=
10408 Make_Raise_Program_Error (Loc,
10409 Reason => PE_Access_Before_Elaboration);
10410
10411 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
10412 Set_Analyzed (Ret_Expr);
10413
10414 Act_Body :=
10415 Make_Subprogram_Body (Loc,
10416 Specification =>
10417 Make_Function_Specification (Loc,
10418 Defining_Unit_Name =>
10419 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10420 Parameter_Specifications =>
10421 New_Copy_List
10422 (Parameter_Specifications (Parent (Anon_Id))),
10423 Result_Definition =>
10424 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10425
10426 Declarations => Empty_List,
10427 Handled_Statement_Sequence =>
10428 Make_Handled_Sequence_Of_Statements (Loc,
10429 Statements =>
10430 New_List
10431 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10432 end if;
10433
10434 Pack_Body := Make_Package_Body (Loc,
10435 Defining_Unit_Name => New_Copy (Pack_Id),
10436 Declarations => New_List (Act_Body));
10437
10438 Insert_After (Inst_Node, Pack_Body);
10439 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10440 Analyze (Pack_Body);
10441 end if;
10442
10443 Expander_Mode_Restore;
10444 end Instantiate_Subprogram_Body;
10445
10446 ----------------------
10447 -- Instantiate_Type --
10448 ----------------------
10449
10450 function Instantiate_Type
10451 (Formal : Node_Id;
10452 Actual : Node_Id;
10453 Analyzed_Formal : Node_Id;
10454 Actual_Decls : List_Id) return List_Id
10455 is
10456 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10457 A_Gen_T : constant Entity_Id :=
10458 Defining_Identifier (Analyzed_Formal);
10459 Ancestor : Entity_Id := Empty;
10460 Def : constant Node_Id := Formal_Type_Definition (Formal);
10461 Act_T : Entity_Id;
10462 Decl_Node : Node_Id;
10463 Decl_Nodes : List_Id;
10464 Loc : Source_Ptr;
10465 Subt : Entity_Id;
10466
10467 procedure Validate_Array_Type_Instance;
10468 procedure Validate_Access_Subprogram_Instance;
10469 procedure Validate_Access_Type_Instance;
10470 procedure Validate_Derived_Type_Instance;
10471 procedure Validate_Derived_Interface_Type_Instance;
10472 procedure Validate_Discriminated_Formal_Type;
10473 procedure Validate_Interface_Type_Instance;
10474 procedure Validate_Private_Type_Instance;
10475 procedure Validate_Incomplete_Type_Instance;
10476 -- These procedures perform validation tests for the named case.
10477 -- Validate_Discriminated_Formal_Type is shared by formal private
10478 -- types and Ada 2012 formal incomplete types.
10479
10480 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
10481 -- Check that base types are the same and that the subtypes match
10482 -- statically. Used in several of the above.
10483
10484 --------------------
10485 -- Subtypes_Match --
10486 --------------------
10487
10488 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
10489 T : constant Entity_Id := Get_Instance_Of (Gen_T);
10490
10491 begin
10492 -- Some detailed comments would be useful here ???
10493
10494 return ((Base_Type (T) = Act_T
10495 or else Base_Type (T) = Base_Type (Act_T))
10496 and then Subtypes_Statically_Match (T, Act_T))
10497
10498 or else (Is_Class_Wide_Type (Gen_T)
10499 and then Is_Class_Wide_Type (Act_T)
10500 and then Subtypes_Match
10501 (Get_Instance_Of (Root_Type (Gen_T)),
10502 Root_Type (Act_T)))
10503
10504 or else
10505 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
10506 E_Anonymous_Access_Type)
10507 and then Ekind (Act_T) = Ekind (Gen_T)
10508 and then Subtypes_Statically_Match
10509 (Designated_Type (Gen_T), Designated_Type (Act_T)));
10510 end Subtypes_Match;
10511
10512 -----------------------------------------
10513 -- Validate_Access_Subprogram_Instance --
10514 -----------------------------------------
10515
10516 procedure Validate_Access_Subprogram_Instance is
10517 begin
10518 if not Is_Access_Type (Act_T)
10519 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
10520 then
10521 Error_Msg_NE
10522 ("expect access type in instantiation of &", Actual, Gen_T);
10523 Abandon_Instantiation (Actual);
10524 end if;
10525
10526 -- According to AI05-288, actuals for access_to_subprograms must be
10527 -- subtype conformant with the generic formal. Previous to AI05-288
10528 -- only mode conformance was required.
10529
10530 -- This is a binding interpretation that applies to previous versions
10531 -- of the language, no need to maintain previous weaker checks.
10532
10533 Check_Subtype_Conformant
10534 (Designated_Type (Act_T),
10535 Designated_Type (A_Gen_T),
10536 Actual,
10537 Get_Inst => True);
10538
10539 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
10540 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
10541 Error_Msg_NE
10542 ("protected access type not allowed for formal &",
10543 Actual, Gen_T);
10544 end if;
10545
10546 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
10547 Error_Msg_NE
10548 ("expect protected access type for formal &",
10549 Actual, Gen_T);
10550 end if;
10551 end Validate_Access_Subprogram_Instance;
10552
10553 -----------------------------------
10554 -- Validate_Access_Type_Instance --
10555 -----------------------------------
10556
10557 procedure Validate_Access_Type_Instance is
10558 Desig_Type : constant Entity_Id :=
10559 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
10560 Desig_Act : Entity_Id;
10561
10562 begin
10563 if not Is_Access_Type (Act_T) then
10564 Error_Msg_NE
10565 ("expect access type in instantiation of &", Actual, Gen_T);
10566 Abandon_Instantiation (Actual);
10567 end if;
10568
10569 if Is_Access_Constant (A_Gen_T) then
10570 if not Is_Access_Constant (Act_T) then
10571 Error_Msg_N
10572 ("actual type must be access-to-constant type", Actual);
10573 Abandon_Instantiation (Actual);
10574 end if;
10575 else
10576 if Is_Access_Constant (Act_T) then
10577 Error_Msg_N
10578 ("actual type must be access-to-variable type", Actual);
10579 Abandon_Instantiation (Actual);
10580
10581 elsif Ekind (A_Gen_T) = E_General_Access_Type
10582 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
10583 then
10584 Error_Msg_N -- CODEFIX
10585 ("actual must be general access type!", Actual);
10586 Error_Msg_NE -- CODEFIX
10587 ("add ALL to }!", Actual, Act_T);
10588 Abandon_Instantiation (Actual);
10589 end if;
10590 end if;
10591
10592 -- The designated subtypes, that is to say the subtypes introduced
10593 -- by an access type declaration (and not by a subtype declaration)
10594 -- must match.
10595
10596 Desig_Act := Designated_Type (Base_Type (Act_T));
10597
10598 -- The designated type may have been introduced through a limited_
10599 -- with clause, in which case retrieve the non-limited view. This
10600 -- applies to incomplete types as well as to class-wide types.
10601
10602 if From_Limited_With (Desig_Act) then
10603 Desig_Act := Available_View (Desig_Act);
10604 end if;
10605
10606 if not Subtypes_Match
10607 (Desig_Type, Desig_Act) then
10608 Error_Msg_NE
10609 ("designated type of actual does not match that of formal &",
10610 Actual, Gen_T);
10611 Abandon_Instantiation (Actual);
10612
10613 elsif Is_Access_Type (Designated_Type (Act_T))
10614 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
10615 /=
10616 Is_Constrained (Designated_Type (Desig_Type))
10617 then
10618 Error_Msg_NE
10619 ("designated type of actual does not match that of formal &",
10620 Actual, Gen_T);
10621 Abandon_Instantiation (Actual);
10622 end if;
10623
10624 -- Ada 2005: null-exclusion indicators of the two types must agree
10625
10626 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
10627 Error_Msg_NE
10628 ("non null exclusion of actual and formal & do not match",
10629 Actual, Gen_T);
10630 end if;
10631 end Validate_Access_Type_Instance;
10632
10633 ----------------------------------
10634 -- Validate_Array_Type_Instance --
10635 ----------------------------------
10636
10637 procedure Validate_Array_Type_Instance is
10638 I1 : Node_Id;
10639 I2 : Node_Id;
10640 T2 : Entity_Id;
10641
10642 function Formal_Dimensions return Int;
10643 -- Count number of dimensions in array type formal
10644
10645 -----------------------
10646 -- Formal_Dimensions --
10647 -----------------------
10648
10649 function Formal_Dimensions return Int is
10650 Num : Int := 0;
10651 Index : Node_Id;
10652
10653 begin
10654 if Nkind (Def) = N_Constrained_Array_Definition then
10655 Index := First (Discrete_Subtype_Definitions (Def));
10656 else
10657 Index := First (Subtype_Marks (Def));
10658 end if;
10659
10660 while Present (Index) loop
10661 Num := Num + 1;
10662 Next_Index (Index);
10663 end loop;
10664
10665 return Num;
10666 end Formal_Dimensions;
10667
10668 -- Start of processing for Validate_Array_Type_Instance
10669
10670 begin
10671 if not Is_Array_Type (Act_T) then
10672 Error_Msg_NE
10673 ("expect array type in instantiation of &", Actual, Gen_T);
10674 Abandon_Instantiation (Actual);
10675
10676 elsif Nkind (Def) = N_Constrained_Array_Definition then
10677 if not (Is_Constrained (Act_T)) then
10678 Error_Msg_NE
10679 ("expect constrained array in instantiation of &",
10680 Actual, Gen_T);
10681 Abandon_Instantiation (Actual);
10682 end if;
10683
10684 else
10685 if Is_Constrained (Act_T) then
10686 Error_Msg_NE
10687 ("expect unconstrained array in instantiation of &",
10688 Actual, Gen_T);
10689 Abandon_Instantiation (Actual);
10690 end if;
10691 end if;
10692
10693 if Formal_Dimensions /= Number_Dimensions (Act_T) then
10694 Error_Msg_NE
10695 ("dimensions of actual do not match formal &", Actual, Gen_T);
10696 Abandon_Instantiation (Actual);
10697 end if;
10698
10699 I1 := First_Index (A_Gen_T);
10700 I2 := First_Index (Act_T);
10701 for J in 1 .. Formal_Dimensions loop
10702
10703 -- If the indexes of the actual were given by a subtype_mark,
10704 -- the index was transformed into a range attribute. Retrieve
10705 -- the original type mark for checking.
10706
10707 if Is_Entity_Name (Original_Node (I2)) then
10708 T2 := Entity (Original_Node (I2));
10709 else
10710 T2 := Etype (I2);
10711 end if;
10712
10713 if not Subtypes_Match
10714 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
10715 then
10716 Error_Msg_NE
10717 ("index types of actual do not match those of formal &",
10718 Actual, Gen_T);
10719 Abandon_Instantiation (Actual);
10720 end if;
10721
10722 Next_Index (I1);
10723 Next_Index (I2);
10724 end loop;
10725
10726 -- Check matching subtypes. Note that there are complex visibility
10727 -- issues when the generic is a child unit and some aspect of the
10728 -- generic type is declared in a parent unit of the generic. We do
10729 -- the test to handle this special case only after a direct check
10730 -- for static matching has failed. The case where both the component
10731 -- type and the array type are separate formals, and the component
10732 -- type is a private view may also require special checking in
10733 -- Subtypes_Match.
10734
10735 if Subtypes_Match
10736 (Component_Type (A_Gen_T), Component_Type (Act_T))
10737 or else Subtypes_Match
10738 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
10739 Component_Type (Act_T))
10740 then
10741 null;
10742 else
10743 Error_Msg_NE
10744 ("component subtype of actual does not match that of formal &",
10745 Actual, Gen_T);
10746 Abandon_Instantiation (Actual);
10747 end if;
10748
10749 if Has_Aliased_Components (A_Gen_T)
10750 and then not Has_Aliased_Components (Act_T)
10751 then
10752 Error_Msg_NE
10753 ("actual must have aliased components to match formal type &",
10754 Actual, Gen_T);
10755 end if;
10756 end Validate_Array_Type_Instance;
10757
10758 -----------------------------------------------
10759 -- Validate_Derived_Interface_Type_Instance --
10760 -----------------------------------------------
10761
10762 procedure Validate_Derived_Interface_Type_Instance is
10763 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
10764 Elmt : Elmt_Id;
10765
10766 begin
10767 -- First apply interface instance checks
10768
10769 Validate_Interface_Type_Instance;
10770
10771 -- Verify that immediate parent interface is an ancestor of
10772 -- the actual.
10773
10774 if Present (Par)
10775 and then not Interface_Present_In_Ancestor (Act_T, Par)
10776 then
10777 Error_Msg_NE
10778 ("interface actual must include progenitor&", Actual, Par);
10779 end if;
10780
10781 -- Now verify that the actual includes all other ancestors of
10782 -- the formal.
10783
10784 Elmt := First_Elmt (Interfaces (A_Gen_T));
10785 while Present (Elmt) loop
10786 if not Interface_Present_In_Ancestor
10787 (Act_T, Get_Instance_Of (Node (Elmt)))
10788 then
10789 Error_Msg_NE
10790 ("interface actual must include progenitor&",
10791 Actual, Node (Elmt));
10792 end if;
10793
10794 Next_Elmt (Elmt);
10795 end loop;
10796 end Validate_Derived_Interface_Type_Instance;
10797
10798 ------------------------------------
10799 -- Validate_Derived_Type_Instance --
10800 ------------------------------------
10801
10802 procedure Validate_Derived_Type_Instance is
10803 Actual_Discr : Entity_Id;
10804 Ancestor_Discr : Entity_Id;
10805
10806 begin
10807 -- If the parent type in the generic declaration is itself a previous
10808 -- formal type, then it is local to the generic and absent from the
10809 -- analyzed generic definition. In that case the ancestor is the
10810 -- instance of the formal (which must have been instantiated
10811 -- previously), unless the ancestor is itself a formal derived type.
10812 -- In this latter case (which is the subject of Corrigendum 8652/0038
10813 -- (AI-202) the ancestor of the formals is the ancestor of its
10814 -- parent. Otherwise, the analyzed generic carries the parent type.
10815 -- If the parent type is defined in a previous formal package, then
10816 -- the scope of that formal package is that of the generic type
10817 -- itself, and it has already been mapped into the corresponding type
10818 -- in the actual package.
10819
10820 -- Common case: parent type defined outside of the generic
10821
10822 if Is_Entity_Name (Subtype_Mark (Def))
10823 and then Present (Entity (Subtype_Mark (Def)))
10824 then
10825 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
10826
10827 -- Check whether parent is defined in a previous formal package
10828
10829 elsif
10830 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
10831 then
10832 Ancestor :=
10833 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
10834
10835 -- The type may be a local derivation, or a type extension of a
10836 -- previous formal, or of a formal of a parent package.
10837
10838 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
10839 or else
10840 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
10841 then
10842 -- Check whether the parent is another derived formal type in the
10843 -- same generic unit.
10844
10845 if Etype (A_Gen_T) /= A_Gen_T
10846 and then Is_Generic_Type (Etype (A_Gen_T))
10847 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
10848 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
10849 then
10850 -- Locate ancestor of parent from the subtype declaration
10851 -- created for the actual.
10852
10853 declare
10854 Decl : Node_Id;
10855
10856 begin
10857 Decl := First (Actual_Decls);
10858 while Present (Decl) loop
10859 if Nkind (Decl) = N_Subtype_Declaration
10860 and then Chars (Defining_Identifier (Decl)) =
10861 Chars (Etype (A_Gen_T))
10862 then
10863 Ancestor := Generic_Parent_Type (Decl);
10864 exit;
10865 else
10866 Next (Decl);
10867 end if;
10868 end loop;
10869 end;
10870
10871 pragma Assert (Present (Ancestor));
10872
10873 -- The ancestor itself may be a previous formal that has been
10874 -- instantiated.
10875
10876 Ancestor := Get_Instance_Of (Ancestor);
10877
10878 else
10879 Ancestor :=
10880 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
10881 end if;
10882
10883 -- An unusual case: the actual is a type declared in a parent unit,
10884 -- but is not a formal type so there is no instance_of for it.
10885 -- Retrieve it by analyzing the record extension.
10886
10887 elsif Is_Child_Unit (Scope (A_Gen_T))
10888 and then In_Open_Scopes (Scope (Act_T))
10889 and then Is_Generic_Instance (Scope (Act_T))
10890 then
10891 Analyze (Subtype_Mark (Def));
10892 Ancestor := Entity (Subtype_Mark (Def));
10893
10894 else
10895 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
10896 end if;
10897
10898 -- If the formal derived type has pragma Preelaborable_Initialization
10899 -- then the actual type must have preelaborable initialization.
10900
10901 if Known_To_Have_Preelab_Init (A_Gen_T)
10902 and then not Has_Preelaborable_Initialization (Act_T)
10903 then
10904 Error_Msg_NE
10905 ("actual for & must have preelaborable initialization",
10906 Actual, Gen_T);
10907 end if;
10908
10909 -- Ada 2005 (AI-251)
10910
10911 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
10912 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
10913 Error_Msg_NE
10914 ("(Ada 2005) expected type implementing & in instantiation",
10915 Actual, Ancestor);
10916 end if;
10917
10918 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
10919 Error_Msg_NE
10920 ("expect type derived from & in instantiation",
10921 Actual, First_Subtype (Ancestor));
10922 Abandon_Instantiation (Actual);
10923 end if;
10924
10925 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
10926 -- that the formal type declaration has been rewritten as a private
10927 -- extension.
10928
10929 if Ada_Version >= Ada_2005
10930 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
10931 and then Synchronized_Present (Parent (A_Gen_T))
10932 then
10933 -- The actual must be a synchronized tagged type
10934
10935 if not Is_Tagged_Type (Act_T) then
10936 Error_Msg_N
10937 ("actual of synchronized type must be tagged", Actual);
10938 Abandon_Instantiation (Actual);
10939
10940 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
10941 and then Nkind (Type_Definition (Parent (Act_T))) =
10942 N_Derived_Type_Definition
10943 and then not Synchronized_Present (Type_Definition
10944 (Parent (Act_T)))
10945 then
10946 Error_Msg_N
10947 ("actual of synchronized type must be synchronized", Actual);
10948 Abandon_Instantiation (Actual);
10949 end if;
10950 end if;
10951
10952 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
10953 -- removes the second instance of the phrase "or allow pass by copy".
10954
10955 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
10956 Error_Msg_N
10957 ("cannot have atomic actual type for non-atomic formal type",
10958 Actual);
10959
10960 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
10961 Error_Msg_N
10962 ("cannot have volatile actual type for non-volatile formal type",
10963 Actual);
10964 end if;
10965
10966 -- It should not be necessary to check for unknown discriminants on
10967 -- Formal, but for some reason Has_Unknown_Discriminants is false for
10968 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
10969 -- needs fixing. ???
10970
10971 if not Is_Indefinite_Subtype (A_Gen_T)
10972 and then not Unknown_Discriminants_Present (Formal)
10973 and then Is_Indefinite_Subtype (Act_T)
10974 then
10975 Error_Msg_N
10976 ("actual subtype must be constrained", Actual);
10977 Abandon_Instantiation (Actual);
10978 end if;
10979
10980 if not Unknown_Discriminants_Present (Formal) then
10981 if Is_Constrained (Ancestor) then
10982 if not Is_Constrained (Act_T) then
10983 Error_Msg_N
10984 ("actual subtype must be constrained", Actual);
10985 Abandon_Instantiation (Actual);
10986 end if;
10987
10988 -- Ancestor is unconstrained, Check if generic formal and actual
10989 -- agree on constrainedness. The check only applies to array types
10990 -- and discriminated types.
10991
10992 elsif Is_Constrained (Act_T) then
10993 if Ekind (Ancestor) = E_Access_Type
10994 or else
10995 (not Is_Constrained (A_Gen_T)
10996 and then Is_Composite_Type (A_Gen_T))
10997 then
10998 Error_Msg_N
10999 ("actual subtype must be unconstrained", Actual);
11000 Abandon_Instantiation (Actual);
11001 end if;
11002
11003 -- A class-wide type is only allowed if the formal has unknown
11004 -- discriminants.
11005
11006 elsif Is_Class_Wide_Type (Act_T)
11007 and then not Has_Unknown_Discriminants (Ancestor)
11008 then
11009 Error_Msg_NE
11010 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11011 Abandon_Instantiation (Actual);
11012
11013 -- Otherwise, the formal and actual shall have the same number
11014 -- of discriminants and each discriminant of the actual must
11015 -- correspond to a discriminant of the formal.
11016
11017 elsif Has_Discriminants (Act_T)
11018 and then not Has_Unknown_Discriminants (Act_T)
11019 and then Has_Discriminants (Ancestor)
11020 then
11021 Actual_Discr := First_Discriminant (Act_T);
11022 Ancestor_Discr := First_Discriminant (Ancestor);
11023 while Present (Actual_Discr)
11024 and then Present (Ancestor_Discr)
11025 loop
11026 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11027 No (Corresponding_Discriminant (Actual_Discr))
11028 then
11029 Error_Msg_NE
11030 ("discriminant & does not correspond " &
11031 "to ancestor discriminant", Actual, Actual_Discr);
11032 Abandon_Instantiation (Actual);
11033 end if;
11034
11035 Next_Discriminant (Actual_Discr);
11036 Next_Discriminant (Ancestor_Discr);
11037 end loop;
11038
11039 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11040 Error_Msg_NE
11041 ("actual for & must have same number of discriminants",
11042 Actual, Gen_T);
11043 Abandon_Instantiation (Actual);
11044 end if;
11045
11046 -- This case should be caught by the earlier check for
11047 -- constrainedness, but the check here is added for completeness.
11048
11049 elsif Has_Discriminants (Act_T)
11050 and then not Has_Unknown_Discriminants (Act_T)
11051 then
11052 Error_Msg_NE
11053 ("actual for & must not have discriminants", Actual, Gen_T);
11054 Abandon_Instantiation (Actual);
11055
11056 elsif Has_Discriminants (Ancestor) then
11057 Error_Msg_NE
11058 ("actual for & must have known discriminants", Actual, Gen_T);
11059 Abandon_Instantiation (Actual);
11060 end if;
11061
11062 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
11063 Error_Msg_N
11064 ("constraint on actual is incompatible with formal", Actual);
11065 Abandon_Instantiation (Actual);
11066 end if;
11067 end if;
11068
11069 -- If the formal and actual types are abstract, check that there
11070 -- are no abstract primitives of the actual type that correspond to
11071 -- nonabstract primitives of the formal type (second sentence of
11072 -- RM95-3.9.3(9)).
11073
11074 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11075 Check_Abstract_Primitives : declare
11076 Gen_Prims : constant Elist_Id :=
11077 Primitive_Operations (A_Gen_T);
11078 Gen_Elmt : Elmt_Id;
11079 Gen_Subp : Entity_Id;
11080 Anc_Subp : Entity_Id;
11081 Anc_Formal : Entity_Id;
11082 Anc_F_Type : Entity_Id;
11083
11084 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11085 Act_Elmt : Elmt_Id;
11086 Act_Subp : Entity_Id;
11087 Act_Formal : Entity_Id;
11088 Act_F_Type : Entity_Id;
11089
11090 Subprograms_Correspond : Boolean;
11091
11092 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11093 -- Returns true if T2 is derived directly or indirectly from
11094 -- T1, including derivations from interfaces. T1 and T2 are
11095 -- required to be specific tagged base types.
11096
11097 ------------------------
11098 -- Is_Tagged_Ancestor --
11099 ------------------------
11100
11101 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11102 is
11103 Intfc_Elmt : Elmt_Id;
11104
11105 begin
11106 -- The predicate is satisfied if the types are the same
11107
11108 if T1 = T2 then
11109 return True;
11110
11111 -- If we've reached the top of the derivation chain then
11112 -- we know that T1 is not an ancestor of T2.
11113
11114 elsif Etype (T2) = T2 then
11115 return False;
11116
11117 -- Proceed to check T2's immediate parent
11118
11119 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11120 return True;
11121
11122 -- Finally, check to see if T1 is an ancestor of any of T2's
11123 -- progenitors.
11124
11125 else
11126 Intfc_Elmt := First_Elmt (Interfaces (T2));
11127 while Present (Intfc_Elmt) loop
11128 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11129 return True;
11130 end if;
11131
11132 Next_Elmt (Intfc_Elmt);
11133 end loop;
11134 end if;
11135
11136 return False;
11137 end Is_Tagged_Ancestor;
11138
11139 -- Start of processing for Check_Abstract_Primitives
11140
11141 begin
11142 -- Loop over all of the formal derived type's primitives
11143
11144 Gen_Elmt := First_Elmt (Gen_Prims);
11145 while Present (Gen_Elmt) loop
11146 Gen_Subp := Node (Gen_Elmt);
11147
11148 -- If the primitive of the formal is not abstract, then
11149 -- determine whether there is a corresponding primitive of
11150 -- the actual type that's abstract.
11151
11152 if not Is_Abstract_Subprogram (Gen_Subp) then
11153 Act_Elmt := First_Elmt (Act_Prims);
11154 while Present (Act_Elmt) loop
11155 Act_Subp := Node (Act_Elmt);
11156
11157 -- If we find an abstract primitive of the actual,
11158 -- then we need to test whether it corresponds to the
11159 -- subprogram from which the generic formal primitive
11160 -- is inherited.
11161
11162 if Is_Abstract_Subprogram (Act_Subp) then
11163 Anc_Subp := Alias (Gen_Subp);
11164
11165 -- Test whether we have a corresponding primitive
11166 -- by comparing names, kinds, formal types, and
11167 -- result types.
11168
11169 if Chars (Anc_Subp) = Chars (Act_Subp)
11170 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11171 then
11172 Anc_Formal := First_Formal (Anc_Subp);
11173 Act_Formal := First_Formal (Act_Subp);
11174 while Present (Anc_Formal)
11175 and then Present (Act_Formal)
11176 loop
11177 Anc_F_Type := Etype (Anc_Formal);
11178 Act_F_Type := Etype (Act_Formal);
11179
11180 if Ekind (Anc_F_Type)
11181 = E_Anonymous_Access_Type
11182 then
11183 Anc_F_Type := Designated_Type (Anc_F_Type);
11184
11185 if Ekind (Act_F_Type)
11186 = E_Anonymous_Access_Type
11187 then
11188 Act_F_Type :=
11189 Designated_Type (Act_F_Type);
11190 else
11191 exit;
11192 end if;
11193
11194 elsif
11195 Ekind (Act_F_Type) = E_Anonymous_Access_Type
11196 then
11197 exit;
11198 end if;
11199
11200 Anc_F_Type := Base_Type (Anc_F_Type);
11201 Act_F_Type := Base_Type (Act_F_Type);
11202
11203 -- If the formal is controlling, then the
11204 -- the type of the actual primitive's formal
11205 -- must be derived directly or indirectly
11206 -- from the type of the ancestor primitive's
11207 -- formal.
11208
11209 if Is_Controlling_Formal (Anc_Formal) then
11210 if not Is_Tagged_Ancestor
11211 (Anc_F_Type, Act_F_Type)
11212 then
11213 exit;
11214 end if;
11215
11216 -- Otherwise the types of the formals must
11217 -- be the same.
11218
11219 elsif Anc_F_Type /= Act_F_Type then
11220 exit;
11221 end if;
11222
11223 Next_Entity (Anc_Formal);
11224 Next_Entity (Act_Formal);
11225 end loop;
11226
11227 -- If we traversed through all of the formals
11228 -- then so far the subprograms correspond, so
11229 -- now check that any result types correspond.
11230
11231 if No (Anc_Formal) and then No (Act_Formal) then
11232 Subprograms_Correspond := True;
11233
11234 if Ekind (Act_Subp) = E_Function then
11235 Anc_F_Type := Etype (Anc_Subp);
11236 Act_F_Type := Etype (Act_Subp);
11237
11238 if Ekind (Anc_F_Type)
11239 = E_Anonymous_Access_Type
11240 then
11241 Anc_F_Type :=
11242 Designated_Type (Anc_F_Type);
11243
11244 if Ekind (Act_F_Type)
11245 = E_Anonymous_Access_Type
11246 then
11247 Act_F_Type :=
11248 Designated_Type (Act_F_Type);
11249 else
11250 Subprograms_Correspond := False;
11251 end if;
11252
11253 elsif
11254 Ekind (Act_F_Type)
11255 = E_Anonymous_Access_Type
11256 then
11257 Subprograms_Correspond := False;
11258 end if;
11259
11260 Anc_F_Type := Base_Type (Anc_F_Type);
11261 Act_F_Type := Base_Type (Act_F_Type);
11262
11263 -- Now either the result types must be
11264 -- the same or, if the result type is
11265 -- controlling, the result type of the
11266 -- actual primitive must descend from the
11267 -- result type of the ancestor primitive.
11268
11269 if Subprograms_Correspond
11270 and then Anc_F_Type /= Act_F_Type
11271 and then
11272 Has_Controlling_Result (Anc_Subp)
11273 and then
11274 not Is_Tagged_Ancestor
11275 (Anc_F_Type, Act_F_Type)
11276 then
11277 Subprograms_Correspond := False;
11278 end if;
11279 end if;
11280
11281 -- Found a matching subprogram belonging to
11282 -- formal ancestor type, so actual subprogram
11283 -- corresponds and this violates 3.9.3(9).
11284
11285 if Subprograms_Correspond then
11286 Error_Msg_NE
11287 ("abstract subprogram & overrides " &
11288 "nonabstract subprogram of ancestor",
11289 Actual,
11290 Act_Subp);
11291 end if;
11292 end if;
11293 end if;
11294 end if;
11295
11296 Next_Elmt (Act_Elmt);
11297 end loop;
11298 end if;
11299
11300 Next_Elmt (Gen_Elmt);
11301 end loop;
11302 end Check_Abstract_Primitives;
11303 end if;
11304
11305 -- Verify that limitedness matches. If parent is a limited
11306 -- interface then the generic formal is not unless declared
11307 -- explicitly so. If not declared limited, the actual cannot be
11308 -- limited (see AI05-0087).
11309
11310 -- Even though this AI is a binding interpretation, we enable the
11311 -- check only in Ada 2012 mode, because this improper construct
11312 -- shows up in user code and in existing B-tests.
11313
11314 if Is_Limited_Type (Act_T)
11315 and then not Is_Limited_Type (A_Gen_T)
11316 and then Ada_Version >= Ada_2012
11317 then
11318 if In_Instance then
11319 null;
11320 else
11321 Error_Msg_NE
11322 ("actual for non-limited & cannot be a limited type", Actual,
11323 Gen_T);
11324 Explain_Limited_Type (Act_T, Actual);
11325 Abandon_Instantiation (Actual);
11326 end if;
11327 end if;
11328 end Validate_Derived_Type_Instance;
11329
11330 ----------------------------------------
11331 -- Validate_Discriminated_Formal_Type --
11332 ----------------------------------------
11333
11334 procedure Validate_Discriminated_Formal_Type is
11335 Formal_Discr : Entity_Id;
11336 Actual_Discr : Entity_Id;
11337 Formal_Subt : Entity_Id;
11338
11339 begin
11340 if Has_Discriminants (A_Gen_T) then
11341 if not Has_Discriminants (Act_T) then
11342 Error_Msg_NE
11343 ("actual for & must have discriminants", Actual, Gen_T);
11344 Abandon_Instantiation (Actual);
11345
11346 elsif Is_Constrained (Act_T) then
11347 Error_Msg_NE
11348 ("actual for & must be unconstrained", Actual, Gen_T);
11349 Abandon_Instantiation (Actual);
11350
11351 else
11352 Formal_Discr := First_Discriminant (A_Gen_T);
11353 Actual_Discr := First_Discriminant (Act_T);
11354 while Formal_Discr /= Empty loop
11355 if Actual_Discr = Empty then
11356 Error_Msg_NE
11357 ("discriminants on actual do not match formal",
11358 Actual, Gen_T);
11359 Abandon_Instantiation (Actual);
11360 end if;
11361
11362 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
11363
11364 -- Access discriminants match if designated types do
11365
11366 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
11367 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
11368 E_Anonymous_Access_Type
11369 and then
11370 Get_Instance_Of
11371 (Designated_Type (Base_Type (Formal_Subt))) =
11372 Designated_Type (Base_Type (Etype (Actual_Discr)))
11373 then
11374 null;
11375
11376 elsif Base_Type (Formal_Subt) /=
11377 Base_Type (Etype (Actual_Discr))
11378 then
11379 Error_Msg_NE
11380 ("types of actual discriminants must match formal",
11381 Actual, Gen_T);
11382 Abandon_Instantiation (Actual);
11383
11384 elsif not Subtypes_Statically_Match
11385 (Formal_Subt, Etype (Actual_Discr))
11386 and then Ada_Version >= Ada_95
11387 then
11388 Error_Msg_NE
11389 ("subtypes of actual discriminants must match formal",
11390 Actual, Gen_T);
11391 Abandon_Instantiation (Actual);
11392 end if;
11393
11394 Next_Discriminant (Formal_Discr);
11395 Next_Discriminant (Actual_Discr);
11396 end loop;
11397
11398 if Actual_Discr /= Empty then
11399 Error_Msg_NE
11400 ("discriminants on actual do not match formal",
11401 Actual, Gen_T);
11402 Abandon_Instantiation (Actual);
11403 end if;
11404 end if;
11405 end if;
11406 end Validate_Discriminated_Formal_Type;
11407
11408 ---------------------------------------
11409 -- Validate_Incomplete_Type_Instance --
11410 ---------------------------------------
11411
11412 procedure Validate_Incomplete_Type_Instance is
11413 begin
11414 if not Is_Tagged_Type (Act_T)
11415 and then Is_Tagged_Type (A_Gen_T)
11416 then
11417 Error_Msg_NE
11418 ("actual for & must be a tagged type", Actual, Gen_T);
11419 end if;
11420
11421 Validate_Discriminated_Formal_Type;
11422 end Validate_Incomplete_Type_Instance;
11423
11424 --------------------------------------
11425 -- Validate_Interface_Type_Instance --
11426 --------------------------------------
11427
11428 procedure Validate_Interface_Type_Instance is
11429 begin
11430 if not Is_Interface (Act_T) then
11431 Error_Msg_NE
11432 ("actual for formal interface type must be an interface",
11433 Actual, Gen_T);
11434
11435 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
11436 or else
11437 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
11438 or else
11439 Is_Protected_Interface (A_Gen_T) /=
11440 Is_Protected_Interface (Act_T)
11441 or else
11442 Is_Synchronized_Interface (A_Gen_T) /=
11443 Is_Synchronized_Interface (Act_T)
11444 then
11445 Error_Msg_NE
11446 ("actual for interface& does not match (RM 12.5.5(4))",
11447 Actual, Gen_T);
11448 end if;
11449 end Validate_Interface_Type_Instance;
11450
11451 ------------------------------------
11452 -- Validate_Private_Type_Instance --
11453 ------------------------------------
11454
11455 procedure Validate_Private_Type_Instance is
11456 begin
11457 if Is_Limited_Type (Act_T)
11458 and then not Is_Limited_Type (A_Gen_T)
11459 then
11460 if In_Instance then
11461 null;
11462 else
11463 Error_Msg_NE
11464 ("actual for non-limited & cannot be a limited type", Actual,
11465 Gen_T);
11466 Explain_Limited_Type (Act_T, Actual);
11467 Abandon_Instantiation (Actual);
11468 end if;
11469
11470 elsif Known_To_Have_Preelab_Init (A_Gen_T)
11471 and then not Has_Preelaborable_Initialization (Act_T)
11472 then
11473 Error_Msg_NE
11474 ("actual for & must have preelaborable initialization", Actual,
11475 Gen_T);
11476
11477 elsif Is_Indefinite_Subtype (Act_T)
11478 and then not Is_Indefinite_Subtype (A_Gen_T)
11479 and then Ada_Version >= Ada_95
11480 then
11481 Error_Msg_NE
11482 ("actual for & must be a definite subtype", Actual, Gen_T);
11483
11484 elsif not Is_Tagged_Type (Act_T)
11485 and then Is_Tagged_Type (A_Gen_T)
11486 then
11487 Error_Msg_NE
11488 ("actual for & must be a tagged type", Actual, Gen_T);
11489 end if;
11490
11491 Validate_Discriminated_Formal_Type;
11492 Ancestor := Gen_T;
11493 end Validate_Private_Type_Instance;
11494
11495 -- Start of processing for Instantiate_Type
11496
11497 begin
11498 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
11499 Error_Msg_N ("duplicate instantiation of generic type", Actual);
11500 return New_List (Error);
11501
11502 elsif not Is_Entity_Name (Actual)
11503 or else not Is_Type (Entity (Actual))
11504 then
11505 Error_Msg_NE
11506 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
11507 Abandon_Instantiation (Actual);
11508
11509 else
11510 Act_T := Entity (Actual);
11511
11512 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
11513 -- as a generic actual parameter if the corresponding formal type
11514 -- does not have a known_discriminant_part, or is a formal derived
11515 -- type that is an Unchecked_Union type.
11516
11517 if Is_Unchecked_Union (Base_Type (Act_T)) then
11518 if not Has_Discriminants (A_Gen_T)
11519 or else
11520 (Is_Derived_Type (A_Gen_T)
11521 and then
11522 Is_Unchecked_Union (A_Gen_T))
11523 then
11524 null;
11525 else
11526 Error_Msg_N ("unchecked union cannot be the actual for a" &
11527 " discriminated formal type", Act_T);
11528
11529 end if;
11530 end if;
11531
11532 -- Deal with fixed/floating restrictions
11533
11534 if Is_Floating_Point_Type (Act_T) then
11535 Check_Restriction (No_Floating_Point, Actual);
11536 elsif Is_Fixed_Point_Type (Act_T) then
11537 Check_Restriction (No_Fixed_Point, Actual);
11538 end if;
11539
11540 -- Deal with error of using incomplete type as generic actual.
11541 -- This includes limited views of a type, even if the non-limited
11542 -- view may be available.
11543
11544 if Ekind (Act_T) = E_Incomplete_Type
11545 or else (Is_Class_Wide_Type (Act_T)
11546 and then
11547 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
11548 then
11549 -- If the formal is an incomplete type, the actual can be
11550 -- incomplete as well.
11551
11552 if Ekind (A_Gen_T) = E_Incomplete_Type then
11553 null;
11554
11555 elsif Is_Class_Wide_Type (Act_T)
11556 or else No (Full_View (Act_T))
11557 then
11558 Error_Msg_N ("premature use of incomplete type", Actual);
11559 Abandon_Instantiation (Actual);
11560 else
11561 Act_T := Full_View (Act_T);
11562 Set_Entity (Actual, Act_T);
11563
11564 if Has_Private_Component (Act_T) then
11565 Error_Msg_N
11566 ("premature use of type with private component", Actual);
11567 end if;
11568 end if;
11569
11570 -- Deal with error of premature use of private type as generic actual
11571
11572 elsif Is_Private_Type (Act_T)
11573 and then Is_Private_Type (Base_Type (Act_T))
11574 and then not Is_Generic_Type (Act_T)
11575 and then not Is_Derived_Type (Act_T)
11576 and then No (Full_View (Root_Type (Act_T)))
11577 then
11578 -- If the formal is an incomplete type, the actual can be
11579 -- private or incomplete as well.
11580
11581 if Ekind (A_Gen_T) = E_Incomplete_Type then
11582 null;
11583 else
11584 Error_Msg_N ("premature use of private type", Actual);
11585 end if;
11586
11587 elsif Has_Private_Component (Act_T) then
11588 Error_Msg_N
11589 ("premature use of type with private component", Actual);
11590 end if;
11591
11592 Set_Instance_Of (A_Gen_T, Act_T);
11593
11594 -- If the type is generic, the class-wide type may also be used
11595
11596 if Is_Tagged_Type (A_Gen_T)
11597 and then Is_Tagged_Type (Act_T)
11598 and then not Is_Class_Wide_Type (A_Gen_T)
11599 then
11600 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
11601 Class_Wide_Type (Act_T));
11602 end if;
11603
11604 if not Is_Abstract_Type (A_Gen_T)
11605 and then Is_Abstract_Type (Act_T)
11606 then
11607 Error_Msg_N
11608 ("actual of non-abstract formal cannot be abstract", Actual);
11609 end if;
11610
11611 -- A generic scalar type is a first subtype for which we generate
11612 -- an anonymous base type. Indicate that the instance of this base
11613 -- is the base type of the actual.
11614
11615 if Is_Scalar_Type (A_Gen_T) then
11616 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
11617 end if;
11618 end if;
11619
11620 if Error_Posted (Act_T) then
11621 null;
11622 else
11623 case Nkind (Def) is
11624 when N_Formal_Private_Type_Definition =>
11625 Validate_Private_Type_Instance;
11626
11627 when N_Formal_Incomplete_Type_Definition =>
11628 Validate_Incomplete_Type_Instance;
11629
11630 when N_Formal_Derived_Type_Definition =>
11631 Validate_Derived_Type_Instance;
11632
11633 when N_Formal_Discrete_Type_Definition =>
11634 if not Is_Discrete_Type (Act_T) then
11635 Error_Msg_NE
11636 ("expect discrete type in instantiation of&",
11637 Actual, Gen_T);
11638 Abandon_Instantiation (Actual);
11639 end if;
11640
11641 when N_Formal_Signed_Integer_Type_Definition =>
11642 if not Is_Signed_Integer_Type (Act_T) then
11643 Error_Msg_NE
11644 ("expect signed integer type in instantiation of&",
11645 Actual, Gen_T);
11646 Abandon_Instantiation (Actual);
11647 end if;
11648
11649 when N_Formal_Modular_Type_Definition =>
11650 if not Is_Modular_Integer_Type (Act_T) then
11651 Error_Msg_NE
11652 ("expect modular type in instantiation of &",
11653 Actual, Gen_T);
11654 Abandon_Instantiation (Actual);
11655 end if;
11656
11657 when N_Formal_Floating_Point_Definition =>
11658 if not Is_Floating_Point_Type (Act_T) then
11659 Error_Msg_NE
11660 ("expect float type in instantiation of &", Actual, Gen_T);
11661 Abandon_Instantiation (Actual);
11662 end if;
11663
11664 when N_Formal_Ordinary_Fixed_Point_Definition =>
11665 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
11666 Error_Msg_NE
11667 ("expect ordinary fixed point type in instantiation of &",
11668 Actual, Gen_T);
11669 Abandon_Instantiation (Actual);
11670 end if;
11671
11672 when N_Formal_Decimal_Fixed_Point_Definition =>
11673 if not Is_Decimal_Fixed_Point_Type (Act_T) then
11674 Error_Msg_NE
11675 ("expect decimal type in instantiation of &",
11676 Actual, Gen_T);
11677 Abandon_Instantiation (Actual);
11678 end if;
11679
11680 when N_Array_Type_Definition =>
11681 Validate_Array_Type_Instance;
11682
11683 when N_Access_To_Object_Definition =>
11684 Validate_Access_Type_Instance;
11685
11686 when N_Access_Function_Definition |
11687 N_Access_Procedure_Definition =>
11688 Validate_Access_Subprogram_Instance;
11689
11690 when N_Record_Definition =>
11691 Validate_Interface_Type_Instance;
11692
11693 when N_Derived_Type_Definition =>
11694 Validate_Derived_Interface_Type_Instance;
11695
11696 when others =>
11697 raise Program_Error;
11698
11699 end case;
11700 end if;
11701
11702 Subt := New_Copy (Gen_T);
11703
11704 -- Use adjusted sloc of subtype name as the location for other nodes in
11705 -- the subtype declaration.
11706
11707 Loc := Sloc (Subt);
11708
11709 Decl_Node :=
11710 Make_Subtype_Declaration (Loc,
11711 Defining_Identifier => Subt,
11712 Subtype_Indication => New_Reference_To (Act_T, Loc));
11713
11714 if Is_Private_Type (Act_T) then
11715 Set_Has_Private_View (Subtype_Indication (Decl_Node));
11716
11717 elsif Is_Access_Type (Act_T)
11718 and then Is_Private_Type (Designated_Type (Act_T))
11719 then
11720 Set_Has_Private_View (Subtype_Indication (Decl_Node));
11721 end if;
11722
11723 Decl_Nodes := New_List (Decl_Node);
11724
11725 -- Flag actual derived types so their elaboration produces the
11726 -- appropriate renamings for the primitive operations of the ancestor.
11727 -- Flag actual for formal private types as well, to determine whether
11728 -- operations in the private part may override inherited operations.
11729 -- If the formal has an interface list, the ancestor is not the
11730 -- parent, but the analyzed formal that includes the interface
11731 -- operations of all its progenitors.
11732
11733 -- Same treatment for formal private types, so we can check whether the
11734 -- type is tagged limited when validating derivations in the private
11735 -- part. (See AI05-096).
11736
11737 if Nkind (Def) = N_Formal_Derived_Type_Definition then
11738 if Present (Interface_List (Def)) then
11739 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
11740 else
11741 Set_Generic_Parent_Type (Decl_Node, Ancestor);
11742 end if;
11743
11744 elsif Nkind_In (Def,
11745 N_Formal_Private_Type_Definition,
11746 N_Formal_Incomplete_Type_Definition)
11747 then
11748 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
11749 end if;
11750
11751 -- If the actual is a synchronized type that implements an interface,
11752 -- the primitive operations are attached to the corresponding record,
11753 -- and we have to treat it as an additional generic actual, so that its
11754 -- primitive operations become visible in the instance. The task or
11755 -- protected type itself does not carry primitive operations.
11756
11757 if Is_Concurrent_Type (Act_T)
11758 and then Is_Tagged_Type (Act_T)
11759 and then Present (Corresponding_Record_Type (Act_T))
11760 and then Present (Ancestor)
11761 and then Is_Interface (Ancestor)
11762 then
11763 declare
11764 Corr_Rec : constant Entity_Id :=
11765 Corresponding_Record_Type (Act_T);
11766 New_Corr : Entity_Id;
11767 Corr_Decl : Node_Id;
11768
11769 begin
11770 New_Corr := Make_Temporary (Loc, 'S');
11771 Corr_Decl :=
11772 Make_Subtype_Declaration (Loc,
11773 Defining_Identifier => New_Corr,
11774 Subtype_Indication =>
11775 New_Reference_To (Corr_Rec, Loc));
11776 Append_To (Decl_Nodes, Corr_Decl);
11777
11778 if Ekind (Act_T) = E_Task_Type then
11779 Set_Ekind (Subt, E_Task_Subtype);
11780 else
11781 Set_Ekind (Subt, E_Protected_Subtype);
11782 end if;
11783
11784 Set_Corresponding_Record_Type (Subt, Corr_Rec);
11785 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
11786 Set_Generic_Parent_Type (Decl_Node, Empty);
11787 end;
11788 end if;
11789
11790 return Decl_Nodes;
11791 end Instantiate_Type;
11792
11793 ---------------------
11794 -- Is_In_Main_Unit --
11795 ---------------------
11796
11797 function Is_In_Main_Unit (N : Node_Id) return Boolean is
11798 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
11799 Current_Unit : Node_Id;
11800
11801 begin
11802 if Unum = Main_Unit then
11803 return True;
11804
11805 -- If the current unit is a subunit then it is either the main unit or
11806 -- is being compiled as part of the main unit.
11807
11808 elsif Nkind (N) = N_Compilation_Unit then
11809 return Nkind (Unit (N)) = N_Subunit;
11810 end if;
11811
11812 Current_Unit := Parent (N);
11813 while Present (Current_Unit)
11814 and then Nkind (Current_Unit) /= N_Compilation_Unit
11815 loop
11816 Current_Unit := Parent (Current_Unit);
11817 end loop;
11818
11819 -- The instantiation node is in the main unit, or else the current node
11820 -- (perhaps as the result of nested instantiations) is in the main unit,
11821 -- or in the declaration of the main unit, which in this last case must
11822 -- be a body.
11823
11824 return Unum = Main_Unit
11825 or else Current_Unit = Cunit (Main_Unit)
11826 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
11827 or else (Present (Library_Unit (Current_Unit))
11828 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
11829 end Is_In_Main_Unit;
11830
11831 ----------------------------
11832 -- Load_Parent_Of_Generic --
11833 ----------------------------
11834
11835 procedure Load_Parent_Of_Generic
11836 (N : Node_Id;
11837 Spec : Node_Id;
11838 Body_Optional : Boolean := False)
11839 is
11840 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
11841 Saved_Style_Check : constant Boolean := Style_Check;
11842 Saved_Warnings : constant Warning_Record := Save_Warnings;
11843 True_Parent : Node_Id;
11844 Inst_Node : Node_Id;
11845 OK : Boolean;
11846 Previous_Instances : constant Elist_Id := New_Elmt_List;
11847
11848 procedure Collect_Previous_Instances (Decls : List_Id);
11849 -- Collect all instantiations in the given list of declarations, that
11850 -- precede the generic that we need to load. If the bodies of these
11851 -- instantiations are available, we must analyze them, to ensure that
11852 -- the public symbols generated are the same when the unit is compiled
11853 -- to generate code, and when it is compiled in the context of a unit
11854 -- that needs a particular nested instance. This process is applied to
11855 -- both package and subprogram instances.
11856
11857 --------------------------------
11858 -- Collect_Previous_Instances --
11859 --------------------------------
11860
11861 procedure Collect_Previous_Instances (Decls : List_Id) is
11862 Decl : Node_Id;
11863
11864 begin
11865 Decl := First (Decls);
11866 while Present (Decl) loop
11867 if Sloc (Decl) >= Sloc (Inst_Node) then
11868 return;
11869
11870 -- If Decl is an instantiation, then record it as requiring
11871 -- instantiation of the corresponding body, except if it is an
11872 -- abbreviated instantiation generated internally for conformance
11873 -- checking purposes only for the case of a formal package
11874 -- declared without a box (see Instantiate_Formal_Package). Such
11875 -- an instantiation does not generate any code (the actual code
11876 -- comes from actual) and thus does not need to be analyzed here.
11877 -- If the instantiation appears with a generic package body it is
11878 -- not analyzed here either.
11879
11880 elsif Nkind (Decl) = N_Package_Instantiation
11881 and then not Is_Internal (Defining_Entity (Decl))
11882 then
11883 Append_Elmt (Decl, Previous_Instances);
11884
11885 -- For a subprogram instantiation, omit instantiations intrinsic
11886 -- operations (Unchecked_Conversions, etc.) that have no bodies.
11887
11888 elsif Nkind_In (Decl, N_Function_Instantiation,
11889 N_Procedure_Instantiation)
11890 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
11891 then
11892 Append_Elmt (Decl, Previous_Instances);
11893
11894 elsif Nkind (Decl) = N_Package_Declaration then
11895 Collect_Previous_Instances
11896 (Visible_Declarations (Specification (Decl)));
11897 Collect_Previous_Instances
11898 (Private_Declarations (Specification (Decl)));
11899
11900 -- Previous non-generic bodies may contain instances as well
11901
11902 elsif Nkind (Decl) = N_Package_Body
11903 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
11904 then
11905 Collect_Previous_Instances (Declarations (Decl));
11906
11907 elsif Nkind (Decl) = N_Subprogram_Body
11908 and then not Acts_As_Spec (Decl)
11909 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
11910 then
11911 Collect_Previous_Instances (Declarations (Decl));
11912 end if;
11913
11914 Next (Decl);
11915 end loop;
11916 end Collect_Previous_Instances;
11917
11918 -- Start of processing for Load_Parent_Of_Generic
11919
11920 begin
11921 if not In_Same_Source_Unit (N, Spec)
11922 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
11923 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
11924 and then not Is_In_Main_Unit (Spec))
11925 then
11926 -- Find body of parent of spec, and analyze it. A special case arises
11927 -- when the parent is an instantiation, that is to say when we are
11928 -- currently instantiating a nested generic. In that case, there is
11929 -- no separate file for the body of the enclosing instance. Instead,
11930 -- the enclosing body must be instantiated as if it were a pending
11931 -- instantiation, in order to produce the body for the nested generic
11932 -- we require now. Note that in that case the generic may be defined
11933 -- in a package body, the instance defined in the same package body,
11934 -- and the original enclosing body may not be in the main unit.
11935
11936 Inst_Node := Empty;
11937
11938 True_Parent := Parent (Spec);
11939 while Present (True_Parent)
11940 and then Nkind (True_Parent) /= N_Compilation_Unit
11941 loop
11942 if Nkind (True_Parent) = N_Package_Declaration
11943 and then
11944 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
11945 then
11946 -- Parent is a compilation unit that is an instantiation.
11947 -- Instantiation node has been replaced with package decl.
11948
11949 Inst_Node := Original_Node (True_Parent);
11950 exit;
11951
11952 elsif Nkind (True_Parent) = N_Package_Declaration
11953 and then Present (Generic_Parent (Specification (True_Parent)))
11954 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
11955 then
11956 -- Parent is an instantiation within another specification.
11957 -- Declaration for instance has been inserted before original
11958 -- instantiation node. A direct link would be preferable?
11959
11960 Inst_Node := Next (True_Parent);
11961 while Present (Inst_Node)
11962 and then Nkind (Inst_Node) /= N_Package_Instantiation
11963 loop
11964 Next (Inst_Node);
11965 end loop;
11966
11967 -- If the instance appears within a generic, and the generic
11968 -- unit is defined within a formal package of the enclosing
11969 -- generic, there is no generic body available, and none
11970 -- needed. A more precise test should be used ???
11971
11972 if No (Inst_Node) then
11973 return;
11974 end if;
11975
11976 exit;
11977
11978 else
11979 True_Parent := Parent (True_Parent);
11980 end if;
11981 end loop;
11982
11983 -- Case where we are currently instantiating a nested generic
11984
11985 if Present (Inst_Node) then
11986 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
11987
11988 -- Instantiation node and declaration of instantiated package
11989 -- were exchanged when only the declaration was needed.
11990 -- Restore instantiation node before proceeding with body.
11991
11992 Set_Unit (Parent (True_Parent), Inst_Node);
11993 end if;
11994
11995 -- Now complete instantiation of enclosing body, if it appears in
11996 -- some other unit. If it appears in the current unit, the body
11997 -- will have been instantiated already.
11998
11999 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12000
12001 -- We need to determine the expander mode to instantiate the
12002 -- enclosing body. Because the generic body we need may use
12003 -- global entities declared in the enclosing package (including
12004 -- aggregates) it is in general necessary to compile this body
12005 -- with expansion enabled, except if we are within a generic
12006 -- package, in which case the usual generic rule applies.
12007
12008 declare
12009 Exp_Status : Boolean := True;
12010 Scop : Entity_Id;
12011
12012 begin
12013 -- Loop through scopes looking for generic package
12014
12015 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12016 while Present (Scop)
12017 and then Scop /= Standard_Standard
12018 loop
12019 if Ekind (Scop) = E_Generic_Package then
12020 Exp_Status := False;
12021 exit;
12022 end if;
12023
12024 Scop := Scope (Scop);
12025 end loop;
12026
12027 -- Collect previous instantiations in the unit that contains
12028 -- the desired generic.
12029
12030 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12031 and then not Body_Optional
12032 then
12033 declare
12034 Decl : Elmt_Id;
12035 Info : Pending_Body_Info;
12036 Par : Node_Id;
12037
12038 begin
12039 Par := Parent (Inst_Node);
12040 while Present (Par) loop
12041 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12042 Par := Parent (Par);
12043 end loop;
12044
12045 pragma Assert (Present (Par));
12046
12047 if Nkind (Par) = N_Package_Body then
12048 Collect_Previous_Instances (Declarations (Par));
12049
12050 elsif Nkind (Par) = N_Package_Declaration then
12051 Collect_Previous_Instances
12052 (Visible_Declarations (Specification (Par)));
12053 Collect_Previous_Instances
12054 (Private_Declarations (Specification (Par)));
12055
12056 else
12057 -- Enclosing unit is a subprogram body. In this
12058 -- case all instance bodies are processed in order
12059 -- and there is no need to collect them separately.
12060
12061 null;
12062 end if;
12063
12064 Decl := First_Elmt (Previous_Instances);
12065 while Present (Decl) loop
12066 Info :=
12067 (Inst_Node => Node (Decl),
12068 Act_Decl =>
12069 Instance_Spec (Node (Decl)),
12070 Expander_Status => Exp_Status,
12071 Current_Sem_Unit =>
12072 Get_Code_Unit (Sloc (Node (Decl))),
12073 Scope_Suppress => Scope_Suppress,
12074 Local_Suppress_Stack_Top =>
12075 Local_Suppress_Stack_Top,
12076 Version => Ada_Version,
12077 Version_Pragma => Ada_Version_Pragma,
12078 Warnings => Save_Warnings);
12079
12080 -- Package instance
12081
12082 if
12083 Nkind (Node (Decl)) = N_Package_Instantiation
12084 then
12085 Instantiate_Package_Body
12086 (Info, Body_Optional => True);
12087
12088 -- Subprogram instance
12089
12090 else
12091 -- The instance_spec is the wrapper package,
12092 -- and the subprogram declaration is the last
12093 -- declaration in the wrapper.
12094
12095 Info.Act_Decl :=
12096 Last
12097 (Visible_Declarations
12098 (Specification (Info.Act_Decl)));
12099
12100 Instantiate_Subprogram_Body
12101 (Info, Body_Optional => True);
12102 end if;
12103
12104 Next_Elmt (Decl);
12105 end loop;
12106 end;
12107 end if;
12108
12109 Instantiate_Package_Body
12110 (Body_Info =>
12111 ((Inst_Node => Inst_Node,
12112 Act_Decl => True_Parent,
12113 Expander_Status => Exp_Status,
12114 Current_Sem_Unit => Get_Code_Unit
12115 (Sloc (Inst_Node)),
12116 Scope_Suppress => Scope_Suppress,
12117 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12118 Version => Ada_Version,
12119 Version_Pragma => Ada_Version_Pragma,
12120 Warnings => Save_Warnings)),
12121 Body_Optional => Body_Optional);
12122 end;
12123 end if;
12124
12125 -- Case where we are not instantiating a nested generic
12126
12127 else
12128 Opt.Style_Check := False;
12129 Expander_Mode_Save_And_Set (True);
12130 Load_Needed_Body (Comp_Unit, OK);
12131 Opt.Style_Check := Saved_Style_Check;
12132 Restore_Warnings (Saved_Warnings);
12133 Expander_Mode_Restore;
12134
12135 if not OK
12136 and then Unit_Requires_Body (Defining_Entity (Spec))
12137 and then not Body_Optional
12138 then
12139 declare
12140 Bname : constant Unit_Name_Type :=
12141 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12142
12143 begin
12144 -- In CodePeer mode, the missing body may make the analysis
12145 -- incomplete, but we do not treat it as fatal.
12146
12147 if CodePeer_Mode then
12148 return;
12149
12150 else
12151 Error_Msg_Unit_1 := Bname;
12152 Error_Msg_N ("this instantiation requires$!", N);
12153 Error_Msg_File_1 :=
12154 Get_File_Name (Bname, Subunit => False);
12155 Error_Msg_N ("\but file{ was not found!", N);
12156 raise Unrecoverable_Error;
12157 end if;
12158 end;
12159 end if;
12160 end if;
12161 end if;
12162
12163 -- If loading parent of the generic caused an instantiation circularity,
12164 -- we abandon compilation at this point, because otherwise in some cases
12165 -- we get into trouble with infinite recursions after this point.
12166
12167 if Circularity_Detected then
12168 raise Unrecoverable_Error;
12169 end if;
12170 end Load_Parent_Of_Generic;
12171
12172 ---------------------------------
12173 -- Map_Formal_Package_Entities --
12174 ---------------------------------
12175
12176 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
12177 E1 : Entity_Id;
12178 E2 : Entity_Id;
12179
12180 begin
12181 Set_Instance_Of (Form, Act);
12182
12183 -- Traverse formal and actual package to map the corresponding entities.
12184 -- We skip over internal entities that may be generated during semantic
12185 -- analysis, and find the matching entities by name, given that they
12186 -- must appear in the same order.
12187
12188 E1 := First_Entity (Form);
12189 E2 := First_Entity (Act);
12190 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
12191 -- Could this test be a single condition??? Seems like it could, and
12192 -- isn't FPE (Form) a constant anyway???
12193
12194 if not Is_Internal (E1)
12195 and then Present (Parent (E1))
12196 and then not Is_Class_Wide_Type (E1)
12197 and then not Is_Internal_Name (Chars (E1))
12198 then
12199 while Present (E2) and then Chars (E2) /= Chars (E1) loop
12200 Next_Entity (E2);
12201 end loop;
12202
12203 if No (E2) then
12204 exit;
12205 else
12206 Set_Instance_Of (E1, E2);
12207
12208 if Is_Type (E1) and then Is_Tagged_Type (E2) then
12209 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
12210 end if;
12211
12212 if Is_Constrained (E1) then
12213 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
12214 end if;
12215
12216 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
12217 Map_Formal_Package_Entities (E1, E2);
12218 end if;
12219 end if;
12220 end if;
12221
12222 Next_Entity (E1);
12223 end loop;
12224 end Map_Formal_Package_Entities;
12225
12226 -----------------------
12227 -- Move_Freeze_Nodes --
12228 -----------------------
12229
12230 procedure Move_Freeze_Nodes
12231 (Out_Of : Entity_Id;
12232 After : Node_Id;
12233 L : List_Id)
12234 is
12235 Decl : Node_Id;
12236 Next_Decl : Node_Id;
12237 Next_Node : Node_Id := After;
12238 Spec : Node_Id;
12239
12240 function Is_Outer_Type (T : Entity_Id) return Boolean;
12241 -- Check whether entity is declared in a scope external to that of the
12242 -- generic unit.
12243
12244 -------------------
12245 -- Is_Outer_Type --
12246 -------------------
12247
12248 function Is_Outer_Type (T : Entity_Id) return Boolean is
12249 Scop : Entity_Id := Scope (T);
12250
12251 begin
12252 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
12253 return True;
12254
12255 else
12256 while Scop /= Standard_Standard loop
12257 if Scop = Out_Of then
12258 return False;
12259 else
12260 Scop := Scope (Scop);
12261 end if;
12262 end loop;
12263
12264 return True;
12265 end if;
12266 end Is_Outer_Type;
12267
12268 -- Start of processing for Move_Freeze_Nodes
12269
12270 begin
12271 if No (L) then
12272 return;
12273 end if;
12274
12275 -- First remove the freeze nodes that may appear before all other
12276 -- declarations.
12277
12278 Decl := First (L);
12279 while Present (Decl)
12280 and then Nkind (Decl) = N_Freeze_Entity
12281 and then Is_Outer_Type (Entity (Decl))
12282 loop
12283 Decl := Remove_Head (L);
12284 Insert_After (Next_Node, Decl);
12285 Set_Analyzed (Decl, False);
12286 Next_Node := Decl;
12287 Decl := First (L);
12288 end loop;
12289
12290 -- Next scan the list of declarations and remove each freeze node that
12291 -- appears ahead of the current node.
12292
12293 while Present (Decl) loop
12294 while Present (Next (Decl))
12295 and then Nkind (Next (Decl)) = N_Freeze_Entity
12296 and then Is_Outer_Type (Entity (Next (Decl)))
12297 loop
12298 Next_Decl := Remove_Next (Decl);
12299 Insert_After (Next_Node, Next_Decl);
12300 Set_Analyzed (Next_Decl, False);
12301 Next_Node := Next_Decl;
12302 end loop;
12303
12304 -- If the declaration is a nested package or concurrent type, then
12305 -- recurse. Nested generic packages will have been processed from the
12306 -- inside out.
12307
12308 case Nkind (Decl) is
12309 when N_Package_Declaration =>
12310 Spec := Specification (Decl);
12311
12312 when N_Task_Type_Declaration =>
12313 Spec := Task_Definition (Decl);
12314
12315 when N_Protected_Type_Declaration =>
12316 Spec := Protected_Definition (Decl);
12317
12318 when others =>
12319 Spec := Empty;
12320 end case;
12321
12322 if Present (Spec) then
12323 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
12324 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
12325 end if;
12326
12327 Next (Decl);
12328 end loop;
12329 end Move_Freeze_Nodes;
12330
12331 ----------------
12332 -- Next_Assoc --
12333 ----------------
12334
12335 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
12336 begin
12337 return Generic_Renamings.Table (E).Next_In_HTable;
12338 end Next_Assoc;
12339
12340 ------------------------
12341 -- Preanalyze_Actuals --
12342 ------------------------
12343
12344 procedure Preanalyze_Actuals (N : Node_Id) is
12345 Assoc : Node_Id;
12346 Act : Node_Id;
12347 Errs : constant Int := Serious_Errors_Detected;
12348
12349 Cur : Entity_Id := Empty;
12350 -- Current homograph of the instance name
12351
12352 Vis : Boolean;
12353 -- Saved visibility status of the current homograph
12354
12355 begin
12356 Assoc := First (Generic_Associations (N));
12357
12358 -- If the instance is a child unit, its name may hide an outer homonym,
12359 -- so make it invisible to perform name resolution on the actuals.
12360
12361 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
12362 and then Present
12363 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
12364 then
12365 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
12366
12367 if Is_Compilation_Unit (Cur) then
12368 Vis := Is_Immediately_Visible (Cur);
12369 Set_Is_Immediately_Visible (Cur, False);
12370 else
12371 Cur := Empty;
12372 end if;
12373 end if;
12374
12375 while Present (Assoc) loop
12376 if Nkind (Assoc) /= N_Others_Choice then
12377 Act := Explicit_Generic_Actual_Parameter (Assoc);
12378
12379 -- Within a nested instantiation, a defaulted actual is an empty
12380 -- association, so nothing to analyze. If the subprogram actual
12381 -- is an attribute, analyze prefix only, because actual is not a
12382 -- complete attribute reference.
12383
12384 -- If actual is an allocator, analyze expression only. The full
12385 -- analysis can generate code, and if instance is a compilation
12386 -- unit we have to wait until the package instance is installed
12387 -- to have a proper place to insert this code.
12388
12389 -- String literals may be operators, but at this point we do not
12390 -- know whether the actual is a formal subprogram or a string.
12391
12392 if No (Act) then
12393 null;
12394
12395 elsif Nkind (Act) = N_Attribute_Reference then
12396 Analyze (Prefix (Act));
12397
12398 elsif Nkind (Act) = N_Explicit_Dereference then
12399 Analyze (Prefix (Act));
12400
12401 elsif Nkind (Act) = N_Allocator then
12402 declare
12403 Expr : constant Node_Id := Expression (Act);
12404
12405 begin
12406 if Nkind (Expr) = N_Subtype_Indication then
12407 Analyze (Subtype_Mark (Expr));
12408
12409 -- Analyze separately each discriminant constraint, when
12410 -- given with a named association.
12411
12412 declare
12413 Constr : Node_Id;
12414
12415 begin
12416 Constr := First (Constraints (Constraint (Expr)));
12417 while Present (Constr) loop
12418 if Nkind (Constr) = N_Discriminant_Association then
12419 Analyze (Expression (Constr));
12420 else
12421 Analyze (Constr);
12422 end if;
12423
12424 Next (Constr);
12425 end loop;
12426 end;
12427
12428 else
12429 Analyze (Expr);
12430 end if;
12431 end;
12432
12433 elsif Nkind (Act) /= N_Operator_Symbol then
12434 Analyze (Act);
12435 end if;
12436
12437 -- Ensure that a ghost subprogram does not act as generic actual
12438
12439 if Is_Entity_Name (Act)
12440 and then Is_Ghost_Subprogram (Entity (Act))
12441 then
12442 Error_Msg_N
12443 ("ghost subprogram & cannot act as generic actual", Act);
12444 Abandon_Instantiation (Act);
12445
12446 elsif Errs /= Serious_Errors_Detected then
12447
12448 -- Do a minimal analysis of the generic, to prevent spurious
12449 -- warnings complaining about the generic being unreferenced,
12450 -- before abandoning the instantiation.
12451
12452 Analyze (Name (N));
12453
12454 if Is_Entity_Name (Name (N))
12455 and then Etype (Name (N)) /= Any_Type
12456 then
12457 Generate_Reference (Entity (Name (N)), Name (N));
12458 Set_Is_Instantiated (Entity (Name (N)));
12459 end if;
12460
12461 if Present (Cur) then
12462
12463 -- For the case of a child instance hiding an outer homonym,
12464 -- provide additional warning which might explain the error.
12465
12466 Set_Is_Immediately_Visible (Cur, Vis);
12467 Error_Msg_NE ("& hides outer unit with the same name??",
12468 N, Defining_Unit_Name (N));
12469 end if;
12470
12471 Abandon_Instantiation (Act);
12472 end if;
12473 end if;
12474
12475 Next (Assoc);
12476 end loop;
12477
12478 if Present (Cur) then
12479 Set_Is_Immediately_Visible (Cur, Vis);
12480 end if;
12481 end Preanalyze_Actuals;
12482
12483 -------------------
12484 -- Remove_Parent --
12485 -------------------
12486
12487 procedure Remove_Parent (In_Body : Boolean := False) is
12488 S : Entity_Id := Current_Scope;
12489 -- S is the scope containing the instantiation just completed. The scope
12490 -- stack contains the parent instances of the instantiation, followed by
12491 -- the original S.
12492
12493 Cur_P : Entity_Id;
12494 E : Entity_Id;
12495 P : Entity_Id;
12496 Hidden : Elmt_Id;
12497
12498 begin
12499 -- After child instantiation is complete, remove from scope stack the
12500 -- extra copy of the current scope, and then remove parent instances.
12501
12502 if not In_Body then
12503 Pop_Scope;
12504
12505 while Current_Scope /= S loop
12506 P := Current_Scope;
12507 End_Package_Scope (Current_Scope);
12508
12509 if In_Open_Scopes (P) then
12510 E := First_Entity (P);
12511 while Present (E) loop
12512 Set_Is_Immediately_Visible (E, True);
12513 Next_Entity (E);
12514 end loop;
12515
12516 -- If instantiation is declared in a block, it is the enclosing
12517 -- scope that might be a parent instance. Note that only one
12518 -- block can be involved, because the parent instances have
12519 -- been installed within it.
12520
12521 if Ekind (P) = E_Block then
12522 Cur_P := Scope (P);
12523 else
12524 Cur_P := P;
12525 end if;
12526
12527 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
12528 -- We are within an instance of some sibling. Retain
12529 -- visibility of parent, for proper subsequent cleanup, and
12530 -- reinstall private declarations as well.
12531
12532 Set_In_Private_Part (P);
12533 Install_Private_Declarations (P);
12534 end if;
12535
12536 -- If the ultimate parent is a top-level unit recorded in
12537 -- Instance_Parent_Unit, then reset its visibility to what it was
12538 -- before instantiation. (It's not clear what the purpose is of
12539 -- testing whether Scope (P) is In_Open_Scopes, but that test was
12540 -- present before the ultimate parent test was added.???)
12541
12542 elsif not In_Open_Scopes (Scope (P))
12543 or else (P = Instance_Parent_Unit
12544 and then not Parent_Unit_Visible)
12545 then
12546 Set_Is_Immediately_Visible (P, False);
12547
12548 -- If the current scope is itself an instantiation of a generic
12549 -- nested within P, and we are in the private part of body of this
12550 -- instantiation, restore the full views of P, that were removed
12551 -- in End_Package_Scope above. This obscure case can occur when a
12552 -- subunit of a generic contains an instance of a child unit of
12553 -- its generic parent unit.
12554
12555 elsif S = Current_Scope and then Is_Generic_Instance (S) then
12556 declare
12557 Par : constant Entity_Id :=
12558 Generic_Parent (Package_Specification (S));
12559 begin
12560 if Present (Par)
12561 and then P = Scope (Par)
12562 and then (In_Package_Body (S) or else In_Private_Part (S))
12563 then
12564 Set_In_Private_Part (P);
12565 Install_Private_Declarations (P);
12566 end if;
12567 end;
12568 end if;
12569 end loop;
12570
12571 -- Reset visibility of entities in the enclosing scope
12572
12573 Set_Is_Hidden_Open_Scope (Current_Scope, False);
12574
12575 Hidden := First_Elmt (Hidden_Entities);
12576 while Present (Hidden) loop
12577 Set_Is_Immediately_Visible (Node (Hidden), True);
12578 Next_Elmt (Hidden);
12579 end loop;
12580
12581 else
12582 -- Each body is analyzed separately, and there is no context that
12583 -- needs preserving from one body instance to the next, so remove all
12584 -- parent scopes that have been installed.
12585
12586 while Present (S) loop
12587 End_Package_Scope (S);
12588 Set_Is_Immediately_Visible (S, False);
12589 S := Current_Scope;
12590 exit when S = Standard_Standard;
12591 end loop;
12592 end if;
12593 end Remove_Parent;
12594
12595 -----------------
12596 -- Restore_Env --
12597 -----------------
12598
12599 procedure Restore_Env is
12600 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
12601
12602 begin
12603 if No (Current_Instantiated_Parent.Act_Id) then
12604 -- Restore environment after subprogram inlining
12605
12606 Restore_Private_Views (Empty);
12607 end if;
12608
12609 Current_Instantiated_Parent := Saved.Instantiated_Parent;
12610 Exchanged_Views := Saved.Exchanged_Views;
12611 Hidden_Entities := Saved.Hidden_Entities;
12612 Current_Sem_Unit := Saved.Current_Sem_Unit;
12613 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
12614 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
12615
12616 Restore_Opt_Config_Switches (Saved.Switches);
12617
12618 Instance_Envs.Decrement_Last;
12619 end Restore_Env;
12620
12621 ---------------------------
12622 -- Restore_Private_Views --
12623 ---------------------------
12624
12625 procedure Restore_Private_Views
12626 (Pack_Id : Entity_Id;
12627 Is_Package : Boolean := True)
12628 is
12629 M : Elmt_Id;
12630 E : Entity_Id;
12631 Typ : Entity_Id;
12632 Dep_Elmt : Elmt_Id;
12633 Dep_Typ : Node_Id;
12634
12635 procedure Restore_Nested_Formal (Formal : Entity_Id);
12636 -- Hide the generic formals of formal packages declared with box which
12637 -- were reachable in the current instantiation.
12638
12639 ---------------------------
12640 -- Restore_Nested_Formal --
12641 ---------------------------
12642
12643 procedure Restore_Nested_Formal (Formal : Entity_Id) is
12644 Ent : Entity_Id;
12645
12646 begin
12647 if Present (Renamed_Object (Formal))
12648 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
12649 then
12650 return;
12651
12652 elsif Present (Associated_Formal_Package (Formal)) then
12653 Ent := First_Entity (Formal);
12654 while Present (Ent) loop
12655 exit when Ekind (Ent) = E_Package
12656 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
12657
12658 Set_Is_Hidden (Ent);
12659 Set_Is_Potentially_Use_Visible (Ent, False);
12660
12661 -- If package, then recurse
12662
12663 if Ekind (Ent) = E_Package then
12664 Restore_Nested_Formal (Ent);
12665 end if;
12666
12667 Next_Entity (Ent);
12668 end loop;
12669 end if;
12670 end Restore_Nested_Formal;
12671
12672 -- Start of processing for Restore_Private_Views
12673
12674 begin
12675 M := First_Elmt (Exchanged_Views);
12676 while Present (M) loop
12677 Typ := Node (M);
12678
12679 -- Subtypes of types whose views have been exchanged, and that are
12680 -- defined within the instance, were not on the Private_Dependents
12681 -- list on entry to the instance, so they have to be exchanged
12682 -- explicitly now, in order to remain consistent with the view of the
12683 -- parent type.
12684
12685 if Ekind_In (Typ, E_Private_Type,
12686 E_Limited_Private_Type,
12687 E_Record_Type_With_Private)
12688 then
12689 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
12690 while Present (Dep_Elmt) loop
12691 Dep_Typ := Node (Dep_Elmt);
12692
12693 if Scope (Dep_Typ) = Pack_Id
12694 and then Present (Full_View (Dep_Typ))
12695 then
12696 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
12697 Exchange_Declarations (Dep_Typ);
12698 end if;
12699
12700 Next_Elmt (Dep_Elmt);
12701 end loop;
12702 end if;
12703
12704 Exchange_Declarations (Node (M));
12705 Next_Elmt (M);
12706 end loop;
12707
12708 if No (Pack_Id) then
12709 return;
12710 end if;
12711
12712 -- Make the generic formal parameters private, and make the formal types
12713 -- into subtypes of the actuals again.
12714
12715 E := First_Entity (Pack_Id);
12716 while Present (E) loop
12717 Set_Is_Hidden (E, True);
12718
12719 if Is_Type (E)
12720 and then Nkind (Parent (E)) = N_Subtype_Declaration
12721 then
12722 -- If the actual for E is itself a generic actual type from
12723 -- an enclosing instance, E is still a generic actual type
12724 -- outside of the current instance. This matter when resolving
12725 -- an overloaded call that may be ambiguous in the enclosing
12726 -- instance, when two of its actuals coincide.
12727
12728 if Is_Entity_Name (Subtype_Indication (Parent (E)))
12729 and then Is_Generic_Actual_Type
12730 (Entity (Subtype_Indication (Parent (E))))
12731 then
12732 null;
12733 else
12734 Set_Is_Generic_Actual_Type (E, False);
12735 end if;
12736
12737 -- An unusual case of aliasing: the actual may also be directly
12738 -- visible in the generic, and be private there, while it is fully
12739 -- visible in the context of the instance. The internal subtype
12740 -- is private in the instance but has full visibility like its
12741 -- parent in the enclosing scope. This enforces the invariant that
12742 -- the privacy status of all private dependents of a type coincide
12743 -- with that of the parent type. This can only happen when a
12744 -- generic child unit is instantiated within a sibling.
12745
12746 if Is_Private_Type (E)
12747 and then not Is_Private_Type (Etype (E))
12748 then
12749 Exchange_Declarations (E);
12750 end if;
12751
12752 elsif Ekind (E) = E_Package then
12753
12754 -- The end of the renaming list is the renaming of the generic
12755 -- package itself. If the instance is a subprogram, all entities
12756 -- in the corresponding package are renamings. If this entity is
12757 -- a formal package, make its own formals private as well. The
12758 -- actual in this case is itself the renaming of an instantiation.
12759 -- If the entity is not a package renaming, it is the entity
12760 -- created to validate formal package actuals: ignore it.
12761
12762 -- If the actual is itself a formal package for the enclosing
12763 -- generic, or the actual for such a formal package, it remains
12764 -- visible on exit from the instance, and therefore nothing needs
12765 -- to be done either, except to keep it accessible.
12766
12767 if Is_Package and then Renamed_Object (E) = Pack_Id then
12768 exit;
12769
12770 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
12771 null;
12772
12773 elsif
12774 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
12775 then
12776 Set_Is_Hidden (E, False);
12777
12778 else
12779 declare
12780 Act_P : constant Entity_Id := Renamed_Object (E);
12781 Id : Entity_Id;
12782
12783 begin
12784 Id := First_Entity (Act_P);
12785 while Present (Id)
12786 and then Id /= First_Private_Entity (Act_P)
12787 loop
12788 exit when Ekind (Id) = E_Package
12789 and then Renamed_Object (Id) = Act_P;
12790
12791 Set_Is_Hidden (Id, True);
12792 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
12793
12794 if Ekind (Id) = E_Package then
12795 Restore_Nested_Formal (Id);
12796 end if;
12797
12798 Next_Entity (Id);
12799 end loop;
12800 end;
12801 end if;
12802 end if;
12803
12804 Next_Entity (E);
12805 end loop;
12806 end Restore_Private_Views;
12807
12808 --------------
12809 -- Save_Env --
12810 --------------
12811
12812 procedure Save_Env
12813 (Gen_Unit : Entity_Id;
12814 Act_Unit : Entity_Id)
12815 is
12816 begin
12817 Init_Env;
12818 Set_Instance_Env (Gen_Unit, Act_Unit);
12819 end Save_Env;
12820
12821 ----------------------------
12822 -- Save_Global_References --
12823 ----------------------------
12824
12825 procedure Save_Global_References (N : Node_Id) is
12826 Gen_Scope : Entity_Id;
12827 E : Entity_Id;
12828 N2 : Node_Id;
12829
12830 function Is_Global (E : Entity_Id) return Boolean;
12831 -- Check whether entity is defined outside of generic unit. Examine the
12832 -- scope of an entity, and the scope of the scope, etc, until we find
12833 -- either Standard, in which case the entity is global, or the generic
12834 -- unit itself, which indicates that the entity is local. If the entity
12835 -- is the generic unit itself, as in the case of a recursive call, or
12836 -- the enclosing generic unit, if different from the current scope, then
12837 -- it is local as well, because it will be replaced at the point of
12838 -- instantiation. On the other hand, if it is a reference to a child
12839 -- unit of a common ancestor, which appears in an instantiation, it is
12840 -- global because it is used to denote a specific compilation unit at
12841 -- the time the instantiations will be analyzed.
12842
12843 procedure Reset_Entity (N : Node_Id);
12844 -- Save semantic information on global entity so that it is not resolved
12845 -- again at instantiation time.
12846
12847 procedure Save_Entity_Descendants (N : Node_Id);
12848 -- Apply Save_Global_References to the two syntactic descendants of
12849 -- non-terminal nodes that carry an Associated_Node and are processed
12850 -- through Reset_Entity. Once the global entity (if any) has been
12851 -- captured together with its type, only two syntactic descendants need
12852 -- to be traversed to complete the processing of the tree rooted at N.
12853 -- This applies to Selected_Components, Expanded_Names, and to Operator
12854 -- nodes. N can also be a character literal, identifier, or operator
12855 -- symbol node, but the call has no effect in these cases.
12856
12857 procedure Save_Global_Defaults (N1, N2 : Node_Id);
12858 -- Default actuals in nested instances must be handled specially
12859 -- because there is no link to them from the original tree. When an
12860 -- actual subprogram is given by a default, we add an explicit generic
12861 -- association for it in the instantiation node. When we save the
12862 -- global references on the name of the instance, we recover the list
12863 -- of generic associations, and add an explicit one to the original
12864 -- generic tree, through which a global actual can be preserved.
12865 -- Similarly, if a child unit is instantiated within a sibling, in the
12866 -- context of the parent, we must preserve the identifier of the parent
12867 -- so that it can be properly resolved in a subsequent instantiation.
12868
12869 procedure Save_Global_Descendant (D : Union_Id);
12870 -- Apply Save_Global_References recursively to the descendents of the
12871 -- current node.
12872
12873 procedure Save_References (N : Node_Id);
12874 -- This is the recursive procedure that does the work, once the
12875 -- enclosing generic scope has been established.
12876
12877 ---------------
12878 -- Is_Global --
12879 ---------------
12880
12881 function Is_Global (E : Entity_Id) return Boolean is
12882 Se : Entity_Id;
12883
12884 function Is_Instance_Node (Decl : Node_Id) return Boolean;
12885 -- Determine whether the parent node of a reference to a child unit
12886 -- denotes an instantiation or a formal package, in which case the
12887 -- reference to the child unit is global, even if it appears within
12888 -- the current scope (e.g. when the instance appears within the body
12889 -- of an ancestor).
12890
12891 ----------------------
12892 -- Is_Instance_Node --
12893 ----------------------
12894
12895 function Is_Instance_Node (Decl : Node_Id) return Boolean is
12896 begin
12897 return Nkind (Decl) in N_Generic_Instantiation
12898 or else
12899 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
12900 end Is_Instance_Node;
12901
12902 -- Start of processing for Is_Global
12903
12904 begin
12905 if E = Gen_Scope then
12906 return False;
12907
12908 elsif E = Standard_Standard then
12909 return True;
12910
12911 elsif Is_Child_Unit (E)
12912 and then (Is_Instance_Node (Parent (N2))
12913 or else (Nkind (Parent (N2)) = N_Expanded_Name
12914 and then N2 = Selector_Name (Parent (N2))
12915 and then
12916 Is_Instance_Node (Parent (Parent (N2)))))
12917 then
12918 return True;
12919
12920 else
12921 Se := Scope (E);
12922 while Se /= Gen_Scope loop
12923 if Se = Standard_Standard then
12924 return True;
12925 else
12926 Se := Scope (Se);
12927 end if;
12928 end loop;
12929
12930 return False;
12931 end if;
12932 end Is_Global;
12933
12934 ------------------
12935 -- Reset_Entity --
12936 ------------------
12937
12938 procedure Reset_Entity (N : Node_Id) is
12939
12940 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
12941 -- If the type of N2 is global to the generic unit, save the type in
12942 -- the generic node. Just as we perform name capture for explicit
12943 -- references within the generic, we must capture the global types
12944 -- of local entities because they may participate in resolution in
12945 -- the instance.
12946
12947 function Top_Ancestor (E : Entity_Id) return Entity_Id;
12948 -- Find the ultimate ancestor of the current unit. If it is not a
12949 -- generic unit, then the name of the current unit in the prefix of
12950 -- an expanded name must be replaced with its generic homonym to
12951 -- ensure that it will be properly resolved in an instance.
12952
12953 ---------------------
12954 -- Set_Global_Type --
12955 ---------------------
12956
12957 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
12958 Typ : constant Entity_Id := Etype (N2);
12959
12960 begin
12961 Set_Etype (N, Typ);
12962
12963 if Entity (N) /= N2
12964 and then Has_Private_View (Entity (N))
12965 then
12966 -- If the entity of N is not the associated node, this is a
12967 -- nested generic and it has an associated node as well, whose
12968 -- type is already the full view (see below). Indicate that the
12969 -- original node has a private view.
12970
12971 Set_Has_Private_View (N);
12972 end if;
12973
12974 -- If not a private type, nothing else to do
12975
12976 if not Is_Private_Type (Typ) then
12977 if Is_Array_Type (Typ)
12978 and then Is_Private_Type (Component_Type (Typ))
12979 then
12980 Set_Has_Private_View (N);
12981 end if;
12982
12983 -- If it is a derivation of a private type in a context where no
12984 -- full view is needed, nothing to do either.
12985
12986 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
12987 null;
12988
12989 -- Otherwise mark the type for flipping and use the full view when
12990 -- available.
12991
12992 else
12993 Set_Has_Private_View (N);
12994
12995 if Present (Full_View (Typ)) then
12996 Set_Etype (N2, Full_View (Typ));
12997 end if;
12998 end if;
12999 end Set_Global_Type;
13000
13001 ------------------
13002 -- Top_Ancestor --
13003 ------------------
13004
13005 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13006 Par : Entity_Id;
13007
13008 begin
13009 Par := E;
13010 while Is_Child_Unit (Par) loop
13011 Par := Scope (Par);
13012 end loop;
13013
13014 return Par;
13015 end Top_Ancestor;
13016
13017 -- Start of processing for Reset_Entity
13018
13019 begin
13020 N2 := Get_Associated_Node (N);
13021 E := Entity (N2);
13022
13023 if Present (E) then
13024
13025 -- If the node is an entry call to an entry in an enclosing task,
13026 -- it is rewritten as a selected component. No global entity to
13027 -- preserve in this case, since the expansion will be redone in
13028 -- the instance.
13029
13030 if not Nkind_In (E, N_Defining_Identifier,
13031 N_Defining_Character_Literal,
13032 N_Defining_Operator_Symbol)
13033 then
13034 Set_Associated_Node (N, Empty);
13035 Set_Etype (N, Empty);
13036 return;
13037 end if;
13038
13039 -- If the entity is an itype created as a subtype of an access
13040 -- type with a null exclusion restore source entity for proper
13041 -- visibility. The itype will be created anew in the instance.
13042
13043 if Is_Itype (E)
13044 and then Ekind (E) = E_Access_Subtype
13045 and then Is_Entity_Name (N)
13046 and then Chars (Etype (E)) = Chars (N)
13047 then
13048 E := Etype (E);
13049 Set_Entity (N2, E);
13050 Set_Etype (N2, E);
13051 end if;
13052
13053 if Is_Global (E) then
13054
13055 -- If the entity is a package renaming that is the prefix of
13056 -- an expanded name, it has been rewritten as the renamed
13057 -- package, which is necessary semantically but complicates
13058 -- ASIS tree traversal, so we recover the original entity to
13059 -- expose the renaming. Take into account that the context may
13060 -- be a nested generic and that the original node may itself
13061 -- have an associated node.
13062
13063 if Ekind (E) = E_Package
13064 and then Nkind (Parent (N)) = N_Expanded_Name
13065 and then Present (Original_Node (N2))
13066 and then Present (Entity (Original_Node (N2)))
13067 and then Is_Entity_Name (Entity (Original_Node (N2)))
13068 then
13069 if Is_Global (Entity (Original_Node (N2))) then
13070 N2 := Original_Node (N2);
13071 Set_Associated_Node (N, N2);
13072 Set_Global_Type (N, N2);
13073
13074 else
13075 -- Renaming is local, and will be resolved in instance
13076
13077 Set_Associated_Node (N, Empty);
13078 Set_Etype (N, Empty);
13079 end if;
13080
13081 else
13082 Set_Global_Type (N, N2);
13083 end if;
13084
13085 elsif Nkind (N) = N_Op_Concat
13086 and then Is_Generic_Type (Etype (N2))
13087 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13088 or else
13089 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13090 and then Is_Intrinsic_Subprogram (E)
13091 then
13092 null;
13093
13094 else
13095 -- Entity is local. Mark generic node as unresolved.
13096 -- Note that now it does not have an entity.
13097
13098 Set_Associated_Node (N, Empty);
13099 Set_Etype (N, Empty);
13100 end if;
13101
13102 if Nkind (Parent (N)) in N_Generic_Instantiation
13103 and then N = Name (Parent (N))
13104 then
13105 Save_Global_Defaults (Parent (N), Parent (N2));
13106 end if;
13107
13108 elsif Nkind (Parent (N)) = N_Selected_Component
13109 and then Nkind (Parent (N2)) = N_Expanded_Name
13110 then
13111 if Is_Global (Entity (Parent (N2))) then
13112 Change_Selected_Component_To_Expanded_Name (Parent (N));
13113 Set_Associated_Node (Parent (N), Parent (N2));
13114 Set_Global_Type (Parent (N), Parent (N2));
13115 Save_Entity_Descendants (N);
13116
13117 -- If this is a reference to the current generic entity, replace
13118 -- by the name of the generic homonym of the current package. This
13119 -- is because in an instantiation Par.P.Q will not resolve to the
13120 -- name of the instance, whose enclosing scope is not necessarily
13121 -- Par. We use the generic homonym rather that the name of the
13122 -- generic itself because it may be hidden by a local declaration.
13123
13124 elsif In_Open_Scopes (Entity (Parent (N2)))
13125 and then not
13126 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13127 then
13128 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13129 Rewrite (Parent (N),
13130 Make_Identifier (Sloc (N),
13131 Chars =>
13132 Chars (Generic_Homonym (Entity (Parent (N2))))));
13133 else
13134 Rewrite (Parent (N),
13135 Make_Identifier (Sloc (N),
13136 Chars => Chars (Selector_Name (Parent (N2)))));
13137 end if;
13138 end if;
13139
13140 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13141 and then Parent (N) = Name (Parent (Parent (N)))
13142 then
13143 Save_Global_Defaults
13144 (Parent (Parent (N)), Parent (Parent ((N2))));
13145 end if;
13146
13147 -- A selected component may denote a static constant that has been
13148 -- folded. If the static constant is global to the generic, capture
13149 -- its value. Otherwise the folding will happen in any instantiation.
13150
13151 elsif Nkind (Parent (N)) = N_Selected_Component
13152 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13153 then
13154 if Present (Entity (Original_Node (Parent (N2))))
13155 and then Is_Global (Entity (Original_Node (Parent (N2))))
13156 then
13157 Rewrite (Parent (N), New_Copy (Parent (N2)));
13158 Set_Analyzed (Parent (N), False);
13159
13160 else
13161 null;
13162 end if;
13163
13164 -- A selected component may be transformed into a parameterless
13165 -- function call. If the called entity is global, rewrite the node
13166 -- appropriately, i.e. as an extended name for the global entity.
13167
13168 elsif Nkind (Parent (N)) = N_Selected_Component
13169 and then Nkind (Parent (N2)) = N_Function_Call
13170 and then N = Selector_Name (Parent (N))
13171 then
13172 if No (Parameter_Associations (Parent (N2))) then
13173 if Is_Global (Entity (Name (Parent (N2)))) then
13174 Change_Selected_Component_To_Expanded_Name (Parent (N));
13175 Set_Associated_Node (Parent (N), Name (Parent (N2)));
13176 Set_Global_Type (Parent (N), Name (Parent (N2)));
13177 Save_Entity_Descendants (N);
13178
13179 else
13180 Set_Is_Prefixed_Call (Parent (N));
13181 Set_Associated_Node (N, Empty);
13182 Set_Etype (N, Empty);
13183 end if;
13184
13185 -- In Ada 2005, X.F may be a call to a primitive operation,
13186 -- rewritten as F (X). This rewriting will be done again in an
13187 -- instance, so keep the original node. Global entities will be
13188 -- captured as for other constructs. Indicate that this must
13189 -- resolve as a call, to prevent accidental overloading in the
13190 -- instance, if both a component and a primitive operation appear
13191 -- as candidates.
13192
13193 else
13194 Set_Is_Prefixed_Call (Parent (N));
13195 end if;
13196
13197 -- Entity is local. Reset in generic unit, so that node is resolved
13198 -- anew at the point of instantiation.
13199
13200 else
13201 Set_Associated_Node (N, Empty);
13202 Set_Etype (N, Empty);
13203 end if;
13204 end Reset_Entity;
13205
13206 -----------------------------
13207 -- Save_Entity_Descendants --
13208 -----------------------------
13209
13210 procedure Save_Entity_Descendants (N : Node_Id) is
13211 begin
13212 case Nkind (N) is
13213 when N_Binary_Op =>
13214 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
13215 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13216
13217 when N_Unary_Op =>
13218 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13219
13220 when N_Expanded_Name | N_Selected_Component =>
13221 Save_Global_Descendant (Union_Id (Prefix (N)));
13222 Save_Global_Descendant (Union_Id (Selector_Name (N)));
13223
13224 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
13225 null;
13226
13227 when others =>
13228 raise Program_Error;
13229 end case;
13230 end Save_Entity_Descendants;
13231
13232 --------------------------
13233 -- Save_Global_Defaults --
13234 --------------------------
13235
13236 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
13237 Loc : constant Source_Ptr := Sloc (N1);
13238 Assoc2 : constant List_Id := Generic_Associations (N2);
13239 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
13240 Assoc1 : List_Id;
13241 Act1 : Node_Id;
13242 Act2 : Node_Id;
13243 Def : Node_Id;
13244 Ndec : Node_Id;
13245 Subp : Entity_Id;
13246 Actual : Entity_Id;
13247
13248 begin
13249 Assoc1 := Generic_Associations (N1);
13250
13251 if Present (Assoc1) then
13252 Act1 := First (Assoc1);
13253 else
13254 Act1 := Empty;
13255 Set_Generic_Associations (N1, New_List);
13256 Assoc1 := Generic_Associations (N1);
13257 end if;
13258
13259 if Present (Assoc2) then
13260 Act2 := First (Assoc2);
13261 else
13262 return;
13263 end if;
13264
13265 while Present (Act1) and then Present (Act2) loop
13266 Next (Act1);
13267 Next (Act2);
13268 end loop;
13269
13270 -- Find the associations added for default subprograms
13271
13272 if Present (Act2) then
13273 while Nkind (Act2) /= N_Generic_Association
13274 or else No (Entity (Selector_Name (Act2)))
13275 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
13276 loop
13277 Next (Act2);
13278 end loop;
13279
13280 -- Add a similar association if the default is global. The
13281 -- renaming declaration for the actual has been analyzed, and
13282 -- its alias is the program it renames. Link the actual in the
13283 -- original generic tree with the node in the analyzed tree.
13284
13285 while Present (Act2) loop
13286 Subp := Entity (Selector_Name (Act2));
13287 Def := Explicit_Generic_Actual_Parameter (Act2);
13288
13289 -- Following test is defence against rubbish errors
13290
13291 if No (Alias (Subp)) then
13292 return;
13293 end if;
13294
13295 -- Retrieve the resolved actual from the renaming declaration
13296 -- created for the instantiated formal.
13297
13298 Actual := Entity (Name (Parent (Parent (Subp))));
13299 Set_Entity (Def, Actual);
13300 Set_Etype (Def, Etype (Actual));
13301
13302 if Is_Global (Actual) then
13303 Ndec :=
13304 Make_Generic_Association (Loc,
13305 Selector_Name => New_Occurrence_Of (Subp, Loc),
13306 Explicit_Generic_Actual_Parameter =>
13307 New_Occurrence_Of (Actual, Loc));
13308
13309 Set_Associated_Node
13310 (Explicit_Generic_Actual_Parameter (Ndec), Def);
13311
13312 Append (Ndec, Assoc1);
13313
13314 -- If there are other defaults, add a dummy association in case
13315 -- there are other defaulted formals with the same name.
13316
13317 elsif Present (Next (Act2)) then
13318 Ndec :=
13319 Make_Generic_Association (Loc,
13320 Selector_Name => New_Occurrence_Of (Subp, Loc),
13321 Explicit_Generic_Actual_Parameter => Empty);
13322
13323 Append (Ndec, Assoc1);
13324 end if;
13325
13326 Next (Act2);
13327 end loop;
13328 end if;
13329
13330 if Nkind (Name (N1)) = N_Identifier
13331 and then Is_Child_Unit (Gen_Id)
13332 and then Is_Global (Gen_Id)
13333 and then Is_Generic_Unit (Scope (Gen_Id))
13334 and then In_Open_Scopes (Scope (Gen_Id))
13335 then
13336 -- This is an instantiation of a child unit within a sibling, so
13337 -- that the generic parent is in scope. An eventual instance must
13338 -- occur within the scope of an instance of the parent. Make name
13339 -- in instance into an expanded name, to preserve the identifier
13340 -- of the parent, so it can be resolved subsequently.
13341
13342 Rewrite (Name (N2),
13343 Make_Expanded_Name (Loc,
13344 Chars => Chars (Gen_Id),
13345 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13346 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13347 Set_Entity (Name (N2), Gen_Id);
13348
13349 Rewrite (Name (N1),
13350 Make_Expanded_Name (Loc,
13351 Chars => Chars (Gen_Id),
13352 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13353 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13354
13355 Set_Associated_Node (Name (N1), Name (N2));
13356 Set_Associated_Node (Prefix (Name (N1)), Empty);
13357 Set_Associated_Node
13358 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
13359 Set_Etype (Name (N1), Etype (Gen_Id));
13360 end if;
13361
13362 end Save_Global_Defaults;
13363
13364 ----------------------------
13365 -- Save_Global_Descendant --
13366 ----------------------------
13367
13368 procedure Save_Global_Descendant (D : Union_Id) is
13369 N1 : Node_Id;
13370
13371 begin
13372 if D in Node_Range then
13373 if D = Union_Id (Empty) then
13374 null;
13375
13376 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
13377 Save_References (Node_Id (D));
13378 end if;
13379
13380 elsif D in List_Range then
13381 if D = Union_Id (No_List)
13382 or else Is_Empty_List (List_Id (D))
13383 then
13384 null;
13385
13386 else
13387 N1 := First (List_Id (D));
13388 while Present (N1) loop
13389 Save_References (N1);
13390 Next (N1);
13391 end loop;
13392 end if;
13393
13394 -- Element list or other non-node field, nothing to do
13395
13396 else
13397 null;
13398 end if;
13399 end Save_Global_Descendant;
13400
13401 ---------------------
13402 -- Save_References --
13403 ---------------------
13404
13405 -- This is the recursive procedure that does the work once the enclosing
13406 -- generic scope has been established. We have to treat specially a
13407 -- number of node rewritings that are required by semantic processing
13408 -- and which change the kind of nodes in the generic copy: typically
13409 -- constant-folding, replacing an operator node by a string literal, or
13410 -- a selected component by an expanded name. In each of those cases, the
13411 -- transformation is propagated to the generic unit.
13412
13413 procedure Save_References (N : Node_Id) is
13414 Loc : constant Source_Ptr := Sloc (N);
13415
13416 begin
13417 if N = Empty then
13418 null;
13419
13420 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
13421 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13422 Reset_Entity (N);
13423
13424 elsif Nkind (N) = N_Operator_Symbol
13425 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
13426 then
13427 Change_Operator_Symbol_To_String_Literal (N);
13428 end if;
13429
13430 elsif Nkind (N) in N_Op then
13431 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13432 if Nkind (N) = N_Op_Concat then
13433 Set_Is_Component_Left_Opnd (N,
13434 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13435
13436 Set_Is_Component_Right_Opnd (N,
13437 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13438 end if;
13439
13440 Reset_Entity (N);
13441
13442 else
13443 -- Node may be transformed into call to a user-defined operator
13444
13445 N2 := Get_Associated_Node (N);
13446
13447 if Nkind (N2) = N_Function_Call then
13448 E := Entity (Name (N2));
13449
13450 if Present (E)
13451 and then Is_Global (E)
13452 then
13453 Set_Etype (N, Etype (N2));
13454 else
13455 Set_Associated_Node (N, Empty);
13456 Set_Etype (N, Empty);
13457 end if;
13458
13459 elsif Nkind_In (N2, N_Integer_Literal,
13460 N_Real_Literal,
13461 N_String_Literal)
13462 then
13463 if Present (Original_Node (N2))
13464 and then Nkind (Original_Node (N2)) = Nkind (N)
13465 then
13466
13467 -- Operation was constant-folded. Whenever possible,
13468 -- recover semantic information from unfolded node,
13469 -- for ASIS use.
13470
13471 Set_Associated_Node (N, Original_Node (N2));
13472
13473 if Nkind (N) = N_Op_Concat then
13474 Set_Is_Component_Left_Opnd (N,
13475 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13476 Set_Is_Component_Right_Opnd (N,
13477 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13478 end if;
13479
13480 Reset_Entity (N);
13481
13482 else
13483 -- If original node is already modified, propagate
13484 -- constant-folding to template.
13485
13486 Rewrite (N, New_Copy (N2));
13487 Set_Analyzed (N, False);
13488 end if;
13489
13490 elsif Nkind (N2) = N_Identifier
13491 and then Ekind (Entity (N2)) = E_Enumeration_Literal
13492 then
13493 -- Same if call was folded into a literal, but in this case
13494 -- retain the entity to avoid spurious ambiguities if it is
13495 -- overloaded at the point of instantiation or inlining.
13496
13497 Rewrite (N, New_Copy (N2));
13498 Set_Analyzed (N, False);
13499 end if;
13500 end if;
13501
13502 -- Complete operands check if node has not been constant-folded
13503
13504 if Nkind (N) in N_Op then
13505 Save_Entity_Descendants (N);
13506 end if;
13507
13508 elsif Nkind (N) = N_Identifier then
13509 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13510
13511 -- If this is a discriminant reference, always save it. It is
13512 -- used in the instance to find the corresponding discriminant
13513 -- positionally rather than by name.
13514
13515 Set_Original_Discriminant
13516 (N, Original_Discriminant (Get_Associated_Node (N)));
13517 Reset_Entity (N);
13518
13519 else
13520 N2 := Get_Associated_Node (N);
13521
13522 if Nkind (N2) = N_Function_Call then
13523 E := Entity (Name (N2));
13524
13525 -- Name resolves to a call to parameterless function. If
13526 -- original entity is global, mark node as resolved.
13527
13528 if Present (E)
13529 and then Is_Global (E)
13530 then
13531 Set_Etype (N, Etype (N2));
13532 else
13533 Set_Associated_Node (N, Empty);
13534 Set_Etype (N, Empty);
13535 end if;
13536
13537 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
13538 and then Is_Entity_Name (Original_Node (N2))
13539 then
13540 -- Name resolves to named number that is constant-folded,
13541 -- We must preserve the original name for ASIS use, and
13542 -- undo the constant-folding, which will be repeated in
13543 -- each instance.
13544
13545 Set_Associated_Node (N, Original_Node (N2));
13546 Reset_Entity (N);
13547
13548 elsif Nkind (N2) = N_String_Literal then
13549
13550 -- Name resolves to string literal. Perform the same
13551 -- replacement in generic.
13552
13553 Rewrite (N, New_Copy (N2));
13554
13555 elsif Nkind (N2) = N_Explicit_Dereference then
13556
13557 -- An identifier is rewritten as a dereference if it is the
13558 -- prefix in an implicit dereference (call or attribute).
13559 -- The analysis of an instantiation will expand the node
13560 -- again, so we preserve the original tree but link it to
13561 -- the resolved entity in case it is global.
13562
13563 if Is_Entity_Name (Prefix (N2))
13564 and then Present (Entity (Prefix (N2)))
13565 and then Is_Global (Entity (Prefix (N2)))
13566 then
13567 Set_Associated_Node (N, Prefix (N2));
13568
13569 elsif Nkind (Prefix (N2)) = N_Function_Call
13570 and then Is_Global (Entity (Name (Prefix (N2))))
13571 then
13572 Rewrite (N,
13573 Make_Explicit_Dereference (Loc,
13574 Prefix => Make_Function_Call (Loc,
13575 Name =>
13576 New_Occurrence_Of (Entity (Name (Prefix (N2))),
13577 Loc))));
13578
13579 else
13580 Set_Associated_Node (N, Empty);
13581 Set_Etype (N, Empty);
13582 end if;
13583
13584 -- The subtype mark of a nominally unconstrained object is
13585 -- rewritten as a subtype indication using the bounds of the
13586 -- expression. Recover the original subtype mark.
13587
13588 elsif Nkind (N2) = N_Subtype_Indication
13589 and then Is_Entity_Name (Original_Node (N2))
13590 then
13591 Set_Associated_Node (N, Original_Node (N2));
13592 Reset_Entity (N);
13593
13594 else
13595 null;
13596 end if;
13597 end if;
13598
13599 elsif Nkind (N) in N_Entity then
13600 null;
13601
13602 else
13603 declare
13604 Qual : Node_Id := Empty;
13605 Typ : Entity_Id := Empty;
13606 Nam : Node_Id;
13607
13608 use Atree.Unchecked_Access;
13609 -- This code section is part of implementing an untyped tree
13610 -- traversal, so it needs direct access to node fields.
13611
13612 begin
13613 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
13614 N2 := Get_Associated_Node (N);
13615
13616 if No (N2) then
13617 Typ := Empty;
13618 else
13619 Typ := Etype (N2);
13620
13621 -- In an instance within a generic, use the name of the
13622 -- actual and not the original generic parameter. If the
13623 -- actual is global in the current generic it must be
13624 -- preserved for its instantiation.
13625
13626 if Nkind (Parent (Typ)) = N_Subtype_Declaration
13627 and then
13628 Present (Generic_Parent_Type (Parent (Typ)))
13629 then
13630 Typ := Base_Type (Typ);
13631 Set_Etype (N2, Typ);
13632 end if;
13633 end if;
13634
13635 if No (N2)
13636 or else No (Typ)
13637 or else not Is_Global (Typ)
13638 then
13639 Set_Associated_Node (N, Empty);
13640
13641 -- If the aggregate is an actual in a call, it has been
13642 -- resolved in the current context, to some local type.
13643 -- The enclosing call may have been disambiguated by the
13644 -- aggregate, and this disambiguation might fail at
13645 -- instantiation time because the type to which the
13646 -- aggregate did resolve is not preserved. In order to
13647 -- preserve some of this information, we wrap the
13648 -- aggregate in a qualified expression, using the id of
13649 -- its type. For further disambiguation we qualify the
13650 -- type name with its scope (if visible) because both
13651 -- id's will have corresponding entities in an instance.
13652 -- This resolves most of the problems with missing type
13653 -- information on aggregates in instances.
13654
13655 if Nkind (N2) = Nkind (N)
13656 and then Nkind (Parent (N2)) in N_Subprogram_Call
13657 and then Comes_From_Source (Typ)
13658 then
13659 if Is_Immediately_Visible (Scope (Typ)) then
13660 Nam := Make_Selected_Component (Loc,
13661 Prefix =>
13662 Make_Identifier (Loc, Chars (Scope (Typ))),
13663 Selector_Name =>
13664 Make_Identifier (Loc, Chars (Typ)));
13665 else
13666 Nam := Make_Identifier (Loc, Chars (Typ));
13667 end if;
13668
13669 Qual :=
13670 Make_Qualified_Expression (Loc,
13671 Subtype_Mark => Nam,
13672 Expression => Relocate_Node (N));
13673 end if;
13674 end if;
13675
13676 Save_Global_Descendant (Field1 (N));
13677 Save_Global_Descendant (Field2 (N));
13678 Save_Global_Descendant (Field3 (N));
13679 Save_Global_Descendant (Field5 (N));
13680
13681 if Present (Qual) then
13682 Rewrite (N, Qual);
13683 end if;
13684
13685 -- All other cases than aggregates
13686
13687 else
13688 Save_Global_Descendant (Field1 (N));
13689 Save_Global_Descendant (Field2 (N));
13690 Save_Global_Descendant (Field3 (N));
13691 Save_Global_Descendant (Field4 (N));
13692 Save_Global_Descendant (Field5 (N));
13693 end if;
13694 end;
13695 end if;
13696
13697 -- If a node has aspects, references within their expressions must
13698 -- be saved separately, given that they are not directly in the
13699 -- tree.
13700
13701 if Has_Aspects (N) then
13702 declare
13703 Aspect : Node_Id;
13704 begin
13705 Aspect := First (Aspect_Specifications (N));
13706 while Present (Aspect) loop
13707 Save_Global_References (Expression (Aspect));
13708 Next (Aspect);
13709 end loop;
13710 end;
13711 end if;
13712 end Save_References;
13713
13714 -- Start of processing for Save_Global_References
13715
13716 begin
13717 Gen_Scope := Current_Scope;
13718
13719 -- If the generic unit is a child unit, references to entities in the
13720 -- parent are treated as local, because they will be resolved anew in
13721 -- the context of the instance of the parent.
13722
13723 while Is_Child_Unit (Gen_Scope)
13724 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
13725 loop
13726 Gen_Scope := Scope (Gen_Scope);
13727 end loop;
13728
13729 Save_References (N);
13730 end Save_Global_References;
13731
13732 --------------------------------------
13733 -- Set_Copied_Sloc_For_Inlined_Body --
13734 --------------------------------------
13735
13736 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
13737 begin
13738 Create_Instantiation_Source (N, E, True, S_Adjustment);
13739 end Set_Copied_Sloc_For_Inlined_Body;
13740
13741 ---------------------
13742 -- Set_Instance_Of --
13743 ---------------------
13744
13745 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
13746 begin
13747 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
13748 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
13749 Generic_Renamings.Increment_Last;
13750 end Set_Instance_Of;
13751
13752 --------------------
13753 -- Set_Next_Assoc --
13754 --------------------
13755
13756 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
13757 begin
13758 Generic_Renamings.Table (E).Next_In_HTable := Next;
13759 end Set_Next_Assoc;
13760
13761 -------------------
13762 -- Start_Generic --
13763 -------------------
13764
13765 procedure Start_Generic is
13766 begin
13767 -- ??? More things could be factored out in this routine.
13768 -- Should probably be done at a later stage.
13769
13770 Generic_Flags.Append (Inside_A_Generic);
13771 Inside_A_Generic := True;
13772
13773 Expander_Mode_Save_And_Set (False);
13774 end Start_Generic;
13775
13776 ----------------------
13777 -- Set_Instance_Env --
13778 ----------------------
13779
13780 procedure Set_Instance_Env
13781 (Gen_Unit : Entity_Id;
13782 Act_Unit : Entity_Id)
13783 is
13784 begin
13785 -- Regardless of the current mode, predefined units are analyzed in the
13786 -- most current Ada mode, and earlier version Ada checks do not apply
13787 -- to predefined units. Nothing needs to be done for non-internal units.
13788 -- These are always analyzed in the current mode.
13789
13790 if Is_Internal_File_Name
13791 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
13792 Renamings_Included => True)
13793 then
13794 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
13795 end if;
13796
13797 Current_Instantiated_Parent :=
13798 (Gen_Id => Gen_Unit,
13799 Act_Id => Act_Unit,
13800 Next_In_HTable => Assoc_Null);
13801 end Set_Instance_Env;
13802
13803 -----------------
13804 -- Switch_View --
13805 -----------------
13806
13807 procedure Switch_View (T : Entity_Id) is
13808 BT : constant Entity_Id := Base_Type (T);
13809 Priv_Elmt : Elmt_Id := No_Elmt;
13810 Priv_Sub : Entity_Id;
13811
13812 begin
13813 -- T may be private but its base type may have been exchanged through
13814 -- some other occurrence, in which case there is nothing to switch
13815 -- besides T itself. Note that a private dependent subtype of a private
13816 -- type might not have been switched even if the base type has been,
13817 -- because of the last branch of Check_Private_View (see comment there).
13818
13819 if not Is_Private_Type (BT) then
13820 Prepend_Elmt (Full_View (T), Exchanged_Views);
13821 Exchange_Declarations (T);
13822 return;
13823 end if;
13824
13825 Priv_Elmt := First_Elmt (Private_Dependents (BT));
13826
13827 if Present (Full_View (BT)) then
13828 Prepend_Elmt (Full_View (BT), Exchanged_Views);
13829 Exchange_Declarations (BT);
13830 end if;
13831
13832 while Present (Priv_Elmt) loop
13833 Priv_Sub := (Node (Priv_Elmt));
13834
13835 -- We avoid flipping the subtype if the Etype of its full view is
13836 -- private because this would result in a malformed subtype. This
13837 -- occurs when the Etype of the subtype full view is the full view of
13838 -- the base type (and since the base types were just switched, the
13839 -- subtype is pointing to the wrong view). This is currently the case
13840 -- for tagged record types, access types (maybe more?) and needs to
13841 -- be resolved. ???
13842
13843 if Present (Full_View (Priv_Sub))
13844 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
13845 then
13846 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
13847 Exchange_Declarations (Priv_Sub);
13848 end if;
13849
13850 Next_Elmt (Priv_Elmt);
13851 end loop;
13852 end Switch_View;
13853
13854 -----------------
13855 -- True_Parent --
13856 -----------------
13857
13858 function True_Parent (N : Node_Id) return Node_Id is
13859 begin
13860 if Nkind (Parent (N)) = N_Subunit then
13861 return Parent (Corresponding_Stub (Parent (N)));
13862 else
13863 return Parent (N);
13864 end if;
13865 end True_Parent;
13866
13867 -----------------------------
13868 -- Valid_Default_Attribute --
13869 -----------------------------
13870
13871 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
13872 Attr_Id : constant Attribute_Id :=
13873 Get_Attribute_Id (Attribute_Name (Def));
13874 T : constant Entity_Id := Entity (Prefix (Def));
13875 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
13876 F : Entity_Id;
13877 Num_F : Int;
13878 OK : Boolean;
13879
13880 begin
13881 if No (T)
13882 or else T = Any_Id
13883 then
13884 return;
13885 end if;
13886
13887 Num_F := 0;
13888 F := First_Formal (Nam);
13889 while Present (F) loop
13890 Num_F := Num_F + 1;
13891 Next_Formal (F);
13892 end loop;
13893
13894 case Attr_Id is
13895 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
13896 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
13897 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
13898 Attribute_Unbiased_Rounding =>
13899 OK := Is_Fun
13900 and then Num_F = 1
13901 and then Is_Floating_Point_Type (T);
13902
13903 when Attribute_Image | Attribute_Pred | Attribute_Succ |
13904 Attribute_Value | Attribute_Wide_Image |
13905 Attribute_Wide_Value =>
13906 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
13907
13908 when Attribute_Max | Attribute_Min =>
13909 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
13910
13911 when Attribute_Input =>
13912 OK := (Is_Fun and then Num_F = 1);
13913
13914 when Attribute_Output | Attribute_Read | Attribute_Write =>
13915 OK := (not Is_Fun and then Num_F = 2);
13916
13917 when others =>
13918 OK := False;
13919 end case;
13920
13921 if not OK then
13922 Error_Msg_N ("attribute reference has wrong profile for subprogram",
13923 Def);
13924 end if;
13925 end Valid_Default_Attribute;
13926
13927 end Sem_Ch12;