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