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