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