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