[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 Exp_Util; use Exp_Util;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Itypes; use Itypes;
38 with Lib; use Lib;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Nlists; use Nlists;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Rident; use Rident;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinfo.CN; use Sinfo.CN;
70 with Sinput; use Sinput;
71 with Sinput.L; use Sinput.L;
72 with Snames; use Snames;
73 with Stringt; use Stringt;
74 with Uname; use Uname;
75 with Table;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
80
81 with GNAT.HTable;
82
83 package body Sem_Ch12 is
84
85 ----------------------------------------------------------
86 -- Implementation of Generic Analysis and Instantiation --
87 ----------------------------------------------------------
88
89 -- GNAT implements generics by macro expansion. No attempt is made to share
90 -- generic instantiations (for now). Analysis of a generic definition does
91 -- not perform any expansion action, but the expander must be called on the
92 -- tree for each instantiation, because the expansion may of course depend
93 -- on the generic actuals. All of this is best achieved as follows:
94 --
95 -- a) Semantic analysis of a generic unit is performed on a copy of the
96 -- tree for the generic unit. All tree modifications that follow analysis
97 -- do not affect the original tree. Links are kept between the original
98 -- tree and the copy, in order to recognize non-local references within
99 -- the generic, and propagate them to each instance (recall that name
100 -- resolution is done on the generic declaration: generics are not really
101 -- macros). This is summarized in the following diagram:
102
103 -- .-----------. .----------.
104 -- | semantic |<--------------| generic |
105 -- | copy | | unit |
106 -- | |==============>| |
107 -- |___________| global |__________|
108 -- references | | |
109 -- | | |
110 -- .-----|--|.
111 -- | .-----|---.
112 -- | | .----------.
113 -- | | | generic |
114 -- |__| | |
115 -- |__| instance |
116 -- |__________|
117
118 -- b) Each instantiation copies the original tree, and inserts into it a
119 -- series of declarations that describe the mapping between generic formals
120 -- and actuals. For example, a generic In OUT parameter is an object
121 -- renaming of the corresponding actual, etc. Generic IN parameters are
122 -- constant declarations.
123
124 -- c) In order to give the right visibility for these renamings, we use
125 -- a different scheme for package and subprogram instantiations. For
126 -- packages, the list of renamings is inserted into the package
127 -- specification, before the visible declarations of the package. The
128 -- renamings are analyzed before any of the text of the instance, and are
129 -- thus visible at the right place. Furthermore, outside of the instance,
130 -- the generic parameters are visible and denote their corresponding
131 -- actuals.
132
133 -- For subprograms, we create a container package to hold the renamings
134 -- and the subprogram instance itself. Analysis of the package makes the
135 -- renaming declarations visible to the subprogram. After analyzing the
136 -- package, the defining entity for the subprogram is touched-up so that
137 -- it appears declared in the current scope, and not inside the container
138 -- package.
139
140 -- If the instantiation is a compilation unit, the container package is
141 -- given the same name as the subprogram instance. This ensures that
142 -- the elaboration procedure called by the binder, using the compilation
143 -- unit name, calls in fact the elaboration procedure for the package.
144
145 -- Not surprisingly, private types complicate this approach. By saving in
146 -- the original generic object the non-local references, we guarantee that
147 -- the proper entities are referenced at the point of instantiation.
148 -- However, for private types, this by itself does not insure that the
149 -- proper VIEW of the entity is used (the full type may be visible at the
150 -- point of generic definition, but not at instantiation, or vice-versa).
151 -- In order to reference the proper view, we special-case any reference
152 -- to private types in the generic object, by saving both views, one in
153 -- the generic and one in the semantic copy. At time of instantiation, we
154 -- check whether the two views are consistent, and exchange declarations if
155 -- necessary, in order to restore the correct visibility. Similarly, if
156 -- the instance view is private when the generic view was not, we perform
157 -- the exchange. After completing the instantiation, we restore the
158 -- current visibility. The flag Has_Private_View marks identifiers in the
159 -- the generic unit that require checking.
160
161 -- Visibility within nested generic units requires special handling.
162 -- Consider the following scheme:
163
164 -- type Global is ... -- outside of generic unit.
165 -- generic ...
166 -- package Outer is
167 -- ...
168 -- type Semi_Global is ... -- global to inner.
169
170 -- generic ... -- 1
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
172
173 -- procedure in2 is new inner (...); -- 4
174 -- end Outer;
175
176 -- package New_Outer is new Outer (...); -- 2
177 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
178
179 -- The semantic analysis of Outer captures all occurrences of Global.
180 -- The semantic analysis of Inner (at 1) captures both occurrences of
181 -- Global and Semi_Global.
182
183 -- At point 2 (instantiation of Outer), we also produce a generic copy
184 -- of Inner, even though Inner is, at that point, not being instantiated.
185 -- (This is just part of the semantic analysis of New_Outer).
186
187 -- Critically, references to Global within Inner must be preserved, while
188 -- references to Semi_Global should not preserved, because they must now
189 -- resolve to an entity within New_Outer. To distinguish between these, we
190 -- use a global variable, Current_Instantiated_Parent, which is set when
191 -- performing a generic copy during instantiation (at 2). This variable is
192 -- used when performing a generic copy that is not an instantiation, but
193 -- that is nested within one, as the occurrence of 1 within 2. The analysis
194 -- of a nested generic only preserves references that are global to the
195 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
196 -- determine whether a reference is external to the given parent.
197
198 -- The instantiation at point 3 requires no special treatment. The method
199 -- works as well for further nestings of generic units, but of course the
200 -- variable Current_Instantiated_Parent must be stacked because nested
201 -- instantiations can occur, e.g. the occurrence of 4 within 2.
202
203 -- The instantiation of package and subprogram bodies is handled in a
204 -- similar manner, except that it is delayed until after semantic
205 -- analysis is complete. In this fashion complex cross-dependencies
206 -- between several package declarations and bodies containing generics
207 -- can be compiled which otherwise would diagnose spurious circularities.
208
209 -- For example, it is possible to compile two packages A and B that
210 -- have the following structure:
211
212 -- package A is package B is
213 -- generic ... generic ...
214 -- package G_A is package G_B is
215
216 -- with B; with A;
217 -- package body A is package body B is
218 -- package N_B is new G_B (..) package N_A is new G_A (..)
219
220 -- The table Pending_Instantiations in package Inline is used to keep
221 -- track of body instantiations that are delayed in this manner. Inline
222 -- handles the actual calls to do the body instantiations. This activity
223 -- is part of Inline, since the processing occurs at the same point, and
224 -- for essentially the same reason, as the handling of inlined routines.
225
226 ----------------------------------------------
227 -- Detection of Instantiation Circularities --
228 ----------------------------------------------
229
230 -- If we have a chain of instantiations that is circular, this is static
231 -- error which must be detected at compile time. The detection of these
232 -- circularities is carried out at the point that we insert a generic
233 -- instance spec or body. If there is a circularity, then the analysis of
234 -- the offending spec or body will eventually result in trying to load the
235 -- same unit again, and we detect this problem as we analyze the package
236 -- instantiation for the second time.
237
238 -- At least in some cases after we have detected the circularity, we get
239 -- into trouble if we try to keep going. The following flag is set if a
240 -- circularity is detected, and used to abandon compilation after the
241 -- messages have been posted.
242
243 Circularity_Detected : Boolean := False;
244 -- This should really be reset on encountering a new main unit, but in
245 -- practice we are not using multiple main units so it is not critical.
246
247 --------------------------------------------------
248 -- Formal packages and partial parameterization --
249 --------------------------------------------------
250
251 -- When compiling a generic, a formal package is a local instantiation. If
252 -- declared with a box, its generic formals are visible in the enclosing
253 -- generic. If declared with a partial list of actuals, those actuals that
254 -- are defaulted (covered by an Others clause, or given an explicit box
255 -- initialization) are also visible in the enclosing generic, while those
256 -- that have a corresponding actual are not.
257
258 -- In our source model of instantiation, the same visibility must be
259 -- present in the spec and body of an instance: the names of the formals
260 -- that are defaulted must be made visible within the instance, and made
261 -- invisible (hidden) after the instantiation is complete, so that they
262 -- are not accessible outside of the instance.
263
264 -- In a generic, a formal package is treated like a special instantiation.
265 -- Our Ada 95 compiler handled formals with and without box in different
266 -- ways. With partial parameterization, we use a single model for both.
267 -- We create a package declaration that consists of the specification of
268 -- the generic package, and a set of declarations that map the actuals
269 -- into local renamings, just as we do for bona fide instantiations. For
270 -- defaulted parameters and formals with a box, we copy directly the
271 -- declarations of the formal into this local package. The result is a
272 -- a package whose visible declarations may include generic formals. This
273 -- package is only used for type checking and visibility analysis, and
274 -- never reaches the back-end, so it can freely violate the placement
275 -- rules for generic formal declarations.
276
277 -- The list of declarations (renamings and copies of formals) is built
278 -- by Analyze_Associations, just as for regular instantiations.
279
280 -- At the point of instantiation, conformance checking must be applied only
281 -- to those parameters that were specified in the formal. We perform this
282 -- checking by creating another internal instantiation, this one including
283 -- only the renamings and the formals (the rest of the package spec is not
284 -- relevant to conformance checking). We can then traverse two lists: the
285 -- list of actuals in the instance that corresponds to the formal package,
286 -- and the list of actuals produced for this bogus instantiation. We apply
287 -- the conformance rules to those actuals that are not defaulted (i.e.
288 -- which still appear as generic formals.
289
290 -- When we compile an instance body we must make the right parameters
291 -- visible again. The predicate Is_Generic_Formal indicates which of the
292 -- formals should have its Is_Hidden flag reset.
293
294 -----------------------
295 -- Local subprograms --
296 -----------------------
297
298 procedure Abandon_Instantiation (N : Node_Id);
299 pragma No_Return (Abandon_Instantiation);
300 -- Posts an error message "instantiation abandoned" at the indicated node
301 -- and then raises the exception Instantiation_Error to do it.
302
303 procedure Analyze_Formal_Array_Type
304 (T : in out Entity_Id;
305 Def : Node_Id);
306 -- A formal array type is treated like an array type declaration, and
307 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
308 -- in-out, because in the case of an anonymous type the entity is
309 -- actually created in the procedure.
310
311 -- The following procedures treat other kinds of formal parameters
312
313 procedure Analyze_Formal_Derived_Interface_Type
314 (N : Node_Id;
315 T : Entity_Id;
316 Def : Node_Id);
317
318 procedure Analyze_Formal_Derived_Type
319 (N : Node_Id;
320 T : Entity_Id;
321 Def : Node_Id);
322
323 procedure Analyze_Formal_Interface_Type
324 (N : Node_Id;
325 T : Entity_Id;
326 Def : Node_Id);
327
328 -- The following subprograms create abbreviated declarations for formal
329 -- scalar types. We introduce an anonymous base of the proper class for
330 -- each of them, and define the formals as constrained first subtypes of
331 -- their bases. The bounds are expressions that are non-static in the
332 -- generic.
333
334 procedure Analyze_Formal_Decimal_Fixed_Point_Type
335 (T : Entity_Id; Def : Node_Id);
336 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
337 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
339 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
340 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
341 (T : Entity_Id; Def : Node_Id);
342
343 procedure Analyze_Formal_Private_Type
344 (N : Node_Id;
345 T : Entity_Id;
346 Def : Node_Id);
347 -- Creates a new private type, which does not require completion
348
349 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
350 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
351
352 procedure Analyze_Generic_Formal_Part (N : Node_Id);
353 -- Analyze generic formal part
354
355 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
356 -- Create a new access type with the given designated type
357
358 function Analyze_Associations
359 (I_Node : Node_Id;
360 Formals : List_Id;
361 F_Copy : List_Id) return List_Id;
362 -- At instantiation time, build the list of associations between formals
363 -- and actuals. Each association becomes a renaming declaration for the
364 -- formal entity. F_Copy is the analyzed list of formals in the generic
365 -- copy. It is used to apply legality checks to the actuals. I_Node is the
366 -- instantiation node itself.
367
368 procedure Analyze_Subprogram_Instantiation
369 (N : Node_Id;
370 K : Entity_Kind);
371
372 procedure Build_Instance_Compilation_Unit_Nodes
373 (N : Node_Id;
374 Act_Body : Node_Id;
375 Act_Decl : Node_Id);
376 -- This procedure is used in the case where the generic instance of a
377 -- subprogram body or package body is a library unit. In this case, the
378 -- original library unit node for the generic instantiation must be
379 -- replaced by the resulting generic body, and a link made to a new
380 -- compilation unit node for the generic declaration. The argument N is
381 -- the original generic instantiation. Act_Body and Act_Decl are the body
382 -- and declaration of the instance (either package body and declaration
383 -- nodes or subprogram body and declaration nodes depending on the case).
384 -- On return, the node N has been rewritten with the actual body.
385
386 procedure Check_Access_Definition (N : Node_Id);
387 -- Subsidiary routine to null exclusion processing. Perform an assertion
388 -- check on Ada version and the presence of an access definition in N.
389
390 procedure Check_Formal_Packages (P_Id : Entity_Id);
391 -- Apply the following to all formal packages in generic associations
392
393 procedure Check_Formal_Package_Instance
394 (Formal_Pack : Entity_Id;
395 Actual_Pack : Entity_Id);
396 -- Verify that the actuals of the actual instance match the actuals of
397 -- the template for a formal package that is not declared with a box.
398
399 procedure Check_Forward_Instantiation (Decl : Node_Id);
400 -- If the generic is a local entity and the corresponding body has not
401 -- been seen yet, flag enclosing packages to indicate that it will be
402 -- elaborated after the generic body. Subprograms declared in the same
403 -- package cannot be inlined by the front-end because front-end inlining
404 -- requires a strict linear order of elaboration.
405
406 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
407 -- Check if some association between formals and actuals requires to make
408 -- visible primitives of a tagged type, and make those primitives visible.
409 -- Return the list of primitives whose visibility is modified (to restore
410 -- their visibility later through Restore_Hidden_Primitives). If no
411 -- candidate is found then return No_Elist.
412
413 procedure Check_Hidden_Child_Unit
414 (N : Node_Id;
415 Gen_Unit : Entity_Id;
416 Act_Decl_Id : Entity_Id);
417 -- If the generic unit is an implicit child instance within a parent
418 -- instance, we need to make an explicit test that it is not hidden by
419 -- a child instance of the same name and parent.
420
421 procedure Check_Generic_Actuals
422 (Instance : Entity_Id;
423 Is_Formal_Box : Boolean);
424 -- Similar to previous one. Check the actuals in the instantiation,
425 -- whose views can change between the point of instantiation and the point
426 -- of instantiation of the body. In addition, mark the generic renamings
427 -- as generic actuals, so that they are not compatible with other actuals.
428 -- Recurse on an actual that is a formal package whose declaration has
429 -- a box.
430
431 function Contains_Instance_Of
432 (Inner : Entity_Id;
433 Outer : Entity_Id;
434 N : Node_Id) return Boolean;
435 -- Inner is instantiated within the generic Outer. Check whether Inner
436 -- directly or indirectly contains an instance of Outer or of one of its
437 -- parents, in the case of a subunit. Each generic unit holds a list of
438 -- the entities instantiated within (at any depth). This procedure
439 -- determines whether the set of such lists contains a cycle, i.e. an
440 -- illegal circular instantiation.
441
442 function Denotes_Formal_Package
443 (Pack : Entity_Id;
444 On_Exit : Boolean := False;
445 Instance : Entity_Id := Empty) return Boolean;
446 -- Returns True if E is a formal package of an enclosing generic, or
447 -- the actual for such a formal in an enclosing instantiation. If such
448 -- a package is used as a formal in an nested generic, or as an actual
449 -- in a nested instantiation, the visibility of ITS formals should not
450 -- be modified. When called from within Restore_Private_Views, the flag
451 -- On_Exit is true, to indicate that the search for a possible enclosing
452 -- instance should ignore the current one. In that case Instance denotes
453 -- the declaration for which this is an actual. This declaration may be
454 -- an instantiation in the source, or the internal instantiation that
455 -- corresponds to the actual for a formal package.
456
457 function Earlier (N1, N2 : Node_Id) return Boolean;
458 -- Yields True if N1 and N2 appear in the same compilation unit,
459 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
460 -- traversal of the tree for the unit. Used to determine the placement
461 -- of freeze nodes for instance bodies that may depend on other instances.
462
463 function Find_Actual_Type
464 (Typ : Entity_Id;
465 Gen_Type : Entity_Id) return Entity_Id;
466 -- When validating the actual types of a child instance, check whether
467 -- the formal is a formal type of the parent unit, and retrieve the current
468 -- actual for it. Typ is the entity in the analyzed formal type declaration
469 -- (component or index type of an array type, or designated type of an
470 -- access formal) and Gen_Type is the enclosing analyzed formal array
471 -- or access type. The desired actual may be a formal of a parent, or may
472 -- be declared in a formal package of a parent. In both cases it is a
473 -- generic actual type because it appears within a visible instance.
474 -- Finally, it may be declared in a parent unit without being a formal
475 -- of that unit, in which case it must be retrieved by visibility.
476 -- Ambiguities may still arise if two homonyms are declared in two formal
477 -- packages, and the prefix of the formal type may be needed to resolve
478 -- the ambiguity in the instance ???
479
480 function In_Same_Declarative_Part
481 (F_Node : Node_Id;
482 Inst : Node_Id) return Boolean;
483 -- True if the instantiation Inst and the given freeze_node F_Node appear
484 -- within the same declarative part, ignoring subunits, but with no inter-
485 -- vening subprograms or concurrent units. Used to find the proper plave
486 -- for the freeze node of an instance, when the generic is declared in a
487 -- previous instance. If predicate is true, the freeze node of the instance
488 -- can be placed after the freeze node of the previous instance, Otherwise
489 -- it has to be placed at the end of the current declarative part.
490
491 function In_Main_Context (E : Entity_Id) return Boolean;
492 -- Check whether an instantiation is in the context of the main unit.
493 -- Used to determine whether its body should be elaborated to allow
494 -- front-end inlining.
495
496 procedure Set_Instance_Env
497 (Gen_Unit : Entity_Id;
498 Act_Unit : Entity_Id);
499 -- Save current instance on saved environment, to be used to determine
500 -- the global status of entities in nested instances. Part of Save_Env.
501 -- called after verifying that the generic unit is legal for the instance,
502 -- The procedure also examines whether the generic unit is a predefined
503 -- unit, in order to set configuration switches accordingly. As a result
504 -- the procedure must be called after analyzing and freezing the actuals.
505
506 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
507 -- Associate analyzed generic parameter with corresponding
508 -- instance. Used for semantic checks at instantiation time.
509
510 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
511 -- Traverse the Exchanged_Views list to see if a type was private
512 -- and has already been flipped during this phase of instantiation.
513
514 procedure Hide_Current_Scope;
515 -- When instantiating a generic child unit, the parent context must be
516 -- present, but the instance and all entities that may be generated
517 -- must be inserted in the current scope. We leave the current scope
518 -- on the stack, but make its entities invisible to avoid visibility
519 -- problems. This is reversed at the end of the instantiation. This is
520 -- not done for the instantiation of the bodies, which only require the
521 -- instances of the generic parents to be in scope.
522
523 procedure Install_Body
524 (Act_Body : Node_Id;
525 N : Node_Id;
526 Gen_Body : Node_Id;
527 Gen_Decl : Node_Id);
528 -- If the instantiation happens textually before the body of the generic,
529 -- the instantiation of the body must be analyzed after the generic body,
530 -- and not at the point of instantiation. Such early instantiations can
531 -- happen if the generic and the instance appear in a package declaration
532 -- because the generic body can only appear in the corresponding package
533 -- body. Early instantiations can also appear if generic, instance and
534 -- body are all in the declarative part of a subprogram or entry. Entities
535 -- of packages that are early instantiations are delayed, and their freeze
536 -- node appears after the generic body.
537
538 procedure Insert_Freeze_Node_For_Instance
539 (N : Node_Id;
540 F_Node : Node_Id);
541 -- N denotes a package or a subprogram instantiation and F_Node is the
542 -- associated freeze node. Insert the freeze node before the first source
543 -- body which follows immediately after N. If no such body is found, the
544 -- freeze node is inserted at the end of the declarative region which
545 -- contains N.
546
547 procedure Freeze_Subprogram_Body
548 (Inst_Node : Node_Id;
549 Gen_Body : Node_Id;
550 Pack_Id : Entity_Id);
551 -- The generic body may appear textually after the instance, including
552 -- in the proper body of a stub, or within a different package instance.
553 -- Given that the instance can only be elaborated after the generic, we
554 -- place freeze_nodes for the instance and/or for packages that may enclose
555 -- the instance and the generic, so that the back-end can establish the
556 -- proper order of elaboration.
557
558 procedure Init_Env;
559 -- Establish environment for subsequent instantiation. Separated from
560 -- Save_Env because data-structures for visibility handling must be
561 -- initialized before call to Check_Generic_Child_Unit.
562
563 procedure Install_Formal_Packages (Par : Entity_Id);
564 -- Install the visible part of any formal of the parent that is a formal
565 -- package. Note that for the case of a formal package with a box, this
566 -- includes the formal part of the formal package (12.7(10/2)).
567
568 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
569 -- When compiling an instance of a child unit the parent (which is
570 -- itself an instance) is an enclosing scope that must be made
571 -- immediately visible. This procedure is also used to install the non-
572 -- generic parent of a generic child unit when compiling its body, so
573 -- that full views of types in the parent are made visible.
574
575 procedure Remove_Parent (In_Body : Boolean := False);
576 -- Reverse effect after instantiation of child is complete
577
578 procedure Install_Hidden_Primitives
579 (Prims_List : in out Elist_Id;
580 Gen_T : Entity_Id;
581 Act_T : Entity_Id);
582 -- Remove suffix 'P' from hidden primitives of Act_T to match the
583 -- visibility of primitives of Gen_T. The list of primitives to which
584 -- the suffix is removed is added to Prims_List to restore them later.
585
586 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
587 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
588 -- set to No_Elist.
589
590 procedure Inline_Instance_Body
591 (N : Node_Id;
592 Gen_Unit : Entity_Id;
593 Act_Decl : Node_Id);
594 -- If front-end inlining is requested, instantiate the package body,
595 -- and preserve the visibility of its compilation unit, to insure
596 -- that successive instantiations succeed.
597
598 -- The functions Instantiate_XXX perform various legality checks and build
599 -- the declarations for instantiated generic parameters. In all of these
600 -- Formal is the entity in the generic unit, Actual is the entity of
601 -- expression in the generic associations, and Analyzed_Formal is the
602 -- formal in the generic copy, which contains the semantic information to
603 -- be used to validate the actual.
604
605 function Instantiate_Object
606 (Formal : Node_Id;
607 Actual : Node_Id;
608 Analyzed_Formal : Node_Id) return List_Id;
609
610 function Instantiate_Type
611 (Formal : Node_Id;
612 Actual : Node_Id;
613 Analyzed_Formal : Node_Id;
614 Actual_Decls : List_Id) return List_Id;
615
616 function Instantiate_Formal_Subprogram
617 (Formal : Node_Id;
618 Actual : Node_Id;
619 Analyzed_Formal : Node_Id) return Node_Id;
620
621 function Instantiate_Formal_Package
622 (Formal : Node_Id;
623 Actual : Node_Id;
624 Analyzed_Formal : Node_Id) return List_Id;
625 -- If the formal package is declared with a box, special visibility rules
626 -- apply to its formals: they are in the visible part of the package. This
627 -- is true in the declarative region of the formal package, that is to say
628 -- in the enclosing generic or instantiation. For an instantiation, the
629 -- parameters of the formal package are made visible in an explicit step.
630 -- Furthermore, if the actual has a visible USE clause, these formals must
631 -- be made potentially use-visible as well. On exit from the enclosing
632 -- instantiation, the reverse must be done.
633
634 -- For a formal package declared without a box, there are conformance rules
635 -- that apply to the actuals in the generic declaration and the actuals of
636 -- the actual package in the enclosing instantiation. The simplest way to
637 -- apply these rules is to repeat the instantiation of the formal package
638 -- in the context of the enclosing instance, and compare the generic
639 -- associations of this instantiation with those of the actual package.
640 -- This internal instantiation only needs to contain the renamings of the
641 -- formals: the visible and private declarations themselves need not be
642 -- created.
643
644 -- In Ada 2005, the formal package may be only partially parameterized.
645 -- In that case the visibility step must make visible those actuals whose
646 -- corresponding formals were given with a box. A final complication
647 -- involves inherited operations from formal derived types, which must
648 -- be visible if the type is.
649
650 function Is_In_Main_Unit (N : Node_Id) return Boolean;
651 -- Test if given node is in the main unit
652
653 procedure Load_Parent_Of_Generic
654 (N : Node_Id;
655 Spec : Node_Id;
656 Body_Optional : Boolean := False);
657 -- If the generic appears in a separate non-generic library unit, load the
658 -- corresponding body to retrieve the body of the generic. N is the node
659 -- for the generic instantiation, Spec is the generic package declaration.
660 --
661 -- Body_Optional is a flag that indicates that the body is being loaded to
662 -- ensure that temporaries are generated consistently when there are other
663 -- instances in the current declarative part that precede the one being
664 -- loaded. In that case a missing body is acceptable.
665
666 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
667 -- Add the context clause of the unit containing a generic unit to a
668 -- compilation unit that is, or contains, an instantiation.
669
670 function Get_Associated_Node (N : Node_Id) return Node_Id;
671 -- In order to propagate semantic information back from the analyzed copy
672 -- to the original generic, we maintain links between selected nodes in the
673 -- generic and their corresponding copies. At the end of generic analysis,
674 -- the routine Save_Global_References traverses the generic tree, examines
675 -- the semantic information, and preserves the links to those nodes that
676 -- contain global information. At instantiation, the information from the
677 -- associated node is placed on the new copy, so that name resolution is
678 -- not repeated.
679 --
680 -- Three kinds of source nodes have associated nodes:
681 --
682 -- a) those that can reference (denote) entities, that is identifiers,
683 -- character literals, expanded_names, operator symbols, operators,
684 -- and attribute reference nodes. These nodes have an Entity field
685 -- and are the set of nodes that are in N_Has_Entity.
686 --
687 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
688 --
689 -- c) selected components (N_Selected_Component)
690 --
691 -- For the first class, the associated node preserves the entity if it is
692 -- global. If the generic contains nested instantiations, the associated
693 -- node itself has been recopied, and a chain of them must be followed.
694 --
695 -- For aggregates, the associated node allows retrieval of the type, which
696 -- may otherwise not appear in the generic. The view of this type may be
697 -- different between generic and instantiation, and the full view can be
698 -- installed before the instantiation is analyzed. For aggregates of type
699 -- extensions, the same view exchange may have to be performed for some of
700 -- the ancestor types, if their view is private at the point of
701 -- instantiation.
702 --
703 -- Nodes that are selected components in the parse tree may be rewritten
704 -- as expanded names after resolution, and must be treated as potential
705 -- entity holders, which is why they also have an Associated_Node.
706 --
707 -- Nodes that do not come from source, such as freeze nodes, do not appear
708 -- in the generic tree, and need not have an associated node.
709 --
710 -- The associated node is stored in the Associated_Node field. Note that
711 -- this field overlaps Entity, which is fine, because the whole point is
712 -- that we don't need or want the normal Entity field in this situation.
713
714 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
715 -- Within the generic part, entities in the formal package are
716 -- visible. To validate subsequent type declarations, indicate
717 -- the correspondence between the entities in the analyzed formal,
718 -- and the entities in the actual package. There are three packages
719 -- involved in the instantiation of a formal package: the parent
720 -- generic P1 which appears in the generic declaration, the fake
721 -- instantiation P2 which appears in the analyzed generic, and whose
722 -- visible entities may be used in subsequent formals, and the actual
723 -- P3 in the instance. To validate subsequent formals, me indicate
724 -- that the entities in P2 are mapped into those of P3. The mapping of
725 -- entities has to be done recursively for nested packages.
726
727 procedure Move_Freeze_Nodes
728 (Out_Of : Entity_Id;
729 After : Node_Id;
730 L : List_Id);
731 -- Freeze nodes can be generated in the analysis of a generic unit, but
732 -- will not be seen by the back-end. It is necessary to move those nodes
733 -- to the enclosing scope if they freeze an outer entity. We place them
734 -- at the end of the enclosing generic package, which is semantically
735 -- neutral.
736
737 procedure Preanalyze_Actuals (N : Node_Id);
738 -- Analyze actuals to perform name resolution. Full resolution is done
739 -- later, when the expected types are known, but names have to be captured
740 -- before installing parents of generics, that are not visible for the
741 -- actuals themselves.
742
743 function True_Parent (N : Node_Id) return Node_Id;
744 -- For a subunit, return parent of corresponding stub, else return
745 -- parent of node.
746
747 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
748 -- Verify that an attribute that appears as the default for a formal
749 -- subprogram is a function or procedure with the correct profile.
750
751 -------------------------------------------
752 -- Data Structures for Generic Renamings --
753 -------------------------------------------
754
755 -- The map Generic_Renamings associates generic entities with their
756 -- corresponding actuals. Currently used to validate type instances. It
757 -- will eventually be used for all generic parameters to eliminate the
758 -- need for overload resolution in the instance.
759
760 type Assoc_Ptr is new Int;
761
762 Assoc_Null : constant Assoc_Ptr := -1;
763
764 type Assoc is record
765 Gen_Id : Entity_Id;
766 Act_Id : Entity_Id;
767 Next_In_HTable : Assoc_Ptr;
768 end record;
769
770 package Generic_Renamings is new Table.Table
771 (Table_Component_Type => Assoc,
772 Table_Index_Type => Assoc_Ptr,
773 Table_Low_Bound => 0,
774 Table_Initial => 10,
775 Table_Increment => 100,
776 Table_Name => "Generic_Renamings");
777
778 -- Variable to hold enclosing instantiation. When the environment is
779 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
780
781 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
782
783 -- Hash table for associations
784
785 HTable_Size : constant := 37;
786 type HTable_Range is range 0 .. HTable_Size - 1;
787
788 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
789 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
790 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
791 function Hash (F : Entity_Id) return HTable_Range;
792
793 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
794 Header_Num => HTable_Range,
795 Element => Assoc,
796 Elmt_Ptr => Assoc_Ptr,
797 Null_Ptr => Assoc_Null,
798 Set_Next => Set_Next_Assoc,
799 Next => Next_Assoc,
800 Key => Entity_Id,
801 Get_Key => Get_Gen_Id,
802 Hash => Hash,
803 Equal => "=");
804
805 Exchanged_Views : Elist_Id;
806 -- This list holds the private views that have been exchanged during
807 -- instantiation to restore the visibility of the generic declaration.
808 -- (see comments above). After instantiation, the current visibility is
809 -- reestablished by means of a traversal of this list.
810
811 Hidden_Entities : Elist_Id;
812 -- This list holds the entities of the current scope that are removed
813 -- from immediate visibility when instantiating a child unit. Their
814 -- visibility is restored in Remove_Parent.
815
816 -- Because instantiations can be recursive, the following must be saved
817 -- on entry and restored on exit from an instantiation (spec or body).
818 -- This is done by the two procedures Save_Env and Restore_Env. For
819 -- package and subprogram instantiations (but not for the body instances)
820 -- the action of Save_Env is done in two steps: Init_Env is called before
821 -- Check_Generic_Child_Unit, because setting the parent instances requires
822 -- that the visibility data structures be properly initialized. Once the
823 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
824
825 Parent_Unit_Visible : Boolean := False;
826 -- Parent_Unit_Visible is used when the generic is a child unit, and
827 -- indicates whether the ultimate parent of the generic is visible in the
828 -- instantiation environment. It is used to reset the visibility of the
829 -- parent at the end of the instantiation (see Remove_Parent).
830
831 Instance_Parent_Unit : Entity_Id := Empty;
832 -- This records the ultimate parent unit of an instance of a generic
833 -- child unit and is used in conjunction with Parent_Unit_Visible to
834 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
835
836 type Instance_Env is record
837 Instantiated_Parent : Assoc;
838 Exchanged_Views : Elist_Id;
839 Hidden_Entities : Elist_Id;
840 Current_Sem_Unit : Unit_Number_Type;
841 Parent_Unit_Visible : Boolean := False;
842 Instance_Parent_Unit : Entity_Id := Empty;
843 Switches : Config_Switches_Type;
844 end record;
845
846 package Instance_Envs is new Table.Table (
847 Table_Component_Type => Instance_Env,
848 Table_Index_Type => Int,
849 Table_Low_Bound => 0,
850 Table_Initial => 32,
851 Table_Increment => 100,
852 Table_Name => "Instance_Envs");
853
854 procedure Restore_Private_Views
855 (Pack_Id : Entity_Id;
856 Is_Package : Boolean := True);
857 -- Restore the private views of external types, and unmark the generic
858 -- renamings of actuals, so that they become compatible subtypes again.
859 -- For subprograms, Pack_Id is the package constructed to hold the
860 -- renamings.
861
862 procedure Switch_View (T : Entity_Id);
863 -- Switch the partial and full views of a type and its private
864 -- dependents (i.e. its subtypes and derived types).
865
866 ------------------------------------
867 -- Structures for Error Reporting --
868 ------------------------------------
869
870 Instantiation_Node : Node_Id;
871 -- Used by subprograms that validate instantiation of formal parameters
872 -- where there might be no actual on which to place the error message.
873 -- Also used to locate the instantiation node for generic subunits.
874
875 Instantiation_Error : exception;
876 -- When there is a semantic error in the generic parameter matching,
877 -- there is no point in continuing the instantiation, because the
878 -- number of cascaded errors is unpredictable. This exception aborts
879 -- the instantiation process altogether.
880
881 S_Adjustment : Sloc_Adjustment;
882 -- Offset created for each node in an instantiation, in order to keep
883 -- track of the source position of the instantiation in each of its nodes.
884 -- A subsequent semantic error or warning on a construct of the instance
885 -- points to both places: the original generic node, and the point of
886 -- instantiation. See Sinput and Sinput.L for additional details.
887
888 ------------------------------------------------------------
889 -- Data structure for keeping track when inside a Generic --
890 ------------------------------------------------------------
891
892 -- The following table is used to save values of the Inside_A_Generic
893 -- flag (see spec of Sem) when they are saved by Start_Generic.
894
895 package Generic_Flags is new Table.Table (
896 Table_Component_Type => Boolean,
897 Table_Index_Type => Int,
898 Table_Low_Bound => 0,
899 Table_Initial => 32,
900 Table_Increment => 200,
901 Table_Name => "Generic_Flags");
902
903 ---------------------------
904 -- Abandon_Instantiation --
905 ---------------------------
906
907 procedure Abandon_Instantiation (N : Node_Id) is
908 begin
909 Error_Msg_N ("\instantiation abandoned!", N);
910 raise Instantiation_Error;
911 end Abandon_Instantiation;
912
913 --------------------------
914 -- Analyze_Associations --
915 --------------------------
916
917 function Analyze_Associations
918 (I_Node : Node_Id;
919 Formals : List_Id;
920 F_Copy : List_Id) return List_Id
921 is
922 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
923 Assoc : constant List_Id := New_List;
924 Default_Actuals : constant Elist_Id := New_Elmt_List;
925 Gen_Unit : constant Entity_Id :=
926 Defining_Entity (Parent (F_Copy));
927
928 Actuals : List_Id;
929 Actual : Node_Id;
930 Analyzed_Formal : Node_Id;
931 First_Named : Node_Id := Empty;
932 Formal : Node_Id;
933 Match : Node_Id;
934 Named : Node_Id;
935 Saved_Formal : Node_Id;
936
937 Default_Formals : constant List_Id := New_List;
938 -- If an Others_Choice is present, some of the formals may be defaulted.
939 -- To simplify the treatment of visibility in an instance, we introduce
940 -- individual defaults for each such formal. These defaults are
941 -- appended to the list of associations and replace the Others_Choice.
942
943 Found_Assoc : Node_Id;
944 -- Association for the current formal being match. Empty if there are
945 -- no remaining actuals, or if there is no named association with the
946 -- name of the formal.
947
948 Is_Named_Assoc : Boolean;
949 Num_Matched : Int := 0;
950 Num_Actuals : Int := 0;
951
952 Others_Present : Boolean := False;
953 Others_Choice : Node_Id := Empty;
954 -- In Ada 2005, indicates partial parameterization of a formal
955 -- package. As usual an other association must be last in the list.
956
957 function Build_Function_Wrapper
958 (Formal : Entity_Id;
959 Actual : Entity_Id := Empty) return Node_Id;
960 -- In GNATprove mode, create a wrapper function for actuals that are
961 -- functions with any number of formal parameters, in order to propagate
962 -- their contract to the renaming declarations generated for them.
963 -- If the actual is absent, the formal has a default, and the name of
964 -- the function is that of the formal.
965
966 function Build_Operator_Wrapper
967 (Formal : Entity_Id;
968 Actual : Entity_Id := Empty) return Node_Id;
969 -- In GNATprove mode, create a wrapper function for actuals that are
970 -- operators, in order to propagate their contract to the renaming
971 -- declarations generated for them. If the actual is absent, this is
972 -- a formal with a default, and the name of the operator is that of the
973 -- formal.
974
975 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
976 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
977 -- cannot have a named association for it. AI05-0025 extends this rule
978 -- to formals of formal packages by AI05-0025, and it also applies to
979 -- box-initialized formals.
980
981 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
982 -- Determine whether the parameter types and the return type of Subp
983 -- are fully defined at the point of instantiation.
984
985 function Matching_Actual
986 (F : Entity_Id;
987 A_F : Entity_Id) return Node_Id;
988 -- Find actual that corresponds to a given a formal parameter. If the
989 -- actuals are positional, return the next one, if any. If the actuals
990 -- are named, scan the parameter associations to find the right one.
991 -- A_F is the corresponding entity in the analyzed generic,which is
992 -- placed on the selector name for ASIS use.
993 --
994 -- In Ada 2005, a named association may be given with a box, in which
995 -- case Matching_Actual sets Found_Assoc to the generic association,
996 -- but return Empty for the actual itself. In this case the code below
997 -- creates a corresponding declaration for the formal.
998
999 function Partial_Parameterization return Boolean;
1000 -- Ada 2005: if no match is found for a given formal, check if the
1001 -- association for it includes a box, or whether the associations
1002 -- include an Others clause.
1003
1004 procedure Process_Default (F : Entity_Id);
1005 -- Add a copy of the declaration of generic formal F to the list of
1006 -- associations, and add an explicit box association for F if there
1007 -- is none yet, and the default comes from an Others_Choice.
1008
1009 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1010 -- Determine whether Subp renames one of the subprograms defined in the
1011 -- generated package Standard.
1012
1013 procedure Set_Analyzed_Formal;
1014 -- Find the node in the generic copy that corresponds to a given formal.
1015 -- The semantic information on this node is used to perform legality
1016 -- checks on the actuals. Because semantic analysis can introduce some
1017 -- anonymous entities or modify the declaration node itself, the
1018 -- correspondence between the two lists is not one-one. In addition to
1019 -- anonymous types, the presence a formal equality will introduce an
1020 -- implicit declaration for the corresponding inequality.
1021
1022 ----------------------------
1023 -- Build_Function_Wrapper --
1024 ----------------------------
1025
1026 function Build_Function_Wrapper
1027 (Formal : Entity_Id;
1028 Actual : Entity_Id := Empty) return Node_Id
1029 is
1030 Loc : constant Source_Ptr := Sloc (I_Node);
1031 Actuals : List_Id;
1032 Decl : Node_Id;
1033 Func_Name : Node_Id;
1034 Func : Entity_Id;
1035 N_Parms : Natural;
1036 Parm_Type : Node_Id;
1037 Profile : List_Id := New_List;
1038 Spec : Node_Id;
1039 F : Entity_Id;
1040 New_F : Entity_Id;
1041
1042 begin
1043 -- If there is no actual, the formal has a default and is retrieved
1044 -- by name. Otherwise the wrapper encloses a call to the actual.
1045
1046 if No (Actual) then
1047 Func_Name := Make_Identifier (Loc, Chars (Formal));
1048 else
1049 Func_Name := New_Occurrence_Of (Entity (Actual), Loc);
1050 end if;
1051
1052 Func := Make_Defining_Identifier (Loc, Chars (Formal));
1053 Set_Ekind (Func, E_Function);
1054 Set_Is_Generic_Actual_Subprogram (Func);
1055
1056 Actuals := New_List;
1057 Profile := New_List;
1058
1059 if Present (Actual) then
1060 F := First_Formal (Entity (Actual));
1061 else
1062 F := First_Formal (Formal);
1063 end if;
1064
1065 N_Parms := 0;
1066 while Present (F) loop
1067
1068 -- Create new formal for profile of wrapper, and add a reference
1069 -- to it in the list of actuals for the enclosing call.
1070
1071 New_F := Make_Temporary
1072 (Loc, Character'Val (Character'Pos ('A') + N_Parms));
1073
1074 if No (Actual) then
1075
1076 -- If formal has a class-wide type rewrite as the corresponding
1077 -- attribute, because the class-wide type is not retrievable by
1078 -- visbility.
1079
1080 if Is_Class_Wide_Type (Etype (F)) then
1081 Parm_Type :=
1082 Make_Attribute_Reference (Loc,
1083 Attribute_Name => Name_Class,
1084 Prefix =>
1085 Make_Identifier (Loc, Chars (Etype (Etype (F)))));
1086
1087 else
1088 Parm_Type :=
1089 Make_Identifier (Loc, Chars (Etype (Etype (F))));
1090 end if;
1091
1092 -- If actual is present, use the type of its own formal
1093
1094 else
1095 Parm_Type := New_Occurrence_Of (Etype (F), Loc);
1096 end if;
1097
1098 Append_To (Profile,
1099 Make_Parameter_Specification (Loc,
1100 Defining_Identifier => New_F,
1101 Parameter_Type => Parm_Type));
1102
1103 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
1104 Next_Formal (F);
1105 N_Parms := N_Parms + 1;
1106 end loop;
1107
1108 Spec :=
1109 Make_Function_Specification (Loc,
1110 Defining_Unit_Name => Func,
1111 Parameter_Specifications => Profile,
1112 Result_Definition =>
1113 Make_Identifier (Loc, Chars (Etype (Formal))));
1114 Decl :=
1115 Make_Expression_Function (Loc,
1116 Specification => Spec,
1117 Expression =>
1118 Make_Function_Call (Loc,
1119 Name => Func_Name,
1120 Parameter_Associations => Actuals));
1121
1122 return Decl;
1123 end Build_Function_Wrapper;
1124
1125 ----------------------------
1126 -- Build_Operator_Wrapper --
1127 ----------------------------
1128
1129 function Build_Operator_Wrapper
1130 (Formal : Entity_Id;
1131 Actual : Entity_Id := Empty) return Node_Id
1132 is
1133 Loc : constant Source_Ptr := Sloc (I_Node);
1134 Typ : constant Entity_Id := Etype (Formal);
1135 Is_Binary : constant Boolean :=
1136 Present (Next_Formal (First_Formal (Formal)));
1137
1138 Decl : Node_Id;
1139 Expr : Node_Id;
1140 F1, F2 : Entity_Id;
1141 Func : Entity_Id;
1142 Op_Name : Name_Id;
1143 Spec : Node_Id;
1144 L, R : Node_Id;
1145
1146 begin
1147 if No (Actual) then
1148 Op_Name := Chars (Formal);
1149 else
1150 Op_Name := Chars (Actual);
1151 end if;
1152
1153 -- Create entities for wrapper function and its formals
1154
1155 F1 := Make_Temporary (Loc, 'A');
1156 F2 := Make_Temporary (Loc, 'B');
1157 L := New_Occurrence_Of (F1, Loc);
1158 R := New_Occurrence_Of (F2, Loc);
1159
1160 Func := Make_Defining_Identifier (Loc, Chars (Formal));
1161 Set_Ekind (Func, E_Function);
1162 Set_Is_Generic_Actual_Subprogram (Func);
1163
1164 Spec :=
1165 Make_Function_Specification (Loc,
1166 Defining_Unit_Name => Func,
1167 Parameter_Specifications => New_List (
1168 Make_Parameter_Specification (Loc,
1169 Defining_Identifier => F1,
1170 Parameter_Type =>
1171 Make_Identifier (Loc,
1172 Chars => Chars (Etype (First_Formal (Formal)))))),
1173 Result_Definition => Make_Identifier (Loc, Chars (Typ)));
1174
1175 if Is_Binary then
1176 Append_To (Parameter_Specifications (Spec),
1177 Make_Parameter_Specification (Loc,
1178 Defining_Identifier => F2,
1179 Parameter_Type =>
1180 Make_Identifier (Loc,
1181 Chars (Etype (Next_Formal (First_Formal (Formal)))))));
1182 end if;
1183
1184 -- Build expression as a function call, or as an operator node
1185 -- that corresponds to the name of the actual, starting with binary
1186 -- operators.
1187
1188 if Present (Actual) and then Op_Name not in Any_Operator_Name then
1189 Expr :=
1190 Make_Function_Call (Loc,
1191 Name =>
1192 New_Occurrence_Of (Entity (Actual), Loc),
1193 Parameter_Associations => New_List (L));
1194
1195 if Is_Binary then
1196 Append_To (Parameter_Associations (Expr), R);
1197 end if;
1198
1199 -- Binary operators
1200
1201 elsif Is_Binary then
1202 if Op_Name = Name_Op_And then
1203 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
1204 elsif Op_Name = Name_Op_Or then
1205 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
1206 elsif Op_Name = Name_Op_Xor then
1207 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
1208 elsif Op_Name = Name_Op_Eq then
1209 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
1210 elsif Op_Name = Name_Op_Ne then
1211 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
1212 elsif Op_Name = Name_Op_Le then
1213 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
1214 elsif Op_Name = Name_Op_Gt then
1215 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
1216 elsif Op_Name = Name_Op_Ge then
1217 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
1218 elsif Op_Name = Name_Op_Lt then
1219 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
1220 elsif Op_Name = Name_Op_Add then
1221 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
1222 elsif Op_Name = Name_Op_Subtract then
1223 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
1224 elsif Op_Name = Name_Op_Concat then
1225 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
1226 elsif Op_Name = Name_Op_Multiply then
1227 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
1228 elsif Op_Name = Name_Op_Divide then
1229 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
1230 elsif Op_Name = Name_Op_Mod then
1231 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
1232 elsif Op_Name = Name_Op_Rem then
1233 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
1234 elsif Op_Name = Name_Op_Expon then
1235 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
1236 end if;
1237
1238 -- Unary operators
1239
1240 else
1241 if Op_Name = Name_Op_Add then
1242 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
1243 elsif Op_Name = Name_Op_Subtract then
1244 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
1245 elsif Op_Name = Name_Op_Abs then
1246 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
1247 elsif Op_Name = Name_Op_Not then
1248 Expr := Make_Op_Not (Loc, Right_Opnd => L);
1249 end if;
1250 end if;
1251
1252 -- Propagate visible entity to operator node, either from a
1253 -- given actual or from a default.
1254
1255 if Is_Entity_Name (Actual) and then Nkind (Expr) in N_Op then
1256 Set_Entity (Expr, Entity (Actual));
1257 end if;
1258
1259 Decl :=
1260 Make_Expression_Function (Loc,
1261 Specification => Spec,
1262 Expression => Expr);
1263
1264 return Decl;
1265 end Build_Operator_Wrapper;
1266
1267 ----------------------------------------
1268 -- Check_Overloaded_Formal_Subprogram --
1269 ----------------------------------------
1270
1271 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1272 Temp_Formal : Entity_Id;
1273
1274 begin
1275 Temp_Formal := First (Formals);
1276 while Present (Temp_Formal) loop
1277 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1278 and then Temp_Formal /= Formal
1279 and then
1280 Chars (Defining_Unit_Name (Specification (Formal))) =
1281 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1282 then
1283 if Present (Found_Assoc) then
1284 Error_Msg_N
1285 ("named association not allowed for overloaded formal",
1286 Found_Assoc);
1287
1288 else
1289 Error_Msg_N
1290 ("named association not allowed for overloaded formal",
1291 Others_Choice);
1292 end if;
1293
1294 Abandon_Instantiation (Instantiation_Node);
1295 end if;
1296
1297 Next (Temp_Formal);
1298 end loop;
1299 end Check_Overloaded_Formal_Subprogram;
1300
1301 -------------------------------
1302 -- Has_Fully_Defined_Profile --
1303 -------------------------------
1304
1305 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1306 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1307 -- Determine whethet type Typ is fully defined
1308
1309 ---------------------------
1310 -- Is_Fully_Defined_Type --
1311 ---------------------------
1312
1313 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1314 begin
1315 -- A private type without a full view is not fully defined
1316
1317 if Is_Private_Type (Typ)
1318 and then No (Full_View (Typ))
1319 then
1320 return False;
1321
1322 -- An incomplete type is never fully defined
1323
1324 elsif Is_Incomplete_Type (Typ) then
1325 return False;
1326
1327 -- All other types are fully defined
1328
1329 else
1330 return True;
1331 end if;
1332 end Is_Fully_Defined_Type;
1333
1334 -- Local declarations
1335
1336 Param : Entity_Id;
1337
1338 -- Start of processing for Has_Fully_Defined_Profile
1339
1340 begin
1341 -- Check the parameters
1342
1343 Param := First_Formal (Subp);
1344 while Present (Param) loop
1345 if not Is_Fully_Defined_Type (Etype (Param)) then
1346 return False;
1347 end if;
1348
1349 Next_Formal (Param);
1350 end loop;
1351
1352 -- Check the return type
1353
1354 return Is_Fully_Defined_Type (Etype (Subp));
1355 end Has_Fully_Defined_Profile;
1356
1357 ---------------------
1358 -- Matching_Actual --
1359 ---------------------
1360
1361 function Matching_Actual
1362 (F : Entity_Id;
1363 A_F : Entity_Id) return Node_Id
1364 is
1365 Prev : Node_Id;
1366 Act : Node_Id;
1367
1368 begin
1369 Is_Named_Assoc := False;
1370
1371 -- End of list of purely positional parameters
1372
1373 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1374 Found_Assoc := Empty;
1375 Act := Empty;
1376
1377 -- Case of positional parameter corresponding to current formal
1378
1379 elsif No (Selector_Name (Actual)) then
1380 Found_Assoc := Actual;
1381 Act := Explicit_Generic_Actual_Parameter (Actual);
1382 Num_Matched := Num_Matched + 1;
1383 Next (Actual);
1384
1385 -- Otherwise scan list of named actuals to find the one with the
1386 -- desired name. All remaining actuals have explicit names.
1387
1388 else
1389 Is_Named_Assoc := True;
1390 Found_Assoc := Empty;
1391 Act := Empty;
1392 Prev := Empty;
1393
1394 while Present (Actual) loop
1395 if Chars (Selector_Name (Actual)) = Chars (F) then
1396 Set_Entity (Selector_Name (Actual), A_F);
1397 Set_Etype (Selector_Name (Actual), Etype (A_F));
1398 Generate_Reference (A_F, Selector_Name (Actual));
1399 Found_Assoc := Actual;
1400 Act := Explicit_Generic_Actual_Parameter (Actual);
1401 Num_Matched := Num_Matched + 1;
1402 exit;
1403 end if;
1404
1405 Prev := Actual;
1406 Next (Actual);
1407 end loop;
1408
1409 -- Reset for subsequent searches. In most cases the named
1410 -- associations are in order. If they are not, we reorder them
1411 -- to avoid scanning twice the same actual. This is not just a
1412 -- question of efficiency: there may be multiple defaults with
1413 -- boxes that have the same name. In a nested instantiation we
1414 -- insert actuals for those defaults, and cannot rely on their
1415 -- names to disambiguate them.
1416
1417 if Actual = First_Named then
1418 Next (First_Named);
1419
1420 elsif Present (Actual) then
1421 Insert_Before (First_Named, Remove_Next (Prev));
1422 end if;
1423
1424 Actual := First_Named;
1425 end if;
1426
1427 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1428 Set_Used_As_Generic_Actual (Entity (Act));
1429 end if;
1430
1431 return Act;
1432 end Matching_Actual;
1433
1434 ------------------------------
1435 -- Partial_Parameterization --
1436 ------------------------------
1437
1438 function Partial_Parameterization return Boolean is
1439 begin
1440 return Others_Present
1441 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1442 end Partial_Parameterization;
1443
1444 ---------------------
1445 -- Process_Default --
1446 ---------------------
1447
1448 procedure Process_Default (F : Entity_Id) is
1449 Loc : constant Source_Ptr := Sloc (I_Node);
1450 F_Id : constant Entity_Id := Defining_Entity (F);
1451 Decl : Node_Id;
1452 Default : Node_Id;
1453 Id : Entity_Id;
1454
1455 begin
1456 -- Append copy of formal declaration to associations, and create new
1457 -- defining identifier for it.
1458
1459 Decl := New_Copy_Tree (F);
1460 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1461
1462 if Nkind (F) in N_Formal_Subprogram_Declaration then
1463 Set_Defining_Unit_Name (Specification (Decl), Id);
1464
1465 else
1466 Set_Defining_Identifier (Decl, Id);
1467 end if;
1468
1469 Append (Decl, Assoc);
1470
1471 if No (Found_Assoc) then
1472 Default :=
1473 Make_Generic_Association (Loc,
1474 Selector_Name => New_Occurrence_Of (Id, Loc),
1475 Explicit_Generic_Actual_Parameter => Empty);
1476 Set_Box_Present (Default);
1477 Append (Default, Default_Formals);
1478 end if;
1479 end Process_Default;
1480
1481 ---------------------------------
1482 -- Renames_Standard_Subprogram --
1483 ---------------------------------
1484
1485 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1486 Id : Entity_Id;
1487
1488 begin
1489 Id := Alias (Subp);
1490 while Present (Id) loop
1491 if Scope (Id) = Standard_Standard then
1492 return True;
1493 end if;
1494
1495 Id := Alias (Id);
1496 end loop;
1497
1498 return False;
1499 end Renames_Standard_Subprogram;
1500
1501 -------------------------
1502 -- Set_Analyzed_Formal --
1503 -------------------------
1504
1505 procedure Set_Analyzed_Formal is
1506 Kind : Node_Kind;
1507
1508 begin
1509 while Present (Analyzed_Formal) loop
1510 Kind := Nkind (Analyzed_Formal);
1511
1512 case Nkind (Formal) is
1513
1514 when N_Formal_Subprogram_Declaration =>
1515 exit when Kind in N_Formal_Subprogram_Declaration
1516 and then
1517 Chars
1518 (Defining_Unit_Name (Specification (Formal))) =
1519 Chars
1520 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1521
1522 when N_Formal_Package_Declaration =>
1523 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1524 N_Generic_Package_Declaration,
1525 N_Package_Declaration);
1526
1527 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1528
1529 when others =>
1530
1531 -- Skip freeze nodes, and nodes inserted to replace
1532 -- unrecognized pragmas.
1533
1534 exit when
1535 Kind not in N_Formal_Subprogram_Declaration
1536 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1537 N_Freeze_Entity,
1538 N_Null_Statement,
1539 N_Itype_Reference)
1540 and then Chars (Defining_Identifier (Formal)) =
1541 Chars (Defining_Identifier (Analyzed_Formal));
1542 end case;
1543
1544 Next (Analyzed_Formal);
1545 end loop;
1546 end Set_Analyzed_Formal;
1547
1548 -- Start of processing for Analyze_Associations
1549
1550 begin
1551 Actuals := Generic_Associations (I_Node);
1552
1553 if Present (Actuals) then
1554
1555 -- Check for an Others choice, indicating a partial parameterization
1556 -- for a formal package.
1557
1558 Actual := First (Actuals);
1559 while Present (Actual) loop
1560 if Nkind (Actual) = N_Others_Choice then
1561 Others_Present := True;
1562 Others_Choice := Actual;
1563
1564 if Present (Next (Actual)) then
1565 Error_Msg_N ("others must be last association", Actual);
1566 end if;
1567
1568 -- This subprogram is used both for formal packages and for
1569 -- instantiations. For the latter, associations must all be
1570 -- explicit.
1571
1572 if Nkind (I_Node) /= N_Formal_Package_Declaration
1573 and then Comes_From_Source (I_Node)
1574 then
1575 Error_Msg_N
1576 ("others association not allowed in an instance",
1577 Actual);
1578 end if;
1579
1580 -- In any case, nothing to do after the others association
1581
1582 exit;
1583
1584 elsif Box_Present (Actual)
1585 and then Comes_From_Source (I_Node)
1586 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1587 then
1588 Error_Msg_N
1589 ("box association not allowed in an instance", Actual);
1590 end if;
1591
1592 Next (Actual);
1593 end loop;
1594
1595 -- If named associations are present, save first named association
1596 -- (it may of course be Empty) to facilitate subsequent name search.
1597
1598 First_Named := First (Actuals);
1599 while Present (First_Named)
1600 and then Nkind (First_Named) /= N_Others_Choice
1601 and then No (Selector_Name (First_Named))
1602 loop
1603 Num_Actuals := Num_Actuals + 1;
1604 Next (First_Named);
1605 end loop;
1606 end if;
1607
1608 Named := First_Named;
1609 while Present (Named) loop
1610 if Nkind (Named) /= N_Others_Choice
1611 and then No (Selector_Name (Named))
1612 then
1613 Error_Msg_N ("invalid positional actual after named one", Named);
1614 Abandon_Instantiation (Named);
1615 end if;
1616
1617 -- A named association may lack an actual parameter, if it was
1618 -- introduced for a default subprogram that turns out to be local
1619 -- to the outer instantiation.
1620
1621 if Nkind (Named) /= N_Others_Choice
1622 and then Present (Explicit_Generic_Actual_Parameter (Named))
1623 then
1624 Num_Actuals := Num_Actuals + 1;
1625 end if;
1626
1627 Next (Named);
1628 end loop;
1629
1630 if Present (Formals) then
1631 Formal := First_Non_Pragma (Formals);
1632 Analyzed_Formal := First_Non_Pragma (F_Copy);
1633
1634 if Present (Actuals) then
1635 Actual := First (Actuals);
1636
1637 -- All formals should have default values
1638
1639 else
1640 Actual := Empty;
1641 end if;
1642
1643 while Present (Formal) loop
1644 Set_Analyzed_Formal;
1645 Saved_Formal := Next_Non_Pragma (Formal);
1646
1647 case Nkind (Formal) is
1648 when N_Formal_Object_Declaration =>
1649 Match :=
1650 Matching_Actual (
1651 Defining_Identifier (Formal),
1652 Defining_Identifier (Analyzed_Formal));
1653
1654 if No (Match) and then Partial_Parameterization then
1655 Process_Default (Formal);
1656 else
1657 Append_List
1658 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1659 Assoc);
1660 end if;
1661
1662 when N_Formal_Type_Declaration =>
1663 Match :=
1664 Matching_Actual (
1665 Defining_Identifier (Formal),
1666 Defining_Identifier (Analyzed_Formal));
1667
1668 if No (Match) then
1669 if Partial_Parameterization then
1670 Process_Default (Formal);
1671
1672 else
1673 Error_Msg_Sloc := Sloc (Gen_Unit);
1674 Error_Msg_NE
1675 ("missing actual&",
1676 Instantiation_Node,
1677 Defining_Identifier (Formal));
1678 Error_Msg_NE ("\in instantiation of & declared#",
1679 Instantiation_Node, Gen_Unit);
1680 Abandon_Instantiation (Instantiation_Node);
1681 end if;
1682
1683 else
1684 Analyze (Match);
1685 Append_List
1686 (Instantiate_Type
1687 (Formal, Match, Analyzed_Formal, Assoc),
1688 Assoc);
1689
1690 -- An instantiation is a freeze point for the actuals,
1691 -- unless this is a rewritten formal package, or the
1692 -- formal is an Ada 2012 formal incomplete type.
1693
1694 if Nkind (I_Node) = N_Formal_Package_Declaration
1695 or else
1696 (Ada_Version >= Ada_2012
1697 and then
1698 Ekind (Defining_Identifier (Analyzed_Formal)) =
1699 E_Incomplete_Type)
1700 then
1701 null;
1702
1703 else
1704 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1705 end if;
1706 end if;
1707
1708 -- A remote access-to-class-wide type is not a legal actual
1709 -- for a generic formal of an access type (E.2.2(17/2)).
1710 -- In GNAT an exception to this rule is introduced when
1711 -- the formal is marked as remote using implementation
1712 -- defined aspect/pragma Remote_Access_Type. In that case
1713 -- the actual must be remote as well.
1714
1715 -- If the current instantiation is the construction of a
1716 -- local copy for a formal package the actuals may be
1717 -- defaulted, and there is no matching actual to check.
1718
1719 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1720 and then
1721 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1722 N_Access_To_Object_Definition
1723 and then Present (Match)
1724 then
1725 declare
1726 Formal_Ent : constant Entity_Id :=
1727 Defining_Identifier (Analyzed_Formal);
1728 begin
1729 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1730 = Is_Remote_Types (Formal_Ent)
1731 then
1732 -- Remoteness of formal and actual match
1733
1734 null;
1735
1736 elsif Is_Remote_Types (Formal_Ent) then
1737
1738 -- Remote formal, non-remote actual
1739
1740 Error_Msg_NE
1741 ("actual for& must be remote", Match, Formal_Ent);
1742
1743 else
1744 -- Non-remote formal, remote actual
1745
1746 Error_Msg_NE
1747 ("actual for& may not be remote",
1748 Match, Formal_Ent);
1749 end if;
1750 end;
1751 end if;
1752
1753 when N_Formal_Subprogram_Declaration =>
1754 Match :=
1755 Matching_Actual
1756 (Defining_Unit_Name (Specification (Formal)),
1757 Defining_Unit_Name (Specification (Analyzed_Formal)));
1758
1759 -- If the formal subprogram has the same name as another
1760 -- formal subprogram of the generic, then a named
1761 -- association is illegal (12.3(9)). Exclude named
1762 -- associations that are generated for a nested instance.
1763
1764 if Present (Match)
1765 and then Is_Named_Assoc
1766 and then Comes_From_Source (Found_Assoc)
1767 then
1768 Check_Overloaded_Formal_Subprogram (Formal);
1769 end if;
1770
1771 -- If there is no corresponding actual, this may be case
1772 -- of partial parameterization, or else the formal has a
1773 -- default or a box.
1774
1775 if No (Match) and then Partial_Parameterization then
1776 Process_Default (Formal);
1777
1778 if Nkind (I_Node) = N_Formal_Package_Declaration then
1779 Check_Overloaded_Formal_Subprogram (Formal);
1780 end if;
1781
1782 else
1783 if GNATprove_Mode
1784 and then Present
1785 (Containing_Package_With_Ext_Axioms
1786 (Defining_Entity (Analyzed_Formal)))
1787 and then Ekind (Defining_Entity (Analyzed_Formal)) =
1788 E_Function
1789 then
1790 -- If actual is an entity (function or operator),
1791 -- build wrapper for it.
1792
1793 if Present (Match) then
1794 if Nkind (Match) = N_Operator_Symbol then
1795
1796 -- If the name is a default, find its visible
1797 -- entity at the point of instantiation.
1798
1799 if Is_Entity_Name (Match)
1800 and then No (Entity (Match))
1801 then
1802 Find_Direct_Name (Match);
1803 end if;
1804
1805 Append_To
1806 (Assoc,
1807 Build_Operator_Wrapper
1808 (Defining_Entity (Analyzed_Formal), Match));
1809
1810 else
1811 Append_To (Assoc,
1812 Build_Function_Wrapper
1813 (Defining_Entity (Analyzed_Formal), Match));
1814 end if;
1815
1816 -- Ditto if formal is an operator with a default.
1817
1818 elsif Box_Present (Formal)
1819 and then Nkind (Defining_Entity (Analyzed_Formal)) =
1820 N_Defining_Operator_Symbol
1821 then
1822 Append_To (Assoc,
1823 Build_Operator_Wrapper
1824 (Defining_Entity (Analyzed_Formal)));
1825
1826 -- Otherwise create renaming declaration.
1827
1828 else
1829 Append_To (Assoc,
1830 Build_Function_Wrapper
1831 (Defining_Entity (Analyzed_Formal)));
1832 end if;
1833
1834 else
1835 Append_To (Assoc,
1836 Instantiate_Formal_Subprogram
1837 (Formal, Match, Analyzed_Formal));
1838 end if;
1839
1840 -- An instantiation is a freeze point for the actuals,
1841 -- unless this is a rewritten formal package.
1842
1843 if Nkind (I_Node) /= N_Formal_Package_Declaration
1844 and then Nkind (Match) = N_Identifier
1845 and then Is_Subprogram (Entity (Match))
1846
1847 -- The actual subprogram may rename a routine defined
1848 -- in Standard. Avoid freezing such renamings because
1849 -- subprograms coming from Standard cannot be frozen.
1850
1851 and then
1852 not Renames_Standard_Subprogram (Entity (Match))
1853
1854 -- If the actual subprogram comes from a different
1855 -- unit, it is already frozen, either by a body in
1856 -- that unit or by the end of the declarative part
1857 -- of the unit. This check avoids the freezing of
1858 -- subprograms defined in Standard which are used
1859 -- as generic actuals.
1860
1861 and then In_Same_Code_Unit (Entity (Match), I_Node)
1862 and then Has_Fully_Defined_Profile (Entity (Match))
1863 then
1864 -- Mark the subprogram as having a delayed freeze
1865 -- since this may be an out-of-order action.
1866
1867 Set_Has_Delayed_Freeze (Entity (Match));
1868 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1869 end if;
1870 end if;
1871
1872 -- If this is a nested generic, preserve default for later
1873 -- instantiations.
1874
1875 if No (Match) and then Box_Present (Formal) then
1876 Append_Elmt
1877 (Defining_Unit_Name (Specification (Last (Assoc))),
1878 Default_Actuals);
1879 end if;
1880
1881 when N_Formal_Package_Declaration =>
1882 Match :=
1883 Matching_Actual (
1884 Defining_Identifier (Formal),
1885 Defining_Identifier (Original_Node (Analyzed_Formal)));
1886
1887 if No (Match) then
1888 if Partial_Parameterization then
1889 Process_Default (Formal);
1890
1891 else
1892 Error_Msg_Sloc := Sloc (Gen_Unit);
1893 Error_Msg_NE
1894 ("missing actual&",
1895 Instantiation_Node, Defining_Identifier (Formal));
1896 Error_Msg_NE ("\in instantiation of & declared#",
1897 Instantiation_Node, Gen_Unit);
1898
1899 Abandon_Instantiation (Instantiation_Node);
1900 end if;
1901
1902 else
1903 Analyze (Match);
1904 Append_List
1905 (Instantiate_Formal_Package
1906 (Formal, Match, Analyzed_Formal),
1907 Assoc);
1908 end if;
1909
1910 -- For use type and use package appearing in the generic part,
1911 -- we have already copied them, so we can just move them where
1912 -- they belong (we mustn't recopy them since this would mess up
1913 -- the Sloc values).
1914
1915 when N_Use_Package_Clause |
1916 N_Use_Type_Clause =>
1917 if Nkind (Original_Node (I_Node)) =
1918 N_Formal_Package_Declaration
1919 then
1920 Append (New_Copy_Tree (Formal), Assoc);
1921 else
1922 Remove (Formal);
1923 Append (Formal, Assoc);
1924 end if;
1925
1926 when others =>
1927 raise Program_Error;
1928
1929 end case;
1930
1931 Formal := Saved_Formal;
1932 Next_Non_Pragma (Analyzed_Formal);
1933 end loop;
1934
1935 if Num_Actuals > Num_Matched then
1936 Error_Msg_Sloc := Sloc (Gen_Unit);
1937
1938 if Present (Selector_Name (Actual)) then
1939 Error_Msg_NE
1940 ("unmatched actual&",
1941 Actual, Selector_Name (Actual));
1942 Error_Msg_NE ("\in instantiation of& declared#",
1943 Actual, Gen_Unit);
1944 else
1945 Error_Msg_NE
1946 ("unmatched actual in instantiation of& declared#",
1947 Actual, Gen_Unit);
1948 end if;
1949 end if;
1950
1951 elsif Present (Actuals) then
1952 Error_Msg_N
1953 ("too many actuals in generic instantiation", Instantiation_Node);
1954 end if;
1955
1956 -- An instantiation freezes all generic actuals. The only exceptions
1957 -- to this are incomplete types and subprograms which are not fully
1958 -- defined at the point of instantiation.
1959
1960 declare
1961 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1962 begin
1963 while Present (Elmt) loop
1964 Freeze_Before (I_Node, Node (Elmt));
1965 Next_Elmt (Elmt);
1966 end loop;
1967 end;
1968
1969 -- If there are default subprograms, normalize the tree by adding
1970 -- explicit associations for them. This is required if the instance
1971 -- appears within a generic.
1972
1973 declare
1974 Elmt : Elmt_Id;
1975 Subp : Entity_Id;
1976 New_D : Node_Id;
1977
1978 begin
1979 Elmt := First_Elmt (Default_Actuals);
1980 while Present (Elmt) loop
1981 if No (Actuals) then
1982 Actuals := New_List;
1983 Set_Generic_Associations (I_Node, Actuals);
1984 end if;
1985
1986 Subp := Node (Elmt);
1987 New_D :=
1988 Make_Generic_Association (Sloc (Subp),
1989 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1990 Explicit_Generic_Actual_Parameter =>
1991 New_Occurrence_Of (Subp, Sloc (Subp)));
1992 Mark_Rewrite_Insertion (New_D);
1993 Append_To (Actuals, New_D);
1994 Next_Elmt (Elmt);
1995 end loop;
1996 end;
1997
1998 -- If this is a formal package, normalize the parameter list by adding
1999 -- explicit box associations for the formals that are covered by an
2000 -- Others_Choice.
2001
2002 if not Is_Empty_List (Default_Formals) then
2003 Append_List (Default_Formals, Formals);
2004 end if;
2005
2006 return Assoc;
2007 end Analyze_Associations;
2008
2009 -------------------------------
2010 -- Analyze_Formal_Array_Type --
2011 -------------------------------
2012
2013 procedure Analyze_Formal_Array_Type
2014 (T : in out Entity_Id;
2015 Def : Node_Id)
2016 is
2017 DSS : Node_Id;
2018
2019 begin
2020 -- Treated like a non-generic array declaration, with additional
2021 -- semantic checks.
2022
2023 Enter_Name (T);
2024
2025 if Nkind (Def) = N_Constrained_Array_Definition then
2026 DSS := First (Discrete_Subtype_Definitions (Def));
2027 while Present (DSS) loop
2028 if Nkind_In (DSS, N_Subtype_Indication,
2029 N_Range,
2030 N_Attribute_Reference)
2031 then
2032 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2033 end if;
2034
2035 Next (DSS);
2036 end loop;
2037 end if;
2038
2039 Array_Type_Declaration (T, Def);
2040 Set_Is_Generic_Type (Base_Type (T));
2041
2042 if Ekind (Component_Type (T)) = E_Incomplete_Type
2043 and then No (Full_View (Component_Type (T)))
2044 then
2045 Error_Msg_N ("premature usage of incomplete type", Def);
2046
2047 -- Check that range constraint is not allowed on the component type
2048 -- of a generic formal array type (AARM 12.5.3(3))
2049
2050 elsif Is_Internal (Component_Type (T))
2051 and then Present (Subtype_Indication (Component_Definition (Def)))
2052 and then Nkind (Original_Node
2053 (Subtype_Indication (Component_Definition (Def)))) =
2054 N_Subtype_Indication
2055 then
2056 Error_Msg_N
2057 ("in a formal, a subtype indication can only be "
2058 & "a subtype mark (RM 12.5.3(3))",
2059 Subtype_Indication (Component_Definition (Def)));
2060 end if;
2061
2062 end Analyze_Formal_Array_Type;
2063
2064 ---------------------------------------------
2065 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2066 ---------------------------------------------
2067
2068 -- As for other generic types, we create a valid type representation with
2069 -- legal but arbitrary attributes, whose values are never considered
2070 -- static. For all scalar types we introduce an anonymous base type, with
2071 -- the same attributes. We choose the corresponding integer type to be
2072 -- Standard_Integer.
2073 -- Here and in other similar routines, the Sloc of the generated internal
2074 -- type must be the same as the sloc of the defining identifier of the
2075 -- formal type declaration, to provide proper source navigation.
2076
2077 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2078 (T : Entity_Id;
2079 Def : Node_Id)
2080 is
2081 Loc : constant Source_Ptr := Sloc (Def);
2082
2083 Base : constant Entity_Id :=
2084 New_Internal_Entity
2085 (E_Decimal_Fixed_Point_Type,
2086 Current_Scope,
2087 Sloc (Defining_Identifier (Parent (Def))), 'G');
2088
2089 Int_Base : constant Entity_Id := Standard_Integer;
2090 Delta_Val : constant Ureal := Ureal_1;
2091 Digs_Val : constant Uint := Uint_6;
2092
2093 function Make_Dummy_Bound return Node_Id;
2094 -- Return a properly typed universal real literal to use as a bound
2095
2096 ----------------------
2097 -- Make_Dummy_Bound --
2098 ----------------------
2099
2100 function Make_Dummy_Bound return Node_Id is
2101 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2102 begin
2103 Set_Etype (Bound, Universal_Real);
2104 return Bound;
2105 end Make_Dummy_Bound;
2106
2107 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2108
2109 begin
2110 Enter_Name (T);
2111
2112 Set_Etype (Base, Base);
2113 Set_Size_Info (Base, Int_Base);
2114 Set_RM_Size (Base, RM_Size (Int_Base));
2115 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2116 Set_Digits_Value (Base, Digs_Val);
2117 Set_Delta_Value (Base, Delta_Val);
2118 Set_Small_Value (Base, Delta_Val);
2119 Set_Scalar_Range (Base,
2120 Make_Range (Loc,
2121 Low_Bound => Make_Dummy_Bound,
2122 High_Bound => Make_Dummy_Bound));
2123
2124 Set_Is_Generic_Type (Base);
2125 Set_Parent (Base, Parent (Def));
2126
2127 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2128 Set_Etype (T, Base);
2129 Set_Size_Info (T, Int_Base);
2130 Set_RM_Size (T, RM_Size (Int_Base));
2131 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2132 Set_Digits_Value (T, Digs_Val);
2133 Set_Delta_Value (T, Delta_Val);
2134 Set_Small_Value (T, Delta_Val);
2135 Set_Scalar_Range (T, Scalar_Range (Base));
2136 Set_Is_Constrained (T);
2137
2138 Check_Restriction (No_Fixed_Point, Def);
2139 end Analyze_Formal_Decimal_Fixed_Point_Type;
2140
2141 -------------------------------------------
2142 -- Analyze_Formal_Derived_Interface_Type --
2143 -------------------------------------------
2144
2145 procedure Analyze_Formal_Derived_Interface_Type
2146 (N : Node_Id;
2147 T : Entity_Id;
2148 Def : Node_Id)
2149 is
2150 Loc : constant Source_Ptr := Sloc (Def);
2151
2152 begin
2153 -- Rewrite as a type declaration of a derived type. This ensures that
2154 -- the interface list and primitive operations are properly captured.
2155
2156 Rewrite (N,
2157 Make_Full_Type_Declaration (Loc,
2158 Defining_Identifier => T,
2159 Type_Definition => Def));
2160 Analyze (N);
2161 Set_Is_Generic_Type (T);
2162 end Analyze_Formal_Derived_Interface_Type;
2163
2164 ---------------------------------
2165 -- Analyze_Formal_Derived_Type --
2166 ---------------------------------
2167
2168 procedure Analyze_Formal_Derived_Type
2169 (N : Node_Id;
2170 T : Entity_Id;
2171 Def : Node_Id)
2172 is
2173 Loc : constant Source_Ptr := Sloc (Def);
2174 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2175 New_N : Node_Id;
2176
2177 begin
2178 Set_Is_Generic_Type (T);
2179
2180 if Private_Present (Def) then
2181 New_N :=
2182 Make_Private_Extension_Declaration (Loc,
2183 Defining_Identifier => T,
2184 Discriminant_Specifications => Discriminant_Specifications (N),
2185 Unknown_Discriminants_Present => Unk_Disc,
2186 Subtype_Indication => Subtype_Mark (Def),
2187 Interface_List => Interface_List (Def));
2188
2189 Set_Abstract_Present (New_N, Abstract_Present (Def));
2190 Set_Limited_Present (New_N, Limited_Present (Def));
2191 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2192
2193 else
2194 New_N :=
2195 Make_Full_Type_Declaration (Loc,
2196 Defining_Identifier => T,
2197 Discriminant_Specifications =>
2198 Discriminant_Specifications (Parent (T)),
2199 Type_Definition =>
2200 Make_Derived_Type_Definition (Loc,
2201 Subtype_Indication => Subtype_Mark (Def)));
2202
2203 Set_Abstract_Present
2204 (Type_Definition (New_N), Abstract_Present (Def));
2205 Set_Limited_Present
2206 (Type_Definition (New_N), Limited_Present (Def));
2207 end if;
2208
2209 Rewrite (N, New_N);
2210 Analyze (N);
2211
2212 if Unk_Disc then
2213 if not Is_Composite_Type (T) then
2214 Error_Msg_N
2215 ("unknown discriminants not allowed for elementary types", N);
2216 else
2217 Set_Has_Unknown_Discriminants (T);
2218 Set_Is_Constrained (T, False);
2219 end if;
2220 end if;
2221
2222 -- If the parent type has a known size, so does the formal, which makes
2223 -- legal representation clauses that involve the formal.
2224
2225 Set_Size_Known_At_Compile_Time
2226 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2227 end Analyze_Formal_Derived_Type;
2228
2229 ----------------------------------
2230 -- Analyze_Formal_Discrete_Type --
2231 ----------------------------------
2232
2233 -- The operations defined for a discrete types are those of an enumeration
2234 -- type. The size is set to an arbitrary value, for use in analyzing the
2235 -- generic unit.
2236
2237 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2238 Loc : constant Source_Ptr := Sloc (Def);
2239 Lo : Node_Id;
2240 Hi : Node_Id;
2241
2242 Base : constant Entity_Id :=
2243 New_Internal_Entity
2244 (E_Floating_Point_Type, Current_Scope,
2245 Sloc (Defining_Identifier (Parent (Def))), 'G');
2246
2247 begin
2248 Enter_Name (T);
2249 Set_Ekind (T, E_Enumeration_Subtype);
2250 Set_Etype (T, Base);
2251 Init_Size (T, 8);
2252 Init_Alignment (T);
2253 Set_Is_Generic_Type (T);
2254 Set_Is_Constrained (T);
2255
2256 -- For semantic analysis, the bounds of the type must be set to some
2257 -- non-static value. The simplest is to create attribute nodes for those
2258 -- bounds, that refer to the type itself. These bounds are never
2259 -- analyzed but serve as place-holders.
2260
2261 Lo :=
2262 Make_Attribute_Reference (Loc,
2263 Attribute_Name => Name_First,
2264 Prefix => New_Occurrence_Of (T, Loc));
2265 Set_Etype (Lo, T);
2266
2267 Hi :=
2268 Make_Attribute_Reference (Loc,
2269 Attribute_Name => Name_Last,
2270 Prefix => New_Occurrence_Of (T, Loc));
2271 Set_Etype (Hi, T);
2272
2273 Set_Scalar_Range (T,
2274 Make_Range (Loc,
2275 Low_Bound => Lo,
2276 High_Bound => Hi));
2277
2278 Set_Ekind (Base, E_Enumeration_Type);
2279 Set_Etype (Base, Base);
2280 Init_Size (Base, 8);
2281 Init_Alignment (Base);
2282 Set_Is_Generic_Type (Base);
2283 Set_Scalar_Range (Base, Scalar_Range (T));
2284 Set_Parent (Base, Parent (Def));
2285 end Analyze_Formal_Discrete_Type;
2286
2287 ----------------------------------
2288 -- Analyze_Formal_Floating_Type --
2289 ---------------------------------
2290
2291 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2292 Base : constant Entity_Id :=
2293 New_Internal_Entity
2294 (E_Floating_Point_Type, Current_Scope,
2295 Sloc (Defining_Identifier (Parent (Def))), 'G');
2296
2297 begin
2298 -- The various semantic attributes are taken from the predefined type
2299 -- Float, just so that all of them are initialized. Their values are
2300 -- never used because no constant folding or expansion takes place in
2301 -- the generic itself.
2302
2303 Enter_Name (T);
2304 Set_Ekind (T, E_Floating_Point_Subtype);
2305 Set_Etype (T, Base);
2306 Set_Size_Info (T, (Standard_Float));
2307 Set_RM_Size (T, RM_Size (Standard_Float));
2308 Set_Digits_Value (T, Digits_Value (Standard_Float));
2309 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2310 Set_Is_Constrained (T);
2311
2312 Set_Is_Generic_Type (Base);
2313 Set_Etype (Base, Base);
2314 Set_Size_Info (Base, (Standard_Float));
2315 Set_RM_Size (Base, RM_Size (Standard_Float));
2316 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2317 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2318 Set_Parent (Base, Parent (Def));
2319
2320 Check_Restriction (No_Floating_Point, Def);
2321 end Analyze_Formal_Floating_Type;
2322
2323 -----------------------------------
2324 -- Analyze_Formal_Interface_Type;--
2325 -----------------------------------
2326
2327 procedure Analyze_Formal_Interface_Type
2328 (N : Node_Id;
2329 T : Entity_Id;
2330 Def : Node_Id)
2331 is
2332 Loc : constant Source_Ptr := Sloc (N);
2333 New_N : Node_Id;
2334
2335 begin
2336 New_N :=
2337 Make_Full_Type_Declaration (Loc,
2338 Defining_Identifier => T,
2339 Type_Definition => Def);
2340
2341 Rewrite (N, New_N);
2342 Analyze (N);
2343 Set_Is_Generic_Type (T);
2344 end Analyze_Formal_Interface_Type;
2345
2346 ---------------------------------
2347 -- Analyze_Formal_Modular_Type --
2348 ---------------------------------
2349
2350 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2351 begin
2352 -- Apart from their entity kind, generic modular types are treated like
2353 -- signed integer types, and have the same attributes.
2354
2355 Analyze_Formal_Signed_Integer_Type (T, Def);
2356 Set_Ekind (T, E_Modular_Integer_Subtype);
2357 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2358
2359 end Analyze_Formal_Modular_Type;
2360
2361 ---------------------------------------
2362 -- Analyze_Formal_Object_Declaration --
2363 ---------------------------------------
2364
2365 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2366 E : constant Node_Id := Default_Expression (N);
2367 Id : constant Node_Id := Defining_Identifier (N);
2368 K : Entity_Kind;
2369 T : Node_Id;
2370
2371 begin
2372 Enter_Name (Id);
2373
2374 -- Determine the mode of the formal object
2375
2376 if Out_Present (N) then
2377 K := E_Generic_In_Out_Parameter;
2378
2379 if not In_Present (N) then
2380 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2381 end if;
2382
2383 else
2384 K := E_Generic_In_Parameter;
2385 end if;
2386
2387 if Present (Subtype_Mark (N)) then
2388 Find_Type (Subtype_Mark (N));
2389 T := Entity (Subtype_Mark (N));
2390
2391 -- Verify that there is no redundant null exclusion
2392
2393 if Null_Exclusion_Present (N) then
2394 if not Is_Access_Type (T) then
2395 Error_Msg_N
2396 ("null exclusion can only apply to an access type", N);
2397
2398 elsif Can_Never_Be_Null (T) then
2399 Error_Msg_NE
2400 ("`NOT NULL` not allowed (& already excludes null)",
2401 N, T);
2402 end if;
2403 end if;
2404
2405 -- Ada 2005 (AI-423): Formal object with an access definition
2406
2407 else
2408 Check_Access_Definition (N);
2409 T := Access_Definition
2410 (Related_Nod => N,
2411 N => Access_Definition (N));
2412 end if;
2413
2414 if Ekind (T) = E_Incomplete_Type then
2415 declare
2416 Error_Node : Node_Id;
2417
2418 begin
2419 if Present (Subtype_Mark (N)) then
2420 Error_Node := Subtype_Mark (N);
2421 else
2422 Check_Access_Definition (N);
2423 Error_Node := Access_Definition (N);
2424 end if;
2425
2426 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2427 end;
2428 end if;
2429
2430 if K = E_Generic_In_Parameter then
2431
2432 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2433
2434 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2435 Error_Msg_N
2436 ("generic formal of mode IN must not be of limited type", N);
2437 Explain_Limited_Type (T, N);
2438 end if;
2439
2440 if Is_Abstract_Type (T) then
2441 Error_Msg_N
2442 ("generic formal of mode IN must not be of abstract type", N);
2443 end if;
2444
2445 if Present (E) then
2446 Preanalyze_Spec_Expression (E, T);
2447
2448 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2449 Error_Msg_N
2450 ("initialization not allowed for limited types", E);
2451 Explain_Limited_Type (T, E);
2452 end if;
2453 end if;
2454
2455 Set_Ekind (Id, K);
2456 Set_Etype (Id, T);
2457
2458 -- Case of generic IN OUT parameter
2459
2460 else
2461 -- If the formal has an unconstrained type, construct its actual
2462 -- subtype, as is done for subprogram formals. In this fashion, all
2463 -- its uses can refer to specific bounds.
2464
2465 Set_Ekind (Id, K);
2466 Set_Etype (Id, T);
2467
2468 if (Is_Array_Type (T)
2469 and then not Is_Constrained (T))
2470 or else
2471 (Ekind (T) = E_Record_Type
2472 and then Has_Discriminants (T))
2473 then
2474 declare
2475 Non_Freezing_Ref : constant Node_Id :=
2476 New_Occurrence_Of (Id, Sloc (Id));
2477 Decl : Node_Id;
2478
2479 begin
2480 -- Make sure the actual subtype doesn't generate bogus freezing
2481
2482 Set_Must_Not_Freeze (Non_Freezing_Ref);
2483 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2484 Insert_Before_And_Analyze (N, Decl);
2485 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2486 end;
2487 else
2488 Set_Actual_Subtype (Id, T);
2489 end if;
2490
2491 if Present (E) then
2492 Error_Msg_N
2493 ("initialization not allowed for `IN OUT` formals", N);
2494 end if;
2495 end if;
2496
2497 if Has_Aspects (N) then
2498 Analyze_Aspect_Specifications (N, Id);
2499 end if;
2500 end Analyze_Formal_Object_Declaration;
2501
2502 ----------------------------------------------
2503 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2504 ----------------------------------------------
2505
2506 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2507 (T : Entity_Id;
2508 Def : Node_Id)
2509 is
2510 Loc : constant Source_Ptr := Sloc (Def);
2511 Base : constant Entity_Id :=
2512 New_Internal_Entity
2513 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2514 Sloc (Defining_Identifier (Parent (Def))), 'G');
2515
2516 begin
2517 -- The semantic attributes are set for completeness only, their values
2518 -- will never be used, since all properties of the type are non-static.
2519
2520 Enter_Name (T);
2521 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2522 Set_Etype (T, Base);
2523 Set_Size_Info (T, Standard_Integer);
2524 Set_RM_Size (T, RM_Size (Standard_Integer));
2525 Set_Small_Value (T, Ureal_1);
2526 Set_Delta_Value (T, Ureal_1);
2527 Set_Scalar_Range (T,
2528 Make_Range (Loc,
2529 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2530 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2531 Set_Is_Constrained (T);
2532
2533 Set_Is_Generic_Type (Base);
2534 Set_Etype (Base, Base);
2535 Set_Size_Info (Base, Standard_Integer);
2536 Set_RM_Size (Base, RM_Size (Standard_Integer));
2537 Set_Small_Value (Base, Ureal_1);
2538 Set_Delta_Value (Base, Ureal_1);
2539 Set_Scalar_Range (Base, Scalar_Range (T));
2540 Set_Parent (Base, Parent (Def));
2541
2542 Check_Restriction (No_Fixed_Point, Def);
2543 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2544
2545 ----------------------------------------
2546 -- Analyze_Formal_Package_Declaration --
2547 ----------------------------------------
2548
2549 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2550 Loc : constant Source_Ptr := Sloc (N);
2551 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2552 Formal : Entity_Id;
2553 Gen_Id : constant Node_Id := Name (N);
2554 Gen_Decl : Node_Id;
2555 Gen_Unit : Entity_Id;
2556 New_N : Node_Id;
2557 Parent_Installed : Boolean := False;
2558 Renaming : Node_Id;
2559 Parent_Instance : Entity_Id;
2560 Renaming_In_Par : Entity_Id;
2561 Associations : Boolean := True;
2562
2563 Vis_Prims_List : Elist_Id := No_Elist;
2564 -- List of primitives made temporarily visible in the instantiation
2565 -- to match the visibility of the formal type
2566
2567 function Build_Local_Package return Node_Id;
2568 -- The formal package is rewritten so that its parameters are replaced
2569 -- with corresponding declarations. For parameters with bona fide
2570 -- associations these declarations are created by Analyze_Associations
2571 -- as for a regular instantiation. For boxed parameters, we preserve
2572 -- the formal declarations and analyze them, in order to introduce
2573 -- entities of the right kind in the environment of the formal.
2574
2575 -------------------------
2576 -- Build_Local_Package --
2577 -------------------------
2578
2579 function Build_Local_Package return Node_Id is
2580 Decls : List_Id;
2581 Pack_Decl : Node_Id;
2582
2583 begin
2584 -- Within the formal, the name of the generic package is a renaming
2585 -- of the formal (as for a regular instantiation).
2586
2587 Pack_Decl :=
2588 Make_Package_Declaration (Loc,
2589 Specification =>
2590 Copy_Generic_Node
2591 (Specification (Original_Node (Gen_Decl)),
2592 Empty, Instantiating => True));
2593
2594 Renaming := Make_Package_Renaming_Declaration (Loc,
2595 Defining_Unit_Name =>
2596 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2597 Name => New_Occurrence_Of (Formal, Loc));
2598
2599 if Nkind (Gen_Id) = N_Identifier
2600 and then Chars (Gen_Id) = Chars (Pack_Id)
2601 then
2602 Error_Msg_NE
2603 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2604 end if;
2605
2606 -- If the formal is declared with a box, or with an others choice,
2607 -- create corresponding declarations for all entities in the formal
2608 -- part, so that names with the proper types are available in the
2609 -- specification of the formal package.
2610
2611 -- On the other hand, if there are no associations, then all the
2612 -- formals must have defaults, and this will be checked by the
2613 -- call to Analyze_Associations.
2614
2615 if Box_Present (N)
2616 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2617 then
2618 declare
2619 Formal_Decl : Node_Id;
2620
2621 begin
2622 -- TBA : for a formal package, need to recurse ???
2623
2624 Decls := New_List;
2625 Formal_Decl :=
2626 First
2627 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2628 while Present (Formal_Decl) loop
2629 Append_To
2630 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2631 Next (Formal_Decl);
2632 end loop;
2633 end;
2634
2635 -- If generic associations are present, use Analyze_Associations to
2636 -- create the proper renaming declarations.
2637
2638 else
2639 declare
2640 Act_Tree : constant Node_Id :=
2641 Copy_Generic_Node
2642 (Original_Node (Gen_Decl), Empty,
2643 Instantiating => True);
2644
2645 begin
2646 Generic_Renamings.Set_Last (0);
2647 Generic_Renamings_HTable.Reset;
2648 Instantiation_Node := N;
2649
2650 Decls :=
2651 Analyze_Associations
2652 (I_Node => Original_Node (N),
2653 Formals => Generic_Formal_Declarations (Act_Tree),
2654 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2655
2656 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2657 end;
2658 end if;
2659
2660 Append (Renaming, To => Decls);
2661
2662 -- Add generated declarations ahead of local declarations in
2663 -- the package.
2664
2665 if No (Visible_Declarations (Specification (Pack_Decl))) then
2666 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2667 else
2668 Insert_List_Before
2669 (First (Visible_Declarations (Specification (Pack_Decl))),
2670 Decls);
2671 end if;
2672
2673 return Pack_Decl;
2674 end Build_Local_Package;
2675
2676 -- Start of processing for Analyze_Formal_Package_Declaration
2677
2678 begin
2679 Check_Text_IO_Special_Unit (Gen_Id);
2680
2681 Init_Env;
2682 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2683 Gen_Unit := Entity (Gen_Id);
2684
2685 -- Check for a formal package that is a package renaming
2686
2687 if Present (Renamed_Object (Gen_Unit)) then
2688
2689 -- Indicate that unit is used, before replacing it with renamed
2690 -- entity for use below.
2691
2692 if In_Extended_Main_Source_Unit (N) then
2693 Set_Is_Instantiated (Gen_Unit);
2694 Generate_Reference (Gen_Unit, N);
2695 end if;
2696
2697 Gen_Unit := Renamed_Object (Gen_Unit);
2698 end if;
2699
2700 if Ekind (Gen_Unit) /= E_Generic_Package then
2701 Error_Msg_N ("expect generic package name", Gen_Id);
2702 Restore_Env;
2703 goto Leave;
2704
2705 elsif Gen_Unit = Current_Scope then
2706 Error_Msg_N
2707 ("generic package cannot be used as a formal package of itself",
2708 Gen_Id);
2709 Restore_Env;
2710 goto Leave;
2711
2712 elsif In_Open_Scopes (Gen_Unit) then
2713 if Is_Compilation_Unit (Gen_Unit)
2714 and then Is_Child_Unit (Current_Scope)
2715 then
2716 -- Special-case the error when the formal is a parent, and
2717 -- continue analysis to minimize cascaded errors.
2718
2719 Error_Msg_N
2720 ("generic parent cannot be used as formal package "
2721 & "of a child unit",
2722 Gen_Id);
2723
2724 else
2725 Error_Msg_N
2726 ("generic package cannot be used as a formal package "
2727 & "within itself",
2728 Gen_Id);
2729 Restore_Env;
2730 goto Leave;
2731 end if;
2732 end if;
2733
2734 -- Check that name of formal package does not hide name of generic,
2735 -- or its leading prefix. This check must be done separately because
2736 -- the name of the generic has already been analyzed.
2737
2738 declare
2739 Gen_Name : Entity_Id;
2740
2741 begin
2742 Gen_Name := Gen_Id;
2743 while Nkind (Gen_Name) = N_Expanded_Name loop
2744 Gen_Name := Prefix (Gen_Name);
2745 end loop;
2746
2747 if Chars (Gen_Name) = Chars (Pack_Id) then
2748 Error_Msg_NE
2749 ("& is hidden within declaration of formal package",
2750 Gen_Id, Gen_Name);
2751 end if;
2752 end;
2753
2754 if Box_Present (N)
2755 or else No (Generic_Associations (N))
2756 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2757 then
2758 Associations := False;
2759 end if;
2760
2761 -- If there are no generic associations, the generic parameters appear
2762 -- as local entities and are instantiated like them. We copy the generic
2763 -- package declaration as if it were an instantiation, and analyze it
2764 -- like a regular package, except that we treat the formals as
2765 -- additional visible components.
2766
2767 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2768
2769 if In_Extended_Main_Source_Unit (N) then
2770 Set_Is_Instantiated (Gen_Unit);
2771 Generate_Reference (Gen_Unit, N);
2772 end if;
2773
2774 Formal := New_Copy (Pack_Id);
2775 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2776
2777 begin
2778 -- Make local generic without formals. The formals will be replaced
2779 -- with internal declarations.
2780
2781 New_N := Build_Local_Package;
2782
2783 -- If there are errors in the parameter list, Analyze_Associations
2784 -- raises Instantiation_Error. Patch the declaration to prevent
2785 -- further exception propagation.
2786
2787 exception
2788 when Instantiation_Error =>
2789
2790 Enter_Name (Formal);
2791 Set_Ekind (Formal, E_Variable);
2792 Set_Etype (Formal, Any_Type);
2793 Restore_Hidden_Primitives (Vis_Prims_List);
2794
2795 if Parent_Installed then
2796 Remove_Parent;
2797 end if;
2798
2799 goto Leave;
2800 end;
2801
2802 Rewrite (N, New_N);
2803 Set_Defining_Unit_Name (Specification (New_N), Formal);
2804 Set_Generic_Parent (Specification (N), Gen_Unit);
2805 Set_Instance_Env (Gen_Unit, Formal);
2806 Set_Is_Generic_Instance (Formal);
2807
2808 Enter_Name (Formal);
2809 Set_Ekind (Formal, E_Package);
2810 Set_Etype (Formal, Standard_Void_Type);
2811 Set_Inner_Instances (Formal, New_Elmt_List);
2812 Push_Scope (Formal);
2813
2814 if Is_Child_Unit (Gen_Unit)
2815 and then Parent_Installed
2816 then
2817 -- Similarly, we have to make the name of the formal visible in the
2818 -- parent instance, to resolve properly fully qualified names that
2819 -- may appear in the generic unit. The parent instance has been
2820 -- placed on the scope stack ahead of the current scope.
2821
2822 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2823
2824 Renaming_In_Par :=
2825 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2826 Set_Ekind (Renaming_In_Par, E_Package);
2827 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2828 Set_Scope (Renaming_In_Par, Parent_Instance);
2829 Set_Parent (Renaming_In_Par, Parent (Formal));
2830 Set_Renamed_Object (Renaming_In_Par, Formal);
2831 Append_Entity (Renaming_In_Par, Parent_Instance);
2832 end if;
2833
2834 Analyze (Specification (N));
2835
2836 -- The formals for which associations are provided are not visible
2837 -- outside of the formal package. The others are still declared by a
2838 -- formal parameter declaration.
2839
2840 -- If there are no associations, the only local entity to hide is the
2841 -- generated package renaming itself.
2842
2843 declare
2844 E : Entity_Id;
2845
2846 begin
2847 E := First_Entity (Formal);
2848 while Present (E) loop
2849 if Associations
2850 and then not Is_Generic_Formal (E)
2851 then
2852 Set_Is_Hidden (E);
2853 end if;
2854
2855 if Ekind (E) = E_Package
2856 and then Renamed_Entity (E) = Formal
2857 then
2858 Set_Is_Hidden (E);
2859 exit;
2860 end if;
2861
2862 Next_Entity (E);
2863 end loop;
2864 end;
2865
2866 End_Package_Scope (Formal);
2867 Restore_Hidden_Primitives (Vis_Prims_List);
2868
2869 if Parent_Installed then
2870 Remove_Parent;
2871 end if;
2872
2873 Restore_Env;
2874
2875 -- Inside the generic unit, the formal package is a regular package, but
2876 -- no body is needed for it. Note that after instantiation, the defining
2877 -- unit name we need is in the new tree and not in the original (see
2878 -- Package_Instantiation). A generic formal package is an instance, and
2879 -- can be used as an actual for an inner instance.
2880
2881 Set_Has_Completion (Formal, True);
2882
2883 -- Add semantic information to the original defining identifier.
2884 -- for ASIS use.
2885
2886 Set_Ekind (Pack_Id, E_Package);
2887 Set_Etype (Pack_Id, Standard_Void_Type);
2888 Set_Scope (Pack_Id, Scope (Formal));
2889 Set_Has_Completion (Pack_Id, True);
2890
2891 <<Leave>>
2892 if Has_Aspects (N) then
2893 Analyze_Aspect_Specifications (N, Pack_Id);
2894 end if;
2895 end Analyze_Formal_Package_Declaration;
2896
2897 ---------------------------------
2898 -- Analyze_Formal_Private_Type --
2899 ---------------------------------
2900
2901 procedure Analyze_Formal_Private_Type
2902 (N : Node_Id;
2903 T : Entity_Id;
2904 Def : Node_Id)
2905 is
2906 begin
2907 New_Private_Type (N, T, Def);
2908
2909 -- Set the size to an arbitrary but legal value
2910
2911 Set_Size_Info (T, Standard_Integer);
2912 Set_RM_Size (T, RM_Size (Standard_Integer));
2913 end Analyze_Formal_Private_Type;
2914
2915 ------------------------------------
2916 -- Analyze_Formal_Incomplete_Type --
2917 ------------------------------------
2918
2919 procedure Analyze_Formal_Incomplete_Type
2920 (T : Entity_Id;
2921 Def : Node_Id)
2922 is
2923 begin
2924 Enter_Name (T);
2925 Set_Ekind (T, E_Incomplete_Type);
2926 Set_Etype (T, T);
2927 Set_Private_Dependents (T, New_Elmt_List);
2928
2929 if Tagged_Present (Def) then
2930 Set_Is_Tagged_Type (T);
2931 Make_Class_Wide_Type (T);
2932 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2933 end if;
2934 end Analyze_Formal_Incomplete_Type;
2935
2936 ----------------------------------------
2937 -- Analyze_Formal_Signed_Integer_Type --
2938 ----------------------------------------
2939
2940 procedure Analyze_Formal_Signed_Integer_Type
2941 (T : Entity_Id;
2942 Def : Node_Id)
2943 is
2944 Base : constant Entity_Id :=
2945 New_Internal_Entity
2946 (E_Signed_Integer_Type,
2947 Current_Scope,
2948 Sloc (Defining_Identifier (Parent (Def))), 'G');
2949
2950 begin
2951 Enter_Name (T);
2952
2953 Set_Ekind (T, E_Signed_Integer_Subtype);
2954 Set_Etype (T, Base);
2955 Set_Size_Info (T, Standard_Integer);
2956 Set_RM_Size (T, RM_Size (Standard_Integer));
2957 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2958 Set_Is_Constrained (T);
2959
2960 Set_Is_Generic_Type (Base);
2961 Set_Size_Info (Base, Standard_Integer);
2962 Set_RM_Size (Base, RM_Size (Standard_Integer));
2963 Set_Etype (Base, Base);
2964 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2965 Set_Parent (Base, Parent (Def));
2966 end Analyze_Formal_Signed_Integer_Type;
2967
2968 -------------------------------------------
2969 -- Analyze_Formal_Subprogram_Declaration --
2970 -------------------------------------------
2971
2972 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2973 Spec : constant Node_Id := Specification (N);
2974 Def : constant Node_Id := Default_Name (N);
2975 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2976 Subp : Entity_Id;
2977
2978 begin
2979 if Nam = Error then
2980 return;
2981 end if;
2982
2983 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2984 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2985 goto Leave;
2986 end if;
2987
2988 Analyze_Subprogram_Declaration (N);
2989 Set_Is_Formal_Subprogram (Nam);
2990 Set_Has_Completion (Nam);
2991
2992 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2993 Set_Is_Abstract_Subprogram (Nam);
2994 Set_Is_Dispatching_Operation (Nam);
2995
2996 declare
2997 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2998 begin
2999 if No (Ctrl_Type) then
3000 Error_Msg_N
3001 ("abstract formal subprogram must have a controlling type",
3002 N);
3003
3004 elsif Ada_Version >= Ada_2012
3005 and then Is_Incomplete_Type (Ctrl_Type)
3006 then
3007 Error_Msg_NE
3008 ("controlling type of abstract formal subprogram cannot " &
3009 "be incomplete type", N, Ctrl_Type);
3010
3011 else
3012 Check_Controlling_Formals (Ctrl_Type, Nam);
3013 end if;
3014 end;
3015 end if;
3016
3017 -- Default name is resolved at the point of instantiation
3018
3019 if Box_Present (N) then
3020 null;
3021
3022 -- Else default is bound at the point of generic declaration
3023
3024 elsif Present (Def) then
3025 if Nkind (Def) = N_Operator_Symbol then
3026 Find_Direct_Name (Def);
3027
3028 elsif Nkind (Def) /= N_Attribute_Reference then
3029 Analyze (Def);
3030
3031 else
3032 -- For an attribute reference, analyze the prefix and verify
3033 -- that it has the proper profile for the subprogram.
3034
3035 Analyze (Prefix (Def));
3036 Valid_Default_Attribute (Nam, Def);
3037 goto Leave;
3038 end if;
3039
3040 -- Default name may be overloaded, in which case the interpretation
3041 -- with the correct profile must be selected, as for a renaming.
3042 -- If the definition is an indexed component, it must denote a
3043 -- member of an entry family. If it is a selected component, it
3044 -- can be a protected operation.
3045
3046 if Etype (Def) = Any_Type then
3047 goto Leave;
3048
3049 elsif Nkind (Def) = N_Selected_Component then
3050 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3051 Error_Msg_N ("expect valid subprogram name as default", Def);
3052 end if;
3053
3054 elsif Nkind (Def) = N_Indexed_Component then
3055 if Is_Entity_Name (Prefix (Def)) then
3056 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3057 Error_Msg_N ("expect valid subprogram name as default", Def);
3058 end if;
3059
3060 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3061 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3062 E_Entry_Family
3063 then
3064 Error_Msg_N ("expect valid subprogram name as default", Def);
3065 end if;
3066
3067 else
3068 Error_Msg_N ("expect valid subprogram name as default", Def);
3069 goto Leave;
3070 end if;
3071
3072 elsif Nkind (Def) = N_Character_Literal then
3073
3074 -- Needs some type checks: subprogram should be parameterless???
3075
3076 Resolve (Def, (Etype (Nam)));
3077
3078 elsif not Is_Entity_Name (Def)
3079 or else not Is_Overloadable (Entity (Def))
3080 then
3081 Error_Msg_N ("expect valid subprogram name as default", Def);
3082 goto Leave;
3083
3084 elsif not Is_Overloaded (Def) then
3085 Subp := Entity (Def);
3086
3087 if Subp = Nam then
3088 Error_Msg_N ("premature usage of formal subprogram", Def);
3089
3090 elsif not Entity_Matches_Spec (Subp, Nam) then
3091 Error_Msg_N ("no visible entity matches specification", Def);
3092 end if;
3093
3094 -- More than one interpretation, so disambiguate as for a renaming
3095
3096 else
3097 declare
3098 I : Interp_Index;
3099 I1 : Interp_Index := 0;
3100 It : Interp;
3101 It1 : Interp;
3102
3103 begin
3104 Subp := Any_Id;
3105 Get_First_Interp (Def, I, It);
3106 while Present (It.Nam) loop
3107 if Entity_Matches_Spec (It.Nam, Nam) then
3108 if Subp /= Any_Id then
3109 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3110
3111 if It1 = No_Interp then
3112 Error_Msg_N ("ambiguous default subprogram", Def);
3113 else
3114 Subp := It1.Nam;
3115 end if;
3116
3117 exit;
3118
3119 else
3120 I1 := I;
3121 Subp := It.Nam;
3122 end if;
3123 end if;
3124
3125 Get_Next_Interp (I, It);
3126 end loop;
3127 end;
3128
3129 if Subp /= Any_Id then
3130
3131 -- Subprogram found, generate reference to it
3132
3133 Set_Entity (Def, Subp);
3134 Generate_Reference (Subp, Def);
3135
3136 if Subp = Nam then
3137 Error_Msg_N ("premature usage of formal subprogram", Def);
3138
3139 elsif Ekind (Subp) /= E_Operator then
3140 Check_Mode_Conformant (Subp, Nam);
3141 end if;
3142
3143 else
3144 Error_Msg_N ("no visible subprogram matches specification", N);
3145 end if;
3146 end if;
3147 end if;
3148
3149 <<Leave>>
3150 if Has_Aspects (N) then
3151 Analyze_Aspect_Specifications (N, Nam);
3152 end if;
3153
3154 end Analyze_Formal_Subprogram_Declaration;
3155
3156 -------------------------------------
3157 -- Analyze_Formal_Type_Declaration --
3158 -------------------------------------
3159
3160 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3161 Def : constant Node_Id := Formal_Type_Definition (N);
3162 T : Entity_Id;
3163
3164 begin
3165 T := Defining_Identifier (N);
3166
3167 if Present (Discriminant_Specifications (N))
3168 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3169 then
3170 Error_Msg_N
3171 ("discriminants not allowed for this formal type", T);
3172 end if;
3173
3174 -- Enter the new name, and branch to specific routine
3175
3176 case Nkind (Def) is
3177 when N_Formal_Private_Type_Definition =>
3178 Analyze_Formal_Private_Type (N, T, Def);
3179
3180 when N_Formal_Derived_Type_Definition =>
3181 Analyze_Formal_Derived_Type (N, T, Def);
3182
3183 when N_Formal_Incomplete_Type_Definition =>
3184 Analyze_Formal_Incomplete_Type (T, Def);
3185
3186 when N_Formal_Discrete_Type_Definition =>
3187 Analyze_Formal_Discrete_Type (T, Def);
3188
3189 when N_Formal_Signed_Integer_Type_Definition =>
3190 Analyze_Formal_Signed_Integer_Type (T, Def);
3191
3192 when N_Formal_Modular_Type_Definition =>
3193 Analyze_Formal_Modular_Type (T, Def);
3194
3195 when N_Formal_Floating_Point_Definition =>
3196 Analyze_Formal_Floating_Type (T, Def);
3197
3198 when N_Formal_Ordinary_Fixed_Point_Definition =>
3199 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3200
3201 when N_Formal_Decimal_Fixed_Point_Definition =>
3202 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3203
3204 when N_Array_Type_Definition =>
3205 Analyze_Formal_Array_Type (T, Def);
3206
3207 when N_Access_To_Object_Definition |
3208 N_Access_Function_Definition |
3209 N_Access_Procedure_Definition =>
3210 Analyze_Generic_Access_Type (T, Def);
3211
3212 -- Ada 2005: a interface declaration is encoded as an abstract
3213 -- record declaration or a abstract type derivation.
3214
3215 when N_Record_Definition =>
3216 Analyze_Formal_Interface_Type (N, T, Def);
3217
3218 when N_Derived_Type_Definition =>
3219 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3220
3221 when N_Error =>
3222 null;
3223
3224 when others =>
3225 raise Program_Error;
3226
3227 end case;
3228
3229 Set_Is_Generic_Type (T);
3230
3231 if Has_Aspects (N) then
3232 Analyze_Aspect_Specifications (N, T);
3233 end if;
3234 end Analyze_Formal_Type_Declaration;
3235
3236 ------------------------------------
3237 -- Analyze_Function_Instantiation --
3238 ------------------------------------
3239
3240 procedure Analyze_Function_Instantiation (N : Node_Id) is
3241 begin
3242 Analyze_Subprogram_Instantiation (N, E_Function);
3243 end Analyze_Function_Instantiation;
3244
3245 ---------------------------------
3246 -- Analyze_Generic_Access_Type --
3247 ---------------------------------
3248
3249 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3250 begin
3251 Enter_Name (T);
3252
3253 if Nkind (Def) = N_Access_To_Object_Definition then
3254 Access_Type_Declaration (T, Def);
3255
3256 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3257 and then No (Full_View (Designated_Type (T)))
3258 and then not Is_Generic_Type (Designated_Type (T))
3259 then
3260 Error_Msg_N ("premature usage of incomplete type", Def);
3261
3262 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3263 Error_Msg_N
3264 ("only a subtype mark is allowed in a formal", Def);
3265 end if;
3266
3267 else
3268 Access_Subprogram_Declaration (T, Def);
3269 end if;
3270 end Analyze_Generic_Access_Type;
3271
3272 ---------------------------------
3273 -- Analyze_Generic_Formal_Part --
3274 ---------------------------------
3275
3276 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3277 Gen_Parm_Decl : Node_Id;
3278
3279 begin
3280 -- The generic formals are processed in the scope of the generic unit,
3281 -- where they are immediately visible. The scope is installed by the
3282 -- caller.
3283
3284 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3285
3286 while Present (Gen_Parm_Decl) loop
3287 Analyze (Gen_Parm_Decl);
3288 Next (Gen_Parm_Decl);
3289 end loop;
3290
3291 Generate_Reference_To_Generic_Formals (Current_Scope);
3292 end Analyze_Generic_Formal_Part;
3293
3294 ------------------------------------------
3295 -- Analyze_Generic_Package_Declaration --
3296 ------------------------------------------
3297
3298 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3299 Loc : constant Source_Ptr := Sloc (N);
3300 Id : Entity_Id;
3301 New_N : Node_Id;
3302 Save_Parent : Node_Id;
3303 Renaming : Node_Id;
3304 Decls : constant List_Id :=
3305 Visible_Declarations (Specification (N));
3306 Decl : Node_Id;
3307
3308 begin
3309 Check_SPARK_05_Restriction ("generic is not allowed", N);
3310
3311 -- We introduce a renaming of the enclosing package, to have a usable
3312 -- entity as the prefix of an expanded name for a local entity of the
3313 -- form Par.P.Q, where P is the generic package. This is because a local
3314 -- entity named P may hide it, so that the usual visibility rules in
3315 -- the instance will not resolve properly.
3316
3317 Renaming :=
3318 Make_Package_Renaming_Declaration (Loc,
3319 Defining_Unit_Name =>
3320 Make_Defining_Identifier (Loc,
3321 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3322 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
3323
3324 if Present (Decls) then
3325 Decl := First (Decls);
3326 while Present (Decl)
3327 and then Nkind (Decl) = N_Pragma
3328 loop
3329 Next (Decl);
3330 end loop;
3331
3332 if Present (Decl) then
3333 Insert_Before (Decl, Renaming);
3334 else
3335 Append (Renaming, Visible_Declarations (Specification (N)));
3336 end if;
3337
3338 else
3339 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3340 end if;
3341
3342 -- Create copy of generic unit, and save for instantiation. If the unit
3343 -- is a child unit, do not copy the specifications for the parent, which
3344 -- are not part of the generic tree.
3345
3346 Save_Parent := Parent_Spec (N);
3347 Set_Parent_Spec (N, Empty);
3348
3349 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3350 Set_Parent_Spec (New_N, Save_Parent);
3351 Rewrite (N, New_N);
3352
3353 -- Once the contents of the generic copy and the template are swapped,
3354 -- do the same for their respective aspect specifications.
3355
3356 Exchange_Aspects (N, New_N);
3357 Id := Defining_Entity (N);
3358 Generate_Definition (Id);
3359
3360 -- Expansion is not applied to generic units
3361
3362 Start_Generic;
3363
3364 Enter_Name (Id);
3365 Set_Ekind (Id, E_Generic_Package);
3366 Set_Etype (Id, Standard_Void_Type);
3367 Set_Contract (Id, Make_Contract (Sloc (Id)));
3368
3369 -- Analyze aspects now, so that generated pragmas appear in the
3370 -- declarations before building and analyzing the generic copy.
3371
3372 if Has_Aspects (N) then
3373 Analyze_Aspect_Specifications (N, Id);
3374 end if;
3375
3376 Push_Scope (Id);
3377 Enter_Generic_Scope (Id);
3378 Set_Inner_Instances (Id, New_Elmt_List);
3379
3380 Set_Categorization_From_Pragmas (N);
3381 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3382
3383 -- Link the declaration of the generic homonym in the generic copy to
3384 -- the package it renames, so that it is always resolved properly.
3385
3386 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3387 Set_Entity (Associated_Node (Name (Renaming)), Id);
3388
3389 -- For a library unit, we have reconstructed the entity for the unit,
3390 -- and must reset it in the library tables.
3391
3392 if Nkind (Parent (N)) = N_Compilation_Unit then
3393 Set_Cunit_Entity (Current_Sem_Unit, Id);
3394 end if;
3395
3396 Analyze_Generic_Formal_Part (N);
3397
3398 -- After processing the generic formals, analysis proceeds as for a
3399 -- non-generic package.
3400
3401 Analyze (Specification (N));
3402
3403 Validate_Categorization_Dependency (N, Id);
3404
3405 End_Generic;
3406
3407 End_Package_Scope (Id);
3408 Exit_Generic_Scope (Id);
3409
3410 if Nkind (Parent (N)) /= N_Compilation_Unit then
3411 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3412 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3413 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3414
3415 else
3416 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3417 Validate_RT_RAT_Component (N);
3418
3419 -- If this is a spec without a body, check that generic parameters
3420 -- are referenced.
3421
3422 if not Body_Required (Parent (N)) then
3423 Check_References (Id);
3424 end if;
3425 end if;
3426 end Analyze_Generic_Package_Declaration;
3427
3428 --------------------------------------------
3429 -- Analyze_Generic_Subprogram_Declaration --
3430 --------------------------------------------
3431
3432 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3433 Spec : Node_Id;
3434 Id : Entity_Id;
3435 Formals : List_Id;
3436 New_N : Node_Id;
3437 Result_Type : Entity_Id;
3438 Save_Parent : Node_Id;
3439 Typ : Entity_Id;
3440
3441 begin
3442 Check_SPARK_05_Restriction ("generic is not allowed", N);
3443
3444 -- Create copy of generic unit, and save for instantiation. If the unit
3445 -- is a child unit, do not copy the specifications for the parent, which
3446 -- are not part of the generic tree.
3447
3448 Save_Parent := Parent_Spec (N);
3449 Set_Parent_Spec (N, Empty);
3450
3451 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3452 Set_Parent_Spec (New_N, Save_Parent);
3453 Rewrite (N, New_N);
3454
3455 -- Once the contents of the generic copy and the template are swapped,
3456 -- do the same for their respective aspect specifications.
3457
3458 Exchange_Aspects (N, New_N);
3459
3460 Spec := Specification (N);
3461 Id := Defining_Entity (Spec);
3462 Generate_Definition (Id);
3463 Set_Contract (Id, Make_Contract (Sloc (Id)));
3464
3465 if Nkind (Id) = N_Defining_Operator_Symbol then
3466 Error_Msg_N
3467 ("operator symbol not allowed for generic subprogram", Id);
3468 end if;
3469
3470 Start_Generic;
3471
3472 Enter_Name (Id);
3473 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3474
3475 -- Analyze the aspects of the generic copy to ensure that all generated
3476 -- pragmas (if any) perform their semantic effects.
3477
3478 if Has_Aspects (N) then
3479 Analyze_Aspect_Specifications (N, Id);
3480 end if;
3481
3482 Push_Scope (Id);
3483 Enter_Generic_Scope (Id);
3484 Set_Inner_Instances (Id, New_Elmt_List);
3485 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3486
3487 Analyze_Generic_Formal_Part (N);
3488
3489 Formals := Parameter_Specifications (Spec);
3490
3491 if Present (Formals) then
3492 Process_Formals (Formals, Spec);
3493 end if;
3494
3495 if Nkind (Spec) = N_Function_Specification then
3496 Set_Ekind (Id, E_Generic_Function);
3497
3498 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3499 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3500 Set_Etype (Id, Result_Type);
3501
3502 -- Check restriction imposed by AI05-073: a generic function
3503 -- cannot return an abstract type or an access to such.
3504
3505 -- This is a binding interpretation should it apply to earlier
3506 -- versions of Ada as well as Ada 2012???
3507
3508 if Is_Abstract_Type (Designated_Type (Result_Type))
3509 and then Ada_Version >= Ada_2012
3510 then
3511 Error_Msg_N ("generic function cannot have an access result"
3512 & " that designates an abstract type", Spec);
3513 end if;
3514
3515 else
3516 Find_Type (Result_Definition (Spec));
3517 Typ := Entity (Result_Definition (Spec));
3518
3519 if Is_Abstract_Type (Typ)
3520 and then Ada_Version >= Ada_2012
3521 then
3522 Error_Msg_N
3523 ("generic function cannot have abstract result type", Spec);
3524 end if;
3525
3526 -- If a null exclusion is imposed on the result type, then create
3527 -- a null-excluding itype (an access subtype) and use it as the
3528 -- function's Etype.
3529
3530 if Is_Access_Type (Typ)
3531 and then Null_Exclusion_Present (Spec)
3532 then
3533 Set_Etype (Id,
3534 Create_Null_Excluding_Itype
3535 (T => Typ,
3536 Related_Nod => Spec,
3537 Scope_Id => Defining_Unit_Name (Spec)));
3538 else
3539 Set_Etype (Id, Typ);
3540 end if;
3541 end if;
3542
3543 else
3544 Set_Ekind (Id, E_Generic_Procedure);
3545 Set_Etype (Id, Standard_Void_Type);
3546 end if;
3547
3548 -- For a library unit, we have reconstructed the entity for the unit,
3549 -- and must reset it in the library tables. We also make sure that
3550 -- Body_Required is set properly in the original compilation unit node.
3551
3552 if Nkind (Parent (N)) = N_Compilation_Unit then
3553 Set_Cunit_Entity (Current_Sem_Unit, Id);
3554 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3555 end if;
3556
3557 Set_Categorization_From_Pragmas (N);
3558 Validate_Categorization_Dependency (N, Id);
3559
3560 Save_Global_References (Original_Node (N));
3561
3562 -- For ASIS purposes, convert any postcondition, precondition pragmas
3563 -- into aspects, if N is not a compilation unit by itself, in order to
3564 -- enable the analysis of expressions inside the corresponding PPC
3565 -- pragmas.
3566
3567 if ASIS_Mode and then Is_List_Member (N) then
3568 Make_Aspect_For_PPC_In_Gen_Sub_Decl (N);
3569 end if;
3570
3571 End_Generic;
3572 End_Scope;
3573 Exit_Generic_Scope (Id);
3574 Generate_Reference_To_Formals (Id);
3575
3576 List_Inherited_Pre_Post_Aspects (Id);
3577 end Analyze_Generic_Subprogram_Declaration;
3578
3579 -----------------------------------
3580 -- Analyze_Package_Instantiation --
3581 -----------------------------------
3582
3583 procedure Analyze_Package_Instantiation (N : Node_Id) is
3584 Loc : constant Source_Ptr := Sloc (N);
3585 Gen_Id : constant Node_Id := Name (N);
3586
3587 Act_Decl : Node_Id;
3588 Act_Decl_Name : Node_Id;
3589 Act_Decl_Id : Entity_Id;
3590 Act_Spec : Node_Id;
3591 Act_Tree : Node_Id;
3592
3593 Gen_Decl : Node_Id;
3594 Gen_Unit : Entity_Id;
3595
3596 Is_Actual_Pack : constant Boolean :=
3597 Is_Internal (Defining_Entity (N));
3598
3599 Env_Installed : Boolean := False;
3600 Parent_Installed : Boolean := False;
3601 Renaming_List : List_Id;
3602 Unit_Renaming : Node_Id;
3603 Needs_Body : Boolean;
3604 Inline_Now : Boolean := False;
3605
3606 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3607 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3608
3609 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3610 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3611 -- Save the SPARK_Mode-related data for restore on exit
3612
3613 Save_Style_Check : constant Boolean := Style_Check;
3614 -- Save style check mode for restore on exit
3615
3616 procedure Delay_Descriptors (E : Entity_Id);
3617 -- Delay generation of subprogram descriptors for given entity
3618
3619 function Might_Inline_Subp return Boolean;
3620 -- If inlining is active and the generic contains inlined subprograms,
3621 -- we instantiate the body. This may cause superfluous instantiations,
3622 -- but it is simpler than detecting the need for the body at the point
3623 -- of inlining, when the context of the instance is not available.
3624
3625 function Must_Inline_Subp return Boolean;
3626 -- If inlining is active and the generic contains inlined subprograms,
3627 -- return True if some of the inlined subprograms must be inlined by
3628 -- the frontend.
3629
3630 -----------------------
3631 -- Delay_Descriptors --
3632 -----------------------
3633
3634 procedure Delay_Descriptors (E : Entity_Id) is
3635 begin
3636 if not Delay_Subprogram_Descriptors (E) then
3637 Set_Delay_Subprogram_Descriptors (E);
3638 Pending_Descriptor.Append (E);
3639 end if;
3640 end Delay_Descriptors;
3641
3642 -----------------------
3643 -- Might_Inline_Subp --
3644 -----------------------
3645
3646 function Might_Inline_Subp return Boolean is
3647 E : Entity_Id;
3648
3649 begin
3650 if not Inline_Processing_Required then
3651 return False;
3652
3653 else
3654 E := First_Entity (Gen_Unit);
3655 while Present (E) loop
3656 if Is_Subprogram (E) and then Is_Inlined (E) then
3657 return True;
3658 end if;
3659
3660 Next_Entity (E);
3661 end loop;
3662 end if;
3663
3664 return False;
3665 end Might_Inline_Subp;
3666
3667 ----------------------
3668 -- Must_Inline_Subp --
3669 ----------------------
3670
3671 function Must_Inline_Subp return Boolean is
3672 E : Entity_Id;
3673
3674 begin
3675 if not Inline_Processing_Required then
3676 return False;
3677
3678 else
3679 E := First_Entity (Gen_Unit);
3680 while Present (E) loop
3681 if Is_Subprogram (E)
3682 and then Is_Inlined (E)
3683 and then Must_Inline (E)
3684 then
3685 return True;
3686 end if;
3687
3688 Next_Entity (E);
3689 end loop;
3690 end if;
3691
3692 return False;
3693 end Must_Inline_Subp;
3694
3695 -- Local declarations
3696
3697 Vis_Prims_List : Elist_Id := No_Elist;
3698 -- List of primitives made temporarily visible in the instantiation
3699 -- to match the visibility of the formal type
3700
3701 -- Start of processing for Analyze_Package_Instantiation
3702
3703 begin
3704 Check_SPARK_05_Restriction ("generic is not allowed", N);
3705
3706 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3707 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3708
3709 Check_Text_IO_Special_Unit (Name (N));
3710
3711 -- Make node global for error reporting
3712
3713 Instantiation_Node := N;
3714
3715 -- Turn off style checking in instances. If the check is enabled on the
3716 -- generic unit, a warning in an instance would just be noise. If not
3717 -- enabled on the generic, then a warning in an instance is just wrong.
3718
3719 Style_Check := False;
3720
3721 -- Case of instantiation of a generic package
3722
3723 if Nkind (N) = N_Package_Instantiation then
3724 Act_Decl_Id := New_Copy (Defining_Entity (N));
3725 Set_Comes_From_Source (Act_Decl_Id, True);
3726
3727 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3728 Act_Decl_Name :=
3729 Make_Defining_Program_Unit_Name (Loc,
3730 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
3731 Defining_Identifier => Act_Decl_Id);
3732 else
3733 Act_Decl_Name := Act_Decl_Id;
3734 end if;
3735
3736 -- Case of instantiation of a formal package
3737
3738 else
3739 Act_Decl_Id := Defining_Identifier (N);
3740 Act_Decl_Name := Act_Decl_Id;
3741 end if;
3742
3743 Generate_Definition (Act_Decl_Id);
3744 Preanalyze_Actuals (N);
3745
3746 Init_Env;
3747 Env_Installed := True;
3748
3749 -- Reset renaming map for formal types. The mapping is established
3750 -- when analyzing the generic associations, but some mappings are
3751 -- inherited from formal packages of parent units, and these are
3752 -- constructed when the parents are installed.
3753
3754 Generic_Renamings.Set_Last (0);
3755 Generic_Renamings_HTable.Reset;
3756
3757 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3758 Gen_Unit := Entity (Gen_Id);
3759
3760 -- Verify that it is the name of a generic package
3761
3762 -- A visibility glitch: if the instance is a child unit and the generic
3763 -- is the generic unit of a parent instance (i.e. both the parent and
3764 -- the child units are instances of the same package) the name now
3765 -- denotes the renaming within the parent, not the intended generic
3766 -- unit. See if there is a homonym that is the desired generic. The
3767 -- renaming declaration must be visible inside the instance of the
3768 -- child, but not when analyzing the name in the instantiation itself.
3769
3770 if Ekind (Gen_Unit) = E_Package
3771 and then Present (Renamed_Entity (Gen_Unit))
3772 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3773 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3774 and then Present (Homonym (Gen_Unit))
3775 then
3776 Gen_Unit := Homonym (Gen_Unit);
3777 end if;
3778
3779 if Etype (Gen_Unit) = Any_Type then
3780 Restore_Env;
3781 goto Leave;
3782
3783 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3784
3785 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3786
3787 if From_Limited_With (Gen_Unit) then
3788 Error_Msg_N
3789 ("cannot instantiate a limited withed package", Gen_Id);
3790 else
3791 Error_Msg_NE
3792 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3793 end if;
3794
3795 Restore_Env;
3796 goto Leave;
3797 end if;
3798
3799 if In_Extended_Main_Source_Unit (N) then
3800 Set_Is_Instantiated (Gen_Unit);
3801 Generate_Reference (Gen_Unit, N);
3802
3803 if Present (Renamed_Object (Gen_Unit)) then
3804 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3805 Generate_Reference (Renamed_Object (Gen_Unit), N);
3806 end if;
3807 end if;
3808
3809 if Nkind (Gen_Id) = N_Identifier
3810 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3811 then
3812 Error_Msg_NE
3813 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3814
3815 elsif Nkind (Gen_Id) = N_Expanded_Name
3816 and then Is_Child_Unit (Gen_Unit)
3817 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3818 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3819 then
3820 Error_Msg_N
3821 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3822 end if;
3823
3824 Set_Entity (Gen_Id, Gen_Unit);
3825
3826 -- If generic is a renaming, get original generic unit
3827
3828 if Present (Renamed_Object (Gen_Unit))
3829 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3830 then
3831 Gen_Unit := Renamed_Object (Gen_Unit);
3832 end if;
3833
3834 -- Verify that there are no circular instantiations
3835
3836 if In_Open_Scopes (Gen_Unit) then
3837 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3838 Restore_Env;
3839 goto Leave;
3840
3841 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3842 Error_Msg_Node_2 := Current_Scope;
3843 Error_Msg_NE
3844 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3845 Circularity_Detected := True;
3846 Restore_Env;
3847 goto Leave;
3848
3849 else
3850 -- If the context of the instance is subject to SPARK_Mode "off",
3851 -- set the global flag which signals Analyze_Pragma to ignore all
3852 -- SPARK_Mode pragmas within the instance.
3853
3854 if SPARK_Mode = Off then
3855 Ignore_Pragma_SPARK_Mode := True;
3856 end if;
3857
3858 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3859
3860 -- Initialize renamings map, for error checking, and the list that
3861 -- holds private entities whose views have changed between generic
3862 -- definition and instantiation. If this is the instance created to
3863 -- validate an actual package, the instantiation environment is that
3864 -- of the enclosing instance.
3865
3866 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3867
3868 -- Copy original generic tree, to produce text for instantiation
3869
3870 Act_Tree :=
3871 Copy_Generic_Node
3872 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3873
3874 Act_Spec := Specification (Act_Tree);
3875
3876 -- If this is the instance created to validate an actual package,
3877 -- only the formals matter, do not examine the package spec itself.
3878
3879 if Is_Actual_Pack then
3880 Set_Visible_Declarations (Act_Spec, New_List);
3881 Set_Private_Declarations (Act_Spec, New_List);
3882 end if;
3883
3884 Renaming_List :=
3885 Analyze_Associations
3886 (I_Node => N,
3887 Formals => Generic_Formal_Declarations (Act_Tree),
3888 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3889
3890 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3891
3892 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3893 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3894 Set_Is_Generic_Instance (Act_Decl_Id);
3895 Set_Generic_Parent (Act_Spec, Gen_Unit);
3896
3897 -- References to the generic in its own declaration or its body are
3898 -- references to the instance. Add a renaming declaration for the
3899 -- generic unit itself. This declaration, as well as the renaming
3900 -- declarations for the generic formals, must remain private to the
3901 -- unit: the formals, because this is the language semantics, and
3902 -- the unit because its use is an artifact of the implementation.
3903
3904 Unit_Renaming :=
3905 Make_Package_Renaming_Declaration (Loc,
3906 Defining_Unit_Name =>
3907 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3908 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3909
3910 Append (Unit_Renaming, Renaming_List);
3911
3912 -- The renaming declarations are the first local declarations of the
3913 -- new unit.
3914
3915 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3916 Insert_List_Before
3917 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3918 else
3919 Set_Visible_Declarations (Act_Spec, Renaming_List);
3920 end if;
3921
3922 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3923
3924 -- Propagate the aspect specifications from the package declaration
3925 -- template to the instantiated version of the package declaration.
3926
3927 if Has_Aspects (Act_Tree) then
3928 Set_Aspect_Specifications (Act_Decl,
3929 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3930 end if;
3931
3932 -- Save the instantiation node, for subsequent instantiation of the
3933 -- body, if there is one and we are generating code for the current
3934 -- unit. Mark unit as having a body (avoids premature error message).
3935
3936 -- We instantiate the body if we are generating code, if we are
3937 -- generating cross-reference information, or if we are building
3938 -- trees for ASIS use or GNATprove use.
3939
3940 declare
3941 Enclosing_Body_Present : Boolean := False;
3942 -- If the generic unit is not a compilation unit, then a body may
3943 -- be present in its parent even if none is required. We create a
3944 -- tentative pending instantiation for the body, which will be
3945 -- discarded if none is actually present.
3946
3947 Scop : Entity_Id;
3948
3949 begin
3950 if Scope (Gen_Unit) /= Standard_Standard
3951 and then not Is_Child_Unit (Gen_Unit)
3952 then
3953 Scop := Scope (Gen_Unit);
3954
3955 while Present (Scop)
3956 and then Scop /= Standard_Standard
3957 loop
3958 if Unit_Requires_Body (Scop) then
3959 Enclosing_Body_Present := True;
3960 exit;
3961
3962 elsif In_Open_Scopes (Scop)
3963 and then In_Package_Body (Scop)
3964 then
3965 Enclosing_Body_Present := True;
3966 exit;
3967 end if;
3968
3969 exit when Is_Compilation_Unit (Scop);
3970 Scop := Scope (Scop);
3971 end loop;
3972 end if;
3973
3974 -- If front-end inlining is enabled, and this is a unit for which
3975 -- code will be generated, we instantiate the body at once.
3976
3977 -- This is done if the instance is not the main unit, and if the
3978 -- generic is not a child unit of another generic, to avoid scope
3979 -- problems and the reinstallation of parent instances.
3980
3981 if Expander_Active
3982 and then (not Is_Child_Unit (Gen_Unit)
3983 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3984 and then Might_Inline_Subp
3985 and then not Is_Actual_Pack
3986 then
3987 if not Back_End_Inlining
3988 and then Front_End_Inlining
3989 and then (Is_In_Main_Unit (N)
3990 or else In_Main_Context (Current_Scope))
3991 and then Nkind (Parent (N)) /= N_Compilation_Unit
3992 then
3993 Inline_Now := True;
3994
3995 elsif Back_End_Inlining
3996 and then Must_Inline_Subp
3997 and then (Is_In_Main_Unit (N)
3998 or else In_Main_Context (Current_Scope))
3999 and then Nkind (Parent (N)) /= N_Compilation_Unit
4000 then
4001 Inline_Now := True;
4002
4003 -- In configurable_run_time mode we force the inlining of
4004 -- predefined subprograms marked Inline_Always, to minimize
4005 -- the use of the run-time library.
4006
4007 elsif Is_Predefined_File_Name
4008 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
4009 and then Configurable_Run_Time_Mode
4010 and then Nkind (Parent (N)) /= N_Compilation_Unit
4011 then
4012 Inline_Now := True;
4013 end if;
4014
4015 -- If the current scope is itself an instance within a child
4016 -- unit, there will be duplications in the scope stack, and the
4017 -- unstacking mechanism in Inline_Instance_Body will fail.
4018 -- This loses some rare cases of optimization, and might be
4019 -- improved some day, if we can find a proper abstraction for
4020 -- "the complete compilation context" that can be saved and
4021 -- restored. ???
4022
4023 if Is_Generic_Instance (Current_Scope) then
4024 declare
4025 Curr_Unit : constant Entity_Id :=
4026 Cunit_Entity (Current_Sem_Unit);
4027 begin
4028 if Curr_Unit /= Current_Scope
4029 and then Is_Child_Unit (Curr_Unit)
4030 then
4031 Inline_Now := False;
4032 end if;
4033 end;
4034 end if;
4035 end if;
4036
4037 Needs_Body :=
4038 (Unit_Requires_Body (Gen_Unit)
4039 or else Enclosing_Body_Present
4040 or else Present (Corresponding_Body (Gen_Decl)))
4041 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
4042 and then not Is_Actual_Pack
4043 and then not Inline_Now
4044 and then (Operating_Mode = Generate_Code
4045
4046 -- Need comment for this check ???
4047
4048 or else (Operating_Mode = Check_Semantics
4049 and then (ASIS_Mode or GNATprove_Mode)));
4050
4051 -- If front_end_inlining is enabled, do not instantiate body if
4052 -- within a generic context.
4053
4054 if (Front_End_Inlining and then not Expander_Active)
4055 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4056 then
4057 Needs_Body := False;
4058 end if;
4059
4060 -- If the current context is generic, and the package being
4061 -- instantiated is declared within a formal package, there is no
4062 -- body to instantiate until the enclosing generic is instantiated
4063 -- and there is an actual for the formal package. If the formal
4064 -- package has parameters, we build a regular package instance for
4065 -- it, that precedes the original formal package declaration.
4066
4067 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4068 declare
4069 Decl : constant Node_Id :=
4070 Original_Node
4071 (Unit_Declaration_Node (Scope (Gen_Unit)));
4072 begin
4073 if Nkind (Decl) = N_Formal_Package_Declaration
4074 or else (Nkind (Decl) = N_Package_Declaration
4075 and then Is_List_Member (Decl)
4076 and then Present (Next (Decl))
4077 and then
4078 Nkind (Next (Decl)) =
4079 N_Formal_Package_Declaration)
4080 then
4081 Needs_Body := False;
4082 end if;
4083 end;
4084 end if;
4085 end;
4086
4087 -- For RCI unit calling stubs, we omit the instance body if the
4088 -- instance is the RCI library unit itself.
4089
4090 -- However there is a special case for nested instances: in this case
4091 -- we do generate the instance body, as it might be required, e.g.
4092 -- because it provides stream attributes for some type used in the
4093 -- profile of a remote subprogram. This is consistent with 12.3(12),
4094 -- which indicates that the instance body occurs at the place of the
4095 -- instantiation, and thus is part of the RCI declaration, which is
4096 -- present on all client partitions (this is E.2.3(18)).
4097
4098 -- Note that AI12-0002 may make it illegal at some point to have
4099 -- stream attributes defined in an RCI unit, in which case this
4100 -- special case will become unnecessary. In the meantime, there
4101 -- is known application code in production that depends on this
4102 -- being possible, so we definitely cannot eliminate the body in
4103 -- the case of nested instances for the time being.
4104
4105 -- When we generate a nested instance body, calling stubs for any
4106 -- relevant subprogram will be be inserted immediately after the
4107 -- subprogram declarations, and will take precedence over the
4108 -- subsequent (original) body. (The stub and original body will be
4109 -- complete homographs, but this is permitted in an instance).
4110 -- (Could we do better and remove the original body???)
4111
4112 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4113 and then Comes_From_Source (N)
4114 and then Nkind (Parent (N)) = N_Compilation_Unit
4115 then
4116 Needs_Body := False;
4117 end if;
4118
4119 if Needs_Body then
4120
4121 -- Here is a defence against a ludicrous number of instantiations
4122 -- caused by a circular set of instantiation attempts.
4123
4124 if Pending_Instantiations.Last > Maximum_Instantiations then
4125 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4126 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4127 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4128 raise Unrecoverable_Error;
4129 end if;
4130
4131 -- Indicate that the enclosing scopes contain an instantiation,
4132 -- and that cleanup actions should be delayed until after the
4133 -- instance body is expanded.
4134
4135 Check_Forward_Instantiation (Gen_Decl);
4136 if Nkind (N) = N_Package_Instantiation then
4137 declare
4138 Enclosing_Master : Entity_Id;
4139
4140 begin
4141 -- Loop to search enclosing masters
4142
4143 Enclosing_Master := Current_Scope;
4144 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4145 if Ekind (Enclosing_Master) = E_Package then
4146 if Is_Compilation_Unit (Enclosing_Master) then
4147 if In_Package_Body (Enclosing_Master) then
4148 Delay_Descriptors
4149 (Body_Entity (Enclosing_Master));
4150 else
4151 Delay_Descriptors
4152 (Enclosing_Master);
4153 end if;
4154
4155 exit Scope_Loop;
4156
4157 else
4158 Enclosing_Master := Scope (Enclosing_Master);
4159 end if;
4160
4161 elsif Is_Generic_Unit (Enclosing_Master)
4162 or else Ekind (Enclosing_Master) = E_Void
4163 then
4164 -- Cleanup actions will eventually be performed on the
4165 -- enclosing subprogram or package instance, if any.
4166 -- Enclosing scope is void in the formal part of a
4167 -- generic subprogram.
4168
4169 exit Scope_Loop;
4170
4171 else
4172 if Ekind (Enclosing_Master) = E_Entry
4173 and then
4174 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4175 then
4176 if not Expander_Active then
4177 exit Scope_Loop;
4178 else
4179 Enclosing_Master :=
4180 Protected_Body_Subprogram (Enclosing_Master);
4181 end if;
4182 end if;
4183
4184 Set_Delay_Cleanups (Enclosing_Master);
4185
4186 while Ekind (Enclosing_Master) = E_Block loop
4187 Enclosing_Master := Scope (Enclosing_Master);
4188 end loop;
4189
4190 if Is_Subprogram (Enclosing_Master) then
4191 Delay_Descriptors (Enclosing_Master);
4192
4193 elsif Is_Task_Type (Enclosing_Master) then
4194 declare
4195 TBP : constant Node_Id :=
4196 Get_Task_Body_Procedure
4197 (Enclosing_Master);
4198 begin
4199 if Present (TBP) then
4200 Delay_Descriptors (TBP);
4201 Set_Delay_Cleanups (TBP);
4202 end if;
4203 end;
4204 end if;
4205
4206 exit Scope_Loop;
4207 end if;
4208 end loop Scope_Loop;
4209 end;
4210
4211 -- Make entry in table
4212
4213 Pending_Instantiations.Append
4214 ((Inst_Node => N,
4215 Act_Decl => Act_Decl,
4216 Expander_Status => Expander_Active,
4217 Current_Sem_Unit => Current_Sem_Unit,
4218 Scope_Suppress => Scope_Suppress,
4219 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4220 Version => Ada_Version,
4221 Version_Pragma => Ada_Version_Pragma,
4222 Warnings => Save_Warnings,
4223 SPARK_Mode => SPARK_Mode,
4224 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4225 end if;
4226 end if;
4227
4228 Set_Categorization_From_Pragmas (Act_Decl);
4229
4230 if Parent_Installed then
4231 Hide_Current_Scope;
4232 end if;
4233
4234 Set_Instance_Spec (N, Act_Decl);
4235
4236 -- If not a compilation unit, insert the package declaration before
4237 -- the original instantiation node.
4238
4239 if Nkind (Parent (N)) /= N_Compilation_Unit then
4240 Mark_Rewrite_Insertion (Act_Decl);
4241 Insert_Before (N, Act_Decl);
4242 Analyze (Act_Decl);
4243
4244 -- For an instantiation that is a compilation unit, place
4245 -- declaration on current node so context is complete for analysis
4246 -- (including nested instantiations). If this is the main unit,
4247 -- the declaration eventually replaces the instantiation node.
4248 -- If the instance body is created later, it replaces the
4249 -- instance node, and the declaration is attached to it
4250 -- (see Build_Instance_Compilation_Unit_Nodes).
4251
4252 else
4253 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4254
4255 -- The entity for the current unit is the newly created one,
4256 -- and all semantic information is attached to it.
4257
4258 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4259
4260 -- If this is the main unit, replace the main entity as well
4261
4262 if Current_Sem_Unit = Main_Unit then
4263 Main_Unit_Entity := Act_Decl_Id;
4264 end if;
4265 end if;
4266
4267 Set_Unit (Parent (N), Act_Decl);
4268 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4269 Set_Package_Instantiation (Act_Decl_Id, N);
4270
4271 -- Process aspect specifications of the instance node, if any, to
4272 -- take into account categorization pragmas before analyzing the
4273 -- instance.
4274
4275 if Has_Aspects (N) then
4276 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4277 end if;
4278
4279 Analyze (Act_Decl);
4280 Set_Unit (Parent (N), N);
4281 Set_Body_Required (Parent (N), False);
4282
4283 -- We never need elaboration checks on instantiations, since by
4284 -- definition, the body instantiation is elaborated at the same
4285 -- time as the spec instantiation.
4286
4287 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4288 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4289 end if;
4290
4291 Check_Elab_Instantiation (N);
4292
4293 if ABE_Is_Certain (N) and then Needs_Body then
4294 Pending_Instantiations.Decrement_Last;
4295 end if;
4296
4297 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4298
4299 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4300 First_Private_Entity (Act_Decl_Id));
4301
4302 -- If the instantiation will receive a body, the unit will be
4303 -- transformed into a package body, and receive its own elaboration
4304 -- entity. Otherwise, the nature of the unit is now a package
4305 -- declaration.
4306
4307 if Nkind (Parent (N)) = N_Compilation_Unit
4308 and then not Needs_Body
4309 then
4310 Rewrite (N, Act_Decl);
4311 end if;
4312
4313 if Present (Corresponding_Body (Gen_Decl))
4314 or else Unit_Requires_Body (Gen_Unit)
4315 then
4316 Set_Has_Completion (Act_Decl_Id);
4317 end if;
4318
4319 Check_Formal_Packages (Act_Decl_Id);
4320
4321 Restore_Hidden_Primitives (Vis_Prims_List);
4322 Restore_Private_Views (Act_Decl_Id);
4323
4324 Inherit_Context (Gen_Decl, N);
4325
4326 if Parent_Installed then
4327 Remove_Parent;
4328 end if;
4329
4330 Restore_Env;
4331 Env_Installed := False;
4332 end if;
4333
4334 Validate_Categorization_Dependency (N, Act_Decl_Id);
4335
4336 -- There used to be a check here to prevent instantiations in local
4337 -- contexts if the No_Local_Allocators restriction was active. This
4338 -- check was removed by a binding interpretation in AI-95-00130/07,
4339 -- but we retain the code for documentation purposes.
4340
4341 -- if Ekind (Act_Decl_Id) /= E_Void
4342 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4343 -- then
4344 -- Check_Restriction (No_Local_Allocators, N);
4345 -- end if;
4346
4347 if Inline_Now then
4348 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4349 end if;
4350
4351 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4352 -- be used as defining identifiers for a formal package and for the
4353 -- corresponding expanded package.
4354
4355 if Nkind (N) = N_Formal_Package_Declaration then
4356 Act_Decl_Id := New_Copy (Defining_Entity (N));
4357 Set_Comes_From_Source (Act_Decl_Id, True);
4358 Set_Is_Generic_Instance (Act_Decl_Id, False);
4359 Set_Defining_Identifier (N, Act_Decl_Id);
4360 end if;
4361
4362 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4363 SPARK_Mode := Save_SM;
4364 SPARK_Mode_Pragma := Save_SMP;
4365 Style_Check := Save_Style_Check;
4366
4367 -- Check that if N is an instantiation of System.Dim_Float_IO or
4368 -- System.Dim_Integer_IO, the formal type has a dimension system.
4369
4370 if Nkind (N) = N_Package_Instantiation
4371 and then Is_Dim_IO_Package_Instantiation (N)
4372 then
4373 declare
4374 Assoc : constant Node_Id := First (Generic_Associations (N));
4375 begin
4376 if not Has_Dimension_System
4377 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4378 then
4379 Error_Msg_N ("type with a dimension system expected", Assoc);
4380 end if;
4381 end;
4382 end if;
4383
4384 <<Leave>>
4385 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4386 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4387 end if;
4388
4389 exception
4390 when Instantiation_Error =>
4391 if Parent_Installed then
4392 Remove_Parent;
4393 end if;
4394
4395 if Env_Installed then
4396 Restore_Env;
4397 end if;
4398
4399 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4400 SPARK_Mode := Save_SM;
4401 SPARK_Mode_Pragma := Save_SMP;
4402 Style_Check := Save_Style_Check;
4403 end Analyze_Package_Instantiation;
4404
4405 --------------------------
4406 -- Inline_Instance_Body --
4407 --------------------------
4408
4409 procedure Inline_Instance_Body
4410 (N : Node_Id;
4411 Gen_Unit : Entity_Id;
4412 Act_Decl : Node_Id)
4413 is
4414 Vis : Boolean;
4415 Gen_Comp : constant Entity_Id :=
4416 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4417 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4418 Curr_Scope : Entity_Id := Empty;
4419 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4420 Removed : Boolean := False;
4421 Num_Scopes : Int := 0;
4422
4423 Scope_Stack_Depth : constant Int :=
4424 Scope_Stack.Last - Scope_Stack.First + 1;
4425
4426 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4427 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4428 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4429 List : Elist_Id;
4430 Num_Inner : Int := 0;
4431 N_Instances : Int := 0;
4432 S : Entity_Id;
4433
4434 begin
4435 -- Case of generic unit defined in another unit. We must remove the
4436 -- complete context of the current unit to install that of the generic.
4437
4438 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4439
4440 -- Add some comments for the following two loops ???
4441
4442 S := Current_Scope;
4443 while Present (S) and then S /= Standard_Standard loop
4444 loop
4445 Num_Scopes := Num_Scopes + 1;
4446
4447 Use_Clauses (Num_Scopes) :=
4448 (Scope_Stack.Table
4449 (Scope_Stack.Last - Num_Scopes + 1).
4450 First_Use_Clause);
4451 End_Use_Clauses (Use_Clauses (Num_Scopes));
4452
4453 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4454 or else Scope_Stack.Table
4455 (Scope_Stack.Last - Num_Scopes).Entity
4456 = Scope (S);
4457 end loop;
4458
4459 exit when Is_Generic_Instance (S)
4460 and then (In_Package_Body (S)
4461 or else Ekind (S) = E_Procedure
4462 or else Ekind (S) = E_Function);
4463 S := Scope (S);
4464 end loop;
4465
4466 Vis := Is_Immediately_Visible (Gen_Comp);
4467
4468 -- Find and save all enclosing instances
4469
4470 S := Current_Scope;
4471
4472 while Present (S)
4473 and then S /= Standard_Standard
4474 loop
4475 if Is_Generic_Instance (S) then
4476 N_Instances := N_Instances + 1;
4477 Instances (N_Instances) := S;
4478
4479 exit when In_Package_Body (S);
4480 end if;
4481
4482 S := Scope (S);
4483 end loop;
4484
4485 -- Remove context of current compilation unit, unless we are within a
4486 -- nested package instantiation, in which case the context has been
4487 -- removed previously.
4488
4489 -- If current scope is the body of a child unit, remove context of
4490 -- spec as well. If an enclosing scope is an instance body, the
4491 -- context has already been removed, but the entities in the body
4492 -- must be made invisible as well.
4493
4494 S := Current_Scope;
4495
4496 while Present (S)
4497 and then S /= Standard_Standard
4498 loop
4499 if Is_Generic_Instance (S)
4500 and then (In_Package_Body (S)
4501 or else Ekind (S) = E_Procedure
4502 or else Ekind (S) = E_Function)
4503 then
4504 -- We still have to remove the entities of the enclosing
4505 -- instance from direct visibility.
4506
4507 declare
4508 E : Entity_Id;
4509 begin
4510 E := First_Entity (S);
4511 while Present (E) loop
4512 Set_Is_Immediately_Visible (E, False);
4513 Next_Entity (E);
4514 end loop;
4515 end;
4516
4517 exit;
4518 end if;
4519
4520 if S = Curr_Unit
4521 or else (Ekind (Curr_Unit) = E_Package_Body
4522 and then S = Spec_Entity (Curr_Unit))
4523 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4524 and then S =
4525 Corresponding_Spec
4526 (Unit_Declaration_Node (Curr_Unit)))
4527 then
4528 Removed := True;
4529
4530 -- Remove entities in current scopes from visibility, so that
4531 -- instance body is compiled in a clean environment.
4532
4533 List := Save_Scope_Stack (Handle_Use => False);
4534
4535 if Is_Child_Unit (S) then
4536
4537 -- Remove child unit from stack, as well as inner scopes.
4538 -- Removing the context of a child unit removes parent units
4539 -- as well.
4540
4541 while Current_Scope /= S loop
4542 Num_Inner := Num_Inner + 1;
4543 Inner_Scopes (Num_Inner) := Current_Scope;
4544 Pop_Scope;
4545 end loop;
4546
4547 Pop_Scope;
4548 Remove_Context (Curr_Comp);
4549 Curr_Scope := S;
4550
4551 else
4552 Remove_Context (Curr_Comp);
4553 end if;
4554
4555 if Ekind (Curr_Unit) = E_Package_Body then
4556 Remove_Context (Library_Unit (Curr_Comp));
4557 end if;
4558 end if;
4559
4560 S := Scope (S);
4561 end loop;
4562 pragma Assert (Num_Inner < Num_Scopes);
4563
4564 Push_Scope (Standard_Standard);
4565 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4566 Instantiate_Package_Body
4567 (Body_Info =>
4568 ((Inst_Node => N,
4569 Act_Decl => Act_Decl,
4570 Expander_Status => Expander_Active,
4571 Current_Sem_Unit => Current_Sem_Unit,
4572 Scope_Suppress => Scope_Suppress,
4573 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4574 Version => Ada_Version,
4575 Version_Pragma => Ada_Version_Pragma,
4576 Warnings => Save_Warnings,
4577 SPARK_Mode => SPARK_Mode,
4578 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4579 Inlined_Body => True);
4580
4581 Pop_Scope;
4582
4583 -- Restore context
4584
4585 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4586
4587 -- Reset Generic_Instance flag so that use clauses can be installed
4588 -- in the proper order. (See Use_One_Package for effect of enclosing
4589 -- instances on processing of use clauses).
4590
4591 for J in 1 .. N_Instances loop
4592 Set_Is_Generic_Instance (Instances (J), False);
4593 end loop;
4594
4595 if Removed then
4596 Install_Context (Curr_Comp);
4597
4598 if Present (Curr_Scope)
4599 and then Is_Child_Unit (Curr_Scope)
4600 then
4601 Push_Scope (Curr_Scope);
4602 Set_Is_Immediately_Visible (Curr_Scope);
4603
4604 -- Finally, restore inner scopes as well
4605
4606 for J in reverse 1 .. Num_Inner loop
4607 Push_Scope (Inner_Scopes (J));
4608 end loop;
4609 end if;
4610
4611 Restore_Scope_Stack (List, Handle_Use => False);
4612
4613 if Present (Curr_Scope)
4614 and then
4615 (In_Private_Part (Curr_Scope)
4616 or else In_Package_Body (Curr_Scope))
4617 then
4618 -- Install private declaration of ancestor units, which are
4619 -- currently available. Restore_Scope_Stack and Install_Context
4620 -- only install the visible part of parents.
4621
4622 declare
4623 Par : Entity_Id;
4624 begin
4625 Par := Scope (Curr_Scope);
4626 while (Present (Par))
4627 and then Par /= Standard_Standard
4628 loop
4629 Install_Private_Declarations (Par);
4630 Par := Scope (Par);
4631 end loop;
4632 end;
4633 end if;
4634 end if;
4635
4636 -- Restore use clauses. For a child unit, use clauses in the parents
4637 -- are restored when installing the context, so only those in inner
4638 -- scopes (and those local to the child unit itself) need to be
4639 -- installed explicitly.
4640
4641 if Is_Child_Unit (Curr_Unit)
4642 and then Removed
4643 then
4644 for J in reverse 1 .. Num_Inner + 1 loop
4645 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4646 Use_Clauses (J);
4647 Install_Use_Clauses (Use_Clauses (J));
4648 end loop;
4649
4650 else
4651 for J in reverse 1 .. Num_Scopes loop
4652 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4653 Use_Clauses (J);
4654 Install_Use_Clauses (Use_Clauses (J));
4655 end loop;
4656 end if;
4657
4658 -- Restore status of instances. If one of them is a body, make its
4659 -- local entities visible again.
4660
4661 declare
4662 E : Entity_Id;
4663 Inst : Entity_Id;
4664
4665 begin
4666 for J in 1 .. N_Instances loop
4667 Inst := Instances (J);
4668 Set_Is_Generic_Instance (Inst, True);
4669
4670 if In_Package_Body (Inst)
4671 or else Ekind (S) = E_Procedure
4672 or else Ekind (S) = E_Function
4673 then
4674 E := First_Entity (Instances (J));
4675 while Present (E) loop
4676 Set_Is_Immediately_Visible (E);
4677 Next_Entity (E);
4678 end loop;
4679 end if;
4680 end loop;
4681 end;
4682
4683 -- If generic unit is in current unit, current context is correct
4684
4685 else
4686 Instantiate_Package_Body
4687 (Body_Info =>
4688 ((Inst_Node => N,
4689 Act_Decl => Act_Decl,
4690 Expander_Status => Expander_Active,
4691 Current_Sem_Unit => Current_Sem_Unit,
4692 Scope_Suppress => Scope_Suppress,
4693 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4694 Version => Ada_Version,
4695 Version_Pragma => Ada_Version_Pragma,
4696 Warnings => Save_Warnings,
4697 SPARK_Mode => SPARK_Mode,
4698 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4699 Inlined_Body => True);
4700 end if;
4701 end Inline_Instance_Body;
4702
4703 -------------------------------------
4704 -- Analyze_Procedure_Instantiation --
4705 -------------------------------------
4706
4707 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4708 begin
4709 Analyze_Subprogram_Instantiation (N, E_Procedure);
4710 end Analyze_Procedure_Instantiation;
4711
4712 -----------------------------------
4713 -- Need_Subprogram_Instance_Body --
4714 -----------------------------------
4715
4716 function Need_Subprogram_Instance_Body
4717 (N : Node_Id;
4718 Subp : Entity_Id) return Boolean
4719 is
4720 begin
4721 -- Must be inlined (or inlined renaming)
4722
4723 if (Is_In_Main_Unit (N)
4724 or else Is_Inlined (Subp)
4725 or else Is_Inlined (Alias (Subp)))
4726
4727 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4728
4729 and then (Operating_Mode = Generate_Code
4730 or else (Operating_Mode = Check_Semantics
4731 and then (ASIS_Mode or GNATprove_Mode)))
4732
4733 -- The body is needed when generating code (full expansion), in ASIS
4734 -- mode for other tools, and in GNATprove mode (special expansion) for
4735 -- formal verification of the body itself.
4736
4737 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4738
4739 -- No point in inlining if ABE is inevitable
4740
4741 and then not ABE_Is_Certain (N)
4742
4743 -- Or if subprogram is eliminated
4744
4745 and then not Is_Eliminated (Subp)
4746 then
4747 Pending_Instantiations.Append
4748 ((Inst_Node => N,
4749 Act_Decl => Unit_Declaration_Node (Subp),
4750 Expander_Status => Expander_Active,
4751 Current_Sem_Unit => Current_Sem_Unit,
4752 Scope_Suppress => Scope_Suppress,
4753 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4754 Version => Ada_Version,
4755 Version_Pragma => Ada_Version_Pragma,
4756 Warnings => Save_Warnings,
4757 SPARK_Mode => SPARK_Mode,
4758 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4759 return True;
4760
4761 -- Here if not inlined, or we ignore the inlining
4762
4763 else
4764 return False;
4765 end if;
4766 end Need_Subprogram_Instance_Body;
4767
4768 --------------------------------------
4769 -- Analyze_Subprogram_Instantiation --
4770 --------------------------------------
4771
4772 procedure Analyze_Subprogram_Instantiation
4773 (N : Node_Id;
4774 K : Entity_Kind)
4775 is
4776 Loc : constant Source_Ptr := Sloc (N);
4777 Gen_Id : constant Node_Id := Name (N);
4778
4779 Anon_Id : constant Entity_Id :=
4780 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4781 Chars => New_External_Name
4782 (Chars (Defining_Entity (N)), 'R'));
4783
4784 Act_Decl_Id : Entity_Id;
4785 Act_Decl : Node_Id;
4786 Act_Spec : Node_Id;
4787 Act_Tree : Node_Id;
4788
4789 Env_Installed : Boolean := False;
4790 Gen_Unit : Entity_Id;
4791 Gen_Decl : Node_Id;
4792 Pack_Id : Entity_Id;
4793 Parent_Installed : Boolean := False;
4794 Renaming_List : List_Id;
4795
4796 procedure Analyze_Instance_And_Renamings;
4797 -- The instance must be analyzed in a context that includes the mappings
4798 -- of generic parameters into actuals. We create a package declaration
4799 -- for this purpose, and a subprogram with an internal name within the
4800 -- package. The subprogram instance is simply an alias for the internal
4801 -- subprogram, declared in the current scope.
4802
4803 ------------------------------------
4804 -- Analyze_Instance_And_Renamings --
4805 ------------------------------------
4806
4807 procedure Analyze_Instance_And_Renamings is
4808 Def_Ent : constant Entity_Id := Defining_Entity (N);
4809 Pack_Decl : Node_Id;
4810
4811 begin
4812 if Nkind (Parent (N)) = N_Compilation_Unit then
4813
4814 -- For the case of a compilation unit, the container package has
4815 -- the same name as the instantiation, to insure that the binder
4816 -- calls the elaboration procedure with the right name. Copy the
4817 -- entity of the instance, which may have compilation level flags
4818 -- (e.g. Is_Child_Unit) set.
4819
4820 Pack_Id := New_Copy (Def_Ent);
4821
4822 else
4823 -- Otherwise we use the name of the instantiation concatenated
4824 -- with its source position to ensure uniqueness if there are
4825 -- several instantiations with the same name.
4826
4827 Pack_Id :=
4828 Make_Defining_Identifier (Loc,
4829 Chars => New_External_Name
4830 (Related_Id => Chars (Def_Ent),
4831 Suffix => "GP",
4832 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4833 end if;
4834
4835 Pack_Decl := Make_Package_Declaration (Loc,
4836 Specification => Make_Package_Specification (Loc,
4837 Defining_Unit_Name => Pack_Id,
4838 Visible_Declarations => Renaming_List,
4839 End_Label => Empty));
4840
4841 Set_Instance_Spec (N, Pack_Decl);
4842 Set_Is_Generic_Instance (Pack_Id);
4843 Set_Debug_Info_Needed (Pack_Id);
4844
4845 -- Case of not a compilation unit
4846
4847 if Nkind (Parent (N)) /= N_Compilation_Unit then
4848 Mark_Rewrite_Insertion (Pack_Decl);
4849 Insert_Before (N, Pack_Decl);
4850 Set_Has_Completion (Pack_Id);
4851
4852 -- Case of an instantiation that is a compilation unit
4853
4854 -- Place declaration on current node so context is complete for
4855 -- analysis (including nested instantiations), and for use in a
4856 -- context_clause (see Analyze_With_Clause).
4857
4858 else
4859 Set_Unit (Parent (N), Pack_Decl);
4860 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4861 end if;
4862
4863 Analyze (Pack_Decl);
4864 Check_Formal_Packages (Pack_Id);
4865 Set_Is_Generic_Instance (Pack_Id, False);
4866
4867 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4868 -- above???
4869
4870 -- Body of the enclosing package is supplied when instantiating the
4871 -- subprogram body, after semantic analysis is completed.
4872
4873 if Nkind (Parent (N)) = N_Compilation_Unit then
4874
4875 -- Remove package itself from visibility, so it does not
4876 -- conflict with subprogram.
4877
4878 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4879
4880 -- Set name and scope of internal subprogram so that the proper
4881 -- external name will be generated. The proper scope is the scope
4882 -- of the wrapper package. We need to generate debugging info for
4883 -- the internal subprogram, so set flag accordingly.
4884
4885 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4886 Set_Scope (Anon_Id, Scope (Pack_Id));
4887
4888 -- Mark wrapper package as referenced, to avoid spurious warnings
4889 -- if the instantiation appears in various with_ clauses of
4890 -- subunits of the main unit.
4891
4892 Set_Referenced (Pack_Id);
4893 end if;
4894
4895 Set_Is_Generic_Instance (Anon_Id);
4896 Set_Debug_Info_Needed (Anon_Id);
4897 Act_Decl_Id := New_Copy (Anon_Id);
4898
4899 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4900 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4901 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4902 Set_Comes_From_Source (Act_Decl_Id, True);
4903
4904 -- The signature may involve types that are not frozen yet, but the
4905 -- subprogram will be frozen at the point the wrapper package is
4906 -- frozen, so it does not need its own freeze node. In fact, if one
4907 -- is created, it might conflict with the freezing actions from the
4908 -- wrapper package.
4909
4910 Set_Has_Delayed_Freeze (Anon_Id, False);
4911
4912 -- If the instance is a child unit, mark the Id accordingly. Mark
4913 -- the anonymous entity as well, which is the real subprogram and
4914 -- which is used when the instance appears in a context clause.
4915 -- Similarly, propagate the Is_Eliminated flag to handle properly
4916 -- nested eliminated subprograms.
4917
4918 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4919 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4920 New_Overloaded_Entity (Act_Decl_Id);
4921 Check_Eliminated (Act_Decl_Id);
4922 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4923
4924 -- In compilation unit case, kill elaboration checks on the
4925 -- instantiation, since they are never needed -- the body is
4926 -- instantiated at the same point as the spec.
4927
4928 if Nkind (Parent (N)) = N_Compilation_Unit then
4929 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4930 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4931 Set_Is_Compilation_Unit (Anon_Id);
4932
4933 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4934 end if;
4935
4936 -- The instance is not a freezing point for the new subprogram
4937
4938 Set_Is_Frozen (Act_Decl_Id, False);
4939
4940 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4941 Valid_Operator_Definition (Act_Decl_Id);
4942 end if;
4943
4944 Set_Alias (Act_Decl_Id, Anon_Id);
4945 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4946 Set_Has_Completion (Act_Decl_Id);
4947 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4948
4949 if Nkind (Parent (N)) = N_Compilation_Unit then
4950 Set_Body_Required (Parent (N), False);
4951 end if;
4952 end Analyze_Instance_And_Renamings;
4953
4954 -- Local variables
4955
4956 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
4957 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
4958
4959 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4960 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4961 -- Save the SPARK_Mode-related data for restore on exit
4962
4963 Vis_Prims_List : Elist_Id := No_Elist;
4964 -- List of primitives made temporarily visible in the instantiation
4965 -- to match the visibility of the formal type
4966
4967 -- Start of processing for Analyze_Subprogram_Instantiation
4968
4969 begin
4970 Check_SPARK_05_Restriction ("generic is not allowed", N);
4971
4972 -- Very first thing: check for special Text_IO unit in case we are
4973 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
4974 -- such an instantiation is bogus (these are packages, not subprograms),
4975 -- but we get a better error message if we do this.
4976
4977 Check_Text_IO_Special_Unit (Gen_Id);
4978
4979 -- Make node global for error reporting
4980
4981 Instantiation_Node := N;
4982
4983 -- For package instantiations we turn off style checks, because they
4984 -- will have been emitted in the generic. For subprogram instantiations
4985 -- we want to apply at least the check on overriding indicators so we
4986 -- do not modify the style check status.
4987
4988 -- The renaming declarations for the actuals do not come from source and
4989 -- will not generate spurious warnings.
4990
4991 Preanalyze_Actuals (N);
4992
4993 Init_Env;
4994 Env_Installed := True;
4995 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4996 Gen_Unit := Entity (Gen_Id);
4997
4998 Generate_Reference (Gen_Unit, Gen_Id);
4999
5000 if Nkind (Gen_Id) = N_Identifier
5001 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5002 then
5003 Error_Msg_NE
5004 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5005 end if;
5006
5007 if Etype (Gen_Unit) = Any_Type then
5008 Restore_Env;
5009 return;
5010 end if;
5011
5012 -- Verify that it is a generic subprogram of the right kind, and that
5013 -- it does not lead to a circular instantiation.
5014
5015 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5016 Error_Msg_NE
5017 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5018
5019 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5020 Error_Msg_NE
5021 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5022
5023 elsif In_Open_Scopes (Gen_Unit) then
5024 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5025
5026 else
5027 -- If the context of the instance is subject to SPARK_Mode "off",
5028 -- set the global flag which signals Analyze_Pragma to ignore all
5029 -- SPARK_Mode pragmas within the instance.
5030
5031 if SPARK_Mode = Off then
5032 Ignore_Pragma_SPARK_Mode := True;
5033 end if;
5034
5035 Set_Entity (Gen_Id, Gen_Unit);
5036 Set_Is_Instantiated (Gen_Unit);
5037
5038 if In_Extended_Main_Source_Unit (N) then
5039 Generate_Reference (Gen_Unit, N);
5040 end if;
5041
5042 -- If renaming, get original unit
5043
5044 if Present (Renamed_Object (Gen_Unit))
5045 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
5046 or else
5047 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
5048 then
5049 Gen_Unit := Renamed_Object (Gen_Unit);
5050 Set_Is_Instantiated (Gen_Unit);
5051 Generate_Reference (Gen_Unit, N);
5052 end if;
5053
5054 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5055 Error_Msg_Node_2 := Current_Scope;
5056 Error_Msg_NE
5057 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5058 Circularity_Detected := True;
5059 Restore_Hidden_Primitives (Vis_Prims_List);
5060 goto Leave;
5061 end if;
5062
5063 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5064
5065 -- Initialize renamings map, for error checking
5066
5067 Generic_Renamings.Set_Last (0);
5068 Generic_Renamings_HTable.Reset;
5069
5070 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
5071
5072 -- Copy original generic tree, to produce text for instantiation
5073
5074 Act_Tree :=
5075 Copy_Generic_Node
5076 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5077
5078 -- Inherit overriding indicator from instance node
5079
5080 Act_Spec := Specification (Act_Tree);
5081 Set_Must_Override (Act_Spec, Must_Override (N));
5082 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5083
5084 Renaming_List :=
5085 Analyze_Associations
5086 (I_Node => N,
5087 Formals => Generic_Formal_Declarations (Act_Tree),
5088 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5089
5090 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5091
5092 -- The subprogram itself cannot contain a nested instance, so the
5093 -- current parent is left empty.
5094
5095 Set_Instance_Env (Gen_Unit, Empty);
5096
5097 -- Build the subprogram declaration, which does not appear in the
5098 -- generic template, and give it a sloc consistent with that of the
5099 -- template.
5100
5101 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5102 Set_Generic_Parent (Act_Spec, Gen_Unit);
5103 Act_Decl :=
5104 Make_Subprogram_Declaration (Sloc (Act_Spec),
5105 Specification => Act_Spec);
5106
5107 -- The aspects have been copied previously, but they have to be
5108 -- linked explicitly to the new subprogram declaration. Explicit
5109 -- pre/postconditions on the instance are analyzed below, in a
5110 -- separate step.
5111
5112 Move_Aspects (Act_Tree, To => Act_Decl);
5113 Set_Categorization_From_Pragmas (Act_Decl);
5114
5115 if Parent_Installed then
5116 Hide_Current_Scope;
5117 end if;
5118
5119 Append (Act_Decl, Renaming_List);
5120 Analyze_Instance_And_Renamings;
5121
5122 -- If the generic is marked Import (Intrinsic), then so is the
5123 -- instance. This indicates that there is no body to instantiate. If
5124 -- generic is marked inline, so it the instance, and the anonymous
5125 -- subprogram it renames. If inlined, or else if inlining is enabled
5126 -- for the compilation, we generate the instance body even if it is
5127 -- not within the main unit.
5128
5129 if Is_Intrinsic_Subprogram (Gen_Unit) then
5130 Set_Is_Intrinsic_Subprogram (Anon_Id);
5131 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5132
5133 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5134 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5135 end if;
5136 end if;
5137
5138 -- Inherit convention from generic unit. Intrinsic convention, as for
5139 -- an instance of unchecked conversion, is not inherited because an
5140 -- explicit Ada instance has been created.
5141
5142 if Has_Convention_Pragma (Gen_Unit)
5143 and then Convention (Gen_Unit) /= Convention_Intrinsic
5144 then
5145 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5146 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5147 end if;
5148
5149 Generate_Definition (Act_Decl_Id);
5150 -- Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id)));
5151 -- ??? needed?
5152 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
5153
5154 -- Inherit all inlining-related flags which apply to the generic in
5155 -- the subprogram and its declaration.
5156
5157 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5158 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5159
5160 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5161 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5162
5163 Set_Has_Pragma_Inline_Always
5164 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5165 Set_Has_Pragma_Inline_Always
5166 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5167
5168 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5169 Check_Elab_Instantiation (N);
5170 end if;
5171
5172 if Is_Dispatching_Operation (Act_Decl_Id)
5173 and then Ada_Version >= Ada_2005
5174 then
5175 declare
5176 Formal : Entity_Id;
5177
5178 begin
5179 Formal := First_Formal (Act_Decl_Id);
5180 while Present (Formal) loop
5181 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5182 and then Is_Controlling_Formal (Formal)
5183 and then not Can_Never_Be_Null (Formal)
5184 then
5185 Error_Msg_NE ("access parameter& is controlling,",
5186 N, Formal);
5187 Error_Msg_NE
5188 ("\corresponding parameter of & must be"
5189 & " explicitly null-excluding", N, Gen_Id);
5190 end if;
5191
5192 Next_Formal (Formal);
5193 end loop;
5194 end;
5195 end if;
5196
5197 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5198
5199 Validate_Categorization_Dependency (N, Act_Decl_Id);
5200
5201 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5202 Inherit_Context (Gen_Decl, N);
5203
5204 Restore_Private_Views (Pack_Id, False);
5205
5206 -- If the context requires a full instantiation, mark node for
5207 -- subsequent construction of the body.
5208
5209 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5210 Check_Forward_Instantiation (Gen_Decl);
5211
5212 -- The wrapper package is always delayed, because it does not
5213 -- constitute a freeze point, but to insure that the freeze
5214 -- node is placed properly, it is created directly when
5215 -- instantiating the body (otherwise the freeze node might
5216 -- appear to early for nested instantiations).
5217
5218 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5219
5220 -- For ASIS purposes, indicate that the wrapper package has
5221 -- replaced the instantiation node.
5222
5223 Rewrite (N, Unit (Parent (N)));
5224 Set_Unit (Parent (N), N);
5225 end if;
5226
5227 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5228
5229 -- Replace instance node for library-level instantiations of
5230 -- intrinsic subprograms, for ASIS use.
5231
5232 Rewrite (N, Unit (Parent (N)));
5233 Set_Unit (Parent (N), N);
5234 end if;
5235
5236 if Parent_Installed then
5237 Remove_Parent;
5238 end if;
5239
5240 Restore_Hidden_Primitives (Vis_Prims_List);
5241 Restore_Env;
5242 Env_Installed := False;
5243 Generic_Renamings.Set_Last (0);
5244 Generic_Renamings_HTable.Reset;
5245
5246 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5247 SPARK_Mode := Save_SM;
5248 SPARK_Mode_Pragma := Save_SMP;
5249 end if;
5250
5251 <<Leave>>
5252 if Has_Aspects (N) then
5253 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5254 end if;
5255
5256 exception
5257 when Instantiation_Error =>
5258 if Parent_Installed then
5259 Remove_Parent;
5260 end if;
5261
5262 if Env_Installed then
5263 Restore_Env;
5264 end if;
5265
5266 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5267 SPARK_Mode := Save_SM;
5268 SPARK_Mode_Pragma := Save_SMP;
5269 end Analyze_Subprogram_Instantiation;
5270
5271 -------------------------
5272 -- Get_Associated_Node --
5273 -------------------------
5274
5275 function Get_Associated_Node (N : Node_Id) return Node_Id is
5276 Assoc : Node_Id;
5277
5278 begin
5279 Assoc := Associated_Node (N);
5280
5281 if Nkind (Assoc) /= Nkind (N) then
5282 return Assoc;
5283
5284 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5285 return Assoc;
5286
5287 else
5288 -- If the node is part of an inner generic, it may itself have been
5289 -- remapped into a further generic copy. Associated_Node is otherwise
5290 -- used for the entity of the node, and will be of a different node
5291 -- kind, or else N has been rewritten as a literal or function call.
5292
5293 while Present (Associated_Node (Assoc))
5294 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5295 loop
5296 Assoc := Associated_Node (Assoc);
5297 end loop;
5298
5299 -- Follow and additional link in case the final node was rewritten.
5300 -- This can only happen with nested generic units.
5301
5302 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5303 and then Present (Associated_Node (Assoc))
5304 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5305 N_Explicit_Dereference,
5306 N_Integer_Literal,
5307 N_Real_Literal,
5308 N_String_Literal))
5309 then
5310 Assoc := Associated_Node (Assoc);
5311 end if;
5312
5313 -- An additional special case: an unconstrained type in an object
5314 -- declaration may have been rewritten as a local subtype constrained
5315 -- by the expression in the declaration. We need to recover the
5316 -- original entity which may be global.
5317
5318 if Present (Original_Node (Assoc))
5319 and then Nkind (Parent (N)) = N_Object_Declaration
5320 then
5321 Assoc := Original_Node (Assoc);
5322 end if;
5323
5324 return Assoc;
5325 end if;
5326 end Get_Associated_Node;
5327
5328 -------------------------------------------
5329 -- Build_Instance_Compilation_Unit_Nodes --
5330 -------------------------------------------
5331
5332 procedure Build_Instance_Compilation_Unit_Nodes
5333 (N : Node_Id;
5334 Act_Body : Node_Id;
5335 Act_Decl : Node_Id)
5336 is
5337 Decl_Cunit : Node_Id;
5338 Body_Cunit : Node_Id;
5339 Citem : Node_Id;
5340 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5341 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5342
5343 begin
5344 -- A new compilation unit node is built for the instance declaration
5345
5346 Decl_Cunit :=
5347 Make_Compilation_Unit (Sloc (N),
5348 Context_Items => Empty_List,
5349 Unit => Act_Decl,
5350 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5351
5352 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5353
5354 -- The new compilation unit is linked to its body, but both share the
5355 -- same file, so we do not set Body_Required on the new unit so as not
5356 -- to create a spurious dependency on a non-existent body in the ali.
5357 -- This simplifies CodePeer unit traversal.
5358
5359 -- We use the original instantiation compilation unit as the resulting
5360 -- compilation unit of the instance, since this is the main unit.
5361
5362 Rewrite (N, Act_Body);
5363
5364 -- Propagate the aspect specifications from the package body template to
5365 -- the instantiated version of the package body.
5366
5367 if Has_Aspects (Act_Body) then
5368 Set_Aspect_Specifications
5369 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5370 end if;
5371
5372 Body_Cunit := Parent (N);
5373
5374 -- The two compilation unit nodes are linked by the Library_Unit field
5375
5376 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5377 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5378
5379 -- Preserve the private nature of the package if needed
5380
5381 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5382
5383 -- If the instance is not the main unit, its context, categorization
5384 -- and elaboration entity are not relevant to the compilation.
5385
5386 if Body_Cunit /= Cunit (Main_Unit) then
5387 Make_Instance_Unit (Body_Cunit, In_Main => False);
5388 return;
5389 end if;
5390
5391 -- The context clause items on the instantiation, which are now attached
5392 -- to the body compilation unit (since the body overwrote the original
5393 -- instantiation node), semantically belong on the spec, so copy them
5394 -- there. It's harmless to leave them on the body as well. In fact one
5395 -- could argue that they belong in both places.
5396
5397 Citem := First (Context_Items (Body_Cunit));
5398 while Present (Citem) loop
5399 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5400 Next (Citem);
5401 end loop;
5402
5403 -- Propagate categorization flags on packages, so that they appear in
5404 -- the ali file for the spec of the unit.
5405
5406 if Ekind (New_Main) = E_Package then
5407 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5408 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5409 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5410 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5411 Set_Is_Remote_Call_Interface
5412 (Old_Main, Is_Remote_Call_Interface (New_Main));
5413 end if;
5414
5415 -- Make entry in Units table, so that binder can generate call to
5416 -- elaboration procedure for body, if any.
5417
5418 Make_Instance_Unit (Body_Cunit, In_Main => True);
5419 Main_Unit_Entity := New_Main;
5420 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5421
5422 -- Build elaboration entity, since the instance may certainly generate
5423 -- elaboration code requiring a flag for protection.
5424
5425 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5426 end Build_Instance_Compilation_Unit_Nodes;
5427
5428 -----------------------------
5429 -- Check_Access_Definition --
5430 -----------------------------
5431
5432 procedure Check_Access_Definition (N : Node_Id) is
5433 begin
5434 pragma Assert
5435 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5436 null;
5437 end Check_Access_Definition;
5438
5439 -----------------------------------
5440 -- Check_Formal_Package_Instance --
5441 -----------------------------------
5442
5443 -- If the formal has specific parameters, they must match those of the
5444 -- actual. Both of them are instances, and the renaming declarations for
5445 -- their formal parameters appear in the same order in both. The analyzed
5446 -- formal has been analyzed in the context of the current instance.
5447
5448 procedure Check_Formal_Package_Instance
5449 (Formal_Pack : Entity_Id;
5450 Actual_Pack : Entity_Id)
5451 is
5452 E1 : Entity_Id := First_Entity (Actual_Pack);
5453 E2 : Entity_Id := First_Entity (Formal_Pack);
5454
5455 Expr1 : Node_Id;
5456 Expr2 : Node_Id;
5457
5458 procedure Check_Mismatch (B : Boolean);
5459 -- Common error routine for mismatch between the parameters of the
5460 -- actual instance and those of the formal package.
5461
5462 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5463 -- The formal may come from a nested formal package, and the actual may
5464 -- have been constant-folded. To determine whether the two denote the
5465 -- same entity we may have to traverse several definitions to recover
5466 -- the ultimate entity that they refer to.
5467
5468 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5469 -- Similarly, if the formal comes from a nested formal package, the
5470 -- actual may designate the formal through multiple renamings, which
5471 -- have to be followed to determine the original variable in question.
5472
5473 --------------------
5474 -- Check_Mismatch --
5475 --------------------
5476
5477 procedure Check_Mismatch (B : Boolean) is
5478 Kind : constant Node_Kind := Nkind (Parent (E2));
5479
5480 begin
5481 if Kind = N_Formal_Type_Declaration then
5482 return;
5483
5484 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5485 N_Formal_Package_Declaration)
5486 or else Kind in N_Formal_Subprogram_Declaration
5487 then
5488 null;
5489
5490 elsif B then
5491 Error_Msg_NE
5492 ("actual for & in actual instance does not match formal",
5493 Parent (Actual_Pack), E1);
5494 end if;
5495 end Check_Mismatch;
5496
5497 --------------------------------
5498 -- Same_Instantiated_Constant --
5499 --------------------------------
5500
5501 function Same_Instantiated_Constant
5502 (E1, E2 : Entity_Id) return Boolean
5503 is
5504 Ent : Entity_Id;
5505
5506 begin
5507 Ent := E2;
5508 while Present (Ent) loop
5509 if E1 = Ent then
5510 return True;
5511
5512 elsif Ekind (Ent) /= E_Constant then
5513 return False;
5514
5515 elsif Is_Entity_Name (Constant_Value (Ent)) then
5516 if Entity (Constant_Value (Ent)) = E1 then
5517 return True;
5518 else
5519 Ent := Entity (Constant_Value (Ent));
5520 end if;
5521
5522 -- The actual may be a constant that has been folded. Recover
5523 -- original name.
5524
5525 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5526 Ent := Entity (Original_Node (Constant_Value (Ent)));
5527 else
5528 return False;
5529 end if;
5530 end loop;
5531
5532 return False;
5533 end Same_Instantiated_Constant;
5534
5535 --------------------------------
5536 -- Same_Instantiated_Variable --
5537 --------------------------------
5538
5539 function Same_Instantiated_Variable
5540 (E1, E2 : Entity_Id) return Boolean
5541 is
5542 function Original_Entity (E : Entity_Id) return Entity_Id;
5543 -- Follow chain of renamings to the ultimate ancestor
5544
5545 ---------------------
5546 -- Original_Entity --
5547 ---------------------
5548
5549 function Original_Entity (E : Entity_Id) return Entity_Id is
5550 Orig : Entity_Id;
5551
5552 begin
5553 Orig := E;
5554 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5555 and then Present (Renamed_Object (Orig))
5556 and then Is_Entity_Name (Renamed_Object (Orig))
5557 loop
5558 Orig := Entity (Renamed_Object (Orig));
5559 end loop;
5560
5561 return Orig;
5562 end Original_Entity;
5563
5564 -- Start of processing for Same_Instantiated_Variable
5565
5566 begin
5567 return Ekind (E1) = Ekind (E2)
5568 and then Original_Entity (E1) = Original_Entity (E2);
5569 end Same_Instantiated_Variable;
5570
5571 -- Start of processing for Check_Formal_Package_Instance
5572
5573 begin
5574 while Present (E1)
5575 and then Present (E2)
5576 loop
5577 exit when Ekind (E1) = E_Package
5578 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5579
5580 -- If the formal is the renaming of the formal package, this
5581 -- is the end of its formal part, which may occur before the
5582 -- end of the formal part in the actual in the presence of
5583 -- defaulted parameters in the formal package.
5584
5585 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5586 and then Renamed_Entity (E2) = Scope (E2);
5587
5588 -- The analysis of the actual may generate additional internal
5589 -- entities. If the formal is defaulted, there is no corresponding
5590 -- analysis and the internal entities must be skipped, until we
5591 -- find corresponding entities again.
5592
5593 if Comes_From_Source (E2)
5594 and then not Comes_From_Source (E1)
5595 and then Chars (E1) /= Chars (E2)
5596 then
5597 while Present (E1)
5598 and then Chars (E1) /= Chars (E2)
5599 loop
5600 Next_Entity (E1);
5601 end loop;
5602 end if;
5603
5604 if No (E1) then
5605 return;
5606
5607 -- If the formal entity comes from a formal declaration, it was
5608 -- defaulted in the formal package, and no check is needed on it.
5609
5610 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5611 goto Next_E;
5612
5613 -- Ditto for defaulted formal subprograms.
5614
5615 elsif Is_Overloadable (E1)
5616 and then Nkind (Unit_Declaration_Node (E2)) in
5617 N_Formal_Subprogram_Declaration
5618 then
5619 goto Next_E;
5620
5621 elsif Is_Type (E1) then
5622
5623 -- Subtypes must statically match. E1, E2 are the local entities
5624 -- that are subtypes of the actuals. Itypes generated for other
5625 -- parameters need not be checked, the check will be performed
5626 -- on the parameters themselves.
5627
5628 -- If E2 is a formal type declaration, it is a defaulted parameter
5629 -- and needs no checking.
5630
5631 if not Is_Itype (E1)
5632 and then not Is_Itype (E2)
5633 then
5634 Check_Mismatch
5635 (not Is_Type (E2)
5636 or else Etype (E1) /= Etype (E2)
5637 or else not Subtypes_Statically_Match (E1, E2));
5638 end if;
5639
5640 elsif Ekind (E1) = E_Constant then
5641
5642 -- IN parameters must denote the same static value, or the same
5643 -- constant, or the literal null.
5644
5645 Expr1 := Expression (Parent (E1));
5646
5647 if Ekind (E2) /= E_Constant then
5648 Check_Mismatch (True);
5649 goto Next_E;
5650 else
5651 Expr2 := Expression (Parent (E2));
5652 end if;
5653
5654 if Is_OK_Static_Expression (Expr1) then
5655 if not Is_OK_Static_Expression (Expr2) then
5656 Check_Mismatch (True);
5657
5658 elsif Is_Discrete_Type (Etype (E1)) then
5659 declare
5660 V1 : constant Uint := Expr_Value (Expr1);
5661 V2 : constant Uint := Expr_Value (Expr2);
5662 begin
5663 Check_Mismatch (V1 /= V2);
5664 end;
5665
5666 elsif Is_Real_Type (Etype (E1)) then
5667 declare
5668 V1 : constant Ureal := Expr_Value_R (Expr1);
5669 V2 : constant Ureal := Expr_Value_R (Expr2);
5670 begin
5671 Check_Mismatch (V1 /= V2);
5672 end;
5673
5674 elsif Is_String_Type (Etype (E1))
5675 and then Nkind (Expr1) = N_String_Literal
5676 then
5677 if Nkind (Expr2) /= N_String_Literal then
5678 Check_Mismatch (True);
5679 else
5680 Check_Mismatch
5681 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5682 end if;
5683 end if;
5684
5685 elsif Is_Entity_Name (Expr1) then
5686 if Is_Entity_Name (Expr2) then
5687 if Entity (Expr1) = Entity (Expr2) then
5688 null;
5689 else
5690 Check_Mismatch
5691 (not Same_Instantiated_Constant
5692 (Entity (Expr1), Entity (Expr2)));
5693 end if;
5694 else
5695 Check_Mismatch (True);
5696 end if;
5697
5698 elsif Is_Entity_Name (Original_Node (Expr1))
5699 and then Is_Entity_Name (Expr2)
5700 and then
5701 Same_Instantiated_Constant
5702 (Entity (Original_Node (Expr1)), Entity (Expr2))
5703 then
5704 null;
5705
5706 elsif Nkind (Expr1) = N_Null then
5707 Check_Mismatch (Nkind (Expr1) /= N_Null);
5708
5709 else
5710 Check_Mismatch (True);
5711 end if;
5712
5713 elsif Ekind (E1) = E_Variable then
5714 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5715
5716 elsif Ekind (E1) = E_Package then
5717 Check_Mismatch
5718 (Ekind (E1) /= Ekind (E2)
5719 or else Renamed_Object (E1) /= Renamed_Object (E2));
5720
5721 elsif Is_Overloadable (E1) then
5722
5723 -- Verify that the actual subprograms match. Note that actuals
5724 -- that are attributes are rewritten as subprograms. If the
5725 -- subprogram in the formal package is defaulted, no check is
5726 -- needed. Note that this can only happen in Ada 2005 when the
5727 -- formal package can be partially parameterized.
5728
5729 if Nkind (Unit_Declaration_Node (E1)) =
5730 N_Subprogram_Renaming_Declaration
5731 and then From_Default (Unit_Declaration_Node (E1))
5732 then
5733 null;
5734
5735 -- If the formal package has an "others" box association that
5736 -- covers this formal, there is no need for a check either.
5737
5738 elsif Nkind (Unit_Declaration_Node (E2)) in
5739 N_Formal_Subprogram_Declaration
5740 and then Box_Present (Unit_Declaration_Node (E2))
5741 then
5742 null;
5743
5744 -- No check needed if subprogram is a defaulted null procedure
5745
5746 elsif No (Alias (E2))
5747 and then Ekind (E2) = E_Procedure
5748 and then
5749 Null_Present (Specification (Unit_Declaration_Node (E2)))
5750 then
5751 null;
5752
5753 -- Otherwise the actual in the formal and the actual in the
5754 -- instantiation of the formal must match, up to renamings.
5755
5756 else
5757 Check_Mismatch
5758 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5759 end if;
5760
5761 else
5762 raise Program_Error;
5763 end if;
5764
5765 <<Next_E>>
5766 Next_Entity (E1);
5767 Next_Entity (E2);
5768 end loop;
5769 end Check_Formal_Package_Instance;
5770
5771 ---------------------------
5772 -- Check_Formal_Packages --
5773 ---------------------------
5774
5775 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5776 E : Entity_Id;
5777 Formal_P : Entity_Id;
5778
5779 begin
5780 -- Iterate through the declarations in the instance, looking for package
5781 -- renaming declarations that denote instances of formal packages. Stop
5782 -- when we find the renaming of the current package itself. The
5783 -- declaration for a formal package without a box is followed by an
5784 -- internal entity that repeats the instantiation.
5785
5786 E := First_Entity (P_Id);
5787 while Present (E) loop
5788 if Ekind (E) = E_Package then
5789 if Renamed_Object (E) = P_Id then
5790 exit;
5791
5792 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5793 null;
5794
5795 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5796 Formal_P := Next_Entity (E);
5797 Check_Formal_Package_Instance (Formal_P, E);
5798
5799 -- After checking, remove the internal validating package. It
5800 -- is only needed for semantic checks, and as it may contain
5801 -- generic formal declarations it should not reach gigi.
5802
5803 Remove (Unit_Declaration_Node (Formal_P));
5804 end if;
5805 end if;
5806
5807 Next_Entity (E);
5808 end loop;
5809 end Check_Formal_Packages;
5810
5811 ---------------------------------
5812 -- Check_Forward_Instantiation --
5813 ---------------------------------
5814
5815 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5816 S : Entity_Id;
5817 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5818
5819 begin
5820 -- The instantiation appears before the generic body if we are in the
5821 -- scope of the unit containing the generic, either in its spec or in
5822 -- the package body, and before the generic body.
5823
5824 if Ekind (Gen_Comp) = E_Package_Body then
5825 Gen_Comp := Spec_Entity (Gen_Comp);
5826 end if;
5827
5828 if In_Open_Scopes (Gen_Comp)
5829 and then No (Corresponding_Body (Decl))
5830 then
5831 S := Current_Scope;
5832
5833 while Present (S)
5834 and then not Is_Compilation_Unit (S)
5835 and then not Is_Child_Unit (S)
5836 loop
5837 if Ekind (S) = E_Package then
5838 Set_Has_Forward_Instantiation (S);
5839 end if;
5840
5841 S := Scope (S);
5842 end loop;
5843 end if;
5844 end Check_Forward_Instantiation;
5845
5846 ---------------------------
5847 -- Check_Generic_Actuals --
5848 ---------------------------
5849
5850 -- The visibility of the actuals may be different between the point of
5851 -- generic instantiation and the instantiation of the body.
5852
5853 procedure Check_Generic_Actuals
5854 (Instance : Entity_Id;
5855 Is_Formal_Box : Boolean)
5856 is
5857 E : Entity_Id;
5858 Astype : Entity_Id;
5859
5860 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5861 -- For a formal that is an array type, the component type is often a
5862 -- previous formal in the same unit. The privacy status of the component
5863 -- type will have been examined earlier in the traversal of the
5864 -- corresponding actuals, and this status should not be modified for
5865 -- the array (sub)type itself. However, if the base type of the array
5866 -- (sub)type is private, its full view must be restored in the body to
5867 -- be consistent with subsequent index subtypes, etc.
5868 --
5869 -- To detect this case we have to rescan the list of formals, which is
5870 -- usually short enough to ignore the resulting inefficiency.
5871
5872 -----------------------------
5873 -- Denotes_Previous_Actual --
5874 -----------------------------
5875
5876 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5877 Prev : Entity_Id;
5878
5879 begin
5880 Prev := First_Entity (Instance);
5881 while Present (Prev) loop
5882 if Is_Type (Prev)
5883 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5884 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5885 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5886 then
5887 return True;
5888
5889 elsif Prev = E then
5890 return False;
5891
5892 else
5893 Next_Entity (Prev);
5894 end if;
5895 end loop;
5896
5897 return False;
5898 end Denotes_Previous_Actual;
5899
5900 -- Start of processing for Check_Generic_Actuals
5901
5902 begin
5903 E := First_Entity (Instance);
5904 while Present (E) loop
5905 if Is_Type (E)
5906 and then Nkind (Parent (E)) = N_Subtype_Declaration
5907 and then Scope (Etype (E)) /= Instance
5908 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5909 then
5910 if Is_Array_Type (E)
5911 and then not Is_Private_Type (Etype (E))
5912 and then Denotes_Previous_Actual (Component_Type (E))
5913 then
5914 null;
5915 else
5916 Check_Private_View (Subtype_Indication (Parent (E)));
5917 end if;
5918
5919 Set_Is_Generic_Actual_Type (E, True);
5920 Set_Is_Hidden (E, False);
5921 Set_Is_Potentially_Use_Visible (E,
5922 In_Use (Instance));
5923
5924 -- We constructed the generic actual type as a subtype of the
5925 -- supplied type. This means that it normally would not inherit
5926 -- subtype specific attributes of the actual, which is wrong for
5927 -- the generic case.
5928
5929 Astype := Ancestor_Subtype (E);
5930
5931 if No (Astype) then
5932
5933 -- This can happen when E is an itype that is the full view of
5934 -- a private type completed, e.g. with a constrained array. In
5935 -- that case, use the first subtype, which will carry size
5936 -- information. The base type itself is unconstrained and will
5937 -- not carry it.
5938
5939 Astype := First_Subtype (E);
5940 end if;
5941
5942 Set_Size_Info (E, (Astype));
5943 Set_RM_Size (E, RM_Size (Astype));
5944 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5945
5946 if Is_Discrete_Or_Fixed_Point_Type (E) then
5947 Set_RM_Size (E, RM_Size (Astype));
5948
5949 -- In nested instances, the base type of an access actual may
5950 -- itself be private, and need to be exchanged.
5951
5952 elsif Is_Access_Type (E)
5953 and then Is_Private_Type (Etype (E))
5954 then
5955 Check_Private_View
5956 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5957 end if;
5958
5959 elsif Ekind (E) = E_Package then
5960
5961 -- If this is the renaming for the current instance, we're done.
5962 -- Otherwise it is a formal package. If the corresponding formal
5963 -- was declared with a box, the (instantiations of the) generic
5964 -- formal part are also visible. Otherwise, ignore the entity
5965 -- created to validate the actuals.
5966
5967 if Renamed_Object (E) = Instance then
5968 exit;
5969
5970 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5971 null;
5972
5973 -- The visibility of a formal of an enclosing generic is already
5974 -- correct.
5975
5976 elsif Denotes_Formal_Package (E) then
5977 null;
5978
5979 elsif Present (Associated_Formal_Package (E))
5980 and then not Is_Generic_Formal (E)
5981 then
5982 if Box_Present (Parent (Associated_Formal_Package (E))) then
5983 Check_Generic_Actuals (Renamed_Object (E), True);
5984
5985 else
5986 Check_Generic_Actuals (Renamed_Object (E), False);
5987 end if;
5988
5989 Set_Is_Hidden (E, False);
5990 end if;
5991
5992 -- If this is a subprogram instance (in a wrapper package) the
5993 -- actual is fully visible.
5994
5995 elsif Is_Wrapper_Package (Instance) then
5996 Set_Is_Hidden (E, False);
5997
5998 -- If the formal package is declared with a box, or if the formal
5999 -- parameter is defaulted, it is visible in the body.
6000
6001 elsif Is_Formal_Box
6002 or else Is_Visible_Formal (E)
6003 then
6004 Set_Is_Hidden (E, False);
6005 end if;
6006
6007 if Ekind (E) = E_Constant then
6008
6009 -- If the type of the actual is a private type declared in the
6010 -- enclosing scope of the generic unit, the body of the generic
6011 -- sees the full view of the type (because it has to appear in
6012 -- the corresponding package body). If the type is private now,
6013 -- exchange views to restore the proper visiblity in the instance.
6014
6015 declare
6016 Typ : constant Entity_Id := Base_Type (Etype (E));
6017 -- The type of the actual
6018
6019 Gen_Id : Entity_Id;
6020 -- The generic unit
6021
6022 Parent_Scope : Entity_Id;
6023 -- The enclosing scope of the generic unit
6024
6025 begin
6026 if Is_Wrapper_Package (Instance) then
6027 Gen_Id :=
6028 Generic_Parent
6029 (Specification
6030 (Unit_Declaration_Node
6031 (Related_Instance (Instance))));
6032 else
6033 Gen_Id :=
6034 Generic_Parent (Package_Specification (Instance));
6035 end if;
6036
6037 Parent_Scope := Scope (Gen_Id);
6038
6039 -- The exchange is only needed if the generic is defined
6040 -- within a package which is not a common ancestor of the
6041 -- scope of the instance, and is not already in scope.
6042
6043 if Is_Private_Type (Typ)
6044 and then Scope (Typ) = Parent_Scope
6045 and then Scope (Instance) /= Parent_Scope
6046 and then Ekind (Parent_Scope) = E_Package
6047 and then not Is_Child_Unit (Gen_Id)
6048 then
6049 Switch_View (Typ);
6050
6051 -- If the type of the entity is a subtype, it may also have
6052 -- to be made visible, together with the base type of its
6053 -- full view, after exchange.
6054
6055 if Is_Private_Type (Etype (E)) then
6056 Switch_View (Etype (E));
6057 Switch_View (Base_Type (Etype (E)));
6058 end if;
6059 end if;
6060 end;
6061 end if;
6062
6063 Next_Entity (E);
6064 end loop;
6065 end Check_Generic_Actuals;
6066
6067 ------------------------------
6068 -- Check_Generic_Child_Unit --
6069 ------------------------------
6070
6071 procedure Check_Generic_Child_Unit
6072 (Gen_Id : Node_Id;
6073 Parent_Installed : in out Boolean)
6074 is
6075 Loc : constant Source_Ptr := Sloc (Gen_Id);
6076 Gen_Par : Entity_Id := Empty;
6077 E : Entity_Id;
6078 Inst_Par : Entity_Id;
6079 S : Node_Id;
6080
6081 function Find_Generic_Child
6082 (Scop : Entity_Id;
6083 Id : Node_Id) return Entity_Id;
6084 -- Search generic parent for possible child unit with the given name
6085
6086 function In_Enclosing_Instance return Boolean;
6087 -- Within an instance of the parent, the child unit may be denoted by
6088 -- a simple name, or an abbreviated expanded name. Examine enclosing
6089 -- scopes to locate a possible parent instantiation.
6090
6091 ------------------------
6092 -- Find_Generic_Child --
6093 ------------------------
6094
6095 function Find_Generic_Child
6096 (Scop : Entity_Id;
6097 Id : Node_Id) return Entity_Id
6098 is
6099 E : Entity_Id;
6100
6101 begin
6102 -- If entity of name is already set, instance has already been
6103 -- resolved, e.g. in an enclosing instantiation.
6104
6105 if Present (Entity (Id)) then
6106 if Scope (Entity (Id)) = Scop then
6107 return Entity (Id);
6108 else
6109 return Empty;
6110 end if;
6111
6112 else
6113 E := First_Entity (Scop);
6114 while Present (E) loop
6115 if Chars (E) = Chars (Id)
6116 and then Is_Child_Unit (E)
6117 then
6118 if Is_Child_Unit (E)
6119 and then not Is_Visible_Lib_Unit (E)
6120 then
6121 Error_Msg_NE
6122 ("generic child unit& is not visible", Gen_Id, E);
6123 end if;
6124
6125 Set_Entity (Id, E);
6126 return E;
6127 end if;
6128
6129 Next_Entity (E);
6130 end loop;
6131
6132 return Empty;
6133 end if;
6134 end Find_Generic_Child;
6135
6136 ---------------------------
6137 -- In_Enclosing_Instance --
6138 ---------------------------
6139
6140 function In_Enclosing_Instance return Boolean is
6141 Enclosing_Instance : Node_Id;
6142 Instance_Decl : Node_Id;
6143
6144 begin
6145 -- We do not inline any call that contains instantiations, except
6146 -- for instantiations of Unchecked_Conversion, so if we are within
6147 -- an inlined body the current instance does not require parents.
6148
6149 if In_Inlined_Body then
6150 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6151 return False;
6152 end if;
6153
6154 -- Loop to check enclosing scopes
6155
6156 Enclosing_Instance := Current_Scope;
6157 while Present (Enclosing_Instance) loop
6158 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6159
6160 if Ekind (Enclosing_Instance) = E_Package
6161 and then Is_Generic_Instance (Enclosing_Instance)
6162 and then Present
6163 (Generic_Parent (Specification (Instance_Decl)))
6164 then
6165 -- Check whether the generic we are looking for is a child of
6166 -- this instance.
6167
6168 E := Find_Generic_Child
6169 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6170 exit when Present (E);
6171
6172 else
6173 E := Empty;
6174 end if;
6175
6176 Enclosing_Instance := Scope (Enclosing_Instance);
6177 end loop;
6178
6179 if No (E) then
6180
6181 -- Not a child unit
6182
6183 Analyze (Gen_Id);
6184 return False;
6185
6186 else
6187 Rewrite (Gen_Id,
6188 Make_Expanded_Name (Loc,
6189 Chars => Chars (E),
6190 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6191 Selector_Name => New_Occurrence_Of (E, Loc)));
6192
6193 Set_Entity (Gen_Id, E);
6194 Set_Etype (Gen_Id, Etype (E));
6195 Parent_Installed := False; -- Already in scope.
6196 return True;
6197 end if;
6198 end In_Enclosing_Instance;
6199
6200 -- Start of processing for Check_Generic_Child_Unit
6201
6202 begin
6203 -- If the name of the generic is given by a selected component, it may
6204 -- be the name of a generic child unit, and the prefix is the name of an
6205 -- instance of the parent, in which case the child unit must be visible.
6206 -- If this instance is not in scope, it must be placed there and removed
6207 -- after instantiation, because what is being instantiated is not the
6208 -- original child, but the corresponding child present in the instance
6209 -- of the parent.
6210
6211 -- If the child is instantiated within the parent, it can be given by
6212 -- a simple name. In this case the instance is already in scope, but
6213 -- the child generic must be recovered from the generic parent as well.
6214
6215 if Nkind (Gen_Id) = N_Selected_Component then
6216 S := Selector_Name (Gen_Id);
6217 Analyze (Prefix (Gen_Id));
6218 Inst_Par := Entity (Prefix (Gen_Id));
6219
6220 if Ekind (Inst_Par) = E_Package
6221 and then Present (Renamed_Object (Inst_Par))
6222 then
6223 Inst_Par := Renamed_Object (Inst_Par);
6224 end if;
6225
6226 if Ekind (Inst_Par) = E_Package then
6227 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6228 Gen_Par := Generic_Parent (Parent (Inst_Par));
6229
6230 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6231 and then
6232 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6233 then
6234 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6235 end if;
6236
6237 elsif Ekind (Inst_Par) = E_Generic_Package
6238 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6239 then
6240 -- A formal package may be a real child package, and not the
6241 -- implicit instance within a parent. In this case the child is
6242 -- not visible and has to be retrieved explicitly as well.
6243
6244 Gen_Par := Inst_Par;
6245 end if;
6246
6247 if Present (Gen_Par) then
6248
6249 -- The prefix denotes an instantiation. The entity itself may be a
6250 -- nested generic, or a child unit.
6251
6252 E := Find_Generic_Child (Gen_Par, S);
6253
6254 if Present (E) then
6255 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6256 Set_Entity (Gen_Id, E);
6257 Set_Etype (Gen_Id, Etype (E));
6258 Set_Entity (S, E);
6259 Set_Etype (S, Etype (E));
6260
6261 -- Indicate that this is a reference to the parent
6262
6263 if In_Extended_Main_Source_Unit (Gen_Id) then
6264 Set_Is_Instantiated (Inst_Par);
6265 end if;
6266
6267 -- A common mistake is to replicate the naming scheme of a
6268 -- hierarchy by instantiating a generic child directly, rather
6269 -- than the implicit child in a parent instance:
6270
6271 -- generic .. package Gpar is ..
6272 -- generic .. package Gpar.Child is ..
6273 -- package Par is new Gpar ();
6274
6275 -- with Gpar.Child;
6276 -- package Par.Child is new Gpar.Child ();
6277 -- rather than Par.Child
6278
6279 -- In this case the instantiation is within Par, which is an
6280 -- instance, but Gpar does not denote Par because we are not IN
6281 -- the instance of Gpar, so this is illegal. The test below
6282 -- recognizes this particular case.
6283
6284 if Is_Child_Unit (E)
6285 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6286 and then (not In_Instance
6287 or else Nkind (Parent (Parent (Gen_Id))) =
6288 N_Compilation_Unit)
6289 then
6290 Error_Msg_N
6291 ("prefix of generic child unit must be instance of parent",
6292 Gen_Id);
6293 end if;
6294
6295 if not In_Open_Scopes (Inst_Par)
6296 and then Nkind (Parent (Gen_Id)) not in
6297 N_Generic_Renaming_Declaration
6298 then
6299 Install_Parent (Inst_Par);
6300 Parent_Installed := True;
6301
6302 elsif In_Open_Scopes (Inst_Par) then
6303
6304 -- If the parent is already installed, install the actuals
6305 -- for its formal packages. This is necessary when the child
6306 -- instance is a child of the parent instance: in this case,
6307 -- the parent is placed on the scope stack but the formal
6308 -- packages are not made visible.
6309
6310 Install_Formal_Packages (Inst_Par);
6311 end if;
6312
6313 else
6314 -- If the generic parent does not contain an entity that
6315 -- corresponds to the selector, the instance doesn't either.
6316 -- Analyzing the node will yield the appropriate error message.
6317 -- If the entity is not a child unit, then it is an inner
6318 -- generic in the parent.
6319
6320 Analyze (Gen_Id);
6321 end if;
6322
6323 else
6324 Analyze (Gen_Id);
6325
6326 if Is_Child_Unit (Entity (Gen_Id))
6327 and then
6328 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6329 and then not In_Open_Scopes (Inst_Par)
6330 then
6331 Install_Parent (Inst_Par);
6332 Parent_Installed := True;
6333
6334 -- The generic unit may be the renaming of the implicit child
6335 -- present in an instance. In that case the parent instance is
6336 -- obtained from the name of the renamed entity.
6337
6338 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6339 and then Present (Renamed_Entity (Entity (Gen_Id)))
6340 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6341 then
6342 declare
6343 Renamed_Package : constant Node_Id :=
6344 Name (Parent (Entity (Gen_Id)));
6345 begin
6346 if Nkind (Renamed_Package) = N_Expanded_Name then
6347 Inst_Par := Entity (Prefix (Renamed_Package));
6348 Install_Parent (Inst_Par);
6349 Parent_Installed := True;
6350 end if;
6351 end;
6352 end if;
6353 end if;
6354
6355 elsif Nkind (Gen_Id) = N_Expanded_Name then
6356
6357 -- Entity already present, analyze prefix, whose meaning may be
6358 -- an instance in the current context. If it is an instance of
6359 -- a relative within another, the proper parent may still have
6360 -- to be installed, if they are not of the same generation.
6361
6362 Analyze (Prefix (Gen_Id));
6363
6364 -- In the unlikely case that a local declaration hides the name
6365 -- of the parent package, locate it on the homonym chain. If the
6366 -- context is an instance of the parent, the renaming entity is
6367 -- flagged as such.
6368
6369 Inst_Par := Entity (Prefix (Gen_Id));
6370 while Present (Inst_Par)
6371 and then not Is_Package_Or_Generic_Package (Inst_Par)
6372 loop
6373 Inst_Par := Homonym (Inst_Par);
6374 end loop;
6375
6376 pragma Assert (Present (Inst_Par));
6377 Set_Entity (Prefix (Gen_Id), Inst_Par);
6378
6379 if In_Enclosing_Instance then
6380 null;
6381
6382 elsif Present (Entity (Gen_Id))
6383 and then Is_Child_Unit (Entity (Gen_Id))
6384 and then not In_Open_Scopes (Inst_Par)
6385 then
6386 Install_Parent (Inst_Par);
6387 Parent_Installed := True;
6388 end if;
6389
6390 elsif In_Enclosing_Instance then
6391
6392 -- The child unit is found in some enclosing scope
6393
6394 null;
6395
6396 else
6397 Analyze (Gen_Id);
6398
6399 -- If this is the renaming of the implicit child in a parent
6400 -- instance, recover the parent name and install it.
6401
6402 if Is_Entity_Name (Gen_Id) then
6403 E := Entity (Gen_Id);
6404
6405 if Is_Generic_Unit (E)
6406 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6407 and then Is_Child_Unit (Renamed_Object (E))
6408 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6409 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6410 then
6411 Rewrite (Gen_Id,
6412 New_Copy_Tree (Name (Parent (E))));
6413 Inst_Par := Entity (Prefix (Gen_Id));
6414
6415 if not In_Open_Scopes (Inst_Par) then
6416 Install_Parent (Inst_Par);
6417 Parent_Installed := True;
6418 end if;
6419
6420 -- If it is a child unit of a non-generic parent, it may be
6421 -- use-visible and given by a direct name. Install parent as
6422 -- for other cases.
6423
6424 elsif Is_Generic_Unit (E)
6425 and then Is_Child_Unit (E)
6426 and then
6427 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6428 and then not Is_Generic_Unit (Scope (E))
6429 then
6430 if not In_Open_Scopes (Scope (E)) then
6431 Install_Parent (Scope (E));
6432 Parent_Installed := True;
6433 end if;
6434 end if;
6435 end if;
6436 end if;
6437 end Check_Generic_Child_Unit;
6438
6439 -----------------------------
6440 -- Check_Hidden_Child_Unit --
6441 -----------------------------
6442
6443 procedure Check_Hidden_Child_Unit
6444 (N : Node_Id;
6445 Gen_Unit : Entity_Id;
6446 Act_Decl_Id : Entity_Id)
6447 is
6448 Gen_Id : constant Node_Id := Name (N);
6449
6450 begin
6451 if Is_Child_Unit (Gen_Unit)
6452 and then Is_Child_Unit (Act_Decl_Id)
6453 and then Nkind (Gen_Id) = N_Expanded_Name
6454 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6455 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6456 then
6457 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6458 Error_Msg_NE
6459 ("generic unit & is implicitly declared in &",
6460 Defining_Unit_Name (N), Gen_Unit);
6461 Error_Msg_N ("\instance must have different name",
6462 Defining_Unit_Name (N));
6463 end if;
6464 end Check_Hidden_Child_Unit;
6465
6466 ------------------------
6467 -- Check_Private_View --
6468 ------------------------
6469
6470 procedure Check_Private_View (N : Node_Id) is
6471 T : constant Entity_Id := Etype (N);
6472 BT : Entity_Id;
6473
6474 begin
6475 -- Exchange views if the type was not private in the generic but is
6476 -- private at the point of instantiation. Do not exchange views if
6477 -- the scope of the type is in scope. This can happen if both generic
6478 -- and instance are sibling units, or if type is defined in a parent.
6479 -- In this case the visibility of the type will be correct for all
6480 -- semantic checks.
6481
6482 if Present (T) then
6483 BT := Base_Type (T);
6484
6485 if Is_Private_Type (T)
6486 and then not Has_Private_View (N)
6487 and then Present (Full_View (T))
6488 and then not In_Open_Scopes (Scope (T))
6489 then
6490 -- In the generic, the full type was visible. Save the private
6491 -- entity, for subsequent exchange.
6492
6493 Switch_View (T);
6494
6495 elsif Has_Private_View (N)
6496 and then not Is_Private_Type (T)
6497 and then not Has_Been_Exchanged (T)
6498 and then Etype (Get_Associated_Node (N)) /= T
6499 then
6500 -- Only the private declaration was visible in the generic. If
6501 -- the type appears in a subtype declaration, the subtype in the
6502 -- instance must have a view compatible with that of its parent,
6503 -- which must be exchanged (see corresponding code in Restore_
6504 -- Private_Views). Otherwise, if the type is defined in a parent
6505 -- unit, leave full visibility within instance, which is safe.
6506
6507 if In_Open_Scopes (Scope (Base_Type (T)))
6508 and then not Is_Private_Type (Base_Type (T))
6509 and then Comes_From_Source (Base_Type (T))
6510 then
6511 null;
6512
6513 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6514 or else not In_Private_Part (Scope (Base_Type (T)))
6515 then
6516 Prepend_Elmt (T, Exchanged_Views);
6517 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6518 end if;
6519
6520 -- For composite types with inconsistent representation exchange
6521 -- component types accordingly.
6522
6523 elsif Is_Access_Type (T)
6524 and then Is_Private_Type (Designated_Type (T))
6525 and then not Has_Private_View (N)
6526 and then Present (Full_View (Designated_Type (T)))
6527 then
6528 Switch_View (Designated_Type (T));
6529
6530 elsif Is_Array_Type (T) then
6531 if Is_Private_Type (Component_Type (T))
6532 and then not Has_Private_View (N)
6533 and then Present (Full_View (Component_Type (T)))
6534 then
6535 Switch_View (Component_Type (T));
6536 end if;
6537
6538 -- The normal exchange mechanism relies on the setting of a
6539 -- flag on the reference in the generic. However, an additional
6540 -- mechanism is needed for types that are not explicitly
6541 -- mentioned in the generic, but may be needed in expanded code
6542 -- in the instance. This includes component types of arrays and
6543 -- designated types of access types. This processing must also
6544 -- include the index types of arrays which we take care of here.
6545
6546 declare
6547 Indx : Node_Id;
6548 Typ : Entity_Id;
6549
6550 begin
6551 Indx := First_Index (T);
6552 while Present (Indx) loop
6553 Typ := Base_Type (Etype (Indx));
6554
6555 if Is_Private_Type (Typ)
6556 and then Present (Full_View (Typ))
6557 then
6558 Switch_View (Typ);
6559 end if;
6560
6561 Next_Index (Indx);
6562 end loop;
6563 end;
6564
6565 elsif Is_Private_Type (T)
6566 and then Present (Full_View (T))
6567 and then Is_Array_Type (Full_View (T))
6568 and then Is_Private_Type (Component_Type (Full_View (T)))
6569 then
6570 Switch_View (T);
6571
6572 -- Finally, a non-private subtype may have a private base type, which
6573 -- must be exchanged for consistency. This can happen when a package
6574 -- body is instantiated, when the scope stack is empty but in fact
6575 -- the subtype and the base type are declared in an enclosing scope.
6576
6577 -- Note that in this case we introduce an inconsistency in the view
6578 -- set, because we switch the base type BT, but there could be some
6579 -- private dependent subtypes of BT which remain unswitched. Such
6580 -- subtypes might need to be switched at a later point (see specific
6581 -- provision for that case in Switch_View).
6582
6583 elsif not Is_Private_Type (T)
6584 and then not Has_Private_View (N)
6585 and then Is_Private_Type (BT)
6586 and then Present (Full_View (BT))
6587 and then not Is_Generic_Type (BT)
6588 and then not In_Open_Scopes (BT)
6589 then
6590 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6591 Exchange_Declarations (BT);
6592 end if;
6593 end if;
6594 end Check_Private_View;
6595
6596 -----------------------------
6597 -- Check_Hidden_Primitives --
6598 -----------------------------
6599
6600 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6601 Actual : Node_Id;
6602 Gen_T : Entity_Id;
6603 Result : Elist_Id := No_Elist;
6604
6605 begin
6606 if No (Assoc_List) then
6607 return No_Elist;
6608 end if;
6609
6610 -- Traverse the list of associations between formals and actuals
6611 -- searching for renamings of tagged types
6612
6613 Actual := First (Assoc_List);
6614 while Present (Actual) loop
6615 if Nkind (Actual) = N_Subtype_Declaration then
6616 Gen_T := Generic_Parent_Type (Actual);
6617
6618 if Present (Gen_T)
6619 and then Is_Tagged_Type (Gen_T)
6620 then
6621 -- Traverse the list of primitives of the actual types
6622 -- searching for hidden primitives that are visible in the
6623 -- corresponding generic formal; leave them visible and
6624 -- append them to Result to restore their decoration later.
6625
6626 Install_Hidden_Primitives
6627 (Prims_List => Result,
6628 Gen_T => Gen_T,
6629 Act_T => Entity (Subtype_Indication (Actual)));
6630 end if;
6631 end if;
6632
6633 Next (Actual);
6634 end loop;
6635
6636 return Result;
6637 end Check_Hidden_Primitives;
6638
6639 --------------------------
6640 -- Contains_Instance_Of --
6641 --------------------------
6642
6643 function Contains_Instance_Of
6644 (Inner : Entity_Id;
6645 Outer : Entity_Id;
6646 N : Node_Id) return Boolean
6647 is
6648 Elmt : Elmt_Id;
6649 Scop : Entity_Id;
6650
6651 begin
6652 Scop := Outer;
6653
6654 -- Verify that there are no circular instantiations. We check whether
6655 -- the unit contains an instance of the current scope or some enclosing
6656 -- scope (in case one of the instances appears in a subunit). Longer
6657 -- circularities involving subunits might seem too pathological to
6658 -- consider, but they were not too pathological for the authors of
6659 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6660 -- enclosing generic scopes as containing an instance.
6661
6662 loop
6663 -- Within a generic subprogram body, the scope is not generic, to
6664 -- allow for recursive subprograms. Use the declaration to determine
6665 -- whether this is a generic unit.
6666
6667 if Ekind (Scop) = E_Generic_Package
6668 or else (Is_Subprogram (Scop)
6669 and then Nkind (Unit_Declaration_Node (Scop)) =
6670 N_Generic_Subprogram_Declaration)
6671 then
6672 Elmt := First_Elmt (Inner_Instances (Inner));
6673
6674 while Present (Elmt) loop
6675 if Node (Elmt) = Scop then
6676 Error_Msg_Node_2 := Inner;
6677 Error_Msg_NE
6678 ("circular Instantiation: & instantiated within &!",
6679 N, Scop);
6680 return True;
6681
6682 elsif Node (Elmt) = Inner then
6683 return True;
6684
6685 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6686 Error_Msg_Node_2 := Inner;
6687 Error_Msg_NE
6688 ("circular Instantiation: & instantiated within &!",
6689 N, Node (Elmt));
6690 return True;
6691 end if;
6692
6693 Next_Elmt (Elmt);
6694 end loop;
6695
6696 -- Indicate that Inner is being instantiated within Scop
6697
6698 Append_Elmt (Inner, Inner_Instances (Scop));
6699 end if;
6700
6701 if Scop = Standard_Standard then
6702 exit;
6703 else
6704 Scop := Scope (Scop);
6705 end if;
6706 end loop;
6707
6708 return False;
6709 end Contains_Instance_Of;
6710
6711 -----------------------
6712 -- Copy_Generic_Node --
6713 -----------------------
6714
6715 function Copy_Generic_Node
6716 (N : Node_Id;
6717 Parent_Id : Node_Id;
6718 Instantiating : Boolean) return Node_Id
6719 is
6720 Ent : Entity_Id;
6721 New_N : Node_Id;
6722
6723 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6724 -- Check the given value of one of the Fields referenced by the current
6725 -- node to determine whether to copy it recursively. The field may hold
6726 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6727 -- Char) in which case it need not be copied.
6728
6729 procedure Copy_Descendants;
6730 -- Common utility for various nodes
6731
6732 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6733 -- Make copy of element list
6734
6735 function Copy_Generic_List
6736 (L : List_Id;
6737 Parent_Id : Node_Id) return List_Id;
6738 -- Apply Copy_Node recursively to the members of a node list
6739
6740 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6741 -- True if an identifier is part of the defining program unit name of
6742 -- a child unit. The entity of such an identifier must be kept (for
6743 -- ASIS use) even though as the name of an enclosing generic it would
6744 -- otherwise not be preserved in the generic tree.
6745
6746 ----------------------
6747 -- Copy_Descendants --
6748 ----------------------
6749
6750 procedure Copy_Descendants is
6751
6752 use Atree.Unchecked_Access;
6753 -- This code section is part of the implementation of an untyped
6754 -- tree traversal, so it needs direct access to node fields.
6755
6756 begin
6757 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6758 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6759 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6760 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6761 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6762 end Copy_Descendants;
6763
6764 -----------------------------
6765 -- Copy_Generic_Descendant --
6766 -----------------------------
6767
6768 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6769 begin
6770 if D = Union_Id (Empty) then
6771 return D;
6772
6773 elsif D in Node_Range then
6774 return Union_Id
6775 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6776
6777 elsif D in List_Range then
6778 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6779
6780 elsif D in Elist_Range then
6781 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6782
6783 -- Nothing else is copyable (e.g. Uint values), return as is
6784
6785 else
6786 return D;
6787 end if;
6788 end Copy_Generic_Descendant;
6789
6790 ------------------------
6791 -- Copy_Generic_Elist --
6792 ------------------------
6793
6794 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6795 M : Elmt_Id;
6796 L : Elist_Id;
6797
6798 begin
6799 if Present (E) then
6800 L := New_Elmt_List;
6801 M := First_Elmt (E);
6802 while Present (M) loop
6803 Append_Elmt
6804 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6805 Next_Elmt (M);
6806 end loop;
6807
6808 return L;
6809
6810 else
6811 return No_Elist;
6812 end if;
6813 end Copy_Generic_Elist;
6814
6815 -----------------------
6816 -- Copy_Generic_List --
6817 -----------------------
6818
6819 function Copy_Generic_List
6820 (L : List_Id;
6821 Parent_Id : Node_Id) return List_Id
6822 is
6823 N : Node_Id;
6824 New_L : List_Id;
6825
6826 begin
6827 if Present (L) then
6828 New_L := New_List;
6829 Set_Parent (New_L, Parent_Id);
6830
6831 N := First (L);
6832 while Present (N) loop
6833 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6834 Next (N);
6835 end loop;
6836
6837 return New_L;
6838
6839 else
6840 return No_List;
6841 end if;
6842 end Copy_Generic_List;
6843
6844 ---------------------------
6845 -- In_Defining_Unit_Name --
6846 ---------------------------
6847
6848 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6849 begin
6850 return Present (Parent (Nam))
6851 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6852 or else
6853 (Nkind (Parent (Nam)) = N_Expanded_Name
6854 and then In_Defining_Unit_Name (Parent (Nam))));
6855 end In_Defining_Unit_Name;
6856
6857 -- Start of processing for Copy_Generic_Node
6858
6859 begin
6860 if N = Empty then
6861 return N;
6862 end if;
6863
6864 New_N := New_Copy (N);
6865
6866 -- Copy aspects if present
6867
6868 if Has_Aspects (N) then
6869 Set_Has_Aspects (New_N, False);
6870 Set_Aspect_Specifications
6871 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6872 end if;
6873
6874 if Instantiating then
6875 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6876 end if;
6877
6878 if not Is_List_Member (N) then
6879 Set_Parent (New_N, Parent_Id);
6880 end if;
6881
6882 -- If defining identifier, then all fields have been copied already
6883
6884 if Nkind (New_N) in N_Entity then
6885 null;
6886
6887 -- Special casing for identifiers and other entity names and operators
6888
6889 elsif Nkind_In (New_N, N_Identifier,
6890 N_Character_Literal,
6891 N_Expanded_Name,
6892 N_Operator_Symbol)
6893 or else Nkind (New_N) in N_Op
6894 then
6895 if not Instantiating then
6896
6897 -- Link both nodes in order to assign subsequently the entity of
6898 -- the copy to the original node, in case this is a global
6899 -- reference.
6900
6901 Set_Associated_Node (N, New_N);
6902
6903 -- If we are within an instantiation, this is a nested generic
6904 -- that has already been analyzed at the point of definition.
6905 -- We must preserve references that were global to the enclosing
6906 -- parent at that point. Other occurrences, whether global or
6907 -- local to the current generic, must be resolved anew, so we
6908 -- reset the entity in the generic copy. A global reference has a
6909 -- smaller depth than the parent, or else the same depth in case
6910 -- both are distinct compilation units.
6911
6912 -- A child unit is implicitly declared within the enclosing parent
6913 -- but is in fact global to it, and must be preserved.
6914
6915 -- It is also possible for Current_Instantiated_Parent to be
6916 -- defined, and for this not to be a nested generic, namely if
6917 -- the unit is loaded through Rtsfind. In that case, the entity of
6918 -- New_N is only a link to the associated node, and not a defining
6919 -- occurrence.
6920
6921 -- The entities for parent units in the defining_program_unit of a
6922 -- generic child unit are established when the context of the unit
6923 -- is first analyzed, before the generic copy is made. They are
6924 -- preserved in the copy for use in ASIS queries.
6925
6926 Ent := Entity (New_N);
6927
6928 if No (Current_Instantiated_Parent.Gen_Id) then
6929 if No (Ent)
6930 or else Nkind (Ent) /= N_Defining_Identifier
6931 or else not In_Defining_Unit_Name (N)
6932 then
6933 Set_Associated_Node (New_N, Empty);
6934 end if;
6935
6936 elsif No (Ent)
6937 or else
6938 not Nkind_In (Ent, N_Defining_Identifier,
6939 N_Defining_Character_Literal,
6940 N_Defining_Operator_Symbol)
6941 or else No (Scope (Ent))
6942 or else
6943 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6944 and then not Is_Child_Unit (Ent))
6945 or else
6946 (Scope_Depth (Scope (Ent)) >
6947 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6948 and then
6949 Get_Source_Unit (Ent) =
6950 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6951 then
6952 Set_Associated_Node (New_N, Empty);
6953 end if;
6954
6955 -- Case of instantiating identifier or some other name or operator
6956
6957 else
6958 -- If the associated node is still defined, the entity in it
6959 -- is global, and must be copied to the instance. If this copy
6960 -- is being made for a body to inline, it is applied to an
6961 -- instantiated tree, and the entity is already present and
6962 -- must be also preserved.
6963
6964 declare
6965 Assoc : constant Node_Id := Get_Associated_Node (N);
6966
6967 begin
6968 if Present (Assoc) then
6969 if Nkind (Assoc) = Nkind (N) then
6970 Set_Entity (New_N, Entity (Assoc));
6971 Check_Private_View (N);
6972
6973 -- The name in the call may be a selected component if the
6974 -- call has not been analyzed yet, as may be the case for
6975 -- pre/post conditions in a generic unit.
6976
6977 elsif Nkind (Assoc) = N_Function_Call
6978 and then Is_Entity_Name (Name (Assoc))
6979 then
6980 Set_Entity (New_N, Entity (Name (Assoc)));
6981
6982 elsif Nkind_In (Assoc, N_Defining_Identifier,
6983 N_Defining_Character_Literal,
6984 N_Defining_Operator_Symbol)
6985 and then Expander_Active
6986 then
6987 -- Inlining case: we are copying a tree that contains
6988 -- global entities, which are preserved in the copy to be
6989 -- used for subsequent inlining.
6990
6991 null;
6992
6993 else
6994 Set_Entity (New_N, Empty);
6995 end if;
6996 end if;
6997 end;
6998 end if;
6999
7000 -- For expanded name, we must copy the Prefix and Selector_Name
7001
7002 if Nkind (N) = N_Expanded_Name then
7003 Set_Prefix
7004 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7005
7006 Set_Selector_Name (New_N,
7007 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7008
7009 -- For operators, we must copy the right operand
7010
7011 elsif Nkind (N) in N_Op then
7012 Set_Right_Opnd (New_N,
7013 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7014
7015 -- And for binary operators, the left operand as well
7016
7017 if Nkind (N) in N_Binary_Op then
7018 Set_Left_Opnd (New_N,
7019 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7020 end if;
7021 end if;
7022
7023 -- Special casing for stubs
7024
7025 elsif Nkind (N) in N_Body_Stub then
7026
7027 -- In any case, we must copy the specification or defining
7028 -- identifier as appropriate.
7029
7030 if Nkind (N) = N_Subprogram_Body_Stub then
7031 Set_Specification (New_N,
7032 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7033
7034 else
7035 Set_Defining_Identifier (New_N,
7036 Copy_Generic_Node
7037 (Defining_Identifier (N), New_N, Instantiating));
7038 end if;
7039
7040 -- If we are not instantiating, then this is where we load and
7041 -- analyze subunits, i.e. at the point where the stub occurs. A
7042 -- more permissive system might defer this analysis to the point
7043 -- of instantiation, but this seems too complicated for now.
7044
7045 if not Instantiating then
7046 declare
7047 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7048 Subunit : Node_Id;
7049 Unum : Unit_Number_Type;
7050 New_Body : Node_Id;
7051
7052 begin
7053 -- Make sure that, if it is a subunit of the main unit that is
7054 -- preprocessed and if -gnateG is specified, the preprocessed
7055 -- file will be written.
7056
7057 Lib.Analysing_Subunit_Of_Main :=
7058 Lib.In_Extended_Main_Source_Unit (N);
7059 Unum :=
7060 Load_Unit
7061 (Load_Name => Subunit_Name,
7062 Required => False,
7063 Subunit => True,
7064 Error_Node => N);
7065 Lib.Analysing_Subunit_Of_Main := False;
7066
7067 -- If the proper body is not found, a warning message will be
7068 -- emitted when analyzing the stub, or later at the point of
7069 -- instantiation. Here we just leave the stub as is.
7070
7071 if Unum = No_Unit then
7072 Subunits_Missing := True;
7073 goto Subunit_Not_Found;
7074 end if;
7075
7076 Subunit := Cunit (Unum);
7077
7078 if Nkind (Unit (Subunit)) /= N_Subunit then
7079 Error_Msg_N
7080 ("found child unit instead of expected SEPARATE subunit",
7081 Subunit);
7082 Error_Msg_Sloc := Sloc (N);
7083 Error_Msg_N ("\to complete stub #", Subunit);
7084 goto Subunit_Not_Found;
7085 end if;
7086
7087 -- We must create a generic copy of the subunit, in order to
7088 -- perform semantic analysis on it, and we must replace the
7089 -- stub in the original generic unit with the subunit, in order
7090 -- to preserve non-local references within.
7091
7092 -- Only the proper body needs to be copied. Library_Unit and
7093 -- context clause are simply inherited by the generic copy.
7094 -- Note that the copy (which may be recursive if there are
7095 -- nested subunits) must be done first, before attaching it to
7096 -- the enclosing generic.
7097
7098 New_Body :=
7099 Copy_Generic_Node
7100 (Proper_Body (Unit (Subunit)),
7101 Empty, Instantiating => False);
7102
7103 -- Now place the original proper body in the original generic
7104 -- unit. This is a body, not a compilation unit.
7105
7106 Rewrite (N, Proper_Body (Unit (Subunit)));
7107 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7108 Set_Was_Originally_Stub (N);
7109
7110 -- Finally replace the body of the subunit with its copy, and
7111 -- make this new subunit into the library unit of the generic
7112 -- copy, which does not have stubs any longer.
7113
7114 Set_Proper_Body (Unit (Subunit), New_Body);
7115 Set_Library_Unit (New_N, Subunit);
7116 Inherit_Context (Unit (Subunit), N);
7117 end;
7118
7119 -- If we are instantiating, this must be an error case, since
7120 -- otherwise we would have replaced the stub node by the proper body
7121 -- that corresponds. So just ignore it in the copy (i.e. we have
7122 -- copied it, and that is good enough).
7123
7124 else
7125 null;
7126 end if;
7127
7128 <<Subunit_Not_Found>> null;
7129
7130 -- If the node is a compilation unit, it is the subunit of a stub, which
7131 -- has been loaded already (see code below). In this case, the library
7132 -- unit field of N points to the parent unit (which is a compilation
7133 -- unit) and need not (and cannot) be copied.
7134
7135 -- When the proper body of the stub is analyzed, the library_unit link
7136 -- is used to establish the proper context (see sem_ch10).
7137
7138 -- The other fields of a compilation unit are copied as usual
7139
7140 elsif Nkind (N) = N_Compilation_Unit then
7141
7142 -- This code can only be executed when not instantiating, because in
7143 -- the copy made for an instantiation, the compilation unit node has
7144 -- disappeared at the point that a stub is replaced by its proper
7145 -- body.
7146
7147 pragma Assert (not Instantiating);
7148
7149 Set_Context_Items (New_N,
7150 Copy_Generic_List (Context_Items (N), New_N));
7151
7152 Set_Unit (New_N,
7153 Copy_Generic_Node (Unit (N), New_N, False));
7154
7155 Set_First_Inlined_Subprogram (New_N,
7156 Copy_Generic_Node
7157 (First_Inlined_Subprogram (N), New_N, False));
7158
7159 Set_Aux_Decls_Node (New_N,
7160 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7161
7162 -- For an assignment node, the assignment is known to be semantically
7163 -- legal if we are instantiating the template. This avoids incorrect
7164 -- diagnostics in generated code.
7165
7166 elsif Nkind (N) = N_Assignment_Statement then
7167
7168 -- Copy name and expression fields in usual manner
7169
7170 Set_Name (New_N,
7171 Copy_Generic_Node (Name (N), New_N, Instantiating));
7172
7173 Set_Expression (New_N,
7174 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7175
7176 if Instantiating then
7177 Set_Assignment_OK (Name (New_N), True);
7178 end if;
7179
7180 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7181 if not Instantiating then
7182 Set_Associated_Node (N, New_N);
7183
7184 else
7185 if Present (Get_Associated_Node (N))
7186 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7187 then
7188 -- In the generic the aggregate has some composite type. If at
7189 -- the point of instantiation the type has a private view,
7190 -- install the full view (and that of its ancestors, if any).
7191
7192 declare
7193 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7194 Rt : Entity_Id;
7195
7196 begin
7197 if Present (T)
7198 and then Is_Private_Type (T)
7199 then
7200 Switch_View (T);
7201 end if;
7202
7203 if Present (T)
7204 and then Is_Tagged_Type (T)
7205 and then Is_Derived_Type (T)
7206 then
7207 Rt := Root_Type (T);
7208
7209 loop
7210 T := Etype (T);
7211
7212 if Is_Private_Type (T) then
7213 Switch_View (T);
7214 end if;
7215
7216 exit when T = Rt;
7217 end loop;
7218 end if;
7219 end;
7220 end if;
7221 end if;
7222
7223 -- Do not copy the associated node, which points to the generic copy
7224 -- of the aggregate.
7225
7226 declare
7227 use Atree.Unchecked_Access;
7228 -- This code section is part of the implementation of an untyped
7229 -- tree traversal, so it needs direct access to node fields.
7230
7231 begin
7232 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7233 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7234 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7235 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7236 end;
7237
7238 -- Allocators do not have an identifier denoting the access type, so we
7239 -- must locate it through the expression to check whether the views are
7240 -- consistent.
7241
7242 elsif Nkind (N) = N_Allocator
7243 and then Nkind (Expression (N)) = N_Qualified_Expression
7244 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7245 and then Instantiating
7246 then
7247 declare
7248 T : constant Node_Id :=
7249 Get_Associated_Node (Subtype_Mark (Expression (N)));
7250 Acc_T : Entity_Id;
7251
7252 begin
7253 if Present (T) then
7254
7255 -- Retrieve the allocator node in the generic copy
7256
7257 Acc_T := Etype (Parent (Parent (T)));
7258 if Present (Acc_T)
7259 and then Is_Private_Type (Acc_T)
7260 then
7261 Switch_View (Acc_T);
7262 end if;
7263 end if;
7264
7265 Copy_Descendants;
7266 end;
7267
7268 -- For a proper body, we must catch the case of a proper body that
7269 -- replaces a stub. This represents the point at which a separate
7270 -- compilation unit, and hence template file, may be referenced, so we
7271 -- must make a new source instantiation entry for the template of the
7272 -- subunit, and ensure that all nodes in the subunit are adjusted using
7273 -- this new source instantiation entry.
7274
7275 elsif Nkind (N) in N_Proper_Body then
7276 declare
7277 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7278
7279 begin
7280 if Instantiating and then Was_Originally_Stub (N) then
7281 Create_Instantiation_Source
7282 (Instantiation_Node,
7283 Defining_Entity (N),
7284 False,
7285 S_Adjustment);
7286 end if;
7287
7288 -- Now copy the fields of the proper body, using the new
7289 -- adjustment factor if one was needed as per test above.
7290
7291 Copy_Descendants;
7292
7293 -- Restore the original adjustment factor in case changed
7294
7295 S_Adjustment := Save_Adjustment;
7296 end;
7297
7298 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
7299 -- generic unit, not to the instantiating unit.
7300
7301 elsif Nkind (N) = N_Pragma and then Instantiating then
7302 declare
7303 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
7304 begin
7305 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
7306 New_N := Make_Null_Statement (Sloc (N));
7307 else
7308 Copy_Descendants;
7309 end if;
7310 end;
7311
7312 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7313
7314 -- No descendant fields need traversing
7315
7316 null;
7317
7318 elsif Nkind (N) = N_String_Literal
7319 and then Present (Etype (N))
7320 and then Instantiating
7321 then
7322 -- If the string is declared in an outer scope, the string_literal
7323 -- subtype created for it may have the wrong scope. We force the
7324 -- reanalysis of the constant to generate a new itype in the proper
7325 -- context.
7326
7327 Set_Etype (New_N, Empty);
7328 Set_Analyzed (New_N, False);
7329
7330 -- For the remaining nodes, copy their descendants recursively
7331
7332 else
7333 Copy_Descendants;
7334
7335 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7336 Set_Generic_Parent (Specification (New_N), N);
7337
7338 -- Should preserve Corresponding_Spec??? (12.3(14))
7339 end if;
7340 end if;
7341
7342 return New_N;
7343 end Copy_Generic_Node;
7344
7345 ----------------------------
7346 -- Denotes_Formal_Package --
7347 ----------------------------
7348
7349 function Denotes_Formal_Package
7350 (Pack : Entity_Id;
7351 On_Exit : Boolean := False;
7352 Instance : Entity_Id := Empty) return Boolean
7353 is
7354 Par : Entity_Id;
7355 Scop : constant Entity_Id := Scope (Pack);
7356 E : Entity_Id;
7357
7358 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7359 -- The package in question may be an actual for a previous formal
7360 -- package P of the current instance, so examine its actuals as well.
7361 -- This must be recursive over other formal packages.
7362
7363 ----------------------------------
7364 -- Is_Actual_Of_Previous_Formal --
7365 ----------------------------------
7366
7367 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7368 E1 : Entity_Id;
7369
7370 begin
7371 E1 := First_Entity (P);
7372 while Present (E1) and then E1 /= Instance loop
7373 if Ekind (E1) = E_Package
7374 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7375 then
7376 if Renamed_Object (E1) = Pack then
7377 return True;
7378
7379 elsif E1 = P or else Renamed_Object (E1) = P then
7380 return False;
7381
7382 elsif Is_Actual_Of_Previous_Formal (E1) then
7383 return True;
7384 end if;
7385 end if;
7386
7387 Next_Entity (E1);
7388 end loop;
7389
7390 return False;
7391 end Is_Actual_Of_Previous_Formal;
7392
7393 -- Start of processing for Denotes_Formal_Package
7394
7395 begin
7396 if On_Exit then
7397 Par :=
7398 Instance_Envs.Table
7399 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7400 else
7401 Par := Current_Instantiated_Parent.Act_Id;
7402 end if;
7403
7404 if Ekind (Scop) = E_Generic_Package
7405 or else Nkind (Unit_Declaration_Node (Scop)) =
7406 N_Generic_Subprogram_Declaration
7407 then
7408 return True;
7409
7410 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7411 N_Formal_Package_Declaration
7412 then
7413 return True;
7414
7415 elsif No (Par) then
7416 return False;
7417
7418 else
7419 -- Check whether this package is associated with a formal package of
7420 -- the enclosing instantiation. Iterate over the list of renamings.
7421
7422 E := First_Entity (Par);
7423 while Present (E) loop
7424 if Ekind (E) /= E_Package
7425 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7426 then
7427 null;
7428
7429 elsif Renamed_Object (E) = Par then
7430 return False;
7431
7432 elsif Renamed_Object (E) = Pack then
7433 return True;
7434
7435 elsif Is_Actual_Of_Previous_Formal (E) then
7436 return True;
7437
7438 end if;
7439
7440 Next_Entity (E);
7441 end loop;
7442
7443 return False;
7444 end if;
7445 end Denotes_Formal_Package;
7446
7447 -----------------
7448 -- End_Generic --
7449 -----------------
7450
7451 procedure End_Generic is
7452 begin
7453 -- ??? More things could be factored out in this routine. Should
7454 -- probably be done at a later stage.
7455
7456 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7457 Generic_Flags.Decrement_Last;
7458
7459 Expander_Mode_Restore;
7460 end End_Generic;
7461
7462 -------------
7463 -- Earlier --
7464 -------------
7465
7466 function Earlier (N1, N2 : Node_Id) return Boolean is
7467 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7468 -- Find distance from given node to enclosing compilation unit
7469
7470 ----------------
7471 -- Find_Depth --
7472 ----------------
7473
7474 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7475 begin
7476 while Present (P)
7477 and then Nkind (P) /= N_Compilation_Unit
7478 loop
7479 P := True_Parent (P);
7480 D := D + 1;
7481 end loop;
7482 end Find_Depth;
7483
7484 -- Local declarations
7485
7486 D1 : Integer := 0;
7487 D2 : Integer := 0;
7488 P1 : Node_Id := N1;
7489 P2 : Node_Id := N2;
7490 T1 : Source_Ptr;
7491 T2 : Source_Ptr;
7492
7493 -- Start of processing for Earlier
7494
7495 begin
7496 Find_Depth (P1, D1);
7497 Find_Depth (P2, D2);
7498
7499 if P1 /= P2 then
7500 return False;
7501 else
7502 P1 := N1;
7503 P2 := N2;
7504 end if;
7505
7506 while D1 > D2 loop
7507 P1 := True_Parent (P1);
7508 D1 := D1 - 1;
7509 end loop;
7510
7511 while D2 > D1 loop
7512 P2 := True_Parent (P2);
7513 D2 := D2 - 1;
7514 end loop;
7515
7516 -- At this point P1 and P2 are at the same distance from the root.
7517 -- We examine their parents until we find a common declarative list.
7518 -- If we reach the root, N1 and N2 do not descend from the same
7519 -- declarative list (e.g. one is nested in the declarative part and
7520 -- the other is in a block in the statement part) and the earlier
7521 -- one is already frozen.
7522
7523 while not Is_List_Member (P1)
7524 or else not Is_List_Member (P2)
7525 or else List_Containing (P1) /= List_Containing (P2)
7526 loop
7527 P1 := True_Parent (P1);
7528 P2 := True_Parent (P2);
7529
7530 if Nkind (Parent (P1)) = N_Subunit then
7531 P1 := Corresponding_Stub (Parent (P1));
7532 end if;
7533
7534 if Nkind (Parent (P2)) = N_Subunit then
7535 P2 := Corresponding_Stub (Parent (P2));
7536 end if;
7537
7538 if P1 = P2 then
7539 return False;
7540 end if;
7541 end loop;
7542
7543 -- Expanded code usually shares the source location of the original
7544 -- construct it was generated for. This however may not necessarely
7545 -- reflect the true location of the code within the tree.
7546
7547 -- Before comparing the slocs of the two nodes, make sure that we are
7548 -- working with correct source locations. Assume that P1 is to the left
7549 -- of P2. If either one does not come from source, traverse the common
7550 -- list heading towards the other node and locate the first source
7551 -- statement.
7552
7553 -- P1 P2
7554 -- ----+===+===+--------------+===+===+----
7555 -- expanded code expanded code
7556
7557 if not Comes_From_Source (P1) then
7558 while Present (P1) loop
7559
7560 -- Neither P2 nor a source statement were located during the
7561 -- search. If we reach the end of the list, then P1 does not
7562 -- occur earlier than P2.
7563
7564 -- ---->
7565 -- start --- P2 ----- P1 --- end
7566
7567 if No (Next (P1)) then
7568 return False;
7569
7570 -- We encounter P2 while going to the right of the list. This
7571 -- means that P1 does indeed appear earlier.
7572
7573 -- ---->
7574 -- start --- P1 ===== P2 --- end
7575 -- expanded code in between
7576
7577 elsif P1 = P2 then
7578 return True;
7579
7580 -- No need to look any further since we have located a source
7581 -- statement.
7582
7583 elsif Comes_From_Source (P1) then
7584 exit;
7585 end if;
7586
7587 -- Keep going right
7588
7589 Next (P1);
7590 end loop;
7591 end if;
7592
7593 if not Comes_From_Source (P2) then
7594 while Present (P2) loop
7595
7596 -- Neither P1 nor a source statement were located during the
7597 -- search. If we reach the start of the list, then P1 does not
7598 -- occur earlier than P2.
7599
7600 -- <----
7601 -- start --- P2 --- P1 --- end
7602
7603 if No (Prev (P2)) then
7604 return False;
7605
7606 -- We encounter P1 while going to the left of the list. This
7607 -- means that P1 does indeed appear earlier.
7608
7609 -- <----
7610 -- start --- P1 ===== P2 --- end
7611 -- expanded code in between
7612
7613 elsif P2 = P1 then
7614 return True;
7615
7616 -- No need to look any further since we have located a source
7617 -- statement.
7618
7619 elsif Comes_From_Source (P2) then
7620 exit;
7621 end if;
7622
7623 -- Keep going left
7624
7625 Prev (P2);
7626 end loop;
7627 end if;
7628
7629 -- At this point either both nodes came from source or we approximated
7630 -- their source locations through neighbouring source statements.
7631
7632 T1 := Top_Level_Location (Sloc (P1));
7633 T2 := Top_Level_Location (Sloc (P2));
7634
7635 -- When two nodes come from the same instance, they have identical top
7636 -- level locations. To determine proper relation within the tree, check
7637 -- their locations within the template.
7638
7639 if T1 = T2 then
7640 return Sloc (P1) < Sloc (P2);
7641
7642 -- The two nodes either come from unrelated instances or do not come
7643 -- from instantiated code at all.
7644
7645 else
7646 return T1 < T2;
7647 end if;
7648 end Earlier;
7649
7650 ----------------------
7651 -- Find_Actual_Type --
7652 ----------------------
7653
7654 function Find_Actual_Type
7655 (Typ : Entity_Id;
7656 Gen_Type : Entity_Id) return Entity_Id
7657 is
7658 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7659 T : Entity_Id;
7660
7661 begin
7662 -- Special processing only applies to child units
7663
7664 if not Is_Child_Unit (Gen_Scope) then
7665 return Get_Instance_Of (Typ);
7666
7667 -- If designated or component type is itself a formal of the child unit,
7668 -- its instance is available.
7669
7670 elsif Scope (Typ) = Gen_Scope then
7671 return Get_Instance_Of (Typ);
7672
7673 -- If the array or access type is not declared in the parent unit,
7674 -- no special processing needed.
7675
7676 elsif not Is_Generic_Type (Typ)
7677 and then Scope (Gen_Scope) /= Scope (Typ)
7678 then
7679 return Get_Instance_Of (Typ);
7680
7681 -- Otherwise, retrieve designated or component type by visibility
7682
7683 else
7684 T := Current_Entity (Typ);
7685 while Present (T) loop
7686 if In_Open_Scopes (Scope (T)) then
7687 return T;
7688
7689 elsif Is_Generic_Actual_Type (T) then
7690 return T;
7691 end if;
7692
7693 T := Homonym (T);
7694 end loop;
7695
7696 return Typ;
7697 end if;
7698 end Find_Actual_Type;
7699
7700 ----------------------------
7701 -- Freeze_Subprogram_Body --
7702 ----------------------------
7703
7704 procedure Freeze_Subprogram_Body
7705 (Inst_Node : Node_Id;
7706 Gen_Body : Node_Id;
7707 Pack_Id : Entity_Id)
7708 is
7709 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7710 Par : constant Entity_Id := Scope (Gen_Unit);
7711 E_G_Id : Entity_Id;
7712 Enc_G : Entity_Id;
7713 Enc_I : Node_Id;
7714 F_Node : Node_Id;
7715
7716 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7717 -- Find innermost package body that encloses the given node, and which
7718 -- is not a compilation unit. Freeze nodes for the instance, or for its
7719 -- enclosing body, may be inserted after the enclosing_body of the
7720 -- generic unit. Used to determine proper placement of freeze node for
7721 -- both package and subprogram instances.
7722
7723 function Package_Freeze_Node (B : Node_Id) return Node_Id;
7724 -- Find entity for given package body, and locate or create a freeze
7725 -- node for it.
7726
7727 ----------------------------
7728 -- Enclosing_Package_Body --
7729 ----------------------------
7730
7731 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
7732 P : Node_Id;
7733
7734 begin
7735 P := Parent (N);
7736 while Present (P)
7737 and then Nkind (Parent (P)) /= N_Compilation_Unit
7738 loop
7739 if Nkind (P) = N_Package_Body then
7740 if Nkind (Parent (P)) = N_Subunit then
7741 return Corresponding_Stub (Parent (P));
7742 else
7743 return P;
7744 end if;
7745 end if;
7746
7747 P := True_Parent (P);
7748 end loop;
7749
7750 return Empty;
7751 end Enclosing_Package_Body;
7752
7753 -------------------------
7754 -- Package_Freeze_Node --
7755 -------------------------
7756
7757 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7758 Id : Entity_Id;
7759
7760 begin
7761 if Nkind (B) = N_Package_Body then
7762 Id := Corresponding_Spec (B);
7763 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7764 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7765 end if;
7766
7767 Ensure_Freeze_Node (Id);
7768 return Freeze_Node (Id);
7769 end Package_Freeze_Node;
7770
7771 -- Start of processing of Freeze_Subprogram_Body
7772
7773 begin
7774 -- If the instance and the generic body appear within the same unit, and
7775 -- the instance precedes the generic, the freeze node for the instance
7776 -- must appear after that of the generic. If the generic is nested
7777 -- within another instance I2, then current instance must be frozen
7778 -- after I2. In both cases, the freeze nodes are those of enclosing
7779 -- packages. Otherwise, the freeze node is placed at the end of the
7780 -- current declarative part.
7781
7782 Enc_G := Enclosing_Package_Body (Gen_Body);
7783 Enc_I := Enclosing_Package_Body (Inst_Node);
7784 Ensure_Freeze_Node (Pack_Id);
7785 F_Node := Freeze_Node (Pack_Id);
7786
7787 if Is_Generic_Instance (Par)
7788 and then Present (Freeze_Node (Par))
7789 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7790 then
7791 -- The parent was a premature instantiation. Insert freeze node at
7792 -- the end the current declarative part.
7793
7794 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7795 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7796
7797 -- Handle the following case:
7798 --
7799 -- package Parent_Inst is new ...
7800 -- Parent_Inst []
7801 --
7802 -- procedure P ... -- this body freezes Parent_Inst
7803 --
7804 -- package Inst is new ...
7805 --
7806 -- In this particular scenario, the freeze node for Inst must be
7807 -- inserted in the same manner as that of Parent_Inst - before the
7808 -- next source body or at the end of the declarative list (body not
7809 -- available). If body P did not exist and Parent_Inst was frozen
7810 -- after Inst, either by a body following Inst or at the end of the
7811 -- declarative region, the freeze node for Inst must be inserted
7812 -- after that of Parent_Inst. This relation is established by
7813 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7814
7815 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7816 List_Containing (Inst_Node)
7817 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7818 then
7819 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7820
7821 else
7822 Insert_After (Freeze_Node (Par), F_Node);
7823 end if;
7824
7825 -- The body enclosing the instance should be frozen after the body that
7826 -- includes the generic, because the body of the instance may make
7827 -- references to entities therein. If the two are not in the same
7828 -- declarative part, or if the one enclosing the instance is frozen
7829 -- already, freeze the instance at the end of the current declarative
7830 -- part.
7831
7832 elsif Is_Generic_Instance (Par)
7833 and then Present (Freeze_Node (Par))
7834 and then Present (Enc_I)
7835 then
7836 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7837 or else
7838 (Nkind (Enc_I) = N_Package_Body
7839 and then
7840 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7841 then
7842 -- The enclosing package may contain several instances. Rather
7843 -- than computing the earliest point at which to insert its freeze
7844 -- node, we place it at the end of the declarative part of the
7845 -- parent of the generic.
7846
7847 Insert_Freeze_Node_For_Instance
7848 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7849 end if;
7850
7851 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7852
7853 elsif Present (Enc_G)
7854 and then Present (Enc_I)
7855 and then Enc_G /= Enc_I
7856 and then Earlier (Inst_Node, Gen_Body)
7857 then
7858 if Nkind (Enc_G) = N_Package_Body then
7859 E_G_Id := Corresponding_Spec (Enc_G);
7860 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7861 E_G_Id :=
7862 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7863 end if;
7864
7865 -- Freeze package that encloses instance, and place node after the
7866 -- package that encloses generic. If enclosing package is already
7867 -- frozen we have to assume it is at the proper place. This may be a
7868 -- potential ABE that requires dynamic checking. Do not add a freeze
7869 -- node if the package that encloses the generic is inside the body
7870 -- that encloses the instance, because the freeze node would be in
7871 -- the wrong scope. Additional contortions needed if the bodies are
7872 -- within a subunit.
7873
7874 declare
7875 Enclosing_Body : Node_Id;
7876
7877 begin
7878 if Nkind (Enc_I) = N_Package_Body_Stub then
7879 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7880 else
7881 Enclosing_Body := Enc_I;
7882 end if;
7883
7884 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7885 Insert_Freeze_Node_For_Instance
7886 (Enc_G, Package_Freeze_Node (Enc_I));
7887 end if;
7888 end;
7889
7890 -- Freeze enclosing subunit before instance
7891
7892 Ensure_Freeze_Node (E_G_Id);
7893
7894 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7895 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7896 end if;
7897
7898 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7899
7900 else
7901 -- If none of the above, insert freeze node at the end of the current
7902 -- declarative part.
7903
7904 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7905 end if;
7906 end Freeze_Subprogram_Body;
7907
7908 ----------------
7909 -- Get_Gen_Id --
7910 ----------------
7911
7912 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7913 begin
7914 return Generic_Renamings.Table (E).Gen_Id;
7915 end Get_Gen_Id;
7916
7917 ---------------------
7918 -- Get_Instance_Of --
7919 ---------------------
7920
7921 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7922 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7923
7924 begin
7925 if Res /= Assoc_Null then
7926 return Generic_Renamings.Table (Res).Act_Id;
7927 else
7928 -- On exit, entity is not instantiated: not a generic parameter, or
7929 -- else parameter of an inner generic unit.
7930
7931 return A;
7932 end if;
7933 end Get_Instance_Of;
7934
7935 ------------------------------------
7936 -- Get_Package_Instantiation_Node --
7937 ------------------------------------
7938
7939 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7940 Decl : Node_Id := Unit_Declaration_Node (A);
7941 Inst : Node_Id;
7942
7943 begin
7944 -- If the Package_Instantiation attribute has been set on the package
7945 -- entity, then use it directly when it (or its Original_Node) refers
7946 -- to an N_Package_Instantiation node. In principle it should be
7947 -- possible to have this field set in all cases, which should be
7948 -- investigated, and would allow this function to be significantly
7949 -- simplified. ???
7950
7951 Inst := Package_Instantiation (A);
7952
7953 if Present (Inst) then
7954 if Nkind (Inst) = N_Package_Instantiation then
7955 return Inst;
7956
7957 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7958 return Original_Node (Inst);
7959 end if;
7960 end if;
7961
7962 -- If the instantiation is a compilation unit that does not need body
7963 -- then the instantiation node has been rewritten as a package
7964 -- declaration for the instance, and we return the original node.
7965
7966 -- If it is a compilation unit and the instance node has not been
7967 -- rewritten, then it is still the unit of the compilation. Finally, if
7968 -- a body is present, this is a parent of the main unit whose body has
7969 -- been compiled for inlining purposes, and the instantiation node has
7970 -- been rewritten with the instance body.
7971
7972 -- Otherwise the instantiation node appears after the declaration. If
7973 -- the entity is a formal package, the declaration may have been
7974 -- rewritten as a generic declaration (in the case of a formal with box)
7975 -- or left as a formal package declaration if it has actuals, and is
7976 -- found with a forward search.
7977
7978 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7979 if Nkind (Decl) = N_Package_Declaration
7980 and then Present (Corresponding_Body (Decl))
7981 then
7982 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7983 end if;
7984
7985 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
7986 return Original_Node (Decl);
7987 else
7988 return Unit (Parent (Decl));
7989 end if;
7990
7991 elsif Nkind (Decl) = N_Package_Declaration
7992 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
7993 then
7994 return Original_Node (Decl);
7995
7996 else
7997 Inst := Next (Decl);
7998 while not Nkind_In (Inst, N_Package_Instantiation,
7999 N_Formal_Package_Declaration)
8000 loop
8001 Next (Inst);
8002 end loop;
8003
8004 return Inst;
8005 end if;
8006 end Get_Package_Instantiation_Node;
8007
8008 ------------------------
8009 -- Has_Been_Exchanged --
8010 ------------------------
8011
8012 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8013 Next : Elmt_Id;
8014
8015 begin
8016 Next := First_Elmt (Exchanged_Views);
8017 while Present (Next) loop
8018 if Full_View (Node (Next)) = E then
8019 return True;
8020 end if;
8021
8022 Next_Elmt (Next);
8023 end loop;
8024
8025 return False;
8026 end Has_Been_Exchanged;
8027
8028 ----------
8029 -- Hash --
8030 ----------
8031
8032 function Hash (F : Entity_Id) return HTable_Range is
8033 begin
8034 return HTable_Range (F mod HTable_Size);
8035 end Hash;
8036
8037 ------------------------
8038 -- Hide_Current_Scope --
8039 ------------------------
8040
8041 procedure Hide_Current_Scope is
8042 C : constant Entity_Id := Current_Scope;
8043 E : Entity_Id;
8044
8045 begin
8046 Set_Is_Hidden_Open_Scope (C);
8047
8048 E := First_Entity (C);
8049 while Present (E) loop
8050 if Is_Immediately_Visible (E) then
8051 Set_Is_Immediately_Visible (E, False);
8052 Append_Elmt (E, Hidden_Entities);
8053 end if;
8054
8055 Next_Entity (E);
8056 end loop;
8057
8058 -- Make the scope name invisible as well. This is necessary, but might
8059 -- conflict with calls to Rtsfind later on, in case the scope is a
8060 -- predefined one. There is no clean solution to this problem, so for
8061 -- now we depend on the user not redefining Standard itself in one of
8062 -- the parent units.
8063
8064 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8065 Set_Is_Immediately_Visible (C, False);
8066 Append_Elmt (C, Hidden_Entities);
8067 end if;
8068
8069 end Hide_Current_Scope;
8070
8071 --------------
8072 -- Init_Env --
8073 --------------
8074
8075 procedure Init_Env is
8076 Saved : Instance_Env;
8077
8078 begin
8079 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8080 Saved.Exchanged_Views := Exchanged_Views;
8081 Saved.Hidden_Entities := Hidden_Entities;
8082 Saved.Current_Sem_Unit := Current_Sem_Unit;
8083 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8084 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8085
8086 -- Save configuration switches. These may be reset if the unit is a
8087 -- predefined unit, and the current mode is not Ada 2005.
8088
8089 Save_Opt_Config_Switches (Saved.Switches);
8090
8091 Instance_Envs.Append (Saved);
8092
8093 Exchanged_Views := New_Elmt_List;
8094 Hidden_Entities := New_Elmt_List;
8095
8096 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8097 -- this is set properly in Set_Instance_Env.
8098
8099 Current_Instantiated_Parent :=
8100 (Current_Scope, Current_Scope, Assoc_Null);
8101 end Init_Env;
8102
8103 ------------------------------
8104 -- In_Same_Declarative_Part --
8105 ------------------------------
8106
8107 function In_Same_Declarative_Part
8108 (F_Node : Node_Id;
8109 Inst : Node_Id) return Boolean
8110 is
8111 Decls : constant Node_Id := Parent (F_Node);
8112 Nod : Node_Id := Parent (Inst);
8113
8114 begin
8115 while Present (Nod) loop
8116 if Nod = Decls then
8117 return True;
8118
8119 elsif Nkind_In (Nod, N_Subprogram_Body,
8120 N_Package_Body,
8121 N_Package_Declaration,
8122 N_Task_Body,
8123 N_Protected_Body,
8124 N_Block_Statement)
8125 then
8126 return False;
8127
8128 elsif Nkind (Nod) = N_Subunit then
8129 Nod := Corresponding_Stub (Nod);
8130
8131 elsif Nkind (Nod) = N_Compilation_Unit then
8132 return False;
8133
8134 else
8135 Nod := Parent (Nod);
8136 end if;
8137 end loop;
8138
8139 return False;
8140 end In_Same_Declarative_Part;
8141
8142 ---------------------
8143 -- In_Main_Context --
8144 ---------------------
8145
8146 function In_Main_Context (E : Entity_Id) return Boolean is
8147 Context : List_Id;
8148 Clause : Node_Id;
8149 Nam : Node_Id;
8150
8151 begin
8152 if not Is_Compilation_Unit (E)
8153 or else Ekind (E) /= E_Package
8154 or else In_Private_Part (E)
8155 then
8156 return False;
8157 end if;
8158
8159 Context := Context_Items (Cunit (Main_Unit));
8160
8161 Clause := First (Context);
8162 while Present (Clause) loop
8163 if Nkind (Clause) = N_With_Clause then
8164 Nam := Name (Clause);
8165
8166 -- If the current scope is part of the context of the main unit,
8167 -- analysis of the corresponding with_clause is not complete, and
8168 -- the entity is not set. We use the Chars field directly, which
8169 -- might produce false positives in rare cases, but guarantees
8170 -- that we produce all the instance bodies we will need.
8171
8172 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8173 or else (Nkind (Nam) = N_Selected_Component
8174 and then Chars (Selector_Name (Nam)) = Chars (E))
8175 then
8176 return True;
8177 end if;
8178 end if;
8179
8180 Next (Clause);
8181 end loop;
8182
8183 return False;
8184 end In_Main_Context;
8185
8186 ---------------------
8187 -- Inherit_Context --
8188 ---------------------
8189
8190 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8191 Current_Context : List_Id;
8192 Current_Unit : Node_Id;
8193 Item : Node_Id;
8194 New_I : Node_Id;
8195
8196 Clause : Node_Id;
8197 OK : Boolean;
8198 Lib_Unit : Node_Id;
8199
8200 begin
8201 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8202
8203 -- The inherited context is attached to the enclosing compilation
8204 -- unit. This is either the main unit, or the declaration for the
8205 -- main unit (in case the instantiation appears within the package
8206 -- declaration and the main unit is its body).
8207
8208 Current_Unit := Parent (Inst);
8209 while Present (Current_Unit)
8210 and then Nkind (Current_Unit) /= N_Compilation_Unit
8211 loop
8212 Current_Unit := Parent (Current_Unit);
8213 end loop;
8214
8215 Current_Context := Context_Items (Current_Unit);
8216
8217 Item := First (Context_Items (Parent (Gen_Decl)));
8218 while Present (Item) loop
8219 if Nkind (Item) = N_With_Clause then
8220 Lib_Unit := Library_Unit (Item);
8221
8222 -- Take care to prevent direct cyclic with's
8223
8224 if Lib_Unit /= Current_Unit then
8225
8226 -- Do not add a unit if it is already in the context
8227
8228 Clause := First (Current_Context);
8229 OK := True;
8230 while Present (Clause) loop
8231 if Nkind (Clause) = N_With_Clause and then
8232 Library_Unit (Clause) = Lib_Unit
8233 then
8234 OK := False;
8235 exit;
8236 end if;
8237
8238 Next (Clause);
8239 end loop;
8240
8241 if OK then
8242 New_I := New_Copy (Item);
8243 Set_Implicit_With (New_I, True);
8244 Set_Implicit_With_From_Instantiation (New_I, True);
8245 Append (New_I, Current_Context);
8246 end if;
8247 end if;
8248 end if;
8249
8250 Next (Item);
8251 end loop;
8252 end if;
8253 end Inherit_Context;
8254
8255 ----------------
8256 -- Initialize --
8257 ----------------
8258
8259 procedure Initialize is
8260 begin
8261 Generic_Renamings.Init;
8262 Instance_Envs.Init;
8263 Generic_Flags.Init;
8264 Generic_Renamings_HTable.Reset;
8265 Circularity_Detected := False;
8266 Exchanged_Views := No_Elist;
8267 Hidden_Entities := No_Elist;
8268 end Initialize;
8269
8270 -------------------------------------
8271 -- Insert_Freeze_Node_For_Instance --
8272 -------------------------------------
8273
8274 procedure Insert_Freeze_Node_For_Instance
8275 (N : Node_Id;
8276 F_Node : Node_Id)
8277 is
8278 Decl : Node_Id;
8279 Decls : List_Id;
8280 Inst : Entity_Id;
8281 Par_N : Node_Id;
8282
8283 function Enclosing_Body (N : Node_Id) return Node_Id;
8284 -- Find enclosing package or subprogram body, if any. Freeze node may
8285 -- be placed at end of current declarative list if previous instance
8286 -- and current one have different enclosing bodies.
8287
8288 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8289 -- Find the local instance, if any, that declares the generic that is
8290 -- being instantiated. If present, the freeze node for this instance
8291 -- must follow the freeze node for the previous instance.
8292
8293 --------------------
8294 -- Enclosing_Body --
8295 --------------------
8296
8297 function Enclosing_Body (N : Node_Id) return Node_Id is
8298 P : Node_Id;
8299
8300 begin
8301 P := Parent (N);
8302 while Present (P)
8303 and then Nkind (Parent (P)) /= N_Compilation_Unit
8304 loop
8305 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8306 if Nkind (Parent (P)) = N_Subunit then
8307 return Corresponding_Stub (Parent (P));
8308 else
8309 return P;
8310 end if;
8311 end if;
8312
8313 P := True_Parent (P);
8314 end loop;
8315
8316 return Empty;
8317 end Enclosing_Body;
8318
8319 -----------------------
8320 -- Previous_Instance --
8321 -----------------------
8322
8323 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8324 S : Entity_Id;
8325
8326 begin
8327 S := Scope (Gen);
8328 while Present (S)
8329 and then S /= Standard_Standard
8330 loop
8331 if Is_Generic_Instance (S)
8332 and then In_Same_Source_Unit (S, N)
8333 then
8334 return S;
8335 end if;
8336
8337 S := Scope (S);
8338 end loop;
8339
8340 return Empty;
8341 end Previous_Instance;
8342
8343 -- Start of processing for Insert_Freeze_Node_For_Instance
8344
8345 begin
8346 if not Is_List_Member (F_Node) then
8347 Decl := N;
8348 Decls := List_Containing (N);
8349 Inst := Entity (F_Node);
8350 Par_N := Parent (Decls);
8351
8352 -- When processing a subprogram instantiation, utilize the actual
8353 -- subprogram instantiation rather than its package wrapper as it
8354 -- carries all the context information.
8355
8356 if Is_Wrapper_Package (Inst) then
8357 Inst := Related_Instance (Inst);
8358 end if;
8359
8360 -- If this is a package instance, check whether the generic is
8361 -- declared in a previous instance and the current instance is
8362 -- not within the previous one.
8363
8364 if Present (Generic_Parent (Parent (Inst)))
8365 and then Is_In_Main_Unit (N)
8366 then
8367 declare
8368 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8369 Par_I : constant Entity_Id :=
8370 Previous_Instance
8371 (Generic_Parent (Parent (Inst)));
8372 Scop : Entity_Id;
8373
8374 begin
8375 if Present (Par_I)
8376 and then Earlier (N, Freeze_Node (Par_I))
8377 then
8378 Scop := Scope (Inst);
8379
8380 -- If the current instance is within the one that contains
8381 -- the generic, the freeze node for the current one must
8382 -- appear in the current declarative part. Ditto, if the
8383 -- current instance is within another package instance or
8384 -- within a body that does not enclose the current instance.
8385 -- In these three cases the freeze node of the previous
8386 -- instance is not relevant.
8387
8388 while Present (Scop)
8389 and then Scop /= Standard_Standard
8390 loop
8391 exit when Scop = Par_I
8392 or else
8393 (Is_Generic_Instance (Scop)
8394 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8395 Scop := Scope (Scop);
8396 end loop;
8397
8398 -- Previous instance encloses current instance
8399
8400 if Scop = Par_I then
8401 null;
8402
8403 -- If the next node is a source body we must freeze in
8404 -- the current scope as well.
8405
8406 elsif Present (Next (N))
8407 and then Nkind_In (Next (N),
8408 N_Subprogram_Body, N_Package_Body)
8409 and then Comes_From_Source (Next (N))
8410 then
8411 null;
8412
8413 -- Current instance is within an unrelated instance
8414
8415 elsif Is_Generic_Instance (Scop) then
8416 null;
8417
8418 -- Current instance is within an unrelated body
8419
8420 elsif Present (Enclosing_N)
8421 and then Enclosing_N /= Enclosing_Body (Par_I)
8422 then
8423 null;
8424
8425 else
8426 Insert_After (Freeze_Node (Par_I), F_Node);
8427 return;
8428 end if;
8429 end if;
8430 end;
8431 end if;
8432
8433 -- When the instantiation occurs in a package declaration, append the
8434 -- freeze node to the private declarations (if any).
8435
8436 if Nkind (Par_N) = N_Package_Specification
8437 and then Decls = Visible_Declarations (Par_N)
8438 and then Present (Private_Declarations (Par_N))
8439 and then not Is_Empty_List (Private_Declarations (Par_N))
8440 then
8441 Decls := Private_Declarations (Par_N);
8442 Decl := First (Decls);
8443 end if;
8444
8445 -- Determine the proper freeze point of a package instantiation. We
8446 -- adhere to the general rule of a package or subprogram body causing
8447 -- freezing of anything before it in the same declarative region. In
8448 -- this case, the proper freeze point of a package instantiation is
8449 -- before the first source body which follows, or before a stub. This
8450 -- ensures that entities coming from the instance are already frozen
8451 -- and usable in source bodies.
8452
8453 if Nkind (Par_N) /= N_Package_Declaration
8454 and then Ekind (Inst) = E_Package
8455 and then Is_Generic_Instance (Inst)
8456 and then
8457 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8458 then
8459 while Present (Decl) loop
8460 if (Nkind (Decl) in N_Unit_Body
8461 or else
8462 Nkind (Decl) in N_Body_Stub)
8463 and then Comes_From_Source (Decl)
8464 then
8465 Insert_Before (Decl, F_Node);
8466 return;
8467 end if;
8468
8469 Next (Decl);
8470 end loop;
8471 end if;
8472
8473 -- In a package declaration, or if no previous body, insert at end
8474 -- of list.
8475
8476 Set_Sloc (F_Node, Sloc (Last (Decls)));
8477 Insert_After (Last (Decls), F_Node);
8478 end if;
8479 end Insert_Freeze_Node_For_Instance;
8480
8481 ------------------
8482 -- Install_Body --
8483 ------------------
8484
8485 procedure Install_Body
8486 (Act_Body : Node_Id;
8487 N : Node_Id;
8488 Gen_Body : Node_Id;
8489 Gen_Decl : Node_Id)
8490 is
8491 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8492 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8493 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8494 Par : constant Entity_Id := Scope (Gen_Id);
8495 Gen_Unit : constant Node_Id :=
8496 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8497 Orig_Body : Node_Id := Gen_Body;
8498 F_Node : Node_Id;
8499 Body_Unit : Node_Id;
8500
8501 Must_Delay : Boolean;
8502
8503 function In_Same_Enclosing_Subp return Boolean;
8504 -- Check whether instance and generic body are within same subprogram.
8505
8506 function True_Sloc (N : Node_Id) return Source_Ptr;
8507 -- If the instance is nested inside a generic unit, the Sloc of the
8508 -- instance indicates the place of the original definition, not the
8509 -- point of the current enclosing instance. Pending a better usage of
8510 -- Slocs to indicate instantiation places, we determine the place of
8511 -- origin of a node by finding the maximum sloc of any ancestor node.
8512 -- Why is this not equivalent to Top_Level_Location ???
8513
8514 ----------------------------
8515 -- In_Same_Enclosing_Subp --
8516 ----------------------------
8517
8518 function In_Same_Enclosing_Subp return Boolean is
8519 Scop : Entity_Id;
8520 Subp : Entity_Id;
8521
8522 begin
8523 Scop := Scope (Act_Id);
8524 while Scop /= Standard_Standard
8525 and then not Is_Overloadable (Scop)
8526 loop
8527 Scop := Scope (Scop);
8528 end loop;
8529
8530 if Scop = Standard_Standard then
8531 return False;
8532 else
8533 Subp := Scop;
8534 end if;
8535
8536 Scop := Scope (Gen_Id);
8537 while Scop /= Standard_Standard loop
8538 if Scop = Subp then
8539 return True;
8540 else
8541 Scop := Scope (Scop);
8542 end if;
8543 end loop;
8544
8545 return False;
8546 end In_Same_Enclosing_Subp;
8547
8548 ---------------
8549 -- True_Sloc --
8550 ---------------
8551
8552 function True_Sloc (N : Node_Id) return Source_Ptr is
8553 Res : Source_Ptr;
8554 N1 : Node_Id;
8555
8556 begin
8557 Res := Sloc (N);
8558 N1 := N;
8559 while Present (N1) and then N1 /= Act_Unit loop
8560 if Sloc (N1) > Res then
8561 Res := Sloc (N1);
8562 end if;
8563
8564 N1 := Parent (N1);
8565 end loop;
8566
8567 return Res;
8568 end True_Sloc;
8569
8570 -- Start of processing for Install_Body
8571
8572 begin
8573 -- If the body is a subunit, the freeze point is the corresponding stub
8574 -- in the current compilation, not the subunit itself.
8575
8576 if Nkind (Parent (Gen_Body)) = N_Subunit then
8577 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8578 else
8579 Orig_Body := Gen_Body;
8580 end if;
8581
8582 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8583
8584 -- If the instantiation and the generic definition appear in the same
8585 -- package declaration, this is an early instantiation. If they appear
8586 -- in the same declarative part, it is an early instantiation only if
8587 -- the generic body appears textually later, and the generic body is
8588 -- also in the main unit.
8589
8590 -- If instance is nested within a subprogram, and the generic body
8591 -- is not, the instance is delayed because the enclosing body is. If
8592 -- instance and body are within the same scope, or the same subprogram
8593 -- body, indicate explicitly that the instance is delayed.
8594
8595 Must_Delay :=
8596 (Gen_Unit = Act_Unit
8597 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8598 N_Generic_Package_Declaration)
8599 or else (Gen_Unit = Body_Unit
8600 and then True_Sloc (N) < Sloc (Orig_Body)))
8601 and then Is_In_Main_Unit (Gen_Unit)
8602 and then (Scope (Act_Id) = Scope (Gen_Id)
8603 or else In_Same_Enclosing_Subp));
8604
8605 -- If this is an early instantiation, the freeze node is placed after
8606 -- the generic body. Otherwise, if the generic appears in an instance,
8607 -- we cannot freeze the current instance until the outer one is frozen.
8608 -- This is only relevant if the current instance is nested within some
8609 -- inner scope not itself within the outer instance. If this scope is
8610 -- a package body in the same declarative part as the outer instance,
8611 -- then that body needs to be frozen after the outer instance. Finally,
8612 -- if no delay is needed, we place the freeze node at the end of the
8613 -- current declarative part.
8614
8615 if Expander_Active then
8616 Ensure_Freeze_Node (Act_Id);
8617 F_Node := Freeze_Node (Act_Id);
8618
8619 if Must_Delay then
8620 Insert_After (Orig_Body, F_Node);
8621
8622 elsif Is_Generic_Instance (Par)
8623 and then Present (Freeze_Node (Par))
8624 and then Scope (Act_Id) /= Par
8625 then
8626 -- Freeze instance of inner generic after instance of enclosing
8627 -- generic.
8628
8629 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8630
8631 -- Handle the following case:
8632
8633 -- package Parent_Inst is new ...
8634 -- Parent_Inst []
8635
8636 -- procedure P ... -- this body freezes Parent_Inst
8637
8638 -- package Inst is new ...
8639
8640 -- In this particular scenario, the freeze node for Inst must
8641 -- be inserted in the same manner as that of Parent_Inst,
8642 -- before the next source body or at the end of the declarative
8643 -- list (body not available). If body P did not exist and
8644 -- Parent_Inst was frozen after Inst, either by a body
8645 -- following Inst or at the end of the declarative region,
8646 -- the freeze node for Inst must be inserted after that of
8647 -- Parent_Inst. This relation is established by comparing
8648 -- the Slocs of Parent_Inst freeze node and Inst.
8649
8650 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8651 List_Containing (N)
8652 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8653 then
8654 Insert_Freeze_Node_For_Instance (N, F_Node);
8655 else
8656 Insert_After (Freeze_Node (Par), F_Node);
8657 end if;
8658
8659 -- Freeze package enclosing instance of inner generic after
8660 -- instance of enclosing generic.
8661
8662 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8663 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8664 then
8665 declare
8666 Enclosing : Entity_Id;
8667
8668 begin
8669 Enclosing := Corresponding_Spec (Parent (N));
8670
8671 if No (Enclosing) then
8672 Enclosing := Defining_Entity (Parent (N));
8673 end if;
8674
8675 Insert_Freeze_Node_For_Instance (N, F_Node);
8676 Ensure_Freeze_Node (Enclosing);
8677
8678 if not Is_List_Member (Freeze_Node (Enclosing)) then
8679
8680 -- The enclosing context is a subunit, insert the freeze
8681 -- node after the stub.
8682
8683 if Nkind (Parent (Parent (N))) = N_Subunit then
8684 Insert_Freeze_Node_For_Instance
8685 (Corresponding_Stub (Parent (Parent (N))),
8686 Freeze_Node (Enclosing));
8687
8688 -- The enclosing context is a package with a stub body
8689 -- which has already been replaced by the real body.
8690 -- Insert the freeze node after the actual body.
8691
8692 elsif Ekind (Enclosing) = E_Package
8693 and then Present (Body_Entity (Enclosing))
8694 and then Was_Originally_Stub
8695 (Parent (Body_Entity (Enclosing)))
8696 then
8697 Insert_Freeze_Node_For_Instance
8698 (Parent (Body_Entity (Enclosing)),
8699 Freeze_Node (Enclosing));
8700
8701 -- The parent instance has been frozen before the body of
8702 -- the enclosing package, insert the freeze node after
8703 -- the body.
8704
8705 elsif List_Containing (Freeze_Node (Par)) =
8706 List_Containing (Parent (N))
8707 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8708 then
8709 Insert_Freeze_Node_For_Instance
8710 (Parent (N), Freeze_Node (Enclosing));
8711
8712 else
8713 Insert_After
8714 (Freeze_Node (Par), Freeze_Node (Enclosing));
8715 end if;
8716 end if;
8717 end;
8718
8719 else
8720 Insert_Freeze_Node_For_Instance (N, F_Node);
8721 end if;
8722
8723 else
8724 Insert_Freeze_Node_For_Instance (N, F_Node);
8725 end if;
8726 end if;
8727
8728 Set_Is_Frozen (Act_Id);
8729 Insert_Before (N, Act_Body);
8730 Mark_Rewrite_Insertion (Act_Body);
8731 end Install_Body;
8732
8733 -----------------------------
8734 -- Install_Formal_Packages --
8735 -----------------------------
8736
8737 procedure Install_Formal_Packages (Par : Entity_Id) is
8738 E : Entity_Id;
8739 Gen : Entity_Id;
8740 Gen_E : Entity_Id := Empty;
8741
8742 begin
8743 E := First_Entity (Par);
8744
8745 -- If we are installing an instance parent, locate the formal packages
8746 -- of its generic parent.
8747
8748 if Is_Generic_Instance (Par) then
8749 Gen := Generic_Parent (Package_Specification (Par));
8750 Gen_E := First_Entity (Gen);
8751 end if;
8752
8753 while Present (E) loop
8754 if Ekind (E) = E_Package
8755 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
8756 then
8757 -- If this is the renaming for the parent instance, done
8758
8759 if Renamed_Object (E) = Par then
8760 exit;
8761
8762 -- The visibility of a formal of an enclosing generic is already
8763 -- correct.
8764
8765 elsif Denotes_Formal_Package (E) then
8766 null;
8767
8768 elsif Present (Associated_Formal_Package (E)) then
8769 Check_Generic_Actuals (Renamed_Object (E), True);
8770 Set_Is_Hidden (E, False);
8771
8772 -- Find formal package in generic unit that corresponds to
8773 -- (instance of) formal package in instance.
8774
8775 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
8776 Next_Entity (Gen_E);
8777 end loop;
8778
8779 if Present (Gen_E) then
8780 Map_Formal_Package_Entities (Gen_E, E);
8781 end if;
8782 end if;
8783 end if;
8784
8785 Next_Entity (E);
8786 if Present (Gen_E) then
8787 Next_Entity (Gen_E);
8788 end if;
8789 end loop;
8790 end Install_Formal_Packages;
8791
8792 --------------------
8793 -- Install_Parent --
8794 --------------------
8795
8796 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
8797 Ancestors : constant Elist_Id := New_Elmt_List;
8798 S : constant Entity_Id := Current_Scope;
8799 Inst_Par : Entity_Id;
8800 First_Par : Entity_Id;
8801 Inst_Node : Node_Id;
8802 Gen_Par : Entity_Id;
8803 First_Gen : Entity_Id;
8804 Elmt : Elmt_Id;
8805
8806 procedure Install_Noninstance_Specs (Par : Entity_Id);
8807 -- Install the scopes of noninstance parent units ending with Par
8808
8809 procedure Install_Spec (Par : Entity_Id);
8810 -- The child unit is within the declarative part of the parent, so the
8811 -- declarations within the parent are immediately visible.
8812
8813 -------------------------------
8814 -- Install_Noninstance_Specs --
8815 -------------------------------
8816
8817 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8818 begin
8819 if Present (Par)
8820 and then Par /= Standard_Standard
8821 and then not In_Open_Scopes (Par)
8822 then
8823 Install_Noninstance_Specs (Scope (Par));
8824 Install_Spec (Par);
8825 end if;
8826 end Install_Noninstance_Specs;
8827
8828 ------------------
8829 -- Install_Spec --
8830 ------------------
8831
8832 procedure Install_Spec (Par : Entity_Id) is
8833 Spec : constant Node_Id := Package_Specification (Par);
8834
8835 begin
8836 -- If this parent of the child instance is a top-level unit,
8837 -- then record the unit and its visibility for later resetting in
8838 -- Remove_Parent. We exclude units that are generic instances, as we
8839 -- only want to record this information for the ultimate top-level
8840 -- noninstance parent (is that always correct???).
8841
8842 if Scope (Par) = Standard_Standard
8843 and then not Is_Generic_Instance (Par)
8844 then
8845 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8846 Instance_Parent_Unit := Par;
8847 end if;
8848
8849 -- Open the parent scope and make it and its declarations visible.
8850 -- If this point is not within a body, then only the visible
8851 -- declarations should be made visible, and installation of the
8852 -- private declarations is deferred until the appropriate point
8853 -- within analysis of the spec being instantiated (see the handling
8854 -- of parent visibility in Analyze_Package_Specification). This is
8855 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8856 -- private view problems that occur when compiling instantiations of
8857 -- a generic child of that package (Generic_Dispatching_Constructor).
8858 -- If the instance freezes a tagged type, inlinings of operations
8859 -- from Ada.Tags may need the full view of type Tag. If inlining took
8860 -- proper account of establishing visibility of inlined subprograms'
8861 -- parents then it should be possible to remove this
8862 -- special check. ???
8863
8864 Push_Scope (Par);
8865 Set_Is_Immediately_Visible (Par);
8866 Install_Visible_Declarations (Par);
8867 Set_Use (Visible_Declarations (Spec));
8868
8869 if In_Body or else Is_RTU (Par, Ada_Tags) then
8870 Install_Private_Declarations (Par);
8871 Set_Use (Private_Declarations (Spec));
8872 end if;
8873 end Install_Spec;
8874
8875 -- Start of processing for Install_Parent
8876
8877 begin
8878 -- We need to install the parent instance to compile the instantiation
8879 -- of the child, but the child instance must appear in the current
8880 -- scope. Given that we cannot place the parent above the current scope
8881 -- in the scope stack, we duplicate the current scope and unstack both
8882 -- after the instantiation is complete.
8883
8884 -- If the parent is itself the instantiation of a child unit, we must
8885 -- also stack the instantiation of its parent, and so on. Each such
8886 -- ancestor is the prefix of the name in a prior instantiation.
8887
8888 -- If this is a nested instance, the parent unit itself resolves to
8889 -- a renaming of the parent instance, whose declaration we need.
8890
8891 -- Finally, the parent may be a generic (not an instance) when the
8892 -- child unit appears as a formal package.
8893
8894 Inst_Par := P;
8895
8896 if Present (Renamed_Entity (Inst_Par)) then
8897 Inst_Par := Renamed_Entity (Inst_Par);
8898 end if;
8899
8900 First_Par := Inst_Par;
8901
8902 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8903
8904 First_Gen := Gen_Par;
8905
8906 while Present (Gen_Par)
8907 and then Is_Child_Unit (Gen_Par)
8908 loop
8909 -- Load grandparent instance as well
8910
8911 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8912
8913 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8914 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8915
8916 if Present (Renamed_Entity (Inst_Par)) then
8917 Inst_Par := Renamed_Entity (Inst_Par);
8918 end if;
8919
8920 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8921
8922 if Present (Gen_Par) then
8923 Prepend_Elmt (Inst_Par, Ancestors);
8924
8925 else
8926 -- Parent is not the name of an instantiation
8927
8928 Install_Noninstance_Specs (Inst_Par);
8929 exit;
8930 end if;
8931
8932 else
8933 -- Previous error
8934
8935 exit;
8936 end if;
8937 end loop;
8938
8939 if Present (First_Gen) then
8940 Append_Elmt (First_Par, Ancestors);
8941 else
8942 Install_Noninstance_Specs (First_Par);
8943 end if;
8944
8945 if not Is_Empty_Elmt_List (Ancestors) then
8946 Elmt := First_Elmt (Ancestors);
8947 while Present (Elmt) loop
8948 Install_Spec (Node (Elmt));
8949 Install_Formal_Packages (Node (Elmt));
8950 Next_Elmt (Elmt);
8951 end loop;
8952 end if;
8953
8954 if not In_Body then
8955 Push_Scope (S);
8956 end if;
8957 end Install_Parent;
8958
8959 -------------------------------
8960 -- Install_Hidden_Primitives --
8961 -------------------------------
8962
8963 procedure Install_Hidden_Primitives
8964 (Prims_List : in out Elist_Id;
8965 Gen_T : Entity_Id;
8966 Act_T : Entity_Id)
8967 is
8968 Elmt : Elmt_Id;
8969 List : Elist_Id := No_Elist;
8970 Prim_G_Elmt : Elmt_Id;
8971 Prim_A_Elmt : Elmt_Id;
8972 Prim_G : Node_Id;
8973 Prim_A : Node_Id;
8974
8975 begin
8976 -- No action needed in case of serious errors because we cannot trust
8977 -- in the order of primitives
8978
8979 if Serious_Errors_Detected > 0 then
8980 return;
8981
8982 -- No action possible if we don't have available the list of primitive
8983 -- operations
8984
8985 elsif No (Gen_T)
8986 or else not Is_Record_Type (Gen_T)
8987 or else not Is_Tagged_Type (Gen_T)
8988 or else not Is_Record_Type (Act_T)
8989 or else not Is_Tagged_Type (Act_T)
8990 then
8991 return;
8992
8993 -- There is no need to handle interface types since their primitives
8994 -- cannot be hidden
8995
8996 elsif Is_Interface (Gen_T) then
8997 return;
8998 end if;
8999
9000 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9001
9002 if not Is_Class_Wide_Type (Act_T) then
9003 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9004 else
9005 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9006 end if;
9007
9008 loop
9009 -- Skip predefined primitives in the generic formal
9010
9011 while Present (Prim_G_Elmt)
9012 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9013 loop
9014 Next_Elmt (Prim_G_Elmt);
9015 end loop;
9016
9017 -- Skip predefined primitives in the generic actual
9018
9019 while Present (Prim_A_Elmt)
9020 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9021 loop
9022 Next_Elmt (Prim_A_Elmt);
9023 end loop;
9024
9025 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9026
9027 Prim_G := Node (Prim_G_Elmt);
9028 Prim_A := Node (Prim_A_Elmt);
9029
9030 -- There is no need to handle interface primitives because their
9031 -- primitives are not hidden
9032
9033 exit when Present (Interface_Alias (Prim_G));
9034
9035 -- Here we install one hidden primitive
9036
9037 if Chars (Prim_G) /= Chars (Prim_A)
9038 and then Has_Suffix (Prim_A, 'P')
9039 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9040 then
9041 Set_Chars (Prim_A, Chars (Prim_G));
9042 Append_New_Elmt (Prim_A, To => List);
9043 end if;
9044
9045 Next_Elmt (Prim_A_Elmt);
9046 Next_Elmt (Prim_G_Elmt);
9047 end loop;
9048
9049 -- Append the elements to the list of temporarily visible primitives
9050 -- avoiding duplicates.
9051
9052 if Present (List) then
9053 if No (Prims_List) then
9054 Prims_List := New_Elmt_List;
9055 end if;
9056
9057 Elmt := First_Elmt (List);
9058 while Present (Elmt) loop
9059 Append_Unique_Elmt (Node (Elmt), Prims_List);
9060 Next_Elmt (Elmt);
9061 end loop;
9062 end if;
9063 end Install_Hidden_Primitives;
9064
9065 -------------------------------
9066 -- Restore_Hidden_Primitives --
9067 -------------------------------
9068
9069 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9070 Prim_Elmt : Elmt_Id;
9071 Prim : Node_Id;
9072
9073 begin
9074 if Prims_List /= No_Elist then
9075 Prim_Elmt := First_Elmt (Prims_List);
9076 while Present (Prim_Elmt) loop
9077 Prim := Node (Prim_Elmt);
9078 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9079 Next_Elmt (Prim_Elmt);
9080 end loop;
9081
9082 Prims_List := No_Elist;
9083 end if;
9084 end Restore_Hidden_Primitives;
9085
9086 --------------------------------
9087 -- Instantiate_Formal_Package --
9088 --------------------------------
9089
9090 function Instantiate_Formal_Package
9091 (Formal : Node_Id;
9092 Actual : Node_Id;
9093 Analyzed_Formal : Node_Id) return List_Id
9094 is
9095 Loc : constant Source_Ptr := Sloc (Actual);
9096 Actual_Pack : Entity_Id;
9097 Formal_Pack : Entity_Id;
9098 Gen_Parent : Entity_Id;
9099 Decls : List_Id;
9100 Nod : Node_Id;
9101 Parent_Spec : Node_Id;
9102
9103 procedure Find_Matching_Actual
9104 (F : Node_Id;
9105 Act : in out Entity_Id);
9106 -- We need to associate each formal entity in the formal package with
9107 -- the corresponding entity in the actual package. The actual package
9108 -- has been analyzed and possibly expanded, and as a result there is
9109 -- no one-to-one correspondence between the two lists (for example,
9110 -- the actual may include subtypes, itypes, and inherited primitive
9111 -- operations, interspersed among the renaming declarations for the
9112 -- actuals) . We retrieve the corresponding actual by name because each
9113 -- actual has the same name as the formal, and they do appear in the
9114 -- same order.
9115
9116 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9117 -- Retrieve entity of defining entity of generic formal parameter.
9118 -- Only the declarations of formals need to be considered when
9119 -- linking them to actuals, but the declarative list may include
9120 -- internal entities generated during analysis, and those are ignored.
9121
9122 procedure Match_Formal_Entity
9123 (Formal_Node : Node_Id;
9124 Formal_Ent : Entity_Id;
9125 Actual_Ent : Entity_Id);
9126 -- Associates the formal entity with the actual. In the case where
9127 -- Formal_Ent is a formal package, this procedure iterates through all
9128 -- of its formals and enters associations between the actuals occurring
9129 -- in the formal package's corresponding actual package (given by
9130 -- Actual_Ent) and the formal package's formal parameters. This
9131 -- procedure recurses if any of the parameters is itself a package.
9132
9133 function Is_Instance_Of
9134 (Act_Spec : Entity_Id;
9135 Gen_Anc : Entity_Id) return Boolean;
9136 -- The actual can be an instantiation of a generic within another
9137 -- instance, in which case there is no direct link from it to the
9138 -- original generic ancestor. In that case, we recognize that the
9139 -- ultimate ancestor is the same by examining names and scopes.
9140
9141 procedure Process_Nested_Formal (Formal : Entity_Id);
9142 -- If the current formal is declared with a box, its own formals are
9143 -- visible in the instance, as they were in the generic, and their
9144 -- Hidden flag must be reset. If some of these formals are themselves
9145 -- packages declared with a box, the processing must be recursive.
9146
9147 --------------------------
9148 -- Find_Matching_Actual --
9149 --------------------------
9150
9151 procedure Find_Matching_Actual
9152 (F : Node_Id;
9153 Act : in out Entity_Id)
9154 is
9155 Formal_Ent : Entity_Id;
9156
9157 begin
9158 case Nkind (Original_Node (F)) is
9159 when N_Formal_Object_Declaration |
9160 N_Formal_Type_Declaration =>
9161 Formal_Ent := Defining_Identifier (F);
9162
9163 while Chars (Act) /= Chars (Formal_Ent) loop
9164 Next_Entity (Act);
9165 end loop;
9166
9167 when N_Formal_Subprogram_Declaration |
9168 N_Formal_Package_Declaration |
9169 N_Package_Declaration |
9170 N_Generic_Package_Declaration =>
9171 Formal_Ent := Defining_Entity (F);
9172
9173 while Chars (Act) /= Chars (Formal_Ent) loop
9174 Next_Entity (Act);
9175 end loop;
9176
9177 when others =>
9178 raise Program_Error;
9179 end case;
9180 end Find_Matching_Actual;
9181
9182 -------------------------
9183 -- Match_Formal_Entity --
9184 -------------------------
9185
9186 procedure Match_Formal_Entity
9187 (Formal_Node : Node_Id;
9188 Formal_Ent : Entity_Id;
9189 Actual_Ent : Entity_Id)
9190 is
9191 Act_Pkg : Entity_Id;
9192
9193 begin
9194 Set_Instance_Of (Formal_Ent, Actual_Ent);
9195
9196 if Ekind (Actual_Ent) = E_Package then
9197
9198 -- Record associations for each parameter
9199
9200 Act_Pkg := Actual_Ent;
9201
9202 declare
9203 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9204 F_Ent : Entity_Id;
9205 F_Node : Node_Id;
9206
9207 Gen_Decl : Node_Id;
9208 Formals : List_Id;
9209 Actual : Entity_Id;
9210
9211 begin
9212 -- Retrieve the actual given in the formal package declaration
9213
9214 Actual := Entity (Name (Original_Node (Formal_Node)));
9215
9216 -- The actual in the formal package declaration may be a
9217 -- renamed generic package, in which case we want to retrieve
9218 -- the original generic in order to traverse its formal part.
9219
9220 if Present (Renamed_Entity (Actual)) then
9221 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9222 else
9223 Gen_Decl := Unit_Declaration_Node (Actual);
9224 end if;
9225
9226 Formals := Generic_Formal_Declarations (Gen_Decl);
9227
9228 if Present (Formals) then
9229 F_Node := First_Non_Pragma (Formals);
9230 else
9231 F_Node := Empty;
9232 end if;
9233
9234 while Present (A_Ent)
9235 and then Present (F_Node)
9236 and then A_Ent /= First_Private_Entity (Act_Pkg)
9237 loop
9238 F_Ent := Get_Formal_Entity (F_Node);
9239
9240 if Present (F_Ent) then
9241
9242 -- This is a formal of the original package. Record
9243 -- association and recurse.
9244
9245 Find_Matching_Actual (F_Node, A_Ent);
9246 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9247 Next_Entity (A_Ent);
9248 end if;
9249
9250 Next_Non_Pragma (F_Node);
9251 end loop;
9252 end;
9253 end if;
9254 end Match_Formal_Entity;
9255
9256 -----------------------
9257 -- Get_Formal_Entity --
9258 -----------------------
9259
9260 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9261 Kind : constant Node_Kind := Nkind (Original_Node (N));
9262 begin
9263 case Kind is
9264 when N_Formal_Object_Declaration =>
9265 return Defining_Identifier (N);
9266
9267 when N_Formal_Type_Declaration =>
9268 return Defining_Identifier (N);
9269
9270 when N_Formal_Subprogram_Declaration =>
9271 return Defining_Unit_Name (Specification (N));
9272
9273 when N_Formal_Package_Declaration =>
9274 return Defining_Identifier (Original_Node (N));
9275
9276 when N_Generic_Package_Declaration =>
9277 return Defining_Identifier (Original_Node (N));
9278
9279 -- All other declarations are introduced by semantic analysis and
9280 -- have no match in the actual.
9281
9282 when others =>
9283 return Empty;
9284 end case;
9285 end Get_Formal_Entity;
9286
9287 --------------------
9288 -- Is_Instance_Of --
9289 --------------------
9290
9291 function Is_Instance_Of
9292 (Act_Spec : Entity_Id;
9293 Gen_Anc : Entity_Id) return Boolean
9294 is
9295 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9296
9297 begin
9298 if No (Gen_Par) then
9299 return False;
9300
9301 -- Simplest case: the generic parent of the actual is the formal
9302
9303 elsif Gen_Par = Gen_Anc then
9304 return True;
9305
9306 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9307 return False;
9308
9309 -- The actual may be obtained through several instantiations. Its
9310 -- scope must itself be an instance of a generic declared in the
9311 -- same scope as the formal. Any other case is detected above.
9312
9313 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9314 return False;
9315
9316 else
9317 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9318 end if;
9319 end Is_Instance_Of;
9320
9321 ---------------------------
9322 -- Process_Nested_Formal --
9323 ---------------------------
9324
9325 procedure Process_Nested_Formal (Formal : Entity_Id) is
9326 Ent : Entity_Id;
9327
9328 begin
9329 if Present (Associated_Formal_Package (Formal))
9330 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9331 then
9332 Ent := First_Entity (Formal);
9333 while Present (Ent) loop
9334 Set_Is_Hidden (Ent, False);
9335 Set_Is_Visible_Formal (Ent);
9336 Set_Is_Potentially_Use_Visible
9337 (Ent, Is_Potentially_Use_Visible (Formal));
9338
9339 if Ekind (Ent) = E_Package then
9340 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9341 Process_Nested_Formal (Ent);
9342 end if;
9343
9344 Next_Entity (Ent);
9345 end loop;
9346 end if;
9347 end Process_Nested_Formal;
9348
9349 -- Start of processing for Instantiate_Formal_Package
9350
9351 begin
9352 Analyze (Actual);
9353
9354 if not Is_Entity_Name (Actual)
9355 or else Ekind (Entity (Actual)) /= E_Package
9356 then
9357 Error_Msg_N
9358 ("expect package instance to instantiate formal", Actual);
9359 Abandon_Instantiation (Actual);
9360 raise Program_Error;
9361
9362 else
9363 Actual_Pack := Entity (Actual);
9364 Set_Is_Instantiated (Actual_Pack);
9365
9366 -- The actual may be a renamed package, or an outer generic formal
9367 -- package whose instantiation is converted into a renaming.
9368
9369 if Present (Renamed_Object (Actual_Pack)) then
9370 Actual_Pack := Renamed_Object (Actual_Pack);
9371 end if;
9372
9373 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9374 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9375 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9376 else
9377 Gen_Parent :=
9378 Generic_Parent (Specification (Analyzed_Formal));
9379 Formal_Pack :=
9380 Defining_Unit_Name (Specification (Analyzed_Formal));
9381 end if;
9382
9383 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9384 Parent_Spec := Package_Specification (Actual_Pack);
9385 else
9386 Parent_Spec := Parent (Actual_Pack);
9387 end if;
9388
9389 if Gen_Parent = Any_Id then
9390 Error_Msg_N
9391 ("previous error in declaration of formal package", Actual);
9392 Abandon_Instantiation (Actual);
9393
9394 elsif
9395 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9396 then
9397 null;
9398
9399 else
9400 Error_Msg_NE
9401 ("actual parameter must be instance of&", Actual, Gen_Parent);
9402 Abandon_Instantiation (Actual);
9403 end if;
9404
9405 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9406 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9407
9408 Nod :=
9409 Make_Package_Renaming_Declaration (Loc,
9410 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9411 Name => New_Occurrence_Of (Actual_Pack, Loc));
9412
9413 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
9414 Defining_Identifier (Formal));
9415 Decls := New_List (Nod);
9416
9417 -- If the formal F has a box, then the generic declarations are
9418 -- visible in the generic G. In an instance of G, the corresponding
9419 -- entities in the actual for F (which are the actuals for the
9420 -- instantiation of the generic that F denotes) must also be made
9421 -- visible for analysis of the current instance. On exit from the
9422 -- current instance, those entities are made private again. If the
9423 -- actual is currently in use, these entities are also use-visible.
9424
9425 -- The loop through the actual entities also steps through the formal
9426 -- entities and enters associations from formals to actuals into the
9427 -- renaming map. This is necessary to properly handle checking of
9428 -- actual parameter associations for later formals that depend on
9429 -- actuals declared in the formal package.
9430
9431 -- In Ada 2005, partial parameterization requires that we make
9432 -- visible the actuals corresponding to formals that were defaulted
9433 -- in the formal package. There formals are identified because they
9434 -- remain formal generics within the formal package, rather than
9435 -- being renamings of the actuals supplied.
9436
9437 declare
9438 Gen_Decl : constant Node_Id :=
9439 Unit_Declaration_Node (Gen_Parent);
9440 Formals : constant List_Id :=
9441 Generic_Formal_Declarations (Gen_Decl);
9442
9443 Actual_Ent : Entity_Id;
9444 Actual_Of_Formal : Node_Id;
9445 Formal_Node : Node_Id;
9446 Formal_Ent : Entity_Id;
9447
9448 begin
9449 if Present (Formals) then
9450 Formal_Node := First_Non_Pragma (Formals);
9451 else
9452 Formal_Node := Empty;
9453 end if;
9454
9455 Actual_Ent := First_Entity (Actual_Pack);
9456 Actual_Of_Formal :=
9457 First (Visible_Declarations (Specification (Analyzed_Formal)));
9458 while Present (Actual_Ent)
9459 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9460 loop
9461 if Present (Formal_Node) then
9462 Formal_Ent := Get_Formal_Entity (Formal_Node);
9463
9464 if Present (Formal_Ent) then
9465 Find_Matching_Actual (Formal_Node, Actual_Ent);
9466 Match_Formal_Entity
9467 (Formal_Node, Formal_Ent, Actual_Ent);
9468
9469 -- We iterate at the same time over the actuals of the
9470 -- local package created for the formal, to determine
9471 -- which one of the formals of the original generic were
9472 -- defaulted in the formal. The corresponding actual
9473 -- entities are visible in the enclosing instance.
9474
9475 if Box_Present (Formal)
9476 or else
9477 (Present (Actual_Of_Formal)
9478 and then
9479 Is_Generic_Formal
9480 (Get_Formal_Entity (Actual_Of_Formal)))
9481 then
9482 Set_Is_Hidden (Actual_Ent, False);
9483 Set_Is_Visible_Formal (Actual_Ent);
9484 Set_Is_Potentially_Use_Visible
9485 (Actual_Ent, In_Use (Actual_Pack));
9486
9487 if Ekind (Actual_Ent) = E_Package then
9488 Process_Nested_Formal (Actual_Ent);
9489 end if;
9490
9491 else
9492 Set_Is_Hidden (Actual_Ent);
9493 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9494 end if;
9495 end if;
9496
9497 Next_Non_Pragma (Formal_Node);
9498 Next (Actual_Of_Formal);
9499
9500 else
9501 -- No further formals to match, but the generic part may
9502 -- contain inherited operation that are not hidden in the
9503 -- enclosing instance.
9504
9505 Next_Entity (Actual_Ent);
9506 end if;
9507 end loop;
9508
9509 -- Inherited subprograms generated by formal derived types are
9510 -- also visible if the types are.
9511
9512 Actual_Ent := First_Entity (Actual_Pack);
9513 while Present (Actual_Ent)
9514 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9515 loop
9516 if Is_Overloadable (Actual_Ent)
9517 and then
9518 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9519 and then
9520 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9521 then
9522 Set_Is_Hidden (Actual_Ent, False);
9523 Set_Is_Potentially_Use_Visible
9524 (Actual_Ent, In_Use (Actual_Pack));
9525 end if;
9526
9527 Next_Entity (Actual_Ent);
9528 end loop;
9529 end;
9530
9531 -- If the formal is not declared with a box, reanalyze it as an
9532 -- abbreviated instantiation, to verify the matching rules of 12.7.
9533 -- The actual checks are performed after the generic associations
9534 -- have been analyzed, to guarantee the same visibility for this
9535 -- instantiation and for the actuals.
9536
9537 -- In Ada 2005, the generic associations for the formal can include
9538 -- defaulted parameters. These are ignored during check. This
9539 -- internal instantiation is removed from the tree after conformance
9540 -- checking, because it contains formal declarations for those
9541 -- defaulted parameters, and those should not reach the back-end.
9542
9543 if not Box_Present (Formal) then
9544 declare
9545 I_Pack : constant Entity_Id :=
9546 Make_Temporary (Sloc (Actual), 'P');
9547
9548 begin
9549 Set_Is_Internal (I_Pack);
9550
9551 Append_To (Decls,
9552 Make_Package_Instantiation (Sloc (Actual),
9553 Defining_Unit_Name => I_Pack,
9554 Name =>
9555 New_Occurrence_Of
9556 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9557 Generic_Associations =>
9558 Generic_Associations (Formal)));
9559 end;
9560 end if;
9561
9562 return Decls;
9563 end if;
9564 end Instantiate_Formal_Package;
9565
9566 -----------------------------------
9567 -- Instantiate_Formal_Subprogram --
9568 -----------------------------------
9569
9570 function Instantiate_Formal_Subprogram
9571 (Formal : Node_Id;
9572 Actual : Node_Id;
9573 Analyzed_Formal : Node_Id) return Node_Id
9574 is
9575 Analyzed_S : constant Entity_Id :=
9576 Defining_Unit_Name (Specification (Analyzed_Formal));
9577 Formal_Sub : constant Entity_Id :=
9578 Defining_Unit_Name (Specification (Formal));
9579
9580 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9581 -- If the generic is a child unit, the parent has been installed on the
9582 -- scope stack, but a default subprogram cannot resolve to something
9583 -- on the parent because that parent is not really part of the visible
9584 -- context (it is there to resolve explicit local entities). If the
9585 -- default has resolved in this way, we remove the entity from immediate
9586 -- visibility and analyze the node again to emit an error message or
9587 -- find another visible candidate.
9588
9589 procedure Valid_Actual_Subprogram (Act : Node_Id);
9590 -- Perform legality check and raise exception on failure
9591
9592 -----------------------
9593 -- From_Parent_Scope --
9594 -----------------------
9595
9596 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9597 Gen_Scope : Node_Id;
9598
9599 begin
9600 Gen_Scope := Scope (Analyzed_S);
9601 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9602 if Scope (Subp) = Scope (Gen_Scope) then
9603 return True;
9604 end if;
9605
9606 Gen_Scope := Scope (Gen_Scope);
9607 end loop;
9608
9609 return False;
9610 end From_Parent_Scope;
9611
9612 -----------------------------
9613 -- Valid_Actual_Subprogram --
9614 -----------------------------
9615
9616 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9617 Act_E : Entity_Id;
9618
9619 begin
9620 if Is_Entity_Name (Act) then
9621 Act_E := Entity (Act);
9622
9623 elsif Nkind (Act) = N_Selected_Component
9624 and then Is_Entity_Name (Selector_Name (Act))
9625 then
9626 Act_E := Entity (Selector_Name (Act));
9627
9628 else
9629 Act_E := Empty;
9630 end if;
9631
9632 if (Present (Act_E) and then Is_Overloadable (Act_E))
9633 or else Nkind_In (Act, N_Attribute_Reference,
9634 N_Indexed_Component,
9635 N_Character_Literal,
9636 N_Explicit_Dereference)
9637 then
9638 return;
9639 end if;
9640
9641 Error_Msg_NE
9642 ("expect subprogram or entry name in instantiation of&",
9643 Instantiation_Node, Formal_Sub);
9644 Abandon_Instantiation (Instantiation_Node);
9645 end Valid_Actual_Subprogram;
9646
9647 -- Local variables
9648
9649 Decl_Node : Node_Id;
9650 Loc : Source_Ptr;
9651 Nam : Node_Id;
9652 New_Spec : Node_Id;
9653
9654 -- Start of processing for Instantiate_Formal_Subprogram
9655
9656 begin
9657 New_Spec := New_Copy_Tree (Specification (Formal));
9658
9659 -- The tree copy has created the proper instantiation sloc for the
9660 -- new specification. Use this location for all other constructed
9661 -- declarations.
9662
9663 Loc := Sloc (Defining_Unit_Name (New_Spec));
9664
9665 -- Create new entity for the actual (New_Copy_Tree does not), and
9666 -- indicate that it is an actual.
9667
9668 Set_Defining_Unit_Name
9669 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9670 Set_Ekind (Defining_Unit_Name (New_Spec), Ekind (Analyzed_S));
9671 Set_Is_Generic_Actual_Subprogram (Defining_Unit_Name (New_Spec));
9672
9673 -- Create new entities for the each of the formals in the specification
9674 -- of the renaming declaration built for the actual.
9675
9676 if Present (Parameter_Specifications (New_Spec)) then
9677 declare
9678 F : Node_Id;
9679 F_Id : Entity_Id;
9680
9681 begin
9682 F := First (Parameter_Specifications (New_Spec));
9683 while Present (F) loop
9684 F_Id := Defining_Identifier (F);
9685
9686 Set_Defining_Identifier (F,
9687 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
9688 Next (F);
9689 end loop;
9690 end;
9691 end if;
9692
9693 -- Find entity of actual. If the actual is an attribute reference, it
9694 -- cannot be resolved here (its formal is missing) but is handled
9695 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9696 -- fully resolved subsequently, when the renaming declaration for the
9697 -- formal is analyzed. If it is an explicit dereference, resolve the
9698 -- prefix but not the actual itself, to prevent interpretation as call.
9699
9700 if Present (Actual) then
9701 Loc := Sloc (Actual);
9702 Set_Sloc (New_Spec, Loc);
9703
9704 if Nkind (Actual) = N_Operator_Symbol then
9705 Find_Direct_Name (Actual);
9706
9707 elsif Nkind (Actual) = N_Explicit_Dereference then
9708 Analyze (Prefix (Actual));
9709
9710 elsif Nkind (Actual) /= N_Attribute_Reference then
9711 Analyze (Actual);
9712 end if;
9713
9714 Valid_Actual_Subprogram (Actual);
9715 Nam := Actual;
9716
9717 elsif Present (Default_Name (Formal)) then
9718 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9719 N_Selected_Component,
9720 N_Indexed_Component,
9721 N_Character_Literal)
9722 and then Present (Entity (Default_Name (Formal)))
9723 then
9724 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9725 else
9726 Nam := New_Copy (Default_Name (Formal));
9727 Set_Sloc (Nam, Loc);
9728 end if;
9729
9730 elsif Box_Present (Formal) then
9731
9732 -- Actual is resolved at the point of instantiation. Create an
9733 -- identifier or operator with the same name as the formal.
9734
9735 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
9736 Nam :=
9737 Make_Operator_Symbol (Loc,
9738 Chars => Chars (Formal_Sub),
9739 Strval => No_String);
9740 else
9741 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
9742 end if;
9743
9744 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
9745 and then Null_Present (Specification (Formal))
9746 then
9747 -- Generate null body for procedure, for use in the instance
9748
9749 Decl_Node :=
9750 Make_Subprogram_Body (Loc,
9751 Specification => New_Spec,
9752 Declarations => New_List,
9753 Handled_Statement_Sequence =>
9754 Make_Handled_Sequence_Of_Statements (Loc,
9755 Statements => New_List (Make_Null_Statement (Loc))));
9756
9757 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
9758 return Decl_Node;
9759
9760 else
9761 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
9762 Error_Msg_NE
9763 ("missing actual&", Instantiation_Node, Formal_Sub);
9764 Error_Msg_NE
9765 ("\in instantiation of & declared#",
9766 Instantiation_Node, Scope (Analyzed_S));
9767 Abandon_Instantiation (Instantiation_Node);
9768 end if;
9769
9770 Decl_Node :=
9771 Make_Subprogram_Renaming_Declaration (Loc,
9772 Specification => New_Spec,
9773 Name => Nam);
9774
9775 -- If we do not have an actual and the formal specified <> then set to
9776 -- get proper default.
9777
9778 if No (Actual) and then Box_Present (Formal) then
9779 Set_From_Default (Decl_Node);
9780 end if;
9781
9782 -- Gather possible interpretations for the actual before analyzing the
9783 -- instance. If overloaded, it will be resolved when analyzing the
9784 -- renaming declaration.
9785
9786 if Box_Present (Formal) and then No (Actual) then
9787 Analyze (Nam);
9788
9789 if Is_Child_Unit (Scope (Analyzed_S))
9790 and then Present (Entity (Nam))
9791 then
9792 if not Is_Overloaded (Nam) then
9793 if From_Parent_Scope (Entity (Nam)) then
9794 Set_Is_Immediately_Visible (Entity (Nam), False);
9795 Set_Entity (Nam, Empty);
9796 Set_Etype (Nam, Empty);
9797
9798 Analyze (Nam);
9799 Set_Is_Immediately_Visible (Entity (Nam));
9800 end if;
9801
9802 else
9803 declare
9804 I : Interp_Index;
9805 It : Interp;
9806
9807 begin
9808 Get_First_Interp (Nam, I, It);
9809 while Present (It.Nam) loop
9810 if From_Parent_Scope (It.Nam) then
9811 Remove_Interp (I);
9812 end if;
9813
9814 Get_Next_Interp (I, It);
9815 end loop;
9816 end;
9817 end if;
9818 end if;
9819 end if;
9820
9821 -- The generic instantiation freezes the actual. This can only be done
9822 -- once the actual is resolved, in the analysis of the renaming
9823 -- declaration. To make the formal subprogram entity available, we set
9824 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9825 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9826 -- of formal abstract subprograms.
9827
9828 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9829
9830 -- We cannot analyze the renaming declaration, and thus find the actual,
9831 -- until all the actuals are assembled in the instance. For subsequent
9832 -- checks of other actuals, indicate the node that will hold the
9833 -- instance of this formal.
9834
9835 Set_Instance_Of (Analyzed_S, Nam);
9836
9837 if Nkind (Actual) = N_Selected_Component
9838 and then Is_Task_Type (Etype (Prefix (Actual)))
9839 and then not Is_Frozen (Etype (Prefix (Actual)))
9840 then
9841 -- The renaming declaration will create a body, which must appear
9842 -- outside of the instantiation, We move the renaming declaration
9843 -- out of the instance, and create an additional renaming inside,
9844 -- to prevent freezing anomalies.
9845
9846 declare
9847 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9848
9849 begin
9850 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9851 Insert_Before (Instantiation_Node, Decl_Node);
9852 Analyze (Decl_Node);
9853
9854 -- Now create renaming within the instance
9855
9856 Decl_Node :=
9857 Make_Subprogram_Renaming_Declaration (Loc,
9858 Specification => New_Copy_Tree (New_Spec),
9859 Name => New_Occurrence_Of (Anon_Id, Loc));
9860
9861 Set_Defining_Unit_Name (Specification (Decl_Node),
9862 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9863 end;
9864 end if;
9865
9866 return Decl_Node;
9867 end Instantiate_Formal_Subprogram;
9868
9869 ------------------------
9870 -- Instantiate_Object --
9871 ------------------------
9872
9873 function Instantiate_Object
9874 (Formal : Node_Id;
9875 Actual : Node_Id;
9876 Analyzed_Formal : Node_Id) return List_Id
9877 is
9878 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9879 A_Gen_Obj : constant Entity_Id :=
9880 Defining_Identifier (Analyzed_Formal);
9881 Acc_Def : Node_Id := Empty;
9882 Act_Assoc : constant Node_Id := Parent (Actual);
9883 Actual_Decl : Node_Id := Empty;
9884 Decl_Node : Node_Id;
9885 Def : Node_Id;
9886 Ftyp : Entity_Id;
9887 List : constant List_Id := New_List;
9888 Loc : constant Source_Ptr := Sloc (Actual);
9889 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9890 Subt_Decl : Node_Id := Empty;
9891 Subt_Mark : Node_Id := Empty;
9892
9893 begin
9894 if Present (Subtype_Mark (Formal)) then
9895 Subt_Mark := Subtype_Mark (Formal);
9896 else
9897 Check_Access_Definition (Formal);
9898 Acc_Def := Access_Definition (Formal);
9899 end if;
9900
9901 -- Sloc for error message on missing actual
9902
9903 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9904
9905 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9906 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9907 end if;
9908
9909 Set_Parent (List, Parent (Actual));
9910
9911 -- OUT present
9912
9913 if Out_Present (Formal) then
9914
9915 -- An IN OUT generic actual must be a name. The instantiation is a
9916 -- renaming declaration. The actual is the name being renamed. We
9917 -- use the actual directly, rather than a copy, because it is not
9918 -- used further in the list of actuals, and because a copy or a use
9919 -- of relocate_node is incorrect if the instance is nested within a
9920 -- generic. In order to simplify ASIS searches, the Generic_Parent
9921 -- field links the declaration to the generic association.
9922
9923 if No (Actual) then
9924 Error_Msg_NE
9925 ("missing actual&",
9926 Instantiation_Node, Gen_Obj);
9927 Error_Msg_NE
9928 ("\in instantiation of & declared#",
9929 Instantiation_Node, Scope (A_Gen_Obj));
9930 Abandon_Instantiation (Instantiation_Node);
9931 end if;
9932
9933 if Present (Subt_Mark) then
9934 Decl_Node :=
9935 Make_Object_Renaming_Declaration (Loc,
9936 Defining_Identifier => New_Copy (Gen_Obj),
9937 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9938 Name => Actual);
9939
9940 else pragma Assert (Present (Acc_Def));
9941 Decl_Node :=
9942 Make_Object_Renaming_Declaration (Loc,
9943 Defining_Identifier => New_Copy (Gen_Obj),
9944 Access_Definition => New_Copy_Tree (Acc_Def),
9945 Name => Actual);
9946 end if;
9947
9948 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9949
9950 -- The analysis of the actual may produce Insert_Action nodes, so
9951 -- the declaration must have a context in which to attach them.
9952
9953 Append (Decl_Node, List);
9954 Analyze (Actual);
9955
9956 -- Return if the analysis of the actual reported some error
9957
9958 if Etype (Actual) = Any_Type then
9959 return List;
9960 end if;
9961
9962 -- This check is performed here because Analyze_Object_Renaming will
9963 -- not check it when Comes_From_Source is False. Note though that the
9964 -- check for the actual being the name of an object will be performed
9965 -- in Analyze_Object_Renaming.
9966
9967 if Is_Object_Reference (Actual)
9968 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
9969 then
9970 Error_Msg_N
9971 ("illegal discriminant-dependent component for in out parameter",
9972 Actual);
9973 end if;
9974
9975 -- The actual has to be resolved in order to check that it is a
9976 -- variable (due to cases such as F (1), where F returns access to
9977 -- an array, and for overloaded prefixes).
9978
9979 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
9980
9981 -- If the type of the formal is not itself a formal, and the current
9982 -- unit is a child unit, the formal type must be declared in a
9983 -- parent, and must be retrieved by visibility.
9984
9985 if Ftyp = Orig_Ftyp
9986 and then Is_Generic_Unit (Scope (Ftyp))
9987 and then Is_Child_Unit (Scope (A_Gen_Obj))
9988 then
9989 declare
9990 Temp : constant Node_Id :=
9991 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
9992 begin
9993 Set_Entity (Temp, Empty);
9994 Find_Type (Temp);
9995 Ftyp := Entity (Temp);
9996 end;
9997 end if;
9998
9999 if Is_Private_Type (Ftyp)
10000 and then not Is_Private_Type (Etype (Actual))
10001 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10002 or else Base_Type (Etype (Actual)) = Ftyp)
10003 then
10004 -- If the actual has the type of the full view of the formal, or
10005 -- else a non-private subtype of the formal, then the visibility
10006 -- of the formal type has changed. Add to the actuals a subtype
10007 -- declaration that will force the exchange of views in the body
10008 -- of the instance as well.
10009
10010 Subt_Decl :=
10011 Make_Subtype_Declaration (Loc,
10012 Defining_Identifier => Make_Temporary (Loc, 'P'),
10013 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10014
10015 Prepend (Subt_Decl, List);
10016
10017 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10018 Exchange_Declarations (Ftyp);
10019 end if;
10020
10021 Resolve (Actual, Ftyp);
10022
10023 if not Denotes_Variable (Actual) then
10024 Error_Msg_NE
10025 ("actual for& must be a variable", Actual, Gen_Obj);
10026
10027 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10028
10029 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10030 -- the type of the actual shall resolve to a specific anonymous
10031 -- access type.
10032
10033 if Ada_Version < Ada_2005
10034 or else
10035 Ekind (Base_Type (Ftyp)) /=
10036 E_Anonymous_Access_Type
10037 or else
10038 Ekind (Base_Type (Etype (Actual))) /=
10039 E_Anonymous_Access_Type
10040 then
10041 Error_Msg_NE ("type of actual does not match type of&",
10042 Actual, Gen_Obj);
10043 end if;
10044 end if;
10045
10046 Note_Possible_Modification (Actual, Sure => True);
10047
10048 -- Check for instantiation of atomic/volatile actual for
10049 -- non-atomic/volatile formal (RM C.6 (12)).
10050
10051 if Is_Atomic_Object (Actual)
10052 and then not Is_Atomic (Orig_Ftyp)
10053 then
10054 Error_Msg_N
10055 ("cannot instantiate non-atomic formal object " &
10056 "with atomic actual", Actual);
10057
10058 elsif Is_Volatile_Object (Actual)
10059 and then not Is_Volatile (Orig_Ftyp)
10060 then
10061 Error_Msg_N
10062 ("cannot instantiate non-volatile formal object " &
10063 "with volatile actual", Actual);
10064 end if;
10065
10066 -- Formal in-parameter
10067
10068 else
10069 -- The instantiation of a generic formal in-parameter is constant
10070 -- declaration. The actual is the expression for that declaration.
10071
10072 if Present (Actual) then
10073 if Present (Subt_Mark) then
10074 Def := Subt_Mark;
10075 else pragma Assert (Present (Acc_Def));
10076 Def := Acc_Def;
10077 end if;
10078
10079 Decl_Node :=
10080 Make_Object_Declaration (Loc,
10081 Defining_Identifier => New_Copy (Gen_Obj),
10082 Constant_Present => True,
10083 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10084 Object_Definition => New_Copy_Tree (Def),
10085 Expression => Actual);
10086
10087 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10088
10089 -- A generic formal object of a tagged type is defined to be
10090 -- aliased so the new constant must also be treated as aliased.
10091
10092 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10093 Set_Aliased_Present (Decl_Node);
10094 end if;
10095
10096 Append (Decl_Node, List);
10097
10098 -- No need to repeat (pre-)analysis of some expression nodes
10099 -- already handled in Preanalyze_Actuals.
10100
10101 if Nkind (Actual) /= N_Allocator then
10102 Analyze (Actual);
10103
10104 -- Return if the analysis of the actual reported some error
10105
10106 if Etype (Actual) = Any_Type then
10107 return List;
10108 end if;
10109 end if;
10110
10111 declare
10112 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10113 Typ : Entity_Id;
10114
10115 begin
10116 Typ := Get_Instance_Of (Formal_Type);
10117
10118 Freeze_Before (Instantiation_Node, Typ);
10119
10120 -- If the actual is an aggregate, perform name resolution on
10121 -- its components (the analysis of an aggregate does not do it)
10122 -- to capture local names that may be hidden if the generic is
10123 -- a child unit.
10124
10125 if Nkind (Actual) = N_Aggregate then
10126 Preanalyze_And_Resolve (Actual, Typ);
10127 end if;
10128
10129 if Is_Limited_Type (Typ)
10130 and then not OK_For_Limited_Init (Typ, Actual)
10131 then
10132 Error_Msg_N
10133 ("initialization not allowed for limited types", Actual);
10134 Explain_Limited_Type (Typ, Actual);
10135 end if;
10136 end;
10137
10138 elsif Present (Default_Expression (Formal)) then
10139
10140 -- Use default to construct declaration
10141
10142 if Present (Subt_Mark) then
10143 Def := Subt_Mark;
10144 else pragma Assert (Present (Acc_Def));
10145 Def := Acc_Def;
10146 end if;
10147
10148 Decl_Node :=
10149 Make_Object_Declaration (Sloc (Formal),
10150 Defining_Identifier => New_Copy (Gen_Obj),
10151 Constant_Present => True,
10152 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10153 Object_Definition => New_Copy (Def),
10154 Expression => New_Copy_Tree
10155 (Default_Expression (Formal)));
10156
10157 Append (Decl_Node, List);
10158 Set_Analyzed (Expression (Decl_Node), False);
10159
10160 else
10161 Error_Msg_NE
10162 ("missing actual&",
10163 Instantiation_Node, Gen_Obj);
10164 Error_Msg_NE ("\in instantiation of & declared#",
10165 Instantiation_Node, Scope (A_Gen_Obj));
10166
10167 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10168
10169 -- Create dummy constant declaration so that instance can be
10170 -- analyzed, to minimize cascaded visibility errors.
10171
10172 if Present (Subt_Mark) then
10173 Def := Subt_Mark;
10174 else pragma Assert (Present (Acc_Def));
10175 Def := Acc_Def;
10176 end if;
10177
10178 Decl_Node :=
10179 Make_Object_Declaration (Loc,
10180 Defining_Identifier => New_Copy (Gen_Obj),
10181 Constant_Present => True,
10182 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10183 Object_Definition => New_Copy (Def),
10184 Expression =>
10185 Make_Attribute_Reference (Sloc (Gen_Obj),
10186 Attribute_Name => Name_First,
10187 Prefix => New_Copy (Def)));
10188
10189 Append (Decl_Node, List);
10190
10191 else
10192 Abandon_Instantiation (Instantiation_Node);
10193 end if;
10194 end if;
10195 end if;
10196
10197 if Nkind (Actual) in N_Has_Entity then
10198 Actual_Decl := Parent (Entity (Actual));
10199 end if;
10200
10201 -- Ada 2005 (AI-423): For a formal object declaration with a null
10202 -- exclusion or an access definition that has a null exclusion: If the
10203 -- actual matching the formal object declaration denotes a generic
10204 -- formal object of another generic unit G, and the instantiation
10205 -- containing the actual occurs within the body of G or within the body
10206 -- of a generic unit declared within the declarative region of G, then
10207 -- the declaration of the formal object of G must have a null exclusion.
10208 -- Otherwise, the subtype of the actual matching the formal object
10209 -- declaration shall exclude null.
10210
10211 if Ada_Version >= Ada_2005
10212 and then Present (Actual_Decl)
10213 and then
10214 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10215 N_Object_Declaration)
10216 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10217 and then not Has_Null_Exclusion (Actual_Decl)
10218 and then Has_Null_Exclusion (Analyzed_Formal)
10219 then
10220 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10221 Error_Msg_N
10222 ("actual must exclude null to match generic formal#", Actual);
10223 end if;
10224
10225 -- An effectively volatile object cannot be used as an actual in
10226 -- a generic instance. The following check is only relevant when
10227 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10228
10229 if SPARK_Mode = On
10230 and then Present (Actual)
10231 and then Is_Effectively_Volatile_Object (Actual)
10232 then
10233 Error_Msg_N
10234 ("volatile object cannot act as actual in generic instantiation "
10235 & "(SPARK RM 7.1.3(8))", Actual);
10236 end if;
10237
10238 return List;
10239 end Instantiate_Object;
10240
10241 ------------------------------
10242 -- Instantiate_Package_Body --
10243 ------------------------------
10244
10245 procedure Instantiate_Package_Body
10246 (Body_Info : Pending_Body_Info;
10247 Inlined_Body : Boolean := False;
10248 Body_Optional : Boolean := False)
10249 is
10250 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10251 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10252 Loc : constant Source_Ptr := Sloc (Inst_Node);
10253
10254 Gen_Id : constant Node_Id := Name (Inst_Node);
10255 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10256 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10257 Act_Spec : constant Node_Id := Specification (Act_Decl);
10258 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10259
10260 Act_Body_Name : Node_Id;
10261 Gen_Body : Node_Id;
10262 Gen_Body_Id : Node_Id;
10263 Act_Body : Node_Id;
10264 Act_Body_Id : Entity_Id;
10265
10266 Parent_Installed : Boolean := False;
10267 Save_Style_Check : constant Boolean := Style_Check;
10268
10269 Par_Ent : Entity_Id := Empty;
10270 Par_Vis : Boolean := False;
10271
10272 Vis_Prims_List : Elist_Id := No_Elist;
10273 -- List of primitives made temporarily visible in the instantiation
10274 -- to match the visibility of the formal type
10275
10276 procedure Check_Initialized_Types;
10277 -- In a generic package body, an entity of a generic private type may
10278 -- appear uninitialized. This is suspicious, unless the actual is a
10279 -- fully initialized type.
10280
10281 -----------------------------
10282 -- Check_Initialized_Types --
10283 -----------------------------
10284
10285 procedure Check_Initialized_Types is
10286 Decl : Node_Id;
10287 Formal : Entity_Id;
10288 Actual : Entity_Id;
10289 Uninit_Var : Entity_Id;
10290
10291 begin
10292 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10293 while Present (Decl) loop
10294 Uninit_Var := Empty;
10295
10296 if Nkind (Decl) = N_Private_Extension_Declaration then
10297 Uninit_Var := Uninitialized_Variable (Decl);
10298
10299 elsif Nkind (Decl) = N_Formal_Type_Declaration
10300 and then Nkind (Formal_Type_Definition (Decl)) =
10301 N_Formal_Private_Type_Definition
10302 then
10303 Uninit_Var :=
10304 Uninitialized_Variable (Formal_Type_Definition (Decl));
10305 end if;
10306
10307 if Present (Uninit_Var) then
10308 Formal := Defining_Identifier (Decl);
10309 Actual := First_Entity (Act_Decl_Id);
10310
10311 -- For each formal there is a subtype declaration that renames
10312 -- the actual and has the same name as the formal. Locate the
10313 -- formal for warning message about uninitialized variables
10314 -- in the generic, for which the actual type should be a fully
10315 -- initialized type.
10316
10317 while Present (Actual) loop
10318 exit when Ekind (Actual) = E_Package
10319 and then Present (Renamed_Object (Actual));
10320
10321 if Chars (Actual) = Chars (Formal)
10322 and then not Is_Scalar_Type (Actual)
10323 and then not Is_Fully_Initialized_Type (Actual)
10324 and then Warn_On_No_Value_Assigned
10325 then
10326 Error_Msg_Node_2 := Formal;
10327 Error_Msg_NE
10328 ("generic unit has uninitialized variable& of "
10329 & "formal private type &?v?", Actual, Uninit_Var);
10330 Error_Msg_NE
10331 ("actual type for& should be fully initialized type?v?",
10332 Actual, Formal);
10333 exit;
10334 end if;
10335
10336 Next_Entity (Actual);
10337 end loop;
10338 end if;
10339
10340 Next (Decl);
10341 end loop;
10342 end Check_Initialized_Types;
10343
10344 -- Start of processing for Instantiate_Package_Body
10345
10346 begin
10347 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10348
10349 -- The instance body may already have been processed, as the parent of
10350 -- another instance that is inlined (Load_Parent_Of_Generic).
10351
10352 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10353 return;
10354 end if;
10355
10356 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10357
10358 -- Re-establish the state of information on which checks are suppressed.
10359 -- This information was set in Body_Info at the point of instantiation,
10360 -- and now we restore it so that the instance is compiled using the
10361 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10362
10363 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10364 Scope_Suppress := Body_Info.Scope_Suppress;
10365 Opt.Ada_Version := Body_Info.Version;
10366 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10367 Restore_Warnings (Body_Info.Warnings);
10368 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10369 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10370
10371 if No (Gen_Body_Id) then
10372
10373 -- Do not look for parent of generic body if none is required.
10374 -- This may happen when the routine is called as part of the
10375 -- Pending_Instantiations processing, when nested instances
10376 -- may precede the one generated from the main unit.
10377
10378 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10379 and then Body_Optional
10380 then
10381 return;
10382 else
10383 Load_Parent_Of_Generic
10384 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10385 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10386 end if;
10387 end if;
10388
10389 -- Establish global variable for sloc adjustment and for error recovery
10390
10391 Instantiation_Node := Inst_Node;
10392
10393 if Present (Gen_Body_Id) then
10394 Save_Env (Gen_Unit, Act_Decl_Id);
10395 Style_Check := False;
10396 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10397
10398 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10399
10400 Create_Instantiation_Source
10401 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10402
10403 Act_Body :=
10404 Copy_Generic_Node
10405 (Original_Node (Gen_Body), Empty, Instantiating => True);
10406
10407 -- Build new name (possibly qualified) for body declaration
10408
10409 Act_Body_Id := New_Copy (Act_Decl_Id);
10410
10411 -- Some attributes of spec entity are not inherited by body entity
10412
10413 Set_Handler_Records (Act_Body_Id, No_List);
10414
10415 if Nkind (Defining_Unit_Name (Act_Spec)) =
10416 N_Defining_Program_Unit_Name
10417 then
10418 Act_Body_Name :=
10419 Make_Defining_Program_Unit_Name (Loc,
10420 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10421 Defining_Identifier => Act_Body_Id);
10422 else
10423 Act_Body_Name := Act_Body_Id;
10424 end if;
10425
10426 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10427
10428 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10429 Check_Generic_Actuals (Act_Decl_Id, False);
10430 Check_Initialized_Types;
10431
10432 -- Install primitives hidden at the point of the instantiation but
10433 -- visible when processing the generic formals
10434
10435 declare
10436 E : Entity_Id;
10437
10438 begin
10439 E := First_Entity (Act_Decl_Id);
10440 while Present (E) loop
10441 if Is_Type (E)
10442 and then Is_Generic_Actual_Type (E)
10443 and then Is_Tagged_Type (E)
10444 then
10445 Install_Hidden_Primitives
10446 (Prims_List => Vis_Prims_List,
10447 Gen_T => Generic_Parent_Type (Parent (E)),
10448 Act_T => E);
10449 end if;
10450
10451 Next_Entity (E);
10452 end loop;
10453 end;
10454
10455 -- If it is a child unit, make the parent instance (which is an
10456 -- instance of the parent of the generic) visible. The parent
10457 -- instance is the prefix of the name of the generic unit.
10458
10459 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10460 and then Nkind (Gen_Id) = N_Expanded_Name
10461 then
10462 Par_Ent := Entity (Prefix (Gen_Id));
10463 Par_Vis := Is_Immediately_Visible (Par_Ent);
10464 Install_Parent (Par_Ent, In_Body => True);
10465 Parent_Installed := True;
10466
10467 elsif Is_Child_Unit (Gen_Unit) then
10468 Par_Ent := Scope (Gen_Unit);
10469 Par_Vis := Is_Immediately_Visible (Par_Ent);
10470 Install_Parent (Par_Ent, In_Body => True);
10471 Parent_Installed := True;
10472 end if;
10473
10474 -- If the instantiation is a library unit, and this is the main unit,
10475 -- then build the resulting compilation unit nodes for the instance.
10476 -- If this is a compilation unit but it is not the main unit, then it
10477 -- is the body of a unit in the context, that is being compiled
10478 -- because it is encloses some inlined unit or another generic unit
10479 -- being instantiated. In that case, this body is not part of the
10480 -- current compilation, and is not attached to the tree, but its
10481 -- parent must be set for analysis.
10482
10483 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10484
10485 -- Replace instance node with body of instance, and create new
10486 -- node for corresponding instance declaration.
10487
10488 Build_Instance_Compilation_Unit_Nodes
10489 (Inst_Node, Act_Body, Act_Decl);
10490 Analyze (Inst_Node);
10491
10492 if Parent (Inst_Node) = Cunit (Main_Unit) then
10493
10494 -- If the instance is a child unit itself, then set the scope
10495 -- of the expanded body to be the parent of the instantiation
10496 -- (ensuring that the fully qualified name will be generated
10497 -- for the elaboration subprogram).
10498
10499 if Nkind (Defining_Unit_Name (Act_Spec)) =
10500 N_Defining_Program_Unit_Name
10501 then
10502 Set_Scope
10503 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10504 end if;
10505 end if;
10506
10507 -- Case where instantiation is not a library unit
10508
10509 else
10510 -- If this is an early instantiation, i.e. appears textually
10511 -- before the corresponding body and must be elaborated first,
10512 -- indicate that the body instance is to be delayed.
10513
10514 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10515
10516 -- Now analyze the body. We turn off all checks if this is an
10517 -- internal unit, since there is no reason to have checks on for
10518 -- any predefined run-time library code. All such code is designed
10519 -- to be compiled with checks off.
10520
10521 -- Note that we do NOT apply this criterion to children of GNAT
10522 -- The latter units must suppress checks explicitly if needed.
10523
10524 if Is_Predefined_File_Name
10525 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10526 then
10527 Analyze (Act_Body, Suppress => All_Checks);
10528 else
10529 Analyze (Act_Body);
10530 end if;
10531 end if;
10532
10533 Inherit_Context (Gen_Body, Inst_Node);
10534
10535 -- Remove the parent instances if they have been placed on the scope
10536 -- stack to compile the body.
10537
10538 if Parent_Installed then
10539 Remove_Parent (In_Body => True);
10540
10541 -- Restore the previous visibility of the parent
10542
10543 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10544 end if;
10545
10546 Restore_Hidden_Primitives (Vis_Prims_List);
10547 Restore_Private_Views (Act_Decl_Id);
10548
10549 -- Remove the current unit from visibility if this is an instance
10550 -- that is not elaborated on the fly for inlining purposes.
10551
10552 if not Inlined_Body then
10553 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10554 end if;
10555
10556 Restore_Env;
10557 Style_Check := Save_Style_Check;
10558
10559 -- If we have no body, and the unit requires a body, then complain. This
10560 -- complaint is suppressed if we have detected other errors (since a
10561 -- common reason for missing the body is that it had errors).
10562 -- In CodePeer mode, a warning has been emitted already, no need for
10563 -- further messages.
10564
10565 elsif Unit_Requires_Body (Gen_Unit)
10566 and then not Body_Optional
10567 then
10568 if CodePeer_Mode then
10569 null;
10570
10571 elsif Serious_Errors_Detected = 0 then
10572 Error_Msg_NE
10573 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10574
10575 -- Don't attempt to perform any cleanup actions if some other error
10576 -- was already detected, since this can cause blowups.
10577
10578 else
10579 return;
10580 end if;
10581
10582 -- Case of package that does not need a body
10583
10584 else
10585 -- If the instantiation of the declaration is a library unit, rewrite
10586 -- the original package instantiation as a package declaration in the
10587 -- compilation unit node.
10588
10589 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10590 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10591 Rewrite (Inst_Node, Act_Decl);
10592
10593 -- Generate elaboration entity, in case spec has elaboration code.
10594 -- This cannot be done when the instance is analyzed, because it
10595 -- is not known yet whether the body exists.
10596
10597 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10598 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10599
10600 -- If the instantiation is not a library unit, then append the
10601 -- declaration to the list of implicitly generated entities, unless
10602 -- it is already a list member which means that it was already
10603 -- processed
10604
10605 elsif not Is_List_Member (Act_Decl) then
10606 Mark_Rewrite_Insertion (Act_Decl);
10607 Insert_Before (Inst_Node, Act_Decl);
10608 end if;
10609 end if;
10610
10611 Expander_Mode_Restore;
10612 end Instantiate_Package_Body;
10613
10614 ---------------------------------
10615 -- Instantiate_Subprogram_Body --
10616 ---------------------------------
10617
10618 procedure Instantiate_Subprogram_Body
10619 (Body_Info : Pending_Body_Info;
10620 Body_Optional : Boolean := False)
10621 is
10622 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10623 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10624 Loc : constant Source_Ptr := Sloc (Inst_Node);
10625 Gen_Id : constant Node_Id := Name (Inst_Node);
10626 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10627 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10628 Anon_Id : constant Entity_Id :=
10629 Defining_Unit_Name (Specification (Act_Decl));
10630 Pack_Id : constant Entity_Id :=
10631 Defining_Unit_Name (Parent (Act_Decl));
10632 Decls : List_Id;
10633 Gen_Body : Node_Id;
10634 Gen_Body_Id : Node_Id;
10635 Act_Body : Node_Id;
10636 Pack_Body : Node_Id;
10637 Prev_Formal : Entity_Id;
10638 Ret_Expr : Node_Id;
10639 Unit_Renaming : Node_Id;
10640
10641 Parent_Installed : Boolean := False;
10642
10643 Saved_Style_Check : constant Boolean := Style_Check;
10644 Saved_Warnings : constant Warning_Record := Save_Warnings;
10645
10646 Par_Ent : Entity_Id := Empty;
10647 Par_Vis : Boolean := False;
10648
10649 begin
10650 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10651
10652 -- Subprogram body may have been created already because of an inline
10653 -- pragma, or because of multiple elaborations of the enclosing package
10654 -- when several instances of the subprogram appear in the main unit.
10655
10656 if Present (Corresponding_Body (Act_Decl)) then
10657 return;
10658 end if;
10659
10660 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10661
10662 -- Re-establish the state of information on which checks are suppressed.
10663 -- This information was set in Body_Info at the point of instantiation,
10664 -- and now we restore it so that the instance is compiled using the
10665 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10666
10667 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10668 Scope_Suppress := Body_Info.Scope_Suppress;
10669 Opt.Ada_Version := Body_Info.Version;
10670 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10671 Restore_Warnings (Body_Info.Warnings);
10672 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10673 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10674
10675 if No (Gen_Body_Id) then
10676
10677 -- For imported generic subprogram, no body to compile, complete
10678 -- the spec entity appropriately.
10679
10680 if Is_Imported (Gen_Unit) then
10681 Set_Is_Imported (Anon_Id);
10682 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10683 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
10684 Set_Convention (Anon_Id, Convention (Gen_Unit));
10685 Set_Has_Completion (Anon_Id);
10686 return;
10687
10688 -- For other cases, compile the body
10689
10690 else
10691 Load_Parent_Of_Generic
10692 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10693 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10694 end if;
10695 end if;
10696
10697 Instantiation_Node := Inst_Node;
10698
10699 if Present (Gen_Body_Id) then
10700 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10701
10702 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
10703
10704 -- Either body is not present, or context is non-expanding, as
10705 -- when compiling a subunit. Mark the instance as completed, and
10706 -- diagnose a missing body when needed.
10707
10708 if Expander_Active
10709 and then Operating_Mode = Generate_Code
10710 then
10711 Error_Msg_N
10712 ("missing proper body for instantiation", Gen_Body);
10713 end if;
10714
10715 Set_Has_Completion (Anon_Id);
10716 return;
10717 end if;
10718
10719 Save_Env (Gen_Unit, Anon_Id);
10720 Style_Check := False;
10721 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10722 Create_Instantiation_Source
10723 (Inst_Node,
10724 Gen_Body_Id,
10725 False,
10726 S_Adjustment);
10727
10728 Act_Body :=
10729 Copy_Generic_Node
10730 (Original_Node (Gen_Body), Empty, Instantiating => True);
10731
10732 -- Create proper defining name for the body, to correspond to
10733 -- the one in the spec.
10734
10735 Set_Defining_Unit_Name (Specification (Act_Body),
10736 Make_Defining_Identifier
10737 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
10738 Set_Corresponding_Spec (Act_Body, Anon_Id);
10739 Set_Has_Completion (Anon_Id);
10740 Check_Generic_Actuals (Pack_Id, False);
10741
10742 -- Generate a reference to link the visible subprogram instance to
10743 -- the generic body, which for navigation purposes is the only
10744 -- available source for the instance.
10745
10746 Generate_Reference
10747 (Related_Instance (Pack_Id),
10748 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
10749
10750 -- If it is a child unit, make the parent instance (which is an
10751 -- instance of the parent of the generic) visible. The parent
10752 -- instance is the prefix of the name of the generic unit.
10753
10754 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10755 and then Nkind (Gen_Id) = N_Expanded_Name
10756 then
10757 Par_Ent := Entity (Prefix (Gen_Id));
10758 Par_Vis := Is_Immediately_Visible (Par_Ent);
10759 Install_Parent (Par_Ent, In_Body => True);
10760 Parent_Installed := True;
10761
10762 elsif Is_Child_Unit (Gen_Unit) then
10763 Par_Ent := Scope (Gen_Unit);
10764 Par_Vis := Is_Immediately_Visible (Par_Ent);
10765 Install_Parent (Par_Ent, In_Body => True);
10766 Parent_Installed := True;
10767 end if;
10768
10769 -- Inside its body, a reference to the generic unit is a reference
10770 -- to the instance. The corresponding renaming is the first
10771 -- declaration in the body.
10772
10773 Unit_Renaming :=
10774 Make_Subprogram_Renaming_Declaration (Loc,
10775 Specification =>
10776 Copy_Generic_Node (
10777 Specification (Original_Node (Gen_Body)),
10778 Empty,
10779 Instantiating => True),
10780 Name => New_Occurrence_Of (Anon_Id, Loc));
10781
10782 -- If there is a formal subprogram with the same name as the unit
10783 -- itself, do not add this renaming declaration. This is a temporary
10784 -- fix for one ACVC test. ???
10785
10786 Prev_Formal := First_Entity (Pack_Id);
10787 while Present (Prev_Formal) loop
10788 if Chars (Prev_Formal) = Chars (Gen_Unit)
10789 and then Is_Overloadable (Prev_Formal)
10790 then
10791 exit;
10792 end if;
10793
10794 Next_Entity (Prev_Formal);
10795 end loop;
10796
10797 if Present (Prev_Formal) then
10798 Decls := New_List (Act_Body);
10799 else
10800 Decls := New_List (Unit_Renaming, Act_Body);
10801 end if;
10802
10803 -- The subprogram body is placed in the body of a dummy package body,
10804 -- whose spec contains the subprogram declaration as well as the
10805 -- renaming declarations for the generic parameters.
10806
10807 Pack_Body := Make_Package_Body (Loc,
10808 Defining_Unit_Name => New_Copy (Pack_Id),
10809 Declarations => Decls);
10810
10811 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10812
10813 -- If the instantiation is a library unit, then build resulting
10814 -- compilation unit nodes for the instance. The declaration of
10815 -- the enclosing package is the grandparent of the subprogram
10816 -- declaration. First replace the instantiation node as the unit
10817 -- of the corresponding compilation.
10818
10819 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10820 if Parent (Inst_Node) = Cunit (Main_Unit) then
10821 Set_Unit (Parent (Inst_Node), Inst_Node);
10822 Build_Instance_Compilation_Unit_Nodes
10823 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
10824 Analyze (Inst_Node);
10825 else
10826 Set_Parent (Pack_Body, Parent (Inst_Node));
10827 Analyze (Pack_Body);
10828 end if;
10829
10830 else
10831 Insert_Before (Inst_Node, Pack_Body);
10832 Mark_Rewrite_Insertion (Pack_Body);
10833 Analyze (Pack_Body);
10834
10835 if Expander_Active then
10836 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
10837 end if;
10838 end if;
10839
10840 Inherit_Context (Gen_Body, Inst_Node);
10841
10842 Restore_Private_Views (Pack_Id, False);
10843
10844 if Parent_Installed then
10845 Remove_Parent (In_Body => True);
10846
10847 -- Restore the previous visibility of the parent
10848
10849 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10850 end if;
10851
10852 Restore_Env;
10853 Style_Check := Saved_Style_Check;
10854 Restore_Warnings (Saved_Warnings);
10855
10856 -- Body not found. Error was emitted already. If there were no previous
10857 -- errors, this may be an instance whose scope is a premature instance.
10858 -- In that case we must insure that the (legal) program does raise
10859 -- program error if executed. We generate a subprogram body for this
10860 -- purpose. See DEC ac30vso.
10861
10862 -- Should not reference proprietary DEC tests in comments ???
10863
10864 elsif Serious_Errors_Detected = 0
10865 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
10866 then
10867 if Body_Optional then
10868 return;
10869
10870 elsif Ekind (Anon_Id) = E_Procedure then
10871 Act_Body :=
10872 Make_Subprogram_Body (Loc,
10873 Specification =>
10874 Make_Procedure_Specification (Loc,
10875 Defining_Unit_Name =>
10876 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10877 Parameter_Specifications =>
10878 New_Copy_List
10879 (Parameter_Specifications (Parent (Anon_Id)))),
10880
10881 Declarations => Empty_List,
10882 Handled_Statement_Sequence =>
10883 Make_Handled_Sequence_Of_Statements (Loc,
10884 Statements =>
10885 New_List (
10886 Make_Raise_Program_Error (Loc,
10887 Reason =>
10888 PE_Access_Before_Elaboration))));
10889
10890 else
10891 Ret_Expr :=
10892 Make_Raise_Program_Error (Loc,
10893 Reason => PE_Access_Before_Elaboration);
10894
10895 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
10896 Set_Analyzed (Ret_Expr);
10897
10898 Act_Body :=
10899 Make_Subprogram_Body (Loc,
10900 Specification =>
10901 Make_Function_Specification (Loc,
10902 Defining_Unit_Name =>
10903 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10904 Parameter_Specifications =>
10905 New_Copy_List
10906 (Parameter_Specifications (Parent (Anon_Id))),
10907 Result_Definition =>
10908 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10909
10910 Declarations => Empty_List,
10911 Handled_Statement_Sequence =>
10912 Make_Handled_Sequence_Of_Statements (Loc,
10913 Statements =>
10914 New_List
10915 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10916 end if;
10917
10918 Pack_Body := Make_Package_Body (Loc,
10919 Defining_Unit_Name => New_Copy (Pack_Id),
10920 Declarations => New_List (Act_Body));
10921
10922 Insert_After (Inst_Node, Pack_Body);
10923 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10924 Analyze (Pack_Body);
10925 end if;
10926
10927 Expander_Mode_Restore;
10928 end Instantiate_Subprogram_Body;
10929
10930 ----------------------
10931 -- Instantiate_Type --
10932 ----------------------
10933
10934 function Instantiate_Type
10935 (Formal : Node_Id;
10936 Actual : Node_Id;
10937 Analyzed_Formal : Node_Id;
10938 Actual_Decls : List_Id) return List_Id
10939 is
10940 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10941 A_Gen_T : constant Entity_Id :=
10942 Defining_Identifier (Analyzed_Formal);
10943 Ancestor : Entity_Id := Empty;
10944 Def : constant Node_Id := Formal_Type_Definition (Formal);
10945 Act_T : Entity_Id;
10946 Decl_Node : Node_Id;
10947 Decl_Nodes : List_Id;
10948 Loc : Source_Ptr;
10949 Subt : Entity_Id;
10950
10951 procedure Diagnose_Predicated_Actual;
10952 -- There are a number of constructs in which a discrete type with
10953 -- predicates is illegal, e.g. as an index in an array type declaration.
10954 -- If a generic type is used is such a construct in a generic package
10955 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
10956 -- of the generic contract that the actual cannot have predicates.
10957
10958 procedure Validate_Array_Type_Instance;
10959 procedure Validate_Access_Subprogram_Instance;
10960 procedure Validate_Access_Type_Instance;
10961 procedure Validate_Derived_Type_Instance;
10962 procedure Validate_Derived_Interface_Type_Instance;
10963 procedure Validate_Discriminated_Formal_Type;
10964 procedure Validate_Interface_Type_Instance;
10965 procedure Validate_Private_Type_Instance;
10966 procedure Validate_Incomplete_Type_Instance;
10967 -- These procedures perform validation tests for the named case.
10968 -- Validate_Discriminated_Formal_Type is shared by formal private
10969 -- types and Ada 2012 formal incomplete types.
10970
10971 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
10972 -- Check that base types are the same and that the subtypes match
10973 -- statically. Used in several of the above.
10974
10975 ---------------------------------
10976 -- Diagnose_Predicated_Actual --
10977 ---------------------------------
10978
10979 procedure Diagnose_Predicated_Actual is
10980 begin
10981 if No_Predicate_On_Actual (A_Gen_T)
10982 and then Has_Predicates (Act_T)
10983 then
10984 Error_Msg_NE
10985 ("actual for& cannot be a type with predicate",
10986 Instantiation_Node, A_Gen_T);
10987
10988 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
10989 and then Has_Predicates (Act_T)
10990 and then not Has_Static_Predicate_Aspect (Act_T)
10991 then
10992 Error_Msg_NE
10993 ("actual for& cannot be a type with a dynamic predicate",
10994 Instantiation_Node, A_Gen_T);
10995 end if;
10996 end Diagnose_Predicated_Actual;
10997
10998 --------------------
10999 -- Subtypes_Match --
11000 --------------------
11001
11002 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11003 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11004
11005 begin
11006 -- Some detailed comments would be useful here ???
11007
11008 return ((Base_Type (T) = Act_T
11009 or else Base_Type (T) = Base_Type (Act_T))
11010 and then Subtypes_Statically_Match (T, Act_T))
11011
11012 or else (Is_Class_Wide_Type (Gen_T)
11013 and then Is_Class_Wide_Type (Act_T)
11014 and then Subtypes_Match
11015 (Get_Instance_Of (Root_Type (Gen_T)),
11016 Root_Type (Act_T)))
11017
11018 or else
11019 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11020 E_Anonymous_Access_Type)
11021 and then Ekind (Act_T) = Ekind (Gen_T)
11022 and then Subtypes_Statically_Match
11023 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11024 end Subtypes_Match;
11025
11026 -----------------------------------------
11027 -- Validate_Access_Subprogram_Instance --
11028 -----------------------------------------
11029
11030 procedure Validate_Access_Subprogram_Instance is
11031 begin
11032 if not Is_Access_Type (Act_T)
11033 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11034 then
11035 Error_Msg_NE
11036 ("expect access type in instantiation of &", Actual, Gen_T);
11037 Abandon_Instantiation (Actual);
11038 end if;
11039
11040 -- According to AI05-288, actuals for access_to_subprograms must be
11041 -- subtype conformant with the generic formal. Previous to AI05-288
11042 -- only mode conformance was required.
11043
11044 -- This is a binding interpretation that applies to previous versions
11045 -- of the language, no need to maintain previous weaker checks.
11046
11047 Check_Subtype_Conformant
11048 (Designated_Type (Act_T),
11049 Designated_Type (A_Gen_T),
11050 Actual,
11051 Get_Inst => True);
11052
11053 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11054 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11055 Error_Msg_NE
11056 ("protected access type not allowed for formal &",
11057 Actual, Gen_T);
11058 end if;
11059
11060 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11061 Error_Msg_NE
11062 ("expect protected access type for formal &",
11063 Actual, Gen_T);
11064 end if;
11065 end Validate_Access_Subprogram_Instance;
11066
11067 -----------------------------------
11068 -- Validate_Access_Type_Instance --
11069 -----------------------------------
11070
11071 procedure Validate_Access_Type_Instance is
11072 Desig_Type : constant Entity_Id :=
11073 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11074 Desig_Act : Entity_Id;
11075
11076 begin
11077 if not Is_Access_Type (Act_T) then
11078 Error_Msg_NE
11079 ("expect access type in instantiation of &", Actual, Gen_T);
11080 Abandon_Instantiation (Actual);
11081 end if;
11082
11083 if Is_Access_Constant (A_Gen_T) then
11084 if not Is_Access_Constant (Act_T) then
11085 Error_Msg_N
11086 ("actual type must be access-to-constant type", Actual);
11087 Abandon_Instantiation (Actual);
11088 end if;
11089 else
11090 if Is_Access_Constant (Act_T) then
11091 Error_Msg_N
11092 ("actual type must be access-to-variable type", Actual);
11093 Abandon_Instantiation (Actual);
11094
11095 elsif Ekind (A_Gen_T) = E_General_Access_Type
11096 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11097 then
11098 Error_Msg_N -- CODEFIX
11099 ("actual must be general access type!", Actual);
11100 Error_Msg_NE -- CODEFIX
11101 ("add ALL to }!", Actual, Act_T);
11102 Abandon_Instantiation (Actual);
11103 end if;
11104 end if;
11105
11106 -- The designated subtypes, that is to say the subtypes introduced
11107 -- by an access type declaration (and not by a subtype declaration)
11108 -- must match.
11109
11110 Desig_Act := Designated_Type (Base_Type (Act_T));
11111
11112 -- The designated type may have been introduced through a limited_
11113 -- with clause, in which case retrieve the non-limited view. This
11114 -- applies to incomplete types as well as to class-wide types.
11115
11116 if From_Limited_With (Desig_Act) then
11117 Desig_Act := Available_View (Desig_Act);
11118 end if;
11119
11120 if not Subtypes_Match (Desig_Type, Desig_Act) then
11121 Error_Msg_NE
11122 ("designated type of actual does not match that of formal &",
11123 Actual, Gen_T);
11124
11125 if not Predicates_Match (Desig_Type, Desig_Act) then
11126 Error_Msg_N ("\predicates do not match", Actual);
11127 end if;
11128
11129 Abandon_Instantiation (Actual);
11130
11131 elsif Is_Access_Type (Designated_Type (Act_T))
11132 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11133 /=
11134 Is_Constrained (Designated_Type (Desig_Type))
11135 then
11136 Error_Msg_NE
11137 ("designated type of actual does not match that of formal &",
11138 Actual, Gen_T);
11139
11140 if not Predicates_Match (Desig_Type, Desig_Act) then
11141 Error_Msg_N ("\predicates do not match", Actual);
11142 end if;
11143
11144 Abandon_Instantiation (Actual);
11145 end if;
11146
11147 -- Ada 2005: null-exclusion indicators of the two types must agree
11148
11149 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11150 Error_Msg_NE
11151 ("non null exclusion of actual and formal & do not match",
11152 Actual, Gen_T);
11153 end if;
11154 end Validate_Access_Type_Instance;
11155
11156 ----------------------------------
11157 -- Validate_Array_Type_Instance --
11158 ----------------------------------
11159
11160 procedure Validate_Array_Type_Instance is
11161 I1 : Node_Id;
11162 I2 : Node_Id;
11163 T2 : Entity_Id;
11164
11165 function Formal_Dimensions return Int;
11166 -- Count number of dimensions in array type formal
11167
11168 -----------------------
11169 -- Formal_Dimensions --
11170 -----------------------
11171
11172 function Formal_Dimensions return Int is
11173 Num : Int := 0;
11174 Index : Node_Id;
11175
11176 begin
11177 if Nkind (Def) = N_Constrained_Array_Definition then
11178 Index := First (Discrete_Subtype_Definitions (Def));
11179 else
11180 Index := First (Subtype_Marks (Def));
11181 end if;
11182
11183 while Present (Index) loop
11184 Num := Num + 1;
11185 Next_Index (Index);
11186 end loop;
11187
11188 return Num;
11189 end Formal_Dimensions;
11190
11191 -- Start of processing for Validate_Array_Type_Instance
11192
11193 begin
11194 if not Is_Array_Type (Act_T) then
11195 Error_Msg_NE
11196 ("expect array type in instantiation of &", Actual, Gen_T);
11197 Abandon_Instantiation (Actual);
11198
11199 elsif Nkind (Def) = N_Constrained_Array_Definition then
11200 if not (Is_Constrained (Act_T)) then
11201 Error_Msg_NE
11202 ("expect constrained array in instantiation of &",
11203 Actual, Gen_T);
11204 Abandon_Instantiation (Actual);
11205 end if;
11206
11207 else
11208 if Is_Constrained (Act_T) then
11209 Error_Msg_NE
11210 ("expect unconstrained array in instantiation of &",
11211 Actual, Gen_T);
11212 Abandon_Instantiation (Actual);
11213 end if;
11214 end if;
11215
11216 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11217 Error_Msg_NE
11218 ("dimensions of actual do not match formal &", Actual, Gen_T);
11219 Abandon_Instantiation (Actual);
11220 end if;
11221
11222 I1 := First_Index (A_Gen_T);
11223 I2 := First_Index (Act_T);
11224 for J in 1 .. Formal_Dimensions loop
11225
11226 -- If the indexes of the actual were given by a subtype_mark,
11227 -- the index was transformed into a range attribute. Retrieve
11228 -- the original type mark for checking.
11229
11230 if Is_Entity_Name (Original_Node (I2)) then
11231 T2 := Entity (Original_Node (I2));
11232 else
11233 T2 := Etype (I2);
11234 end if;
11235
11236 if not Subtypes_Match
11237 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11238 then
11239 Error_Msg_NE
11240 ("index types of actual do not match those of formal &",
11241 Actual, Gen_T);
11242 Abandon_Instantiation (Actual);
11243 end if;
11244
11245 Next_Index (I1);
11246 Next_Index (I2);
11247 end loop;
11248
11249 -- Check matching subtypes. Note that there are complex visibility
11250 -- issues when the generic is a child unit and some aspect of the
11251 -- generic type is declared in a parent unit of the generic. We do
11252 -- the test to handle this special case only after a direct check
11253 -- for static matching has failed. The case where both the component
11254 -- type and the array type are separate formals, and the component
11255 -- type is a private view may also require special checking in
11256 -- Subtypes_Match.
11257
11258 if Subtypes_Match
11259 (Component_Type (A_Gen_T), Component_Type (Act_T))
11260 or else Subtypes_Match
11261 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11262 Component_Type (Act_T))
11263 then
11264 null;
11265 else
11266 Error_Msg_NE
11267 ("component subtype of actual does not match that of formal &",
11268 Actual, Gen_T);
11269 Abandon_Instantiation (Actual);
11270 end if;
11271
11272 if Has_Aliased_Components (A_Gen_T)
11273 and then not Has_Aliased_Components (Act_T)
11274 then
11275 Error_Msg_NE
11276 ("actual must have aliased components to match formal type &",
11277 Actual, Gen_T);
11278 end if;
11279 end Validate_Array_Type_Instance;
11280
11281 -----------------------------------------------
11282 -- Validate_Derived_Interface_Type_Instance --
11283 -----------------------------------------------
11284
11285 procedure Validate_Derived_Interface_Type_Instance is
11286 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11287 Elmt : Elmt_Id;
11288
11289 begin
11290 -- First apply interface instance checks
11291
11292 Validate_Interface_Type_Instance;
11293
11294 -- Verify that immediate parent interface is an ancestor of
11295 -- the actual.
11296
11297 if Present (Par)
11298 and then not Interface_Present_In_Ancestor (Act_T, Par)
11299 then
11300 Error_Msg_NE
11301 ("interface actual must include progenitor&", Actual, Par);
11302 end if;
11303
11304 -- Now verify that the actual includes all other ancestors of
11305 -- the formal.
11306
11307 Elmt := First_Elmt (Interfaces (A_Gen_T));
11308 while Present (Elmt) loop
11309 if not Interface_Present_In_Ancestor
11310 (Act_T, Get_Instance_Of (Node (Elmt)))
11311 then
11312 Error_Msg_NE
11313 ("interface actual must include progenitor&",
11314 Actual, Node (Elmt));
11315 end if;
11316
11317 Next_Elmt (Elmt);
11318 end loop;
11319 end Validate_Derived_Interface_Type_Instance;
11320
11321 ------------------------------------
11322 -- Validate_Derived_Type_Instance --
11323 ------------------------------------
11324
11325 procedure Validate_Derived_Type_Instance is
11326 Actual_Discr : Entity_Id;
11327 Ancestor_Discr : Entity_Id;
11328
11329 begin
11330 -- If the parent type in the generic declaration is itself a previous
11331 -- formal type, then it is local to the generic and absent from the
11332 -- analyzed generic definition. In that case the ancestor is the
11333 -- instance of the formal (which must have been instantiated
11334 -- previously), unless the ancestor is itself a formal derived type.
11335 -- In this latter case (which is the subject of Corrigendum 8652/0038
11336 -- (AI-202) the ancestor of the formals is the ancestor of its
11337 -- parent. Otherwise, the analyzed generic carries the parent type.
11338 -- If the parent type is defined in a previous formal package, then
11339 -- the scope of that formal package is that of the generic type
11340 -- itself, and it has already been mapped into the corresponding type
11341 -- in the actual package.
11342
11343 -- Common case: parent type defined outside of the generic
11344
11345 if Is_Entity_Name (Subtype_Mark (Def))
11346 and then Present (Entity (Subtype_Mark (Def)))
11347 then
11348 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11349
11350 -- Check whether parent is defined in a previous formal package
11351
11352 elsif
11353 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11354 then
11355 Ancestor :=
11356 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11357
11358 -- The type may be a local derivation, or a type extension of a
11359 -- previous formal, or of a formal of a parent package.
11360
11361 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11362 or else
11363 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11364 then
11365 -- Check whether the parent is another derived formal type in the
11366 -- same generic unit.
11367
11368 if Etype (A_Gen_T) /= A_Gen_T
11369 and then Is_Generic_Type (Etype (A_Gen_T))
11370 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11371 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11372 then
11373 -- Locate ancestor of parent from the subtype declaration
11374 -- created for the actual.
11375
11376 declare
11377 Decl : Node_Id;
11378
11379 begin
11380 Decl := First (Actual_Decls);
11381 while Present (Decl) loop
11382 if Nkind (Decl) = N_Subtype_Declaration
11383 and then Chars (Defining_Identifier (Decl)) =
11384 Chars (Etype (A_Gen_T))
11385 then
11386 Ancestor := Generic_Parent_Type (Decl);
11387 exit;
11388 else
11389 Next (Decl);
11390 end if;
11391 end loop;
11392 end;
11393
11394 pragma Assert (Present (Ancestor));
11395
11396 -- The ancestor itself may be a previous formal that has been
11397 -- instantiated.
11398
11399 Ancestor := Get_Instance_Of (Ancestor);
11400
11401 else
11402 Ancestor :=
11403 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11404 end if;
11405
11406 -- An unusual case: the actual is a type declared in a parent unit,
11407 -- but is not a formal type so there is no instance_of for it.
11408 -- Retrieve it by analyzing the record extension.
11409
11410 elsif Is_Child_Unit (Scope (A_Gen_T))
11411 and then In_Open_Scopes (Scope (Act_T))
11412 and then Is_Generic_Instance (Scope (Act_T))
11413 then
11414 Analyze (Subtype_Mark (Def));
11415 Ancestor := Entity (Subtype_Mark (Def));
11416
11417 else
11418 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11419 end if;
11420
11421 -- If the formal derived type has pragma Preelaborable_Initialization
11422 -- then the actual type must have preelaborable initialization.
11423
11424 if Known_To_Have_Preelab_Init (A_Gen_T)
11425 and then not Has_Preelaborable_Initialization (Act_T)
11426 then
11427 Error_Msg_NE
11428 ("actual for & must have preelaborable initialization",
11429 Actual, Gen_T);
11430 end if;
11431
11432 -- Ada 2005 (AI-251)
11433
11434 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11435 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11436 Error_Msg_NE
11437 ("(Ada 2005) expected type implementing & in instantiation",
11438 Actual, Ancestor);
11439 end if;
11440
11441 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11442 Error_Msg_NE
11443 ("expect type derived from & in instantiation",
11444 Actual, First_Subtype (Ancestor));
11445 Abandon_Instantiation (Actual);
11446 end if;
11447
11448 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11449 -- that the formal type declaration has been rewritten as a private
11450 -- extension.
11451
11452 if Ada_Version >= Ada_2005
11453 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11454 and then Synchronized_Present (Parent (A_Gen_T))
11455 then
11456 -- The actual must be a synchronized tagged type
11457
11458 if not Is_Tagged_Type (Act_T) then
11459 Error_Msg_N
11460 ("actual of synchronized type must be tagged", Actual);
11461 Abandon_Instantiation (Actual);
11462
11463 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11464 and then Nkind (Type_Definition (Parent (Act_T))) =
11465 N_Derived_Type_Definition
11466 and then not Synchronized_Present (Type_Definition
11467 (Parent (Act_T)))
11468 then
11469 Error_Msg_N
11470 ("actual of synchronized type must be synchronized", Actual);
11471 Abandon_Instantiation (Actual);
11472 end if;
11473 end if;
11474
11475 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11476 -- removes the second instance of the phrase "or allow pass by copy".
11477
11478 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11479 Error_Msg_N
11480 ("cannot have atomic actual type for non-atomic formal type",
11481 Actual);
11482
11483 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11484 Error_Msg_N
11485 ("cannot have volatile actual type for non-volatile formal type",
11486 Actual);
11487 end if;
11488
11489 -- It should not be necessary to check for unknown discriminants on
11490 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11491 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11492 -- needs fixing. ???
11493
11494 if not Is_Indefinite_Subtype (A_Gen_T)
11495 and then not Unknown_Discriminants_Present (Formal)
11496 and then Is_Indefinite_Subtype (Act_T)
11497 then
11498 Error_Msg_N
11499 ("actual subtype must be constrained", Actual);
11500 Abandon_Instantiation (Actual);
11501 end if;
11502
11503 if not Unknown_Discriminants_Present (Formal) then
11504 if Is_Constrained (Ancestor) then
11505 if not Is_Constrained (Act_T) then
11506 Error_Msg_N
11507 ("actual subtype must be constrained", Actual);
11508 Abandon_Instantiation (Actual);
11509 end if;
11510
11511 -- Ancestor is unconstrained, Check if generic formal and actual
11512 -- agree on constrainedness. The check only applies to array types
11513 -- and discriminated types.
11514
11515 elsif Is_Constrained (Act_T) then
11516 if Ekind (Ancestor) = E_Access_Type
11517 or else
11518 (not Is_Constrained (A_Gen_T)
11519 and then Is_Composite_Type (A_Gen_T))
11520 then
11521 Error_Msg_N
11522 ("actual subtype must be unconstrained", Actual);
11523 Abandon_Instantiation (Actual);
11524 end if;
11525
11526 -- A class-wide type is only allowed if the formal has unknown
11527 -- discriminants.
11528
11529 elsif Is_Class_Wide_Type (Act_T)
11530 and then not Has_Unknown_Discriminants (Ancestor)
11531 then
11532 Error_Msg_NE
11533 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11534 Abandon_Instantiation (Actual);
11535
11536 -- Otherwise, the formal and actual must have the same number
11537 -- of discriminants and each discriminant of the actual must
11538 -- correspond to a discriminant of the formal.
11539
11540 elsif Has_Discriminants (Act_T)
11541 and then not Has_Unknown_Discriminants (Act_T)
11542 and then Has_Discriminants (Ancestor)
11543 then
11544 Actual_Discr := First_Discriminant (Act_T);
11545 Ancestor_Discr := First_Discriminant (Ancestor);
11546 while Present (Actual_Discr)
11547 and then Present (Ancestor_Discr)
11548 loop
11549 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11550 No (Corresponding_Discriminant (Actual_Discr))
11551 then
11552 Error_Msg_NE
11553 ("discriminant & does not correspond " &
11554 "to ancestor discriminant", Actual, Actual_Discr);
11555 Abandon_Instantiation (Actual);
11556 end if;
11557
11558 Next_Discriminant (Actual_Discr);
11559 Next_Discriminant (Ancestor_Discr);
11560 end loop;
11561
11562 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11563 Error_Msg_NE
11564 ("actual for & must have same number of discriminants",
11565 Actual, Gen_T);
11566 Abandon_Instantiation (Actual);
11567 end if;
11568
11569 -- This case should be caught by the earlier check for
11570 -- constrainedness, but the check here is added for completeness.
11571
11572 elsif Has_Discriminants (Act_T)
11573 and then not Has_Unknown_Discriminants (Act_T)
11574 then
11575 Error_Msg_NE
11576 ("actual for & must not have discriminants", Actual, Gen_T);
11577 Abandon_Instantiation (Actual);
11578
11579 elsif Has_Discriminants (Ancestor) then
11580 Error_Msg_NE
11581 ("actual for & must have known discriminants", Actual, Gen_T);
11582 Abandon_Instantiation (Actual);
11583 end if;
11584
11585 if not Subtypes_Statically_Compatible
11586 (Act_T, Ancestor, Formal_Derived_Matching => True)
11587 then
11588 Error_Msg_N
11589 ("constraint on actual is incompatible with formal", Actual);
11590 Abandon_Instantiation (Actual);
11591 end if;
11592 end if;
11593
11594 -- If the formal and actual types are abstract, check that there
11595 -- are no abstract primitives of the actual type that correspond to
11596 -- nonabstract primitives of the formal type (second sentence of
11597 -- RM95-3.9.3(9)).
11598
11599 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11600 Check_Abstract_Primitives : declare
11601 Gen_Prims : constant Elist_Id :=
11602 Primitive_Operations (A_Gen_T);
11603 Gen_Elmt : Elmt_Id;
11604 Gen_Subp : Entity_Id;
11605 Anc_Subp : Entity_Id;
11606 Anc_Formal : Entity_Id;
11607 Anc_F_Type : Entity_Id;
11608
11609 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11610 Act_Elmt : Elmt_Id;
11611 Act_Subp : Entity_Id;
11612 Act_Formal : Entity_Id;
11613 Act_F_Type : Entity_Id;
11614
11615 Subprograms_Correspond : Boolean;
11616
11617 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11618 -- Returns true if T2 is derived directly or indirectly from
11619 -- T1, including derivations from interfaces. T1 and T2 are
11620 -- required to be specific tagged base types.
11621
11622 ------------------------
11623 -- Is_Tagged_Ancestor --
11624 ------------------------
11625
11626 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11627 is
11628 Intfc_Elmt : Elmt_Id;
11629
11630 begin
11631 -- The predicate is satisfied if the types are the same
11632
11633 if T1 = T2 then
11634 return True;
11635
11636 -- If we've reached the top of the derivation chain then
11637 -- we know that T1 is not an ancestor of T2.
11638
11639 elsif Etype (T2) = T2 then
11640 return False;
11641
11642 -- Proceed to check T2's immediate parent
11643
11644 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11645 return True;
11646
11647 -- Finally, check to see if T1 is an ancestor of any of T2's
11648 -- progenitors.
11649
11650 else
11651 Intfc_Elmt := First_Elmt (Interfaces (T2));
11652 while Present (Intfc_Elmt) loop
11653 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11654 return True;
11655 end if;
11656
11657 Next_Elmt (Intfc_Elmt);
11658 end loop;
11659 end if;
11660
11661 return False;
11662 end Is_Tagged_Ancestor;
11663
11664 -- Start of processing for Check_Abstract_Primitives
11665
11666 begin
11667 -- Loop over all of the formal derived type's primitives
11668
11669 Gen_Elmt := First_Elmt (Gen_Prims);
11670 while Present (Gen_Elmt) loop
11671 Gen_Subp := Node (Gen_Elmt);
11672
11673 -- If the primitive of the formal is not abstract, then
11674 -- determine whether there is a corresponding primitive of
11675 -- the actual type that's abstract.
11676
11677 if not Is_Abstract_Subprogram (Gen_Subp) then
11678 Act_Elmt := First_Elmt (Act_Prims);
11679 while Present (Act_Elmt) loop
11680 Act_Subp := Node (Act_Elmt);
11681
11682 -- If we find an abstract primitive of the actual,
11683 -- then we need to test whether it corresponds to the
11684 -- subprogram from which the generic formal primitive
11685 -- is inherited.
11686
11687 if Is_Abstract_Subprogram (Act_Subp) then
11688 Anc_Subp := Alias (Gen_Subp);
11689
11690 -- Test whether we have a corresponding primitive
11691 -- by comparing names, kinds, formal types, and
11692 -- result types.
11693
11694 if Chars (Anc_Subp) = Chars (Act_Subp)
11695 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11696 then
11697 Anc_Formal := First_Formal (Anc_Subp);
11698 Act_Formal := First_Formal (Act_Subp);
11699 while Present (Anc_Formal)
11700 and then Present (Act_Formal)
11701 loop
11702 Anc_F_Type := Etype (Anc_Formal);
11703 Act_F_Type := Etype (Act_Formal);
11704
11705 if Ekind (Anc_F_Type)
11706 = E_Anonymous_Access_Type
11707 then
11708 Anc_F_Type := Designated_Type (Anc_F_Type);
11709
11710 if Ekind (Act_F_Type)
11711 = E_Anonymous_Access_Type
11712 then
11713 Act_F_Type :=
11714 Designated_Type (Act_F_Type);
11715 else
11716 exit;
11717 end if;
11718
11719 elsif
11720 Ekind (Act_F_Type) = E_Anonymous_Access_Type
11721 then
11722 exit;
11723 end if;
11724
11725 Anc_F_Type := Base_Type (Anc_F_Type);
11726 Act_F_Type := Base_Type (Act_F_Type);
11727
11728 -- If the formal is controlling, then the
11729 -- the type of the actual primitive's formal
11730 -- must be derived directly or indirectly
11731 -- from the type of the ancestor primitive's
11732 -- formal.
11733
11734 if Is_Controlling_Formal (Anc_Formal) then
11735 if not Is_Tagged_Ancestor
11736 (Anc_F_Type, Act_F_Type)
11737 then
11738 exit;
11739 end if;
11740
11741 -- Otherwise the types of the formals must
11742 -- be the same.
11743
11744 elsif Anc_F_Type /= Act_F_Type then
11745 exit;
11746 end if;
11747
11748 Next_Entity (Anc_Formal);
11749 Next_Entity (Act_Formal);
11750 end loop;
11751
11752 -- If we traversed through all of the formals
11753 -- then so far the subprograms correspond, so
11754 -- now check that any result types correspond.
11755
11756 if No (Anc_Formal) and then No (Act_Formal) then
11757 Subprograms_Correspond := True;
11758
11759 if Ekind (Act_Subp) = E_Function then
11760 Anc_F_Type := Etype (Anc_Subp);
11761 Act_F_Type := Etype (Act_Subp);
11762
11763 if Ekind (Anc_F_Type)
11764 = E_Anonymous_Access_Type
11765 then
11766 Anc_F_Type :=
11767 Designated_Type (Anc_F_Type);
11768
11769 if Ekind (Act_F_Type)
11770 = E_Anonymous_Access_Type
11771 then
11772 Act_F_Type :=
11773 Designated_Type (Act_F_Type);
11774 else
11775 Subprograms_Correspond := False;
11776 end if;
11777
11778 elsif
11779 Ekind (Act_F_Type)
11780 = E_Anonymous_Access_Type
11781 then
11782 Subprograms_Correspond := False;
11783 end if;
11784
11785 Anc_F_Type := Base_Type (Anc_F_Type);
11786 Act_F_Type := Base_Type (Act_F_Type);
11787
11788 -- Now either the result types must be
11789 -- the same or, if the result type is
11790 -- controlling, the result type of the
11791 -- actual primitive must descend from the
11792 -- result type of the ancestor primitive.
11793
11794 if Subprograms_Correspond
11795 and then Anc_F_Type /= Act_F_Type
11796 and then
11797 Has_Controlling_Result (Anc_Subp)
11798 and then
11799 not Is_Tagged_Ancestor
11800 (Anc_F_Type, Act_F_Type)
11801 then
11802 Subprograms_Correspond := False;
11803 end if;
11804 end if;
11805
11806 -- Found a matching subprogram belonging to
11807 -- formal ancestor type, so actual subprogram
11808 -- corresponds and this violates 3.9.3(9).
11809
11810 if Subprograms_Correspond then
11811 Error_Msg_NE
11812 ("abstract subprogram & overrides " &
11813 "nonabstract subprogram of ancestor",
11814 Actual,
11815 Act_Subp);
11816 end if;
11817 end if;
11818 end if;
11819 end if;
11820
11821 Next_Elmt (Act_Elmt);
11822 end loop;
11823 end if;
11824
11825 Next_Elmt (Gen_Elmt);
11826 end loop;
11827 end Check_Abstract_Primitives;
11828 end if;
11829
11830 -- Verify that limitedness matches. If parent is a limited
11831 -- interface then the generic formal is not unless declared
11832 -- explicitly so. If not declared limited, the actual cannot be
11833 -- limited (see AI05-0087).
11834
11835 -- Even though this AI is a binding interpretation, we enable the
11836 -- check only in Ada 2012 mode, because this improper construct
11837 -- shows up in user code and in existing B-tests.
11838
11839 if Is_Limited_Type (Act_T)
11840 and then not Is_Limited_Type (A_Gen_T)
11841 and then Ada_Version >= Ada_2012
11842 then
11843 if In_Instance then
11844 null;
11845 else
11846 Error_Msg_NE
11847 ("actual for non-limited & cannot be a limited type", Actual,
11848 Gen_T);
11849 Explain_Limited_Type (Act_T, Actual);
11850 Abandon_Instantiation (Actual);
11851 end if;
11852 end if;
11853 end Validate_Derived_Type_Instance;
11854
11855 ----------------------------------------
11856 -- Validate_Discriminated_Formal_Type --
11857 ----------------------------------------
11858
11859 procedure Validate_Discriminated_Formal_Type is
11860 Formal_Discr : Entity_Id;
11861 Actual_Discr : Entity_Id;
11862 Formal_Subt : Entity_Id;
11863
11864 begin
11865 if Has_Discriminants (A_Gen_T) then
11866 if not Has_Discriminants (Act_T) then
11867 Error_Msg_NE
11868 ("actual for & must have discriminants", Actual, Gen_T);
11869 Abandon_Instantiation (Actual);
11870
11871 elsif Is_Constrained (Act_T) then
11872 Error_Msg_NE
11873 ("actual for & must be unconstrained", Actual, Gen_T);
11874 Abandon_Instantiation (Actual);
11875
11876 else
11877 Formal_Discr := First_Discriminant (A_Gen_T);
11878 Actual_Discr := First_Discriminant (Act_T);
11879 while Formal_Discr /= Empty loop
11880 if Actual_Discr = Empty then
11881 Error_Msg_NE
11882 ("discriminants on actual do not match formal",
11883 Actual, Gen_T);
11884 Abandon_Instantiation (Actual);
11885 end if;
11886
11887 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
11888
11889 -- Access discriminants match if designated types do
11890
11891 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
11892 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
11893 E_Anonymous_Access_Type
11894 and then
11895 Get_Instance_Of
11896 (Designated_Type (Base_Type (Formal_Subt))) =
11897 Designated_Type (Base_Type (Etype (Actual_Discr)))
11898 then
11899 null;
11900
11901 elsif Base_Type (Formal_Subt) /=
11902 Base_Type (Etype (Actual_Discr))
11903 then
11904 Error_Msg_NE
11905 ("types of actual discriminants must match formal",
11906 Actual, Gen_T);
11907 Abandon_Instantiation (Actual);
11908
11909 elsif not Subtypes_Statically_Match
11910 (Formal_Subt, Etype (Actual_Discr))
11911 and then Ada_Version >= Ada_95
11912 then
11913 Error_Msg_NE
11914 ("subtypes of actual discriminants must match formal",
11915 Actual, Gen_T);
11916 Abandon_Instantiation (Actual);
11917 end if;
11918
11919 Next_Discriminant (Formal_Discr);
11920 Next_Discriminant (Actual_Discr);
11921 end loop;
11922
11923 if Actual_Discr /= Empty then
11924 Error_Msg_NE
11925 ("discriminants on actual do not match formal",
11926 Actual, Gen_T);
11927 Abandon_Instantiation (Actual);
11928 end if;
11929 end if;
11930 end if;
11931 end Validate_Discriminated_Formal_Type;
11932
11933 ---------------------------------------
11934 -- Validate_Incomplete_Type_Instance --
11935 ---------------------------------------
11936
11937 procedure Validate_Incomplete_Type_Instance is
11938 begin
11939 if not Is_Tagged_Type (Act_T)
11940 and then Is_Tagged_Type (A_Gen_T)
11941 then
11942 Error_Msg_NE
11943 ("actual for & must be a tagged type", Actual, Gen_T);
11944 end if;
11945
11946 Validate_Discriminated_Formal_Type;
11947 end Validate_Incomplete_Type_Instance;
11948
11949 --------------------------------------
11950 -- Validate_Interface_Type_Instance --
11951 --------------------------------------
11952
11953 procedure Validate_Interface_Type_Instance is
11954 begin
11955 if not Is_Interface (Act_T) then
11956 Error_Msg_NE
11957 ("actual for formal interface type must be an interface",
11958 Actual, Gen_T);
11959
11960 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
11961 or else
11962 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
11963 or else
11964 Is_Protected_Interface (A_Gen_T) /=
11965 Is_Protected_Interface (Act_T)
11966 or else
11967 Is_Synchronized_Interface (A_Gen_T) /=
11968 Is_Synchronized_Interface (Act_T)
11969 then
11970 Error_Msg_NE
11971 ("actual for interface& does not match (RM 12.5.5(4))",
11972 Actual, Gen_T);
11973 end if;
11974 end Validate_Interface_Type_Instance;
11975
11976 ------------------------------------
11977 -- Validate_Private_Type_Instance --
11978 ------------------------------------
11979
11980 procedure Validate_Private_Type_Instance is
11981 begin
11982 if Is_Limited_Type (Act_T)
11983 and then not Is_Limited_Type (A_Gen_T)
11984 then
11985 if In_Instance then
11986 null;
11987 else
11988 Error_Msg_NE
11989 ("actual for non-limited & cannot be a limited type", Actual,
11990 Gen_T);
11991 Explain_Limited_Type (Act_T, Actual);
11992 Abandon_Instantiation (Actual);
11993 end if;
11994
11995 elsif Known_To_Have_Preelab_Init (A_Gen_T)
11996 and then not Has_Preelaborable_Initialization (Act_T)
11997 then
11998 Error_Msg_NE
11999 ("actual for & must have preelaborable initialization", Actual,
12000 Gen_T);
12001
12002 elsif Is_Indefinite_Subtype (Act_T)
12003 and then not Is_Indefinite_Subtype (A_Gen_T)
12004 and then Ada_Version >= Ada_95
12005 then
12006 Error_Msg_NE
12007 ("actual for & must be a definite subtype", Actual, Gen_T);
12008
12009 elsif not Is_Tagged_Type (Act_T)
12010 and then Is_Tagged_Type (A_Gen_T)
12011 then
12012 Error_Msg_NE
12013 ("actual for & must be a tagged type", Actual, Gen_T);
12014 end if;
12015
12016 Validate_Discriminated_Formal_Type;
12017 Ancestor := Gen_T;
12018 end Validate_Private_Type_Instance;
12019
12020 -- Start of processing for Instantiate_Type
12021
12022 begin
12023 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12024 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12025 return New_List (Error);
12026
12027 elsif not Is_Entity_Name (Actual)
12028 or else not Is_Type (Entity (Actual))
12029 then
12030 Error_Msg_NE
12031 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12032 Abandon_Instantiation (Actual);
12033
12034 else
12035 Act_T := Entity (Actual);
12036
12037 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12038 -- as a generic actual parameter if the corresponding formal type
12039 -- does not have a known_discriminant_part, or is a formal derived
12040 -- type that is an Unchecked_Union type.
12041
12042 if Is_Unchecked_Union (Base_Type (Act_T)) then
12043 if not Has_Discriminants (A_Gen_T)
12044 or else
12045 (Is_Derived_Type (A_Gen_T)
12046 and then
12047 Is_Unchecked_Union (A_Gen_T))
12048 then
12049 null;
12050 else
12051 Error_Msg_N ("unchecked union cannot be the actual for a" &
12052 " discriminated formal type", Act_T);
12053
12054 end if;
12055 end if;
12056
12057 -- Deal with fixed/floating restrictions
12058
12059 if Is_Floating_Point_Type (Act_T) then
12060 Check_Restriction (No_Floating_Point, Actual);
12061 elsif Is_Fixed_Point_Type (Act_T) then
12062 Check_Restriction (No_Fixed_Point, Actual);
12063 end if;
12064
12065 -- Deal with error of using incomplete type as generic actual.
12066 -- This includes limited views of a type, even if the non-limited
12067 -- view may be available.
12068
12069 if Ekind (Act_T) = E_Incomplete_Type
12070 or else (Is_Class_Wide_Type (Act_T)
12071 and then
12072 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12073 then
12074 -- If the formal is an incomplete type, the actual can be
12075 -- incomplete as well.
12076
12077 if Ekind (A_Gen_T) = E_Incomplete_Type then
12078 null;
12079
12080 elsif Is_Class_Wide_Type (Act_T)
12081 or else No (Full_View (Act_T))
12082 then
12083 Error_Msg_N ("premature use of incomplete type", Actual);
12084 Abandon_Instantiation (Actual);
12085 else
12086 Act_T := Full_View (Act_T);
12087 Set_Entity (Actual, Act_T);
12088
12089 if Has_Private_Component (Act_T) then
12090 Error_Msg_N
12091 ("premature use of type with private component", Actual);
12092 end if;
12093 end if;
12094
12095 -- Deal with error of premature use of private type as generic actual
12096
12097 elsif Is_Private_Type (Act_T)
12098 and then Is_Private_Type (Base_Type (Act_T))
12099 and then not Is_Generic_Type (Act_T)
12100 and then not Is_Derived_Type (Act_T)
12101 and then No (Full_View (Root_Type (Act_T)))
12102 then
12103 -- If the formal is an incomplete type, the actual can be
12104 -- private or incomplete as well.
12105
12106 if Ekind (A_Gen_T) = E_Incomplete_Type then
12107 null;
12108 else
12109 Error_Msg_N ("premature use of private type", Actual);
12110 end if;
12111
12112 elsif Has_Private_Component (Act_T) then
12113 Error_Msg_N
12114 ("premature use of type with private component", Actual);
12115 end if;
12116
12117 Set_Instance_Of (A_Gen_T, Act_T);
12118
12119 -- If the type is generic, the class-wide type may also be used
12120
12121 if Is_Tagged_Type (A_Gen_T)
12122 and then Is_Tagged_Type (Act_T)
12123 and then not Is_Class_Wide_Type (A_Gen_T)
12124 then
12125 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12126 Class_Wide_Type (Act_T));
12127 end if;
12128
12129 if not Is_Abstract_Type (A_Gen_T)
12130 and then Is_Abstract_Type (Act_T)
12131 then
12132 Error_Msg_N
12133 ("actual of non-abstract formal cannot be abstract", Actual);
12134 end if;
12135
12136 -- A generic scalar type is a first subtype for which we generate
12137 -- an anonymous base type. Indicate that the instance of this base
12138 -- is the base type of the actual.
12139
12140 if Is_Scalar_Type (A_Gen_T) then
12141 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12142 end if;
12143 end if;
12144
12145 if Error_Posted (Act_T) then
12146 null;
12147 else
12148 case Nkind (Def) is
12149 when N_Formal_Private_Type_Definition =>
12150 Validate_Private_Type_Instance;
12151
12152 when N_Formal_Incomplete_Type_Definition =>
12153 Validate_Incomplete_Type_Instance;
12154
12155 when N_Formal_Derived_Type_Definition =>
12156 Validate_Derived_Type_Instance;
12157
12158 when N_Formal_Discrete_Type_Definition =>
12159 if not Is_Discrete_Type (Act_T) then
12160 Error_Msg_NE
12161 ("expect discrete type in instantiation of&",
12162 Actual, Gen_T);
12163 Abandon_Instantiation (Actual);
12164 end if;
12165
12166 Diagnose_Predicated_Actual;
12167
12168 when N_Formal_Signed_Integer_Type_Definition =>
12169 if not Is_Signed_Integer_Type (Act_T) then
12170 Error_Msg_NE
12171 ("expect signed integer type in instantiation of&",
12172 Actual, Gen_T);
12173 Abandon_Instantiation (Actual);
12174 end if;
12175
12176 Diagnose_Predicated_Actual;
12177
12178 when N_Formal_Modular_Type_Definition =>
12179 if not Is_Modular_Integer_Type (Act_T) then
12180 Error_Msg_NE
12181 ("expect modular type in instantiation of &",
12182 Actual, Gen_T);
12183 Abandon_Instantiation (Actual);
12184 end if;
12185
12186 Diagnose_Predicated_Actual;
12187
12188 when N_Formal_Floating_Point_Definition =>
12189 if not Is_Floating_Point_Type (Act_T) then
12190 Error_Msg_NE
12191 ("expect float type in instantiation of &", Actual, Gen_T);
12192 Abandon_Instantiation (Actual);
12193 end if;
12194
12195 when N_Formal_Ordinary_Fixed_Point_Definition =>
12196 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12197 Error_Msg_NE
12198 ("expect ordinary fixed point type in instantiation of &",
12199 Actual, Gen_T);
12200 Abandon_Instantiation (Actual);
12201 end if;
12202
12203 when N_Formal_Decimal_Fixed_Point_Definition =>
12204 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12205 Error_Msg_NE
12206 ("expect decimal type in instantiation of &",
12207 Actual, Gen_T);
12208 Abandon_Instantiation (Actual);
12209 end if;
12210
12211 when N_Array_Type_Definition =>
12212 Validate_Array_Type_Instance;
12213
12214 when N_Access_To_Object_Definition =>
12215 Validate_Access_Type_Instance;
12216
12217 when N_Access_Function_Definition |
12218 N_Access_Procedure_Definition =>
12219 Validate_Access_Subprogram_Instance;
12220
12221 when N_Record_Definition =>
12222 Validate_Interface_Type_Instance;
12223
12224 when N_Derived_Type_Definition =>
12225 Validate_Derived_Interface_Type_Instance;
12226
12227 when others =>
12228 raise Program_Error;
12229
12230 end case;
12231 end if;
12232
12233 Subt := New_Copy (Gen_T);
12234
12235 -- Use adjusted sloc of subtype name as the location for other nodes in
12236 -- the subtype declaration.
12237
12238 Loc := Sloc (Subt);
12239
12240 Decl_Node :=
12241 Make_Subtype_Declaration (Loc,
12242 Defining_Identifier => Subt,
12243 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12244
12245 if Is_Private_Type (Act_T) then
12246 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12247
12248 elsif Is_Access_Type (Act_T)
12249 and then Is_Private_Type (Designated_Type (Act_T))
12250 then
12251 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12252 end if;
12253
12254 Decl_Nodes := New_List (Decl_Node);
12255
12256 -- Flag actual derived types so their elaboration produces the
12257 -- appropriate renamings for the primitive operations of the ancestor.
12258 -- Flag actual for formal private types as well, to determine whether
12259 -- operations in the private part may override inherited operations.
12260 -- If the formal has an interface list, the ancestor is not the
12261 -- parent, but the analyzed formal that includes the interface
12262 -- operations of all its progenitors.
12263
12264 -- Same treatment for formal private types, so we can check whether the
12265 -- type is tagged limited when validating derivations in the private
12266 -- part. (See AI05-096).
12267
12268 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12269 if Present (Interface_List (Def)) then
12270 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12271 else
12272 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12273 end if;
12274
12275 elsif Nkind_In (Def,
12276 N_Formal_Private_Type_Definition,
12277 N_Formal_Incomplete_Type_Definition)
12278 then
12279 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12280 end if;
12281
12282 -- If the actual is a synchronized type that implements an interface,
12283 -- the primitive operations are attached to the corresponding record,
12284 -- and we have to treat it as an additional generic actual, so that its
12285 -- primitive operations become visible in the instance. The task or
12286 -- protected type itself does not carry primitive operations.
12287
12288 if Is_Concurrent_Type (Act_T)
12289 and then Is_Tagged_Type (Act_T)
12290 and then Present (Corresponding_Record_Type (Act_T))
12291 and then Present (Ancestor)
12292 and then Is_Interface (Ancestor)
12293 then
12294 declare
12295 Corr_Rec : constant Entity_Id :=
12296 Corresponding_Record_Type (Act_T);
12297 New_Corr : Entity_Id;
12298 Corr_Decl : Node_Id;
12299
12300 begin
12301 New_Corr := Make_Temporary (Loc, 'S');
12302 Corr_Decl :=
12303 Make_Subtype_Declaration (Loc,
12304 Defining_Identifier => New_Corr,
12305 Subtype_Indication =>
12306 New_Occurrence_Of (Corr_Rec, Loc));
12307 Append_To (Decl_Nodes, Corr_Decl);
12308
12309 if Ekind (Act_T) = E_Task_Type then
12310 Set_Ekind (Subt, E_Task_Subtype);
12311 else
12312 Set_Ekind (Subt, E_Protected_Subtype);
12313 end if;
12314
12315 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12316 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12317 Set_Generic_Parent_Type (Decl_Node, Empty);
12318 end;
12319 end if;
12320
12321 return Decl_Nodes;
12322 end Instantiate_Type;
12323
12324 ---------------------
12325 -- Is_In_Main_Unit --
12326 ---------------------
12327
12328 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12329 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12330 Current_Unit : Node_Id;
12331
12332 begin
12333 if Unum = Main_Unit then
12334 return True;
12335
12336 -- If the current unit is a subunit then it is either the main unit or
12337 -- is being compiled as part of the main unit.
12338
12339 elsif Nkind (N) = N_Compilation_Unit then
12340 return Nkind (Unit (N)) = N_Subunit;
12341 end if;
12342
12343 Current_Unit := Parent (N);
12344 while Present (Current_Unit)
12345 and then Nkind (Current_Unit) /= N_Compilation_Unit
12346 loop
12347 Current_Unit := Parent (Current_Unit);
12348 end loop;
12349
12350 -- The instantiation node is in the main unit, or else the current node
12351 -- (perhaps as the result of nested instantiations) is in the main unit,
12352 -- or in the declaration of the main unit, which in this last case must
12353 -- be a body.
12354
12355 return Unum = Main_Unit
12356 or else Current_Unit = Cunit (Main_Unit)
12357 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12358 or else (Present (Library_Unit (Current_Unit))
12359 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12360 end Is_In_Main_Unit;
12361
12362 ----------------------------
12363 -- Load_Parent_Of_Generic --
12364 ----------------------------
12365
12366 procedure Load_Parent_Of_Generic
12367 (N : Node_Id;
12368 Spec : Node_Id;
12369 Body_Optional : Boolean := False)
12370 is
12371 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12372 Saved_Style_Check : constant Boolean := Style_Check;
12373 Saved_Warnings : constant Warning_Record := Save_Warnings;
12374 True_Parent : Node_Id;
12375 Inst_Node : Node_Id;
12376 OK : Boolean;
12377 Previous_Instances : constant Elist_Id := New_Elmt_List;
12378
12379 procedure Collect_Previous_Instances (Decls : List_Id);
12380 -- Collect all instantiations in the given list of declarations, that
12381 -- precede the generic that we need to load. If the bodies of these
12382 -- instantiations are available, we must analyze them, to ensure that
12383 -- the public symbols generated are the same when the unit is compiled
12384 -- to generate code, and when it is compiled in the context of a unit
12385 -- that needs a particular nested instance. This process is applied to
12386 -- both package and subprogram instances.
12387
12388 --------------------------------
12389 -- Collect_Previous_Instances --
12390 --------------------------------
12391
12392 procedure Collect_Previous_Instances (Decls : List_Id) is
12393 Decl : Node_Id;
12394
12395 begin
12396 Decl := First (Decls);
12397 while Present (Decl) loop
12398 if Sloc (Decl) >= Sloc (Inst_Node) then
12399 return;
12400
12401 -- If Decl is an instantiation, then record it as requiring
12402 -- instantiation of the corresponding body, except if it is an
12403 -- abbreviated instantiation generated internally for conformance
12404 -- checking purposes only for the case of a formal package
12405 -- declared without a box (see Instantiate_Formal_Package). Such
12406 -- an instantiation does not generate any code (the actual code
12407 -- comes from actual) and thus does not need to be analyzed here.
12408 -- If the instantiation appears with a generic package body it is
12409 -- not analyzed here either.
12410
12411 elsif Nkind (Decl) = N_Package_Instantiation
12412 and then not Is_Internal (Defining_Entity (Decl))
12413 then
12414 Append_Elmt (Decl, Previous_Instances);
12415
12416 -- For a subprogram instantiation, omit instantiations intrinsic
12417 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12418
12419 elsif Nkind_In (Decl, N_Function_Instantiation,
12420 N_Procedure_Instantiation)
12421 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12422 then
12423 Append_Elmt (Decl, Previous_Instances);
12424
12425 elsif Nkind (Decl) = N_Package_Declaration then
12426 Collect_Previous_Instances
12427 (Visible_Declarations (Specification (Decl)));
12428 Collect_Previous_Instances
12429 (Private_Declarations (Specification (Decl)));
12430
12431 -- Previous non-generic bodies may contain instances as well
12432
12433 elsif Nkind (Decl) = N_Package_Body
12434 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12435 then
12436 Collect_Previous_Instances (Declarations (Decl));
12437
12438 elsif Nkind (Decl) = N_Subprogram_Body
12439 and then not Acts_As_Spec (Decl)
12440 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12441 then
12442 Collect_Previous_Instances (Declarations (Decl));
12443 end if;
12444
12445 Next (Decl);
12446 end loop;
12447 end Collect_Previous_Instances;
12448
12449 -- Start of processing for Load_Parent_Of_Generic
12450
12451 begin
12452 if not In_Same_Source_Unit (N, Spec)
12453 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12454 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12455 and then not Is_In_Main_Unit (Spec))
12456 then
12457 -- Find body of parent of spec, and analyze it. A special case arises
12458 -- when the parent is an instantiation, that is to say when we are
12459 -- currently instantiating a nested generic. In that case, there is
12460 -- no separate file for the body of the enclosing instance. Instead,
12461 -- the enclosing body must be instantiated as if it were a pending
12462 -- instantiation, in order to produce the body for the nested generic
12463 -- we require now. Note that in that case the generic may be defined
12464 -- in a package body, the instance defined in the same package body,
12465 -- and the original enclosing body may not be in the main unit.
12466
12467 Inst_Node := Empty;
12468
12469 True_Parent := Parent (Spec);
12470 while Present (True_Parent)
12471 and then Nkind (True_Parent) /= N_Compilation_Unit
12472 loop
12473 if Nkind (True_Parent) = N_Package_Declaration
12474 and then
12475 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12476 then
12477 -- Parent is a compilation unit that is an instantiation.
12478 -- Instantiation node has been replaced with package decl.
12479
12480 Inst_Node := Original_Node (True_Parent);
12481 exit;
12482
12483 elsif Nkind (True_Parent) = N_Package_Declaration
12484 and then Present (Generic_Parent (Specification (True_Parent)))
12485 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12486 then
12487 -- Parent is an instantiation within another specification.
12488 -- Declaration for instance has been inserted before original
12489 -- instantiation node. A direct link would be preferable?
12490
12491 Inst_Node := Next (True_Parent);
12492 while Present (Inst_Node)
12493 and then Nkind (Inst_Node) /= N_Package_Instantiation
12494 loop
12495 Next (Inst_Node);
12496 end loop;
12497
12498 -- If the instance appears within a generic, and the generic
12499 -- unit is defined within a formal package of the enclosing
12500 -- generic, there is no generic body available, and none
12501 -- needed. A more precise test should be used ???
12502
12503 if No (Inst_Node) then
12504 return;
12505 end if;
12506
12507 exit;
12508
12509 else
12510 True_Parent := Parent (True_Parent);
12511 end if;
12512 end loop;
12513
12514 -- Case where we are currently instantiating a nested generic
12515
12516 if Present (Inst_Node) then
12517 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12518
12519 -- Instantiation node and declaration of instantiated package
12520 -- were exchanged when only the declaration was needed.
12521 -- Restore instantiation node before proceeding with body.
12522
12523 Set_Unit (Parent (True_Parent), Inst_Node);
12524 end if;
12525
12526 -- Now complete instantiation of enclosing body, if it appears in
12527 -- some other unit. If it appears in the current unit, the body
12528 -- will have been instantiated already.
12529
12530 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12531
12532 -- We need to determine the expander mode to instantiate the
12533 -- enclosing body. Because the generic body we need may use
12534 -- global entities declared in the enclosing package (including
12535 -- aggregates) it is in general necessary to compile this body
12536 -- with expansion enabled, except if we are within a generic
12537 -- package, in which case the usual generic rule applies.
12538
12539 declare
12540 Exp_Status : Boolean := True;
12541 Scop : Entity_Id;
12542
12543 begin
12544 -- Loop through scopes looking for generic package
12545
12546 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12547 while Present (Scop)
12548 and then Scop /= Standard_Standard
12549 loop
12550 if Ekind (Scop) = E_Generic_Package then
12551 Exp_Status := False;
12552 exit;
12553 end if;
12554
12555 Scop := Scope (Scop);
12556 end loop;
12557
12558 -- Collect previous instantiations in the unit that contains
12559 -- the desired generic.
12560
12561 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12562 and then not Body_Optional
12563 then
12564 declare
12565 Decl : Elmt_Id;
12566 Info : Pending_Body_Info;
12567 Par : Node_Id;
12568
12569 begin
12570 Par := Parent (Inst_Node);
12571 while Present (Par) loop
12572 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12573 Par := Parent (Par);
12574 end loop;
12575
12576 pragma Assert (Present (Par));
12577
12578 if Nkind (Par) = N_Package_Body then
12579 Collect_Previous_Instances (Declarations (Par));
12580
12581 elsif Nkind (Par) = N_Package_Declaration then
12582 Collect_Previous_Instances
12583 (Visible_Declarations (Specification (Par)));
12584 Collect_Previous_Instances
12585 (Private_Declarations (Specification (Par)));
12586
12587 else
12588 -- Enclosing unit is a subprogram body. In this
12589 -- case all instance bodies are processed in order
12590 -- and there is no need to collect them separately.
12591
12592 null;
12593 end if;
12594
12595 Decl := First_Elmt (Previous_Instances);
12596 while Present (Decl) loop
12597 Info :=
12598 (Inst_Node => Node (Decl),
12599 Act_Decl =>
12600 Instance_Spec (Node (Decl)),
12601 Expander_Status => Exp_Status,
12602 Current_Sem_Unit =>
12603 Get_Code_Unit (Sloc (Node (Decl))),
12604 Scope_Suppress => Scope_Suppress,
12605 Local_Suppress_Stack_Top =>
12606 Local_Suppress_Stack_Top,
12607 Version => Ada_Version,
12608 Version_Pragma => Ada_Version_Pragma,
12609 Warnings => Save_Warnings,
12610 SPARK_Mode => SPARK_Mode,
12611 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12612
12613 -- Package instance
12614
12615 if
12616 Nkind (Node (Decl)) = N_Package_Instantiation
12617 then
12618 Instantiate_Package_Body
12619 (Info, Body_Optional => True);
12620
12621 -- Subprogram instance
12622
12623 else
12624 -- The instance_spec is the wrapper package,
12625 -- and the subprogram declaration is the last
12626 -- declaration in the wrapper.
12627
12628 Info.Act_Decl :=
12629 Last
12630 (Visible_Declarations
12631 (Specification (Info.Act_Decl)));
12632
12633 Instantiate_Subprogram_Body
12634 (Info, Body_Optional => True);
12635 end if;
12636
12637 Next_Elmt (Decl);
12638 end loop;
12639 end;
12640 end if;
12641
12642 Instantiate_Package_Body
12643 (Body_Info =>
12644 ((Inst_Node => Inst_Node,
12645 Act_Decl => True_Parent,
12646 Expander_Status => Exp_Status,
12647 Current_Sem_Unit => Get_Code_Unit
12648 (Sloc (Inst_Node)),
12649 Scope_Suppress => Scope_Suppress,
12650 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12651 Version => Ada_Version,
12652 Version_Pragma => Ada_Version_Pragma,
12653 Warnings => Save_Warnings,
12654 SPARK_Mode => SPARK_Mode,
12655 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12656 Body_Optional => Body_Optional);
12657 end;
12658 end if;
12659
12660 -- Case where we are not instantiating a nested generic
12661
12662 else
12663 Opt.Style_Check := False;
12664 Expander_Mode_Save_And_Set (True);
12665 Load_Needed_Body (Comp_Unit, OK);
12666 Opt.Style_Check := Saved_Style_Check;
12667 Restore_Warnings (Saved_Warnings);
12668 Expander_Mode_Restore;
12669
12670 if not OK
12671 and then Unit_Requires_Body (Defining_Entity (Spec))
12672 and then not Body_Optional
12673 then
12674 declare
12675 Bname : constant Unit_Name_Type :=
12676 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12677
12678 begin
12679 -- In CodePeer mode, the missing body may make the analysis
12680 -- incomplete, but we do not treat it as fatal.
12681
12682 if CodePeer_Mode then
12683 return;
12684
12685 else
12686 Error_Msg_Unit_1 := Bname;
12687 Error_Msg_N ("this instantiation requires$!", N);
12688 Error_Msg_File_1 :=
12689 Get_File_Name (Bname, Subunit => False);
12690 Error_Msg_N ("\but file{ was not found!", N);
12691 raise Unrecoverable_Error;
12692 end if;
12693 end;
12694 end if;
12695 end if;
12696 end if;
12697
12698 -- If loading parent of the generic caused an instantiation circularity,
12699 -- we abandon compilation at this point, because otherwise in some cases
12700 -- we get into trouble with infinite recursions after this point.
12701
12702 if Circularity_Detected then
12703 raise Unrecoverable_Error;
12704 end if;
12705 end Load_Parent_Of_Generic;
12706
12707 ---------------------------------
12708 -- Map_Formal_Package_Entities --
12709 ---------------------------------
12710
12711 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
12712 E1 : Entity_Id;
12713 E2 : Entity_Id;
12714
12715 begin
12716 Set_Instance_Of (Form, Act);
12717
12718 -- Traverse formal and actual package to map the corresponding entities.
12719 -- We skip over internal entities that may be generated during semantic
12720 -- analysis, and find the matching entities by name, given that they
12721 -- must appear in the same order.
12722
12723 E1 := First_Entity (Form);
12724 E2 := First_Entity (Act);
12725 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
12726 -- Could this test be a single condition??? Seems like it could, and
12727 -- isn't FPE (Form) a constant anyway???
12728
12729 if not Is_Internal (E1)
12730 and then Present (Parent (E1))
12731 and then not Is_Class_Wide_Type (E1)
12732 and then not Is_Internal_Name (Chars (E1))
12733 then
12734 while Present (E2) and then Chars (E2) /= Chars (E1) loop
12735 Next_Entity (E2);
12736 end loop;
12737
12738 if No (E2) then
12739 exit;
12740 else
12741 Set_Instance_Of (E1, E2);
12742
12743 if Is_Type (E1) and then Is_Tagged_Type (E2) then
12744 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
12745 end if;
12746
12747 if Is_Constrained (E1) then
12748 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
12749 end if;
12750
12751 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
12752 Map_Formal_Package_Entities (E1, E2);
12753 end if;
12754 end if;
12755 end if;
12756
12757 Next_Entity (E1);
12758 end loop;
12759 end Map_Formal_Package_Entities;
12760
12761 -----------------------
12762 -- Move_Freeze_Nodes --
12763 -----------------------
12764
12765 procedure Move_Freeze_Nodes
12766 (Out_Of : Entity_Id;
12767 After : Node_Id;
12768 L : List_Id)
12769 is
12770 Decl : Node_Id;
12771 Next_Decl : Node_Id;
12772 Next_Node : Node_Id := After;
12773 Spec : Node_Id;
12774
12775 function Is_Outer_Type (T : Entity_Id) return Boolean;
12776 -- Check whether entity is declared in a scope external to that of the
12777 -- generic unit.
12778
12779 -------------------
12780 -- Is_Outer_Type --
12781 -------------------
12782
12783 function Is_Outer_Type (T : Entity_Id) return Boolean is
12784 Scop : Entity_Id := Scope (T);
12785
12786 begin
12787 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
12788 return True;
12789
12790 else
12791 while Scop /= Standard_Standard loop
12792 if Scop = Out_Of then
12793 return False;
12794 else
12795 Scop := Scope (Scop);
12796 end if;
12797 end loop;
12798
12799 return True;
12800 end if;
12801 end Is_Outer_Type;
12802
12803 -- Start of processing for Move_Freeze_Nodes
12804
12805 begin
12806 if No (L) then
12807 return;
12808 end if;
12809
12810 -- First remove the freeze nodes that may appear before all other
12811 -- declarations.
12812
12813 Decl := First (L);
12814 while Present (Decl)
12815 and then Nkind (Decl) = N_Freeze_Entity
12816 and then Is_Outer_Type (Entity (Decl))
12817 loop
12818 Decl := Remove_Head (L);
12819 Insert_After (Next_Node, Decl);
12820 Set_Analyzed (Decl, False);
12821 Next_Node := Decl;
12822 Decl := First (L);
12823 end loop;
12824
12825 -- Next scan the list of declarations and remove each freeze node that
12826 -- appears ahead of the current node.
12827
12828 while Present (Decl) loop
12829 while Present (Next (Decl))
12830 and then Nkind (Next (Decl)) = N_Freeze_Entity
12831 and then Is_Outer_Type (Entity (Next (Decl)))
12832 loop
12833 Next_Decl := Remove_Next (Decl);
12834 Insert_After (Next_Node, Next_Decl);
12835 Set_Analyzed (Next_Decl, False);
12836 Next_Node := Next_Decl;
12837 end loop;
12838
12839 -- If the declaration is a nested package or concurrent type, then
12840 -- recurse. Nested generic packages will have been processed from the
12841 -- inside out.
12842
12843 case Nkind (Decl) is
12844 when N_Package_Declaration =>
12845 Spec := Specification (Decl);
12846
12847 when N_Task_Type_Declaration =>
12848 Spec := Task_Definition (Decl);
12849
12850 when N_Protected_Type_Declaration =>
12851 Spec := Protected_Definition (Decl);
12852
12853 when others =>
12854 Spec := Empty;
12855 end case;
12856
12857 if Present (Spec) then
12858 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
12859 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
12860 end if;
12861
12862 Next (Decl);
12863 end loop;
12864 end Move_Freeze_Nodes;
12865
12866 ----------------
12867 -- Next_Assoc --
12868 ----------------
12869
12870 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
12871 begin
12872 return Generic_Renamings.Table (E).Next_In_HTable;
12873 end Next_Assoc;
12874
12875 ------------------------
12876 -- Preanalyze_Actuals --
12877 ------------------------
12878
12879 procedure Preanalyze_Actuals (N : Node_Id) is
12880 Assoc : Node_Id;
12881 Act : Node_Id;
12882 Errs : constant Int := Serious_Errors_Detected;
12883
12884 Cur : Entity_Id := Empty;
12885 -- Current homograph of the instance name
12886
12887 Vis : Boolean;
12888 -- Saved visibility status of the current homograph
12889
12890 begin
12891 Assoc := First (Generic_Associations (N));
12892
12893 -- If the instance is a child unit, its name may hide an outer homonym,
12894 -- so make it invisible to perform name resolution on the actuals.
12895
12896 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
12897 and then Present
12898 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
12899 then
12900 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
12901
12902 if Is_Compilation_Unit (Cur) then
12903 Vis := Is_Immediately_Visible (Cur);
12904 Set_Is_Immediately_Visible (Cur, False);
12905 else
12906 Cur := Empty;
12907 end if;
12908 end if;
12909
12910 while Present (Assoc) loop
12911 if Nkind (Assoc) /= N_Others_Choice then
12912 Act := Explicit_Generic_Actual_Parameter (Assoc);
12913
12914 -- Within a nested instantiation, a defaulted actual is an empty
12915 -- association, so nothing to analyze. If the subprogram actual
12916 -- is an attribute, analyze prefix only, because actual is not a
12917 -- complete attribute reference.
12918
12919 -- If actual is an allocator, analyze expression only. The full
12920 -- analysis can generate code, and if instance is a compilation
12921 -- unit we have to wait until the package instance is installed
12922 -- to have a proper place to insert this code.
12923
12924 -- String literals may be operators, but at this point we do not
12925 -- know whether the actual is a formal subprogram or a string.
12926
12927 if No (Act) then
12928 null;
12929
12930 elsif Nkind (Act) = N_Attribute_Reference then
12931 Analyze (Prefix (Act));
12932
12933 elsif Nkind (Act) = N_Explicit_Dereference then
12934 Analyze (Prefix (Act));
12935
12936 elsif Nkind (Act) = N_Allocator then
12937 declare
12938 Expr : constant Node_Id := Expression (Act);
12939
12940 begin
12941 if Nkind (Expr) = N_Subtype_Indication then
12942 Analyze (Subtype_Mark (Expr));
12943
12944 -- Analyze separately each discriminant constraint, when
12945 -- given with a named association.
12946
12947 declare
12948 Constr : Node_Id;
12949
12950 begin
12951 Constr := First (Constraints (Constraint (Expr)));
12952 while Present (Constr) loop
12953 if Nkind (Constr) = N_Discriminant_Association then
12954 Analyze (Expression (Constr));
12955 else
12956 Analyze (Constr);
12957 end if;
12958
12959 Next (Constr);
12960 end loop;
12961 end;
12962
12963 else
12964 Analyze (Expr);
12965 end if;
12966 end;
12967
12968 elsif Nkind (Act) /= N_Operator_Symbol then
12969 Analyze (Act);
12970 end if;
12971
12972 -- Ensure that a ghost subprogram does not act as generic actual
12973
12974 if Is_Entity_Name (Act)
12975 and then Is_Ghost_Subprogram (Entity (Act))
12976 then
12977 Error_Msg_N
12978 ("ghost subprogram & cannot act as generic actual", Act);
12979 Abandon_Instantiation (Act);
12980
12981 elsif Errs /= Serious_Errors_Detected then
12982
12983 -- Do a minimal analysis of the generic, to prevent spurious
12984 -- warnings complaining about the generic being unreferenced,
12985 -- before abandoning the instantiation.
12986
12987 Analyze (Name (N));
12988
12989 if Is_Entity_Name (Name (N))
12990 and then Etype (Name (N)) /= Any_Type
12991 then
12992 Generate_Reference (Entity (Name (N)), Name (N));
12993 Set_Is_Instantiated (Entity (Name (N)));
12994 end if;
12995
12996 if Present (Cur) then
12997
12998 -- For the case of a child instance hiding an outer homonym,
12999 -- provide additional warning which might explain the error.
13000
13001 Set_Is_Immediately_Visible (Cur, Vis);
13002 Error_Msg_NE ("& hides outer unit with the same name??",
13003 N, Defining_Unit_Name (N));
13004 end if;
13005
13006 Abandon_Instantiation (Act);
13007 end if;
13008 end if;
13009
13010 Next (Assoc);
13011 end loop;
13012
13013 if Present (Cur) then
13014 Set_Is_Immediately_Visible (Cur, Vis);
13015 end if;
13016 end Preanalyze_Actuals;
13017
13018 -------------------
13019 -- Remove_Parent --
13020 -------------------
13021
13022 procedure Remove_Parent (In_Body : Boolean := False) is
13023 S : Entity_Id := Current_Scope;
13024 -- S is the scope containing the instantiation just completed. The scope
13025 -- stack contains the parent instances of the instantiation, followed by
13026 -- the original S.
13027
13028 Cur_P : Entity_Id;
13029 E : Entity_Id;
13030 P : Entity_Id;
13031 Hidden : Elmt_Id;
13032
13033 begin
13034 -- After child instantiation is complete, remove from scope stack the
13035 -- extra copy of the current scope, and then remove parent instances.
13036
13037 if not In_Body then
13038 Pop_Scope;
13039
13040 while Current_Scope /= S loop
13041 P := Current_Scope;
13042 End_Package_Scope (Current_Scope);
13043
13044 if In_Open_Scopes (P) then
13045 E := First_Entity (P);
13046 while Present (E) loop
13047 Set_Is_Immediately_Visible (E, True);
13048 Next_Entity (E);
13049 end loop;
13050
13051 -- If instantiation is declared in a block, it is the enclosing
13052 -- scope that might be a parent instance. Note that only one
13053 -- block can be involved, because the parent instances have
13054 -- been installed within it.
13055
13056 if Ekind (P) = E_Block then
13057 Cur_P := Scope (P);
13058 else
13059 Cur_P := P;
13060 end if;
13061
13062 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13063 -- We are within an instance of some sibling. Retain
13064 -- visibility of parent, for proper subsequent cleanup, and
13065 -- reinstall private declarations as well.
13066
13067 Set_In_Private_Part (P);
13068 Install_Private_Declarations (P);
13069 end if;
13070
13071 -- If the ultimate parent is a top-level unit recorded in
13072 -- Instance_Parent_Unit, then reset its visibility to what it was
13073 -- before instantiation. (It's not clear what the purpose is of
13074 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13075 -- present before the ultimate parent test was added.???)
13076
13077 elsif not In_Open_Scopes (Scope (P))
13078 or else (P = Instance_Parent_Unit
13079 and then not Parent_Unit_Visible)
13080 then
13081 Set_Is_Immediately_Visible (P, False);
13082
13083 -- If the current scope is itself an instantiation of a generic
13084 -- nested within P, and we are in the private part of body of this
13085 -- instantiation, restore the full views of P, that were removed
13086 -- in End_Package_Scope above. This obscure case can occur when a
13087 -- subunit of a generic contains an instance of a child unit of
13088 -- its generic parent unit.
13089
13090 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13091 declare
13092 Par : constant Entity_Id :=
13093 Generic_Parent (Package_Specification (S));
13094 begin
13095 if Present (Par)
13096 and then P = Scope (Par)
13097 and then (In_Package_Body (S) or else In_Private_Part (S))
13098 then
13099 Set_In_Private_Part (P);
13100 Install_Private_Declarations (P);
13101 end if;
13102 end;
13103 end if;
13104 end loop;
13105
13106 -- Reset visibility of entities in the enclosing scope
13107
13108 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13109
13110 Hidden := First_Elmt (Hidden_Entities);
13111 while Present (Hidden) loop
13112 Set_Is_Immediately_Visible (Node (Hidden), True);
13113 Next_Elmt (Hidden);
13114 end loop;
13115
13116 else
13117 -- Each body is analyzed separately, and there is no context that
13118 -- needs preserving from one body instance to the next, so remove all
13119 -- parent scopes that have been installed.
13120
13121 while Present (S) loop
13122 End_Package_Scope (S);
13123 Set_Is_Immediately_Visible (S, False);
13124 S := Current_Scope;
13125 exit when S = Standard_Standard;
13126 end loop;
13127 end if;
13128 end Remove_Parent;
13129
13130 -----------------
13131 -- Restore_Env --
13132 -----------------
13133
13134 procedure Restore_Env is
13135 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13136
13137 begin
13138 if No (Current_Instantiated_Parent.Act_Id) then
13139 -- Restore environment after subprogram inlining
13140
13141 Restore_Private_Views (Empty);
13142 end if;
13143
13144 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13145 Exchanged_Views := Saved.Exchanged_Views;
13146 Hidden_Entities := Saved.Hidden_Entities;
13147 Current_Sem_Unit := Saved.Current_Sem_Unit;
13148 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13149 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13150
13151 Restore_Opt_Config_Switches (Saved.Switches);
13152
13153 Instance_Envs.Decrement_Last;
13154 end Restore_Env;
13155
13156 ---------------------------
13157 -- Restore_Private_Views --
13158 ---------------------------
13159
13160 procedure Restore_Private_Views
13161 (Pack_Id : Entity_Id;
13162 Is_Package : Boolean := True)
13163 is
13164 M : Elmt_Id;
13165 E : Entity_Id;
13166 Typ : Entity_Id;
13167 Dep_Elmt : Elmt_Id;
13168 Dep_Typ : Node_Id;
13169
13170 procedure Restore_Nested_Formal (Formal : Entity_Id);
13171 -- Hide the generic formals of formal packages declared with box which
13172 -- were reachable in the current instantiation.
13173
13174 ---------------------------
13175 -- Restore_Nested_Formal --
13176 ---------------------------
13177
13178 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13179 Ent : Entity_Id;
13180
13181 begin
13182 if Present (Renamed_Object (Formal))
13183 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13184 then
13185 return;
13186
13187 elsif Present (Associated_Formal_Package (Formal)) then
13188 Ent := First_Entity (Formal);
13189 while Present (Ent) loop
13190 exit when Ekind (Ent) = E_Package
13191 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13192
13193 Set_Is_Hidden (Ent);
13194 Set_Is_Potentially_Use_Visible (Ent, False);
13195
13196 -- If package, then recurse
13197
13198 if Ekind (Ent) = E_Package then
13199 Restore_Nested_Formal (Ent);
13200 end if;
13201
13202 Next_Entity (Ent);
13203 end loop;
13204 end if;
13205 end Restore_Nested_Formal;
13206
13207 -- Start of processing for Restore_Private_Views
13208
13209 begin
13210 M := First_Elmt (Exchanged_Views);
13211 while Present (M) loop
13212 Typ := Node (M);
13213
13214 -- Subtypes of types whose views have been exchanged, and that are
13215 -- defined within the instance, were not on the Private_Dependents
13216 -- list on entry to the instance, so they have to be exchanged
13217 -- explicitly now, in order to remain consistent with the view of the
13218 -- parent type.
13219
13220 if Ekind_In (Typ, E_Private_Type,
13221 E_Limited_Private_Type,
13222 E_Record_Type_With_Private)
13223 then
13224 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13225 while Present (Dep_Elmt) loop
13226 Dep_Typ := Node (Dep_Elmt);
13227
13228 if Scope (Dep_Typ) = Pack_Id
13229 and then Present (Full_View (Dep_Typ))
13230 then
13231 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13232 Exchange_Declarations (Dep_Typ);
13233 end if;
13234
13235 Next_Elmt (Dep_Elmt);
13236 end loop;
13237 end if;
13238
13239 Exchange_Declarations (Node (M));
13240 Next_Elmt (M);
13241 end loop;
13242
13243 if No (Pack_Id) then
13244 return;
13245 end if;
13246
13247 -- Make the generic formal parameters private, and make the formal types
13248 -- into subtypes of the actuals again.
13249
13250 E := First_Entity (Pack_Id);
13251 while Present (E) loop
13252 Set_Is_Hidden (E, True);
13253
13254 if Is_Type (E)
13255 and then Nkind (Parent (E)) = N_Subtype_Declaration
13256 then
13257 -- If the actual for E is itself a generic actual type from
13258 -- an enclosing instance, E is still a generic actual type
13259 -- outside of the current instance. This matter when resolving
13260 -- an overloaded call that may be ambiguous in the enclosing
13261 -- instance, when two of its actuals coincide.
13262
13263 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13264 and then Is_Generic_Actual_Type
13265 (Entity (Subtype_Indication (Parent (E))))
13266 then
13267 null;
13268 else
13269 Set_Is_Generic_Actual_Type (E, False);
13270 end if;
13271
13272 -- An unusual case of aliasing: the actual may also be directly
13273 -- visible in the generic, and be private there, while it is fully
13274 -- visible in the context of the instance. The internal subtype
13275 -- is private in the instance but has full visibility like its
13276 -- parent in the enclosing scope. This enforces the invariant that
13277 -- the privacy status of all private dependents of a type coincide
13278 -- with that of the parent type. This can only happen when a
13279 -- generic child unit is instantiated within a sibling.
13280
13281 if Is_Private_Type (E)
13282 and then not Is_Private_Type (Etype (E))
13283 then
13284 Exchange_Declarations (E);
13285 end if;
13286
13287 elsif Ekind (E) = E_Package then
13288
13289 -- The end of the renaming list is the renaming of the generic
13290 -- package itself. If the instance is a subprogram, all entities
13291 -- in the corresponding package are renamings. If this entity is
13292 -- a formal package, make its own formals private as well. The
13293 -- actual in this case is itself the renaming of an instantiation.
13294 -- If the entity is not a package renaming, it is the entity
13295 -- created to validate formal package actuals: ignore it.
13296
13297 -- If the actual is itself a formal package for the enclosing
13298 -- generic, or the actual for such a formal package, it remains
13299 -- visible on exit from the instance, and therefore nothing needs
13300 -- to be done either, except to keep it accessible.
13301
13302 if Is_Package and then Renamed_Object (E) = Pack_Id then
13303 exit;
13304
13305 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13306 null;
13307
13308 elsif
13309 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13310 then
13311 Set_Is_Hidden (E, False);
13312
13313 else
13314 declare
13315 Act_P : constant Entity_Id := Renamed_Object (E);
13316 Id : Entity_Id;
13317
13318 begin
13319 Id := First_Entity (Act_P);
13320 while Present (Id)
13321 and then Id /= First_Private_Entity (Act_P)
13322 loop
13323 exit when Ekind (Id) = E_Package
13324 and then Renamed_Object (Id) = Act_P;
13325
13326 Set_Is_Hidden (Id, True);
13327 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13328
13329 if Ekind (Id) = E_Package then
13330 Restore_Nested_Formal (Id);
13331 end if;
13332
13333 Next_Entity (Id);
13334 end loop;
13335 end;
13336 end if;
13337 end if;
13338
13339 Next_Entity (E);
13340 end loop;
13341 end Restore_Private_Views;
13342
13343 --------------
13344 -- Save_Env --
13345 --------------
13346
13347 procedure Save_Env
13348 (Gen_Unit : Entity_Id;
13349 Act_Unit : Entity_Id)
13350 is
13351 begin
13352 Init_Env;
13353 Set_Instance_Env (Gen_Unit, Act_Unit);
13354 end Save_Env;
13355
13356 ----------------------------
13357 -- Save_Global_References --
13358 ----------------------------
13359
13360 procedure Save_Global_References (N : Node_Id) is
13361 Gen_Scope : Entity_Id;
13362 E : Entity_Id;
13363 N2 : Node_Id;
13364
13365 function Is_Global (E : Entity_Id) return Boolean;
13366 -- Check whether entity is defined outside of generic unit. Examine the
13367 -- scope of an entity, and the scope of the scope, etc, until we find
13368 -- either Standard, in which case the entity is global, or the generic
13369 -- unit itself, which indicates that the entity is local. If the entity
13370 -- is the generic unit itself, as in the case of a recursive call, or
13371 -- the enclosing generic unit, if different from the current scope, then
13372 -- it is local as well, because it will be replaced at the point of
13373 -- instantiation. On the other hand, if it is a reference to a child
13374 -- unit of a common ancestor, which appears in an instantiation, it is
13375 -- global because it is used to denote a specific compilation unit at
13376 -- the time the instantiations will be analyzed.
13377
13378 procedure Reset_Entity (N : Node_Id);
13379 -- Save semantic information on global entity so that it is not resolved
13380 -- again at instantiation time.
13381
13382 procedure Save_Entity_Descendants (N : Node_Id);
13383 -- Apply Save_Global_References to the two syntactic descendants of
13384 -- non-terminal nodes that carry an Associated_Node and are processed
13385 -- through Reset_Entity. Once the global entity (if any) has been
13386 -- captured together with its type, only two syntactic descendants need
13387 -- to be traversed to complete the processing of the tree rooted at N.
13388 -- This applies to Selected_Components, Expanded_Names, and to Operator
13389 -- nodes. N can also be a character literal, identifier, or operator
13390 -- symbol node, but the call has no effect in these cases.
13391
13392 procedure Save_Global_Defaults (N1, N2 : Node_Id);
13393 -- Default actuals in nested instances must be handled specially
13394 -- because there is no link to them from the original tree. When an
13395 -- actual subprogram is given by a default, we add an explicit generic
13396 -- association for it in the instantiation node. When we save the
13397 -- global references on the name of the instance, we recover the list
13398 -- of generic associations, and add an explicit one to the original
13399 -- generic tree, through which a global actual can be preserved.
13400 -- Similarly, if a child unit is instantiated within a sibling, in the
13401 -- context of the parent, we must preserve the identifier of the parent
13402 -- so that it can be properly resolved in a subsequent instantiation.
13403
13404 procedure Save_Global_Descendant (D : Union_Id);
13405 -- Apply Save_Global_References recursively to the descendents of the
13406 -- current node.
13407
13408 procedure Save_References (N : Node_Id);
13409 -- This is the recursive procedure that does the work, once the
13410 -- enclosing generic scope has been established.
13411
13412 ---------------
13413 -- Is_Global --
13414 ---------------
13415
13416 function Is_Global (E : Entity_Id) return Boolean is
13417 Se : Entity_Id;
13418
13419 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13420 -- Determine whether the parent node of a reference to a child unit
13421 -- denotes an instantiation or a formal package, in which case the
13422 -- reference to the child unit is global, even if it appears within
13423 -- the current scope (e.g. when the instance appears within the body
13424 -- of an ancestor).
13425
13426 ----------------------
13427 -- Is_Instance_Node --
13428 ----------------------
13429
13430 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13431 begin
13432 return Nkind (Decl) in N_Generic_Instantiation
13433 or else
13434 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13435 end Is_Instance_Node;
13436
13437 -- Start of processing for Is_Global
13438
13439 begin
13440 if E = Gen_Scope then
13441 return False;
13442
13443 elsif E = Standard_Standard then
13444 return True;
13445
13446 elsif Is_Child_Unit (E)
13447 and then (Is_Instance_Node (Parent (N2))
13448 or else (Nkind (Parent (N2)) = N_Expanded_Name
13449 and then N2 = Selector_Name (Parent (N2))
13450 and then
13451 Is_Instance_Node (Parent (Parent (N2)))))
13452 then
13453 return True;
13454
13455 else
13456 Se := Scope (E);
13457 while Se /= Gen_Scope loop
13458 if Se = Standard_Standard then
13459 return True;
13460 else
13461 Se := Scope (Se);
13462 end if;
13463 end loop;
13464
13465 return False;
13466 end if;
13467 end Is_Global;
13468
13469 ------------------
13470 -- Reset_Entity --
13471 ------------------
13472
13473 procedure Reset_Entity (N : Node_Id) is
13474
13475 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13476 -- If the type of N2 is global to the generic unit, save the type in
13477 -- the generic node. Just as we perform name capture for explicit
13478 -- references within the generic, we must capture the global types
13479 -- of local entities because they may participate in resolution in
13480 -- the instance.
13481
13482 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13483 -- Find the ultimate ancestor of the current unit. If it is not a
13484 -- generic unit, then the name of the current unit in the prefix of
13485 -- an expanded name must be replaced with its generic homonym to
13486 -- ensure that it will be properly resolved in an instance.
13487
13488 ---------------------
13489 -- Set_Global_Type --
13490 ---------------------
13491
13492 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13493 Typ : constant Entity_Id := Etype (N2);
13494
13495 begin
13496 Set_Etype (N, Typ);
13497
13498 if Entity (N) /= N2
13499 and then Has_Private_View (Entity (N))
13500 then
13501 -- If the entity of N is not the associated node, this is a
13502 -- nested generic and it has an associated node as well, whose
13503 -- type is already the full view (see below). Indicate that the
13504 -- original node has a private view.
13505
13506 Set_Has_Private_View (N);
13507 end if;
13508
13509 -- If not a private type, nothing else to do
13510
13511 if not Is_Private_Type (Typ) then
13512 if Is_Array_Type (Typ)
13513 and then Is_Private_Type (Component_Type (Typ))
13514 then
13515 Set_Has_Private_View (N);
13516 end if;
13517
13518 -- If it is a derivation of a private type in a context where no
13519 -- full view is needed, nothing to do either.
13520
13521 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13522 null;
13523
13524 -- Otherwise mark the type for flipping and use the full view when
13525 -- available.
13526
13527 else
13528 Set_Has_Private_View (N);
13529
13530 if Present (Full_View (Typ)) then
13531 Set_Etype (N2, Full_View (Typ));
13532 end if;
13533 end if;
13534 end Set_Global_Type;
13535
13536 ------------------
13537 -- Top_Ancestor --
13538 ------------------
13539
13540 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13541 Par : Entity_Id;
13542
13543 begin
13544 Par := E;
13545 while Is_Child_Unit (Par) loop
13546 Par := Scope (Par);
13547 end loop;
13548
13549 return Par;
13550 end Top_Ancestor;
13551
13552 -- Start of processing for Reset_Entity
13553
13554 begin
13555 N2 := Get_Associated_Node (N);
13556 E := Entity (N2);
13557
13558 if Present (E) then
13559
13560 -- If the node is an entry call to an entry in an enclosing task,
13561 -- it is rewritten as a selected component. No global entity to
13562 -- preserve in this case, since the expansion will be redone in
13563 -- the instance.
13564
13565 if not Nkind_In (E, N_Defining_Identifier,
13566 N_Defining_Character_Literal,
13567 N_Defining_Operator_Symbol)
13568 then
13569 Set_Associated_Node (N, Empty);
13570 Set_Etype (N, Empty);
13571 return;
13572 end if;
13573
13574 -- If the entity is an itype created as a subtype of an access
13575 -- type with a null exclusion restore source entity for proper
13576 -- visibility. The itype will be created anew in the instance.
13577
13578 if Is_Itype (E)
13579 and then Ekind (E) = E_Access_Subtype
13580 and then Is_Entity_Name (N)
13581 and then Chars (Etype (E)) = Chars (N)
13582 then
13583 E := Etype (E);
13584 Set_Entity (N2, E);
13585 Set_Etype (N2, E);
13586 end if;
13587
13588 if Is_Global (E) then
13589
13590 -- If the entity is a package renaming that is the prefix of
13591 -- an expanded name, it has been rewritten as the renamed
13592 -- package, which is necessary semantically but complicates
13593 -- ASIS tree traversal, so we recover the original entity to
13594 -- expose the renaming. Take into account that the context may
13595 -- be a nested generic, that the original node may itself have
13596 -- an associated node that had better be an entity, and that
13597 -- the current node is still a selected component.
13598
13599 if Ekind (E) = E_Package
13600 and then Nkind (N) = N_Selected_Component
13601 and then Nkind (Parent (N)) = N_Expanded_Name
13602 and then Present (Original_Node (N2))
13603 and then Is_Entity_Name (Original_Node (N2))
13604 and then Present (Entity (Original_Node (N2)))
13605 then
13606 if Is_Global (Entity (Original_Node (N2))) then
13607 N2 := Original_Node (N2);
13608 Set_Associated_Node (N, N2);
13609 Set_Global_Type (N, N2);
13610
13611 else
13612 -- Renaming is local, and will be resolved in instance
13613
13614 Set_Associated_Node (N, Empty);
13615 Set_Etype (N, Empty);
13616 end if;
13617
13618 else
13619 Set_Global_Type (N, N2);
13620 end if;
13621
13622 elsif Nkind (N) = N_Op_Concat
13623 and then Is_Generic_Type (Etype (N2))
13624 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13625 or else
13626 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13627 and then Is_Intrinsic_Subprogram (E)
13628 then
13629 null;
13630
13631 else
13632 -- Entity is local. Mark generic node as unresolved.
13633 -- Note that now it does not have an entity.
13634
13635 Set_Associated_Node (N, Empty);
13636 Set_Etype (N, Empty);
13637 end if;
13638
13639 if Nkind (Parent (N)) in N_Generic_Instantiation
13640 and then N = Name (Parent (N))
13641 then
13642 Save_Global_Defaults (Parent (N), Parent (N2));
13643 end if;
13644
13645 elsif Nkind (Parent (N)) = N_Selected_Component
13646 and then Nkind (Parent (N2)) = N_Expanded_Name
13647 then
13648 if Is_Global (Entity (Parent (N2))) then
13649 Change_Selected_Component_To_Expanded_Name (Parent (N));
13650 Set_Associated_Node (Parent (N), Parent (N2));
13651 Set_Global_Type (Parent (N), Parent (N2));
13652 Save_Entity_Descendants (N);
13653
13654 -- If this is a reference to the current generic entity, replace
13655 -- by the name of the generic homonym of the current package. This
13656 -- is because in an instantiation Par.P.Q will not resolve to the
13657 -- name of the instance, whose enclosing scope is not necessarily
13658 -- Par. We use the generic homonym rather that the name of the
13659 -- generic itself because it may be hidden by a local declaration.
13660
13661 elsif In_Open_Scopes (Entity (Parent (N2)))
13662 and then not
13663 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13664 then
13665 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13666 Rewrite (Parent (N),
13667 Make_Identifier (Sloc (N),
13668 Chars =>
13669 Chars (Generic_Homonym (Entity (Parent (N2))))));
13670 else
13671 Rewrite (Parent (N),
13672 Make_Identifier (Sloc (N),
13673 Chars => Chars (Selector_Name (Parent (N2)))));
13674 end if;
13675 end if;
13676
13677 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13678 and then Parent (N) = Name (Parent (Parent (N)))
13679 then
13680 Save_Global_Defaults
13681 (Parent (Parent (N)), Parent (Parent ((N2))));
13682 end if;
13683
13684 -- A selected component may denote a static constant that has been
13685 -- folded. If the static constant is global to the generic, capture
13686 -- its value. Otherwise the folding will happen in any instantiation.
13687
13688 elsif Nkind (Parent (N)) = N_Selected_Component
13689 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13690 then
13691 if Present (Entity (Original_Node (Parent (N2))))
13692 and then Is_Global (Entity (Original_Node (Parent (N2))))
13693 then
13694 Rewrite (Parent (N), New_Copy (Parent (N2)));
13695 Set_Analyzed (Parent (N), False);
13696
13697 else
13698 null;
13699 end if;
13700
13701 -- A selected component may be transformed into a parameterless
13702 -- function call. If the called entity is global, rewrite the node
13703 -- appropriately, i.e. as an extended name for the global entity.
13704
13705 elsif Nkind (Parent (N)) = N_Selected_Component
13706 and then Nkind (Parent (N2)) = N_Function_Call
13707 and then N = Selector_Name (Parent (N))
13708 then
13709 if No (Parameter_Associations (Parent (N2))) then
13710 if Is_Global (Entity (Name (Parent (N2)))) then
13711 Change_Selected_Component_To_Expanded_Name (Parent (N));
13712 Set_Associated_Node (Parent (N), Name (Parent (N2)));
13713 Set_Global_Type (Parent (N), Name (Parent (N2)));
13714 Save_Entity_Descendants (N);
13715
13716 else
13717 Set_Is_Prefixed_Call (Parent (N));
13718 Set_Associated_Node (N, Empty);
13719 Set_Etype (N, Empty);
13720 end if;
13721
13722 -- In Ada 2005, X.F may be a call to a primitive operation,
13723 -- rewritten as F (X). This rewriting will be done again in an
13724 -- instance, so keep the original node. Global entities will be
13725 -- captured as for other constructs. Indicate that this must
13726 -- resolve as a call, to prevent accidental overloading in the
13727 -- instance, if both a component and a primitive operation appear
13728 -- as candidates.
13729
13730 else
13731 Set_Is_Prefixed_Call (Parent (N));
13732 end if;
13733
13734 -- Entity is local. Reset in generic unit, so that node is resolved
13735 -- anew at the point of instantiation.
13736
13737 else
13738 Set_Associated_Node (N, Empty);
13739 Set_Etype (N, Empty);
13740 end if;
13741 end Reset_Entity;
13742
13743 -----------------------------
13744 -- Save_Entity_Descendants --
13745 -----------------------------
13746
13747 procedure Save_Entity_Descendants (N : Node_Id) is
13748 begin
13749 case Nkind (N) is
13750 when N_Binary_Op =>
13751 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
13752 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13753
13754 when N_Unary_Op =>
13755 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13756
13757 when N_Expanded_Name | N_Selected_Component =>
13758 Save_Global_Descendant (Union_Id (Prefix (N)));
13759 Save_Global_Descendant (Union_Id (Selector_Name (N)));
13760
13761 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
13762 null;
13763
13764 when others =>
13765 raise Program_Error;
13766 end case;
13767 end Save_Entity_Descendants;
13768
13769 --------------------------
13770 -- Save_Global_Defaults --
13771 --------------------------
13772
13773 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
13774 Loc : constant Source_Ptr := Sloc (N1);
13775 Assoc2 : constant List_Id := Generic_Associations (N2);
13776 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
13777 Assoc1 : List_Id;
13778 Act1 : Node_Id;
13779 Act2 : Node_Id;
13780 Def : Node_Id;
13781 Ndec : Node_Id;
13782 Subp : Entity_Id;
13783 Actual : Entity_Id;
13784
13785 begin
13786 Assoc1 := Generic_Associations (N1);
13787
13788 if Present (Assoc1) then
13789 Act1 := First (Assoc1);
13790 else
13791 Act1 := Empty;
13792 Set_Generic_Associations (N1, New_List);
13793 Assoc1 := Generic_Associations (N1);
13794 end if;
13795
13796 if Present (Assoc2) then
13797 Act2 := First (Assoc2);
13798 else
13799 return;
13800 end if;
13801
13802 while Present (Act1) and then Present (Act2) loop
13803 Next (Act1);
13804 Next (Act2);
13805 end loop;
13806
13807 -- Find the associations added for default subprograms
13808
13809 if Present (Act2) then
13810 while Nkind (Act2) /= N_Generic_Association
13811 or else No (Entity (Selector_Name (Act2)))
13812 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
13813 loop
13814 Next (Act2);
13815 end loop;
13816
13817 -- Add a similar association if the default is global. The
13818 -- renaming declaration for the actual has been analyzed, and
13819 -- its alias is the program it renames. Link the actual in the
13820 -- original generic tree with the node in the analyzed tree.
13821
13822 while Present (Act2) loop
13823 Subp := Entity (Selector_Name (Act2));
13824 Def := Explicit_Generic_Actual_Parameter (Act2);
13825
13826 -- Following test is defence against rubbish errors
13827
13828 if No (Alias (Subp)) then
13829 return;
13830 end if;
13831
13832 -- Retrieve the resolved actual from the renaming declaration
13833 -- created for the instantiated formal.
13834
13835 Actual := Entity (Name (Parent (Parent (Subp))));
13836 Set_Entity (Def, Actual);
13837 Set_Etype (Def, Etype (Actual));
13838
13839 if Is_Global (Actual) then
13840 Ndec :=
13841 Make_Generic_Association (Loc,
13842 Selector_Name => New_Occurrence_Of (Subp, Loc),
13843 Explicit_Generic_Actual_Parameter =>
13844 New_Occurrence_Of (Actual, Loc));
13845
13846 Set_Associated_Node
13847 (Explicit_Generic_Actual_Parameter (Ndec), Def);
13848
13849 Append (Ndec, Assoc1);
13850
13851 -- If there are other defaults, add a dummy association in case
13852 -- there are other defaulted formals with the same name.
13853
13854 elsif Present (Next (Act2)) then
13855 Ndec :=
13856 Make_Generic_Association (Loc,
13857 Selector_Name => New_Occurrence_Of (Subp, Loc),
13858 Explicit_Generic_Actual_Parameter => Empty);
13859
13860 Append (Ndec, Assoc1);
13861 end if;
13862
13863 Next (Act2);
13864 end loop;
13865 end if;
13866
13867 if Nkind (Name (N1)) = N_Identifier
13868 and then Is_Child_Unit (Gen_Id)
13869 and then Is_Global (Gen_Id)
13870 and then Is_Generic_Unit (Scope (Gen_Id))
13871 and then In_Open_Scopes (Scope (Gen_Id))
13872 then
13873 -- This is an instantiation of a child unit within a sibling, so
13874 -- that the generic parent is in scope. An eventual instance must
13875 -- occur within the scope of an instance of the parent. Make name
13876 -- in instance into an expanded name, to preserve the identifier
13877 -- of the parent, so it can be resolved subsequently.
13878
13879 Rewrite (Name (N2),
13880 Make_Expanded_Name (Loc,
13881 Chars => Chars (Gen_Id),
13882 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13883 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13884 Set_Entity (Name (N2), Gen_Id);
13885
13886 Rewrite (Name (N1),
13887 Make_Expanded_Name (Loc,
13888 Chars => Chars (Gen_Id),
13889 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13890 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13891
13892 Set_Associated_Node (Name (N1), Name (N2));
13893 Set_Associated_Node (Prefix (Name (N1)), Empty);
13894 Set_Associated_Node
13895 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
13896 Set_Etype (Name (N1), Etype (Gen_Id));
13897 end if;
13898
13899 end Save_Global_Defaults;
13900
13901 ----------------------------
13902 -- Save_Global_Descendant --
13903 ----------------------------
13904
13905 procedure Save_Global_Descendant (D : Union_Id) is
13906 N1 : Node_Id;
13907
13908 begin
13909 if D in Node_Range then
13910 if D = Union_Id (Empty) then
13911 null;
13912
13913 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
13914 Save_References (Node_Id (D));
13915 end if;
13916
13917 elsif D in List_Range then
13918 if D = Union_Id (No_List)
13919 or else Is_Empty_List (List_Id (D))
13920 then
13921 null;
13922
13923 else
13924 N1 := First (List_Id (D));
13925 while Present (N1) loop
13926 Save_References (N1);
13927 Next (N1);
13928 end loop;
13929 end if;
13930
13931 -- Element list or other non-node field, nothing to do
13932
13933 else
13934 null;
13935 end if;
13936 end Save_Global_Descendant;
13937
13938 ---------------------
13939 -- Save_References --
13940 ---------------------
13941
13942 -- This is the recursive procedure that does the work once the enclosing
13943 -- generic scope has been established. We have to treat specially a
13944 -- number of node rewritings that are required by semantic processing
13945 -- and which change the kind of nodes in the generic copy: typically
13946 -- constant-folding, replacing an operator node by a string literal, or
13947 -- a selected component by an expanded name. In each of those cases, the
13948 -- transformation is propagated to the generic unit.
13949
13950 procedure Save_References (N : Node_Id) is
13951 Loc : constant Source_Ptr := Sloc (N);
13952
13953 begin
13954 if N = Empty then
13955 null;
13956
13957 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
13958 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13959 Reset_Entity (N);
13960
13961 elsif Nkind (N) = N_Operator_Symbol
13962 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
13963 then
13964 Change_Operator_Symbol_To_String_Literal (N);
13965 end if;
13966
13967 elsif Nkind (N) in N_Op then
13968 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13969 if Nkind (N) = N_Op_Concat then
13970 Set_Is_Component_Left_Opnd (N,
13971 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13972
13973 Set_Is_Component_Right_Opnd (N,
13974 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13975 end if;
13976
13977 Reset_Entity (N);
13978
13979 else
13980 -- Node may be transformed into call to a user-defined operator
13981
13982 N2 := Get_Associated_Node (N);
13983
13984 if Nkind (N2) = N_Function_Call then
13985 E := Entity (Name (N2));
13986
13987 if Present (E)
13988 and then Is_Global (E)
13989 then
13990 Set_Etype (N, Etype (N2));
13991 else
13992 Set_Associated_Node (N, Empty);
13993 Set_Etype (N, Empty);
13994 end if;
13995
13996 elsif Nkind_In (N2, N_Integer_Literal,
13997 N_Real_Literal,
13998 N_String_Literal)
13999 then
14000 if Present (Original_Node (N2))
14001 and then Nkind (Original_Node (N2)) = Nkind (N)
14002 then
14003
14004 -- Operation was constant-folded. Whenever possible,
14005 -- recover semantic information from unfolded node,
14006 -- for ASIS use.
14007
14008 Set_Associated_Node (N, Original_Node (N2));
14009
14010 if Nkind (N) = N_Op_Concat then
14011 Set_Is_Component_Left_Opnd (N,
14012 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14013 Set_Is_Component_Right_Opnd (N,
14014 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14015 end if;
14016
14017 Reset_Entity (N);
14018
14019 else
14020 -- If original node is already modified, propagate
14021 -- constant-folding to template.
14022
14023 Rewrite (N, New_Copy (N2));
14024 Set_Analyzed (N, False);
14025 end if;
14026
14027 elsif Nkind (N2) = N_Identifier
14028 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14029 then
14030 -- Same if call was folded into a literal, but in this case
14031 -- retain the entity to avoid spurious ambiguities if it is
14032 -- overloaded at the point of instantiation or inlining.
14033
14034 Rewrite (N, New_Copy (N2));
14035 Set_Analyzed (N, False);
14036 end if;
14037 end if;
14038
14039 -- Complete operands check if node has not been constant-folded
14040
14041 if Nkind (N) in N_Op then
14042 Save_Entity_Descendants (N);
14043 end if;
14044
14045 elsif Nkind (N) = N_Identifier then
14046 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14047
14048 -- If this is a discriminant reference, always save it. It is
14049 -- used in the instance to find the corresponding discriminant
14050 -- positionally rather than by name.
14051
14052 Set_Original_Discriminant
14053 (N, Original_Discriminant (Get_Associated_Node (N)));
14054 Reset_Entity (N);
14055
14056 else
14057 N2 := Get_Associated_Node (N);
14058
14059 if Nkind (N2) = N_Function_Call then
14060 E := Entity (Name (N2));
14061
14062 -- Name resolves to a call to parameterless function. If
14063 -- original entity is global, mark node as resolved.
14064
14065 if Present (E)
14066 and then Is_Global (E)
14067 then
14068 Set_Etype (N, Etype (N2));
14069 else
14070 Set_Associated_Node (N, Empty);
14071 Set_Etype (N, Empty);
14072 end if;
14073
14074 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14075 and then Is_Entity_Name (Original_Node (N2))
14076 then
14077 -- Name resolves to named number that is constant-folded,
14078 -- We must preserve the original name for ASIS use, and
14079 -- undo the constant-folding, which will be repeated in
14080 -- each instance.
14081
14082 Set_Associated_Node (N, Original_Node (N2));
14083 Reset_Entity (N);
14084
14085 elsif Nkind (N2) = N_String_Literal then
14086
14087 -- Name resolves to string literal. Perform the same
14088 -- replacement in generic.
14089
14090 Rewrite (N, New_Copy (N2));
14091
14092 elsif Nkind (N2) = N_Explicit_Dereference then
14093
14094 -- An identifier is rewritten as a dereference if it is the
14095 -- prefix in an implicit dereference (call or attribute).
14096 -- The analysis of an instantiation will expand the node
14097 -- again, so we preserve the original tree but link it to
14098 -- the resolved entity in case it is global.
14099
14100 if Is_Entity_Name (Prefix (N2))
14101 and then Present (Entity (Prefix (N2)))
14102 and then Is_Global (Entity (Prefix (N2)))
14103 then
14104 Set_Associated_Node (N, Prefix (N2));
14105
14106 elsif Nkind (Prefix (N2)) = N_Function_Call
14107 and then Is_Global (Entity (Name (Prefix (N2))))
14108 then
14109 Rewrite (N,
14110 Make_Explicit_Dereference (Loc,
14111 Prefix => Make_Function_Call (Loc,
14112 Name =>
14113 New_Occurrence_Of (Entity (Name (Prefix (N2))),
14114 Loc))));
14115
14116 else
14117 Set_Associated_Node (N, Empty);
14118 Set_Etype (N, Empty);
14119 end if;
14120
14121 -- The subtype mark of a nominally unconstrained object is
14122 -- rewritten as a subtype indication using the bounds of the
14123 -- expression. Recover the original subtype mark.
14124
14125 elsif Nkind (N2) = N_Subtype_Indication
14126 and then Is_Entity_Name (Original_Node (N2))
14127 then
14128 Set_Associated_Node (N, Original_Node (N2));
14129 Reset_Entity (N);
14130
14131 else
14132 null;
14133 end if;
14134 end if;
14135
14136 elsif Nkind (N) in N_Entity then
14137 null;
14138
14139 else
14140 declare
14141 Qual : Node_Id := Empty;
14142 Typ : Entity_Id := Empty;
14143 Nam : Node_Id;
14144
14145 use Atree.Unchecked_Access;
14146 -- This code section is part of implementing an untyped tree
14147 -- traversal, so it needs direct access to node fields.
14148
14149 begin
14150 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14151 N2 := Get_Associated_Node (N);
14152
14153 if No (N2) then
14154 Typ := Empty;
14155 else
14156 Typ := Etype (N2);
14157
14158 -- In an instance within a generic, use the name of the
14159 -- actual and not the original generic parameter. If the
14160 -- actual is global in the current generic it must be
14161 -- preserved for its instantiation.
14162
14163 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14164 and then
14165 Present (Generic_Parent_Type (Parent (Typ)))
14166 then
14167 Typ := Base_Type (Typ);
14168 Set_Etype (N2, Typ);
14169 end if;
14170 end if;
14171
14172 if No (N2)
14173 or else No (Typ)
14174 or else not Is_Global (Typ)
14175 then
14176 Set_Associated_Node (N, Empty);
14177
14178 -- If the aggregate is an actual in a call, it has been
14179 -- resolved in the current context, to some local type.
14180 -- The enclosing call may have been disambiguated by the
14181 -- aggregate, and this disambiguation might fail at
14182 -- instantiation time because the type to which the
14183 -- aggregate did resolve is not preserved. In order to
14184 -- preserve some of this information, we wrap the
14185 -- aggregate in a qualified expression, using the id of
14186 -- its type. For further disambiguation we qualify the
14187 -- type name with its scope (if visible) because both
14188 -- id's will have corresponding entities in an instance.
14189 -- This resolves most of the problems with missing type
14190 -- information on aggregates in instances.
14191
14192 if Nkind (N2) = Nkind (N)
14193 and then Nkind (Parent (N2)) in N_Subprogram_Call
14194 and then Comes_From_Source (Typ)
14195 then
14196 if Is_Immediately_Visible (Scope (Typ)) then
14197 Nam := Make_Selected_Component (Loc,
14198 Prefix =>
14199 Make_Identifier (Loc, Chars (Scope (Typ))),
14200 Selector_Name =>
14201 Make_Identifier (Loc, Chars (Typ)));
14202 else
14203 Nam := Make_Identifier (Loc, Chars (Typ));
14204 end if;
14205
14206 Qual :=
14207 Make_Qualified_Expression (Loc,
14208 Subtype_Mark => Nam,
14209 Expression => Relocate_Node (N));
14210 end if;
14211 end if;
14212
14213 Save_Global_Descendant (Field1 (N));
14214 Save_Global_Descendant (Field2 (N));
14215 Save_Global_Descendant (Field3 (N));
14216 Save_Global_Descendant (Field5 (N));
14217
14218 if Present (Qual) then
14219 Rewrite (N, Qual);
14220 end if;
14221
14222 -- All other cases than aggregates
14223
14224 else
14225 Save_Global_Descendant (Field1 (N));
14226 Save_Global_Descendant (Field2 (N));
14227 Save_Global_Descendant (Field3 (N));
14228 Save_Global_Descendant (Field4 (N));
14229 Save_Global_Descendant (Field5 (N));
14230 end if;
14231 end;
14232 end if;
14233
14234 -- If a node has aspects, references within their expressions must
14235 -- be saved separately, given they are not directly in the tree.
14236
14237 if Has_Aspects (N) then
14238 declare
14239 Aspect : Node_Id;
14240
14241 begin
14242 Aspect := First (Aspect_Specifications (N));
14243 while Present (Aspect) loop
14244 if Present (Expression (Aspect)) then
14245 Save_Global_References (Expression (Aspect));
14246 end if;
14247
14248 Next (Aspect);
14249 end loop;
14250 end;
14251 end if;
14252 end Save_References;
14253
14254 -- Start of processing for Save_Global_References
14255
14256 begin
14257 Gen_Scope := Current_Scope;
14258
14259 -- If the generic unit is a child unit, references to entities in the
14260 -- parent are treated as local, because they will be resolved anew in
14261 -- the context of the instance of the parent.
14262
14263 while Is_Child_Unit (Gen_Scope)
14264 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14265 loop
14266 Gen_Scope := Scope (Gen_Scope);
14267 end loop;
14268
14269 Save_References (N);
14270 end Save_Global_References;
14271
14272 --------------------------------------
14273 -- Set_Copied_Sloc_For_Inlined_Body --
14274 --------------------------------------
14275
14276 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14277 begin
14278 Create_Instantiation_Source (N, E, True, S_Adjustment);
14279 end Set_Copied_Sloc_For_Inlined_Body;
14280
14281 ---------------------
14282 -- Set_Instance_Of --
14283 ---------------------
14284
14285 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14286 begin
14287 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14288 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14289 Generic_Renamings.Increment_Last;
14290 end Set_Instance_Of;
14291
14292 --------------------
14293 -- Set_Next_Assoc --
14294 --------------------
14295
14296 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14297 begin
14298 Generic_Renamings.Table (E).Next_In_HTable := Next;
14299 end Set_Next_Assoc;
14300
14301 -------------------
14302 -- Start_Generic --
14303 -------------------
14304
14305 procedure Start_Generic is
14306 begin
14307 -- ??? More things could be factored out in this routine.
14308 -- Should probably be done at a later stage.
14309
14310 Generic_Flags.Append (Inside_A_Generic);
14311 Inside_A_Generic := True;
14312
14313 Expander_Mode_Save_And_Set (False);
14314 end Start_Generic;
14315
14316 ----------------------
14317 -- Set_Instance_Env --
14318 ----------------------
14319
14320 procedure Set_Instance_Env
14321 (Gen_Unit : Entity_Id;
14322 Act_Unit : Entity_Id)
14323 is
14324 Assertion_Status : constant Boolean := Assertions_Enabled;
14325 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14326 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14327
14328 begin
14329 -- Regardless of the current mode, predefined units are analyzed in the
14330 -- most current Ada mode, and earlier version Ada checks do not apply
14331 -- to predefined units. Nothing needs to be done for non-internal units.
14332 -- These are always analyzed in the current mode.
14333
14334 if Is_Internal_File_Name
14335 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14336 Renamings_Included => True)
14337 then
14338 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14339
14340 -- In Ada2012 we may want to enable assertions in an instance of a
14341 -- predefined unit, in which case we need to preserve the current
14342 -- setting for the Assertions_Enabled flag. This will become more
14343 -- critical when pre/postconditions are added to predefined units,
14344 -- as is already the case for some numeric libraries.
14345
14346 if Ada_Version >= Ada_2012 then
14347 Assertions_Enabled := Assertion_Status;
14348 end if;
14349
14350 -- SPARK_Mode for an instance is the one applicable at the point of
14351 -- instantiation.
14352
14353 SPARK_Mode := Save_SPARK_Mode;
14354 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14355 end if;
14356
14357 Current_Instantiated_Parent :=
14358 (Gen_Id => Gen_Unit,
14359 Act_Id => Act_Unit,
14360 Next_In_HTable => Assoc_Null);
14361 end Set_Instance_Env;
14362
14363 -----------------
14364 -- Switch_View --
14365 -----------------
14366
14367 procedure Switch_View (T : Entity_Id) is
14368 BT : constant Entity_Id := Base_Type (T);
14369 Priv_Elmt : Elmt_Id := No_Elmt;
14370 Priv_Sub : Entity_Id;
14371
14372 begin
14373 -- T may be private but its base type may have been exchanged through
14374 -- some other occurrence, in which case there is nothing to switch
14375 -- besides T itself. Note that a private dependent subtype of a private
14376 -- type might not have been switched even if the base type has been,
14377 -- because of the last branch of Check_Private_View (see comment there).
14378
14379 if not Is_Private_Type (BT) then
14380 Prepend_Elmt (Full_View (T), Exchanged_Views);
14381 Exchange_Declarations (T);
14382 return;
14383 end if;
14384
14385 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14386
14387 if Present (Full_View (BT)) then
14388 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14389 Exchange_Declarations (BT);
14390 end if;
14391
14392 while Present (Priv_Elmt) loop
14393 Priv_Sub := (Node (Priv_Elmt));
14394
14395 -- We avoid flipping the subtype if the Etype of its full view is
14396 -- private because this would result in a malformed subtype. This
14397 -- occurs when the Etype of the subtype full view is the full view of
14398 -- the base type (and since the base types were just switched, the
14399 -- subtype is pointing to the wrong view). This is currently the case
14400 -- for tagged record types, access types (maybe more?) and needs to
14401 -- be resolved. ???
14402
14403 if Present (Full_View (Priv_Sub))
14404 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14405 then
14406 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14407 Exchange_Declarations (Priv_Sub);
14408 end if;
14409
14410 Next_Elmt (Priv_Elmt);
14411 end loop;
14412 end Switch_View;
14413
14414 -----------------
14415 -- True_Parent --
14416 -----------------
14417
14418 function True_Parent (N : Node_Id) return Node_Id is
14419 begin
14420 if Nkind (Parent (N)) = N_Subunit then
14421 return Parent (Corresponding_Stub (Parent (N)));
14422 else
14423 return Parent (N);
14424 end if;
14425 end True_Parent;
14426
14427 -----------------------------
14428 -- Valid_Default_Attribute --
14429 -----------------------------
14430
14431 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
14432 Attr_Id : constant Attribute_Id :=
14433 Get_Attribute_Id (Attribute_Name (Def));
14434 T : constant Entity_Id := Entity (Prefix (Def));
14435 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
14436 F : Entity_Id;
14437 Num_F : Int;
14438 OK : Boolean;
14439
14440 begin
14441 if No (T)
14442 or else T = Any_Id
14443 then
14444 return;
14445 end if;
14446
14447 Num_F := 0;
14448 F := First_Formal (Nam);
14449 while Present (F) loop
14450 Num_F := Num_F + 1;
14451 Next_Formal (F);
14452 end loop;
14453
14454 case Attr_Id is
14455 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
14456 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
14457 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
14458 Attribute_Unbiased_Rounding =>
14459 OK := Is_Fun
14460 and then Num_F = 1
14461 and then Is_Floating_Point_Type (T);
14462
14463 when Attribute_Image | Attribute_Pred | Attribute_Succ |
14464 Attribute_Value | Attribute_Wide_Image |
14465 Attribute_Wide_Value =>
14466 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
14467
14468 when Attribute_Max | Attribute_Min =>
14469 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
14470
14471 when Attribute_Input =>
14472 OK := (Is_Fun and then Num_F = 1);
14473
14474 when Attribute_Output | Attribute_Read | Attribute_Write =>
14475 OK := (not Is_Fun and then Num_F = 2);
14476
14477 when others =>
14478 OK := False;
14479 end case;
14480
14481 if not OK then
14482 Error_Msg_N ("attribute reference has wrong profile for subprogram",
14483 Def);
14484 end if;
14485 end Valid_Default_Attribute;
14486
14487 end Sem_Ch12;