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