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