2014-08-01 Ed Schonberg <schonberg@adacore.com>
[gcc.git] / gcc / ada / sem_ch12.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Disp; use Exp_Disp;
33 with Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Itypes; use Itypes;
37 with Lib; use Lib;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Nlists; use Nlists;
41 with Namet; use Namet;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Rident; use Rident;
45 with Restrict; use Restrict;
46 with Rtsfind; use Rtsfind;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch7; use Sem_Ch7;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch10; use Sem_Ch10;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Elab; use Sem_Elab;
59 with Sem_Elim; use Sem_Elim;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Prag; use Sem_Prag;
62 with Sem_Res; use Sem_Res;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Stand; use Stand;
67 with Sinfo; use Sinfo;
68 with Sinfo.CN; use Sinfo.CN;
69 with Sinput; use Sinput;
70 with Sinput.L; use Sinput.L;
71 with Snames; use Snames;
72 with Stringt; use Stringt;
73 with Uname; use Uname;
74 with Table;
75 with Tbuild; use Tbuild;
76 with Uintp; use Uintp;
77 with Urealp; use Urealp;
78 with Warnsw; use Warnsw;
79
80 with GNAT.HTable;
81
82 package body Sem_Ch12 is
83
84 ----------------------------------------------------------
85 -- Implementation of Generic Analysis and Instantiation --
86 ----------------------------------------------------------
87
88 -- GNAT implements generics by macro expansion. No attempt is made to share
89 -- generic instantiations (for now). Analysis of a generic definition does
90 -- not perform any expansion action, but the expander must be called on the
91 -- tree for each instantiation, because the expansion may of course depend
92 -- on the generic actuals. All of this is best achieved as follows:
93 --
94 -- a) Semantic analysis of a generic unit is performed on a copy of the
95 -- tree for the generic unit. All tree modifications that follow analysis
96 -- do not affect the original tree. Links are kept between the original
97 -- tree and the copy, in order to recognize non-local references within
98 -- the generic, and propagate them to each instance (recall that name
99 -- resolution is done on the generic declaration: generics are not really
100 -- macros). This is summarized in the following diagram:
101
102 -- .-----------. .----------.
103 -- | semantic |<--------------| generic |
104 -- | copy | | unit |
105 -- | |==============>| |
106 -- |___________| global |__________|
107 -- references | | |
108 -- | | |
109 -- .-----|--|.
110 -- | .-----|---.
111 -- | | .----------.
112 -- | | | generic |
113 -- |__| | |
114 -- |__| instance |
115 -- |__________|
116
117 -- b) Each instantiation copies the original tree, and inserts into it a
118 -- series of declarations that describe the mapping between generic formals
119 -- and actuals. For example, a generic In OUT parameter is an object
120 -- renaming of the corresponding actual, etc. Generic IN parameters are
121 -- constant declarations.
122
123 -- c) In order to give the right visibility for these renamings, we use
124 -- a different scheme for package and subprogram instantiations. For
125 -- packages, the list of renamings is inserted into the package
126 -- specification, before the visible declarations of the package. The
127 -- renamings are analyzed before any of the text of the instance, and are
128 -- thus visible at the right place. Furthermore, outside of the instance,
129 -- the generic parameters are visible and denote their corresponding
130 -- actuals.
131
132 -- For subprograms, we create a container package to hold the renamings
133 -- and the subprogram instance itself. Analysis of the package makes the
134 -- renaming declarations visible to the subprogram. After analyzing the
135 -- package, the defining entity for the subprogram is touched-up so that
136 -- it appears declared in the current scope, and not inside the container
137 -- package.
138
139 -- If the instantiation is a compilation unit, the container package is
140 -- given the same name as the subprogram instance. This ensures that
141 -- the elaboration procedure called by the binder, using the compilation
142 -- unit name, calls in fact the elaboration procedure for the package.
143
144 -- Not surprisingly, private types complicate this approach. By saving in
145 -- the original generic object the non-local references, we guarantee that
146 -- the proper entities are referenced at the point of instantiation.
147 -- However, for private types, this by itself does not insure that the
148 -- proper VIEW of the entity is used (the full type may be visible at the
149 -- point of generic definition, but not at instantiation, or vice-versa).
150 -- In order to reference the proper view, we special-case any reference
151 -- to private types in the generic object, by saving both views, one in
152 -- the generic and one in the semantic copy. At time of instantiation, we
153 -- check whether the two views are consistent, and exchange declarations if
154 -- necessary, in order to restore the correct visibility. Similarly, if
155 -- the instance view is private when the generic view was not, we perform
156 -- the exchange. After completing the instantiation, we restore the
157 -- current visibility. The flag Has_Private_View marks identifiers in the
158 -- the generic unit that require checking.
159
160 -- Visibility within nested generic units requires special handling.
161 -- Consider the following scheme:
162
163 -- type Global is ... -- outside of generic unit.
164 -- generic ...
165 -- package Outer is
166 -- ...
167 -- type Semi_Global is ... -- global to inner.
168
169 -- generic ... -- 1
170 -- procedure inner (X1 : Global; X2 : Semi_Global);
171
172 -- procedure in2 is new inner (...); -- 4
173 -- end Outer;
174
175 -- package New_Outer is new Outer (...); -- 2
176 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
177
178 -- The semantic analysis of Outer captures all occurrences of Global.
179 -- The semantic analysis of Inner (at 1) captures both occurrences of
180 -- Global and Semi_Global.
181
182 -- At point 2 (instantiation of Outer), we also produce a generic copy
183 -- of Inner, even though Inner is, at that point, not being instantiated.
184 -- (This is just part of the semantic analysis of New_Outer).
185
186 -- Critically, references to Global within Inner must be preserved, while
187 -- references to Semi_Global should not preserved, because they must now
188 -- resolve to an entity within New_Outer. To distinguish between these, we
189 -- use a global variable, Current_Instantiated_Parent, which is set when
190 -- performing a generic copy during instantiation (at 2). This variable is
191 -- used when performing a generic copy that is not an instantiation, but
192 -- that is nested within one, as the occurrence of 1 within 2. The analysis
193 -- of a nested generic only preserves references that are global to the
194 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
195 -- determine whether a reference is external to the given parent.
196
197 -- The instantiation at point 3 requires no special treatment. The method
198 -- works as well for further nestings of generic units, but of course the
199 -- variable Current_Instantiated_Parent must be stacked because nested
200 -- instantiations can occur, e.g. the occurrence of 4 within 2.
201
202 -- The instantiation of package and subprogram bodies is handled in a
203 -- similar manner, except that it is delayed until after semantic
204 -- analysis is complete. In this fashion complex cross-dependencies
205 -- between several package declarations and bodies containing generics
206 -- can be compiled which otherwise would diagnose spurious circularities.
207
208 -- For example, it is possible to compile two packages A and B that
209 -- have the following structure:
210
211 -- package A is package B is
212 -- generic ... generic ...
213 -- package G_A is package G_B is
214
215 -- with B; with A;
216 -- package body A is package body B is
217 -- package N_B is new G_B (..) package N_A is new G_A (..)
218
219 -- The table Pending_Instantiations in package Inline is used to keep
220 -- track of body instantiations that are delayed in this manner. Inline
221 -- handles the actual calls to do the body instantiations. This activity
222 -- is part of Inline, since the processing occurs at the same point, and
223 -- for essentially the same reason, as the handling of inlined routines.
224
225 ----------------------------------------------
226 -- Detection of Instantiation Circularities --
227 ----------------------------------------------
228
229 -- If we have a chain of instantiations that is circular, this is static
230 -- error which must be detected at compile time. The detection of these
231 -- circularities is carried out at the point that we insert a generic
232 -- instance spec or body. If there is a circularity, then the analysis of
233 -- the offending spec or body will eventually result in trying to load the
234 -- same unit again, and we detect this problem as we analyze the package
235 -- instantiation for the second time.
236
237 -- At least in some cases after we have detected the circularity, we get
238 -- into trouble if we try to keep going. The following flag is set if a
239 -- circularity is detected, and used to abandon compilation after the
240 -- messages have been posted.
241
242 Circularity_Detected : Boolean := False;
243 -- This should really be reset on encountering a new main unit, but in
244 -- practice we are not using multiple main units so it is not critical.
245
246 --------------------------------------------------
247 -- Formal packages and partial parameterization --
248 --------------------------------------------------
249
250 -- When compiling a generic, a formal package is a local instantiation. If
251 -- declared with a box, its generic formals are visible in the enclosing
252 -- generic. If declared with a partial list of actuals, those actuals that
253 -- are defaulted (covered by an Others clause, or given an explicit box
254 -- initialization) are also visible in the enclosing generic, while those
255 -- that have a corresponding actual are not.
256
257 -- In our source model of instantiation, the same visibility must be
258 -- present in the spec and body of an instance: the names of the formals
259 -- that are defaulted must be made visible within the instance, and made
260 -- invisible (hidden) after the instantiation is complete, so that they
261 -- are not accessible outside of the instance.
262
263 -- In a generic, a formal package is treated like a special instantiation.
264 -- Our Ada 95 compiler handled formals with and without box in different
265 -- ways. With partial parameterization, we use a single model for both.
266 -- We create a package declaration that consists of the specification of
267 -- the generic package, and a set of declarations that map the actuals
268 -- into local renamings, just as we do for bona fide instantiations. For
269 -- defaulted parameters and formals with a box, we copy directly the
270 -- declarations of the formal into this local package. The result is a
271 -- a package whose visible declarations may include generic formals. This
272 -- package is only used for type checking and visibility analysis, and
273 -- never reaches the back-end, so it can freely violate the placement
274 -- rules for generic formal declarations.
275
276 -- The list of declarations (renamings and copies of formals) is built
277 -- by Analyze_Associations, just as for regular instantiations.
278
279 -- At the point of instantiation, conformance checking must be applied only
280 -- to those parameters that were specified in the formal. We perform this
281 -- checking by creating another internal instantiation, this one including
282 -- only the renamings and the formals (the rest of the package spec is not
283 -- relevant to conformance checking). We can then traverse two lists: the
284 -- list of actuals in the instance that corresponds to the formal package,
285 -- and the list of actuals produced for this bogus instantiation. We apply
286 -- the conformance rules to those actuals that are not defaulted (i.e.
287 -- which still appear as generic formals.
288
289 -- When we compile an instance body we must make the right parameters
290 -- visible again. The predicate Is_Generic_Formal indicates which of the
291 -- formals should have its Is_Hidden flag reset.
292
293 -----------------------
294 -- Local subprograms --
295 -----------------------
296
297 procedure Abandon_Instantiation (N : Node_Id);
298 pragma No_Return (Abandon_Instantiation);
299 -- Posts an error message "instantiation abandoned" at the indicated node
300 -- and then raises the exception Instantiation_Error to do it.
301
302 procedure Analyze_Formal_Array_Type
303 (T : in out Entity_Id;
304 Def : Node_Id);
305 -- A formal array type is treated like an array type declaration, and
306 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
307 -- in-out, because in the case of an anonymous type the entity is
308 -- actually created in the procedure.
309
310 -- The following procedures treat other kinds of formal parameters
311
312 procedure Analyze_Formal_Derived_Interface_Type
313 (N : Node_Id;
314 T : Entity_Id;
315 Def : Node_Id);
316
317 procedure Analyze_Formal_Derived_Type
318 (N : Node_Id;
319 T : Entity_Id;
320 Def : Node_Id);
321
322 procedure Analyze_Formal_Interface_Type
323 (N : Node_Id;
324 T : Entity_Id;
325 Def : Node_Id);
326
327 -- The following subprograms create abbreviated declarations for formal
328 -- scalar types. We introduce an anonymous base of the proper class for
329 -- each of them, and define the formals as constrained first subtypes of
330 -- their bases. The bounds are expressions that are non-static in the
331 -- generic.
332
333 procedure Analyze_Formal_Decimal_Fixed_Point_Type
334 (T : Entity_Id; Def : Node_Id);
335 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
336 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
337 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
339 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
340 (T : Entity_Id; Def : Node_Id);
341
342 procedure Analyze_Formal_Private_Type
343 (N : Node_Id;
344 T : Entity_Id;
345 Def : Node_Id);
346 -- Creates a new private type, which does not require completion
347
348 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
349 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
350
351 procedure Analyze_Generic_Formal_Part (N : Node_Id);
352 -- Analyze generic formal part
353
354 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
355 -- Create a new access type with the given designated type
356
357 function Analyze_Associations
358 (I_Node : Node_Id;
359 Formals : List_Id;
360 F_Copy : List_Id) return List_Id;
361 -- At instantiation time, build the list of associations between formals
362 -- and actuals. Each association becomes a renaming declaration for the
363 -- formal entity. F_Copy is the analyzed list of formals in the generic
364 -- copy. It is used to apply legality checks to the actuals. I_Node is the
365 -- instantiation node itself.
366
367 procedure Analyze_Subprogram_Instantiation
368 (N : Node_Id;
369 K : Entity_Kind);
370
371 procedure Build_Instance_Compilation_Unit_Nodes
372 (N : Node_Id;
373 Act_Body : Node_Id;
374 Act_Decl : Node_Id);
375 -- This procedure is used in the case where the generic instance of a
376 -- subprogram body or package body is a library unit. In this case, the
377 -- original library unit node for the generic instantiation must be
378 -- replaced by the resulting generic body, and a link made to a new
379 -- compilation unit node for the generic declaration. The argument N is
380 -- the original generic instantiation. Act_Body and Act_Decl are the body
381 -- and declaration of the instance (either package body and declaration
382 -- nodes or subprogram body and declaration nodes depending on the case).
383 -- On return, the node N has been rewritten with the actual body.
384
385 procedure Check_Access_Definition (N : Node_Id);
386 -- Subsidiary routine to null exclusion processing. Perform an assertion
387 -- check on Ada version and the presence of an access definition in N.
388
389 procedure Check_Formal_Packages (P_Id : Entity_Id);
390 -- Apply the following to all formal packages in generic associations
391
392 procedure Check_Formal_Package_Instance
393 (Formal_Pack : Entity_Id;
394 Actual_Pack : Entity_Id);
395 -- Verify that the actuals of the actual instance match the actuals of
396 -- the template for a formal package that is not declared with a box.
397
398 procedure Check_Forward_Instantiation (Decl : Node_Id);
399 -- If the generic is a local entity and the corresponding body has not
400 -- been seen yet, flag enclosing packages to indicate that it will be
401 -- elaborated after the generic body. Subprograms declared in the same
402 -- package cannot be inlined by the front-end because front-end inlining
403 -- requires a strict linear order of elaboration.
404
405 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
406 -- Check if some association between formals and actuals requires to make
407 -- visible primitives of a tagged type, and make those primitives visible.
408 -- Return the list of primitives whose visibility is modified (to restore
409 -- their visibility later through Restore_Hidden_Primitives). If no
410 -- candidate is found then return No_Elist.
411
412 procedure Check_Hidden_Child_Unit
413 (N : Node_Id;
414 Gen_Unit : Entity_Id;
415 Act_Decl_Id : Entity_Id);
416 -- If the generic unit is an implicit child instance within a parent
417 -- instance, we need to make an explicit test that it is not hidden by
418 -- a child instance of the same name and parent.
419
420 procedure Check_Generic_Actuals
421 (Instance : Entity_Id;
422 Is_Formal_Box : Boolean);
423 -- Similar to previous one. Check the actuals in the instantiation,
424 -- whose views can change between the point of instantiation and the point
425 -- of instantiation of the body. In addition, mark the generic renamings
426 -- as generic actuals, so that they are not compatible with other actuals.
427 -- Recurse on an actual that is a formal package whose declaration has
428 -- a box.
429
430 function Contains_Instance_Of
431 (Inner : Entity_Id;
432 Outer : Entity_Id;
433 N : Node_Id) return Boolean;
434 -- Inner is instantiated within the generic Outer. Check whether Inner
435 -- directly or indirectly contains an instance of Outer or of one of its
436 -- parents, in the case of a subunit. Each generic unit holds a list of
437 -- the entities instantiated within (at any depth). This procedure
438 -- determines whether the set of such lists contains a cycle, i.e. an
439 -- illegal circular instantiation.
440
441 function Denotes_Formal_Package
442 (Pack : Entity_Id;
443 On_Exit : Boolean := False;
444 Instance : Entity_Id := Empty) return Boolean;
445 -- Returns True if E is a formal package of an enclosing generic, or
446 -- the actual for such a formal in an enclosing instantiation. If such
447 -- a package is used as a formal in an nested generic, or as an actual
448 -- in a nested instantiation, the visibility of ITS formals should not
449 -- be modified. When called from within Restore_Private_Views, the flag
450 -- On_Exit is true, to indicate that the search for a possible enclosing
451 -- instance should ignore the current one. In that case Instance denotes
452 -- the declaration for which this is an actual. This declaration may be
453 -- an instantiation in the source, or the internal instantiation that
454 -- corresponds to the actual for a formal package.
455
456 function Earlier (N1, N2 : Node_Id) return Boolean;
457 -- Yields True if N1 and N2 appear in the same compilation unit,
458 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
459 -- traversal of the tree for the unit. Used to determine the placement
460 -- of freeze nodes for instance bodies that may depend on other instances.
461
462 function Find_Actual_Type
463 (Typ : Entity_Id;
464 Gen_Type : Entity_Id) return Entity_Id;
465 -- When validating the actual types of a child instance, check whether
466 -- the formal is a formal type of the parent unit, and retrieve the current
467 -- actual for it. Typ is the entity in the analyzed formal type declaration
468 -- (component or index type of an array type, or designated type of an
469 -- access formal) and Gen_Type is the enclosing analyzed formal array
470 -- or access type. The desired actual may be a formal of a parent, or may
471 -- be declared in a formal package of a parent. In both cases it is a
472 -- generic actual type because it appears within a visible instance.
473 -- Finally, it may be declared in a parent unit without being a formal
474 -- of that unit, in which case it must be retrieved by visibility.
475 -- Ambiguities may still arise if two homonyms are declared in two formal
476 -- packages, and the prefix of the formal type may be needed to resolve
477 -- the ambiguity in the instance ???
478
479 function In_Same_Declarative_Part
480 (F_Node : Node_Id;
481 Inst : Node_Id) return Boolean;
482 -- True if the instantiation Inst and the given freeze_node F_Node appear
483 -- within the same declarative part, ignoring subunits, but with no inter-
484 -- vening subprograms or concurrent units. Used to find the proper plave
485 -- for the freeze node of an instance, when the generic is declared in a
486 -- previous instance. If predicate is true, the freeze node of the instance
487 -- can be placed after the freeze node of the previous instance, Otherwise
488 -- it has to be placed at the end of the current declarative part.
489
490 function In_Main_Context (E : Entity_Id) return Boolean;
491 -- Check whether an instantiation is in the context of the main unit.
492 -- Used to determine whether its body should be elaborated to allow
493 -- front-end inlining.
494
495 procedure Set_Instance_Env
496 (Gen_Unit : Entity_Id;
497 Act_Unit : Entity_Id);
498 -- Save current instance on saved environment, to be used to determine
499 -- the global status of entities in nested instances. Part of Save_Env.
500 -- called after verifying that the generic unit is legal for the instance,
501 -- The procedure also examines whether the generic unit is a predefined
502 -- unit, in order to set configuration switches accordingly. As a result
503 -- the procedure must be called after analyzing and freezing the actuals.
504
505 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
506 -- Associate analyzed generic parameter with corresponding
507 -- instance. Used for semantic checks at instantiation time.
508
509 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
510 -- Traverse the Exchanged_Views list to see if a type was private
511 -- and has already been flipped during this phase of instantiation.
512
513 procedure Hide_Current_Scope;
514 -- When instantiating a generic child unit, the parent context must be
515 -- present, but the instance and all entities that may be generated
516 -- must be inserted in the current scope. We leave the current scope
517 -- on the stack, but make its entities invisible to avoid visibility
518 -- problems. This is reversed at the end of the instantiation. This is
519 -- not done for the instantiation of the bodies, which only require the
520 -- instances of the generic parents to be in scope.
521
522 procedure Install_Body
523 (Act_Body : Node_Id;
524 N : Node_Id;
525 Gen_Body : Node_Id;
526 Gen_Decl : Node_Id);
527 -- If the instantiation happens textually before the body of the generic,
528 -- the instantiation of the body must be analyzed after the generic body,
529 -- and not at the point of instantiation. Such early instantiations can
530 -- happen if the generic and the instance appear in a package declaration
531 -- because the generic body can only appear in the corresponding package
532 -- body. Early instantiations can also appear if generic, instance and
533 -- body are all in the declarative part of a subprogram or entry. Entities
534 -- of packages that are early instantiations are delayed, and their freeze
535 -- node appears after the generic body.
536
537 procedure Insert_Freeze_Node_For_Instance
538 (N : Node_Id;
539 F_Node : Node_Id);
540 -- N denotes a package or a subprogram instantiation and F_Node is the
541 -- associated freeze node. Insert the freeze node before the first source
542 -- body which follows immediately after N. If no such body is found, the
543 -- freeze node is inserted at the end of the declarative region which
544 -- contains N.
545
546 procedure Freeze_Subprogram_Body
547 (Inst_Node : Node_Id;
548 Gen_Body : Node_Id;
549 Pack_Id : Entity_Id);
550 -- The generic body may appear textually after the instance, including
551 -- in the proper body of a stub, or within a different package instance.
552 -- Given that the instance can only be elaborated after the generic, we
553 -- place freeze_nodes for the instance and/or for packages that may enclose
554 -- the instance and the generic, so that the back-end can establish the
555 -- proper order of elaboration.
556
557 procedure Init_Env;
558 -- Establish environment for subsequent instantiation. Separated from
559 -- Save_Env because data-structures for visibility handling must be
560 -- initialized before call to Check_Generic_Child_Unit.
561
562 procedure Install_Formal_Packages (Par : Entity_Id);
563 -- Install the visible part of any formal of the parent that is a formal
564 -- package. Note that for the case of a formal package with a box, this
565 -- includes the formal part of the formal package (12.7(10/2)).
566
567 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
568 -- When compiling an instance of a child unit the parent (which is
569 -- itself an instance) is an enclosing scope that must be made
570 -- immediately visible. This procedure is also used to install the non-
571 -- generic parent of a generic child unit when compiling its body, so
572 -- that full views of types in the parent are made visible.
573
574 procedure Remove_Parent (In_Body : Boolean := False);
575 -- Reverse effect after instantiation of child is complete
576
577 procedure Install_Hidden_Primitives
578 (Prims_List : in out Elist_Id;
579 Gen_T : Entity_Id;
580 Act_T : Entity_Id);
581 -- Remove suffix 'P' from hidden primitives of Act_T to match the
582 -- visibility of primitives of Gen_T. The list of primitives to which
583 -- the suffix is removed is added to Prims_List to restore them later.
584
585 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
586 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
587 -- set to No_Elist.
588
589 procedure Inline_Instance_Body
590 (N : Node_Id;
591 Gen_Unit : Entity_Id;
592 Act_Decl : Node_Id);
593 -- If front-end inlining is requested, instantiate the package body,
594 -- and preserve the visibility of its compilation unit, to insure
595 -- that successive instantiations succeed.
596
597 -- The functions Instantiate_XXX perform various legality checks and build
598 -- the declarations for instantiated generic parameters. In all of these
599 -- Formal is the entity in the generic unit, Actual is the entity of
600 -- expression in the generic associations, and Analyzed_Formal is the
601 -- formal in the generic copy, which contains the semantic information to
602 -- be used to validate the actual.
603
604 function Instantiate_Object
605 (Formal : Node_Id;
606 Actual : Node_Id;
607 Analyzed_Formal : Node_Id) return List_Id;
608
609 function Instantiate_Type
610 (Formal : Node_Id;
611 Actual : Node_Id;
612 Analyzed_Formal : Node_Id;
613 Actual_Decls : List_Id) return List_Id;
614
615 function Instantiate_Formal_Subprogram
616 (Formal : Node_Id;
617 Actual : Node_Id;
618 Analyzed_Formal : Node_Id) return Node_Id;
619
620 function Instantiate_Formal_Package
621 (Formal : Node_Id;
622 Actual : Node_Id;
623 Analyzed_Formal : Node_Id) return List_Id;
624 -- If the formal package is declared with a box, special visibility rules
625 -- apply to its formals: they are in the visible part of the package. This
626 -- is true in the declarative region of the formal package, that is to say
627 -- in the enclosing generic or instantiation. For an instantiation, the
628 -- parameters of the formal package are made visible in an explicit step.
629 -- Furthermore, if the actual has a visible USE clause, these formals must
630 -- be made potentially use-visible as well. On exit from the enclosing
631 -- instantiation, the reverse must be done.
632
633 -- For a formal package declared without a box, there are conformance rules
634 -- that apply to the actuals in the generic declaration and the actuals of
635 -- the actual package in the enclosing instantiation. The simplest way to
636 -- apply these rules is to repeat the instantiation of the formal package
637 -- in the context of the enclosing instance, and compare the generic
638 -- associations of this instantiation with those of the actual package.
639 -- This internal instantiation only needs to contain the renamings of the
640 -- formals: the visible and private declarations themselves need not be
641 -- created.
642
643 -- In Ada 2005, the formal package may be only partially parameterized.
644 -- In that case the visibility step must make visible those actuals whose
645 -- corresponding formals were given with a box. A final complication
646 -- involves inherited operations from formal derived types, which must
647 -- be visible if the type is.
648
649 function Is_In_Main_Unit (N : Node_Id) return Boolean;
650 -- Test if given node is in the main unit
651
652 procedure Load_Parent_Of_Generic
653 (N : Node_Id;
654 Spec : Node_Id;
655 Body_Optional : Boolean := False);
656 -- If the generic appears in a separate non-generic library unit, load the
657 -- corresponding body to retrieve the body of the generic. N is the node
658 -- for the generic instantiation, Spec is the generic package declaration.
659 --
660 -- Body_Optional is a flag that indicates that the body is being loaded to
661 -- ensure that temporaries are generated consistently when there are other
662 -- instances in the current declarative part that precede the one being
663 -- loaded. In that case a missing body is acceptable.
664
665 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
666 -- Add the context clause of the unit containing a generic unit to a
667 -- compilation unit that is, or contains, an instantiation.
668
669 function Get_Associated_Node (N : Node_Id) return Node_Id;
670 -- In order to propagate semantic information back from the analyzed copy
671 -- to the original generic, we maintain links between selected nodes in the
672 -- generic and their corresponding copies. At the end of generic analysis,
673 -- the routine Save_Global_References traverses the generic tree, examines
674 -- the semantic information, and preserves the links to those nodes that
675 -- contain global information. At instantiation, the information from the
676 -- associated node is placed on the new copy, so that name resolution is
677 -- not repeated.
678 --
679 -- Three kinds of source nodes have associated nodes:
680 --
681 -- a) those that can reference (denote) entities, that is identifiers,
682 -- character literals, expanded_names, operator symbols, operators,
683 -- and attribute reference nodes. These nodes have an Entity field
684 -- and are the set of nodes that are in N_Has_Entity.
685 --
686 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
687 --
688 -- c) selected components (N_Selected_Component)
689 --
690 -- For the first class, the associated node preserves the entity if it is
691 -- global. If the generic contains nested instantiations, the associated
692 -- node itself has been recopied, and a chain of them must be followed.
693 --
694 -- For aggregates, the associated node allows retrieval of the type, which
695 -- may otherwise not appear in the generic. The view of this type may be
696 -- different between generic and instantiation, and the full view can be
697 -- installed before the instantiation is analyzed. For aggregates of type
698 -- extensions, the same view exchange may have to be performed for some of
699 -- the ancestor types, if their view is private at the point of
700 -- instantiation.
701 --
702 -- Nodes that are selected components in the parse tree may be rewritten
703 -- as expanded names after resolution, and must be treated as potential
704 -- entity holders, which is why they also have an Associated_Node.
705 --
706 -- Nodes that do not come from source, such as freeze nodes, do not appear
707 -- in the generic tree, and need not have an associated node.
708 --
709 -- The associated node is stored in the Associated_Node field. Note that
710 -- this field overlaps Entity, which is fine, because the whole point is
711 -- that we don't need or want the normal Entity field in this situation.
712
713 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
714 -- Within the generic part, entities in the formal package are
715 -- visible. To validate subsequent type declarations, indicate
716 -- the correspondence between the entities in the analyzed formal,
717 -- and the entities in the actual package. There are three packages
718 -- involved in the instantiation of a formal package: the parent
719 -- generic P1 which appears in the generic declaration, the fake
720 -- instantiation P2 which appears in the analyzed generic, and whose
721 -- visible entities may be used in subsequent formals, and the actual
722 -- P3 in the instance. To validate subsequent formals, me indicate
723 -- that the entities in P2 are mapped into those of P3. The mapping of
724 -- entities has to be done recursively for nested packages.
725
726 procedure Move_Freeze_Nodes
727 (Out_Of : Entity_Id;
728 After : Node_Id;
729 L : List_Id);
730 -- Freeze nodes can be generated in the analysis of a generic unit, but
731 -- will not be seen by the back-end. It is necessary to move those nodes
732 -- to the enclosing scope if they freeze an outer entity. We place them
733 -- at the end of the enclosing generic package, which is semantically
734 -- neutral.
735
736 procedure Preanalyze_Actuals (N : Node_Id);
737 -- Analyze actuals to perform name resolution. Full resolution is done
738 -- later, when the expected types are known, but names have to be captured
739 -- before installing parents of generics, that are not visible for the
740 -- actuals themselves.
741
742 function True_Parent (N : Node_Id) return Node_Id;
743 -- For a subunit, return parent of corresponding stub, else return
744 -- parent of node.
745
746 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
747 -- Verify that an attribute that appears as the default for a formal
748 -- subprogram is a function or procedure with the correct profile.
749
750 -------------------------------------------
751 -- Data Structures for Generic Renamings --
752 -------------------------------------------
753
754 -- The map Generic_Renamings associates generic entities with their
755 -- corresponding actuals. Currently used to validate type instances. It
756 -- will eventually be used for all generic parameters to eliminate the
757 -- need for overload resolution in the instance.
758
759 type Assoc_Ptr is new Int;
760
761 Assoc_Null : constant Assoc_Ptr := -1;
762
763 type Assoc is record
764 Gen_Id : Entity_Id;
765 Act_Id : Entity_Id;
766 Next_In_HTable : Assoc_Ptr;
767 end record;
768
769 package Generic_Renamings is new Table.Table
770 (Table_Component_Type => Assoc,
771 Table_Index_Type => Assoc_Ptr,
772 Table_Low_Bound => 0,
773 Table_Initial => 10,
774 Table_Increment => 100,
775 Table_Name => "Generic_Renamings");
776
777 -- Variable to hold enclosing instantiation. When the environment is
778 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
779
780 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
781
782 -- Hash table for associations
783
784 HTable_Size : constant := 37;
785 type HTable_Range is range 0 .. HTable_Size - 1;
786
787 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
788 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
789 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
790 function Hash (F : Entity_Id) return HTable_Range;
791
792 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
793 Header_Num => HTable_Range,
794 Element => Assoc,
795 Elmt_Ptr => Assoc_Ptr,
796 Null_Ptr => Assoc_Null,
797 Set_Next => Set_Next_Assoc,
798 Next => Next_Assoc,
799 Key => Entity_Id,
800 Get_Key => Get_Gen_Id,
801 Hash => Hash,
802 Equal => "=");
803
804 Exchanged_Views : Elist_Id;
805 -- This list holds the private views that have been exchanged during
806 -- instantiation to restore the visibility of the generic declaration.
807 -- (see comments above). After instantiation, the current visibility is
808 -- reestablished by means of a traversal of this list.
809
810 Hidden_Entities : Elist_Id;
811 -- This list holds the entities of the current scope that are removed
812 -- from immediate visibility when instantiating a child unit. Their
813 -- visibility is restored in Remove_Parent.
814
815 -- Because instantiations can be recursive, the following must be saved
816 -- on entry and restored on exit from an instantiation (spec or body).
817 -- This is done by the two procedures Save_Env and Restore_Env. For
818 -- package and subprogram instantiations (but not for the body instances)
819 -- the action of Save_Env is done in two steps: Init_Env is called before
820 -- Check_Generic_Child_Unit, because setting the parent instances requires
821 -- that the visibility data structures be properly initialized. Once the
822 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
823
824 Parent_Unit_Visible : Boolean := False;
825 -- Parent_Unit_Visible is used when the generic is a child unit, and
826 -- indicates whether the ultimate parent of the generic is visible in the
827 -- instantiation environment. It is used to reset the visibility of the
828 -- parent at the end of the instantiation (see Remove_Parent).
829
830 Instance_Parent_Unit : Entity_Id := Empty;
831 -- This records the ultimate parent unit of an instance of a generic
832 -- child unit and is used in conjunction with Parent_Unit_Visible to
833 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
834
835 type Instance_Env is record
836 Instantiated_Parent : Assoc;
837 Exchanged_Views : Elist_Id;
838 Hidden_Entities : Elist_Id;
839 Current_Sem_Unit : Unit_Number_Type;
840 Parent_Unit_Visible : Boolean := False;
841 Instance_Parent_Unit : Entity_Id := Empty;
842 Switches : Config_Switches_Type;
843 end record;
844
845 package Instance_Envs is new Table.Table (
846 Table_Component_Type => Instance_Env,
847 Table_Index_Type => Int,
848 Table_Low_Bound => 0,
849 Table_Initial => 32,
850 Table_Increment => 100,
851 Table_Name => "Instance_Envs");
852
853 procedure Restore_Private_Views
854 (Pack_Id : Entity_Id;
855 Is_Package : Boolean := True);
856 -- Restore the private views of external types, and unmark the generic
857 -- renamings of actuals, so that they become compatible subtypes again.
858 -- For subprograms, Pack_Id is the package constructed to hold the
859 -- renamings.
860
861 procedure Switch_View (T : Entity_Id);
862 -- Switch the partial and full views of a type and its private
863 -- dependents (i.e. its subtypes and derived types).
864
865 ------------------------------------
866 -- Structures for Error Reporting --
867 ------------------------------------
868
869 Instantiation_Node : Node_Id;
870 -- Used by subprograms that validate instantiation of formal parameters
871 -- where there might be no actual on which to place the error message.
872 -- Also used to locate the instantiation node for generic subunits.
873
874 Instantiation_Error : exception;
875 -- When there is a semantic error in the generic parameter matching,
876 -- there is no point in continuing the instantiation, because the
877 -- number of cascaded errors is unpredictable. This exception aborts
878 -- the instantiation process altogether.
879
880 S_Adjustment : Sloc_Adjustment;
881 -- Offset created for each node in an instantiation, in order to keep
882 -- track of the source position of the instantiation in each of its nodes.
883 -- A subsequent semantic error or warning on a construct of the instance
884 -- points to both places: the original generic node, and the point of
885 -- instantiation. See Sinput and Sinput.L for additional details.
886
887 ------------------------------------------------------------
888 -- Data structure for keeping track when inside a Generic --
889 ------------------------------------------------------------
890
891 -- The following table is used to save values of the Inside_A_Generic
892 -- flag (see spec of Sem) when they are saved by Start_Generic.
893
894 package Generic_Flags is new Table.Table (
895 Table_Component_Type => Boolean,
896 Table_Index_Type => Int,
897 Table_Low_Bound => 0,
898 Table_Initial => 32,
899 Table_Increment => 200,
900 Table_Name => "Generic_Flags");
901
902 ---------------------------
903 -- Abandon_Instantiation --
904 ---------------------------
905
906 procedure Abandon_Instantiation (N : Node_Id) is
907 begin
908 Error_Msg_N ("\instantiation abandoned!", N);
909 raise Instantiation_Error;
910 end Abandon_Instantiation;
911
912 --------------------------
913 -- Analyze_Associations --
914 --------------------------
915
916 function Analyze_Associations
917 (I_Node : Node_Id;
918 Formals : List_Id;
919 F_Copy : List_Id) return List_Id
920 is
921 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
922 Assoc : constant List_Id := New_List;
923 Default_Actuals : constant Elist_Id := New_Elmt_List;
924 Gen_Unit : constant Entity_Id :=
925 Defining_Entity (Parent (F_Copy));
926
927 Actuals : List_Id;
928 Actual : Node_Id;
929 Analyzed_Formal : Node_Id;
930 First_Named : Node_Id := Empty;
931 Formal : Node_Id;
932 Match : Node_Id;
933 Named : Node_Id;
934 Saved_Formal : Node_Id;
935
936 Default_Formals : constant List_Id := New_List;
937 -- If an Others_Choice is present, some of the formals may be defaulted.
938 -- To simplify the treatment of visibility in an instance, we introduce
939 -- individual defaults for each such formal. These defaults are
940 -- appended to the list of associations and replace the Others_Choice.
941
942 Found_Assoc : Node_Id;
943 -- Association for the current formal being match. Empty if there are
944 -- no remaining actuals, or if there is no named association with the
945 -- name of the formal.
946
947 Is_Named_Assoc : Boolean;
948 Num_Matched : Int := 0;
949 Num_Actuals : Int := 0;
950
951 Others_Present : Boolean := False;
952 Others_Choice : Node_Id := Empty;
953 -- In Ada 2005, indicates partial parameterization of a formal
954 -- package. As usual an other association must be last in the list.
955
956 function Build_Wrapper
957 (Formal : Entity_Id;
958 Actual : Entity_Id := Empty) return Node_Id;
959 -- In GNATProve mode, create a wrapper function for actuals that are
960 -- operators, in order to propagate their contract to the renaming
961 -- declarations generated for them. If the actual is absent, this is
962 -- a formal with a default, and the name of the operator is that of the
963 -- formal.
964
965 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
966 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
967 -- cannot have a named association for it. AI05-0025 extends this rule
968 -- to formals of formal packages by AI05-0025, and it also applies to
969 -- box-initialized formals.
970
971 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
972 -- Determine whether the parameter types and the return type of Subp
973 -- are fully defined at the point of instantiation.
974
975 function Matching_Actual
976 (F : Entity_Id;
977 A_F : Entity_Id) return Node_Id;
978 -- Find actual that corresponds to a given a formal parameter. If the
979 -- actuals are positional, return the next one, if any. If the actuals
980 -- are named, scan the parameter associations to find the right one.
981 -- A_F is the corresponding entity in the analyzed generic,which is
982 -- placed on the selector name for ASIS use.
983 --
984 -- In Ada 2005, a named association may be given with a box, in which
985 -- case Matching_Actual sets Found_Assoc to the generic association,
986 -- but return Empty for the actual itself. In this case the code below
987 -- creates a corresponding declaration for the formal.
988
989 function Partial_Parameterization return Boolean;
990 -- Ada 2005: if no match is found for a given formal, check if the
991 -- association for it includes a box, or whether the associations
992 -- include an Others clause.
993
994 procedure Process_Default (F : Entity_Id);
995 -- Add a copy of the declaration of generic formal F to the list of
996 -- associations, and add an explicit box association for F if there
997 -- is none yet, and the default comes from an Others_Choice.
998
999 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1000 -- Determine whether Subp renames one of the subprograms defined in the
1001 -- generated package Standard.
1002
1003 procedure Set_Analyzed_Formal;
1004 -- Find the node in the generic copy that corresponds to a given formal.
1005 -- The semantic information on this node is used to perform legality
1006 -- checks on the actuals. Because semantic analysis can introduce some
1007 -- anonymous entities or modify the declaration node itself, the
1008 -- correspondence between the two lists is not one-one. In addition to
1009 -- anonymous types, the presence a formal equality will introduce an
1010 -- implicit declaration for the corresponding inequality.
1011
1012 -------------------
1013 -- Build_Wrapper --
1014 -------------------
1015
1016 function Build_Wrapper
1017 (Formal : Entity_Id;
1018 Actual : Entity_Id := Empty) return Node_Id
1019 is
1020 Loc : constant Source_Ptr := Sloc (I_Node);
1021 Typ : constant Entity_Id := Etype (Formal);
1022 Is_Binary : constant Boolean :=
1023 Present (Next_Formal (First_Formal (Formal)));
1024
1025 Decl : Node_Id;
1026 Expr : Node_Id;
1027 F1, F2 : Entity_Id;
1028 Func : Entity_Id;
1029 Op_Name : Name_Id;
1030 Spec : Node_Id;
1031
1032 L, R : Node_Id;
1033
1034 begin
1035 if No (Actual) then
1036 Op_Name := Chars (Formal);
1037 else
1038 Op_Name := Chars (Actual);
1039 end if;
1040
1041 -- Create entities for wrapper function and its formals
1042
1043 F1 := Make_Temporary (Loc, 'A');
1044 F2 := Make_Temporary (Loc, 'B');
1045 L := New_Occurrence_Of (F1, Loc);
1046 R := New_Occurrence_Of (F2, Loc);
1047
1048 Func := Make_Defining_Identifier (Loc, Chars (Formal));
1049 Set_Ekind (Func, E_Function);
1050 Set_Is_Generic_Actual_Subprogram (Func);
1051
1052 Spec :=
1053 Make_Function_Specification (Loc,
1054 Defining_Unit_Name => Func,
1055 Parameter_Specifications => New_List (
1056 Make_Parameter_Specification (Loc,
1057 Defining_Identifier => F1,
1058 Parameter_Type =>
1059 Make_Identifier (Loc,
1060 Chars => Chars (Etype (First_Formal (Formal)))))),
1061 Result_Definition => Make_Identifier (Loc, Chars (Typ)));
1062
1063 if Is_Binary then
1064 Append_To (Parameter_Specifications (Spec),
1065 Make_Parameter_Specification (Loc,
1066 Defining_Identifier => F2,
1067 Parameter_Type =>
1068 Make_Identifier (Loc,
1069 Chars (Etype (Next_Formal (First_Formal (Formal)))))));
1070 end if;
1071
1072 -- Build expression as a function call, or as an operator node
1073 -- that corresponds to the name of the actual, starting with binary
1074 -- operators.
1075
1076 if Present (Actual) and then Op_Name not in Any_Operator_Name then
1077 Expr :=
1078 Make_Function_Call (Loc,
1079 Name =>
1080 New_Occurrence_Of (Entity (Actual), Loc),
1081 Parameter_Associations => New_List (L));
1082
1083 if Is_Binary then
1084 Append_To (Parameter_Associations (Expr), R);
1085 end if;
1086
1087 -- Binary operators
1088
1089 elsif Is_Binary then
1090 if Op_Name = Name_Op_And then
1091 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
1092 elsif Op_Name = Name_Op_Or then
1093 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
1094 elsif Op_Name = Name_Op_Xor then
1095 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
1096 elsif Op_Name = Name_Op_Eq then
1097 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
1098 elsif Op_Name = Name_Op_Ne then
1099 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
1100 elsif Op_Name = Name_Op_Le then
1101 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
1102 elsif Op_Name = Name_Op_Gt then
1103 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
1104 elsif Op_Name = Name_Op_Ge then
1105 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
1106 elsif Op_Name = Name_Op_Lt then
1107 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
1108 elsif Op_Name = Name_Op_Add then
1109 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
1110 elsif Op_Name = Name_Op_Subtract then
1111 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
1112 elsif Op_Name = Name_Op_Concat then
1113 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
1114 elsif Op_Name = Name_Op_Multiply then
1115 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
1116 elsif Op_Name = Name_Op_Divide then
1117 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
1118 elsif Op_Name = Name_Op_Mod then
1119 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
1120 elsif Op_Name = Name_Op_Rem then
1121 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
1122 elsif Op_Name = Name_Op_Expon then
1123 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
1124 end if;
1125
1126 -- Unary operators
1127
1128 else
1129 if Op_Name = Name_Op_Add then
1130 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
1131 elsif Op_Name = Name_Op_Subtract then
1132 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
1133 elsif Op_Name = Name_Op_Abs then
1134 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
1135 elsif Op_Name = Name_Op_Not then
1136 Expr := Make_Op_Not (Loc, Right_Opnd => L);
1137 end if;
1138 end if;
1139
1140 Decl :=
1141 Make_Expression_Function (Loc,
1142 Specification => Spec,
1143 Expression => Expr);
1144
1145 return Decl;
1146 end Build_Wrapper;
1147
1148 ----------------------------------------
1149 -- Check_Overloaded_Formal_Subprogram --
1150 ----------------------------------------
1151
1152 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1153 Temp_Formal : Entity_Id;
1154
1155 begin
1156 Temp_Formal := First (Formals);
1157 while Present (Temp_Formal) loop
1158 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1159 and then Temp_Formal /= Formal
1160 and then
1161 Chars (Defining_Unit_Name (Specification (Formal))) =
1162 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1163 then
1164 if Present (Found_Assoc) then
1165 Error_Msg_N
1166 ("named association not allowed for overloaded formal",
1167 Found_Assoc);
1168
1169 else
1170 Error_Msg_N
1171 ("named association not allowed for overloaded formal",
1172 Others_Choice);
1173 end if;
1174
1175 Abandon_Instantiation (Instantiation_Node);
1176 end if;
1177
1178 Next (Temp_Formal);
1179 end loop;
1180 end Check_Overloaded_Formal_Subprogram;
1181
1182 -------------------------------
1183 -- Has_Fully_Defined_Profile --
1184 -------------------------------
1185
1186 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1187 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1188 -- Determine whethet type Typ is fully defined
1189
1190 ---------------------------
1191 -- Is_Fully_Defined_Type --
1192 ---------------------------
1193
1194 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1195 begin
1196 -- A private type without a full view is not fully defined
1197
1198 if Is_Private_Type (Typ)
1199 and then No (Full_View (Typ))
1200 then
1201 return False;
1202
1203 -- An incomplete type is never fully defined
1204
1205 elsif Is_Incomplete_Type (Typ) then
1206 return False;
1207
1208 -- All other types are fully defined
1209
1210 else
1211 return True;
1212 end if;
1213 end Is_Fully_Defined_Type;
1214
1215 -- Local declarations
1216
1217 Param : Entity_Id;
1218
1219 -- Start of processing for Has_Fully_Defined_Profile
1220
1221 begin
1222 -- Check the parameters
1223
1224 Param := First_Formal (Subp);
1225 while Present (Param) loop
1226 if not Is_Fully_Defined_Type (Etype (Param)) then
1227 return False;
1228 end if;
1229
1230 Next_Formal (Param);
1231 end loop;
1232
1233 -- Check the return type
1234
1235 return Is_Fully_Defined_Type (Etype (Subp));
1236 end Has_Fully_Defined_Profile;
1237
1238 ---------------------
1239 -- Matching_Actual --
1240 ---------------------
1241
1242 function Matching_Actual
1243 (F : Entity_Id;
1244 A_F : Entity_Id) return Node_Id
1245 is
1246 Prev : Node_Id;
1247 Act : Node_Id;
1248
1249 begin
1250 Is_Named_Assoc := False;
1251
1252 -- End of list of purely positional parameters
1253
1254 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1255 Found_Assoc := Empty;
1256 Act := Empty;
1257
1258 -- Case of positional parameter corresponding to current formal
1259
1260 elsif No (Selector_Name (Actual)) then
1261 Found_Assoc := Actual;
1262 Act := Explicit_Generic_Actual_Parameter (Actual);
1263 Num_Matched := Num_Matched + 1;
1264 Next (Actual);
1265
1266 -- Otherwise scan list of named actuals to find the one with the
1267 -- desired name. All remaining actuals have explicit names.
1268
1269 else
1270 Is_Named_Assoc := True;
1271 Found_Assoc := Empty;
1272 Act := Empty;
1273 Prev := Empty;
1274
1275 while Present (Actual) loop
1276 if Chars (Selector_Name (Actual)) = Chars (F) then
1277 Set_Entity (Selector_Name (Actual), A_F);
1278 Set_Etype (Selector_Name (Actual), Etype (A_F));
1279 Generate_Reference (A_F, Selector_Name (Actual));
1280 Found_Assoc := Actual;
1281 Act := Explicit_Generic_Actual_Parameter (Actual);
1282 Num_Matched := Num_Matched + 1;
1283 exit;
1284 end if;
1285
1286 Prev := Actual;
1287 Next (Actual);
1288 end loop;
1289
1290 -- Reset for subsequent searches. In most cases the named
1291 -- associations are in order. If they are not, we reorder them
1292 -- to avoid scanning twice the same actual. This is not just a
1293 -- question of efficiency: there may be multiple defaults with
1294 -- boxes that have the same name. In a nested instantiation we
1295 -- insert actuals for those defaults, and cannot rely on their
1296 -- names to disambiguate them.
1297
1298 if Actual = First_Named then
1299 Next (First_Named);
1300
1301 elsif Present (Actual) then
1302 Insert_Before (First_Named, Remove_Next (Prev));
1303 end if;
1304
1305 Actual := First_Named;
1306 end if;
1307
1308 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1309 Set_Used_As_Generic_Actual (Entity (Act));
1310 end if;
1311
1312 return Act;
1313 end Matching_Actual;
1314
1315 ------------------------------
1316 -- Partial_Parameterization --
1317 ------------------------------
1318
1319 function Partial_Parameterization return Boolean is
1320 begin
1321 return Others_Present
1322 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1323 end Partial_Parameterization;
1324
1325 ---------------------
1326 -- Process_Default --
1327 ---------------------
1328
1329 procedure Process_Default (F : Entity_Id) is
1330 Loc : constant Source_Ptr := Sloc (I_Node);
1331 F_Id : constant Entity_Id := Defining_Entity (F);
1332 Decl : Node_Id;
1333 Default : Node_Id;
1334 Id : Entity_Id;
1335
1336 begin
1337 -- Append copy of formal declaration to associations, and create new
1338 -- defining identifier for it.
1339
1340 Decl := New_Copy_Tree (F);
1341 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1342
1343 if Nkind (F) in N_Formal_Subprogram_Declaration then
1344 Set_Defining_Unit_Name (Specification (Decl), Id);
1345
1346 else
1347 Set_Defining_Identifier (Decl, Id);
1348 end if;
1349
1350 Append (Decl, Assoc);
1351
1352 if No (Found_Assoc) then
1353 Default :=
1354 Make_Generic_Association (Loc,
1355 Selector_Name => New_Occurrence_Of (Id, Loc),
1356 Explicit_Generic_Actual_Parameter => Empty);
1357 Set_Box_Present (Default);
1358 Append (Default, Default_Formals);
1359 end if;
1360 end Process_Default;
1361
1362 ---------------------------------
1363 -- Renames_Standard_Subprogram --
1364 ---------------------------------
1365
1366 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1367 Id : Entity_Id;
1368
1369 begin
1370 Id := Alias (Subp);
1371 while Present (Id) loop
1372 if Scope (Id) = Standard_Standard then
1373 return True;
1374 end if;
1375
1376 Id := Alias (Id);
1377 end loop;
1378
1379 return False;
1380 end Renames_Standard_Subprogram;
1381
1382 -------------------------
1383 -- Set_Analyzed_Formal --
1384 -------------------------
1385
1386 procedure Set_Analyzed_Formal is
1387 Kind : Node_Kind;
1388
1389 begin
1390 while Present (Analyzed_Formal) loop
1391 Kind := Nkind (Analyzed_Formal);
1392
1393 case Nkind (Formal) is
1394
1395 when N_Formal_Subprogram_Declaration =>
1396 exit when Kind in N_Formal_Subprogram_Declaration
1397 and then
1398 Chars
1399 (Defining_Unit_Name (Specification (Formal))) =
1400 Chars
1401 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1402
1403 when N_Formal_Package_Declaration =>
1404 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1405 N_Generic_Package_Declaration,
1406 N_Package_Declaration);
1407
1408 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1409
1410 when others =>
1411
1412 -- Skip freeze nodes, and nodes inserted to replace
1413 -- unrecognized pragmas.
1414
1415 exit when
1416 Kind not in N_Formal_Subprogram_Declaration
1417 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1418 N_Freeze_Entity,
1419 N_Null_Statement,
1420 N_Itype_Reference)
1421 and then Chars (Defining_Identifier (Formal)) =
1422 Chars (Defining_Identifier (Analyzed_Formal));
1423 end case;
1424
1425 Next (Analyzed_Formal);
1426 end loop;
1427 end Set_Analyzed_Formal;
1428
1429 -- Start of processing for Analyze_Associations
1430
1431 begin
1432 Actuals := Generic_Associations (I_Node);
1433
1434 if Present (Actuals) then
1435
1436 -- Check for an Others choice, indicating a partial parameterization
1437 -- for a formal package.
1438
1439 Actual := First (Actuals);
1440 while Present (Actual) loop
1441 if Nkind (Actual) = N_Others_Choice then
1442 Others_Present := True;
1443 Others_Choice := Actual;
1444
1445 if Present (Next (Actual)) then
1446 Error_Msg_N ("others must be last association", Actual);
1447 end if;
1448
1449 -- This subprogram is used both for formal packages and for
1450 -- instantiations. For the latter, associations must all be
1451 -- explicit.
1452
1453 if Nkind (I_Node) /= N_Formal_Package_Declaration
1454 and then Comes_From_Source (I_Node)
1455 then
1456 Error_Msg_N
1457 ("others association not allowed in an instance",
1458 Actual);
1459 end if;
1460
1461 -- In any case, nothing to do after the others association
1462
1463 exit;
1464
1465 elsif Box_Present (Actual)
1466 and then Comes_From_Source (I_Node)
1467 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1468 then
1469 Error_Msg_N
1470 ("box association not allowed in an instance", Actual);
1471 end if;
1472
1473 Next (Actual);
1474 end loop;
1475
1476 -- If named associations are present, save first named association
1477 -- (it may of course be Empty) to facilitate subsequent name search.
1478
1479 First_Named := First (Actuals);
1480 while Present (First_Named)
1481 and then Nkind (First_Named) /= N_Others_Choice
1482 and then No (Selector_Name (First_Named))
1483 loop
1484 Num_Actuals := Num_Actuals + 1;
1485 Next (First_Named);
1486 end loop;
1487 end if;
1488
1489 Named := First_Named;
1490 while Present (Named) loop
1491 if Nkind (Named) /= N_Others_Choice
1492 and then No (Selector_Name (Named))
1493 then
1494 Error_Msg_N ("invalid positional actual after named one", Named);
1495 Abandon_Instantiation (Named);
1496 end if;
1497
1498 -- A named association may lack an actual parameter, if it was
1499 -- introduced for a default subprogram that turns out to be local
1500 -- to the outer instantiation.
1501
1502 if Nkind (Named) /= N_Others_Choice
1503 and then Present (Explicit_Generic_Actual_Parameter (Named))
1504 then
1505 Num_Actuals := Num_Actuals + 1;
1506 end if;
1507
1508 Next (Named);
1509 end loop;
1510
1511 if Present (Formals) then
1512 Formal := First_Non_Pragma (Formals);
1513 Analyzed_Formal := First_Non_Pragma (F_Copy);
1514
1515 if Present (Actuals) then
1516 Actual := First (Actuals);
1517
1518 -- All formals should have default values
1519
1520 else
1521 Actual := Empty;
1522 end if;
1523
1524 while Present (Formal) loop
1525 Set_Analyzed_Formal;
1526 Saved_Formal := Next_Non_Pragma (Formal);
1527
1528 case Nkind (Formal) is
1529 when N_Formal_Object_Declaration =>
1530 Match :=
1531 Matching_Actual (
1532 Defining_Identifier (Formal),
1533 Defining_Identifier (Analyzed_Formal));
1534
1535 if No (Match) and then Partial_Parameterization then
1536 Process_Default (Formal);
1537 else
1538 Append_List
1539 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1540 Assoc);
1541 end if;
1542
1543 when N_Formal_Type_Declaration =>
1544 Match :=
1545 Matching_Actual (
1546 Defining_Identifier (Formal),
1547 Defining_Identifier (Analyzed_Formal));
1548
1549 if No (Match) then
1550 if Partial_Parameterization then
1551 Process_Default (Formal);
1552
1553 else
1554 Error_Msg_Sloc := Sloc (Gen_Unit);
1555 Error_Msg_NE
1556 ("missing actual&",
1557 Instantiation_Node,
1558 Defining_Identifier (Formal));
1559 Error_Msg_NE ("\in instantiation of & declared#",
1560 Instantiation_Node, Gen_Unit);
1561 Abandon_Instantiation (Instantiation_Node);
1562 end if;
1563
1564 else
1565 Analyze (Match);
1566 Append_List
1567 (Instantiate_Type
1568 (Formal, Match, Analyzed_Formal, Assoc),
1569 Assoc);
1570
1571 -- An instantiation is a freeze point for the actuals,
1572 -- unless this is a rewritten formal package, or the
1573 -- formal is an Ada 2012 formal incomplete type.
1574
1575 if Nkind (I_Node) = N_Formal_Package_Declaration
1576 or else
1577 (Ada_Version >= Ada_2012
1578 and then
1579 Ekind (Defining_Identifier (Analyzed_Formal)) =
1580 E_Incomplete_Type)
1581 then
1582 null;
1583
1584 else
1585 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1586 end if;
1587 end if;
1588
1589 -- A remote access-to-class-wide type is not a legal actual
1590 -- for a generic formal of an access type (E.2.2(17/2)).
1591 -- In GNAT an exception to this rule is introduced when
1592 -- the formal is marked as remote using implementation
1593 -- defined aspect/pragma Remote_Access_Type. In that case
1594 -- the actual must be remote as well.
1595
1596 -- If the current instantiation is the construction of a
1597 -- local copy for a formal package the actuals may be
1598 -- defaulted, and there is no matching actual to check.
1599
1600 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1601 and then
1602 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1603 N_Access_To_Object_Definition
1604 and then Present (Match)
1605 then
1606 declare
1607 Formal_Ent : constant Entity_Id :=
1608 Defining_Identifier (Analyzed_Formal);
1609 begin
1610 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1611 = Is_Remote_Types (Formal_Ent)
1612 then
1613 -- Remoteness of formal and actual match
1614
1615 null;
1616
1617 elsif Is_Remote_Types (Formal_Ent) then
1618
1619 -- Remote formal, non-remote actual
1620
1621 Error_Msg_NE
1622 ("actual for& must be remote", Match, Formal_Ent);
1623
1624 else
1625 -- Non-remote formal, remote actual
1626
1627 Error_Msg_NE
1628 ("actual for& may not be remote",
1629 Match, Formal_Ent);
1630 end if;
1631 end;
1632 end if;
1633
1634 when N_Formal_Subprogram_Declaration =>
1635 Match :=
1636 Matching_Actual
1637 (Defining_Unit_Name (Specification (Formal)),
1638 Defining_Unit_Name (Specification (Analyzed_Formal)));
1639
1640 -- If the formal subprogram has the same name as another
1641 -- formal subprogram of the generic, then a named
1642 -- association is illegal (12.3(9)). Exclude named
1643 -- associations that are generated for a nested instance.
1644
1645 if Present (Match)
1646 and then Is_Named_Assoc
1647 and then Comes_From_Source (Found_Assoc)
1648 then
1649 Check_Overloaded_Formal_Subprogram (Formal);
1650 end if;
1651
1652 -- If there is no corresponding actual, this may be case
1653 -- of partial parameterization, or else the formal has a
1654 -- default or a box.
1655
1656 if No (Match) and then Partial_Parameterization then
1657 Process_Default (Formal);
1658
1659 if Nkind (I_Node) = N_Formal_Package_Declaration then
1660 Check_Overloaded_Formal_Subprogram (Formal);
1661 end if;
1662
1663 else
1664 if GNATprove_Mode
1665 and then Ekind (Defining_Entity (Analyzed_Formal))
1666 = E_Function
1667 then
1668
1669 -- If actual is an entity (function or operator),
1670 -- build wrapper for it.
1671
1672 if Present (Match) and then Is_Entity_Name (Match) then
1673 Append_To (Assoc,
1674 Build_Wrapper
1675 (Defining_Entity (Analyzed_Formal), Match));
1676
1677 -- Ditto if formal is an operator with a default.
1678
1679 elsif Box_Present (Formal)
1680 and then Nkind (Defining_Entity (Analyzed_Formal))
1681 = N_Defining_Operator_Symbol
1682
1683 then
1684 Append_To (Assoc,
1685 Build_Wrapper
1686 (Defining_Entity (Analyzed_Formal)));
1687
1688 -- Otherwise create renaming declaration.
1689
1690 else
1691 Append_To (Assoc,
1692 Instantiate_Formal_Subprogram
1693 (Formal, Match, Analyzed_Formal));
1694 end if;
1695
1696 else
1697 Append_To (Assoc,
1698 Instantiate_Formal_Subprogram
1699 (Formal, Match, Analyzed_Formal));
1700 end if;
1701
1702 -- An instantiation is a freeze point for the actuals,
1703 -- unless this is a rewritten formal package.
1704
1705 if Nkind (I_Node) /= N_Formal_Package_Declaration
1706 and then Nkind (Match) = N_Identifier
1707 and then Is_Subprogram (Entity (Match))
1708
1709 -- The actual subprogram may rename a routine defined
1710 -- in Standard. Avoid freezing such renamings because
1711 -- subprograms coming from Standard cannot be frozen.
1712
1713 and then
1714 not Renames_Standard_Subprogram (Entity (Match))
1715
1716 -- If the actual subprogram comes from a different
1717 -- unit, it is already frozen, either by a body in
1718 -- that unit or by the end of the declarative part
1719 -- of the unit. This check avoids the freezing of
1720 -- subprograms defined in Standard which are used
1721 -- as generic actuals.
1722
1723 and then In_Same_Code_Unit (Entity (Match), I_Node)
1724 and then Has_Fully_Defined_Profile (Entity (Match))
1725 then
1726 -- Mark the subprogram as having a delayed freeze
1727 -- since this may be an out-of-order action.
1728
1729 Set_Has_Delayed_Freeze (Entity (Match));
1730 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1731 end if;
1732 end if;
1733
1734 -- If this is a nested generic, preserve default for later
1735 -- instantiations.
1736
1737 if No (Match)
1738 and then Box_Present (Formal)
1739 then
1740 Append_Elmt
1741 (Defining_Unit_Name (Specification (Last (Assoc))),
1742 Default_Actuals);
1743 end if;
1744
1745 when N_Formal_Package_Declaration =>
1746 Match :=
1747 Matching_Actual (
1748 Defining_Identifier (Formal),
1749 Defining_Identifier (Original_Node (Analyzed_Formal)));
1750
1751 if No (Match) then
1752 if Partial_Parameterization then
1753 Process_Default (Formal);
1754
1755 else
1756 Error_Msg_Sloc := Sloc (Gen_Unit);
1757 Error_Msg_NE
1758 ("missing actual&",
1759 Instantiation_Node, Defining_Identifier (Formal));
1760 Error_Msg_NE ("\in instantiation of & declared#",
1761 Instantiation_Node, Gen_Unit);
1762
1763 Abandon_Instantiation (Instantiation_Node);
1764 end if;
1765
1766 else
1767 Analyze (Match);
1768 Append_List
1769 (Instantiate_Formal_Package
1770 (Formal, Match, Analyzed_Formal),
1771 Assoc);
1772 end if;
1773
1774 -- For use type and use package appearing in the generic part,
1775 -- we have already copied them, so we can just move them where
1776 -- they belong (we mustn't recopy them since this would mess up
1777 -- the Sloc values).
1778
1779 when N_Use_Package_Clause |
1780 N_Use_Type_Clause =>
1781 if Nkind (Original_Node (I_Node)) =
1782 N_Formal_Package_Declaration
1783 then
1784 Append (New_Copy_Tree (Formal), Assoc);
1785 else
1786 Remove (Formal);
1787 Append (Formal, Assoc);
1788 end if;
1789
1790 when others =>
1791 raise Program_Error;
1792
1793 end case;
1794
1795 Formal := Saved_Formal;
1796 Next_Non_Pragma (Analyzed_Formal);
1797 end loop;
1798
1799 if Num_Actuals > Num_Matched then
1800 Error_Msg_Sloc := Sloc (Gen_Unit);
1801
1802 if Present (Selector_Name (Actual)) then
1803 Error_Msg_NE
1804 ("unmatched actual&",
1805 Actual, Selector_Name (Actual));
1806 Error_Msg_NE ("\in instantiation of& declared#",
1807 Actual, Gen_Unit);
1808 else
1809 Error_Msg_NE
1810 ("unmatched actual in instantiation of& declared#",
1811 Actual, Gen_Unit);
1812 end if;
1813 end if;
1814
1815 elsif Present (Actuals) then
1816 Error_Msg_N
1817 ("too many actuals in generic instantiation", Instantiation_Node);
1818 end if;
1819
1820 -- An instantiation freezes all generic actuals. The only exceptions
1821 -- to this are incomplete types and subprograms which are not fully
1822 -- defined at the point of instantiation.
1823
1824 declare
1825 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1826 begin
1827 while Present (Elmt) loop
1828 Freeze_Before (I_Node, Node (Elmt));
1829 Next_Elmt (Elmt);
1830 end loop;
1831 end;
1832
1833 -- If there are default subprograms, normalize the tree by adding
1834 -- explicit associations for them. This is required if the instance
1835 -- appears within a generic.
1836
1837 declare
1838 Elmt : Elmt_Id;
1839 Subp : Entity_Id;
1840 New_D : Node_Id;
1841
1842 begin
1843 Elmt := First_Elmt (Default_Actuals);
1844 while Present (Elmt) loop
1845 if No (Actuals) then
1846 Actuals := New_List;
1847 Set_Generic_Associations (I_Node, Actuals);
1848 end if;
1849
1850 Subp := Node (Elmt);
1851 New_D :=
1852 Make_Generic_Association (Sloc (Subp),
1853 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1854 Explicit_Generic_Actual_Parameter =>
1855 New_Occurrence_Of (Subp, Sloc (Subp)));
1856 Mark_Rewrite_Insertion (New_D);
1857 Append_To (Actuals, New_D);
1858 Next_Elmt (Elmt);
1859 end loop;
1860 end;
1861
1862 -- If this is a formal package, normalize the parameter list by adding
1863 -- explicit box associations for the formals that are covered by an
1864 -- Others_Choice.
1865
1866 if not Is_Empty_List (Default_Formals) then
1867 Append_List (Default_Formals, Formals);
1868 end if;
1869
1870 return Assoc;
1871 end Analyze_Associations;
1872
1873 -------------------------------
1874 -- Analyze_Formal_Array_Type --
1875 -------------------------------
1876
1877 procedure Analyze_Formal_Array_Type
1878 (T : in out Entity_Id;
1879 Def : Node_Id)
1880 is
1881 DSS : Node_Id;
1882
1883 begin
1884 -- Treated like a non-generic array declaration, with additional
1885 -- semantic checks.
1886
1887 Enter_Name (T);
1888
1889 if Nkind (Def) = N_Constrained_Array_Definition then
1890 DSS := First (Discrete_Subtype_Definitions (Def));
1891 while Present (DSS) loop
1892 if Nkind_In (DSS, N_Subtype_Indication,
1893 N_Range,
1894 N_Attribute_Reference)
1895 then
1896 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1897 end if;
1898
1899 Next (DSS);
1900 end loop;
1901 end if;
1902
1903 Array_Type_Declaration (T, Def);
1904 Set_Is_Generic_Type (Base_Type (T));
1905
1906 if Ekind (Component_Type (T)) = E_Incomplete_Type
1907 and then No (Full_View (Component_Type (T)))
1908 then
1909 Error_Msg_N ("premature usage of incomplete type", Def);
1910
1911 -- Check that range constraint is not allowed on the component type
1912 -- of a generic formal array type (AARM 12.5.3(3))
1913
1914 elsif Is_Internal (Component_Type (T))
1915 and then Present (Subtype_Indication (Component_Definition (Def)))
1916 and then Nkind (Original_Node
1917 (Subtype_Indication (Component_Definition (Def)))) =
1918 N_Subtype_Indication
1919 then
1920 Error_Msg_N
1921 ("in a formal, a subtype indication can only be "
1922 & "a subtype mark (RM 12.5.3(3))",
1923 Subtype_Indication (Component_Definition (Def)));
1924 end if;
1925
1926 end Analyze_Formal_Array_Type;
1927
1928 ---------------------------------------------
1929 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1930 ---------------------------------------------
1931
1932 -- As for other generic types, we create a valid type representation with
1933 -- legal but arbitrary attributes, whose values are never considered
1934 -- static. For all scalar types we introduce an anonymous base type, with
1935 -- the same attributes. We choose the corresponding integer type to be
1936 -- Standard_Integer.
1937 -- Here and in other similar routines, the Sloc of the generated internal
1938 -- type must be the same as the sloc of the defining identifier of the
1939 -- formal type declaration, to provide proper source navigation.
1940
1941 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1942 (T : Entity_Id;
1943 Def : Node_Id)
1944 is
1945 Loc : constant Source_Ptr := Sloc (Def);
1946
1947 Base : constant Entity_Id :=
1948 New_Internal_Entity
1949 (E_Decimal_Fixed_Point_Type,
1950 Current_Scope,
1951 Sloc (Defining_Identifier (Parent (Def))), 'G');
1952
1953 Int_Base : constant Entity_Id := Standard_Integer;
1954 Delta_Val : constant Ureal := Ureal_1;
1955 Digs_Val : constant Uint := Uint_6;
1956
1957 function Make_Dummy_Bound return Node_Id;
1958 -- Return a properly typed universal real literal to use as a bound
1959
1960 ----------------------
1961 -- Make_Dummy_Bound --
1962 ----------------------
1963
1964 function Make_Dummy_Bound return Node_Id is
1965 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1966 begin
1967 Set_Etype (Bound, Universal_Real);
1968 return Bound;
1969 end Make_Dummy_Bound;
1970
1971 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1972
1973 begin
1974 Enter_Name (T);
1975
1976 Set_Etype (Base, Base);
1977 Set_Size_Info (Base, Int_Base);
1978 Set_RM_Size (Base, RM_Size (Int_Base));
1979 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1980 Set_Digits_Value (Base, Digs_Val);
1981 Set_Delta_Value (Base, Delta_Val);
1982 Set_Small_Value (Base, Delta_Val);
1983 Set_Scalar_Range (Base,
1984 Make_Range (Loc,
1985 Low_Bound => Make_Dummy_Bound,
1986 High_Bound => Make_Dummy_Bound));
1987
1988 Set_Is_Generic_Type (Base);
1989 Set_Parent (Base, Parent (Def));
1990
1991 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1992 Set_Etype (T, Base);
1993 Set_Size_Info (T, Int_Base);
1994 Set_RM_Size (T, RM_Size (Int_Base));
1995 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1996 Set_Digits_Value (T, Digs_Val);
1997 Set_Delta_Value (T, Delta_Val);
1998 Set_Small_Value (T, Delta_Val);
1999 Set_Scalar_Range (T, Scalar_Range (Base));
2000 Set_Is_Constrained (T);
2001
2002 Check_Restriction (No_Fixed_Point, Def);
2003 end Analyze_Formal_Decimal_Fixed_Point_Type;
2004
2005 -------------------------------------------
2006 -- Analyze_Formal_Derived_Interface_Type --
2007 -------------------------------------------
2008
2009 procedure Analyze_Formal_Derived_Interface_Type
2010 (N : Node_Id;
2011 T : Entity_Id;
2012 Def : Node_Id)
2013 is
2014 Loc : constant Source_Ptr := Sloc (Def);
2015
2016 begin
2017 -- Rewrite as a type declaration of a derived type. This ensures that
2018 -- the interface list and primitive operations are properly captured.
2019
2020 Rewrite (N,
2021 Make_Full_Type_Declaration (Loc,
2022 Defining_Identifier => T,
2023 Type_Definition => Def));
2024 Analyze (N);
2025 Set_Is_Generic_Type (T);
2026 end Analyze_Formal_Derived_Interface_Type;
2027
2028 ---------------------------------
2029 -- Analyze_Formal_Derived_Type --
2030 ---------------------------------
2031
2032 procedure Analyze_Formal_Derived_Type
2033 (N : Node_Id;
2034 T : Entity_Id;
2035 Def : Node_Id)
2036 is
2037 Loc : constant Source_Ptr := Sloc (Def);
2038 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2039 New_N : Node_Id;
2040
2041 begin
2042 Set_Is_Generic_Type (T);
2043
2044 if Private_Present (Def) then
2045 New_N :=
2046 Make_Private_Extension_Declaration (Loc,
2047 Defining_Identifier => T,
2048 Discriminant_Specifications => Discriminant_Specifications (N),
2049 Unknown_Discriminants_Present => Unk_Disc,
2050 Subtype_Indication => Subtype_Mark (Def),
2051 Interface_List => Interface_List (Def));
2052
2053 Set_Abstract_Present (New_N, Abstract_Present (Def));
2054 Set_Limited_Present (New_N, Limited_Present (Def));
2055 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2056
2057 else
2058 New_N :=
2059 Make_Full_Type_Declaration (Loc,
2060 Defining_Identifier => T,
2061 Discriminant_Specifications =>
2062 Discriminant_Specifications (Parent (T)),
2063 Type_Definition =>
2064 Make_Derived_Type_Definition (Loc,
2065 Subtype_Indication => Subtype_Mark (Def)));
2066
2067 Set_Abstract_Present
2068 (Type_Definition (New_N), Abstract_Present (Def));
2069 Set_Limited_Present
2070 (Type_Definition (New_N), Limited_Present (Def));
2071 end if;
2072
2073 Rewrite (N, New_N);
2074 Analyze (N);
2075
2076 if Unk_Disc then
2077 if not Is_Composite_Type (T) then
2078 Error_Msg_N
2079 ("unknown discriminants not allowed for elementary types", N);
2080 else
2081 Set_Has_Unknown_Discriminants (T);
2082 Set_Is_Constrained (T, False);
2083 end if;
2084 end if;
2085
2086 -- If the parent type has a known size, so does the formal, which makes
2087 -- legal representation clauses that involve the formal.
2088
2089 Set_Size_Known_At_Compile_Time
2090 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2091 end Analyze_Formal_Derived_Type;
2092
2093 ----------------------------------
2094 -- Analyze_Formal_Discrete_Type --
2095 ----------------------------------
2096
2097 -- The operations defined for a discrete types are those of an enumeration
2098 -- type. The size is set to an arbitrary value, for use in analyzing the
2099 -- generic unit.
2100
2101 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2102 Loc : constant Source_Ptr := Sloc (Def);
2103 Lo : Node_Id;
2104 Hi : Node_Id;
2105
2106 Base : constant Entity_Id :=
2107 New_Internal_Entity
2108 (E_Floating_Point_Type, Current_Scope,
2109 Sloc (Defining_Identifier (Parent (Def))), 'G');
2110
2111 begin
2112 Enter_Name (T);
2113 Set_Ekind (T, E_Enumeration_Subtype);
2114 Set_Etype (T, Base);
2115 Init_Size (T, 8);
2116 Init_Alignment (T);
2117 Set_Is_Generic_Type (T);
2118 Set_Is_Constrained (T);
2119
2120 -- For semantic analysis, the bounds of the type must be set to some
2121 -- non-static value. The simplest is to create attribute nodes for those
2122 -- bounds, that refer to the type itself. These bounds are never
2123 -- analyzed but serve as place-holders.
2124
2125 Lo :=
2126 Make_Attribute_Reference (Loc,
2127 Attribute_Name => Name_First,
2128 Prefix => New_Occurrence_Of (T, Loc));
2129 Set_Etype (Lo, T);
2130
2131 Hi :=
2132 Make_Attribute_Reference (Loc,
2133 Attribute_Name => Name_Last,
2134 Prefix => New_Occurrence_Of (T, Loc));
2135 Set_Etype (Hi, T);
2136
2137 Set_Scalar_Range (T,
2138 Make_Range (Loc,
2139 Low_Bound => Lo,
2140 High_Bound => Hi));
2141
2142 Set_Ekind (Base, E_Enumeration_Type);
2143 Set_Etype (Base, Base);
2144 Init_Size (Base, 8);
2145 Init_Alignment (Base);
2146 Set_Is_Generic_Type (Base);
2147 Set_Scalar_Range (Base, Scalar_Range (T));
2148 Set_Parent (Base, Parent (Def));
2149 end Analyze_Formal_Discrete_Type;
2150
2151 ----------------------------------
2152 -- Analyze_Formal_Floating_Type --
2153 ---------------------------------
2154
2155 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2156 Base : constant Entity_Id :=
2157 New_Internal_Entity
2158 (E_Floating_Point_Type, Current_Scope,
2159 Sloc (Defining_Identifier (Parent (Def))), 'G');
2160
2161 begin
2162 -- The various semantic attributes are taken from the predefined type
2163 -- Float, just so that all of them are initialized. Their values are
2164 -- never used because no constant folding or expansion takes place in
2165 -- the generic itself.
2166
2167 Enter_Name (T);
2168 Set_Ekind (T, E_Floating_Point_Subtype);
2169 Set_Etype (T, Base);
2170 Set_Size_Info (T, (Standard_Float));
2171 Set_RM_Size (T, RM_Size (Standard_Float));
2172 Set_Digits_Value (T, Digits_Value (Standard_Float));
2173 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2174 Set_Is_Constrained (T);
2175
2176 Set_Is_Generic_Type (Base);
2177 Set_Etype (Base, Base);
2178 Set_Size_Info (Base, (Standard_Float));
2179 Set_RM_Size (Base, RM_Size (Standard_Float));
2180 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2181 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2182 Set_Parent (Base, Parent (Def));
2183
2184 Check_Restriction (No_Floating_Point, Def);
2185 end Analyze_Formal_Floating_Type;
2186
2187 -----------------------------------
2188 -- Analyze_Formal_Interface_Type;--
2189 -----------------------------------
2190
2191 procedure Analyze_Formal_Interface_Type
2192 (N : Node_Id;
2193 T : Entity_Id;
2194 Def : Node_Id)
2195 is
2196 Loc : constant Source_Ptr := Sloc (N);
2197 New_N : Node_Id;
2198
2199 begin
2200 New_N :=
2201 Make_Full_Type_Declaration (Loc,
2202 Defining_Identifier => T,
2203 Type_Definition => Def);
2204
2205 Rewrite (N, New_N);
2206 Analyze (N);
2207 Set_Is_Generic_Type (T);
2208 end Analyze_Formal_Interface_Type;
2209
2210 ---------------------------------
2211 -- Analyze_Formal_Modular_Type --
2212 ---------------------------------
2213
2214 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2215 begin
2216 -- Apart from their entity kind, generic modular types are treated like
2217 -- signed integer types, and have the same attributes.
2218
2219 Analyze_Formal_Signed_Integer_Type (T, Def);
2220 Set_Ekind (T, E_Modular_Integer_Subtype);
2221 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2222
2223 end Analyze_Formal_Modular_Type;
2224
2225 ---------------------------------------
2226 -- Analyze_Formal_Object_Declaration --
2227 ---------------------------------------
2228
2229 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2230 E : constant Node_Id := Default_Expression (N);
2231 Id : constant Node_Id := Defining_Identifier (N);
2232 K : Entity_Kind;
2233 T : Node_Id;
2234
2235 begin
2236 Enter_Name (Id);
2237
2238 -- Determine the mode of the formal object
2239
2240 if Out_Present (N) then
2241 K := E_Generic_In_Out_Parameter;
2242
2243 if not In_Present (N) then
2244 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2245 end if;
2246
2247 else
2248 K := E_Generic_In_Parameter;
2249 end if;
2250
2251 if Present (Subtype_Mark (N)) then
2252 Find_Type (Subtype_Mark (N));
2253 T := Entity (Subtype_Mark (N));
2254
2255 -- Verify that there is no redundant null exclusion
2256
2257 if Null_Exclusion_Present (N) then
2258 if not Is_Access_Type (T) then
2259 Error_Msg_N
2260 ("null exclusion can only apply to an access type", N);
2261
2262 elsif Can_Never_Be_Null (T) then
2263 Error_Msg_NE
2264 ("`NOT NULL` not allowed (& already excludes null)",
2265 N, T);
2266 end if;
2267 end if;
2268
2269 -- Ada 2005 (AI-423): Formal object with an access definition
2270
2271 else
2272 Check_Access_Definition (N);
2273 T := Access_Definition
2274 (Related_Nod => N,
2275 N => Access_Definition (N));
2276 end if;
2277
2278 if Ekind (T) = E_Incomplete_Type then
2279 declare
2280 Error_Node : Node_Id;
2281
2282 begin
2283 if Present (Subtype_Mark (N)) then
2284 Error_Node := Subtype_Mark (N);
2285 else
2286 Check_Access_Definition (N);
2287 Error_Node := Access_Definition (N);
2288 end if;
2289
2290 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2291 end;
2292 end if;
2293
2294 if K = E_Generic_In_Parameter then
2295
2296 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2297
2298 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2299 Error_Msg_N
2300 ("generic formal of mode IN must not be of limited type", N);
2301 Explain_Limited_Type (T, N);
2302 end if;
2303
2304 if Is_Abstract_Type (T) then
2305 Error_Msg_N
2306 ("generic formal of mode IN must not be of abstract type", N);
2307 end if;
2308
2309 if Present (E) then
2310 Preanalyze_Spec_Expression (E, T);
2311
2312 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2313 Error_Msg_N
2314 ("initialization not allowed for limited types", E);
2315 Explain_Limited_Type (T, E);
2316 end if;
2317 end if;
2318
2319 Set_Ekind (Id, K);
2320 Set_Etype (Id, T);
2321
2322 -- Case of generic IN OUT parameter
2323
2324 else
2325 -- If the formal has an unconstrained type, construct its actual
2326 -- subtype, as is done for subprogram formals. In this fashion, all
2327 -- its uses can refer to specific bounds.
2328
2329 Set_Ekind (Id, K);
2330 Set_Etype (Id, T);
2331
2332 if (Is_Array_Type (T)
2333 and then not Is_Constrained (T))
2334 or else
2335 (Ekind (T) = E_Record_Type
2336 and then Has_Discriminants (T))
2337 then
2338 declare
2339 Non_Freezing_Ref : constant Node_Id :=
2340 New_Occurrence_Of (Id, Sloc (Id));
2341 Decl : Node_Id;
2342
2343 begin
2344 -- Make sure the actual subtype doesn't generate bogus freezing
2345
2346 Set_Must_Not_Freeze (Non_Freezing_Ref);
2347 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2348 Insert_Before_And_Analyze (N, Decl);
2349 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2350 end;
2351 else
2352 Set_Actual_Subtype (Id, T);
2353 end if;
2354
2355 if Present (E) then
2356 Error_Msg_N
2357 ("initialization not allowed for `IN OUT` formals", N);
2358 end if;
2359 end if;
2360
2361 if Has_Aspects (N) then
2362 Analyze_Aspect_Specifications (N, Id);
2363 end if;
2364 end Analyze_Formal_Object_Declaration;
2365
2366 ----------------------------------------------
2367 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2368 ----------------------------------------------
2369
2370 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2371 (T : Entity_Id;
2372 Def : Node_Id)
2373 is
2374 Loc : constant Source_Ptr := Sloc (Def);
2375 Base : constant Entity_Id :=
2376 New_Internal_Entity
2377 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2378 Sloc (Defining_Identifier (Parent (Def))), 'G');
2379
2380 begin
2381 -- The semantic attributes are set for completeness only, their values
2382 -- will never be used, since all properties of the type are non-static.
2383
2384 Enter_Name (T);
2385 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2386 Set_Etype (T, Base);
2387 Set_Size_Info (T, Standard_Integer);
2388 Set_RM_Size (T, RM_Size (Standard_Integer));
2389 Set_Small_Value (T, Ureal_1);
2390 Set_Delta_Value (T, Ureal_1);
2391 Set_Scalar_Range (T,
2392 Make_Range (Loc,
2393 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2394 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2395 Set_Is_Constrained (T);
2396
2397 Set_Is_Generic_Type (Base);
2398 Set_Etype (Base, Base);
2399 Set_Size_Info (Base, Standard_Integer);
2400 Set_RM_Size (Base, RM_Size (Standard_Integer));
2401 Set_Small_Value (Base, Ureal_1);
2402 Set_Delta_Value (Base, Ureal_1);
2403 Set_Scalar_Range (Base, Scalar_Range (T));
2404 Set_Parent (Base, Parent (Def));
2405
2406 Check_Restriction (No_Fixed_Point, Def);
2407 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2408
2409 ----------------------------------------
2410 -- Analyze_Formal_Package_Declaration --
2411 ----------------------------------------
2412
2413 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2414 Loc : constant Source_Ptr := Sloc (N);
2415 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2416 Formal : Entity_Id;
2417 Gen_Id : constant Node_Id := Name (N);
2418 Gen_Decl : Node_Id;
2419 Gen_Unit : Entity_Id;
2420 New_N : Node_Id;
2421 Parent_Installed : Boolean := False;
2422 Renaming : Node_Id;
2423 Parent_Instance : Entity_Id;
2424 Renaming_In_Par : Entity_Id;
2425 Associations : Boolean := True;
2426
2427 Vis_Prims_List : Elist_Id := No_Elist;
2428 -- List of primitives made temporarily visible in the instantiation
2429 -- to match the visibility of the formal type
2430
2431 function Build_Local_Package return Node_Id;
2432 -- The formal package is rewritten so that its parameters are replaced
2433 -- with corresponding declarations. For parameters with bona fide
2434 -- associations these declarations are created by Analyze_Associations
2435 -- as for a regular instantiation. For boxed parameters, we preserve
2436 -- the formal declarations and analyze them, in order to introduce
2437 -- entities of the right kind in the environment of the formal.
2438
2439 -------------------------
2440 -- Build_Local_Package --
2441 -------------------------
2442
2443 function Build_Local_Package return Node_Id is
2444 Decls : List_Id;
2445 Pack_Decl : Node_Id;
2446
2447 begin
2448 -- Within the formal, the name of the generic package is a renaming
2449 -- of the formal (as for a regular instantiation).
2450
2451 Pack_Decl :=
2452 Make_Package_Declaration (Loc,
2453 Specification =>
2454 Copy_Generic_Node
2455 (Specification (Original_Node (Gen_Decl)),
2456 Empty, Instantiating => True));
2457
2458 Renaming := Make_Package_Renaming_Declaration (Loc,
2459 Defining_Unit_Name =>
2460 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2461 Name => New_Occurrence_Of (Formal, Loc));
2462
2463 if Nkind (Gen_Id) = N_Identifier
2464 and then Chars (Gen_Id) = Chars (Pack_Id)
2465 then
2466 Error_Msg_NE
2467 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2468 end if;
2469
2470 -- If the formal is declared with a box, or with an others choice,
2471 -- create corresponding declarations for all entities in the formal
2472 -- part, so that names with the proper types are available in the
2473 -- specification of the formal package.
2474
2475 -- On the other hand, if there are no associations, then all the
2476 -- formals must have defaults, and this will be checked by the
2477 -- call to Analyze_Associations.
2478
2479 if Box_Present (N)
2480 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2481 then
2482 declare
2483 Formal_Decl : Node_Id;
2484
2485 begin
2486 -- TBA : for a formal package, need to recurse ???
2487
2488 Decls := New_List;
2489 Formal_Decl :=
2490 First
2491 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2492 while Present (Formal_Decl) loop
2493 Append_To
2494 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2495 Next (Formal_Decl);
2496 end loop;
2497 end;
2498
2499 -- If generic associations are present, use Analyze_Associations to
2500 -- create the proper renaming declarations.
2501
2502 else
2503 declare
2504 Act_Tree : constant Node_Id :=
2505 Copy_Generic_Node
2506 (Original_Node (Gen_Decl), Empty,
2507 Instantiating => True);
2508
2509 begin
2510 Generic_Renamings.Set_Last (0);
2511 Generic_Renamings_HTable.Reset;
2512 Instantiation_Node := N;
2513
2514 Decls :=
2515 Analyze_Associations
2516 (I_Node => Original_Node (N),
2517 Formals => Generic_Formal_Declarations (Act_Tree),
2518 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2519
2520 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2521 end;
2522 end if;
2523
2524 Append (Renaming, To => Decls);
2525
2526 -- Add generated declarations ahead of local declarations in
2527 -- the package.
2528
2529 if No (Visible_Declarations (Specification (Pack_Decl))) then
2530 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2531 else
2532 Insert_List_Before
2533 (First (Visible_Declarations (Specification (Pack_Decl))),
2534 Decls);
2535 end if;
2536
2537 return Pack_Decl;
2538 end Build_Local_Package;
2539
2540 -- Start of processing for Analyze_Formal_Package_Declaration
2541
2542 begin
2543 Check_Text_IO_Special_Unit (Gen_Id);
2544
2545 Init_Env;
2546 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2547 Gen_Unit := Entity (Gen_Id);
2548
2549 -- Check for a formal package that is a package renaming
2550
2551 if Present (Renamed_Object (Gen_Unit)) then
2552
2553 -- Indicate that unit is used, before replacing it with renamed
2554 -- entity for use below.
2555
2556 if In_Extended_Main_Source_Unit (N) then
2557 Set_Is_Instantiated (Gen_Unit);
2558 Generate_Reference (Gen_Unit, N);
2559 end if;
2560
2561 Gen_Unit := Renamed_Object (Gen_Unit);
2562 end if;
2563
2564 if Ekind (Gen_Unit) /= E_Generic_Package then
2565 Error_Msg_N ("expect generic package name", Gen_Id);
2566 Restore_Env;
2567 goto Leave;
2568
2569 elsif Gen_Unit = Current_Scope then
2570 Error_Msg_N
2571 ("generic package cannot be used as a formal package of itself",
2572 Gen_Id);
2573 Restore_Env;
2574 goto Leave;
2575
2576 elsif In_Open_Scopes (Gen_Unit) then
2577 if Is_Compilation_Unit (Gen_Unit)
2578 and then Is_Child_Unit (Current_Scope)
2579 then
2580 -- Special-case the error when the formal is a parent, and
2581 -- continue analysis to minimize cascaded errors.
2582
2583 Error_Msg_N
2584 ("generic parent cannot be used as formal package "
2585 & "of a child unit",
2586 Gen_Id);
2587
2588 else
2589 Error_Msg_N
2590 ("generic package cannot be used as a formal package "
2591 & "within itself",
2592 Gen_Id);
2593 Restore_Env;
2594 goto Leave;
2595 end if;
2596 end if;
2597
2598 -- Check that name of formal package does not hide name of generic,
2599 -- or its leading prefix. This check must be done separately because
2600 -- the name of the generic has already been analyzed.
2601
2602 declare
2603 Gen_Name : Entity_Id;
2604
2605 begin
2606 Gen_Name := Gen_Id;
2607 while Nkind (Gen_Name) = N_Expanded_Name loop
2608 Gen_Name := Prefix (Gen_Name);
2609 end loop;
2610
2611 if Chars (Gen_Name) = Chars (Pack_Id) then
2612 Error_Msg_NE
2613 ("& is hidden within declaration of formal package",
2614 Gen_Id, Gen_Name);
2615 end if;
2616 end;
2617
2618 if Box_Present (N)
2619 or else No (Generic_Associations (N))
2620 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2621 then
2622 Associations := False;
2623 end if;
2624
2625 -- If there are no generic associations, the generic parameters appear
2626 -- as local entities and are instantiated like them. We copy the generic
2627 -- package declaration as if it were an instantiation, and analyze it
2628 -- like a regular package, except that we treat the formals as
2629 -- additional visible components.
2630
2631 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2632
2633 if In_Extended_Main_Source_Unit (N) then
2634 Set_Is_Instantiated (Gen_Unit);
2635 Generate_Reference (Gen_Unit, N);
2636 end if;
2637
2638 Formal := New_Copy (Pack_Id);
2639 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2640
2641 begin
2642 -- Make local generic without formals. The formals will be replaced
2643 -- with internal declarations.
2644
2645 New_N := Build_Local_Package;
2646
2647 -- If there are errors in the parameter list, Analyze_Associations
2648 -- raises Instantiation_Error. Patch the declaration to prevent
2649 -- further exception propagation.
2650
2651 exception
2652 when Instantiation_Error =>
2653
2654 Enter_Name (Formal);
2655 Set_Ekind (Formal, E_Variable);
2656 Set_Etype (Formal, Any_Type);
2657 Restore_Hidden_Primitives (Vis_Prims_List);
2658
2659 if Parent_Installed then
2660 Remove_Parent;
2661 end if;
2662
2663 goto Leave;
2664 end;
2665
2666 Rewrite (N, New_N);
2667 Set_Defining_Unit_Name (Specification (New_N), Formal);
2668 Set_Generic_Parent (Specification (N), Gen_Unit);
2669 Set_Instance_Env (Gen_Unit, Formal);
2670 Set_Is_Generic_Instance (Formal);
2671
2672 Enter_Name (Formal);
2673 Set_Ekind (Formal, E_Package);
2674 Set_Etype (Formal, Standard_Void_Type);
2675 Set_Inner_Instances (Formal, New_Elmt_List);
2676 Push_Scope (Formal);
2677
2678 if Is_Child_Unit (Gen_Unit)
2679 and then Parent_Installed
2680 then
2681 -- Similarly, we have to make the name of the formal visible in the
2682 -- parent instance, to resolve properly fully qualified names that
2683 -- may appear in the generic unit. The parent instance has been
2684 -- placed on the scope stack ahead of the current scope.
2685
2686 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2687
2688 Renaming_In_Par :=
2689 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2690 Set_Ekind (Renaming_In_Par, E_Package);
2691 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2692 Set_Scope (Renaming_In_Par, Parent_Instance);
2693 Set_Parent (Renaming_In_Par, Parent (Formal));
2694 Set_Renamed_Object (Renaming_In_Par, Formal);
2695 Append_Entity (Renaming_In_Par, Parent_Instance);
2696 end if;
2697
2698 Analyze (Specification (N));
2699
2700 -- The formals for which associations are provided are not visible
2701 -- outside of the formal package. The others are still declared by a
2702 -- formal parameter declaration.
2703
2704 -- If there are no associations, the only local entity to hide is the
2705 -- generated package renaming itself.
2706
2707 declare
2708 E : Entity_Id;
2709
2710 begin
2711 E := First_Entity (Formal);
2712 while Present (E) loop
2713 if Associations
2714 and then not Is_Generic_Formal (E)
2715 then
2716 Set_Is_Hidden (E);
2717 end if;
2718
2719 if Ekind (E) = E_Package
2720 and then Renamed_Entity (E) = Formal
2721 then
2722 Set_Is_Hidden (E);
2723 exit;
2724 end if;
2725
2726 Next_Entity (E);
2727 end loop;
2728 end;
2729
2730 End_Package_Scope (Formal);
2731 Restore_Hidden_Primitives (Vis_Prims_List);
2732
2733 if Parent_Installed then
2734 Remove_Parent;
2735 end if;
2736
2737 Restore_Env;
2738
2739 -- Inside the generic unit, the formal package is a regular package, but
2740 -- no body is needed for it. Note that after instantiation, the defining
2741 -- unit name we need is in the new tree and not in the original (see
2742 -- Package_Instantiation). A generic formal package is an instance, and
2743 -- can be used as an actual for an inner instance.
2744
2745 Set_Has_Completion (Formal, True);
2746
2747 -- Add semantic information to the original defining identifier.
2748 -- for ASIS use.
2749
2750 Set_Ekind (Pack_Id, E_Package);
2751 Set_Etype (Pack_Id, Standard_Void_Type);
2752 Set_Scope (Pack_Id, Scope (Formal));
2753 Set_Has_Completion (Pack_Id, True);
2754
2755 <<Leave>>
2756 if Has_Aspects (N) then
2757 Analyze_Aspect_Specifications (N, Pack_Id);
2758 end if;
2759 end Analyze_Formal_Package_Declaration;
2760
2761 ---------------------------------
2762 -- Analyze_Formal_Private_Type --
2763 ---------------------------------
2764
2765 procedure Analyze_Formal_Private_Type
2766 (N : Node_Id;
2767 T : Entity_Id;
2768 Def : Node_Id)
2769 is
2770 begin
2771 New_Private_Type (N, T, Def);
2772
2773 -- Set the size to an arbitrary but legal value
2774
2775 Set_Size_Info (T, Standard_Integer);
2776 Set_RM_Size (T, RM_Size (Standard_Integer));
2777 end Analyze_Formal_Private_Type;
2778
2779 ------------------------------------
2780 -- Analyze_Formal_Incomplete_Type --
2781 ------------------------------------
2782
2783 procedure Analyze_Formal_Incomplete_Type
2784 (T : Entity_Id;
2785 Def : Node_Id)
2786 is
2787 begin
2788 Enter_Name (T);
2789 Set_Ekind (T, E_Incomplete_Type);
2790 Set_Etype (T, T);
2791 Set_Private_Dependents (T, New_Elmt_List);
2792
2793 if Tagged_Present (Def) then
2794 Set_Is_Tagged_Type (T);
2795 Make_Class_Wide_Type (T);
2796 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2797 end if;
2798 end Analyze_Formal_Incomplete_Type;
2799
2800 ----------------------------------------
2801 -- Analyze_Formal_Signed_Integer_Type --
2802 ----------------------------------------
2803
2804 procedure Analyze_Formal_Signed_Integer_Type
2805 (T : Entity_Id;
2806 Def : Node_Id)
2807 is
2808 Base : constant Entity_Id :=
2809 New_Internal_Entity
2810 (E_Signed_Integer_Type,
2811 Current_Scope,
2812 Sloc (Defining_Identifier (Parent (Def))), 'G');
2813
2814 begin
2815 Enter_Name (T);
2816
2817 Set_Ekind (T, E_Signed_Integer_Subtype);
2818 Set_Etype (T, Base);
2819 Set_Size_Info (T, Standard_Integer);
2820 Set_RM_Size (T, RM_Size (Standard_Integer));
2821 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2822 Set_Is_Constrained (T);
2823
2824 Set_Is_Generic_Type (Base);
2825 Set_Size_Info (Base, Standard_Integer);
2826 Set_RM_Size (Base, RM_Size (Standard_Integer));
2827 Set_Etype (Base, Base);
2828 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2829 Set_Parent (Base, Parent (Def));
2830 end Analyze_Formal_Signed_Integer_Type;
2831
2832 -------------------------------------------
2833 -- Analyze_Formal_Subprogram_Declaration --
2834 -------------------------------------------
2835
2836 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2837 Spec : constant Node_Id := Specification (N);
2838 Def : constant Node_Id := Default_Name (N);
2839 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2840 Subp : Entity_Id;
2841
2842 begin
2843 if Nam = Error then
2844 return;
2845 end if;
2846
2847 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2848 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2849 goto Leave;
2850 end if;
2851
2852 Analyze_Subprogram_Declaration (N);
2853 Set_Is_Formal_Subprogram (Nam);
2854 Set_Has_Completion (Nam);
2855
2856 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2857 Set_Is_Abstract_Subprogram (Nam);
2858 Set_Is_Dispatching_Operation (Nam);
2859
2860 declare
2861 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2862 begin
2863 if No (Ctrl_Type) then
2864 Error_Msg_N
2865 ("abstract formal subprogram must have a controlling type",
2866 N);
2867
2868 elsif Ada_Version >= Ada_2012
2869 and then Is_Incomplete_Type (Ctrl_Type)
2870 then
2871 Error_Msg_NE
2872 ("controlling type of abstract formal subprogram cannot " &
2873 "be incomplete type", N, Ctrl_Type);
2874
2875 else
2876 Check_Controlling_Formals (Ctrl_Type, Nam);
2877 end if;
2878 end;
2879 end if;
2880
2881 -- Default name is resolved at the point of instantiation
2882
2883 if Box_Present (N) then
2884 null;
2885
2886 -- Else default is bound at the point of generic declaration
2887
2888 elsif Present (Def) then
2889 if Nkind (Def) = N_Operator_Symbol then
2890 Find_Direct_Name (Def);
2891
2892 elsif Nkind (Def) /= N_Attribute_Reference then
2893 Analyze (Def);
2894
2895 else
2896 -- For an attribute reference, analyze the prefix and verify
2897 -- that it has the proper profile for the subprogram.
2898
2899 Analyze (Prefix (Def));
2900 Valid_Default_Attribute (Nam, Def);
2901 goto Leave;
2902 end if;
2903
2904 -- Default name may be overloaded, in which case the interpretation
2905 -- with the correct profile must be selected, as for a renaming.
2906 -- If the definition is an indexed component, it must denote a
2907 -- member of an entry family. If it is a selected component, it
2908 -- can be a protected operation.
2909
2910 if Etype (Def) = Any_Type then
2911 goto Leave;
2912
2913 elsif Nkind (Def) = N_Selected_Component then
2914 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2915 Error_Msg_N ("expect valid subprogram name as default", Def);
2916 end if;
2917
2918 elsif Nkind (Def) = N_Indexed_Component then
2919 if Is_Entity_Name (Prefix (Def)) then
2920 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2921 Error_Msg_N ("expect valid subprogram name as default", Def);
2922 end if;
2923
2924 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2925 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2926 E_Entry_Family
2927 then
2928 Error_Msg_N ("expect valid subprogram name as default", Def);
2929 end if;
2930
2931 else
2932 Error_Msg_N ("expect valid subprogram name as default", Def);
2933 goto Leave;
2934 end if;
2935
2936 elsif Nkind (Def) = N_Character_Literal then
2937
2938 -- Needs some type checks: subprogram should be parameterless???
2939
2940 Resolve (Def, (Etype (Nam)));
2941
2942 elsif not Is_Entity_Name (Def)
2943 or else not Is_Overloadable (Entity (Def))
2944 then
2945 Error_Msg_N ("expect valid subprogram name as default", Def);
2946 goto Leave;
2947
2948 elsif not Is_Overloaded (Def) then
2949 Subp := Entity (Def);
2950
2951 if Subp = Nam then
2952 Error_Msg_N ("premature usage of formal subprogram", Def);
2953
2954 elsif not Entity_Matches_Spec (Subp, Nam) then
2955 Error_Msg_N ("no visible entity matches specification", Def);
2956 end if;
2957
2958 -- More than one interpretation, so disambiguate as for a renaming
2959
2960 else
2961 declare
2962 I : Interp_Index;
2963 I1 : Interp_Index := 0;
2964 It : Interp;
2965 It1 : Interp;
2966
2967 begin
2968 Subp := Any_Id;
2969 Get_First_Interp (Def, I, It);
2970 while Present (It.Nam) loop
2971 if Entity_Matches_Spec (It.Nam, Nam) then
2972 if Subp /= Any_Id then
2973 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2974
2975 if It1 = No_Interp then
2976 Error_Msg_N ("ambiguous default subprogram", Def);
2977 else
2978 Subp := It1.Nam;
2979 end if;
2980
2981 exit;
2982
2983 else
2984 I1 := I;
2985 Subp := It.Nam;
2986 end if;
2987 end if;
2988
2989 Get_Next_Interp (I, It);
2990 end loop;
2991 end;
2992
2993 if Subp /= Any_Id then
2994
2995 -- Subprogram found, generate reference to it
2996
2997 Set_Entity (Def, Subp);
2998 Generate_Reference (Subp, Def);
2999
3000 if Subp = Nam then
3001 Error_Msg_N ("premature usage of formal subprogram", Def);
3002
3003 elsif Ekind (Subp) /= E_Operator then
3004 Check_Mode_Conformant (Subp, Nam);
3005 end if;
3006
3007 else
3008 Error_Msg_N ("no visible subprogram matches specification", N);
3009 end if;
3010 end if;
3011 end if;
3012
3013 <<Leave>>
3014 if Has_Aspects (N) then
3015 Analyze_Aspect_Specifications (N, Nam);
3016 end if;
3017
3018 end Analyze_Formal_Subprogram_Declaration;
3019
3020 -------------------------------------
3021 -- Analyze_Formal_Type_Declaration --
3022 -------------------------------------
3023
3024 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3025 Def : constant Node_Id := Formal_Type_Definition (N);
3026 T : Entity_Id;
3027
3028 begin
3029 T := Defining_Identifier (N);
3030
3031 if Present (Discriminant_Specifications (N))
3032 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3033 then
3034 Error_Msg_N
3035 ("discriminants not allowed for this formal type", T);
3036 end if;
3037
3038 -- Enter the new name, and branch to specific routine
3039
3040 case Nkind (Def) is
3041 when N_Formal_Private_Type_Definition =>
3042 Analyze_Formal_Private_Type (N, T, Def);
3043
3044 when N_Formal_Derived_Type_Definition =>
3045 Analyze_Formal_Derived_Type (N, T, Def);
3046
3047 when N_Formal_Incomplete_Type_Definition =>
3048 Analyze_Formal_Incomplete_Type (T, Def);
3049
3050 when N_Formal_Discrete_Type_Definition =>
3051 Analyze_Formal_Discrete_Type (T, Def);
3052
3053 when N_Formal_Signed_Integer_Type_Definition =>
3054 Analyze_Formal_Signed_Integer_Type (T, Def);
3055
3056 when N_Formal_Modular_Type_Definition =>
3057 Analyze_Formal_Modular_Type (T, Def);
3058
3059 when N_Formal_Floating_Point_Definition =>
3060 Analyze_Formal_Floating_Type (T, Def);
3061
3062 when N_Formal_Ordinary_Fixed_Point_Definition =>
3063 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3064
3065 when N_Formal_Decimal_Fixed_Point_Definition =>
3066 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3067
3068 when N_Array_Type_Definition =>
3069 Analyze_Formal_Array_Type (T, Def);
3070
3071 when N_Access_To_Object_Definition |
3072 N_Access_Function_Definition |
3073 N_Access_Procedure_Definition =>
3074 Analyze_Generic_Access_Type (T, Def);
3075
3076 -- Ada 2005: a interface declaration is encoded as an abstract
3077 -- record declaration or a abstract type derivation.
3078
3079 when N_Record_Definition =>
3080 Analyze_Formal_Interface_Type (N, T, Def);
3081
3082 when N_Derived_Type_Definition =>
3083 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3084
3085 when N_Error =>
3086 null;
3087
3088 when others =>
3089 raise Program_Error;
3090
3091 end case;
3092
3093 Set_Is_Generic_Type (T);
3094
3095 if Has_Aspects (N) then
3096 Analyze_Aspect_Specifications (N, T);
3097 end if;
3098 end Analyze_Formal_Type_Declaration;
3099
3100 ------------------------------------
3101 -- Analyze_Function_Instantiation --
3102 ------------------------------------
3103
3104 procedure Analyze_Function_Instantiation (N : Node_Id) is
3105 begin
3106 Analyze_Subprogram_Instantiation (N, E_Function);
3107 end Analyze_Function_Instantiation;
3108
3109 ---------------------------------
3110 -- Analyze_Generic_Access_Type --
3111 ---------------------------------
3112
3113 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3114 begin
3115 Enter_Name (T);
3116
3117 if Nkind (Def) = N_Access_To_Object_Definition then
3118 Access_Type_Declaration (T, Def);
3119
3120 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3121 and then No (Full_View (Designated_Type (T)))
3122 and then not Is_Generic_Type (Designated_Type (T))
3123 then
3124 Error_Msg_N ("premature usage of incomplete type", Def);
3125
3126 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3127 Error_Msg_N
3128 ("only a subtype mark is allowed in a formal", Def);
3129 end if;
3130
3131 else
3132 Access_Subprogram_Declaration (T, Def);
3133 end if;
3134 end Analyze_Generic_Access_Type;
3135
3136 ---------------------------------
3137 -- Analyze_Generic_Formal_Part --
3138 ---------------------------------
3139
3140 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3141 Gen_Parm_Decl : Node_Id;
3142
3143 begin
3144 -- The generic formals are processed in the scope of the generic unit,
3145 -- where they are immediately visible. The scope is installed by the
3146 -- caller.
3147
3148 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3149
3150 while Present (Gen_Parm_Decl) loop
3151 Analyze (Gen_Parm_Decl);
3152 Next (Gen_Parm_Decl);
3153 end loop;
3154
3155 Generate_Reference_To_Generic_Formals (Current_Scope);
3156 end Analyze_Generic_Formal_Part;
3157
3158 ------------------------------------------
3159 -- Analyze_Generic_Package_Declaration --
3160 ------------------------------------------
3161
3162 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3163 Loc : constant Source_Ptr := Sloc (N);
3164 Id : Entity_Id;
3165 New_N : Node_Id;
3166 Save_Parent : Node_Id;
3167 Renaming : Node_Id;
3168 Decls : constant List_Id :=
3169 Visible_Declarations (Specification (N));
3170 Decl : Node_Id;
3171
3172 begin
3173 Check_SPARK_Restriction ("generic is not allowed", N);
3174
3175 -- We introduce a renaming of the enclosing package, to have a usable
3176 -- entity as the prefix of an expanded name for a local entity of the
3177 -- form Par.P.Q, where P is the generic package. This is because a local
3178 -- entity named P may hide it, so that the usual visibility rules in
3179 -- the instance will not resolve properly.
3180
3181 Renaming :=
3182 Make_Package_Renaming_Declaration (Loc,
3183 Defining_Unit_Name =>
3184 Make_Defining_Identifier (Loc,
3185 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3186 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
3187
3188 if Present (Decls) then
3189 Decl := First (Decls);
3190 while Present (Decl)
3191 and then Nkind (Decl) = N_Pragma
3192 loop
3193 Next (Decl);
3194 end loop;
3195
3196 if Present (Decl) then
3197 Insert_Before (Decl, Renaming);
3198 else
3199 Append (Renaming, Visible_Declarations (Specification (N)));
3200 end if;
3201
3202 else
3203 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3204 end if;
3205
3206 -- Create copy of generic unit, and save for instantiation. If the unit
3207 -- is a child unit, do not copy the specifications for the parent, which
3208 -- are not part of the generic tree.
3209
3210 Save_Parent := Parent_Spec (N);
3211 Set_Parent_Spec (N, Empty);
3212
3213 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3214 Set_Parent_Spec (New_N, Save_Parent);
3215 Rewrite (N, New_N);
3216
3217 -- Once the contents of the generic copy and the template are swapped,
3218 -- do the same for their respective aspect specifications.
3219
3220 Exchange_Aspects (N, New_N);
3221 Id := Defining_Entity (N);
3222 Generate_Definition (Id);
3223
3224 -- Expansion is not applied to generic units
3225
3226 Start_Generic;
3227
3228 Enter_Name (Id);
3229 Set_Ekind (Id, E_Generic_Package);
3230 Set_Etype (Id, Standard_Void_Type);
3231 Set_Contract (Id, Make_Contract (Sloc (Id)));
3232
3233 -- Analyze aspects now, so that generated pragmas appear in the
3234 -- declarations before building and analyzing the generic copy.
3235
3236 if Has_Aspects (N) then
3237 Analyze_Aspect_Specifications (N, Id);
3238 end if;
3239
3240 Push_Scope (Id);
3241 Enter_Generic_Scope (Id);
3242 Set_Inner_Instances (Id, New_Elmt_List);
3243
3244 Set_Categorization_From_Pragmas (N);
3245 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3246
3247 -- Link the declaration of the generic homonym in the generic copy to
3248 -- the package it renames, so that it is always resolved properly.
3249
3250 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3251 Set_Entity (Associated_Node (Name (Renaming)), Id);
3252
3253 -- For a library unit, we have reconstructed the entity for the unit,
3254 -- and must reset it in the library tables.
3255
3256 if Nkind (Parent (N)) = N_Compilation_Unit then
3257 Set_Cunit_Entity (Current_Sem_Unit, Id);
3258 end if;
3259
3260 Analyze_Generic_Formal_Part (N);
3261
3262 -- After processing the generic formals, analysis proceeds as for a
3263 -- non-generic package.
3264
3265 Analyze (Specification (N));
3266
3267 Validate_Categorization_Dependency (N, Id);
3268
3269 End_Generic;
3270
3271 End_Package_Scope (Id);
3272 Exit_Generic_Scope (Id);
3273
3274 if Nkind (Parent (N)) /= N_Compilation_Unit then
3275 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3276 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3277 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3278
3279 else
3280 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3281 Validate_RT_RAT_Component (N);
3282
3283 -- If this is a spec without a body, check that generic parameters
3284 -- are referenced.
3285
3286 if not Body_Required (Parent (N)) then
3287 Check_References (Id);
3288 end if;
3289 end if;
3290 end Analyze_Generic_Package_Declaration;
3291
3292 --------------------------------------------
3293 -- Analyze_Generic_Subprogram_Declaration --
3294 --------------------------------------------
3295
3296 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3297 Spec : Node_Id;
3298 Id : Entity_Id;
3299 Formals : List_Id;
3300 New_N : Node_Id;
3301 Result_Type : Entity_Id;
3302 Save_Parent : Node_Id;
3303 Typ : Entity_Id;
3304
3305 begin
3306 Check_SPARK_Restriction ("generic is not allowed", N);
3307
3308 -- Create copy of generic unit, and save for instantiation. If the unit
3309 -- is a child unit, do not copy the specifications for the parent, which
3310 -- are not part of the generic tree.
3311
3312 Save_Parent := Parent_Spec (N);
3313 Set_Parent_Spec (N, Empty);
3314
3315 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3316 Set_Parent_Spec (New_N, Save_Parent);
3317 Rewrite (N, New_N);
3318
3319 Check_SPARK_Mode_In_Generic (N);
3320
3321 -- The aspect specifications are not attached to the tree, and must
3322 -- be copied and attached to the generic copy explicitly.
3323
3324 if Present (Aspect_Specifications (New_N)) then
3325 declare
3326 Aspects : constant List_Id := Aspect_Specifications (N);
3327 begin
3328 Set_Has_Aspects (N, False);
3329 Move_Aspects (New_N, To => N);
3330 Set_Has_Aspects (Original_Node (N), False);
3331 Set_Aspect_Specifications (Original_Node (N), Aspects);
3332 end;
3333 end if;
3334
3335 Spec := Specification (N);
3336 Id := Defining_Entity (Spec);
3337 Generate_Definition (Id);
3338 Set_Contract (Id, Make_Contract (Sloc (Id)));
3339
3340 if Nkind (Id) = N_Defining_Operator_Symbol then
3341 Error_Msg_N
3342 ("operator symbol not allowed for generic subprogram", Id);
3343 end if;
3344
3345 Start_Generic;
3346
3347 Enter_Name (Id);
3348
3349 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3350 Push_Scope (Id);
3351 Enter_Generic_Scope (Id);
3352 Set_Inner_Instances (Id, New_Elmt_List);
3353 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3354
3355 Analyze_Generic_Formal_Part (N);
3356
3357 Formals := Parameter_Specifications (Spec);
3358
3359 if Present (Formals) then
3360 Process_Formals (Formals, Spec);
3361 end if;
3362
3363 if Nkind (Spec) = N_Function_Specification then
3364 Set_Ekind (Id, E_Generic_Function);
3365
3366 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3367 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3368 Set_Etype (Id, Result_Type);
3369
3370 -- Check restriction imposed by AI05-073: a generic function
3371 -- cannot return an abstract type or an access to such.
3372
3373 -- This is a binding interpretation should it apply to earlier
3374 -- versions of Ada as well as Ada 2012???
3375
3376 if Is_Abstract_Type (Designated_Type (Result_Type))
3377 and then Ada_Version >= Ada_2012
3378 then
3379 Error_Msg_N ("generic function cannot have an access result"
3380 & " that designates an abstract type", Spec);
3381 end if;
3382
3383 else
3384 Find_Type (Result_Definition (Spec));
3385 Typ := Entity (Result_Definition (Spec));
3386
3387 if Is_Abstract_Type (Typ)
3388 and then Ada_Version >= Ada_2012
3389 then
3390 Error_Msg_N
3391 ("generic function cannot have abstract result type", Spec);
3392 end if;
3393
3394 -- If a null exclusion is imposed on the result type, then create
3395 -- a null-excluding itype (an access subtype) and use it as the
3396 -- function's Etype.
3397
3398 if Is_Access_Type (Typ)
3399 and then Null_Exclusion_Present (Spec)
3400 then
3401 Set_Etype (Id,
3402 Create_Null_Excluding_Itype
3403 (T => Typ,
3404 Related_Nod => Spec,
3405 Scope_Id => Defining_Unit_Name (Spec)));
3406 else
3407 Set_Etype (Id, Typ);
3408 end if;
3409 end if;
3410
3411 else
3412 Set_Ekind (Id, E_Generic_Procedure);
3413 Set_Etype (Id, Standard_Void_Type);
3414 end if;
3415
3416 -- For a library unit, we have reconstructed the entity for the unit,
3417 -- and must reset it in the library tables. We also make sure that
3418 -- Body_Required is set properly in the original compilation unit node.
3419
3420 if Nkind (Parent (N)) = N_Compilation_Unit then
3421 Set_Cunit_Entity (Current_Sem_Unit, Id);
3422 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3423 end if;
3424
3425 Set_Categorization_From_Pragmas (N);
3426 Validate_Categorization_Dependency (N, Id);
3427
3428 Save_Global_References (Original_Node (N));
3429
3430 -- For ASIS purposes, convert any postcondition, precondition pragmas
3431 -- into aspects, if N is not a compilation unit by itself, in order to
3432 -- enable the analysis of expressions inside the corresponding PPC
3433 -- pragmas.
3434
3435 if ASIS_Mode and then Is_List_Member (N) then
3436 Make_Aspect_For_PPC_In_Gen_Sub_Decl (N);
3437 end if;
3438
3439 -- To capture global references, analyze the expressions of aspects,
3440 -- and propagate information to original tree. Note that in this case
3441 -- analysis of attributes is not delayed until the freeze point.
3442
3443 -- It seems very hard to recreate the proper visibility of the generic
3444 -- subprogram at a later point because the analysis of an aspect may
3445 -- create pragmas after the generic copies have been made ???
3446
3447 if Has_Aspects (N) then
3448 declare
3449 Aspect : Node_Id;
3450
3451 begin
3452 Aspect := First (Aspect_Specifications (N));
3453 while Present (Aspect) loop
3454 if Get_Aspect_Id (Aspect) /= Aspect_Warnings
3455 and then Present (Expression (Aspect))
3456 then
3457 Analyze (Expression (Aspect));
3458 end if;
3459
3460 Next (Aspect);
3461 end loop;
3462
3463 Aspect := First (Aspect_Specifications (Original_Node (N)));
3464 while Present (Aspect) loop
3465 if Present (Expression (Aspect)) then
3466 Save_Global_References (Expression (Aspect));
3467 end if;
3468
3469 Next (Aspect);
3470 end loop;
3471 end;
3472 end if;
3473
3474 End_Generic;
3475 End_Scope;
3476 Exit_Generic_Scope (Id);
3477 Generate_Reference_To_Formals (Id);
3478
3479 List_Inherited_Pre_Post_Aspects (Id);
3480 end Analyze_Generic_Subprogram_Declaration;
3481
3482 -----------------------------------
3483 -- Analyze_Package_Instantiation --
3484 -----------------------------------
3485
3486 procedure Analyze_Package_Instantiation (N : Node_Id) is
3487 Loc : constant Source_Ptr := Sloc (N);
3488 Gen_Id : constant Node_Id := Name (N);
3489
3490 Act_Decl : Node_Id;
3491 Act_Decl_Name : Node_Id;
3492 Act_Decl_Id : Entity_Id;
3493 Act_Spec : Node_Id;
3494 Act_Tree : Node_Id;
3495
3496 Gen_Decl : Node_Id;
3497 Gen_Unit : Entity_Id;
3498
3499 Is_Actual_Pack : constant Boolean :=
3500 Is_Internal (Defining_Entity (N));
3501
3502 Env_Installed : Boolean := False;
3503 Parent_Installed : Boolean := False;
3504 Renaming_List : List_Id;
3505 Unit_Renaming : Node_Id;
3506 Needs_Body : Boolean;
3507 Inline_Now : Boolean := False;
3508
3509 Save_Style_Check : constant Boolean := Style_Check;
3510 -- Save style check mode for restore on exit
3511
3512 procedure Delay_Descriptors (E : Entity_Id);
3513 -- Delay generation of subprogram descriptors for given entity
3514
3515 function Might_Inline_Subp return Boolean;
3516 -- If inlining is active and the generic contains inlined subprograms,
3517 -- we instantiate the body. This may cause superfluous instantiations,
3518 -- but it is simpler than detecting the need for the body at the point
3519 -- of inlining, when the context of the instance is not available.
3520
3521 function Must_Inline_Subp return Boolean;
3522 -- If inlining is active and the generic contains inlined subprograms,
3523 -- return True if some of the inlined subprograms must be inlined by
3524 -- the frontend.
3525
3526 -----------------------
3527 -- Delay_Descriptors --
3528 -----------------------
3529
3530 procedure Delay_Descriptors (E : Entity_Id) is
3531 begin
3532 if not Delay_Subprogram_Descriptors (E) then
3533 Set_Delay_Subprogram_Descriptors (E);
3534 Pending_Descriptor.Append (E);
3535 end if;
3536 end Delay_Descriptors;
3537
3538 -----------------------
3539 -- Might_Inline_Subp --
3540 -----------------------
3541
3542 function Might_Inline_Subp return Boolean is
3543 E : Entity_Id;
3544
3545 begin
3546 if not Inline_Processing_Required then
3547 return False;
3548
3549 else
3550 E := First_Entity (Gen_Unit);
3551 while Present (E) loop
3552 if Is_Subprogram (E)
3553 and then Is_Inlined (E)
3554 then
3555 return True;
3556 end if;
3557
3558 Next_Entity (E);
3559 end loop;
3560 end if;
3561
3562 return False;
3563 end Might_Inline_Subp;
3564
3565 ----------------------
3566 -- Must_Inline_Subp --
3567 ----------------------
3568
3569 function Must_Inline_Subp return Boolean is
3570 E : Entity_Id;
3571
3572 begin
3573 if not Inline_Processing_Required then
3574 return False;
3575
3576 else
3577 E := First_Entity (Gen_Unit);
3578 while Present (E) loop
3579 if Is_Subprogram (E)
3580 and then Is_Inlined (E)
3581 and then Must_Inline (E)
3582 then
3583 return True;
3584 end if;
3585
3586 Next_Entity (E);
3587 end loop;
3588 end if;
3589
3590 return False;
3591 end Must_Inline_Subp;
3592
3593 -- Local declarations
3594
3595 Vis_Prims_List : Elist_Id := No_Elist;
3596 -- List of primitives made temporarily visible in the instantiation
3597 -- to match the visibility of the formal type
3598
3599 -- Start of processing for Analyze_Package_Instantiation
3600
3601 begin
3602 Check_SPARK_Restriction ("generic is not allowed", N);
3603
3604 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3605 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3606
3607 Check_Text_IO_Special_Unit (Name (N));
3608
3609 -- Make node global for error reporting
3610
3611 Instantiation_Node := N;
3612
3613 -- Turn off style checking in instances. If the check is enabled on the
3614 -- generic unit, a warning in an instance would just be noise. If not
3615 -- enabled on the generic, then a warning in an instance is just wrong.
3616
3617 Style_Check := False;
3618
3619 -- Case of instantiation of a generic package
3620
3621 if Nkind (N) = N_Package_Instantiation then
3622 Act_Decl_Id := New_Copy (Defining_Entity (N));
3623 Set_Comes_From_Source (Act_Decl_Id, True);
3624
3625 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3626 Act_Decl_Name :=
3627 Make_Defining_Program_Unit_Name (Loc,
3628 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
3629 Defining_Identifier => Act_Decl_Id);
3630 else
3631 Act_Decl_Name := Act_Decl_Id;
3632 end if;
3633
3634 -- Case of instantiation of a formal package
3635
3636 else
3637 Act_Decl_Id := Defining_Identifier (N);
3638 Act_Decl_Name := Act_Decl_Id;
3639 end if;
3640
3641 Generate_Definition (Act_Decl_Id);
3642 Preanalyze_Actuals (N);
3643
3644 Init_Env;
3645 Env_Installed := True;
3646
3647 -- Reset renaming map for formal types. The mapping is established
3648 -- when analyzing the generic associations, but some mappings are
3649 -- inherited from formal packages of parent units, and these are
3650 -- constructed when the parents are installed.
3651
3652 Generic_Renamings.Set_Last (0);
3653 Generic_Renamings_HTable.Reset;
3654
3655 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3656 Gen_Unit := Entity (Gen_Id);
3657
3658 -- Verify that it is the name of a generic package
3659
3660 -- A visibility glitch: if the instance is a child unit and the generic
3661 -- is the generic unit of a parent instance (i.e. both the parent and
3662 -- the child units are instances of the same package) the name now
3663 -- denotes the renaming within the parent, not the intended generic
3664 -- unit. See if there is a homonym that is the desired generic. The
3665 -- renaming declaration must be visible inside the instance of the
3666 -- child, but not when analyzing the name in the instantiation itself.
3667
3668 if Ekind (Gen_Unit) = E_Package
3669 and then Present (Renamed_Entity (Gen_Unit))
3670 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3671 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3672 and then Present (Homonym (Gen_Unit))
3673 then
3674 Gen_Unit := Homonym (Gen_Unit);
3675 end if;
3676
3677 if Etype (Gen_Unit) = Any_Type then
3678 Restore_Env;
3679 goto Leave;
3680
3681 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3682
3683 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3684
3685 if From_Limited_With (Gen_Unit) then
3686 Error_Msg_N
3687 ("cannot instantiate a limited withed package", Gen_Id);
3688 else
3689 Error_Msg_NE
3690 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3691 end if;
3692
3693 Restore_Env;
3694 goto Leave;
3695 end if;
3696
3697 if In_Extended_Main_Source_Unit (N) then
3698 Set_Is_Instantiated (Gen_Unit);
3699 Generate_Reference (Gen_Unit, N);
3700
3701 if Present (Renamed_Object (Gen_Unit)) then
3702 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3703 Generate_Reference (Renamed_Object (Gen_Unit), N);
3704 end if;
3705 end if;
3706
3707 if Nkind (Gen_Id) = N_Identifier
3708 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3709 then
3710 Error_Msg_NE
3711 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3712
3713 elsif Nkind (Gen_Id) = N_Expanded_Name
3714 and then Is_Child_Unit (Gen_Unit)
3715 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3716 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3717 then
3718 Error_Msg_N
3719 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3720 end if;
3721
3722 Set_Entity (Gen_Id, Gen_Unit);
3723
3724 -- If generic is a renaming, get original generic unit
3725
3726 if Present (Renamed_Object (Gen_Unit))
3727 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3728 then
3729 Gen_Unit := Renamed_Object (Gen_Unit);
3730 end if;
3731
3732 -- Verify that there are no circular instantiations
3733
3734 if In_Open_Scopes (Gen_Unit) then
3735 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3736 Restore_Env;
3737 goto Leave;
3738
3739 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3740 Error_Msg_Node_2 := Current_Scope;
3741 Error_Msg_NE
3742 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3743 Circularity_Detected := True;
3744 Restore_Env;
3745 goto Leave;
3746
3747 else
3748 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3749
3750 -- Initialize renamings map, for error checking, and the list that
3751 -- holds private entities whose views have changed between generic
3752 -- definition and instantiation. If this is the instance created to
3753 -- validate an actual package, the instantiation environment is that
3754 -- of the enclosing instance.
3755
3756 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3757
3758 -- Copy original generic tree, to produce text for instantiation
3759
3760 Act_Tree :=
3761 Copy_Generic_Node
3762 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3763
3764 Act_Spec := Specification (Act_Tree);
3765
3766 -- If this is the instance created to validate an actual package,
3767 -- only the formals matter, do not examine the package spec itself.
3768
3769 if Is_Actual_Pack then
3770 Set_Visible_Declarations (Act_Spec, New_List);
3771 Set_Private_Declarations (Act_Spec, New_List);
3772 end if;
3773
3774 Renaming_List :=
3775 Analyze_Associations
3776 (I_Node => N,
3777 Formals => Generic_Formal_Declarations (Act_Tree),
3778 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3779
3780 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3781
3782 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3783 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3784 Set_Is_Generic_Instance (Act_Decl_Id);
3785 Set_Generic_Parent (Act_Spec, Gen_Unit);
3786
3787 -- References to the generic in its own declaration or its body are
3788 -- references to the instance. Add a renaming declaration for the
3789 -- generic unit itself. This declaration, as well as the renaming
3790 -- declarations for the generic formals, must remain private to the
3791 -- unit: the formals, because this is the language semantics, and
3792 -- the unit because its use is an artifact of the implementation.
3793
3794 Unit_Renaming :=
3795 Make_Package_Renaming_Declaration (Loc,
3796 Defining_Unit_Name =>
3797 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3798 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3799
3800 Append (Unit_Renaming, Renaming_List);
3801
3802 -- The renaming declarations are the first local declarations of the
3803 -- new unit.
3804
3805 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3806 Insert_List_Before
3807 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3808 else
3809 Set_Visible_Declarations (Act_Spec, Renaming_List);
3810 end if;
3811
3812 Act_Decl :=
3813 Make_Package_Declaration (Loc,
3814 Specification => Act_Spec);
3815
3816 -- Propagate the aspect specifications from the package declaration
3817 -- template to the instantiated version of the package declaration.
3818
3819 if Has_Aspects (Act_Tree) then
3820 Set_Aspect_Specifications (Act_Decl,
3821 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3822 end if;
3823
3824 -- Save the instantiation node, for subsequent instantiation of the
3825 -- body, if there is one and we are generating code for the current
3826 -- unit. Mark unit as having a body (avoids premature error message).
3827
3828 -- We instantiate the body if we are generating code, if we are
3829 -- generating cross-reference information, or if we are building
3830 -- trees for ASIS use or GNATprove use.
3831
3832 declare
3833 Enclosing_Body_Present : Boolean := False;
3834 -- If the generic unit is not a compilation unit, then a body may
3835 -- be present in its parent even if none is required. We create a
3836 -- tentative pending instantiation for the body, which will be
3837 -- discarded if none is actually present.
3838
3839 Scop : Entity_Id;
3840
3841 begin
3842 if Scope (Gen_Unit) /= Standard_Standard
3843 and then not Is_Child_Unit (Gen_Unit)
3844 then
3845 Scop := Scope (Gen_Unit);
3846
3847 while Present (Scop)
3848 and then Scop /= Standard_Standard
3849 loop
3850 if Unit_Requires_Body (Scop) then
3851 Enclosing_Body_Present := True;
3852 exit;
3853
3854 elsif In_Open_Scopes (Scop)
3855 and then In_Package_Body (Scop)
3856 then
3857 Enclosing_Body_Present := True;
3858 exit;
3859 end if;
3860
3861 exit when Is_Compilation_Unit (Scop);
3862 Scop := Scope (Scop);
3863 end loop;
3864 end if;
3865
3866 -- If front-end inlining is enabled, and this is a unit for which
3867 -- code will be generated, we instantiate the body at once.
3868
3869 -- This is done if the instance is not the main unit, and if the
3870 -- generic is not a child unit of another generic, to avoid scope
3871 -- problems and the reinstallation of parent instances.
3872
3873 if Expander_Active
3874 and then (not Is_Child_Unit (Gen_Unit)
3875 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3876 and then Might_Inline_Subp
3877 and then not Is_Actual_Pack
3878 then
3879 if not Back_End_Inlining
3880 and then Front_End_Inlining
3881 and then (Is_In_Main_Unit (N)
3882 or else In_Main_Context (Current_Scope))
3883 and then Nkind (Parent (N)) /= N_Compilation_Unit
3884 then
3885 Inline_Now := True;
3886
3887 elsif Back_End_Inlining
3888 and then Must_Inline_Subp
3889 and then (Is_In_Main_Unit (N)
3890 or else In_Main_Context (Current_Scope))
3891 and then Nkind (Parent (N)) /= N_Compilation_Unit
3892 then
3893 Inline_Now := True;
3894
3895 -- In configurable_run_time mode we force the inlining of
3896 -- predefined subprograms marked Inline_Always, to minimize
3897 -- the use of the run-time library.
3898
3899 elsif Is_Predefined_File_Name
3900 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3901 and then Configurable_Run_Time_Mode
3902 and then Nkind (Parent (N)) /= N_Compilation_Unit
3903 then
3904 Inline_Now := True;
3905 end if;
3906
3907 -- If the current scope is itself an instance within a child
3908 -- unit, there will be duplications in the scope stack, and the
3909 -- unstacking mechanism in Inline_Instance_Body will fail.
3910 -- This loses some rare cases of optimization, and might be
3911 -- improved some day, if we can find a proper abstraction for
3912 -- "the complete compilation context" that can be saved and
3913 -- restored. ???
3914
3915 if Is_Generic_Instance (Current_Scope) then
3916 declare
3917 Curr_Unit : constant Entity_Id :=
3918 Cunit_Entity (Current_Sem_Unit);
3919 begin
3920 if Curr_Unit /= Current_Scope
3921 and then Is_Child_Unit (Curr_Unit)
3922 then
3923 Inline_Now := False;
3924 end if;
3925 end;
3926 end if;
3927 end if;
3928
3929 Needs_Body :=
3930 (Unit_Requires_Body (Gen_Unit)
3931 or else Enclosing_Body_Present
3932 or else Present (Corresponding_Body (Gen_Decl)))
3933 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3934 and then not Is_Actual_Pack
3935 and then not Inline_Now
3936 and then (Operating_Mode = Generate_Code
3937
3938 -- Need comment for this check ???
3939
3940 or else (Operating_Mode = Check_Semantics
3941 and then (ASIS_Mode or GNATprove_Mode)));
3942
3943 -- If front_end_inlining is enabled, do not instantiate body if
3944 -- within a generic context.
3945
3946 if (Front_End_Inlining and then not Expander_Active)
3947 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3948 then
3949 Needs_Body := False;
3950 end if;
3951
3952 -- If the current context is generic, and the package being
3953 -- instantiated is declared within a formal package, there is no
3954 -- body to instantiate until the enclosing generic is instantiated
3955 -- and there is an actual for the formal package. If the formal
3956 -- package has parameters, we build a regular package instance for
3957 -- it, that precedes the original formal package declaration.
3958
3959 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3960 declare
3961 Decl : constant Node_Id :=
3962 Original_Node
3963 (Unit_Declaration_Node (Scope (Gen_Unit)));
3964 begin
3965 if Nkind (Decl) = N_Formal_Package_Declaration
3966 or else (Nkind (Decl) = N_Package_Declaration
3967 and then Is_List_Member (Decl)
3968 and then Present (Next (Decl))
3969 and then
3970 Nkind (Next (Decl)) =
3971 N_Formal_Package_Declaration)
3972 then
3973 Needs_Body := False;
3974 end if;
3975 end;
3976 end if;
3977 end;
3978
3979 -- For RCI unit calling stubs, we omit the instance body if the
3980 -- instance is the RCI library unit itself.
3981
3982 -- However there is a special case for nested instances: in this case
3983 -- we do generate the instance body, as it might be required, e.g.
3984 -- because it provides stream attributes for some type used in the
3985 -- profile of a remote subprogram. This is consistent with 12.3(12),
3986 -- which indicates that the instance body occurs at the place of the
3987 -- instantiation, and thus is part of the RCI declaration, which is
3988 -- present on all client partitions (this is E.2.3(18)).
3989
3990 -- Note that AI12-0002 may make it illegal at some point to have
3991 -- stream attributes defined in an RCI unit, in which case this
3992 -- special case will become unnecessary. In the meantime, there
3993 -- is known application code in production that depends on this
3994 -- being possible, so we definitely cannot eliminate the body in
3995 -- the case of nested instances for the time being.
3996
3997 -- When we generate a nested instance body, calling stubs for any
3998 -- relevant subprogram will be be inserted immediately after the
3999 -- subprogram declarations, and will take precedence over the
4000 -- subsequent (original) body. (The stub and original body will be
4001 -- complete homographs, but this is permitted in an instance).
4002 -- (Could we do better and remove the original body???)
4003
4004 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4005 and then Comes_From_Source (N)
4006 and then Nkind (Parent (N)) = N_Compilation_Unit
4007 then
4008 Needs_Body := False;
4009 end if;
4010
4011 if Needs_Body then
4012
4013 -- Here is a defence against a ludicrous number of instantiations
4014 -- caused by a circular set of instantiation attempts.
4015
4016 if Pending_Instantiations.Last > Maximum_Instantiations then
4017 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4018 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4019 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4020 raise Unrecoverable_Error;
4021 end if;
4022
4023 -- Indicate that the enclosing scopes contain an instantiation,
4024 -- and that cleanup actions should be delayed until after the
4025 -- instance body is expanded.
4026
4027 Check_Forward_Instantiation (Gen_Decl);
4028 if Nkind (N) = N_Package_Instantiation then
4029 declare
4030 Enclosing_Master : Entity_Id;
4031
4032 begin
4033 -- Loop to search enclosing masters
4034
4035 Enclosing_Master := Current_Scope;
4036 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4037 if Ekind (Enclosing_Master) = E_Package then
4038 if Is_Compilation_Unit (Enclosing_Master) then
4039 if In_Package_Body (Enclosing_Master) then
4040 Delay_Descriptors
4041 (Body_Entity (Enclosing_Master));
4042 else
4043 Delay_Descriptors
4044 (Enclosing_Master);
4045 end if;
4046
4047 exit Scope_Loop;
4048
4049 else
4050 Enclosing_Master := Scope (Enclosing_Master);
4051 end if;
4052
4053 elsif Is_Generic_Unit (Enclosing_Master)
4054 or else Ekind (Enclosing_Master) = E_Void
4055 then
4056 -- Cleanup actions will eventually be performed on the
4057 -- enclosing subprogram or package instance, if any.
4058 -- Enclosing scope is void in the formal part of a
4059 -- generic subprogram.
4060
4061 exit Scope_Loop;
4062
4063 else
4064 if Ekind (Enclosing_Master) = E_Entry
4065 and then
4066 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4067 then
4068 if not Expander_Active then
4069 exit Scope_Loop;
4070 else
4071 Enclosing_Master :=
4072 Protected_Body_Subprogram (Enclosing_Master);
4073 end if;
4074 end if;
4075
4076 Set_Delay_Cleanups (Enclosing_Master);
4077
4078 while Ekind (Enclosing_Master) = E_Block loop
4079 Enclosing_Master := Scope (Enclosing_Master);
4080 end loop;
4081
4082 if Is_Subprogram (Enclosing_Master) then
4083 Delay_Descriptors (Enclosing_Master);
4084
4085 elsif Is_Task_Type (Enclosing_Master) then
4086 declare
4087 TBP : constant Node_Id :=
4088 Get_Task_Body_Procedure
4089 (Enclosing_Master);
4090 begin
4091 if Present (TBP) then
4092 Delay_Descriptors (TBP);
4093 Set_Delay_Cleanups (TBP);
4094 end if;
4095 end;
4096 end if;
4097
4098 exit Scope_Loop;
4099 end if;
4100 end loop Scope_Loop;
4101 end;
4102
4103 -- Make entry in table
4104
4105 Pending_Instantiations.Append
4106 ((Inst_Node => N,
4107 Act_Decl => Act_Decl,
4108 Expander_Status => Expander_Active,
4109 Current_Sem_Unit => Current_Sem_Unit,
4110 Scope_Suppress => Scope_Suppress,
4111 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4112 Version => Ada_Version,
4113 Version_Pragma => Ada_Version_Pragma,
4114 Warnings => Save_Warnings,
4115 SPARK_Mode => SPARK_Mode,
4116 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4117 end if;
4118 end if;
4119
4120 Set_Categorization_From_Pragmas (Act_Decl);
4121
4122 if Parent_Installed then
4123 Hide_Current_Scope;
4124 end if;
4125
4126 Set_Instance_Spec (N, Act_Decl);
4127
4128 -- If not a compilation unit, insert the package declaration before
4129 -- the original instantiation node.
4130
4131 if Nkind (Parent (N)) /= N_Compilation_Unit then
4132 Mark_Rewrite_Insertion (Act_Decl);
4133 Insert_Before (N, Act_Decl);
4134 Analyze (Act_Decl);
4135
4136 -- For an instantiation that is a compilation unit, place
4137 -- declaration on current node so context is complete for analysis
4138 -- (including nested instantiations). If this is the main unit,
4139 -- the declaration eventually replaces the instantiation node.
4140 -- If the instance body is created later, it replaces the
4141 -- instance node, and the declaration is attached to it
4142 -- (see Build_Instance_Compilation_Unit_Nodes).
4143
4144 else
4145 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4146
4147 -- The entity for the current unit is the newly created one,
4148 -- and all semantic information is attached to it.
4149
4150 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4151
4152 -- If this is the main unit, replace the main entity as well
4153
4154 if Current_Sem_Unit = Main_Unit then
4155 Main_Unit_Entity := Act_Decl_Id;
4156 end if;
4157 end if;
4158
4159 Set_Unit (Parent (N), Act_Decl);
4160 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4161 Set_Package_Instantiation (Act_Decl_Id, N);
4162
4163 -- Process aspect specifications of the instance node, if any, to
4164 -- take into account categorization pragmas before analyzing the
4165 -- instance.
4166
4167 if Has_Aspects (N) then
4168 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4169 end if;
4170
4171 Analyze (Act_Decl);
4172 Set_Unit (Parent (N), N);
4173 Set_Body_Required (Parent (N), False);
4174
4175 -- We never need elaboration checks on instantiations, since by
4176 -- definition, the body instantiation is elaborated at the same
4177 -- time as the spec instantiation.
4178
4179 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4180 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4181 end if;
4182
4183 Check_Elab_Instantiation (N);
4184
4185 if ABE_Is_Certain (N) and then Needs_Body then
4186 Pending_Instantiations.Decrement_Last;
4187 end if;
4188
4189 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4190
4191 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4192 First_Private_Entity (Act_Decl_Id));
4193
4194 -- If the instantiation will receive a body, the unit will be
4195 -- transformed into a package body, and receive its own elaboration
4196 -- entity. Otherwise, the nature of the unit is now a package
4197 -- declaration.
4198
4199 if Nkind (Parent (N)) = N_Compilation_Unit
4200 and then not Needs_Body
4201 then
4202 Rewrite (N, Act_Decl);
4203 end if;
4204
4205 if Present (Corresponding_Body (Gen_Decl))
4206 or else Unit_Requires_Body (Gen_Unit)
4207 then
4208 Set_Has_Completion (Act_Decl_Id);
4209 end if;
4210
4211 Check_Formal_Packages (Act_Decl_Id);
4212
4213 Restore_Hidden_Primitives (Vis_Prims_List);
4214 Restore_Private_Views (Act_Decl_Id);
4215
4216 Inherit_Context (Gen_Decl, N);
4217
4218 if Parent_Installed then
4219 Remove_Parent;
4220 end if;
4221
4222 Restore_Env;
4223 Env_Installed := False;
4224 end if;
4225
4226 Validate_Categorization_Dependency (N, Act_Decl_Id);
4227
4228 -- There used to be a check here to prevent instantiations in local
4229 -- contexts if the No_Local_Allocators restriction was active. This
4230 -- check was removed by a binding interpretation in AI-95-00130/07,
4231 -- but we retain the code for documentation purposes.
4232
4233 -- if Ekind (Act_Decl_Id) /= E_Void
4234 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4235 -- then
4236 -- Check_Restriction (No_Local_Allocators, N);
4237 -- end if;
4238
4239 if Inline_Now then
4240 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4241 end if;
4242
4243 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4244 -- be used as defining identifiers for a formal package and for the
4245 -- corresponding expanded package.
4246
4247 if Nkind (N) = N_Formal_Package_Declaration then
4248 Act_Decl_Id := New_Copy (Defining_Entity (N));
4249 Set_Comes_From_Source (Act_Decl_Id, True);
4250 Set_Is_Generic_Instance (Act_Decl_Id, False);
4251 Set_Defining_Identifier (N, Act_Decl_Id);
4252 end if;
4253
4254 Style_Check := Save_Style_Check;
4255
4256 -- Check that if N is an instantiation of System.Dim_Float_IO or
4257 -- System.Dim_Integer_IO, the formal type has a dimension system.
4258
4259 if Nkind (N) = N_Package_Instantiation
4260 and then Is_Dim_IO_Package_Instantiation (N)
4261 then
4262 declare
4263 Assoc : constant Node_Id := First (Generic_Associations (N));
4264 begin
4265 if not Has_Dimension_System
4266 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4267 then
4268 Error_Msg_N ("type with a dimension system expected", Assoc);
4269 end if;
4270 end;
4271 end if;
4272
4273 <<Leave>>
4274 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4275 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4276 end if;
4277
4278 exception
4279 when Instantiation_Error =>
4280 if Parent_Installed then
4281 Remove_Parent;
4282 end if;
4283
4284 if Env_Installed then
4285 Restore_Env;
4286 end if;
4287
4288 Style_Check := Save_Style_Check;
4289 end Analyze_Package_Instantiation;
4290
4291 --------------------------
4292 -- Inline_Instance_Body --
4293 --------------------------
4294
4295 procedure Inline_Instance_Body
4296 (N : Node_Id;
4297 Gen_Unit : Entity_Id;
4298 Act_Decl : Node_Id)
4299 is
4300 Vis : Boolean;
4301 Gen_Comp : constant Entity_Id :=
4302 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4303 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4304 Curr_Scope : Entity_Id := Empty;
4305 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4306 Removed : Boolean := False;
4307 Num_Scopes : Int := 0;
4308
4309 Scope_Stack_Depth : constant Int :=
4310 Scope_Stack.Last - Scope_Stack.First + 1;
4311
4312 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4313 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4314 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4315 List : Elist_Id;
4316 Num_Inner : Int := 0;
4317 N_Instances : Int := 0;
4318 S : Entity_Id;
4319
4320 begin
4321 -- Case of generic unit defined in another unit. We must remove the
4322 -- complete context of the current unit to install that of the generic.
4323
4324 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4325
4326 -- Add some comments for the following two loops ???
4327
4328 S := Current_Scope;
4329 while Present (S) and then S /= Standard_Standard loop
4330 loop
4331 Num_Scopes := Num_Scopes + 1;
4332
4333 Use_Clauses (Num_Scopes) :=
4334 (Scope_Stack.Table
4335 (Scope_Stack.Last - Num_Scopes + 1).
4336 First_Use_Clause);
4337 End_Use_Clauses (Use_Clauses (Num_Scopes));
4338
4339 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4340 or else Scope_Stack.Table
4341 (Scope_Stack.Last - Num_Scopes).Entity
4342 = Scope (S);
4343 end loop;
4344
4345 exit when Is_Generic_Instance (S)
4346 and then (In_Package_Body (S)
4347 or else Ekind (S) = E_Procedure
4348 or else Ekind (S) = E_Function);
4349 S := Scope (S);
4350 end loop;
4351
4352 Vis := Is_Immediately_Visible (Gen_Comp);
4353
4354 -- Find and save all enclosing instances
4355
4356 S := Current_Scope;
4357
4358 while Present (S)
4359 and then S /= Standard_Standard
4360 loop
4361 if Is_Generic_Instance (S) then
4362 N_Instances := N_Instances + 1;
4363 Instances (N_Instances) := S;
4364
4365 exit when In_Package_Body (S);
4366 end if;
4367
4368 S := Scope (S);
4369 end loop;
4370
4371 -- Remove context of current compilation unit, unless we are within a
4372 -- nested package instantiation, in which case the context has been
4373 -- removed previously.
4374
4375 -- If current scope is the body of a child unit, remove context of
4376 -- spec as well. If an enclosing scope is an instance body, the
4377 -- context has already been removed, but the entities in the body
4378 -- must be made invisible as well.
4379
4380 S := Current_Scope;
4381
4382 while Present (S)
4383 and then S /= Standard_Standard
4384 loop
4385 if Is_Generic_Instance (S)
4386 and then (In_Package_Body (S)
4387 or else Ekind (S) = E_Procedure
4388 or else Ekind (S) = E_Function)
4389 then
4390 -- We still have to remove the entities of the enclosing
4391 -- instance from direct visibility.
4392
4393 declare
4394 E : Entity_Id;
4395 begin
4396 E := First_Entity (S);
4397 while Present (E) loop
4398 Set_Is_Immediately_Visible (E, False);
4399 Next_Entity (E);
4400 end loop;
4401 end;
4402
4403 exit;
4404 end if;
4405
4406 if S = Curr_Unit
4407 or else (Ekind (Curr_Unit) = E_Package_Body
4408 and then S = Spec_Entity (Curr_Unit))
4409 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4410 and then S =
4411 Corresponding_Spec
4412 (Unit_Declaration_Node (Curr_Unit)))
4413 then
4414 Removed := True;
4415
4416 -- Remove entities in current scopes from visibility, so that
4417 -- instance body is compiled in a clean environment.
4418
4419 List := Save_Scope_Stack (Handle_Use => False);
4420
4421 if Is_Child_Unit (S) then
4422
4423 -- Remove child unit from stack, as well as inner scopes.
4424 -- Removing the context of a child unit removes parent units
4425 -- as well.
4426
4427 while Current_Scope /= S loop
4428 Num_Inner := Num_Inner + 1;
4429 Inner_Scopes (Num_Inner) := Current_Scope;
4430 Pop_Scope;
4431 end loop;
4432
4433 Pop_Scope;
4434 Remove_Context (Curr_Comp);
4435 Curr_Scope := S;
4436
4437 else
4438 Remove_Context (Curr_Comp);
4439 end if;
4440
4441 if Ekind (Curr_Unit) = E_Package_Body then
4442 Remove_Context (Library_Unit (Curr_Comp));
4443 end if;
4444 end if;
4445
4446 S := Scope (S);
4447 end loop;
4448 pragma Assert (Num_Inner < Num_Scopes);
4449
4450 Push_Scope (Standard_Standard);
4451 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4452 Instantiate_Package_Body
4453 (Body_Info =>
4454 ((Inst_Node => N,
4455 Act_Decl => Act_Decl,
4456 Expander_Status => Expander_Active,
4457 Current_Sem_Unit => Current_Sem_Unit,
4458 Scope_Suppress => Scope_Suppress,
4459 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4460 Version => Ada_Version,
4461 Version_Pragma => Ada_Version_Pragma,
4462 Warnings => Save_Warnings,
4463 SPARK_Mode => SPARK_Mode,
4464 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4465 Inlined_Body => True);
4466
4467 Pop_Scope;
4468
4469 -- Restore context
4470
4471 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4472
4473 -- Reset Generic_Instance flag so that use clauses can be installed
4474 -- in the proper order. (See Use_One_Package for effect of enclosing
4475 -- instances on processing of use clauses).
4476
4477 for J in 1 .. N_Instances loop
4478 Set_Is_Generic_Instance (Instances (J), False);
4479 end loop;
4480
4481 if Removed then
4482 Install_Context (Curr_Comp);
4483
4484 if Present (Curr_Scope)
4485 and then Is_Child_Unit (Curr_Scope)
4486 then
4487 Push_Scope (Curr_Scope);
4488 Set_Is_Immediately_Visible (Curr_Scope);
4489
4490 -- Finally, restore inner scopes as well
4491
4492 for J in reverse 1 .. Num_Inner loop
4493 Push_Scope (Inner_Scopes (J));
4494 end loop;
4495 end if;
4496
4497 Restore_Scope_Stack (List, Handle_Use => False);
4498
4499 if Present (Curr_Scope)
4500 and then
4501 (In_Private_Part (Curr_Scope)
4502 or else In_Package_Body (Curr_Scope))
4503 then
4504 -- Install private declaration of ancestor units, which are
4505 -- currently available. Restore_Scope_Stack and Install_Context
4506 -- only install the visible part of parents.
4507
4508 declare
4509 Par : Entity_Id;
4510 begin
4511 Par := Scope (Curr_Scope);
4512 while (Present (Par))
4513 and then Par /= Standard_Standard
4514 loop
4515 Install_Private_Declarations (Par);
4516 Par := Scope (Par);
4517 end loop;
4518 end;
4519 end if;
4520 end if;
4521
4522 -- Restore use clauses. For a child unit, use clauses in the parents
4523 -- are restored when installing the context, so only those in inner
4524 -- scopes (and those local to the child unit itself) need to be
4525 -- installed explicitly.
4526
4527 if Is_Child_Unit (Curr_Unit)
4528 and then Removed
4529 then
4530 for J in reverse 1 .. Num_Inner + 1 loop
4531 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4532 Use_Clauses (J);
4533 Install_Use_Clauses (Use_Clauses (J));
4534 end loop;
4535
4536 else
4537 for J in reverse 1 .. Num_Scopes loop
4538 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4539 Use_Clauses (J);
4540 Install_Use_Clauses (Use_Clauses (J));
4541 end loop;
4542 end if;
4543
4544 -- Restore status of instances. If one of them is a body, make its
4545 -- local entities visible again.
4546
4547 declare
4548 E : Entity_Id;
4549 Inst : Entity_Id;
4550
4551 begin
4552 for J in 1 .. N_Instances loop
4553 Inst := Instances (J);
4554 Set_Is_Generic_Instance (Inst, True);
4555
4556 if In_Package_Body (Inst)
4557 or else Ekind (S) = E_Procedure
4558 or else Ekind (S) = E_Function
4559 then
4560 E := First_Entity (Instances (J));
4561 while Present (E) loop
4562 Set_Is_Immediately_Visible (E);
4563 Next_Entity (E);
4564 end loop;
4565 end if;
4566 end loop;
4567 end;
4568
4569 -- If generic unit is in current unit, current context is correct
4570
4571 else
4572 Instantiate_Package_Body
4573 (Body_Info =>
4574 ((Inst_Node => N,
4575 Act_Decl => Act_Decl,
4576 Expander_Status => Expander_Active,
4577 Current_Sem_Unit => Current_Sem_Unit,
4578 Scope_Suppress => Scope_Suppress,
4579 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4580 Version => Ada_Version,
4581 Version_Pragma => Ada_Version_Pragma,
4582 Warnings => Save_Warnings,
4583 SPARK_Mode => SPARK_Mode,
4584 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4585 Inlined_Body => True);
4586 end if;
4587 end Inline_Instance_Body;
4588
4589 -------------------------------------
4590 -- Analyze_Procedure_Instantiation --
4591 -------------------------------------
4592
4593 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4594 begin
4595 Analyze_Subprogram_Instantiation (N, E_Procedure);
4596 end Analyze_Procedure_Instantiation;
4597
4598 -----------------------------------
4599 -- Need_Subprogram_Instance_Body --
4600 -----------------------------------
4601
4602 function Need_Subprogram_Instance_Body
4603 (N : Node_Id;
4604 Subp : Entity_Id) return Boolean
4605 is
4606 begin
4607 -- Must be inlined (or inlined renaming)
4608
4609 if (Is_In_Main_Unit (N)
4610 or else Is_Inlined (Subp)
4611 or else Is_Inlined (Alias (Subp)))
4612
4613 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4614
4615 and then (Operating_Mode = Generate_Code
4616 or else (Operating_Mode = Check_Semantics
4617 and then (ASIS_Mode or GNATprove_Mode)))
4618
4619 -- The body is needed when generating code (full expansion), in ASIS
4620 -- mode for other tools, and in GNATprove mode (special expansion) for
4621 -- formal verification of the body itself.
4622
4623 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4624
4625 -- No point in inlining if ABE is inevitable
4626
4627 and then not ABE_Is_Certain (N)
4628
4629 -- Or if subprogram is eliminated
4630
4631 and then not Is_Eliminated (Subp)
4632 then
4633 Pending_Instantiations.Append
4634 ((Inst_Node => N,
4635 Act_Decl => Unit_Declaration_Node (Subp),
4636 Expander_Status => Expander_Active,
4637 Current_Sem_Unit => Current_Sem_Unit,
4638 Scope_Suppress => Scope_Suppress,
4639 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4640 Version => Ada_Version,
4641 Version_Pragma => Ada_Version_Pragma,
4642 Warnings => Save_Warnings,
4643 SPARK_Mode => SPARK_Mode,
4644 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4645 return True;
4646
4647 -- Here if not inlined, or we ignore the inlining
4648
4649 else
4650 return False;
4651 end if;
4652 end Need_Subprogram_Instance_Body;
4653
4654 --------------------------------------
4655 -- Analyze_Subprogram_Instantiation --
4656 --------------------------------------
4657
4658 procedure Analyze_Subprogram_Instantiation
4659 (N : Node_Id;
4660 K : Entity_Kind)
4661 is
4662 Loc : constant Source_Ptr := Sloc (N);
4663 Gen_Id : constant Node_Id := Name (N);
4664
4665 Anon_Id : constant Entity_Id :=
4666 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4667 Chars => New_External_Name
4668 (Chars (Defining_Entity (N)), 'R'));
4669
4670 Act_Decl_Id : Entity_Id;
4671 Act_Decl : Node_Id;
4672 Act_Spec : Node_Id;
4673 Act_Tree : Node_Id;
4674
4675 Env_Installed : Boolean := False;
4676 Gen_Unit : Entity_Id;
4677 Gen_Decl : Node_Id;
4678 Pack_Id : Entity_Id;
4679 Parent_Installed : Boolean := False;
4680 Renaming_List : List_Id;
4681
4682 procedure Analyze_Instance_And_Renamings;
4683 -- The instance must be analyzed in a context that includes the mappings
4684 -- of generic parameters into actuals. We create a package declaration
4685 -- for this purpose, and a subprogram with an internal name within the
4686 -- package. The subprogram instance is simply an alias for the internal
4687 -- subprogram, declared in the current scope.
4688
4689 ------------------------------------
4690 -- Analyze_Instance_And_Renamings --
4691 ------------------------------------
4692
4693 procedure Analyze_Instance_And_Renamings is
4694 Def_Ent : constant Entity_Id := Defining_Entity (N);
4695 Pack_Decl : Node_Id;
4696
4697 begin
4698 if Nkind (Parent (N)) = N_Compilation_Unit then
4699
4700 -- For the case of a compilation unit, the container package has
4701 -- the same name as the instantiation, to insure that the binder
4702 -- calls the elaboration procedure with the right name. Copy the
4703 -- entity of the instance, which may have compilation level flags
4704 -- (e.g. Is_Child_Unit) set.
4705
4706 Pack_Id := New_Copy (Def_Ent);
4707
4708 else
4709 -- Otherwise we use the name of the instantiation concatenated
4710 -- with its source position to ensure uniqueness if there are
4711 -- several instantiations with the same name.
4712
4713 Pack_Id :=
4714 Make_Defining_Identifier (Loc,
4715 Chars => New_External_Name
4716 (Related_Id => Chars (Def_Ent),
4717 Suffix => "GP",
4718 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4719 end if;
4720
4721 Pack_Decl := Make_Package_Declaration (Loc,
4722 Specification => Make_Package_Specification (Loc,
4723 Defining_Unit_Name => Pack_Id,
4724 Visible_Declarations => Renaming_List,
4725 End_Label => Empty));
4726
4727 Set_Instance_Spec (N, Pack_Decl);
4728 Set_Is_Generic_Instance (Pack_Id);
4729 Set_Debug_Info_Needed (Pack_Id);
4730
4731 -- Case of not a compilation unit
4732
4733 if Nkind (Parent (N)) /= N_Compilation_Unit then
4734 Mark_Rewrite_Insertion (Pack_Decl);
4735 Insert_Before (N, Pack_Decl);
4736 Set_Has_Completion (Pack_Id);
4737
4738 -- Case of an instantiation that is a compilation unit
4739
4740 -- Place declaration on current node so context is complete for
4741 -- analysis (including nested instantiations), and for use in a
4742 -- context_clause (see Analyze_With_Clause).
4743
4744 else
4745 Set_Unit (Parent (N), Pack_Decl);
4746 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4747 end if;
4748
4749 Analyze (Pack_Decl);
4750 Check_Formal_Packages (Pack_Id);
4751 Set_Is_Generic_Instance (Pack_Id, False);
4752
4753 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4754 -- above???
4755
4756 -- Body of the enclosing package is supplied when instantiating the
4757 -- subprogram body, after semantic analysis is completed.
4758
4759 if Nkind (Parent (N)) = N_Compilation_Unit then
4760
4761 -- Remove package itself from visibility, so it does not
4762 -- conflict with subprogram.
4763
4764 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4765
4766 -- Set name and scope of internal subprogram so that the proper
4767 -- external name will be generated. The proper scope is the scope
4768 -- of the wrapper package. We need to generate debugging info for
4769 -- the internal subprogram, so set flag accordingly.
4770
4771 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4772 Set_Scope (Anon_Id, Scope (Pack_Id));
4773
4774 -- Mark wrapper package as referenced, to avoid spurious warnings
4775 -- if the instantiation appears in various with_ clauses of
4776 -- subunits of the main unit.
4777
4778 Set_Referenced (Pack_Id);
4779 end if;
4780
4781 Set_Is_Generic_Instance (Anon_Id);
4782 Set_Debug_Info_Needed (Anon_Id);
4783 Act_Decl_Id := New_Copy (Anon_Id);
4784
4785 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4786 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4787 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4788 Set_Comes_From_Source (Act_Decl_Id, True);
4789
4790 -- The signature may involve types that are not frozen yet, but the
4791 -- subprogram will be frozen at the point the wrapper package is
4792 -- frozen, so it does not need its own freeze node. In fact, if one
4793 -- is created, it might conflict with the freezing actions from the
4794 -- wrapper package.
4795
4796 Set_Has_Delayed_Freeze (Anon_Id, False);
4797
4798 -- If the instance is a child unit, mark the Id accordingly. Mark
4799 -- the anonymous entity as well, which is the real subprogram and
4800 -- which is used when the instance appears in a context clause.
4801 -- Similarly, propagate the Is_Eliminated flag to handle properly
4802 -- nested eliminated subprograms.
4803
4804 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4805 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4806 New_Overloaded_Entity (Act_Decl_Id);
4807 Check_Eliminated (Act_Decl_Id);
4808 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4809
4810 -- In compilation unit case, kill elaboration checks on the
4811 -- instantiation, since they are never needed -- the body is
4812 -- instantiated at the same point as the spec.
4813
4814 if Nkind (Parent (N)) = N_Compilation_Unit then
4815 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4816 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4817 Set_Is_Compilation_Unit (Anon_Id);
4818
4819 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4820 end if;
4821
4822 -- The instance is not a freezing point for the new subprogram
4823
4824 Set_Is_Frozen (Act_Decl_Id, False);
4825
4826 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4827 Valid_Operator_Definition (Act_Decl_Id);
4828 end if;
4829
4830 Set_Alias (Act_Decl_Id, Anon_Id);
4831 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4832 Set_Has_Completion (Act_Decl_Id);
4833 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4834
4835 if Nkind (Parent (N)) = N_Compilation_Unit then
4836 Set_Body_Required (Parent (N), False);
4837 end if;
4838 end Analyze_Instance_And_Renamings;
4839
4840 -- Local variables
4841
4842 Vis_Prims_List : Elist_Id := No_Elist;
4843 -- List of primitives made temporarily visible in the instantiation
4844 -- to match the visibility of the formal type
4845
4846 -- Start of processing for Analyze_Subprogram_Instantiation
4847
4848 begin
4849 Check_SPARK_Restriction ("generic is not allowed", N);
4850
4851 -- Very first thing: check for special Text_IO unit in case we are
4852 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
4853 -- such an instantiation is bogus (these are packages, not subprograms),
4854 -- but we get a better error message if we do this.
4855
4856 Check_Text_IO_Special_Unit (Gen_Id);
4857
4858 -- Make node global for error reporting
4859
4860 Instantiation_Node := N;
4861
4862 -- For package instantiations we turn off style checks, because they
4863 -- will have been emitted in the generic. For subprogram instantiations
4864 -- we want to apply at least the check on overriding indicators so we
4865 -- do not modify the style check status.
4866
4867 -- The renaming declarations for the actuals do not come from source and
4868 -- will not generate spurious warnings.
4869
4870 Preanalyze_Actuals (N);
4871
4872 Init_Env;
4873 Env_Installed := True;
4874 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4875 Gen_Unit := Entity (Gen_Id);
4876
4877 Generate_Reference (Gen_Unit, Gen_Id);
4878
4879 if Nkind (Gen_Id) = N_Identifier
4880 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4881 then
4882 Error_Msg_NE
4883 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4884 end if;
4885
4886 if Etype (Gen_Unit) = Any_Type then
4887 Restore_Env;
4888 return;
4889 end if;
4890
4891 -- Verify that it is a generic subprogram of the right kind, and that
4892 -- it does not lead to a circular instantiation.
4893
4894 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
4895 Error_Msg_NE
4896 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
4897
4898 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
4899 Error_Msg_NE
4900 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
4901
4902 elsif In_Open_Scopes (Gen_Unit) then
4903 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4904
4905 else
4906 Set_Entity (Gen_Id, Gen_Unit);
4907 Set_Is_Instantiated (Gen_Unit);
4908
4909 if In_Extended_Main_Source_Unit (N) then
4910 Generate_Reference (Gen_Unit, N);
4911 end if;
4912
4913 -- If renaming, get original unit
4914
4915 if Present (Renamed_Object (Gen_Unit))
4916 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4917 or else
4918 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4919 then
4920 Gen_Unit := Renamed_Object (Gen_Unit);
4921 Set_Is_Instantiated (Gen_Unit);
4922 Generate_Reference (Gen_Unit, N);
4923 end if;
4924
4925 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4926 Error_Msg_Node_2 := Current_Scope;
4927 Error_Msg_NE
4928 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4929 Circularity_Detected := True;
4930 Restore_Hidden_Primitives (Vis_Prims_List);
4931 goto Leave;
4932 end if;
4933
4934 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4935
4936 -- Initialize renamings map, for error checking
4937
4938 Generic_Renamings.Set_Last (0);
4939 Generic_Renamings_HTable.Reset;
4940
4941 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4942
4943 -- Copy original generic tree, to produce text for instantiation
4944
4945 Act_Tree :=
4946 Copy_Generic_Node
4947 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4948
4949 -- Inherit overriding indicator from instance node
4950
4951 Act_Spec := Specification (Act_Tree);
4952 Set_Must_Override (Act_Spec, Must_Override (N));
4953 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4954
4955 Renaming_List :=
4956 Analyze_Associations
4957 (I_Node => N,
4958 Formals => Generic_Formal_Declarations (Act_Tree),
4959 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4960
4961 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4962
4963 -- The subprogram itself cannot contain a nested instance, so the
4964 -- current parent is left empty.
4965
4966 Set_Instance_Env (Gen_Unit, Empty);
4967
4968 -- Build the subprogram declaration, which does not appear in the
4969 -- generic template, and give it a sloc consistent with that of the
4970 -- template.
4971
4972 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4973 Set_Generic_Parent (Act_Spec, Gen_Unit);
4974 Act_Decl :=
4975 Make_Subprogram_Declaration (Sloc (Act_Spec),
4976 Specification => Act_Spec);
4977
4978 -- The aspects have been copied previously, but they have to be
4979 -- linked explicitly to the new subprogram declaration. Explicit
4980 -- pre/postconditions on the instance are analyzed below, in a
4981 -- separate step.
4982
4983 Move_Aspects (Act_Tree, To => Act_Decl);
4984 Set_Categorization_From_Pragmas (Act_Decl);
4985
4986 if Parent_Installed then
4987 Hide_Current_Scope;
4988 end if;
4989
4990 Append (Act_Decl, Renaming_List);
4991 Analyze_Instance_And_Renamings;
4992
4993 -- If the generic is marked Import (Intrinsic), then so is the
4994 -- instance. This indicates that there is no body to instantiate. If
4995 -- generic is marked inline, so it the instance, and the anonymous
4996 -- subprogram it renames. If inlined, or else if inlining is enabled
4997 -- for the compilation, we generate the instance body even if it is
4998 -- not within the main unit.
4999
5000 if Is_Intrinsic_Subprogram (Gen_Unit) then
5001 Set_Is_Intrinsic_Subprogram (Anon_Id);
5002 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5003
5004 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5005 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5006 end if;
5007 end if;
5008
5009 -- Inherit convention from generic unit. Intrinsic convention, as for
5010 -- an instance of unchecked conversion, is not inherited because an
5011 -- explicit Ada instance has been created.
5012
5013 if Has_Convention_Pragma (Gen_Unit)
5014 and then Convention (Gen_Unit) /= Convention_Intrinsic
5015 then
5016 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5017 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5018 end if;
5019
5020 Generate_Definition (Act_Decl_Id);
5021 -- Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id)));
5022 -- ??? needed?
5023 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
5024
5025 -- Inherit all inlining-related flags which apply to the generic in
5026 -- the subprogram and its declaration.
5027
5028 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5029 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5030
5031 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5032 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5033
5034 Set_Has_Pragma_Inline_Always
5035 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5036 Set_Has_Pragma_Inline_Always
5037 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5038
5039 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5040 Check_Elab_Instantiation (N);
5041 end if;
5042
5043 if Is_Dispatching_Operation (Act_Decl_Id)
5044 and then Ada_Version >= Ada_2005
5045 then
5046 declare
5047 Formal : Entity_Id;
5048
5049 begin
5050 Formal := First_Formal (Act_Decl_Id);
5051 while Present (Formal) loop
5052 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5053 and then Is_Controlling_Formal (Formal)
5054 and then not Can_Never_Be_Null (Formal)
5055 then
5056 Error_Msg_NE ("access parameter& is controlling,",
5057 N, Formal);
5058 Error_Msg_NE
5059 ("\corresponding parameter of & must be"
5060 & " explicitly null-excluding", N, Gen_Id);
5061 end if;
5062
5063 Next_Formal (Formal);
5064 end loop;
5065 end;
5066 end if;
5067
5068 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5069
5070 Validate_Categorization_Dependency (N, Act_Decl_Id);
5071
5072 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5073 Inherit_Context (Gen_Decl, N);
5074
5075 Restore_Private_Views (Pack_Id, False);
5076
5077 -- If the context requires a full instantiation, mark node for
5078 -- subsequent construction of the body.
5079
5080 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5081 Check_Forward_Instantiation (Gen_Decl);
5082
5083 -- The wrapper package is always delayed, because it does not
5084 -- constitute a freeze point, but to insure that the freeze
5085 -- node is placed properly, it is created directly when
5086 -- instantiating the body (otherwise the freeze node might
5087 -- appear to early for nested instantiations).
5088
5089 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5090
5091 -- For ASIS purposes, indicate that the wrapper package has
5092 -- replaced the instantiation node.
5093
5094 Rewrite (N, Unit (Parent (N)));
5095 Set_Unit (Parent (N), N);
5096 end if;
5097
5098 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5099
5100 -- Replace instance node for library-level instantiations of
5101 -- intrinsic subprograms, for ASIS use.
5102
5103 Rewrite (N, Unit (Parent (N)));
5104 Set_Unit (Parent (N), N);
5105 end if;
5106
5107 if Parent_Installed then
5108 Remove_Parent;
5109 end if;
5110
5111 Restore_Hidden_Primitives (Vis_Prims_List);
5112 Restore_Env;
5113 Env_Installed := False;
5114 Generic_Renamings.Set_Last (0);
5115 Generic_Renamings_HTable.Reset;
5116 end if;
5117
5118 <<Leave>>
5119 if Has_Aspects (N) then
5120 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5121 end if;
5122
5123 exception
5124 when Instantiation_Error =>
5125 if Parent_Installed then
5126 Remove_Parent;
5127 end if;
5128
5129 if Env_Installed then
5130 Restore_Env;
5131 end if;
5132 end Analyze_Subprogram_Instantiation;
5133
5134 -------------------------
5135 -- Get_Associated_Node --
5136 -------------------------
5137
5138 function Get_Associated_Node (N : Node_Id) return Node_Id is
5139 Assoc : Node_Id;
5140
5141 begin
5142 Assoc := Associated_Node (N);
5143
5144 if Nkind (Assoc) /= Nkind (N) then
5145 return Assoc;
5146
5147 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5148 return Assoc;
5149
5150 else
5151 -- If the node is part of an inner generic, it may itself have been
5152 -- remapped into a further generic copy. Associated_Node is otherwise
5153 -- used for the entity of the node, and will be of a different node
5154 -- kind, or else N has been rewritten as a literal or function call.
5155
5156 while Present (Associated_Node (Assoc))
5157 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5158 loop
5159 Assoc := Associated_Node (Assoc);
5160 end loop;
5161
5162 -- Follow and additional link in case the final node was rewritten.
5163 -- This can only happen with nested generic units.
5164
5165 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5166 and then Present (Associated_Node (Assoc))
5167 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5168 N_Explicit_Dereference,
5169 N_Integer_Literal,
5170 N_Real_Literal,
5171 N_String_Literal))
5172 then
5173 Assoc := Associated_Node (Assoc);
5174 end if;
5175
5176 -- An additional special case: an unconstrained type in an object
5177 -- declaration may have been rewritten as a local subtype constrained
5178 -- by the expression in the declaration. We need to recover the
5179 -- original entity which may be global.
5180
5181 if Present (Original_Node (Assoc))
5182 and then Nkind (Parent (N)) = N_Object_Declaration
5183 then
5184 Assoc := Original_Node (Assoc);
5185 end if;
5186
5187 return Assoc;
5188 end if;
5189 end Get_Associated_Node;
5190
5191 -------------------------------------------
5192 -- Build_Instance_Compilation_Unit_Nodes --
5193 -------------------------------------------
5194
5195 procedure Build_Instance_Compilation_Unit_Nodes
5196 (N : Node_Id;
5197 Act_Body : Node_Id;
5198 Act_Decl : Node_Id)
5199 is
5200 Decl_Cunit : Node_Id;
5201 Body_Cunit : Node_Id;
5202 Citem : Node_Id;
5203 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5204 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5205
5206 begin
5207 -- A new compilation unit node is built for the instance declaration
5208
5209 Decl_Cunit :=
5210 Make_Compilation_Unit (Sloc (N),
5211 Context_Items => Empty_List,
5212 Unit => Act_Decl,
5213 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5214
5215 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5216
5217 -- The new compilation unit is linked to its body, but both share the
5218 -- same file, so we do not set Body_Required on the new unit so as not
5219 -- to create a spurious dependency on a non-existent body in the ali.
5220 -- This simplifies CodePeer unit traversal.
5221
5222 -- We use the original instantiation compilation unit as the resulting
5223 -- compilation unit of the instance, since this is the main unit.
5224
5225 Rewrite (N, Act_Body);
5226
5227 -- Propagate the aspect specifications from the package body template to
5228 -- the instantiated version of the package body.
5229
5230 if Has_Aspects (Act_Body) then
5231 Set_Aspect_Specifications
5232 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5233 end if;
5234
5235 Body_Cunit := Parent (N);
5236
5237 -- The two compilation unit nodes are linked by the Library_Unit field
5238
5239 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5240 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5241
5242 -- Preserve the private nature of the package if needed
5243
5244 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5245
5246 -- If the instance is not the main unit, its context, categorization
5247 -- and elaboration entity are not relevant to the compilation.
5248
5249 if Body_Cunit /= Cunit (Main_Unit) then
5250 Make_Instance_Unit (Body_Cunit, In_Main => False);
5251 return;
5252 end if;
5253
5254 -- The context clause items on the instantiation, which are now attached
5255 -- to the body compilation unit (since the body overwrote the original
5256 -- instantiation node), semantically belong on the spec, so copy them
5257 -- there. It's harmless to leave them on the body as well. In fact one
5258 -- could argue that they belong in both places.
5259
5260 Citem := First (Context_Items (Body_Cunit));
5261 while Present (Citem) loop
5262 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5263 Next (Citem);
5264 end loop;
5265
5266 -- Propagate categorization flags on packages, so that they appear in
5267 -- the ali file for the spec of the unit.
5268
5269 if Ekind (New_Main) = E_Package then
5270 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5271 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5272 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5273 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5274 Set_Is_Remote_Call_Interface
5275 (Old_Main, Is_Remote_Call_Interface (New_Main));
5276 end if;
5277
5278 -- Make entry in Units table, so that binder can generate call to
5279 -- elaboration procedure for body, if any.
5280
5281 Make_Instance_Unit (Body_Cunit, In_Main => True);
5282 Main_Unit_Entity := New_Main;
5283 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5284
5285 -- Build elaboration entity, since the instance may certainly generate
5286 -- elaboration code requiring a flag for protection.
5287
5288 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5289 end Build_Instance_Compilation_Unit_Nodes;
5290
5291 -----------------------------
5292 -- Check_Access_Definition --
5293 -----------------------------
5294
5295 procedure Check_Access_Definition (N : Node_Id) is
5296 begin
5297 pragma Assert
5298 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5299 null;
5300 end Check_Access_Definition;
5301
5302 -----------------------------------
5303 -- Check_Formal_Package_Instance --
5304 -----------------------------------
5305
5306 -- If the formal has specific parameters, they must match those of the
5307 -- actual. Both of them are instances, and the renaming declarations for
5308 -- their formal parameters appear in the same order in both. The analyzed
5309 -- formal has been analyzed in the context of the current instance.
5310
5311 procedure Check_Formal_Package_Instance
5312 (Formal_Pack : Entity_Id;
5313 Actual_Pack : Entity_Id)
5314 is
5315 E1 : Entity_Id := First_Entity (Actual_Pack);
5316 E2 : Entity_Id := First_Entity (Formal_Pack);
5317
5318 Expr1 : Node_Id;
5319 Expr2 : Node_Id;
5320
5321 procedure Check_Mismatch (B : Boolean);
5322 -- Common error routine for mismatch between the parameters of the
5323 -- actual instance and those of the formal package.
5324
5325 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5326 -- The formal may come from a nested formal package, and the actual may
5327 -- have been constant-folded. To determine whether the two denote the
5328 -- same entity we may have to traverse several definitions to recover
5329 -- the ultimate entity that they refer to.
5330
5331 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5332 -- Similarly, if the formal comes from a nested formal package, the
5333 -- actual may designate the formal through multiple renamings, which
5334 -- have to be followed to determine the original variable in question.
5335
5336 --------------------
5337 -- Check_Mismatch --
5338 --------------------
5339
5340 procedure Check_Mismatch (B : Boolean) is
5341 Kind : constant Node_Kind := Nkind (Parent (E2));
5342
5343 begin
5344 if Kind = N_Formal_Type_Declaration then
5345 return;
5346
5347 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5348 N_Formal_Package_Declaration)
5349 or else Kind in N_Formal_Subprogram_Declaration
5350 then
5351 null;
5352
5353 elsif B then
5354 Error_Msg_NE
5355 ("actual for & in actual instance does not match formal",
5356 Parent (Actual_Pack), E1);
5357 end if;
5358 end Check_Mismatch;
5359
5360 --------------------------------
5361 -- Same_Instantiated_Constant --
5362 --------------------------------
5363
5364 function Same_Instantiated_Constant
5365 (E1, E2 : Entity_Id) return Boolean
5366 is
5367 Ent : Entity_Id;
5368
5369 begin
5370 Ent := E2;
5371 while Present (Ent) loop
5372 if E1 = Ent then
5373 return True;
5374
5375 elsif Ekind (Ent) /= E_Constant then
5376 return False;
5377
5378 elsif Is_Entity_Name (Constant_Value (Ent)) then
5379 if Entity (Constant_Value (Ent)) = E1 then
5380 return True;
5381 else
5382 Ent := Entity (Constant_Value (Ent));
5383 end if;
5384
5385 -- The actual may be a constant that has been folded. Recover
5386 -- original name.
5387
5388 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5389 Ent := Entity (Original_Node (Constant_Value (Ent)));
5390 else
5391 return False;
5392 end if;
5393 end loop;
5394
5395 return False;
5396 end Same_Instantiated_Constant;
5397
5398 --------------------------------
5399 -- Same_Instantiated_Variable --
5400 --------------------------------
5401
5402 function Same_Instantiated_Variable
5403 (E1, E2 : Entity_Id) return Boolean
5404 is
5405 function Original_Entity (E : Entity_Id) return Entity_Id;
5406 -- Follow chain of renamings to the ultimate ancestor
5407
5408 ---------------------
5409 -- Original_Entity --
5410 ---------------------
5411
5412 function Original_Entity (E : Entity_Id) return Entity_Id is
5413 Orig : Entity_Id;
5414
5415 begin
5416 Orig := E;
5417 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5418 and then Present (Renamed_Object (Orig))
5419 and then Is_Entity_Name (Renamed_Object (Orig))
5420 loop
5421 Orig := Entity (Renamed_Object (Orig));
5422 end loop;
5423
5424 return Orig;
5425 end Original_Entity;
5426
5427 -- Start of processing for Same_Instantiated_Variable
5428
5429 begin
5430 return Ekind (E1) = Ekind (E2)
5431 and then Original_Entity (E1) = Original_Entity (E2);
5432 end Same_Instantiated_Variable;
5433
5434 -- Start of processing for Check_Formal_Package_Instance
5435
5436 begin
5437 while Present (E1)
5438 and then Present (E2)
5439 loop
5440 exit when Ekind (E1) = E_Package
5441 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5442
5443 -- If the formal is the renaming of the formal package, this
5444 -- is the end of its formal part, which may occur before the
5445 -- end of the formal part in the actual in the presence of
5446 -- defaulted parameters in the formal package.
5447
5448 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5449 and then Renamed_Entity (E2) = Scope (E2);
5450
5451 -- The analysis of the actual may generate additional internal
5452 -- entities. If the formal is defaulted, there is no corresponding
5453 -- analysis and the internal entities must be skipped, until we
5454 -- find corresponding entities again.
5455
5456 if Comes_From_Source (E2)
5457 and then not Comes_From_Source (E1)
5458 and then Chars (E1) /= Chars (E2)
5459 then
5460 while Present (E1)
5461 and then Chars (E1) /= Chars (E2)
5462 loop
5463 Next_Entity (E1);
5464 end loop;
5465 end if;
5466
5467 if No (E1) then
5468 return;
5469
5470 -- If the formal entity comes from a formal declaration, it was
5471 -- defaulted in the formal package, and no check is needed on it.
5472
5473 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5474 goto Next_E;
5475
5476 -- Ditto for defaulted formal subprograms.
5477
5478 elsif Is_Overloadable (E1)
5479 and then Nkind (Unit_Declaration_Node (E2)) in
5480 N_Formal_Subprogram_Declaration
5481 then
5482 goto Next_E;
5483
5484 elsif Is_Type (E1) then
5485
5486 -- Subtypes must statically match. E1, E2 are the local entities
5487 -- that are subtypes of the actuals. Itypes generated for other
5488 -- parameters need not be checked, the check will be performed
5489 -- on the parameters themselves.
5490
5491 -- If E2 is a formal type declaration, it is a defaulted parameter
5492 -- and needs no checking.
5493
5494 if not Is_Itype (E1)
5495 and then not Is_Itype (E2)
5496 then
5497 Check_Mismatch
5498 (not Is_Type (E2)
5499 or else Etype (E1) /= Etype (E2)
5500 or else not Subtypes_Statically_Match (E1, E2));
5501 end if;
5502
5503 elsif Ekind (E1) = E_Constant then
5504
5505 -- IN parameters must denote the same static value, or the same
5506 -- constant, or the literal null.
5507
5508 Expr1 := Expression (Parent (E1));
5509
5510 if Ekind (E2) /= E_Constant then
5511 Check_Mismatch (True);
5512 goto Next_E;
5513 else
5514 Expr2 := Expression (Parent (E2));
5515 end if;
5516
5517 if Is_OK_Static_Expression (Expr1) then
5518 if not Is_OK_Static_Expression (Expr2) then
5519 Check_Mismatch (True);
5520
5521 elsif Is_Discrete_Type (Etype (E1)) then
5522 declare
5523 V1 : constant Uint := Expr_Value (Expr1);
5524 V2 : constant Uint := Expr_Value (Expr2);
5525 begin
5526 Check_Mismatch (V1 /= V2);
5527 end;
5528
5529 elsif Is_Real_Type (Etype (E1)) then
5530 declare
5531 V1 : constant Ureal := Expr_Value_R (Expr1);
5532 V2 : constant Ureal := Expr_Value_R (Expr2);
5533 begin
5534 Check_Mismatch (V1 /= V2);
5535 end;
5536
5537 elsif Is_String_Type (Etype (E1))
5538 and then Nkind (Expr1) = N_String_Literal
5539 then
5540 if Nkind (Expr2) /= N_String_Literal then
5541 Check_Mismatch (True);
5542 else
5543 Check_Mismatch
5544 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5545 end if;
5546 end if;
5547
5548 elsif Is_Entity_Name (Expr1) then
5549 if Is_Entity_Name (Expr2) then
5550 if Entity (Expr1) = Entity (Expr2) then
5551 null;
5552 else
5553 Check_Mismatch
5554 (not Same_Instantiated_Constant
5555 (Entity (Expr1), Entity (Expr2)));
5556 end if;
5557 else
5558 Check_Mismatch (True);
5559 end if;
5560
5561 elsif Is_Entity_Name (Original_Node (Expr1))
5562 and then Is_Entity_Name (Expr2)
5563 and then
5564 Same_Instantiated_Constant
5565 (Entity (Original_Node (Expr1)), Entity (Expr2))
5566 then
5567 null;
5568
5569 elsif Nkind (Expr1) = N_Null then
5570 Check_Mismatch (Nkind (Expr1) /= N_Null);
5571
5572 else
5573 Check_Mismatch (True);
5574 end if;
5575
5576 elsif Ekind (E1) = E_Variable then
5577 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5578
5579 elsif Ekind (E1) = E_Package then
5580 Check_Mismatch
5581 (Ekind (E1) /= Ekind (E2)
5582 or else Renamed_Object (E1) /= Renamed_Object (E2));
5583
5584 elsif Is_Overloadable (E1) then
5585
5586 -- Verify that the actual subprograms match. Note that actuals
5587 -- that are attributes are rewritten as subprograms. If the
5588 -- subprogram in the formal package is defaulted, no check is
5589 -- needed. Note that this can only happen in Ada 2005 when the
5590 -- formal package can be partially parameterized.
5591
5592 if Nkind (Unit_Declaration_Node (E1)) =
5593 N_Subprogram_Renaming_Declaration
5594 and then From_Default (Unit_Declaration_Node (E1))
5595 then
5596 null;
5597
5598 -- If the formal package has an "others" box association that
5599 -- covers this formal, there is no need for a check either.
5600
5601 elsif Nkind (Unit_Declaration_Node (E2)) in
5602 N_Formal_Subprogram_Declaration
5603 and then Box_Present (Unit_Declaration_Node (E2))
5604 then
5605 null;
5606
5607 -- No check needed if subprogram is a defaulted null procedure
5608
5609 elsif No (Alias (E2))
5610 and then Ekind (E2) = E_Procedure
5611 and then
5612 Null_Present (Specification (Unit_Declaration_Node (E2)))
5613 then
5614 null;
5615
5616 -- Otherwise the actual in the formal and the actual in the
5617 -- instantiation of the formal must match, up to renamings.
5618
5619 else
5620 Check_Mismatch
5621 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5622 end if;
5623
5624 else
5625 raise Program_Error;
5626 end if;
5627
5628 <<Next_E>>
5629 Next_Entity (E1);
5630 Next_Entity (E2);
5631 end loop;
5632 end Check_Formal_Package_Instance;
5633
5634 ---------------------------
5635 -- Check_Formal_Packages --
5636 ---------------------------
5637
5638 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5639 E : Entity_Id;
5640 Formal_P : Entity_Id;
5641
5642 begin
5643 -- Iterate through the declarations in the instance, looking for package
5644 -- renaming declarations that denote instances of formal packages. Stop
5645 -- when we find the renaming of the current package itself. The
5646 -- declaration for a formal package without a box is followed by an
5647 -- internal entity that repeats the instantiation.
5648
5649 E := First_Entity (P_Id);
5650 while Present (E) loop
5651 if Ekind (E) = E_Package then
5652 if Renamed_Object (E) = P_Id then
5653 exit;
5654
5655 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5656 null;
5657
5658 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5659 Formal_P := Next_Entity (E);
5660 Check_Formal_Package_Instance (Formal_P, E);
5661
5662 -- After checking, remove the internal validating package. It
5663 -- is only needed for semantic checks, and as it may contain
5664 -- generic formal declarations it should not reach gigi.
5665
5666 Remove (Unit_Declaration_Node (Formal_P));
5667 end if;
5668 end if;
5669
5670 Next_Entity (E);
5671 end loop;
5672 end Check_Formal_Packages;
5673
5674 ---------------------------------
5675 -- Check_Forward_Instantiation --
5676 ---------------------------------
5677
5678 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5679 S : Entity_Id;
5680 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5681
5682 begin
5683 -- The instantiation appears before the generic body if we are in the
5684 -- scope of the unit containing the generic, either in its spec or in
5685 -- the package body, and before the generic body.
5686
5687 if Ekind (Gen_Comp) = E_Package_Body then
5688 Gen_Comp := Spec_Entity (Gen_Comp);
5689 end if;
5690
5691 if In_Open_Scopes (Gen_Comp)
5692 and then No (Corresponding_Body (Decl))
5693 then
5694 S := Current_Scope;
5695
5696 while Present (S)
5697 and then not Is_Compilation_Unit (S)
5698 and then not Is_Child_Unit (S)
5699 loop
5700 if Ekind (S) = E_Package then
5701 Set_Has_Forward_Instantiation (S);
5702 end if;
5703
5704 S := Scope (S);
5705 end loop;
5706 end if;
5707 end Check_Forward_Instantiation;
5708
5709 ---------------------------
5710 -- Check_Generic_Actuals --
5711 ---------------------------
5712
5713 -- The visibility of the actuals may be different between the point of
5714 -- generic instantiation and the instantiation of the body.
5715
5716 procedure Check_Generic_Actuals
5717 (Instance : Entity_Id;
5718 Is_Formal_Box : Boolean)
5719 is
5720 E : Entity_Id;
5721 Astype : Entity_Id;
5722
5723 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5724 -- For a formal that is an array type, the component type is often a
5725 -- previous formal in the same unit. The privacy status of the component
5726 -- type will have been examined earlier in the traversal of the
5727 -- corresponding actuals, and this status should not be modified for
5728 -- the array (sub)type itself. However, if the base type of the array
5729 -- (sub)type is private, its full view must be restored in the body to
5730 -- be consistent with subsequent index subtypes, etc.
5731 --
5732 -- To detect this case we have to rescan the list of formals, which is
5733 -- usually short enough to ignore the resulting inefficiency.
5734
5735 -----------------------------
5736 -- Denotes_Previous_Actual --
5737 -----------------------------
5738
5739 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5740 Prev : Entity_Id;
5741
5742 begin
5743 Prev := First_Entity (Instance);
5744 while Present (Prev) loop
5745 if Is_Type (Prev)
5746 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5747 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5748 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5749 then
5750 return True;
5751
5752 elsif Prev = E then
5753 return False;
5754
5755 else
5756 Next_Entity (Prev);
5757 end if;
5758 end loop;
5759
5760 return False;
5761 end Denotes_Previous_Actual;
5762
5763 -- Start of processing for Check_Generic_Actuals
5764
5765 begin
5766 E := First_Entity (Instance);
5767 while Present (E) loop
5768 if Is_Type (E)
5769 and then Nkind (Parent (E)) = N_Subtype_Declaration
5770 and then Scope (Etype (E)) /= Instance
5771 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5772 then
5773 if Is_Array_Type (E)
5774 and then not Is_Private_Type (Etype (E))
5775 and then Denotes_Previous_Actual (Component_Type (E))
5776 then
5777 null;
5778 else
5779 Check_Private_View (Subtype_Indication (Parent (E)));
5780 end if;
5781
5782 Set_Is_Generic_Actual_Type (E, True);
5783 Set_Is_Hidden (E, False);
5784 Set_Is_Potentially_Use_Visible (E,
5785 In_Use (Instance));
5786
5787 -- We constructed the generic actual type as a subtype of the
5788 -- supplied type. This means that it normally would not inherit
5789 -- subtype specific attributes of the actual, which is wrong for
5790 -- the generic case.
5791
5792 Astype := Ancestor_Subtype (E);
5793
5794 if No (Astype) then
5795
5796 -- This can happen when E is an itype that is the full view of
5797 -- a private type completed, e.g. with a constrained array. In
5798 -- that case, use the first subtype, which will carry size
5799 -- information. The base type itself is unconstrained and will
5800 -- not carry it.
5801
5802 Astype := First_Subtype (E);
5803 end if;
5804
5805 Set_Size_Info (E, (Astype));
5806 Set_RM_Size (E, RM_Size (Astype));
5807 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5808
5809 if Is_Discrete_Or_Fixed_Point_Type (E) then
5810 Set_RM_Size (E, RM_Size (Astype));
5811
5812 -- In nested instances, the base type of an access actual may
5813 -- itself be private, and need to be exchanged.
5814
5815 elsif Is_Access_Type (E)
5816 and then Is_Private_Type (Etype (E))
5817 then
5818 Check_Private_View
5819 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5820 end if;
5821
5822 elsif Ekind (E) = E_Package then
5823
5824 -- If this is the renaming for the current instance, we're done.
5825 -- Otherwise it is a formal package. If the corresponding formal
5826 -- was declared with a box, the (instantiations of the) generic
5827 -- formal part are also visible. Otherwise, ignore the entity
5828 -- created to validate the actuals.
5829
5830 if Renamed_Object (E) = Instance then
5831 exit;
5832
5833 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5834 null;
5835
5836 -- The visibility of a formal of an enclosing generic is already
5837 -- correct.
5838
5839 elsif Denotes_Formal_Package (E) then
5840 null;
5841
5842 elsif Present (Associated_Formal_Package (E))
5843 and then not Is_Generic_Formal (E)
5844 then
5845 if Box_Present (Parent (Associated_Formal_Package (E))) then
5846 Check_Generic_Actuals (Renamed_Object (E), True);
5847
5848 else
5849 Check_Generic_Actuals (Renamed_Object (E), False);
5850 end if;
5851
5852 Set_Is_Hidden (E, False);
5853 end if;
5854
5855 -- If this is a subprogram instance (in a wrapper package) the
5856 -- actual is fully visible.
5857
5858 elsif Is_Wrapper_Package (Instance) then
5859 Set_Is_Hidden (E, False);
5860
5861 -- If the formal package is declared with a box, or if the formal
5862 -- parameter is defaulted, it is visible in the body.
5863
5864 elsif Is_Formal_Box
5865 or else Is_Visible_Formal (E)
5866 then
5867 Set_Is_Hidden (E, False);
5868 end if;
5869
5870 if Ekind (E) = E_Constant then
5871
5872 -- If the type of the actual is a private type declared in the
5873 -- enclosing scope of the generic unit, the body of the generic
5874 -- sees the full view of the type (because it has to appear in
5875 -- the corresponding package body). If the type is private now,
5876 -- exchange views to restore the proper visiblity in the instance.
5877
5878 declare
5879 Typ : constant Entity_Id := Base_Type (Etype (E));
5880 -- The type of the actual
5881
5882 Gen_Id : Entity_Id;
5883 -- The generic unit
5884
5885 Parent_Scope : Entity_Id;
5886 -- The enclosing scope of the generic unit
5887
5888 begin
5889 if Is_Wrapper_Package (Instance) then
5890 Gen_Id :=
5891 Generic_Parent
5892 (Specification
5893 (Unit_Declaration_Node
5894 (Related_Instance (Instance))));
5895 else
5896 Gen_Id :=
5897 Generic_Parent (Package_Specification (Instance));
5898 end if;
5899
5900 Parent_Scope := Scope (Gen_Id);
5901
5902 -- The exchange is only needed if the generic is defined
5903 -- within a package which is not a common ancestor of the
5904 -- scope of the instance, and is not already in scope.
5905
5906 if Is_Private_Type (Typ)
5907 and then Scope (Typ) = Parent_Scope
5908 and then Scope (Instance) /= Parent_Scope
5909 and then Ekind (Parent_Scope) = E_Package
5910 and then not Is_Child_Unit (Gen_Id)
5911 then
5912 Switch_View (Typ);
5913
5914 -- If the type of the entity is a subtype, it may also have
5915 -- to be made visible, together with the base type of its
5916 -- full view, after exchange.
5917
5918 if Is_Private_Type (Etype (E)) then
5919 Switch_View (Etype (E));
5920 Switch_View (Base_Type (Etype (E)));
5921 end if;
5922 end if;
5923 end;
5924 end if;
5925
5926 Next_Entity (E);
5927 end loop;
5928 end Check_Generic_Actuals;
5929
5930 ------------------------------
5931 -- Check_Generic_Child_Unit --
5932 ------------------------------
5933
5934 procedure Check_Generic_Child_Unit
5935 (Gen_Id : Node_Id;
5936 Parent_Installed : in out Boolean)
5937 is
5938 Loc : constant Source_Ptr := Sloc (Gen_Id);
5939 Gen_Par : Entity_Id := Empty;
5940 E : Entity_Id;
5941 Inst_Par : Entity_Id;
5942 S : Node_Id;
5943
5944 function Find_Generic_Child
5945 (Scop : Entity_Id;
5946 Id : Node_Id) return Entity_Id;
5947 -- Search generic parent for possible child unit with the given name
5948
5949 function In_Enclosing_Instance return Boolean;
5950 -- Within an instance of the parent, the child unit may be denoted by
5951 -- a simple name, or an abbreviated expanded name. Examine enclosing
5952 -- scopes to locate a possible parent instantiation.
5953
5954 ------------------------
5955 -- Find_Generic_Child --
5956 ------------------------
5957
5958 function Find_Generic_Child
5959 (Scop : Entity_Id;
5960 Id : Node_Id) return Entity_Id
5961 is
5962 E : Entity_Id;
5963
5964 begin
5965 -- If entity of name is already set, instance has already been
5966 -- resolved, e.g. in an enclosing instantiation.
5967
5968 if Present (Entity (Id)) then
5969 if Scope (Entity (Id)) = Scop then
5970 return Entity (Id);
5971 else
5972 return Empty;
5973 end if;
5974
5975 else
5976 E := First_Entity (Scop);
5977 while Present (E) loop
5978 if Chars (E) = Chars (Id)
5979 and then Is_Child_Unit (E)
5980 then
5981 if Is_Child_Unit (E)
5982 and then not Is_Visible_Lib_Unit (E)
5983 then
5984 Error_Msg_NE
5985 ("generic child unit& is not visible", Gen_Id, E);
5986 end if;
5987
5988 Set_Entity (Id, E);
5989 return E;
5990 end if;
5991
5992 Next_Entity (E);
5993 end loop;
5994
5995 return Empty;
5996 end if;
5997 end Find_Generic_Child;
5998
5999 ---------------------------
6000 -- In_Enclosing_Instance --
6001 ---------------------------
6002
6003 function In_Enclosing_Instance return Boolean is
6004 Enclosing_Instance : Node_Id;
6005 Instance_Decl : Node_Id;
6006
6007 begin
6008 -- We do not inline any call that contains instantiations, except
6009 -- for instantiations of Unchecked_Conversion, so if we are within
6010 -- an inlined body the current instance does not require parents.
6011
6012 if In_Inlined_Body then
6013 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6014 return False;
6015 end if;
6016
6017 -- Loop to check enclosing scopes
6018
6019 Enclosing_Instance := Current_Scope;
6020 while Present (Enclosing_Instance) loop
6021 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6022
6023 if Ekind (Enclosing_Instance) = E_Package
6024 and then Is_Generic_Instance (Enclosing_Instance)
6025 and then Present
6026 (Generic_Parent (Specification (Instance_Decl)))
6027 then
6028 -- Check whether the generic we are looking for is a child of
6029 -- this instance.
6030
6031 E := Find_Generic_Child
6032 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6033 exit when Present (E);
6034
6035 else
6036 E := Empty;
6037 end if;
6038
6039 Enclosing_Instance := Scope (Enclosing_Instance);
6040 end loop;
6041
6042 if No (E) then
6043
6044 -- Not a child unit
6045
6046 Analyze (Gen_Id);
6047 return False;
6048
6049 else
6050 Rewrite (Gen_Id,
6051 Make_Expanded_Name (Loc,
6052 Chars => Chars (E),
6053 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6054 Selector_Name => New_Occurrence_Of (E, Loc)));
6055
6056 Set_Entity (Gen_Id, E);
6057 Set_Etype (Gen_Id, Etype (E));
6058 Parent_Installed := False; -- Already in scope.
6059 return True;
6060 end if;
6061 end In_Enclosing_Instance;
6062
6063 -- Start of processing for Check_Generic_Child_Unit
6064
6065 begin
6066 -- If the name of the generic is given by a selected component, it may
6067 -- be the name of a generic child unit, and the prefix is the name of an
6068 -- instance of the parent, in which case the child unit must be visible.
6069 -- If this instance is not in scope, it must be placed there and removed
6070 -- after instantiation, because what is being instantiated is not the
6071 -- original child, but the corresponding child present in the instance
6072 -- of the parent.
6073
6074 -- If the child is instantiated within the parent, it can be given by
6075 -- a simple name. In this case the instance is already in scope, but
6076 -- the child generic must be recovered from the generic parent as well.
6077
6078 if Nkind (Gen_Id) = N_Selected_Component then
6079 S := Selector_Name (Gen_Id);
6080 Analyze (Prefix (Gen_Id));
6081 Inst_Par := Entity (Prefix (Gen_Id));
6082
6083 if Ekind (Inst_Par) = E_Package
6084 and then Present (Renamed_Object (Inst_Par))
6085 then
6086 Inst_Par := Renamed_Object (Inst_Par);
6087 end if;
6088
6089 if Ekind (Inst_Par) = E_Package then
6090 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6091 Gen_Par := Generic_Parent (Parent (Inst_Par));
6092
6093 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6094 and then
6095 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6096 then
6097 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6098 end if;
6099
6100 elsif Ekind (Inst_Par) = E_Generic_Package
6101 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6102 then
6103 -- A formal package may be a real child package, and not the
6104 -- implicit instance within a parent. In this case the child is
6105 -- not visible and has to be retrieved explicitly as well.
6106
6107 Gen_Par := Inst_Par;
6108 end if;
6109
6110 if Present (Gen_Par) then
6111
6112 -- The prefix denotes an instantiation. The entity itself may be a
6113 -- nested generic, or a child unit.
6114
6115 E := Find_Generic_Child (Gen_Par, S);
6116
6117 if Present (E) then
6118 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6119 Set_Entity (Gen_Id, E);
6120 Set_Etype (Gen_Id, Etype (E));
6121 Set_Entity (S, E);
6122 Set_Etype (S, Etype (E));
6123
6124 -- Indicate that this is a reference to the parent
6125
6126 if In_Extended_Main_Source_Unit (Gen_Id) then
6127 Set_Is_Instantiated (Inst_Par);
6128 end if;
6129
6130 -- A common mistake is to replicate the naming scheme of a
6131 -- hierarchy by instantiating a generic child directly, rather
6132 -- than the implicit child in a parent instance:
6133
6134 -- generic .. package Gpar is ..
6135 -- generic .. package Gpar.Child is ..
6136 -- package Par is new Gpar ();
6137
6138 -- with Gpar.Child;
6139 -- package Par.Child is new Gpar.Child ();
6140 -- rather than Par.Child
6141
6142 -- In this case the instantiation is within Par, which is an
6143 -- instance, but Gpar does not denote Par because we are not IN
6144 -- the instance of Gpar, so this is illegal. The test below
6145 -- recognizes this particular case.
6146
6147 if Is_Child_Unit (E)
6148 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6149 and then (not In_Instance
6150 or else Nkind (Parent (Parent (Gen_Id))) =
6151 N_Compilation_Unit)
6152 then
6153 Error_Msg_N
6154 ("prefix of generic child unit must be instance of parent",
6155 Gen_Id);
6156 end if;
6157
6158 if not In_Open_Scopes (Inst_Par)
6159 and then Nkind (Parent (Gen_Id)) not in
6160 N_Generic_Renaming_Declaration
6161 then
6162 Install_Parent (Inst_Par);
6163 Parent_Installed := True;
6164
6165 elsif In_Open_Scopes (Inst_Par) then
6166
6167 -- If the parent is already installed, install the actuals
6168 -- for its formal packages. This is necessary when the child
6169 -- instance is a child of the parent instance: in this case,
6170 -- the parent is placed on the scope stack but the formal
6171 -- packages are not made visible.
6172
6173 Install_Formal_Packages (Inst_Par);
6174 end if;
6175
6176 else
6177 -- If the generic parent does not contain an entity that
6178 -- corresponds to the selector, the instance doesn't either.
6179 -- Analyzing the node will yield the appropriate error message.
6180 -- If the entity is not a child unit, then it is an inner
6181 -- generic in the parent.
6182
6183 Analyze (Gen_Id);
6184 end if;
6185
6186 else
6187 Analyze (Gen_Id);
6188
6189 if Is_Child_Unit (Entity (Gen_Id))
6190 and then
6191 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6192 and then not In_Open_Scopes (Inst_Par)
6193 then
6194 Install_Parent (Inst_Par);
6195 Parent_Installed := True;
6196
6197 -- The generic unit may be the renaming of the implicit child
6198 -- present in an instance. In that case the parent instance is
6199 -- obtained from the name of the renamed entity.
6200
6201 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6202 and then Present (Renamed_Entity (Entity (Gen_Id)))
6203 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6204 then
6205 declare
6206 Renamed_Package : constant Node_Id :=
6207 Name (Parent (Entity (Gen_Id)));
6208 begin
6209 if Nkind (Renamed_Package) = N_Expanded_Name then
6210 Inst_Par := Entity (Prefix (Renamed_Package));
6211 Install_Parent (Inst_Par);
6212 Parent_Installed := True;
6213 end if;
6214 end;
6215 end if;
6216 end if;
6217
6218 elsif Nkind (Gen_Id) = N_Expanded_Name then
6219
6220 -- Entity already present, analyze prefix, whose meaning may be
6221 -- an instance in the current context. If it is an instance of
6222 -- a relative within another, the proper parent may still have
6223 -- to be installed, if they are not of the same generation.
6224
6225 Analyze (Prefix (Gen_Id));
6226
6227 -- In the unlikely case that a local declaration hides the name
6228 -- of the parent package, locate it on the homonym chain. If the
6229 -- context is an instance of the parent, the renaming entity is
6230 -- flagged as such.
6231
6232 Inst_Par := Entity (Prefix (Gen_Id));
6233 while Present (Inst_Par)
6234 and then not Is_Package_Or_Generic_Package (Inst_Par)
6235 loop
6236 Inst_Par := Homonym (Inst_Par);
6237 end loop;
6238
6239 pragma Assert (Present (Inst_Par));
6240 Set_Entity (Prefix (Gen_Id), Inst_Par);
6241
6242 if In_Enclosing_Instance then
6243 null;
6244
6245 elsif Present (Entity (Gen_Id))
6246 and then Is_Child_Unit (Entity (Gen_Id))
6247 and then not In_Open_Scopes (Inst_Par)
6248 then
6249 Install_Parent (Inst_Par);
6250 Parent_Installed := True;
6251 end if;
6252
6253 elsif In_Enclosing_Instance then
6254
6255 -- The child unit is found in some enclosing scope
6256
6257 null;
6258
6259 else
6260 Analyze (Gen_Id);
6261
6262 -- If this is the renaming of the implicit child in a parent
6263 -- instance, recover the parent name and install it.
6264
6265 if Is_Entity_Name (Gen_Id) then
6266 E := Entity (Gen_Id);
6267
6268 if Is_Generic_Unit (E)
6269 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6270 and then Is_Child_Unit (Renamed_Object (E))
6271 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6272 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6273 then
6274 Rewrite (Gen_Id,
6275 New_Copy_Tree (Name (Parent (E))));
6276 Inst_Par := Entity (Prefix (Gen_Id));
6277
6278 if not In_Open_Scopes (Inst_Par) then
6279 Install_Parent (Inst_Par);
6280 Parent_Installed := True;
6281 end if;
6282
6283 -- If it is a child unit of a non-generic parent, it may be
6284 -- use-visible and given by a direct name. Install parent as
6285 -- for other cases.
6286
6287 elsif Is_Generic_Unit (E)
6288 and then Is_Child_Unit (E)
6289 and then
6290 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6291 and then not Is_Generic_Unit (Scope (E))
6292 then
6293 if not In_Open_Scopes (Scope (E)) then
6294 Install_Parent (Scope (E));
6295 Parent_Installed := True;
6296 end if;
6297 end if;
6298 end if;
6299 end if;
6300 end Check_Generic_Child_Unit;
6301
6302 -----------------------------
6303 -- Check_Hidden_Child_Unit --
6304 -----------------------------
6305
6306 procedure Check_Hidden_Child_Unit
6307 (N : Node_Id;
6308 Gen_Unit : Entity_Id;
6309 Act_Decl_Id : Entity_Id)
6310 is
6311 Gen_Id : constant Node_Id := Name (N);
6312
6313 begin
6314 if Is_Child_Unit (Gen_Unit)
6315 and then Is_Child_Unit (Act_Decl_Id)
6316 and then Nkind (Gen_Id) = N_Expanded_Name
6317 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6318 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6319 then
6320 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6321 Error_Msg_NE
6322 ("generic unit & is implicitly declared in &",
6323 Defining_Unit_Name (N), Gen_Unit);
6324 Error_Msg_N ("\instance must have different name",
6325 Defining_Unit_Name (N));
6326 end if;
6327 end Check_Hidden_Child_Unit;
6328
6329 ------------------------
6330 -- Check_Private_View --
6331 ------------------------
6332
6333 procedure Check_Private_View (N : Node_Id) is
6334 T : constant Entity_Id := Etype (N);
6335 BT : Entity_Id;
6336
6337 begin
6338 -- Exchange views if the type was not private in the generic but is
6339 -- private at the point of instantiation. Do not exchange views if
6340 -- the scope of the type is in scope. This can happen if both generic
6341 -- and instance are sibling units, or if type is defined in a parent.
6342 -- In this case the visibility of the type will be correct for all
6343 -- semantic checks.
6344
6345 if Present (T) then
6346 BT := Base_Type (T);
6347
6348 if Is_Private_Type (T)
6349 and then not Has_Private_View (N)
6350 and then Present (Full_View (T))
6351 and then not In_Open_Scopes (Scope (T))
6352 then
6353 -- In the generic, the full type was visible. Save the private
6354 -- entity, for subsequent exchange.
6355
6356 Switch_View (T);
6357
6358 elsif Has_Private_View (N)
6359 and then not Is_Private_Type (T)
6360 and then not Has_Been_Exchanged (T)
6361 and then Etype (Get_Associated_Node (N)) /= T
6362 then
6363 -- Only the private declaration was visible in the generic. If
6364 -- the type appears in a subtype declaration, the subtype in the
6365 -- instance must have a view compatible with that of its parent,
6366 -- which must be exchanged (see corresponding code in Restore_
6367 -- Private_Views). Otherwise, if the type is defined in a parent
6368 -- unit, leave full visibility within instance, which is safe.
6369
6370 if In_Open_Scopes (Scope (Base_Type (T)))
6371 and then not Is_Private_Type (Base_Type (T))
6372 and then Comes_From_Source (Base_Type (T))
6373 then
6374 null;
6375
6376 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6377 or else not In_Private_Part (Scope (Base_Type (T)))
6378 then
6379 Prepend_Elmt (T, Exchanged_Views);
6380 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6381 end if;
6382
6383 -- For composite types with inconsistent representation exchange
6384 -- component types accordingly.
6385
6386 elsif Is_Access_Type (T)
6387 and then Is_Private_Type (Designated_Type (T))
6388 and then not Has_Private_View (N)
6389 and then Present (Full_View (Designated_Type (T)))
6390 then
6391 Switch_View (Designated_Type (T));
6392
6393 elsif Is_Array_Type (T) then
6394 if Is_Private_Type (Component_Type (T))
6395 and then not Has_Private_View (N)
6396 and then Present (Full_View (Component_Type (T)))
6397 then
6398 Switch_View (Component_Type (T));
6399 end if;
6400
6401 -- The normal exchange mechanism relies on the setting of a
6402 -- flag on the reference in the generic. However, an additional
6403 -- mechanism is needed for types that are not explicitly
6404 -- mentioned in the generic, but may be needed in expanded code
6405 -- in the instance. This includes component types of arrays and
6406 -- designated types of access types. This processing must also
6407 -- include the index types of arrays which we take care of here.
6408
6409 declare
6410 Indx : Node_Id;
6411 Typ : Entity_Id;
6412
6413 begin
6414 Indx := First_Index (T);
6415 while Present (Indx) loop
6416 Typ := Base_Type (Etype (Indx));
6417
6418 if Is_Private_Type (Typ)
6419 and then Present (Full_View (Typ))
6420 then
6421 Switch_View (Typ);
6422 end if;
6423
6424 Next_Index (Indx);
6425 end loop;
6426 end;
6427
6428 elsif Is_Private_Type (T)
6429 and then Present (Full_View (T))
6430 and then Is_Array_Type (Full_View (T))
6431 and then Is_Private_Type (Component_Type (Full_View (T)))
6432 then
6433 Switch_View (T);
6434
6435 -- Finally, a non-private subtype may have a private base type, which
6436 -- must be exchanged for consistency. This can happen when a package
6437 -- body is instantiated, when the scope stack is empty but in fact
6438 -- the subtype and the base type are declared in an enclosing scope.
6439
6440 -- Note that in this case we introduce an inconsistency in the view
6441 -- set, because we switch the base type BT, but there could be some
6442 -- private dependent subtypes of BT which remain unswitched. Such
6443 -- subtypes might need to be switched at a later point (see specific
6444 -- provision for that case in Switch_View).
6445
6446 elsif not Is_Private_Type (T)
6447 and then not Has_Private_View (N)
6448 and then Is_Private_Type (BT)
6449 and then Present (Full_View (BT))
6450 and then not Is_Generic_Type (BT)
6451 and then not In_Open_Scopes (BT)
6452 then
6453 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6454 Exchange_Declarations (BT);
6455 end if;
6456 end if;
6457 end Check_Private_View;
6458
6459 -----------------------------
6460 -- Check_Hidden_Primitives --
6461 -----------------------------
6462
6463 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6464 Actual : Node_Id;
6465 Gen_T : Entity_Id;
6466 Result : Elist_Id := No_Elist;
6467
6468 begin
6469 if No (Assoc_List) then
6470 return No_Elist;
6471 end if;
6472
6473 -- Traverse the list of associations between formals and actuals
6474 -- searching for renamings of tagged types
6475
6476 Actual := First (Assoc_List);
6477 while Present (Actual) loop
6478 if Nkind (Actual) = N_Subtype_Declaration then
6479 Gen_T := Generic_Parent_Type (Actual);
6480
6481 if Present (Gen_T)
6482 and then Is_Tagged_Type (Gen_T)
6483 then
6484 -- Traverse the list of primitives of the actual types
6485 -- searching for hidden primitives that are visible in the
6486 -- corresponding generic formal; leave them visible and
6487 -- append them to Result to restore their decoration later.
6488
6489 Install_Hidden_Primitives
6490 (Prims_List => Result,
6491 Gen_T => Gen_T,
6492 Act_T => Entity (Subtype_Indication (Actual)));
6493 end if;
6494 end if;
6495
6496 Next (Actual);
6497 end loop;
6498
6499 return Result;
6500 end Check_Hidden_Primitives;
6501
6502 --------------------------
6503 -- Contains_Instance_Of --
6504 --------------------------
6505
6506 function Contains_Instance_Of
6507 (Inner : Entity_Id;
6508 Outer : Entity_Id;
6509 N : Node_Id) return Boolean
6510 is
6511 Elmt : Elmt_Id;
6512 Scop : Entity_Id;
6513
6514 begin
6515 Scop := Outer;
6516
6517 -- Verify that there are no circular instantiations. We check whether
6518 -- the unit contains an instance of the current scope or some enclosing
6519 -- scope (in case one of the instances appears in a subunit). Longer
6520 -- circularities involving subunits might seem too pathological to
6521 -- consider, but they were not too pathological for the authors of
6522 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6523 -- enclosing generic scopes as containing an instance.
6524
6525 loop
6526 -- Within a generic subprogram body, the scope is not generic, to
6527 -- allow for recursive subprograms. Use the declaration to determine
6528 -- whether this is a generic unit.
6529
6530 if Ekind (Scop) = E_Generic_Package
6531 or else (Is_Subprogram (Scop)
6532 and then Nkind (Unit_Declaration_Node (Scop)) =
6533 N_Generic_Subprogram_Declaration)
6534 then
6535 Elmt := First_Elmt (Inner_Instances (Inner));
6536
6537 while Present (Elmt) loop
6538 if Node (Elmt) = Scop then
6539 Error_Msg_Node_2 := Inner;
6540 Error_Msg_NE
6541 ("circular Instantiation: & instantiated within &!",
6542 N, Scop);
6543 return True;
6544
6545 elsif Node (Elmt) = Inner then
6546 return True;
6547
6548 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6549 Error_Msg_Node_2 := Inner;
6550 Error_Msg_NE
6551 ("circular Instantiation: & instantiated within &!",
6552 N, Node (Elmt));
6553 return True;
6554 end if;
6555
6556 Next_Elmt (Elmt);
6557 end loop;
6558
6559 -- Indicate that Inner is being instantiated within Scop
6560
6561 Append_Elmt (Inner, Inner_Instances (Scop));
6562 end if;
6563
6564 if Scop = Standard_Standard then
6565 exit;
6566 else
6567 Scop := Scope (Scop);
6568 end if;
6569 end loop;
6570
6571 return False;
6572 end Contains_Instance_Of;
6573
6574 -----------------------
6575 -- Copy_Generic_Node --
6576 -----------------------
6577
6578 function Copy_Generic_Node
6579 (N : Node_Id;
6580 Parent_Id : Node_Id;
6581 Instantiating : Boolean) return Node_Id
6582 is
6583 Ent : Entity_Id;
6584 New_N : Node_Id;
6585
6586 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6587 -- Check the given value of one of the Fields referenced by the current
6588 -- node to determine whether to copy it recursively. The field may hold
6589 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6590 -- Char) in which case it need not be copied.
6591
6592 procedure Copy_Descendants;
6593 -- Common utility for various nodes
6594
6595 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6596 -- Make copy of element list
6597
6598 function Copy_Generic_List
6599 (L : List_Id;
6600 Parent_Id : Node_Id) return List_Id;
6601 -- Apply Copy_Node recursively to the members of a node list
6602
6603 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6604 -- True if an identifier is part of the defining program unit name of
6605 -- a child unit. The entity of such an identifier must be kept (for
6606 -- ASIS use) even though as the name of an enclosing generic it would
6607 -- otherwise not be preserved in the generic tree.
6608
6609 ----------------------
6610 -- Copy_Descendants --
6611 ----------------------
6612
6613 procedure Copy_Descendants is
6614
6615 use Atree.Unchecked_Access;
6616 -- This code section is part of the implementation of an untyped
6617 -- tree traversal, so it needs direct access to node fields.
6618
6619 begin
6620 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6621 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6622 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6623 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6624 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6625 end Copy_Descendants;
6626
6627 -----------------------------
6628 -- Copy_Generic_Descendant --
6629 -----------------------------
6630
6631 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6632 begin
6633 if D = Union_Id (Empty) then
6634 return D;
6635
6636 elsif D in Node_Range then
6637 return Union_Id
6638 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6639
6640 elsif D in List_Range then
6641 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6642
6643 elsif D in Elist_Range then
6644 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6645
6646 -- Nothing else is copyable (e.g. Uint values), return as is
6647
6648 else
6649 return D;
6650 end if;
6651 end Copy_Generic_Descendant;
6652
6653 ------------------------
6654 -- Copy_Generic_Elist --
6655 ------------------------
6656
6657 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6658 M : Elmt_Id;
6659 L : Elist_Id;
6660
6661 begin
6662 if Present (E) then
6663 L := New_Elmt_List;
6664 M := First_Elmt (E);
6665 while Present (M) loop
6666 Append_Elmt
6667 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6668 Next_Elmt (M);
6669 end loop;
6670
6671 return L;
6672
6673 else
6674 return No_Elist;
6675 end if;
6676 end Copy_Generic_Elist;
6677
6678 -----------------------
6679 -- Copy_Generic_List --
6680 -----------------------
6681
6682 function Copy_Generic_List
6683 (L : List_Id;
6684 Parent_Id : Node_Id) return List_Id
6685 is
6686 N : Node_Id;
6687 New_L : List_Id;
6688
6689 begin
6690 if Present (L) then
6691 New_L := New_List;
6692 Set_Parent (New_L, Parent_Id);
6693
6694 N := First (L);
6695 while Present (N) loop
6696 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6697 Next (N);
6698 end loop;
6699
6700 return New_L;
6701
6702 else
6703 return No_List;
6704 end if;
6705 end Copy_Generic_List;
6706
6707 ---------------------------
6708 -- In_Defining_Unit_Name --
6709 ---------------------------
6710
6711 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6712 begin
6713 return Present (Parent (Nam))
6714 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6715 or else
6716 (Nkind (Parent (Nam)) = N_Expanded_Name
6717 and then In_Defining_Unit_Name (Parent (Nam))));
6718 end In_Defining_Unit_Name;
6719
6720 -- Start of processing for Copy_Generic_Node
6721
6722 begin
6723 if N = Empty then
6724 return N;
6725 end if;
6726
6727 New_N := New_Copy (N);
6728
6729 -- Copy aspects if present
6730
6731 if Has_Aspects (N) then
6732 Set_Has_Aspects (New_N, False);
6733 Set_Aspect_Specifications
6734 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6735 end if;
6736
6737 if Instantiating then
6738 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6739 end if;
6740
6741 if not Is_List_Member (N) then
6742 Set_Parent (New_N, Parent_Id);
6743 end if;
6744
6745 -- If defining identifier, then all fields have been copied already
6746
6747 if Nkind (New_N) in N_Entity then
6748 null;
6749
6750 -- Special casing for identifiers and other entity names and operators
6751
6752 elsif Nkind_In (New_N, N_Identifier,
6753 N_Character_Literal,
6754 N_Expanded_Name,
6755 N_Operator_Symbol)
6756 or else Nkind (New_N) in N_Op
6757 then
6758 if not Instantiating then
6759
6760 -- Link both nodes in order to assign subsequently the entity of
6761 -- the copy to the original node, in case this is a global
6762 -- reference.
6763
6764 Set_Associated_Node (N, New_N);
6765
6766 -- If we are within an instantiation, this is a nested generic
6767 -- that has already been analyzed at the point of definition.
6768 -- We must preserve references that were global to the enclosing
6769 -- parent at that point. Other occurrences, whether global or
6770 -- local to the current generic, must be resolved anew, so we
6771 -- reset the entity in the generic copy. A global reference has a
6772 -- smaller depth than the parent, or else the same depth in case
6773 -- both are distinct compilation units.
6774
6775 -- A child unit is implicitly declared within the enclosing parent
6776 -- but is in fact global to it, and must be preserved.
6777
6778 -- It is also possible for Current_Instantiated_Parent to be
6779 -- defined, and for this not to be a nested generic, namely if
6780 -- the unit is loaded through Rtsfind. In that case, the entity of
6781 -- New_N is only a link to the associated node, and not a defining
6782 -- occurrence.
6783
6784 -- The entities for parent units in the defining_program_unit of a
6785 -- generic child unit are established when the context of the unit
6786 -- is first analyzed, before the generic copy is made. They are
6787 -- preserved in the copy for use in ASIS queries.
6788
6789 Ent := Entity (New_N);
6790
6791 if No (Current_Instantiated_Parent.Gen_Id) then
6792 if No (Ent)
6793 or else Nkind (Ent) /= N_Defining_Identifier
6794 or else not In_Defining_Unit_Name (N)
6795 then
6796 Set_Associated_Node (New_N, Empty);
6797 end if;
6798
6799 elsif No (Ent)
6800 or else
6801 not Nkind_In (Ent, N_Defining_Identifier,
6802 N_Defining_Character_Literal,
6803 N_Defining_Operator_Symbol)
6804 or else No (Scope (Ent))
6805 or else
6806 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6807 and then not Is_Child_Unit (Ent))
6808 or else
6809 (Scope_Depth (Scope (Ent)) >
6810 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6811 and then
6812 Get_Source_Unit (Ent) =
6813 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6814 then
6815 Set_Associated_Node (New_N, Empty);
6816 end if;
6817
6818 -- Case of instantiating identifier or some other name or operator
6819
6820 else
6821 -- If the associated node is still defined, the entity in it
6822 -- is global, and must be copied to the instance. If this copy
6823 -- is being made for a body to inline, it is applied to an
6824 -- instantiated tree, and the entity is already present and
6825 -- must be also preserved.
6826
6827 declare
6828 Assoc : constant Node_Id := Get_Associated_Node (N);
6829
6830 begin
6831 if Present (Assoc) then
6832 if Nkind (Assoc) = Nkind (N) then
6833 Set_Entity (New_N, Entity (Assoc));
6834 Check_Private_View (N);
6835
6836 -- The name in the call may be a selected component if the
6837 -- call has not been analyzed yet, as may be the case for
6838 -- pre/post conditions in a generic unit.
6839
6840 elsif Nkind (Assoc) = N_Function_Call
6841 and then Is_Entity_Name (Name (Assoc))
6842 then
6843 Set_Entity (New_N, Entity (Name (Assoc)));
6844
6845 elsif Nkind_In (Assoc, N_Defining_Identifier,
6846 N_Defining_Character_Literal,
6847 N_Defining_Operator_Symbol)
6848 and then Expander_Active
6849 then
6850 -- Inlining case: we are copying a tree that contains
6851 -- global entities, which are preserved in the copy to be
6852 -- used for subsequent inlining.
6853
6854 null;
6855
6856 else
6857 Set_Entity (New_N, Empty);
6858 end if;
6859 end if;
6860 end;
6861 end if;
6862
6863 -- For expanded name, we must copy the Prefix and Selector_Name
6864
6865 if Nkind (N) = N_Expanded_Name then
6866 Set_Prefix
6867 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
6868
6869 Set_Selector_Name (New_N,
6870 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
6871
6872 -- For operators, we must copy the right operand
6873
6874 elsif Nkind (N) in N_Op then
6875 Set_Right_Opnd (New_N,
6876 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
6877
6878 -- And for binary operators, the left operand as well
6879
6880 if Nkind (N) in N_Binary_Op then
6881 Set_Left_Opnd (New_N,
6882 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
6883 end if;
6884 end if;
6885
6886 -- Special casing for stubs
6887
6888 elsif Nkind (N) in N_Body_Stub then
6889
6890 -- In any case, we must copy the specification or defining
6891 -- identifier as appropriate.
6892
6893 if Nkind (N) = N_Subprogram_Body_Stub then
6894 Set_Specification (New_N,
6895 Copy_Generic_Node (Specification (N), New_N, Instantiating));
6896
6897 else
6898 Set_Defining_Identifier (New_N,
6899 Copy_Generic_Node
6900 (Defining_Identifier (N), New_N, Instantiating));
6901 end if;
6902
6903 -- If we are not instantiating, then this is where we load and
6904 -- analyze subunits, i.e. at the point where the stub occurs. A
6905 -- more permissive system might defer this analysis to the point
6906 -- of instantiation, but this seems too complicated for now.
6907
6908 if not Instantiating then
6909 declare
6910 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
6911 Subunit : Node_Id;
6912 Unum : Unit_Number_Type;
6913 New_Body : Node_Id;
6914
6915 begin
6916 -- Make sure that, if it is a subunit of the main unit that is
6917 -- preprocessed and if -gnateG is specified, the preprocessed
6918 -- file will be written.
6919
6920 Lib.Analysing_Subunit_Of_Main :=
6921 Lib.In_Extended_Main_Source_Unit (N);
6922 Unum :=
6923 Load_Unit
6924 (Load_Name => Subunit_Name,
6925 Required => False,
6926 Subunit => True,
6927 Error_Node => N);
6928 Lib.Analysing_Subunit_Of_Main := False;
6929
6930 -- If the proper body is not found, a warning message will be
6931 -- emitted when analyzing the stub, or later at the point of
6932 -- instantiation. Here we just leave the stub as is.
6933
6934 if Unum = No_Unit then
6935 Subunits_Missing := True;
6936 goto Subunit_Not_Found;
6937 end if;
6938
6939 Subunit := Cunit (Unum);
6940
6941 if Nkind (Unit (Subunit)) /= N_Subunit then
6942 Error_Msg_N
6943 ("found child unit instead of expected SEPARATE subunit",
6944 Subunit);
6945 Error_Msg_Sloc := Sloc (N);
6946 Error_Msg_N ("\to complete stub #", Subunit);
6947 goto Subunit_Not_Found;
6948 end if;
6949
6950 -- We must create a generic copy of the subunit, in order to
6951 -- perform semantic analysis on it, and we must replace the
6952 -- stub in the original generic unit with the subunit, in order
6953 -- to preserve non-local references within.
6954
6955 -- Only the proper body needs to be copied. Library_Unit and
6956 -- context clause are simply inherited by the generic copy.
6957 -- Note that the copy (which may be recursive if there are
6958 -- nested subunits) must be done first, before attaching it to
6959 -- the enclosing generic.
6960
6961 New_Body :=
6962 Copy_Generic_Node
6963 (Proper_Body (Unit (Subunit)),
6964 Empty, Instantiating => False);
6965
6966 -- Now place the original proper body in the original generic
6967 -- unit. This is a body, not a compilation unit.
6968
6969 Rewrite (N, Proper_Body (Unit (Subunit)));
6970 Set_Is_Compilation_Unit (Defining_Entity (N), False);
6971 Set_Was_Originally_Stub (N);
6972
6973 -- Finally replace the body of the subunit with its copy, and
6974 -- make this new subunit into the library unit of the generic
6975 -- copy, which does not have stubs any longer.
6976
6977 Set_Proper_Body (Unit (Subunit), New_Body);
6978 Set_Library_Unit (New_N, Subunit);
6979 Inherit_Context (Unit (Subunit), N);
6980 end;
6981
6982 -- If we are instantiating, this must be an error case, since
6983 -- otherwise we would have replaced the stub node by the proper body
6984 -- that corresponds. So just ignore it in the copy (i.e. we have
6985 -- copied it, and that is good enough).
6986
6987 else
6988 null;
6989 end if;
6990
6991 <<Subunit_Not_Found>> null;
6992
6993 -- If the node is a compilation unit, it is the subunit of a stub, which
6994 -- has been loaded already (see code below). In this case, the library
6995 -- unit field of N points to the parent unit (which is a compilation
6996 -- unit) and need not (and cannot) be copied.
6997
6998 -- When the proper body of the stub is analyzed, the library_unit link
6999 -- is used to establish the proper context (see sem_ch10).
7000
7001 -- The other fields of a compilation unit are copied as usual
7002
7003 elsif Nkind (N) = N_Compilation_Unit then
7004
7005 -- This code can only be executed when not instantiating, because in
7006 -- the copy made for an instantiation, the compilation unit node has
7007 -- disappeared at the point that a stub is replaced by its proper
7008 -- body.
7009
7010 pragma Assert (not Instantiating);
7011
7012 Set_Context_Items (New_N,
7013 Copy_Generic_List (Context_Items (N), New_N));
7014
7015 Set_Unit (New_N,
7016 Copy_Generic_Node (Unit (N), New_N, False));
7017
7018 Set_First_Inlined_Subprogram (New_N,
7019 Copy_Generic_Node
7020 (First_Inlined_Subprogram (N), New_N, False));
7021
7022 Set_Aux_Decls_Node (New_N,
7023 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7024
7025 -- For an assignment node, the assignment is known to be semantically
7026 -- legal if we are instantiating the template. This avoids incorrect
7027 -- diagnostics in generated code.
7028
7029 elsif Nkind (N) = N_Assignment_Statement then
7030
7031 -- Copy name and expression fields in usual manner
7032
7033 Set_Name (New_N,
7034 Copy_Generic_Node (Name (N), New_N, Instantiating));
7035
7036 Set_Expression (New_N,
7037 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7038
7039 if Instantiating then
7040 Set_Assignment_OK (Name (New_N), True);
7041 end if;
7042
7043 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7044 if not Instantiating then
7045 Set_Associated_Node (N, New_N);
7046
7047 else
7048 if Present (Get_Associated_Node (N))
7049 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7050 then
7051 -- In the generic the aggregate has some composite type. If at
7052 -- the point of instantiation the type has a private view,
7053 -- install the full view (and that of its ancestors, if any).
7054
7055 declare
7056 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7057 Rt : Entity_Id;
7058
7059 begin
7060 if Present (T)
7061 and then Is_Private_Type (T)
7062 then
7063 Switch_View (T);
7064 end if;
7065
7066 if Present (T)
7067 and then Is_Tagged_Type (T)
7068 and then Is_Derived_Type (T)
7069 then
7070 Rt := Root_Type (T);
7071
7072 loop
7073 T := Etype (T);
7074
7075 if Is_Private_Type (T) then
7076 Switch_View (T);
7077 end if;
7078
7079 exit when T = Rt;
7080 end loop;
7081 end if;
7082 end;
7083 end if;
7084 end if;
7085
7086 -- Do not copy the associated node, which points to the generic copy
7087 -- of the aggregate.
7088
7089 declare
7090 use Atree.Unchecked_Access;
7091 -- This code section is part of the implementation of an untyped
7092 -- tree traversal, so it needs direct access to node fields.
7093
7094 begin
7095 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7096 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7097 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7098 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7099 end;
7100
7101 -- Allocators do not have an identifier denoting the access type, so we
7102 -- must locate it through the expression to check whether the views are
7103 -- consistent.
7104
7105 elsif Nkind (N) = N_Allocator
7106 and then Nkind (Expression (N)) = N_Qualified_Expression
7107 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7108 and then Instantiating
7109 then
7110 declare
7111 T : constant Node_Id :=
7112 Get_Associated_Node (Subtype_Mark (Expression (N)));
7113 Acc_T : Entity_Id;
7114
7115 begin
7116 if Present (T) then
7117
7118 -- Retrieve the allocator node in the generic copy
7119
7120 Acc_T := Etype (Parent (Parent (T)));
7121 if Present (Acc_T)
7122 and then Is_Private_Type (Acc_T)
7123 then
7124 Switch_View (Acc_T);
7125 end if;
7126 end if;
7127
7128 Copy_Descendants;
7129 end;
7130
7131 -- For a proper body, we must catch the case of a proper body that
7132 -- replaces a stub. This represents the point at which a separate
7133 -- compilation unit, and hence template file, may be referenced, so we
7134 -- must make a new source instantiation entry for the template of the
7135 -- subunit, and ensure that all nodes in the subunit are adjusted using
7136 -- this new source instantiation entry.
7137
7138 elsif Nkind (N) in N_Proper_Body then
7139 declare
7140 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7141
7142 begin
7143 if Instantiating and then Was_Originally_Stub (N) then
7144 Create_Instantiation_Source
7145 (Instantiation_Node,
7146 Defining_Entity (N),
7147 False,
7148 S_Adjustment);
7149 end if;
7150
7151 -- Now copy the fields of the proper body, using the new
7152 -- adjustment factor if one was needed as per test above.
7153
7154 Copy_Descendants;
7155
7156 -- Restore the original adjustment factor in case changed
7157
7158 S_Adjustment := Save_Adjustment;
7159 end;
7160
7161 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
7162 -- generic unit, not to the instantiating unit.
7163
7164 elsif Nkind (N) = N_Pragma and then Instantiating then
7165 declare
7166 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
7167 begin
7168 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
7169 New_N := Make_Null_Statement (Sloc (N));
7170 else
7171 Copy_Descendants;
7172 end if;
7173 end;
7174
7175 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7176
7177 -- No descendant fields need traversing
7178
7179 null;
7180
7181 elsif Nkind (N) = N_String_Literal
7182 and then Present (Etype (N))
7183 and then Instantiating
7184 then
7185 -- If the string is declared in an outer scope, the string_literal
7186 -- subtype created for it may have the wrong scope. We force the
7187 -- reanalysis of the constant to generate a new itype in the proper
7188 -- context.
7189
7190 Set_Etype (New_N, Empty);
7191 Set_Analyzed (New_N, False);
7192
7193 -- For the remaining nodes, copy their descendants recursively
7194
7195 else
7196 Copy_Descendants;
7197
7198 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7199 Set_Generic_Parent (Specification (New_N), N);
7200
7201 -- Should preserve Corresponding_Spec??? (12.3(14))
7202 end if;
7203 end if;
7204
7205 return New_N;
7206 end Copy_Generic_Node;
7207
7208 ----------------------------
7209 -- Denotes_Formal_Package --
7210 ----------------------------
7211
7212 function Denotes_Formal_Package
7213 (Pack : Entity_Id;
7214 On_Exit : Boolean := False;
7215 Instance : Entity_Id := Empty) return Boolean
7216 is
7217 Par : Entity_Id;
7218 Scop : constant Entity_Id := Scope (Pack);
7219 E : Entity_Id;
7220
7221 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7222 -- The package in question may be an actual for a previous formal
7223 -- package P of the current instance, so examine its actuals as well.
7224 -- This must be recursive over other formal packages.
7225
7226 ----------------------------------
7227 -- Is_Actual_Of_Previous_Formal --
7228 ----------------------------------
7229
7230 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7231 E1 : Entity_Id;
7232
7233 begin
7234 E1 := First_Entity (P);
7235 while Present (E1) and then E1 /= Instance loop
7236 if Ekind (E1) = E_Package
7237 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7238 then
7239 if Renamed_Object (E1) = Pack then
7240 return True;
7241
7242 elsif E1 = P or else Renamed_Object (E1) = P then
7243 return False;
7244
7245 elsif Is_Actual_Of_Previous_Formal (E1) then
7246 return True;
7247 end if;
7248 end if;
7249
7250 Next_Entity (E1);
7251 end loop;
7252
7253 return False;
7254 end Is_Actual_Of_Previous_Formal;
7255
7256 -- Start of processing for Denotes_Formal_Package
7257
7258 begin
7259 if On_Exit then
7260 Par :=
7261 Instance_Envs.Table
7262 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7263 else
7264 Par := Current_Instantiated_Parent.Act_Id;
7265 end if;
7266
7267 if Ekind (Scop) = E_Generic_Package
7268 or else Nkind (Unit_Declaration_Node (Scop)) =
7269 N_Generic_Subprogram_Declaration
7270 then
7271 return True;
7272
7273 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7274 N_Formal_Package_Declaration
7275 then
7276 return True;
7277
7278 elsif No (Par) then
7279 return False;
7280
7281 else
7282 -- Check whether this package is associated with a formal package of
7283 -- the enclosing instantiation. Iterate over the list of renamings.
7284
7285 E := First_Entity (Par);
7286 while Present (E) loop
7287 if Ekind (E) /= E_Package
7288 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7289 then
7290 null;
7291
7292 elsif Renamed_Object (E) = Par then
7293 return False;
7294
7295 elsif Renamed_Object (E) = Pack then
7296 return True;
7297
7298 elsif Is_Actual_Of_Previous_Formal (E) then
7299 return True;
7300
7301 end if;
7302
7303 Next_Entity (E);
7304 end loop;
7305
7306 return False;
7307 end if;
7308 end Denotes_Formal_Package;
7309
7310 -----------------
7311 -- End_Generic --
7312 -----------------
7313
7314 procedure End_Generic is
7315 begin
7316 -- ??? More things could be factored out in this routine. Should
7317 -- probably be done at a later stage.
7318
7319 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7320 Generic_Flags.Decrement_Last;
7321
7322 Expander_Mode_Restore;
7323 end End_Generic;
7324
7325 -------------
7326 -- Earlier --
7327 -------------
7328
7329 function Earlier (N1, N2 : Node_Id) return Boolean is
7330 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7331 -- Find distance from given node to enclosing compilation unit
7332
7333 ----------------
7334 -- Find_Depth --
7335 ----------------
7336
7337 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7338 begin
7339 while Present (P)
7340 and then Nkind (P) /= N_Compilation_Unit
7341 loop
7342 P := True_Parent (P);
7343 D := D + 1;
7344 end loop;
7345 end Find_Depth;
7346
7347 -- Local declarations
7348
7349 D1 : Integer := 0;
7350 D2 : Integer := 0;
7351 P1 : Node_Id := N1;
7352 P2 : Node_Id := N2;
7353 T1 : Source_Ptr;
7354 T2 : Source_Ptr;
7355
7356 -- Start of processing for Earlier
7357
7358 begin
7359 Find_Depth (P1, D1);
7360 Find_Depth (P2, D2);
7361
7362 if P1 /= P2 then
7363 return False;
7364 else
7365 P1 := N1;
7366 P2 := N2;
7367 end if;
7368
7369 while D1 > D2 loop
7370 P1 := True_Parent (P1);
7371 D1 := D1 - 1;
7372 end loop;
7373
7374 while D2 > D1 loop
7375 P2 := True_Parent (P2);
7376 D2 := D2 - 1;
7377 end loop;
7378
7379 -- At this point P1 and P2 are at the same distance from the root.
7380 -- We examine their parents until we find a common declarative list.
7381 -- If we reach the root, N1 and N2 do not descend from the same
7382 -- declarative list (e.g. one is nested in the declarative part and
7383 -- the other is in a block in the statement part) and the earlier
7384 -- one is already frozen.
7385
7386 while not Is_List_Member (P1)
7387 or else not Is_List_Member (P2)
7388 or else List_Containing (P1) /= List_Containing (P2)
7389 loop
7390 P1 := True_Parent (P1);
7391 P2 := True_Parent (P2);
7392
7393 if Nkind (Parent (P1)) = N_Subunit then
7394 P1 := Corresponding_Stub (Parent (P1));
7395 end if;
7396
7397 if Nkind (Parent (P2)) = N_Subunit then
7398 P2 := Corresponding_Stub (Parent (P2));
7399 end if;
7400
7401 if P1 = P2 then
7402 return False;
7403 end if;
7404 end loop;
7405
7406 -- Expanded code usually shares the source location of the original
7407 -- construct it was generated for. This however may not necessarely
7408 -- reflect the true location of the code within the tree.
7409
7410 -- Before comparing the slocs of the two nodes, make sure that we are
7411 -- working with correct source locations. Assume that P1 is to the left
7412 -- of P2. If either one does not come from source, traverse the common
7413 -- list heading towards the other node and locate the first source
7414 -- statement.
7415
7416 -- P1 P2
7417 -- ----+===+===+--------------+===+===+----
7418 -- expanded code expanded code
7419
7420 if not Comes_From_Source (P1) then
7421 while Present (P1) loop
7422
7423 -- Neither P2 nor a source statement were located during the
7424 -- search. If we reach the end of the list, then P1 does not
7425 -- occur earlier than P2.
7426
7427 -- ---->
7428 -- start --- P2 ----- P1 --- end
7429
7430 if No (Next (P1)) then
7431 return False;
7432
7433 -- We encounter P2 while going to the right of the list. This
7434 -- means that P1 does indeed appear earlier.
7435
7436 -- ---->
7437 -- start --- P1 ===== P2 --- end
7438 -- expanded code in between
7439
7440 elsif P1 = P2 then
7441 return True;
7442
7443 -- No need to look any further since we have located a source
7444 -- statement.
7445
7446 elsif Comes_From_Source (P1) then
7447 exit;
7448 end if;
7449
7450 -- Keep going right
7451
7452 Next (P1);
7453 end loop;
7454 end if;
7455
7456 if not Comes_From_Source (P2) then
7457 while Present (P2) loop
7458
7459 -- Neither P1 nor a source statement were located during the
7460 -- search. If we reach the start of the list, then P1 does not
7461 -- occur earlier than P2.
7462
7463 -- <----
7464 -- start --- P2 --- P1 --- end
7465
7466 if No (Prev (P2)) then
7467 return False;
7468
7469 -- We encounter P1 while going to the left of the list. This
7470 -- means that P1 does indeed appear earlier.
7471
7472 -- <----
7473 -- start --- P1 ===== P2 --- end
7474 -- expanded code in between
7475
7476 elsif P2 = P1 then
7477 return True;
7478
7479 -- No need to look any further since we have located a source
7480 -- statement.
7481
7482 elsif Comes_From_Source (P2) then
7483 exit;
7484 end if;
7485
7486 -- Keep going left
7487
7488 Prev (P2);
7489 end loop;
7490 end if;
7491
7492 -- At this point either both nodes came from source or we approximated
7493 -- their source locations through neighbouring source statements.
7494
7495 T1 := Top_Level_Location (Sloc (P1));
7496 T2 := Top_Level_Location (Sloc (P2));
7497
7498 -- When two nodes come from the same instance, they have identical top
7499 -- level locations. To determine proper relation within the tree, check
7500 -- their locations within the template.
7501
7502 if T1 = T2 then
7503 return Sloc (P1) < Sloc (P2);
7504
7505 -- The two nodes either come from unrelated instances or do not come
7506 -- from instantiated code at all.
7507
7508 else
7509 return T1 < T2;
7510 end if;
7511 end Earlier;
7512
7513 ----------------------
7514 -- Find_Actual_Type --
7515 ----------------------
7516
7517 function Find_Actual_Type
7518 (Typ : Entity_Id;
7519 Gen_Type : Entity_Id) return Entity_Id
7520 is
7521 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7522 T : Entity_Id;
7523
7524 begin
7525 -- Special processing only applies to child units
7526
7527 if not Is_Child_Unit (Gen_Scope) then
7528 return Get_Instance_Of (Typ);
7529
7530 -- If designated or component type is itself a formal of the child unit,
7531 -- its instance is available.
7532
7533 elsif Scope (Typ) = Gen_Scope then
7534 return Get_Instance_Of (Typ);
7535
7536 -- If the array or access type is not declared in the parent unit,
7537 -- no special processing needed.
7538
7539 elsif not Is_Generic_Type (Typ)
7540 and then Scope (Gen_Scope) /= Scope (Typ)
7541 then
7542 return Get_Instance_Of (Typ);
7543
7544 -- Otherwise, retrieve designated or component type by visibility
7545
7546 else
7547 T := Current_Entity (Typ);
7548 while Present (T) loop
7549 if In_Open_Scopes (Scope (T)) then
7550 return T;
7551
7552 elsif Is_Generic_Actual_Type (T) then
7553 return T;
7554 end if;
7555
7556 T := Homonym (T);
7557 end loop;
7558
7559 return Typ;
7560 end if;
7561 end Find_Actual_Type;
7562
7563 ----------------------------
7564 -- Freeze_Subprogram_Body --
7565 ----------------------------
7566
7567 procedure Freeze_Subprogram_Body
7568 (Inst_Node : Node_Id;
7569 Gen_Body : Node_Id;
7570 Pack_Id : Entity_Id)
7571 is
7572 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7573 Par : constant Entity_Id := Scope (Gen_Unit);
7574 E_G_Id : Entity_Id;
7575 Enc_G : Entity_Id;
7576 Enc_I : Node_Id;
7577 F_Node : Node_Id;
7578
7579 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7580 -- Find innermost package body that encloses the given node, and which
7581 -- is not a compilation unit. Freeze nodes for the instance, or for its
7582 -- enclosing body, may be inserted after the enclosing_body of the
7583 -- generic unit. Used to determine proper placement of freeze node for
7584 -- both package and subprogram instances.
7585
7586 function Package_Freeze_Node (B : Node_Id) return Node_Id;
7587 -- Find entity for given package body, and locate or create a freeze
7588 -- node for it.
7589
7590 ----------------------------
7591 -- Enclosing_Package_Body --
7592 ----------------------------
7593
7594 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
7595 P : Node_Id;
7596
7597 begin
7598 P := Parent (N);
7599 while Present (P)
7600 and then Nkind (Parent (P)) /= N_Compilation_Unit
7601 loop
7602 if Nkind (P) = N_Package_Body then
7603 if Nkind (Parent (P)) = N_Subunit then
7604 return Corresponding_Stub (Parent (P));
7605 else
7606 return P;
7607 end if;
7608 end if;
7609
7610 P := True_Parent (P);
7611 end loop;
7612
7613 return Empty;
7614 end Enclosing_Package_Body;
7615
7616 -------------------------
7617 -- Package_Freeze_Node --
7618 -------------------------
7619
7620 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7621 Id : Entity_Id;
7622
7623 begin
7624 if Nkind (B) = N_Package_Body then
7625 Id := Corresponding_Spec (B);
7626 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7627 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7628 end if;
7629
7630 Ensure_Freeze_Node (Id);
7631 return Freeze_Node (Id);
7632 end Package_Freeze_Node;
7633
7634 -- Start of processing of Freeze_Subprogram_Body
7635
7636 begin
7637 -- If the instance and the generic body appear within the same unit, and
7638 -- the instance precedes the generic, the freeze node for the instance
7639 -- must appear after that of the generic. If the generic is nested
7640 -- within another instance I2, then current instance must be frozen
7641 -- after I2. In both cases, the freeze nodes are those of enclosing
7642 -- packages. Otherwise, the freeze node is placed at the end of the
7643 -- current declarative part.
7644
7645 Enc_G := Enclosing_Package_Body (Gen_Body);
7646 Enc_I := Enclosing_Package_Body (Inst_Node);
7647 Ensure_Freeze_Node (Pack_Id);
7648 F_Node := Freeze_Node (Pack_Id);
7649
7650 if Is_Generic_Instance (Par)
7651 and then Present (Freeze_Node (Par))
7652 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7653 then
7654 -- The parent was a premature instantiation. Insert freeze node at
7655 -- the end the current declarative part.
7656
7657 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7658 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7659
7660 -- Handle the following case:
7661 --
7662 -- package Parent_Inst is new ...
7663 -- Parent_Inst []
7664 --
7665 -- procedure P ... -- this body freezes Parent_Inst
7666 --
7667 -- package Inst is new ...
7668 --
7669 -- In this particular scenario, the freeze node for Inst must be
7670 -- inserted in the same manner as that of Parent_Inst - before the
7671 -- next source body or at the end of the declarative list (body not
7672 -- available). If body P did not exist and Parent_Inst was frozen
7673 -- after Inst, either by a body following Inst or at the end of the
7674 -- declarative region, the freeze node for Inst must be inserted
7675 -- after that of Parent_Inst. This relation is established by
7676 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7677
7678 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7679 List_Containing (Inst_Node)
7680 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7681 then
7682 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7683
7684 else
7685 Insert_After (Freeze_Node (Par), F_Node);
7686 end if;
7687
7688 -- The body enclosing the instance should be frozen after the body that
7689 -- includes the generic, because the body of the instance may make
7690 -- references to entities therein. If the two are not in the same
7691 -- declarative part, or if the one enclosing the instance is frozen
7692 -- already, freeze the instance at the end of the current declarative
7693 -- part.
7694
7695 elsif Is_Generic_Instance (Par)
7696 and then Present (Freeze_Node (Par))
7697 and then Present (Enc_I)
7698 then
7699 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7700 or else
7701 (Nkind (Enc_I) = N_Package_Body
7702 and then
7703 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7704 then
7705 -- The enclosing package may contain several instances. Rather
7706 -- than computing the earliest point at which to insert its freeze
7707 -- node, we place it at the end of the declarative part of the
7708 -- parent of the generic.
7709
7710 Insert_Freeze_Node_For_Instance
7711 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7712 end if;
7713
7714 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7715
7716 elsif Present (Enc_G)
7717 and then Present (Enc_I)
7718 and then Enc_G /= Enc_I
7719 and then Earlier (Inst_Node, Gen_Body)
7720 then
7721 if Nkind (Enc_G) = N_Package_Body then
7722 E_G_Id := Corresponding_Spec (Enc_G);
7723 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7724 E_G_Id :=
7725 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7726 end if;
7727
7728 -- Freeze package that encloses instance, and place node after the
7729 -- package that encloses generic. If enclosing package is already
7730 -- frozen we have to assume it is at the proper place. This may be a
7731 -- potential ABE that requires dynamic checking. Do not add a freeze
7732 -- node if the package that encloses the generic is inside the body
7733 -- that encloses the instance, because the freeze node would be in
7734 -- the wrong scope. Additional contortions needed if the bodies are
7735 -- within a subunit.
7736
7737 declare
7738 Enclosing_Body : Node_Id;
7739
7740 begin
7741 if Nkind (Enc_I) = N_Package_Body_Stub then
7742 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7743 else
7744 Enclosing_Body := Enc_I;
7745 end if;
7746
7747 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7748 Insert_Freeze_Node_For_Instance
7749 (Enc_G, Package_Freeze_Node (Enc_I));
7750 end if;
7751 end;
7752
7753 -- Freeze enclosing subunit before instance
7754
7755 Ensure_Freeze_Node (E_G_Id);
7756
7757 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7758 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7759 end if;
7760
7761 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7762
7763 else
7764 -- If none of the above, insert freeze node at the end of the current
7765 -- declarative part.
7766
7767 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7768 end if;
7769 end Freeze_Subprogram_Body;
7770
7771 ----------------
7772 -- Get_Gen_Id --
7773 ----------------
7774
7775 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7776 begin
7777 return Generic_Renamings.Table (E).Gen_Id;
7778 end Get_Gen_Id;
7779
7780 ---------------------
7781 -- Get_Instance_Of --
7782 ---------------------
7783
7784 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7785 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7786
7787 begin
7788 if Res /= Assoc_Null then
7789 return Generic_Renamings.Table (Res).Act_Id;
7790 else
7791 -- On exit, entity is not instantiated: not a generic parameter, or
7792 -- else parameter of an inner generic unit.
7793
7794 return A;
7795 end if;
7796 end Get_Instance_Of;
7797
7798 ------------------------------------
7799 -- Get_Package_Instantiation_Node --
7800 ------------------------------------
7801
7802 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7803 Decl : Node_Id := Unit_Declaration_Node (A);
7804 Inst : Node_Id;
7805
7806 begin
7807 -- If the Package_Instantiation attribute has been set on the package
7808 -- entity, then use it directly when it (or its Original_Node) refers
7809 -- to an N_Package_Instantiation node. In principle it should be
7810 -- possible to have this field set in all cases, which should be
7811 -- investigated, and would allow this function to be significantly
7812 -- simplified. ???
7813
7814 Inst := Package_Instantiation (A);
7815
7816 if Present (Inst) then
7817 if Nkind (Inst) = N_Package_Instantiation then
7818 return Inst;
7819
7820 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7821 return Original_Node (Inst);
7822 end if;
7823 end if;
7824
7825 -- If the instantiation is a compilation unit that does not need body
7826 -- then the instantiation node has been rewritten as a package
7827 -- declaration for the instance, and we return the original node.
7828
7829 -- If it is a compilation unit and the instance node has not been
7830 -- rewritten, then it is still the unit of the compilation. Finally, if
7831 -- a body is present, this is a parent of the main unit whose body has
7832 -- been compiled for inlining purposes, and the instantiation node has
7833 -- been rewritten with the instance body.
7834
7835 -- Otherwise the instantiation node appears after the declaration. If
7836 -- the entity is a formal package, the declaration may have been
7837 -- rewritten as a generic declaration (in the case of a formal with box)
7838 -- or left as a formal package declaration if it has actuals, and is
7839 -- found with a forward search.
7840
7841 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7842 if Nkind (Decl) = N_Package_Declaration
7843 and then Present (Corresponding_Body (Decl))
7844 then
7845 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7846 end if;
7847
7848 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
7849 return Original_Node (Decl);
7850 else
7851 return Unit (Parent (Decl));
7852 end if;
7853
7854 elsif Nkind (Decl) = N_Package_Declaration
7855 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
7856 then
7857 return Original_Node (Decl);
7858
7859 else
7860 Inst := Next (Decl);
7861 while not Nkind_In (Inst, N_Package_Instantiation,
7862 N_Formal_Package_Declaration)
7863 loop
7864 Next (Inst);
7865 end loop;
7866
7867 return Inst;
7868 end if;
7869 end Get_Package_Instantiation_Node;
7870
7871 ------------------------
7872 -- Has_Been_Exchanged --
7873 ------------------------
7874
7875 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
7876 Next : Elmt_Id;
7877
7878 begin
7879 Next := First_Elmt (Exchanged_Views);
7880 while Present (Next) loop
7881 if Full_View (Node (Next)) = E then
7882 return True;
7883 end if;
7884
7885 Next_Elmt (Next);
7886 end loop;
7887
7888 return False;
7889 end Has_Been_Exchanged;
7890
7891 ----------
7892 -- Hash --
7893 ----------
7894
7895 function Hash (F : Entity_Id) return HTable_Range is
7896 begin
7897 return HTable_Range (F mod HTable_Size);
7898 end Hash;
7899
7900 ------------------------
7901 -- Hide_Current_Scope --
7902 ------------------------
7903
7904 procedure Hide_Current_Scope is
7905 C : constant Entity_Id := Current_Scope;
7906 E : Entity_Id;
7907
7908 begin
7909 Set_Is_Hidden_Open_Scope (C);
7910
7911 E := First_Entity (C);
7912 while Present (E) loop
7913 if Is_Immediately_Visible (E) then
7914 Set_Is_Immediately_Visible (E, False);
7915 Append_Elmt (E, Hidden_Entities);
7916 end if;
7917
7918 Next_Entity (E);
7919 end loop;
7920
7921 -- Make the scope name invisible as well. This is necessary, but might
7922 -- conflict with calls to Rtsfind later on, in case the scope is a
7923 -- predefined one. There is no clean solution to this problem, so for
7924 -- now we depend on the user not redefining Standard itself in one of
7925 -- the parent units.
7926
7927 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
7928 Set_Is_Immediately_Visible (C, False);
7929 Append_Elmt (C, Hidden_Entities);
7930 end if;
7931
7932 end Hide_Current_Scope;
7933
7934 --------------
7935 -- Init_Env --
7936 --------------
7937
7938 procedure Init_Env is
7939 Saved : Instance_Env;
7940
7941 begin
7942 Saved.Instantiated_Parent := Current_Instantiated_Parent;
7943 Saved.Exchanged_Views := Exchanged_Views;
7944 Saved.Hidden_Entities := Hidden_Entities;
7945 Saved.Current_Sem_Unit := Current_Sem_Unit;
7946 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
7947 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
7948
7949 -- Save configuration switches. These may be reset if the unit is a
7950 -- predefined unit, and the current mode is not Ada 2005.
7951
7952 Save_Opt_Config_Switches (Saved.Switches);
7953
7954 Instance_Envs.Append (Saved);
7955
7956 Exchanged_Views := New_Elmt_List;
7957 Hidden_Entities := New_Elmt_List;
7958
7959 -- Make dummy entry for Instantiated parent. If generic unit is legal,
7960 -- this is set properly in Set_Instance_Env.
7961
7962 Current_Instantiated_Parent :=
7963 (Current_Scope, Current_Scope, Assoc_Null);
7964 end Init_Env;
7965
7966 ------------------------------
7967 -- In_Same_Declarative_Part --
7968 ------------------------------
7969
7970 function In_Same_Declarative_Part
7971 (F_Node : Node_Id;
7972 Inst : Node_Id) return Boolean
7973 is
7974 Decls : constant Node_Id := Parent (F_Node);
7975 Nod : Node_Id := Parent (Inst);
7976
7977 begin
7978 while Present (Nod) loop
7979 if Nod = Decls then
7980 return True;
7981
7982 elsif Nkind_In (Nod, N_Subprogram_Body,
7983 N_Package_Body,
7984 N_Package_Declaration,
7985 N_Task_Body,
7986 N_Protected_Body,
7987 N_Block_Statement)
7988 then
7989 return False;
7990
7991 elsif Nkind (Nod) = N_Subunit then
7992 Nod := Corresponding_Stub (Nod);
7993
7994 elsif Nkind (Nod) = N_Compilation_Unit then
7995 return False;
7996
7997 else
7998 Nod := Parent (Nod);
7999 end if;
8000 end loop;
8001
8002 return False;
8003 end In_Same_Declarative_Part;
8004
8005 ---------------------
8006 -- In_Main_Context --
8007 ---------------------
8008
8009 function In_Main_Context (E : Entity_Id) return Boolean is
8010 Context : List_Id;
8011 Clause : Node_Id;
8012 Nam : Node_Id;
8013
8014 begin
8015 if not Is_Compilation_Unit (E)
8016 or else Ekind (E) /= E_Package
8017 or else In_Private_Part (E)
8018 then
8019 return False;
8020 end if;
8021
8022 Context := Context_Items (Cunit (Main_Unit));
8023
8024 Clause := First (Context);
8025 while Present (Clause) loop
8026 if Nkind (Clause) = N_With_Clause then
8027 Nam := Name (Clause);
8028
8029 -- If the current scope is part of the context of the main unit,
8030 -- analysis of the corresponding with_clause is not complete, and
8031 -- the entity is not set. We use the Chars field directly, which
8032 -- might produce false positives in rare cases, but guarantees
8033 -- that we produce all the instance bodies we will need.
8034
8035 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8036 or else (Nkind (Nam) = N_Selected_Component
8037 and then Chars (Selector_Name (Nam)) = Chars (E))
8038 then
8039 return True;
8040 end if;
8041 end if;
8042
8043 Next (Clause);
8044 end loop;
8045
8046 return False;
8047 end In_Main_Context;
8048
8049 ---------------------
8050 -- Inherit_Context --
8051 ---------------------
8052
8053 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8054 Current_Context : List_Id;
8055 Current_Unit : Node_Id;
8056 Item : Node_Id;
8057 New_I : Node_Id;
8058
8059 Clause : Node_Id;
8060 OK : Boolean;
8061 Lib_Unit : Node_Id;
8062
8063 begin
8064 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8065
8066 -- The inherited context is attached to the enclosing compilation
8067 -- unit. This is either the main unit, or the declaration for the
8068 -- main unit (in case the instantiation appears within the package
8069 -- declaration and the main unit is its body).
8070
8071 Current_Unit := Parent (Inst);
8072 while Present (Current_Unit)
8073 and then Nkind (Current_Unit) /= N_Compilation_Unit
8074 loop
8075 Current_Unit := Parent (Current_Unit);
8076 end loop;
8077
8078 Current_Context := Context_Items (Current_Unit);
8079
8080 Item := First (Context_Items (Parent (Gen_Decl)));
8081 while Present (Item) loop
8082 if Nkind (Item) = N_With_Clause then
8083 Lib_Unit := Library_Unit (Item);
8084
8085 -- Take care to prevent direct cyclic with's
8086
8087 if Lib_Unit /= Current_Unit then
8088
8089 -- Do not add a unit if it is already in the context
8090
8091 Clause := First (Current_Context);
8092 OK := True;
8093 while Present (Clause) loop
8094 if Nkind (Clause) = N_With_Clause and then
8095 Library_Unit (Clause) = Lib_Unit
8096 then
8097 OK := False;
8098 exit;
8099 end if;
8100
8101 Next (Clause);
8102 end loop;
8103
8104 if OK then
8105 New_I := New_Copy (Item);
8106 Set_Implicit_With (New_I, True);
8107 Set_Implicit_With_From_Instantiation (New_I, True);
8108 Append (New_I, Current_Context);
8109 end if;
8110 end if;
8111 end if;
8112
8113 Next (Item);
8114 end loop;
8115 end if;
8116 end Inherit_Context;
8117
8118 ----------------
8119 -- Initialize --
8120 ----------------
8121
8122 procedure Initialize is
8123 begin
8124 Generic_Renamings.Init;
8125 Instance_Envs.Init;
8126 Generic_Flags.Init;
8127 Generic_Renamings_HTable.Reset;
8128 Circularity_Detected := False;
8129 Exchanged_Views := No_Elist;
8130 Hidden_Entities := No_Elist;
8131 end Initialize;
8132
8133 -------------------------------------
8134 -- Insert_Freeze_Node_For_Instance --
8135 -------------------------------------
8136
8137 procedure Insert_Freeze_Node_For_Instance
8138 (N : Node_Id;
8139 F_Node : Node_Id)
8140 is
8141 Decl : Node_Id;
8142 Decls : List_Id;
8143 Inst : Entity_Id;
8144 Par_N : Node_Id;
8145
8146 function Enclosing_Body (N : Node_Id) return Node_Id;
8147 -- Find enclosing package or subprogram body, if any. Freeze node may
8148 -- be placed at end of current declarative list if previous instance
8149 -- and current one have different enclosing bodies.
8150
8151 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8152 -- Find the local instance, if any, that declares the generic that is
8153 -- being instantiated. If present, the freeze node for this instance
8154 -- must follow the freeze node for the previous instance.
8155
8156 --------------------
8157 -- Enclosing_Body --
8158 --------------------
8159
8160 function Enclosing_Body (N : Node_Id) return Node_Id is
8161 P : Node_Id;
8162
8163 begin
8164 P := Parent (N);
8165 while Present (P)
8166 and then Nkind (Parent (P)) /= N_Compilation_Unit
8167 loop
8168 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8169 if Nkind (Parent (P)) = N_Subunit then
8170 return Corresponding_Stub (Parent (P));
8171 else
8172 return P;
8173 end if;
8174 end if;
8175
8176 P := True_Parent (P);
8177 end loop;
8178
8179 return Empty;
8180 end Enclosing_Body;
8181
8182 -----------------------
8183 -- Previous_Instance --
8184 -----------------------
8185
8186 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8187 S : Entity_Id;
8188
8189 begin
8190 S := Scope (Gen);
8191 while Present (S)
8192 and then S /= Standard_Standard
8193 loop
8194 if Is_Generic_Instance (S)
8195 and then In_Same_Source_Unit (S, N)
8196 then
8197 return S;
8198 end if;
8199
8200 S := Scope (S);
8201 end loop;
8202
8203 return Empty;
8204 end Previous_Instance;
8205
8206 -- Start of processing for Insert_Freeze_Node_For_Instance
8207
8208 begin
8209 if not Is_List_Member (F_Node) then
8210 Decl := N;
8211 Decls := List_Containing (N);
8212 Inst := Entity (F_Node);
8213 Par_N := Parent (Decls);
8214
8215 -- When processing a subprogram instantiation, utilize the actual
8216 -- subprogram instantiation rather than its package wrapper as it
8217 -- carries all the context information.
8218
8219 if Is_Wrapper_Package (Inst) then
8220 Inst := Related_Instance (Inst);
8221 end if;
8222
8223 -- If this is a package instance, check whether the generic is
8224 -- declared in a previous instance and the current instance is
8225 -- not within the previous one.
8226
8227 if Present (Generic_Parent (Parent (Inst)))
8228 and then Is_In_Main_Unit (N)
8229 then
8230 declare
8231 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8232 Par_I : constant Entity_Id :=
8233 Previous_Instance
8234 (Generic_Parent (Parent (Inst)));
8235 Scop : Entity_Id;
8236
8237 begin
8238 if Present (Par_I)
8239 and then Earlier (N, Freeze_Node (Par_I))
8240 then
8241 Scop := Scope (Inst);
8242
8243 -- If the current instance is within the one that contains
8244 -- the generic, the freeze node for the current one must
8245 -- appear in the current declarative part. Ditto, if the
8246 -- current instance is within another package instance or
8247 -- within a body that does not enclose the current instance.
8248 -- In these three cases the freeze node of the previous
8249 -- instance is not relevant.
8250
8251 while Present (Scop)
8252 and then Scop /= Standard_Standard
8253 loop
8254 exit when Scop = Par_I
8255 or else
8256 (Is_Generic_Instance (Scop)
8257 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8258 Scop := Scope (Scop);
8259 end loop;
8260
8261 -- Previous instance encloses current instance
8262
8263 if Scop = Par_I then
8264 null;
8265
8266 -- If the next node is a source body we must freeze in
8267 -- the current scope as well.
8268
8269 elsif Present (Next (N))
8270 and then Nkind_In (Next (N),
8271 N_Subprogram_Body, N_Package_Body)
8272 and then Comes_From_Source (Next (N))
8273 then
8274 null;
8275
8276 -- Current instance is within an unrelated instance
8277
8278 elsif Is_Generic_Instance (Scop) then
8279 null;
8280
8281 -- Current instance is within an unrelated body
8282
8283 elsif Present (Enclosing_N)
8284 and then Enclosing_N /= Enclosing_Body (Par_I)
8285 then
8286 null;
8287
8288 else
8289 Insert_After (Freeze_Node (Par_I), F_Node);
8290 return;
8291 end if;
8292 end if;
8293 end;
8294 end if;
8295
8296 -- When the instantiation occurs in a package declaration, append the
8297 -- freeze node to the private declarations (if any).
8298
8299 if Nkind (Par_N) = N_Package_Specification
8300 and then Decls = Visible_Declarations (Par_N)
8301 and then Present (Private_Declarations (Par_N))
8302 and then not Is_Empty_List (Private_Declarations (Par_N))
8303 then
8304 Decls := Private_Declarations (Par_N);
8305 Decl := First (Decls);
8306 end if;
8307
8308 -- Determine the proper freeze point of a package instantiation. We
8309 -- adhere to the general rule of a package or subprogram body causing
8310 -- freezing of anything before it in the same declarative region. In
8311 -- this case, the proper freeze point of a package instantiation is
8312 -- before the first source body which follows, or before a stub. This
8313 -- ensures that entities coming from the instance are already frozen
8314 -- and usable in source bodies.
8315
8316 if Nkind (Par_N) /= N_Package_Declaration
8317 and then Ekind (Inst) = E_Package
8318 and then Is_Generic_Instance (Inst)
8319 and then
8320 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8321 then
8322 while Present (Decl) loop
8323 if (Nkind (Decl) in N_Unit_Body
8324 or else
8325 Nkind (Decl) in N_Body_Stub)
8326 and then Comes_From_Source (Decl)
8327 then
8328 Insert_Before (Decl, F_Node);
8329 return;
8330 end if;
8331
8332 Next (Decl);
8333 end loop;
8334 end if;
8335
8336 -- In a package declaration, or if no previous body, insert at end
8337 -- of list.
8338
8339 Set_Sloc (F_Node, Sloc (Last (Decls)));
8340 Insert_After (Last (Decls), F_Node);
8341 end if;
8342 end Insert_Freeze_Node_For_Instance;
8343
8344 ------------------
8345 -- Install_Body --
8346 ------------------
8347
8348 procedure Install_Body
8349 (Act_Body : Node_Id;
8350 N : Node_Id;
8351 Gen_Body : Node_Id;
8352 Gen_Decl : Node_Id)
8353 is
8354 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8355 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8356 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8357 Par : constant Entity_Id := Scope (Gen_Id);
8358 Gen_Unit : constant Node_Id :=
8359 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8360 Orig_Body : Node_Id := Gen_Body;
8361 F_Node : Node_Id;
8362 Body_Unit : Node_Id;
8363
8364 Must_Delay : Boolean;
8365
8366 function In_Same_Enclosing_Subp return Boolean;
8367 -- Check whether instance and generic body are within same subprogram.
8368
8369 function True_Sloc (N : Node_Id) return Source_Ptr;
8370 -- If the instance is nested inside a generic unit, the Sloc of the
8371 -- instance indicates the place of the original definition, not the
8372 -- point of the current enclosing instance. Pending a better usage of
8373 -- Slocs to indicate instantiation places, we determine the place of
8374 -- origin of a node by finding the maximum sloc of any ancestor node.
8375 -- Why is this not equivalent to Top_Level_Location ???
8376
8377 ----------------------------
8378 -- In_Same_Enclosing_Subp --
8379 ----------------------------
8380
8381 function In_Same_Enclosing_Subp return Boolean is
8382 Scop : Entity_Id;
8383 Subp : Entity_Id;
8384
8385 begin
8386 Scop := Scope (Act_Id);
8387 while Scop /= Standard_Standard
8388 and then not Is_Overloadable (Scop)
8389 loop
8390 Scop := Scope (Scop);
8391 end loop;
8392
8393 if Scop = Standard_Standard then
8394 return False;
8395 else
8396 Subp := Scop;
8397 end if;
8398
8399 Scop := Scope (Gen_Id);
8400 while Scop /= Standard_Standard loop
8401 if Scop = Subp then
8402 return True;
8403 else
8404 Scop := Scope (Scop);
8405 end if;
8406 end loop;
8407
8408 return False;
8409 end In_Same_Enclosing_Subp;
8410
8411 ---------------
8412 -- True_Sloc --
8413 ---------------
8414
8415 function True_Sloc (N : Node_Id) return Source_Ptr is
8416 Res : Source_Ptr;
8417 N1 : Node_Id;
8418
8419 begin
8420 Res := Sloc (N);
8421 N1 := N;
8422 while Present (N1) and then N1 /= Act_Unit loop
8423 if Sloc (N1) > Res then
8424 Res := Sloc (N1);
8425 end if;
8426
8427 N1 := Parent (N1);
8428 end loop;
8429
8430 return Res;
8431 end True_Sloc;
8432
8433 -- Start of processing for Install_Body
8434
8435 begin
8436 -- If the body is a subunit, the freeze point is the corresponding stub
8437 -- in the current compilation, not the subunit itself.
8438
8439 if Nkind (Parent (Gen_Body)) = N_Subunit then
8440 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8441 else
8442 Orig_Body := Gen_Body;
8443 end if;
8444
8445 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8446
8447 -- If the instantiation and the generic definition appear in the same
8448 -- package declaration, this is an early instantiation. If they appear
8449 -- in the same declarative part, it is an early instantiation only if
8450 -- the generic body appears textually later, and the generic body is
8451 -- also in the main unit.
8452
8453 -- If instance is nested within a subprogram, and the generic body
8454 -- is not, the instance is delayed because the enclosing body is. If
8455 -- instance and body are within the same scope, or the same subprogram
8456 -- body, indicate explicitly that the instance is delayed.
8457
8458 Must_Delay :=
8459 (Gen_Unit = Act_Unit
8460 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8461 N_Generic_Package_Declaration)
8462 or else (Gen_Unit = Body_Unit
8463 and then True_Sloc (N) < Sloc (Orig_Body)))
8464 and then Is_In_Main_Unit (Gen_Unit)
8465 and then (Scope (Act_Id) = Scope (Gen_Id)
8466 or else In_Same_Enclosing_Subp));
8467
8468 -- If this is an early instantiation, the freeze node is placed after
8469 -- the generic body. Otherwise, if the generic appears in an instance,
8470 -- we cannot freeze the current instance until the outer one is frozen.
8471 -- This is only relevant if the current instance is nested within some
8472 -- inner scope not itself within the outer instance. If this scope is
8473 -- a package body in the same declarative part as the outer instance,
8474 -- then that body needs to be frozen after the outer instance. Finally,
8475 -- if no delay is needed, we place the freeze node at the end of the
8476 -- current declarative part.
8477
8478 if Expander_Active then
8479 Ensure_Freeze_Node (Act_Id);
8480 F_Node := Freeze_Node (Act_Id);
8481
8482 if Must_Delay then
8483 Insert_After (Orig_Body, F_Node);
8484
8485 elsif Is_Generic_Instance (Par)
8486 and then Present (Freeze_Node (Par))
8487 and then Scope (Act_Id) /= Par
8488 then
8489 -- Freeze instance of inner generic after instance of enclosing
8490 -- generic.
8491
8492 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8493
8494 -- Handle the following case:
8495
8496 -- package Parent_Inst is new ...
8497 -- Parent_Inst []
8498
8499 -- procedure P ... -- this body freezes Parent_Inst
8500
8501 -- package Inst is new ...
8502
8503 -- In this particular scenario, the freeze node for Inst must
8504 -- be inserted in the same manner as that of Parent_Inst,
8505 -- before the next source body or at the end of the declarative
8506 -- list (body not available). If body P did not exist and
8507 -- Parent_Inst was frozen after Inst, either by a body
8508 -- following Inst or at the end of the declarative region,
8509 -- the freeze node for Inst must be inserted after that of
8510 -- Parent_Inst. This relation is established by comparing
8511 -- the Slocs of Parent_Inst freeze node and Inst.
8512
8513 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8514 List_Containing (N)
8515 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8516 then
8517 Insert_Freeze_Node_For_Instance (N, F_Node);
8518 else
8519 Insert_After (Freeze_Node (Par), F_Node);
8520 end if;
8521
8522 -- Freeze package enclosing instance of inner generic after
8523 -- instance of enclosing generic.
8524
8525 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8526 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8527 then
8528 declare
8529 Enclosing : Entity_Id;
8530
8531 begin
8532 Enclosing := Corresponding_Spec (Parent (N));
8533
8534 if No (Enclosing) then
8535 Enclosing := Defining_Entity (Parent (N));
8536 end if;
8537
8538 Insert_Freeze_Node_For_Instance (N, F_Node);
8539 Ensure_Freeze_Node (Enclosing);
8540
8541 if not Is_List_Member (Freeze_Node (Enclosing)) then
8542
8543 -- The enclosing context is a subunit, insert the freeze
8544 -- node after the stub.
8545
8546 if Nkind (Parent (Parent (N))) = N_Subunit then
8547 Insert_Freeze_Node_For_Instance
8548 (Corresponding_Stub (Parent (Parent (N))),
8549 Freeze_Node (Enclosing));
8550
8551 -- The enclosing context is a package with a stub body
8552 -- which has already been replaced by the real body.
8553 -- Insert the freeze node after the actual body.
8554
8555 elsif Ekind (Enclosing) = E_Package
8556 and then Present (Body_Entity (Enclosing))
8557 and then Was_Originally_Stub
8558 (Parent (Body_Entity (Enclosing)))
8559 then
8560 Insert_Freeze_Node_For_Instance
8561 (Parent (Body_Entity (Enclosing)),
8562 Freeze_Node (Enclosing));
8563
8564 -- The parent instance has been frozen before the body of
8565 -- the enclosing package, insert the freeze node after
8566 -- the body.
8567
8568 elsif List_Containing (Freeze_Node (Par)) =
8569 List_Containing (Parent (N))
8570 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8571 then
8572 Insert_Freeze_Node_For_Instance
8573 (Parent (N), Freeze_Node (Enclosing));
8574
8575 else
8576 Insert_After
8577 (Freeze_Node (Par), Freeze_Node (Enclosing));
8578 end if;
8579 end if;
8580 end;
8581
8582 else
8583 Insert_Freeze_Node_For_Instance (N, F_Node);
8584 end if;
8585
8586 else
8587 Insert_Freeze_Node_For_Instance (N, F_Node);
8588 end if;
8589 end if;
8590
8591 Set_Is_Frozen (Act_Id);
8592 Insert_Before (N, Act_Body);
8593 Mark_Rewrite_Insertion (Act_Body);
8594 end Install_Body;
8595
8596 -----------------------------
8597 -- Install_Formal_Packages --
8598 -----------------------------
8599
8600 procedure Install_Formal_Packages (Par : Entity_Id) is
8601 E : Entity_Id;
8602 Gen : Entity_Id;
8603 Gen_E : Entity_Id := Empty;
8604
8605 begin
8606 E := First_Entity (Par);
8607
8608 -- If we are installing an instance parent, locate the formal packages
8609 -- of its generic parent.
8610
8611 if Is_Generic_Instance (Par) then
8612 Gen := Generic_Parent (Package_Specification (Par));
8613 Gen_E := First_Entity (Gen);
8614 end if;
8615
8616 while Present (E) loop
8617 if Ekind (E) = E_Package
8618 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
8619 then
8620 -- If this is the renaming for the parent instance, done
8621
8622 if Renamed_Object (E) = Par then
8623 exit;
8624
8625 -- The visibility of a formal of an enclosing generic is already
8626 -- correct.
8627
8628 elsif Denotes_Formal_Package (E) then
8629 null;
8630
8631 elsif Present (Associated_Formal_Package (E)) then
8632 Check_Generic_Actuals (Renamed_Object (E), True);
8633 Set_Is_Hidden (E, False);
8634
8635 -- Find formal package in generic unit that corresponds to
8636 -- (instance of) formal package in instance.
8637
8638 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
8639 Next_Entity (Gen_E);
8640 end loop;
8641
8642 if Present (Gen_E) then
8643 Map_Formal_Package_Entities (Gen_E, E);
8644 end if;
8645 end if;
8646 end if;
8647
8648 Next_Entity (E);
8649 if Present (Gen_E) then
8650 Next_Entity (Gen_E);
8651 end if;
8652 end loop;
8653 end Install_Formal_Packages;
8654
8655 --------------------
8656 -- Install_Parent --
8657 --------------------
8658
8659 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
8660 Ancestors : constant Elist_Id := New_Elmt_List;
8661 S : constant Entity_Id := Current_Scope;
8662 Inst_Par : Entity_Id;
8663 First_Par : Entity_Id;
8664 Inst_Node : Node_Id;
8665 Gen_Par : Entity_Id;
8666 First_Gen : Entity_Id;
8667 Elmt : Elmt_Id;
8668
8669 procedure Install_Noninstance_Specs (Par : Entity_Id);
8670 -- Install the scopes of noninstance parent units ending with Par
8671
8672 procedure Install_Spec (Par : Entity_Id);
8673 -- The child unit is within the declarative part of the parent, so the
8674 -- declarations within the parent are immediately visible.
8675
8676 -------------------------------
8677 -- Install_Noninstance_Specs --
8678 -------------------------------
8679
8680 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8681 begin
8682 if Present (Par)
8683 and then Par /= Standard_Standard
8684 and then not In_Open_Scopes (Par)
8685 then
8686 Install_Noninstance_Specs (Scope (Par));
8687 Install_Spec (Par);
8688 end if;
8689 end Install_Noninstance_Specs;
8690
8691 ------------------
8692 -- Install_Spec --
8693 ------------------
8694
8695 procedure Install_Spec (Par : Entity_Id) is
8696 Spec : constant Node_Id := Package_Specification (Par);
8697
8698 begin
8699 -- If this parent of the child instance is a top-level unit,
8700 -- then record the unit and its visibility for later resetting in
8701 -- Remove_Parent. We exclude units that are generic instances, as we
8702 -- only want to record this information for the ultimate top-level
8703 -- noninstance parent (is that always correct???).
8704
8705 if Scope (Par) = Standard_Standard
8706 and then not Is_Generic_Instance (Par)
8707 then
8708 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8709 Instance_Parent_Unit := Par;
8710 end if;
8711
8712 -- Open the parent scope and make it and its declarations visible.
8713 -- If this point is not within a body, then only the visible
8714 -- declarations should be made visible, and installation of the
8715 -- private declarations is deferred until the appropriate point
8716 -- within analysis of the spec being instantiated (see the handling
8717 -- of parent visibility in Analyze_Package_Specification). This is
8718 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8719 -- private view problems that occur when compiling instantiations of
8720 -- a generic child of that package (Generic_Dispatching_Constructor).
8721 -- If the instance freezes a tagged type, inlinings of operations
8722 -- from Ada.Tags may need the full view of type Tag. If inlining took
8723 -- proper account of establishing visibility of inlined subprograms'
8724 -- parents then it should be possible to remove this
8725 -- special check. ???
8726
8727 Push_Scope (Par);
8728 Set_Is_Immediately_Visible (Par);
8729 Install_Visible_Declarations (Par);
8730 Set_Use (Visible_Declarations (Spec));
8731
8732 if In_Body or else Is_RTU (Par, Ada_Tags) then
8733 Install_Private_Declarations (Par);
8734 Set_Use (Private_Declarations (Spec));
8735 end if;
8736 end Install_Spec;
8737
8738 -- Start of processing for Install_Parent
8739
8740 begin
8741 -- We need to install the parent instance to compile the instantiation
8742 -- of the child, but the child instance must appear in the current
8743 -- scope. Given that we cannot place the parent above the current scope
8744 -- in the scope stack, we duplicate the current scope and unstack both
8745 -- after the instantiation is complete.
8746
8747 -- If the parent is itself the instantiation of a child unit, we must
8748 -- also stack the instantiation of its parent, and so on. Each such
8749 -- ancestor is the prefix of the name in a prior instantiation.
8750
8751 -- If this is a nested instance, the parent unit itself resolves to
8752 -- a renaming of the parent instance, whose declaration we need.
8753
8754 -- Finally, the parent may be a generic (not an instance) when the
8755 -- child unit appears as a formal package.
8756
8757 Inst_Par := P;
8758
8759 if Present (Renamed_Entity (Inst_Par)) then
8760 Inst_Par := Renamed_Entity (Inst_Par);
8761 end if;
8762
8763 First_Par := Inst_Par;
8764
8765 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8766
8767 First_Gen := Gen_Par;
8768
8769 while Present (Gen_Par)
8770 and then Is_Child_Unit (Gen_Par)
8771 loop
8772 -- Load grandparent instance as well
8773
8774 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8775
8776 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8777 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8778
8779 if Present (Renamed_Entity (Inst_Par)) then
8780 Inst_Par := Renamed_Entity (Inst_Par);
8781 end if;
8782
8783 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8784
8785 if Present (Gen_Par) then
8786 Prepend_Elmt (Inst_Par, Ancestors);
8787
8788 else
8789 -- Parent is not the name of an instantiation
8790
8791 Install_Noninstance_Specs (Inst_Par);
8792 exit;
8793 end if;
8794
8795 else
8796 -- Previous error
8797
8798 exit;
8799 end if;
8800 end loop;
8801
8802 if Present (First_Gen) then
8803 Append_Elmt (First_Par, Ancestors);
8804 else
8805 Install_Noninstance_Specs (First_Par);
8806 end if;
8807
8808 if not Is_Empty_Elmt_List (Ancestors) then
8809 Elmt := First_Elmt (Ancestors);
8810 while Present (Elmt) loop
8811 Install_Spec (Node (Elmt));
8812 Install_Formal_Packages (Node (Elmt));
8813 Next_Elmt (Elmt);
8814 end loop;
8815 end if;
8816
8817 if not In_Body then
8818 Push_Scope (S);
8819 end if;
8820 end Install_Parent;
8821
8822 -------------------------------
8823 -- Install_Hidden_Primitives --
8824 -------------------------------
8825
8826 procedure Install_Hidden_Primitives
8827 (Prims_List : in out Elist_Id;
8828 Gen_T : Entity_Id;
8829 Act_T : Entity_Id)
8830 is
8831 Elmt : Elmt_Id;
8832 List : Elist_Id := No_Elist;
8833 Prim_G_Elmt : Elmt_Id;
8834 Prim_A_Elmt : Elmt_Id;
8835 Prim_G : Node_Id;
8836 Prim_A : Node_Id;
8837
8838 begin
8839 -- No action needed in case of serious errors because we cannot trust
8840 -- in the order of primitives
8841
8842 if Serious_Errors_Detected > 0 then
8843 return;
8844
8845 -- No action possible if we don't have available the list of primitive
8846 -- operations
8847
8848 elsif No (Gen_T)
8849 or else not Is_Record_Type (Gen_T)
8850 or else not Is_Tagged_Type (Gen_T)
8851 or else not Is_Record_Type (Act_T)
8852 or else not Is_Tagged_Type (Act_T)
8853 then
8854 return;
8855
8856 -- There is no need to handle interface types since their primitives
8857 -- cannot be hidden
8858
8859 elsif Is_Interface (Gen_T) then
8860 return;
8861 end if;
8862
8863 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
8864
8865 if not Is_Class_Wide_Type (Act_T) then
8866 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
8867 else
8868 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
8869 end if;
8870
8871 loop
8872 -- Skip predefined primitives in the generic formal
8873
8874 while Present (Prim_G_Elmt)
8875 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
8876 loop
8877 Next_Elmt (Prim_G_Elmt);
8878 end loop;
8879
8880 -- Skip predefined primitives in the generic actual
8881
8882 while Present (Prim_A_Elmt)
8883 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
8884 loop
8885 Next_Elmt (Prim_A_Elmt);
8886 end loop;
8887
8888 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
8889
8890 Prim_G := Node (Prim_G_Elmt);
8891 Prim_A := Node (Prim_A_Elmt);
8892
8893 -- There is no need to handle interface primitives because their
8894 -- primitives are not hidden
8895
8896 exit when Present (Interface_Alias (Prim_G));
8897
8898 -- Here we install one hidden primitive
8899
8900 if Chars (Prim_G) /= Chars (Prim_A)
8901 and then Has_Suffix (Prim_A, 'P')
8902 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
8903 then
8904 Set_Chars (Prim_A, Chars (Prim_G));
8905
8906 if List = No_Elist then
8907 List := New_Elmt_List;
8908 end if;
8909
8910 Append_Elmt (Prim_A, List);
8911 end if;
8912
8913 Next_Elmt (Prim_A_Elmt);
8914 Next_Elmt (Prim_G_Elmt);
8915 end loop;
8916
8917 -- Append the elements to the list of temporarily visible primitives
8918 -- avoiding duplicates.
8919
8920 if Present (List) then
8921 if No (Prims_List) then
8922 Prims_List := New_Elmt_List;
8923 end if;
8924
8925 Elmt := First_Elmt (List);
8926 while Present (Elmt) loop
8927 Append_Unique_Elmt (Node (Elmt), Prims_List);
8928 Next_Elmt (Elmt);
8929 end loop;
8930 end if;
8931 end Install_Hidden_Primitives;
8932
8933 -------------------------------
8934 -- Restore_Hidden_Primitives --
8935 -------------------------------
8936
8937 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
8938 Prim_Elmt : Elmt_Id;
8939 Prim : Node_Id;
8940
8941 begin
8942 if Prims_List /= No_Elist then
8943 Prim_Elmt := First_Elmt (Prims_List);
8944 while Present (Prim_Elmt) loop
8945 Prim := Node (Prim_Elmt);
8946 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
8947 Next_Elmt (Prim_Elmt);
8948 end loop;
8949
8950 Prims_List := No_Elist;
8951 end if;
8952 end Restore_Hidden_Primitives;
8953
8954 --------------------------------
8955 -- Instantiate_Formal_Package --
8956 --------------------------------
8957
8958 function Instantiate_Formal_Package
8959 (Formal : Node_Id;
8960 Actual : Node_Id;
8961 Analyzed_Formal : Node_Id) return List_Id
8962 is
8963 Loc : constant Source_Ptr := Sloc (Actual);
8964 Actual_Pack : Entity_Id;
8965 Formal_Pack : Entity_Id;
8966 Gen_Parent : Entity_Id;
8967 Decls : List_Id;
8968 Nod : Node_Id;
8969 Parent_Spec : Node_Id;
8970
8971 procedure Find_Matching_Actual
8972 (F : Node_Id;
8973 Act : in out Entity_Id);
8974 -- We need to associate each formal entity in the formal package with
8975 -- the corresponding entity in the actual package. The actual package
8976 -- has been analyzed and possibly expanded, and as a result there is
8977 -- no one-to-one correspondence between the two lists (for example,
8978 -- the actual may include subtypes, itypes, and inherited primitive
8979 -- operations, interspersed among the renaming declarations for the
8980 -- actuals) . We retrieve the corresponding actual by name because each
8981 -- actual has the same name as the formal, and they do appear in the
8982 -- same order.
8983
8984 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
8985 -- Retrieve entity of defining entity of generic formal parameter.
8986 -- Only the declarations of formals need to be considered when
8987 -- linking them to actuals, but the declarative list may include
8988 -- internal entities generated during analysis, and those are ignored.
8989
8990 procedure Match_Formal_Entity
8991 (Formal_Node : Node_Id;
8992 Formal_Ent : Entity_Id;
8993 Actual_Ent : Entity_Id);
8994 -- Associates the formal entity with the actual. In the case where
8995 -- Formal_Ent is a formal package, this procedure iterates through all
8996 -- of its formals and enters associations between the actuals occurring
8997 -- in the formal package's corresponding actual package (given by
8998 -- Actual_Ent) and the formal package's formal parameters. This
8999 -- procedure recurses if any of the parameters is itself a package.
9000
9001 function Is_Instance_Of
9002 (Act_Spec : Entity_Id;
9003 Gen_Anc : Entity_Id) return Boolean;
9004 -- The actual can be an instantiation of a generic within another
9005 -- instance, in which case there is no direct link from it to the
9006 -- original generic ancestor. In that case, we recognize that the
9007 -- ultimate ancestor is the same by examining names and scopes.
9008
9009 procedure Process_Nested_Formal (Formal : Entity_Id);
9010 -- If the current formal is declared with a box, its own formals are
9011 -- visible in the instance, as they were in the generic, and their
9012 -- Hidden flag must be reset. If some of these formals are themselves
9013 -- packages declared with a box, the processing must be recursive.
9014
9015 --------------------------
9016 -- Find_Matching_Actual --
9017 --------------------------
9018
9019 procedure Find_Matching_Actual
9020 (F : Node_Id;
9021 Act : in out Entity_Id)
9022 is
9023 Formal_Ent : Entity_Id;
9024
9025 begin
9026 case Nkind (Original_Node (F)) is
9027 when N_Formal_Object_Declaration |
9028 N_Formal_Type_Declaration =>
9029 Formal_Ent := Defining_Identifier (F);
9030
9031 while Chars (Act) /= Chars (Formal_Ent) loop
9032 Next_Entity (Act);
9033 end loop;
9034
9035 when N_Formal_Subprogram_Declaration |
9036 N_Formal_Package_Declaration |
9037 N_Package_Declaration |
9038 N_Generic_Package_Declaration =>
9039 Formal_Ent := Defining_Entity (F);
9040
9041 while Chars (Act) /= Chars (Formal_Ent) loop
9042 Next_Entity (Act);
9043 end loop;
9044
9045 when others =>
9046 raise Program_Error;
9047 end case;
9048 end Find_Matching_Actual;
9049
9050 -------------------------
9051 -- Match_Formal_Entity --
9052 -------------------------
9053
9054 procedure Match_Formal_Entity
9055 (Formal_Node : Node_Id;
9056 Formal_Ent : Entity_Id;
9057 Actual_Ent : Entity_Id)
9058 is
9059 Act_Pkg : Entity_Id;
9060
9061 begin
9062 Set_Instance_Of (Formal_Ent, Actual_Ent);
9063
9064 if Ekind (Actual_Ent) = E_Package then
9065
9066 -- Record associations for each parameter
9067
9068 Act_Pkg := Actual_Ent;
9069
9070 declare
9071 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9072 F_Ent : Entity_Id;
9073 F_Node : Node_Id;
9074
9075 Gen_Decl : Node_Id;
9076 Formals : List_Id;
9077 Actual : Entity_Id;
9078
9079 begin
9080 -- Retrieve the actual given in the formal package declaration
9081
9082 Actual := Entity (Name (Original_Node (Formal_Node)));
9083
9084 -- The actual in the formal package declaration may be a
9085 -- renamed generic package, in which case we want to retrieve
9086 -- the original generic in order to traverse its formal part.
9087
9088 if Present (Renamed_Entity (Actual)) then
9089 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9090 else
9091 Gen_Decl := Unit_Declaration_Node (Actual);
9092 end if;
9093
9094 Formals := Generic_Formal_Declarations (Gen_Decl);
9095
9096 if Present (Formals) then
9097 F_Node := First_Non_Pragma (Formals);
9098 else
9099 F_Node := Empty;
9100 end if;
9101
9102 while Present (A_Ent)
9103 and then Present (F_Node)
9104 and then A_Ent /= First_Private_Entity (Act_Pkg)
9105 loop
9106 F_Ent := Get_Formal_Entity (F_Node);
9107
9108 if Present (F_Ent) then
9109
9110 -- This is a formal of the original package. Record
9111 -- association and recurse.
9112
9113 Find_Matching_Actual (F_Node, A_Ent);
9114 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9115 Next_Entity (A_Ent);
9116 end if;
9117
9118 Next_Non_Pragma (F_Node);
9119 end loop;
9120 end;
9121 end if;
9122 end Match_Formal_Entity;
9123
9124 -----------------------
9125 -- Get_Formal_Entity --
9126 -----------------------
9127
9128 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9129 Kind : constant Node_Kind := Nkind (Original_Node (N));
9130 begin
9131 case Kind is
9132 when N_Formal_Object_Declaration =>
9133 return Defining_Identifier (N);
9134
9135 when N_Formal_Type_Declaration =>
9136 return Defining_Identifier (N);
9137
9138 when N_Formal_Subprogram_Declaration =>
9139 return Defining_Unit_Name (Specification (N));
9140
9141 when N_Formal_Package_Declaration =>
9142 return Defining_Identifier (Original_Node (N));
9143
9144 when N_Generic_Package_Declaration =>
9145 return Defining_Identifier (Original_Node (N));
9146
9147 -- All other declarations are introduced by semantic analysis and
9148 -- have no match in the actual.
9149
9150 when others =>
9151 return Empty;
9152 end case;
9153 end Get_Formal_Entity;
9154
9155 --------------------
9156 -- Is_Instance_Of --
9157 --------------------
9158
9159 function Is_Instance_Of
9160 (Act_Spec : Entity_Id;
9161 Gen_Anc : Entity_Id) return Boolean
9162 is
9163 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9164
9165 begin
9166 if No (Gen_Par) then
9167 return False;
9168
9169 -- Simplest case: the generic parent of the actual is the formal
9170
9171 elsif Gen_Par = Gen_Anc then
9172 return True;
9173
9174 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9175 return False;
9176
9177 -- The actual may be obtained through several instantiations. Its
9178 -- scope must itself be an instance of a generic declared in the
9179 -- same scope as the formal. Any other case is detected above.
9180
9181 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9182 return False;
9183
9184 else
9185 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9186 end if;
9187 end Is_Instance_Of;
9188
9189 ---------------------------
9190 -- Process_Nested_Formal --
9191 ---------------------------
9192
9193 procedure Process_Nested_Formal (Formal : Entity_Id) is
9194 Ent : Entity_Id;
9195
9196 begin
9197 if Present (Associated_Formal_Package (Formal))
9198 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9199 then
9200 Ent := First_Entity (Formal);
9201 while Present (Ent) loop
9202 Set_Is_Hidden (Ent, False);
9203 Set_Is_Visible_Formal (Ent);
9204 Set_Is_Potentially_Use_Visible
9205 (Ent, Is_Potentially_Use_Visible (Formal));
9206
9207 if Ekind (Ent) = E_Package then
9208 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9209 Process_Nested_Formal (Ent);
9210 end if;
9211
9212 Next_Entity (Ent);
9213 end loop;
9214 end if;
9215 end Process_Nested_Formal;
9216
9217 -- Start of processing for Instantiate_Formal_Package
9218
9219 begin
9220 Analyze (Actual);
9221
9222 if not Is_Entity_Name (Actual)
9223 or else Ekind (Entity (Actual)) /= E_Package
9224 then
9225 Error_Msg_N
9226 ("expect package instance to instantiate formal", Actual);
9227 Abandon_Instantiation (Actual);
9228 raise Program_Error;
9229
9230 else
9231 Actual_Pack := Entity (Actual);
9232 Set_Is_Instantiated (Actual_Pack);
9233
9234 -- The actual may be a renamed package, or an outer generic formal
9235 -- package whose instantiation is converted into a renaming.
9236
9237 if Present (Renamed_Object (Actual_Pack)) then
9238 Actual_Pack := Renamed_Object (Actual_Pack);
9239 end if;
9240
9241 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9242 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9243 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9244 else
9245 Gen_Parent :=
9246 Generic_Parent (Specification (Analyzed_Formal));
9247 Formal_Pack :=
9248 Defining_Unit_Name (Specification (Analyzed_Formal));
9249 end if;
9250
9251 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9252 Parent_Spec := Package_Specification (Actual_Pack);
9253 else
9254 Parent_Spec := Parent (Actual_Pack);
9255 end if;
9256
9257 if Gen_Parent = Any_Id then
9258 Error_Msg_N
9259 ("previous error in declaration of formal package", Actual);
9260 Abandon_Instantiation (Actual);
9261
9262 elsif
9263 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9264 then
9265 null;
9266
9267 else
9268 Error_Msg_NE
9269 ("actual parameter must be instance of&", Actual, Gen_Parent);
9270 Abandon_Instantiation (Actual);
9271 end if;
9272
9273 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9274 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9275
9276 Nod :=
9277 Make_Package_Renaming_Declaration (Loc,
9278 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9279 Name => New_Occurrence_Of (Actual_Pack, Loc));
9280
9281 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
9282 Defining_Identifier (Formal));
9283 Decls := New_List (Nod);
9284
9285 -- If the formal F has a box, then the generic declarations are
9286 -- visible in the generic G. In an instance of G, the corresponding
9287 -- entities in the actual for F (which are the actuals for the
9288 -- instantiation of the generic that F denotes) must also be made
9289 -- visible for analysis of the current instance. On exit from the
9290 -- current instance, those entities are made private again. If the
9291 -- actual is currently in use, these entities are also use-visible.
9292
9293 -- The loop through the actual entities also steps through the formal
9294 -- entities and enters associations from formals to actuals into the
9295 -- renaming map. This is necessary to properly handle checking of
9296 -- actual parameter associations for later formals that depend on
9297 -- actuals declared in the formal package.
9298
9299 -- In Ada 2005, partial parameterization requires that we make
9300 -- visible the actuals corresponding to formals that were defaulted
9301 -- in the formal package. There formals are identified because they
9302 -- remain formal generics within the formal package, rather than
9303 -- being renamings of the actuals supplied.
9304
9305 declare
9306 Gen_Decl : constant Node_Id :=
9307 Unit_Declaration_Node (Gen_Parent);
9308 Formals : constant List_Id :=
9309 Generic_Formal_Declarations (Gen_Decl);
9310
9311 Actual_Ent : Entity_Id;
9312 Actual_Of_Formal : Node_Id;
9313 Formal_Node : Node_Id;
9314 Formal_Ent : Entity_Id;
9315
9316 begin
9317 if Present (Formals) then
9318 Formal_Node := First_Non_Pragma (Formals);
9319 else
9320 Formal_Node := Empty;
9321 end if;
9322
9323 Actual_Ent := First_Entity (Actual_Pack);
9324 Actual_Of_Formal :=
9325 First (Visible_Declarations (Specification (Analyzed_Formal)));
9326 while Present (Actual_Ent)
9327 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9328 loop
9329 if Present (Formal_Node) then
9330 Formal_Ent := Get_Formal_Entity (Formal_Node);
9331
9332 if Present (Formal_Ent) then
9333 Find_Matching_Actual (Formal_Node, Actual_Ent);
9334 Match_Formal_Entity
9335 (Formal_Node, Formal_Ent, Actual_Ent);
9336
9337 -- We iterate at the same time over the actuals of the
9338 -- local package created for the formal, to determine
9339 -- which one of the formals of the original generic were
9340 -- defaulted in the formal. The corresponding actual
9341 -- entities are visible in the enclosing instance.
9342
9343 if Box_Present (Formal)
9344 or else
9345 (Present (Actual_Of_Formal)
9346 and then
9347 Is_Generic_Formal
9348 (Get_Formal_Entity (Actual_Of_Formal)))
9349 then
9350 Set_Is_Hidden (Actual_Ent, False);
9351 Set_Is_Visible_Formal (Actual_Ent);
9352 Set_Is_Potentially_Use_Visible
9353 (Actual_Ent, In_Use (Actual_Pack));
9354
9355 if Ekind (Actual_Ent) = E_Package then
9356 Process_Nested_Formal (Actual_Ent);
9357 end if;
9358
9359 else
9360 Set_Is_Hidden (Actual_Ent);
9361 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9362 end if;
9363 end if;
9364
9365 Next_Non_Pragma (Formal_Node);
9366 Next (Actual_Of_Formal);
9367
9368 else
9369 -- No further formals to match, but the generic part may
9370 -- contain inherited operation that are not hidden in the
9371 -- enclosing instance.
9372
9373 Next_Entity (Actual_Ent);
9374 end if;
9375 end loop;
9376
9377 -- Inherited subprograms generated by formal derived types are
9378 -- also visible if the types are.
9379
9380 Actual_Ent := First_Entity (Actual_Pack);
9381 while Present (Actual_Ent)
9382 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9383 loop
9384 if Is_Overloadable (Actual_Ent)
9385 and then
9386 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9387 and then
9388 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9389 then
9390 Set_Is_Hidden (Actual_Ent, False);
9391 Set_Is_Potentially_Use_Visible
9392 (Actual_Ent, In_Use (Actual_Pack));
9393 end if;
9394
9395 Next_Entity (Actual_Ent);
9396 end loop;
9397 end;
9398
9399 -- If the formal is not declared with a box, reanalyze it as an
9400 -- abbreviated instantiation, to verify the matching rules of 12.7.
9401 -- The actual checks are performed after the generic associations
9402 -- have been analyzed, to guarantee the same visibility for this
9403 -- instantiation and for the actuals.
9404
9405 -- In Ada 2005, the generic associations for the formal can include
9406 -- defaulted parameters. These are ignored during check. This
9407 -- internal instantiation is removed from the tree after conformance
9408 -- checking, because it contains formal declarations for those
9409 -- defaulted parameters, and those should not reach the back-end.
9410
9411 if not Box_Present (Formal) then
9412 declare
9413 I_Pack : constant Entity_Id :=
9414 Make_Temporary (Sloc (Actual), 'P');
9415
9416 begin
9417 Set_Is_Internal (I_Pack);
9418
9419 Append_To (Decls,
9420 Make_Package_Instantiation (Sloc (Actual),
9421 Defining_Unit_Name => I_Pack,
9422 Name =>
9423 New_Occurrence_Of
9424 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9425 Generic_Associations =>
9426 Generic_Associations (Formal)));
9427 end;
9428 end if;
9429
9430 return Decls;
9431 end if;
9432 end Instantiate_Formal_Package;
9433
9434 -----------------------------------
9435 -- Instantiate_Formal_Subprogram --
9436 -----------------------------------
9437
9438 function Instantiate_Formal_Subprogram
9439 (Formal : Node_Id;
9440 Actual : Node_Id;
9441 Analyzed_Formal : Node_Id) return Node_Id
9442 is
9443 Loc : Source_Ptr;
9444 Formal_Sub : constant Entity_Id :=
9445 Defining_Unit_Name (Specification (Formal));
9446 Analyzed_S : constant Entity_Id :=
9447 Defining_Unit_Name (Specification (Analyzed_Formal));
9448 Decl_Node : Node_Id;
9449 Nam : Node_Id;
9450 New_Spec : Node_Id;
9451
9452 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9453 -- If the generic is a child unit, the parent has been installed on the
9454 -- scope stack, but a default subprogram cannot resolve to something
9455 -- on the parent because that parent is not really part of the visible
9456 -- context (it is there to resolve explicit local entities). If the
9457 -- default has resolved in this way, we remove the entity from immediate
9458 -- visibility and analyze the node again to emit an error message or
9459 -- find another visible candidate.
9460
9461 procedure Valid_Actual_Subprogram (Act : Node_Id);
9462 -- Perform legality check and raise exception on failure
9463
9464 -----------------------
9465 -- From_Parent_Scope --
9466 -----------------------
9467
9468 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9469 Gen_Scope : Node_Id;
9470
9471 begin
9472 Gen_Scope := Scope (Analyzed_S);
9473 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9474 if Scope (Subp) = Scope (Gen_Scope) then
9475 return True;
9476 end if;
9477
9478 Gen_Scope := Scope (Gen_Scope);
9479 end loop;
9480
9481 return False;
9482 end From_Parent_Scope;
9483
9484 -----------------------------
9485 -- Valid_Actual_Subprogram --
9486 -----------------------------
9487
9488 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9489 Act_E : Entity_Id;
9490
9491 begin
9492 if Is_Entity_Name (Act) then
9493 Act_E := Entity (Act);
9494
9495 elsif Nkind (Act) = N_Selected_Component
9496 and then Is_Entity_Name (Selector_Name (Act))
9497 then
9498 Act_E := Entity (Selector_Name (Act));
9499
9500 else
9501 Act_E := Empty;
9502 end if;
9503
9504 if (Present (Act_E) and then Is_Overloadable (Act_E))
9505 or else Nkind_In (Act, N_Attribute_Reference,
9506 N_Indexed_Component,
9507 N_Character_Literal,
9508 N_Explicit_Dereference)
9509 then
9510 return;
9511 end if;
9512
9513 Error_Msg_NE
9514 ("expect subprogram or entry name in instantiation of&",
9515 Instantiation_Node, Formal_Sub);
9516 Abandon_Instantiation (Instantiation_Node);
9517
9518 end Valid_Actual_Subprogram;
9519
9520 -- Start of processing for Instantiate_Formal_Subprogram
9521
9522 begin
9523 New_Spec := New_Copy_Tree (Specification (Formal));
9524
9525 -- The tree copy has created the proper instantiation sloc for the
9526 -- new specification. Use this location for all other constructed
9527 -- declarations.
9528
9529 Loc := Sloc (Defining_Unit_Name (New_Spec));
9530
9531 -- Create new entity for the actual (New_Copy_Tree does not)
9532
9533 Set_Defining_Unit_Name
9534 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9535
9536 -- Create new entities for the each of the formals in the
9537 -- specification of the renaming declaration built for the actual.
9538
9539 if Present (Parameter_Specifications (New_Spec)) then
9540 declare
9541 F : Node_Id;
9542 begin
9543 F := First (Parameter_Specifications (New_Spec));
9544 while Present (F) loop
9545 Set_Defining_Identifier (F,
9546 Make_Defining_Identifier (Sloc (F),
9547 Chars => Chars (Defining_Identifier (F))));
9548 Next (F);
9549 end loop;
9550 end;
9551 end if;
9552
9553 -- Find entity of actual. If the actual is an attribute reference, it
9554 -- cannot be resolved here (its formal is missing) but is handled
9555 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9556 -- fully resolved subsequently, when the renaming declaration for the
9557 -- formal is analyzed. If it is an explicit dereference, resolve the
9558 -- prefix but not the actual itself, to prevent interpretation as call.
9559
9560 if Present (Actual) then
9561 Loc := Sloc (Actual);
9562 Set_Sloc (New_Spec, Loc);
9563
9564 if Nkind (Actual) = N_Operator_Symbol then
9565 Find_Direct_Name (Actual);
9566
9567 elsif Nkind (Actual) = N_Explicit_Dereference then
9568 Analyze (Prefix (Actual));
9569
9570 elsif Nkind (Actual) /= N_Attribute_Reference then
9571 Analyze (Actual);
9572 end if;
9573
9574 Valid_Actual_Subprogram (Actual);
9575 Nam := Actual;
9576
9577 elsif Present (Default_Name (Formal)) then
9578 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9579 N_Selected_Component,
9580 N_Indexed_Component,
9581 N_Character_Literal)
9582 and then Present (Entity (Default_Name (Formal)))
9583 then
9584 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9585 else
9586 Nam := New_Copy (Default_Name (Formal));
9587 Set_Sloc (Nam, Loc);
9588 end if;
9589
9590 elsif Box_Present (Formal) then
9591
9592 -- Actual is resolved at the point of instantiation. Create an
9593 -- identifier or operator with the same name as the formal.
9594
9595 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
9596 Nam := Make_Operator_Symbol (Loc,
9597 Chars => Chars (Formal_Sub),
9598 Strval => No_String);
9599 else
9600 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
9601 end if;
9602
9603 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
9604 and then Null_Present (Specification (Formal))
9605 then
9606 -- Generate null body for procedure, for use in the instance
9607
9608 Decl_Node :=
9609 Make_Subprogram_Body (Loc,
9610 Specification => New_Spec,
9611 Declarations => New_List,
9612 Handled_Statement_Sequence =>
9613 Make_Handled_Sequence_Of_Statements (Loc,
9614 Statements => New_List (Make_Null_Statement (Loc))));
9615
9616 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
9617 return Decl_Node;
9618
9619 else
9620 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
9621 Error_Msg_NE
9622 ("missing actual&", Instantiation_Node, Formal_Sub);
9623 Error_Msg_NE
9624 ("\in instantiation of & declared#",
9625 Instantiation_Node, Scope (Analyzed_S));
9626 Abandon_Instantiation (Instantiation_Node);
9627 end if;
9628
9629 Decl_Node :=
9630 Make_Subprogram_Renaming_Declaration (Loc,
9631 Specification => New_Spec,
9632 Name => Nam);
9633
9634 -- If we do not have an actual and the formal specified <> then set to
9635 -- get proper default.
9636
9637 if No (Actual) and then Box_Present (Formal) then
9638 Set_From_Default (Decl_Node);
9639 end if;
9640
9641 -- Gather possible interpretations for the actual before analyzing the
9642 -- instance. If overloaded, it will be resolved when analyzing the
9643 -- renaming declaration.
9644
9645 if Box_Present (Formal)
9646 and then No (Actual)
9647 then
9648 Analyze (Nam);
9649
9650 if Is_Child_Unit (Scope (Analyzed_S))
9651 and then Present (Entity (Nam))
9652 then
9653 if not Is_Overloaded (Nam) then
9654 if From_Parent_Scope (Entity (Nam)) then
9655 Set_Is_Immediately_Visible (Entity (Nam), False);
9656 Set_Entity (Nam, Empty);
9657 Set_Etype (Nam, Empty);
9658
9659 Analyze (Nam);
9660 Set_Is_Immediately_Visible (Entity (Nam));
9661 end if;
9662
9663 else
9664 declare
9665 I : Interp_Index;
9666 It : Interp;
9667
9668 begin
9669 Get_First_Interp (Nam, I, It);
9670 while Present (It.Nam) loop
9671 if From_Parent_Scope (It.Nam) then
9672 Remove_Interp (I);
9673 end if;
9674
9675 Get_Next_Interp (I, It);
9676 end loop;
9677 end;
9678 end if;
9679 end if;
9680 end if;
9681
9682 -- The generic instantiation freezes the actual. This can only be done
9683 -- once the actual is resolved, in the analysis of the renaming
9684 -- declaration. To make the formal subprogram entity available, we set
9685 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9686 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9687 -- of formal abstract subprograms.
9688
9689 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9690
9691 -- We cannot analyze the renaming declaration, and thus find the actual,
9692 -- until all the actuals are assembled in the instance. For subsequent
9693 -- checks of other actuals, indicate the node that will hold the
9694 -- instance of this formal.
9695
9696 Set_Instance_Of (Analyzed_S, Nam);
9697
9698 if Nkind (Actual) = N_Selected_Component
9699 and then Is_Task_Type (Etype (Prefix (Actual)))
9700 and then not Is_Frozen (Etype (Prefix (Actual)))
9701 then
9702 -- The renaming declaration will create a body, which must appear
9703 -- outside of the instantiation, We move the renaming declaration
9704 -- out of the instance, and create an additional renaming inside,
9705 -- to prevent freezing anomalies.
9706
9707 declare
9708 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9709
9710 begin
9711 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9712 Insert_Before (Instantiation_Node, Decl_Node);
9713 Analyze (Decl_Node);
9714
9715 -- Now create renaming within the instance
9716
9717 Decl_Node :=
9718 Make_Subprogram_Renaming_Declaration (Loc,
9719 Specification => New_Copy_Tree (New_Spec),
9720 Name => New_Occurrence_Of (Anon_Id, Loc));
9721
9722 Set_Defining_Unit_Name (Specification (Decl_Node),
9723 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9724 end;
9725 end if;
9726
9727 return Decl_Node;
9728 end Instantiate_Formal_Subprogram;
9729
9730 ------------------------
9731 -- Instantiate_Object --
9732 ------------------------
9733
9734 function Instantiate_Object
9735 (Formal : Node_Id;
9736 Actual : Node_Id;
9737 Analyzed_Formal : Node_Id) return List_Id
9738 is
9739 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9740 A_Gen_Obj : constant Entity_Id :=
9741 Defining_Identifier (Analyzed_Formal);
9742 Acc_Def : Node_Id := Empty;
9743 Act_Assoc : constant Node_Id := Parent (Actual);
9744 Actual_Decl : Node_Id := Empty;
9745 Decl_Node : Node_Id;
9746 Def : Node_Id;
9747 Ftyp : Entity_Id;
9748 List : constant List_Id := New_List;
9749 Loc : constant Source_Ptr := Sloc (Actual);
9750 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9751 Subt_Decl : Node_Id := Empty;
9752 Subt_Mark : Node_Id := Empty;
9753
9754 begin
9755 if Present (Subtype_Mark (Formal)) then
9756 Subt_Mark := Subtype_Mark (Formal);
9757 else
9758 Check_Access_Definition (Formal);
9759 Acc_Def := Access_Definition (Formal);
9760 end if;
9761
9762 -- Sloc for error message on missing actual
9763
9764 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9765
9766 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9767 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9768 end if;
9769
9770 Set_Parent (List, Parent (Actual));
9771
9772 -- OUT present
9773
9774 if Out_Present (Formal) then
9775
9776 -- An IN OUT generic actual must be a name. The instantiation is a
9777 -- renaming declaration. The actual is the name being renamed. We
9778 -- use the actual directly, rather than a copy, because it is not
9779 -- used further in the list of actuals, and because a copy or a use
9780 -- of relocate_node is incorrect if the instance is nested within a
9781 -- generic. In order to simplify ASIS searches, the Generic_Parent
9782 -- field links the declaration to the generic association.
9783
9784 if No (Actual) then
9785 Error_Msg_NE
9786 ("missing actual&",
9787 Instantiation_Node, Gen_Obj);
9788 Error_Msg_NE
9789 ("\in instantiation of & declared#",
9790 Instantiation_Node, Scope (A_Gen_Obj));
9791 Abandon_Instantiation (Instantiation_Node);
9792 end if;
9793
9794 if Present (Subt_Mark) then
9795 Decl_Node :=
9796 Make_Object_Renaming_Declaration (Loc,
9797 Defining_Identifier => New_Copy (Gen_Obj),
9798 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9799 Name => Actual);
9800
9801 else pragma Assert (Present (Acc_Def));
9802 Decl_Node :=
9803 Make_Object_Renaming_Declaration (Loc,
9804 Defining_Identifier => New_Copy (Gen_Obj),
9805 Access_Definition => New_Copy_Tree (Acc_Def),
9806 Name => Actual);
9807 end if;
9808
9809 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9810
9811 -- The analysis of the actual may produce Insert_Action nodes, so
9812 -- the declaration must have a context in which to attach them.
9813
9814 Append (Decl_Node, List);
9815 Analyze (Actual);
9816
9817 -- Return if the analysis of the actual reported some error
9818
9819 if Etype (Actual) = Any_Type then
9820 return List;
9821 end if;
9822
9823 -- This check is performed here because Analyze_Object_Renaming will
9824 -- not check it when Comes_From_Source is False. Note though that the
9825 -- check for the actual being the name of an object will be performed
9826 -- in Analyze_Object_Renaming.
9827
9828 if Is_Object_Reference (Actual)
9829 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
9830 then
9831 Error_Msg_N
9832 ("illegal discriminant-dependent component for in out parameter",
9833 Actual);
9834 end if;
9835
9836 -- The actual has to be resolved in order to check that it is a
9837 -- variable (due to cases such as F (1), where F returns access to
9838 -- an array, and for overloaded prefixes).
9839
9840 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
9841
9842 -- If the type of the formal is not itself a formal, and the current
9843 -- unit is a child unit, the formal type must be declared in a
9844 -- parent, and must be retrieved by visibility.
9845
9846 if Ftyp = Orig_Ftyp
9847 and then Is_Generic_Unit (Scope (Ftyp))
9848 and then Is_Child_Unit (Scope (A_Gen_Obj))
9849 then
9850 declare
9851 Temp : constant Node_Id :=
9852 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
9853 begin
9854 Set_Entity (Temp, Empty);
9855 Find_Type (Temp);
9856 Ftyp := Entity (Temp);
9857 end;
9858 end if;
9859
9860 if Is_Private_Type (Ftyp)
9861 and then not Is_Private_Type (Etype (Actual))
9862 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
9863 or else Base_Type (Etype (Actual)) = Ftyp)
9864 then
9865 -- If the actual has the type of the full view of the formal, or
9866 -- else a non-private subtype of the formal, then the visibility
9867 -- of the formal type has changed. Add to the actuals a subtype
9868 -- declaration that will force the exchange of views in the body
9869 -- of the instance as well.
9870
9871 Subt_Decl :=
9872 Make_Subtype_Declaration (Loc,
9873 Defining_Identifier => Make_Temporary (Loc, 'P'),
9874 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
9875
9876 Prepend (Subt_Decl, List);
9877
9878 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
9879 Exchange_Declarations (Ftyp);
9880 end if;
9881
9882 Resolve (Actual, Ftyp);
9883
9884 if not Denotes_Variable (Actual) then
9885 Error_Msg_NE
9886 ("actual for& must be a variable", Actual, Gen_Obj);
9887
9888 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
9889
9890 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
9891 -- the type of the actual shall resolve to a specific anonymous
9892 -- access type.
9893
9894 if Ada_Version < Ada_2005
9895 or else
9896 Ekind (Base_Type (Ftyp)) /=
9897 E_Anonymous_Access_Type
9898 or else
9899 Ekind (Base_Type (Etype (Actual))) /=
9900 E_Anonymous_Access_Type
9901 then
9902 Error_Msg_NE ("type of actual does not match type of&",
9903 Actual, Gen_Obj);
9904 end if;
9905 end if;
9906
9907 Note_Possible_Modification (Actual, Sure => True);
9908
9909 -- Check for instantiation of atomic/volatile actual for
9910 -- non-atomic/volatile formal (RM C.6 (12)).
9911
9912 if Is_Atomic_Object (Actual)
9913 and then not Is_Atomic (Orig_Ftyp)
9914 then
9915 Error_Msg_N
9916 ("cannot instantiate non-atomic formal object " &
9917 "with atomic actual", Actual);
9918
9919 elsif Is_Volatile_Object (Actual)
9920 and then not Is_Volatile (Orig_Ftyp)
9921 then
9922 Error_Msg_N
9923 ("cannot instantiate non-volatile formal object " &
9924 "with volatile actual", Actual);
9925 end if;
9926
9927 -- Formal in-parameter
9928
9929 else
9930 -- The instantiation of a generic formal in-parameter is constant
9931 -- declaration. The actual is the expression for that declaration.
9932
9933 if Present (Actual) then
9934 if Present (Subt_Mark) then
9935 Def := Subt_Mark;
9936 else pragma Assert (Present (Acc_Def));
9937 Def := Acc_Def;
9938 end if;
9939
9940 Decl_Node :=
9941 Make_Object_Declaration (Loc,
9942 Defining_Identifier => New_Copy (Gen_Obj),
9943 Constant_Present => True,
9944 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9945 Object_Definition => New_Copy_Tree (Def),
9946 Expression => Actual);
9947
9948 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9949
9950 -- A generic formal object of a tagged type is defined to be
9951 -- aliased so the new constant must also be treated as aliased.
9952
9953 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
9954 Set_Aliased_Present (Decl_Node);
9955 end if;
9956
9957 Append (Decl_Node, List);
9958
9959 -- No need to repeat (pre-)analysis of some expression nodes
9960 -- already handled in Preanalyze_Actuals.
9961
9962 if Nkind (Actual) /= N_Allocator then
9963 Analyze (Actual);
9964
9965 -- Return if the analysis of the actual reported some error
9966
9967 if Etype (Actual) = Any_Type then
9968 return List;
9969 end if;
9970 end if;
9971
9972 declare
9973 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
9974 Typ : Entity_Id;
9975
9976 begin
9977 Typ := Get_Instance_Of (Formal_Type);
9978
9979 Freeze_Before (Instantiation_Node, Typ);
9980
9981 -- If the actual is an aggregate, perform name resolution on
9982 -- its components (the analysis of an aggregate does not do it)
9983 -- to capture local names that may be hidden if the generic is
9984 -- a child unit.
9985
9986 if Nkind (Actual) = N_Aggregate then
9987 Preanalyze_And_Resolve (Actual, Typ);
9988 end if;
9989
9990 if Is_Limited_Type (Typ)
9991 and then not OK_For_Limited_Init (Typ, Actual)
9992 then
9993 Error_Msg_N
9994 ("initialization not allowed for limited types", Actual);
9995 Explain_Limited_Type (Typ, Actual);
9996 end if;
9997 end;
9998
9999 elsif Present (Default_Expression (Formal)) then
10000
10001 -- Use default to construct declaration
10002
10003 if Present (Subt_Mark) then
10004 Def := Subt_Mark;
10005 else pragma Assert (Present (Acc_Def));
10006 Def := Acc_Def;
10007 end if;
10008
10009 Decl_Node :=
10010 Make_Object_Declaration (Sloc (Formal),
10011 Defining_Identifier => New_Copy (Gen_Obj),
10012 Constant_Present => True,
10013 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10014 Object_Definition => New_Copy (Def),
10015 Expression => New_Copy_Tree
10016 (Default_Expression (Formal)));
10017
10018 Append (Decl_Node, List);
10019 Set_Analyzed (Expression (Decl_Node), False);
10020
10021 else
10022 Error_Msg_NE
10023 ("missing actual&",
10024 Instantiation_Node, Gen_Obj);
10025 Error_Msg_NE ("\in instantiation of & declared#",
10026 Instantiation_Node, Scope (A_Gen_Obj));
10027
10028 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10029
10030 -- Create dummy constant declaration so that instance can be
10031 -- analyzed, to minimize cascaded visibility errors.
10032
10033 if Present (Subt_Mark) then
10034 Def := Subt_Mark;
10035 else pragma Assert (Present (Acc_Def));
10036 Def := Acc_Def;
10037 end if;
10038
10039 Decl_Node :=
10040 Make_Object_Declaration (Loc,
10041 Defining_Identifier => New_Copy (Gen_Obj),
10042 Constant_Present => True,
10043 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10044 Object_Definition => New_Copy (Def),
10045 Expression =>
10046 Make_Attribute_Reference (Sloc (Gen_Obj),
10047 Attribute_Name => Name_First,
10048 Prefix => New_Copy (Def)));
10049
10050 Append (Decl_Node, List);
10051
10052 else
10053 Abandon_Instantiation (Instantiation_Node);
10054 end if;
10055 end if;
10056 end if;
10057
10058 if Nkind (Actual) in N_Has_Entity then
10059 Actual_Decl := Parent (Entity (Actual));
10060 end if;
10061
10062 -- Ada 2005 (AI-423): For a formal object declaration with a null
10063 -- exclusion or an access definition that has a null exclusion: If the
10064 -- actual matching the formal object declaration denotes a generic
10065 -- formal object of another generic unit G, and the instantiation
10066 -- containing the actual occurs within the body of G or within the body
10067 -- of a generic unit declared within the declarative region of G, then
10068 -- the declaration of the formal object of G must have a null exclusion.
10069 -- Otherwise, the subtype of the actual matching the formal object
10070 -- declaration shall exclude null.
10071
10072 if Ada_Version >= Ada_2005
10073 and then Present (Actual_Decl)
10074 and then
10075 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10076 N_Object_Declaration)
10077 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10078 and then not Has_Null_Exclusion (Actual_Decl)
10079 and then Has_Null_Exclusion (Analyzed_Formal)
10080 then
10081 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10082 Error_Msg_N
10083 ("actual must exclude null to match generic formal#", Actual);
10084 end if;
10085
10086 -- An effectively volatile object cannot be used as an actual in
10087 -- a generic instance. The following check is only relevant when
10088 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10089
10090 if SPARK_Mode = On
10091 and then Present (Actual)
10092 and then Is_Effectively_Volatile_Object (Actual)
10093 then
10094 Error_Msg_N
10095 ("volatile object cannot act as actual in generic instantiation "
10096 & "(SPARK RM 7.1.3(8))", Actual);
10097 end if;
10098
10099 return List;
10100 end Instantiate_Object;
10101
10102 ------------------------------
10103 -- Instantiate_Package_Body --
10104 ------------------------------
10105
10106 procedure Instantiate_Package_Body
10107 (Body_Info : Pending_Body_Info;
10108 Inlined_Body : Boolean := False;
10109 Body_Optional : Boolean := False)
10110 is
10111 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10112 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10113 Loc : constant Source_Ptr := Sloc (Inst_Node);
10114
10115 Gen_Id : constant Node_Id := Name (Inst_Node);
10116 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10117 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10118 Act_Spec : constant Node_Id := Specification (Act_Decl);
10119 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10120
10121 Act_Body_Name : Node_Id;
10122 Gen_Body : Node_Id;
10123 Gen_Body_Id : Node_Id;
10124 Act_Body : Node_Id;
10125 Act_Body_Id : Entity_Id;
10126
10127 Parent_Installed : Boolean := False;
10128 Save_Style_Check : constant Boolean := Style_Check;
10129
10130 Par_Ent : Entity_Id := Empty;
10131 Par_Vis : Boolean := False;
10132
10133 Vis_Prims_List : Elist_Id := No_Elist;
10134 -- List of primitives made temporarily visible in the instantiation
10135 -- to match the visibility of the formal type
10136
10137 procedure Check_Initialized_Types;
10138 -- In a generic package body, an entity of a generic private type may
10139 -- appear uninitialized. This is suspicious, unless the actual is a
10140 -- fully initialized type.
10141
10142 -----------------------------
10143 -- Check_Initialized_Types --
10144 -----------------------------
10145
10146 procedure Check_Initialized_Types is
10147 Decl : Node_Id;
10148 Formal : Entity_Id;
10149 Actual : Entity_Id;
10150 Uninit_Var : Entity_Id;
10151
10152 begin
10153 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10154 while Present (Decl) loop
10155 Uninit_Var := Empty;
10156
10157 if Nkind (Decl) = N_Private_Extension_Declaration then
10158 Uninit_Var := Uninitialized_Variable (Decl);
10159
10160 elsif Nkind (Decl) = N_Formal_Type_Declaration
10161 and then Nkind (Formal_Type_Definition (Decl)) =
10162 N_Formal_Private_Type_Definition
10163 then
10164 Uninit_Var :=
10165 Uninitialized_Variable (Formal_Type_Definition (Decl));
10166 end if;
10167
10168 if Present (Uninit_Var) then
10169 Formal := Defining_Identifier (Decl);
10170 Actual := First_Entity (Act_Decl_Id);
10171
10172 -- For each formal there is a subtype declaration that renames
10173 -- the actual and has the same name as the formal. Locate the
10174 -- formal for warning message about uninitialized variables
10175 -- in the generic, for which the actual type should be a fully
10176 -- initialized type.
10177
10178 while Present (Actual) loop
10179 exit when Ekind (Actual) = E_Package
10180 and then Present (Renamed_Object (Actual));
10181
10182 if Chars (Actual) = Chars (Formal)
10183 and then not Is_Scalar_Type (Actual)
10184 and then not Is_Fully_Initialized_Type (Actual)
10185 and then Warn_On_No_Value_Assigned
10186 then
10187 Error_Msg_Node_2 := Formal;
10188 Error_Msg_NE
10189 ("generic unit has uninitialized variable& of "
10190 & "formal private type &?v?", Actual, Uninit_Var);
10191 Error_Msg_NE
10192 ("actual type for& should be fully initialized type?v?",
10193 Actual, Formal);
10194 exit;
10195 end if;
10196
10197 Next_Entity (Actual);
10198 end loop;
10199 end if;
10200
10201 Next (Decl);
10202 end loop;
10203 end Check_Initialized_Types;
10204
10205 -- Start of processing for Instantiate_Package_Body
10206
10207 begin
10208 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10209
10210 -- The instance body may already have been processed, as the parent of
10211 -- another instance that is inlined (Load_Parent_Of_Generic).
10212
10213 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10214 return;
10215 end if;
10216
10217 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10218
10219 -- Re-establish the state of information on which checks are suppressed.
10220 -- This information was set in Body_Info at the point of instantiation,
10221 -- and now we restore it so that the instance is compiled using the
10222 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10223
10224 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10225 Scope_Suppress := Body_Info.Scope_Suppress;
10226 Opt.Ada_Version := Body_Info.Version;
10227 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10228 Restore_Warnings (Body_Info.Warnings);
10229 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10230 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10231
10232 if No (Gen_Body_Id) then
10233
10234 -- Do not look for parent of generic body if none is required.
10235 -- This may happen when the routine is called as part of the
10236 -- Pending_Instantiations processing, when nested instances
10237 -- may precede the one generated from the main unit.
10238
10239 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10240 and then Body_Optional
10241 then
10242 return;
10243 else
10244 Load_Parent_Of_Generic
10245 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10246 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10247 end if;
10248 end if;
10249
10250 -- Establish global variable for sloc adjustment and for error recovery
10251
10252 Instantiation_Node := Inst_Node;
10253
10254 if Present (Gen_Body_Id) then
10255 Save_Env (Gen_Unit, Act_Decl_Id);
10256 Style_Check := False;
10257 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10258
10259 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10260
10261 Create_Instantiation_Source
10262 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10263
10264 Act_Body :=
10265 Copy_Generic_Node
10266 (Original_Node (Gen_Body), Empty, Instantiating => True);
10267
10268 -- Build new name (possibly qualified) for body declaration
10269
10270 Act_Body_Id := New_Copy (Act_Decl_Id);
10271
10272 -- Some attributes of spec entity are not inherited by body entity
10273
10274 Set_Handler_Records (Act_Body_Id, No_List);
10275
10276 if Nkind (Defining_Unit_Name (Act_Spec)) =
10277 N_Defining_Program_Unit_Name
10278 then
10279 Act_Body_Name :=
10280 Make_Defining_Program_Unit_Name (Loc,
10281 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10282 Defining_Identifier => Act_Body_Id);
10283 else
10284 Act_Body_Name := Act_Body_Id;
10285 end if;
10286
10287 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10288
10289 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10290 Check_Generic_Actuals (Act_Decl_Id, False);
10291 Check_Initialized_Types;
10292
10293 -- Install primitives hidden at the point of the instantiation but
10294 -- visible when processing the generic formals
10295
10296 declare
10297 E : Entity_Id;
10298
10299 begin
10300 E := First_Entity (Act_Decl_Id);
10301 while Present (E) loop
10302 if Is_Type (E)
10303 and then Is_Generic_Actual_Type (E)
10304 and then Is_Tagged_Type (E)
10305 then
10306 Install_Hidden_Primitives
10307 (Prims_List => Vis_Prims_List,
10308 Gen_T => Generic_Parent_Type (Parent (E)),
10309 Act_T => E);
10310 end if;
10311
10312 Next_Entity (E);
10313 end loop;
10314 end;
10315
10316 -- If it is a child unit, make the parent instance (which is an
10317 -- instance of the parent of the generic) visible. The parent
10318 -- instance is the prefix of the name of the generic unit.
10319
10320 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10321 and then Nkind (Gen_Id) = N_Expanded_Name
10322 then
10323 Par_Ent := Entity (Prefix (Gen_Id));
10324 Par_Vis := Is_Immediately_Visible (Par_Ent);
10325 Install_Parent (Par_Ent, In_Body => True);
10326 Parent_Installed := True;
10327
10328 elsif Is_Child_Unit (Gen_Unit) then
10329 Par_Ent := Scope (Gen_Unit);
10330 Par_Vis := Is_Immediately_Visible (Par_Ent);
10331 Install_Parent (Par_Ent, In_Body => True);
10332 Parent_Installed := True;
10333 end if;
10334
10335 -- If the instantiation is a library unit, and this is the main unit,
10336 -- then build the resulting compilation unit nodes for the instance.
10337 -- If this is a compilation unit but it is not the main unit, then it
10338 -- is the body of a unit in the context, that is being compiled
10339 -- because it is encloses some inlined unit or another generic unit
10340 -- being instantiated. In that case, this body is not part of the
10341 -- current compilation, and is not attached to the tree, but its
10342 -- parent must be set for analysis.
10343
10344 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10345
10346 -- Replace instance node with body of instance, and create new
10347 -- node for corresponding instance declaration.
10348
10349 Build_Instance_Compilation_Unit_Nodes
10350 (Inst_Node, Act_Body, Act_Decl);
10351 Analyze (Inst_Node);
10352
10353 if Parent (Inst_Node) = Cunit (Main_Unit) then
10354
10355 -- If the instance is a child unit itself, then set the scope
10356 -- of the expanded body to be the parent of the instantiation
10357 -- (ensuring that the fully qualified name will be generated
10358 -- for the elaboration subprogram).
10359
10360 if Nkind (Defining_Unit_Name (Act_Spec)) =
10361 N_Defining_Program_Unit_Name
10362 then
10363 Set_Scope
10364 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10365 end if;
10366 end if;
10367
10368 -- Case where instantiation is not a library unit
10369
10370 else
10371 -- If this is an early instantiation, i.e. appears textually
10372 -- before the corresponding body and must be elaborated first,
10373 -- indicate that the body instance is to be delayed.
10374
10375 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10376
10377 -- Now analyze the body. We turn off all checks if this is an
10378 -- internal unit, since there is no reason to have checks on for
10379 -- any predefined run-time library code. All such code is designed
10380 -- to be compiled with checks off.
10381
10382 -- Note that we do NOT apply this criterion to children of GNAT
10383 -- (or on VMS, children of DEC). The latter units must suppress
10384 -- checks explicitly if this is needed.
10385
10386 if Is_Predefined_File_Name
10387 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10388 then
10389 Analyze (Act_Body, Suppress => All_Checks);
10390 else
10391 Analyze (Act_Body);
10392 end if;
10393 end if;
10394
10395 Inherit_Context (Gen_Body, Inst_Node);
10396
10397 -- Remove the parent instances if they have been placed on the scope
10398 -- stack to compile the body.
10399
10400 if Parent_Installed then
10401 Remove_Parent (In_Body => True);
10402
10403 -- Restore the previous visibility of the parent
10404
10405 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10406 end if;
10407
10408 Restore_Hidden_Primitives (Vis_Prims_List);
10409 Restore_Private_Views (Act_Decl_Id);
10410
10411 -- Remove the current unit from visibility if this is an instance
10412 -- that is not elaborated on the fly for inlining purposes.
10413
10414 if not Inlined_Body then
10415 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10416 end if;
10417
10418 Restore_Env;
10419 Style_Check := Save_Style_Check;
10420
10421 -- If we have no body, and the unit requires a body, then complain. This
10422 -- complaint is suppressed if we have detected other errors (since a
10423 -- common reason for missing the body is that it had errors).
10424 -- In CodePeer mode, a warning has been emitted already, no need for
10425 -- further messages.
10426
10427 elsif Unit_Requires_Body (Gen_Unit)
10428 and then not Body_Optional
10429 then
10430 if CodePeer_Mode then
10431 null;
10432
10433 elsif Serious_Errors_Detected = 0 then
10434 Error_Msg_NE
10435 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10436
10437 -- Don't attempt to perform any cleanup actions if some other error
10438 -- was already detected, since this can cause blowups.
10439
10440 else
10441 return;
10442 end if;
10443
10444 -- Case of package that does not need a body
10445
10446 else
10447 -- If the instantiation of the declaration is a library unit, rewrite
10448 -- the original package instantiation as a package declaration in the
10449 -- compilation unit node.
10450
10451 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10452 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10453 Rewrite (Inst_Node, Act_Decl);
10454
10455 -- Generate elaboration entity, in case spec has elaboration code.
10456 -- This cannot be done when the instance is analyzed, because it
10457 -- is not known yet whether the body exists.
10458
10459 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10460 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10461
10462 -- If the instantiation is not a library unit, then append the
10463 -- declaration to the list of implicitly generated entities, unless
10464 -- it is already a list member which means that it was already
10465 -- processed
10466
10467 elsif not Is_List_Member (Act_Decl) then
10468 Mark_Rewrite_Insertion (Act_Decl);
10469 Insert_Before (Inst_Node, Act_Decl);
10470 end if;
10471 end if;
10472
10473 Expander_Mode_Restore;
10474 end Instantiate_Package_Body;
10475
10476 ---------------------------------
10477 -- Instantiate_Subprogram_Body --
10478 ---------------------------------
10479
10480 procedure Instantiate_Subprogram_Body
10481 (Body_Info : Pending_Body_Info;
10482 Body_Optional : Boolean := False)
10483 is
10484 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10485 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10486 Loc : constant Source_Ptr := Sloc (Inst_Node);
10487 Gen_Id : constant Node_Id := Name (Inst_Node);
10488 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10489 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10490 Anon_Id : constant Entity_Id :=
10491 Defining_Unit_Name (Specification (Act_Decl));
10492 Pack_Id : constant Entity_Id :=
10493 Defining_Unit_Name (Parent (Act_Decl));
10494 Decls : List_Id;
10495 Gen_Body : Node_Id;
10496 Gen_Body_Id : Node_Id;
10497 Act_Body : Node_Id;
10498 Pack_Body : Node_Id;
10499 Prev_Formal : Entity_Id;
10500 Ret_Expr : Node_Id;
10501 Unit_Renaming : Node_Id;
10502
10503 Parent_Installed : Boolean := False;
10504
10505 Saved_Style_Check : constant Boolean := Style_Check;
10506 Saved_Warnings : constant Warning_Record := Save_Warnings;
10507
10508 Par_Ent : Entity_Id := Empty;
10509 Par_Vis : Boolean := False;
10510
10511 begin
10512 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10513
10514 -- Subprogram body may have been created already because of an inline
10515 -- pragma, or because of multiple elaborations of the enclosing package
10516 -- when several instances of the subprogram appear in the main unit.
10517
10518 if Present (Corresponding_Body (Act_Decl)) then
10519 return;
10520 end if;
10521
10522 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10523
10524 -- Re-establish the state of information on which checks are suppressed.
10525 -- This information was set in Body_Info at the point of instantiation,
10526 -- and now we restore it so that the instance is compiled using the
10527 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10528
10529 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10530 Scope_Suppress := Body_Info.Scope_Suppress;
10531 Opt.Ada_Version := Body_Info.Version;
10532 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10533 Restore_Warnings (Body_Info.Warnings);
10534 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10535 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10536
10537 if No (Gen_Body_Id) then
10538
10539 -- For imported generic subprogram, no body to compile, complete
10540 -- the spec entity appropriately.
10541
10542 if Is_Imported (Gen_Unit) then
10543 Set_Is_Imported (Anon_Id);
10544 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10545 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
10546 Set_Convention (Anon_Id, Convention (Gen_Unit));
10547 Set_Has_Completion (Anon_Id);
10548 return;
10549
10550 -- For other cases, compile the body
10551
10552 else
10553 Load_Parent_Of_Generic
10554 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10555 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10556 end if;
10557 end if;
10558
10559 Instantiation_Node := Inst_Node;
10560
10561 if Present (Gen_Body_Id) then
10562 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10563
10564 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
10565
10566 -- Either body is not present, or context is non-expanding, as
10567 -- when compiling a subunit. Mark the instance as completed, and
10568 -- diagnose a missing body when needed.
10569
10570 if Expander_Active
10571 and then Operating_Mode = Generate_Code
10572 then
10573 Error_Msg_N
10574 ("missing proper body for instantiation", Gen_Body);
10575 end if;
10576
10577 Set_Has_Completion (Anon_Id);
10578 return;
10579 end if;
10580
10581 Save_Env (Gen_Unit, Anon_Id);
10582 Style_Check := False;
10583 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10584 Create_Instantiation_Source
10585 (Inst_Node,
10586 Gen_Body_Id,
10587 False,
10588 S_Adjustment);
10589
10590 Act_Body :=
10591 Copy_Generic_Node
10592 (Original_Node (Gen_Body), Empty, Instantiating => True);
10593
10594 -- Create proper defining name for the body, to correspond to
10595 -- the one in the spec.
10596
10597 Set_Defining_Unit_Name (Specification (Act_Body),
10598 Make_Defining_Identifier
10599 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
10600 Set_Corresponding_Spec (Act_Body, Anon_Id);
10601 Set_Has_Completion (Anon_Id);
10602 Check_Generic_Actuals (Pack_Id, False);
10603
10604 -- Generate a reference to link the visible subprogram instance to
10605 -- the generic body, which for navigation purposes is the only
10606 -- available source for the instance.
10607
10608 Generate_Reference
10609 (Related_Instance (Pack_Id),
10610 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
10611
10612 -- If it is a child unit, make the parent instance (which is an
10613 -- instance of the parent of the generic) visible. The parent
10614 -- instance is the prefix of the name of the generic unit.
10615
10616 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10617 and then Nkind (Gen_Id) = N_Expanded_Name
10618 then
10619 Par_Ent := Entity (Prefix (Gen_Id));
10620 Par_Vis := Is_Immediately_Visible (Par_Ent);
10621 Install_Parent (Par_Ent, In_Body => True);
10622 Parent_Installed := True;
10623
10624 elsif Is_Child_Unit (Gen_Unit) then
10625 Par_Ent := Scope (Gen_Unit);
10626 Par_Vis := Is_Immediately_Visible (Par_Ent);
10627 Install_Parent (Par_Ent, In_Body => True);
10628 Parent_Installed := True;
10629 end if;
10630
10631 -- Inside its body, a reference to the generic unit is a reference
10632 -- to the instance. The corresponding renaming is the first
10633 -- declaration in the body.
10634
10635 Unit_Renaming :=
10636 Make_Subprogram_Renaming_Declaration (Loc,
10637 Specification =>
10638 Copy_Generic_Node (
10639 Specification (Original_Node (Gen_Body)),
10640 Empty,
10641 Instantiating => True),
10642 Name => New_Occurrence_Of (Anon_Id, Loc));
10643
10644 -- If there is a formal subprogram with the same name as the unit
10645 -- itself, do not add this renaming declaration. This is a temporary
10646 -- fix for one ACVC test. ???
10647
10648 Prev_Formal := First_Entity (Pack_Id);
10649 while Present (Prev_Formal) loop
10650 if Chars (Prev_Formal) = Chars (Gen_Unit)
10651 and then Is_Overloadable (Prev_Formal)
10652 then
10653 exit;
10654 end if;
10655
10656 Next_Entity (Prev_Formal);
10657 end loop;
10658
10659 if Present (Prev_Formal) then
10660 Decls := New_List (Act_Body);
10661 else
10662 Decls := New_List (Unit_Renaming, Act_Body);
10663 end if;
10664
10665 -- The subprogram body is placed in the body of a dummy package body,
10666 -- whose spec contains the subprogram declaration as well as the
10667 -- renaming declarations for the generic parameters.
10668
10669 Pack_Body := Make_Package_Body (Loc,
10670 Defining_Unit_Name => New_Copy (Pack_Id),
10671 Declarations => Decls);
10672
10673 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10674
10675 -- If the instantiation is a library unit, then build resulting
10676 -- compilation unit nodes for the instance. The declaration of
10677 -- the enclosing package is the grandparent of the subprogram
10678 -- declaration. First replace the instantiation node as the unit
10679 -- of the corresponding compilation.
10680
10681 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10682 if Parent (Inst_Node) = Cunit (Main_Unit) then
10683 Set_Unit (Parent (Inst_Node), Inst_Node);
10684 Build_Instance_Compilation_Unit_Nodes
10685 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
10686 Analyze (Inst_Node);
10687 else
10688 Set_Parent (Pack_Body, Parent (Inst_Node));
10689 Analyze (Pack_Body);
10690 end if;
10691
10692 else
10693 Insert_Before (Inst_Node, Pack_Body);
10694 Mark_Rewrite_Insertion (Pack_Body);
10695 Analyze (Pack_Body);
10696
10697 if Expander_Active then
10698 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
10699 end if;
10700 end if;
10701
10702 Inherit_Context (Gen_Body, Inst_Node);
10703
10704 Restore_Private_Views (Pack_Id, False);
10705
10706 if Parent_Installed then
10707 Remove_Parent (In_Body => True);
10708
10709 -- Restore the previous visibility of the parent
10710
10711 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10712 end if;
10713
10714 Restore_Env;
10715 Style_Check := Saved_Style_Check;
10716 Restore_Warnings (Saved_Warnings);
10717
10718 -- Body not found. Error was emitted already. If there were no previous
10719 -- errors, this may be an instance whose scope is a premature instance.
10720 -- In that case we must insure that the (legal) program does raise
10721 -- program error if executed. We generate a subprogram body for this
10722 -- purpose. See DEC ac30vso.
10723
10724 -- Should not reference proprietary DEC tests in comments ???
10725
10726 elsif Serious_Errors_Detected = 0
10727 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
10728 then
10729 if Body_Optional then
10730 return;
10731
10732 elsif Ekind (Anon_Id) = E_Procedure then
10733 Act_Body :=
10734 Make_Subprogram_Body (Loc,
10735 Specification =>
10736 Make_Procedure_Specification (Loc,
10737 Defining_Unit_Name =>
10738 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10739 Parameter_Specifications =>
10740 New_Copy_List
10741 (Parameter_Specifications (Parent (Anon_Id)))),
10742
10743 Declarations => Empty_List,
10744 Handled_Statement_Sequence =>
10745 Make_Handled_Sequence_Of_Statements (Loc,
10746 Statements =>
10747 New_List (
10748 Make_Raise_Program_Error (Loc,
10749 Reason =>
10750 PE_Access_Before_Elaboration))));
10751
10752 else
10753 Ret_Expr :=
10754 Make_Raise_Program_Error (Loc,
10755 Reason => PE_Access_Before_Elaboration);
10756
10757 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
10758 Set_Analyzed (Ret_Expr);
10759
10760 Act_Body :=
10761 Make_Subprogram_Body (Loc,
10762 Specification =>
10763 Make_Function_Specification (Loc,
10764 Defining_Unit_Name =>
10765 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10766 Parameter_Specifications =>
10767 New_Copy_List
10768 (Parameter_Specifications (Parent (Anon_Id))),
10769 Result_Definition =>
10770 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10771
10772 Declarations => Empty_List,
10773 Handled_Statement_Sequence =>
10774 Make_Handled_Sequence_Of_Statements (Loc,
10775 Statements =>
10776 New_List
10777 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10778 end if;
10779
10780 Pack_Body := Make_Package_Body (Loc,
10781 Defining_Unit_Name => New_Copy (Pack_Id),
10782 Declarations => New_List (Act_Body));
10783
10784 Insert_After (Inst_Node, Pack_Body);
10785 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10786 Analyze (Pack_Body);
10787 end if;
10788
10789 Expander_Mode_Restore;
10790 end Instantiate_Subprogram_Body;
10791
10792 ----------------------
10793 -- Instantiate_Type --
10794 ----------------------
10795
10796 function Instantiate_Type
10797 (Formal : Node_Id;
10798 Actual : Node_Id;
10799 Analyzed_Formal : Node_Id;
10800 Actual_Decls : List_Id) return List_Id
10801 is
10802 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10803 A_Gen_T : constant Entity_Id :=
10804 Defining_Identifier (Analyzed_Formal);
10805 Ancestor : Entity_Id := Empty;
10806 Def : constant Node_Id := Formal_Type_Definition (Formal);
10807 Act_T : Entity_Id;
10808 Decl_Node : Node_Id;
10809 Decl_Nodes : List_Id;
10810 Loc : Source_Ptr;
10811 Subt : Entity_Id;
10812
10813 procedure Diagnose_Predicated_Actual;
10814 -- There are a number of constructs in which a discrete type with
10815 -- predicates is illegal, e.g. as an index in an array type declaration.
10816 -- If a generic type is used is such a construct in a generic package
10817 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
10818 -- of the generic contract that the actual cannot have predicates.
10819
10820 procedure Validate_Array_Type_Instance;
10821 procedure Validate_Access_Subprogram_Instance;
10822 procedure Validate_Access_Type_Instance;
10823 procedure Validate_Derived_Type_Instance;
10824 procedure Validate_Derived_Interface_Type_Instance;
10825 procedure Validate_Discriminated_Formal_Type;
10826 procedure Validate_Interface_Type_Instance;
10827 procedure Validate_Private_Type_Instance;
10828 procedure Validate_Incomplete_Type_Instance;
10829 -- These procedures perform validation tests for the named case.
10830 -- Validate_Discriminated_Formal_Type is shared by formal private
10831 -- types and Ada 2012 formal incomplete types.
10832
10833 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
10834 -- Check that base types are the same and that the subtypes match
10835 -- statically. Used in several of the above.
10836
10837 ---------------------------------
10838 -- Diagnose_Predicated_Actual --
10839 ---------------------------------
10840
10841 procedure Diagnose_Predicated_Actual is
10842 begin
10843 if No_Predicate_On_Actual (A_Gen_T)
10844 and then Has_Predicates (Act_T)
10845 then
10846 Error_Msg_NE
10847 ("actual for& cannot be a type with predicate",
10848 Instantiation_Node, A_Gen_T);
10849
10850 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
10851 and then Has_Predicates (Act_T)
10852 and then not Has_Static_Predicate_Aspect (Act_T)
10853 then
10854 Error_Msg_NE
10855 ("actual for& cannot be a type with a dynamic predicate",
10856 Instantiation_Node, A_Gen_T);
10857 end if;
10858 end Diagnose_Predicated_Actual;
10859
10860 --------------------
10861 -- Subtypes_Match --
10862 --------------------
10863
10864 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
10865 T : constant Entity_Id := Get_Instance_Of (Gen_T);
10866
10867 begin
10868 -- Some detailed comments would be useful here ???
10869
10870 return ((Base_Type (T) = Act_T
10871 or else Base_Type (T) = Base_Type (Act_T))
10872 and then Subtypes_Statically_Match (T, Act_T))
10873
10874 or else (Is_Class_Wide_Type (Gen_T)
10875 and then Is_Class_Wide_Type (Act_T)
10876 and then Subtypes_Match
10877 (Get_Instance_Of (Root_Type (Gen_T)),
10878 Root_Type (Act_T)))
10879
10880 or else
10881 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
10882 E_Anonymous_Access_Type)
10883 and then Ekind (Act_T) = Ekind (Gen_T)
10884 and then Subtypes_Statically_Match
10885 (Designated_Type (Gen_T), Designated_Type (Act_T)));
10886 end Subtypes_Match;
10887
10888 -----------------------------------------
10889 -- Validate_Access_Subprogram_Instance --
10890 -----------------------------------------
10891
10892 procedure Validate_Access_Subprogram_Instance is
10893 begin
10894 if not Is_Access_Type (Act_T)
10895 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
10896 then
10897 Error_Msg_NE
10898 ("expect access type in instantiation of &", Actual, Gen_T);
10899 Abandon_Instantiation (Actual);
10900 end if;
10901
10902 -- According to AI05-288, actuals for access_to_subprograms must be
10903 -- subtype conformant with the generic formal. Previous to AI05-288
10904 -- only mode conformance was required.
10905
10906 -- This is a binding interpretation that applies to previous versions
10907 -- of the language, no need to maintain previous weaker checks.
10908
10909 Check_Subtype_Conformant
10910 (Designated_Type (Act_T),
10911 Designated_Type (A_Gen_T),
10912 Actual,
10913 Get_Inst => True);
10914
10915 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
10916 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
10917 Error_Msg_NE
10918 ("protected access type not allowed for formal &",
10919 Actual, Gen_T);
10920 end if;
10921
10922 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
10923 Error_Msg_NE
10924 ("expect protected access type for formal &",
10925 Actual, Gen_T);
10926 end if;
10927 end Validate_Access_Subprogram_Instance;
10928
10929 -----------------------------------
10930 -- Validate_Access_Type_Instance --
10931 -----------------------------------
10932
10933 procedure Validate_Access_Type_Instance is
10934 Desig_Type : constant Entity_Id :=
10935 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
10936 Desig_Act : Entity_Id;
10937
10938 begin
10939 if not Is_Access_Type (Act_T) then
10940 Error_Msg_NE
10941 ("expect access type in instantiation of &", Actual, Gen_T);
10942 Abandon_Instantiation (Actual);
10943 end if;
10944
10945 if Is_Access_Constant (A_Gen_T) then
10946 if not Is_Access_Constant (Act_T) then
10947 Error_Msg_N
10948 ("actual type must be access-to-constant type", Actual);
10949 Abandon_Instantiation (Actual);
10950 end if;
10951 else
10952 if Is_Access_Constant (Act_T) then
10953 Error_Msg_N
10954 ("actual type must be access-to-variable type", Actual);
10955 Abandon_Instantiation (Actual);
10956
10957 elsif Ekind (A_Gen_T) = E_General_Access_Type
10958 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
10959 then
10960 Error_Msg_N -- CODEFIX
10961 ("actual must be general access type!", Actual);
10962 Error_Msg_NE -- CODEFIX
10963 ("add ALL to }!", Actual, Act_T);
10964 Abandon_Instantiation (Actual);
10965 end if;
10966 end if;
10967
10968 -- The designated subtypes, that is to say the subtypes introduced
10969 -- by an access type declaration (and not by a subtype declaration)
10970 -- must match.
10971
10972 Desig_Act := Designated_Type (Base_Type (Act_T));
10973
10974 -- The designated type may have been introduced through a limited_
10975 -- with clause, in which case retrieve the non-limited view. This
10976 -- applies to incomplete types as well as to class-wide types.
10977
10978 if From_Limited_With (Desig_Act) then
10979 Desig_Act := Available_View (Desig_Act);
10980 end if;
10981
10982 if not Subtypes_Match (Desig_Type, Desig_Act) then
10983 Error_Msg_NE
10984 ("designated type of actual does not match that of formal &",
10985 Actual, Gen_T);
10986
10987 if not Predicates_Match (Desig_Type, Desig_Act) then
10988 Error_Msg_N ("\predicates do not match", Actual);
10989 end if;
10990
10991 Abandon_Instantiation (Actual);
10992
10993 elsif Is_Access_Type (Designated_Type (Act_T))
10994 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
10995 /=
10996 Is_Constrained (Designated_Type (Desig_Type))
10997 then
10998 Error_Msg_NE
10999 ("designated type of actual does not match that of formal &",
11000 Actual, Gen_T);
11001
11002 if not Predicates_Match (Desig_Type, Desig_Act) then
11003 Error_Msg_N ("\predicates do not match", Actual);
11004 end if;
11005
11006 Abandon_Instantiation (Actual);
11007 end if;
11008
11009 -- Ada 2005: null-exclusion indicators of the two types must agree
11010
11011 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11012 Error_Msg_NE
11013 ("non null exclusion of actual and formal & do not match",
11014 Actual, Gen_T);
11015 end if;
11016 end Validate_Access_Type_Instance;
11017
11018 ----------------------------------
11019 -- Validate_Array_Type_Instance --
11020 ----------------------------------
11021
11022 procedure Validate_Array_Type_Instance is
11023 I1 : Node_Id;
11024 I2 : Node_Id;
11025 T2 : Entity_Id;
11026
11027 function Formal_Dimensions return Int;
11028 -- Count number of dimensions in array type formal
11029
11030 -----------------------
11031 -- Formal_Dimensions --
11032 -----------------------
11033
11034 function Formal_Dimensions return Int is
11035 Num : Int := 0;
11036 Index : Node_Id;
11037
11038 begin
11039 if Nkind (Def) = N_Constrained_Array_Definition then
11040 Index := First (Discrete_Subtype_Definitions (Def));
11041 else
11042 Index := First (Subtype_Marks (Def));
11043 end if;
11044
11045 while Present (Index) loop
11046 Num := Num + 1;
11047 Next_Index (Index);
11048 end loop;
11049
11050 return Num;
11051 end Formal_Dimensions;
11052
11053 -- Start of processing for Validate_Array_Type_Instance
11054
11055 begin
11056 if not Is_Array_Type (Act_T) then
11057 Error_Msg_NE
11058 ("expect array type in instantiation of &", Actual, Gen_T);
11059 Abandon_Instantiation (Actual);
11060
11061 elsif Nkind (Def) = N_Constrained_Array_Definition then
11062 if not (Is_Constrained (Act_T)) then
11063 Error_Msg_NE
11064 ("expect constrained array in instantiation of &",
11065 Actual, Gen_T);
11066 Abandon_Instantiation (Actual);
11067 end if;
11068
11069 else
11070 if Is_Constrained (Act_T) then
11071 Error_Msg_NE
11072 ("expect unconstrained array in instantiation of &",
11073 Actual, Gen_T);
11074 Abandon_Instantiation (Actual);
11075 end if;
11076 end if;
11077
11078 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11079 Error_Msg_NE
11080 ("dimensions of actual do not match formal &", Actual, Gen_T);
11081 Abandon_Instantiation (Actual);
11082 end if;
11083
11084 I1 := First_Index (A_Gen_T);
11085 I2 := First_Index (Act_T);
11086 for J in 1 .. Formal_Dimensions loop
11087
11088 -- If the indexes of the actual were given by a subtype_mark,
11089 -- the index was transformed into a range attribute. Retrieve
11090 -- the original type mark for checking.
11091
11092 if Is_Entity_Name (Original_Node (I2)) then
11093 T2 := Entity (Original_Node (I2));
11094 else
11095 T2 := Etype (I2);
11096 end if;
11097
11098 if not Subtypes_Match
11099 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11100 then
11101 Error_Msg_NE
11102 ("index types of actual do not match those of formal &",
11103 Actual, Gen_T);
11104 Abandon_Instantiation (Actual);
11105 end if;
11106
11107 Next_Index (I1);
11108 Next_Index (I2);
11109 end loop;
11110
11111 -- Check matching subtypes. Note that there are complex visibility
11112 -- issues when the generic is a child unit and some aspect of the
11113 -- generic type is declared in a parent unit of the generic. We do
11114 -- the test to handle this special case only after a direct check
11115 -- for static matching has failed. The case where both the component
11116 -- type and the array type are separate formals, and the component
11117 -- type is a private view may also require special checking in
11118 -- Subtypes_Match.
11119
11120 if Subtypes_Match
11121 (Component_Type (A_Gen_T), Component_Type (Act_T))
11122 or else Subtypes_Match
11123 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11124 Component_Type (Act_T))
11125 then
11126 null;
11127 else
11128 Error_Msg_NE
11129 ("component subtype of actual does not match that of formal &",
11130 Actual, Gen_T);
11131 Abandon_Instantiation (Actual);
11132 end if;
11133
11134 if Has_Aliased_Components (A_Gen_T)
11135 and then not Has_Aliased_Components (Act_T)
11136 then
11137 Error_Msg_NE
11138 ("actual must have aliased components to match formal type &",
11139 Actual, Gen_T);
11140 end if;
11141 end Validate_Array_Type_Instance;
11142
11143 -----------------------------------------------
11144 -- Validate_Derived_Interface_Type_Instance --
11145 -----------------------------------------------
11146
11147 procedure Validate_Derived_Interface_Type_Instance is
11148 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11149 Elmt : Elmt_Id;
11150
11151 begin
11152 -- First apply interface instance checks
11153
11154 Validate_Interface_Type_Instance;
11155
11156 -- Verify that immediate parent interface is an ancestor of
11157 -- the actual.
11158
11159 if Present (Par)
11160 and then not Interface_Present_In_Ancestor (Act_T, Par)
11161 then
11162 Error_Msg_NE
11163 ("interface actual must include progenitor&", Actual, Par);
11164 end if;
11165
11166 -- Now verify that the actual includes all other ancestors of
11167 -- the formal.
11168
11169 Elmt := First_Elmt (Interfaces (A_Gen_T));
11170 while Present (Elmt) loop
11171 if not Interface_Present_In_Ancestor
11172 (Act_T, Get_Instance_Of (Node (Elmt)))
11173 then
11174 Error_Msg_NE
11175 ("interface actual must include progenitor&",
11176 Actual, Node (Elmt));
11177 end if;
11178
11179 Next_Elmt (Elmt);
11180 end loop;
11181 end Validate_Derived_Interface_Type_Instance;
11182
11183 ------------------------------------
11184 -- Validate_Derived_Type_Instance --
11185 ------------------------------------
11186
11187 procedure Validate_Derived_Type_Instance is
11188 Actual_Discr : Entity_Id;
11189 Ancestor_Discr : Entity_Id;
11190
11191 begin
11192 -- If the parent type in the generic declaration is itself a previous
11193 -- formal type, then it is local to the generic and absent from the
11194 -- analyzed generic definition. In that case the ancestor is the
11195 -- instance of the formal (which must have been instantiated
11196 -- previously), unless the ancestor is itself a formal derived type.
11197 -- In this latter case (which is the subject of Corrigendum 8652/0038
11198 -- (AI-202) the ancestor of the formals is the ancestor of its
11199 -- parent. Otherwise, the analyzed generic carries the parent type.
11200 -- If the parent type is defined in a previous formal package, then
11201 -- the scope of that formal package is that of the generic type
11202 -- itself, and it has already been mapped into the corresponding type
11203 -- in the actual package.
11204
11205 -- Common case: parent type defined outside of the generic
11206
11207 if Is_Entity_Name (Subtype_Mark (Def))
11208 and then Present (Entity (Subtype_Mark (Def)))
11209 then
11210 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11211
11212 -- Check whether parent is defined in a previous formal package
11213
11214 elsif
11215 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11216 then
11217 Ancestor :=
11218 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11219
11220 -- The type may be a local derivation, or a type extension of a
11221 -- previous formal, or of a formal of a parent package.
11222
11223 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11224 or else
11225 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11226 then
11227 -- Check whether the parent is another derived formal type in the
11228 -- same generic unit.
11229
11230 if Etype (A_Gen_T) /= A_Gen_T
11231 and then Is_Generic_Type (Etype (A_Gen_T))
11232 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11233 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11234 then
11235 -- Locate ancestor of parent from the subtype declaration
11236 -- created for the actual.
11237
11238 declare
11239 Decl : Node_Id;
11240
11241 begin
11242 Decl := First (Actual_Decls);
11243 while Present (Decl) loop
11244 if Nkind (Decl) = N_Subtype_Declaration
11245 and then Chars (Defining_Identifier (Decl)) =
11246 Chars (Etype (A_Gen_T))
11247 then
11248 Ancestor := Generic_Parent_Type (Decl);
11249 exit;
11250 else
11251 Next (Decl);
11252 end if;
11253 end loop;
11254 end;
11255
11256 pragma Assert (Present (Ancestor));
11257
11258 -- The ancestor itself may be a previous formal that has been
11259 -- instantiated.
11260
11261 Ancestor := Get_Instance_Of (Ancestor);
11262
11263 else
11264 Ancestor :=
11265 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11266 end if;
11267
11268 -- An unusual case: the actual is a type declared in a parent unit,
11269 -- but is not a formal type so there is no instance_of for it.
11270 -- Retrieve it by analyzing the record extension.
11271
11272 elsif Is_Child_Unit (Scope (A_Gen_T))
11273 and then In_Open_Scopes (Scope (Act_T))
11274 and then Is_Generic_Instance (Scope (Act_T))
11275 then
11276 Analyze (Subtype_Mark (Def));
11277 Ancestor := Entity (Subtype_Mark (Def));
11278
11279 else
11280 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11281 end if;
11282
11283 -- If the formal derived type has pragma Preelaborable_Initialization
11284 -- then the actual type must have preelaborable initialization.
11285
11286 if Known_To_Have_Preelab_Init (A_Gen_T)
11287 and then not Has_Preelaborable_Initialization (Act_T)
11288 then
11289 Error_Msg_NE
11290 ("actual for & must have preelaborable initialization",
11291 Actual, Gen_T);
11292 end if;
11293
11294 -- Ada 2005 (AI-251)
11295
11296 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11297 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11298 Error_Msg_NE
11299 ("(Ada 2005) expected type implementing & in instantiation",
11300 Actual, Ancestor);
11301 end if;
11302
11303 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11304 Error_Msg_NE
11305 ("expect type derived from & in instantiation",
11306 Actual, First_Subtype (Ancestor));
11307 Abandon_Instantiation (Actual);
11308 end if;
11309
11310 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11311 -- that the formal type declaration has been rewritten as a private
11312 -- extension.
11313
11314 if Ada_Version >= Ada_2005
11315 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11316 and then Synchronized_Present (Parent (A_Gen_T))
11317 then
11318 -- The actual must be a synchronized tagged type
11319
11320 if not Is_Tagged_Type (Act_T) then
11321 Error_Msg_N
11322 ("actual of synchronized type must be tagged", Actual);
11323 Abandon_Instantiation (Actual);
11324
11325 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11326 and then Nkind (Type_Definition (Parent (Act_T))) =
11327 N_Derived_Type_Definition
11328 and then not Synchronized_Present (Type_Definition
11329 (Parent (Act_T)))
11330 then
11331 Error_Msg_N
11332 ("actual of synchronized type must be synchronized", Actual);
11333 Abandon_Instantiation (Actual);
11334 end if;
11335 end if;
11336
11337 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11338 -- removes the second instance of the phrase "or allow pass by copy".
11339
11340 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11341 Error_Msg_N
11342 ("cannot have atomic actual type for non-atomic formal type",
11343 Actual);
11344
11345 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11346 Error_Msg_N
11347 ("cannot have volatile actual type for non-volatile formal type",
11348 Actual);
11349 end if;
11350
11351 -- It should not be necessary to check for unknown discriminants on
11352 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11353 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11354 -- needs fixing. ???
11355
11356 if not Is_Indefinite_Subtype (A_Gen_T)
11357 and then not Unknown_Discriminants_Present (Formal)
11358 and then Is_Indefinite_Subtype (Act_T)
11359 then
11360 Error_Msg_N
11361 ("actual subtype must be constrained", Actual);
11362 Abandon_Instantiation (Actual);
11363 end if;
11364
11365 if not Unknown_Discriminants_Present (Formal) then
11366 if Is_Constrained (Ancestor) then
11367 if not Is_Constrained (Act_T) then
11368 Error_Msg_N
11369 ("actual subtype must be constrained", Actual);
11370 Abandon_Instantiation (Actual);
11371 end if;
11372
11373 -- Ancestor is unconstrained, Check if generic formal and actual
11374 -- agree on constrainedness. The check only applies to array types
11375 -- and discriminated types.
11376
11377 elsif Is_Constrained (Act_T) then
11378 if Ekind (Ancestor) = E_Access_Type
11379 or else
11380 (not Is_Constrained (A_Gen_T)
11381 and then Is_Composite_Type (A_Gen_T))
11382 then
11383 Error_Msg_N
11384 ("actual subtype must be unconstrained", Actual);
11385 Abandon_Instantiation (Actual);
11386 end if;
11387
11388 -- A class-wide type is only allowed if the formal has unknown
11389 -- discriminants.
11390
11391 elsif Is_Class_Wide_Type (Act_T)
11392 and then not Has_Unknown_Discriminants (Ancestor)
11393 then
11394 Error_Msg_NE
11395 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11396 Abandon_Instantiation (Actual);
11397
11398 -- Otherwise, the formal and actual shall have the same number
11399 -- of discriminants and each discriminant of the actual must
11400 -- correspond to a discriminant of the formal.
11401
11402 elsif Has_Discriminants (Act_T)
11403 and then not Has_Unknown_Discriminants (Act_T)
11404 and then Has_Discriminants (Ancestor)
11405 then
11406 Actual_Discr := First_Discriminant (Act_T);
11407 Ancestor_Discr := First_Discriminant (Ancestor);
11408 while Present (Actual_Discr)
11409 and then Present (Ancestor_Discr)
11410 loop
11411 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11412 No (Corresponding_Discriminant (Actual_Discr))
11413 then
11414 Error_Msg_NE
11415 ("discriminant & does not correspond " &
11416 "to ancestor discriminant", Actual, Actual_Discr);
11417 Abandon_Instantiation (Actual);
11418 end if;
11419
11420 Next_Discriminant (Actual_Discr);
11421 Next_Discriminant (Ancestor_Discr);
11422 end loop;
11423
11424 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11425 Error_Msg_NE
11426 ("actual for & must have same number of discriminants",
11427 Actual, Gen_T);
11428 Abandon_Instantiation (Actual);
11429 end if;
11430
11431 -- This case should be caught by the earlier check for
11432 -- constrainedness, but the check here is added for completeness.
11433
11434 elsif Has_Discriminants (Act_T)
11435 and then not Has_Unknown_Discriminants (Act_T)
11436 then
11437 Error_Msg_NE
11438 ("actual for & must not have discriminants", Actual, Gen_T);
11439 Abandon_Instantiation (Actual);
11440
11441 elsif Has_Discriminants (Ancestor) then
11442 Error_Msg_NE
11443 ("actual for & must have known discriminants", Actual, Gen_T);
11444 Abandon_Instantiation (Actual);
11445 end if;
11446
11447 if not Subtypes_Statically_Compatible
11448 (Act_T, Ancestor, Formal_Derived_Matching => True)
11449 then
11450 Error_Msg_N
11451 ("constraint on actual is incompatible with formal", Actual);
11452 Abandon_Instantiation (Actual);
11453 end if;
11454 end if;
11455
11456 -- If the formal and actual types are abstract, check that there
11457 -- are no abstract primitives of the actual type that correspond to
11458 -- nonabstract primitives of the formal type (second sentence of
11459 -- RM95-3.9.3(9)).
11460
11461 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11462 Check_Abstract_Primitives : declare
11463 Gen_Prims : constant Elist_Id :=
11464 Primitive_Operations (A_Gen_T);
11465 Gen_Elmt : Elmt_Id;
11466 Gen_Subp : Entity_Id;
11467 Anc_Subp : Entity_Id;
11468 Anc_Formal : Entity_Id;
11469 Anc_F_Type : Entity_Id;
11470
11471 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11472 Act_Elmt : Elmt_Id;
11473 Act_Subp : Entity_Id;
11474 Act_Formal : Entity_Id;
11475 Act_F_Type : Entity_Id;
11476
11477 Subprograms_Correspond : Boolean;
11478
11479 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11480 -- Returns true if T2 is derived directly or indirectly from
11481 -- T1, including derivations from interfaces. T1 and T2 are
11482 -- required to be specific tagged base types.
11483
11484 ------------------------
11485 -- Is_Tagged_Ancestor --
11486 ------------------------
11487
11488 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11489 is
11490 Intfc_Elmt : Elmt_Id;
11491
11492 begin
11493 -- The predicate is satisfied if the types are the same
11494
11495 if T1 = T2 then
11496 return True;
11497
11498 -- If we've reached the top of the derivation chain then
11499 -- we know that T1 is not an ancestor of T2.
11500
11501 elsif Etype (T2) = T2 then
11502 return False;
11503
11504 -- Proceed to check T2's immediate parent
11505
11506 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11507 return True;
11508
11509 -- Finally, check to see if T1 is an ancestor of any of T2's
11510 -- progenitors.
11511
11512 else
11513 Intfc_Elmt := First_Elmt (Interfaces (T2));
11514 while Present (Intfc_Elmt) loop
11515 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11516 return True;
11517 end if;
11518
11519 Next_Elmt (Intfc_Elmt);
11520 end loop;
11521 end if;
11522
11523 return False;
11524 end Is_Tagged_Ancestor;
11525
11526 -- Start of processing for Check_Abstract_Primitives
11527
11528 begin
11529 -- Loop over all of the formal derived type's primitives
11530
11531 Gen_Elmt := First_Elmt (Gen_Prims);
11532 while Present (Gen_Elmt) loop
11533 Gen_Subp := Node (Gen_Elmt);
11534
11535 -- If the primitive of the formal is not abstract, then
11536 -- determine whether there is a corresponding primitive of
11537 -- the actual type that's abstract.
11538
11539 if not Is_Abstract_Subprogram (Gen_Subp) then
11540 Act_Elmt := First_Elmt (Act_Prims);
11541 while Present (Act_Elmt) loop
11542 Act_Subp := Node (Act_Elmt);
11543
11544 -- If we find an abstract primitive of the actual,
11545 -- then we need to test whether it corresponds to the
11546 -- subprogram from which the generic formal primitive
11547 -- is inherited.
11548
11549 if Is_Abstract_Subprogram (Act_Subp) then
11550 Anc_Subp := Alias (Gen_Subp);
11551
11552 -- Test whether we have a corresponding primitive
11553 -- by comparing names, kinds, formal types, and
11554 -- result types.
11555
11556 if Chars (Anc_Subp) = Chars (Act_Subp)
11557 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11558 then
11559 Anc_Formal := First_Formal (Anc_Subp);
11560 Act_Formal := First_Formal (Act_Subp);
11561 while Present (Anc_Formal)
11562 and then Present (Act_Formal)
11563 loop
11564 Anc_F_Type := Etype (Anc_Formal);
11565 Act_F_Type := Etype (Act_Formal);
11566
11567 if Ekind (Anc_F_Type)
11568 = E_Anonymous_Access_Type
11569 then
11570 Anc_F_Type := Designated_Type (Anc_F_Type);
11571
11572 if Ekind (Act_F_Type)
11573 = E_Anonymous_Access_Type
11574 then
11575 Act_F_Type :=
11576 Designated_Type (Act_F_Type);
11577 else
11578 exit;
11579 end if;
11580
11581 elsif
11582 Ekind (Act_F_Type) = E_Anonymous_Access_Type
11583 then
11584 exit;
11585 end if;
11586
11587 Anc_F_Type := Base_Type (Anc_F_Type);
11588 Act_F_Type := Base_Type (Act_F_Type);
11589
11590 -- If the formal is controlling, then the
11591 -- the type of the actual primitive's formal
11592 -- must be derived directly or indirectly
11593 -- from the type of the ancestor primitive's
11594 -- formal.
11595
11596 if Is_Controlling_Formal (Anc_Formal) then
11597 if not Is_Tagged_Ancestor
11598 (Anc_F_Type, Act_F_Type)
11599 then
11600 exit;
11601 end if;
11602
11603 -- Otherwise the types of the formals must
11604 -- be the same.
11605
11606 elsif Anc_F_Type /= Act_F_Type then
11607 exit;
11608 end if;
11609
11610 Next_Entity (Anc_Formal);
11611 Next_Entity (Act_Formal);
11612 end loop;
11613
11614 -- If we traversed through all of the formals
11615 -- then so far the subprograms correspond, so
11616 -- now check that any result types correspond.
11617
11618 if No (Anc_Formal) and then No (Act_Formal) then
11619 Subprograms_Correspond := True;
11620
11621 if Ekind (Act_Subp) = E_Function then
11622 Anc_F_Type := Etype (Anc_Subp);
11623 Act_F_Type := Etype (Act_Subp);
11624
11625 if Ekind (Anc_F_Type)
11626 = E_Anonymous_Access_Type
11627 then
11628 Anc_F_Type :=
11629 Designated_Type (Anc_F_Type);
11630
11631 if Ekind (Act_F_Type)
11632 = E_Anonymous_Access_Type
11633 then
11634 Act_F_Type :=
11635 Designated_Type (Act_F_Type);
11636 else
11637 Subprograms_Correspond := False;
11638 end if;
11639
11640 elsif
11641 Ekind (Act_F_Type)
11642 = E_Anonymous_Access_Type
11643 then
11644 Subprograms_Correspond := False;
11645 end if;
11646
11647 Anc_F_Type := Base_Type (Anc_F_Type);
11648 Act_F_Type := Base_Type (Act_F_Type);
11649
11650 -- Now either the result types must be
11651 -- the same or, if the result type is
11652 -- controlling, the result type of the
11653 -- actual primitive must descend from the
11654 -- result type of the ancestor primitive.
11655
11656 if Subprograms_Correspond
11657 and then Anc_F_Type /= Act_F_Type
11658 and then
11659 Has_Controlling_Result (Anc_Subp)
11660 and then
11661 not Is_Tagged_Ancestor
11662 (Anc_F_Type, Act_F_Type)
11663 then
11664 Subprograms_Correspond := False;
11665 end if;
11666 end if;
11667
11668 -- Found a matching subprogram belonging to
11669 -- formal ancestor type, so actual subprogram
11670 -- corresponds and this violates 3.9.3(9).
11671
11672 if Subprograms_Correspond then
11673 Error_Msg_NE
11674 ("abstract subprogram & overrides " &
11675 "nonabstract subprogram of ancestor",
11676 Actual,
11677 Act_Subp);
11678 end if;
11679 end if;
11680 end if;
11681 end if;
11682
11683 Next_Elmt (Act_Elmt);
11684 end loop;
11685 end if;
11686
11687 Next_Elmt (Gen_Elmt);
11688 end loop;
11689 end Check_Abstract_Primitives;
11690 end if;
11691
11692 -- Verify that limitedness matches. If parent is a limited
11693 -- interface then the generic formal is not unless declared
11694 -- explicitly so. If not declared limited, the actual cannot be
11695 -- limited (see AI05-0087).
11696
11697 -- Even though this AI is a binding interpretation, we enable the
11698 -- check only in Ada 2012 mode, because this improper construct
11699 -- shows up in user code and in existing B-tests.
11700
11701 if Is_Limited_Type (Act_T)
11702 and then not Is_Limited_Type (A_Gen_T)
11703 and then Ada_Version >= Ada_2012
11704 then
11705 if In_Instance then
11706 null;
11707 else
11708 Error_Msg_NE
11709 ("actual for non-limited & cannot be a limited type", Actual,
11710 Gen_T);
11711 Explain_Limited_Type (Act_T, Actual);
11712 Abandon_Instantiation (Actual);
11713 end if;
11714 end if;
11715 end Validate_Derived_Type_Instance;
11716
11717 ----------------------------------------
11718 -- Validate_Discriminated_Formal_Type --
11719 ----------------------------------------
11720
11721 procedure Validate_Discriminated_Formal_Type is
11722 Formal_Discr : Entity_Id;
11723 Actual_Discr : Entity_Id;
11724 Formal_Subt : Entity_Id;
11725
11726 begin
11727 if Has_Discriminants (A_Gen_T) then
11728 if not Has_Discriminants (Act_T) then
11729 Error_Msg_NE
11730 ("actual for & must have discriminants", Actual, Gen_T);
11731 Abandon_Instantiation (Actual);
11732
11733 elsif Is_Constrained (Act_T) then
11734 Error_Msg_NE
11735 ("actual for & must be unconstrained", Actual, Gen_T);
11736 Abandon_Instantiation (Actual);
11737
11738 else
11739 Formal_Discr := First_Discriminant (A_Gen_T);
11740 Actual_Discr := First_Discriminant (Act_T);
11741 while Formal_Discr /= Empty loop
11742 if Actual_Discr = Empty then
11743 Error_Msg_NE
11744 ("discriminants on actual do not match formal",
11745 Actual, Gen_T);
11746 Abandon_Instantiation (Actual);
11747 end if;
11748
11749 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
11750
11751 -- Access discriminants match if designated types do
11752
11753 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
11754 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
11755 E_Anonymous_Access_Type
11756 and then
11757 Get_Instance_Of
11758 (Designated_Type (Base_Type (Formal_Subt))) =
11759 Designated_Type (Base_Type (Etype (Actual_Discr)))
11760 then
11761 null;
11762
11763 elsif Base_Type (Formal_Subt) /=
11764 Base_Type (Etype (Actual_Discr))
11765 then
11766 Error_Msg_NE
11767 ("types of actual discriminants must match formal",
11768 Actual, Gen_T);
11769 Abandon_Instantiation (Actual);
11770
11771 elsif not Subtypes_Statically_Match
11772 (Formal_Subt, Etype (Actual_Discr))
11773 and then Ada_Version >= Ada_95
11774 then
11775 Error_Msg_NE
11776 ("subtypes of actual discriminants must match formal",
11777 Actual, Gen_T);
11778 Abandon_Instantiation (Actual);
11779 end if;
11780
11781 Next_Discriminant (Formal_Discr);
11782 Next_Discriminant (Actual_Discr);
11783 end loop;
11784
11785 if Actual_Discr /= Empty then
11786 Error_Msg_NE
11787 ("discriminants on actual do not match formal",
11788 Actual, Gen_T);
11789 Abandon_Instantiation (Actual);
11790 end if;
11791 end if;
11792 end if;
11793 end Validate_Discriminated_Formal_Type;
11794
11795 ---------------------------------------
11796 -- Validate_Incomplete_Type_Instance --
11797 ---------------------------------------
11798
11799 procedure Validate_Incomplete_Type_Instance is
11800 begin
11801 if not Is_Tagged_Type (Act_T)
11802 and then Is_Tagged_Type (A_Gen_T)
11803 then
11804 Error_Msg_NE
11805 ("actual for & must be a tagged type", Actual, Gen_T);
11806 end if;
11807
11808 Validate_Discriminated_Formal_Type;
11809 end Validate_Incomplete_Type_Instance;
11810
11811 --------------------------------------
11812 -- Validate_Interface_Type_Instance --
11813 --------------------------------------
11814
11815 procedure Validate_Interface_Type_Instance is
11816 begin
11817 if not Is_Interface (Act_T) then
11818 Error_Msg_NE
11819 ("actual for formal interface type must be an interface",
11820 Actual, Gen_T);
11821
11822 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
11823 or else
11824 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
11825 or else
11826 Is_Protected_Interface (A_Gen_T) /=
11827 Is_Protected_Interface (Act_T)
11828 or else
11829 Is_Synchronized_Interface (A_Gen_T) /=
11830 Is_Synchronized_Interface (Act_T)
11831 then
11832 Error_Msg_NE
11833 ("actual for interface& does not match (RM 12.5.5(4))",
11834 Actual, Gen_T);
11835 end if;
11836 end Validate_Interface_Type_Instance;
11837
11838 ------------------------------------
11839 -- Validate_Private_Type_Instance --
11840 ------------------------------------
11841
11842 procedure Validate_Private_Type_Instance is
11843 begin
11844 if Is_Limited_Type (Act_T)
11845 and then not Is_Limited_Type (A_Gen_T)
11846 then
11847 if In_Instance then
11848 null;
11849 else
11850 Error_Msg_NE
11851 ("actual for non-limited & cannot be a limited type", Actual,
11852 Gen_T);
11853 Explain_Limited_Type (Act_T, Actual);
11854 Abandon_Instantiation (Actual);
11855 end if;
11856
11857 elsif Known_To_Have_Preelab_Init (A_Gen_T)
11858 and then not Has_Preelaborable_Initialization (Act_T)
11859 then
11860 Error_Msg_NE
11861 ("actual for & must have preelaborable initialization", Actual,
11862 Gen_T);
11863
11864 elsif Is_Indefinite_Subtype (Act_T)
11865 and then not Is_Indefinite_Subtype (A_Gen_T)
11866 and then Ada_Version >= Ada_95
11867 then
11868 Error_Msg_NE
11869 ("actual for & must be a definite subtype", Actual, Gen_T);
11870
11871 elsif not Is_Tagged_Type (Act_T)
11872 and then Is_Tagged_Type (A_Gen_T)
11873 then
11874 Error_Msg_NE
11875 ("actual for & must be a tagged type", Actual, Gen_T);
11876 end if;
11877
11878 Validate_Discriminated_Formal_Type;
11879 Ancestor := Gen_T;
11880 end Validate_Private_Type_Instance;
11881
11882 -- Start of processing for Instantiate_Type
11883
11884 begin
11885 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
11886 Error_Msg_N ("duplicate instantiation of generic type", Actual);
11887 return New_List (Error);
11888
11889 elsif not Is_Entity_Name (Actual)
11890 or else not Is_Type (Entity (Actual))
11891 then
11892 Error_Msg_NE
11893 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
11894 Abandon_Instantiation (Actual);
11895
11896 else
11897 Act_T := Entity (Actual);
11898
11899 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
11900 -- as a generic actual parameter if the corresponding formal type
11901 -- does not have a known_discriminant_part, or is a formal derived
11902 -- type that is an Unchecked_Union type.
11903
11904 if Is_Unchecked_Union (Base_Type (Act_T)) then
11905 if not Has_Discriminants (A_Gen_T)
11906 or else
11907 (Is_Derived_Type (A_Gen_T)
11908 and then
11909 Is_Unchecked_Union (A_Gen_T))
11910 then
11911 null;
11912 else
11913 Error_Msg_N ("unchecked union cannot be the actual for a" &
11914 " discriminated formal type", Act_T);
11915
11916 end if;
11917 end if;
11918
11919 -- Deal with fixed/floating restrictions
11920
11921 if Is_Floating_Point_Type (Act_T) then
11922 Check_Restriction (No_Floating_Point, Actual);
11923 elsif Is_Fixed_Point_Type (Act_T) then
11924 Check_Restriction (No_Fixed_Point, Actual);
11925 end if;
11926
11927 -- Deal with error of using incomplete type as generic actual.
11928 -- This includes limited views of a type, even if the non-limited
11929 -- view may be available.
11930
11931 if Ekind (Act_T) = E_Incomplete_Type
11932 or else (Is_Class_Wide_Type (Act_T)
11933 and then
11934 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
11935 then
11936 -- If the formal is an incomplete type, the actual can be
11937 -- incomplete as well.
11938
11939 if Ekind (A_Gen_T) = E_Incomplete_Type then
11940 null;
11941
11942 elsif Is_Class_Wide_Type (Act_T)
11943 or else No (Full_View (Act_T))
11944 then
11945 Error_Msg_N ("premature use of incomplete type", Actual);
11946 Abandon_Instantiation (Actual);
11947 else
11948 Act_T := Full_View (Act_T);
11949 Set_Entity (Actual, Act_T);
11950
11951 if Has_Private_Component (Act_T) then
11952 Error_Msg_N
11953 ("premature use of type with private component", Actual);
11954 end if;
11955 end if;
11956
11957 -- Deal with error of premature use of private type as generic actual
11958
11959 elsif Is_Private_Type (Act_T)
11960 and then Is_Private_Type (Base_Type (Act_T))
11961 and then not Is_Generic_Type (Act_T)
11962 and then not Is_Derived_Type (Act_T)
11963 and then No (Full_View (Root_Type (Act_T)))
11964 then
11965 -- If the formal is an incomplete type, the actual can be
11966 -- private or incomplete as well.
11967
11968 if Ekind (A_Gen_T) = E_Incomplete_Type then
11969 null;
11970 else
11971 Error_Msg_N ("premature use of private type", Actual);
11972 end if;
11973
11974 elsif Has_Private_Component (Act_T) then
11975 Error_Msg_N
11976 ("premature use of type with private component", Actual);
11977 end if;
11978
11979 Set_Instance_Of (A_Gen_T, Act_T);
11980
11981 -- If the type is generic, the class-wide type may also be used
11982
11983 if Is_Tagged_Type (A_Gen_T)
11984 and then Is_Tagged_Type (Act_T)
11985 and then not Is_Class_Wide_Type (A_Gen_T)
11986 then
11987 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
11988 Class_Wide_Type (Act_T));
11989 end if;
11990
11991 if not Is_Abstract_Type (A_Gen_T)
11992 and then Is_Abstract_Type (Act_T)
11993 then
11994 Error_Msg_N
11995 ("actual of non-abstract formal cannot be abstract", Actual);
11996 end if;
11997
11998 -- A generic scalar type is a first subtype for which we generate
11999 -- an anonymous base type. Indicate that the instance of this base
12000 -- is the base type of the actual.
12001
12002 if Is_Scalar_Type (A_Gen_T) then
12003 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12004 end if;
12005 end if;
12006
12007 if Error_Posted (Act_T) then
12008 null;
12009 else
12010 case Nkind (Def) is
12011 when N_Formal_Private_Type_Definition =>
12012 Validate_Private_Type_Instance;
12013
12014 when N_Formal_Incomplete_Type_Definition =>
12015 Validate_Incomplete_Type_Instance;
12016
12017 when N_Formal_Derived_Type_Definition =>
12018 Validate_Derived_Type_Instance;
12019
12020 when N_Formal_Discrete_Type_Definition =>
12021 if not Is_Discrete_Type (Act_T) then
12022 Error_Msg_NE
12023 ("expect discrete type in instantiation of&",
12024 Actual, Gen_T);
12025 Abandon_Instantiation (Actual);
12026 end if;
12027
12028 Diagnose_Predicated_Actual;
12029
12030 when N_Formal_Signed_Integer_Type_Definition =>
12031 if not Is_Signed_Integer_Type (Act_T) then
12032 Error_Msg_NE
12033 ("expect signed integer type in instantiation of&",
12034 Actual, Gen_T);
12035 Abandon_Instantiation (Actual);
12036 end if;
12037
12038 Diagnose_Predicated_Actual;
12039
12040 when N_Formal_Modular_Type_Definition =>
12041 if not Is_Modular_Integer_Type (Act_T) then
12042 Error_Msg_NE
12043 ("expect modular type in instantiation of &",
12044 Actual, Gen_T);
12045 Abandon_Instantiation (Actual);
12046 end if;
12047
12048 Diagnose_Predicated_Actual;
12049
12050 when N_Formal_Floating_Point_Definition =>
12051 if not Is_Floating_Point_Type (Act_T) then
12052 Error_Msg_NE
12053 ("expect float type in instantiation of &", Actual, Gen_T);
12054 Abandon_Instantiation (Actual);
12055 end if;
12056
12057 when N_Formal_Ordinary_Fixed_Point_Definition =>
12058 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12059 Error_Msg_NE
12060 ("expect ordinary fixed point type in instantiation of &",
12061 Actual, Gen_T);
12062 Abandon_Instantiation (Actual);
12063 end if;
12064
12065 when N_Formal_Decimal_Fixed_Point_Definition =>
12066 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12067 Error_Msg_NE
12068 ("expect decimal type in instantiation of &",
12069 Actual, Gen_T);
12070 Abandon_Instantiation (Actual);
12071 end if;
12072
12073 when N_Array_Type_Definition =>
12074 Validate_Array_Type_Instance;
12075
12076 when N_Access_To_Object_Definition =>
12077 Validate_Access_Type_Instance;
12078
12079 when N_Access_Function_Definition |
12080 N_Access_Procedure_Definition =>
12081 Validate_Access_Subprogram_Instance;
12082
12083 when N_Record_Definition =>
12084 Validate_Interface_Type_Instance;
12085
12086 when N_Derived_Type_Definition =>
12087 Validate_Derived_Interface_Type_Instance;
12088
12089 when others =>
12090 raise Program_Error;
12091
12092 end case;
12093 end if;
12094
12095 Subt := New_Copy (Gen_T);
12096
12097 -- Use adjusted sloc of subtype name as the location for other nodes in
12098 -- the subtype declaration.
12099
12100 Loc := Sloc (Subt);
12101
12102 Decl_Node :=
12103 Make_Subtype_Declaration (Loc,
12104 Defining_Identifier => Subt,
12105 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12106
12107 if Is_Private_Type (Act_T) then
12108 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12109
12110 elsif Is_Access_Type (Act_T)
12111 and then Is_Private_Type (Designated_Type (Act_T))
12112 then
12113 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12114 end if;
12115
12116 Decl_Nodes := New_List (Decl_Node);
12117
12118 -- Flag actual derived types so their elaboration produces the
12119 -- appropriate renamings for the primitive operations of the ancestor.
12120 -- Flag actual for formal private types as well, to determine whether
12121 -- operations in the private part may override inherited operations.
12122 -- If the formal has an interface list, the ancestor is not the
12123 -- parent, but the analyzed formal that includes the interface
12124 -- operations of all its progenitors.
12125
12126 -- Same treatment for formal private types, so we can check whether the
12127 -- type is tagged limited when validating derivations in the private
12128 -- part. (See AI05-096).
12129
12130 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12131 if Present (Interface_List (Def)) then
12132 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12133 else
12134 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12135 end if;
12136
12137 elsif Nkind_In (Def,
12138 N_Formal_Private_Type_Definition,
12139 N_Formal_Incomplete_Type_Definition)
12140 then
12141 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12142 end if;
12143
12144 -- If the actual is a synchronized type that implements an interface,
12145 -- the primitive operations are attached to the corresponding record,
12146 -- and we have to treat it as an additional generic actual, so that its
12147 -- primitive operations become visible in the instance. The task or
12148 -- protected type itself does not carry primitive operations.
12149
12150 if Is_Concurrent_Type (Act_T)
12151 and then Is_Tagged_Type (Act_T)
12152 and then Present (Corresponding_Record_Type (Act_T))
12153 and then Present (Ancestor)
12154 and then Is_Interface (Ancestor)
12155 then
12156 declare
12157 Corr_Rec : constant Entity_Id :=
12158 Corresponding_Record_Type (Act_T);
12159 New_Corr : Entity_Id;
12160 Corr_Decl : Node_Id;
12161
12162 begin
12163 New_Corr := Make_Temporary (Loc, 'S');
12164 Corr_Decl :=
12165 Make_Subtype_Declaration (Loc,
12166 Defining_Identifier => New_Corr,
12167 Subtype_Indication =>
12168 New_Occurrence_Of (Corr_Rec, Loc));
12169 Append_To (Decl_Nodes, Corr_Decl);
12170
12171 if Ekind (Act_T) = E_Task_Type then
12172 Set_Ekind (Subt, E_Task_Subtype);
12173 else
12174 Set_Ekind (Subt, E_Protected_Subtype);
12175 end if;
12176
12177 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12178 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12179 Set_Generic_Parent_Type (Decl_Node, Empty);
12180 end;
12181 end if;
12182
12183 return Decl_Nodes;
12184 end Instantiate_Type;
12185
12186 ---------------------
12187 -- Is_In_Main_Unit --
12188 ---------------------
12189
12190 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12191 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12192 Current_Unit : Node_Id;
12193
12194 begin
12195 if Unum = Main_Unit then
12196 return True;
12197
12198 -- If the current unit is a subunit then it is either the main unit or
12199 -- is being compiled as part of the main unit.
12200
12201 elsif Nkind (N) = N_Compilation_Unit then
12202 return Nkind (Unit (N)) = N_Subunit;
12203 end if;
12204
12205 Current_Unit := Parent (N);
12206 while Present (Current_Unit)
12207 and then Nkind (Current_Unit) /= N_Compilation_Unit
12208 loop
12209 Current_Unit := Parent (Current_Unit);
12210 end loop;
12211
12212 -- The instantiation node is in the main unit, or else the current node
12213 -- (perhaps as the result of nested instantiations) is in the main unit,
12214 -- or in the declaration of the main unit, which in this last case must
12215 -- be a body.
12216
12217 return Unum = Main_Unit
12218 or else Current_Unit = Cunit (Main_Unit)
12219 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12220 or else (Present (Library_Unit (Current_Unit))
12221 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12222 end Is_In_Main_Unit;
12223
12224 ----------------------------
12225 -- Load_Parent_Of_Generic --
12226 ----------------------------
12227
12228 procedure Load_Parent_Of_Generic
12229 (N : Node_Id;
12230 Spec : Node_Id;
12231 Body_Optional : Boolean := False)
12232 is
12233 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12234 Saved_Style_Check : constant Boolean := Style_Check;
12235 Saved_Warnings : constant Warning_Record := Save_Warnings;
12236 True_Parent : Node_Id;
12237 Inst_Node : Node_Id;
12238 OK : Boolean;
12239 Previous_Instances : constant Elist_Id := New_Elmt_List;
12240
12241 procedure Collect_Previous_Instances (Decls : List_Id);
12242 -- Collect all instantiations in the given list of declarations, that
12243 -- precede the generic that we need to load. If the bodies of these
12244 -- instantiations are available, we must analyze them, to ensure that
12245 -- the public symbols generated are the same when the unit is compiled
12246 -- to generate code, and when it is compiled in the context of a unit
12247 -- that needs a particular nested instance. This process is applied to
12248 -- both package and subprogram instances.
12249
12250 --------------------------------
12251 -- Collect_Previous_Instances --
12252 --------------------------------
12253
12254 procedure Collect_Previous_Instances (Decls : List_Id) is
12255 Decl : Node_Id;
12256
12257 begin
12258 Decl := First (Decls);
12259 while Present (Decl) loop
12260 if Sloc (Decl) >= Sloc (Inst_Node) then
12261 return;
12262
12263 -- If Decl is an instantiation, then record it as requiring
12264 -- instantiation of the corresponding body, except if it is an
12265 -- abbreviated instantiation generated internally for conformance
12266 -- checking purposes only for the case of a formal package
12267 -- declared without a box (see Instantiate_Formal_Package). Such
12268 -- an instantiation does not generate any code (the actual code
12269 -- comes from actual) and thus does not need to be analyzed here.
12270 -- If the instantiation appears with a generic package body it is
12271 -- not analyzed here either.
12272
12273 elsif Nkind (Decl) = N_Package_Instantiation
12274 and then not Is_Internal (Defining_Entity (Decl))
12275 then
12276 Append_Elmt (Decl, Previous_Instances);
12277
12278 -- For a subprogram instantiation, omit instantiations intrinsic
12279 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12280
12281 elsif Nkind_In (Decl, N_Function_Instantiation,
12282 N_Procedure_Instantiation)
12283 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12284 then
12285 Append_Elmt (Decl, Previous_Instances);
12286
12287 elsif Nkind (Decl) = N_Package_Declaration then
12288 Collect_Previous_Instances
12289 (Visible_Declarations (Specification (Decl)));
12290 Collect_Previous_Instances
12291 (Private_Declarations (Specification (Decl)));
12292
12293 -- Previous non-generic bodies may contain instances as well
12294
12295 elsif Nkind (Decl) = N_Package_Body
12296 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12297 then
12298 Collect_Previous_Instances (Declarations (Decl));
12299
12300 elsif Nkind (Decl) = N_Subprogram_Body
12301 and then not Acts_As_Spec (Decl)
12302 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12303 then
12304 Collect_Previous_Instances (Declarations (Decl));
12305 end if;
12306
12307 Next (Decl);
12308 end loop;
12309 end Collect_Previous_Instances;
12310
12311 -- Start of processing for Load_Parent_Of_Generic
12312
12313 begin
12314 if not In_Same_Source_Unit (N, Spec)
12315 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12316 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12317 and then not Is_In_Main_Unit (Spec))
12318 then
12319 -- Find body of parent of spec, and analyze it. A special case arises
12320 -- when the parent is an instantiation, that is to say when we are
12321 -- currently instantiating a nested generic. In that case, there is
12322 -- no separate file for the body of the enclosing instance. Instead,
12323 -- the enclosing body must be instantiated as if it were a pending
12324 -- instantiation, in order to produce the body for the nested generic
12325 -- we require now. Note that in that case the generic may be defined
12326 -- in a package body, the instance defined in the same package body,
12327 -- and the original enclosing body may not be in the main unit.
12328
12329 Inst_Node := Empty;
12330
12331 True_Parent := Parent (Spec);
12332 while Present (True_Parent)
12333 and then Nkind (True_Parent) /= N_Compilation_Unit
12334 loop
12335 if Nkind (True_Parent) = N_Package_Declaration
12336 and then
12337 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12338 then
12339 -- Parent is a compilation unit that is an instantiation.
12340 -- Instantiation node has been replaced with package decl.
12341
12342 Inst_Node := Original_Node (True_Parent);
12343 exit;
12344
12345 elsif Nkind (True_Parent) = N_Package_Declaration
12346 and then Present (Generic_Parent (Specification (True_Parent)))
12347 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12348 then
12349 -- Parent is an instantiation within another specification.
12350 -- Declaration for instance has been inserted before original
12351 -- instantiation node. A direct link would be preferable?
12352
12353 Inst_Node := Next (True_Parent);
12354 while Present (Inst_Node)
12355 and then Nkind (Inst_Node) /= N_Package_Instantiation
12356 loop
12357 Next (Inst_Node);
12358 end loop;
12359
12360 -- If the instance appears within a generic, and the generic
12361 -- unit is defined within a formal package of the enclosing
12362 -- generic, there is no generic body available, and none
12363 -- needed. A more precise test should be used ???
12364
12365 if No (Inst_Node) then
12366 return;
12367 end if;
12368
12369 exit;
12370
12371 else
12372 True_Parent := Parent (True_Parent);
12373 end if;
12374 end loop;
12375
12376 -- Case where we are currently instantiating a nested generic
12377
12378 if Present (Inst_Node) then
12379 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12380
12381 -- Instantiation node and declaration of instantiated package
12382 -- were exchanged when only the declaration was needed.
12383 -- Restore instantiation node before proceeding with body.
12384
12385 Set_Unit (Parent (True_Parent), Inst_Node);
12386 end if;
12387
12388 -- Now complete instantiation of enclosing body, if it appears in
12389 -- some other unit. If it appears in the current unit, the body
12390 -- will have been instantiated already.
12391
12392 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12393
12394 -- We need to determine the expander mode to instantiate the
12395 -- enclosing body. Because the generic body we need may use
12396 -- global entities declared in the enclosing package (including
12397 -- aggregates) it is in general necessary to compile this body
12398 -- with expansion enabled, except if we are within a generic
12399 -- package, in which case the usual generic rule applies.
12400
12401 declare
12402 Exp_Status : Boolean := True;
12403 Scop : Entity_Id;
12404
12405 begin
12406 -- Loop through scopes looking for generic package
12407
12408 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12409 while Present (Scop)
12410 and then Scop /= Standard_Standard
12411 loop
12412 if Ekind (Scop) = E_Generic_Package then
12413 Exp_Status := False;
12414 exit;
12415 end if;
12416
12417 Scop := Scope (Scop);
12418 end loop;
12419
12420 -- Collect previous instantiations in the unit that contains
12421 -- the desired generic.
12422
12423 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12424 and then not Body_Optional
12425 then
12426 declare
12427 Decl : Elmt_Id;
12428 Info : Pending_Body_Info;
12429 Par : Node_Id;
12430
12431 begin
12432 Par := Parent (Inst_Node);
12433 while Present (Par) loop
12434 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12435 Par := Parent (Par);
12436 end loop;
12437
12438 pragma Assert (Present (Par));
12439
12440 if Nkind (Par) = N_Package_Body then
12441 Collect_Previous_Instances (Declarations (Par));
12442
12443 elsif Nkind (Par) = N_Package_Declaration then
12444 Collect_Previous_Instances
12445 (Visible_Declarations (Specification (Par)));
12446 Collect_Previous_Instances
12447 (Private_Declarations (Specification (Par)));
12448
12449 else
12450 -- Enclosing unit is a subprogram body. In this
12451 -- case all instance bodies are processed in order
12452 -- and there is no need to collect them separately.
12453
12454 null;
12455 end if;
12456
12457 Decl := First_Elmt (Previous_Instances);
12458 while Present (Decl) loop
12459 Info :=
12460 (Inst_Node => Node (Decl),
12461 Act_Decl =>
12462 Instance_Spec (Node (Decl)),
12463 Expander_Status => Exp_Status,
12464 Current_Sem_Unit =>
12465 Get_Code_Unit (Sloc (Node (Decl))),
12466 Scope_Suppress => Scope_Suppress,
12467 Local_Suppress_Stack_Top =>
12468 Local_Suppress_Stack_Top,
12469 Version => Ada_Version,
12470 Version_Pragma => Ada_Version_Pragma,
12471 Warnings => Save_Warnings,
12472 SPARK_Mode => SPARK_Mode,
12473 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12474
12475 -- Package instance
12476
12477 if
12478 Nkind (Node (Decl)) = N_Package_Instantiation
12479 then
12480 Instantiate_Package_Body
12481 (Info, Body_Optional => True);
12482
12483 -- Subprogram instance
12484
12485 else
12486 -- The instance_spec is the wrapper package,
12487 -- and the subprogram declaration is the last
12488 -- declaration in the wrapper.
12489
12490 Info.Act_Decl :=
12491 Last
12492 (Visible_Declarations
12493 (Specification (Info.Act_Decl)));
12494
12495 Instantiate_Subprogram_Body
12496 (Info, Body_Optional => True);
12497 end if;
12498
12499 Next_Elmt (Decl);
12500 end loop;
12501 end;
12502 end if;
12503
12504 Instantiate_Package_Body
12505 (Body_Info =>
12506 ((Inst_Node => Inst_Node,
12507 Act_Decl => True_Parent,
12508 Expander_Status => Exp_Status,
12509 Current_Sem_Unit => Get_Code_Unit
12510 (Sloc (Inst_Node)),
12511 Scope_Suppress => Scope_Suppress,
12512 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12513 Version => Ada_Version,
12514 Version_Pragma => Ada_Version_Pragma,
12515 Warnings => Save_Warnings,
12516 SPARK_Mode => SPARK_Mode,
12517 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12518 Body_Optional => Body_Optional);
12519 end;
12520 end if;
12521
12522 -- Case where we are not instantiating a nested generic
12523
12524 else
12525 Opt.Style_Check := False;
12526 Expander_Mode_Save_And_Set (True);
12527 Load_Needed_Body (Comp_Unit, OK);
12528 Opt.Style_Check := Saved_Style_Check;
12529 Restore_Warnings (Saved_Warnings);
12530 Expander_Mode_Restore;
12531
12532 if not OK
12533 and then Unit_Requires_Body (Defining_Entity (Spec))
12534 and then not Body_Optional
12535 then
12536 declare
12537 Bname : constant Unit_Name_Type :=
12538 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12539
12540 begin
12541 -- In CodePeer mode, the missing body may make the analysis
12542 -- incomplete, but we do not treat it as fatal.
12543
12544 if CodePeer_Mode then
12545 return;
12546
12547 else
12548 Error_Msg_Unit_1 := Bname;
12549 Error_Msg_N ("this instantiation requires$!", N);
12550 Error_Msg_File_1 :=
12551 Get_File_Name (Bname, Subunit => False);
12552 Error_Msg_N ("\but file{ was not found!", N);
12553 raise Unrecoverable_Error;
12554 end if;
12555 end;
12556 end if;
12557 end if;
12558 end if;
12559
12560 -- If loading parent of the generic caused an instantiation circularity,
12561 -- we abandon compilation at this point, because otherwise in some cases
12562 -- we get into trouble with infinite recursions after this point.
12563
12564 if Circularity_Detected then
12565 raise Unrecoverable_Error;
12566 end if;
12567 end Load_Parent_Of_Generic;
12568
12569 ---------------------------------
12570 -- Map_Formal_Package_Entities --
12571 ---------------------------------
12572
12573 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
12574 E1 : Entity_Id;
12575 E2 : Entity_Id;
12576
12577 begin
12578 Set_Instance_Of (Form, Act);
12579
12580 -- Traverse formal and actual package to map the corresponding entities.
12581 -- We skip over internal entities that may be generated during semantic
12582 -- analysis, and find the matching entities by name, given that they
12583 -- must appear in the same order.
12584
12585 E1 := First_Entity (Form);
12586 E2 := First_Entity (Act);
12587 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
12588 -- Could this test be a single condition??? Seems like it could, and
12589 -- isn't FPE (Form) a constant anyway???
12590
12591 if not Is_Internal (E1)
12592 and then Present (Parent (E1))
12593 and then not Is_Class_Wide_Type (E1)
12594 and then not Is_Internal_Name (Chars (E1))
12595 then
12596 while Present (E2) and then Chars (E2) /= Chars (E1) loop
12597 Next_Entity (E2);
12598 end loop;
12599
12600 if No (E2) then
12601 exit;
12602 else
12603 Set_Instance_Of (E1, E2);
12604
12605 if Is_Type (E1) and then Is_Tagged_Type (E2) then
12606 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
12607 end if;
12608
12609 if Is_Constrained (E1) then
12610 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
12611 end if;
12612
12613 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
12614 Map_Formal_Package_Entities (E1, E2);
12615 end if;
12616 end if;
12617 end if;
12618
12619 Next_Entity (E1);
12620 end loop;
12621 end Map_Formal_Package_Entities;
12622
12623 -----------------------
12624 -- Move_Freeze_Nodes --
12625 -----------------------
12626
12627 procedure Move_Freeze_Nodes
12628 (Out_Of : Entity_Id;
12629 After : Node_Id;
12630 L : List_Id)
12631 is
12632 Decl : Node_Id;
12633 Next_Decl : Node_Id;
12634 Next_Node : Node_Id := After;
12635 Spec : Node_Id;
12636
12637 function Is_Outer_Type (T : Entity_Id) return Boolean;
12638 -- Check whether entity is declared in a scope external to that of the
12639 -- generic unit.
12640
12641 -------------------
12642 -- Is_Outer_Type --
12643 -------------------
12644
12645 function Is_Outer_Type (T : Entity_Id) return Boolean is
12646 Scop : Entity_Id := Scope (T);
12647
12648 begin
12649 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
12650 return True;
12651
12652 else
12653 while Scop /= Standard_Standard loop
12654 if Scop = Out_Of then
12655 return False;
12656 else
12657 Scop := Scope (Scop);
12658 end if;
12659 end loop;
12660
12661 return True;
12662 end if;
12663 end Is_Outer_Type;
12664
12665 -- Start of processing for Move_Freeze_Nodes
12666
12667 begin
12668 if No (L) then
12669 return;
12670 end if;
12671
12672 -- First remove the freeze nodes that may appear before all other
12673 -- declarations.
12674
12675 Decl := First (L);
12676 while Present (Decl)
12677 and then Nkind (Decl) = N_Freeze_Entity
12678 and then Is_Outer_Type (Entity (Decl))
12679 loop
12680 Decl := Remove_Head (L);
12681 Insert_After (Next_Node, Decl);
12682 Set_Analyzed (Decl, False);
12683 Next_Node := Decl;
12684 Decl := First (L);
12685 end loop;
12686
12687 -- Next scan the list of declarations and remove each freeze node that
12688 -- appears ahead of the current node.
12689
12690 while Present (Decl) loop
12691 while Present (Next (Decl))
12692 and then Nkind (Next (Decl)) = N_Freeze_Entity
12693 and then Is_Outer_Type (Entity (Next (Decl)))
12694 loop
12695 Next_Decl := Remove_Next (Decl);
12696 Insert_After (Next_Node, Next_Decl);
12697 Set_Analyzed (Next_Decl, False);
12698 Next_Node := Next_Decl;
12699 end loop;
12700
12701 -- If the declaration is a nested package or concurrent type, then
12702 -- recurse. Nested generic packages will have been processed from the
12703 -- inside out.
12704
12705 case Nkind (Decl) is
12706 when N_Package_Declaration =>
12707 Spec := Specification (Decl);
12708
12709 when N_Task_Type_Declaration =>
12710 Spec := Task_Definition (Decl);
12711
12712 when N_Protected_Type_Declaration =>
12713 Spec := Protected_Definition (Decl);
12714
12715 when others =>
12716 Spec := Empty;
12717 end case;
12718
12719 if Present (Spec) then
12720 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
12721 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
12722 end if;
12723
12724 Next (Decl);
12725 end loop;
12726 end Move_Freeze_Nodes;
12727
12728 ----------------
12729 -- Next_Assoc --
12730 ----------------
12731
12732 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
12733 begin
12734 return Generic_Renamings.Table (E).Next_In_HTable;
12735 end Next_Assoc;
12736
12737 ------------------------
12738 -- Preanalyze_Actuals --
12739 ------------------------
12740
12741 procedure Preanalyze_Actuals (N : Node_Id) is
12742 Assoc : Node_Id;
12743 Act : Node_Id;
12744 Errs : constant Int := Serious_Errors_Detected;
12745
12746 Cur : Entity_Id := Empty;
12747 -- Current homograph of the instance name
12748
12749 Vis : Boolean;
12750 -- Saved visibility status of the current homograph
12751
12752 begin
12753 Assoc := First (Generic_Associations (N));
12754
12755 -- If the instance is a child unit, its name may hide an outer homonym,
12756 -- so make it invisible to perform name resolution on the actuals.
12757
12758 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
12759 and then Present
12760 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
12761 then
12762 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
12763
12764 if Is_Compilation_Unit (Cur) then
12765 Vis := Is_Immediately_Visible (Cur);
12766 Set_Is_Immediately_Visible (Cur, False);
12767 else
12768 Cur := Empty;
12769 end if;
12770 end if;
12771
12772 while Present (Assoc) loop
12773 if Nkind (Assoc) /= N_Others_Choice then
12774 Act := Explicit_Generic_Actual_Parameter (Assoc);
12775
12776 -- Within a nested instantiation, a defaulted actual is an empty
12777 -- association, so nothing to analyze. If the subprogram actual
12778 -- is an attribute, analyze prefix only, because actual is not a
12779 -- complete attribute reference.
12780
12781 -- If actual is an allocator, analyze expression only. The full
12782 -- analysis can generate code, and if instance is a compilation
12783 -- unit we have to wait until the package instance is installed
12784 -- to have a proper place to insert this code.
12785
12786 -- String literals may be operators, but at this point we do not
12787 -- know whether the actual is a formal subprogram or a string.
12788
12789 if No (Act) then
12790 null;
12791
12792 elsif Nkind (Act) = N_Attribute_Reference then
12793 Analyze (Prefix (Act));
12794
12795 elsif Nkind (Act) = N_Explicit_Dereference then
12796 Analyze (Prefix (Act));
12797
12798 elsif Nkind (Act) = N_Allocator then
12799 declare
12800 Expr : constant Node_Id := Expression (Act);
12801
12802 begin
12803 if Nkind (Expr) = N_Subtype_Indication then
12804 Analyze (Subtype_Mark (Expr));
12805
12806 -- Analyze separately each discriminant constraint, when
12807 -- given with a named association.
12808
12809 declare
12810 Constr : Node_Id;
12811
12812 begin
12813 Constr := First (Constraints (Constraint (Expr)));
12814 while Present (Constr) loop
12815 if Nkind (Constr) = N_Discriminant_Association then
12816 Analyze (Expression (Constr));
12817 else
12818 Analyze (Constr);
12819 end if;
12820
12821 Next (Constr);
12822 end loop;
12823 end;
12824
12825 else
12826 Analyze (Expr);
12827 end if;
12828 end;
12829
12830 elsif Nkind (Act) /= N_Operator_Symbol then
12831 Analyze (Act);
12832 end if;
12833
12834 -- Ensure that a ghost subprogram does not act as generic actual
12835
12836 if Is_Entity_Name (Act)
12837 and then Is_Ghost_Subprogram (Entity (Act))
12838 then
12839 Error_Msg_N
12840 ("ghost subprogram & cannot act as generic actual", Act);
12841 Abandon_Instantiation (Act);
12842
12843 elsif Errs /= Serious_Errors_Detected then
12844
12845 -- Do a minimal analysis of the generic, to prevent spurious
12846 -- warnings complaining about the generic being unreferenced,
12847 -- before abandoning the instantiation.
12848
12849 Analyze (Name (N));
12850
12851 if Is_Entity_Name (Name (N))
12852 and then Etype (Name (N)) /= Any_Type
12853 then
12854 Generate_Reference (Entity (Name (N)), Name (N));
12855 Set_Is_Instantiated (Entity (Name (N)));
12856 end if;
12857
12858 if Present (Cur) then
12859
12860 -- For the case of a child instance hiding an outer homonym,
12861 -- provide additional warning which might explain the error.
12862
12863 Set_Is_Immediately_Visible (Cur, Vis);
12864 Error_Msg_NE ("& hides outer unit with the same name??",
12865 N, Defining_Unit_Name (N));
12866 end if;
12867
12868 Abandon_Instantiation (Act);
12869 end if;
12870 end if;
12871
12872 Next (Assoc);
12873 end loop;
12874
12875 if Present (Cur) then
12876 Set_Is_Immediately_Visible (Cur, Vis);
12877 end if;
12878 end Preanalyze_Actuals;
12879
12880 -------------------
12881 -- Remove_Parent --
12882 -------------------
12883
12884 procedure Remove_Parent (In_Body : Boolean := False) is
12885 S : Entity_Id := Current_Scope;
12886 -- S is the scope containing the instantiation just completed. The scope
12887 -- stack contains the parent instances of the instantiation, followed by
12888 -- the original S.
12889
12890 Cur_P : Entity_Id;
12891 E : Entity_Id;
12892 P : Entity_Id;
12893 Hidden : Elmt_Id;
12894
12895 begin
12896 -- After child instantiation is complete, remove from scope stack the
12897 -- extra copy of the current scope, and then remove parent instances.
12898
12899 if not In_Body then
12900 Pop_Scope;
12901
12902 while Current_Scope /= S loop
12903 P := Current_Scope;
12904 End_Package_Scope (Current_Scope);
12905
12906 if In_Open_Scopes (P) then
12907 E := First_Entity (P);
12908 while Present (E) loop
12909 Set_Is_Immediately_Visible (E, True);
12910 Next_Entity (E);
12911 end loop;
12912
12913 -- If instantiation is declared in a block, it is the enclosing
12914 -- scope that might be a parent instance. Note that only one
12915 -- block can be involved, because the parent instances have
12916 -- been installed within it.
12917
12918 if Ekind (P) = E_Block then
12919 Cur_P := Scope (P);
12920 else
12921 Cur_P := P;
12922 end if;
12923
12924 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
12925 -- We are within an instance of some sibling. Retain
12926 -- visibility of parent, for proper subsequent cleanup, and
12927 -- reinstall private declarations as well.
12928
12929 Set_In_Private_Part (P);
12930 Install_Private_Declarations (P);
12931 end if;
12932
12933 -- If the ultimate parent is a top-level unit recorded in
12934 -- Instance_Parent_Unit, then reset its visibility to what it was
12935 -- before instantiation. (It's not clear what the purpose is of
12936 -- testing whether Scope (P) is In_Open_Scopes, but that test was
12937 -- present before the ultimate parent test was added.???)
12938
12939 elsif not In_Open_Scopes (Scope (P))
12940 or else (P = Instance_Parent_Unit
12941 and then not Parent_Unit_Visible)
12942 then
12943 Set_Is_Immediately_Visible (P, False);
12944
12945 -- If the current scope is itself an instantiation of a generic
12946 -- nested within P, and we are in the private part of body of this
12947 -- instantiation, restore the full views of P, that were removed
12948 -- in End_Package_Scope above. This obscure case can occur when a
12949 -- subunit of a generic contains an instance of a child unit of
12950 -- its generic parent unit.
12951
12952 elsif S = Current_Scope and then Is_Generic_Instance (S) then
12953 declare
12954 Par : constant Entity_Id :=
12955 Generic_Parent (Package_Specification (S));
12956 begin
12957 if Present (Par)
12958 and then P = Scope (Par)
12959 and then (In_Package_Body (S) or else In_Private_Part (S))
12960 then
12961 Set_In_Private_Part (P);
12962 Install_Private_Declarations (P);
12963 end if;
12964 end;
12965 end if;
12966 end loop;
12967
12968 -- Reset visibility of entities in the enclosing scope
12969
12970 Set_Is_Hidden_Open_Scope (Current_Scope, False);
12971
12972 Hidden := First_Elmt (Hidden_Entities);
12973 while Present (Hidden) loop
12974 Set_Is_Immediately_Visible (Node (Hidden), True);
12975 Next_Elmt (Hidden);
12976 end loop;
12977
12978 else
12979 -- Each body is analyzed separately, and there is no context that
12980 -- needs preserving from one body instance to the next, so remove all
12981 -- parent scopes that have been installed.
12982
12983 while Present (S) loop
12984 End_Package_Scope (S);
12985 Set_Is_Immediately_Visible (S, False);
12986 S := Current_Scope;
12987 exit when S = Standard_Standard;
12988 end loop;
12989 end if;
12990 end Remove_Parent;
12991
12992 -----------------
12993 -- Restore_Env --
12994 -----------------
12995
12996 procedure Restore_Env is
12997 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
12998
12999 begin
13000 if No (Current_Instantiated_Parent.Act_Id) then
13001 -- Restore environment after subprogram inlining
13002
13003 Restore_Private_Views (Empty);
13004 end if;
13005
13006 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13007 Exchanged_Views := Saved.Exchanged_Views;
13008 Hidden_Entities := Saved.Hidden_Entities;
13009 Current_Sem_Unit := Saved.Current_Sem_Unit;
13010 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13011 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13012
13013 Restore_Opt_Config_Switches (Saved.Switches);
13014
13015 Instance_Envs.Decrement_Last;
13016 end Restore_Env;
13017
13018 ---------------------------
13019 -- Restore_Private_Views --
13020 ---------------------------
13021
13022 procedure Restore_Private_Views
13023 (Pack_Id : Entity_Id;
13024 Is_Package : Boolean := True)
13025 is
13026 M : Elmt_Id;
13027 E : Entity_Id;
13028 Typ : Entity_Id;
13029 Dep_Elmt : Elmt_Id;
13030 Dep_Typ : Node_Id;
13031
13032 procedure Restore_Nested_Formal (Formal : Entity_Id);
13033 -- Hide the generic formals of formal packages declared with box which
13034 -- were reachable in the current instantiation.
13035
13036 ---------------------------
13037 -- Restore_Nested_Formal --
13038 ---------------------------
13039
13040 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13041 Ent : Entity_Id;
13042
13043 begin
13044 if Present (Renamed_Object (Formal))
13045 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13046 then
13047 return;
13048
13049 elsif Present (Associated_Formal_Package (Formal)) then
13050 Ent := First_Entity (Formal);
13051 while Present (Ent) loop
13052 exit when Ekind (Ent) = E_Package
13053 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13054
13055 Set_Is_Hidden (Ent);
13056 Set_Is_Potentially_Use_Visible (Ent, False);
13057
13058 -- If package, then recurse
13059
13060 if Ekind (Ent) = E_Package then
13061 Restore_Nested_Formal (Ent);
13062 end if;
13063
13064 Next_Entity (Ent);
13065 end loop;
13066 end if;
13067 end Restore_Nested_Formal;
13068
13069 -- Start of processing for Restore_Private_Views
13070
13071 begin
13072 M := First_Elmt (Exchanged_Views);
13073 while Present (M) loop
13074 Typ := Node (M);
13075
13076 -- Subtypes of types whose views have been exchanged, and that are
13077 -- defined within the instance, were not on the Private_Dependents
13078 -- list on entry to the instance, so they have to be exchanged
13079 -- explicitly now, in order to remain consistent with the view of the
13080 -- parent type.
13081
13082 if Ekind_In (Typ, E_Private_Type,
13083 E_Limited_Private_Type,
13084 E_Record_Type_With_Private)
13085 then
13086 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13087 while Present (Dep_Elmt) loop
13088 Dep_Typ := Node (Dep_Elmt);
13089
13090 if Scope (Dep_Typ) = Pack_Id
13091 and then Present (Full_View (Dep_Typ))
13092 then
13093 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13094 Exchange_Declarations (Dep_Typ);
13095 end if;
13096
13097 Next_Elmt (Dep_Elmt);
13098 end loop;
13099 end if;
13100
13101 Exchange_Declarations (Node (M));
13102 Next_Elmt (M);
13103 end loop;
13104
13105 if No (Pack_Id) then
13106 return;
13107 end if;
13108
13109 -- Make the generic formal parameters private, and make the formal types
13110 -- into subtypes of the actuals again.
13111
13112 E := First_Entity (Pack_Id);
13113 while Present (E) loop
13114 Set_Is_Hidden (E, True);
13115
13116 if Is_Type (E)
13117 and then Nkind (Parent (E)) = N_Subtype_Declaration
13118 then
13119 -- If the actual for E is itself a generic actual type from
13120 -- an enclosing instance, E is still a generic actual type
13121 -- outside of the current instance. This matter when resolving
13122 -- an overloaded call that may be ambiguous in the enclosing
13123 -- instance, when two of its actuals coincide.
13124
13125 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13126 and then Is_Generic_Actual_Type
13127 (Entity (Subtype_Indication (Parent (E))))
13128 then
13129 null;
13130 else
13131 Set_Is_Generic_Actual_Type (E, False);
13132 end if;
13133
13134 -- An unusual case of aliasing: the actual may also be directly
13135 -- visible in the generic, and be private there, while it is fully
13136 -- visible in the context of the instance. The internal subtype
13137 -- is private in the instance but has full visibility like its
13138 -- parent in the enclosing scope. This enforces the invariant that
13139 -- the privacy status of all private dependents of a type coincide
13140 -- with that of the parent type. This can only happen when a
13141 -- generic child unit is instantiated within a sibling.
13142
13143 if Is_Private_Type (E)
13144 and then not Is_Private_Type (Etype (E))
13145 then
13146 Exchange_Declarations (E);
13147 end if;
13148
13149 elsif Ekind (E) = E_Package then
13150
13151 -- The end of the renaming list is the renaming of the generic
13152 -- package itself. If the instance is a subprogram, all entities
13153 -- in the corresponding package are renamings. If this entity is
13154 -- a formal package, make its own formals private as well. The
13155 -- actual in this case is itself the renaming of an instantiation.
13156 -- If the entity is not a package renaming, it is the entity
13157 -- created to validate formal package actuals: ignore it.
13158
13159 -- If the actual is itself a formal package for the enclosing
13160 -- generic, or the actual for such a formal package, it remains
13161 -- visible on exit from the instance, and therefore nothing needs
13162 -- to be done either, except to keep it accessible.
13163
13164 if Is_Package and then Renamed_Object (E) = Pack_Id then
13165 exit;
13166
13167 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13168 null;
13169
13170 elsif
13171 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13172 then
13173 Set_Is_Hidden (E, False);
13174
13175 else
13176 declare
13177 Act_P : constant Entity_Id := Renamed_Object (E);
13178 Id : Entity_Id;
13179
13180 begin
13181 Id := First_Entity (Act_P);
13182 while Present (Id)
13183 and then Id /= First_Private_Entity (Act_P)
13184 loop
13185 exit when Ekind (Id) = E_Package
13186 and then Renamed_Object (Id) = Act_P;
13187
13188 Set_Is_Hidden (Id, True);
13189 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13190
13191 if Ekind (Id) = E_Package then
13192 Restore_Nested_Formal (Id);
13193 end if;
13194
13195 Next_Entity (Id);
13196 end loop;
13197 end;
13198 end if;
13199 end if;
13200
13201 Next_Entity (E);
13202 end loop;
13203 end Restore_Private_Views;
13204
13205 --------------
13206 -- Save_Env --
13207 --------------
13208
13209 procedure Save_Env
13210 (Gen_Unit : Entity_Id;
13211 Act_Unit : Entity_Id)
13212 is
13213 begin
13214 Init_Env;
13215 Set_Instance_Env (Gen_Unit, Act_Unit);
13216 end Save_Env;
13217
13218 ----------------------------
13219 -- Save_Global_References --
13220 ----------------------------
13221
13222 procedure Save_Global_References (N : Node_Id) is
13223 Gen_Scope : Entity_Id;
13224 E : Entity_Id;
13225 N2 : Node_Id;
13226
13227 function Is_Global (E : Entity_Id) return Boolean;
13228 -- Check whether entity is defined outside of generic unit. Examine the
13229 -- scope of an entity, and the scope of the scope, etc, until we find
13230 -- either Standard, in which case the entity is global, or the generic
13231 -- unit itself, which indicates that the entity is local. If the entity
13232 -- is the generic unit itself, as in the case of a recursive call, or
13233 -- the enclosing generic unit, if different from the current scope, then
13234 -- it is local as well, because it will be replaced at the point of
13235 -- instantiation. On the other hand, if it is a reference to a child
13236 -- unit of a common ancestor, which appears in an instantiation, it is
13237 -- global because it is used to denote a specific compilation unit at
13238 -- the time the instantiations will be analyzed.
13239
13240 procedure Reset_Entity (N : Node_Id);
13241 -- Save semantic information on global entity so that it is not resolved
13242 -- again at instantiation time.
13243
13244 procedure Save_Entity_Descendants (N : Node_Id);
13245 -- Apply Save_Global_References to the two syntactic descendants of
13246 -- non-terminal nodes that carry an Associated_Node and are processed
13247 -- through Reset_Entity. Once the global entity (if any) has been
13248 -- captured together with its type, only two syntactic descendants need
13249 -- to be traversed to complete the processing of the tree rooted at N.
13250 -- This applies to Selected_Components, Expanded_Names, and to Operator
13251 -- nodes. N can also be a character literal, identifier, or operator
13252 -- symbol node, but the call has no effect in these cases.
13253
13254 procedure Save_Global_Defaults (N1, N2 : Node_Id);
13255 -- Default actuals in nested instances must be handled specially
13256 -- because there is no link to them from the original tree. When an
13257 -- actual subprogram is given by a default, we add an explicit generic
13258 -- association for it in the instantiation node. When we save the
13259 -- global references on the name of the instance, we recover the list
13260 -- of generic associations, and add an explicit one to the original
13261 -- generic tree, through which a global actual can be preserved.
13262 -- Similarly, if a child unit is instantiated within a sibling, in the
13263 -- context of the parent, we must preserve the identifier of the parent
13264 -- so that it can be properly resolved in a subsequent instantiation.
13265
13266 procedure Save_Global_Descendant (D : Union_Id);
13267 -- Apply Save_Global_References recursively to the descendents of the
13268 -- current node.
13269
13270 procedure Save_References (N : Node_Id);
13271 -- This is the recursive procedure that does the work, once the
13272 -- enclosing generic scope has been established.
13273
13274 ---------------
13275 -- Is_Global --
13276 ---------------
13277
13278 function Is_Global (E : Entity_Id) return Boolean is
13279 Se : Entity_Id;
13280
13281 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13282 -- Determine whether the parent node of a reference to a child unit
13283 -- denotes an instantiation or a formal package, in which case the
13284 -- reference to the child unit is global, even if it appears within
13285 -- the current scope (e.g. when the instance appears within the body
13286 -- of an ancestor).
13287
13288 ----------------------
13289 -- Is_Instance_Node --
13290 ----------------------
13291
13292 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13293 begin
13294 return Nkind (Decl) in N_Generic_Instantiation
13295 or else
13296 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13297 end Is_Instance_Node;
13298
13299 -- Start of processing for Is_Global
13300
13301 begin
13302 if E = Gen_Scope then
13303 return False;
13304
13305 elsif E = Standard_Standard then
13306 return True;
13307
13308 elsif Is_Child_Unit (E)
13309 and then (Is_Instance_Node (Parent (N2))
13310 or else (Nkind (Parent (N2)) = N_Expanded_Name
13311 and then N2 = Selector_Name (Parent (N2))
13312 and then
13313 Is_Instance_Node (Parent (Parent (N2)))))
13314 then
13315 return True;
13316
13317 else
13318 Se := Scope (E);
13319 while Se /= Gen_Scope loop
13320 if Se = Standard_Standard then
13321 return True;
13322 else
13323 Se := Scope (Se);
13324 end if;
13325 end loop;
13326
13327 return False;
13328 end if;
13329 end Is_Global;
13330
13331 ------------------
13332 -- Reset_Entity --
13333 ------------------
13334
13335 procedure Reset_Entity (N : Node_Id) is
13336
13337 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13338 -- If the type of N2 is global to the generic unit, save the type in
13339 -- the generic node. Just as we perform name capture for explicit
13340 -- references within the generic, we must capture the global types
13341 -- of local entities because they may participate in resolution in
13342 -- the instance.
13343
13344 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13345 -- Find the ultimate ancestor of the current unit. If it is not a
13346 -- generic unit, then the name of the current unit in the prefix of
13347 -- an expanded name must be replaced with its generic homonym to
13348 -- ensure that it will be properly resolved in an instance.
13349
13350 ---------------------
13351 -- Set_Global_Type --
13352 ---------------------
13353
13354 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13355 Typ : constant Entity_Id := Etype (N2);
13356
13357 begin
13358 Set_Etype (N, Typ);
13359
13360 if Entity (N) /= N2
13361 and then Has_Private_View (Entity (N))
13362 then
13363 -- If the entity of N is not the associated node, this is a
13364 -- nested generic and it has an associated node as well, whose
13365 -- type is already the full view (see below). Indicate that the
13366 -- original node has a private view.
13367
13368 Set_Has_Private_View (N);
13369 end if;
13370
13371 -- If not a private type, nothing else to do
13372
13373 if not Is_Private_Type (Typ) then
13374 if Is_Array_Type (Typ)
13375 and then Is_Private_Type (Component_Type (Typ))
13376 then
13377 Set_Has_Private_View (N);
13378 end if;
13379
13380 -- If it is a derivation of a private type in a context where no
13381 -- full view is needed, nothing to do either.
13382
13383 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13384 null;
13385
13386 -- Otherwise mark the type for flipping and use the full view when
13387 -- available.
13388
13389 else
13390 Set_Has_Private_View (N);
13391
13392 if Present (Full_View (Typ)) then
13393 Set_Etype (N2, Full_View (Typ));
13394 end if;
13395 end if;
13396 end Set_Global_Type;
13397
13398 ------------------
13399 -- Top_Ancestor --
13400 ------------------
13401
13402 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13403 Par : Entity_Id;
13404
13405 begin
13406 Par := E;
13407 while Is_Child_Unit (Par) loop
13408 Par := Scope (Par);
13409 end loop;
13410
13411 return Par;
13412 end Top_Ancestor;
13413
13414 -- Start of processing for Reset_Entity
13415
13416 begin
13417 N2 := Get_Associated_Node (N);
13418 E := Entity (N2);
13419
13420 if Present (E) then
13421
13422 -- If the node is an entry call to an entry in an enclosing task,
13423 -- it is rewritten as a selected component. No global entity to
13424 -- preserve in this case, since the expansion will be redone in
13425 -- the instance.
13426
13427 if not Nkind_In (E, N_Defining_Identifier,
13428 N_Defining_Character_Literal,
13429 N_Defining_Operator_Symbol)
13430 then
13431 Set_Associated_Node (N, Empty);
13432 Set_Etype (N, Empty);
13433 return;
13434 end if;
13435
13436 -- If the entity is an itype created as a subtype of an access
13437 -- type with a null exclusion restore source entity for proper
13438 -- visibility. The itype will be created anew in the instance.
13439
13440 if Is_Itype (E)
13441 and then Ekind (E) = E_Access_Subtype
13442 and then Is_Entity_Name (N)
13443 and then Chars (Etype (E)) = Chars (N)
13444 then
13445 E := Etype (E);
13446 Set_Entity (N2, E);
13447 Set_Etype (N2, E);
13448 end if;
13449
13450 if Is_Global (E) then
13451
13452 -- If the entity is a package renaming that is the prefix of
13453 -- an expanded name, it has been rewritten as the renamed
13454 -- package, which is necessary semantically but complicates
13455 -- ASIS tree traversal, so we recover the original entity to
13456 -- expose the renaming. Take into account that the context may
13457 -- be a nested generic, that the original node may itself have
13458 -- an associated node that had better be an entity, and that
13459 -- the current node is still a selected component.
13460
13461 if Ekind (E) = E_Package
13462 and then Nkind (N) = N_Selected_Component
13463 and then Nkind (Parent (N)) = N_Expanded_Name
13464 and then Present (Original_Node (N2))
13465 and then Is_Entity_Name (Original_Node (N2))
13466 and then Present (Entity (Original_Node (N2)))
13467 then
13468 if Is_Global (Entity (Original_Node (N2))) then
13469 N2 := Original_Node (N2);
13470 Set_Associated_Node (N, N2);
13471 Set_Global_Type (N, N2);
13472
13473 else
13474 -- Renaming is local, and will be resolved in instance
13475
13476 Set_Associated_Node (N, Empty);
13477 Set_Etype (N, Empty);
13478 end if;
13479
13480 else
13481 Set_Global_Type (N, N2);
13482 end if;
13483
13484 elsif Nkind (N) = N_Op_Concat
13485 and then Is_Generic_Type (Etype (N2))
13486 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13487 or else
13488 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13489 and then Is_Intrinsic_Subprogram (E)
13490 then
13491 null;
13492
13493 else
13494 -- Entity is local. Mark generic node as unresolved.
13495 -- Note that now it does not have an entity.
13496
13497 Set_Associated_Node (N, Empty);
13498 Set_Etype (N, Empty);
13499 end if;
13500
13501 if Nkind (Parent (N)) in N_Generic_Instantiation
13502 and then N = Name (Parent (N))
13503 then
13504 Save_Global_Defaults (Parent (N), Parent (N2));
13505 end if;
13506
13507 elsif Nkind (Parent (N)) = N_Selected_Component
13508 and then Nkind (Parent (N2)) = N_Expanded_Name
13509 then
13510 if Is_Global (Entity (Parent (N2))) then
13511 Change_Selected_Component_To_Expanded_Name (Parent (N));
13512 Set_Associated_Node (Parent (N), Parent (N2));
13513 Set_Global_Type (Parent (N), Parent (N2));
13514 Save_Entity_Descendants (N);
13515
13516 -- If this is a reference to the current generic entity, replace
13517 -- by the name of the generic homonym of the current package. This
13518 -- is because in an instantiation Par.P.Q will not resolve to the
13519 -- name of the instance, whose enclosing scope is not necessarily
13520 -- Par. We use the generic homonym rather that the name of the
13521 -- generic itself because it may be hidden by a local declaration.
13522
13523 elsif In_Open_Scopes (Entity (Parent (N2)))
13524 and then not
13525 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13526 then
13527 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13528 Rewrite (Parent (N),
13529 Make_Identifier (Sloc (N),
13530 Chars =>
13531 Chars (Generic_Homonym (Entity (Parent (N2))))));
13532 else
13533 Rewrite (Parent (N),
13534 Make_Identifier (Sloc (N),
13535 Chars => Chars (Selector_Name (Parent (N2)))));
13536 end if;
13537 end if;
13538
13539 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13540 and then Parent (N) = Name (Parent (Parent (N)))
13541 then
13542 Save_Global_Defaults
13543 (Parent (Parent (N)), Parent (Parent ((N2))));
13544 end if;
13545
13546 -- A selected component may denote a static constant that has been
13547 -- folded. If the static constant is global to the generic, capture
13548 -- its value. Otherwise the folding will happen in any instantiation.
13549
13550 elsif Nkind (Parent (N)) = N_Selected_Component
13551 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13552 then
13553 if Present (Entity (Original_Node (Parent (N2))))
13554 and then Is_Global (Entity (Original_Node (Parent (N2))))
13555 then
13556 Rewrite (Parent (N), New_Copy (Parent (N2)));
13557 Set_Analyzed (Parent (N), False);
13558
13559 else
13560 null;
13561 end if;
13562
13563 -- A selected component may be transformed into a parameterless
13564 -- function call. If the called entity is global, rewrite the node
13565 -- appropriately, i.e. as an extended name for the global entity.
13566
13567 elsif Nkind (Parent (N)) = N_Selected_Component
13568 and then Nkind (Parent (N2)) = N_Function_Call
13569 and then N = Selector_Name (Parent (N))
13570 then
13571 if No (Parameter_Associations (Parent (N2))) then
13572 if Is_Global (Entity (Name (Parent (N2)))) then
13573 Change_Selected_Component_To_Expanded_Name (Parent (N));
13574 Set_Associated_Node (Parent (N), Name (Parent (N2)));
13575 Set_Global_Type (Parent (N), Name (Parent (N2)));
13576 Save_Entity_Descendants (N);
13577
13578 else
13579 Set_Is_Prefixed_Call (Parent (N));
13580 Set_Associated_Node (N, Empty);
13581 Set_Etype (N, Empty);
13582 end if;
13583
13584 -- In Ada 2005, X.F may be a call to a primitive operation,
13585 -- rewritten as F (X). This rewriting will be done again in an
13586 -- instance, so keep the original node. Global entities will be
13587 -- captured as for other constructs. Indicate that this must
13588 -- resolve as a call, to prevent accidental overloading in the
13589 -- instance, if both a component and a primitive operation appear
13590 -- as candidates.
13591
13592 else
13593 Set_Is_Prefixed_Call (Parent (N));
13594 end if;
13595
13596 -- Entity is local. Reset in generic unit, so that node is resolved
13597 -- anew at the point of instantiation.
13598
13599 else
13600 Set_Associated_Node (N, Empty);
13601 Set_Etype (N, Empty);
13602 end if;
13603 end Reset_Entity;
13604
13605 -----------------------------
13606 -- Save_Entity_Descendants --
13607 -----------------------------
13608
13609 procedure Save_Entity_Descendants (N : Node_Id) is
13610 begin
13611 case Nkind (N) is
13612 when N_Binary_Op =>
13613 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
13614 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13615
13616 when N_Unary_Op =>
13617 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13618
13619 when N_Expanded_Name | N_Selected_Component =>
13620 Save_Global_Descendant (Union_Id (Prefix (N)));
13621 Save_Global_Descendant (Union_Id (Selector_Name (N)));
13622
13623 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
13624 null;
13625
13626 when others =>
13627 raise Program_Error;
13628 end case;
13629 end Save_Entity_Descendants;
13630
13631 --------------------------
13632 -- Save_Global_Defaults --
13633 --------------------------
13634
13635 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
13636 Loc : constant Source_Ptr := Sloc (N1);
13637 Assoc2 : constant List_Id := Generic_Associations (N2);
13638 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
13639 Assoc1 : List_Id;
13640 Act1 : Node_Id;
13641 Act2 : Node_Id;
13642 Def : Node_Id;
13643 Ndec : Node_Id;
13644 Subp : Entity_Id;
13645 Actual : Entity_Id;
13646
13647 begin
13648 Assoc1 := Generic_Associations (N1);
13649
13650 if Present (Assoc1) then
13651 Act1 := First (Assoc1);
13652 else
13653 Act1 := Empty;
13654 Set_Generic_Associations (N1, New_List);
13655 Assoc1 := Generic_Associations (N1);
13656 end if;
13657
13658 if Present (Assoc2) then
13659 Act2 := First (Assoc2);
13660 else
13661 return;
13662 end if;
13663
13664 while Present (Act1) and then Present (Act2) loop
13665 Next (Act1);
13666 Next (Act2);
13667 end loop;
13668
13669 -- Find the associations added for default subprograms
13670
13671 if Present (Act2) then
13672 while Nkind (Act2) /= N_Generic_Association
13673 or else No (Entity (Selector_Name (Act2)))
13674 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
13675 loop
13676 Next (Act2);
13677 end loop;
13678
13679 -- Add a similar association if the default is global. The
13680 -- renaming declaration for the actual has been analyzed, and
13681 -- its alias is the program it renames. Link the actual in the
13682 -- original generic tree with the node in the analyzed tree.
13683
13684 while Present (Act2) loop
13685 Subp := Entity (Selector_Name (Act2));
13686 Def := Explicit_Generic_Actual_Parameter (Act2);
13687
13688 -- Following test is defence against rubbish errors
13689
13690 if No (Alias (Subp)) then
13691 return;
13692 end if;
13693
13694 -- Retrieve the resolved actual from the renaming declaration
13695 -- created for the instantiated formal.
13696
13697 Actual := Entity (Name (Parent (Parent (Subp))));
13698 Set_Entity (Def, Actual);
13699 Set_Etype (Def, Etype (Actual));
13700
13701 if Is_Global (Actual) then
13702 Ndec :=
13703 Make_Generic_Association (Loc,
13704 Selector_Name => New_Occurrence_Of (Subp, Loc),
13705 Explicit_Generic_Actual_Parameter =>
13706 New_Occurrence_Of (Actual, Loc));
13707
13708 Set_Associated_Node
13709 (Explicit_Generic_Actual_Parameter (Ndec), Def);
13710
13711 Append (Ndec, Assoc1);
13712
13713 -- If there are other defaults, add a dummy association in case
13714 -- there are other defaulted formals with the same name.
13715
13716 elsif Present (Next (Act2)) then
13717 Ndec :=
13718 Make_Generic_Association (Loc,
13719 Selector_Name => New_Occurrence_Of (Subp, Loc),
13720 Explicit_Generic_Actual_Parameter => Empty);
13721
13722 Append (Ndec, Assoc1);
13723 end if;
13724
13725 Next (Act2);
13726 end loop;
13727 end if;
13728
13729 if Nkind (Name (N1)) = N_Identifier
13730 and then Is_Child_Unit (Gen_Id)
13731 and then Is_Global (Gen_Id)
13732 and then Is_Generic_Unit (Scope (Gen_Id))
13733 and then In_Open_Scopes (Scope (Gen_Id))
13734 then
13735 -- This is an instantiation of a child unit within a sibling, so
13736 -- that the generic parent is in scope. An eventual instance must
13737 -- occur within the scope of an instance of the parent. Make name
13738 -- in instance into an expanded name, to preserve the identifier
13739 -- of the parent, so it can be resolved subsequently.
13740
13741 Rewrite (Name (N2),
13742 Make_Expanded_Name (Loc,
13743 Chars => Chars (Gen_Id),
13744 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13745 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13746 Set_Entity (Name (N2), Gen_Id);
13747
13748 Rewrite (Name (N1),
13749 Make_Expanded_Name (Loc,
13750 Chars => Chars (Gen_Id),
13751 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13752 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13753
13754 Set_Associated_Node (Name (N1), Name (N2));
13755 Set_Associated_Node (Prefix (Name (N1)), Empty);
13756 Set_Associated_Node
13757 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
13758 Set_Etype (Name (N1), Etype (Gen_Id));
13759 end if;
13760
13761 end Save_Global_Defaults;
13762
13763 ----------------------------
13764 -- Save_Global_Descendant --
13765 ----------------------------
13766
13767 procedure Save_Global_Descendant (D : Union_Id) is
13768 N1 : Node_Id;
13769
13770 begin
13771 if D in Node_Range then
13772 if D = Union_Id (Empty) then
13773 null;
13774
13775 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
13776 Save_References (Node_Id (D));
13777 end if;
13778
13779 elsif D in List_Range then
13780 if D = Union_Id (No_List)
13781 or else Is_Empty_List (List_Id (D))
13782 then
13783 null;
13784
13785 else
13786 N1 := First (List_Id (D));
13787 while Present (N1) loop
13788 Save_References (N1);
13789 Next (N1);
13790 end loop;
13791 end if;
13792
13793 -- Element list or other non-node field, nothing to do
13794
13795 else
13796 null;
13797 end if;
13798 end Save_Global_Descendant;
13799
13800 ---------------------
13801 -- Save_References --
13802 ---------------------
13803
13804 -- This is the recursive procedure that does the work once the enclosing
13805 -- generic scope has been established. We have to treat specially a
13806 -- number of node rewritings that are required by semantic processing
13807 -- and which change the kind of nodes in the generic copy: typically
13808 -- constant-folding, replacing an operator node by a string literal, or
13809 -- a selected component by an expanded name. In each of those cases, the
13810 -- transformation is propagated to the generic unit.
13811
13812 procedure Save_References (N : Node_Id) is
13813 Loc : constant Source_Ptr := Sloc (N);
13814
13815 begin
13816 if N = Empty then
13817 null;
13818
13819 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
13820 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13821 Reset_Entity (N);
13822
13823 elsif Nkind (N) = N_Operator_Symbol
13824 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
13825 then
13826 Change_Operator_Symbol_To_String_Literal (N);
13827 end if;
13828
13829 elsif Nkind (N) in N_Op then
13830 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13831 if Nkind (N) = N_Op_Concat then
13832 Set_Is_Component_Left_Opnd (N,
13833 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13834
13835 Set_Is_Component_Right_Opnd (N,
13836 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13837 end if;
13838
13839 Reset_Entity (N);
13840
13841 else
13842 -- Node may be transformed into call to a user-defined operator
13843
13844 N2 := Get_Associated_Node (N);
13845
13846 if Nkind (N2) = N_Function_Call then
13847 E := Entity (Name (N2));
13848
13849 if Present (E)
13850 and then Is_Global (E)
13851 then
13852 Set_Etype (N, Etype (N2));
13853 else
13854 Set_Associated_Node (N, Empty);
13855 Set_Etype (N, Empty);
13856 end if;
13857
13858 elsif Nkind_In (N2, N_Integer_Literal,
13859 N_Real_Literal,
13860 N_String_Literal)
13861 then
13862 if Present (Original_Node (N2))
13863 and then Nkind (Original_Node (N2)) = Nkind (N)
13864 then
13865
13866 -- Operation was constant-folded. Whenever possible,
13867 -- recover semantic information from unfolded node,
13868 -- for ASIS use.
13869
13870 Set_Associated_Node (N, Original_Node (N2));
13871
13872 if Nkind (N) = N_Op_Concat then
13873 Set_Is_Component_Left_Opnd (N,
13874 Is_Component_Left_Opnd (Get_Associated_Node (N)));
13875 Set_Is_Component_Right_Opnd (N,
13876 Is_Component_Right_Opnd (Get_Associated_Node (N)));
13877 end if;
13878
13879 Reset_Entity (N);
13880
13881 else
13882 -- If original node is already modified, propagate
13883 -- constant-folding to template.
13884
13885 Rewrite (N, New_Copy (N2));
13886 Set_Analyzed (N, False);
13887 end if;
13888
13889 elsif Nkind (N2) = N_Identifier
13890 and then Ekind (Entity (N2)) = E_Enumeration_Literal
13891 then
13892 -- Same if call was folded into a literal, but in this case
13893 -- retain the entity to avoid spurious ambiguities if it is
13894 -- overloaded at the point of instantiation or inlining.
13895
13896 Rewrite (N, New_Copy (N2));
13897 Set_Analyzed (N, False);
13898 end if;
13899 end if;
13900
13901 -- Complete operands check if node has not been constant-folded
13902
13903 if Nkind (N) in N_Op then
13904 Save_Entity_Descendants (N);
13905 end if;
13906
13907 elsif Nkind (N) = N_Identifier then
13908 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13909
13910 -- If this is a discriminant reference, always save it. It is
13911 -- used in the instance to find the corresponding discriminant
13912 -- positionally rather than by name.
13913
13914 Set_Original_Discriminant
13915 (N, Original_Discriminant (Get_Associated_Node (N)));
13916 Reset_Entity (N);
13917
13918 else
13919 N2 := Get_Associated_Node (N);
13920
13921 if Nkind (N2) = N_Function_Call then
13922 E := Entity (Name (N2));
13923
13924 -- Name resolves to a call to parameterless function. If
13925 -- original entity is global, mark node as resolved.
13926
13927 if Present (E)
13928 and then Is_Global (E)
13929 then
13930 Set_Etype (N, Etype (N2));
13931 else
13932 Set_Associated_Node (N, Empty);
13933 Set_Etype (N, Empty);
13934 end if;
13935
13936 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
13937 and then Is_Entity_Name (Original_Node (N2))
13938 then
13939 -- Name resolves to named number that is constant-folded,
13940 -- We must preserve the original name for ASIS use, and
13941 -- undo the constant-folding, which will be repeated in
13942 -- each instance.
13943
13944 Set_Associated_Node (N, Original_Node (N2));
13945 Reset_Entity (N);
13946
13947 elsif Nkind (N2) = N_String_Literal then
13948
13949 -- Name resolves to string literal. Perform the same
13950 -- replacement in generic.
13951
13952 Rewrite (N, New_Copy (N2));
13953
13954 elsif Nkind (N2) = N_Explicit_Dereference then
13955
13956 -- An identifier is rewritten as a dereference if it is the
13957 -- prefix in an implicit dereference (call or attribute).
13958 -- The analysis of an instantiation will expand the node
13959 -- again, so we preserve the original tree but link it to
13960 -- the resolved entity in case it is global.
13961
13962 if Is_Entity_Name (Prefix (N2))
13963 and then Present (Entity (Prefix (N2)))
13964 and then Is_Global (Entity (Prefix (N2)))
13965 then
13966 Set_Associated_Node (N, Prefix (N2));
13967
13968 elsif Nkind (Prefix (N2)) = N_Function_Call
13969 and then Is_Global (Entity (Name (Prefix (N2))))
13970 then
13971 Rewrite (N,
13972 Make_Explicit_Dereference (Loc,
13973 Prefix => Make_Function_Call (Loc,
13974 Name =>
13975 New_Occurrence_Of (Entity (Name (Prefix (N2))),
13976 Loc))));
13977
13978 else
13979 Set_Associated_Node (N, Empty);
13980 Set_Etype (N, Empty);
13981 end if;
13982
13983 -- The subtype mark of a nominally unconstrained object is
13984 -- rewritten as a subtype indication using the bounds of the
13985 -- expression. Recover the original subtype mark.
13986
13987 elsif Nkind (N2) = N_Subtype_Indication
13988 and then Is_Entity_Name (Original_Node (N2))
13989 then
13990 Set_Associated_Node (N, Original_Node (N2));
13991 Reset_Entity (N);
13992
13993 else
13994 null;
13995 end if;
13996 end if;
13997
13998 elsif Nkind (N) in N_Entity then
13999 null;
14000
14001 else
14002 declare
14003 Qual : Node_Id := Empty;
14004 Typ : Entity_Id := Empty;
14005 Nam : Node_Id;
14006
14007 use Atree.Unchecked_Access;
14008 -- This code section is part of implementing an untyped tree
14009 -- traversal, so it needs direct access to node fields.
14010
14011 begin
14012 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14013 N2 := Get_Associated_Node (N);
14014
14015 if No (N2) then
14016 Typ := Empty;
14017 else
14018 Typ := Etype (N2);
14019
14020 -- In an instance within a generic, use the name of the
14021 -- actual and not the original generic parameter. If the
14022 -- actual is global in the current generic it must be
14023 -- preserved for its instantiation.
14024
14025 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14026 and then
14027 Present (Generic_Parent_Type (Parent (Typ)))
14028 then
14029 Typ := Base_Type (Typ);
14030 Set_Etype (N2, Typ);
14031 end if;
14032 end if;
14033
14034 if No (N2)
14035 or else No (Typ)
14036 or else not Is_Global (Typ)
14037 then
14038 Set_Associated_Node (N, Empty);
14039
14040 -- If the aggregate is an actual in a call, it has been
14041 -- resolved in the current context, to some local type.
14042 -- The enclosing call may have been disambiguated by the
14043 -- aggregate, and this disambiguation might fail at
14044 -- instantiation time because the type to which the
14045 -- aggregate did resolve is not preserved. In order to
14046 -- preserve some of this information, we wrap the
14047 -- aggregate in a qualified expression, using the id of
14048 -- its type. For further disambiguation we qualify the
14049 -- type name with its scope (if visible) because both
14050 -- id's will have corresponding entities in an instance.
14051 -- This resolves most of the problems with missing type
14052 -- information on aggregates in instances.
14053
14054 if Nkind (N2) = Nkind (N)
14055 and then Nkind (Parent (N2)) in N_Subprogram_Call
14056 and then Comes_From_Source (Typ)
14057 then
14058 if Is_Immediately_Visible (Scope (Typ)) then
14059 Nam := Make_Selected_Component (Loc,
14060 Prefix =>
14061 Make_Identifier (Loc, Chars (Scope (Typ))),
14062 Selector_Name =>
14063 Make_Identifier (Loc, Chars (Typ)));
14064 else
14065 Nam := Make_Identifier (Loc, Chars (Typ));
14066 end if;
14067
14068 Qual :=
14069 Make_Qualified_Expression (Loc,
14070 Subtype_Mark => Nam,
14071 Expression => Relocate_Node (N));
14072 end if;
14073 end if;
14074
14075 Save_Global_Descendant (Field1 (N));
14076 Save_Global_Descendant (Field2 (N));
14077 Save_Global_Descendant (Field3 (N));
14078 Save_Global_Descendant (Field5 (N));
14079
14080 if Present (Qual) then
14081 Rewrite (N, Qual);
14082 end if;
14083
14084 -- All other cases than aggregates
14085
14086 else
14087 Save_Global_Descendant (Field1 (N));
14088 Save_Global_Descendant (Field2 (N));
14089 Save_Global_Descendant (Field3 (N));
14090 Save_Global_Descendant (Field4 (N));
14091 Save_Global_Descendant (Field5 (N));
14092 end if;
14093 end;
14094 end if;
14095
14096 -- If a node has aspects, references within their expressions must
14097 -- be saved separately, given they are not directly in the tree.
14098
14099 if Has_Aspects (N) then
14100 declare
14101 Aspect : Node_Id;
14102
14103 begin
14104 Aspect := First (Aspect_Specifications (N));
14105 while Present (Aspect) loop
14106 if Present (Expression (Aspect)) then
14107 Save_Global_References (Expression (Aspect));
14108 end if;
14109
14110 Next (Aspect);
14111 end loop;
14112 end;
14113 end if;
14114 end Save_References;
14115
14116 -- Start of processing for Save_Global_References
14117
14118 begin
14119 Gen_Scope := Current_Scope;
14120
14121 -- If the generic unit is a child unit, references to entities in the
14122 -- parent are treated as local, because they will be resolved anew in
14123 -- the context of the instance of the parent.
14124
14125 while Is_Child_Unit (Gen_Scope)
14126 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14127 loop
14128 Gen_Scope := Scope (Gen_Scope);
14129 end loop;
14130
14131 Save_References (N);
14132 end Save_Global_References;
14133
14134 --------------------------------------
14135 -- Set_Copied_Sloc_For_Inlined_Body --
14136 --------------------------------------
14137
14138 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14139 begin
14140 Create_Instantiation_Source (N, E, True, S_Adjustment);
14141 end Set_Copied_Sloc_For_Inlined_Body;
14142
14143 ---------------------
14144 -- Set_Instance_Of --
14145 ---------------------
14146
14147 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14148 begin
14149 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14150 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14151 Generic_Renamings.Increment_Last;
14152 end Set_Instance_Of;
14153
14154 --------------------
14155 -- Set_Next_Assoc --
14156 --------------------
14157
14158 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14159 begin
14160 Generic_Renamings.Table (E).Next_In_HTable := Next;
14161 end Set_Next_Assoc;
14162
14163 -------------------
14164 -- Start_Generic --
14165 -------------------
14166
14167 procedure Start_Generic is
14168 begin
14169 -- ??? More things could be factored out in this routine.
14170 -- Should probably be done at a later stage.
14171
14172 Generic_Flags.Append (Inside_A_Generic);
14173 Inside_A_Generic := True;
14174
14175 Expander_Mode_Save_And_Set (False);
14176 end Start_Generic;
14177
14178 ----------------------
14179 -- Set_Instance_Env --
14180 ----------------------
14181
14182 procedure Set_Instance_Env
14183 (Gen_Unit : Entity_Id;
14184 Act_Unit : Entity_Id)
14185 is
14186 Assertion_Status : constant Boolean := Assertions_Enabled;
14187 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14188 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14189
14190 begin
14191 -- Regardless of the current mode, predefined units are analyzed in the
14192 -- most current Ada mode, and earlier version Ada checks do not apply
14193 -- to predefined units. Nothing needs to be done for non-internal units.
14194 -- These are always analyzed in the current mode.
14195
14196 if Is_Internal_File_Name
14197 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14198 Renamings_Included => True)
14199 then
14200 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14201
14202 -- In Ada2012 we may want to enable assertions in an instance of a
14203 -- predefined unit, in which case we need to preserve the current
14204 -- setting for the Assertions_Enabled flag. This will become more
14205 -- critical when pre/postconditions are added to predefined units,
14206 -- as is already the case for some numeric libraries.
14207
14208 if Ada_Version >= Ada_2012 then
14209 Assertions_Enabled := Assertion_Status;
14210 end if;
14211
14212 -- SPARK_Mode for an instance is the one applicable at the point of
14213 -- instantiation.
14214
14215 SPARK_Mode := Save_SPARK_Mode;
14216 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14217 end if;
14218
14219 Current_Instantiated_Parent :=
14220 (Gen_Id => Gen_Unit,
14221 Act_Id => Act_Unit,
14222 Next_In_HTable => Assoc_Null);
14223 end Set_Instance_Env;
14224
14225 -----------------
14226 -- Switch_View --
14227 -----------------
14228
14229 procedure Switch_View (T : Entity_Id) is
14230 BT : constant Entity_Id := Base_Type (T);
14231 Priv_Elmt : Elmt_Id := No_Elmt;
14232 Priv_Sub : Entity_Id;
14233
14234 begin
14235 -- T may be private but its base type may have been exchanged through
14236 -- some other occurrence, in which case there is nothing to switch
14237 -- besides T itself. Note that a private dependent subtype of a private
14238 -- type might not have been switched even if the base type has been,
14239 -- because of the last branch of Check_Private_View (see comment there).
14240
14241 if not Is_Private_Type (BT) then
14242 Prepend_Elmt (Full_View (T), Exchanged_Views);
14243 Exchange_Declarations (T);
14244 return;
14245 end if;
14246
14247 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14248
14249 if Present (Full_View (BT)) then
14250 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14251 Exchange_Declarations (BT);
14252 end if;
14253
14254 while Present (Priv_Elmt) loop
14255 Priv_Sub := (Node (Priv_Elmt));
14256
14257 -- We avoid flipping the subtype if the Etype of its full view is
14258 -- private because this would result in a malformed subtype. This
14259 -- occurs when the Etype of the subtype full view is the full view of
14260 -- the base type (and since the base types were just switched, the
14261 -- subtype is pointing to the wrong view). This is currently the case
14262 -- for tagged record types, access types (maybe more?) and needs to
14263 -- be resolved. ???
14264
14265 if Present (Full_View (Priv_Sub))
14266 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14267 then
14268 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14269 Exchange_Declarations (Priv_Sub);
14270 end if;
14271
14272 Next_Elmt (Priv_Elmt);
14273 end loop;
14274 end Switch_View;
14275
14276 -----------------
14277 -- True_Parent --
14278 -----------------
14279
14280 function True_Parent (N : Node_Id) return Node_Id is
14281 begin
14282 if Nkind (Parent (N)) = N_Subunit then
14283 return Parent (Corresponding_Stub (Parent (N)));
14284 else
14285 return Parent (N);
14286 end if;
14287 end True_Parent;
14288
14289 -----------------------------
14290 -- Valid_Default_Attribute --
14291 -----------------------------
14292
14293 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
14294 Attr_Id : constant Attribute_Id :=
14295 Get_Attribute_Id (Attribute_Name (Def));
14296 T : constant Entity_Id := Entity (Prefix (Def));
14297 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
14298 F : Entity_Id;
14299 Num_F : Int;
14300 OK : Boolean;
14301
14302 begin
14303 if No (T)
14304 or else T = Any_Id
14305 then
14306 return;
14307 end if;
14308
14309 Num_F := 0;
14310 F := First_Formal (Nam);
14311 while Present (F) loop
14312 Num_F := Num_F + 1;
14313 Next_Formal (F);
14314 end loop;
14315
14316 case Attr_Id is
14317 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
14318 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
14319 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
14320 Attribute_Unbiased_Rounding =>
14321 OK := Is_Fun
14322 and then Num_F = 1
14323 and then Is_Floating_Point_Type (T);
14324
14325 when Attribute_Image | Attribute_Pred | Attribute_Succ |
14326 Attribute_Value | Attribute_Wide_Image |
14327 Attribute_Wide_Value =>
14328 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
14329
14330 when Attribute_Max | Attribute_Min =>
14331 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
14332
14333 when Attribute_Input =>
14334 OK := (Is_Fun and then Num_F = 1);
14335
14336 when Attribute_Output | Attribute_Read | Attribute_Write =>
14337 OK := (not Is_Fun and then Num_F = 2);
14338
14339 when others =>
14340 OK := False;
14341 end case;
14342
14343 if not OK then
14344 Error_Msg_N ("attribute reference has wrong profile for subprogram",
14345 Def);
14346 end if;
14347 end Valid_Default_Attribute;
14348
14349 end Sem_Ch12;