[multiple changes]
[gcc.git] / gcc / ada / sem_ch12.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Disp; use Exp_Disp;
33 with Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Itypes; use Itypes;
37 with Lib; use Lib;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Nlists; use Nlists;
41 with Namet; use Namet;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Rident; use Rident;
45 with Restrict; use Restrict;
46 with Rtsfind; use Rtsfind;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch7; use Sem_Ch7;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch10; use Sem_Ch10;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Elab; use Sem_Elab;
59 with Sem_Elim; use Sem_Elim;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Prag; use Sem_Prag;
62 with Sem_Res; use Sem_Res;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Stand; use Stand;
67 with Sinfo; use Sinfo;
68 with Sinfo.CN; use Sinfo.CN;
69 with Sinput; use Sinput;
70 with Sinput.L; use Sinput.L;
71 with Snames; use Snames;
72 with Stringt; use Stringt;
73 with Uname; use Uname;
74 with Table;
75 with Tbuild; use Tbuild;
76 with Uintp; use Uintp;
77 with Urealp; use Urealp;
78 with Warnsw; use Warnsw;
79
80 with GNAT.HTable;
81
82 package body Sem_Ch12 is
83
84 ----------------------------------------------------------
85 -- Implementation of Generic Analysis and Instantiation --
86 ----------------------------------------------------------
87
88 -- GNAT implements generics by macro expansion. No attempt is made to share
89 -- generic instantiations (for now). Analysis of a generic definition does
90 -- not perform any expansion action, but the expander must be called on the
91 -- tree for each instantiation, because the expansion may of course depend
92 -- on the generic actuals. All of this is best achieved as follows:
93 --
94 -- a) Semantic analysis of a generic unit is performed on a copy of the
95 -- tree for the generic unit. All tree modifications that follow analysis
96 -- do not affect the original tree. Links are kept between the original
97 -- tree and the copy, in order to recognize non-local references within
98 -- the generic, and propagate them to each instance (recall that name
99 -- resolution is done on the generic declaration: generics are not really
100 -- macros). This is summarized in the following diagram:
101
102 -- .-----------. .----------.
103 -- | semantic |<--------------| generic |
104 -- | copy | | unit |
105 -- | |==============>| |
106 -- |___________| global |__________|
107 -- references | | |
108 -- | | |
109 -- .-----|--|.
110 -- | .-----|---.
111 -- | | .----------.
112 -- | | | generic |
113 -- |__| | |
114 -- |__| instance |
115 -- |__________|
116
117 -- b) Each instantiation copies the original tree, and inserts into it a
118 -- series of declarations that describe the mapping between generic formals
119 -- and actuals. For example, a generic In OUT parameter is an object
120 -- renaming of the corresponding actual, etc. Generic IN parameters are
121 -- constant declarations.
122
123 -- c) In order to give the right visibility for these renamings, we use
124 -- a different scheme for package and subprogram instantiations. For
125 -- packages, the list of renamings is inserted into the package
126 -- specification, before the visible declarations of the package. The
127 -- renamings are analyzed before any of the text of the instance, and are
128 -- thus visible at the right place. Furthermore, outside of the instance,
129 -- the generic parameters are visible and denote their corresponding
130 -- actuals.
131
132 -- For subprograms, we create a container package to hold the renamings
133 -- and the subprogram instance itself. Analysis of the package makes the
134 -- renaming declarations visible to the subprogram. After analyzing the
135 -- package, the defining entity for the subprogram is touched-up so that
136 -- it appears declared in the current scope, and not inside the container
137 -- package.
138
139 -- If the instantiation is a compilation unit, the container package is
140 -- given the same name as the subprogram instance. This ensures that
141 -- the elaboration procedure called by the binder, using the compilation
142 -- unit name, calls in fact the elaboration procedure for the package.
143
144 -- Not surprisingly, private types complicate this approach. By saving in
145 -- the original generic object the non-local references, we guarantee that
146 -- the proper entities are referenced at the point of instantiation.
147 -- However, for private types, this by itself does not insure that the
148 -- proper VIEW of the entity is used (the full type may be visible at the
149 -- point of generic definition, but not at instantiation, or vice-versa).
150 -- In order to reference the proper view, we special-case any reference
151 -- to private types in the generic object, by saving both views, one in
152 -- the generic and one in the semantic copy. At time of instantiation, we
153 -- check whether the two views are consistent, and exchange declarations if
154 -- necessary, in order to restore the correct visibility. Similarly, if
155 -- the instance view is private when the generic view was not, we perform
156 -- the exchange. After completing the instantiation, we restore the
157 -- current visibility. The flag Has_Private_View marks identifiers in the
158 -- the generic unit that require checking.
159
160 -- Visibility within nested generic units requires special handling.
161 -- Consider the following scheme:
162
163 -- type Global is ... -- outside of generic unit.
164 -- generic ...
165 -- package Outer is
166 -- ...
167 -- type Semi_Global is ... -- global to inner.
168
169 -- generic ... -- 1
170 -- procedure inner (X1 : Global; X2 : Semi_Global);
171
172 -- procedure in2 is new inner (...); -- 4
173 -- end Outer;
174
175 -- package New_Outer is new Outer (...); -- 2
176 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
177
178 -- The semantic analysis of Outer captures all occurrences of Global.
179 -- The semantic analysis of Inner (at 1) captures both occurrences of
180 -- Global and Semi_Global.
181
182 -- At point 2 (instantiation of Outer), we also produce a generic copy
183 -- of Inner, even though Inner is, at that point, not being instantiated.
184 -- (This is just part of the semantic analysis of New_Outer).
185
186 -- Critically, references to Global within Inner must be preserved, while
187 -- references to Semi_Global should not preserved, because they must now
188 -- resolve to an entity within New_Outer. To distinguish between these, we
189 -- use a global variable, Current_Instantiated_Parent, which is set when
190 -- performing a generic copy during instantiation (at 2). This variable is
191 -- used when performing a generic copy that is not an instantiation, but
192 -- that is nested within one, as the occurrence of 1 within 2. The analysis
193 -- of a nested generic only preserves references that are global to the
194 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
195 -- determine whether a reference is external to the given parent.
196
197 -- The instantiation at point 3 requires no special treatment. The method
198 -- works as well for further nestings of generic units, but of course the
199 -- variable Current_Instantiated_Parent must be stacked because nested
200 -- instantiations can occur, e.g. the occurrence of 4 within 2.
201
202 -- The instantiation of package and subprogram bodies is handled in a
203 -- similar manner, except that it is delayed until after semantic
204 -- analysis is complete. In this fashion complex cross-dependencies
205 -- between several package declarations and bodies containing generics
206 -- can be compiled which otherwise would diagnose spurious circularities.
207
208 -- For example, it is possible to compile two packages A and B that
209 -- have the following structure:
210
211 -- package A is package B is
212 -- generic ... generic ...
213 -- package G_A is package G_B is
214
215 -- with B; with A;
216 -- package body A is package body B is
217 -- package N_B is new G_B (..) package N_A is new G_A (..)
218
219 -- The table Pending_Instantiations in package Inline is used to keep
220 -- track of body instantiations that are delayed in this manner. Inline
221 -- handles the actual calls to do the body instantiations. This activity
222 -- is part of Inline, since the processing occurs at the same point, and
223 -- for essentially the same reason, as the handling of inlined routines.
224
225 ----------------------------------------------
226 -- Detection of Instantiation Circularities --
227 ----------------------------------------------
228
229 -- If we have a chain of instantiations that is circular, this is static
230 -- error which must be detected at compile time. The detection of these
231 -- circularities is carried out at the point that we insert a generic
232 -- instance spec or body. If there is a circularity, then the analysis of
233 -- the offending spec or body will eventually result in trying to load the
234 -- same unit again, and we detect this problem as we analyze the package
235 -- instantiation for the second time.
236
237 -- At least in some cases after we have detected the circularity, we get
238 -- into trouble if we try to keep going. The following flag is set if a
239 -- circularity is detected, and used to abandon compilation after the
240 -- messages have been posted.
241
242 Circularity_Detected : Boolean := False;
243 -- This should really be reset on encountering a new main unit, but in
244 -- practice we are not using multiple main units so it is not critical.
245
246 --------------------------------------------------
247 -- Formal packages and partial parameterization --
248 --------------------------------------------------
249
250 -- When compiling a generic, a formal package is a local instantiation. If
251 -- declared with a box, its generic formals are visible in the enclosing
252 -- generic. If declared with a partial list of actuals, those actuals that
253 -- are defaulted (covered by an Others clause, or given an explicit box
254 -- initialization) are also visible in the enclosing generic, while those
255 -- that have a corresponding actual are not.
256
257 -- In our source model of instantiation, the same visibility must be
258 -- present in the spec and body of an instance: the names of the formals
259 -- that are defaulted must be made visible within the instance, and made
260 -- invisible (hidden) after the instantiation is complete, so that they
261 -- are not accessible outside of the instance.
262
263 -- In a generic, a formal package is treated like a special instantiation.
264 -- Our Ada 95 compiler handled formals with and without box in different
265 -- ways. With partial parameterization, we use a single model for both.
266 -- We create a package declaration that consists of the specification of
267 -- the generic package, and a set of declarations that map the actuals
268 -- into local renamings, just as we do for bona fide instantiations. For
269 -- defaulted parameters and formals with a box, we copy directly the
270 -- declarations of the formal into this local package. The result is a
271 -- a package whose visible declarations may include generic formals. This
272 -- package is only used for type checking and visibility analysis, and
273 -- never reaches the back-end, so it can freely violate the placement
274 -- rules for generic formal declarations.
275
276 -- The list of declarations (renamings and copies of formals) is built
277 -- by Analyze_Associations, just as for regular instantiations.
278
279 -- At the point of instantiation, conformance checking must be applied only
280 -- to those parameters that were specified in the formal. We perform this
281 -- checking by creating another internal instantiation, this one including
282 -- only the renamings and the formals (the rest of the package spec is not
283 -- relevant to conformance checking). We can then traverse two lists: the
284 -- list of actuals in the instance that corresponds to the formal package,
285 -- and the list of actuals produced for this bogus instantiation. We apply
286 -- the conformance rules to those actuals that are not defaulted (i.e.
287 -- which still appear as generic formals.
288
289 -- When we compile an instance body we must make the right parameters
290 -- visible again. The predicate Is_Generic_Formal indicates which of the
291 -- formals should have its Is_Hidden flag reset.
292
293 -----------------------
294 -- Local subprograms --
295 -----------------------
296
297 procedure Abandon_Instantiation (N : Node_Id);
298 pragma No_Return (Abandon_Instantiation);
299 -- Posts an error message "instantiation abandoned" at the indicated node
300 -- and then raises the exception Instantiation_Error to do it.
301
302 procedure Analyze_Formal_Array_Type
303 (T : in out Entity_Id;
304 Def : Node_Id);
305 -- A formal array type is treated like an array type declaration, and
306 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
307 -- in-out, because in the case of an anonymous type the entity is
308 -- actually created in the procedure.
309
310 -- The following procedures treat other kinds of formal parameters
311
312 procedure Analyze_Formal_Derived_Interface_Type
313 (N : Node_Id;
314 T : Entity_Id;
315 Def : Node_Id);
316
317 procedure Analyze_Formal_Derived_Type
318 (N : Node_Id;
319 T : Entity_Id;
320 Def : Node_Id);
321
322 procedure Analyze_Formal_Interface_Type
323 (N : Node_Id;
324 T : Entity_Id;
325 Def : Node_Id);
326
327 -- The following subprograms create abbreviated declarations for formal
328 -- scalar types. We introduce an anonymous base of the proper class for
329 -- each of them, and define the formals as constrained first subtypes of
330 -- their bases. The bounds are expressions that are non-static in the
331 -- generic.
332
333 procedure Analyze_Formal_Decimal_Fixed_Point_Type
334 (T : Entity_Id; Def : Node_Id);
335 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
336 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
337 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
339 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
340 (T : Entity_Id; Def : Node_Id);
341
342 procedure Analyze_Formal_Private_Type
343 (N : Node_Id;
344 T : Entity_Id;
345 Def : Node_Id);
346 -- Creates a new private type, which does not require completion
347
348 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
349 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
350
351 procedure Analyze_Generic_Formal_Part (N : Node_Id);
352 -- Analyze generic formal part
353
354 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
355 -- Create a new access type with the given designated type
356
357 function Analyze_Associations
358 (I_Node : Node_Id;
359 Formals : List_Id;
360 F_Copy : List_Id) return List_Id;
361 -- At instantiation time, build the list of associations between formals
362 -- and actuals. Each association becomes a renaming declaration for the
363 -- formal entity. F_Copy is the analyzed list of formals in the generic
364 -- copy. It is used to apply legality checks to the actuals. I_Node is the
365 -- instantiation node itself.
366
367 procedure Analyze_Subprogram_Instantiation
368 (N : Node_Id;
369 K : Entity_Kind);
370
371 procedure Build_Instance_Compilation_Unit_Nodes
372 (N : Node_Id;
373 Act_Body : Node_Id;
374 Act_Decl : Node_Id);
375 -- This procedure is used in the case where the generic instance of a
376 -- subprogram body or package body is a library unit. In this case, the
377 -- original library unit node for the generic instantiation must be
378 -- replaced by the resulting generic body, and a link made to a new
379 -- compilation unit node for the generic declaration. The argument N is
380 -- the original generic instantiation. Act_Body and Act_Decl are the body
381 -- and declaration of the instance (either package body and declaration
382 -- nodes or subprogram body and declaration nodes depending on the case).
383 -- On return, the node N has been rewritten with the actual body.
384
385 procedure Check_Access_Definition (N : Node_Id);
386 -- Subsidiary routine to null exclusion processing. Perform an assertion
387 -- check on Ada version and the presence of an access definition in N.
388
389 procedure Check_Formal_Packages (P_Id : Entity_Id);
390 -- Apply the following to all formal packages in generic associations
391
392 procedure Check_Formal_Package_Instance
393 (Formal_Pack : Entity_Id;
394 Actual_Pack : Entity_Id);
395 -- Verify that the actuals of the actual instance match the actuals of
396 -- the template for a formal package that is not declared with a box.
397
398 procedure Check_Forward_Instantiation (Decl : Node_Id);
399 -- If the generic is a local entity and the corresponding body has not
400 -- been seen yet, flag enclosing packages to indicate that it will be
401 -- elaborated after the generic body. Subprograms declared in the same
402 -- package cannot be inlined by the front-end because front-end inlining
403 -- requires a strict linear order of elaboration.
404
405 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
406 -- Check if some association between formals and actuals requires to make
407 -- visible primitives of a tagged type, and make those primitives visible.
408 -- Return the list of primitives whose visibility is modified (to restore
409 -- their visibility later through Restore_Hidden_Primitives). If no
410 -- candidate is found then return No_Elist.
411
412 procedure Check_Hidden_Child_Unit
413 (N : Node_Id;
414 Gen_Unit : Entity_Id;
415 Act_Decl_Id : Entity_Id);
416 -- If the generic unit is an implicit child instance within a parent
417 -- instance, we need to make an explicit test that it is not hidden by
418 -- a child instance of the same name and parent.
419
420 procedure Check_Generic_Actuals
421 (Instance : Entity_Id;
422 Is_Formal_Box : Boolean);
423 -- Similar to previous one. Check the actuals in the instantiation,
424 -- whose views can change between the point of instantiation and the point
425 -- of instantiation of the body. In addition, mark the generic renamings
426 -- as generic actuals, so that they are not compatible with other actuals.
427 -- Recurse on an actual that is a formal package whose declaration has
428 -- a box.
429
430 function Contains_Instance_Of
431 (Inner : Entity_Id;
432 Outer : Entity_Id;
433 N : Node_Id) return Boolean;
434 -- Inner is instantiated within the generic Outer. Check whether Inner
435 -- directly or indirectly contains an instance of Outer or of one of its
436 -- parents, in the case of a subunit. Each generic unit holds a list of
437 -- the entities instantiated within (at any depth). This procedure
438 -- determines whether the set of such lists contains a cycle, i.e. an
439 -- illegal circular instantiation.
440
441 function Denotes_Formal_Package
442 (Pack : Entity_Id;
443 On_Exit : Boolean := False;
444 Instance : Entity_Id := Empty) return Boolean;
445 -- Returns True if E is a formal package of an enclosing generic, or
446 -- the actual for such a formal in an enclosing instantiation. If such
447 -- a package is used as a formal in an nested generic, or as an actual
448 -- in a nested instantiation, the visibility of ITS formals should not
449 -- be modified. When called from within Restore_Private_Views, the flag
450 -- On_Exit is true, to indicate that the search for a possible enclosing
451 -- instance should ignore the current one. In that case Instance denotes
452 -- the declaration for which this is an actual. This declaration may be
453 -- an instantiation in the source, or the internal instantiation that
454 -- corresponds to the actual for a formal package.
455
456 function Earlier (N1, N2 : Node_Id) return Boolean;
457 -- Yields True if N1 and N2 appear in the same compilation unit,
458 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
459 -- traversal of the tree for the unit. Used to determine the placement
460 -- of freeze nodes for instance bodies that may depend on other instances.
461
462 function Find_Actual_Type
463 (Typ : Entity_Id;
464 Gen_Type : Entity_Id) return Entity_Id;
465 -- When validating the actual types of a child instance, check whether
466 -- the formal is a formal type of the parent unit, and retrieve the current
467 -- actual for it. Typ is the entity in the analyzed formal type declaration
468 -- (component or index type of an array type, or designated type of an
469 -- access formal) and Gen_Type is the enclosing analyzed formal array
470 -- or access type. The desired actual may be a formal of a parent, or may
471 -- be declared in a formal package of a parent. In both cases it is a
472 -- generic actual type because it appears within a visible instance.
473 -- Finally, it may be declared in a parent unit without being a formal
474 -- of that unit, in which case it must be retrieved by visibility.
475 -- Ambiguities may still arise if two homonyms are declared in two formal
476 -- packages, and the prefix of the formal type may be needed to resolve
477 -- the ambiguity in the instance ???
478
479 function In_Same_Declarative_Part
480 (F_Node : Node_Id;
481 Inst : Node_Id) return Boolean;
482 -- True if the instantiation Inst and the given freeze_node F_Node appear
483 -- within the same declarative part, ignoring subunits, but with no inter-
484 -- vening subprograms or concurrent units. Used to find the proper plave
485 -- for the freeze node of an instance, when the generic is declared in a
486 -- previous instance. If predicate is true, the freeze node of the instance
487 -- can be placed after the freeze node of the previous instance, Otherwise
488 -- it has to be placed at the end of the current declarative part.
489
490 function In_Main_Context (E : Entity_Id) return Boolean;
491 -- Check whether an instantiation is in the context of the main unit.
492 -- Used to determine whether its body should be elaborated to allow
493 -- front-end inlining.
494
495 procedure Set_Instance_Env
496 (Gen_Unit : Entity_Id;
497 Act_Unit : Entity_Id);
498 -- Save current instance on saved environment, to be used to determine
499 -- the global status of entities in nested instances. Part of Save_Env.
500 -- called after verifying that the generic unit is legal for the instance,
501 -- The procedure also examines whether the generic unit is a predefined
502 -- unit, in order to set configuration switches accordingly. As a result
503 -- the procedure must be called after analyzing and freezing the actuals.
504
505 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
506 -- Associate analyzed generic parameter with corresponding
507 -- instance. Used for semantic checks at instantiation time.
508
509 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
510 -- Traverse the Exchanged_Views list to see if a type was private
511 -- and has already been flipped during this phase of instantiation.
512
513 procedure Hide_Current_Scope;
514 -- When instantiating a generic child unit, the parent context must be
515 -- present, but the instance and all entities that may be generated
516 -- must be inserted in the current scope. We leave the current scope
517 -- on the stack, but make its entities invisible to avoid visibility
518 -- problems. This is reversed at the end of the instantiation. This is
519 -- not done for the instantiation of the bodies, which only require the
520 -- instances of the generic parents to be in scope.
521
522 procedure Install_Body
523 (Act_Body : Node_Id;
524 N : Node_Id;
525 Gen_Body : Node_Id;
526 Gen_Decl : Node_Id);
527 -- If the instantiation happens textually before the body of the generic,
528 -- the instantiation of the body must be analyzed after the generic body,
529 -- and not at the point of instantiation. Such early instantiations can
530 -- happen if the generic and the instance appear in a package declaration
531 -- because the generic body can only appear in the corresponding package
532 -- body. Early instantiations can also appear if generic, instance and
533 -- body are all in the declarative part of a subprogram or entry. Entities
534 -- of packages that are early instantiations are delayed, and their freeze
535 -- node appears after the generic body.
536
537 procedure Insert_Freeze_Node_For_Instance
538 (N : Node_Id;
539 F_Node : Node_Id);
540 -- N denotes a package or a subprogram instantiation and F_Node is the
541 -- associated freeze node. Insert the freeze node before the first source
542 -- body which follows immediately after N. If no such body is found, the
543 -- freeze node is inserted at the end of the declarative region which
544 -- contains N.
545
546 procedure Freeze_Subprogram_Body
547 (Inst_Node : Node_Id;
548 Gen_Body : Node_Id;
549 Pack_Id : Entity_Id);
550 -- The generic body may appear textually after the instance, including
551 -- in the proper body of a stub, or within a different package instance.
552 -- Given that the instance can only be elaborated after the generic, we
553 -- place freeze_nodes for the instance and/or for packages that may enclose
554 -- the instance and the generic, so that the back-end can establish the
555 -- proper order of elaboration.
556
557 procedure Init_Env;
558 -- Establish environment for subsequent instantiation. Separated from
559 -- Save_Env because data-structures for visibility handling must be
560 -- initialized before call to Check_Generic_Child_Unit.
561
562 procedure Install_Formal_Packages (Par : Entity_Id);
563 -- Install the visible part of any formal of the parent that is a formal
564 -- package. Note that for the case of a formal package with a box, this
565 -- includes the formal part of the formal package (12.7(10/2)).
566
567 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
568 -- When compiling an instance of a child unit the parent (which is
569 -- itself an instance) is an enclosing scope that must be made
570 -- immediately visible. This procedure is also used to install the non-
571 -- generic parent of a generic child unit when compiling its body, so
572 -- that full views of types in the parent are made visible.
573
574 procedure Remove_Parent (In_Body : Boolean := False);
575 -- Reverse effect after instantiation of child is complete
576
577 procedure Install_Hidden_Primitives
578 (Prims_List : in out Elist_Id;
579 Gen_T : Entity_Id;
580 Act_T : Entity_Id);
581 -- Remove suffix 'P' from hidden primitives of Act_T to match the
582 -- visibility of primitives of Gen_T. The list of primitives to which
583 -- the suffix is removed is added to Prims_List to restore them later.
584
585 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
586 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
587 -- set to No_Elist.
588
589 procedure Inline_Instance_Body
590 (N : Node_Id;
591 Gen_Unit : Entity_Id;
592 Act_Decl : Node_Id);
593 -- If front-end inlining is requested, instantiate the package body,
594 -- and preserve the visibility of its compilation unit, to insure
595 -- that successive instantiations succeed.
596
597 -- The functions Instantiate_XXX perform various legality checks and build
598 -- the declarations for instantiated generic parameters. In all of these
599 -- Formal is the entity in the generic unit, Actual is the entity of
600 -- expression in the generic associations, and Analyzed_Formal is the
601 -- formal in the generic copy, which contains the semantic information to
602 -- be used to validate the actual.
603
604 function Instantiate_Object
605 (Formal : Node_Id;
606 Actual : Node_Id;
607 Analyzed_Formal : Node_Id) return List_Id;
608
609 function Instantiate_Type
610 (Formal : Node_Id;
611 Actual : Node_Id;
612 Analyzed_Formal : Node_Id;
613 Actual_Decls : List_Id) return List_Id;
614
615 function Instantiate_Formal_Subprogram
616 (Formal : Node_Id;
617 Actual : Node_Id;
618 Analyzed_Formal : Node_Id) return Node_Id;
619
620 function Instantiate_Formal_Package
621 (Formal : Node_Id;
622 Actual : Node_Id;
623 Analyzed_Formal : Node_Id) return List_Id;
624 -- If the formal package is declared with a box, special visibility rules
625 -- apply to its formals: they are in the visible part of the package. This
626 -- is true in the declarative region of the formal package, that is to say
627 -- in the enclosing generic or instantiation. For an instantiation, the
628 -- parameters of the formal package are made visible in an explicit step.
629 -- Furthermore, if the actual has a visible USE clause, these formals must
630 -- be made potentially use-visible as well. On exit from the enclosing
631 -- instantiation, the reverse must be done.
632
633 -- For a formal package declared without a box, there are conformance rules
634 -- that apply to the actuals in the generic declaration and the actuals of
635 -- the actual package in the enclosing instantiation. The simplest way to
636 -- apply these rules is to repeat the instantiation of the formal package
637 -- in the context of the enclosing instance, and compare the generic
638 -- associations of this instantiation with those of the actual package.
639 -- This internal instantiation only needs to contain the renamings of the
640 -- formals: the visible and private declarations themselves need not be
641 -- created.
642
643 -- In Ada 2005, the formal package may be only partially parameterized.
644 -- In that case the visibility step must make visible those actuals whose
645 -- corresponding formals were given with a box. A final complication
646 -- involves inherited operations from formal derived types, which must
647 -- be visible if the type is.
648
649 function Is_In_Main_Unit (N : Node_Id) return Boolean;
650 -- Test if given node is in the main unit
651
652 procedure Load_Parent_Of_Generic
653 (N : Node_Id;
654 Spec : Node_Id;
655 Body_Optional : Boolean := False);
656 -- If the generic appears in a separate non-generic library unit, load the
657 -- corresponding body to retrieve the body of the generic. N is the node
658 -- for the generic instantiation, Spec is the generic package declaration.
659 --
660 -- Body_Optional is a flag that indicates that the body is being loaded to
661 -- ensure that temporaries are generated consistently when there are other
662 -- instances in the current declarative part that precede the one being
663 -- loaded. In that case a missing body is acceptable.
664
665 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
666 -- Add the context clause of the unit containing a generic unit to a
667 -- compilation unit that is, or contains, an instantiation.
668
669 function Get_Associated_Node (N : Node_Id) return Node_Id;
670 -- In order to propagate semantic information back from the analyzed copy
671 -- to the original generic, we maintain links between selected nodes in the
672 -- generic and their corresponding copies. At the end of generic analysis,
673 -- the routine Save_Global_References traverses the generic tree, examines
674 -- the semantic information, and preserves the links to those nodes that
675 -- contain global information. At instantiation, the information from the
676 -- associated node is placed on the new copy, so that name resolution is
677 -- not repeated.
678 --
679 -- Three kinds of source nodes have associated nodes:
680 --
681 -- a) those that can reference (denote) entities, that is identifiers,
682 -- character literals, expanded_names, operator symbols, operators,
683 -- and attribute reference nodes. These nodes have an Entity field
684 -- and are the set of nodes that are in N_Has_Entity.
685 --
686 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
687 --
688 -- c) selected components (N_Selected_Component)
689 --
690 -- For the first class, the associated node preserves the entity if it is
691 -- global. If the generic contains nested instantiations, the associated
692 -- node itself has been recopied, and a chain of them must be followed.
693 --
694 -- For aggregates, the associated node allows retrieval of the type, which
695 -- may otherwise not appear in the generic. The view of this type may be
696 -- different between generic and instantiation, and the full view can be
697 -- installed before the instantiation is analyzed. For aggregates of type
698 -- extensions, the same view exchange may have to be performed for some of
699 -- the ancestor types, if their view is private at the point of
700 -- instantiation.
701 --
702 -- Nodes that are selected components in the parse tree may be rewritten
703 -- as expanded names after resolution, and must be treated as potential
704 -- entity holders, which is why they also have an Associated_Node.
705 --
706 -- Nodes that do not come from source, such as freeze nodes, do not appear
707 -- in the generic tree, and need not have an associated node.
708 --
709 -- The associated node is stored in the Associated_Node field. Note that
710 -- this field overlaps Entity, which is fine, because the whole point is
711 -- that we don't need or want the normal Entity field in this situation.
712
713 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
714 -- Within the generic part, entities in the formal package are
715 -- visible. To validate subsequent type declarations, indicate
716 -- the correspondence between the entities in the analyzed formal,
717 -- and the entities in the actual package. There are three packages
718 -- involved in the instantiation of a formal package: the parent
719 -- generic P1 which appears in the generic declaration, the fake
720 -- instantiation P2 which appears in the analyzed generic, and whose
721 -- visible entities may be used in subsequent formals, and the actual
722 -- P3 in the instance. To validate subsequent formals, me indicate
723 -- that the entities in P2 are mapped into those of P3. The mapping of
724 -- entities has to be done recursively for nested packages.
725
726 procedure Move_Freeze_Nodes
727 (Out_Of : Entity_Id;
728 After : Node_Id;
729 L : List_Id);
730 -- Freeze nodes can be generated in the analysis of a generic unit, but
731 -- will not be seen by the back-end. It is necessary to move those nodes
732 -- to the enclosing scope if they freeze an outer entity. We place them
733 -- at the end of the enclosing generic package, which is semantically
734 -- neutral.
735
736 procedure Preanalyze_Actuals (N : Node_Id);
737 -- Analyze actuals to perform name resolution. Full resolution is done
738 -- later, when the expected types are known, but names have to be captured
739 -- before installing parents of generics, that are not visible for the
740 -- actuals themselves.
741
742 function True_Parent (N : Node_Id) return Node_Id;
743 -- For a subunit, return parent of corresponding stub, else return
744 -- parent of node.
745
746 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
747 -- Verify that an attribute that appears as the default for a formal
748 -- subprogram is a function or procedure with the correct profile.
749
750 -------------------------------------------
751 -- Data Structures for Generic Renamings --
752 -------------------------------------------
753
754 -- The map Generic_Renamings associates generic entities with their
755 -- corresponding actuals. Currently used to validate type instances. It
756 -- will eventually be used for all generic parameters to eliminate the
757 -- need for overload resolution in the instance.
758
759 type Assoc_Ptr is new Int;
760
761 Assoc_Null : constant Assoc_Ptr := -1;
762
763 type Assoc is record
764 Gen_Id : Entity_Id;
765 Act_Id : Entity_Id;
766 Next_In_HTable : Assoc_Ptr;
767 end record;
768
769 package Generic_Renamings is new Table.Table
770 (Table_Component_Type => Assoc,
771 Table_Index_Type => Assoc_Ptr,
772 Table_Low_Bound => 0,
773 Table_Initial => 10,
774 Table_Increment => 100,
775 Table_Name => "Generic_Renamings");
776
777 -- Variable to hold enclosing instantiation. When the environment is
778 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
779
780 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
781
782 -- Hash table for associations
783
784 HTable_Size : constant := 37;
785 type HTable_Range is range 0 .. HTable_Size - 1;
786
787 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
788 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
789 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
790 function Hash (F : Entity_Id) return HTable_Range;
791
792 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
793 Header_Num => HTable_Range,
794 Element => Assoc,
795 Elmt_Ptr => Assoc_Ptr,
796 Null_Ptr => Assoc_Null,
797 Set_Next => Set_Next_Assoc,
798 Next => Next_Assoc,
799 Key => Entity_Id,
800 Get_Key => Get_Gen_Id,
801 Hash => Hash,
802 Equal => "=");
803
804 Exchanged_Views : Elist_Id;
805 -- This list holds the private views that have been exchanged during
806 -- instantiation to restore the visibility of the generic declaration.
807 -- (see comments above). After instantiation, the current visibility is
808 -- reestablished by means of a traversal of this list.
809
810 Hidden_Entities : Elist_Id;
811 -- This list holds the entities of the current scope that are removed
812 -- from immediate visibility when instantiating a child unit. Their
813 -- visibility is restored in Remove_Parent.
814
815 -- Because instantiations can be recursive, the following must be saved
816 -- on entry and restored on exit from an instantiation (spec or body).
817 -- This is done by the two procedures Save_Env and Restore_Env. For
818 -- package and subprogram instantiations (but not for the body instances)
819 -- the action of Save_Env is done in two steps: Init_Env is called before
820 -- Check_Generic_Child_Unit, because setting the parent instances requires
821 -- that the visibility data structures be properly initialized. Once the
822 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
823
824 Parent_Unit_Visible : Boolean := False;
825 -- Parent_Unit_Visible is used when the generic is a child unit, and
826 -- indicates whether the ultimate parent of the generic is visible in the
827 -- instantiation environment. It is used to reset the visibility of the
828 -- parent at the end of the instantiation (see Remove_Parent).
829
830 Instance_Parent_Unit : Entity_Id := Empty;
831 -- This records the ultimate parent unit of an instance of a generic
832 -- child unit and is used in conjunction with Parent_Unit_Visible to
833 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
834
835 type Instance_Env is record
836 Instantiated_Parent : Assoc;
837 Exchanged_Views : Elist_Id;
838 Hidden_Entities : Elist_Id;
839 Current_Sem_Unit : Unit_Number_Type;
840 Parent_Unit_Visible : Boolean := False;
841 Instance_Parent_Unit : Entity_Id := Empty;
842 Switches : Config_Switches_Type;
843 end record;
844
845 package Instance_Envs is new Table.Table (
846 Table_Component_Type => Instance_Env,
847 Table_Index_Type => Int,
848 Table_Low_Bound => 0,
849 Table_Initial => 32,
850 Table_Increment => 100,
851 Table_Name => "Instance_Envs");
852
853 procedure Restore_Private_Views
854 (Pack_Id : Entity_Id;
855 Is_Package : Boolean := True);
856 -- Restore the private views of external types, and unmark the generic
857 -- renamings of actuals, so that they become compatible subtypes again.
858 -- For subprograms, Pack_Id is the package constructed to hold the
859 -- renamings.
860
861 procedure Switch_View (T : Entity_Id);
862 -- Switch the partial and full views of a type and its private
863 -- dependents (i.e. its subtypes and derived types).
864
865 ------------------------------------
866 -- Structures for Error Reporting --
867 ------------------------------------
868
869 Instantiation_Node : Node_Id;
870 -- Used by subprograms that validate instantiation of formal parameters
871 -- where there might be no actual on which to place the error message.
872 -- Also used to locate the instantiation node for generic subunits.
873
874 Instantiation_Error : exception;
875 -- When there is a semantic error in the generic parameter matching,
876 -- there is no point in continuing the instantiation, because the
877 -- number of cascaded errors is unpredictable. This exception aborts
878 -- the instantiation process altogether.
879
880 S_Adjustment : Sloc_Adjustment;
881 -- Offset created for each node in an instantiation, in order to keep
882 -- track of the source position of the instantiation in each of its nodes.
883 -- A subsequent semantic error or warning on a construct of the instance
884 -- points to both places: the original generic node, and the point of
885 -- instantiation. See Sinput and Sinput.L for additional details.
886
887 ------------------------------------------------------------
888 -- Data structure for keeping track when inside a Generic --
889 ------------------------------------------------------------
890
891 -- The following table is used to save values of the Inside_A_Generic
892 -- flag (see spec of Sem) when they are saved by Start_Generic.
893
894 package Generic_Flags is new Table.Table (
895 Table_Component_Type => Boolean,
896 Table_Index_Type => Int,
897 Table_Low_Bound => 0,
898 Table_Initial => 32,
899 Table_Increment => 200,
900 Table_Name => "Generic_Flags");
901
902 ---------------------------
903 -- Abandon_Instantiation --
904 ---------------------------
905
906 procedure Abandon_Instantiation (N : Node_Id) is
907 begin
908 Error_Msg_N ("\instantiation abandoned!", N);
909 raise Instantiation_Error;
910 end Abandon_Instantiation;
911
912 --------------------------
913 -- Analyze_Associations --
914 --------------------------
915
916 function Analyze_Associations
917 (I_Node : Node_Id;
918 Formals : List_Id;
919 F_Copy : List_Id) return List_Id
920 is
921 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
922 Assoc : constant List_Id := New_List;
923 Default_Actuals : constant Elist_Id := New_Elmt_List;
924 Gen_Unit : constant Entity_Id :=
925 Defining_Entity (Parent (F_Copy));
926
927 Actuals : List_Id;
928 Actual : Node_Id;
929 Analyzed_Formal : Node_Id;
930 First_Named : Node_Id := Empty;
931 Formal : Node_Id;
932 Match : Node_Id;
933 Named : Node_Id;
934 Saved_Formal : Node_Id;
935
936 Default_Formals : constant List_Id := New_List;
937 -- If an Others_Choice is present, some of the formals may be defaulted.
938 -- To simplify the treatment of visibility in an instance, we introduce
939 -- individual defaults for each such formal. These defaults are
940 -- appended to the list of associations and replace the Others_Choice.
941
942 Found_Assoc : Node_Id;
943 -- Association for the current formal being match. Empty if there are
944 -- no remaining actuals, or if there is no named association with the
945 -- name of the formal.
946
947 Is_Named_Assoc : Boolean;
948 Num_Matched : Int := 0;
949 Num_Actuals : Int := 0;
950
951 Others_Present : Boolean := False;
952 Others_Choice : Node_Id := Empty;
953 -- In Ada 2005, indicates partial parameterization of a formal
954 -- package. As usual an other association must be last in the list.
955
956 function Build_Wrapper
957 (Formal : Entity_Id;
958 Actual : Entity_Id := Empty) return Node_Id;
959 -- In GNATProve mode, create a wrapper function for actuals that are
960 -- operators, in order to propagate their contract to the renaming
961 -- declarations generated for them. If the actual is absent, this is
962 -- a formal with a default, and the name of the operator is that of the
963 -- formal.
964
965 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
966 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
967 -- cannot have a named association for it. AI05-0025 extends this rule
968 -- to formals of formal packages by AI05-0025, and it also applies to
969 -- box-initialized formals.
970
971 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
972 -- Determine whether the parameter types and the return type of Subp
973 -- are fully defined at the point of instantiation.
974
975 function Matching_Actual
976 (F : Entity_Id;
977 A_F : Entity_Id) return Node_Id;
978 -- Find actual that corresponds to a given a formal parameter. If the
979 -- actuals are positional, return the next one, if any. If the actuals
980 -- are named, scan the parameter associations to find the right one.
981 -- A_F is the corresponding entity in the analyzed generic,which is
982 -- placed on the selector name for ASIS use.
983 --
984 -- In Ada 2005, a named association may be given with a box, in which
985 -- case Matching_Actual sets Found_Assoc to the generic association,
986 -- but return Empty for the actual itself. In this case the code below
987 -- creates a corresponding declaration for the formal.
988
989 function Partial_Parameterization return Boolean;
990 -- Ada 2005: if no match is found for a given formal, check if the
991 -- association for it includes a box, or whether the associations
992 -- include an Others clause.
993
994 procedure Process_Default (F : Entity_Id);
995 -- Add a copy of the declaration of generic formal F to the list of
996 -- associations, and add an explicit box association for F if there
997 -- is none yet, and the default comes from an Others_Choice.
998
999 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1000 -- Determine whether Subp renames one of the subprograms defined in the
1001 -- generated package Standard.
1002
1003 procedure Set_Analyzed_Formal;
1004 -- Find the node in the generic copy that corresponds to a given formal.
1005 -- The semantic information on this node is used to perform legality
1006 -- checks on the actuals. Because semantic analysis can introduce some
1007 -- anonymous entities or modify the declaration node itself, the
1008 -- correspondence between the two lists is not one-one. In addition to
1009 -- anonymous types, the presence a formal equality will introduce an
1010 -- implicit declaration for the corresponding inequality.
1011
1012 -------------------
1013 -- Build_Wrapper --
1014 -------------------
1015
1016 function Build_Wrapper
1017 (Formal : Entity_Id;
1018 Actual : Entity_Id := Empty) return Node_Id
1019 is
1020 Loc : constant Source_Ptr := Sloc (I_Node);
1021 Typ : constant Entity_Id := Etype (Formal);
1022 Is_Binary : constant Boolean :=
1023 Present (Next_Formal (First_Formal (Formal)));
1024
1025 Decl : Node_Id;
1026 Expr : Node_Id;
1027 F1, F2 : Entity_Id;
1028 Func : Entity_Id;
1029 Op_Name : Name_Id;
1030 Spec : Node_Id;
1031
1032 L, R : Node_Id;
1033
1034 begin
1035 if No (Actual) then
1036 Op_Name := Chars (Formal);
1037 else
1038 Op_Name := Chars (Actual);
1039 end if;
1040
1041 -- Create entities for wrapper function and its formals
1042
1043 F1 := Make_Temporary (Loc, 'A');
1044 F2 := Make_Temporary (Loc, 'B');
1045 L := New_Occurrence_Of (F1, Loc);
1046 R := New_Occurrence_Of (F2, Loc);
1047
1048 Func := Make_Defining_Identifier (Loc, Chars (Formal));
1049 Set_Ekind (Func, E_Function);
1050 Set_Is_Generic_Actual_Subprogram (Func);
1051
1052 Spec :=
1053 Make_Function_Specification (Loc,
1054 Defining_Unit_Name => Func,
1055 Parameter_Specifications => New_List (
1056 Make_Parameter_Specification (Loc,
1057 Defining_Identifier => F1,
1058 Parameter_Type =>
1059 Make_Identifier (Loc,
1060 Chars => Chars (Etype (First_Formal (Formal)))))),
1061 Result_Definition => Make_Identifier (Loc, Chars (Typ)));
1062
1063 if Is_Binary then
1064 Append_To (Parameter_Specifications (Spec),
1065 Make_Parameter_Specification (Loc,
1066 Defining_Identifier => F2,
1067 Parameter_Type =>
1068 Make_Identifier (Loc,
1069 Chars (Etype (Next_Formal (First_Formal (Formal)))))));
1070 end if;
1071
1072 -- Build expression as a function call, or as an operator node
1073 -- that corresponds to the name of the actual, starting with binary
1074 -- operators.
1075
1076 if Present (Actual) and then Op_Name not in Any_Operator_Name then
1077 Expr :=
1078 Make_Function_Call (Loc,
1079 Name =>
1080 New_Occurrence_Of (Entity (Actual), Loc),
1081 Parameter_Associations => New_List (L));
1082
1083 if Is_Binary then
1084 Append_To (Parameter_Associations (Expr), R);
1085 end if;
1086
1087 -- Binary operators
1088
1089 elsif Is_Binary then
1090 if Op_Name = Name_Op_And then
1091 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
1092 elsif Op_Name = Name_Op_Or then
1093 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
1094 elsif Op_Name = Name_Op_Xor then
1095 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
1096 elsif Op_Name = Name_Op_Eq then
1097 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
1098 elsif Op_Name = Name_Op_Ne then
1099 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
1100 elsif Op_Name = Name_Op_Le then
1101 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
1102 elsif Op_Name = Name_Op_Gt then
1103 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
1104 elsif Op_Name = Name_Op_Ge then
1105 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
1106 elsif Op_Name = Name_Op_Lt then
1107 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
1108 elsif Op_Name = Name_Op_Add then
1109 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
1110 elsif Op_Name = Name_Op_Subtract then
1111 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
1112 elsif Op_Name = Name_Op_Concat then
1113 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
1114 elsif Op_Name = Name_Op_Multiply then
1115 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
1116 elsif Op_Name = Name_Op_Divide then
1117 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
1118 elsif Op_Name = Name_Op_Mod then
1119 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
1120 elsif Op_Name = Name_Op_Rem then
1121 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
1122 elsif Op_Name = Name_Op_Expon then
1123 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
1124 end if;
1125
1126 -- Unary operators
1127
1128 else
1129 if Op_Name = Name_Op_Add then
1130 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
1131 elsif Op_Name = Name_Op_Subtract then
1132 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
1133 elsif Op_Name = Name_Op_Abs then
1134 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
1135 elsif Op_Name = Name_Op_Not then
1136 Expr := Make_Op_Not (Loc, Right_Opnd => L);
1137 end if;
1138 end if;
1139
1140 -- Propagate visible entity to operator node, either from a
1141 -- given actual or from a default.
1142
1143 if Is_Entity_Name (Actual)
1144 and then Nkind (Expr) in N_Op
1145 then
1146 Set_Entity (Expr, Entity (Actual));
1147 end if;
1148
1149 Decl :=
1150 Make_Expression_Function (Loc,
1151 Specification => Spec,
1152 Expression => Expr);
1153
1154 return Decl;
1155 end Build_Wrapper;
1156
1157 ----------------------------------------
1158 -- Check_Overloaded_Formal_Subprogram --
1159 ----------------------------------------
1160
1161 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1162 Temp_Formal : Entity_Id;
1163
1164 begin
1165 Temp_Formal := First (Formals);
1166 while Present (Temp_Formal) loop
1167 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1168 and then Temp_Formal /= Formal
1169 and then
1170 Chars (Defining_Unit_Name (Specification (Formal))) =
1171 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1172 then
1173 if Present (Found_Assoc) then
1174 Error_Msg_N
1175 ("named association not allowed for overloaded formal",
1176 Found_Assoc);
1177
1178 else
1179 Error_Msg_N
1180 ("named association not allowed for overloaded formal",
1181 Others_Choice);
1182 end if;
1183
1184 Abandon_Instantiation (Instantiation_Node);
1185 end if;
1186
1187 Next (Temp_Formal);
1188 end loop;
1189 end Check_Overloaded_Formal_Subprogram;
1190
1191 -------------------------------
1192 -- Has_Fully_Defined_Profile --
1193 -------------------------------
1194
1195 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1196 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1197 -- Determine whethet type Typ is fully defined
1198
1199 ---------------------------
1200 -- Is_Fully_Defined_Type --
1201 ---------------------------
1202
1203 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1204 begin
1205 -- A private type without a full view is not fully defined
1206
1207 if Is_Private_Type (Typ)
1208 and then No (Full_View (Typ))
1209 then
1210 return False;
1211
1212 -- An incomplete type is never fully defined
1213
1214 elsif Is_Incomplete_Type (Typ) then
1215 return False;
1216
1217 -- All other types are fully defined
1218
1219 else
1220 return True;
1221 end if;
1222 end Is_Fully_Defined_Type;
1223
1224 -- Local declarations
1225
1226 Param : Entity_Id;
1227
1228 -- Start of processing for Has_Fully_Defined_Profile
1229
1230 begin
1231 -- Check the parameters
1232
1233 Param := First_Formal (Subp);
1234 while Present (Param) loop
1235 if not Is_Fully_Defined_Type (Etype (Param)) then
1236 return False;
1237 end if;
1238
1239 Next_Formal (Param);
1240 end loop;
1241
1242 -- Check the return type
1243
1244 return Is_Fully_Defined_Type (Etype (Subp));
1245 end Has_Fully_Defined_Profile;
1246
1247 ---------------------
1248 -- Matching_Actual --
1249 ---------------------
1250
1251 function Matching_Actual
1252 (F : Entity_Id;
1253 A_F : Entity_Id) return Node_Id
1254 is
1255 Prev : Node_Id;
1256 Act : Node_Id;
1257
1258 begin
1259 Is_Named_Assoc := False;
1260
1261 -- End of list of purely positional parameters
1262
1263 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1264 Found_Assoc := Empty;
1265 Act := Empty;
1266
1267 -- Case of positional parameter corresponding to current formal
1268
1269 elsif No (Selector_Name (Actual)) then
1270 Found_Assoc := Actual;
1271 Act := Explicit_Generic_Actual_Parameter (Actual);
1272 Num_Matched := Num_Matched + 1;
1273 Next (Actual);
1274
1275 -- Otherwise scan list of named actuals to find the one with the
1276 -- desired name. All remaining actuals have explicit names.
1277
1278 else
1279 Is_Named_Assoc := True;
1280 Found_Assoc := Empty;
1281 Act := Empty;
1282 Prev := Empty;
1283
1284 while Present (Actual) loop
1285 if Chars (Selector_Name (Actual)) = Chars (F) then
1286 Set_Entity (Selector_Name (Actual), A_F);
1287 Set_Etype (Selector_Name (Actual), Etype (A_F));
1288 Generate_Reference (A_F, Selector_Name (Actual));
1289 Found_Assoc := Actual;
1290 Act := Explicit_Generic_Actual_Parameter (Actual);
1291 Num_Matched := Num_Matched + 1;
1292 exit;
1293 end if;
1294
1295 Prev := Actual;
1296 Next (Actual);
1297 end loop;
1298
1299 -- Reset for subsequent searches. In most cases the named
1300 -- associations are in order. If they are not, we reorder them
1301 -- to avoid scanning twice the same actual. This is not just a
1302 -- question of efficiency: there may be multiple defaults with
1303 -- boxes that have the same name. In a nested instantiation we
1304 -- insert actuals for those defaults, and cannot rely on their
1305 -- names to disambiguate them.
1306
1307 if Actual = First_Named then
1308 Next (First_Named);
1309
1310 elsif Present (Actual) then
1311 Insert_Before (First_Named, Remove_Next (Prev));
1312 end if;
1313
1314 Actual := First_Named;
1315 end if;
1316
1317 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1318 Set_Used_As_Generic_Actual (Entity (Act));
1319 end if;
1320
1321 return Act;
1322 end Matching_Actual;
1323
1324 ------------------------------
1325 -- Partial_Parameterization --
1326 ------------------------------
1327
1328 function Partial_Parameterization return Boolean is
1329 begin
1330 return Others_Present
1331 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1332 end Partial_Parameterization;
1333
1334 ---------------------
1335 -- Process_Default --
1336 ---------------------
1337
1338 procedure Process_Default (F : Entity_Id) is
1339 Loc : constant Source_Ptr := Sloc (I_Node);
1340 F_Id : constant Entity_Id := Defining_Entity (F);
1341 Decl : Node_Id;
1342 Default : Node_Id;
1343 Id : Entity_Id;
1344
1345 begin
1346 -- Append copy of formal declaration to associations, and create new
1347 -- defining identifier for it.
1348
1349 Decl := New_Copy_Tree (F);
1350 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1351
1352 if Nkind (F) in N_Formal_Subprogram_Declaration then
1353 Set_Defining_Unit_Name (Specification (Decl), Id);
1354
1355 else
1356 Set_Defining_Identifier (Decl, Id);
1357 end if;
1358
1359 Append (Decl, Assoc);
1360
1361 if No (Found_Assoc) then
1362 Default :=
1363 Make_Generic_Association (Loc,
1364 Selector_Name => New_Occurrence_Of (Id, Loc),
1365 Explicit_Generic_Actual_Parameter => Empty);
1366 Set_Box_Present (Default);
1367 Append (Default, Default_Formals);
1368 end if;
1369 end Process_Default;
1370
1371 ---------------------------------
1372 -- Renames_Standard_Subprogram --
1373 ---------------------------------
1374
1375 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1376 Id : Entity_Id;
1377
1378 begin
1379 Id := Alias (Subp);
1380 while Present (Id) loop
1381 if Scope (Id) = Standard_Standard then
1382 return True;
1383 end if;
1384
1385 Id := Alias (Id);
1386 end loop;
1387
1388 return False;
1389 end Renames_Standard_Subprogram;
1390
1391 -------------------------
1392 -- Set_Analyzed_Formal --
1393 -------------------------
1394
1395 procedure Set_Analyzed_Formal is
1396 Kind : Node_Kind;
1397
1398 begin
1399 while Present (Analyzed_Formal) loop
1400 Kind := Nkind (Analyzed_Formal);
1401
1402 case Nkind (Formal) is
1403
1404 when N_Formal_Subprogram_Declaration =>
1405 exit when Kind in N_Formal_Subprogram_Declaration
1406 and then
1407 Chars
1408 (Defining_Unit_Name (Specification (Formal))) =
1409 Chars
1410 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1411
1412 when N_Formal_Package_Declaration =>
1413 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1414 N_Generic_Package_Declaration,
1415 N_Package_Declaration);
1416
1417 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1418
1419 when others =>
1420
1421 -- Skip freeze nodes, and nodes inserted to replace
1422 -- unrecognized pragmas.
1423
1424 exit when
1425 Kind not in N_Formal_Subprogram_Declaration
1426 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1427 N_Freeze_Entity,
1428 N_Null_Statement,
1429 N_Itype_Reference)
1430 and then Chars (Defining_Identifier (Formal)) =
1431 Chars (Defining_Identifier (Analyzed_Formal));
1432 end case;
1433
1434 Next (Analyzed_Formal);
1435 end loop;
1436 end Set_Analyzed_Formal;
1437
1438 -- Start of processing for Analyze_Associations
1439
1440 begin
1441 Actuals := Generic_Associations (I_Node);
1442
1443 if Present (Actuals) then
1444
1445 -- Check for an Others choice, indicating a partial parameterization
1446 -- for a formal package.
1447
1448 Actual := First (Actuals);
1449 while Present (Actual) loop
1450 if Nkind (Actual) = N_Others_Choice then
1451 Others_Present := True;
1452 Others_Choice := Actual;
1453
1454 if Present (Next (Actual)) then
1455 Error_Msg_N ("others must be last association", Actual);
1456 end if;
1457
1458 -- This subprogram is used both for formal packages and for
1459 -- instantiations. For the latter, associations must all be
1460 -- explicit.
1461
1462 if Nkind (I_Node) /= N_Formal_Package_Declaration
1463 and then Comes_From_Source (I_Node)
1464 then
1465 Error_Msg_N
1466 ("others association not allowed in an instance",
1467 Actual);
1468 end if;
1469
1470 -- In any case, nothing to do after the others association
1471
1472 exit;
1473
1474 elsif Box_Present (Actual)
1475 and then Comes_From_Source (I_Node)
1476 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1477 then
1478 Error_Msg_N
1479 ("box association not allowed in an instance", Actual);
1480 end if;
1481
1482 Next (Actual);
1483 end loop;
1484
1485 -- If named associations are present, save first named association
1486 -- (it may of course be Empty) to facilitate subsequent name search.
1487
1488 First_Named := First (Actuals);
1489 while Present (First_Named)
1490 and then Nkind (First_Named) /= N_Others_Choice
1491 and then No (Selector_Name (First_Named))
1492 loop
1493 Num_Actuals := Num_Actuals + 1;
1494 Next (First_Named);
1495 end loop;
1496 end if;
1497
1498 Named := First_Named;
1499 while Present (Named) loop
1500 if Nkind (Named) /= N_Others_Choice
1501 and then No (Selector_Name (Named))
1502 then
1503 Error_Msg_N ("invalid positional actual after named one", Named);
1504 Abandon_Instantiation (Named);
1505 end if;
1506
1507 -- A named association may lack an actual parameter, if it was
1508 -- introduced for a default subprogram that turns out to be local
1509 -- to the outer instantiation.
1510
1511 if Nkind (Named) /= N_Others_Choice
1512 and then Present (Explicit_Generic_Actual_Parameter (Named))
1513 then
1514 Num_Actuals := Num_Actuals + 1;
1515 end if;
1516
1517 Next (Named);
1518 end loop;
1519
1520 if Present (Formals) then
1521 Formal := First_Non_Pragma (Formals);
1522 Analyzed_Formal := First_Non_Pragma (F_Copy);
1523
1524 if Present (Actuals) then
1525 Actual := First (Actuals);
1526
1527 -- All formals should have default values
1528
1529 else
1530 Actual := Empty;
1531 end if;
1532
1533 while Present (Formal) loop
1534 Set_Analyzed_Formal;
1535 Saved_Formal := Next_Non_Pragma (Formal);
1536
1537 case Nkind (Formal) is
1538 when N_Formal_Object_Declaration =>
1539 Match :=
1540 Matching_Actual (
1541 Defining_Identifier (Formal),
1542 Defining_Identifier (Analyzed_Formal));
1543
1544 if No (Match) and then Partial_Parameterization then
1545 Process_Default (Formal);
1546 else
1547 Append_List
1548 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1549 Assoc);
1550 end if;
1551
1552 when N_Formal_Type_Declaration =>
1553 Match :=
1554 Matching_Actual (
1555 Defining_Identifier (Formal),
1556 Defining_Identifier (Analyzed_Formal));
1557
1558 if No (Match) then
1559 if Partial_Parameterization then
1560 Process_Default (Formal);
1561
1562 else
1563 Error_Msg_Sloc := Sloc (Gen_Unit);
1564 Error_Msg_NE
1565 ("missing actual&",
1566 Instantiation_Node,
1567 Defining_Identifier (Formal));
1568 Error_Msg_NE ("\in instantiation of & declared#",
1569 Instantiation_Node, Gen_Unit);
1570 Abandon_Instantiation (Instantiation_Node);
1571 end if;
1572
1573 else
1574 Analyze (Match);
1575 Append_List
1576 (Instantiate_Type
1577 (Formal, Match, Analyzed_Formal, Assoc),
1578 Assoc);
1579
1580 -- An instantiation is a freeze point for the actuals,
1581 -- unless this is a rewritten formal package, or the
1582 -- formal is an Ada 2012 formal incomplete type.
1583
1584 if Nkind (I_Node) = N_Formal_Package_Declaration
1585 or else
1586 (Ada_Version >= Ada_2012
1587 and then
1588 Ekind (Defining_Identifier (Analyzed_Formal)) =
1589 E_Incomplete_Type)
1590 then
1591 null;
1592
1593 else
1594 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1595 end if;
1596 end if;
1597
1598 -- A remote access-to-class-wide type is not a legal actual
1599 -- for a generic formal of an access type (E.2.2(17/2)).
1600 -- In GNAT an exception to this rule is introduced when
1601 -- the formal is marked as remote using implementation
1602 -- defined aspect/pragma Remote_Access_Type. In that case
1603 -- the actual must be remote as well.
1604
1605 -- If the current instantiation is the construction of a
1606 -- local copy for a formal package the actuals may be
1607 -- defaulted, and there is no matching actual to check.
1608
1609 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1610 and then
1611 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1612 N_Access_To_Object_Definition
1613 and then Present (Match)
1614 then
1615 declare
1616 Formal_Ent : constant Entity_Id :=
1617 Defining_Identifier (Analyzed_Formal);
1618 begin
1619 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1620 = Is_Remote_Types (Formal_Ent)
1621 then
1622 -- Remoteness of formal and actual match
1623
1624 null;
1625
1626 elsif Is_Remote_Types (Formal_Ent) then
1627
1628 -- Remote formal, non-remote actual
1629
1630 Error_Msg_NE
1631 ("actual for& must be remote", Match, Formal_Ent);
1632
1633 else
1634 -- Non-remote formal, remote actual
1635
1636 Error_Msg_NE
1637 ("actual for& may not be remote",
1638 Match, Formal_Ent);
1639 end if;
1640 end;
1641 end if;
1642
1643 when N_Formal_Subprogram_Declaration =>
1644 Match :=
1645 Matching_Actual
1646 (Defining_Unit_Name (Specification (Formal)),
1647 Defining_Unit_Name (Specification (Analyzed_Formal)));
1648
1649 -- If the formal subprogram has the same name as another
1650 -- formal subprogram of the generic, then a named
1651 -- association is illegal (12.3(9)). Exclude named
1652 -- associations that are generated for a nested instance.
1653
1654 if Present (Match)
1655 and then Is_Named_Assoc
1656 and then Comes_From_Source (Found_Assoc)
1657 then
1658 Check_Overloaded_Formal_Subprogram (Formal);
1659 end if;
1660
1661 -- If there is no corresponding actual, this may be case
1662 -- of partial parameterization, or else the formal has a
1663 -- default or a box.
1664
1665 if No (Match) and then Partial_Parameterization then
1666 Process_Default (Formal);
1667
1668 if Nkind (I_Node) = N_Formal_Package_Declaration then
1669 Check_Overloaded_Formal_Subprogram (Formal);
1670 end if;
1671
1672 else
1673 if GNATprove_Mode
1674 and then Ekind (Defining_Entity (Analyzed_Formal))
1675 = E_Function
1676 then
1677
1678 -- If actual is an entity (function or operator),
1679 -- build wrapper for it.
1680
1681 if Present (Match)
1682 and then Nkind (Match) = N_Operator_Symbol
1683 then
1684
1685 -- If the name is a default, find its visible
1686 -- entity at the point of instantiation.
1687
1688 if Is_Entity_Name (Match)
1689 and then No (Entity (Match))
1690 then
1691 Find_Direct_Name (Match);
1692 end if;
1693
1694 Append_To (Assoc,
1695 Build_Wrapper
1696 (Defining_Entity (Analyzed_Formal), Match));
1697
1698 -- Ditto if formal is an operator with a default.
1699
1700 elsif Box_Present (Formal)
1701 and then Nkind (Defining_Entity (Analyzed_Formal))
1702 = N_Defining_Operator_Symbol
1703 then
1704 Append_To (Assoc,
1705 Build_Wrapper
1706 (Defining_Entity (Analyzed_Formal)));
1707
1708 -- Otherwise create renaming declaration.
1709
1710 else
1711 Append_To (Assoc,
1712 Instantiate_Formal_Subprogram
1713 (Formal, Match, Analyzed_Formal));
1714 end if;
1715
1716 else
1717 Append_To (Assoc,
1718 Instantiate_Formal_Subprogram
1719 (Formal, Match, Analyzed_Formal));
1720 end if;
1721
1722 -- An instantiation is a freeze point for the actuals,
1723 -- unless this is a rewritten formal package.
1724
1725 if Nkind (I_Node) /= N_Formal_Package_Declaration
1726 and then Nkind (Match) = N_Identifier
1727 and then Is_Subprogram (Entity (Match))
1728
1729 -- The actual subprogram may rename a routine defined
1730 -- in Standard. Avoid freezing such renamings because
1731 -- subprograms coming from Standard cannot be frozen.
1732
1733 and then
1734 not Renames_Standard_Subprogram (Entity (Match))
1735
1736 -- If the actual subprogram comes from a different
1737 -- unit, it is already frozen, either by a body in
1738 -- that unit or by the end of the declarative part
1739 -- of the unit. This check avoids the freezing of
1740 -- subprograms defined in Standard which are used
1741 -- as generic actuals.
1742
1743 and then In_Same_Code_Unit (Entity (Match), I_Node)
1744 and then Has_Fully_Defined_Profile (Entity (Match))
1745 then
1746 -- Mark the subprogram as having a delayed freeze
1747 -- since this may be an out-of-order action.
1748
1749 Set_Has_Delayed_Freeze (Entity (Match));
1750 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1751 end if;
1752 end if;
1753
1754 -- If this is a nested generic, preserve default for later
1755 -- instantiations.
1756
1757 if No (Match)
1758 and then Box_Present (Formal)
1759 then
1760 Append_Elmt
1761 (Defining_Unit_Name (Specification (Last (Assoc))),
1762 Default_Actuals);
1763 end if;
1764
1765 when N_Formal_Package_Declaration =>
1766 Match :=
1767 Matching_Actual (
1768 Defining_Identifier (Formal),
1769 Defining_Identifier (Original_Node (Analyzed_Formal)));
1770
1771 if No (Match) then
1772 if Partial_Parameterization then
1773 Process_Default (Formal);
1774
1775 else
1776 Error_Msg_Sloc := Sloc (Gen_Unit);
1777 Error_Msg_NE
1778 ("missing actual&",
1779 Instantiation_Node, Defining_Identifier (Formal));
1780 Error_Msg_NE ("\in instantiation of & declared#",
1781 Instantiation_Node, Gen_Unit);
1782
1783 Abandon_Instantiation (Instantiation_Node);
1784 end if;
1785
1786 else
1787 Analyze (Match);
1788 Append_List
1789 (Instantiate_Formal_Package
1790 (Formal, Match, Analyzed_Formal),
1791 Assoc);
1792 end if;
1793
1794 -- For use type and use package appearing in the generic part,
1795 -- we have already copied them, so we can just move them where
1796 -- they belong (we mustn't recopy them since this would mess up
1797 -- the Sloc values).
1798
1799 when N_Use_Package_Clause |
1800 N_Use_Type_Clause =>
1801 if Nkind (Original_Node (I_Node)) =
1802 N_Formal_Package_Declaration
1803 then
1804 Append (New_Copy_Tree (Formal), Assoc);
1805 else
1806 Remove (Formal);
1807 Append (Formal, Assoc);
1808 end if;
1809
1810 when others =>
1811 raise Program_Error;
1812
1813 end case;
1814
1815 Formal := Saved_Formal;
1816 Next_Non_Pragma (Analyzed_Formal);
1817 end loop;
1818
1819 if Num_Actuals > Num_Matched then
1820 Error_Msg_Sloc := Sloc (Gen_Unit);
1821
1822 if Present (Selector_Name (Actual)) then
1823 Error_Msg_NE
1824 ("unmatched actual&",
1825 Actual, Selector_Name (Actual));
1826 Error_Msg_NE ("\in instantiation of& declared#",
1827 Actual, Gen_Unit);
1828 else
1829 Error_Msg_NE
1830 ("unmatched actual in instantiation of& declared#",
1831 Actual, Gen_Unit);
1832 end if;
1833 end if;
1834
1835 elsif Present (Actuals) then
1836 Error_Msg_N
1837 ("too many actuals in generic instantiation", Instantiation_Node);
1838 end if;
1839
1840 -- An instantiation freezes all generic actuals. The only exceptions
1841 -- to this are incomplete types and subprograms which are not fully
1842 -- defined at the point of instantiation.
1843
1844 declare
1845 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1846 begin
1847 while Present (Elmt) loop
1848 Freeze_Before (I_Node, Node (Elmt));
1849 Next_Elmt (Elmt);
1850 end loop;
1851 end;
1852
1853 -- If there are default subprograms, normalize the tree by adding
1854 -- explicit associations for them. This is required if the instance
1855 -- appears within a generic.
1856
1857 declare
1858 Elmt : Elmt_Id;
1859 Subp : Entity_Id;
1860 New_D : Node_Id;
1861
1862 begin
1863 Elmt := First_Elmt (Default_Actuals);
1864 while Present (Elmt) loop
1865 if No (Actuals) then
1866 Actuals := New_List;
1867 Set_Generic_Associations (I_Node, Actuals);
1868 end if;
1869
1870 Subp := Node (Elmt);
1871 New_D :=
1872 Make_Generic_Association (Sloc (Subp),
1873 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1874 Explicit_Generic_Actual_Parameter =>
1875 New_Occurrence_Of (Subp, Sloc (Subp)));
1876 Mark_Rewrite_Insertion (New_D);
1877 Append_To (Actuals, New_D);
1878 Next_Elmt (Elmt);
1879 end loop;
1880 end;
1881
1882 -- If this is a formal package, normalize the parameter list by adding
1883 -- explicit box associations for the formals that are covered by an
1884 -- Others_Choice.
1885
1886 if not Is_Empty_List (Default_Formals) then
1887 Append_List (Default_Formals, Formals);
1888 end if;
1889
1890 return Assoc;
1891 end Analyze_Associations;
1892
1893 -------------------------------
1894 -- Analyze_Formal_Array_Type --
1895 -------------------------------
1896
1897 procedure Analyze_Formal_Array_Type
1898 (T : in out Entity_Id;
1899 Def : Node_Id)
1900 is
1901 DSS : Node_Id;
1902
1903 begin
1904 -- Treated like a non-generic array declaration, with additional
1905 -- semantic checks.
1906
1907 Enter_Name (T);
1908
1909 if Nkind (Def) = N_Constrained_Array_Definition then
1910 DSS := First (Discrete_Subtype_Definitions (Def));
1911 while Present (DSS) loop
1912 if Nkind_In (DSS, N_Subtype_Indication,
1913 N_Range,
1914 N_Attribute_Reference)
1915 then
1916 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1917 end if;
1918
1919 Next (DSS);
1920 end loop;
1921 end if;
1922
1923 Array_Type_Declaration (T, Def);
1924 Set_Is_Generic_Type (Base_Type (T));
1925
1926 if Ekind (Component_Type (T)) = E_Incomplete_Type
1927 and then No (Full_View (Component_Type (T)))
1928 then
1929 Error_Msg_N ("premature usage of incomplete type", Def);
1930
1931 -- Check that range constraint is not allowed on the component type
1932 -- of a generic formal array type (AARM 12.5.3(3))
1933
1934 elsif Is_Internal (Component_Type (T))
1935 and then Present (Subtype_Indication (Component_Definition (Def)))
1936 and then Nkind (Original_Node
1937 (Subtype_Indication (Component_Definition (Def)))) =
1938 N_Subtype_Indication
1939 then
1940 Error_Msg_N
1941 ("in a formal, a subtype indication can only be "
1942 & "a subtype mark (RM 12.5.3(3))",
1943 Subtype_Indication (Component_Definition (Def)));
1944 end if;
1945
1946 end Analyze_Formal_Array_Type;
1947
1948 ---------------------------------------------
1949 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1950 ---------------------------------------------
1951
1952 -- As for other generic types, we create a valid type representation with
1953 -- legal but arbitrary attributes, whose values are never considered
1954 -- static. For all scalar types we introduce an anonymous base type, with
1955 -- the same attributes. We choose the corresponding integer type to be
1956 -- Standard_Integer.
1957 -- Here and in other similar routines, the Sloc of the generated internal
1958 -- type must be the same as the sloc of the defining identifier of the
1959 -- formal type declaration, to provide proper source navigation.
1960
1961 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1962 (T : Entity_Id;
1963 Def : Node_Id)
1964 is
1965 Loc : constant Source_Ptr := Sloc (Def);
1966
1967 Base : constant Entity_Id :=
1968 New_Internal_Entity
1969 (E_Decimal_Fixed_Point_Type,
1970 Current_Scope,
1971 Sloc (Defining_Identifier (Parent (Def))), 'G');
1972
1973 Int_Base : constant Entity_Id := Standard_Integer;
1974 Delta_Val : constant Ureal := Ureal_1;
1975 Digs_Val : constant Uint := Uint_6;
1976
1977 function Make_Dummy_Bound return Node_Id;
1978 -- Return a properly typed universal real literal to use as a bound
1979
1980 ----------------------
1981 -- Make_Dummy_Bound --
1982 ----------------------
1983
1984 function Make_Dummy_Bound return Node_Id is
1985 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1986 begin
1987 Set_Etype (Bound, Universal_Real);
1988 return Bound;
1989 end Make_Dummy_Bound;
1990
1991 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1992
1993 begin
1994 Enter_Name (T);
1995
1996 Set_Etype (Base, Base);
1997 Set_Size_Info (Base, Int_Base);
1998 Set_RM_Size (Base, RM_Size (Int_Base));
1999 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2000 Set_Digits_Value (Base, Digs_Val);
2001 Set_Delta_Value (Base, Delta_Val);
2002 Set_Small_Value (Base, Delta_Val);
2003 Set_Scalar_Range (Base,
2004 Make_Range (Loc,
2005 Low_Bound => Make_Dummy_Bound,
2006 High_Bound => Make_Dummy_Bound));
2007
2008 Set_Is_Generic_Type (Base);
2009 Set_Parent (Base, Parent (Def));
2010
2011 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2012 Set_Etype (T, Base);
2013 Set_Size_Info (T, Int_Base);
2014 Set_RM_Size (T, RM_Size (Int_Base));
2015 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2016 Set_Digits_Value (T, Digs_Val);
2017 Set_Delta_Value (T, Delta_Val);
2018 Set_Small_Value (T, Delta_Val);
2019 Set_Scalar_Range (T, Scalar_Range (Base));
2020 Set_Is_Constrained (T);
2021
2022 Check_Restriction (No_Fixed_Point, Def);
2023 end Analyze_Formal_Decimal_Fixed_Point_Type;
2024
2025 -------------------------------------------
2026 -- Analyze_Formal_Derived_Interface_Type --
2027 -------------------------------------------
2028
2029 procedure Analyze_Formal_Derived_Interface_Type
2030 (N : Node_Id;
2031 T : Entity_Id;
2032 Def : Node_Id)
2033 is
2034 Loc : constant Source_Ptr := Sloc (Def);
2035
2036 begin
2037 -- Rewrite as a type declaration of a derived type. This ensures that
2038 -- the interface list and primitive operations are properly captured.
2039
2040 Rewrite (N,
2041 Make_Full_Type_Declaration (Loc,
2042 Defining_Identifier => T,
2043 Type_Definition => Def));
2044 Analyze (N);
2045 Set_Is_Generic_Type (T);
2046 end Analyze_Formal_Derived_Interface_Type;
2047
2048 ---------------------------------
2049 -- Analyze_Formal_Derived_Type --
2050 ---------------------------------
2051
2052 procedure Analyze_Formal_Derived_Type
2053 (N : Node_Id;
2054 T : Entity_Id;
2055 Def : Node_Id)
2056 is
2057 Loc : constant Source_Ptr := Sloc (Def);
2058 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2059 New_N : Node_Id;
2060
2061 begin
2062 Set_Is_Generic_Type (T);
2063
2064 if Private_Present (Def) then
2065 New_N :=
2066 Make_Private_Extension_Declaration (Loc,
2067 Defining_Identifier => T,
2068 Discriminant_Specifications => Discriminant_Specifications (N),
2069 Unknown_Discriminants_Present => Unk_Disc,
2070 Subtype_Indication => Subtype_Mark (Def),
2071 Interface_List => Interface_List (Def));
2072
2073 Set_Abstract_Present (New_N, Abstract_Present (Def));
2074 Set_Limited_Present (New_N, Limited_Present (Def));
2075 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2076
2077 else
2078 New_N :=
2079 Make_Full_Type_Declaration (Loc,
2080 Defining_Identifier => T,
2081 Discriminant_Specifications =>
2082 Discriminant_Specifications (Parent (T)),
2083 Type_Definition =>
2084 Make_Derived_Type_Definition (Loc,
2085 Subtype_Indication => Subtype_Mark (Def)));
2086
2087 Set_Abstract_Present
2088 (Type_Definition (New_N), Abstract_Present (Def));
2089 Set_Limited_Present
2090 (Type_Definition (New_N), Limited_Present (Def));
2091 end if;
2092
2093 Rewrite (N, New_N);
2094 Analyze (N);
2095
2096 if Unk_Disc then
2097 if not Is_Composite_Type (T) then
2098 Error_Msg_N
2099 ("unknown discriminants not allowed for elementary types", N);
2100 else
2101 Set_Has_Unknown_Discriminants (T);
2102 Set_Is_Constrained (T, False);
2103 end if;
2104 end if;
2105
2106 -- If the parent type has a known size, so does the formal, which makes
2107 -- legal representation clauses that involve the formal.
2108
2109 Set_Size_Known_At_Compile_Time
2110 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2111 end Analyze_Formal_Derived_Type;
2112
2113 ----------------------------------
2114 -- Analyze_Formal_Discrete_Type --
2115 ----------------------------------
2116
2117 -- The operations defined for a discrete types are those of an enumeration
2118 -- type. The size is set to an arbitrary value, for use in analyzing the
2119 -- generic unit.
2120
2121 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2122 Loc : constant Source_Ptr := Sloc (Def);
2123 Lo : Node_Id;
2124 Hi : Node_Id;
2125
2126 Base : constant Entity_Id :=
2127 New_Internal_Entity
2128 (E_Floating_Point_Type, Current_Scope,
2129 Sloc (Defining_Identifier (Parent (Def))), 'G');
2130
2131 begin
2132 Enter_Name (T);
2133 Set_Ekind (T, E_Enumeration_Subtype);
2134 Set_Etype (T, Base);
2135 Init_Size (T, 8);
2136 Init_Alignment (T);
2137 Set_Is_Generic_Type (T);
2138 Set_Is_Constrained (T);
2139
2140 -- For semantic analysis, the bounds of the type must be set to some
2141 -- non-static value. The simplest is to create attribute nodes for those
2142 -- bounds, that refer to the type itself. These bounds are never
2143 -- analyzed but serve as place-holders.
2144
2145 Lo :=
2146 Make_Attribute_Reference (Loc,
2147 Attribute_Name => Name_First,
2148 Prefix => New_Occurrence_Of (T, Loc));
2149 Set_Etype (Lo, T);
2150
2151 Hi :=
2152 Make_Attribute_Reference (Loc,
2153 Attribute_Name => Name_Last,
2154 Prefix => New_Occurrence_Of (T, Loc));
2155 Set_Etype (Hi, T);
2156
2157 Set_Scalar_Range (T,
2158 Make_Range (Loc,
2159 Low_Bound => Lo,
2160 High_Bound => Hi));
2161
2162 Set_Ekind (Base, E_Enumeration_Type);
2163 Set_Etype (Base, Base);
2164 Init_Size (Base, 8);
2165 Init_Alignment (Base);
2166 Set_Is_Generic_Type (Base);
2167 Set_Scalar_Range (Base, Scalar_Range (T));
2168 Set_Parent (Base, Parent (Def));
2169 end Analyze_Formal_Discrete_Type;
2170
2171 ----------------------------------
2172 -- Analyze_Formal_Floating_Type --
2173 ---------------------------------
2174
2175 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2176 Base : constant Entity_Id :=
2177 New_Internal_Entity
2178 (E_Floating_Point_Type, Current_Scope,
2179 Sloc (Defining_Identifier (Parent (Def))), 'G');
2180
2181 begin
2182 -- The various semantic attributes are taken from the predefined type
2183 -- Float, just so that all of them are initialized. Their values are
2184 -- never used because no constant folding or expansion takes place in
2185 -- the generic itself.
2186
2187 Enter_Name (T);
2188 Set_Ekind (T, E_Floating_Point_Subtype);
2189 Set_Etype (T, Base);
2190 Set_Size_Info (T, (Standard_Float));
2191 Set_RM_Size (T, RM_Size (Standard_Float));
2192 Set_Digits_Value (T, Digits_Value (Standard_Float));
2193 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2194 Set_Is_Constrained (T);
2195
2196 Set_Is_Generic_Type (Base);
2197 Set_Etype (Base, Base);
2198 Set_Size_Info (Base, (Standard_Float));
2199 Set_RM_Size (Base, RM_Size (Standard_Float));
2200 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2201 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2202 Set_Parent (Base, Parent (Def));
2203
2204 Check_Restriction (No_Floating_Point, Def);
2205 end Analyze_Formal_Floating_Type;
2206
2207 -----------------------------------
2208 -- Analyze_Formal_Interface_Type;--
2209 -----------------------------------
2210
2211 procedure Analyze_Formal_Interface_Type
2212 (N : Node_Id;
2213 T : Entity_Id;
2214 Def : Node_Id)
2215 is
2216 Loc : constant Source_Ptr := Sloc (N);
2217 New_N : Node_Id;
2218
2219 begin
2220 New_N :=
2221 Make_Full_Type_Declaration (Loc,
2222 Defining_Identifier => T,
2223 Type_Definition => Def);
2224
2225 Rewrite (N, New_N);
2226 Analyze (N);
2227 Set_Is_Generic_Type (T);
2228 end Analyze_Formal_Interface_Type;
2229
2230 ---------------------------------
2231 -- Analyze_Formal_Modular_Type --
2232 ---------------------------------
2233
2234 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2235 begin
2236 -- Apart from their entity kind, generic modular types are treated like
2237 -- signed integer types, and have the same attributes.
2238
2239 Analyze_Formal_Signed_Integer_Type (T, Def);
2240 Set_Ekind (T, E_Modular_Integer_Subtype);
2241 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2242
2243 end Analyze_Formal_Modular_Type;
2244
2245 ---------------------------------------
2246 -- Analyze_Formal_Object_Declaration --
2247 ---------------------------------------
2248
2249 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2250 E : constant Node_Id := Default_Expression (N);
2251 Id : constant Node_Id := Defining_Identifier (N);
2252 K : Entity_Kind;
2253 T : Node_Id;
2254
2255 begin
2256 Enter_Name (Id);
2257
2258 -- Determine the mode of the formal object
2259
2260 if Out_Present (N) then
2261 K := E_Generic_In_Out_Parameter;
2262
2263 if not In_Present (N) then
2264 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2265 end if;
2266
2267 else
2268 K := E_Generic_In_Parameter;
2269 end if;
2270
2271 if Present (Subtype_Mark (N)) then
2272 Find_Type (Subtype_Mark (N));
2273 T := Entity (Subtype_Mark (N));
2274
2275 -- Verify that there is no redundant null exclusion
2276
2277 if Null_Exclusion_Present (N) then
2278 if not Is_Access_Type (T) then
2279 Error_Msg_N
2280 ("null exclusion can only apply to an access type", N);
2281
2282 elsif Can_Never_Be_Null (T) then
2283 Error_Msg_NE
2284 ("`NOT NULL` not allowed (& already excludes null)",
2285 N, T);
2286 end if;
2287 end if;
2288
2289 -- Ada 2005 (AI-423): Formal object with an access definition
2290
2291 else
2292 Check_Access_Definition (N);
2293 T := Access_Definition
2294 (Related_Nod => N,
2295 N => Access_Definition (N));
2296 end if;
2297
2298 if Ekind (T) = E_Incomplete_Type then
2299 declare
2300 Error_Node : Node_Id;
2301
2302 begin
2303 if Present (Subtype_Mark (N)) then
2304 Error_Node := Subtype_Mark (N);
2305 else
2306 Check_Access_Definition (N);
2307 Error_Node := Access_Definition (N);
2308 end if;
2309
2310 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2311 end;
2312 end if;
2313
2314 if K = E_Generic_In_Parameter then
2315
2316 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2317
2318 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2319 Error_Msg_N
2320 ("generic formal of mode IN must not be of limited type", N);
2321 Explain_Limited_Type (T, N);
2322 end if;
2323
2324 if Is_Abstract_Type (T) then
2325 Error_Msg_N
2326 ("generic formal of mode IN must not be of abstract type", N);
2327 end if;
2328
2329 if Present (E) then
2330 Preanalyze_Spec_Expression (E, T);
2331
2332 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2333 Error_Msg_N
2334 ("initialization not allowed for limited types", E);
2335 Explain_Limited_Type (T, E);
2336 end if;
2337 end if;
2338
2339 Set_Ekind (Id, K);
2340 Set_Etype (Id, T);
2341
2342 -- Case of generic IN OUT parameter
2343
2344 else
2345 -- If the formal has an unconstrained type, construct its actual
2346 -- subtype, as is done for subprogram formals. In this fashion, all
2347 -- its uses can refer to specific bounds.
2348
2349 Set_Ekind (Id, K);
2350 Set_Etype (Id, T);
2351
2352 if (Is_Array_Type (T)
2353 and then not Is_Constrained (T))
2354 or else
2355 (Ekind (T) = E_Record_Type
2356 and then Has_Discriminants (T))
2357 then
2358 declare
2359 Non_Freezing_Ref : constant Node_Id :=
2360 New_Occurrence_Of (Id, Sloc (Id));
2361 Decl : Node_Id;
2362
2363 begin
2364 -- Make sure the actual subtype doesn't generate bogus freezing
2365
2366 Set_Must_Not_Freeze (Non_Freezing_Ref);
2367 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2368 Insert_Before_And_Analyze (N, Decl);
2369 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2370 end;
2371 else
2372 Set_Actual_Subtype (Id, T);
2373 end if;
2374
2375 if Present (E) then
2376 Error_Msg_N
2377 ("initialization not allowed for `IN OUT` formals", N);
2378 end if;
2379 end if;
2380
2381 if Has_Aspects (N) then
2382 Analyze_Aspect_Specifications (N, Id);
2383 end if;
2384 end Analyze_Formal_Object_Declaration;
2385
2386 ----------------------------------------------
2387 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2388 ----------------------------------------------
2389
2390 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2391 (T : Entity_Id;
2392 Def : Node_Id)
2393 is
2394 Loc : constant Source_Ptr := Sloc (Def);
2395 Base : constant Entity_Id :=
2396 New_Internal_Entity
2397 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2398 Sloc (Defining_Identifier (Parent (Def))), 'G');
2399
2400 begin
2401 -- The semantic attributes are set for completeness only, their values
2402 -- will never be used, since all properties of the type are non-static.
2403
2404 Enter_Name (T);
2405 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2406 Set_Etype (T, Base);
2407 Set_Size_Info (T, Standard_Integer);
2408 Set_RM_Size (T, RM_Size (Standard_Integer));
2409 Set_Small_Value (T, Ureal_1);
2410 Set_Delta_Value (T, Ureal_1);
2411 Set_Scalar_Range (T,
2412 Make_Range (Loc,
2413 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2414 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2415 Set_Is_Constrained (T);
2416
2417 Set_Is_Generic_Type (Base);
2418 Set_Etype (Base, Base);
2419 Set_Size_Info (Base, Standard_Integer);
2420 Set_RM_Size (Base, RM_Size (Standard_Integer));
2421 Set_Small_Value (Base, Ureal_1);
2422 Set_Delta_Value (Base, Ureal_1);
2423 Set_Scalar_Range (Base, Scalar_Range (T));
2424 Set_Parent (Base, Parent (Def));
2425
2426 Check_Restriction (No_Fixed_Point, Def);
2427 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2428
2429 ----------------------------------------
2430 -- Analyze_Formal_Package_Declaration --
2431 ----------------------------------------
2432
2433 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2434 Loc : constant Source_Ptr := Sloc (N);
2435 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2436 Formal : Entity_Id;
2437 Gen_Id : constant Node_Id := Name (N);
2438 Gen_Decl : Node_Id;
2439 Gen_Unit : Entity_Id;
2440 New_N : Node_Id;
2441 Parent_Installed : Boolean := False;
2442 Renaming : Node_Id;
2443 Parent_Instance : Entity_Id;
2444 Renaming_In_Par : Entity_Id;
2445 Associations : Boolean := True;
2446
2447 Vis_Prims_List : Elist_Id := No_Elist;
2448 -- List of primitives made temporarily visible in the instantiation
2449 -- to match the visibility of the formal type
2450
2451 function Build_Local_Package return Node_Id;
2452 -- The formal package is rewritten so that its parameters are replaced
2453 -- with corresponding declarations. For parameters with bona fide
2454 -- associations these declarations are created by Analyze_Associations
2455 -- as for a regular instantiation. For boxed parameters, we preserve
2456 -- the formal declarations and analyze them, in order to introduce
2457 -- entities of the right kind in the environment of the formal.
2458
2459 -------------------------
2460 -- Build_Local_Package --
2461 -------------------------
2462
2463 function Build_Local_Package return Node_Id is
2464 Decls : List_Id;
2465 Pack_Decl : Node_Id;
2466
2467 begin
2468 -- Within the formal, the name of the generic package is a renaming
2469 -- of the formal (as for a regular instantiation).
2470
2471 Pack_Decl :=
2472 Make_Package_Declaration (Loc,
2473 Specification =>
2474 Copy_Generic_Node
2475 (Specification (Original_Node (Gen_Decl)),
2476 Empty, Instantiating => True));
2477
2478 Renaming := Make_Package_Renaming_Declaration (Loc,
2479 Defining_Unit_Name =>
2480 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2481 Name => New_Occurrence_Of (Formal, Loc));
2482
2483 if Nkind (Gen_Id) = N_Identifier
2484 and then Chars (Gen_Id) = Chars (Pack_Id)
2485 then
2486 Error_Msg_NE
2487 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2488 end if;
2489
2490 -- If the formal is declared with a box, or with an others choice,
2491 -- create corresponding declarations for all entities in the formal
2492 -- part, so that names with the proper types are available in the
2493 -- specification of the formal package.
2494
2495 -- On the other hand, if there are no associations, then all the
2496 -- formals must have defaults, and this will be checked by the
2497 -- call to Analyze_Associations.
2498
2499 if Box_Present (N)
2500 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2501 then
2502 declare
2503 Formal_Decl : Node_Id;
2504
2505 begin
2506 -- TBA : for a formal package, need to recurse ???
2507
2508 Decls := New_List;
2509 Formal_Decl :=
2510 First
2511 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2512 while Present (Formal_Decl) loop
2513 Append_To
2514 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2515 Next (Formal_Decl);
2516 end loop;
2517 end;
2518
2519 -- If generic associations are present, use Analyze_Associations to
2520 -- create the proper renaming declarations.
2521
2522 else
2523 declare
2524 Act_Tree : constant Node_Id :=
2525 Copy_Generic_Node
2526 (Original_Node (Gen_Decl), Empty,
2527 Instantiating => True);
2528
2529 begin
2530 Generic_Renamings.Set_Last (0);
2531 Generic_Renamings_HTable.Reset;
2532 Instantiation_Node := N;
2533
2534 Decls :=
2535 Analyze_Associations
2536 (I_Node => Original_Node (N),
2537 Formals => Generic_Formal_Declarations (Act_Tree),
2538 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2539
2540 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2541 end;
2542 end if;
2543
2544 Append (Renaming, To => Decls);
2545
2546 -- Add generated declarations ahead of local declarations in
2547 -- the package.
2548
2549 if No (Visible_Declarations (Specification (Pack_Decl))) then
2550 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2551 else
2552 Insert_List_Before
2553 (First (Visible_Declarations (Specification (Pack_Decl))),
2554 Decls);
2555 end if;
2556
2557 return Pack_Decl;
2558 end Build_Local_Package;
2559
2560 -- Start of processing for Analyze_Formal_Package_Declaration
2561
2562 begin
2563 Check_Text_IO_Special_Unit (Gen_Id);
2564
2565 Init_Env;
2566 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2567 Gen_Unit := Entity (Gen_Id);
2568
2569 -- Check for a formal package that is a package renaming
2570
2571 if Present (Renamed_Object (Gen_Unit)) then
2572
2573 -- Indicate that unit is used, before replacing it with renamed
2574 -- entity for use below.
2575
2576 if In_Extended_Main_Source_Unit (N) then
2577 Set_Is_Instantiated (Gen_Unit);
2578 Generate_Reference (Gen_Unit, N);
2579 end if;
2580
2581 Gen_Unit := Renamed_Object (Gen_Unit);
2582 end if;
2583
2584 if Ekind (Gen_Unit) /= E_Generic_Package then
2585 Error_Msg_N ("expect generic package name", Gen_Id);
2586 Restore_Env;
2587 goto Leave;
2588
2589 elsif Gen_Unit = Current_Scope then
2590 Error_Msg_N
2591 ("generic package cannot be used as a formal package of itself",
2592 Gen_Id);
2593 Restore_Env;
2594 goto Leave;
2595
2596 elsif In_Open_Scopes (Gen_Unit) then
2597 if Is_Compilation_Unit (Gen_Unit)
2598 and then Is_Child_Unit (Current_Scope)
2599 then
2600 -- Special-case the error when the formal is a parent, and
2601 -- continue analysis to minimize cascaded errors.
2602
2603 Error_Msg_N
2604 ("generic parent cannot be used as formal package "
2605 & "of a child unit",
2606 Gen_Id);
2607
2608 else
2609 Error_Msg_N
2610 ("generic package cannot be used as a formal package "
2611 & "within itself",
2612 Gen_Id);
2613 Restore_Env;
2614 goto Leave;
2615 end if;
2616 end if;
2617
2618 -- Check that name of formal package does not hide name of generic,
2619 -- or its leading prefix. This check must be done separately because
2620 -- the name of the generic has already been analyzed.
2621
2622 declare
2623 Gen_Name : Entity_Id;
2624
2625 begin
2626 Gen_Name := Gen_Id;
2627 while Nkind (Gen_Name) = N_Expanded_Name loop
2628 Gen_Name := Prefix (Gen_Name);
2629 end loop;
2630
2631 if Chars (Gen_Name) = Chars (Pack_Id) then
2632 Error_Msg_NE
2633 ("& is hidden within declaration of formal package",
2634 Gen_Id, Gen_Name);
2635 end if;
2636 end;
2637
2638 if Box_Present (N)
2639 or else No (Generic_Associations (N))
2640 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2641 then
2642 Associations := False;
2643 end if;
2644
2645 -- If there are no generic associations, the generic parameters appear
2646 -- as local entities and are instantiated like them. We copy the generic
2647 -- package declaration as if it were an instantiation, and analyze it
2648 -- like a regular package, except that we treat the formals as
2649 -- additional visible components.
2650
2651 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2652
2653 if In_Extended_Main_Source_Unit (N) then
2654 Set_Is_Instantiated (Gen_Unit);
2655 Generate_Reference (Gen_Unit, N);
2656 end if;
2657
2658 Formal := New_Copy (Pack_Id);
2659 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2660
2661 begin
2662 -- Make local generic without formals. The formals will be replaced
2663 -- with internal declarations.
2664
2665 New_N := Build_Local_Package;
2666
2667 -- If there are errors in the parameter list, Analyze_Associations
2668 -- raises Instantiation_Error. Patch the declaration to prevent
2669 -- further exception propagation.
2670
2671 exception
2672 when Instantiation_Error =>
2673
2674 Enter_Name (Formal);
2675 Set_Ekind (Formal, E_Variable);
2676 Set_Etype (Formal, Any_Type);
2677 Restore_Hidden_Primitives (Vis_Prims_List);
2678
2679 if Parent_Installed then
2680 Remove_Parent;
2681 end if;
2682
2683 goto Leave;
2684 end;
2685
2686 Rewrite (N, New_N);
2687 Set_Defining_Unit_Name (Specification (New_N), Formal);
2688 Set_Generic_Parent (Specification (N), Gen_Unit);
2689 Set_Instance_Env (Gen_Unit, Formal);
2690 Set_Is_Generic_Instance (Formal);
2691
2692 Enter_Name (Formal);
2693 Set_Ekind (Formal, E_Package);
2694 Set_Etype (Formal, Standard_Void_Type);
2695 Set_Inner_Instances (Formal, New_Elmt_List);
2696 Push_Scope (Formal);
2697
2698 if Is_Child_Unit (Gen_Unit)
2699 and then Parent_Installed
2700 then
2701 -- Similarly, we have to make the name of the formal visible in the
2702 -- parent instance, to resolve properly fully qualified names that
2703 -- may appear in the generic unit. The parent instance has been
2704 -- placed on the scope stack ahead of the current scope.
2705
2706 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2707
2708 Renaming_In_Par :=
2709 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2710 Set_Ekind (Renaming_In_Par, E_Package);
2711 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2712 Set_Scope (Renaming_In_Par, Parent_Instance);
2713 Set_Parent (Renaming_In_Par, Parent (Formal));
2714 Set_Renamed_Object (Renaming_In_Par, Formal);
2715 Append_Entity (Renaming_In_Par, Parent_Instance);
2716 end if;
2717
2718 Analyze (Specification (N));
2719
2720 -- The formals for which associations are provided are not visible
2721 -- outside of the formal package. The others are still declared by a
2722 -- formal parameter declaration.
2723
2724 -- If there are no associations, the only local entity to hide is the
2725 -- generated package renaming itself.
2726
2727 declare
2728 E : Entity_Id;
2729
2730 begin
2731 E := First_Entity (Formal);
2732 while Present (E) loop
2733 if Associations
2734 and then not Is_Generic_Formal (E)
2735 then
2736 Set_Is_Hidden (E);
2737 end if;
2738
2739 if Ekind (E) = E_Package
2740 and then Renamed_Entity (E) = Formal
2741 then
2742 Set_Is_Hidden (E);
2743 exit;
2744 end if;
2745
2746 Next_Entity (E);
2747 end loop;
2748 end;
2749
2750 End_Package_Scope (Formal);
2751 Restore_Hidden_Primitives (Vis_Prims_List);
2752
2753 if Parent_Installed then
2754 Remove_Parent;
2755 end if;
2756
2757 Restore_Env;
2758
2759 -- Inside the generic unit, the formal package is a regular package, but
2760 -- no body is needed for it. Note that after instantiation, the defining
2761 -- unit name we need is in the new tree and not in the original (see
2762 -- Package_Instantiation). A generic formal package is an instance, and
2763 -- can be used as an actual for an inner instance.
2764
2765 Set_Has_Completion (Formal, True);
2766
2767 -- Add semantic information to the original defining identifier.
2768 -- for ASIS use.
2769
2770 Set_Ekind (Pack_Id, E_Package);
2771 Set_Etype (Pack_Id, Standard_Void_Type);
2772 Set_Scope (Pack_Id, Scope (Formal));
2773 Set_Has_Completion (Pack_Id, True);
2774
2775 <<Leave>>
2776 if Has_Aspects (N) then
2777 Analyze_Aspect_Specifications (N, Pack_Id);
2778 end if;
2779 end Analyze_Formal_Package_Declaration;
2780
2781 ---------------------------------
2782 -- Analyze_Formal_Private_Type --
2783 ---------------------------------
2784
2785 procedure Analyze_Formal_Private_Type
2786 (N : Node_Id;
2787 T : Entity_Id;
2788 Def : Node_Id)
2789 is
2790 begin
2791 New_Private_Type (N, T, Def);
2792
2793 -- Set the size to an arbitrary but legal value
2794
2795 Set_Size_Info (T, Standard_Integer);
2796 Set_RM_Size (T, RM_Size (Standard_Integer));
2797 end Analyze_Formal_Private_Type;
2798
2799 ------------------------------------
2800 -- Analyze_Formal_Incomplete_Type --
2801 ------------------------------------
2802
2803 procedure Analyze_Formal_Incomplete_Type
2804 (T : Entity_Id;
2805 Def : Node_Id)
2806 is
2807 begin
2808 Enter_Name (T);
2809 Set_Ekind (T, E_Incomplete_Type);
2810 Set_Etype (T, T);
2811 Set_Private_Dependents (T, New_Elmt_List);
2812
2813 if Tagged_Present (Def) then
2814 Set_Is_Tagged_Type (T);
2815 Make_Class_Wide_Type (T);
2816 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2817 end if;
2818 end Analyze_Formal_Incomplete_Type;
2819
2820 ----------------------------------------
2821 -- Analyze_Formal_Signed_Integer_Type --
2822 ----------------------------------------
2823
2824 procedure Analyze_Formal_Signed_Integer_Type
2825 (T : Entity_Id;
2826 Def : Node_Id)
2827 is
2828 Base : constant Entity_Id :=
2829 New_Internal_Entity
2830 (E_Signed_Integer_Type,
2831 Current_Scope,
2832 Sloc (Defining_Identifier (Parent (Def))), 'G');
2833
2834 begin
2835 Enter_Name (T);
2836
2837 Set_Ekind (T, E_Signed_Integer_Subtype);
2838 Set_Etype (T, Base);
2839 Set_Size_Info (T, Standard_Integer);
2840 Set_RM_Size (T, RM_Size (Standard_Integer));
2841 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2842 Set_Is_Constrained (T);
2843
2844 Set_Is_Generic_Type (Base);
2845 Set_Size_Info (Base, Standard_Integer);
2846 Set_RM_Size (Base, RM_Size (Standard_Integer));
2847 Set_Etype (Base, Base);
2848 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2849 Set_Parent (Base, Parent (Def));
2850 end Analyze_Formal_Signed_Integer_Type;
2851
2852 -------------------------------------------
2853 -- Analyze_Formal_Subprogram_Declaration --
2854 -------------------------------------------
2855
2856 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2857 Spec : constant Node_Id := Specification (N);
2858 Def : constant Node_Id := Default_Name (N);
2859 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2860 Subp : Entity_Id;
2861
2862 begin
2863 if Nam = Error then
2864 return;
2865 end if;
2866
2867 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2868 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2869 goto Leave;
2870 end if;
2871
2872 Analyze_Subprogram_Declaration (N);
2873 Set_Is_Formal_Subprogram (Nam);
2874 Set_Has_Completion (Nam);
2875
2876 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2877 Set_Is_Abstract_Subprogram (Nam);
2878 Set_Is_Dispatching_Operation (Nam);
2879
2880 declare
2881 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2882 begin
2883 if No (Ctrl_Type) then
2884 Error_Msg_N
2885 ("abstract formal subprogram must have a controlling type",
2886 N);
2887
2888 elsif Ada_Version >= Ada_2012
2889 and then Is_Incomplete_Type (Ctrl_Type)
2890 then
2891 Error_Msg_NE
2892 ("controlling type of abstract formal subprogram cannot " &
2893 "be incomplete type", N, Ctrl_Type);
2894
2895 else
2896 Check_Controlling_Formals (Ctrl_Type, Nam);
2897 end if;
2898 end;
2899 end if;
2900
2901 -- Default name is resolved at the point of instantiation
2902
2903 if Box_Present (N) then
2904 null;
2905
2906 -- Else default is bound at the point of generic declaration
2907
2908 elsif Present (Def) then
2909 if Nkind (Def) = N_Operator_Symbol then
2910 Find_Direct_Name (Def);
2911
2912 elsif Nkind (Def) /= N_Attribute_Reference then
2913 Analyze (Def);
2914
2915 else
2916 -- For an attribute reference, analyze the prefix and verify
2917 -- that it has the proper profile for the subprogram.
2918
2919 Analyze (Prefix (Def));
2920 Valid_Default_Attribute (Nam, Def);
2921 goto Leave;
2922 end if;
2923
2924 -- Default name may be overloaded, in which case the interpretation
2925 -- with the correct profile must be selected, as for a renaming.
2926 -- If the definition is an indexed component, it must denote a
2927 -- member of an entry family. If it is a selected component, it
2928 -- can be a protected operation.
2929
2930 if Etype (Def) = Any_Type then
2931 goto Leave;
2932
2933 elsif Nkind (Def) = N_Selected_Component then
2934 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2935 Error_Msg_N ("expect valid subprogram name as default", Def);
2936 end if;
2937
2938 elsif Nkind (Def) = N_Indexed_Component then
2939 if Is_Entity_Name (Prefix (Def)) then
2940 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2941 Error_Msg_N ("expect valid subprogram name as default", Def);
2942 end if;
2943
2944 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2945 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2946 E_Entry_Family
2947 then
2948 Error_Msg_N ("expect valid subprogram name as default", Def);
2949 end if;
2950
2951 else
2952 Error_Msg_N ("expect valid subprogram name as default", Def);
2953 goto Leave;
2954 end if;
2955
2956 elsif Nkind (Def) = N_Character_Literal then
2957
2958 -- Needs some type checks: subprogram should be parameterless???
2959
2960 Resolve (Def, (Etype (Nam)));
2961
2962 elsif not Is_Entity_Name (Def)
2963 or else not Is_Overloadable (Entity (Def))
2964 then
2965 Error_Msg_N ("expect valid subprogram name as default", Def);
2966 goto Leave;
2967
2968 elsif not Is_Overloaded (Def) then
2969 Subp := Entity (Def);
2970
2971 if Subp = Nam then
2972 Error_Msg_N ("premature usage of formal subprogram", Def);
2973
2974 elsif not Entity_Matches_Spec (Subp, Nam) then
2975 Error_Msg_N ("no visible entity matches specification", Def);
2976 end if;
2977
2978 -- More than one interpretation, so disambiguate as for a renaming
2979
2980 else
2981 declare
2982 I : Interp_Index;
2983 I1 : Interp_Index := 0;
2984 It : Interp;
2985 It1 : Interp;
2986
2987 begin
2988 Subp := Any_Id;
2989 Get_First_Interp (Def, I, It);
2990 while Present (It.Nam) loop
2991 if Entity_Matches_Spec (It.Nam, Nam) then
2992 if Subp /= Any_Id then
2993 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2994
2995 if It1 = No_Interp then
2996 Error_Msg_N ("ambiguous default subprogram", Def);
2997 else
2998 Subp := It1.Nam;
2999 end if;
3000
3001 exit;
3002
3003 else
3004 I1 := I;
3005 Subp := It.Nam;
3006 end if;
3007 end if;
3008
3009 Get_Next_Interp (I, It);
3010 end loop;
3011 end;
3012
3013 if Subp /= Any_Id then
3014
3015 -- Subprogram found, generate reference to it
3016
3017 Set_Entity (Def, Subp);
3018 Generate_Reference (Subp, Def);
3019
3020 if Subp = Nam then
3021 Error_Msg_N ("premature usage of formal subprogram", Def);
3022
3023 elsif Ekind (Subp) /= E_Operator then
3024 Check_Mode_Conformant (Subp, Nam);
3025 end if;
3026
3027 else
3028 Error_Msg_N ("no visible subprogram matches specification", N);
3029 end if;
3030 end if;
3031 end if;
3032
3033 <<Leave>>
3034 if Has_Aspects (N) then
3035 Analyze_Aspect_Specifications (N, Nam);
3036 end if;
3037
3038 end Analyze_Formal_Subprogram_Declaration;
3039
3040 -------------------------------------
3041 -- Analyze_Formal_Type_Declaration --
3042 -------------------------------------
3043
3044 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3045 Def : constant Node_Id := Formal_Type_Definition (N);
3046 T : Entity_Id;
3047
3048 begin
3049 T := Defining_Identifier (N);
3050
3051 if Present (Discriminant_Specifications (N))
3052 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3053 then
3054 Error_Msg_N
3055 ("discriminants not allowed for this formal type", T);
3056 end if;
3057
3058 -- Enter the new name, and branch to specific routine
3059
3060 case Nkind (Def) is
3061 when N_Formal_Private_Type_Definition =>
3062 Analyze_Formal_Private_Type (N, T, Def);
3063
3064 when N_Formal_Derived_Type_Definition =>
3065 Analyze_Formal_Derived_Type (N, T, Def);
3066
3067 when N_Formal_Incomplete_Type_Definition =>
3068 Analyze_Formal_Incomplete_Type (T, Def);
3069
3070 when N_Formal_Discrete_Type_Definition =>
3071 Analyze_Formal_Discrete_Type (T, Def);
3072
3073 when N_Formal_Signed_Integer_Type_Definition =>
3074 Analyze_Formal_Signed_Integer_Type (T, Def);
3075
3076 when N_Formal_Modular_Type_Definition =>
3077 Analyze_Formal_Modular_Type (T, Def);
3078
3079 when N_Formal_Floating_Point_Definition =>
3080 Analyze_Formal_Floating_Type (T, Def);
3081
3082 when N_Formal_Ordinary_Fixed_Point_Definition =>
3083 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3084
3085 when N_Formal_Decimal_Fixed_Point_Definition =>
3086 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3087
3088 when N_Array_Type_Definition =>
3089 Analyze_Formal_Array_Type (T, Def);
3090
3091 when N_Access_To_Object_Definition |
3092 N_Access_Function_Definition |
3093 N_Access_Procedure_Definition =>
3094 Analyze_Generic_Access_Type (T, Def);
3095
3096 -- Ada 2005: a interface declaration is encoded as an abstract
3097 -- record declaration or a abstract type derivation.
3098
3099 when N_Record_Definition =>
3100 Analyze_Formal_Interface_Type (N, T, Def);
3101
3102 when N_Derived_Type_Definition =>
3103 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3104
3105 when N_Error =>
3106 null;
3107
3108 when others =>
3109 raise Program_Error;
3110
3111 end case;
3112
3113 Set_Is_Generic_Type (T);
3114
3115 if Has_Aspects (N) then
3116 Analyze_Aspect_Specifications (N, T);
3117 end if;
3118 end Analyze_Formal_Type_Declaration;
3119
3120 ------------------------------------
3121 -- Analyze_Function_Instantiation --
3122 ------------------------------------
3123
3124 procedure Analyze_Function_Instantiation (N : Node_Id) is
3125 begin
3126 Analyze_Subprogram_Instantiation (N, E_Function);
3127 end Analyze_Function_Instantiation;
3128
3129 ---------------------------------
3130 -- Analyze_Generic_Access_Type --
3131 ---------------------------------
3132
3133 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3134 begin
3135 Enter_Name (T);
3136
3137 if Nkind (Def) = N_Access_To_Object_Definition then
3138 Access_Type_Declaration (T, Def);
3139
3140 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3141 and then No (Full_View (Designated_Type (T)))
3142 and then not Is_Generic_Type (Designated_Type (T))
3143 then
3144 Error_Msg_N ("premature usage of incomplete type", Def);
3145
3146 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3147 Error_Msg_N
3148 ("only a subtype mark is allowed in a formal", Def);
3149 end if;
3150
3151 else
3152 Access_Subprogram_Declaration (T, Def);
3153 end if;
3154 end Analyze_Generic_Access_Type;
3155
3156 ---------------------------------
3157 -- Analyze_Generic_Formal_Part --
3158 ---------------------------------
3159
3160 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3161 Gen_Parm_Decl : Node_Id;
3162
3163 begin
3164 -- The generic formals are processed in the scope of the generic unit,
3165 -- where they are immediately visible. The scope is installed by the
3166 -- caller.
3167
3168 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3169
3170 while Present (Gen_Parm_Decl) loop
3171 Analyze (Gen_Parm_Decl);
3172 Next (Gen_Parm_Decl);
3173 end loop;
3174
3175 Generate_Reference_To_Generic_Formals (Current_Scope);
3176 end Analyze_Generic_Formal_Part;
3177
3178 ------------------------------------------
3179 -- Analyze_Generic_Package_Declaration --
3180 ------------------------------------------
3181
3182 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3183 Loc : constant Source_Ptr := Sloc (N);
3184 Id : Entity_Id;
3185 New_N : Node_Id;
3186 Save_Parent : Node_Id;
3187 Renaming : Node_Id;
3188 Decls : constant List_Id :=
3189 Visible_Declarations (Specification (N));
3190 Decl : Node_Id;
3191
3192 begin
3193 Check_SPARK_Restriction ("generic is not allowed", N);
3194
3195 -- We introduce a renaming of the enclosing package, to have a usable
3196 -- entity as the prefix of an expanded name for a local entity of the
3197 -- form Par.P.Q, where P is the generic package. This is because a local
3198 -- entity named P may hide it, so that the usual visibility rules in
3199 -- the instance will not resolve properly.
3200
3201 Renaming :=
3202 Make_Package_Renaming_Declaration (Loc,
3203 Defining_Unit_Name =>
3204 Make_Defining_Identifier (Loc,
3205 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3206 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
3207
3208 if Present (Decls) then
3209 Decl := First (Decls);
3210 while Present (Decl)
3211 and then Nkind (Decl) = N_Pragma
3212 loop
3213 Next (Decl);
3214 end loop;
3215
3216 if Present (Decl) then
3217 Insert_Before (Decl, Renaming);
3218 else
3219 Append (Renaming, Visible_Declarations (Specification (N)));
3220 end if;
3221
3222 else
3223 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3224 end if;
3225
3226 -- Create copy of generic unit, and save for instantiation. If the unit
3227 -- is a child unit, do not copy the specifications for the parent, which
3228 -- are not part of the generic tree.
3229
3230 Save_Parent := Parent_Spec (N);
3231 Set_Parent_Spec (N, Empty);
3232
3233 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3234 Set_Parent_Spec (New_N, Save_Parent);
3235 Rewrite (N, New_N);
3236
3237 -- Once the contents of the generic copy and the template are swapped,
3238 -- do the same for their respective aspect specifications.
3239
3240 Exchange_Aspects (N, New_N);
3241 Id := Defining_Entity (N);
3242 Generate_Definition (Id);
3243
3244 -- Expansion is not applied to generic units
3245
3246 Start_Generic;
3247
3248 Enter_Name (Id);
3249 Set_Ekind (Id, E_Generic_Package);
3250 Set_Etype (Id, Standard_Void_Type);
3251 Set_Contract (Id, Make_Contract (Sloc (Id)));
3252
3253 -- Analyze aspects now, so that generated pragmas appear in the
3254 -- declarations before building and analyzing the generic copy.
3255
3256 if Has_Aspects (N) then
3257 Analyze_Aspect_Specifications (N, Id);
3258 end if;
3259
3260 Push_Scope (Id);
3261 Enter_Generic_Scope (Id);
3262 Set_Inner_Instances (Id, New_Elmt_List);
3263
3264 Set_Categorization_From_Pragmas (N);
3265 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3266
3267 -- Link the declaration of the generic homonym in the generic copy to
3268 -- the package it renames, so that it is always resolved properly.
3269
3270 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3271 Set_Entity (Associated_Node (Name (Renaming)), Id);
3272
3273 -- For a library unit, we have reconstructed the entity for the unit,
3274 -- and must reset it in the library tables.
3275
3276 if Nkind (Parent (N)) = N_Compilation_Unit then
3277 Set_Cunit_Entity (Current_Sem_Unit, Id);
3278 end if;
3279
3280 Analyze_Generic_Formal_Part (N);
3281
3282 -- After processing the generic formals, analysis proceeds as for a
3283 -- non-generic package.
3284
3285 Analyze (Specification (N));
3286
3287 Validate_Categorization_Dependency (N, Id);
3288
3289 End_Generic;
3290
3291 End_Package_Scope (Id);
3292 Exit_Generic_Scope (Id);
3293
3294 if Nkind (Parent (N)) /= N_Compilation_Unit then
3295 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3296 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3297 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3298
3299 else
3300 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3301 Validate_RT_RAT_Component (N);
3302
3303 -- If this is a spec without a body, check that generic parameters
3304 -- are referenced.
3305
3306 if not Body_Required (Parent (N)) then
3307 Check_References (Id);
3308 end if;
3309 end if;
3310 end Analyze_Generic_Package_Declaration;
3311
3312 --------------------------------------------
3313 -- Analyze_Generic_Subprogram_Declaration --
3314 --------------------------------------------
3315
3316 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3317 Spec : Node_Id;
3318 Id : Entity_Id;
3319 Formals : List_Id;
3320 New_N : Node_Id;
3321 Result_Type : Entity_Id;
3322 Save_Parent : Node_Id;
3323 Typ : Entity_Id;
3324
3325 begin
3326 Check_SPARK_Restriction ("generic is not allowed", N);
3327
3328 -- Create copy of generic unit, and save for instantiation. If the unit
3329 -- is a child unit, do not copy the specifications for the parent, which
3330 -- are not part of the generic tree.
3331
3332 Save_Parent := Parent_Spec (N);
3333 Set_Parent_Spec (N, Empty);
3334
3335 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3336 Set_Parent_Spec (New_N, Save_Parent);
3337 Rewrite (N, New_N);
3338
3339 Check_SPARK_Mode_In_Generic (N);
3340
3341 -- The aspect specifications are not attached to the tree, and must
3342 -- be copied and attached to the generic copy explicitly.
3343
3344 if Present (Aspect_Specifications (New_N)) then
3345 declare
3346 Aspects : constant List_Id := Aspect_Specifications (N);
3347 begin
3348 Set_Has_Aspects (N, False);
3349 Move_Aspects (New_N, To => N);
3350 Set_Has_Aspects (Original_Node (N), False);
3351 Set_Aspect_Specifications (Original_Node (N), Aspects);
3352 end;
3353 end if;
3354
3355 Spec := Specification (N);
3356 Id := Defining_Entity (Spec);
3357 Generate_Definition (Id);
3358 Set_Contract (Id, Make_Contract (Sloc (Id)));
3359
3360 if Nkind (Id) = N_Defining_Operator_Symbol then
3361 Error_Msg_N
3362 ("operator symbol not allowed for generic subprogram", Id);
3363 end if;
3364
3365 Start_Generic;
3366
3367 Enter_Name (Id);
3368
3369 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3370 Push_Scope (Id);
3371 Enter_Generic_Scope (Id);
3372 Set_Inner_Instances (Id, New_Elmt_List);
3373 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3374
3375 Analyze_Generic_Formal_Part (N);
3376
3377 Formals := Parameter_Specifications (Spec);
3378
3379 if Present (Formals) then
3380 Process_Formals (Formals, Spec);
3381 end if;
3382
3383 if Nkind (Spec) = N_Function_Specification then
3384 Set_Ekind (Id, E_Generic_Function);
3385
3386 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3387 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3388 Set_Etype (Id, Result_Type);
3389
3390 -- Check restriction imposed by AI05-073: a generic function
3391 -- cannot return an abstract type or an access to such.
3392
3393 -- This is a binding interpretation should it apply to earlier
3394 -- versions of Ada as well as Ada 2012???
3395
3396 if Is_Abstract_Type (Designated_Type (Result_Type))
3397 and then Ada_Version >= Ada_2012
3398 then
3399 Error_Msg_N ("generic function cannot have an access result"
3400 & " that designates an abstract type", Spec);
3401 end if;
3402
3403 else
3404 Find_Type (Result_Definition (Spec));
3405 Typ := Entity (Result_Definition (Spec));
3406
3407 if Is_Abstract_Type (Typ)
3408 and then Ada_Version >= Ada_2012
3409 then
3410 Error_Msg_N
3411 ("generic function cannot have abstract result type", Spec);
3412 end if;
3413
3414 -- If a null exclusion is imposed on the result type, then create
3415 -- a null-excluding itype (an access subtype) and use it as the
3416 -- function's Etype.
3417
3418 if Is_Access_Type (Typ)
3419 and then Null_Exclusion_Present (Spec)
3420 then
3421 Set_Etype (Id,
3422 Create_Null_Excluding_Itype
3423 (T => Typ,
3424 Related_Nod => Spec,
3425 Scope_Id => Defining_Unit_Name (Spec)));
3426 else
3427 Set_Etype (Id, Typ);
3428 end if;
3429 end if;
3430
3431 else
3432 Set_Ekind (Id, E_Generic_Procedure);
3433 Set_Etype (Id, Standard_Void_Type);
3434 end if;
3435
3436 -- For a library unit, we have reconstructed the entity for the unit,
3437 -- and must reset it in the library tables. We also make sure that
3438 -- Body_Required is set properly in the original compilation unit node.
3439
3440 if Nkind (Parent (N)) = N_Compilation_Unit then
3441 Set_Cunit_Entity (Current_Sem_Unit, Id);
3442 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3443 end if;
3444
3445 Set_Categorization_From_Pragmas (N);
3446 Validate_Categorization_Dependency (N, Id);
3447
3448 Save_Global_References (Original_Node (N));
3449
3450 -- For ASIS purposes, convert any postcondition, precondition pragmas
3451 -- into aspects, if N is not a compilation unit by itself, in order to
3452 -- enable the analysis of expressions inside the corresponding PPC
3453 -- pragmas.
3454
3455 if ASIS_Mode and then Is_List_Member (N) then
3456 Make_Aspect_For_PPC_In_Gen_Sub_Decl (N);
3457 end if;
3458
3459 -- To capture global references, analyze the expressions of aspects,
3460 -- and propagate information to original tree. Note that in this case
3461 -- analysis of attributes is not delayed until the freeze point.
3462
3463 -- It seems very hard to recreate the proper visibility of the generic
3464 -- subprogram at a later point because the analysis of an aspect may
3465 -- create pragmas after the generic copies have been made ???
3466
3467 if Has_Aspects (N) then
3468 declare
3469 Aspect : Node_Id;
3470
3471 begin
3472 Aspect := First (Aspect_Specifications (N));
3473 while Present (Aspect) loop
3474 if Get_Aspect_Id (Aspect) /= Aspect_Warnings
3475 and then Present (Expression (Aspect))
3476 then
3477 Analyze (Expression (Aspect));
3478 end if;
3479
3480 Next (Aspect);
3481 end loop;
3482
3483 Aspect := First (Aspect_Specifications (Original_Node (N)));
3484 while Present (Aspect) loop
3485 if Present (Expression (Aspect)) then
3486 Save_Global_References (Expression (Aspect));
3487 end if;
3488
3489 Next (Aspect);
3490 end loop;
3491 end;
3492 end if;
3493
3494 End_Generic;
3495 End_Scope;
3496 Exit_Generic_Scope (Id);
3497 Generate_Reference_To_Formals (Id);
3498
3499 List_Inherited_Pre_Post_Aspects (Id);
3500 end Analyze_Generic_Subprogram_Declaration;
3501
3502 -----------------------------------
3503 -- Analyze_Package_Instantiation --
3504 -----------------------------------
3505
3506 procedure Analyze_Package_Instantiation (N : Node_Id) is
3507 Loc : constant Source_Ptr := Sloc (N);
3508 Gen_Id : constant Node_Id := Name (N);
3509
3510 Act_Decl : Node_Id;
3511 Act_Decl_Name : Node_Id;
3512 Act_Decl_Id : Entity_Id;
3513 Act_Spec : Node_Id;
3514 Act_Tree : Node_Id;
3515
3516 Gen_Decl : Node_Id;
3517 Gen_Unit : Entity_Id;
3518
3519 Is_Actual_Pack : constant Boolean :=
3520 Is_Internal (Defining_Entity (N));
3521
3522 Env_Installed : Boolean := False;
3523 Parent_Installed : Boolean := False;
3524 Renaming_List : List_Id;
3525 Unit_Renaming : Node_Id;
3526 Needs_Body : Boolean;
3527 Inline_Now : Boolean := False;
3528
3529 Save_Style_Check : constant Boolean := Style_Check;
3530 -- Save style check mode for restore on exit
3531
3532 procedure Delay_Descriptors (E : Entity_Id);
3533 -- Delay generation of subprogram descriptors for given entity
3534
3535 function Might_Inline_Subp return Boolean;
3536 -- If inlining is active and the generic contains inlined subprograms,
3537 -- we instantiate the body. This may cause superfluous instantiations,
3538 -- but it is simpler than detecting the need for the body at the point
3539 -- of inlining, when the context of the instance is not available.
3540
3541 function Must_Inline_Subp return Boolean;
3542 -- If inlining is active and the generic contains inlined subprograms,
3543 -- return True if some of the inlined subprograms must be inlined by
3544 -- the frontend.
3545
3546 -----------------------
3547 -- Delay_Descriptors --
3548 -----------------------
3549
3550 procedure Delay_Descriptors (E : Entity_Id) is
3551 begin
3552 if not Delay_Subprogram_Descriptors (E) then
3553 Set_Delay_Subprogram_Descriptors (E);
3554 Pending_Descriptor.Append (E);
3555 end if;
3556 end Delay_Descriptors;
3557
3558 -----------------------
3559 -- Might_Inline_Subp --
3560 -----------------------
3561
3562 function Might_Inline_Subp return Boolean is
3563 E : Entity_Id;
3564
3565 begin
3566 if not Inline_Processing_Required then
3567 return False;
3568
3569 else
3570 E := First_Entity (Gen_Unit);
3571 while Present (E) loop
3572 if Is_Subprogram (E)
3573 and then Is_Inlined (E)
3574 then
3575 return True;
3576 end if;
3577
3578 Next_Entity (E);
3579 end loop;
3580 end if;
3581
3582 return False;
3583 end Might_Inline_Subp;
3584
3585 ----------------------
3586 -- Must_Inline_Subp --
3587 ----------------------
3588
3589 function Must_Inline_Subp return Boolean is
3590 E : Entity_Id;
3591
3592 begin
3593 if not Inline_Processing_Required then
3594 return False;
3595
3596 else
3597 E := First_Entity (Gen_Unit);
3598 while Present (E) loop
3599 if Is_Subprogram (E)
3600 and then Is_Inlined (E)
3601 and then Must_Inline (E)
3602 then
3603 return True;
3604 end if;
3605
3606 Next_Entity (E);
3607 end loop;
3608 end if;
3609
3610 return False;
3611 end Must_Inline_Subp;
3612
3613 -- Local declarations
3614
3615 Vis_Prims_List : Elist_Id := No_Elist;
3616 -- List of primitives made temporarily visible in the instantiation
3617 -- to match the visibility of the formal type
3618
3619 -- Start of processing for Analyze_Package_Instantiation
3620
3621 begin
3622 Check_SPARK_Restriction ("generic is not allowed", N);
3623
3624 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3625 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3626
3627 Check_Text_IO_Special_Unit (Name (N));
3628
3629 -- Make node global for error reporting
3630
3631 Instantiation_Node := N;
3632
3633 -- Turn off style checking in instances. If the check is enabled on the
3634 -- generic unit, a warning in an instance would just be noise. If not
3635 -- enabled on the generic, then a warning in an instance is just wrong.
3636
3637 Style_Check := False;
3638
3639 -- Case of instantiation of a generic package
3640
3641 if Nkind (N) = N_Package_Instantiation then
3642 Act_Decl_Id := New_Copy (Defining_Entity (N));
3643 Set_Comes_From_Source (Act_Decl_Id, True);
3644
3645 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3646 Act_Decl_Name :=
3647 Make_Defining_Program_Unit_Name (Loc,
3648 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
3649 Defining_Identifier => Act_Decl_Id);
3650 else
3651 Act_Decl_Name := Act_Decl_Id;
3652 end if;
3653
3654 -- Case of instantiation of a formal package
3655
3656 else
3657 Act_Decl_Id := Defining_Identifier (N);
3658 Act_Decl_Name := Act_Decl_Id;
3659 end if;
3660
3661 Generate_Definition (Act_Decl_Id);
3662 Preanalyze_Actuals (N);
3663
3664 Init_Env;
3665 Env_Installed := True;
3666
3667 -- Reset renaming map for formal types. The mapping is established
3668 -- when analyzing the generic associations, but some mappings are
3669 -- inherited from formal packages of parent units, and these are
3670 -- constructed when the parents are installed.
3671
3672 Generic_Renamings.Set_Last (0);
3673 Generic_Renamings_HTable.Reset;
3674
3675 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3676 Gen_Unit := Entity (Gen_Id);
3677
3678 -- Verify that it is the name of a generic package
3679
3680 -- A visibility glitch: if the instance is a child unit and the generic
3681 -- is the generic unit of a parent instance (i.e. both the parent and
3682 -- the child units are instances of the same package) the name now
3683 -- denotes the renaming within the parent, not the intended generic
3684 -- unit. See if there is a homonym that is the desired generic. The
3685 -- renaming declaration must be visible inside the instance of the
3686 -- child, but not when analyzing the name in the instantiation itself.
3687
3688 if Ekind (Gen_Unit) = E_Package
3689 and then Present (Renamed_Entity (Gen_Unit))
3690 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3691 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3692 and then Present (Homonym (Gen_Unit))
3693 then
3694 Gen_Unit := Homonym (Gen_Unit);
3695 end if;
3696
3697 if Etype (Gen_Unit) = Any_Type then
3698 Restore_Env;
3699 goto Leave;
3700
3701 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3702
3703 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3704
3705 if From_Limited_With (Gen_Unit) then
3706 Error_Msg_N
3707 ("cannot instantiate a limited withed package", Gen_Id);
3708 else
3709 Error_Msg_NE
3710 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3711 end if;
3712
3713 Restore_Env;
3714 goto Leave;
3715 end if;
3716
3717 if In_Extended_Main_Source_Unit (N) then
3718 Set_Is_Instantiated (Gen_Unit);
3719 Generate_Reference (Gen_Unit, N);
3720
3721 if Present (Renamed_Object (Gen_Unit)) then
3722 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3723 Generate_Reference (Renamed_Object (Gen_Unit), N);
3724 end if;
3725 end if;
3726
3727 if Nkind (Gen_Id) = N_Identifier
3728 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3729 then
3730 Error_Msg_NE
3731 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3732
3733 elsif Nkind (Gen_Id) = N_Expanded_Name
3734 and then Is_Child_Unit (Gen_Unit)
3735 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3736 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3737 then
3738 Error_Msg_N
3739 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3740 end if;
3741
3742 Set_Entity (Gen_Id, Gen_Unit);
3743
3744 -- If generic is a renaming, get original generic unit
3745
3746 if Present (Renamed_Object (Gen_Unit))
3747 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3748 then
3749 Gen_Unit := Renamed_Object (Gen_Unit);
3750 end if;
3751
3752 -- Verify that there are no circular instantiations
3753
3754 if In_Open_Scopes (Gen_Unit) then
3755 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3756 Restore_Env;
3757 goto Leave;
3758
3759 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3760 Error_Msg_Node_2 := Current_Scope;
3761 Error_Msg_NE
3762 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3763 Circularity_Detected := True;
3764 Restore_Env;
3765 goto Leave;
3766
3767 else
3768 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3769
3770 -- Initialize renamings map, for error checking, and the list that
3771 -- holds private entities whose views have changed between generic
3772 -- definition and instantiation. If this is the instance created to
3773 -- validate an actual package, the instantiation environment is that
3774 -- of the enclosing instance.
3775
3776 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3777
3778 -- Copy original generic tree, to produce text for instantiation
3779
3780 Act_Tree :=
3781 Copy_Generic_Node
3782 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3783
3784 Act_Spec := Specification (Act_Tree);
3785
3786 -- If this is the instance created to validate an actual package,
3787 -- only the formals matter, do not examine the package spec itself.
3788
3789 if Is_Actual_Pack then
3790 Set_Visible_Declarations (Act_Spec, New_List);
3791 Set_Private_Declarations (Act_Spec, New_List);
3792 end if;
3793
3794 Renaming_List :=
3795 Analyze_Associations
3796 (I_Node => N,
3797 Formals => Generic_Formal_Declarations (Act_Tree),
3798 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3799
3800 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3801
3802 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3803 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3804 Set_Is_Generic_Instance (Act_Decl_Id);
3805 Set_Generic_Parent (Act_Spec, Gen_Unit);
3806
3807 -- References to the generic in its own declaration or its body are
3808 -- references to the instance. Add a renaming declaration for the
3809 -- generic unit itself. This declaration, as well as the renaming
3810 -- declarations for the generic formals, must remain private to the
3811 -- unit: the formals, because this is the language semantics, and
3812 -- the unit because its use is an artifact of the implementation.
3813
3814 Unit_Renaming :=
3815 Make_Package_Renaming_Declaration (Loc,
3816 Defining_Unit_Name =>
3817 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3818 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3819
3820 Append (Unit_Renaming, Renaming_List);
3821
3822 -- The renaming declarations are the first local declarations of the
3823 -- new unit.
3824
3825 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3826 Insert_List_Before
3827 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3828 else
3829 Set_Visible_Declarations (Act_Spec, Renaming_List);
3830 end if;
3831
3832 Act_Decl :=
3833 Make_Package_Declaration (Loc,
3834 Specification => Act_Spec);
3835
3836 -- Propagate the aspect specifications from the package declaration
3837 -- template to the instantiated version of the package declaration.
3838
3839 if Has_Aspects (Act_Tree) then
3840 Set_Aspect_Specifications (Act_Decl,
3841 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3842 end if;
3843
3844 -- Save the instantiation node, for subsequent instantiation of the
3845 -- body, if there is one and we are generating code for the current
3846 -- unit. Mark unit as having a body (avoids premature error message).
3847
3848 -- We instantiate the body if we are generating code, if we are
3849 -- generating cross-reference information, or if we are building
3850 -- trees for ASIS use or GNATprove use.
3851
3852 declare
3853 Enclosing_Body_Present : Boolean := False;
3854 -- If the generic unit is not a compilation unit, then a body may
3855 -- be present in its parent even if none is required. We create a
3856 -- tentative pending instantiation for the body, which will be
3857 -- discarded if none is actually present.
3858
3859 Scop : Entity_Id;
3860
3861 begin
3862 if Scope (Gen_Unit) /= Standard_Standard
3863 and then not Is_Child_Unit (Gen_Unit)
3864 then
3865 Scop := Scope (Gen_Unit);
3866
3867 while Present (Scop)
3868 and then Scop /= Standard_Standard
3869 loop
3870 if Unit_Requires_Body (Scop) then
3871 Enclosing_Body_Present := True;
3872 exit;
3873
3874 elsif In_Open_Scopes (Scop)
3875 and then In_Package_Body (Scop)
3876 then
3877 Enclosing_Body_Present := True;
3878 exit;
3879 end if;
3880
3881 exit when Is_Compilation_Unit (Scop);
3882 Scop := Scope (Scop);
3883 end loop;
3884 end if;
3885
3886 -- If front-end inlining is enabled, and this is a unit for which
3887 -- code will be generated, we instantiate the body at once.
3888
3889 -- This is done if the instance is not the main unit, and if the
3890 -- generic is not a child unit of another generic, to avoid scope
3891 -- problems and the reinstallation of parent instances.
3892
3893 if Expander_Active
3894 and then (not Is_Child_Unit (Gen_Unit)
3895 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3896 and then Might_Inline_Subp
3897 and then not Is_Actual_Pack
3898 then
3899 if not Back_End_Inlining
3900 and then Front_End_Inlining
3901 and then (Is_In_Main_Unit (N)
3902 or else In_Main_Context (Current_Scope))
3903 and then Nkind (Parent (N)) /= N_Compilation_Unit
3904 then
3905 Inline_Now := True;
3906
3907 elsif Back_End_Inlining
3908 and then Must_Inline_Subp
3909 and then (Is_In_Main_Unit (N)
3910 or else In_Main_Context (Current_Scope))
3911 and then Nkind (Parent (N)) /= N_Compilation_Unit
3912 then
3913 Inline_Now := True;
3914
3915 -- In configurable_run_time mode we force the inlining of
3916 -- predefined subprograms marked Inline_Always, to minimize
3917 -- the use of the run-time library.
3918
3919 elsif Is_Predefined_File_Name
3920 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3921 and then Configurable_Run_Time_Mode
3922 and then Nkind (Parent (N)) /= N_Compilation_Unit
3923 then
3924 Inline_Now := True;
3925 end if;
3926
3927 -- If the current scope is itself an instance within a child
3928 -- unit, there will be duplications in the scope stack, and the
3929 -- unstacking mechanism in Inline_Instance_Body will fail.
3930 -- This loses some rare cases of optimization, and might be
3931 -- improved some day, if we can find a proper abstraction for
3932 -- "the complete compilation context" that can be saved and
3933 -- restored. ???
3934
3935 if Is_Generic_Instance (Current_Scope) then
3936 declare
3937 Curr_Unit : constant Entity_Id :=
3938 Cunit_Entity (Current_Sem_Unit);
3939 begin
3940 if Curr_Unit /= Current_Scope
3941 and then Is_Child_Unit (Curr_Unit)
3942 then
3943 Inline_Now := False;
3944 end if;
3945 end;
3946 end if;
3947 end if;
3948
3949 Needs_Body :=
3950 (Unit_Requires_Body (Gen_Unit)
3951 or else Enclosing_Body_Present
3952 or else Present (Corresponding_Body (Gen_Decl)))
3953 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3954 and then not Is_Actual_Pack
3955 and then not Inline_Now
3956 and then (Operating_Mode = Generate_Code
3957
3958 -- Need comment for this check ???
3959
3960 or else (Operating_Mode = Check_Semantics
3961 and then (ASIS_Mode or GNATprove_Mode)));
3962
3963 -- If front_end_inlining is enabled, do not instantiate body if
3964 -- within a generic context.
3965
3966 if (Front_End_Inlining and then not Expander_Active)
3967 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3968 then
3969 Needs_Body := False;
3970 end if;
3971
3972 -- If the current context is generic, and the package being
3973 -- instantiated is declared within a formal package, there is no
3974 -- body to instantiate until the enclosing generic is instantiated
3975 -- and there is an actual for the formal package. If the formal
3976 -- package has parameters, we build a regular package instance for
3977 -- it, that precedes the original formal package declaration.
3978
3979 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3980 declare
3981 Decl : constant Node_Id :=
3982 Original_Node
3983 (Unit_Declaration_Node (Scope (Gen_Unit)));
3984 begin
3985 if Nkind (Decl) = N_Formal_Package_Declaration
3986 or else (Nkind (Decl) = N_Package_Declaration
3987 and then Is_List_Member (Decl)
3988 and then Present (Next (Decl))
3989 and then
3990 Nkind (Next (Decl)) =
3991 N_Formal_Package_Declaration)
3992 then
3993 Needs_Body := False;
3994 end if;
3995 end;
3996 end if;
3997 end;
3998
3999 -- For RCI unit calling stubs, we omit the instance body if the
4000 -- instance is the RCI library unit itself.
4001
4002 -- However there is a special case for nested instances: in this case
4003 -- we do generate the instance body, as it might be required, e.g.
4004 -- because it provides stream attributes for some type used in the
4005 -- profile of a remote subprogram. This is consistent with 12.3(12),
4006 -- which indicates that the instance body occurs at the place of the
4007 -- instantiation, and thus is part of the RCI declaration, which is
4008 -- present on all client partitions (this is E.2.3(18)).
4009
4010 -- Note that AI12-0002 may make it illegal at some point to have
4011 -- stream attributes defined in an RCI unit, in which case this
4012 -- special case will become unnecessary. In the meantime, there
4013 -- is known application code in production that depends on this
4014 -- being possible, so we definitely cannot eliminate the body in
4015 -- the case of nested instances for the time being.
4016
4017 -- When we generate a nested instance body, calling stubs for any
4018 -- relevant subprogram will be be inserted immediately after the
4019 -- subprogram declarations, and will take precedence over the
4020 -- subsequent (original) body. (The stub and original body will be
4021 -- complete homographs, but this is permitted in an instance).
4022 -- (Could we do better and remove the original body???)
4023
4024 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4025 and then Comes_From_Source (N)
4026 and then Nkind (Parent (N)) = N_Compilation_Unit
4027 then
4028 Needs_Body := False;
4029 end if;
4030
4031 if Needs_Body then
4032
4033 -- Here is a defence against a ludicrous number of instantiations
4034 -- caused by a circular set of instantiation attempts.
4035
4036 if Pending_Instantiations.Last > Maximum_Instantiations then
4037 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4038 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4039 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4040 raise Unrecoverable_Error;
4041 end if;
4042
4043 -- Indicate that the enclosing scopes contain an instantiation,
4044 -- and that cleanup actions should be delayed until after the
4045 -- instance body is expanded.
4046
4047 Check_Forward_Instantiation (Gen_Decl);
4048 if Nkind (N) = N_Package_Instantiation then
4049 declare
4050 Enclosing_Master : Entity_Id;
4051
4052 begin
4053 -- Loop to search enclosing masters
4054
4055 Enclosing_Master := Current_Scope;
4056 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4057 if Ekind (Enclosing_Master) = E_Package then
4058 if Is_Compilation_Unit (Enclosing_Master) then
4059 if In_Package_Body (Enclosing_Master) then
4060 Delay_Descriptors
4061 (Body_Entity (Enclosing_Master));
4062 else
4063 Delay_Descriptors
4064 (Enclosing_Master);
4065 end if;
4066
4067 exit Scope_Loop;
4068
4069 else
4070 Enclosing_Master := Scope (Enclosing_Master);
4071 end if;
4072
4073 elsif Is_Generic_Unit (Enclosing_Master)
4074 or else Ekind (Enclosing_Master) = E_Void
4075 then
4076 -- Cleanup actions will eventually be performed on the
4077 -- enclosing subprogram or package instance, if any.
4078 -- Enclosing scope is void in the formal part of a
4079 -- generic subprogram.
4080
4081 exit Scope_Loop;
4082
4083 else
4084 if Ekind (Enclosing_Master) = E_Entry
4085 and then
4086 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4087 then
4088 if not Expander_Active then
4089 exit Scope_Loop;
4090 else
4091 Enclosing_Master :=
4092 Protected_Body_Subprogram (Enclosing_Master);
4093 end if;
4094 end if;
4095
4096 Set_Delay_Cleanups (Enclosing_Master);
4097
4098 while Ekind (Enclosing_Master) = E_Block loop
4099 Enclosing_Master := Scope (Enclosing_Master);
4100 end loop;
4101
4102 if Is_Subprogram (Enclosing_Master) then
4103 Delay_Descriptors (Enclosing_Master);
4104
4105 elsif Is_Task_Type (Enclosing_Master) then
4106 declare
4107 TBP : constant Node_Id :=
4108 Get_Task_Body_Procedure
4109 (Enclosing_Master);
4110 begin
4111 if Present (TBP) then
4112 Delay_Descriptors (TBP);
4113 Set_Delay_Cleanups (TBP);
4114 end if;
4115 end;
4116 end if;
4117
4118 exit Scope_Loop;
4119 end if;
4120 end loop Scope_Loop;
4121 end;
4122
4123 -- Make entry in table
4124
4125 Pending_Instantiations.Append
4126 ((Inst_Node => N,
4127 Act_Decl => Act_Decl,
4128 Expander_Status => Expander_Active,
4129 Current_Sem_Unit => Current_Sem_Unit,
4130 Scope_Suppress => Scope_Suppress,
4131 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4132 Version => Ada_Version,
4133 Version_Pragma => Ada_Version_Pragma,
4134 Warnings => Save_Warnings,
4135 SPARK_Mode => SPARK_Mode,
4136 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4137 end if;
4138 end if;
4139
4140 Set_Categorization_From_Pragmas (Act_Decl);
4141
4142 if Parent_Installed then
4143 Hide_Current_Scope;
4144 end if;
4145
4146 Set_Instance_Spec (N, Act_Decl);
4147
4148 -- If not a compilation unit, insert the package declaration before
4149 -- the original instantiation node.
4150
4151 if Nkind (Parent (N)) /= N_Compilation_Unit then
4152 Mark_Rewrite_Insertion (Act_Decl);
4153 Insert_Before (N, Act_Decl);
4154 Analyze (Act_Decl);
4155
4156 -- For an instantiation that is a compilation unit, place
4157 -- declaration on current node so context is complete for analysis
4158 -- (including nested instantiations). If this is the main unit,
4159 -- the declaration eventually replaces the instantiation node.
4160 -- If the instance body is created later, it replaces the
4161 -- instance node, and the declaration is attached to it
4162 -- (see Build_Instance_Compilation_Unit_Nodes).
4163
4164 else
4165 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4166
4167 -- The entity for the current unit is the newly created one,
4168 -- and all semantic information is attached to it.
4169
4170 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4171
4172 -- If this is the main unit, replace the main entity as well
4173
4174 if Current_Sem_Unit = Main_Unit then
4175 Main_Unit_Entity := Act_Decl_Id;
4176 end if;
4177 end if;
4178
4179 Set_Unit (Parent (N), Act_Decl);
4180 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4181 Set_Package_Instantiation (Act_Decl_Id, N);
4182
4183 -- Process aspect specifications of the instance node, if any, to
4184 -- take into account categorization pragmas before analyzing the
4185 -- instance.
4186
4187 if Has_Aspects (N) then
4188 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4189 end if;
4190
4191 Analyze (Act_Decl);
4192 Set_Unit (Parent (N), N);
4193 Set_Body_Required (Parent (N), False);
4194
4195 -- We never need elaboration checks on instantiations, since by
4196 -- definition, the body instantiation is elaborated at the same
4197 -- time as the spec instantiation.
4198
4199 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4200 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4201 end if;
4202
4203 Check_Elab_Instantiation (N);
4204
4205 if ABE_Is_Certain (N) and then Needs_Body then
4206 Pending_Instantiations.Decrement_Last;
4207 end if;
4208
4209 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4210
4211 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4212 First_Private_Entity (Act_Decl_Id));
4213
4214 -- If the instantiation will receive a body, the unit will be
4215 -- transformed into a package body, and receive its own elaboration
4216 -- entity. Otherwise, the nature of the unit is now a package
4217 -- declaration.
4218
4219 if Nkind (Parent (N)) = N_Compilation_Unit
4220 and then not Needs_Body
4221 then
4222 Rewrite (N, Act_Decl);
4223 end if;
4224
4225 if Present (Corresponding_Body (Gen_Decl))
4226 or else Unit_Requires_Body (Gen_Unit)
4227 then
4228 Set_Has_Completion (Act_Decl_Id);
4229 end if;
4230
4231 Check_Formal_Packages (Act_Decl_Id);
4232
4233 Restore_Hidden_Primitives (Vis_Prims_List);
4234 Restore_Private_Views (Act_Decl_Id);
4235
4236 Inherit_Context (Gen_Decl, N);
4237
4238 if Parent_Installed then
4239 Remove_Parent;
4240 end if;
4241
4242 Restore_Env;
4243 Env_Installed := False;
4244 end if;
4245
4246 Validate_Categorization_Dependency (N, Act_Decl_Id);
4247
4248 -- There used to be a check here to prevent instantiations in local
4249 -- contexts if the No_Local_Allocators restriction was active. This
4250 -- check was removed by a binding interpretation in AI-95-00130/07,
4251 -- but we retain the code for documentation purposes.
4252
4253 -- if Ekind (Act_Decl_Id) /= E_Void
4254 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4255 -- then
4256 -- Check_Restriction (No_Local_Allocators, N);
4257 -- end if;
4258
4259 if Inline_Now then
4260 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4261 end if;
4262
4263 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4264 -- be used as defining identifiers for a formal package and for the
4265 -- corresponding expanded package.
4266
4267 if Nkind (N) = N_Formal_Package_Declaration then
4268 Act_Decl_Id := New_Copy (Defining_Entity (N));
4269 Set_Comes_From_Source (Act_Decl_Id, True);
4270 Set_Is_Generic_Instance (Act_Decl_Id, False);
4271 Set_Defining_Identifier (N, Act_Decl_Id);
4272 end if;
4273
4274 Style_Check := Save_Style_Check;
4275
4276 -- Check that if N is an instantiation of System.Dim_Float_IO or
4277 -- System.Dim_Integer_IO, the formal type has a dimension system.
4278
4279 if Nkind (N) = N_Package_Instantiation
4280 and then Is_Dim_IO_Package_Instantiation (N)
4281 then
4282 declare
4283 Assoc : constant Node_Id := First (Generic_Associations (N));
4284 begin
4285 if not Has_Dimension_System
4286 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4287 then
4288 Error_Msg_N ("type with a dimension system expected", Assoc);
4289 end if;
4290 end;
4291 end if;
4292
4293 <<Leave>>
4294 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4295 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4296 end if;
4297
4298 exception
4299 when Instantiation_Error =>
4300 if Parent_Installed then
4301 Remove_Parent;
4302 end if;
4303
4304 if Env_Installed then
4305 Restore_Env;
4306 end if;
4307
4308 Style_Check := Save_Style_Check;
4309 end Analyze_Package_Instantiation;
4310
4311 --------------------------
4312 -- Inline_Instance_Body --
4313 --------------------------
4314
4315 procedure Inline_Instance_Body
4316 (N : Node_Id;
4317 Gen_Unit : Entity_Id;
4318 Act_Decl : Node_Id)
4319 is
4320 Vis : Boolean;
4321 Gen_Comp : constant Entity_Id :=
4322 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4323 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4324 Curr_Scope : Entity_Id := Empty;
4325 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4326 Removed : Boolean := False;
4327 Num_Scopes : Int := 0;
4328
4329 Scope_Stack_Depth : constant Int :=
4330 Scope_Stack.Last - Scope_Stack.First + 1;
4331
4332 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4333 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4334 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4335 List : Elist_Id;
4336 Num_Inner : Int := 0;
4337 N_Instances : Int := 0;
4338 S : Entity_Id;
4339
4340 begin
4341 -- Case of generic unit defined in another unit. We must remove the
4342 -- complete context of the current unit to install that of the generic.
4343
4344 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4345
4346 -- Add some comments for the following two loops ???
4347
4348 S := Current_Scope;
4349 while Present (S) and then S /= Standard_Standard loop
4350 loop
4351 Num_Scopes := Num_Scopes + 1;
4352
4353 Use_Clauses (Num_Scopes) :=
4354 (Scope_Stack.Table
4355 (Scope_Stack.Last - Num_Scopes + 1).
4356 First_Use_Clause);
4357 End_Use_Clauses (Use_Clauses (Num_Scopes));
4358
4359 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4360 or else Scope_Stack.Table
4361 (Scope_Stack.Last - Num_Scopes).Entity
4362 = Scope (S);
4363 end loop;
4364
4365 exit when Is_Generic_Instance (S)
4366 and then (In_Package_Body (S)
4367 or else Ekind (S) = E_Procedure
4368 or else Ekind (S) = E_Function);
4369 S := Scope (S);
4370 end loop;
4371
4372 Vis := Is_Immediately_Visible (Gen_Comp);
4373
4374 -- Find and save all enclosing instances
4375
4376 S := Current_Scope;
4377
4378 while Present (S)
4379 and then S /= Standard_Standard
4380 loop
4381 if Is_Generic_Instance (S) then
4382 N_Instances := N_Instances + 1;
4383 Instances (N_Instances) := S;
4384
4385 exit when In_Package_Body (S);
4386 end if;
4387
4388 S := Scope (S);
4389 end loop;
4390
4391 -- Remove context of current compilation unit, unless we are within a
4392 -- nested package instantiation, in which case the context has been
4393 -- removed previously.
4394
4395 -- If current scope is the body of a child unit, remove context of
4396 -- spec as well. If an enclosing scope is an instance body, the
4397 -- context has already been removed, but the entities in the body
4398 -- must be made invisible as well.
4399
4400 S := Current_Scope;
4401
4402 while Present (S)
4403 and then S /= Standard_Standard
4404 loop
4405 if Is_Generic_Instance (S)
4406 and then (In_Package_Body (S)
4407 or else Ekind (S) = E_Procedure
4408 or else Ekind (S) = E_Function)
4409 then
4410 -- We still have to remove the entities of the enclosing
4411 -- instance from direct visibility.
4412
4413 declare
4414 E : Entity_Id;
4415 begin
4416 E := First_Entity (S);
4417 while Present (E) loop
4418 Set_Is_Immediately_Visible (E, False);
4419 Next_Entity (E);
4420 end loop;
4421 end;
4422
4423 exit;
4424 end if;
4425
4426 if S = Curr_Unit
4427 or else (Ekind (Curr_Unit) = E_Package_Body
4428 and then S = Spec_Entity (Curr_Unit))
4429 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4430 and then S =
4431 Corresponding_Spec
4432 (Unit_Declaration_Node (Curr_Unit)))
4433 then
4434 Removed := True;
4435
4436 -- Remove entities in current scopes from visibility, so that
4437 -- instance body is compiled in a clean environment.
4438
4439 List := Save_Scope_Stack (Handle_Use => False);
4440
4441 if Is_Child_Unit (S) then
4442
4443 -- Remove child unit from stack, as well as inner scopes.
4444 -- Removing the context of a child unit removes parent units
4445 -- as well.
4446
4447 while Current_Scope /= S loop
4448 Num_Inner := Num_Inner + 1;
4449 Inner_Scopes (Num_Inner) := Current_Scope;
4450 Pop_Scope;
4451 end loop;
4452
4453 Pop_Scope;
4454 Remove_Context (Curr_Comp);
4455 Curr_Scope := S;
4456
4457 else
4458 Remove_Context (Curr_Comp);
4459 end if;
4460
4461 if Ekind (Curr_Unit) = E_Package_Body then
4462 Remove_Context (Library_Unit (Curr_Comp));
4463 end if;
4464 end if;
4465
4466 S := Scope (S);
4467 end loop;
4468 pragma Assert (Num_Inner < Num_Scopes);
4469
4470 Push_Scope (Standard_Standard);
4471 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4472 Instantiate_Package_Body
4473 (Body_Info =>
4474 ((Inst_Node => N,
4475 Act_Decl => Act_Decl,
4476 Expander_Status => Expander_Active,
4477 Current_Sem_Unit => Current_Sem_Unit,
4478 Scope_Suppress => Scope_Suppress,
4479 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4480 Version => Ada_Version,
4481 Version_Pragma => Ada_Version_Pragma,
4482 Warnings => Save_Warnings,
4483 SPARK_Mode => SPARK_Mode,
4484 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4485 Inlined_Body => True);
4486
4487 Pop_Scope;
4488
4489 -- Restore context
4490
4491 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4492
4493 -- Reset Generic_Instance flag so that use clauses can be installed
4494 -- in the proper order. (See Use_One_Package for effect of enclosing
4495 -- instances on processing of use clauses).
4496
4497 for J in 1 .. N_Instances loop
4498 Set_Is_Generic_Instance (Instances (J), False);
4499 end loop;
4500
4501 if Removed then
4502 Install_Context (Curr_Comp);
4503
4504 if Present (Curr_Scope)
4505 and then Is_Child_Unit (Curr_Scope)
4506 then
4507 Push_Scope (Curr_Scope);
4508 Set_Is_Immediately_Visible (Curr_Scope);
4509
4510 -- Finally, restore inner scopes as well
4511
4512 for J in reverse 1 .. Num_Inner loop
4513 Push_Scope (Inner_Scopes (J));
4514 end loop;
4515 end if;
4516
4517 Restore_Scope_Stack (List, Handle_Use => False);
4518
4519 if Present (Curr_Scope)
4520 and then
4521 (In_Private_Part (Curr_Scope)
4522 or else In_Package_Body (Curr_Scope))
4523 then
4524 -- Install private declaration of ancestor units, which are
4525 -- currently available. Restore_Scope_Stack and Install_Context
4526 -- only install the visible part of parents.
4527
4528 declare
4529 Par : Entity_Id;
4530 begin
4531 Par := Scope (Curr_Scope);
4532 while (Present (Par))
4533 and then Par /= Standard_Standard
4534 loop
4535 Install_Private_Declarations (Par);
4536 Par := Scope (Par);
4537 end loop;
4538 end;
4539 end if;
4540 end if;
4541
4542 -- Restore use clauses. For a child unit, use clauses in the parents
4543 -- are restored when installing the context, so only those in inner
4544 -- scopes (and those local to the child unit itself) need to be
4545 -- installed explicitly.
4546
4547 if Is_Child_Unit (Curr_Unit)
4548 and then Removed
4549 then
4550 for J in reverse 1 .. Num_Inner + 1 loop
4551 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4552 Use_Clauses (J);
4553 Install_Use_Clauses (Use_Clauses (J));
4554 end loop;
4555
4556 else
4557 for J in reverse 1 .. Num_Scopes loop
4558 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4559 Use_Clauses (J);
4560 Install_Use_Clauses (Use_Clauses (J));
4561 end loop;
4562 end if;
4563
4564 -- Restore status of instances. If one of them is a body, make its
4565 -- local entities visible again.
4566
4567 declare
4568 E : Entity_Id;
4569 Inst : Entity_Id;
4570
4571 begin
4572 for J in 1 .. N_Instances loop
4573 Inst := Instances (J);
4574 Set_Is_Generic_Instance (Inst, True);
4575
4576 if In_Package_Body (Inst)
4577 or else Ekind (S) = E_Procedure
4578 or else Ekind (S) = E_Function
4579 then
4580 E := First_Entity (Instances (J));
4581 while Present (E) loop
4582 Set_Is_Immediately_Visible (E);
4583 Next_Entity (E);
4584 end loop;
4585 end if;
4586 end loop;
4587 end;
4588
4589 -- If generic unit is in current unit, current context is correct
4590
4591 else
4592 Instantiate_Package_Body
4593 (Body_Info =>
4594 ((Inst_Node => N,
4595 Act_Decl => Act_Decl,
4596 Expander_Status => Expander_Active,
4597 Current_Sem_Unit => Current_Sem_Unit,
4598 Scope_Suppress => Scope_Suppress,
4599 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4600 Version => Ada_Version,
4601 Version_Pragma => Ada_Version_Pragma,
4602 Warnings => Save_Warnings,
4603 SPARK_Mode => SPARK_Mode,
4604 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4605 Inlined_Body => True);
4606 end if;
4607 end Inline_Instance_Body;
4608
4609 -------------------------------------
4610 -- Analyze_Procedure_Instantiation --
4611 -------------------------------------
4612
4613 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4614 begin
4615 Analyze_Subprogram_Instantiation (N, E_Procedure);
4616 end Analyze_Procedure_Instantiation;
4617
4618 -----------------------------------
4619 -- Need_Subprogram_Instance_Body --
4620 -----------------------------------
4621
4622 function Need_Subprogram_Instance_Body
4623 (N : Node_Id;
4624 Subp : Entity_Id) return Boolean
4625 is
4626 begin
4627 -- Must be inlined (or inlined renaming)
4628
4629 if (Is_In_Main_Unit (N)
4630 or else Is_Inlined (Subp)
4631 or else Is_Inlined (Alias (Subp)))
4632
4633 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4634
4635 and then (Operating_Mode = Generate_Code
4636 or else (Operating_Mode = Check_Semantics
4637 and then (ASIS_Mode or GNATprove_Mode)))
4638
4639 -- The body is needed when generating code (full expansion), in ASIS
4640 -- mode for other tools, and in GNATprove mode (special expansion) for
4641 -- formal verification of the body itself.
4642
4643 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4644
4645 -- No point in inlining if ABE is inevitable
4646
4647 and then not ABE_Is_Certain (N)
4648
4649 -- Or if subprogram is eliminated
4650
4651 and then not Is_Eliminated (Subp)
4652 then
4653 Pending_Instantiations.Append
4654 ((Inst_Node => N,
4655 Act_Decl => Unit_Declaration_Node (Subp),
4656 Expander_Status => Expander_Active,
4657 Current_Sem_Unit => Current_Sem_Unit,
4658 Scope_Suppress => Scope_Suppress,
4659 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4660 Version => Ada_Version,
4661 Version_Pragma => Ada_Version_Pragma,
4662 Warnings => Save_Warnings,
4663 SPARK_Mode => SPARK_Mode,
4664 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4665 return True;
4666
4667 -- Here if not inlined, or we ignore the inlining
4668
4669 else
4670 return False;
4671 end if;
4672 end Need_Subprogram_Instance_Body;
4673
4674 --------------------------------------
4675 -- Analyze_Subprogram_Instantiation --
4676 --------------------------------------
4677
4678 procedure Analyze_Subprogram_Instantiation
4679 (N : Node_Id;
4680 K : Entity_Kind)
4681 is
4682 Loc : constant Source_Ptr := Sloc (N);
4683 Gen_Id : constant Node_Id := Name (N);
4684
4685 Anon_Id : constant Entity_Id :=
4686 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4687 Chars => New_External_Name
4688 (Chars (Defining_Entity (N)), 'R'));
4689
4690 Act_Decl_Id : Entity_Id;
4691 Act_Decl : Node_Id;
4692 Act_Spec : Node_Id;
4693 Act_Tree : Node_Id;
4694
4695 Env_Installed : Boolean := False;
4696 Gen_Unit : Entity_Id;
4697 Gen_Decl : Node_Id;
4698 Pack_Id : Entity_Id;
4699 Parent_Installed : Boolean := False;
4700 Renaming_List : List_Id;
4701
4702 procedure Analyze_Instance_And_Renamings;
4703 -- The instance must be analyzed in a context that includes the mappings
4704 -- of generic parameters into actuals. We create a package declaration
4705 -- for this purpose, and a subprogram with an internal name within the
4706 -- package. The subprogram instance is simply an alias for the internal
4707 -- subprogram, declared in the current scope.
4708
4709 ------------------------------------
4710 -- Analyze_Instance_And_Renamings --
4711 ------------------------------------
4712
4713 procedure Analyze_Instance_And_Renamings is
4714 Def_Ent : constant Entity_Id := Defining_Entity (N);
4715 Pack_Decl : Node_Id;
4716
4717 begin
4718 if Nkind (Parent (N)) = N_Compilation_Unit then
4719
4720 -- For the case of a compilation unit, the container package has
4721 -- the same name as the instantiation, to insure that the binder
4722 -- calls the elaboration procedure with the right name. Copy the
4723 -- entity of the instance, which may have compilation level flags
4724 -- (e.g. Is_Child_Unit) set.
4725
4726 Pack_Id := New_Copy (Def_Ent);
4727
4728 else
4729 -- Otherwise we use the name of the instantiation concatenated
4730 -- with its source position to ensure uniqueness if there are
4731 -- several instantiations with the same name.
4732
4733 Pack_Id :=
4734 Make_Defining_Identifier (Loc,
4735 Chars => New_External_Name
4736 (Related_Id => Chars (Def_Ent),
4737 Suffix => "GP",
4738 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4739 end if;
4740
4741 Pack_Decl := Make_Package_Declaration (Loc,
4742 Specification => Make_Package_Specification (Loc,
4743 Defining_Unit_Name => Pack_Id,
4744 Visible_Declarations => Renaming_List,
4745 End_Label => Empty));
4746
4747 Set_Instance_Spec (N, Pack_Decl);
4748 Set_Is_Generic_Instance (Pack_Id);
4749 Set_Debug_Info_Needed (Pack_Id);
4750
4751 -- Case of not a compilation unit
4752
4753 if Nkind (Parent (N)) /= N_Compilation_Unit then
4754 Mark_Rewrite_Insertion (Pack_Decl);
4755 Insert_Before (N, Pack_Decl);
4756 Set_Has_Completion (Pack_Id);
4757
4758 -- Case of an instantiation that is a compilation unit
4759
4760 -- Place declaration on current node so context is complete for
4761 -- analysis (including nested instantiations), and for use in a
4762 -- context_clause (see Analyze_With_Clause).
4763
4764 else
4765 Set_Unit (Parent (N), Pack_Decl);
4766 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4767 end if;
4768
4769 Analyze (Pack_Decl);
4770 Check_Formal_Packages (Pack_Id);
4771 Set_Is_Generic_Instance (Pack_Id, False);
4772
4773 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4774 -- above???
4775
4776 -- Body of the enclosing package is supplied when instantiating the
4777 -- subprogram body, after semantic analysis is completed.
4778
4779 if Nkind (Parent (N)) = N_Compilation_Unit then
4780
4781 -- Remove package itself from visibility, so it does not
4782 -- conflict with subprogram.
4783
4784 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4785
4786 -- Set name and scope of internal subprogram so that the proper
4787 -- external name will be generated. The proper scope is the scope
4788 -- of the wrapper package. We need to generate debugging info for
4789 -- the internal subprogram, so set flag accordingly.
4790
4791 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4792 Set_Scope (Anon_Id, Scope (Pack_Id));
4793
4794 -- Mark wrapper package as referenced, to avoid spurious warnings
4795 -- if the instantiation appears in various with_ clauses of
4796 -- subunits of the main unit.
4797
4798 Set_Referenced (Pack_Id);
4799 end if;
4800
4801 Set_Is_Generic_Instance (Anon_Id);
4802 Set_Debug_Info_Needed (Anon_Id);
4803 Act_Decl_Id := New_Copy (Anon_Id);
4804
4805 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4806 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4807 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4808 Set_Comes_From_Source (Act_Decl_Id, True);
4809
4810 -- The signature may involve types that are not frozen yet, but the
4811 -- subprogram will be frozen at the point the wrapper package is
4812 -- frozen, so it does not need its own freeze node. In fact, if one
4813 -- is created, it might conflict with the freezing actions from the
4814 -- wrapper package.
4815
4816 Set_Has_Delayed_Freeze (Anon_Id, False);
4817
4818 -- If the instance is a child unit, mark the Id accordingly. Mark
4819 -- the anonymous entity as well, which is the real subprogram and
4820 -- which is used when the instance appears in a context clause.
4821 -- Similarly, propagate the Is_Eliminated flag to handle properly
4822 -- nested eliminated subprograms.
4823
4824 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4825 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4826 New_Overloaded_Entity (Act_Decl_Id);
4827 Check_Eliminated (Act_Decl_Id);
4828 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4829
4830 -- In compilation unit case, kill elaboration checks on the
4831 -- instantiation, since they are never needed -- the body is
4832 -- instantiated at the same point as the spec.
4833
4834 if Nkind (Parent (N)) = N_Compilation_Unit then
4835 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4836 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4837 Set_Is_Compilation_Unit (Anon_Id);
4838
4839 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4840 end if;
4841
4842 -- The instance is not a freezing point for the new subprogram
4843
4844 Set_Is_Frozen (Act_Decl_Id, False);
4845
4846 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4847 Valid_Operator_Definition (Act_Decl_Id);
4848 end if;
4849
4850 Set_Alias (Act_Decl_Id, Anon_Id);
4851 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4852 Set_Has_Completion (Act_Decl_Id);
4853 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4854
4855 if Nkind (Parent (N)) = N_Compilation_Unit then
4856 Set_Body_Required (Parent (N), False);
4857 end if;
4858 end Analyze_Instance_And_Renamings;
4859
4860 -- Local variables
4861
4862 Vis_Prims_List : Elist_Id := No_Elist;
4863 -- List of primitives made temporarily visible in the instantiation
4864 -- to match the visibility of the formal type
4865
4866 -- Start of processing for Analyze_Subprogram_Instantiation
4867
4868 begin
4869 Check_SPARK_Restriction ("generic is not allowed", N);
4870
4871 -- Very first thing: check for special Text_IO unit in case we are
4872 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
4873 -- such an instantiation is bogus (these are packages, not subprograms),
4874 -- but we get a better error message if we do this.
4875
4876 Check_Text_IO_Special_Unit (Gen_Id);
4877
4878 -- Make node global for error reporting
4879
4880 Instantiation_Node := N;
4881
4882 -- For package instantiations we turn off style checks, because they
4883 -- will have been emitted in the generic. For subprogram instantiations
4884 -- we want to apply at least the check on overriding indicators so we
4885 -- do not modify the style check status.
4886
4887 -- The renaming declarations for the actuals do not come from source and
4888 -- will not generate spurious warnings.
4889
4890 Preanalyze_Actuals (N);
4891
4892 Init_Env;
4893 Env_Installed := True;
4894 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4895 Gen_Unit := Entity (Gen_Id);
4896
4897 Generate_Reference (Gen_Unit, Gen_Id);
4898
4899 if Nkind (Gen_Id) = N_Identifier
4900 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4901 then
4902 Error_Msg_NE
4903 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4904 end if;
4905
4906 if Etype (Gen_Unit) = Any_Type then
4907 Restore_Env;
4908 return;
4909 end if;
4910
4911 -- Verify that it is a generic subprogram of the right kind, and that
4912 -- it does not lead to a circular instantiation.
4913
4914 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
4915 Error_Msg_NE
4916 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
4917
4918 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
4919 Error_Msg_NE
4920 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
4921
4922 elsif In_Open_Scopes (Gen_Unit) then
4923 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4924
4925 else
4926 Set_Entity (Gen_Id, Gen_Unit);
4927 Set_Is_Instantiated (Gen_Unit);
4928
4929 if In_Extended_Main_Source_Unit (N) then
4930 Generate_Reference (Gen_Unit, N);
4931 end if;
4932
4933 -- If renaming, get original unit
4934
4935 if Present (Renamed_Object (Gen_Unit))
4936 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4937 or else
4938 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4939 then
4940 Gen_Unit := Renamed_Object (Gen_Unit);
4941 Set_Is_Instantiated (Gen_Unit);
4942 Generate_Reference (Gen_Unit, N);
4943 end if;
4944
4945 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4946 Error_Msg_Node_2 := Current_Scope;
4947 Error_Msg_NE
4948 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4949 Circularity_Detected := True;
4950 Restore_Hidden_Primitives (Vis_Prims_List);
4951 goto Leave;
4952 end if;
4953
4954 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4955
4956 -- Initialize renamings map, for error checking
4957
4958 Generic_Renamings.Set_Last (0);
4959 Generic_Renamings_HTable.Reset;
4960
4961 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4962
4963 -- Copy original generic tree, to produce text for instantiation
4964
4965 Act_Tree :=
4966 Copy_Generic_Node
4967 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4968
4969 -- Inherit overriding indicator from instance node
4970
4971 Act_Spec := Specification (Act_Tree);
4972 Set_Must_Override (Act_Spec, Must_Override (N));
4973 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4974
4975 Renaming_List :=
4976 Analyze_Associations
4977 (I_Node => N,
4978 Formals => Generic_Formal_Declarations (Act_Tree),
4979 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4980
4981 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4982
4983 -- The subprogram itself cannot contain a nested instance, so the
4984 -- current parent is left empty.
4985
4986 Set_Instance_Env (Gen_Unit, Empty);
4987
4988 -- Build the subprogram declaration, which does not appear in the
4989 -- generic template, and give it a sloc consistent with that of the
4990 -- template.
4991
4992 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4993 Set_Generic_Parent (Act_Spec, Gen_Unit);
4994 Act_Decl :=
4995 Make_Subprogram_Declaration (Sloc (Act_Spec),
4996 Specification => Act_Spec);
4997
4998 -- The aspects have been copied previously, but they have to be
4999 -- linked explicitly to the new subprogram declaration. Explicit
5000 -- pre/postconditions on the instance are analyzed below, in a
5001 -- separate step.
5002
5003 Move_Aspects (Act_Tree, To => Act_Decl);
5004 Set_Categorization_From_Pragmas (Act_Decl);
5005
5006 if Parent_Installed then
5007 Hide_Current_Scope;
5008 end if;
5009
5010 Append (Act_Decl, Renaming_List);
5011 Analyze_Instance_And_Renamings;
5012
5013 -- If the generic is marked Import (Intrinsic), then so is the
5014 -- instance. This indicates that there is no body to instantiate. If
5015 -- generic is marked inline, so it the instance, and the anonymous
5016 -- subprogram it renames. If inlined, or else if inlining is enabled
5017 -- for the compilation, we generate the instance body even if it is
5018 -- not within the main unit.
5019
5020 if Is_Intrinsic_Subprogram (Gen_Unit) then
5021 Set_Is_Intrinsic_Subprogram (Anon_Id);
5022 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5023
5024 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5025 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5026 end if;
5027 end if;
5028
5029 -- Inherit convention from generic unit. Intrinsic convention, as for
5030 -- an instance of unchecked conversion, is not inherited because an
5031 -- explicit Ada instance has been created.
5032
5033 if Has_Convention_Pragma (Gen_Unit)
5034 and then Convention (Gen_Unit) /= Convention_Intrinsic
5035 then
5036 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5037 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5038 end if;
5039
5040 Generate_Definition (Act_Decl_Id);
5041 -- Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id)));
5042 -- ??? needed?
5043 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
5044
5045 -- Inherit all inlining-related flags which apply to the generic in
5046 -- the subprogram and its declaration.
5047
5048 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5049 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5050
5051 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5052 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5053
5054 Set_Has_Pragma_Inline_Always
5055 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5056 Set_Has_Pragma_Inline_Always
5057 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5058
5059 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5060 Check_Elab_Instantiation (N);
5061 end if;
5062
5063 if Is_Dispatching_Operation (Act_Decl_Id)
5064 and then Ada_Version >= Ada_2005
5065 then
5066 declare
5067 Formal : Entity_Id;
5068
5069 begin
5070 Formal := First_Formal (Act_Decl_Id);
5071 while Present (Formal) loop
5072 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5073 and then Is_Controlling_Formal (Formal)
5074 and then not Can_Never_Be_Null (Formal)
5075 then
5076 Error_Msg_NE ("access parameter& is controlling,",
5077 N, Formal);
5078 Error_Msg_NE
5079 ("\corresponding parameter of & must be"
5080 & " explicitly null-excluding", N, Gen_Id);
5081 end if;
5082
5083 Next_Formal (Formal);
5084 end loop;
5085 end;
5086 end if;
5087
5088 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5089
5090 Validate_Categorization_Dependency (N, Act_Decl_Id);
5091
5092 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5093 Inherit_Context (Gen_Decl, N);
5094
5095 Restore_Private_Views (Pack_Id, False);
5096
5097 -- If the context requires a full instantiation, mark node for
5098 -- subsequent construction of the body.
5099
5100 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5101 Check_Forward_Instantiation (Gen_Decl);
5102
5103 -- The wrapper package is always delayed, because it does not
5104 -- constitute a freeze point, but to insure that the freeze
5105 -- node is placed properly, it is created directly when
5106 -- instantiating the body (otherwise the freeze node might
5107 -- appear to early for nested instantiations).
5108
5109 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5110
5111 -- For ASIS purposes, indicate that the wrapper package has
5112 -- replaced the instantiation node.
5113
5114 Rewrite (N, Unit (Parent (N)));
5115 Set_Unit (Parent (N), N);
5116 end if;
5117
5118 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5119
5120 -- Replace instance node for library-level instantiations of
5121 -- intrinsic subprograms, for ASIS use.
5122
5123 Rewrite (N, Unit (Parent (N)));
5124 Set_Unit (Parent (N), N);
5125 end if;
5126
5127 if Parent_Installed then
5128 Remove_Parent;
5129 end if;
5130
5131 Restore_Hidden_Primitives (Vis_Prims_List);
5132 Restore_Env;
5133 Env_Installed := False;
5134 Generic_Renamings.Set_Last (0);
5135 Generic_Renamings_HTable.Reset;
5136 end if;
5137
5138 <<Leave>>
5139 if Has_Aspects (N) then
5140 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5141 end if;
5142
5143 exception
5144 when Instantiation_Error =>
5145 if Parent_Installed then
5146 Remove_Parent;
5147 end if;
5148
5149 if Env_Installed then
5150 Restore_Env;
5151 end if;
5152 end Analyze_Subprogram_Instantiation;
5153
5154 -------------------------
5155 -- Get_Associated_Node --
5156 -------------------------
5157
5158 function Get_Associated_Node (N : Node_Id) return Node_Id is
5159 Assoc : Node_Id;
5160
5161 begin
5162 Assoc := Associated_Node (N);
5163
5164 if Nkind (Assoc) /= Nkind (N) then
5165 return Assoc;
5166
5167 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5168 return Assoc;
5169
5170 else
5171 -- If the node is part of an inner generic, it may itself have been
5172 -- remapped into a further generic copy. Associated_Node is otherwise
5173 -- used for the entity of the node, and will be of a different node
5174 -- kind, or else N has been rewritten as a literal or function call.
5175
5176 while Present (Associated_Node (Assoc))
5177 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5178 loop
5179 Assoc := Associated_Node (Assoc);
5180 end loop;
5181
5182 -- Follow and additional link in case the final node was rewritten.
5183 -- This can only happen with nested generic units.
5184
5185 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5186 and then Present (Associated_Node (Assoc))
5187 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5188 N_Explicit_Dereference,
5189 N_Integer_Literal,
5190 N_Real_Literal,
5191 N_String_Literal))
5192 then
5193 Assoc := Associated_Node (Assoc);
5194 end if;
5195
5196 -- An additional special case: an unconstrained type in an object
5197 -- declaration may have been rewritten as a local subtype constrained
5198 -- by the expression in the declaration. We need to recover the
5199 -- original entity which may be global.
5200
5201 if Present (Original_Node (Assoc))
5202 and then Nkind (Parent (N)) = N_Object_Declaration
5203 then
5204 Assoc := Original_Node (Assoc);
5205 end if;
5206
5207 return Assoc;
5208 end if;
5209 end Get_Associated_Node;
5210
5211 -------------------------------------------
5212 -- Build_Instance_Compilation_Unit_Nodes --
5213 -------------------------------------------
5214
5215 procedure Build_Instance_Compilation_Unit_Nodes
5216 (N : Node_Id;
5217 Act_Body : Node_Id;
5218 Act_Decl : Node_Id)
5219 is
5220 Decl_Cunit : Node_Id;
5221 Body_Cunit : Node_Id;
5222 Citem : Node_Id;
5223 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5224 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5225
5226 begin
5227 -- A new compilation unit node is built for the instance declaration
5228
5229 Decl_Cunit :=
5230 Make_Compilation_Unit (Sloc (N),
5231 Context_Items => Empty_List,
5232 Unit => Act_Decl,
5233 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5234
5235 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5236
5237 -- The new compilation unit is linked to its body, but both share the
5238 -- same file, so we do not set Body_Required on the new unit so as not
5239 -- to create a spurious dependency on a non-existent body in the ali.
5240 -- This simplifies CodePeer unit traversal.
5241
5242 -- We use the original instantiation compilation unit as the resulting
5243 -- compilation unit of the instance, since this is the main unit.
5244
5245 Rewrite (N, Act_Body);
5246
5247 -- Propagate the aspect specifications from the package body template to
5248 -- the instantiated version of the package body.
5249
5250 if Has_Aspects (Act_Body) then
5251 Set_Aspect_Specifications
5252 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5253 end if;
5254
5255 Body_Cunit := Parent (N);
5256
5257 -- The two compilation unit nodes are linked by the Library_Unit field
5258
5259 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5260 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5261
5262 -- Preserve the private nature of the package if needed
5263
5264 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5265
5266 -- If the instance is not the main unit, its context, categorization
5267 -- and elaboration entity are not relevant to the compilation.
5268
5269 if Body_Cunit /= Cunit (Main_Unit) then
5270 Make_Instance_Unit (Body_Cunit, In_Main => False);
5271 return;
5272 end if;
5273
5274 -- The context clause items on the instantiation, which are now attached
5275 -- to the body compilation unit (since the body overwrote the original
5276 -- instantiation node), semantically belong on the spec, so copy them
5277 -- there. It's harmless to leave them on the body as well. In fact one
5278 -- could argue that they belong in both places.
5279
5280 Citem := First (Context_Items (Body_Cunit));
5281 while Present (Citem) loop
5282 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5283 Next (Citem);
5284 end loop;
5285
5286 -- Propagate categorization flags on packages, so that they appear in
5287 -- the ali file for the spec of the unit.
5288
5289 if Ekind (New_Main) = E_Package then
5290 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5291 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5292 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5293 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5294 Set_Is_Remote_Call_Interface
5295 (Old_Main, Is_Remote_Call_Interface (New_Main));
5296 end if;
5297
5298 -- Make entry in Units table, so that binder can generate call to
5299 -- elaboration procedure for body, if any.
5300
5301 Make_Instance_Unit (Body_Cunit, In_Main => True);
5302 Main_Unit_Entity := New_Main;
5303 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5304
5305 -- Build elaboration entity, since the instance may certainly generate
5306 -- elaboration code requiring a flag for protection.
5307
5308 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5309 end Build_Instance_Compilation_Unit_Nodes;
5310
5311 -----------------------------
5312 -- Check_Access_Definition --
5313 -----------------------------
5314
5315 procedure Check_Access_Definition (N : Node_Id) is
5316 begin
5317 pragma Assert
5318 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5319 null;
5320 end Check_Access_Definition;
5321
5322 -----------------------------------
5323 -- Check_Formal_Package_Instance --
5324 -----------------------------------
5325
5326 -- If the formal has specific parameters, they must match those of the
5327 -- actual. Both of them are instances, and the renaming declarations for
5328 -- their formal parameters appear in the same order in both. The analyzed
5329 -- formal has been analyzed in the context of the current instance.
5330
5331 procedure Check_Formal_Package_Instance
5332 (Formal_Pack : Entity_Id;
5333 Actual_Pack : Entity_Id)
5334 is
5335 E1 : Entity_Id := First_Entity (Actual_Pack);
5336 E2 : Entity_Id := First_Entity (Formal_Pack);
5337
5338 Expr1 : Node_Id;
5339 Expr2 : Node_Id;
5340
5341 procedure Check_Mismatch (B : Boolean);
5342 -- Common error routine for mismatch between the parameters of the
5343 -- actual instance and those of the formal package.
5344
5345 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5346 -- The formal may come from a nested formal package, and the actual may
5347 -- have been constant-folded. To determine whether the two denote the
5348 -- same entity we may have to traverse several definitions to recover
5349 -- the ultimate entity that they refer to.
5350
5351 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5352 -- Similarly, if the formal comes from a nested formal package, the
5353 -- actual may designate the formal through multiple renamings, which
5354 -- have to be followed to determine the original variable in question.
5355
5356 --------------------
5357 -- Check_Mismatch --
5358 --------------------
5359
5360 procedure Check_Mismatch (B : Boolean) is
5361 Kind : constant Node_Kind := Nkind (Parent (E2));
5362
5363 begin
5364 if Kind = N_Formal_Type_Declaration then
5365 return;
5366
5367 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5368 N_Formal_Package_Declaration)
5369 or else Kind in N_Formal_Subprogram_Declaration
5370 then
5371 null;
5372
5373 elsif B then
5374 Error_Msg_NE
5375 ("actual for & in actual instance does not match formal",
5376 Parent (Actual_Pack), E1);
5377 end if;
5378 end Check_Mismatch;
5379
5380 --------------------------------
5381 -- Same_Instantiated_Constant --
5382 --------------------------------
5383
5384 function Same_Instantiated_Constant
5385 (E1, E2 : Entity_Id) return Boolean
5386 is
5387 Ent : Entity_Id;
5388
5389 begin
5390 Ent := E2;
5391 while Present (Ent) loop
5392 if E1 = Ent then
5393 return True;
5394
5395 elsif Ekind (Ent) /= E_Constant then
5396 return False;
5397
5398 elsif Is_Entity_Name (Constant_Value (Ent)) then
5399 if Entity (Constant_Value (Ent)) = E1 then
5400 return True;
5401 else
5402 Ent := Entity (Constant_Value (Ent));
5403 end if;
5404
5405 -- The actual may be a constant that has been folded. Recover
5406 -- original name.
5407
5408 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5409 Ent := Entity (Original_Node (Constant_Value (Ent)));
5410 else
5411 return False;
5412 end if;
5413 end loop;
5414
5415 return False;
5416 end Same_Instantiated_Constant;
5417
5418 --------------------------------
5419 -- Same_Instantiated_Variable --
5420 --------------------------------
5421
5422 function Same_Instantiated_Variable
5423 (E1, E2 : Entity_Id) return Boolean
5424 is
5425 function Original_Entity (E : Entity_Id) return Entity_Id;
5426 -- Follow chain of renamings to the ultimate ancestor
5427
5428 ---------------------
5429 -- Original_Entity --
5430 ---------------------
5431
5432 function Original_Entity (E : Entity_Id) return Entity_Id is
5433 Orig : Entity_Id;
5434
5435 begin
5436 Orig := E;
5437 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5438 and then Present (Renamed_Object (Orig))
5439 and then Is_Entity_Name (Renamed_Object (Orig))
5440 loop
5441 Orig := Entity (Renamed_Object (Orig));
5442 end loop;
5443
5444 return Orig;
5445 end Original_Entity;
5446
5447 -- Start of processing for Same_Instantiated_Variable
5448
5449 begin
5450 return Ekind (E1) = Ekind (E2)
5451 and then Original_Entity (E1) = Original_Entity (E2);
5452 end Same_Instantiated_Variable;
5453
5454 -- Start of processing for Check_Formal_Package_Instance
5455
5456 begin
5457 while Present (E1)
5458 and then Present (E2)
5459 loop
5460 exit when Ekind (E1) = E_Package
5461 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5462
5463 -- If the formal is the renaming of the formal package, this
5464 -- is the end of its formal part, which may occur before the
5465 -- end of the formal part in the actual in the presence of
5466 -- defaulted parameters in the formal package.
5467
5468 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5469 and then Renamed_Entity (E2) = Scope (E2);
5470
5471 -- The analysis of the actual may generate additional internal
5472 -- entities. If the formal is defaulted, there is no corresponding
5473 -- analysis and the internal entities must be skipped, until we
5474 -- find corresponding entities again.
5475
5476 if Comes_From_Source (E2)
5477 and then not Comes_From_Source (E1)
5478 and then Chars (E1) /= Chars (E2)
5479 then
5480 while Present (E1)
5481 and then Chars (E1) /= Chars (E2)
5482 loop
5483 Next_Entity (E1);
5484 end loop;
5485 end if;
5486
5487 if No (E1) then
5488 return;
5489
5490 -- If the formal entity comes from a formal declaration, it was
5491 -- defaulted in the formal package, and no check is needed on it.
5492
5493 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5494 goto Next_E;
5495
5496 -- Ditto for defaulted formal subprograms.
5497
5498 elsif Is_Overloadable (E1)
5499 and then Nkind (Unit_Declaration_Node (E2)) in
5500 N_Formal_Subprogram_Declaration
5501 then
5502 goto Next_E;
5503
5504 elsif Is_Type (E1) then
5505
5506 -- Subtypes must statically match. E1, E2 are the local entities
5507 -- that are subtypes of the actuals. Itypes generated for other
5508 -- parameters need not be checked, the check will be performed
5509 -- on the parameters themselves.
5510
5511 -- If E2 is a formal type declaration, it is a defaulted parameter
5512 -- and needs no checking.
5513
5514 if not Is_Itype (E1)
5515 and then not Is_Itype (E2)
5516 then
5517 Check_Mismatch
5518 (not Is_Type (E2)
5519 or else Etype (E1) /= Etype (E2)
5520 or else not Subtypes_Statically_Match (E1, E2));
5521 end if;
5522
5523 elsif Ekind (E1) = E_Constant then
5524
5525 -- IN parameters must denote the same static value, or the same
5526 -- constant, or the literal null.
5527
5528 Expr1 := Expression (Parent (E1));
5529
5530 if Ekind (E2) /= E_Constant then
5531 Check_Mismatch (True);
5532 goto Next_E;
5533 else
5534 Expr2 := Expression (Parent (E2));
5535 end if;
5536
5537 if Is_OK_Static_Expression (Expr1) then
5538 if not Is_OK_Static_Expression (Expr2) then
5539 Check_Mismatch (True);
5540
5541 elsif Is_Discrete_Type (Etype (E1)) then
5542 declare
5543 V1 : constant Uint := Expr_Value (Expr1);
5544 V2 : constant Uint := Expr_Value (Expr2);
5545 begin
5546 Check_Mismatch (V1 /= V2);
5547 end;
5548
5549 elsif Is_Real_Type (Etype (E1)) then
5550 declare
5551 V1 : constant Ureal := Expr_Value_R (Expr1);
5552 V2 : constant Ureal := Expr_Value_R (Expr2);
5553 begin
5554 Check_Mismatch (V1 /= V2);
5555 end;
5556
5557 elsif Is_String_Type (Etype (E1))
5558 and then Nkind (Expr1) = N_String_Literal
5559 then
5560 if Nkind (Expr2) /= N_String_Literal then
5561 Check_Mismatch (True);
5562 else
5563 Check_Mismatch
5564 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5565 end if;
5566 end if;
5567
5568 elsif Is_Entity_Name (Expr1) then
5569 if Is_Entity_Name (Expr2) then
5570 if Entity (Expr1) = Entity (Expr2) then
5571 null;
5572 else
5573 Check_Mismatch
5574 (not Same_Instantiated_Constant
5575 (Entity (Expr1), Entity (Expr2)));
5576 end if;
5577 else
5578 Check_Mismatch (True);
5579 end if;
5580
5581 elsif Is_Entity_Name (Original_Node (Expr1))
5582 and then Is_Entity_Name (Expr2)
5583 and then
5584 Same_Instantiated_Constant
5585 (Entity (Original_Node (Expr1)), Entity (Expr2))
5586 then
5587 null;
5588
5589 elsif Nkind (Expr1) = N_Null then
5590 Check_Mismatch (Nkind (Expr1) /= N_Null);
5591
5592 else
5593 Check_Mismatch (True);
5594 end if;
5595
5596 elsif Ekind (E1) = E_Variable then
5597 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5598
5599 elsif Ekind (E1) = E_Package then
5600 Check_Mismatch
5601 (Ekind (E1) /= Ekind (E2)
5602 or else Renamed_Object (E1) /= Renamed_Object (E2));
5603
5604 elsif Is_Overloadable (E1) then
5605
5606 -- Verify that the actual subprograms match. Note that actuals
5607 -- that are attributes are rewritten as subprograms. If the
5608 -- subprogram in the formal package is defaulted, no check is
5609 -- needed. Note that this can only happen in Ada 2005 when the
5610 -- formal package can be partially parameterized.
5611
5612 if Nkind (Unit_Declaration_Node (E1)) =
5613 N_Subprogram_Renaming_Declaration
5614 and then From_Default (Unit_Declaration_Node (E1))
5615 then
5616 null;
5617
5618 -- If the formal package has an "others" box association that
5619 -- covers this formal, there is no need for a check either.
5620
5621 elsif Nkind (Unit_Declaration_Node (E2)) in
5622 N_Formal_Subprogram_Declaration
5623 and then Box_Present (Unit_Declaration_Node (E2))
5624 then
5625 null;
5626
5627 -- No check needed if subprogram is a defaulted null procedure
5628
5629 elsif No (Alias (E2))
5630 and then Ekind (E2) = E_Procedure
5631 and then
5632 Null_Present (Specification (Unit_Declaration_Node (E2)))
5633 then
5634 null;
5635
5636 -- Otherwise the actual in the formal and the actual in the
5637 -- instantiation of the formal must match, up to renamings.
5638
5639 else
5640 Check_Mismatch
5641 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5642 end if;
5643
5644 else
5645 raise Program_Error;
5646 end if;
5647
5648 <<Next_E>>
5649 Next_Entity (E1);
5650 Next_Entity (E2);
5651 end loop;
5652 end Check_Formal_Package_Instance;
5653
5654 ---------------------------
5655 -- Check_Formal_Packages --
5656 ---------------------------
5657
5658 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5659 E : Entity_Id;
5660 Formal_P : Entity_Id;
5661
5662 begin
5663 -- Iterate through the declarations in the instance, looking for package
5664 -- renaming declarations that denote instances of formal packages. Stop
5665 -- when we find the renaming of the current package itself. The
5666 -- declaration for a formal package without a box is followed by an
5667 -- internal entity that repeats the instantiation.
5668
5669 E := First_Entity (P_Id);
5670 while Present (E) loop
5671 if Ekind (E) = E_Package then
5672 if Renamed_Object (E) = P_Id then
5673 exit;
5674
5675 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5676 null;
5677
5678 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5679 Formal_P := Next_Entity (E);
5680 Check_Formal_Package_Instance (Formal_P, E);
5681
5682 -- After checking, remove the internal validating package. It
5683 -- is only needed for semantic checks, and as it may contain
5684 -- generic formal declarations it should not reach gigi.
5685
5686 Remove (Unit_Declaration_Node (Formal_P));
5687 end if;
5688 end if;
5689
5690 Next_Entity (E);
5691 end loop;
5692 end Check_Formal_Packages;
5693
5694 ---------------------------------
5695 -- Check_Forward_Instantiation --
5696 ---------------------------------
5697
5698 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5699 S : Entity_Id;
5700 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5701
5702 begin
5703 -- The instantiation appears before the generic body if we are in the
5704 -- scope of the unit containing the generic, either in its spec or in
5705 -- the package body, and before the generic body.
5706
5707 if Ekind (Gen_Comp) = E_Package_Body then
5708 Gen_Comp := Spec_Entity (Gen_Comp);
5709 end if;
5710
5711 if In_Open_Scopes (Gen_Comp)
5712 and then No (Corresponding_Body (Decl))
5713 then
5714 S := Current_Scope;
5715
5716 while Present (S)
5717 and then not Is_Compilation_Unit (S)
5718 and then not Is_Child_Unit (S)
5719 loop
5720 if Ekind (S) = E_Package then
5721 Set_Has_Forward_Instantiation (S);
5722 end if;
5723
5724 S := Scope (S);
5725 end loop;
5726 end if;
5727 end Check_Forward_Instantiation;
5728
5729 ---------------------------
5730 -- Check_Generic_Actuals --
5731 ---------------------------
5732
5733 -- The visibility of the actuals may be different between the point of
5734 -- generic instantiation and the instantiation of the body.
5735
5736 procedure Check_Generic_Actuals
5737 (Instance : Entity_Id;
5738 Is_Formal_Box : Boolean)
5739 is
5740 E : Entity_Id;
5741 Astype : Entity_Id;
5742
5743 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5744 -- For a formal that is an array type, the component type is often a
5745 -- previous formal in the same unit. The privacy status of the component
5746 -- type will have been examined earlier in the traversal of the
5747 -- corresponding actuals, and this status should not be modified for
5748 -- the array (sub)type itself. However, if the base type of the array
5749 -- (sub)type is private, its full view must be restored in the body to
5750 -- be consistent with subsequent index subtypes, etc.
5751 --
5752 -- To detect this case we have to rescan the list of formals, which is
5753 -- usually short enough to ignore the resulting inefficiency.
5754
5755 -----------------------------
5756 -- Denotes_Previous_Actual --
5757 -----------------------------
5758
5759 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5760 Prev : Entity_Id;
5761
5762 begin
5763 Prev := First_Entity (Instance);
5764 while Present (Prev) loop
5765 if Is_Type (Prev)
5766 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5767 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5768 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5769 then
5770 return True;
5771
5772 elsif Prev = E then
5773 return False;
5774
5775 else
5776 Next_Entity (Prev);
5777 end if;
5778 end loop;
5779
5780 return False;
5781 end Denotes_Previous_Actual;
5782
5783 -- Start of processing for Check_Generic_Actuals
5784
5785 begin
5786 E := First_Entity (Instance);
5787 while Present (E) loop
5788 if Is_Type (E)
5789 and then Nkind (Parent (E)) = N_Subtype_Declaration
5790 and then Scope (Etype (E)) /= Instance
5791 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5792 then
5793 if Is_Array_Type (E)
5794 and then not Is_Private_Type (Etype (E))
5795 and then Denotes_Previous_Actual (Component_Type (E))
5796 then
5797 null;
5798 else
5799 Check_Private_View (Subtype_Indication (Parent (E)));
5800 end if;
5801
5802 Set_Is_Generic_Actual_Type (E, True);
5803 Set_Is_Hidden (E, False);
5804 Set_Is_Potentially_Use_Visible (E,
5805 In_Use (Instance));
5806
5807 -- We constructed the generic actual type as a subtype of the
5808 -- supplied type. This means that it normally would not inherit
5809 -- subtype specific attributes of the actual, which is wrong for
5810 -- the generic case.
5811
5812 Astype := Ancestor_Subtype (E);
5813
5814 if No (Astype) then
5815
5816 -- This can happen when E is an itype that is the full view of
5817 -- a private type completed, e.g. with a constrained array. In
5818 -- that case, use the first subtype, which will carry size
5819 -- information. The base type itself is unconstrained and will
5820 -- not carry it.
5821
5822 Astype := First_Subtype (E);
5823 end if;
5824
5825 Set_Size_Info (E, (Astype));
5826 Set_RM_Size (E, RM_Size (Astype));
5827 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5828
5829 if Is_Discrete_Or_Fixed_Point_Type (E) then
5830 Set_RM_Size (E, RM_Size (Astype));
5831
5832 -- In nested instances, the base type of an access actual may
5833 -- itself be private, and need to be exchanged.
5834
5835 elsif Is_Access_Type (E)
5836 and then Is_Private_Type (Etype (E))
5837 then
5838 Check_Private_View
5839 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5840 end if;
5841
5842 elsif Ekind (E) = E_Package then
5843
5844 -- If this is the renaming for the current instance, we're done.
5845 -- Otherwise it is a formal package. If the corresponding formal
5846 -- was declared with a box, the (instantiations of the) generic
5847 -- formal part are also visible. Otherwise, ignore the entity
5848 -- created to validate the actuals.
5849
5850 if Renamed_Object (E) = Instance then
5851 exit;
5852
5853 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5854 null;
5855
5856 -- The visibility of a formal of an enclosing generic is already
5857 -- correct.
5858
5859 elsif Denotes_Formal_Package (E) then
5860 null;
5861
5862 elsif Present (Associated_Formal_Package (E))
5863 and then not Is_Generic_Formal (E)
5864 then
5865 if Box_Present (Parent (Associated_Formal_Package (E))) then
5866 Check_Generic_Actuals (Renamed_Object (E), True);
5867
5868 else
5869 Check_Generic_Actuals (Renamed_Object (E), False);
5870 end if;
5871
5872 Set_Is_Hidden (E, False);
5873 end if;
5874
5875 -- If this is a subprogram instance (in a wrapper package) the
5876 -- actual is fully visible.
5877
5878 elsif Is_Wrapper_Package (Instance) then
5879 Set_Is_Hidden (E, False);
5880
5881 -- If the formal package is declared with a box, or if the formal
5882 -- parameter is defaulted, it is visible in the body.
5883
5884 elsif Is_Formal_Box
5885 or else Is_Visible_Formal (E)
5886 then
5887 Set_Is_Hidden (E, False);
5888 end if;
5889
5890 if Ekind (E) = E_Constant then
5891
5892 -- If the type of the actual is a private type declared in the
5893 -- enclosing scope of the generic unit, the body of the generic
5894 -- sees the full view of the type (because it has to appear in
5895 -- the corresponding package body). If the type is private now,
5896 -- exchange views to restore the proper visiblity in the instance.
5897
5898 declare
5899 Typ : constant Entity_Id := Base_Type (Etype (E));
5900 -- The type of the actual
5901
5902 Gen_Id : Entity_Id;
5903 -- The generic unit
5904
5905 Parent_Scope : Entity_Id;
5906 -- The enclosing scope of the generic unit
5907
5908 begin
5909 if Is_Wrapper_Package (Instance) then
5910 Gen_Id :=
5911 Generic_Parent
5912 (Specification
5913 (Unit_Declaration_Node
5914 (Related_Instance (Instance))));
5915 else
5916 Gen_Id :=
5917 Generic_Parent (Package_Specification (Instance));
5918 end if;
5919
5920 Parent_Scope := Scope (Gen_Id);
5921
5922 -- The exchange is only needed if the generic is defined
5923 -- within a package which is not a common ancestor of the
5924 -- scope of the instance, and is not already in scope.
5925
5926 if Is_Private_Type (Typ)
5927 and then Scope (Typ) = Parent_Scope
5928 and then Scope (Instance) /= Parent_Scope
5929 and then Ekind (Parent_Scope) = E_Package
5930 and then not Is_Child_Unit (Gen_Id)
5931 then
5932 Switch_View (Typ);
5933
5934 -- If the type of the entity is a subtype, it may also have
5935 -- to be made visible, together with the base type of its
5936 -- full view, after exchange.
5937
5938 if Is_Private_Type (Etype (E)) then
5939 Switch_View (Etype (E));
5940 Switch_View (Base_Type (Etype (E)));
5941 end if;
5942 end if;
5943 end;
5944 end if;
5945
5946 Next_Entity (E);
5947 end loop;
5948 end Check_Generic_Actuals;
5949
5950 ------------------------------
5951 -- Check_Generic_Child_Unit --
5952 ------------------------------
5953
5954 procedure Check_Generic_Child_Unit
5955 (Gen_Id : Node_Id;
5956 Parent_Installed : in out Boolean)
5957 is
5958 Loc : constant Source_Ptr := Sloc (Gen_Id);
5959 Gen_Par : Entity_Id := Empty;
5960 E : Entity_Id;
5961 Inst_Par : Entity_Id;
5962 S : Node_Id;
5963
5964 function Find_Generic_Child
5965 (Scop : Entity_Id;
5966 Id : Node_Id) return Entity_Id;
5967 -- Search generic parent for possible child unit with the given name
5968
5969 function In_Enclosing_Instance return Boolean;
5970 -- Within an instance of the parent, the child unit may be denoted by
5971 -- a simple name, or an abbreviated expanded name. Examine enclosing
5972 -- scopes to locate a possible parent instantiation.
5973
5974 ------------------------
5975 -- Find_Generic_Child --
5976 ------------------------
5977
5978 function Find_Generic_Child
5979 (Scop : Entity_Id;
5980 Id : Node_Id) return Entity_Id
5981 is
5982 E : Entity_Id;
5983
5984 begin
5985 -- If entity of name is already set, instance has already been
5986 -- resolved, e.g. in an enclosing instantiation.
5987
5988 if Present (Entity (Id)) then
5989 if Scope (Entity (Id)) = Scop then
5990 return Entity (Id);
5991 else
5992 return Empty;
5993 end if;
5994
5995 else
5996 E := First_Entity (Scop);
5997 while Present (E) loop
5998 if Chars (E) = Chars (Id)
5999 and then Is_Child_Unit (E)
6000 then
6001 if Is_Child_Unit (E)
6002 and then not Is_Visible_Lib_Unit (E)
6003 then
6004 Error_Msg_NE
6005 ("generic child unit& is not visible", Gen_Id, E);
6006 end if;
6007
6008 Set_Entity (Id, E);
6009 return E;
6010 end if;
6011
6012 Next_Entity (E);
6013 end loop;
6014
6015 return Empty;
6016 end if;
6017 end Find_Generic_Child;
6018
6019 ---------------------------
6020 -- In_Enclosing_Instance --
6021 ---------------------------
6022
6023 function In_Enclosing_Instance return Boolean is
6024 Enclosing_Instance : Node_Id;
6025 Instance_Decl : Node_Id;
6026
6027 begin
6028 -- We do not inline any call that contains instantiations, except
6029 -- for instantiations of Unchecked_Conversion, so if we are within
6030 -- an inlined body the current instance does not require parents.
6031
6032 if In_Inlined_Body then
6033 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6034 return False;
6035 end if;
6036
6037 -- Loop to check enclosing scopes
6038
6039 Enclosing_Instance := Current_Scope;
6040 while Present (Enclosing_Instance) loop
6041 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6042
6043 if Ekind (Enclosing_Instance) = E_Package
6044 and then Is_Generic_Instance (Enclosing_Instance)
6045 and then Present
6046 (Generic_Parent (Specification (Instance_Decl)))
6047 then
6048 -- Check whether the generic we are looking for is a child of
6049 -- this instance.
6050
6051 E := Find_Generic_Child
6052 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6053 exit when Present (E);
6054
6055 else
6056 E := Empty;
6057 end if;
6058
6059 Enclosing_Instance := Scope (Enclosing_Instance);
6060 end loop;
6061
6062 if No (E) then
6063
6064 -- Not a child unit
6065
6066 Analyze (Gen_Id);
6067 return False;
6068
6069 else
6070 Rewrite (Gen_Id,
6071 Make_Expanded_Name (Loc,
6072 Chars => Chars (E),
6073 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6074 Selector_Name => New_Occurrence_Of (E, Loc)));
6075
6076 Set_Entity (Gen_Id, E);
6077 Set_Etype (Gen_Id, Etype (E));
6078 Parent_Installed := False; -- Already in scope.
6079 return True;
6080 end if;
6081 end In_Enclosing_Instance;
6082
6083 -- Start of processing for Check_Generic_Child_Unit
6084
6085 begin
6086 -- If the name of the generic is given by a selected component, it may
6087 -- be the name of a generic child unit, and the prefix is the name of an
6088 -- instance of the parent, in which case the child unit must be visible.
6089 -- If this instance is not in scope, it must be placed there and removed
6090 -- after instantiation, because what is being instantiated is not the
6091 -- original child, but the corresponding child present in the instance
6092 -- of the parent.
6093
6094 -- If the child is instantiated within the parent, it can be given by
6095 -- a simple name. In this case the instance is already in scope, but
6096 -- the child generic must be recovered from the generic parent as well.
6097
6098 if Nkind (Gen_Id) = N_Selected_Component then
6099 S := Selector_Name (Gen_Id);
6100 Analyze (Prefix (Gen_Id));
6101 Inst_Par := Entity (Prefix (Gen_Id));
6102
6103 if Ekind (Inst_Par) = E_Package
6104 and then Present (Renamed_Object (Inst_Par))
6105 then
6106 Inst_Par := Renamed_Object (Inst_Par);
6107 end if;
6108
6109 if Ekind (Inst_Par) = E_Package then
6110 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6111 Gen_Par := Generic_Parent (Parent (Inst_Par));
6112
6113 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6114 and then
6115 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6116 then
6117 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6118 end if;
6119
6120 elsif Ekind (Inst_Par) = E_Generic_Package
6121 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6122 then
6123 -- A formal package may be a real child package, and not the
6124 -- implicit instance within a parent. In this case the child is
6125 -- not visible and has to be retrieved explicitly as well.
6126
6127 Gen_Par := Inst_Par;
6128 end if;
6129
6130 if Present (Gen_Par) then
6131
6132 -- The prefix denotes an instantiation. The entity itself may be a
6133 -- nested generic, or a child unit.
6134
6135 E := Find_Generic_Child (Gen_Par, S);
6136
6137 if Present (E) then
6138 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6139 Set_Entity (Gen_Id, E);
6140 Set_Etype (Gen_Id, Etype (E));
6141 Set_Entity (S, E);
6142 Set_Etype (S, Etype (E));
6143
6144 -- Indicate that this is a reference to the parent
6145
6146 if In_Extended_Main_Source_Unit (Gen_Id) then
6147 Set_Is_Instantiated (Inst_Par);
6148 end if;
6149
6150 -- A common mistake is to replicate the naming scheme of a
6151 -- hierarchy by instantiating a generic child directly, rather
6152 -- than the implicit child in a parent instance:
6153
6154 -- generic .. package Gpar is ..
6155 -- generic .. package Gpar.Child is ..
6156 -- package Par is new Gpar ();
6157
6158 -- with Gpar.Child;
6159 -- package Par.Child is new Gpar.Child ();
6160 -- rather than Par.Child
6161
6162 -- In this case the instantiation is within Par, which is an
6163 -- instance, but Gpar does not denote Par because we are not IN
6164 -- the instance of Gpar, so this is illegal. The test below
6165 -- recognizes this particular case.
6166
6167 if Is_Child_Unit (E)
6168 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6169 and then (not In_Instance
6170 or else Nkind (Parent (Parent (Gen_Id))) =
6171 N_Compilation_Unit)
6172 then
6173 Error_Msg_N
6174 ("prefix of generic child unit must be instance of parent",
6175 Gen_Id);
6176 end if;
6177
6178 if not In_Open_Scopes (Inst_Par)
6179 and then Nkind (Parent (Gen_Id)) not in
6180 N_Generic_Renaming_Declaration
6181 then
6182 Install_Parent (Inst_Par);
6183 Parent_Installed := True;
6184
6185 elsif In_Open_Scopes (Inst_Par) then
6186
6187 -- If the parent is already installed, install the actuals
6188 -- for its formal packages. This is necessary when the child
6189 -- instance is a child of the parent instance: in this case,
6190 -- the parent is placed on the scope stack but the formal
6191 -- packages are not made visible.
6192
6193 Install_Formal_Packages (Inst_Par);
6194 end if;
6195
6196 else
6197 -- If the generic parent does not contain an entity that
6198 -- corresponds to the selector, the instance doesn't either.
6199 -- Analyzing the node will yield the appropriate error message.
6200 -- If the entity is not a child unit, then it is an inner
6201 -- generic in the parent.
6202
6203 Analyze (Gen_Id);
6204 end if;
6205
6206 else
6207 Analyze (Gen_Id);
6208
6209 if Is_Child_Unit (Entity (Gen_Id))
6210 and then
6211 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6212 and then not In_Open_Scopes (Inst_Par)
6213 then
6214 Install_Parent (Inst_Par);
6215 Parent_Installed := True;
6216
6217 -- The generic unit may be the renaming of the implicit child
6218 -- present in an instance. In that case the parent instance is
6219 -- obtained from the name of the renamed entity.
6220
6221 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6222 and then Present (Renamed_Entity (Entity (Gen_Id)))
6223 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6224 then
6225 declare
6226 Renamed_Package : constant Node_Id :=
6227 Name (Parent (Entity (Gen_Id)));
6228 begin
6229 if Nkind (Renamed_Package) = N_Expanded_Name then
6230 Inst_Par := Entity (Prefix (Renamed_Package));
6231 Install_Parent (Inst_Par);
6232 Parent_Installed := True;
6233 end if;
6234 end;
6235 end if;
6236 end if;
6237
6238 elsif Nkind (Gen_Id) = N_Expanded_Name then
6239
6240 -- Entity already present, analyze prefix, whose meaning may be
6241 -- an instance in the current context. If it is an instance of
6242 -- a relative within another, the proper parent may still have
6243 -- to be installed, if they are not of the same generation.
6244
6245 Analyze (Prefix (Gen_Id));
6246
6247 -- In the unlikely case that a local declaration hides the name
6248 -- of the parent package, locate it on the homonym chain. If the
6249 -- context is an instance of the parent, the renaming entity is
6250 -- flagged as such.
6251
6252 Inst_Par := Entity (Prefix (Gen_Id));
6253 while Present (Inst_Par)
6254 and then not Is_Package_Or_Generic_Package (Inst_Par)
6255 loop
6256 Inst_Par := Homonym (Inst_Par);
6257 end loop;
6258
6259 pragma Assert (Present (Inst_Par));
6260 Set_Entity (Prefix (Gen_Id), Inst_Par);
6261
6262 if In_Enclosing_Instance then
6263 null;
6264
6265 elsif Present (Entity (Gen_Id))
6266 and then Is_Child_Unit (Entity (Gen_Id))
6267 and then not In_Open_Scopes (Inst_Par)
6268 then
6269 Install_Parent (Inst_Par);
6270 Parent_Installed := True;
6271 end if;
6272
6273 elsif In_Enclosing_Instance then
6274
6275 -- The child unit is found in some enclosing scope
6276
6277 null;
6278
6279 else
6280 Analyze (Gen_Id);
6281
6282 -- If this is the renaming of the implicit child in a parent
6283 -- instance, recover the parent name and install it.
6284
6285 if Is_Entity_Name (Gen_Id) then
6286 E := Entity (Gen_Id);
6287
6288 if Is_Generic_Unit (E)
6289 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6290 and then Is_Child_Unit (Renamed_Object (E))
6291 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6292 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6293 then
6294 Rewrite (Gen_Id,
6295 New_Copy_Tree (Name (Parent (E))));
6296 Inst_Par := Entity (Prefix (Gen_Id));
6297
6298 if not In_Open_Scopes (Inst_Par) then
6299 Install_Parent (Inst_Par);
6300 Parent_Installed := True;
6301 end if;
6302
6303 -- If it is a child unit of a non-generic parent, it may be
6304 -- use-visible and given by a direct name. Install parent as
6305 -- for other cases.
6306
6307 elsif Is_Generic_Unit (E)
6308 and then Is_Child_Unit (E)
6309 and then
6310 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6311 and then not Is_Generic_Unit (Scope (E))
6312 then
6313 if not In_Open_Scopes (Scope (E)) then
6314 Install_Parent (Scope (E));
6315 Parent_Installed := True;
6316 end if;
6317 end if;
6318 end if;
6319 end if;
6320 end Check_Generic_Child_Unit;
6321
6322 -----------------------------
6323 -- Check_Hidden_Child_Unit --
6324 -----------------------------
6325
6326 procedure Check_Hidden_Child_Unit
6327 (N : Node_Id;
6328 Gen_Unit : Entity_Id;
6329 Act_Decl_Id : Entity_Id)
6330 is
6331 Gen_Id : constant Node_Id := Name (N);
6332
6333 begin
6334 if Is_Child_Unit (Gen_Unit)
6335 and then Is_Child_Unit (Act_Decl_Id)
6336 and then Nkind (Gen_Id) = N_Expanded_Name
6337 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6338 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6339 then
6340 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6341 Error_Msg_NE
6342 ("generic unit & is implicitly declared in &",
6343 Defining_Unit_Name (N), Gen_Unit);
6344 Error_Msg_N ("\instance must have different name",
6345 Defining_Unit_Name (N));
6346 end if;
6347 end Check_Hidden_Child_Unit;
6348
6349 ------------------------
6350 -- Check_Private_View --
6351 ------------------------
6352
6353 procedure Check_Private_View (N : Node_Id) is
6354 T : constant Entity_Id := Etype (N);
6355 BT : Entity_Id;
6356
6357 begin
6358 -- Exchange views if the type was not private in the generic but is
6359 -- private at the point of instantiation. Do not exchange views if
6360 -- the scope of the type is in scope. This can happen if both generic
6361 -- and instance are sibling units, or if type is defined in a parent.
6362 -- In this case the visibility of the type will be correct for all
6363 -- semantic checks.
6364
6365 if Present (T) then
6366 BT := Base_Type (T);
6367
6368 if Is_Private_Type (T)
6369 and then not Has_Private_View (N)
6370 and then Present (Full_View (T))
6371 and then not In_Open_Scopes (Scope (T))
6372 then
6373 -- In the generic, the full type was visible. Save the private
6374 -- entity, for subsequent exchange.
6375
6376 Switch_View (T);
6377
6378 elsif Has_Private_View (N)
6379 and then not Is_Private_Type (T)
6380 and then not Has_Been_Exchanged (T)
6381 and then Etype (Get_Associated_Node (N)) /= T
6382 then
6383 -- Only the private declaration was visible in the generic. If
6384 -- the type appears in a subtype declaration, the subtype in the
6385 -- instance must have a view compatible with that of its parent,
6386 -- which must be exchanged (see corresponding code in Restore_
6387 -- Private_Views). Otherwise, if the type is defined in a parent
6388 -- unit, leave full visibility within instance, which is safe.
6389
6390 if In_Open_Scopes (Scope (Base_Type (T)))
6391 and then not Is_Private_Type (Base_Type (T))
6392 and then Comes_From_Source (Base_Type (T))
6393 then
6394 null;
6395
6396 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6397 or else not In_Private_Part (Scope (Base_Type (T)))
6398 then
6399 Prepend_Elmt (T, Exchanged_Views);
6400 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6401 end if;
6402
6403 -- For composite types with inconsistent representation exchange
6404 -- component types accordingly.
6405
6406 elsif Is_Access_Type (T)
6407 and then Is_Private_Type (Designated_Type (T))
6408 and then not Has_Private_View (N)
6409 and then Present (Full_View (Designated_Type (T)))
6410 then
6411 Switch_View (Designated_Type (T));
6412
6413 elsif Is_Array_Type (T) then
6414 if Is_Private_Type (Component_Type (T))
6415 and then not Has_Private_View (N)
6416 and then Present (Full_View (Component_Type (T)))
6417 then
6418 Switch_View (Component_Type (T));
6419 end if;
6420
6421 -- The normal exchange mechanism relies on the setting of a
6422 -- flag on the reference in the generic. However, an additional
6423 -- mechanism is needed for types that are not explicitly
6424 -- mentioned in the generic, but may be needed in expanded code
6425 -- in the instance. This includes component types of arrays and
6426 -- designated types of access types. This processing must also
6427 -- include the index types of arrays which we take care of here.
6428
6429 declare
6430 Indx : Node_Id;
6431 Typ : Entity_Id;
6432
6433 begin
6434 Indx := First_Index (T);
6435 while Present (Indx) loop
6436 Typ := Base_Type (Etype (Indx));
6437
6438 if Is_Private_Type (Typ)
6439 and then Present (Full_View (Typ))
6440 then
6441 Switch_View (Typ);
6442 end if;
6443
6444 Next_Index (Indx);
6445 end loop;
6446 end;
6447
6448 elsif Is_Private_Type (T)
6449 and then Present (Full_View (T))
6450 and then Is_Array_Type (Full_View (T))
6451 and then Is_Private_Type (Component_Type (Full_View (T)))
6452 then
6453 Switch_View (T);
6454
6455 -- Finally, a non-private subtype may have a private base type, which
6456 -- must be exchanged for consistency. This can happen when a package
6457 -- body is instantiated, when the scope stack is empty but in fact
6458 -- the subtype and the base type are declared in an enclosing scope.
6459
6460 -- Note that in this case we introduce an inconsistency in the view
6461 -- set, because we switch the base type BT, but there could be some
6462 -- private dependent subtypes of BT which remain unswitched. Such
6463 -- subtypes might need to be switched at a later point (see specific
6464 -- provision for that case in Switch_View).
6465
6466 elsif not Is_Private_Type (T)
6467 and then not Has_Private_View (N)
6468 and then Is_Private_Type (BT)
6469 and then Present (Full_View (BT))
6470 and then not Is_Generic_Type (BT)
6471 and then not In_Open_Scopes (BT)
6472 then
6473 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6474 Exchange_Declarations (BT);
6475 end if;
6476 end if;
6477 end Check_Private_View;
6478
6479 -----------------------------
6480 -- Check_Hidden_Primitives --
6481 -----------------------------
6482
6483 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6484 Actual : Node_Id;
6485 Gen_T : Entity_Id;
6486 Result : Elist_Id := No_Elist;
6487
6488 begin
6489 if No (Assoc_List) then
6490 return No_Elist;
6491 end if;
6492
6493 -- Traverse the list of associations between formals and actuals
6494 -- searching for renamings of tagged types
6495
6496 Actual := First (Assoc_List);
6497 while Present (Actual) loop
6498 if Nkind (Actual) = N_Subtype_Declaration then
6499 Gen_T := Generic_Parent_Type (Actual);
6500
6501 if Present (Gen_T)
6502 and then Is_Tagged_Type (Gen_T)
6503 then
6504 -- Traverse the list of primitives of the actual types
6505 -- searching for hidden primitives that are visible in the
6506 -- corresponding generic formal; leave them visible and
6507 -- append them to Result to restore their decoration later.
6508
6509 Install_Hidden_Primitives
6510 (Prims_List => Result,
6511 Gen_T => Gen_T,
6512 Act_T => Entity (Subtype_Indication (Actual)));
6513 end if;
6514 end if;
6515
6516 Next (Actual);
6517 end loop;
6518
6519 return Result;
6520 end Check_Hidden_Primitives;
6521
6522 --------------------------
6523 -- Contains_Instance_Of --
6524 --------------------------
6525
6526 function Contains_Instance_Of
6527 (Inner : Entity_Id;
6528 Outer : Entity_Id;
6529 N : Node_Id) return Boolean
6530 is
6531 Elmt : Elmt_Id;
6532 Scop : Entity_Id;
6533
6534 begin
6535 Scop := Outer;
6536
6537 -- Verify that there are no circular instantiations. We check whether
6538 -- the unit contains an instance of the current scope or some enclosing
6539 -- scope (in case one of the instances appears in a subunit). Longer
6540 -- circularities involving subunits might seem too pathological to
6541 -- consider, but they were not too pathological for the authors of
6542 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6543 -- enclosing generic scopes as containing an instance.
6544
6545 loop
6546 -- Within a generic subprogram body, the scope is not generic, to
6547 -- allow for recursive subprograms. Use the declaration to determine
6548 -- whether this is a generic unit.
6549
6550 if Ekind (Scop) = E_Generic_Package
6551 or else (Is_Subprogram (Scop)
6552 and then Nkind (Unit_Declaration_Node (Scop)) =
6553 N_Generic_Subprogram_Declaration)
6554 then
6555 Elmt := First_Elmt (Inner_Instances (Inner));
6556
6557 while Present (Elmt) loop
6558 if Node (Elmt) = Scop then
6559 Error_Msg_Node_2 := Inner;
6560 Error_Msg_NE
6561 ("circular Instantiation: & instantiated within &!",
6562 N, Scop);
6563 return True;
6564
6565 elsif Node (Elmt) = Inner then
6566 return True;
6567
6568 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6569 Error_Msg_Node_2 := Inner;
6570 Error_Msg_NE
6571 ("circular Instantiation: & instantiated within &!",
6572 N, Node (Elmt));
6573 return True;
6574 end if;
6575
6576 Next_Elmt (Elmt);
6577 end loop;
6578
6579 -- Indicate that Inner is being instantiated within Scop
6580
6581 Append_Elmt (Inner, Inner_Instances (Scop));
6582 end if;
6583
6584 if Scop = Standard_Standard then
6585 exit;
6586 else
6587 Scop := Scope (Scop);
6588 end if;
6589 end loop;
6590
6591 return False;
6592 end Contains_Instance_Of;
6593
6594 -----------------------
6595 -- Copy_Generic_Node --
6596 -----------------------
6597
6598 function Copy_Generic_Node
6599 (N : Node_Id;
6600 Parent_Id : Node_Id;
6601 Instantiating : Boolean) return Node_Id
6602 is
6603 Ent : Entity_Id;
6604 New_N : Node_Id;
6605
6606 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6607 -- Check the given value of one of the Fields referenced by the current
6608 -- node to determine whether to copy it recursively. The field may hold
6609 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6610 -- Char) in which case it need not be copied.
6611
6612 procedure Copy_Descendants;
6613 -- Common utility for various nodes
6614
6615 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6616 -- Make copy of element list
6617
6618 function Copy_Generic_List
6619 (L : List_Id;
6620 Parent_Id : Node_Id) return List_Id;
6621 -- Apply Copy_Node recursively to the members of a node list
6622
6623 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6624 -- True if an identifier is part of the defining program unit name of
6625 -- a child unit. The entity of such an identifier must be kept (for
6626 -- ASIS use) even though as the name of an enclosing generic it would
6627 -- otherwise not be preserved in the generic tree.
6628
6629 ----------------------
6630 -- Copy_Descendants --
6631 ----------------------
6632
6633 procedure Copy_Descendants is
6634
6635 use Atree.Unchecked_Access;
6636 -- This code section is part of the implementation of an untyped
6637 -- tree traversal, so it needs direct access to node fields.
6638
6639 begin
6640 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6641 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6642 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6643 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6644 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6645 end Copy_Descendants;
6646
6647 -----------------------------
6648 -- Copy_Generic_Descendant --
6649 -----------------------------
6650
6651 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6652 begin
6653 if D = Union_Id (Empty) then
6654 return D;
6655
6656 elsif D in Node_Range then
6657 return Union_Id
6658 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6659
6660 elsif D in List_Range then
6661 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6662
6663 elsif D in Elist_Range then
6664 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6665
6666 -- Nothing else is copyable (e.g. Uint values), return as is
6667
6668 else
6669 return D;
6670 end if;
6671 end Copy_Generic_Descendant;
6672
6673 ------------------------
6674 -- Copy_Generic_Elist --
6675 ------------------------
6676
6677 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6678 M : Elmt_Id;
6679 L : Elist_Id;
6680
6681 begin
6682 if Present (E) then
6683 L := New_Elmt_List;
6684 M := First_Elmt (E);
6685 while Present (M) loop
6686 Append_Elmt
6687 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6688 Next_Elmt (M);
6689 end loop;
6690
6691 return L;
6692
6693 else
6694 return No_Elist;
6695 end if;
6696 end Copy_Generic_Elist;
6697
6698 -----------------------
6699 -- Copy_Generic_List --
6700 -----------------------
6701
6702 function Copy_Generic_List
6703 (L : List_Id;
6704 Parent_Id : Node_Id) return List_Id
6705 is
6706 N : Node_Id;
6707 New_L : List_Id;
6708
6709 begin
6710 if Present (L) then
6711 New_L := New_List;
6712 Set_Parent (New_L, Parent_Id);
6713
6714 N := First (L);
6715 while Present (N) loop
6716 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6717 Next (N);
6718 end loop;
6719
6720 return New_L;
6721
6722 else
6723 return No_List;
6724 end if;
6725 end Copy_Generic_List;
6726
6727 ---------------------------
6728 -- In_Defining_Unit_Name --
6729 ---------------------------
6730
6731 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6732 begin
6733 return Present (Parent (Nam))
6734 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6735 or else
6736 (Nkind (Parent (Nam)) = N_Expanded_Name
6737 and then In_Defining_Unit_Name (Parent (Nam))));
6738 end In_Defining_Unit_Name;
6739
6740 -- Start of processing for Copy_Generic_Node
6741
6742 begin
6743 if N = Empty then
6744 return N;
6745 end if;
6746
6747 New_N := New_Copy (N);
6748
6749 -- Copy aspects if present
6750
6751 if Has_Aspects (N) then
6752 Set_Has_Aspects (New_N, False);
6753 Set_Aspect_Specifications
6754 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6755 end if;
6756
6757 if Instantiating then
6758 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6759 end if;
6760
6761 if not Is_List_Member (N) then
6762 Set_Parent (New_N, Parent_Id);
6763 end if;
6764
6765 -- If defining identifier, then all fields have been copied already
6766
6767 if Nkind (New_N) in N_Entity then
6768 null;
6769
6770 -- Special casing for identifiers and other entity names and operators
6771
6772 elsif Nkind_In (New_N, N_Identifier,
6773 N_Character_Literal,
6774 N_Expanded_Name,
6775 N_Operator_Symbol)
6776 or else Nkind (New_N) in N_Op
6777 then
6778 if not Instantiating then
6779
6780 -- Link both nodes in order to assign subsequently the entity of
6781 -- the copy to the original node, in case this is a global
6782 -- reference.
6783
6784 Set_Associated_Node (N, New_N);
6785
6786 -- If we are within an instantiation, this is a nested generic
6787 -- that has already been analyzed at the point of definition.
6788 -- We must preserve references that were global to the enclosing
6789 -- parent at that point. Other occurrences, whether global or
6790 -- local to the current generic, must be resolved anew, so we
6791 -- reset the entity in the generic copy. A global reference has a
6792 -- smaller depth than the parent, or else the same depth in case
6793 -- both are distinct compilation units.
6794
6795 -- A child unit is implicitly declared within the enclosing parent
6796 -- but is in fact global to it, and must be preserved.
6797
6798 -- It is also possible for Current_Instantiated_Parent to be
6799 -- defined, and for this not to be a nested generic, namely if
6800 -- the unit is loaded through Rtsfind. In that case, the entity of
6801 -- New_N is only a link to the associated node, and not a defining
6802 -- occurrence.
6803
6804 -- The entities for parent units in the defining_program_unit of a
6805 -- generic child unit are established when the context of the unit
6806 -- is first analyzed, before the generic copy is made. They are
6807 -- preserved in the copy for use in ASIS queries.
6808
6809 Ent := Entity (New_N);
6810
6811 if No (Current_Instantiated_Parent.Gen_Id) then
6812 if No (Ent)
6813 or else Nkind (Ent) /= N_Defining_Identifier
6814 or else not In_Defining_Unit_Name (N)
6815 then
6816 Set_Associated_Node (New_N, Empty);
6817 end if;
6818
6819 elsif No (Ent)
6820 or else
6821 not Nkind_In (Ent, N_Defining_Identifier,
6822 N_Defining_Character_Literal,
6823 N_Defining_Operator_Symbol)
6824 or else No (Scope (Ent))
6825 or else
6826 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6827 and then not Is_Child_Unit (Ent))
6828 or else
6829 (Scope_Depth (Scope (Ent)) >
6830 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6831 and then
6832 Get_Source_Unit (Ent) =
6833 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6834 then
6835 Set_Associated_Node (New_N, Empty);
6836 end if;
6837
6838 -- Case of instantiating identifier or some other name or operator
6839
6840 else
6841 -- If the associated node is still defined, the entity in it
6842 -- is global, and must be copied to the instance. If this copy
6843 -- is being made for a body to inline, it is applied to an
6844 -- instantiated tree, and the entity is already present and
6845 -- must be also preserved.
6846
6847 declare
6848 Assoc : constant Node_Id := Get_Associated_Node (N);
6849
6850 begin
6851 if Present (Assoc) then
6852 if Nkind (Assoc) = Nkind (N) then
6853 Set_Entity (New_N, Entity (Assoc));
6854 Check_Private_View (N);
6855
6856 -- The name in the call may be a selected component if the
6857 -- call has not been analyzed yet, as may be the case for
6858 -- pre/post conditions in a generic unit.
6859
6860 elsif Nkind (Assoc) = N_Function_Call
6861 and then Is_Entity_Name (Name (Assoc))
6862 then
6863 Set_Entity (New_N, Entity (Name (Assoc)));
6864
6865 elsif Nkind_In (Assoc, N_Defining_Identifier,
6866 N_Defining_Character_Literal,
6867 N_Defining_Operator_Symbol)
6868 and then Expander_Active
6869 then
6870 -- Inlining case: we are copying a tree that contains
6871 -- global entities, which are preserved in the copy to be
6872 -- used for subsequent inlining.
6873
6874 null;
6875
6876 else
6877 Set_Entity (New_N, Empty);
6878 end if;
6879 end if;
6880 end;
6881 end if;
6882
6883 -- For expanded name, we must copy the Prefix and Selector_Name
6884
6885 if Nkind (N) = N_Expanded_Name then
6886 Set_Prefix
6887 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
6888
6889 Set_Selector_Name (New_N,
6890 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
6891
6892 -- For operators, we must copy the right operand
6893
6894 elsif Nkind (N) in N_Op then
6895 Set_Right_Opnd (New_N,
6896 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
6897
6898 -- And for binary operators, the left operand as well
6899
6900 if Nkind (N) in N_Binary_Op then
6901 Set_Left_Opnd (New_N,
6902 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
6903 end if;
6904 end if;
6905
6906 -- Special casing for stubs
6907
6908 elsif Nkind (N) in N_Body_Stub then
6909
6910 -- In any case, we must copy the specification or defining
6911 -- identifier as appropriate.
6912
6913 if Nkind (N) = N_Subprogram_Body_Stub then
6914 Set_Specification (New_N,
6915 Copy_Generic_Node (Specification (N), New_N, Instantiating));
6916
6917 else
6918 Set_Defining_Identifier (New_N,
6919 Copy_Generic_Node
6920 (Defining_Identifier (N), New_N, Instantiating));
6921 end if;
6922
6923 -- If we are not instantiating, then this is where we load and
6924 -- analyze subunits, i.e. at the point where the stub occurs. A
6925 -- more permissive system might defer this analysis to the point
6926 -- of instantiation, but this seems too complicated for now.
6927
6928 if not Instantiating then
6929 declare
6930 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
6931 Subunit : Node_Id;
6932 Unum : Unit_Number_Type;
6933 New_Body : Node_Id;
6934
6935 begin
6936 -- Make sure that, if it is a subunit of the main unit that is
6937 -- preprocessed and if -gnateG is specified, the preprocessed
6938 -- file will be written.
6939
6940 Lib.Analysing_Subunit_Of_Main :=
6941 Lib.In_Extended_Main_Source_Unit (N);
6942 Unum :=
6943 Load_Unit
6944 (Load_Name => Subunit_Name,
6945 Required => False,
6946 Subunit => True,
6947 Error_Node => N);
6948 Lib.Analysing_Subunit_Of_Main := False;
6949
6950 -- If the proper body is not found, a warning message will be
6951 -- emitted when analyzing the stub, or later at the point of
6952 -- instantiation. Here we just leave the stub as is.
6953
6954 if Unum = No_Unit then
6955 Subunits_Missing := True;
6956 goto Subunit_Not_Found;
6957 end if;
6958
6959 Subunit := Cunit (Unum);
6960
6961 if Nkind (Unit (Subunit)) /= N_Subunit then
6962 Error_Msg_N
6963 ("found child unit instead of expected SEPARATE subunit",
6964 Subunit);
6965 Error_Msg_Sloc := Sloc (N);
6966 Error_Msg_N ("\to complete stub #", Subunit);
6967 goto Subunit_Not_Found;
6968 end if;
6969
6970 -- We must create a generic copy of the subunit, in order to
6971 -- perform semantic analysis on it, and we must replace the
6972 -- stub in the original generic unit with the subunit, in order
6973 -- to preserve non-local references within.
6974
6975 -- Only the proper body needs to be copied. Library_Unit and
6976 -- context clause are simply inherited by the generic copy.
6977 -- Note that the copy (which may be recursive if there are
6978 -- nested subunits) must be done first, before attaching it to
6979 -- the enclosing generic.
6980
6981 New_Body :=
6982 Copy_Generic_Node
6983 (Proper_Body (Unit (Subunit)),
6984 Empty, Instantiating => False);
6985
6986 -- Now place the original proper body in the original generic
6987 -- unit. This is a body, not a compilation unit.
6988
6989 Rewrite (N, Proper_Body (Unit (Subunit)));
6990 Set_Is_Compilation_Unit (Defining_Entity (N), False);
6991 Set_Was_Originally_Stub (N);
6992
6993 -- Finally replace the body of the subunit with its copy, and
6994 -- make this new subunit into the library unit of the generic
6995 -- copy, which does not have stubs any longer.
6996
6997 Set_Proper_Body (Unit (Subunit), New_Body);
6998 Set_Library_Unit (New_N, Subunit);
6999 Inherit_Context (Unit (Subunit), N);
7000 end;
7001
7002 -- If we are instantiating, this must be an error case, since
7003 -- otherwise we would have replaced the stub node by the proper body
7004 -- that corresponds. So just ignore it in the copy (i.e. we have
7005 -- copied it, and that is good enough).
7006
7007 else
7008 null;
7009 end if;
7010
7011 <<Subunit_Not_Found>> null;
7012
7013 -- If the node is a compilation unit, it is the subunit of a stub, which
7014 -- has been loaded already (see code below). In this case, the library
7015 -- unit field of N points to the parent unit (which is a compilation
7016 -- unit) and need not (and cannot) be copied.
7017
7018 -- When the proper body of the stub is analyzed, the library_unit link
7019 -- is used to establish the proper context (see sem_ch10).
7020
7021 -- The other fields of a compilation unit are copied as usual
7022
7023 elsif Nkind (N) = N_Compilation_Unit then
7024
7025 -- This code can only be executed when not instantiating, because in
7026 -- the copy made for an instantiation, the compilation unit node has
7027 -- disappeared at the point that a stub is replaced by its proper
7028 -- body.
7029
7030 pragma Assert (not Instantiating);
7031
7032 Set_Context_Items (New_N,
7033 Copy_Generic_List (Context_Items (N), New_N));
7034
7035 Set_Unit (New_N,
7036 Copy_Generic_Node (Unit (N), New_N, False));
7037
7038 Set_First_Inlined_Subprogram (New_N,
7039 Copy_Generic_Node
7040 (First_Inlined_Subprogram (N), New_N, False));
7041
7042 Set_Aux_Decls_Node (New_N,
7043 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7044
7045 -- For an assignment node, the assignment is known to be semantically
7046 -- legal if we are instantiating the template. This avoids incorrect
7047 -- diagnostics in generated code.
7048
7049 elsif Nkind (N) = N_Assignment_Statement then
7050
7051 -- Copy name and expression fields in usual manner
7052
7053 Set_Name (New_N,
7054 Copy_Generic_Node (Name (N), New_N, Instantiating));
7055
7056 Set_Expression (New_N,
7057 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7058
7059 if Instantiating then
7060 Set_Assignment_OK (Name (New_N), True);
7061 end if;
7062
7063 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7064 if not Instantiating then
7065 Set_Associated_Node (N, New_N);
7066
7067 else
7068 if Present (Get_Associated_Node (N))
7069 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7070 then
7071 -- In the generic the aggregate has some composite type. If at
7072 -- the point of instantiation the type has a private view,
7073 -- install the full view (and that of its ancestors, if any).
7074
7075 declare
7076 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7077 Rt : Entity_Id;
7078
7079 begin
7080 if Present (T)
7081 and then Is_Private_Type (T)
7082 then
7083 Switch_View (T);
7084 end if;
7085
7086 if Present (T)
7087 and then Is_Tagged_Type (T)
7088 and then Is_Derived_Type (T)
7089 then
7090 Rt := Root_Type (T);
7091
7092 loop
7093 T := Etype (T);
7094
7095 if Is_Private_Type (T) then
7096 Switch_View (T);
7097 end if;
7098
7099 exit when T = Rt;
7100 end loop;
7101 end if;
7102 end;
7103 end if;
7104 end if;
7105
7106 -- Do not copy the associated node, which points to the generic copy
7107 -- of the aggregate.
7108
7109 declare
7110 use Atree.Unchecked_Access;
7111 -- This code section is part of the implementation of an untyped
7112 -- tree traversal, so it needs direct access to node fields.
7113
7114 begin
7115 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7116 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7117 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7118 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7119 end;
7120
7121 -- Allocators do not have an identifier denoting the access type, so we
7122 -- must locate it through the expression to check whether the views are
7123 -- consistent.
7124
7125 elsif Nkind (N) = N_Allocator
7126 and then Nkind (Expression (N)) = N_Qualified_Expression
7127 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7128 and then Instantiating
7129 then
7130 declare
7131 T : constant Node_Id :=
7132 Get_Associated_Node (Subtype_Mark (Expression (N)));
7133 Acc_T : Entity_Id;
7134
7135 begin
7136 if Present (T) then
7137
7138 -- Retrieve the allocator node in the generic copy
7139
7140 Acc_T := Etype (Parent (Parent (T)));
7141 if Present (Acc_T)
7142 and then Is_Private_Type (Acc_T)
7143 then
7144 Switch_View (Acc_T);
7145 end if;
7146 end if;
7147
7148 Copy_Descendants;
7149 end;
7150
7151 -- For a proper body, we must catch the case of a proper body that
7152 -- replaces a stub. This represents the point at which a separate
7153 -- compilation unit, and hence template file, may be referenced, so we
7154 -- must make a new source instantiation entry for the template of the
7155 -- subunit, and ensure that all nodes in the subunit are adjusted using
7156 -- this new source instantiation entry.
7157
7158 elsif Nkind (N) in N_Proper_Body then
7159 declare
7160 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7161
7162 begin
7163 if Instantiating and then Was_Originally_Stub (N) then
7164 Create_Instantiation_Source
7165 (Instantiation_Node,
7166 Defining_Entity (N),
7167 False,
7168 S_Adjustment);
7169 end if;
7170
7171 -- Now copy the fields of the proper body, using the new
7172 -- adjustment factor if one was needed as per test above.
7173
7174 Copy_Descendants;
7175
7176 -- Restore the original adjustment factor in case changed
7177
7178 S_Adjustment := Save_Adjustment;
7179 end;
7180
7181 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
7182 -- generic unit, not to the instantiating unit.
7183
7184 elsif Nkind (N) = N_Pragma and then Instantiating then
7185 declare
7186 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
7187 begin
7188 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
7189 New_N := Make_Null_Statement (Sloc (N));
7190 else
7191 Copy_Descendants;
7192 end if;
7193 end;
7194
7195 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7196
7197 -- No descendant fields need traversing
7198
7199 null;
7200
7201 elsif Nkind (N) = N_String_Literal
7202 and then Present (Etype (N))
7203 and then Instantiating
7204 then
7205 -- If the string is declared in an outer scope, the string_literal
7206 -- subtype created for it may have the wrong scope. We force the
7207 -- reanalysis of the constant to generate a new itype in the proper
7208 -- context.
7209
7210 Set_Etype (New_N, Empty);
7211 Set_Analyzed (New_N, False);
7212
7213 -- For the remaining nodes, copy their descendants recursively
7214
7215 else
7216 Copy_Descendants;
7217
7218 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7219 Set_Generic_Parent (Specification (New_N), N);
7220
7221 -- Should preserve Corresponding_Spec??? (12.3(14))
7222 end if;
7223 end if;
7224
7225 return New_N;
7226 end Copy_Generic_Node;
7227
7228 ----------------------------
7229 -- Denotes_Formal_Package --
7230 ----------------------------
7231
7232 function Denotes_Formal_Package
7233 (Pack : Entity_Id;
7234 On_Exit : Boolean := False;
7235 Instance : Entity_Id := Empty) return Boolean
7236 is
7237 Par : Entity_Id;
7238 Scop : constant Entity_Id := Scope (Pack);
7239 E : Entity_Id;
7240
7241 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7242 -- The package in question may be an actual for a previous formal
7243 -- package P of the current instance, so examine its actuals as well.
7244 -- This must be recursive over other formal packages.
7245
7246 ----------------------------------
7247 -- Is_Actual_Of_Previous_Formal --
7248 ----------------------------------
7249
7250 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7251 E1 : Entity_Id;
7252
7253 begin
7254 E1 := First_Entity (P);
7255 while Present (E1) and then E1 /= Instance loop
7256 if Ekind (E1) = E_Package
7257 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7258 then
7259 if Renamed_Object (E1) = Pack then
7260 return True;
7261
7262 elsif E1 = P or else Renamed_Object (E1) = P then
7263 return False;
7264
7265 elsif Is_Actual_Of_Previous_Formal (E1) then
7266 return True;
7267 end if;
7268 end if;
7269
7270 Next_Entity (E1);
7271 end loop;
7272
7273 return False;
7274 end Is_Actual_Of_Previous_Formal;
7275
7276 -- Start of processing for Denotes_Formal_Package
7277
7278 begin
7279 if On_Exit then
7280 Par :=
7281 Instance_Envs.Table
7282 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7283 else
7284 Par := Current_Instantiated_Parent.Act_Id;
7285 end if;
7286
7287 if Ekind (Scop) = E_Generic_Package
7288 or else Nkind (Unit_Declaration_Node (Scop)) =
7289 N_Generic_Subprogram_Declaration
7290 then
7291 return True;
7292
7293 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7294 N_Formal_Package_Declaration
7295 then
7296 return True;
7297
7298 elsif No (Par) then
7299 return False;
7300
7301 else
7302 -- Check whether this package is associated with a formal package of
7303 -- the enclosing instantiation. Iterate over the list of renamings.
7304
7305 E := First_Entity (Par);
7306 while Present (E) loop
7307 if Ekind (E) /= E_Package
7308 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7309 then
7310 null;
7311
7312 elsif Renamed_Object (E) = Par then
7313 return False;
7314
7315 elsif Renamed_Object (E) = Pack then
7316 return True;
7317
7318 elsif Is_Actual_Of_Previous_Formal (E) then
7319 return True;
7320
7321 end if;
7322
7323 Next_Entity (E);
7324 end loop;
7325
7326 return False;
7327 end if;
7328 end Denotes_Formal_Package;
7329
7330 -----------------
7331 -- End_Generic --
7332 -----------------
7333
7334 procedure End_Generic is
7335 begin
7336 -- ??? More things could be factored out in this routine. Should
7337 -- probably be done at a later stage.
7338
7339 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7340 Generic_Flags.Decrement_Last;
7341
7342 Expander_Mode_Restore;
7343 end End_Generic;
7344
7345 -------------
7346 -- Earlier --
7347 -------------
7348
7349 function Earlier (N1, N2 : Node_Id) return Boolean is
7350 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7351 -- Find distance from given node to enclosing compilation unit
7352
7353 ----------------
7354 -- Find_Depth --
7355 ----------------
7356
7357 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7358 begin
7359 while Present (P)
7360 and then Nkind (P) /= N_Compilation_Unit
7361 loop
7362 P := True_Parent (P);
7363 D := D + 1;
7364 end loop;
7365 end Find_Depth;
7366
7367 -- Local declarations
7368
7369 D1 : Integer := 0;
7370 D2 : Integer := 0;
7371 P1 : Node_Id := N1;
7372 P2 : Node_Id := N2;
7373 T1 : Source_Ptr;
7374 T2 : Source_Ptr;
7375
7376 -- Start of processing for Earlier
7377
7378 begin
7379 Find_Depth (P1, D1);
7380 Find_Depth (P2, D2);
7381
7382 if P1 /= P2 then
7383 return False;
7384 else
7385 P1 := N1;
7386 P2 := N2;
7387 end if;
7388
7389 while D1 > D2 loop
7390 P1 := True_Parent (P1);
7391 D1 := D1 - 1;
7392 end loop;
7393
7394 while D2 > D1 loop
7395 P2 := True_Parent (P2);
7396 D2 := D2 - 1;
7397 end loop;
7398
7399 -- At this point P1 and P2 are at the same distance from the root.
7400 -- We examine their parents until we find a common declarative list.
7401 -- If we reach the root, N1 and N2 do not descend from the same
7402 -- declarative list (e.g. one is nested in the declarative part and
7403 -- the other is in a block in the statement part) and the earlier
7404 -- one is already frozen.
7405
7406 while not Is_List_Member (P1)
7407 or else not Is_List_Member (P2)
7408 or else List_Containing (P1) /= List_Containing (P2)
7409 loop
7410 P1 := True_Parent (P1);
7411 P2 := True_Parent (P2);
7412
7413 if Nkind (Parent (P1)) = N_Subunit then
7414 P1 := Corresponding_Stub (Parent (P1));
7415 end if;
7416
7417 if Nkind (Parent (P2)) = N_Subunit then
7418 P2 := Corresponding_Stub (Parent (P2));
7419 end if;
7420
7421 if P1 = P2 then
7422 return False;
7423 end if;
7424 end loop;
7425
7426 -- Expanded code usually shares the source location of the original
7427 -- construct it was generated for. This however may not necessarely
7428 -- reflect the true location of the code within the tree.
7429
7430 -- Before comparing the slocs of the two nodes, make sure that we are
7431 -- working with correct source locations. Assume that P1 is to the left
7432 -- of P2. If either one does not come from source, traverse the common
7433 -- list heading towards the other node and locate the first source
7434 -- statement.
7435
7436 -- P1 P2
7437 -- ----+===+===+--------------+===+===+----
7438 -- expanded code expanded code
7439
7440 if not Comes_From_Source (P1) then
7441 while Present (P1) loop
7442
7443 -- Neither P2 nor a source statement were located during the
7444 -- search. If we reach the end of the list, then P1 does not
7445 -- occur earlier than P2.
7446
7447 -- ---->
7448 -- start --- P2 ----- P1 --- end
7449
7450 if No (Next (P1)) then
7451 return False;
7452
7453 -- We encounter P2 while going to the right of the list. This
7454 -- means that P1 does indeed appear earlier.
7455
7456 -- ---->
7457 -- start --- P1 ===== P2 --- end
7458 -- expanded code in between
7459
7460 elsif P1 = P2 then
7461 return True;
7462
7463 -- No need to look any further since we have located a source
7464 -- statement.
7465
7466 elsif Comes_From_Source (P1) then
7467 exit;
7468 end if;
7469
7470 -- Keep going right
7471
7472 Next (P1);
7473 end loop;
7474 end if;
7475
7476 if not Comes_From_Source (P2) then
7477 while Present (P2) loop
7478
7479 -- Neither P1 nor a source statement were located during the
7480 -- search. If we reach the start of the list, then P1 does not
7481 -- occur earlier than P2.
7482
7483 -- <----
7484 -- start --- P2 --- P1 --- end
7485
7486 if No (Prev (P2)) then
7487 return False;
7488
7489 -- We encounter P1 while going to the left of the list. This
7490 -- means that P1 does indeed appear earlier.
7491
7492 -- <----
7493 -- start --- P1 ===== P2 --- end
7494 -- expanded code in between
7495
7496 elsif P2 = P1 then
7497 return True;
7498
7499 -- No need to look any further since we have located a source
7500 -- statement.
7501
7502 elsif Comes_From_Source (P2) then
7503 exit;
7504 end if;
7505
7506 -- Keep going left
7507
7508 Prev (P2);
7509 end loop;
7510 end if;
7511
7512 -- At this point either both nodes came from source or we approximated
7513 -- their source locations through neighbouring source statements.
7514
7515 T1 := Top_Level_Location (Sloc (P1));
7516 T2 := Top_Level_Location (Sloc (P2));
7517
7518 -- When two nodes come from the same instance, they have identical top
7519 -- level locations. To determine proper relation within the tree, check
7520 -- their locations within the template.
7521
7522 if T1 = T2 then
7523 return Sloc (P1) < Sloc (P2);
7524
7525 -- The two nodes either come from unrelated instances or do not come
7526 -- from instantiated code at all.
7527
7528 else
7529 return T1 < T2;
7530 end if;
7531 end Earlier;
7532
7533 ----------------------
7534 -- Find_Actual_Type --
7535 ----------------------
7536
7537 function Find_Actual_Type
7538 (Typ : Entity_Id;
7539 Gen_Type : Entity_Id) return Entity_Id
7540 is
7541 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7542 T : Entity_Id;
7543
7544 begin
7545 -- Special processing only applies to child units
7546
7547 if not Is_Child_Unit (Gen_Scope) then
7548 return Get_Instance_Of (Typ);
7549
7550 -- If designated or component type is itself a formal of the child unit,
7551 -- its instance is available.
7552
7553 elsif Scope (Typ) = Gen_Scope then
7554 return Get_Instance_Of (Typ);
7555
7556 -- If the array or access type is not declared in the parent unit,
7557 -- no special processing needed.
7558
7559 elsif not Is_Generic_Type (Typ)
7560 and then Scope (Gen_Scope) /= Scope (Typ)
7561 then
7562 return Get_Instance_Of (Typ);
7563
7564 -- Otherwise, retrieve designated or component type by visibility
7565
7566 else
7567 T := Current_Entity (Typ);
7568 while Present (T) loop
7569 if In_Open_Scopes (Scope (T)) then
7570 return T;
7571
7572 elsif Is_Generic_Actual_Type (T) then
7573 return T;
7574 end if;
7575
7576 T := Homonym (T);
7577 end loop;
7578
7579 return Typ;
7580 end if;
7581 end Find_Actual_Type;
7582
7583 ----------------------------
7584 -- Freeze_Subprogram_Body --
7585 ----------------------------
7586
7587 procedure Freeze_Subprogram_Body
7588 (Inst_Node : Node_Id;
7589 Gen_Body : Node_Id;
7590 Pack_Id : Entity_Id)
7591 is
7592 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7593 Par : constant Entity_Id := Scope (Gen_Unit);
7594 E_G_Id : Entity_Id;
7595 Enc_G : Entity_Id;
7596 Enc_I : Node_Id;
7597 F_Node : Node_Id;
7598
7599 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7600 -- Find innermost package body that encloses the given node, and which
7601 -- is not a compilation unit. Freeze nodes for the instance, or for its
7602 -- enclosing body, may be inserted after the enclosing_body of the
7603 -- generic unit. Used to determine proper placement of freeze node for
7604 -- both package and subprogram instances.
7605
7606 function Package_Freeze_Node (B : Node_Id) return Node_Id;
7607 -- Find entity for given package body, and locate or create a freeze
7608 -- node for it.
7609
7610 ----------------------------
7611 -- Enclosing_Package_Body --
7612 ----------------------------
7613
7614 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
7615 P : Node_Id;
7616
7617 begin
7618 P := Parent (N);
7619 while Present (P)
7620 and then Nkind (Parent (P)) /= N_Compilation_Unit
7621 loop
7622 if Nkind (P) = N_Package_Body then
7623 if Nkind (Parent (P)) = N_Subunit then
7624 return Corresponding_Stub (Parent (P));
7625 else
7626 return P;
7627 end if;
7628 end if;
7629
7630 P := True_Parent (P);
7631 end loop;
7632
7633 return Empty;
7634 end Enclosing_Package_Body;
7635
7636 -------------------------
7637 -- Package_Freeze_Node --
7638 -------------------------
7639
7640 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7641 Id : Entity_Id;
7642
7643 begin
7644 if Nkind (B) = N_Package_Body then
7645 Id := Corresponding_Spec (B);
7646 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7647 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7648 end if;
7649
7650 Ensure_Freeze_Node (Id);
7651 return Freeze_Node (Id);
7652 end Package_Freeze_Node;
7653
7654 -- Start of processing of Freeze_Subprogram_Body
7655
7656 begin
7657 -- If the instance and the generic body appear within the same unit, and
7658 -- the instance precedes the generic, the freeze node for the instance
7659 -- must appear after that of the generic. If the generic is nested
7660 -- within another instance I2, then current instance must be frozen
7661 -- after I2. In both cases, the freeze nodes are those of enclosing
7662 -- packages. Otherwise, the freeze node is placed at the end of the
7663 -- current declarative part.
7664
7665 Enc_G := Enclosing_Package_Body (Gen_Body);
7666 Enc_I := Enclosing_Package_Body (Inst_Node);
7667 Ensure_Freeze_Node (Pack_Id);
7668 F_Node := Freeze_Node (Pack_Id);
7669
7670 if Is_Generic_Instance (Par)
7671 and then Present (Freeze_Node (Par))
7672 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7673 then
7674 -- The parent was a premature instantiation. Insert freeze node at
7675 -- the end the current declarative part.
7676
7677 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7678 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7679
7680 -- Handle the following case:
7681 --
7682 -- package Parent_Inst is new ...
7683 -- Parent_Inst []
7684 --
7685 -- procedure P ... -- this body freezes Parent_Inst
7686 --
7687 -- package Inst is new ...
7688 --
7689 -- In this particular scenario, the freeze node for Inst must be
7690 -- inserted in the same manner as that of Parent_Inst - before the
7691 -- next source body or at the end of the declarative list (body not
7692 -- available). If body P did not exist and Parent_Inst was frozen
7693 -- after Inst, either by a body following Inst or at the end of the
7694 -- declarative region, the freeze node for Inst must be inserted
7695 -- after that of Parent_Inst. This relation is established by
7696 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7697
7698 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7699 List_Containing (Inst_Node)
7700 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7701 then
7702 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7703
7704 else
7705 Insert_After (Freeze_Node (Par), F_Node);
7706 end if;
7707
7708 -- The body enclosing the instance should be frozen after the body that
7709 -- includes the generic, because the body of the instance may make
7710 -- references to entities therein. If the two are not in the same
7711 -- declarative part, or if the one enclosing the instance is frozen
7712 -- already, freeze the instance at the end of the current declarative
7713 -- part.
7714
7715 elsif Is_Generic_Instance (Par)
7716 and then Present (Freeze_Node (Par))
7717 and then Present (Enc_I)
7718 then
7719 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7720 or else
7721 (Nkind (Enc_I) = N_Package_Body
7722 and then
7723 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7724 then
7725 -- The enclosing package may contain several instances. Rather
7726 -- than computing the earliest point at which to insert its freeze
7727 -- node, we place it at the end of the declarative part of the
7728 -- parent of the generic.
7729
7730 Insert_Freeze_Node_For_Instance
7731 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7732 end if;
7733
7734 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7735
7736 elsif Present (Enc_G)
7737 and then Present (Enc_I)
7738 and then Enc_G /= Enc_I
7739 and then Earlier (Inst_Node, Gen_Body)
7740 then
7741 if Nkind (Enc_G) = N_Package_Body then
7742 E_G_Id := Corresponding_Spec (Enc_G);
7743 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7744 E_G_Id :=
7745 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7746 end if;
7747
7748 -- Freeze package that encloses instance, and place node after the
7749 -- package that encloses generic. If enclosing package is already
7750 -- frozen we have to assume it is at the proper place. This may be a
7751 -- potential ABE that requires dynamic checking. Do not add a freeze
7752 -- node if the package that encloses the generic is inside the body
7753 -- that encloses the instance, because the freeze node would be in
7754 -- the wrong scope. Additional contortions needed if the bodies are
7755 -- within a subunit.
7756
7757 declare
7758 Enclosing_Body : Node_Id;
7759
7760 begin
7761 if Nkind (Enc_I) = N_Package_Body_Stub then
7762 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7763 else
7764 Enclosing_Body := Enc_I;
7765 end if;
7766
7767 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7768 Insert_Freeze_Node_For_Instance
7769 (Enc_G, Package_Freeze_Node (Enc_I));
7770 end if;
7771 end;
7772
7773 -- Freeze enclosing subunit before instance
7774
7775 Ensure_Freeze_Node (E_G_Id);
7776
7777 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7778 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7779 end if;
7780
7781 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7782
7783 else
7784 -- If none of the above, insert freeze node at the end of the current
7785 -- declarative part.
7786
7787 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7788 end if;
7789 end Freeze_Subprogram_Body;
7790
7791 ----------------
7792 -- Get_Gen_Id --
7793 ----------------
7794
7795 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7796 begin
7797 return Generic_Renamings.Table (E).Gen_Id;
7798 end Get_Gen_Id;
7799
7800 ---------------------
7801 -- Get_Instance_Of --
7802 ---------------------
7803
7804 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7805 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7806
7807 begin
7808 if Res /= Assoc_Null then
7809 return Generic_Renamings.Table (Res).Act_Id;
7810 else
7811 -- On exit, entity is not instantiated: not a generic parameter, or
7812 -- else parameter of an inner generic unit.
7813
7814 return A;
7815 end if;
7816 end Get_Instance_Of;
7817
7818 ------------------------------------
7819 -- Get_Package_Instantiation_Node --
7820 ------------------------------------
7821
7822 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7823 Decl : Node_Id := Unit_Declaration_Node (A);
7824 Inst : Node_Id;
7825
7826 begin
7827 -- If the Package_Instantiation attribute has been set on the package
7828 -- entity, then use it directly when it (or its Original_Node) refers
7829 -- to an N_Package_Instantiation node. In principle it should be
7830 -- possible to have this field set in all cases, which should be
7831 -- investigated, and would allow this function to be significantly
7832 -- simplified. ???
7833
7834 Inst := Package_Instantiation (A);
7835
7836 if Present (Inst) then
7837 if Nkind (Inst) = N_Package_Instantiation then
7838 return Inst;
7839
7840 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7841 return Original_Node (Inst);
7842 end if;
7843 end if;
7844
7845 -- If the instantiation is a compilation unit that does not need body
7846 -- then the instantiation node has been rewritten as a package
7847 -- declaration for the instance, and we return the original node.
7848
7849 -- If it is a compilation unit and the instance node has not been
7850 -- rewritten, then it is still the unit of the compilation. Finally, if
7851 -- a body is present, this is a parent of the main unit whose body has
7852 -- been compiled for inlining purposes, and the instantiation node has
7853 -- been rewritten with the instance body.
7854
7855 -- Otherwise the instantiation node appears after the declaration. If
7856 -- the entity is a formal package, the declaration may have been
7857 -- rewritten as a generic declaration (in the case of a formal with box)
7858 -- or left as a formal package declaration if it has actuals, and is
7859 -- found with a forward search.
7860
7861 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7862 if Nkind (Decl) = N_Package_Declaration
7863 and then Present (Corresponding_Body (Decl))
7864 then
7865 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7866 end if;
7867
7868 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
7869 return Original_Node (Decl);
7870 else
7871 return Unit (Parent (Decl));
7872 end if;
7873
7874 elsif Nkind (Decl) = N_Package_Declaration
7875 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
7876 then
7877 return Original_Node (Decl);
7878
7879 else
7880 Inst := Next (Decl);
7881 while not Nkind_In (Inst, N_Package_Instantiation,
7882 N_Formal_Package_Declaration)
7883 loop
7884 Next (Inst);
7885 end loop;
7886
7887 return Inst;
7888 end if;
7889 end Get_Package_Instantiation_Node;
7890
7891 ------------------------
7892 -- Has_Been_Exchanged --
7893 ------------------------
7894
7895 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
7896 Next : Elmt_Id;
7897
7898 begin
7899 Next := First_Elmt (Exchanged_Views);
7900 while Present (Next) loop
7901 if Full_View (Node (Next)) = E then
7902 return True;
7903 end if;
7904
7905 Next_Elmt (Next);
7906 end loop;
7907
7908 return False;
7909 end Has_Been_Exchanged;
7910
7911 ----------
7912 -- Hash --
7913 ----------
7914
7915 function Hash (F : Entity_Id) return HTable_Range is
7916 begin
7917 return HTable_Range (F mod HTable_Size);
7918 end Hash;
7919
7920 ------------------------
7921 -- Hide_Current_Scope --
7922 ------------------------
7923
7924 procedure Hide_Current_Scope is
7925 C : constant Entity_Id := Current_Scope;
7926 E : Entity_Id;
7927
7928 begin
7929 Set_Is_Hidden_Open_Scope (C);
7930
7931 E := First_Entity (C);
7932 while Present (E) loop
7933 if Is_Immediately_Visible (E) then
7934 Set_Is_Immediately_Visible (E, False);
7935 Append_Elmt (E, Hidden_Entities);
7936 end if;
7937
7938 Next_Entity (E);
7939 end loop;
7940
7941 -- Make the scope name invisible as well. This is necessary, but might
7942 -- conflict with calls to Rtsfind later on, in case the scope is a
7943 -- predefined one. There is no clean solution to this problem, so for
7944 -- now we depend on the user not redefining Standard itself in one of
7945 -- the parent units.
7946
7947 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
7948 Set_Is_Immediately_Visible (C, False);
7949 Append_Elmt (C, Hidden_Entities);
7950 end if;
7951
7952 end Hide_Current_Scope;
7953
7954 --------------
7955 -- Init_Env --
7956 --------------
7957
7958 procedure Init_Env is
7959 Saved : Instance_Env;
7960
7961 begin
7962 Saved.Instantiated_Parent := Current_Instantiated_Parent;
7963 Saved.Exchanged_Views := Exchanged_Views;
7964 Saved.Hidden_Entities := Hidden_Entities;
7965 Saved.Current_Sem_Unit := Current_Sem_Unit;
7966 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
7967 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
7968
7969 -- Save configuration switches. These may be reset if the unit is a
7970 -- predefined unit, and the current mode is not Ada 2005.
7971
7972 Save_Opt_Config_Switches (Saved.Switches);
7973
7974 Instance_Envs.Append (Saved);
7975
7976 Exchanged_Views := New_Elmt_List;
7977 Hidden_Entities := New_Elmt_List;
7978
7979 -- Make dummy entry for Instantiated parent. If generic unit is legal,
7980 -- this is set properly in Set_Instance_Env.
7981
7982 Current_Instantiated_Parent :=
7983 (Current_Scope, Current_Scope, Assoc_Null);
7984 end Init_Env;
7985
7986 ------------------------------
7987 -- In_Same_Declarative_Part --
7988 ------------------------------
7989
7990 function In_Same_Declarative_Part
7991 (F_Node : Node_Id;
7992 Inst : Node_Id) return Boolean
7993 is
7994 Decls : constant Node_Id := Parent (F_Node);
7995 Nod : Node_Id := Parent (Inst);
7996
7997 begin
7998 while Present (Nod) loop
7999 if Nod = Decls then
8000 return True;
8001
8002 elsif Nkind_In (Nod, N_Subprogram_Body,
8003 N_Package_Body,
8004 N_Package_Declaration,
8005 N_Task_Body,
8006 N_Protected_Body,
8007 N_Block_Statement)
8008 then
8009 return False;
8010
8011 elsif Nkind (Nod) = N_Subunit then
8012 Nod := Corresponding_Stub (Nod);
8013
8014 elsif Nkind (Nod) = N_Compilation_Unit then
8015 return False;
8016
8017 else
8018 Nod := Parent (Nod);
8019 end if;
8020 end loop;
8021
8022 return False;
8023 end In_Same_Declarative_Part;
8024
8025 ---------------------
8026 -- In_Main_Context --
8027 ---------------------
8028
8029 function In_Main_Context (E : Entity_Id) return Boolean is
8030 Context : List_Id;
8031 Clause : Node_Id;
8032 Nam : Node_Id;
8033
8034 begin
8035 if not Is_Compilation_Unit (E)
8036 or else Ekind (E) /= E_Package
8037 or else In_Private_Part (E)
8038 then
8039 return False;
8040 end if;
8041
8042 Context := Context_Items (Cunit (Main_Unit));
8043
8044 Clause := First (Context);
8045 while Present (Clause) loop
8046 if Nkind (Clause) = N_With_Clause then
8047 Nam := Name (Clause);
8048
8049 -- If the current scope is part of the context of the main unit,
8050 -- analysis of the corresponding with_clause is not complete, and
8051 -- the entity is not set. We use the Chars field directly, which
8052 -- might produce false positives in rare cases, but guarantees
8053 -- that we produce all the instance bodies we will need.
8054
8055 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8056 or else (Nkind (Nam) = N_Selected_Component
8057 and then Chars (Selector_Name (Nam)) = Chars (E))
8058 then
8059 return True;
8060 end if;
8061 end if;
8062
8063 Next (Clause);
8064 end loop;
8065
8066 return False;
8067 end In_Main_Context;
8068
8069 ---------------------
8070 -- Inherit_Context --
8071 ---------------------
8072
8073 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8074 Current_Context : List_Id;
8075 Current_Unit : Node_Id;
8076 Item : Node_Id;
8077 New_I : Node_Id;
8078
8079 Clause : Node_Id;
8080 OK : Boolean;
8081 Lib_Unit : Node_Id;
8082
8083 begin
8084 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8085
8086 -- The inherited context is attached to the enclosing compilation
8087 -- unit. This is either the main unit, or the declaration for the
8088 -- main unit (in case the instantiation appears within the package
8089 -- declaration and the main unit is its body).
8090
8091 Current_Unit := Parent (Inst);
8092 while Present (Current_Unit)
8093 and then Nkind (Current_Unit) /= N_Compilation_Unit
8094 loop
8095 Current_Unit := Parent (Current_Unit);
8096 end loop;
8097
8098 Current_Context := Context_Items (Current_Unit);
8099
8100 Item := First (Context_Items (Parent (Gen_Decl)));
8101 while Present (Item) loop
8102 if Nkind (Item) = N_With_Clause then
8103 Lib_Unit := Library_Unit (Item);
8104
8105 -- Take care to prevent direct cyclic with's
8106
8107 if Lib_Unit /= Current_Unit then
8108
8109 -- Do not add a unit if it is already in the context
8110
8111 Clause := First (Current_Context);
8112 OK := True;
8113 while Present (Clause) loop
8114 if Nkind (Clause) = N_With_Clause and then
8115 Library_Unit (Clause) = Lib_Unit
8116 then
8117 OK := False;
8118 exit;
8119 end if;
8120
8121 Next (Clause);
8122 end loop;
8123
8124 if OK then
8125 New_I := New_Copy (Item);
8126 Set_Implicit_With (New_I, True);
8127 Set_Implicit_With_From_Instantiation (New_I, True);
8128 Append (New_I, Current_Context);
8129 end if;
8130 end if;
8131 end if;
8132
8133 Next (Item);
8134 end loop;
8135 end if;
8136 end Inherit_Context;
8137
8138 ----------------
8139 -- Initialize --
8140 ----------------
8141
8142 procedure Initialize is
8143 begin
8144 Generic_Renamings.Init;
8145 Instance_Envs.Init;
8146 Generic_Flags.Init;
8147 Generic_Renamings_HTable.Reset;
8148 Circularity_Detected := False;
8149 Exchanged_Views := No_Elist;
8150 Hidden_Entities := No_Elist;
8151 end Initialize;
8152
8153 -------------------------------------
8154 -- Insert_Freeze_Node_For_Instance --
8155 -------------------------------------
8156
8157 procedure Insert_Freeze_Node_For_Instance
8158 (N : Node_Id;
8159 F_Node : Node_Id)
8160 is
8161 Decl : Node_Id;
8162 Decls : List_Id;
8163 Inst : Entity_Id;
8164 Par_N : Node_Id;
8165
8166 function Enclosing_Body (N : Node_Id) return Node_Id;
8167 -- Find enclosing package or subprogram body, if any. Freeze node may
8168 -- be placed at end of current declarative list if previous instance
8169 -- and current one have different enclosing bodies.
8170
8171 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8172 -- Find the local instance, if any, that declares the generic that is
8173 -- being instantiated. If present, the freeze node for this instance
8174 -- must follow the freeze node for the previous instance.
8175
8176 --------------------
8177 -- Enclosing_Body --
8178 --------------------
8179
8180 function Enclosing_Body (N : Node_Id) return Node_Id is
8181 P : Node_Id;
8182
8183 begin
8184 P := Parent (N);
8185 while Present (P)
8186 and then Nkind (Parent (P)) /= N_Compilation_Unit
8187 loop
8188 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8189 if Nkind (Parent (P)) = N_Subunit then
8190 return Corresponding_Stub (Parent (P));
8191 else
8192 return P;
8193 end if;
8194 end if;
8195
8196 P := True_Parent (P);
8197 end loop;
8198
8199 return Empty;
8200 end Enclosing_Body;
8201
8202 -----------------------
8203 -- Previous_Instance --
8204 -----------------------
8205
8206 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8207 S : Entity_Id;
8208
8209 begin
8210 S := Scope (Gen);
8211 while Present (S)
8212 and then S /= Standard_Standard
8213 loop
8214 if Is_Generic_Instance (S)
8215 and then In_Same_Source_Unit (S, N)
8216 then
8217 return S;
8218 end if;
8219
8220 S := Scope (S);
8221 end loop;
8222
8223 return Empty;
8224 end Previous_Instance;
8225
8226 -- Start of processing for Insert_Freeze_Node_For_Instance
8227
8228 begin
8229 if not Is_List_Member (F_Node) then
8230 Decl := N;
8231 Decls := List_Containing (N);
8232 Inst := Entity (F_Node);
8233 Par_N := Parent (Decls);
8234
8235 -- When processing a subprogram instantiation, utilize the actual
8236 -- subprogram instantiation rather than its package wrapper as it
8237 -- carries all the context information.
8238
8239 if Is_Wrapper_Package (Inst) then
8240 Inst := Related_Instance (Inst);
8241 end if;
8242
8243 -- If this is a package instance, check whether the generic is
8244 -- declared in a previous instance and the current instance is
8245 -- not within the previous one.
8246
8247 if Present (Generic_Parent (Parent (Inst)))
8248 and then Is_In_Main_Unit (N)
8249 then
8250 declare
8251 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8252 Par_I : constant Entity_Id :=
8253 Previous_Instance
8254 (Generic_Parent (Parent (Inst)));
8255 Scop : Entity_Id;
8256
8257 begin
8258 if Present (Par_I)
8259 and then Earlier (N, Freeze_Node (Par_I))
8260 then
8261 Scop := Scope (Inst);
8262
8263 -- If the current instance is within the one that contains
8264 -- the generic, the freeze node for the current one must
8265 -- appear in the current declarative part. Ditto, if the
8266 -- current instance is within another package instance or
8267 -- within a body that does not enclose the current instance.
8268 -- In these three cases the freeze node of the previous
8269 -- instance is not relevant.
8270
8271 while Present (Scop)
8272 and then Scop /= Standard_Standard
8273 loop
8274 exit when Scop = Par_I
8275 or else
8276 (Is_Generic_Instance (Scop)
8277 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8278 Scop := Scope (Scop);
8279 end loop;
8280
8281 -- Previous instance encloses current instance
8282
8283 if Scop = Par_I then
8284 null;
8285
8286 -- If the next node is a source body we must freeze in
8287 -- the current scope as well.
8288
8289 elsif Present (Next (N))
8290 and then Nkind_In (Next (N),
8291 N_Subprogram_Body, N_Package_Body)
8292 and then Comes_From_Source (Next (N))
8293 then
8294 null;
8295
8296 -- Current instance is within an unrelated instance
8297
8298 elsif Is_Generic_Instance (Scop) then
8299 null;
8300
8301 -- Current instance is within an unrelated body
8302
8303 elsif Present (Enclosing_N)
8304 and then Enclosing_N /= Enclosing_Body (Par_I)
8305 then
8306 null;
8307
8308 else
8309 Insert_After (Freeze_Node (Par_I), F_Node);
8310 return;
8311 end if;
8312 end if;
8313 end;
8314 end if;
8315
8316 -- When the instantiation occurs in a package declaration, append the
8317 -- freeze node to the private declarations (if any).
8318
8319 if Nkind (Par_N) = N_Package_Specification
8320 and then Decls = Visible_Declarations (Par_N)
8321 and then Present (Private_Declarations (Par_N))
8322 and then not Is_Empty_List (Private_Declarations (Par_N))
8323 then
8324 Decls := Private_Declarations (Par_N);
8325 Decl := First (Decls);
8326 end if;
8327
8328 -- Determine the proper freeze point of a package instantiation. We
8329 -- adhere to the general rule of a package or subprogram body causing
8330 -- freezing of anything before it in the same declarative region. In
8331 -- this case, the proper freeze point of a package instantiation is
8332 -- before the first source body which follows, or before a stub. This
8333 -- ensures that entities coming from the instance are already frozen
8334 -- and usable in source bodies.
8335
8336 if Nkind (Par_N) /= N_Package_Declaration
8337 and then Ekind (Inst) = E_Package
8338 and then Is_Generic_Instance (Inst)
8339 and then
8340 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8341 then
8342 while Present (Decl) loop
8343 if (Nkind (Decl) in N_Unit_Body
8344 or else
8345 Nkind (Decl) in N_Body_Stub)
8346 and then Comes_From_Source (Decl)
8347 then
8348 Insert_Before (Decl, F_Node);
8349 return;
8350 end if;
8351
8352 Next (Decl);
8353 end loop;
8354 end if;
8355
8356 -- In a package declaration, or if no previous body, insert at end
8357 -- of list.
8358
8359 Set_Sloc (F_Node, Sloc (Last (Decls)));
8360 Insert_After (Last (Decls), F_Node);
8361 end if;
8362 end Insert_Freeze_Node_For_Instance;
8363
8364 ------------------
8365 -- Install_Body --
8366 ------------------
8367
8368 procedure Install_Body
8369 (Act_Body : Node_Id;
8370 N : Node_Id;
8371 Gen_Body : Node_Id;
8372 Gen_Decl : Node_Id)
8373 is
8374 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8375 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8376 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8377 Par : constant Entity_Id := Scope (Gen_Id);
8378 Gen_Unit : constant Node_Id :=
8379 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8380 Orig_Body : Node_Id := Gen_Body;
8381 F_Node : Node_Id;
8382 Body_Unit : Node_Id;
8383
8384 Must_Delay : Boolean;
8385
8386 function In_Same_Enclosing_Subp return Boolean;
8387 -- Check whether instance and generic body are within same subprogram.
8388
8389 function True_Sloc (N : Node_Id) return Source_Ptr;
8390 -- If the instance is nested inside a generic unit, the Sloc of the
8391 -- instance indicates the place of the original definition, not the
8392 -- point of the current enclosing instance. Pending a better usage of
8393 -- Slocs to indicate instantiation places, we determine the place of
8394 -- origin of a node by finding the maximum sloc of any ancestor node.
8395 -- Why is this not equivalent to Top_Level_Location ???
8396
8397 ----------------------------
8398 -- In_Same_Enclosing_Subp --
8399 ----------------------------
8400
8401 function In_Same_Enclosing_Subp return Boolean is
8402 Scop : Entity_Id;
8403 Subp : Entity_Id;
8404
8405 begin
8406 Scop := Scope (Act_Id);
8407 while Scop /= Standard_Standard
8408 and then not Is_Overloadable (Scop)
8409 loop
8410 Scop := Scope (Scop);
8411 end loop;
8412
8413 if Scop = Standard_Standard then
8414 return False;
8415 else
8416 Subp := Scop;
8417 end if;
8418
8419 Scop := Scope (Gen_Id);
8420 while Scop /= Standard_Standard loop
8421 if Scop = Subp then
8422 return True;
8423 else
8424 Scop := Scope (Scop);
8425 end if;
8426 end loop;
8427
8428 return False;
8429 end In_Same_Enclosing_Subp;
8430
8431 ---------------
8432 -- True_Sloc --
8433 ---------------
8434
8435 function True_Sloc (N : Node_Id) return Source_Ptr is
8436 Res : Source_Ptr;
8437 N1 : Node_Id;
8438
8439 begin
8440 Res := Sloc (N);
8441 N1 := N;
8442 while Present (N1) and then N1 /= Act_Unit loop
8443 if Sloc (N1) > Res then
8444 Res := Sloc (N1);
8445 end if;
8446
8447 N1 := Parent (N1);
8448 end loop;
8449
8450 return Res;
8451 end True_Sloc;
8452
8453 -- Start of processing for Install_Body
8454
8455 begin
8456 -- If the body is a subunit, the freeze point is the corresponding stub
8457 -- in the current compilation, not the subunit itself.
8458
8459 if Nkind (Parent (Gen_Body)) = N_Subunit then
8460 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8461 else
8462 Orig_Body := Gen_Body;
8463 end if;
8464
8465 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8466
8467 -- If the instantiation and the generic definition appear in the same
8468 -- package declaration, this is an early instantiation. If they appear
8469 -- in the same declarative part, it is an early instantiation only if
8470 -- the generic body appears textually later, and the generic body is
8471 -- also in the main unit.
8472
8473 -- If instance is nested within a subprogram, and the generic body
8474 -- is not, the instance is delayed because the enclosing body is. If
8475 -- instance and body are within the same scope, or the same subprogram
8476 -- body, indicate explicitly that the instance is delayed.
8477
8478 Must_Delay :=
8479 (Gen_Unit = Act_Unit
8480 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8481 N_Generic_Package_Declaration)
8482 or else (Gen_Unit = Body_Unit
8483 and then True_Sloc (N) < Sloc (Orig_Body)))
8484 and then Is_In_Main_Unit (Gen_Unit)
8485 and then (Scope (Act_Id) = Scope (Gen_Id)
8486 or else In_Same_Enclosing_Subp));
8487
8488 -- If this is an early instantiation, the freeze node is placed after
8489 -- the generic body. Otherwise, if the generic appears in an instance,
8490 -- we cannot freeze the current instance until the outer one is frozen.
8491 -- This is only relevant if the current instance is nested within some
8492 -- inner scope not itself within the outer instance. If this scope is
8493 -- a package body in the same declarative part as the outer instance,
8494 -- then that body needs to be frozen after the outer instance. Finally,
8495 -- if no delay is needed, we place the freeze node at the end of the
8496 -- current declarative part.
8497
8498 if Expander_Active then
8499 Ensure_Freeze_Node (Act_Id);
8500 F_Node := Freeze_Node (Act_Id);
8501
8502 if Must_Delay then
8503 Insert_After (Orig_Body, F_Node);
8504
8505 elsif Is_Generic_Instance (Par)
8506 and then Present (Freeze_Node (Par))
8507 and then Scope (Act_Id) /= Par
8508 then
8509 -- Freeze instance of inner generic after instance of enclosing
8510 -- generic.
8511
8512 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8513
8514 -- Handle the following case:
8515
8516 -- package Parent_Inst is new ...
8517 -- Parent_Inst []
8518
8519 -- procedure P ... -- this body freezes Parent_Inst
8520
8521 -- package Inst is new ...
8522
8523 -- In this particular scenario, the freeze node for Inst must
8524 -- be inserted in the same manner as that of Parent_Inst,
8525 -- before the next source body or at the end of the declarative
8526 -- list (body not available). If body P did not exist and
8527 -- Parent_Inst was frozen after Inst, either by a body
8528 -- following Inst or at the end of the declarative region,
8529 -- the freeze node for Inst must be inserted after that of
8530 -- Parent_Inst. This relation is established by comparing
8531 -- the Slocs of Parent_Inst freeze node and Inst.
8532
8533 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8534 List_Containing (N)
8535 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8536 then
8537 Insert_Freeze_Node_For_Instance (N, F_Node);
8538 else
8539 Insert_After (Freeze_Node (Par), F_Node);
8540 end if;
8541
8542 -- Freeze package enclosing instance of inner generic after
8543 -- instance of enclosing generic.
8544
8545 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8546 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8547 then
8548 declare
8549 Enclosing : Entity_Id;
8550
8551 begin
8552 Enclosing := Corresponding_Spec (Parent (N));
8553
8554 if No (Enclosing) then
8555 Enclosing := Defining_Entity (Parent (N));
8556 end if;
8557
8558 Insert_Freeze_Node_For_Instance (N, F_Node);
8559 Ensure_Freeze_Node (Enclosing);
8560
8561 if not Is_List_Member (Freeze_Node (Enclosing)) then
8562
8563 -- The enclosing context is a subunit, insert the freeze
8564 -- node after the stub.
8565
8566 if Nkind (Parent (Parent (N))) = N_Subunit then
8567 Insert_Freeze_Node_For_Instance
8568 (Corresponding_Stub (Parent (Parent (N))),
8569 Freeze_Node (Enclosing));
8570
8571 -- The enclosing context is a package with a stub body
8572 -- which has already been replaced by the real body.
8573 -- Insert the freeze node after the actual body.
8574
8575 elsif Ekind (Enclosing) = E_Package
8576 and then Present (Body_Entity (Enclosing))
8577 and then Was_Originally_Stub
8578 (Parent (Body_Entity (Enclosing)))
8579 then
8580 Insert_Freeze_Node_For_Instance
8581 (Parent (Body_Entity (Enclosing)),
8582 Freeze_Node (Enclosing));
8583
8584 -- The parent instance has been frozen before the body of
8585 -- the enclosing package, insert the freeze node after
8586 -- the body.
8587
8588 elsif List_Containing (Freeze_Node (Par)) =
8589 List_Containing (Parent (N))
8590 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8591 then
8592 Insert_Freeze_Node_For_Instance
8593 (Parent (N), Freeze_Node (Enclosing));
8594
8595 else
8596 Insert_After
8597 (Freeze_Node (Par), Freeze_Node (Enclosing));
8598 end if;
8599 end if;
8600 end;
8601
8602 else
8603 Insert_Freeze_Node_For_Instance (N, F_Node);
8604 end if;
8605
8606 else
8607 Insert_Freeze_Node_For_Instance (N, F_Node);
8608 end if;
8609 end if;
8610
8611 Set_Is_Frozen (Act_Id);
8612 Insert_Before (N, Act_Body);
8613 Mark_Rewrite_Insertion (Act_Body);
8614 end Install_Body;
8615
8616 -----------------------------
8617 -- Install_Formal_Packages --
8618 -----------------------------
8619
8620 procedure Install_Formal_Packages (Par : Entity_Id) is
8621 E : Entity_Id;
8622 Gen : Entity_Id;
8623 Gen_E : Entity_Id := Empty;
8624
8625 begin
8626 E := First_Entity (Par);
8627
8628 -- If we are installing an instance parent, locate the formal packages
8629 -- of its generic parent.
8630
8631 if Is_Generic_Instance (Par) then
8632 Gen := Generic_Parent (Package_Specification (Par));
8633 Gen_E := First_Entity (Gen);
8634 end if;
8635
8636 while Present (E) loop
8637 if Ekind (E) = E_Package
8638 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
8639 then
8640 -- If this is the renaming for the parent instance, done
8641
8642 if Renamed_Object (E) = Par then
8643 exit;
8644
8645 -- The visibility of a formal of an enclosing generic is already
8646 -- correct.
8647
8648 elsif Denotes_Formal_Package (E) then
8649 null;
8650
8651 elsif Present (Associated_Formal_Package (E)) then
8652 Check_Generic_Actuals (Renamed_Object (E), True);
8653 Set_Is_Hidden (E, False);
8654
8655 -- Find formal package in generic unit that corresponds to
8656 -- (instance of) formal package in instance.
8657
8658 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
8659 Next_Entity (Gen_E);
8660 end loop;
8661
8662 if Present (Gen_E) then
8663 Map_Formal_Package_Entities (Gen_E, E);
8664 end if;
8665 end if;
8666 end if;
8667
8668 Next_Entity (E);
8669 if Present (Gen_E) then
8670 Next_Entity (Gen_E);
8671 end if;
8672 end loop;
8673 end Install_Formal_Packages;
8674
8675 --------------------
8676 -- Install_Parent --
8677 --------------------
8678
8679 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
8680 Ancestors : constant Elist_Id := New_Elmt_List;
8681 S : constant Entity_Id := Current_Scope;
8682 Inst_Par : Entity_Id;
8683 First_Par : Entity_Id;
8684 Inst_Node : Node_Id;
8685 Gen_Par : Entity_Id;
8686 First_Gen : Entity_Id;
8687 Elmt : Elmt_Id;
8688
8689 procedure Install_Noninstance_Specs (Par : Entity_Id);
8690 -- Install the scopes of noninstance parent units ending with Par
8691
8692 procedure Install_Spec (Par : Entity_Id);
8693 -- The child unit is within the declarative part of the parent, so the
8694 -- declarations within the parent are immediately visible.
8695
8696 -------------------------------
8697 -- Install_Noninstance_Specs --
8698 -------------------------------
8699
8700 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8701 begin
8702 if Present (Par)
8703 and then Par /= Standard_Standard
8704 and then not In_Open_Scopes (Par)
8705 then
8706 Install_Noninstance_Specs (Scope (Par));
8707 Install_Spec (Par);
8708 end if;
8709 end Install_Noninstance_Specs;
8710
8711 ------------------
8712 -- Install_Spec --
8713 ------------------
8714
8715 procedure Install_Spec (Par : Entity_Id) is
8716 Spec : constant Node_Id := Package_Specification (Par);
8717
8718 begin
8719 -- If this parent of the child instance is a top-level unit,
8720 -- then record the unit and its visibility for later resetting in
8721 -- Remove_Parent. We exclude units that are generic instances, as we
8722 -- only want to record this information for the ultimate top-level
8723 -- noninstance parent (is that always correct???).
8724
8725 if Scope (Par) = Standard_Standard
8726 and then not Is_Generic_Instance (Par)
8727 then
8728 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8729 Instance_Parent_Unit := Par;
8730 end if;
8731
8732 -- Open the parent scope and make it and its declarations visible.
8733 -- If this point is not within a body, then only the visible
8734 -- declarations should be made visible, and installation of the
8735 -- private declarations is deferred until the appropriate point
8736 -- within analysis of the spec being instantiated (see the handling
8737 -- of parent visibility in Analyze_Package_Specification). This is
8738 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8739 -- private view problems that occur when compiling instantiations of
8740 -- a generic child of that package (Generic_Dispatching_Constructor).
8741 -- If the instance freezes a tagged type, inlinings of operations
8742 -- from Ada.Tags may need the full view of type Tag. If inlining took
8743 -- proper account of establishing visibility of inlined subprograms'
8744 -- parents then it should be possible to remove this
8745 -- special check. ???
8746
8747 Push_Scope (Par);
8748 Set_Is_Immediately_Visible (Par);
8749 Install_Visible_Declarations (Par);
8750 Set_Use (Visible_Declarations (Spec));
8751
8752 if In_Body or else Is_RTU (Par, Ada_Tags) then
8753 Install_Private_Declarations (Par);
8754 Set_Use (Private_Declarations (Spec));
8755 end if;
8756 end Install_Spec;
8757
8758 -- Start of processing for Install_Parent
8759
8760 begin
8761 -- We need to install the parent instance to compile the instantiation
8762 -- of the child, but the child instance must appear in the current
8763 -- scope. Given that we cannot place the parent above the current scope
8764 -- in the scope stack, we duplicate the current scope and unstack both
8765 -- after the instantiation is complete.
8766
8767 -- If the parent is itself the instantiation of a child unit, we must
8768 -- also stack the instantiation of its parent, and so on. Each such
8769 -- ancestor is the prefix of the name in a prior instantiation.
8770
8771 -- If this is a nested instance, the parent unit itself resolves to
8772 -- a renaming of the parent instance, whose declaration we need.
8773
8774 -- Finally, the parent may be a generic (not an instance) when the
8775 -- child unit appears as a formal package.
8776
8777 Inst_Par := P;
8778
8779 if Present (Renamed_Entity (Inst_Par)) then
8780 Inst_Par := Renamed_Entity (Inst_Par);
8781 end if;
8782
8783 First_Par := Inst_Par;
8784
8785 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8786
8787 First_Gen := Gen_Par;
8788
8789 while Present (Gen_Par)
8790 and then Is_Child_Unit (Gen_Par)
8791 loop
8792 -- Load grandparent instance as well
8793
8794 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8795
8796 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8797 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8798
8799 if Present (Renamed_Entity (Inst_Par)) then
8800 Inst_Par := Renamed_Entity (Inst_Par);
8801 end if;
8802
8803 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8804
8805 if Present (Gen_Par) then
8806 Prepend_Elmt (Inst_Par, Ancestors);
8807
8808 else
8809 -- Parent is not the name of an instantiation
8810
8811 Install_Noninstance_Specs (Inst_Par);
8812 exit;
8813 end if;
8814
8815 else
8816 -- Previous error
8817
8818 exit;
8819 end if;
8820 end loop;
8821
8822 if Present (First_Gen) then
8823 Append_Elmt (First_Par, Ancestors);
8824 else
8825 Install_Noninstance_Specs (First_Par);
8826 end if;
8827
8828 if not Is_Empty_Elmt_List (Ancestors) then
8829 Elmt := First_Elmt (Ancestors);
8830 while Present (Elmt) loop
8831 Install_Spec (Node (Elmt));
8832 Install_Formal_Packages (Node (Elmt));
8833 Next_Elmt (Elmt);
8834 end loop;
8835 end if;
8836
8837 if not In_Body then
8838 Push_Scope (S);
8839 end if;
8840 end Install_Parent;
8841
8842 -------------------------------
8843 -- Install_Hidden_Primitives --
8844 -------------------------------
8845
8846 procedure Install_Hidden_Primitives
8847 (Prims_List : in out Elist_Id;
8848 Gen_T : Entity_Id;
8849 Act_T : Entity_Id)
8850 is
8851 Elmt : Elmt_Id;
8852 List : Elist_Id := No_Elist;
8853 Prim_G_Elmt : Elmt_Id;
8854 Prim_A_Elmt : Elmt_Id;
8855 Prim_G : Node_Id;
8856 Prim_A : Node_Id;
8857
8858 begin
8859 -- No action needed in case of serious errors because we cannot trust
8860 -- in the order of primitives
8861
8862 if Serious_Errors_Detected > 0 then
8863 return;
8864
8865 -- No action possible if we don't have available the list of primitive
8866 -- operations
8867
8868 elsif No (Gen_T)
8869 or else not Is_Record_Type (Gen_T)
8870 or else not Is_Tagged_Type (Gen_T)
8871 or else not Is_Record_Type (Act_T)
8872 or else not Is_Tagged_Type (Act_T)
8873 then
8874 return;
8875
8876 -- There is no need to handle interface types since their primitives
8877 -- cannot be hidden
8878
8879 elsif Is_Interface (Gen_T) then
8880 return;
8881 end if;
8882
8883 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
8884
8885 if not Is_Class_Wide_Type (Act_T) then
8886 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
8887 else
8888 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
8889 end if;
8890
8891 loop
8892 -- Skip predefined primitives in the generic formal
8893
8894 while Present (Prim_G_Elmt)
8895 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
8896 loop
8897 Next_Elmt (Prim_G_Elmt);
8898 end loop;
8899
8900 -- Skip predefined primitives in the generic actual
8901
8902 while Present (Prim_A_Elmt)
8903 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
8904 loop
8905 Next_Elmt (Prim_A_Elmt);
8906 end loop;
8907
8908 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
8909
8910 Prim_G := Node (Prim_G_Elmt);
8911 Prim_A := Node (Prim_A_Elmt);
8912
8913 -- There is no need to handle interface primitives because their
8914 -- primitives are not hidden
8915
8916 exit when Present (Interface_Alias (Prim_G));
8917
8918 -- Here we install one hidden primitive
8919
8920 if Chars (Prim_G) /= Chars (Prim_A)
8921 and then Has_Suffix (Prim_A, 'P')
8922 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
8923 then
8924 Set_Chars (Prim_A, Chars (Prim_G));
8925
8926 if List = No_Elist then
8927 List := New_Elmt_List;
8928 end if;
8929
8930 Append_Elmt (Prim_A, List);
8931 end if;
8932
8933 Next_Elmt (Prim_A_Elmt);
8934 Next_Elmt (Prim_G_Elmt);
8935 end loop;
8936
8937 -- Append the elements to the list of temporarily visible primitives
8938 -- avoiding duplicates.
8939
8940 if Present (List) then
8941 if No (Prims_List) then
8942 Prims_List := New_Elmt_List;
8943 end if;
8944
8945 Elmt := First_Elmt (List);
8946 while Present (Elmt) loop
8947 Append_Unique_Elmt (Node (Elmt), Prims_List);
8948 Next_Elmt (Elmt);
8949 end loop;
8950 end if;
8951 end Install_Hidden_Primitives;
8952
8953 -------------------------------
8954 -- Restore_Hidden_Primitives --
8955 -------------------------------
8956
8957 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
8958 Prim_Elmt : Elmt_Id;
8959 Prim : Node_Id;
8960
8961 begin
8962 if Prims_List /= No_Elist then
8963 Prim_Elmt := First_Elmt (Prims_List);
8964 while Present (Prim_Elmt) loop
8965 Prim := Node (Prim_Elmt);
8966 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
8967 Next_Elmt (Prim_Elmt);
8968 end loop;
8969
8970 Prims_List := No_Elist;
8971 end if;
8972 end Restore_Hidden_Primitives;
8973
8974 --------------------------------
8975 -- Instantiate_Formal_Package --
8976 --------------------------------
8977
8978 function Instantiate_Formal_Package
8979 (Formal : Node_Id;
8980 Actual : Node_Id;
8981 Analyzed_Formal : Node_Id) return List_Id
8982 is
8983 Loc : constant Source_Ptr := Sloc (Actual);
8984 Actual_Pack : Entity_Id;
8985 Formal_Pack : Entity_Id;
8986 Gen_Parent : Entity_Id;
8987 Decls : List_Id;
8988 Nod : Node_Id;
8989 Parent_Spec : Node_Id;
8990
8991 procedure Find_Matching_Actual
8992 (F : Node_Id;
8993 Act : in out Entity_Id);
8994 -- We need to associate each formal entity in the formal package with
8995 -- the corresponding entity in the actual package. The actual package
8996 -- has been analyzed and possibly expanded, and as a result there is
8997 -- no one-to-one correspondence between the two lists (for example,
8998 -- the actual may include subtypes, itypes, and inherited primitive
8999 -- operations, interspersed among the renaming declarations for the
9000 -- actuals) . We retrieve the corresponding actual by name because each
9001 -- actual has the same name as the formal, and they do appear in the
9002 -- same order.
9003
9004 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9005 -- Retrieve entity of defining entity of generic formal parameter.
9006 -- Only the declarations of formals need to be considered when
9007 -- linking them to actuals, but the declarative list may include
9008 -- internal entities generated during analysis, and those are ignored.
9009
9010 procedure Match_Formal_Entity
9011 (Formal_Node : Node_Id;
9012 Formal_Ent : Entity_Id;
9013 Actual_Ent : Entity_Id);
9014 -- Associates the formal entity with the actual. In the case where
9015 -- Formal_Ent is a formal package, this procedure iterates through all
9016 -- of its formals and enters associations between the actuals occurring
9017 -- in the formal package's corresponding actual package (given by
9018 -- Actual_Ent) and the formal package's formal parameters. This
9019 -- procedure recurses if any of the parameters is itself a package.
9020
9021 function Is_Instance_Of
9022 (Act_Spec : Entity_Id;
9023 Gen_Anc : Entity_Id) return Boolean;
9024 -- The actual can be an instantiation of a generic within another
9025 -- instance, in which case there is no direct link from it to the
9026 -- original generic ancestor. In that case, we recognize that the
9027 -- ultimate ancestor is the same by examining names and scopes.
9028
9029 procedure Process_Nested_Formal (Formal : Entity_Id);
9030 -- If the current formal is declared with a box, its own formals are
9031 -- visible in the instance, as they were in the generic, and their
9032 -- Hidden flag must be reset. If some of these formals are themselves
9033 -- packages declared with a box, the processing must be recursive.
9034
9035 --------------------------
9036 -- Find_Matching_Actual --
9037 --------------------------
9038
9039 procedure Find_Matching_Actual
9040 (F : Node_Id;
9041 Act : in out Entity_Id)
9042 is
9043 Formal_Ent : Entity_Id;
9044
9045 begin
9046 case Nkind (Original_Node (F)) is
9047 when N_Formal_Object_Declaration |
9048 N_Formal_Type_Declaration =>
9049 Formal_Ent := Defining_Identifier (F);
9050
9051 while Chars (Act) /= Chars (Formal_Ent) loop
9052 Next_Entity (Act);
9053 end loop;
9054
9055 when N_Formal_Subprogram_Declaration |
9056 N_Formal_Package_Declaration |
9057 N_Package_Declaration |
9058 N_Generic_Package_Declaration =>
9059 Formal_Ent := Defining_Entity (F);
9060
9061 while Chars (Act) /= Chars (Formal_Ent) loop
9062 Next_Entity (Act);
9063 end loop;
9064
9065 when others =>
9066 raise Program_Error;
9067 end case;
9068 end Find_Matching_Actual;
9069
9070 -------------------------
9071 -- Match_Formal_Entity --
9072 -------------------------
9073
9074 procedure Match_Formal_Entity
9075 (Formal_Node : Node_Id;
9076 Formal_Ent : Entity_Id;
9077 Actual_Ent : Entity_Id)
9078 is
9079 Act_Pkg : Entity_Id;
9080
9081 begin
9082 Set_Instance_Of (Formal_Ent, Actual_Ent);
9083
9084 if Ekind (Actual_Ent) = E_Package then
9085
9086 -- Record associations for each parameter
9087
9088 Act_Pkg := Actual_Ent;
9089
9090 declare
9091 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9092 F_Ent : Entity_Id;
9093 F_Node : Node_Id;
9094
9095 Gen_Decl : Node_Id;
9096 Formals : List_Id;
9097 Actual : Entity_Id;
9098
9099 begin
9100 -- Retrieve the actual given in the formal package declaration
9101
9102 Actual := Entity (Name (Original_Node (Formal_Node)));
9103
9104 -- The actual in the formal package declaration may be a
9105 -- renamed generic package, in which case we want to retrieve
9106 -- the original generic in order to traverse its formal part.
9107
9108 if Present (Renamed_Entity (Actual)) then
9109 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9110 else
9111 Gen_Decl := Unit_Declaration_Node (Actual);
9112 end if;
9113
9114 Formals := Generic_Formal_Declarations (Gen_Decl);
9115
9116 if Present (Formals) then
9117 F_Node := First_Non_Pragma (Formals);
9118 else
9119 F_Node := Empty;
9120 end if;
9121
9122 while Present (A_Ent)
9123 and then Present (F_Node)
9124 and then A_Ent /= First_Private_Entity (Act_Pkg)
9125 loop
9126 F_Ent := Get_Formal_Entity (F_Node);
9127
9128 if Present (F_Ent) then
9129
9130 -- This is a formal of the original package. Record
9131 -- association and recurse.
9132
9133 Find_Matching_Actual (F_Node, A_Ent);
9134 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9135 Next_Entity (A_Ent);
9136 end if;
9137
9138 Next_Non_Pragma (F_Node);
9139 end loop;
9140 end;
9141 end if;
9142 end Match_Formal_Entity;
9143
9144 -----------------------
9145 -- Get_Formal_Entity --
9146 -----------------------
9147
9148 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9149 Kind : constant Node_Kind := Nkind (Original_Node (N));
9150 begin
9151 case Kind is
9152 when N_Formal_Object_Declaration =>
9153 return Defining_Identifier (N);
9154
9155 when N_Formal_Type_Declaration =>
9156 return Defining_Identifier (N);
9157
9158 when N_Formal_Subprogram_Declaration =>
9159 return Defining_Unit_Name (Specification (N));
9160
9161 when N_Formal_Package_Declaration =>
9162 return Defining_Identifier (Original_Node (N));
9163
9164 when N_Generic_Package_Declaration =>
9165 return Defining_Identifier (Original_Node (N));
9166
9167 -- All other declarations are introduced by semantic analysis and
9168 -- have no match in the actual.
9169
9170 when others =>
9171 return Empty;
9172 end case;
9173 end Get_Formal_Entity;
9174
9175 --------------------
9176 -- Is_Instance_Of --
9177 --------------------
9178
9179 function Is_Instance_Of
9180 (Act_Spec : Entity_Id;
9181 Gen_Anc : Entity_Id) return Boolean
9182 is
9183 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9184
9185 begin
9186 if No (Gen_Par) then
9187 return False;
9188
9189 -- Simplest case: the generic parent of the actual is the formal
9190
9191 elsif Gen_Par = Gen_Anc then
9192 return True;
9193
9194 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9195 return False;
9196
9197 -- The actual may be obtained through several instantiations. Its
9198 -- scope must itself be an instance of a generic declared in the
9199 -- same scope as the formal. Any other case is detected above.
9200
9201 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9202 return False;
9203
9204 else
9205 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9206 end if;
9207 end Is_Instance_Of;
9208
9209 ---------------------------
9210 -- Process_Nested_Formal --
9211 ---------------------------
9212
9213 procedure Process_Nested_Formal (Formal : Entity_Id) is
9214 Ent : Entity_Id;
9215
9216 begin
9217 if Present (Associated_Formal_Package (Formal))
9218 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9219 then
9220 Ent := First_Entity (Formal);
9221 while Present (Ent) loop
9222 Set_Is_Hidden (Ent, False);
9223 Set_Is_Visible_Formal (Ent);
9224 Set_Is_Potentially_Use_Visible
9225 (Ent, Is_Potentially_Use_Visible (Formal));
9226
9227 if Ekind (Ent) = E_Package then
9228 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9229 Process_Nested_Formal (Ent);
9230 end if;
9231
9232 Next_Entity (Ent);
9233 end loop;
9234 end if;
9235 end Process_Nested_Formal;
9236
9237 -- Start of processing for Instantiate_Formal_Package
9238
9239 begin
9240 Analyze (Actual);
9241
9242 if not Is_Entity_Name (Actual)
9243 or else Ekind (Entity (Actual)) /= E_Package
9244 then
9245 Error_Msg_N
9246 ("expect package instance to instantiate formal", Actual);
9247 Abandon_Instantiation (Actual);
9248 raise Program_Error;
9249
9250 else
9251 Actual_Pack := Entity (Actual);
9252 Set_Is_Instantiated (Actual_Pack);
9253
9254 -- The actual may be a renamed package, or an outer generic formal
9255 -- package whose instantiation is converted into a renaming.
9256
9257 if Present (Renamed_Object (Actual_Pack)) then
9258 Actual_Pack := Renamed_Object (Actual_Pack);
9259 end if;
9260
9261 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9262 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9263 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9264 else
9265 Gen_Parent :=
9266 Generic_Parent (Specification (Analyzed_Formal));
9267 Formal_Pack :=
9268 Defining_Unit_Name (Specification (Analyzed_Formal));
9269 end if;
9270
9271 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9272 Parent_Spec := Package_Specification (Actual_Pack);
9273 else
9274 Parent_Spec := Parent (Actual_Pack);
9275 end if;
9276
9277 if Gen_Parent = Any_Id then
9278 Error_Msg_N
9279 ("previous error in declaration of formal package", Actual);
9280 Abandon_Instantiation (Actual);
9281
9282 elsif
9283 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9284 then
9285 null;
9286
9287 else
9288 Error_Msg_NE
9289 ("actual parameter must be instance of&", Actual, Gen_Parent);
9290 Abandon_Instantiation (Actual);
9291 end if;
9292
9293 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9294 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9295
9296 Nod :=
9297 Make_Package_Renaming_Declaration (Loc,
9298 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9299 Name => New_Occurrence_Of (Actual_Pack, Loc));
9300
9301 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
9302 Defining_Identifier (Formal));
9303 Decls := New_List (Nod);
9304
9305 -- If the formal F has a box, then the generic declarations are
9306 -- visible in the generic G. In an instance of G, the corresponding
9307 -- entities in the actual for F (which are the actuals for the
9308 -- instantiation of the generic that F denotes) must also be made
9309 -- visible for analysis of the current instance. On exit from the
9310 -- current instance, those entities are made private again. If the
9311 -- actual is currently in use, these entities are also use-visible.
9312
9313 -- The loop through the actual entities also steps through the formal
9314 -- entities and enters associations from formals to actuals into the
9315 -- renaming map. This is necessary to properly handle checking of
9316 -- actual parameter associations for later formals that depend on
9317 -- actuals declared in the formal package.
9318
9319 -- In Ada 2005, partial parameterization requires that we make
9320 -- visible the actuals corresponding to formals that were defaulted
9321 -- in the formal package. There formals are identified because they
9322 -- remain formal generics within the formal package, rather than
9323 -- being renamings of the actuals supplied.
9324
9325 declare
9326 Gen_Decl : constant Node_Id :=
9327 Unit_Declaration_Node (Gen_Parent);
9328 Formals : constant List_Id :=
9329 Generic_Formal_Declarations (Gen_Decl);
9330
9331 Actual_Ent : Entity_Id;
9332 Actual_Of_Formal : Node_Id;
9333 Formal_Node : Node_Id;
9334 Formal_Ent : Entity_Id;
9335
9336 begin
9337 if Present (Formals) then
9338 Formal_Node := First_Non_Pragma (Formals);
9339 else
9340 Formal_Node := Empty;
9341 end if;
9342
9343 Actual_Ent := First_Entity (Actual_Pack);
9344 Actual_Of_Formal :=
9345 First (Visible_Declarations (Specification (Analyzed_Formal)));
9346 while Present (Actual_Ent)
9347 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9348 loop
9349 if Present (Formal_Node) then
9350 Formal_Ent := Get_Formal_Entity (Formal_Node);
9351
9352 if Present (Formal_Ent) then
9353 Find_Matching_Actual (Formal_Node, Actual_Ent);
9354 Match_Formal_Entity
9355 (Formal_Node, Formal_Ent, Actual_Ent);
9356
9357 -- We iterate at the same time over the actuals of the
9358 -- local package created for the formal, to determine
9359 -- which one of the formals of the original generic were
9360 -- defaulted in the formal. The corresponding actual
9361 -- entities are visible in the enclosing instance.
9362
9363 if Box_Present (Formal)
9364 or else
9365 (Present (Actual_Of_Formal)
9366 and then
9367 Is_Generic_Formal
9368 (Get_Formal_Entity (Actual_Of_Formal)))
9369 then
9370 Set_Is_Hidden (Actual_Ent, False);
9371 Set_Is_Visible_Formal (Actual_Ent);
9372 Set_Is_Potentially_Use_Visible
9373 (Actual_Ent, In_Use (Actual_Pack));
9374
9375 if Ekind (Actual_Ent) = E_Package then
9376 Process_Nested_Formal (Actual_Ent);
9377 end if;
9378
9379 else
9380 Set_Is_Hidden (Actual_Ent);
9381 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9382 end if;
9383 end if;
9384
9385 Next_Non_Pragma (Formal_Node);
9386 Next (Actual_Of_Formal);
9387
9388 else
9389 -- No further formals to match, but the generic part may
9390 -- contain inherited operation that are not hidden in the
9391 -- enclosing instance.
9392
9393 Next_Entity (Actual_Ent);
9394 end if;
9395 end loop;
9396
9397 -- Inherited subprograms generated by formal derived types are
9398 -- also visible if the types are.
9399
9400 Actual_Ent := First_Entity (Actual_Pack);
9401 while Present (Actual_Ent)
9402 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9403 loop
9404 if Is_Overloadable (Actual_Ent)
9405 and then
9406 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9407 and then
9408 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9409 then
9410 Set_Is_Hidden (Actual_Ent, False);
9411 Set_Is_Potentially_Use_Visible
9412 (Actual_Ent, In_Use (Actual_Pack));
9413 end if;
9414
9415 Next_Entity (Actual_Ent);
9416 end loop;
9417 end;
9418
9419 -- If the formal is not declared with a box, reanalyze it as an
9420 -- abbreviated instantiation, to verify the matching rules of 12.7.
9421 -- The actual checks are performed after the generic associations
9422 -- have been analyzed, to guarantee the same visibility for this
9423 -- instantiation and for the actuals.
9424
9425 -- In Ada 2005, the generic associations for the formal can include
9426 -- defaulted parameters. These are ignored during check. This
9427 -- internal instantiation is removed from the tree after conformance
9428 -- checking, because it contains formal declarations for those
9429 -- defaulted parameters, and those should not reach the back-end.
9430
9431 if not Box_Present (Formal) then
9432 declare
9433 I_Pack : constant Entity_Id :=
9434 Make_Temporary (Sloc (Actual), 'P');
9435
9436 begin
9437 Set_Is_Internal (I_Pack);
9438
9439 Append_To (Decls,
9440 Make_Package_Instantiation (Sloc (Actual),
9441 Defining_Unit_Name => I_Pack,
9442 Name =>
9443 New_Occurrence_Of
9444 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9445 Generic_Associations =>
9446 Generic_Associations (Formal)));
9447 end;
9448 end if;
9449
9450 return Decls;
9451 end if;
9452 end Instantiate_Formal_Package;
9453
9454 -----------------------------------
9455 -- Instantiate_Formal_Subprogram --
9456 -----------------------------------
9457
9458 function Instantiate_Formal_Subprogram
9459 (Formal : Node_Id;
9460 Actual : Node_Id;
9461 Analyzed_Formal : Node_Id) return Node_Id
9462 is
9463 Loc : Source_Ptr;
9464 Formal_Sub : constant Entity_Id :=
9465 Defining_Unit_Name (Specification (Formal));
9466 Analyzed_S : constant Entity_Id :=
9467 Defining_Unit_Name (Specification (Analyzed_Formal));
9468 Decl_Node : Node_Id;
9469 Nam : Node_Id;
9470 New_Spec : Node_Id;
9471
9472 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9473 -- If the generic is a child unit, the parent has been installed on the
9474 -- scope stack, but a default subprogram cannot resolve to something
9475 -- on the parent because that parent is not really part of the visible
9476 -- context (it is there to resolve explicit local entities). If the
9477 -- default has resolved in this way, we remove the entity from immediate
9478 -- visibility and analyze the node again to emit an error message or
9479 -- find another visible candidate.
9480
9481 procedure Valid_Actual_Subprogram (Act : Node_Id);
9482 -- Perform legality check and raise exception on failure
9483
9484 -----------------------
9485 -- From_Parent_Scope --
9486 -----------------------
9487
9488 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9489 Gen_Scope : Node_Id;
9490
9491 begin
9492 Gen_Scope := Scope (Analyzed_S);
9493 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9494 if Scope (Subp) = Scope (Gen_Scope) then
9495 return True;
9496 end if;
9497
9498 Gen_Scope := Scope (Gen_Scope);
9499 end loop;
9500
9501 return False;
9502 end From_Parent_Scope;
9503
9504 -----------------------------
9505 -- Valid_Actual_Subprogram --
9506 -----------------------------
9507
9508 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9509 Act_E : Entity_Id;
9510
9511 begin
9512 if Is_Entity_Name (Act) then
9513 Act_E := Entity (Act);
9514
9515 elsif Nkind (Act) = N_Selected_Component
9516 and then Is_Entity_Name (Selector_Name (Act))
9517 then
9518 Act_E := Entity (Selector_Name (Act));
9519
9520 else
9521 Act_E := Empty;
9522 end if;
9523
9524 if (Present (Act_E) and then Is_Overloadable (Act_E))
9525 or else Nkind_In (Act, N_Attribute_Reference,
9526 N_Indexed_Component,
9527 N_Character_Literal,
9528 N_Explicit_Dereference)
9529 then
9530 return;
9531 end if;
9532
9533 Error_Msg_NE
9534 ("expect subprogram or entry name in instantiation of&",
9535 Instantiation_Node, Formal_Sub);
9536 Abandon_Instantiation (Instantiation_Node);
9537
9538 end Valid_Actual_Subprogram;
9539
9540 -- Start of processing for Instantiate_Formal_Subprogram
9541
9542 begin
9543 New_Spec := New_Copy_Tree (Specification (Formal));
9544
9545 -- The tree copy has created the proper instantiation sloc for the
9546 -- new specification. Use this location for all other constructed
9547 -- declarations.
9548
9549 Loc := Sloc (Defining_Unit_Name (New_Spec));
9550
9551 -- Create new entity for the actual (New_Copy_Tree does not)
9552
9553 Set_Defining_Unit_Name
9554 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9555
9556 -- Create new entities for the each of the formals in the
9557 -- specification of the renaming declaration built for the actual.
9558
9559 if Present (Parameter_Specifications (New_Spec)) then
9560 declare
9561 F : Node_Id;
9562 begin
9563 F := First (Parameter_Specifications (New_Spec));
9564 while Present (F) loop
9565 Set_Defining_Identifier (F,
9566 Make_Defining_Identifier (Sloc (F),
9567 Chars => Chars (Defining_Identifier (F))));
9568 Next (F);
9569 end loop;
9570 end;
9571 end if;
9572
9573 -- Find entity of actual. If the actual is an attribute reference, it
9574 -- cannot be resolved here (its formal is missing) but is handled
9575 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9576 -- fully resolved subsequently, when the renaming declaration for the
9577 -- formal is analyzed. If it is an explicit dereference, resolve the
9578 -- prefix but not the actual itself, to prevent interpretation as call.
9579
9580 if Present (Actual) then
9581 Loc := Sloc (Actual);
9582 Set_Sloc (New_Spec, Loc);
9583
9584 if Nkind (Actual) = N_Operator_Symbol then
9585 Find_Direct_Name (Actual);
9586
9587 elsif Nkind (Actual) = N_Explicit_Dereference then
9588 Analyze (Prefix (Actual));
9589
9590 elsif Nkind (Actual) /= N_Attribute_Reference then
9591 Analyze (Actual);
9592 end if;
9593
9594 Valid_Actual_Subprogram (Actual);
9595 Nam := Actual;
9596
9597 elsif Present (Default_Name (Formal)) then
9598 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9599 N_Selected_Component,
9600 N_Indexed_Component,
9601 N_Character_Literal)
9602 and then Present (Entity (Default_Name (Formal)))
9603 then
9604 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9605 else
9606 Nam := New_Copy (Default_Name (Formal));
9607 Set_Sloc (Nam, Loc);
9608 end if;
9609
9610 elsif Box_Present (Formal) then
9611
9612 -- Actual is resolved at the point of instantiation. Create an
9613 -- identifier or operator with the same name as the formal.
9614
9615 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
9616 Nam := Make_Operator_Symbol (Loc,
9617 Chars => Chars (Formal_Sub),
9618 Strval => No_String);
9619 else
9620 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
9621 end if;
9622
9623 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
9624 and then Null_Present (Specification (Formal))
9625 then
9626 -- Generate null body for procedure, for use in the instance
9627
9628 Decl_Node :=
9629 Make_Subprogram_Body (Loc,
9630 Specification => New_Spec,
9631 Declarations => New_List,
9632 Handled_Statement_Sequence =>
9633 Make_Handled_Sequence_Of_Statements (Loc,
9634 Statements => New_List (Make_Null_Statement (Loc))));
9635
9636 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
9637 return Decl_Node;
9638
9639 else
9640 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
9641 Error_Msg_NE
9642 ("missing actual&", Instantiation_Node, Formal_Sub);
9643 Error_Msg_NE
9644 ("\in instantiation of & declared#",
9645 Instantiation_Node, Scope (Analyzed_S));
9646 Abandon_Instantiation (Instantiation_Node);
9647 end if;
9648
9649 Decl_Node :=
9650 Make_Subprogram_Renaming_Declaration (Loc,
9651 Specification => New_Spec,
9652 Name => Nam);
9653
9654 -- If we do not have an actual and the formal specified <> then set to
9655 -- get proper default.
9656
9657 if No (Actual) and then Box_Present (Formal) then
9658 Set_From_Default (Decl_Node);
9659 end if;
9660
9661 -- Gather possible interpretations for the actual before analyzing the
9662 -- instance. If overloaded, it will be resolved when analyzing the
9663 -- renaming declaration.
9664
9665 if Box_Present (Formal)
9666 and then No (Actual)
9667 then
9668 Analyze (Nam);
9669
9670 if Is_Child_Unit (Scope (Analyzed_S))
9671 and then Present (Entity (Nam))
9672 then
9673 if not Is_Overloaded (Nam) then
9674 if From_Parent_Scope (Entity (Nam)) then
9675 Set_Is_Immediately_Visible (Entity (Nam), False);
9676 Set_Entity (Nam, Empty);
9677 Set_Etype (Nam, Empty);
9678
9679 Analyze (Nam);
9680 Set_Is_Immediately_Visible (Entity (Nam));
9681 end if;
9682
9683 else
9684 declare
9685 I : Interp_Index;
9686 It : Interp;
9687
9688 begin
9689 Get_First_Interp (Nam, I, It);
9690 while Present (It.Nam) loop
9691 if From_Parent_Scope (It.Nam) then
9692 Remove_Interp (I);
9693 end if;
9694
9695 Get_Next_Interp (I, It);
9696 end loop;
9697 end;
9698 end if;
9699 end if;
9700 end if;
9701
9702 -- The generic instantiation freezes the actual. This can only be done
9703 -- once the actual is resolved, in the analysis of the renaming
9704 -- declaration. To make the formal subprogram entity available, we set
9705 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9706 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9707 -- of formal abstract subprograms.
9708
9709 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9710
9711 -- We cannot analyze the renaming declaration, and thus find the actual,
9712 -- until all the actuals are assembled in the instance. For subsequent
9713 -- checks of other actuals, indicate the node that will hold the
9714 -- instance of this formal.
9715
9716 Set_Instance_Of (Analyzed_S, Nam);
9717
9718 if Nkind (Actual) = N_Selected_Component
9719 and then Is_Task_Type (Etype (Prefix (Actual)))
9720 and then not Is_Frozen (Etype (Prefix (Actual)))
9721 then
9722 -- The renaming declaration will create a body, which must appear
9723 -- outside of the instantiation, We move the renaming declaration
9724 -- out of the instance, and create an additional renaming inside,
9725 -- to prevent freezing anomalies.
9726
9727 declare
9728 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9729
9730 begin
9731 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9732 Insert_Before (Instantiation_Node, Decl_Node);
9733 Analyze (Decl_Node);
9734
9735 -- Now create renaming within the instance
9736
9737 Decl_Node :=
9738 Make_Subprogram_Renaming_Declaration (Loc,
9739 Specification => New_Copy_Tree (New_Spec),
9740 Name => New_Occurrence_Of (Anon_Id, Loc));
9741
9742 Set_Defining_Unit_Name (Specification (Decl_Node),
9743 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9744 end;
9745 end if;
9746
9747 return Decl_Node;
9748 end Instantiate_Formal_Subprogram;
9749
9750 ------------------------
9751 -- Instantiate_Object --
9752 ------------------------
9753
9754 function Instantiate_Object
9755 (Formal : Node_Id;
9756 Actual : Node_Id;
9757 Analyzed_Formal : Node_Id) return List_Id
9758 is
9759 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9760 A_Gen_Obj : constant Entity_Id :=
9761 Defining_Identifier (Analyzed_Formal);
9762 Acc_Def : Node_Id := Empty;
9763 Act_Assoc : constant Node_Id := Parent (Actual);
9764 Actual_Decl : Node_Id := Empty;
9765 Decl_Node : Node_Id;
9766 Def : Node_Id;
9767 Ftyp : Entity_Id;
9768 List : constant List_Id := New_List;
9769 Loc : constant Source_Ptr := Sloc (Actual);
9770 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9771 Subt_Decl : Node_Id := Empty;
9772 Subt_Mark : Node_Id := Empty;
9773
9774 begin
9775 if Present (Subtype_Mark (Formal)) then
9776 Subt_Mark := Subtype_Mark (Formal);
9777 else
9778 Check_Access_Definition (Formal);
9779 Acc_Def := Access_Definition (Formal);
9780 end if;
9781
9782 -- Sloc for error message on missing actual
9783
9784 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9785
9786 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9787 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9788 end if;
9789
9790 Set_Parent (List, Parent (Actual));
9791
9792 -- OUT present
9793
9794 if Out_Present (Formal) then
9795
9796 -- An IN OUT generic actual must be a name. The instantiation is a
9797 -- renaming declaration. The actual is the name being renamed. We
9798 -- use the actual directly, rather than a copy, because it is not
9799 -- used further in the list of actuals, and because a copy or a use
9800 -- of relocate_node is incorrect if the instance is nested within a
9801 -- generic. In order to simplify ASIS searches, the Generic_Parent
9802 -- field links the declaration to the generic association.
9803
9804 if No (Actual) then
9805 Error_Msg_NE
9806 ("missing actual&",
9807 Instantiation_Node, Gen_Obj);
9808 Error_Msg_NE
9809 ("\in instantiation of & declared#",
9810 Instantiation_Node, Scope (A_Gen_Obj));
9811 Abandon_Instantiation (Instantiation_Node);
9812 end if;
9813
9814 if Present (Subt_Mark) then
9815 Decl_Node :=
9816 Make_Object_Renaming_Declaration (Loc,
9817 Defining_Identifier => New_Copy (Gen_Obj),
9818 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9819 Name => Actual);
9820
9821 else pragma Assert (Present (Acc_Def));
9822 Decl_Node :=
9823 Make_Object_Renaming_Declaration (Loc,
9824 Defining_Identifier => New_Copy (Gen_Obj),
9825 Access_Definition => New_Copy_Tree (Acc_Def),
9826 Name => Actual);
9827 end if;
9828
9829 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9830
9831 -- The analysis of the actual may produce Insert_Action nodes, so
9832 -- the declaration must have a context in which to attach them.
9833
9834 Append (Decl_Node, List);
9835 Analyze (Actual);
9836
9837 -- Return if the analysis of the actual reported some error
9838
9839 if Etype (Actual) = Any_Type then
9840 return List;
9841 end if;
9842
9843 -- This check is performed here because Analyze_Object_Renaming will
9844 -- not check it when Comes_From_Source is False. Note though that the
9845 -- check for the actual being the name of an object will be performed
9846 -- in Analyze_Object_Renaming.
9847
9848 if Is_Object_Reference (Actual)
9849 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
9850 then
9851 Error_Msg_N
9852 ("illegal discriminant-dependent component for in out parameter",
9853 Actual);
9854 end if;
9855
9856 -- The actual has to be resolved in order to check that it is a
9857 -- variable (due to cases such as F (1), where F returns access to
9858 -- an array, and for overloaded prefixes).
9859
9860 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
9861
9862 -- If the type of the formal is not itself a formal, and the current
9863 -- unit is a child unit, the formal type must be declared in a
9864 -- parent, and must be retrieved by visibility.
9865
9866 if Ftyp = Orig_Ftyp
9867 and then Is_Generic_Unit (Scope (Ftyp))
9868 and then Is_Child_Unit (Scope (A_Gen_Obj))
9869 then
9870 declare
9871 Temp : constant Node_Id :=
9872 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
9873 begin
9874 Set_Entity (Temp, Empty);
9875 Find_Type (Temp);
9876 Ftyp := Entity (Temp);
9877 end;
9878 end if;
9879
9880 if Is_Private_Type (Ftyp)
9881 and then not Is_Private_Type (Etype (Actual))
9882 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
9883 or else Base_Type (Etype (Actual)) = Ftyp)
9884 then
9885 -- If the actual has the type of the full view of the formal, or
9886 -- else a non-private subtype of the formal, then the visibility
9887 -- of the formal type has changed. Add to the actuals a subtype
9888 -- declaration that will force the exchange of views in the body
9889 -- of the instance as well.
9890
9891 Subt_Decl :=
9892 Make_Subtype_Declaration (Loc,
9893 Defining_Identifier => Make_Temporary (Loc, 'P'),
9894 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
9895
9896 Prepend (Subt_Decl, List);
9897
9898 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
9899 Exchange_Declarations (Ftyp);
9900 end if;
9901
9902 Resolve (Actual, Ftyp);
9903
9904 if not Denotes_Variable (Actual) then
9905 Error_Msg_NE
9906 ("actual for& must be a variable", Actual, Gen_Obj);
9907
9908 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
9909
9910 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
9911 -- the type of the actual shall resolve to a specific anonymous
9912 -- access type.
9913
9914 if Ada_Version < Ada_2005
9915 or else
9916 Ekind (Base_Type (Ftyp)) /=
9917 E_Anonymous_Access_Type
9918 or else
9919 Ekind (Base_Type (Etype (Actual))) /=
9920 E_Anonymous_Access_Type
9921 then
9922 Error_Msg_NE ("type of actual does not match type of&",
9923 Actual, Gen_Obj);
9924 end if;
9925 end if;
9926
9927 Note_Possible_Modification (Actual, Sure => True);
9928
9929 -- Check for instantiation of atomic/volatile actual for
9930 -- non-atomic/volatile formal (RM C.6 (12)).
9931
9932 if Is_Atomic_Object (Actual)
9933 and then not Is_Atomic (Orig_Ftyp)
9934 then
9935 Error_Msg_N
9936 ("cannot instantiate non-atomic formal object " &
9937 "with atomic actual", Actual);
9938
9939 elsif Is_Volatile_Object (Actual)
9940 and then not Is_Volatile (Orig_Ftyp)
9941 then
9942 Error_Msg_N
9943 ("cannot instantiate non-volatile formal object " &
9944 "with volatile actual", Actual);
9945 end if;
9946
9947 -- Formal in-parameter
9948
9949 else
9950 -- The instantiation of a generic formal in-parameter is constant
9951 -- declaration. The actual is the expression for that declaration.
9952
9953 if Present (Actual) then
9954 if Present (Subt_Mark) then
9955 Def := Subt_Mark;
9956 else pragma Assert (Present (Acc_Def));
9957 Def := Acc_Def;
9958 end if;
9959
9960 Decl_Node :=
9961 Make_Object_Declaration (Loc,
9962 Defining_Identifier => New_Copy (Gen_Obj),
9963 Constant_Present => True,
9964 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9965 Object_Definition => New_Copy_Tree (Def),
9966 Expression => Actual);
9967
9968 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9969
9970 -- A generic formal object of a tagged type is defined to be
9971 -- aliased so the new constant must also be treated as aliased.
9972
9973 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
9974 Set_Aliased_Present (Decl_Node);
9975 end if;
9976
9977 Append (Decl_Node, List);
9978
9979 -- No need to repeat (pre-)analysis of some expression nodes
9980 -- already handled in Preanalyze_Actuals.
9981
9982 if Nkind (Actual) /= N_Allocator then
9983 Analyze (Actual);
9984
9985 -- Return if the analysis of the actual reported some error
9986
9987 if Etype (Actual) = Any_Type then
9988 return List;
9989 end if;
9990 end if;
9991
9992 declare
9993 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
9994 Typ : Entity_Id;
9995
9996 begin
9997 Typ := Get_Instance_Of (Formal_Type);
9998
9999 Freeze_Before (Instantiation_Node, Typ);
10000
10001 -- If the actual is an aggregate, perform name resolution on
10002 -- its components (the analysis of an aggregate does not do it)
10003 -- to capture local names that may be hidden if the generic is
10004 -- a child unit.
10005
10006 if Nkind (Actual) = N_Aggregate then
10007 Preanalyze_And_Resolve (Actual, Typ);
10008 end if;
10009
10010 if Is_Limited_Type (Typ)
10011 and then not OK_For_Limited_Init (Typ, Actual)
10012 then
10013 Error_Msg_N
10014 ("initialization not allowed for limited types", Actual);
10015 Explain_Limited_Type (Typ, Actual);
10016 end if;
10017 end;
10018
10019 elsif Present (Default_Expression (Formal)) then
10020
10021 -- Use default to construct declaration
10022
10023 if Present (Subt_Mark) then
10024 Def := Subt_Mark;
10025 else pragma Assert (Present (Acc_Def));
10026 Def := Acc_Def;
10027 end if;
10028
10029 Decl_Node :=
10030 Make_Object_Declaration (Sloc (Formal),
10031 Defining_Identifier => New_Copy (Gen_Obj),
10032 Constant_Present => True,
10033 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10034 Object_Definition => New_Copy (Def),
10035 Expression => New_Copy_Tree
10036 (Default_Expression (Formal)));
10037
10038 Append (Decl_Node, List);
10039 Set_Analyzed (Expression (Decl_Node), False);
10040
10041 else
10042 Error_Msg_NE
10043 ("missing actual&",
10044 Instantiation_Node, Gen_Obj);
10045 Error_Msg_NE ("\in instantiation of & declared#",
10046 Instantiation_Node, Scope (A_Gen_Obj));
10047
10048 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10049
10050 -- Create dummy constant declaration so that instance can be
10051 -- analyzed, to minimize cascaded visibility errors.
10052
10053 if Present (Subt_Mark) then
10054 Def := Subt_Mark;
10055 else pragma Assert (Present (Acc_Def));
10056 Def := Acc_Def;
10057 end if;
10058
10059 Decl_Node :=
10060 Make_Object_Declaration (Loc,
10061 Defining_Identifier => New_Copy (Gen_Obj),
10062 Constant_Present => True,
10063 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10064 Object_Definition => New_Copy (Def),
10065 Expression =>
10066 Make_Attribute_Reference (Sloc (Gen_Obj),
10067 Attribute_Name => Name_First,
10068 Prefix => New_Copy (Def)));
10069
10070 Append (Decl_Node, List);
10071
10072 else
10073 Abandon_Instantiation (Instantiation_Node);
10074 end if;
10075 end if;
10076 end if;
10077
10078 if Nkind (Actual) in N_Has_Entity then
10079 Actual_Decl := Parent (Entity (Actual));
10080 end if;
10081
10082 -- Ada 2005 (AI-423): For a formal object declaration with a null
10083 -- exclusion or an access definition that has a null exclusion: If the
10084 -- actual matching the formal object declaration denotes a generic
10085 -- formal object of another generic unit G, and the instantiation
10086 -- containing the actual occurs within the body of G or within the body
10087 -- of a generic unit declared within the declarative region of G, then
10088 -- the declaration of the formal object of G must have a null exclusion.
10089 -- Otherwise, the subtype of the actual matching the formal object
10090 -- declaration shall exclude null.
10091
10092 if Ada_Version >= Ada_2005
10093 and then Present (Actual_Decl)
10094 and then
10095 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10096 N_Object_Declaration)
10097 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10098 and then not Has_Null_Exclusion (Actual_Decl)
10099 and then Has_Null_Exclusion (Analyzed_Formal)
10100 then
10101 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10102 Error_Msg_N
10103 ("actual must exclude null to match generic formal#", Actual);
10104 end if;
10105
10106 -- An effectively volatile object cannot be used as an actual in
10107 -- a generic instance. The following check is only relevant when
10108 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10109
10110 if SPARK_Mode = On
10111 and then Present (Actual)
10112 and then Is_Effectively_Volatile_Object (Actual)
10113 then
10114 Error_Msg_N
10115 ("volatile object cannot act as actual in generic instantiation "
10116 & "(SPARK RM 7.1.3(8))", Actual);
10117 end if;
10118
10119 return List;
10120 end Instantiate_Object;
10121
10122 ------------------------------
10123 -- Instantiate_Package_Body --
10124 ------------------------------
10125
10126 procedure Instantiate_Package_Body
10127 (Body_Info : Pending_Body_Info;
10128 Inlined_Body : Boolean := False;
10129 Body_Optional : Boolean := False)
10130 is
10131 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10132 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10133 Loc : constant Source_Ptr := Sloc (Inst_Node);
10134
10135 Gen_Id : constant Node_Id := Name (Inst_Node);
10136 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10137 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10138 Act_Spec : constant Node_Id := Specification (Act_Decl);
10139 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10140
10141 Act_Body_Name : Node_Id;
10142 Gen_Body : Node_Id;
10143 Gen_Body_Id : Node_Id;
10144 Act_Body : Node_Id;
10145 Act_Body_Id : Entity_Id;
10146
10147 Parent_Installed : Boolean := False;
10148 Save_Style_Check : constant Boolean := Style_Check;
10149
10150 Par_Ent : Entity_Id := Empty;
10151 Par_Vis : Boolean := False;
10152
10153 Vis_Prims_List : Elist_Id := No_Elist;
10154 -- List of primitives made temporarily visible in the instantiation
10155 -- to match the visibility of the formal type
10156
10157 procedure Check_Initialized_Types;
10158 -- In a generic package body, an entity of a generic private type may
10159 -- appear uninitialized. This is suspicious, unless the actual is a
10160 -- fully initialized type.
10161
10162 -----------------------------
10163 -- Check_Initialized_Types --
10164 -----------------------------
10165
10166 procedure Check_Initialized_Types is
10167 Decl : Node_Id;
10168 Formal : Entity_Id;
10169 Actual : Entity_Id;
10170 Uninit_Var : Entity_Id;
10171
10172 begin
10173 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10174 while Present (Decl) loop
10175 Uninit_Var := Empty;
10176
10177 if Nkind (Decl) = N_Private_Extension_Declaration then
10178 Uninit_Var := Uninitialized_Variable (Decl);
10179
10180 elsif Nkind (Decl) = N_Formal_Type_Declaration
10181 and then Nkind (Formal_Type_Definition (Decl)) =
10182 N_Formal_Private_Type_Definition
10183 then
10184 Uninit_Var :=
10185 Uninitialized_Variable (Formal_Type_Definition (Decl));
10186 end if;
10187
10188 if Present (Uninit_Var) then
10189 Formal := Defining_Identifier (Decl);
10190 Actual := First_Entity (Act_Decl_Id);
10191
10192 -- For each formal there is a subtype declaration that renames
10193 -- the actual and has the same name as the formal. Locate the
10194 -- formal for warning message about uninitialized variables
10195 -- in the generic, for which the actual type should be a fully
10196 -- initialized type.
10197
10198 while Present (Actual) loop
10199 exit when Ekind (Actual) = E_Package
10200 and then Present (Renamed_Object (Actual));
10201
10202 if Chars (Actual) = Chars (Formal)
10203 and then not Is_Scalar_Type (Actual)
10204 and then not Is_Fully_Initialized_Type (Actual)
10205 and then Warn_On_No_Value_Assigned
10206 then
10207 Error_Msg_Node_2 := Formal;
10208 Error_Msg_NE
10209 ("generic unit has uninitialized variable& of "
10210 & "formal private type &?v?", Actual, Uninit_Var);
10211 Error_Msg_NE
10212 ("actual type for& should be fully initialized type?v?",
10213 Actual, Formal);
10214 exit;
10215 end if;
10216
10217 Next_Entity (Actual);
10218 end loop;
10219 end if;
10220
10221 Next (Decl);
10222 end loop;
10223 end Check_Initialized_Types;
10224
10225 -- Start of processing for Instantiate_Package_Body
10226
10227 begin
10228 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10229
10230 -- The instance body may already have been processed, as the parent of
10231 -- another instance that is inlined (Load_Parent_Of_Generic).
10232
10233 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10234 return;
10235 end if;
10236
10237 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10238
10239 -- Re-establish the state of information on which checks are suppressed.
10240 -- This information was set in Body_Info at the point of instantiation,
10241 -- and now we restore it so that the instance is compiled using the
10242 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10243
10244 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10245 Scope_Suppress := Body_Info.Scope_Suppress;
10246 Opt.Ada_Version := Body_Info.Version;
10247 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10248 Restore_Warnings (Body_Info.Warnings);
10249 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10250 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10251
10252 if No (Gen_Body_Id) then
10253
10254 -- Do not look for parent of generic body if none is required.
10255 -- This may happen when the routine is called as part of the
10256 -- Pending_Instantiations processing, when nested instances
10257 -- may precede the one generated from the main unit.
10258
10259 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10260 and then Body_Optional
10261 then
10262 return;
10263 else
10264 Load_Parent_Of_Generic
10265 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10266 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10267 end if;
10268 end if;
10269
10270 -- Establish global variable for sloc adjustment and for error recovery
10271
10272 Instantiation_Node := Inst_Node;
10273
10274 if Present (Gen_Body_Id) then
10275 Save_Env (Gen_Unit, Act_Decl_Id);
10276 Style_Check := False;
10277 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10278
10279 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10280
10281 Create_Instantiation_Source
10282 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10283
10284 Act_Body :=
10285 Copy_Generic_Node
10286 (Original_Node (Gen_Body), Empty, Instantiating => True);
10287
10288 -- Build new name (possibly qualified) for body declaration
10289
10290 Act_Body_Id := New_Copy (Act_Decl_Id);
10291
10292 -- Some attributes of spec entity are not inherited by body entity
10293
10294 Set_Handler_Records (Act_Body_Id, No_List);
10295
10296 if Nkind (Defining_Unit_Name (Act_Spec)) =
10297 N_Defining_Program_Unit_Name
10298 then
10299 Act_Body_Name :=
10300 Make_Defining_Program_Unit_Name (Loc,
10301 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10302 Defining_Identifier => Act_Body_Id);
10303 else
10304 Act_Body_Name := Act_Body_Id;
10305 end if;
10306
10307 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10308
10309 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10310 Check_Generic_Actuals (Act_Decl_Id, False);
10311 Check_Initialized_Types;
10312
10313 -- Install primitives hidden at the point of the instantiation but
10314 -- visible when processing the generic formals
10315
10316 declare
10317 E : Entity_Id;
10318
10319 begin
10320 E := First_Entity (Act_Decl_Id);
10321 while Present (E) loop
10322 if Is_Type (E)
10323 and then Is_Generic_Actual_Type (E)
10324 and then Is_Tagged_Type (E)
10325 then
10326 Install_Hidden_Primitives
10327 (Prims_List => Vis_Prims_List,
10328 Gen_T => Generic_Parent_Type (Parent (E)),
10329 Act_T => E);
10330 end if;
10331
10332 Next_Entity (E);
10333 end loop;
10334 end;
10335
10336 -- If it is a child unit, make the parent instance (which is an
10337 -- instance of the parent of the generic) visible. The parent
10338 -- instance is the prefix of the name of the generic unit.
10339
10340 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10341 and then Nkind (Gen_Id) = N_Expanded_Name
10342 then
10343 Par_Ent := Entity (Prefix (Gen_Id));
10344 Par_Vis := Is_Immediately_Visible (Par_Ent);
10345 Install_Parent (Par_Ent, In_Body => True);
10346 Parent_Installed := True;
10347
10348 elsif Is_Child_Unit (Gen_Unit) then
10349 Par_Ent := Scope (Gen_Unit);
10350 Par_Vis := Is_Immediately_Visible (Par_Ent);
10351 Install_Parent (Par_Ent, In_Body => True);
10352 Parent_Installed := True;
10353 end if;
10354
10355 -- If the instantiation is a library unit, and this is the main unit,
10356 -- then build the resulting compilation unit nodes for the instance.
10357 -- If this is a compilation unit but it is not the main unit, then it
10358 -- is the body of a unit in the context, that is being compiled
10359 -- because it is encloses some inlined unit or another generic unit
10360 -- being instantiated. In that case, this body is not part of the
10361 -- current compilation, and is not attached to the tree, but its
10362 -- parent must be set for analysis.
10363
10364 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10365
10366 -- Replace instance node with body of instance, and create new
10367 -- node for corresponding instance declaration.
10368
10369 Build_Instance_Compilation_Unit_Nodes
10370 (Inst_Node, Act_Body, Act_Decl);
10371 Analyze (Inst_Node);
10372
10373 if Parent (Inst_Node) = Cunit (Main_Unit) then
10374
10375 -- If the instance is a child unit itself, then set the scope
10376 -- of the expanded body to be the parent of the instantiation
10377 -- (ensuring that the fully qualified name will be generated
10378 -- for the elaboration subprogram).
10379
10380 if Nkind (Defining_Unit_Name (Act_Spec)) =
10381 N_Defining_Program_Unit_Name
10382 then
10383 Set_Scope
10384 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10385 end if;
10386 end if;
10387
10388 -- Case where instantiation is not a library unit
10389
10390 else
10391 -- If this is an early instantiation, i.e. appears textually
10392 -- before the corresponding body and must be elaborated first,
10393 -- indicate that the body instance is to be delayed.
10394
10395 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10396
10397 -- Now analyze the body. We turn off all checks if this is an
10398 -- internal unit, since there is no reason to have checks on for
10399 -- any predefined run-time library code. All such code is designed
10400 -- to be compiled with checks off.
10401
10402 -- Note that we do NOT apply this criterion to children of GNAT
10403 -- (or on VMS, children of DEC). The latter units must suppress
10404 -- checks explicitly if this is needed.
10405
10406 if Is_Predefined_File_Name
10407 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10408 then
10409 Analyze (Act_Body, Suppress => All_Checks);
10410 else
10411 Analyze (Act_Body);
10412 end if;
10413 end if;
10414
10415 Inherit_Context (Gen_Body, Inst_Node);
10416
10417 -- Remove the parent instances if they have been placed on the scope
10418 -- stack to compile the body.
10419
10420 if Parent_Installed then
10421 Remove_Parent (In_Body => True);
10422
10423 -- Restore the previous visibility of the parent
10424
10425 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10426 end if;
10427
10428 Restore_Hidden_Primitives (Vis_Prims_List);
10429 Restore_Private_Views (Act_Decl_Id);
10430
10431 -- Remove the current unit from visibility if this is an instance
10432 -- that is not elaborated on the fly for inlining purposes.
10433
10434 if not Inlined_Body then
10435 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10436 end if;
10437
10438 Restore_Env;
10439 Style_Check := Save_Style_Check;
10440
10441 -- If we have no body, and the unit requires a body, then complain. This
10442 -- complaint is suppressed if we have detected other errors (since a
10443 -- common reason for missing the body is that it had errors).
10444 -- In CodePeer mode, a warning has been emitted already, no need for
10445 -- further messages.
10446
10447 elsif Unit_Requires_Body (Gen_Unit)
10448 and then not Body_Optional
10449 then
10450 if CodePeer_Mode then
10451 null;
10452
10453 elsif Serious_Errors_Detected = 0 then
10454 Error_Msg_NE
10455 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10456
10457 -- Don't attempt to perform any cleanup actions if some other error
10458 -- was already detected, since this can cause blowups.
10459
10460 else
10461 return;
10462 end if;
10463
10464 -- Case of package that does not need a body
10465
10466 else
10467 -- If the instantiation of the declaration is a library unit, rewrite
10468 -- the original package instantiation as a package declaration in the
10469 -- compilation unit node.
10470
10471 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10472 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10473 Rewrite (Inst_Node, Act_Decl);
10474
10475 -- Generate elaboration entity, in case spec has elaboration code.
10476 -- This cannot be done when the instance is analyzed, because it
10477 -- is not known yet whether the body exists.
10478
10479 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10480 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10481
10482 -- If the instantiation is not a library unit, then append the
10483 -- declaration to the list of implicitly generated entities, unless
10484 -- it is already a list member which means that it was already
10485 -- processed
10486
10487 elsif not Is_List_Member (Act_Decl) then
10488 Mark_Rewrite_Insertion (Act_Decl);
10489 Insert_Before (Inst_Node, Act_Decl);
10490 end if;
10491 end if;
10492
10493 Expander_Mode_Restore;
10494 end Instantiate_Package_Body;
10495
10496 ---------------------------------
10497 -- Instantiate_Subprogram_Body --
10498 ---------------------------------
10499
10500 procedure Instantiate_Subprogram_Body
10501 (Body_Info : Pending_Body_Info;
10502 Body_Optional : Boolean := False)
10503 is
10504 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10505 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10506 Loc : constant Source_Ptr := Sloc (Inst_Node);
10507 Gen_Id : constant Node_Id := Name (Inst_Node);
10508 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10509 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10510 Anon_Id : constant Entity_Id :=
10511 Defining_Unit_Name (Specification (Act_Decl));
10512 Pack_Id : constant Entity_Id :=
10513 Defining_Unit_Name (Parent (Act_Decl));
10514 Decls : List_Id;
10515 Gen_Body : Node_Id;
10516 Gen_Body_Id : Node_Id;
10517 Act_Body : Node_Id;
10518 Pack_Body : Node_Id;
10519 Prev_Formal : Entity_Id;
10520 Ret_Expr : Node_Id;
10521 Unit_Renaming : Node_Id;
10522
10523 Parent_Installed : Boolean := False;
10524
10525 Saved_Style_Check : constant Boolean := Style_Check;
10526 Saved_Warnings : constant Warning_Record := Save_Warnings;
10527
10528 Par_Ent : Entity_Id := Empty;
10529 Par_Vis : Boolean := False;
10530
10531 begin
10532 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10533
10534 -- Subprogram body may have been created already because of an inline
10535 -- pragma, or because of multiple elaborations of the enclosing package
10536 -- when several instances of the subprogram appear in the main unit.
10537
10538 if Present (Corresponding_Body (Act_Decl)) then
10539 return;
10540 end if;
10541
10542 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10543
10544 -- Re-establish the state of information on which checks are suppressed.
10545 -- This information was set in Body_Info at the point of instantiation,
10546 -- and now we restore it so that the instance is compiled using the
10547 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10548
10549 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10550 Scope_Suppress := Body_Info.Scope_Suppress;
10551 Opt.Ada_Version := Body_Info.Version;
10552 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10553 Restore_Warnings (Body_Info.Warnings);
10554 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10555 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10556
10557 if No (Gen_Body_Id) then
10558
10559 -- For imported generic subprogram, no body to compile, complete
10560 -- the spec entity appropriately.
10561
10562 if Is_Imported (Gen_Unit) then
10563 Set_Is_Imported (Anon_Id);
10564 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10565 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
10566 Set_Convention (Anon_Id, Convention (Gen_Unit));
10567 Set_Has_Completion (Anon_Id);
10568 return;
10569
10570 -- For other cases, compile the body
10571
10572 else
10573 Load_Parent_Of_Generic
10574 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10575 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10576 end if;
10577 end if;
10578
10579 Instantiation_Node := Inst_Node;
10580
10581 if Present (Gen_Body_Id) then
10582 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10583
10584 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
10585
10586 -- Either body is not present, or context is non-expanding, as
10587 -- when compiling a subunit. Mark the instance as completed, and
10588 -- diagnose a missing body when needed.
10589
10590 if Expander_Active
10591 and then Operating_Mode = Generate_Code
10592 then
10593 Error_Msg_N
10594 ("missing proper body for instantiation", Gen_Body);
10595 end if;
10596
10597 Set_Has_Completion (Anon_Id);
10598 return;
10599 end if;
10600
10601 Save_Env (Gen_Unit, Anon_Id);
10602 Style_Check := False;
10603 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10604 Create_Instantiation_Source
10605 (Inst_Node,
10606 Gen_Body_Id,
10607 False,
10608 S_Adjustment);
10609
10610 Act_Body :=
10611 Copy_Generic_Node
10612 (Original_Node (Gen_Body), Empty, Instantiating => True);
10613
10614 -- Create proper defining name for the body, to correspond to
10615 -- the one in the spec.
10616
10617 Set_Defining_Unit_Name (Specification (Act_Body),
10618 Make_Defining_Identifier
10619 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
10620 Set_Corresponding_Spec (Act_Body, Anon_Id);
10621 Set_Has_Completion (Anon_Id);
10622 Check_Generic_Actuals (Pack_Id, False);
10623
10624 -- Generate a reference to link the visible subprogram instance to
10625 -- the generic body, which for navigation purposes is the only
10626 -- available source for the instance.
10627
10628 Generate_Reference
10629 (Related_Instance (Pack_Id),
10630 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
10631
10632 -- If it is a child unit, make the parent instance (which is an
10633 -- instance of the parent of the generic) visible. The parent
10634 -- instance is the prefix of the name of the generic unit.
10635
10636 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10637 and then Nkind (Gen_Id) = N_Expanded_Name
10638 then
10639 Par_Ent := Entity (Prefix (Gen_Id));
10640 Par_Vis := Is_Immediately_Visible (Par_Ent);
10641 Install_Parent (Par_Ent, In_Body => True);
10642 Parent_Installed := True;
10643
10644 elsif Is_Child_Unit (Gen_Unit) then
10645 Par_Ent := Scope (Gen_Unit);
10646 Par_Vis := Is_Immediately_Visible (Par_Ent);
10647 Install_Parent (Par_Ent, In_Body => True);
10648 Parent_Installed := True;
10649 end if;
10650
10651 -- Inside its body, a reference to the generic unit is a reference
10652 -- to the instance. The corresponding renaming is the first
10653 -- declaration in the body.
10654
10655 Unit_Renaming :=
10656 Make_Subprogram_Renaming_Declaration (Loc,
10657 Specification =>
10658 Copy_Generic_Node (
10659 Specification (Original_Node (Gen_Body)),
10660 Empty,
10661 Instantiating => True),
10662 Name => New_Occurrence_Of (Anon_Id, Loc));
10663
10664 -- If there is a formal subprogram with the same name as the unit
10665 -- itself, do not add this renaming declaration. This is a temporary
10666 -- fix for one ACVC test. ???
10667
10668 Prev_Formal := First_Entity (Pack_Id);
10669 while Present (Prev_Formal) loop
10670 if Chars (Prev_Formal) = Chars (Gen_Unit)
10671 and then Is_Overloadable (Prev_Formal)
10672 then
10673 exit;
10674 end if;
10675
10676 Next_Entity (Prev_Formal);
10677 end loop;
10678
10679 if Present (Prev_Formal) then
10680 Decls := New_List (Act_Body);
10681 else
10682 Decls := New_List (Unit_Renaming, Act_Body);
10683 end if;
10684
10685 -- The subprogram body is placed in the body of a dummy package body,
10686 -- whose spec contains the subprogram declaration as well as the
10687 -- renaming declarations for the generic parameters.
10688
10689 Pack_Body := Make_Package_Body (Loc,
10690 Defining_Unit_Name => New_Copy (Pack_Id),
10691 Declarations => Decls);
10692
10693 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10694
10695 -- If the instantiation is a library unit, then build resulting
10696 -- compilation unit nodes for the instance. The declaration of
10697 -- the enclosing package is the grandparent of the subprogram
10698 -- declaration. First replace the instantiation node as the unit
10699 -- of the corresponding compilation.
10700
10701 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10702 if Parent (Inst_Node) = Cunit (Main_Unit) then
10703 Set_Unit (Parent (Inst_Node), Inst_Node);
10704 Build_Instance_Compilation_Unit_Nodes
10705 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
10706 Analyze (Inst_Node);
10707 else
10708 Set_Parent (Pack_Body, Parent (Inst_Node));
10709 Analyze (Pack_Body);
10710 end if;
10711
10712 else
10713 Insert_Before (Inst_Node, Pack_Body);
10714 Mark_Rewrite_Insertion (Pack_Body);
10715 Analyze (Pack_Body);
10716
10717 if Expander_Active then
10718 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
10719 end if;
10720 end if;
10721
10722 Inherit_Context (Gen_Body, Inst_Node);
10723
10724 Restore_Private_Views (Pack_Id, False);
10725
10726 if Parent_Installed then
10727 Remove_Parent (In_Body => True);
10728
10729 -- Restore the previous visibility of the parent
10730
10731 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10732 end if;
10733
10734 Restore_Env;
10735 Style_Check := Saved_Style_Check;
10736 Restore_Warnings (Saved_Warnings);
10737
10738 -- Body not found. Error was emitted already. If there were no previous
10739 -- errors, this may be an instance whose scope is a premature instance.
10740 -- In that case we must insure that the (legal) program does raise
10741 -- program error if executed. We generate a subprogram body for this
10742 -- purpose. See DEC ac30vso.
10743
10744 -- Should not reference proprietary DEC tests in comments ???
10745
10746 elsif Serious_Errors_Detected = 0
10747 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
10748 then
10749 if Body_Optional then
10750 return;
10751
10752 elsif Ekind (Anon_Id) = E_Procedure then
10753 Act_Body :=
10754 Make_Subprogram_Body (Loc,
10755 Specification =>
10756 Make_Procedure_Specification (Loc,
10757 Defining_Unit_Name =>
10758 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10759 Parameter_Specifications =>
10760 New_Copy_List
10761 (Parameter_Specifications (Parent (Anon_Id)))),
10762
10763 Declarations => Empty_List,
10764 Handled_Statement_Sequence =>
10765 Make_Handled_Sequence_Of_Statements (Loc,
10766 Statements =>
10767 New_List (
10768 Make_Raise_Program_Error (Loc,
10769 Reason =>
10770 PE_Access_Before_Elaboration))));
10771
10772 else
10773 Ret_Expr :=
10774 Make_Raise_Program_Error (Loc,
10775 Reason => PE_Access_Before_Elaboration);
10776
10777 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
10778 Set_Analyzed (Ret_Expr);
10779
10780 Act_Body :=
10781 Make_Subprogram_Body (Loc,
10782 Specification =>
10783 Make_Function_Specification (Loc,
10784 Defining_Unit_Name =>
10785 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10786 Parameter_Specifications =>
10787 New_Copy_List
10788 (Parameter_Specifications (Parent (Anon_Id))),
10789 Result_Definition =>
10790 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10791
10792 Declarations => Empty_List,
10793 Handled_Statement_Sequence =>
10794 Make_Handled_Sequence_Of_Statements (Loc,
10795 Statements =>
10796 New_List
10797 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10798 end if;
10799
10800 Pack_Body := Make_Package_Body (Loc,
10801 Defining_Unit_Name => New_Copy (Pack_Id),
10802 Declarations => New_List (Act_Body));
10803
10804 Insert_After (Inst_Node, Pack_Body);
10805 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10806 Analyze (Pack_Body);
10807 end if;
10808
10809 Expander_Mode_Restore;
10810 end Instantiate_Subprogram_Body;
10811
10812 ----------------------
10813 -- Instantiate_Type --
10814 ----------------------
10815
10816 function Instantiate_Type
10817 (Formal : Node_Id;
10818 Actual : Node_Id;
10819 Analyzed_Formal : Node_Id;
10820 Actual_Decls : List_Id) return List_Id
10821 is
10822 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10823 A_Gen_T : constant Entity_Id :=
10824 Defining_Identifier (Analyzed_Formal);
10825 Ancestor : Entity_Id := Empty;
10826 Def : constant Node_Id := Formal_Type_Definition (Formal);
10827 Act_T : Entity_Id;
10828 Decl_Node : Node_Id;
10829 Decl_Nodes : List_Id;
10830 Loc : Source_Ptr;
10831 Subt : Entity_Id;
10832
10833 procedure Diagnose_Predicated_Actual;
10834 -- There are a number of constructs in which a discrete type with
10835 -- predicates is illegal, e.g. as an index in an array type declaration.
10836 -- If a generic type is used is such a construct in a generic package
10837 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
10838 -- of the generic contract that the actual cannot have predicates.
10839
10840 procedure Validate_Array_Type_Instance;
10841 procedure Validate_Access_Subprogram_Instance;
10842 procedure Validate_Access_Type_Instance;
10843 procedure Validate_Derived_Type_Instance;
10844 procedure Validate_Derived_Interface_Type_Instance;
10845 procedure Validate_Discriminated_Formal_Type;
10846 procedure Validate_Interface_Type_Instance;
10847 procedure Validate_Private_Type_Instance;
10848 procedure Validate_Incomplete_Type_Instance;
10849 -- These procedures perform validation tests for the named case.
10850 -- Validate_Discriminated_Formal_Type is shared by formal private
10851 -- types and Ada 2012 formal incomplete types.
10852
10853 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
10854 -- Check that base types are the same and that the subtypes match
10855 -- statically. Used in several of the above.
10856
10857 ---------------------------------
10858 -- Diagnose_Predicated_Actual --
10859 ---------------------------------
10860
10861 procedure Diagnose_Predicated_Actual is
10862 begin
10863 if No_Predicate_On_Actual (A_Gen_T)
10864 and then Has_Predicates (Act_T)
10865 then
10866 Error_Msg_NE
10867 ("actual for& cannot be a type with predicate",
10868 Instantiation_Node, A_Gen_T);
10869
10870 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
10871 and then Has_Predicates (Act_T)
10872 and then not Has_Static_Predicate_Aspect (Act_T)
10873 then
10874 Error_Msg_NE
10875 ("actual for& cannot be a type with a dynamic predicate",
10876 Instantiation_Node, A_Gen_T);
10877 end if;
10878 end Diagnose_Predicated_Actual;
10879
10880 --------------------
10881 -- Subtypes_Match --
10882 --------------------
10883
10884 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
10885 T : constant Entity_Id := Get_Instance_Of (Gen_T);
10886
10887 begin
10888 -- Some detailed comments would be useful here ???
10889
10890 return ((Base_Type (T) = Act_T
10891 or else Base_Type (T) = Base_Type (Act_T))
10892 and then Subtypes_Statically_Match (T, Act_T))
10893
10894 or else (Is_Class_Wide_Type (Gen_T)
10895 and then Is_Class_Wide_Type (Act_T)
10896 and then Subtypes_Match
10897 (Get_Instance_Of (Root_Type (Gen_T)),
10898 Root_Type (Act_T)))
10899
10900 or else
10901 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
10902 E_Anonymous_Access_Type)
10903 and then Ekind (Act_T) = Ekind (Gen_T)
10904 and then Subtypes_Statically_Match
10905 (Designated_Type (Gen_T), Designated_Type (Act_T)));
10906 end Subtypes_Match;
10907
10908 -----------------------------------------
10909 -- Validate_Access_Subprogram_Instance --
10910 -----------------------------------------
10911
10912 procedure Validate_Access_Subprogram_Instance is
10913 begin
10914 if not Is_Access_Type (Act_T)
10915 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
10916 then
10917 Error_Msg_NE
10918 ("expect access type in instantiation of &", Actual, Gen_T);
10919 Abandon_Instantiation (Actual);
10920 end if;
10921
10922 -- According to AI05-288, actuals for access_to_subprograms must be
10923 -- subtype conformant with the generic formal. Previous to AI05-288
10924 -- only mode conformance was required.
10925
10926 -- This is a binding interpretation that applies to previous versions
10927 -- of the language, no need to maintain previous weaker checks.
10928
10929 Check_Subtype_Conformant
10930 (Designated_Type (Act_T),
10931 Designated_Type (A_Gen_T),
10932 Actual,
10933 Get_Inst => True);
10934
10935 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
10936 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
10937 Error_Msg_NE
10938 ("protected access type not allowed for formal &",
10939 Actual, Gen_T);
10940 end if;
10941
10942 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
10943 Error_Msg_NE
10944 ("expect protected access type for formal &",
10945 Actual, Gen_T);
10946 end if;
10947 end Validate_Access_Subprogram_Instance;
10948
10949 -----------------------------------
10950 -- Validate_Access_Type_Instance --
10951 -----------------------------------
10952
10953 procedure Validate_Access_Type_Instance is
10954 Desig_Type : constant Entity_Id :=
10955 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
10956 Desig_Act : Entity_Id;
10957
10958 begin
10959 if not Is_Access_Type (Act_T) then
10960 Error_Msg_NE
10961 ("expect access type in instantiation of &", Actual, Gen_T);
10962 Abandon_Instantiation (Actual);
10963 end if;
10964
10965 if Is_Access_Constant (A_Gen_T) then
10966 if not Is_Access_Constant (Act_T) then
10967 Error_Msg_N
10968 ("actual type must be access-to-constant type", Actual);
10969 Abandon_Instantiation (Actual);
10970 end if;
10971 else
10972 if Is_Access_Constant (Act_T) then
10973 Error_Msg_N
10974 ("actual type must be access-to-variable type", Actual);
10975 Abandon_Instantiation (Actual);
10976
10977 elsif Ekind (A_Gen_T) = E_General_Access_Type
10978 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
10979 then
10980 Error_Msg_N -- CODEFIX
10981 ("actual must be general access type!", Actual);
10982 Error_Msg_NE -- CODEFIX
10983 ("add ALL to }!", Actual, Act_T);
10984 Abandon_Instantiation (Actual);
10985 end if;
10986 end if;
10987
10988 -- The designated subtypes, that is to say the subtypes introduced
10989 -- by an access type declaration (and not by a subtype declaration)
10990 -- must match.
10991
10992 Desig_Act := Designated_Type (Base_Type (Act_T));
10993
10994 -- The designated type may have been introduced through a limited_
10995 -- with clause, in which case retrieve the non-limited view. This
10996 -- applies to incomplete types as well as to class-wide types.
10997
10998 if From_Limited_With (Desig_Act) then
10999 Desig_Act := Available_View (Desig_Act);
11000 end if;
11001
11002 if not Subtypes_Match (Desig_Type, Desig_Act) then
11003 Error_Msg_NE
11004 ("designated type of actual does not match that of formal &",
11005 Actual, Gen_T);
11006
11007 if not Predicates_Match (Desig_Type, Desig_Act) then
11008 Error_Msg_N ("\predicates do not match", Actual);
11009 end if;
11010
11011 Abandon_Instantiation (Actual);
11012
11013 elsif Is_Access_Type (Designated_Type (Act_T))
11014 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11015 /=
11016 Is_Constrained (Designated_Type (Desig_Type))
11017 then
11018 Error_Msg_NE
11019 ("designated type of actual does not match that of formal &",
11020 Actual, Gen_T);
11021
11022 if not Predicates_Match (Desig_Type, Desig_Act) then
11023 Error_Msg_N ("\predicates do not match", Actual);
11024 end if;
11025
11026 Abandon_Instantiation (Actual);
11027 end if;
11028
11029 -- Ada 2005: null-exclusion indicators of the two types must agree
11030
11031 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11032 Error_Msg_NE
11033 ("non null exclusion of actual and formal & do not match",
11034 Actual, Gen_T);
11035 end if;
11036 end Validate_Access_Type_Instance;
11037
11038 ----------------------------------
11039 -- Validate_Array_Type_Instance --
11040 ----------------------------------
11041
11042 procedure Validate_Array_Type_Instance is
11043 I1 : Node_Id;
11044 I2 : Node_Id;
11045 T2 : Entity_Id;
11046
11047 function Formal_Dimensions return Int;
11048 -- Count number of dimensions in array type formal
11049
11050 -----------------------
11051 -- Formal_Dimensions --
11052 -----------------------
11053
11054 function Formal_Dimensions return Int is
11055 Num : Int := 0;
11056 Index : Node_Id;
11057
11058 begin
11059 if Nkind (Def) = N_Constrained_Array_Definition then
11060 Index := First (Discrete_Subtype_Definitions (Def));
11061 else
11062 Index := First (Subtype_Marks (Def));
11063 end if;
11064
11065 while Present (Index) loop
11066 Num := Num + 1;
11067 Next_Index (Index);
11068 end loop;
11069
11070 return Num;
11071 end Formal_Dimensions;
11072
11073 -- Start of processing for Validate_Array_Type_Instance
11074
11075 begin
11076 if not Is_Array_Type (Act_T) then
11077 Error_Msg_NE
11078 ("expect array type in instantiation of &", Actual, Gen_T);
11079 Abandon_Instantiation (Actual);
11080
11081 elsif Nkind (Def) = N_Constrained_Array_Definition then
11082 if not (Is_Constrained (Act_T)) then
11083 Error_Msg_NE
11084 ("expect constrained array in instantiation of &",
11085 Actual, Gen_T);
11086 Abandon_Instantiation (Actual);
11087 end if;
11088
11089 else
11090 if Is_Constrained (Act_T) then
11091 Error_Msg_NE
11092 ("expect unconstrained array in instantiation of &",
11093 Actual, Gen_T);
11094 Abandon_Instantiation (Actual);
11095 end if;
11096 end if;
11097
11098 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11099 Error_Msg_NE
11100 ("dimensions of actual do not match formal &", Actual, Gen_T);
11101 Abandon_Instantiation (Actual);
11102 end if;
11103
11104 I1 := First_Index (A_Gen_T);
11105 I2 := First_Index (Act_T);
11106 for J in 1 .. Formal_Dimensions loop
11107
11108 -- If the indexes of the actual were given by a subtype_mark,
11109 -- the index was transformed into a range attribute. Retrieve
11110 -- the original type mark for checking.
11111
11112 if Is_Entity_Name (Original_Node (I2)) then
11113 T2 := Entity (Original_Node (I2));
11114 else
11115 T2 := Etype (I2);
11116 end if;
11117
11118 if not Subtypes_Match
11119 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11120 then
11121 Error_Msg_NE
11122 ("index types of actual do not match those of formal &",
11123 Actual, Gen_T);
11124 Abandon_Instantiation (Actual);
11125 end if;
11126
11127 Next_Index (I1);
11128 Next_Index (I2);
11129 end loop;
11130
11131 -- Check matching subtypes. Note that there are complex visibility
11132 -- issues when the generic is a child unit and some aspect of the
11133 -- generic type is declared in a parent unit of the generic. We do
11134 -- the test to handle this special case only after a direct check
11135 -- for static matching has failed. The case where both the component
11136 -- type and the array type are separate formals, and the component
11137 -- type is a private view may also require special checking in
11138 -- Subtypes_Match.
11139
11140 if Subtypes_Match
11141 (Component_Type (A_Gen_T), Component_Type (Act_T))
11142 or else Subtypes_Match
11143 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11144 Component_Type (Act_T))
11145 then
11146 null;
11147 else
11148 Error_Msg_NE
11149 ("component subtype of actual does not match that of formal &",
11150 Actual, Gen_T);
11151 Abandon_Instantiation (Actual);
11152 end if;
11153
11154 if Has_Aliased_Components (A_Gen_T)
11155 and then not Has_Aliased_Components (Act_T)
11156 then
11157 Error_Msg_NE
11158 ("actual must have aliased components to match formal type &",
11159 Actual, Gen_T);
11160 end if;
11161 end Validate_Array_Type_Instance;
11162
11163 -----------------------------------------------
11164 -- Validate_Derived_Interface_Type_Instance --
11165 -----------------------------------------------
11166
11167 procedure Validate_Derived_Interface_Type_Instance is
11168 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11169 Elmt : Elmt_Id;
11170
11171 begin
11172 -- First apply interface instance checks
11173
11174 Validate_Interface_Type_Instance;
11175
11176 -- Verify that immediate parent interface is an ancestor of
11177 -- the actual.
11178
11179 if Present (Par)
11180 and then not Interface_Present_In_Ancestor (Act_T, Par)
11181 then
11182 Error_Msg_NE
11183 ("interface actual must include progenitor&", Actual, Par);
11184 end if;
11185
11186 -- Now verify that the actual includes all other ancestors of
11187 -- the formal.
11188
11189 Elmt := First_Elmt (Interfaces (A_Gen_T));
11190 while Present (Elmt) loop
11191 if not Interface_Present_In_Ancestor
11192 (Act_T, Get_Instance_Of (Node (Elmt)))
11193 then
11194 Error_Msg_NE
11195 ("interface actual must include progenitor&",
11196 Actual, Node (Elmt));
11197 end if;
11198
11199 Next_Elmt (Elmt);
11200 end loop;
11201 end Validate_Derived_Interface_Type_Instance;
11202
11203 ------------------------------------
11204 -- Validate_Derived_Type_Instance --
11205 ------------------------------------
11206
11207 procedure Validate_Derived_Type_Instance is
11208 Actual_Discr : Entity_Id;
11209 Ancestor_Discr : Entity_Id;
11210
11211 begin
11212 -- If the parent type in the generic declaration is itself a previous
11213 -- formal type, then it is local to the generic and absent from the
11214 -- analyzed generic definition. In that case the ancestor is the
11215 -- instance of the formal (which must have been instantiated
11216 -- previously), unless the ancestor is itself a formal derived type.
11217 -- In this latter case (which is the subject of Corrigendum 8652/0038
11218 -- (AI-202) the ancestor of the formals is the ancestor of its
11219 -- parent. Otherwise, the analyzed generic carries the parent type.
11220 -- If the parent type is defined in a previous formal package, then
11221 -- the scope of that formal package is that of the generic type
11222 -- itself, and it has already been mapped into the corresponding type
11223 -- in the actual package.
11224
11225 -- Common case: parent type defined outside of the generic
11226
11227 if Is_Entity_Name (Subtype_Mark (Def))
11228 and then Present (Entity (Subtype_Mark (Def)))
11229 then
11230 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11231
11232 -- Check whether parent is defined in a previous formal package
11233
11234 elsif
11235 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11236 then
11237 Ancestor :=
11238 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11239
11240 -- The type may be a local derivation, or a type extension of a
11241 -- previous formal, or of a formal of a parent package.
11242
11243 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11244 or else
11245 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11246 then
11247 -- Check whether the parent is another derived formal type in the
11248 -- same generic unit.
11249
11250 if Etype (A_Gen_T) /= A_Gen_T
11251 and then Is_Generic_Type (Etype (A_Gen_T))
11252 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11253 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11254 then
11255 -- Locate ancestor of parent from the subtype declaration
11256 -- created for the actual.
11257
11258 declare
11259 Decl : Node_Id;
11260
11261 begin
11262 Decl := First (Actual_Decls);
11263 while Present (Decl) loop
11264 if Nkind (Decl) = N_Subtype_Declaration
11265 and then Chars (Defining_Identifier (Decl)) =
11266 Chars (Etype (A_Gen_T))
11267 then
11268 Ancestor := Generic_Parent_Type (Decl);
11269 exit;
11270 else
11271 Next (Decl);
11272 end if;
11273 end loop;
11274 end;
11275
11276 pragma Assert (Present (Ancestor));
11277
11278 -- The ancestor itself may be a previous formal that has been
11279 -- instantiated.
11280
11281 Ancestor := Get_Instance_Of (Ancestor);
11282
11283 else
11284 Ancestor :=
11285 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11286 end if;
11287
11288 -- An unusual case: the actual is a type declared in a parent unit,
11289 -- but is not a formal type so there is no instance_of for it.
11290 -- Retrieve it by analyzing the record extension.
11291
11292 elsif Is_Child_Unit (Scope (A_Gen_T))
11293 and then In_Open_Scopes (Scope (Act_T))
11294 and then Is_Generic_Instance (Scope (Act_T))
11295 then
11296 Analyze (Subtype_Mark (Def));
11297 Ancestor := Entity (Subtype_Mark (Def));
11298
11299 else
11300 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11301 end if;
11302
11303 -- If the formal derived type has pragma Preelaborable_Initialization
11304 -- then the actual type must have preelaborable initialization.
11305
11306 if Known_To_Have_Preelab_Init (A_Gen_T)
11307 and then not Has_Preelaborable_Initialization (Act_T)
11308 then
11309 Error_Msg_NE
11310 ("actual for & must have preelaborable initialization",
11311 Actual, Gen_T);
11312 end if;
11313
11314 -- Ada 2005 (AI-251)
11315
11316 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11317 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11318 Error_Msg_NE
11319 ("(Ada 2005) expected type implementing & in instantiation",
11320 Actual, Ancestor);
11321 end if;
11322
11323 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11324 Error_Msg_NE
11325 ("expect type derived from & in instantiation",
11326 Actual, First_Subtype (Ancestor));
11327 Abandon_Instantiation (Actual);
11328 end if;
11329
11330 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11331 -- that the formal type declaration has been rewritten as a private
11332 -- extension.
11333
11334 if Ada_Version >= Ada_2005
11335 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11336 and then Synchronized_Present (Parent (A_Gen_T))
11337 then
11338 -- The actual must be a synchronized tagged type
11339
11340 if not Is_Tagged_Type (Act_T) then
11341 Error_Msg_N
11342 ("actual of synchronized type must be tagged", Actual);
11343 Abandon_Instantiation (Actual);
11344
11345 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11346 and then Nkind (Type_Definition (Parent (Act_T))) =
11347 N_Derived_Type_Definition
11348 and then not Synchronized_Present (Type_Definition
11349 (Parent (Act_T)))
11350 then
11351 Error_Msg_N
11352 ("actual of synchronized type must be synchronized", Actual);
11353 Abandon_Instantiation (Actual);
11354 end if;
11355 end if;
11356
11357 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11358 -- removes the second instance of the phrase "or allow pass by copy".
11359
11360 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11361 Error_Msg_N
11362 ("cannot have atomic actual type for non-atomic formal type",
11363 Actual);
11364
11365 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11366 Error_Msg_N
11367 ("cannot have volatile actual type for non-volatile formal type",
11368 Actual);
11369 end if;
11370
11371 -- It should not be necessary to check for unknown discriminants on
11372 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11373 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11374 -- needs fixing. ???
11375
11376 if not Is_Indefinite_Subtype (A_Gen_T)
11377 and then not Unknown_Discriminants_Present (Formal)
11378 and then Is_Indefinite_Subtype (Act_T)
11379 then
11380 Error_Msg_N
11381 ("actual subtype must be constrained", Actual);
11382 Abandon_Instantiation (Actual);
11383 end if;
11384
11385 if not Unknown_Discriminants_Present (Formal) then
11386 if Is_Constrained (Ancestor) then
11387 if not Is_Constrained (Act_T) then
11388 Error_Msg_N
11389 ("actual subtype must be constrained", Actual);
11390 Abandon_Instantiation (Actual);
11391 end if;
11392
11393 -- Ancestor is unconstrained, Check if generic formal and actual
11394 -- agree on constrainedness. The check only applies to array types
11395 -- and discriminated types.
11396
11397 elsif Is_Constrained (Act_T) then
11398 if Ekind (Ancestor) = E_Access_Type
11399 or else
11400 (not Is_Constrained (A_Gen_T)
11401 and then Is_Composite_Type (A_Gen_T))
11402 then
11403 Error_Msg_N
11404 ("actual subtype must be unconstrained", Actual);
11405 Abandon_Instantiation (Actual);
11406 end if;
11407
11408 -- A class-wide type is only allowed if the formal has unknown
11409 -- discriminants.
11410
11411 elsif Is_Class_Wide_Type (Act_T)
11412 and then not Has_Unknown_Discriminants (Ancestor)
11413 then
11414 Error_Msg_NE
11415 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11416 Abandon_Instantiation (Actual);
11417
11418 -- Otherwise, the formal and actual shall have the same number
11419 -- of discriminants and each discriminant of the actual must
11420 -- correspond to a discriminant of the formal.
11421
11422 elsif Has_Discriminants (Act_T)
11423 and then not Has_Unknown_Discriminants (Act_T)
11424 and then Has_Discriminants (Ancestor)
11425 then
11426 Actual_Discr := First_Discriminant (Act_T);
11427 Ancestor_Discr := First_Discriminant (Ancestor);
11428 while Present (Actual_Discr)
11429 and then Present (Ancestor_Discr)
11430 loop
11431 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11432 No (Corresponding_Discriminant (Actual_Discr))
11433 then
11434 Error_Msg_NE
11435 ("discriminant & does not correspond " &
11436 "to ancestor discriminant", Actual, Actual_Discr);
11437 Abandon_Instantiation (Actual);
11438 end if;
11439
11440 Next_Discriminant (Actual_Discr);
11441 Next_Discriminant (Ancestor_Discr);
11442 end loop;
11443
11444 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11445 Error_Msg_NE
11446 ("actual for & must have same number of discriminants",
11447 Actual, Gen_T);
11448 Abandon_Instantiation (Actual);
11449 end if;
11450
11451 -- This case should be caught by the earlier check for
11452 -- constrainedness, but the check here is added for completeness.
11453
11454 elsif Has_Discriminants (Act_T)
11455 and then not Has_Unknown_Discriminants (Act_T)
11456 then
11457 Error_Msg_NE
11458 ("actual for & must not have discriminants", Actual, Gen_T);
11459 Abandon_Instantiation (Actual);
11460
11461 elsif Has_Discriminants (Ancestor) then
11462 Error_Msg_NE
11463 ("actual for & must have known discriminants", Actual, Gen_T);
11464 Abandon_Instantiation (Actual);
11465 end if;
11466
11467 if not Subtypes_Statically_Compatible
11468 (Act_T, Ancestor, Formal_Derived_Matching => True)
11469 then
11470 Error_Msg_N
11471 ("constraint on actual is incompatible with formal", Actual);
11472 Abandon_Instantiation (Actual);
11473 end if;
11474 end if;
11475
11476 -- If the formal and actual types are abstract, check that there
11477 -- are no abstract primitives of the actual type that correspond to
11478 -- nonabstract primitives of the formal type (second sentence of
11479 -- RM95-3.9.3(9)).
11480
11481 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11482 Check_Abstract_Primitives : declare
11483 Gen_Prims : constant Elist_Id :=
11484 Primitive_Operations (A_Gen_T);
11485 Gen_Elmt : Elmt_Id;
11486 Gen_Subp : Entity_Id;
11487 Anc_Subp : Entity_Id;
11488 Anc_Formal : Entity_Id;
11489 Anc_F_Type : Entity_Id;
11490
11491 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11492 Act_Elmt : Elmt_Id;
11493 Act_Subp : Entity_Id;
11494 Act_Formal : Entity_Id;
11495 Act_F_Type : Entity_Id;
11496
11497 Subprograms_Correspond : Boolean;
11498
11499 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11500 -- Returns true if T2 is derived directly or indirectly from
11501 -- T1, including derivations from interfaces. T1 and T2 are
11502 -- required to be specific tagged base types.
11503
11504 ------------------------
11505 -- Is_Tagged_Ancestor --
11506 ------------------------
11507
11508 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11509 is
11510 Intfc_Elmt : Elmt_Id;
11511
11512 begin
11513 -- The predicate is satisfied if the types are the same
11514
11515 if T1 = T2 then
11516 return True;
11517
11518 -- If we've reached the top of the derivation chain then
11519 -- we know that T1 is not an ancestor of T2.
11520
11521 elsif Etype (T2) = T2 then
11522 return False;
11523
11524 -- Proceed to check T2's immediate parent
11525
11526 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11527 return True;
11528
11529 -- Finally, check to see if T1 is an ancestor of any of T2's
11530 -- progenitors.
11531
11532 else
11533 Intfc_Elmt := First_Elmt (Interfaces (T2));
11534 while Present (Intfc_Elmt) loop
11535 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11536 return True;
11537 end if;
11538
11539 Next_Elmt (Intfc_Elmt);
11540 end loop;
11541 end if;
11542
11543 return False;
11544 end Is_Tagged_Ancestor;
11545
11546 -- Start of processing for Check_Abstract_Primitives
11547
11548 begin
11549 -- Loop over all of the formal derived type's primitives
11550
11551 Gen_Elmt := First_Elmt (Gen_Prims);
11552 while Present (Gen_Elmt) loop
11553 Gen_Subp := Node (Gen_Elmt);
11554
11555 -- If the primitive of the formal is not abstract, then
11556 -- determine whether there is a corresponding primitive of
11557 -- the actual type that's abstract.
11558
11559 if not Is_Abstract_Subprogram (Gen_Subp) then
11560 Act_Elmt := First_Elmt (Act_Prims);
11561 while Present (Act_Elmt) loop
11562 Act_Subp := Node (Act_Elmt);
11563
11564 -- If we find an abstract primitive of the actual,
11565 -- then we need to test whether it corresponds to the
11566 -- subprogram from which the generic formal primitive
11567 -- is inherited.
11568
11569 if Is_Abstract_Subprogram (Act_Subp) then
11570 Anc_Subp := Alias (Gen_Subp);
11571
11572 -- Test whether we have a corresponding primitive
11573 -- by comparing names, kinds, formal types, and
11574 -- result types.
11575
11576 if Chars (Anc_Subp) = Chars (Act_Subp)
11577 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11578 then
11579 Anc_Formal := First_Formal (Anc_Subp);
11580 Act_Formal := First_Formal (Act_Subp);
11581 while Present (Anc_Formal)
11582 and then Present (Act_Formal)
11583 loop
11584 Anc_F_Type := Etype (Anc_Formal);
11585 Act_F_Type := Etype (Act_Formal);
11586
11587 if Ekind (Anc_F_Type)
11588 = E_Anonymous_Access_Type
11589 then
11590 Anc_F_Type := Designated_Type (Anc_F_Type);
11591
11592 if Ekind (Act_F_Type)
11593 = E_Anonymous_Access_Type
11594 then
11595 Act_F_Type :=
11596 Designated_Type (Act_F_Type);
11597 else
11598 exit;
11599 end if;
11600
11601 elsif
11602 Ekind (Act_F_Type) = E_Anonymous_Access_Type
11603 then
11604 exit;
11605 end if;
11606
11607 Anc_F_Type := Base_Type (Anc_F_Type);
11608 Act_F_Type := Base_Type (Act_F_Type);
11609
11610 -- If the formal is controlling, then the
11611 -- the type of the actual primitive's formal
11612 -- must be derived directly or indirectly
11613 -- from the type of the ancestor primitive's
11614 -- formal.
11615
11616 if Is_Controlling_Formal (Anc_Formal) then
11617 if not Is_Tagged_Ancestor
11618 (Anc_F_Type, Act_F_Type)
11619 then
11620 exit;
11621 end if;
11622
11623 -- Otherwise the types of the formals must
11624 -- be the same.
11625
11626 elsif Anc_F_Type /= Act_F_Type then
11627 exit;
11628 end if;
11629
11630 Next_Entity (Anc_Formal);
11631 Next_Entity (Act_Formal);
11632 end loop;
11633
11634 -- If we traversed through all of the formals
11635 -- then so far the subprograms correspond, so
11636 -- now check that any result types correspond.
11637
11638 if No (Anc_Formal) and then No (Act_Formal) then
11639 Subprograms_Correspond := True;
11640
11641 if Ekind (Act_Subp) = E_Function then
11642 Anc_F_Type := Etype (Anc_Subp);
11643 Act_F_Type := Etype (Act_Subp);
11644
11645 if Ekind (Anc_F_Type)
11646 = E_Anonymous_Access_Type
11647 then
11648 Anc_F_Type :=
11649 Designated_Type (Anc_F_Type);
11650
11651 if Ekind (Act_F_Type)
11652 = E_Anonymous_Access_Type
11653 then
11654 Act_F_Type :=
11655 Designated_Type (Act_F_Type);
11656 else
11657 Subprograms_Correspond := False;
11658 end if;
11659
11660 elsif
11661 Ekind (Act_F_Type)
11662 = E_Anonymous_Access_Type
11663 then
11664 Subprograms_Correspond := False;
11665 end if;
11666
11667 Anc_F_Type := Base_Type (Anc_F_Type);
11668 Act_F_Type := Base_Type (Act_F_Type);
11669
11670 -- Now either the result types must be
11671 -- the same or, if the result type is
11672 -- controlling, the result type of the
11673 -- actual primitive must descend from the
11674 -- result type of the ancestor primitive.
11675
11676 if Subprograms_Correspond
11677 and then Anc_F_Type /= Act_F_Type
11678 and then
11679 Has_Controlling_Result (Anc_Subp)
11680 and then
11681 not Is_Tagged_Ancestor
11682 (Anc_F_Type, Act_F_Type)
11683 then
11684 Subprograms_Correspond := False;
11685 end if;
11686 end if;
11687
11688 -- Found a matching subprogram belonging to
11689 -- formal ancestor type, so actual subprogram
11690 -- corresponds and this violates 3.9.3(9).
11691
11692 if Subprograms_Correspond then
11693 Error_Msg_NE
11694 ("abstract subprogram & overrides " &
11695 "nonabstract subprogram of ancestor",
11696 Actual,
11697 Act_Subp);
11698 end if;
11699 end if;
11700 end if;
11701 end if;
11702
11703 Next_Elmt (Act_Elmt);
11704 end loop;
11705 end if;
11706
11707 Next_Elmt (Gen_Elmt);
11708 end loop;
11709 end Check_Abstract_Primitives;
11710 end if;
11711
11712 -- Verify that limitedness matches. If parent is a limited
11713 -- interface then the generic formal is not unless declared
11714 -- explicitly so. If not declared limited, the actual cannot be
11715 -- limited (see AI05-0087).
11716
11717 -- Even though this AI is a binding interpretation, we enable the
11718 -- check only in Ada 2012 mode, because this improper construct
11719 -- shows up in user code and in existing B-tests.
11720
11721 if Is_Limited_Type (Act_T)
11722 and then not Is_Limited_Type (A_Gen_T)
11723 and then Ada_Version >= Ada_2012
11724 then
11725 if In_Instance then
11726 null;
11727 else
11728 Error_Msg_NE
11729 ("actual for non-limited & cannot be a limited type", Actual,
11730 Gen_T);
11731 Explain_Limited_Type (Act_T, Actual);
11732 Abandon_Instantiation (Actual);
11733 end if;
11734 end if;
11735 end Validate_Derived_Type_Instance;
11736
11737 ----------------------------------------
11738 -- Validate_Discriminated_Formal_Type --
11739 ----------------------------------------
11740
11741 procedure Validate_Discriminated_Formal_Type is
11742 Formal_Discr : Entity_Id;
11743 Actual_Discr : Entity_Id;
11744 Formal_Subt : Entity_Id;
11745
11746 begin
11747 if Has_Discriminants (A_Gen_T) then
11748 if not Has_Discriminants (Act_T) then
11749 Error_Msg_NE
11750 ("actual for & must have discriminants", Actual, Gen_T);
11751 Abandon_Instantiation (Actual);
11752
11753 elsif Is_Constrained (Act_T) then
11754 Error_Msg_NE
11755 ("actual for & must be unconstrained", Actual, Gen_T);
11756 Abandon_Instantiation (Actual);
11757
11758 else
11759 Formal_Discr := First_Discriminant (A_Gen_T);
11760 Actual_Discr := First_Discriminant (Act_T);
11761 while Formal_Discr /= Empty loop
11762 if Actual_Discr = Empty then
11763 Error_Msg_NE
11764 ("discriminants on actual do not match formal",
11765 Actual, Gen_T);
11766 Abandon_Instantiation (Actual);
11767 end if;
11768
11769 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
11770
11771 -- Access discriminants match if designated types do
11772
11773 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
11774 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
11775 E_Anonymous_Access_Type
11776 and then
11777 Get_Instance_Of
11778 (Designated_Type (Base_Type (Formal_Subt))) =
11779 Designated_Type (Base_Type (Etype (Actual_Discr)))
11780 then
11781 null;
11782
11783 elsif Base_Type (Formal_Subt) /=
11784 Base_Type (Etype (Actual_Discr))
11785 then
11786 Error_Msg_NE
11787 ("types of actual discriminants must match formal",
11788 Actual, Gen_T);
11789 Abandon_Instantiation (Actual);
11790
11791 elsif not Subtypes_Statically_Match
11792 (Formal_Subt, Etype (Actual_Discr))
11793 and then Ada_Version >= Ada_95
11794 then
11795 Error_Msg_NE
11796 ("subtypes of actual discriminants must match formal",
11797 Actual, Gen_T);
11798 Abandon_Instantiation (Actual);
11799 end if;
11800
11801 Next_Discriminant (Formal_Discr);
11802 Next_Discriminant (Actual_Discr);
11803 end loop;
11804
11805 if Actual_Discr /= Empty then
11806 Error_Msg_NE
11807 ("discriminants on actual do not match formal",
11808 Actual, Gen_T);
11809 Abandon_Instantiation (Actual);
11810 end if;
11811 end if;
11812 end if;
11813 end Validate_Discriminated_Formal_Type;
11814
11815 ---------------------------------------
11816 -- Validate_Incomplete_Type_Instance --
11817 ---------------------------------------
11818
11819 procedure Validate_Incomplete_Type_Instance is
11820 begin
11821 if not Is_Tagged_Type (Act_T)
11822 and then Is_Tagged_Type (A_Gen_T)
11823 then
11824 Error_Msg_NE
11825 ("actual for & must be a tagged type", Actual, Gen_T);
11826 end if;
11827
11828 Validate_Discriminated_Formal_Type;
11829 end Validate_Incomplete_Type_Instance;
11830
11831 --------------------------------------
11832 -- Validate_Interface_Type_Instance --
11833 --------------------------------------
11834
11835 procedure Validate_Interface_Type_Instance is
11836 begin
11837 if not Is_Interface (Act_T) then
11838 Error_Msg_NE
11839 ("actual for formal interface type must be an interface",
11840 Actual, Gen_T);
11841
11842 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
11843 or else
11844 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
11845 or else
11846 Is_Protected_Interface (A_Gen_T) /=
11847 Is_Protected_Interface (Act_T)
11848 or else
11849 Is_Synchronized_Interface (A_Gen_T) /=
11850 Is_Synchronized_Interface (Act_T)
11851 then
11852 Error_Msg_NE
11853 ("actual for interface& does not match (RM 12.5.5(4))",
11854 Actual, Gen_T);
11855 end if;
11856 end Validate_Interface_Type_Instance;
11857
11858 ------------------------------------
11859 -- Validate_Private_Type_Instance --
11860 ------------------------------------
11861
11862 procedure Validate_Private_Type_Instance is
11863 begin
11864 if Is_Limited_Type (Act_T)
11865 and then not Is_Limited_Type (A_Gen_T)
11866 then
11867 if In_Instance then
11868 null;
11869 else
11870 Error_Msg_NE
11871 ("actual for non-limited & cannot be a limited type", Actual,
11872 Gen_T);
11873 Explain_Limited_Type (Act_T, Actual);
11874 Abandon_Instantiation (Actual);
11875 end if;
11876
11877 elsif Known_To_Have_Preelab_Init (A_Gen_T)
11878 and then not Has_Preelaborable_Initialization (Act_T)
11879 then
11880 Error_Msg_NE
11881 ("actual for & must have preelaborable initialization", Actual,
11882 Gen_T);
11883
11884 elsif Is_Indefinite_Subtype (Act_T)
11885 and then not Is_Indefinite_Subtype (A_Gen_T)
11886 and then Ada_Version >= Ada_95
11887 then
11888 Error_Msg_NE
11889 ("actual for & must be a definite subtype", Actual, Gen_T);
11890
11891 elsif not Is_Tagged_Type (Act_T)
11892 and then Is_Tagged_Type (A_Gen_T)
11893 then
11894 Error_Msg_NE
11895 ("actual for & must be a tagged type", Actual, Gen_T);
11896 end if;
11897
11898 Validate_Discriminated_Formal_Type;
11899 Ancestor := Gen_T;
11900 end Validate_Private_Type_Instance;
11901
11902 -- Start of processing for Instantiate_Type
11903
11904 begin
11905 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
11906 Error_Msg_N ("duplicate instantiation of generic type", Actual);
11907 return New_List (Error);
11908
11909 elsif not Is_Entity_Name (Actual)
11910 or else not Is_Type (Entity (Actual))
11911 then
11912 Error_Msg_NE
11913 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
11914 Abandon_Instantiation (Actual);
11915
11916 else
11917 Act_T := Entity (Actual);
11918
11919 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
11920 -- as a generic actual parameter if the corresponding formal type
11921 -- does not have a known_discriminant_part, or is a formal derived
11922 -- type that is an Unchecked_Union type.
11923
11924 if Is_Unchecked_Union (Base_Type (Act_T)) then
11925 if not Has_Discriminants (A_Gen_T)
11926 or else
11927 (Is_Derived_Type (A_Gen_T)
11928 and then
11929 Is_Unchecked_Union (A_Gen_T))
11930 then
11931 null;
11932 else
11933 Error_Msg_N ("unchecked union cannot be the actual for a" &
11934 " discriminated formal type", Act_T);
11935
11936 end if;
11937 end if;
11938
11939 -- Deal with fixed/floating restrictions
11940
11941 if Is_Floating_Point_Type (Act_T) then
11942 Check_Restriction (No_Floating_Point, Actual);
11943 elsif Is_Fixed_Point_Type (Act_T) then
11944 Check_Restriction (No_Fixed_Point, Actual);
11945 end if;
11946
11947 -- Deal with error of using incomplete type as generic actual.
11948 -- This includes limited views of a type, even if the non-limited
11949 -- view may be available.
11950
11951 if Ekind (Act_T) = E_Incomplete_Type
11952 or else (Is_Class_Wide_Type (Act_T)
11953 and then
11954 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
11955 then
11956 -- If the formal is an incomplete type, the actual can be
11957 -- incomplete as well.
11958
11959 if Ekind (A_Gen_T) = E_Incomplete_Type then
11960 null;
11961
11962 elsif Is_Class_Wide_Type (Act_T)
11963 or else No (Full_View (Act_T))
11964 then
11965 Error_Msg_N ("premature use of incomplete type", Actual);
11966 Abandon_Instantiation (Actual);
11967 else
11968 Act_T := Full_View (Act_T);
11969 Set_Entity (Actual, Act_T);
11970
11971 if Has_Private_Component (Act_T) then
11972 Error_Msg_N
11973 ("premature use of type with private component", Actual);
11974 end if;
11975 end if;
11976
11977 -- Deal with error of premature use of private type as generic actual
11978
11979 elsif Is_Private_Type (Act_T)
11980 and then Is_Private_Type (Base_Type (Act_T))
11981 and then not Is_Generic_Type (Act_T)
11982 and then not Is_Derived_Type (Act_T)
11983 and then No (Full_View (Root_Type (Act_T)))
11984 then
11985 -- If the formal is an incomplete type, the actual can be
11986 -- private or incomplete as well.
11987
11988 if Ekind (A_Gen_T) = E_Incomplete_Type then
11989 null;
11990 else
11991 Error_Msg_N ("premature use of private type", Actual);
11992 end if;
11993
11994 elsif Has_Private_Component (Act_T) then
11995 Error_Msg_N
11996 ("premature use of type with private component", Actual);
11997 end if;
11998
11999 Set_Instance_Of (A_Gen_T, Act_T);
12000
12001 -- If the type is generic, the class-wide type may also be used
12002
12003 if Is_Tagged_Type (A_Gen_T)
12004 and then Is_Tagged_Type (Act_T)
12005 and then not Is_Class_Wide_Type (A_Gen_T)
12006 then
12007 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12008 Class_Wide_Type (Act_T));
12009 end if;
12010
12011 if not Is_Abstract_Type (A_Gen_T)
12012 and then Is_Abstract_Type (Act_T)
12013 then
12014 Error_Msg_N
12015 ("actual of non-abstract formal cannot be abstract", Actual);
12016 end if;
12017
12018 -- A generic scalar type is a first subtype for which we generate
12019 -- an anonymous base type. Indicate that the instance of this base
12020 -- is the base type of the actual.
12021
12022 if Is_Scalar_Type (A_Gen_T) then
12023 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12024 end if;
12025 end if;
12026
12027 if Error_Posted (Act_T) then
12028 null;
12029 else
12030 case Nkind (Def) is
12031 when N_Formal_Private_Type_Definition =>
12032 Validate_Private_Type_Instance;
12033
12034 when N_Formal_Incomplete_Type_Definition =>
12035 Validate_Incomplete_Type_Instance;
12036
12037 when N_Formal_Derived_Type_Definition =>
12038 Validate_Derived_Type_Instance;
12039
12040 when N_Formal_Discrete_Type_Definition =>
12041 if not Is_Discrete_Type (Act_T) then
12042 Error_Msg_NE
12043 ("expect discrete type in instantiation of&",
12044 Actual, Gen_T);
12045 Abandon_Instantiation (Actual);
12046 end if;
12047
12048 Diagnose_Predicated_Actual;
12049
12050 when N_Formal_Signed_Integer_Type_Definition =>
12051 if not Is_Signed_Integer_Type (Act_T) then
12052 Error_Msg_NE
12053 ("expect signed integer type in instantiation of&",
12054 Actual, Gen_T);
12055 Abandon_Instantiation (Actual);
12056 end if;
12057
12058 Diagnose_Predicated_Actual;
12059
12060 when N_Formal_Modular_Type_Definition =>
12061 if not Is_Modular_Integer_Type (Act_T) then
12062 Error_Msg_NE
12063 ("expect modular type in instantiation of &",
12064 Actual, Gen_T);
12065 Abandon_Instantiation (Actual);
12066 end if;
12067
12068 Diagnose_Predicated_Actual;
12069
12070 when N_Formal_Floating_Point_Definition =>
12071 if not Is_Floating_Point_Type (Act_T) then
12072 Error_Msg_NE
12073 ("expect float type in instantiation of &", Actual, Gen_T);
12074 Abandon_Instantiation (Actual);
12075 end if;
12076
12077 when N_Formal_Ordinary_Fixed_Point_Definition =>
12078 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12079 Error_Msg_NE
12080 ("expect ordinary fixed point type in instantiation of &",
12081 Actual, Gen_T);
12082 Abandon_Instantiation (Actual);
12083 end if;
12084
12085 when N_Formal_Decimal_Fixed_Point_Definition =>
12086 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12087 Error_Msg_NE
12088 ("expect decimal type in instantiation of &",
12089 Actual, Gen_T);
12090 Abandon_Instantiation (Actual);
12091 end if;
12092
12093 when N_Array_Type_Definition =>
12094 Validate_Array_Type_Instance;
12095
12096 when N_Access_To_Object_Definition =>
12097 Validate_Access_Type_Instance;
12098
12099 when N_Access_Function_Definition |
12100 N_Access_Procedure_Definition =>
12101 Validate_Access_Subprogram_Instance;
12102
12103 when N_Record_Definition =>
12104 Validate_Interface_Type_Instance;
12105
12106 when N_Derived_Type_Definition =>
12107 Validate_Derived_Interface_Type_Instance;
12108
12109 when others =>
12110 raise Program_Error;
12111
12112 end case;
12113 end if;
12114
12115 Subt := New_Copy (Gen_T);
12116
12117 -- Use adjusted sloc of subtype name as the location for other nodes in
12118 -- the subtype declaration.
12119
12120 Loc := Sloc (Subt);
12121
12122 Decl_Node :=
12123 Make_Subtype_Declaration (Loc,
12124 Defining_Identifier => Subt,
12125 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12126
12127 if Is_Private_Type (Act_T) then
12128 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12129
12130 elsif Is_Access_Type (Act_T)
12131 and then Is_Private_Type (Designated_Type (Act_T))
12132 then
12133 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12134 end if;
12135
12136 Decl_Nodes := New_List (Decl_Node);
12137
12138 -- Flag actual derived types so their elaboration produces the
12139 -- appropriate renamings for the primitive operations of the ancestor.
12140 -- Flag actual for formal private types as well, to determine whether
12141 -- operations in the private part may override inherited operations.
12142 -- If the formal has an interface list, the ancestor is not the
12143 -- parent, but the analyzed formal that includes the interface
12144 -- operations of all its progenitors.
12145
12146 -- Same treatment for formal private types, so we can check whether the
12147 -- type is tagged limited when validating derivations in the private
12148 -- part. (See AI05-096).
12149
12150 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12151 if Present (Interface_List (Def)) then
12152 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12153 else
12154 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12155 end if;
12156
12157 elsif Nkind_In (Def,
12158 N_Formal_Private_Type_Definition,
12159 N_Formal_Incomplete_Type_Definition)
12160 then
12161 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12162 end if;
12163
12164 -- If the actual is a synchronized type that implements an interface,
12165 -- the primitive operations are attached to the corresponding record,
12166 -- and we have to treat it as an additional generic actual, so that its
12167 -- primitive operations become visible in the instance. The task or
12168 -- protected type itself does not carry primitive operations.
12169
12170 if Is_Concurrent_Type (Act_T)
12171 and then Is_Tagged_Type (Act_T)
12172 and then Present (Corresponding_Record_Type (Act_T))
12173 and then Present (Ancestor)
12174 and then Is_Interface (Ancestor)
12175 then
12176 declare
12177 Corr_Rec : constant Entity_Id :=
12178 Corresponding_Record_Type (Act_T);
12179 New_Corr : Entity_Id;
12180 Corr_Decl : Node_Id;
12181
12182 begin
12183 New_Corr := Make_Temporary (Loc, 'S');
12184 Corr_Decl :=
12185 Make_Subtype_Declaration (Loc,
12186 Defining_Identifier => New_Corr,
12187 Subtype_Indication =>
12188 New_Occurrence_Of (Corr_Rec, Loc));
12189 Append_To (Decl_Nodes, Corr_Decl);
12190
12191 if Ekind (Act_T) = E_Task_Type then
12192 Set_Ekind (Subt, E_Task_Subtype);
12193 else
12194 Set_Ekind (Subt, E_Protected_Subtype);
12195 end if;
12196
12197 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12198 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12199 Set_Generic_Parent_Type (Decl_Node, Empty);
12200 end;
12201 end if;
12202
12203 return Decl_Nodes;
12204 end Instantiate_Type;
12205
12206 ---------------------
12207 -- Is_In_Main_Unit --
12208 ---------------------
12209
12210 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12211 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12212 Current_Unit : Node_Id;
12213
12214 begin
12215 if Unum = Main_Unit then
12216 return True;
12217
12218 -- If the current unit is a subunit then it is either the main unit or
12219 -- is being compiled as part of the main unit.
12220
12221 elsif Nkind (N) = N_Compilation_Unit then
12222 return Nkind (Unit (N)) = N_Subunit;
12223 end if;
12224
12225 Current_Unit := Parent (N);
12226 while Present (Current_Unit)
12227 and then Nkind (Current_Unit) /= N_Compilation_Unit
12228 loop
12229 Current_Unit := Parent (Current_Unit);
12230 end loop;
12231
12232 -- The instantiation node is in the main unit, or else the current node
12233 -- (perhaps as the result of nested instantiations) is in the main unit,
12234 -- or in the declaration of the main unit, which in this last case must
12235 -- be a body.
12236
12237 return Unum = Main_Unit
12238 or else Current_Unit = Cunit (Main_Unit)
12239 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12240 or else (Present (Library_Unit (Current_Unit))
12241 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12242 end Is_In_Main_Unit;
12243
12244 ----------------------------
12245 -- Load_Parent_Of_Generic --
12246 ----------------------------
12247
12248 procedure Load_Parent_Of_Generic
12249 (N : Node_Id;
12250 Spec : Node_Id;
12251 Body_Optional : Boolean := False)
12252 is
12253 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12254 Saved_Style_Check : constant Boolean := Style_Check;
12255 Saved_Warnings : constant Warning_Record := Save_Warnings;
12256 True_Parent : Node_Id;
12257 Inst_Node : Node_Id;
12258 OK : Boolean;
12259 Previous_Instances : constant Elist_Id := New_Elmt_List;
12260
12261 procedure Collect_Previous_Instances (Decls : List_Id);
12262 -- Collect all instantiations in the given list of declarations, that
12263 -- precede the generic that we need to load. If the bodies of these
12264 -- instantiations are available, we must analyze them, to ensure that
12265 -- the public symbols generated are the same when the unit is compiled
12266 -- to generate code, and when it is compiled in the context of a unit
12267 -- that needs a particular nested instance. This process is applied to
12268 -- both package and subprogram instances.
12269
12270 --------------------------------
12271 -- Collect_Previous_Instances --
12272 --------------------------------
12273
12274 procedure Collect_Previous_Instances (Decls : List_Id) is
12275 Decl : Node_Id;
12276
12277 begin
12278 Decl := First (Decls);
12279 while Present (Decl) loop
12280 if Sloc (Decl) >= Sloc (Inst_Node) then
12281 return;
12282
12283 -- If Decl is an instantiation, then record it as requiring
12284 -- instantiation of the corresponding body, except if it is an
12285 -- abbreviated instantiation generated internally for conformance
12286 -- checking purposes only for the case of a formal package
12287 -- declared without a box (see Instantiate_Formal_Package). Such
12288 -- an instantiation does not generate any code (the actual code
12289 -- comes from actual) and thus does not need to be analyzed here.
12290 -- If the instantiation appears with a generic package body it is
12291 -- not analyzed here either.
12292
12293 elsif Nkind (Decl) = N_Package_Instantiation
12294 and then not Is_Internal (Defining_Entity (Decl))
12295 then
12296 Append_Elmt (Decl, Previous_Instances);
12297
12298 -- For a subprogram instantiation, omit instantiations intrinsic
12299 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12300
12301 elsif Nkind_In (Decl, N_Function_Instantiation,
12302 N_Procedure_Instantiation)
12303 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12304 then
12305 Append_Elmt (Decl, Previous_Instances);
12306
12307 elsif Nkind (Decl) = N_Package_Declaration then
12308 Collect_Previous_Instances
12309 (Visible_Declarations (Specification (Decl)));
12310 Collect_Previous_Instances
12311 (Private_Declarations (Specification (Decl)));
12312
12313 -- Previous non-generic bodies may contain instances as well
12314
12315 elsif Nkind (Decl) = N_Package_Body
12316 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12317 then
12318 Collect_Previous_Instances (Declarations (Decl));
12319
12320 elsif Nkind (Decl) = N_Subprogram_Body
12321 and then not Acts_As_Spec (Decl)
12322 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12323 then
12324 Collect_Previous_Instances (Declarations (Decl));
12325 end if;
12326
12327 Next (Decl);
12328 end loop;
12329 end Collect_Previous_Instances;
12330
12331 -- Start of processing for Load_Parent_Of_Generic
12332
12333 begin
12334 if not In_Same_Source_Unit (N, Spec)
12335 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12336 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12337 and then not Is_In_Main_Unit (Spec))
12338 then
12339 -- Find body of parent of spec, and analyze it. A special case arises
12340 -- when the parent is an instantiation, that is to say when we are
12341 -- currently instantiating a nested generic. In that case, there is
12342 -- no separate file for the body of the enclosing instance. Instead,
12343 -- the enclosing body must be instantiated as if it were a pending
12344 -- instantiation, in order to produce the body for the nested generic
12345 -- we require now. Note that in that case the generic may be defined
12346 -- in a package body, the instance defined in the same package body,
12347 -- and the original enclosing body may not be in the main unit.
12348
12349 Inst_Node := Empty;
12350
12351 True_Parent := Parent (Spec);
12352 while Present (True_Parent)
12353 and then Nkind (True_Parent) /= N_Compilation_Unit
12354 loop
12355 if Nkind (True_Parent) = N_Package_Declaration
12356 and then
12357 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12358 then
12359 -- Parent is a compilation unit that is an instantiation.
12360 -- Instantiation node has been replaced with package decl.
12361
12362 Inst_Node := Original_Node (True_Parent);
12363 exit;
12364
12365 elsif Nkind (True_Parent) = N_Package_Declaration
12366 and then Present (Generic_Parent (Specification (True_Parent)))
12367 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12368 then
12369 -- Parent is an instantiation within another specification.
12370 -- Declaration for instance has been inserted before original
12371 -- instantiation node. A direct link would be preferable?
12372
12373 Inst_Node := Next (True_Parent);
12374 while Present (Inst_Node)
12375 and then Nkind (Inst_Node) /= N_Package_Instantiation
12376 loop
12377 Next (Inst_Node);
12378 end loop;
12379
12380 -- If the instance appears within a generic, and the generic
12381 -- unit is defined within a formal package of the enclosing
12382 -- generic, there is no generic body available, and none
12383 -- needed. A more precise test should be used ???
12384
12385 if No (Inst_Node) then
12386 return;
12387 end if;
12388
12389 exit;
12390
12391 else
12392 True_Parent := Parent (True_Parent);
12393 end if;
12394 end loop;
12395
12396 -- Case where we are currently instantiating a nested generic
12397
12398 if Present (Inst_Node) then
12399 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12400
12401 -- Instantiation node and declaration of instantiated package
12402 -- were exchanged when only the declaration was needed.
12403 -- Restore instantiation node before proceeding with body.
12404
12405 Set_Unit (Parent (True_Parent), Inst_Node);
12406 end if;
12407
12408 -- Now complete instantiation of enclosing body, if it appears in
12409 -- some other unit. If it appears in the current unit, the body
12410 -- will have been instantiated already.
12411
12412 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12413
12414 -- We need to determine the expander mode to instantiate the
12415 -- enclosing body. Because the generic body we need may use
12416 -- global entities declared in the enclosing package (including
12417 -- aggregates) it is in general necessary to compile this body
12418 -- with expansion enabled, except if we are within a generic
12419 -- package, in which case the usual generic rule applies.
12420
12421 declare
12422 Exp_Status : Boolean := True;
12423 Scop : Entity_Id;
12424
12425 begin
12426 -- Loop through scopes looking for generic package
12427
12428 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12429 while Present (Scop)
12430 and then Scop /= Standard_Standard
12431 loop
12432 if Ekind (Scop) = E_Generic_Package then
12433 Exp_Status := False;
12434 exit;
12435 end if;
12436
12437 Scop := Scope (Scop);
12438 end loop;
12439
12440 -- Collect previous instantiations in the unit that contains
12441 -- the desired generic.
12442
12443 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12444 and then not Body_Optional
12445 then
12446 declare
12447 Decl : Elmt_Id;
12448 Info : Pending_Body_Info;
12449 Par : Node_Id;
12450
12451 begin
12452 Par := Parent (Inst_Node);
12453 while Present (Par) loop
12454 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12455 Par := Parent (Par);
12456 end loop;
12457
12458 pragma Assert (Present (Par));
12459
12460 if Nkind (Par) = N_Package_Body then
12461 Collect_Previous_Instances (Declarations (Par));
12462
12463 elsif Nkind (Par) = N_Package_Declaration then
12464 Collect_Previous_Instances
12465 (Visible_Declarations (Specification (Par)));
12466 Collect_Previous_Instances
12467 (Private_Declarations (Specification (Par)));
12468
12469 else
12470 -- Enclosing unit is a subprogram body. In this
12471 -- case all instance bodies are processed in order
12472 -- and there is no need to collect them separately.
12473
12474 null;
12475 end if;
12476
12477 Decl := First_Elmt (Previous_Instances);
12478 while Present (Decl) loop
12479 Info :=
12480 (Inst_Node => Node (Decl),
12481 Act_Decl =>
12482 Instance_Spec (Node (Decl)),
12483 Expander_Status => Exp_Status,
12484 Current_Sem_Unit =>
12485 Get_Code_Unit (Sloc (Node (Decl))),
12486 Scope_Suppress => Scope_Suppress,
12487 Local_Suppress_Stack_Top =>
12488 Local_Suppress_Stack_Top,
12489 Version => Ada_Version,
12490 Version_Pragma => Ada_Version_Pragma,
12491 Warnings => Save_Warnings,
12492 SPARK_Mode => SPARK_Mode,
12493 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12494
12495 -- Package instance
12496
12497 if
12498 Nkind (Node (Decl)) = N_Package_Instantiation
12499 then
12500 Instantiate_Package_Body
12501 (Info, Body_Optional => True);
12502
12503 -- Subprogram instance
12504
12505 else
12506 -- The instance_spec is the wrapper package,
12507 -- and the subprogram declaration is the last
12508 -- declaration in the wrapper.
12509
12510 Info.Act_Decl :=
12511 Last
12512 (Visible_Declarations
12513 (Specification (Info.Act_Decl)));
12514
12515 Instantiate_Subprogram_Body
12516 (Info, Body_Optional => True);
12517 end if;
12518
12519 Next_Elmt (Decl);
12520 end loop;
12521 end;
12522 end if;
12523
12524 Instantiate_Package_Body
12525 (Body_Info =>
12526 ((Inst_Node => Inst_Node,
12527 Act_Decl => True_Parent,
12528 Expander_Status => Exp_Status,
12529 Current_Sem_Unit => Get_Code_Unit
12530 (Sloc (Inst_Node)),
12531 Scope_Suppress => Scope_Suppress,
12532 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12533 Version => Ada_Version,
12534 Version_Pragma => Ada_Version_Pragma,
12535 Warnings => Save_Warnings,
12536 SPARK_Mode => SPARK_Mode,
12537 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12538 Body_Optional => Body_Optional);
12539 end;
12540 end if;
12541
12542 -- Case where we are not instantiating a nested generic
12543
12544 else
12545 Opt.Style_Check := False;
12546 Expander_Mode_Save_And_Set (True);
12547 Load_Needed_Body (Comp_Unit, OK);
12548 Opt.Style_Check := Saved_Style_Check;
12549 Restore_Warnings (Saved_Warnings);
12550 Expander_Mode_Restore;
12551
12552 if not OK
12553 and then Unit_Requires_Body (Defining_Entity (Spec))
12554 and then not Body_Optional
12555 then
12556 declare
12557 Bname : constant Unit_Name_Type :=
12558 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12559
12560 begin
12561 -- In CodePeer mode, the missing body may make the analysis
12562 -- incomplete, but we do not treat it as fatal.
12563
12564 if CodePeer_Mode then
12565 return;
12566
12567 else
12568 Error_Msg_Unit_1 := Bname;
12569 Error_Msg_N ("this instantiation requires$!", N);
12570 Error_Msg_File_1 :=
12571 Get_File_Name (Bname, Subunit => False);
12572 Error_Msg_N ("\but file{ was not found!", N);
12573 raise Unrecoverable_Error;
12574 end if;
12575 end;
12576 end if;
12577 end if;
12578 end if;
12579
12580 -- If loading parent of the generic caused an instantiation circularity,
12581 -- we abandon compilation at this point, because otherwise in some cases
12582 -- we get into trouble with infinite recursions after this point.
12583
12584 if Circularity_Detected then
12585 raise Unrecoverable_Error;
12586 end if;
12587 end Load_Parent_Of_Generic;
12588
12589 ---------------------------------
12590 -- Map_Formal_Package_Entities --
12591 ---------------------------------
12592
12593 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
12594 E1 : Entity_Id;
12595 E2 : Entity_Id;
12596
12597 begin
12598 Set_Instance_Of (Form, Act);
12599
12600 -- Traverse formal and actual package to map the corresponding entities.
12601 -- We skip over internal entities that may be generated during semantic
12602 -- analysis, and find the matching entities by name, given that they
12603 -- must appear in the same order.
12604
12605 E1 := First_Entity (Form);
12606 E2 := First_Entity (Act);
12607 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
12608 -- Could this test be a single condition??? Seems like it could, and
12609 -- isn't FPE (Form) a constant anyway???
12610
12611 if not Is_Internal (E1)
12612 and then Present (Parent (E1))
12613 and then not Is_Class_Wide_Type (E1)
12614 and then not Is_Internal_Name (Chars (E1))
12615 then
12616 while Present (E2) and then Chars (E2) /= Chars (E1) loop
12617 Next_Entity (E2);
12618 end loop;
12619
12620 if No (E2) then
12621 exit;
12622 else
12623 Set_Instance_Of (E1, E2);
12624
12625 if Is_Type (E1) and then Is_Tagged_Type (E2) then
12626 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
12627 end if;
12628
12629 if Is_Constrained (E1) then
12630 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
12631 end if;
12632
12633 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
12634 Map_Formal_Package_Entities (E1, E2);
12635 end if;
12636 end if;
12637 end if;
12638
12639 Next_Entity (E1);
12640 end loop;
12641 end Map_Formal_Package_Entities;
12642
12643 -----------------------
12644 -- Move_Freeze_Nodes --
12645 -----------------------
12646
12647 procedure Move_Freeze_Nodes
12648 (Out_Of : Entity_Id;
12649 After : Node_Id;
12650 L : List_Id)
12651 is
12652 Decl : Node_Id;
12653 Next_Decl : Node_Id;
12654 Next_Node : Node_Id := After;
12655 Spec : Node_Id;
12656
12657 function Is_Outer_Type (T : Entity_Id) return Boolean;
12658 -- Check whether entity is declared in a scope external to that of the
12659 -- generic unit.
12660
12661 -------------------
12662 -- Is_Outer_Type --
12663 -------------------
12664
12665 function Is_Outer_Type (T : Entity_Id) return Boolean is
12666 Scop : Entity_Id := Scope (T);
12667
12668 begin
12669 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
12670 return True;
12671
12672 else
12673 while Scop /= Standard_Standard loop
12674 if Scop = Out_Of then
12675 return False;
12676 else
12677 Scop := Scope (Scop);
12678 end if;
12679 end loop;
12680
12681 return True;
12682 end if;
12683 end Is_Outer_Type;
12684
12685 -- Start of processing for Move_Freeze_Nodes
12686
12687 begin
12688 if No (L) then
12689 return;
12690 end if;
12691
12692 -- First remove the freeze nodes that may appear before all other
12693 -- declarations.
12694
12695 Decl := First (L);
12696 while Present (Decl)
12697 and then Nkind (Decl) = N_Freeze_Entity
12698 and then Is_Outer_Type (Entity (Decl))
12699 loop
12700 Decl := Remove_Head (L);
12701 Insert_After (Next_Node, Decl);
12702 Set_Analyzed (Decl, False);
12703 Next_Node := Decl;
12704 Decl := First (L);
12705 end loop;
12706
12707 -- Next scan the list of declarations and remove each freeze node that
12708 -- appears ahead of the current node.
12709
12710 while Present (Decl) loop
12711 while Present (Next (Decl))
12712 and then Nkind (Next (Decl)) = N_Freeze_Entity
12713 and then Is_Outer_Type (Entity (Next (Decl)))
12714 loop
12715 Next_Decl := Remove_Next (Decl);
12716 Insert_After (Next_Node, Next_Decl);
12717 Set_Analyzed (Next_Decl, False);
12718 Next_Node := Next_Decl;
12719 end loop;
12720
12721 -- If the declaration is a nested package or concurrent type, then
12722 -- recurse. Nested generic packages will have been processed from the
12723 -- inside out.
12724
12725 case Nkind (Decl) is
12726 when N_Package_Declaration =>
12727 Spec := Specification (Decl);
12728
12729 when N_Task_Type_Declaration =>
12730 Spec := Task_Definition (Decl);
12731
12732 when N_Protected_Type_Declaration =>
12733 Spec := Protected_Definition (Decl);
12734
12735 when others =>
12736 Spec := Empty;
12737 end case;
12738
12739 if Present (Spec) then
12740 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
12741 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
12742 end if;
12743
12744 Next (Decl);
12745 end loop;
12746 end Move_Freeze_Nodes;
12747
12748 ----------------
12749 -- Next_Assoc --
12750 ----------------
12751
12752 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
12753 begin
12754 return Generic_Renamings.Table (E).Next_In_HTable;
12755 end Next_Assoc;
12756
12757 ------------------------
12758 -- Preanalyze_Actuals --
12759 ------------------------
12760
12761 procedure Preanalyze_Actuals (N : Node_Id) is
12762 Assoc : Node_Id;
12763 Act : Node_Id;
12764 Errs : constant Int := Serious_Errors_Detected;
12765
12766 Cur : Entity_Id := Empty;
12767 -- Current homograph of the instance name
12768
12769 Vis : Boolean;
12770 -- Saved visibility status of the current homograph
12771
12772 begin
12773 Assoc := First (Generic_Associations (N));
12774
12775 -- If the instance is a child unit, its name may hide an outer homonym,
12776 -- so make it invisible to perform name resolution on the actuals.
12777
12778 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
12779 and then Present
12780 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
12781 then
12782 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
12783
12784 if Is_Compilation_Unit (Cur) then
12785 Vis := Is_Immediately_Visible (Cur);
12786 Set_Is_Immediately_Visible (Cur, False);
12787 else
12788 Cur := Empty;
12789 end if;
12790 end if;
12791
12792 while Present (Assoc) loop
12793 if Nkind (Assoc) /= N_Others_Choice then
12794 Act := Explicit_Generic_Actual_Parameter (Assoc);
12795
12796 -- Within a nested instantiation, a defaulted actual is an empty
12797 -- association, so nothing to analyze. If the subprogram actual
12798 -- is an attribute, analyze prefix only, because actual is not a
12799 -- complete attribute reference.
12800
12801 -- If actual is an allocator, analyze expression only. The full
12802 -- analysis can generate code, and if instance is a compilation
12803 -- unit we have to wait until the package instance is installed
12804 -- to have a proper place to insert this code.
12805
12806 -- String literals may be operators, but at this point we do not
12807 -- know whether the actual is a formal subprogram or a string.
12808
12809 if No (Act) then
12810 null;
12811
12812 elsif Nkind (Act) = N_Attribute_Reference then
12813 Analyze (Prefix (Act));
12814
12815 elsif Nkind (Act) = N_Explicit_Dereference then
12816 Analyze (Prefix (Act));
12817
12818 elsif Nkind (Act) = N_Allocator then
12819 declare
12820 Expr : constant Node_Id := Expression (Act);
12821
12822 begin
12823 if Nkind (Expr) = N_Subtype_Indication then
12824 Analyze (Subtype_Mark (Expr));
12825
12826 -- Analyze separately each discriminant constraint, when
12827 -- given with a named association.
12828
12829 declare
12830 Constr : Node_Id;
12831
12832 begin
12833 Constr := First (Constraints (Constraint (Expr)));
12834 while Present (Constr) loop
12835 if Nkind (Constr) = N_Discriminant_Association then
12836 Analyze (Expression (Constr));
12837 else
12838 Analyze (Constr);
12839 end if;
12840
12841 Next (Constr);
12842 end loop;
12843 end;
12844
12845 else
12846 Analyze (Expr);
12847 end if;
12848 end;
12849
12850 elsif Nkind (Act) /= N_Operator_Symbol then
12851 Analyze (Act);
12852 end if;
12853
12854 -- Ensure that a ghost subprogram does not act as generic actual
12855
12856 if Is_Entity_Name (Act)
12857 and then Is_Ghost_Subprogram (Entity (Act))
12858 then
12859 Error_Msg_N
12860 ("ghost subprogram & cannot act as generic actual", Act);
12861 Abandon_Instantiation (Act);
12862
12863 elsif Errs /= Serious_Errors_Detected then
12864
12865 -- Do a minimal analysis of the generic, to prevent spurious
12866 -- warnings complaining about the generic being unreferenced,
12867 -- before abandoning the instantiation.
12868
12869 Analyze (Name (N));
12870
12871 if Is_Entity_Name (Name (N))
12872 and then Etype (Name (N)) /= Any_Type
12873 then
12874 Generate_Reference (Entity (Name (N)), Name (N));
12875 Set_Is_Instantiated (Entity (Name (N)));
12876 end if;
12877
12878 if Present (Cur) then
12879
12880 -- For the case of a child instance hiding an outer homonym,
12881 -- provide additional warning which might explain the error.
12882
12883 Set_Is_Immediately_Visible (Cur, Vis);
12884 Error_Msg_NE ("& hides outer unit with the same name??",
12885 N, Defining_Unit_Name (N));
12886 end if;
12887
12888 Abandon_Instantiation (Act);
12889 end if;
12890 end if;
12891
12892 Next (Assoc);
12893 end loop;
12894
12895 if Present (Cur) then
12896 Set_Is_Immediately_Visible (Cur, Vis);
12897 end if;
12898 end Preanalyze_Actuals;
12899
12900 -------------------
12901 -- Remove_Parent --
12902 -------------------
12903
12904 procedure Remove_Parent (In_Body : Boolean := False) is
12905 S : Entity_Id := Current_Scope;
12906 -- S is the scope containing the instantiation just completed. The scope
12907 -- stack contains the parent instances of the instantiation, followed by
12908 -- the original S.
12909
12910 Cur_P : Entity_Id;
12911 E : Entity_Id;
12912 P : Entity_Id;
12913 Hidden : Elmt_Id;
12914
12915 begin
12916 -- After child instantiation is complete, remove from scope stack the
12917 -- extra copy of the current scope, and then remove parent instances.
12918
12919 if not In_Body then
12920 Pop_Scope;
12921
12922 while Current_Scope /= S loop
12923 P := Current_Scope;
12924 End_Package_Scope (Current_Scope);
12925
12926 if In_Open_Scopes (P) then
12927 E := First_Entity (P);
12928 while Present (E) loop
12929 Set_Is_Immediately_Visible (E, True);
12930 Next_Entity (E);
12931 end loop;
12932
12933 -- If instantiation is declared in a block, it is the enclosing
12934 -- scope that might be a parent instance. Note that only one
12935 -- block can be involved, because the parent instances have
12936 -- been installed within it.
12937
12938 if Ekind (P) = E_Block then
12939 Cur_P := Scope (P);
12940 else
12941 Cur_P := P;
12942 end if;
12943
12944 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
12945 -- We are within an instance of some sibling. Retain
12946 -- visibility of parent, for proper subsequent cleanup, and
12947 -- reinstall private declarations as well.
12948
12949 Set_In_Private_Part (P);
12950 Install_Private_Declarations (P);
12951 end if;
12952
12953 -- If the ultimate parent is a top-level unit recorded in
12954 -- Instance_Parent_Unit, then reset its visibility to what it was
12955 -- before instantiation. (It's not clear what the purpose is of
12956 -- testing whether Scope (P) is In_Open_Scopes, but that test was
12957 -- present before the ultimate parent test was added.???)
12958
12959 elsif not In_Open_Scopes (Scope (P))
12960 or else (P = Instance_Parent_Unit
12961 and then not Parent_Unit_Visible)
12962 then
12963 Set_Is_Immediately_Visible (P, False);
12964
12965 -- If the current scope is itself an instantiation of a generic
12966 -- nested within P, and we are in the private part of body of this
12967 -- instantiation, restore the full views of P, that were removed
12968 -- in End_Package_Scope above. This obscure case can occur when a
12969 -- subunit of a generic contains an instance of a child unit of
12970 -- its generic parent unit.
12971
12972 elsif S = Current_Scope and then Is_Generic_Instance (S) then
12973 declare
12974 Par : constant Entity_Id :=
12975 Generic_Parent (Package_Specification (S));
12976 begin
12977 if Present (Par)
12978 and then P = Scope (Par)
12979 and then (In_Package_Body (S) or else In_Private_Part (S))
12980 then
12981 Set_In_Private_Part (P);
12982 Install_Private_Declarations (P);
12983 end if;
12984 end;
12985 end if;
12986 end loop;
12987
12988 -- Reset visibility of entities in the enclosing scope
12989
12990 Set_Is_Hidden_Open_Scope (Current_Scope, False);
12991
12992 Hidden := First_Elmt (Hidden_Entities);
12993 while Present (Hidden) loop
12994 Set_Is_Immediately_Visible (Node (Hidden), True);
12995 Next_Elmt (Hidden);
12996 end loop;
12997
12998 else
12999 -- Each body is analyzed separately, and there is no context that
13000 -- needs preserving from one body instance to the next, so remove all
13001 -- parent scopes that have been installed.
13002
13003 while Present (S) loop
13004 End_Package_Scope (S);
13005 Set_Is_Immediately_Visible (S, False);
13006 S := Current_Scope;
13007 exit when S = Standard_Standard;
13008 end loop;
13009 end if;
13010 end Remove_Parent;
13011
13012 -----------------
13013 -- Restore_Env --
13014 -----------------
13015
13016 procedure Restore_Env is
13017 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13018
13019 begin
13020 if No (Current_Instantiated_Parent.Act_Id) then
13021 -- Restore environment after subprogram inlining
13022
13023 Restore_Private_Views (Empty);
13024 end if;
13025
13026 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13027 Exchanged_Views := Saved.Exchanged_Views;
13028 Hidden_Entities := Saved.Hidden_Entities;
13029 Current_Sem_Unit := Saved.Current_Sem_Unit;
13030 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13031 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13032
13033 Restore_Opt_Config_Switches (Saved.Switches);
13034
13035 Instance_Envs.Decrement_Last;
13036 end Restore_Env;
13037
13038 ---------------------------
13039 -- Restore_Private_Views --
13040 ---------------------------
13041
13042 procedure Restore_Private_Views
13043 (Pack_Id : Entity_Id;
13044 Is_Package : Boolean := True)
13045 is
13046 M : Elmt_Id;
13047 E : Entity_Id;
13048 Typ : Entity_Id;
13049 Dep_Elmt : Elmt_Id;
13050 Dep_Typ : Node_Id;
13051
13052 procedure Restore_Nested_Formal (Formal : Entity_Id);
13053 -- Hide the generic formals of formal packages declared with box which
13054 -- were reachable in the current instantiation.
13055
13056 ---------------------------
13057 -- Restore_Nested_Formal --
13058 ---------------------------
13059
13060 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13061 Ent : Entity_Id;
13062
13063 begin
13064 if Present (Renamed_Object (Formal))
13065 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13066 then
13067 return;
13068
13069 elsif Present (Associated_Formal_Package (Formal)) then
13070 Ent := First_Entity (Formal);
13071 while Present (Ent) loop
13072 exit when Ekind (Ent) = E_Package
13073 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13074
13075 Set_Is_Hidden (Ent);
13076 Set_Is_Potentially_Use_Visible (Ent, False);
13077
13078 -- If package, then recurse
13079
13080 if Ekind (Ent) = E_Package then
13081 Restore_Nested_Formal (Ent);
13082 end if;
13083
13084 Next_Entity (Ent);
13085 end loop;
13086 end if;
13087 end Restore_Nested_Formal;
13088
13089 -- Start of processing for Restore_Private_Views
13090
13091 begin
13092 M := First_Elmt (Exchanged_Views);
13093 while Present (M) loop
13094 Typ := Node (M);
13095
13096 -- Subtypes of types whose views have been exchanged, and that are
13097 -- defined within the instance, were not on the Private_Dependents
13098 -- list on entry to the instance, so they have to be exchanged
13099 -- explicitly now, in order to remain consistent with the view of the
13100 -- parent type.
13101
13102 if Ekind_In (Typ, E_Private_Type,
13103 E_Limited_Private_Type,
13104 E_Record_Type_With_Private)
13105 then
13106 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13107 while Present (Dep_Elmt) loop
13108 Dep_Typ := Node (Dep_Elmt);
13109
13110 if Scope (Dep_Typ) = Pack_Id
13111 and then Present (Full_View (Dep_Typ))
13112 then
13113 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13114 Exchange_Declarations (Dep_Typ);
13115 end if;
13116
13117 Next_Elmt (Dep_Elmt);
13118 end loop;
13119 end if;
13120
13121 Exchange_Declarations (Node (M));
13122 Next_Elmt (M);
13123 end loop;
13124
13125 if No (Pack_Id) then
13126 return;
13127 end if;
13128
13129 -- Make the generic formal parameters private, and make the formal types
13130 -- into subtypes of the actuals again.
13131
13132 E := First_Entity (Pack_Id);
13133 while Present (E) loop
13134 Set_Is_Hidden (E, True);
13135
13136 if Is_Type (E)
13137 and then Nkind (Parent (E)) = N_Subtype_Declaration
13138 then
13139 -- If the actual for E is itself a generic actual type from
13140 -- an enclosing instance, E is still a generic actual type
13141 -- outside of the current instance. This matter when resolving
13142 -- an overloaded call that may be ambiguous in the enclosing
13143 -- instance, when two of its actuals coincide.
13144
13145 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13146 and then Is_Generic_Actual_Type
13147 (Entity (Subtype_Indication (Parent (E))))
13148 then
13149 null;
13150 else
13151 Set_Is_Generic_Actual_Type (E, False);
13152 end if;
13153
13154 -- An unusual case of aliasing: the actual may also be directly
13155 -- visible in the generic, and be private there, while it is fully
13156 -- visible in the context of the instance. The internal subtype
13157 -- is private in the instance but has full visibility like its
13158 -- parent in the enclosing scope. This enforces the invariant that
13159 -- the privacy status of all private dependents of a type coincide
13160 -- with that of the parent type. This can only happen when a
13161 -- generic child unit is instantiated within a sibling.
13162
13163 if Is_Private_Type (E)
13164 and then not Is_Private_Type (Etype (E))
13165 then
13166 Exchange_Declarations (E);
13167 end if;
13168
13169 elsif Ekind (E) = E_Package then
13170
13171 -- The end of the renaming list is the renaming of the generic
13172 -- package itself. If the instance is a subprogram, all entities
13173 -- in the corresponding package are renamings. If this entity is
13174 -- a formal package, make its own formals private as well. The
13175 -- actual in this case is itself the renaming of an instantiation.
13176 -- If the entity is not a package renaming, it is the entity
13177 -- created to validate formal package actuals: ignore it.
13178
13179 -- If the actual is itself a formal package for the enclosing
13180 -- generic, or the actual for such a formal package, it remains
13181 -- visible on exit from the instance, and therefore nothing needs
13182 -- to be done either, except to keep it accessible.
13183
13184 if Is_Package and then Renamed_Object (E) = Pack_Id then
13185 exit;
13186
13187 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13188 null;
13189
13190 elsif
13191 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13192 then
13193 Set_Is_Hidden (E, False);
13194
13195 else
13196 declare
13197 Act_P : constant Entity_Id := Renamed_Object (E);
13198 Id : Entity_Id;
13199
13200 begin
13201 Id := First_Entity (Act_P);
13202 while Present (Id)
13203 and then Id /= First_Private_Entity (Act_P)
13204 loop
13205 exit when Ekind (Id) = E_Package
13206 and then Renamed_Object (Id) = Act_P;
13207
13208 Set_Is_Hidden (Id, True);
13209 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13210
13211 if Ekind (Id) = E_Package then
13212 Restore_Nested_Formal (Id);
13213 end if;
13214
13215 Next_Entity (Id);
13216 end loop;
13217 end;
13218 end if;
13219 end if;
13220
13221 Next_Entity (E);
13222 end loop;
13223 end Restore_Private_Views;
13224
13225 --------------
13226 -- Save_Env --
13227 --------------
13228
13229 procedure Save_Env
13230 (Gen_Unit : Entity_Id;
13231 Act_Unit : Entity_Id)
13232 is
13233 begin
13234 Init_Env;
13235 Set_Instance_Env (Gen_Unit, Act_Unit);
13236 end Save_Env;
13237
13238 ----------------------------
13239 -- Save_Global_References --
13240 ----------------------------
13241
13242 procedure Save_Global_References (N : Node_Id) is
13243 Gen_Scope : Entity_Id;
13244 E : Entity_Id;
13245 N2 : Node_Id;
13246
13247 function Is_Global (E : Entity_Id) return Boolean;
13248 -- Check whether entity is defined outside of generic unit. Examine the
13249 -- scope of an entity, and the scope of the scope, etc, until we find
13250 -- either Standard, in which case the entity is global, or the generic
13251 -- unit itself, which indicates that the entity is local. If the entity
13252 -- is the generic unit itself, as in the case of a recursive call, or
13253 -- the enclosing generic unit, if different from the current scope, then
13254 -- it is local as well, because it will be replaced at the point of
13255 -- instantiation. On the other hand, if it is a reference to a child
13256 -- unit of a common ancestor, which appears in an instantiation, it is
13257 -- global because it is used to denote a specific compilation unit at
13258 -- the time the instantiations will be analyzed.
13259
13260 procedure Reset_Entity (N : Node_Id);
13261 -- Save semantic information on global entity so that it is not resolved
13262 -- again at instantiation time.
13263
13264 procedure Save_Entity_Descendants (N : Node_Id);
13265 -- Apply Save_Global_References to the two syntactic descendants of
13266 -- non-terminal nodes that carry an Associated_Node and are processed
13267 -- through Reset_Entity. Once the global entity (if any) has been
13268 -- captured together with its type, only two syntactic descendants need
13269 -- to be traversed to complete the processing of the tree rooted at N.
13270 -- This applies to Selected_Components, Expanded_Names, and to Operator
13271 -- nodes. N can also be a character literal, identifier, or operator
13272 -- symbol node, but the call has no effect in these cases.
13273
13274 procedure Save_Global_Defaults (N1, N2 : Node_Id);
13275 -- Default actuals in nested instances must be handled specially
13276 -- because there is no link to them from the original tree. When an
13277 -- actual subprogram is given by a default, we add an explicit generic
13278 -- association for it in the instantiation node. When we save the
13279 -- global references on the name of the instance, we recover the list
13280 -- of generic associations, and add an explicit one to the original
13281 -- generic tree, through which a global actual can be preserved.
13282 -- Similarly, if a child unit is instantiated within a sibling, in the
13283 -- context of the parent, we must preserve the identifier of the parent
13284 -- so that it can be properly resolved in a subsequent instantiation.
13285
13286 procedure Save_Global_Descendant (D : Union_Id);
13287 -- Apply Save_Global_References recursively to the descendents of the
13288 -- current node.
13289
13290 procedure Save_References (N : Node_Id);
13291 -- This is the recursive procedure that does the work, once the
13292 -- enclosing generic scope has been established.
13293
13294 ---------------
13295 -- Is_Global --
13296 ---------------
13297
13298 function Is_Global (E : Entity_Id) return Boolean is
13299 Se : Entity_Id;
13300
13301 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13302 -- Determine whether the parent node of a reference to a child unit
13303 -- denotes an instantiation or a formal package, in which case the
13304 -- reference to the child unit is global, even if it appears within
13305 -- the current scope (e.g. when the instance appears within the body
13306 -- of an ancestor).
13307
13308 ----------------------
13309 -- Is_Instance_Node --
13310 ----------------------
13311
13312 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13313 begin
13314 return Nkind (Decl) in N_Generic_Instantiation
13315 or else
13316 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13317 end Is_Instance_Node;
13318
13319 -- Start of processing for Is_Global
13320
13321 begin
13322 if E = Gen_Scope then
13323 return False;
13324
13325 elsif E = Standard_Standard then
13326 return True;
13327
13328 elsif Is_Child_Unit (E)
13329 and then (Is_Instance_Node (Parent (N2))
13330 or else (Nkind (Parent (N2)) = N_Expanded_Name
13331 and then N2 = Selector_Name (Parent (N2))
13332 and then
13333 Is_Instance_Node (Parent (Parent (N2)))))
13334 then
13335 return True;
13336
13337 else
13338 Se := Scope (E);
13339 while Se /= Gen_Scope loop
13340 if Se = Standard_Standard then
13341 return True;
13342 else
13343 Se := Scope (Se);
13344 end if;
13345 end loop;
13346
13347 return False;
13348 end if;
13349 end Is_Global;
13350
13351 ------------------
13352 -- Reset_Entity --
13353 ------------------
13354
13355 procedure Reset_Entity (N : Node_Id) is
13356
13357 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13358 -- If the type of N2 is global to the generic unit, save the type in
13359 -- the generic node. Just as we perform name capture for explicit
13360 -- references within the generic, we must capture the global types
13361 -- of local entities because they may participate in resolution in
13362 -- the instance.
13363
13364 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13365 -- Find the ultimate ancestor of the current unit. If it is not a
13366 -- generic unit, then the name of the current unit in the prefix of
13367 -- an expanded name must be replaced with its generic homonym to
13368 -- ensure that it will be properly resolved in an instance.
13369
13370 ---------------------
13371 -- Set_Global_Type --
13372 ---------------------
13373
13374 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13375 Typ : constant Entity_Id := Etype (N2);
13376
13377 begin
13378 Set_Etype (N, Typ);
13379
13380 if Entity (N) /= N2
13381 and then Has_Private_View (Entity (N))
13382 then
13383 -- If the entity of N is not the associated node, this is a
13384 -- nested generic and it has an associated node as well, whose
13385 -- type is already the full view (see below). Indicate that the
13386 -- original node has a private view.
13387
13388 Set_Has_Private_View (N);
13389 end if;
13390
13391 -- If not a private type, nothing else to do
13392
13393 if not Is_Private_Type (Typ) then
13394 if Is_Array_Type (Typ)
13395 and then Is_Private_Type (Component_Type (Typ))
13396 then
13397 Set_Has_Private_View (N);
13398 end if;
13399
13400 -- If it is a derivation of a private type in a context where no
13401 -- full view is needed, nothing to do either.
13402
13403 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13404 null;
13405
13406 -- Otherwise mark the type for flipping and use the full view when
13407 -- available.
13408
13409 else
13410 Set_Has_Private_View (N);
13411
13412 if Present (Full_View (Typ)) then
13413 Set_Etype (N2, Full_View (Typ));
13414 end if;
13415 end if;
13416 end Set_Global_Type;
13417
13418 ------------------
13419 -- Top_Ancestor --
13420 ------------------
13421
13422 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13423 Par : Entity_Id;
13424
13425 begin
13426 Par := E;
13427 while Is_Child_Unit (Par) loop
13428 Par := Scope (Par);
13429 end loop;
13430
13431 return Par;
13432 end Top_Ancestor;
13433
13434 -- Start of processing for Reset_Entity
13435
13436 begin
13437 N2 := Get_Associated_Node (N);
13438 E := Entity (N2);
13439
13440 if Present (E) then
13441
13442 -- If the node is an entry call to an entry in an enclosing task,
13443 -- it is rewritten as a selected component. No global entity to
13444 -- preserve in this case, since the expansion will be redone in
13445 -- the instance.
13446
13447 if not Nkind_In (E, N_Defining_Identifier,
13448 N_Defining_Character_Literal,
13449 N_Defining_Operator_Symbol)
13450 then
13451 Set_Associated_Node (N, Empty);
13452 Set_Etype (N, Empty);
13453 return;
13454 end if;
13455
13456 -- If the entity is an itype created as a subtype of an access
13457 -- type with a null exclusion restore source entity for proper
13458 -- visibility. The itype will be created anew in the instance.
13459
13460 if Is_Itype (E)
13461 and then Ekind (E) = E_Access_Subtype
13462 and then Is_Entity_Name (N)
13463 and then Chars (Etype (E)) = Chars (N)
13464 then
13465 E := Etype (E);
13466 Set_Entity (N2, E);
13467 Set_Etype (N2, E);
13468 end if;
13469
13470 if Is_Global (E) then
13471
13472 -- If the entity is a package renaming that is the prefix of
13473 -- an expanded name, it has been rewritten as the renamed
13474 -- package, which is necessary semantically but complicates
13475 -- ASIS tree traversal, so we recover the original entity to
13476 -- expose the renaming. Take into account that the context may
13477 -- be a nested generic, that the original node may itself have
13478 -- an associated node that had better be an entity, and that
13479 -- the current node is still a selected component.
13480
13481 if Ekind (E) = E_Package
13482 and then Nkind (N) = N_Selected_Component
13483 and then Nkind (Parent (N)) = N_Expanded_Name
13484 and then Present (Original_Node (N2))
13485 and then Is_Entity_Name (Original_Node (N2))
13486 and then Present (Entity (Original_Node (N2)))
13487 then
13488 if Is_Global (Entity (Original_Node (N2))) then
13489 N2 := Original_Node (N2);
13490 Set_Associated_Node (N, N2);
13491 Set_Global_Type (N, N2);
13492
13493 else
13494 -- Renaming is local, and will be resolved in instance
13495
13496 Set_Associated_Node (N, Empty);
13497 Set_Etype (N, Empty);
13498 end if;
13499
13500 else
13501 Set_Global_Type (N, N2);
13502 end if;
13503
13504 elsif Nkind (N) = N_Op_Concat
13505 and then Is_Generic_Type (Etype (N2))
13506 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13507 or else
13508 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13509 and then Is_Intrinsic_Subprogram (E)
13510 then
13511 null;
13512
13513 else
13514 -- Entity is local. Mark generic node as unresolved.
13515 -- Note that now it does not have an entity.
13516
13517 Set_Associated_Node (N, Empty);
13518 Set_Etype (N, Empty);
13519 end if;
13520
13521 if Nkind (Parent (N)) in N_Generic_Instantiation
13522 and then N = Name (Parent (N))
13523 then
13524 Save_Global_Defaults (Parent (N), Parent (N2));
13525 end if;
13526
13527 elsif Nkind (Parent (N)) = N_Selected_Component
13528 and then Nkind (Parent (N2)) = N_Expanded_Name
13529 then
13530 if Is_Global (Entity (Parent (N2))) then
13531 Change_Selected_Component_To_Expanded_Name (Parent (N));
13532 Set_Associated_Node (Parent (N), Parent (N2));
13533 Set_Global_Type (Parent (N), Parent (N2));
13534 Save_Entity_Descendants (N);
13535
13536 -- If this is a reference to the current generic entity, replace
13537 -- by the name of the generic homonym of the current package. This
13538 -- is because in an instantiation Par.P.Q will not resolve to the
13539 -- name of the instance, whose enclosing scope is not necessarily
13540 -- Par. We use the generic homonym rather that the name of the
13541 -- generic itself because it may be hidden by a local declaration.
13542
13543 elsif In_Open_Scopes (Entity (Parent (N2)))
13544 and then not
13545 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13546 then
13547 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13548 Rewrite (Parent (N),
13549 Make_Identifier (Sloc (N),
13550 Chars =>
13551 Chars (Generic_Homonym (Entity (Parent (N2))))));
13552 else
13553 Rewrite (Parent (N),
13554 Make_Identifier (Sloc (N),
13555 Chars => Chars (Selector_Name (Parent (N2)))));
13556 end if;
13557 end if;
13558
13559 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13560 and then Parent (N) = Name (Parent (Parent (N)))
13561 then
13562 Save_Global_Defaults
13563 (Parent (Parent (N)), Parent (Parent ((N2))));
13564 end if;
13565
13566 -- A selected component may denote a static constant that has been
13567 -- folded. If the static constant is global to the generic, capture
13568 -- its value. Otherwise the folding will happen in any instantiation.
13569
13570 elsif Nkind (Parent (N)) = N_Selected_Component
13571 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13572 then
13573 if Present (Entity (Original_Node (Parent (N2))))
13574 and then Is_Global (Entity (Original_Node (Parent (N2))))
13575 then
13576 Rewrite (Parent (N), New_Copy (Parent (N2)));
13577 Set_Analyzed (Parent (N), False);
13578
13579 else
13580 null;
13581 end if;
13582
13583 -- A selected component may be transformed into a parameterless
13584 -- function call. If the called entity is global, rewrite the node
13585 -- appropriately, i.e. as an extended name for the global entity.
13586
13587 elsif Nkind (Parent (N)) = N_Selected_Component
13588 and then Nkind (Parent (N2)) = N_Function_Call
13589 and then N = Selector_Name (Parent (N))
13590 then
13591 if No (Parameter_Associations (Parent (N2))) then
13592 if Is_Global (Entity (Name (Parent (N2)))) then
13593 Change_Selected_Component_To_Expanded_Name (Parent (N));
13594 Set_Associated_Node (Parent (N), Name (Parent (N2)));
13595 Set_Global_Type (Parent (N), Name (Parent (N2)));
13596 Save_Entity_Descendants (N);
13597
13598 else
13599 Set_Is_Prefixed_Call (Parent (N));
13600 Set_Associated_Node (N, Empty);
13601 Set_Etype (N, Empty);
13602 end if;
13603
13604 -- In Ada 2005, X.F may be a call to a primitive operation,
13605 -- rewritten as F (X). This rewriting will be done again in an
13606 -- instance, so keep the original node. Global entities will be
13607 -- captured as for other constructs. Indicate that this must
13608 -- resolve as a call, to prevent accidental overloading in the
13609 -- instance, if both a component and a primitive operation appear
13610 -- as candidates.
13611
13612 else
13613 Set_Is_Prefixed_Call (Parent (N));
13614 end if;
13615
13616 -- Entity is local. Reset in generic unit, so that node is resolved
13617 -- anew at the point of instantiation.
13618
13619 else
13620 Set_Associated_Node (N, Empty);
13621 Set_Etype (N, Empty);
13622 end if;
13623 end Reset_Entity;
13624
13625 -----------------------------
13626 -- Save_Entity_Descendants --
13627 -----------------------------
13628
13629 procedure Save_Entity_Descendants (N : Node_Id) is
13630 begin
13631 case Nkind (N) is
13632 when N_Binary_Op =>
13633 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
13634 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13635
13636 when N_Unary_Op =>
13637 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13638
13639 when N_Expanded_Name | N_Selected_Component =>
13640 Save_Global_Descendant (Union_Id (Prefix (N)));
13641 Save_Global_Descendant (Union_Id (Selector_Name (N)));
13642
13643 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
13644 null;
13645
13646 when others =>
13647 raise Program_Error;
13648 end case;
13649 end Save_Entity_Descendants;
13650
13651 --------------------------
13652 -- Save_Global_Defaults --
13653 --------------------------
13654
13655 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
13656 Loc : constant Source_Ptr := Sloc (N1);
13657 Assoc2 : constant List_Id := Generic_Associations (N2);
13658 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
13659 Assoc1 : List_Id;
13660 Act1 : Node_Id;
13661 Act2 : Node_Id;
13662 Def : Node_Id;
13663 Ndec : Node_Id;
13664 Subp : Entity_Id;
13665 Actual : Entity_Id;
13666
13667 begin
13668 Assoc1 := Generic_Associations (N1);
13669
13670 if Present (Assoc1) then
13671 Act1 := First (Assoc1);
13672 else
13673 Act1 := Empty;
13674 Set_Generic_Associations (N1, New_List);
13675 Assoc1 := Generic_Associations (N1);
13676 end if;
13677
13678 if Present (Assoc2) then
13679 Act2 := First (Assoc2);
13680 else
13681 return;
13682 end if;
13683
13684 while Present (Act1) and then Present (Act2) loop
13685 Next (Act1);
13686 Next (Act2);
13687 end loop;
13688
13689 -- Find the associations added for default subprograms
13690
13691 if Present (Act2) then
13692 while Nkind (Act2) /= N_Generic_Association
13693 or else No (Entity (Selector_Name (Act2)))
13694 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
13695 loop
13696 Next (Act2);
13697 end loop;
13698
13699 -- Add a similar association if the default is global. The
13700 -- renaming declaration for the actual has been analyzed, and
13701 -- its alias is the program it renames. Link the actual in the
13702 -- original generic tree with the node in the analyzed tree.
13703
13704 while Present (Act2) loop
13705 Subp := Entity (Selector_Name (Act2));
13706 Def := Explicit_Generic_Actual_Parameter (Act2);
13707
13708 -- Following test is defence against rubbish errors
13709
13710 if No (Alias (Subp)) then
13711 return;
13712 end if;
13713
13714 -- Retrieve the resolved actual from the renaming declaration
13715 -- created for the instantiated formal.
13716
13717 Actual := Entity (Name (Parent (Parent (Subp))));
13718 Set_Entity (Def, Actual);
13719 Set_Etype (Def, Etype (Actual));
13720
13721 if Is_Global (Actual) then
13722 Ndec :=
13723 Make_Generic_Association (Loc,
13724 Selector_Name => New_Occurrence_Of (Subp, Loc),
13725 Explicit_Generic_Actual_Parameter =>
13726 New_Occurrence_Of (Actual, Loc));
13727
13728 Set_Associated_Node
13729 (Explicit_Generic_Actual_Parameter (Ndec), Def);
13730
13731 Append (Ndec, Assoc1);
13732
13733 -- If there are other defaults, add a dummy association in case
13734 -- there are other defaulted formals with the same name.
13735
13736 elsif Present (Next (Act2)) then
13737 Ndec :=
13738 Make_Generic_Association (Loc,
13739 Selector_Name => New_Occurrence_Of (Subp, Loc),
13740 Explicit_Generic_Actual_Parameter => Empty);
13741
13742 Append (Ndec, Assoc1);
13743 end if;
13744
13745 Next (Act2);
13746 end loop;
13747 end if;
13748
13749 if Nkind (Name (N1)) = N_Identifier
13750 and then Is_Child_Unit (Gen_Id)
13751 and then Is_Global (Gen_Id)
13752 and then Is_Generic_Unit (Scope (Gen_Id))
13753 and then In_Open_Scopes (Scope (Gen_Id))
13754 then
13755 -- This is an instantiation of a child unit within a sibling, so
13756 -- that the generic parent is in scope. An eventual instance must
13757 -- occur within the scope of an instance of the parent. Make name
13758 -- in instance into an expanded name, to preserve the identifier
13759 -- of the parent, so it can be resolved subsequently.
13760
13761 Rewrite (Name (N2),
13762 Make_Expanded_Name (Loc,
13763 Chars => Chars (Gen_Id),
13764 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13765 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13766 Set_Entity (Name (N2), Gen_Id);
13767
13768 Rewrite (Name (N1),
13769 Make_Expanded_Name (Loc,
13770 Chars => Chars (Gen_Id),
13771 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13772 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13773
13774 Set_Associated_Node (Name (N1), Name (N2));
13775 Set_Associated_Node (Prefix (Name (N1)), Empty);
13776 Set_Associated_Node
13777 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
13778 Set_Etype (Name (N1), Etype (Gen_Id));
13779 end if;
13780
13781 end Save_Global_Defaults;
13782
13783 ----------------------------
13784 -- Save_Global_Descendant --
13785 ----------------------------
13786
13787 procedure Save_Global_Descendant (D : Union_Id) is
13788 N1 : Node_Id;
13789
13790 begin
13791 if D in Node_Range then
13792 if D = Union_Id (Empty) then
13793 null;
13794
13795 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
13796 Save_References (Node_Id (D));
13797 end if;
13798
13799 elsif D in List_Range then
13800 if D = Union_Id (No_List)
13801 or else Is_Empty_List (List_Id (D))
13802 then
13803 null;
13804
13805 else
13806 N1 := First (List_Id (D));
13807 while Present (N1) loop
13808 Save_References (N1);
13809 Next (N1);
13810 end loop;
13811 end if;
13812
13813 -- Element list or other non-node field, nothing to do
13814
13815 else
13816 null;
13817 end if;
13818 end Save_Global_Descendant;
13819
13820 ---------------------
13821 -- Save_References --
13822 ---------------------
13823
13824 -- This is the recursive procedure that does the work once the enclosing
13825 -- generic scope has been established. We have to treat specially a
13826 -- number of node rewritings that are required by semantic processing
13827 -- and which change the kind of nodes in the generic copy: typically
13828 -- constant-folding, replacing an operator node by a string literal, or
13829 -- a selected component by an expanded name. In each of those cases, the
13830 -- transformation is propagated to the generic unit.
13831
13832 procedure Save_References (N : Node_Id) is
13833 Loc : constant Source_Ptr := Sloc (N);
13834
13835 begin
13836 if N = Empty then
13837 null;
13838
13839 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
13840 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13841 Reset_Entity (N);
13842
13843 elsif Nkind (N) = N_Operator_Symbol
13844 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
13845 then
13846 Change_Operator_Symbol_To_String_Literal (N);
13847 end if;
13848
13849 elsif Nkind (N) in N_Op then
13850 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13851 if Nkind (N) = N_Op_Concat then
13852 Set_Is_Component_Left_Opnd (N,
13853 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13854
13855 Set_Is_Component_Right_Opnd (N,
13856 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13857 end if;
13858
13859 Reset_Entity (N);
13860
13861 else
13862 -- Node may be transformed into call to a user-defined operator
13863
13864 N2 := Get_Associated_Node (N);
13865
13866 if Nkind (N2) = N_Function_Call then
13867 E := Entity (Name (N2));
13868
13869 if Present (E)
13870 and then Is_Global (E)
13871 then
13872 Set_Etype (N, Etype (N2));
13873 else
13874 Set_Associated_Node (N, Empty);
13875 Set_Etype (N, Empty);
13876 end if;
13877
13878 elsif Nkind_In (N2, N_Integer_Literal,
13879 N_Real_Literal,
13880 N_String_Literal)
13881 then
13882 if Present (Original_Node (N2))
13883 and then Nkind (Original_Node (N2)) = Nkind (N)
13884 then
13885
13886 -- Operation was constant-folded. Whenever possible,
13887 -- recover semantic information from unfolded node,
13888 -- for ASIS use.
13889
13890 Set_Associated_Node (N, Original_Node (N2));
13891
13892 if Nkind (N) = N_Op_Concat then
13893 Set_Is_Component_Left_Opnd (N,
13894 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13895 Set_Is_Component_Right_Opnd (N,
13896 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13897 end if;
13898
13899 Reset_Entity (N);
13900
13901 else
13902 -- If original node is already modified, propagate
13903 -- constant-folding to template.
13904
13905 Rewrite (N, New_Copy (N2));
13906 Set_Analyzed (N, False);
13907 end if;
13908
13909 elsif Nkind (N2) = N_Identifier
13910 and then Ekind (Entity (N2)) = E_Enumeration_Literal
13911 then
13912 -- Same if call was folded into a literal, but in this case
13913 -- retain the entity to avoid spurious ambiguities if it is
13914 -- overloaded at the point of instantiation or inlining.
13915
13916 Rewrite (N, New_Copy (N2));
13917 Set_Analyzed (N, False);
13918 end if;
13919 end if;
13920
13921 -- Complete operands check if node has not been constant-folded
13922
13923 if Nkind (N) in N_Op then
13924 Save_Entity_Descendants (N);
13925 end if;
13926
13927 elsif Nkind (N) = N_Identifier then
13928 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13929
13930 -- If this is a discriminant reference, always save it. It is
13931 -- used in the instance to find the corresponding discriminant
13932 -- positionally rather than by name.
13933
13934 Set_Original_Discriminant
13935 (N, Original_Discriminant (Get_Associated_Node (N)));
13936 Reset_Entity (N);
13937
13938 else
13939 N2 := Get_Associated_Node (N);
13940
13941 if Nkind (N2) = N_Function_Call then
13942 E := Entity (Name (N2));
13943
13944 -- Name resolves to a call to parameterless function. If
13945 -- original entity is global, mark node as resolved.
13946
13947 if Present (E)
13948 and then Is_Global (E)
13949 then
13950 Set_Etype (N, Etype (N2));
13951 else
13952 Set_Associated_Node (N, Empty);
13953 Set_Etype (N, Empty);
13954 end if;
13955
13956 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
13957 and then Is_Entity_Name (Original_Node (N2))
13958 then
13959 -- Name resolves to named number that is constant-folded,
13960 -- We must preserve the original name for ASIS use, and
13961 -- undo the constant-folding, which will be repeated in
13962 -- each instance.
13963
13964 Set_Associated_Node (N, Original_Node (N2));
13965 Reset_Entity (N);
13966
13967 elsif Nkind (N2) = N_String_Literal then
13968
13969 -- Name resolves to string literal. Perform the same
13970 -- replacement in generic.
13971
13972 Rewrite (N, New_Copy (N2));
13973
13974 elsif Nkind (N2) = N_Explicit_Dereference then
13975
13976 -- An identifier is rewritten as a dereference if it is the
13977 -- prefix in an implicit dereference (call or attribute).
13978 -- The analysis of an instantiation will expand the node
13979 -- again, so we preserve the original tree but link it to
13980 -- the resolved entity in case it is global.
13981
13982 if Is_Entity_Name (Prefix (N2))
13983 and then Present (Entity (Prefix (N2)))
13984 and then Is_Global (Entity (Prefix (N2)))
13985 then
13986 Set_Associated_Node (N, Prefix (N2));
13987
13988 elsif Nkind (Prefix (N2)) = N_Function_Call
13989 and then Is_Global (Entity (Name (Prefix (N2))))
13990 then
13991 Rewrite (N,
13992 Make_Explicit_Dereference (Loc,
13993 Prefix => Make_Function_Call (Loc,
13994 Name =>
13995 New_Occurrence_Of (Entity (Name (Prefix (N2))),
13996 Loc))));
13997
13998 else
13999 Set_Associated_Node (N, Empty);
14000 Set_Etype (N, Empty);
14001 end if;
14002
14003 -- The subtype mark of a nominally unconstrained object is
14004 -- rewritten as a subtype indication using the bounds of the
14005 -- expression. Recover the original subtype mark.
14006
14007 elsif Nkind (N2) = N_Subtype_Indication
14008 and then Is_Entity_Name (Original_Node (N2))
14009 then
14010 Set_Associated_Node (N, Original_Node (N2));
14011 Reset_Entity (N);
14012
14013 else
14014 null;
14015 end if;
14016 end if;
14017
14018 elsif Nkind (N) in N_Entity then
14019 null;
14020
14021 else
14022 declare
14023 Qual : Node_Id := Empty;
14024 Typ : Entity_Id := Empty;
14025 Nam : Node_Id;
14026
14027 use Atree.Unchecked_Access;
14028 -- This code section is part of implementing an untyped tree
14029 -- traversal, so it needs direct access to node fields.
14030
14031 begin
14032 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14033 N2 := Get_Associated_Node (N);
14034
14035 if No (N2) then
14036 Typ := Empty;
14037 else
14038 Typ := Etype (N2);
14039
14040 -- In an instance within a generic, use the name of the
14041 -- actual and not the original generic parameter. If the
14042 -- actual is global in the current generic it must be
14043 -- preserved for its instantiation.
14044
14045 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14046 and then
14047 Present (Generic_Parent_Type (Parent (Typ)))
14048 then
14049 Typ := Base_Type (Typ);
14050 Set_Etype (N2, Typ);
14051 end if;
14052 end if;
14053
14054 if No (N2)
14055 or else No (Typ)
14056 or else not Is_Global (Typ)
14057 then
14058 Set_Associated_Node (N, Empty);
14059
14060 -- If the aggregate is an actual in a call, it has been
14061 -- resolved in the current context, to some local type.
14062 -- The enclosing call may have been disambiguated by the
14063 -- aggregate, and this disambiguation might fail at
14064 -- instantiation time because the type to which the
14065 -- aggregate did resolve is not preserved. In order to
14066 -- preserve some of this information, we wrap the
14067 -- aggregate in a qualified expression, using the id of
14068 -- its type. For further disambiguation we qualify the
14069 -- type name with its scope (if visible) because both
14070 -- id's will have corresponding entities in an instance.
14071 -- This resolves most of the problems with missing type
14072 -- information on aggregates in instances.
14073
14074 if Nkind (N2) = Nkind (N)
14075 and then Nkind (Parent (N2)) in N_Subprogram_Call
14076 and then Comes_From_Source (Typ)
14077 then
14078 if Is_Immediately_Visible (Scope (Typ)) then
14079 Nam := Make_Selected_Component (Loc,
14080 Prefix =>
14081 Make_Identifier (Loc, Chars (Scope (Typ))),
14082 Selector_Name =>
14083 Make_Identifier (Loc, Chars (Typ)));
14084 else
14085 Nam := Make_Identifier (Loc, Chars (Typ));
14086 end if;
14087
14088 Qual :=
14089 Make_Qualified_Expression (Loc,
14090 Subtype_Mark => Nam,
14091 Expression => Relocate_Node (N));
14092 end if;
14093 end if;
14094
14095 Save_Global_Descendant (Field1 (N));
14096 Save_Global_Descendant (Field2 (N));
14097 Save_Global_Descendant (Field3 (N));
14098 Save_Global_Descendant (Field5 (N));
14099
14100 if Present (Qual) then
14101 Rewrite (N, Qual);
14102 end if;
14103
14104 -- All other cases than aggregates
14105
14106 else
14107 Save_Global_Descendant (Field1 (N));
14108 Save_Global_Descendant (Field2 (N));
14109 Save_Global_Descendant (Field3 (N));
14110 Save_Global_Descendant (Field4 (N));
14111 Save_Global_Descendant (Field5 (N));
14112 end if;
14113 end;
14114 end if;
14115
14116 -- If a node has aspects, references within their expressions must
14117 -- be saved separately, given they are not directly in the tree.
14118
14119 if Has_Aspects (N) then
14120 declare
14121 Aspect : Node_Id;
14122
14123 begin
14124 Aspect := First (Aspect_Specifications (N));
14125 while Present (Aspect) loop
14126 if Present (Expression (Aspect)) then
14127 Save_Global_References (Expression (Aspect));
14128 end if;
14129
14130 Next (Aspect);
14131 end loop;
14132 end;
14133 end if;
14134 end Save_References;
14135
14136 -- Start of processing for Save_Global_References
14137
14138 begin
14139 Gen_Scope := Current_Scope;
14140
14141 -- If the generic unit is a child unit, references to entities in the
14142 -- parent are treated as local, because they will be resolved anew in
14143 -- the context of the instance of the parent.
14144
14145 while Is_Child_Unit (Gen_Scope)
14146 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14147 loop
14148 Gen_Scope := Scope (Gen_Scope);
14149 end loop;
14150
14151 Save_References (N);
14152 end Save_Global_References;
14153
14154 --------------------------------------
14155 -- Set_Copied_Sloc_For_Inlined_Body --
14156 --------------------------------------
14157
14158 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14159 begin
14160 Create_Instantiation_Source (N, E, True, S_Adjustment);
14161 end Set_Copied_Sloc_For_Inlined_Body;
14162
14163 ---------------------
14164 -- Set_Instance_Of --
14165 ---------------------
14166
14167 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14168 begin
14169 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14170 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14171 Generic_Renamings.Increment_Last;
14172 end Set_Instance_Of;
14173
14174 --------------------
14175 -- Set_Next_Assoc --
14176 --------------------
14177
14178 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14179 begin
14180 Generic_Renamings.Table (E).Next_In_HTable := Next;
14181 end Set_Next_Assoc;
14182
14183 -------------------
14184 -- Start_Generic --
14185 -------------------
14186
14187 procedure Start_Generic is
14188 begin
14189 -- ??? More things could be factored out in this routine.
14190 -- Should probably be done at a later stage.
14191
14192 Generic_Flags.Append (Inside_A_Generic);
14193 Inside_A_Generic := True;
14194
14195 Expander_Mode_Save_And_Set (False);
14196 end Start_Generic;
14197
14198 ----------------------
14199 -- Set_Instance_Env --
14200 ----------------------
14201
14202 procedure Set_Instance_Env
14203 (Gen_Unit : Entity_Id;
14204 Act_Unit : Entity_Id)
14205 is
14206 Assertion_Status : constant Boolean := Assertions_Enabled;
14207 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14208 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14209
14210 begin
14211 -- Regardless of the current mode, predefined units are analyzed in the
14212 -- most current Ada mode, and earlier version Ada checks do not apply
14213 -- to predefined units. Nothing needs to be done for non-internal units.
14214 -- These are always analyzed in the current mode.
14215
14216 if Is_Internal_File_Name
14217 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14218 Renamings_Included => True)
14219 then
14220 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14221
14222 -- In Ada2012 we may want to enable assertions in an instance of a
14223 -- predefined unit, in which case we need to preserve the current
14224 -- setting for the Assertions_Enabled flag. This will become more
14225 -- critical when pre/postconditions are added to predefined units,
14226 -- as is already the case for some numeric libraries.
14227
14228 if Ada_Version >= Ada_2012 then
14229 Assertions_Enabled := Assertion_Status;
14230 end if;
14231
14232 -- SPARK_Mode for an instance is the one applicable at the point of
14233 -- instantiation.
14234
14235 SPARK_Mode := Save_SPARK_Mode;
14236 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14237 end if;
14238
14239 Current_Instantiated_Parent :=
14240 (Gen_Id => Gen_Unit,
14241 Act_Id => Act_Unit,
14242 Next_In_HTable => Assoc_Null);
14243 end Set_Instance_Env;
14244
14245 -----------------
14246 -- Switch_View --
14247 -----------------
14248
14249 procedure Switch_View (T : Entity_Id) is
14250 BT : constant Entity_Id := Base_Type (T);
14251 Priv_Elmt : Elmt_Id := No_Elmt;
14252 Priv_Sub : Entity_Id;
14253
14254 begin
14255 -- T may be private but its base type may have been exchanged through
14256 -- some other occurrence, in which case there is nothing to switch
14257 -- besides T itself. Note that a private dependent subtype of a private
14258 -- type might not have been switched even if the base type has been,
14259 -- because of the last branch of Check_Private_View (see comment there).
14260
14261 if not Is_Private_Type (BT) then
14262 Prepend_Elmt (Full_View (T), Exchanged_Views);
14263 Exchange_Declarations (T);
14264 return;
14265 end if;
14266
14267 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14268
14269 if Present (Full_View (BT)) then
14270 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14271 Exchange_Declarations (BT);
14272 end if;
14273
14274 while Present (Priv_Elmt) loop
14275 Priv_Sub := (Node (Priv_Elmt));
14276
14277 -- We avoid flipping the subtype if the Etype of its full view is
14278 -- private because this would result in a malformed subtype. This
14279 -- occurs when the Etype of the subtype full view is the full view of
14280 -- the base type (and since the base types were just switched, the
14281 -- subtype is pointing to the wrong view). This is currently the case
14282 -- for tagged record types, access types (maybe more?) and needs to
14283 -- be resolved. ???
14284
14285 if Present (Full_View (Priv_Sub))
14286 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14287 then
14288 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14289 Exchange_Declarations (Priv_Sub);
14290 end if;
14291
14292 Next_Elmt (Priv_Elmt);
14293 end loop;
14294 end Switch_View;
14295
14296 -----------------
14297 -- True_Parent --
14298 -----------------
14299
14300 function True_Parent (N : Node_Id) return Node_Id is
14301 begin
14302 if Nkind (Parent (N)) = N_Subunit then
14303 return Parent (Corresponding_Stub (Parent (N)));
14304 else
14305 return Parent (N);
14306 end if;
14307 end True_Parent;
14308
14309 -----------------------------
14310 -- Valid_Default_Attribute --
14311 -----------------------------
14312
14313 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
14314 Attr_Id : constant Attribute_Id :=
14315 Get_Attribute_Id (Attribute_Name (Def));
14316 T : constant Entity_Id := Entity (Prefix (Def));
14317 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
14318 F : Entity_Id;
14319 Num_F : Int;
14320 OK : Boolean;
14321
14322 begin
14323 if No (T)
14324 or else T = Any_Id
14325 then
14326 return;
14327 end if;
14328
14329 Num_F := 0;
14330 F := First_Formal (Nam);
14331 while Present (F) loop
14332 Num_F := Num_F + 1;
14333 Next_Formal (F);
14334 end loop;
14335
14336 case Attr_Id is
14337 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
14338 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
14339 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
14340 Attribute_Unbiased_Rounding =>
14341 OK := Is_Fun
14342 and then Num_F = 1
14343 and then Is_Floating_Point_Type (T);
14344
14345 when Attribute_Image | Attribute_Pred | Attribute_Succ |
14346 Attribute_Value | Attribute_Wide_Image |
14347 Attribute_Wide_Value =>
14348 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
14349
14350 when Attribute_Max | Attribute_Min =>
14351 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
14352
14353 when Attribute_Input =>
14354 OK := (Is_Fun and then Num_F = 1);
14355
14356 when Attribute_Output | Attribute_Read | Attribute_Write =>
14357 OK := (not Is_Fun and then Num_F = 2);
14358
14359 when others =>
14360 OK := False;
14361 end case;
14362
14363 if not OK then
14364 Error_Msg_N ("attribute reference has wrong profile for subprogram",
14365 Def);
14366 end if;
14367 end Valid_Default_Attribute;
14368
14369 end Sem_Ch12;