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