PR sanitizer/65081
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob0d698cffec7a178ea75fd12f7337471ec67b1187
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-2015, 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 Ghost; use Ghost;
37 with Itypes; use Itypes;
38 with Lib; use Lib;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Nlists; use Nlists;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Rident; use Rident;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinfo.CN; use Sinfo.CN;
70 with Sinput; use Sinput;
71 with Sinput.L; use Sinput.L;
72 with Snames; use Snames;
73 with Stringt; use Stringt;
74 with Uname; use Uname;
75 with Table;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
81 with GNAT.HTable;
83 package body Sem_Ch12 is
85 ----------------------------------------------------------
86 -- Implementation of Generic Analysis and Instantiation --
87 ----------------------------------------------------------
89 -- GNAT implements generics by macro expansion. No attempt is made to share
90 -- generic instantiations (for now). Analysis of a generic definition does
91 -- not perform any expansion action, but the expander must be called on the
92 -- tree for each instantiation, because the expansion may of course depend
93 -- on the generic actuals. All of this is best achieved as follows:
95 -- a) Semantic analysis of a generic unit is performed on a copy of the
96 -- tree for the generic unit. All tree modifications that follow analysis
97 -- do not affect the original tree. Links are kept between the original
98 -- tree and the copy, in order to recognize non-local references within
99 -- the generic, and propagate them to each instance (recall that name
100 -- resolution is done on the generic declaration: generics are not really
101 -- macros). This is summarized in the following diagram:
103 -- .-----------. .----------.
104 -- | semantic |<--------------| generic |
105 -- | copy | | unit |
106 -- | |==============>| |
107 -- |___________| global |__________|
108 -- references | | |
109 -- | | |
110 -- .-----|--|.
111 -- | .-----|---.
112 -- | | .----------.
113 -- | | | generic |
114 -- |__| | |
115 -- |__| instance |
116 -- |__________|
118 -- b) Each instantiation copies the original tree, and inserts into it a
119 -- series of declarations that describe the mapping between generic formals
120 -- and actuals. For example, a generic In OUT parameter is an object
121 -- renaming of the corresponding actual, etc. Generic IN parameters are
122 -- constant declarations.
124 -- c) In order to give the right visibility for these renamings, we use
125 -- a different scheme for package and subprogram instantiations. For
126 -- packages, the list of renamings is inserted into the package
127 -- specification, before the visible declarations of the package. The
128 -- renamings are analyzed before any of the text of the instance, and are
129 -- thus visible at the right place. Furthermore, outside of the instance,
130 -- the generic parameters are visible and denote their corresponding
131 -- actuals.
133 -- For subprograms, we create a container package to hold the renamings
134 -- and the subprogram instance itself. Analysis of the package makes the
135 -- renaming declarations visible to the subprogram. After analyzing the
136 -- package, the defining entity for the subprogram is touched-up so that
137 -- it appears declared in the current scope, and not inside the container
138 -- package.
140 -- If the instantiation is a compilation unit, the container package is
141 -- given the same name as the subprogram instance. This ensures that
142 -- the elaboration procedure called by the binder, using the compilation
143 -- unit name, calls in fact the elaboration procedure for the package.
145 -- Not surprisingly, private types complicate this approach. By saving in
146 -- the original generic object the non-local references, we guarantee that
147 -- the proper entities are referenced at the point of instantiation.
148 -- However, for private types, this by itself does not insure that the
149 -- proper VIEW of the entity is used (the full type may be visible at the
150 -- point of generic definition, but not at instantiation, or vice-versa).
151 -- In order to reference the proper view, we special-case any reference
152 -- to private types in the generic object, by saving both views, one in
153 -- the generic and one in the semantic copy. At time of instantiation, we
154 -- check whether the two views are consistent, and exchange declarations if
155 -- necessary, in order to restore the correct visibility. Similarly, if
156 -- the instance view is private when the generic view was not, we perform
157 -- the exchange. After completing the instantiation, we restore the
158 -- current visibility. The flag Has_Private_View marks identifiers in the
159 -- the generic unit that require checking.
161 -- Visibility within nested generic units requires special handling.
162 -- Consider the following scheme:
164 -- type Global is ... -- outside of generic unit.
165 -- generic ...
166 -- package Outer is
167 -- ...
168 -- type Semi_Global is ... -- global to inner.
170 -- generic ... -- 1
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
173 -- procedure in2 is new inner (...); -- 4
174 -- end Outer;
176 -- package New_Outer is new Outer (...); -- 2
177 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
179 -- The semantic analysis of Outer captures all occurrences of Global.
180 -- The semantic analysis of Inner (at 1) captures both occurrences of
181 -- Global and Semi_Global.
183 -- At point 2 (instantiation of Outer), we also produce a generic copy
184 -- of Inner, even though Inner is, at that point, not being instantiated.
185 -- (This is just part of the semantic analysis of New_Outer).
187 -- Critically, references to Global within Inner must be preserved, while
188 -- references to Semi_Global should not preserved, because they must now
189 -- resolve to an entity within New_Outer. To distinguish between these, we
190 -- use a global variable, Current_Instantiated_Parent, which is set when
191 -- performing a generic copy during instantiation (at 2). This variable is
192 -- used when performing a generic copy that is not an instantiation, but
193 -- that is nested within one, as the occurrence of 1 within 2. The analysis
194 -- of a nested generic only preserves references that are global to the
195 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
196 -- determine whether a reference is external to the given parent.
198 -- The instantiation at point 3 requires no special treatment. The method
199 -- works as well for further nestings of generic units, but of course the
200 -- variable Current_Instantiated_Parent must be stacked because nested
201 -- instantiations can occur, e.g. the occurrence of 4 within 2.
203 -- The instantiation of package and subprogram bodies is handled in a
204 -- similar manner, except that it is delayed until after semantic
205 -- analysis is complete. In this fashion complex cross-dependencies
206 -- between several package declarations and bodies containing generics
207 -- can be compiled which otherwise would diagnose spurious circularities.
209 -- For example, it is possible to compile two packages A and B that
210 -- have the following structure:
212 -- package A is package B is
213 -- generic ... generic ...
214 -- package G_A is package G_B is
216 -- with B; with A;
217 -- package body A is package body B is
218 -- package N_B is new G_B (..) package N_A is new G_A (..)
220 -- The table Pending_Instantiations in package Inline is used to keep
221 -- track of body instantiations that are delayed in this manner. Inline
222 -- handles the actual calls to do the body instantiations. This activity
223 -- is part of Inline, since the processing occurs at the same point, and
224 -- for essentially the same reason, as the handling of inlined routines.
226 ----------------------------------------------
227 -- Detection of Instantiation Circularities --
228 ----------------------------------------------
230 -- If we have a chain of instantiations that is circular, this is static
231 -- error which must be detected at compile time. The detection of these
232 -- circularities is carried out at the point that we insert a generic
233 -- instance spec or body. If there is a circularity, then the analysis of
234 -- the offending spec or body will eventually result in trying to load the
235 -- same unit again, and we detect this problem as we analyze the package
236 -- instantiation for the second time.
238 -- At least in some cases after we have detected the circularity, we get
239 -- into trouble if we try to keep going. The following flag is set if a
240 -- circularity is detected, and used to abandon compilation after the
241 -- messages have been posted.
243 Circularity_Detected : Boolean := False;
244 -- This should really be reset on encountering a new main unit, but in
245 -- practice we are not using multiple main units so it is not critical.
247 --------------------------------------------------
248 -- Formal packages and partial parameterization --
249 --------------------------------------------------
251 -- When compiling a generic, a formal package is a local instantiation. If
252 -- declared with a box, its generic formals are visible in the enclosing
253 -- generic. If declared with a partial list of actuals, those actuals that
254 -- are defaulted (covered by an Others clause, or given an explicit box
255 -- initialization) are also visible in the enclosing generic, while those
256 -- that have a corresponding actual are not.
258 -- In our source model of instantiation, the same visibility must be
259 -- present in the spec and body of an instance: the names of the formals
260 -- that are defaulted must be made visible within the instance, and made
261 -- invisible (hidden) after the instantiation is complete, so that they
262 -- are not accessible outside of the instance.
264 -- In a generic, a formal package is treated like a special instantiation.
265 -- Our Ada 95 compiler handled formals with and without box in different
266 -- ways. With partial parameterization, we use a single model for both.
267 -- We create a package declaration that consists of the specification of
268 -- the generic package, and a set of declarations that map the actuals
269 -- into local renamings, just as we do for bona fide instantiations. For
270 -- defaulted parameters and formals with a box, we copy directly the
271 -- declarations of the formal into this local package. The result is a
272 -- a package whose visible declarations may include generic formals. This
273 -- package is only used for type checking and visibility analysis, and
274 -- never reaches the back-end, so it can freely violate the placement
275 -- rules for generic formal declarations.
277 -- The list of declarations (renamings and copies of formals) is built
278 -- by Analyze_Associations, just as for regular instantiations.
280 -- At the point of instantiation, conformance checking must be applied only
281 -- to those parameters that were specified in the formal. We perform this
282 -- checking by creating another internal instantiation, this one including
283 -- only the renamings and the formals (the rest of the package spec is not
284 -- relevant to conformance checking). We can then traverse two lists: the
285 -- list of actuals in the instance that corresponds to the formal package,
286 -- and the list of actuals produced for this bogus instantiation. We apply
287 -- the conformance rules to those actuals that are not defaulted (i.e.
288 -- which still appear as generic formals.
290 -- When we compile an instance body we must make the right parameters
291 -- visible again. The predicate Is_Generic_Formal indicates which of the
292 -- formals should have its Is_Hidden flag reset.
294 -----------------------
295 -- Local subprograms --
296 -----------------------
298 procedure Abandon_Instantiation (N : Node_Id);
299 pragma No_Return (Abandon_Instantiation);
300 -- Posts an error message "instantiation abandoned" at the indicated node
301 -- and then raises the exception Instantiation_Error to do it.
303 procedure Analyze_Formal_Array_Type
304 (T : in out Entity_Id;
305 Def : Node_Id);
306 -- A formal array type is treated like an array type declaration, and
307 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
308 -- in-out, because in the case of an anonymous type the entity is
309 -- actually created in the procedure.
311 -- The following procedures treat other kinds of formal parameters
313 procedure Analyze_Formal_Derived_Interface_Type
314 (N : Node_Id;
315 T : Entity_Id;
316 Def : Node_Id);
318 procedure Analyze_Formal_Derived_Type
319 (N : Node_Id;
320 T : Entity_Id;
321 Def : Node_Id);
323 procedure Analyze_Formal_Interface_Type
324 (N : Node_Id;
325 T : Entity_Id;
326 Def : Node_Id);
328 -- The following subprograms create abbreviated declarations for formal
329 -- scalar types. We introduce an anonymous base of the proper class for
330 -- each of them, and define the formals as constrained first subtypes of
331 -- their bases. The bounds are expressions that are non-static in the
332 -- generic.
334 procedure Analyze_Formal_Decimal_Fixed_Point_Type
335 (T : Entity_Id; Def : Node_Id);
336 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
337 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
339 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
340 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
341 (T : Entity_Id; Def : Node_Id);
343 procedure Analyze_Formal_Private_Type
344 (N : Node_Id;
345 T : Entity_Id;
346 Def : Node_Id);
347 -- Creates a new private type, which does not require completion
349 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
350 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
352 procedure Analyze_Generic_Formal_Part (N : Node_Id);
353 -- Analyze generic formal part
355 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
356 -- Create a new access type with the given designated type
358 function Analyze_Associations
359 (I_Node : Node_Id;
360 Formals : List_Id;
361 F_Copy : List_Id) return List_Id;
362 -- At instantiation time, build the list of associations between formals
363 -- and actuals. Each association becomes a renaming declaration for the
364 -- formal entity. F_Copy is the analyzed list of formals in the generic
365 -- copy. It is used to apply legality checks to the actuals. I_Node is the
366 -- instantiation node itself.
368 procedure Analyze_Subprogram_Instantiation
369 (N : Node_Id;
370 K : Entity_Kind);
372 procedure Build_Instance_Compilation_Unit_Nodes
373 (N : Node_Id;
374 Act_Body : Node_Id;
375 Act_Decl : Node_Id);
376 -- This procedure is used in the case where the generic instance of a
377 -- subprogram body or package body is a library unit. In this case, the
378 -- original library unit node for the generic instantiation must be
379 -- replaced by the resulting generic body, and a link made to a new
380 -- compilation unit node for the generic declaration. The argument N is
381 -- the original generic instantiation. Act_Body and Act_Decl are the body
382 -- and declaration of the instance (either package body and declaration
383 -- nodes or subprogram body and declaration nodes depending on the case).
384 -- On return, the node N has been rewritten with the actual body.
386 procedure Check_Access_Definition (N : Node_Id);
387 -- Subsidiary routine to null exclusion processing. Perform an assertion
388 -- check on Ada version and the presence of an access definition in N.
390 procedure Check_Formal_Packages (P_Id : Entity_Id);
391 -- Apply the following to all formal packages in generic associations
393 procedure Check_Formal_Package_Instance
394 (Formal_Pack : Entity_Id;
395 Actual_Pack : Entity_Id);
396 -- Verify that the actuals of the actual instance match the actuals of
397 -- the template for a formal package that is not declared with a box.
399 procedure Check_Forward_Instantiation (Decl : Node_Id);
400 -- If the generic is a local entity and the corresponding body has not
401 -- been seen yet, flag enclosing packages to indicate that it will be
402 -- elaborated after the generic body. Subprograms declared in the same
403 -- package cannot be inlined by the front-end because front-end inlining
404 -- requires a strict linear order of elaboration.
406 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
407 -- Check if some association between formals and actuals requires to make
408 -- visible primitives of a tagged type, and make those primitives visible.
409 -- Return the list of primitives whose visibility is modified (to restore
410 -- their visibility later through Restore_Hidden_Primitives). If no
411 -- candidate is found then return No_Elist.
413 procedure Check_Hidden_Child_Unit
414 (N : Node_Id;
415 Gen_Unit : Entity_Id;
416 Act_Decl_Id : Entity_Id);
417 -- If the generic unit is an implicit child instance within a parent
418 -- instance, we need to make an explicit test that it is not hidden by
419 -- a child instance of the same name and parent.
421 procedure Check_Generic_Actuals
422 (Instance : Entity_Id;
423 Is_Formal_Box : Boolean);
424 -- Similar to previous one. Check the actuals in the instantiation,
425 -- whose views can change between the point of instantiation and the point
426 -- of instantiation of the body. In addition, mark the generic renamings
427 -- as generic actuals, so that they are not compatible with other actuals.
428 -- Recurse on an actual that is a formal package whose declaration has
429 -- a box.
431 function Contains_Instance_Of
432 (Inner : Entity_Id;
433 Outer : Entity_Id;
434 N : Node_Id) return Boolean;
435 -- Inner is instantiated within the generic Outer. Check whether Inner
436 -- directly or indirectly contains an instance of Outer or of one of its
437 -- parents, in the case of a subunit. Each generic unit holds a list of
438 -- the entities instantiated within (at any depth). This procedure
439 -- determines whether the set of such lists contains a cycle, i.e. an
440 -- illegal circular instantiation.
442 function Denotes_Formal_Package
443 (Pack : Entity_Id;
444 On_Exit : Boolean := False;
445 Instance : Entity_Id := Empty) return Boolean;
446 -- Returns True if E is a formal package of an enclosing generic, or
447 -- the actual for such a formal in an enclosing instantiation. If such
448 -- a package is used as a formal in an nested generic, or as an actual
449 -- in a nested instantiation, the visibility of ITS formals should not
450 -- be modified. When called from within Restore_Private_Views, the flag
451 -- On_Exit is true, to indicate that the search for a possible enclosing
452 -- instance should ignore the current one. In that case Instance denotes
453 -- the declaration for which this is an actual. This declaration may be
454 -- an instantiation in the source, or the internal instantiation that
455 -- corresponds to the actual for a formal package.
457 function Earlier (N1, N2 : Node_Id) return Boolean;
458 -- Yields True if N1 and N2 appear in the same compilation unit,
459 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
460 -- traversal of the tree for the unit. Used to determine the placement
461 -- of freeze nodes for instance bodies that may depend on other instances.
463 function Find_Actual_Type
464 (Typ : Entity_Id;
465 Gen_Type : Entity_Id) return Entity_Id;
466 -- When validating the actual types of a child instance, check whether
467 -- the formal is a formal type of the parent unit, and retrieve the current
468 -- actual for it. Typ is the entity in the analyzed formal type declaration
469 -- (component or index type of an array type, or designated type of an
470 -- access formal) and Gen_Type is the enclosing analyzed formal array
471 -- or access type. The desired actual may be a formal of a parent, or may
472 -- be declared in a formal package of a parent. In both cases it is a
473 -- generic actual type because it appears within a visible instance.
474 -- Finally, it may be declared in a parent unit without being a formal
475 -- of that unit, in which case it must be retrieved by visibility.
476 -- Ambiguities may still arise if two homonyms are declared in two formal
477 -- packages, and the prefix of the formal type may be needed to resolve
478 -- the ambiguity in the instance ???
480 function In_Same_Declarative_Part
481 (F_Node : Node_Id;
482 Inst : Node_Id) return Boolean;
483 -- True if the instantiation Inst and the given freeze_node F_Node appear
484 -- within the same declarative part, ignoring subunits, but with no inter-
485 -- vening subprograms or concurrent units. Used to find the proper plave
486 -- for the freeze node of an instance, when the generic is declared in a
487 -- previous instance. If predicate is true, the freeze node of the instance
488 -- can be placed after the freeze node of the previous instance, Otherwise
489 -- it has to be placed at the end of the current declarative part.
491 function In_Main_Context (E : Entity_Id) return Boolean;
492 -- Check whether an instantiation is in the context of the main unit.
493 -- Used to determine whether its body should be elaborated to allow
494 -- front-end inlining.
496 procedure Set_Instance_Env
497 (Gen_Unit : Entity_Id;
498 Act_Unit : Entity_Id);
499 -- Save current instance on saved environment, to be used to determine
500 -- the global status of entities in nested instances. Part of Save_Env.
501 -- called after verifying that the generic unit is legal for the instance,
502 -- The procedure also examines whether the generic unit is a predefined
503 -- unit, in order to set configuration switches accordingly. As a result
504 -- the procedure must be called after analyzing and freezing the actuals.
506 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
507 -- Associate analyzed generic parameter with corresponding
508 -- instance. Used for semantic checks at instantiation time.
510 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
511 -- Traverse the Exchanged_Views list to see if a type was private
512 -- and has already been flipped during this phase of instantiation.
514 procedure Hide_Current_Scope;
515 -- When instantiating a generic child unit, the parent context must be
516 -- present, but the instance and all entities that may be generated
517 -- must be inserted in the current scope. We leave the current scope
518 -- on the stack, but make its entities invisible to avoid visibility
519 -- problems. This is reversed at the end of the instantiation. This is
520 -- not done for the instantiation of the bodies, which only require the
521 -- instances of the generic parents to be in scope.
523 procedure Install_Body
524 (Act_Body : Node_Id;
525 N : Node_Id;
526 Gen_Body : Node_Id;
527 Gen_Decl : Node_Id);
528 -- If the instantiation happens textually before the body of the generic,
529 -- the instantiation of the body must be analyzed after the generic body,
530 -- and not at the point of instantiation. Such early instantiations can
531 -- happen if the generic and the instance appear in a package declaration
532 -- because the generic body can only appear in the corresponding package
533 -- body. Early instantiations can also appear if generic, instance and
534 -- body are all in the declarative part of a subprogram or entry. Entities
535 -- of packages that are early instantiations are delayed, and their freeze
536 -- node appears after the generic body.
538 procedure Insert_Freeze_Node_For_Instance
539 (N : Node_Id;
540 F_Node : Node_Id);
541 -- N denotes a package or a subprogram instantiation and F_Node is the
542 -- associated freeze node. Insert the freeze node before the first source
543 -- body which follows immediately after N. If no such body is found, the
544 -- freeze node is inserted at the end of the declarative region which
545 -- contains N.
547 procedure Freeze_Subprogram_Body
548 (Inst_Node : Node_Id;
549 Gen_Body : Node_Id;
550 Pack_Id : Entity_Id);
551 -- The generic body may appear textually after the instance, including
552 -- in the proper body of a stub, or within a different package instance.
553 -- Given that the instance can only be elaborated after the generic, we
554 -- place freeze_nodes for the instance and/or for packages that may enclose
555 -- the instance and the generic, so that the back-end can establish the
556 -- proper order of elaboration.
558 procedure Init_Env;
559 -- Establish environment for subsequent instantiation. Separated from
560 -- Save_Env because data-structures for visibility handling must be
561 -- initialized before call to Check_Generic_Child_Unit.
563 procedure Install_Formal_Packages (Par : Entity_Id);
564 -- Install the visible part of any formal of the parent that is a formal
565 -- package. Note that for the case of a formal package with a box, this
566 -- includes the formal part of the formal package (12.7(10/2)).
568 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
569 -- When compiling an instance of a child unit the parent (which is
570 -- itself an instance) is an enclosing scope that must be made
571 -- immediately visible. This procedure is also used to install the non-
572 -- generic parent of a generic child unit when compiling its body, so
573 -- that full views of types in the parent are made visible.
575 procedure Remove_Parent (In_Body : Boolean := False);
576 -- Reverse effect after instantiation of child is complete
578 procedure Install_Hidden_Primitives
579 (Prims_List : in out Elist_Id;
580 Gen_T : Entity_Id;
581 Act_T : Entity_Id);
582 -- Remove suffix 'P' from hidden primitives of Act_T to match the
583 -- visibility of primitives of Gen_T. The list of primitives to which
584 -- the suffix is removed is added to Prims_List to restore them later.
586 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
587 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
588 -- set to No_Elist.
590 procedure Inline_Instance_Body
591 (N : Node_Id;
592 Gen_Unit : Entity_Id;
593 Act_Decl : Node_Id);
594 -- If front-end inlining is requested, instantiate the package body,
595 -- and preserve the visibility of its compilation unit, to insure
596 -- that successive instantiations succeed.
598 -- The functions Instantiate_XXX perform various legality checks and build
599 -- the declarations for instantiated generic parameters. In all of these
600 -- Formal is the entity in the generic unit, Actual is the entity of
601 -- expression in the generic associations, and Analyzed_Formal is the
602 -- formal in the generic copy, which contains the semantic information to
603 -- be used to validate the actual.
605 function Instantiate_Object
606 (Formal : Node_Id;
607 Actual : Node_Id;
608 Analyzed_Formal : Node_Id) return List_Id;
610 function Instantiate_Type
611 (Formal : Node_Id;
612 Actual : Node_Id;
613 Analyzed_Formal : Node_Id;
614 Actual_Decls : List_Id) return List_Id;
616 function Instantiate_Formal_Subprogram
617 (Formal : Node_Id;
618 Actual : Node_Id;
619 Analyzed_Formal : Node_Id) return Node_Id;
621 function Instantiate_Formal_Package
622 (Formal : Node_Id;
623 Actual : Node_Id;
624 Analyzed_Formal : Node_Id) return List_Id;
625 -- If the formal package is declared with a box, special visibility rules
626 -- apply to its formals: they are in the visible part of the package. This
627 -- is true in the declarative region of the formal package, that is to say
628 -- in the enclosing generic or instantiation. For an instantiation, the
629 -- parameters of the formal package are made visible in an explicit step.
630 -- Furthermore, if the actual has a visible USE clause, these formals must
631 -- be made potentially use-visible as well. On exit from the enclosing
632 -- instantiation, the reverse must be done.
634 -- For a formal package declared without a box, there are conformance rules
635 -- that apply to the actuals in the generic declaration and the actuals of
636 -- the actual package in the enclosing instantiation. The simplest way to
637 -- apply these rules is to repeat the instantiation of the formal package
638 -- in the context of the enclosing instance, and compare the generic
639 -- associations of this instantiation with those of the actual package.
640 -- This internal instantiation only needs to contain the renamings of the
641 -- formals: the visible and private declarations themselves need not be
642 -- created.
644 -- In Ada 2005, the formal package may be only partially parameterized.
645 -- In that case the visibility step must make visible those actuals whose
646 -- corresponding formals were given with a box. A final complication
647 -- involves inherited operations from formal derived types, which must
648 -- be visible if the type is.
650 function Is_In_Main_Unit (N : Node_Id) return Boolean;
651 -- Test if given node is in the main unit
653 procedure Load_Parent_Of_Generic
654 (N : Node_Id;
655 Spec : Node_Id;
656 Body_Optional : Boolean := False);
657 -- If the generic appears in a separate non-generic library unit, load the
658 -- corresponding body to retrieve the body of the generic. N is the node
659 -- for the generic instantiation, Spec is the generic package declaration.
661 -- Body_Optional is a flag that indicates that the body is being loaded to
662 -- ensure that temporaries are generated consistently when there are other
663 -- instances in the current declarative part that precede the one being
664 -- loaded. In that case a missing body is acceptable.
666 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
667 -- Add the context clause of the unit containing a generic unit to a
668 -- compilation unit that is, or contains, an instantiation.
670 function Get_Associated_Node (N : Node_Id) return Node_Id;
671 -- In order to propagate semantic information back from the analyzed copy
672 -- to the original generic, we maintain links between selected nodes in the
673 -- generic and their corresponding copies. At the end of generic analysis,
674 -- the routine Save_Global_References traverses the generic tree, examines
675 -- the semantic information, and preserves the links to those nodes that
676 -- contain global information. At instantiation, the information from the
677 -- associated node is placed on the new copy, so that name resolution is
678 -- not repeated.
680 -- Three kinds of source nodes have associated nodes:
682 -- a) those that can reference (denote) entities, that is identifiers,
683 -- character literals, expanded_names, operator symbols, operators,
684 -- and attribute reference nodes. These nodes have an Entity field
685 -- and are the set of nodes that are in N_Has_Entity.
687 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
689 -- c) selected components (N_Selected_Component)
691 -- For the first class, the associated node preserves the entity if it is
692 -- global. If the generic contains nested instantiations, the associated
693 -- node itself has been recopied, and a chain of them must be followed.
695 -- For aggregates, the associated node allows retrieval of the type, which
696 -- may otherwise not appear in the generic. The view of this type may be
697 -- different between generic and instantiation, and the full view can be
698 -- installed before the instantiation is analyzed. For aggregates of type
699 -- extensions, the same view exchange may have to be performed for some of
700 -- the ancestor types, if their view is private at the point of
701 -- instantiation.
703 -- Nodes that are selected components in the parse tree may be rewritten
704 -- as expanded names after resolution, and must be treated as potential
705 -- entity holders, which is why they also have an Associated_Node.
707 -- Nodes that do not come from source, such as freeze nodes, do not appear
708 -- in the generic tree, and need not have an associated node.
710 -- The associated node is stored in the Associated_Node field. Note that
711 -- this field overlaps Entity, which is fine, because the whole point is
712 -- that we don't need or want the normal Entity field in this situation.
714 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
715 -- Within the generic part, entities in the formal package are
716 -- visible. To validate subsequent type declarations, indicate
717 -- the correspondence between the entities in the analyzed formal,
718 -- and the entities in the actual package. There are three packages
719 -- involved in the instantiation of a formal package: the parent
720 -- generic P1 which appears in the generic declaration, the fake
721 -- instantiation P2 which appears in the analyzed generic, and whose
722 -- visible entities may be used in subsequent formals, and the actual
723 -- P3 in the instance. To validate subsequent formals, me indicate
724 -- that the entities in P2 are mapped into those of P3. The mapping of
725 -- entities has to be done recursively for nested packages.
727 procedure Move_Freeze_Nodes
728 (Out_Of : Entity_Id;
729 After : Node_Id;
730 L : List_Id);
731 -- Freeze nodes can be generated in the analysis of a generic unit, but
732 -- will not be seen by the back-end. It is necessary to move those nodes
733 -- to the enclosing scope if they freeze an outer entity. We place them
734 -- at the end of the enclosing generic package, which is semantically
735 -- neutral.
737 procedure Preanalyze_Actuals (N : Node_Id);
738 -- Analyze actuals to perform name resolution. Full resolution is done
739 -- later, when the expected types are known, but names have to be captured
740 -- before installing parents of generics, that are not visible for the
741 -- actuals themselves.
743 function True_Parent (N : Node_Id) return Node_Id;
744 -- For a subunit, return parent of corresponding stub, else return
745 -- parent of node.
747 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
748 -- Verify that an attribute that appears as the default for a formal
749 -- subprogram is a function or procedure with the correct profile.
751 -------------------------------------------
752 -- Data Structures for Generic Renamings --
753 -------------------------------------------
755 -- The map Generic_Renamings associates generic entities with their
756 -- corresponding actuals. Currently used to validate type instances. It
757 -- will eventually be used for all generic parameters to eliminate the
758 -- need for overload resolution in the instance.
760 type Assoc_Ptr is new Int;
762 Assoc_Null : constant Assoc_Ptr := -1;
764 type Assoc is record
765 Gen_Id : Entity_Id;
766 Act_Id : Entity_Id;
767 Next_In_HTable : Assoc_Ptr;
768 end record;
770 package Generic_Renamings is new Table.Table
771 (Table_Component_Type => Assoc,
772 Table_Index_Type => Assoc_Ptr,
773 Table_Low_Bound => 0,
774 Table_Initial => 10,
775 Table_Increment => 100,
776 Table_Name => "Generic_Renamings");
778 -- Variable to hold enclosing instantiation. When the environment is
779 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
781 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
783 -- Hash table for associations
785 HTable_Size : constant := 37;
786 type HTable_Range is range 0 .. HTable_Size - 1;
788 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
789 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
790 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
791 function Hash (F : Entity_Id) return HTable_Range;
793 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
794 Header_Num => HTable_Range,
795 Element => Assoc,
796 Elmt_Ptr => Assoc_Ptr,
797 Null_Ptr => Assoc_Null,
798 Set_Next => Set_Next_Assoc,
799 Next => Next_Assoc,
800 Key => Entity_Id,
801 Get_Key => Get_Gen_Id,
802 Hash => Hash,
803 Equal => "=");
805 Exchanged_Views : Elist_Id;
806 -- This list holds the private views that have been exchanged during
807 -- instantiation to restore the visibility of the generic declaration.
808 -- (see comments above). After instantiation, the current visibility is
809 -- reestablished by means of a traversal of this list.
811 Hidden_Entities : Elist_Id;
812 -- This list holds the entities of the current scope that are removed
813 -- from immediate visibility when instantiating a child unit. Their
814 -- visibility is restored in Remove_Parent.
816 -- Because instantiations can be recursive, the following must be saved
817 -- on entry and restored on exit from an instantiation (spec or body).
818 -- This is done by the two procedures Save_Env and Restore_Env. For
819 -- package and subprogram instantiations (but not for the body instances)
820 -- the action of Save_Env is done in two steps: Init_Env is called before
821 -- Check_Generic_Child_Unit, because setting the parent instances requires
822 -- that the visibility data structures be properly initialized. Once the
823 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
825 Parent_Unit_Visible : Boolean := False;
826 -- Parent_Unit_Visible is used when the generic is a child unit, and
827 -- indicates whether the ultimate parent of the generic is visible in the
828 -- instantiation environment. It is used to reset the visibility of the
829 -- parent at the end of the instantiation (see Remove_Parent).
831 Instance_Parent_Unit : Entity_Id := Empty;
832 -- This records the ultimate parent unit of an instance of a generic
833 -- child unit and is used in conjunction with Parent_Unit_Visible to
834 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
836 type Instance_Env is record
837 Instantiated_Parent : Assoc;
838 Exchanged_Views : Elist_Id;
839 Hidden_Entities : Elist_Id;
840 Current_Sem_Unit : Unit_Number_Type;
841 Parent_Unit_Visible : Boolean := False;
842 Instance_Parent_Unit : Entity_Id := Empty;
843 Switches : Config_Switches_Type;
844 end record;
846 package Instance_Envs is new Table.Table (
847 Table_Component_Type => Instance_Env,
848 Table_Index_Type => Int,
849 Table_Low_Bound => 0,
850 Table_Initial => 32,
851 Table_Increment => 100,
852 Table_Name => "Instance_Envs");
854 procedure Restore_Private_Views
855 (Pack_Id : Entity_Id;
856 Is_Package : Boolean := True);
857 -- Restore the private views of external types, and unmark the generic
858 -- renamings of actuals, so that they become compatible subtypes again.
859 -- For subprograms, Pack_Id is the package constructed to hold the
860 -- renamings.
862 procedure Switch_View (T : Entity_Id);
863 -- Switch the partial and full views of a type and its private
864 -- dependents (i.e. its subtypes and derived types).
866 ------------------------------------
867 -- Structures for Error Reporting --
868 ------------------------------------
870 Instantiation_Node : Node_Id;
871 -- Used by subprograms that validate instantiation of formal parameters
872 -- where there might be no actual on which to place the error message.
873 -- Also used to locate the instantiation node for generic subunits.
875 Instantiation_Error : exception;
876 -- When there is a semantic error in the generic parameter matching,
877 -- there is no point in continuing the instantiation, because the
878 -- number of cascaded errors is unpredictable. This exception aborts
879 -- the instantiation process altogether.
881 S_Adjustment : Sloc_Adjustment;
882 -- Offset created for each node in an instantiation, in order to keep
883 -- track of the source position of the instantiation in each of its nodes.
884 -- A subsequent semantic error or warning on a construct of the instance
885 -- points to both places: the original generic node, and the point of
886 -- instantiation. See Sinput and Sinput.L for additional details.
888 ------------------------------------------------------------
889 -- Data structure for keeping track when inside a Generic --
890 ------------------------------------------------------------
892 -- The following table is used to save values of the Inside_A_Generic
893 -- flag (see spec of Sem) when they are saved by Start_Generic.
895 package Generic_Flags is new Table.Table (
896 Table_Component_Type => Boolean,
897 Table_Index_Type => Int,
898 Table_Low_Bound => 0,
899 Table_Initial => 32,
900 Table_Increment => 200,
901 Table_Name => "Generic_Flags");
903 ---------------------------
904 -- Abandon_Instantiation --
905 ---------------------------
907 procedure Abandon_Instantiation (N : Node_Id) is
908 begin
909 Error_Msg_N ("\instantiation abandoned!", N);
910 raise Instantiation_Error;
911 end Abandon_Instantiation;
913 --------------------------
914 -- Analyze_Associations --
915 --------------------------
917 function Analyze_Associations
918 (I_Node : Node_Id;
919 Formals : List_Id;
920 F_Copy : List_Id) return List_Id
922 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
923 Assoc : constant List_Id := New_List;
924 Default_Actuals : constant List_Id := New_List;
925 Gen_Unit : constant Entity_Id :=
926 Defining_Entity (Parent (F_Copy));
928 Actuals : List_Id;
929 Actual : Node_Id;
930 Analyzed_Formal : Node_Id;
931 First_Named : Node_Id := Empty;
932 Formal : Node_Id;
933 Match : Node_Id;
934 Named : Node_Id;
935 Saved_Formal : Node_Id;
937 Default_Formals : constant List_Id := New_List;
938 -- If an Others_Choice is present, some of the formals may be defaulted.
939 -- To simplify the treatment of visibility in an instance, we introduce
940 -- individual defaults for each such formal. These defaults are
941 -- appended to the list of associations and replace the Others_Choice.
943 Found_Assoc : Node_Id;
944 -- Association for the current formal being match. Empty if there are
945 -- no remaining actuals, or if there is no named association with the
946 -- name of the formal.
948 Is_Named_Assoc : Boolean;
949 Num_Matched : Int := 0;
950 Num_Actuals : Int := 0;
952 Others_Present : Boolean := False;
953 Others_Choice : Node_Id := Empty;
954 -- In Ada 2005, indicates partial parameterization of a formal
955 -- package. As usual an other association must be last in the list.
957 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
958 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
959 -- cannot have a named association for it. AI05-0025 extends this rule
960 -- to formals of formal packages by AI05-0025, and it also applies to
961 -- box-initialized formals.
963 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
964 -- Determine whether the parameter types and the return type of Subp
965 -- are fully defined at the point of instantiation.
967 function Matching_Actual
968 (F : Entity_Id;
969 A_F : Entity_Id) return Node_Id;
970 -- Find actual that corresponds to a given a formal parameter. If the
971 -- actuals are positional, return the next one, if any. If the actuals
972 -- are named, scan the parameter associations to find the right one.
973 -- A_F is the corresponding entity in the analyzed generic,which is
974 -- placed on the selector name for ASIS use.
976 -- In Ada 2005, a named association may be given with a box, in which
977 -- case Matching_Actual sets Found_Assoc to the generic association,
978 -- but return Empty for the actual itself. In this case the code below
979 -- creates a corresponding declaration for the formal.
981 function Partial_Parameterization return Boolean;
982 -- Ada 2005: if no match is found for a given formal, check if the
983 -- association for it includes a box, or whether the associations
984 -- include an Others clause.
986 procedure Process_Default (F : Entity_Id);
987 -- Add a copy of the declaration of generic formal F to the list of
988 -- associations, and add an explicit box association for F if there
989 -- is none yet, and the default comes from an Others_Choice.
991 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
992 -- Determine whether Subp renames one of the subprograms defined in the
993 -- generated package Standard.
995 procedure Set_Analyzed_Formal;
996 -- Find the node in the generic copy that corresponds to a given formal.
997 -- The semantic information on this node is used to perform legality
998 -- checks on the actuals. Because semantic analysis can introduce some
999 -- anonymous entities or modify the declaration node itself, the
1000 -- correspondence between the two lists is not one-one. In addition to
1001 -- anonymous types, the presence a formal equality will introduce an
1002 -- implicit declaration for the corresponding inequality.
1004 ----------------------------------------
1005 -- Check_Overloaded_Formal_Subprogram --
1006 ----------------------------------------
1008 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1009 Temp_Formal : Entity_Id;
1011 begin
1012 Temp_Formal := First (Formals);
1013 while Present (Temp_Formal) loop
1014 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1015 and then Temp_Formal /= Formal
1016 and then
1017 Chars (Defining_Unit_Name (Specification (Formal))) =
1018 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1019 then
1020 if Present (Found_Assoc) then
1021 Error_Msg_N
1022 ("named association not allowed for overloaded formal",
1023 Found_Assoc);
1025 else
1026 Error_Msg_N
1027 ("named association not allowed for overloaded formal",
1028 Others_Choice);
1029 end if;
1031 Abandon_Instantiation (Instantiation_Node);
1032 end if;
1034 Next (Temp_Formal);
1035 end loop;
1036 end Check_Overloaded_Formal_Subprogram;
1038 -------------------------------
1039 -- Has_Fully_Defined_Profile --
1040 -------------------------------
1042 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1043 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1044 -- Determine whethet type Typ is fully defined
1046 ---------------------------
1047 -- Is_Fully_Defined_Type --
1048 ---------------------------
1050 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1051 begin
1052 -- A private type without a full view is not fully defined
1054 if Is_Private_Type (Typ)
1055 and then No (Full_View (Typ))
1056 then
1057 return False;
1059 -- An incomplete type is never fully defined
1061 elsif Is_Incomplete_Type (Typ) then
1062 return False;
1064 -- All other types are fully defined
1066 else
1067 return True;
1068 end if;
1069 end Is_Fully_Defined_Type;
1071 -- Local declarations
1073 Param : Entity_Id;
1075 -- Start of processing for Has_Fully_Defined_Profile
1077 begin
1078 -- Check the parameters
1080 Param := First_Formal (Subp);
1081 while Present (Param) loop
1082 if not Is_Fully_Defined_Type (Etype (Param)) then
1083 return False;
1084 end if;
1086 Next_Formal (Param);
1087 end loop;
1089 -- Check the return type
1091 return Is_Fully_Defined_Type (Etype (Subp));
1092 end Has_Fully_Defined_Profile;
1094 ---------------------
1095 -- Matching_Actual --
1096 ---------------------
1098 function Matching_Actual
1099 (F : Entity_Id;
1100 A_F : Entity_Id) return Node_Id
1102 Prev : Node_Id;
1103 Act : Node_Id;
1105 begin
1106 Is_Named_Assoc := False;
1108 -- End of list of purely positional parameters
1110 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1111 Found_Assoc := Empty;
1112 Act := Empty;
1114 -- Case of positional parameter corresponding to current formal
1116 elsif No (Selector_Name (Actual)) then
1117 Found_Assoc := Actual;
1118 Act := Explicit_Generic_Actual_Parameter (Actual);
1119 Num_Matched := Num_Matched + 1;
1120 Next (Actual);
1122 -- Otherwise scan list of named actuals to find the one with the
1123 -- desired name. All remaining actuals have explicit names.
1125 else
1126 Is_Named_Assoc := True;
1127 Found_Assoc := Empty;
1128 Act := Empty;
1129 Prev := Empty;
1131 while Present (Actual) loop
1132 if Chars (Selector_Name (Actual)) = Chars (F) then
1133 Set_Entity (Selector_Name (Actual), A_F);
1134 Set_Etype (Selector_Name (Actual), Etype (A_F));
1135 Generate_Reference (A_F, Selector_Name (Actual));
1136 Found_Assoc := Actual;
1137 Act := Explicit_Generic_Actual_Parameter (Actual);
1138 Num_Matched := Num_Matched + 1;
1139 exit;
1140 end if;
1142 Prev := Actual;
1143 Next (Actual);
1144 end loop;
1146 -- Reset for subsequent searches. In most cases the named
1147 -- associations are in order. If they are not, we reorder them
1148 -- to avoid scanning twice the same actual. This is not just a
1149 -- question of efficiency: there may be multiple defaults with
1150 -- boxes that have the same name. In a nested instantiation we
1151 -- insert actuals for those defaults, and cannot rely on their
1152 -- names to disambiguate them.
1154 if Actual = First_Named then
1155 Next (First_Named);
1157 elsif Present (Actual) then
1158 Insert_Before (First_Named, Remove_Next (Prev));
1159 end if;
1161 Actual := First_Named;
1162 end if;
1164 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1165 Set_Used_As_Generic_Actual (Entity (Act));
1166 end if;
1168 return Act;
1169 end Matching_Actual;
1171 ------------------------------
1172 -- Partial_Parameterization --
1173 ------------------------------
1175 function Partial_Parameterization return Boolean is
1176 begin
1177 return Others_Present
1178 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1179 end Partial_Parameterization;
1181 ---------------------
1182 -- Process_Default --
1183 ---------------------
1185 procedure Process_Default (F : Entity_Id) is
1186 Loc : constant Source_Ptr := Sloc (I_Node);
1187 F_Id : constant Entity_Id := Defining_Entity (F);
1188 Decl : Node_Id;
1189 Default : Node_Id;
1190 Id : Entity_Id;
1192 begin
1193 -- Append copy of formal declaration to associations, and create new
1194 -- defining identifier for it.
1196 Decl := New_Copy_Tree (F);
1197 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1199 if Nkind (F) in N_Formal_Subprogram_Declaration then
1200 Set_Defining_Unit_Name (Specification (Decl), Id);
1202 else
1203 Set_Defining_Identifier (Decl, Id);
1204 end if;
1206 Append (Decl, Assoc);
1208 if No (Found_Assoc) then
1209 Default :=
1210 Make_Generic_Association (Loc,
1211 Selector_Name =>
1212 New_Occurrence_Of (Id, Loc),
1213 Explicit_Generic_Actual_Parameter => Empty);
1214 Set_Box_Present (Default);
1215 Append (Default, Default_Formals);
1216 end if;
1217 end Process_Default;
1219 ---------------------------------
1220 -- Renames_Standard_Subprogram --
1221 ---------------------------------
1223 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1224 Id : Entity_Id;
1226 begin
1227 Id := Alias (Subp);
1228 while Present (Id) loop
1229 if Scope (Id) = Standard_Standard then
1230 return True;
1231 end if;
1233 Id := Alias (Id);
1234 end loop;
1236 return False;
1237 end Renames_Standard_Subprogram;
1239 -------------------------
1240 -- Set_Analyzed_Formal --
1241 -------------------------
1243 procedure Set_Analyzed_Formal is
1244 Kind : Node_Kind;
1246 begin
1247 while Present (Analyzed_Formal) loop
1248 Kind := Nkind (Analyzed_Formal);
1250 case Nkind (Formal) is
1252 when N_Formal_Subprogram_Declaration =>
1253 exit when Kind in N_Formal_Subprogram_Declaration
1254 and then
1255 Chars
1256 (Defining_Unit_Name (Specification (Formal))) =
1257 Chars
1258 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1260 when N_Formal_Package_Declaration =>
1261 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1262 N_Generic_Package_Declaration,
1263 N_Package_Declaration);
1265 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1267 when others =>
1269 -- Skip freeze nodes, and nodes inserted to replace
1270 -- unrecognized pragmas.
1272 exit when
1273 Kind not in N_Formal_Subprogram_Declaration
1274 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1275 N_Freeze_Entity,
1276 N_Null_Statement,
1277 N_Itype_Reference)
1278 and then Chars (Defining_Identifier (Formal)) =
1279 Chars (Defining_Identifier (Analyzed_Formal));
1280 end case;
1282 Next (Analyzed_Formal);
1283 end loop;
1284 end Set_Analyzed_Formal;
1286 -- Start of processing for Analyze_Associations
1288 begin
1289 Actuals := Generic_Associations (I_Node);
1291 if Present (Actuals) then
1293 -- Check for an Others choice, indicating a partial parameterization
1294 -- for a formal package.
1296 Actual := First (Actuals);
1297 while Present (Actual) loop
1298 if Nkind (Actual) = N_Others_Choice then
1299 Others_Present := True;
1300 Others_Choice := Actual;
1302 if Present (Next (Actual)) then
1303 Error_Msg_N ("others must be last association", Actual);
1304 end if;
1306 -- This subprogram is used both for formal packages and for
1307 -- instantiations. For the latter, associations must all be
1308 -- explicit.
1310 if Nkind (I_Node) /= N_Formal_Package_Declaration
1311 and then Comes_From_Source (I_Node)
1312 then
1313 Error_Msg_N
1314 ("others association not allowed in an instance",
1315 Actual);
1316 end if;
1318 -- In any case, nothing to do after the others association
1320 exit;
1322 elsif Box_Present (Actual)
1323 and then Comes_From_Source (I_Node)
1324 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1325 then
1326 Error_Msg_N
1327 ("box association not allowed in an instance", Actual);
1328 end if;
1330 Next (Actual);
1331 end loop;
1333 -- If named associations are present, save first named association
1334 -- (it may of course be Empty) to facilitate subsequent name search.
1336 First_Named := First (Actuals);
1337 while Present (First_Named)
1338 and then Nkind (First_Named) /= N_Others_Choice
1339 and then No (Selector_Name (First_Named))
1340 loop
1341 Num_Actuals := Num_Actuals + 1;
1342 Next (First_Named);
1343 end loop;
1344 end if;
1346 Named := First_Named;
1347 while Present (Named) loop
1348 if Nkind (Named) /= N_Others_Choice
1349 and then No (Selector_Name (Named))
1350 then
1351 Error_Msg_N ("invalid positional actual after named one", Named);
1352 Abandon_Instantiation (Named);
1353 end if;
1355 -- A named association may lack an actual parameter, if it was
1356 -- introduced for a default subprogram that turns out to be local
1357 -- to the outer instantiation.
1359 if Nkind (Named) /= N_Others_Choice
1360 and then Present (Explicit_Generic_Actual_Parameter (Named))
1361 then
1362 Num_Actuals := Num_Actuals + 1;
1363 end if;
1365 Next (Named);
1366 end loop;
1368 if Present (Formals) then
1369 Formal := First_Non_Pragma (Formals);
1370 Analyzed_Formal := First_Non_Pragma (F_Copy);
1372 if Present (Actuals) then
1373 Actual := First (Actuals);
1375 -- All formals should have default values
1377 else
1378 Actual := Empty;
1379 end if;
1381 while Present (Formal) loop
1382 Set_Analyzed_Formal;
1383 Saved_Formal := Next_Non_Pragma (Formal);
1385 case Nkind (Formal) is
1386 when N_Formal_Object_Declaration =>
1387 Match :=
1388 Matching_Actual
1389 (Defining_Identifier (Formal),
1390 Defining_Identifier (Analyzed_Formal));
1392 if No (Match) and then Partial_Parameterization then
1393 Process_Default (Formal);
1395 else
1396 Append_List
1397 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1398 Assoc);
1400 -- For a defaulted in_parameter, create an entry in the
1401 -- the list of defaulted actuals, for GNATProve use. Do
1402 -- not included these defaults for an instance nested
1403 -- within a generic, because the defaults are also used
1404 -- in the analysis of the enclosing generic, and only
1405 -- defaulted subprograms are relevant there.
1407 if No (Match) and then not Inside_A_Generic then
1408 Append_To (Default_Actuals,
1409 Make_Generic_Association (Sloc (I_Node),
1410 Selector_Name =>
1411 New_Occurrence_Of
1412 (Defining_Identifier (Formal), Sloc (I_Node)),
1413 Explicit_Generic_Actual_Parameter =>
1414 New_Copy_Tree (Default_Expression (Formal))));
1415 end if;
1416 end if;
1418 -- If the object is a call to an expression function, this
1419 -- is a freezing point for it.
1421 if Is_Entity_Name (Match)
1422 and then Present (Entity (Match))
1423 and then Nkind
1424 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1425 = N_Expression_Function
1426 then
1427 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1428 end if;
1430 when N_Formal_Type_Declaration =>
1431 Match :=
1432 Matching_Actual
1433 (Defining_Identifier (Formal),
1434 Defining_Identifier (Analyzed_Formal));
1436 if No (Match) then
1437 if Partial_Parameterization then
1438 Process_Default (Formal);
1440 else
1441 Error_Msg_Sloc := Sloc (Gen_Unit);
1442 Error_Msg_NE
1443 ("missing actual&",
1444 Instantiation_Node, Defining_Identifier (Formal));
1445 Error_Msg_NE
1446 ("\in instantiation of & declared#",
1447 Instantiation_Node, Gen_Unit);
1448 Abandon_Instantiation (Instantiation_Node);
1449 end if;
1451 else
1452 Analyze (Match);
1453 Append_List
1454 (Instantiate_Type
1455 (Formal, Match, Analyzed_Formal, Assoc),
1456 Assoc);
1458 -- An instantiation is a freeze point for the actuals,
1459 -- unless this is a rewritten formal package, or the
1460 -- formal is an Ada 2012 formal incomplete type.
1462 if Nkind (I_Node) = N_Formal_Package_Declaration
1463 or else
1464 (Ada_Version >= Ada_2012
1465 and then
1466 Ekind (Defining_Identifier (Analyzed_Formal)) =
1467 E_Incomplete_Type)
1468 then
1469 null;
1471 else
1472 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1473 end if;
1474 end if;
1476 -- A remote access-to-class-wide type is not a legal actual
1477 -- for a generic formal of an access type (E.2.2(17/2)).
1478 -- In GNAT an exception to this rule is introduced when
1479 -- the formal is marked as remote using implementation
1480 -- defined aspect/pragma Remote_Access_Type. In that case
1481 -- the actual must be remote as well.
1483 -- If the current instantiation is the construction of a
1484 -- local copy for a formal package the actuals may be
1485 -- defaulted, and there is no matching actual to check.
1487 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1488 and then
1489 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1490 N_Access_To_Object_Definition
1491 and then Present (Match)
1492 then
1493 declare
1494 Formal_Ent : constant Entity_Id :=
1495 Defining_Identifier (Analyzed_Formal);
1496 begin
1497 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1498 = Is_Remote_Types (Formal_Ent)
1499 then
1500 -- Remoteness of formal and actual match
1502 null;
1504 elsif Is_Remote_Types (Formal_Ent) then
1506 -- Remote formal, non-remote actual
1508 Error_Msg_NE
1509 ("actual for& must be remote", Match, Formal_Ent);
1511 else
1512 -- Non-remote formal, remote actual
1514 Error_Msg_NE
1515 ("actual for& may not be remote",
1516 Match, Formal_Ent);
1517 end if;
1518 end;
1519 end if;
1521 when N_Formal_Subprogram_Declaration =>
1522 Match :=
1523 Matching_Actual
1524 (Defining_Unit_Name (Specification (Formal)),
1525 Defining_Unit_Name (Specification (Analyzed_Formal)));
1527 -- If the formal subprogram has the same name as another
1528 -- formal subprogram of the generic, then a named
1529 -- association is illegal (12.3(9)). Exclude named
1530 -- associations that are generated for a nested instance.
1532 if Present (Match)
1533 and then Is_Named_Assoc
1534 and then Comes_From_Source (Found_Assoc)
1535 then
1536 Check_Overloaded_Formal_Subprogram (Formal);
1537 end if;
1539 -- If there is no corresponding actual, this may be case
1540 -- of partial parameterization, or else the formal has a
1541 -- default or a box.
1543 if No (Match) and then Partial_Parameterization then
1544 Process_Default (Formal);
1546 if Nkind (I_Node) = N_Formal_Package_Declaration then
1547 Check_Overloaded_Formal_Subprogram (Formal);
1548 end if;
1550 else
1551 Append_To (Assoc,
1552 Instantiate_Formal_Subprogram
1553 (Formal, Match, Analyzed_Formal));
1555 -- An instantiation is a freeze point for the actuals,
1556 -- unless this is a rewritten formal package.
1558 if Nkind (I_Node) /= N_Formal_Package_Declaration
1559 and then Nkind (Match) = N_Identifier
1560 and then Is_Subprogram (Entity (Match))
1562 -- The actual subprogram may rename a routine defined
1563 -- in Standard. Avoid freezing such renamings because
1564 -- subprograms coming from Standard cannot be frozen.
1566 and then
1567 not Renames_Standard_Subprogram (Entity (Match))
1569 -- If the actual subprogram comes from a different
1570 -- unit, it is already frozen, either by a body in
1571 -- that unit or by the end of the declarative part
1572 -- of the unit. This check avoids the freezing of
1573 -- subprograms defined in Standard which are used
1574 -- as generic actuals.
1576 and then In_Same_Code_Unit (Entity (Match), I_Node)
1577 and then Has_Fully_Defined_Profile (Entity (Match))
1578 then
1579 -- Mark the subprogram as having a delayed freeze
1580 -- since this may be an out-of-order action.
1582 Set_Has_Delayed_Freeze (Entity (Match));
1583 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1584 end if;
1585 end if;
1587 -- If this is a nested generic, preserve default for later
1588 -- instantiations. We do this as well for GNATProve use,
1589 -- so that the list of generic associations is complete.
1591 if No (Match) and then Box_Present (Formal) then
1592 declare
1593 Subp : constant Entity_Id :=
1594 Defining_Unit_Name (Specification (Last (Assoc)));
1596 begin
1597 Append_To (Default_Actuals,
1598 Make_Generic_Association (Sloc (I_Node),
1599 Selector_Name =>
1600 New_Occurrence_Of (Subp, Sloc (I_Node)),
1601 Explicit_Generic_Actual_Parameter =>
1602 New_Occurrence_Of (Subp, Sloc (I_Node))));
1603 end;
1604 end if;
1606 when N_Formal_Package_Declaration =>
1607 Match :=
1608 Matching_Actual
1609 (Defining_Identifier (Formal),
1610 Defining_Identifier (Original_Node (Analyzed_Formal)));
1612 if No (Match) then
1613 if Partial_Parameterization then
1614 Process_Default (Formal);
1616 else
1617 Error_Msg_Sloc := Sloc (Gen_Unit);
1618 Error_Msg_NE
1619 ("missing actual&",
1620 Instantiation_Node, Defining_Identifier (Formal));
1621 Error_Msg_NE
1622 ("\in instantiation of & declared#",
1623 Instantiation_Node, Gen_Unit);
1625 Abandon_Instantiation (Instantiation_Node);
1626 end if;
1628 else
1629 Analyze (Match);
1630 Append_List
1631 (Instantiate_Formal_Package
1632 (Formal, Match, Analyzed_Formal),
1633 Assoc);
1634 end if;
1636 -- For use type and use package appearing in the generic part,
1637 -- we have already copied them, so we can just move them where
1638 -- they belong (we mustn't recopy them since this would mess up
1639 -- the Sloc values).
1641 when N_Use_Package_Clause |
1642 N_Use_Type_Clause =>
1643 if Nkind (Original_Node (I_Node)) =
1644 N_Formal_Package_Declaration
1645 then
1646 Append (New_Copy_Tree (Formal), Assoc);
1647 else
1648 Remove (Formal);
1649 Append (Formal, Assoc);
1650 end if;
1652 when others =>
1653 raise Program_Error;
1655 end case;
1657 Formal := Saved_Formal;
1658 Next_Non_Pragma (Analyzed_Formal);
1659 end loop;
1661 if Num_Actuals > Num_Matched then
1662 Error_Msg_Sloc := Sloc (Gen_Unit);
1664 if Present (Selector_Name (Actual)) then
1665 Error_Msg_NE
1666 ("unmatched actual &", Actual, Selector_Name (Actual));
1667 Error_Msg_NE
1668 ("\in instantiation of & declared#", Actual, Gen_Unit);
1669 else
1670 Error_Msg_NE
1671 ("unmatched actual in instantiation of & declared#",
1672 Actual, Gen_Unit);
1673 end if;
1674 end if;
1676 elsif Present (Actuals) then
1677 Error_Msg_N
1678 ("too many actuals in generic instantiation", Instantiation_Node);
1679 end if;
1681 -- An instantiation freezes all generic actuals. The only exceptions
1682 -- to this are incomplete types and subprograms which are not fully
1683 -- defined at the point of instantiation.
1685 declare
1686 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1687 begin
1688 while Present (Elmt) loop
1689 Freeze_Before (I_Node, Node (Elmt));
1690 Next_Elmt (Elmt);
1691 end loop;
1692 end;
1694 -- If there are default subprograms, normalize the tree by adding
1695 -- explicit associations for them. This is required if the instance
1696 -- appears within a generic.
1698 if not Is_Empty_List (Default_Actuals) then
1699 declare
1700 Default : Node_Id;
1702 begin
1703 Default := First (Default_Actuals);
1704 while Present (Default) loop
1705 Mark_Rewrite_Insertion (Default);
1706 Next (Default);
1707 end loop;
1709 if No (Actuals) then
1710 Set_Generic_Associations (I_Node, Default_Actuals);
1711 else
1712 Append_List_To (Actuals, Default_Actuals);
1713 end if;
1714 end;
1715 end if;
1717 -- If this is a formal package, normalize the parameter list by adding
1718 -- explicit box associations for the formals that are covered by an
1719 -- Others_Choice.
1721 if not Is_Empty_List (Default_Formals) then
1722 Append_List (Default_Formals, Formals);
1723 end if;
1725 return Assoc;
1726 end Analyze_Associations;
1728 -------------------------------
1729 -- Analyze_Formal_Array_Type --
1730 -------------------------------
1732 procedure Analyze_Formal_Array_Type
1733 (T : in out Entity_Id;
1734 Def : Node_Id)
1736 DSS : Node_Id;
1738 begin
1739 -- Treated like a non-generic array declaration, with additional
1740 -- semantic checks.
1742 Enter_Name (T);
1744 if Nkind (Def) = N_Constrained_Array_Definition then
1745 DSS := First (Discrete_Subtype_Definitions (Def));
1746 while Present (DSS) loop
1747 if Nkind_In (DSS, N_Subtype_Indication,
1748 N_Range,
1749 N_Attribute_Reference)
1750 then
1751 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1752 end if;
1754 Next (DSS);
1755 end loop;
1756 end if;
1758 Array_Type_Declaration (T, Def);
1759 Set_Is_Generic_Type (Base_Type (T));
1761 if Ekind (Component_Type (T)) = E_Incomplete_Type
1762 and then No (Full_View (Component_Type (T)))
1763 then
1764 Error_Msg_N ("premature usage of incomplete type", Def);
1766 -- Check that range constraint is not allowed on the component type
1767 -- of a generic formal array type (AARM 12.5.3(3))
1769 elsif Is_Internal (Component_Type (T))
1770 and then Present (Subtype_Indication (Component_Definition (Def)))
1771 and then Nkind (Original_Node
1772 (Subtype_Indication (Component_Definition (Def)))) =
1773 N_Subtype_Indication
1774 then
1775 Error_Msg_N
1776 ("in a formal, a subtype indication can only be "
1777 & "a subtype mark (RM 12.5.3(3))",
1778 Subtype_Indication (Component_Definition (Def)));
1779 end if;
1781 end Analyze_Formal_Array_Type;
1783 ---------------------------------------------
1784 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1785 ---------------------------------------------
1787 -- As for other generic types, we create a valid type representation with
1788 -- legal but arbitrary attributes, whose values are never considered
1789 -- static. For all scalar types we introduce an anonymous base type, with
1790 -- the same attributes. We choose the corresponding integer type to be
1791 -- Standard_Integer.
1792 -- Here and in other similar routines, the Sloc of the generated internal
1793 -- type must be the same as the sloc of the defining identifier of the
1794 -- formal type declaration, to provide proper source navigation.
1796 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1797 (T : Entity_Id;
1798 Def : Node_Id)
1800 Loc : constant Source_Ptr := Sloc (Def);
1802 Base : constant Entity_Id :=
1803 New_Internal_Entity
1804 (E_Decimal_Fixed_Point_Type,
1805 Current_Scope,
1806 Sloc (Defining_Identifier (Parent (Def))), 'G');
1808 Int_Base : constant Entity_Id := Standard_Integer;
1809 Delta_Val : constant Ureal := Ureal_1;
1810 Digs_Val : constant Uint := Uint_6;
1812 function Make_Dummy_Bound return Node_Id;
1813 -- Return a properly typed universal real literal to use as a bound
1815 ----------------------
1816 -- Make_Dummy_Bound --
1817 ----------------------
1819 function Make_Dummy_Bound return Node_Id is
1820 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1821 begin
1822 Set_Etype (Bound, Universal_Real);
1823 return Bound;
1824 end Make_Dummy_Bound;
1826 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1828 begin
1829 Enter_Name (T);
1831 Set_Etype (Base, Base);
1832 Set_Size_Info (Base, Int_Base);
1833 Set_RM_Size (Base, RM_Size (Int_Base));
1834 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1835 Set_Digits_Value (Base, Digs_Val);
1836 Set_Delta_Value (Base, Delta_Val);
1837 Set_Small_Value (Base, Delta_Val);
1838 Set_Scalar_Range (Base,
1839 Make_Range (Loc,
1840 Low_Bound => Make_Dummy_Bound,
1841 High_Bound => Make_Dummy_Bound));
1843 Set_Is_Generic_Type (Base);
1844 Set_Parent (Base, Parent (Def));
1846 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1847 Set_Etype (T, Base);
1848 Set_Size_Info (T, Int_Base);
1849 Set_RM_Size (T, RM_Size (Int_Base));
1850 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1851 Set_Digits_Value (T, Digs_Val);
1852 Set_Delta_Value (T, Delta_Val);
1853 Set_Small_Value (T, Delta_Val);
1854 Set_Scalar_Range (T, Scalar_Range (Base));
1855 Set_Is_Constrained (T);
1857 Check_Restriction (No_Fixed_Point, Def);
1858 end Analyze_Formal_Decimal_Fixed_Point_Type;
1860 -------------------------------------------
1861 -- Analyze_Formal_Derived_Interface_Type --
1862 -------------------------------------------
1864 procedure Analyze_Formal_Derived_Interface_Type
1865 (N : Node_Id;
1866 T : Entity_Id;
1867 Def : Node_Id)
1869 Loc : constant Source_Ptr := Sloc (Def);
1871 begin
1872 -- Rewrite as a type declaration of a derived type. This ensures that
1873 -- the interface list and primitive operations are properly captured.
1875 Rewrite (N,
1876 Make_Full_Type_Declaration (Loc,
1877 Defining_Identifier => T,
1878 Type_Definition => Def));
1879 Analyze (N);
1880 Set_Is_Generic_Type (T);
1881 end Analyze_Formal_Derived_Interface_Type;
1883 ---------------------------------
1884 -- Analyze_Formal_Derived_Type --
1885 ---------------------------------
1887 procedure Analyze_Formal_Derived_Type
1888 (N : Node_Id;
1889 T : Entity_Id;
1890 Def : Node_Id)
1892 Loc : constant Source_Ptr := Sloc (Def);
1893 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1894 New_N : Node_Id;
1896 begin
1897 Set_Is_Generic_Type (T);
1899 if Private_Present (Def) then
1900 New_N :=
1901 Make_Private_Extension_Declaration (Loc,
1902 Defining_Identifier => T,
1903 Discriminant_Specifications => Discriminant_Specifications (N),
1904 Unknown_Discriminants_Present => Unk_Disc,
1905 Subtype_Indication => Subtype_Mark (Def),
1906 Interface_List => Interface_List (Def));
1908 Set_Abstract_Present (New_N, Abstract_Present (Def));
1909 Set_Limited_Present (New_N, Limited_Present (Def));
1910 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
1912 else
1913 New_N :=
1914 Make_Full_Type_Declaration (Loc,
1915 Defining_Identifier => T,
1916 Discriminant_Specifications =>
1917 Discriminant_Specifications (Parent (T)),
1918 Type_Definition =>
1919 Make_Derived_Type_Definition (Loc,
1920 Subtype_Indication => Subtype_Mark (Def)));
1922 Set_Abstract_Present
1923 (Type_Definition (New_N), Abstract_Present (Def));
1924 Set_Limited_Present
1925 (Type_Definition (New_N), Limited_Present (Def));
1926 end if;
1928 Rewrite (N, New_N);
1929 Analyze (N);
1931 if Unk_Disc then
1932 if not Is_Composite_Type (T) then
1933 Error_Msg_N
1934 ("unknown discriminants not allowed for elementary types", N);
1935 else
1936 Set_Has_Unknown_Discriminants (T);
1937 Set_Is_Constrained (T, False);
1938 end if;
1939 end if;
1941 -- If the parent type has a known size, so does the formal, which makes
1942 -- legal representation clauses that involve the formal.
1944 Set_Size_Known_At_Compile_Time
1945 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1946 end Analyze_Formal_Derived_Type;
1948 ----------------------------------
1949 -- Analyze_Formal_Discrete_Type --
1950 ----------------------------------
1952 -- The operations defined for a discrete types are those of an enumeration
1953 -- type. The size is set to an arbitrary value, for use in analyzing the
1954 -- generic unit.
1956 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1957 Loc : constant Source_Ptr := Sloc (Def);
1958 Lo : Node_Id;
1959 Hi : Node_Id;
1961 Base : constant Entity_Id :=
1962 New_Internal_Entity
1963 (E_Floating_Point_Type, Current_Scope,
1964 Sloc (Defining_Identifier (Parent (Def))), 'G');
1966 begin
1967 Enter_Name (T);
1968 Set_Ekind (T, E_Enumeration_Subtype);
1969 Set_Etype (T, Base);
1970 Init_Size (T, 8);
1971 Init_Alignment (T);
1972 Set_Is_Generic_Type (T);
1973 Set_Is_Constrained (T);
1975 -- For semantic analysis, the bounds of the type must be set to some
1976 -- non-static value. The simplest is to create attribute nodes for those
1977 -- bounds, that refer to the type itself. These bounds are never
1978 -- analyzed but serve as place-holders.
1980 Lo :=
1981 Make_Attribute_Reference (Loc,
1982 Attribute_Name => Name_First,
1983 Prefix => New_Occurrence_Of (T, Loc));
1984 Set_Etype (Lo, T);
1986 Hi :=
1987 Make_Attribute_Reference (Loc,
1988 Attribute_Name => Name_Last,
1989 Prefix => New_Occurrence_Of (T, Loc));
1990 Set_Etype (Hi, T);
1992 Set_Scalar_Range (T,
1993 Make_Range (Loc,
1994 Low_Bound => Lo,
1995 High_Bound => Hi));
1997 Set_Ekind (Base, E_Enumeration_Type);
1998 Set_Etype (Base, Base);
1999 Init_Size (Base, 8);
2000 Init_Alignment (Base);
2001 Set_Is_Generic_Type (Base);
2002 Set_Scalar_Range (Base, Scalar_Range (T));
2003 Set_Parent (Base, Parent (Def));
2004 end Analyze_Formal_Discrete_Type;
2006 ----------------------------------
2007 -- Analyze_Formal_Floating_Type --
2008 ---------------------------------
2010 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2011 Base : constant Entity_Id :=
2012 New_Internal_Entity
2013 (E_Floating_Point_Type, Current_Scope,
2014 Sloc (Defining_Identifier (Parent (Def))), 'G');
2016 begin
2017 -- The various semantic attributes are taken from the predefined type
2018 -- Float, just so that all of them are initialized. Their values are
2019 -- never used because no constant folding or expansion takes place in
2020 -- the generic itself.
2022 Enter_Name (T);
2023 Set_Ekind (T, E_Floating_Point_Subtype);
2024 Set_Etype (T, Base);
2025 Set_Size_Info (T, (Standard_Float));
2026 Set_RM_Size (T, RM_Size (Standard_Float));
2027 Set_Digits_Value (T, Digits_Value (Standard_Float));
2028 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2029 Set_Is_Constrained (T);
2031 Set_Is_Generic_Type (Base);
2032 Set_Etype (Base, Base);
2033 Set_Size_Info (Base, (Standard_Float));
2034 Set_RM_Size (Base, RM_Size (Standard_Float));
2035 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2036 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2037 Set_Parent (Base, Parent (Def));
2039 Check_Restriction (No_Floating_Point, Def);
2040 end Analyze_Formal_Floating_Type;
2042 -----------------------------------
2043 -- Analyze_Formal_Interface_Type;--
2044 -----------------------------------
2046 procedure Analyze_Formal_Interface_Type
2047 (N : Node_Id;
2048 T : Entity_Id;
2049 Def : Node_Id)
2051 Loc : constant Source_Ptr := Sloc (N);
2052 New_N : Node_Id;
2054 begin
2055 New_N :=
2056 Make_Full_Type_Declaration (Loc,
2057 Defining_Identifier => T,
2058 Type_Definition => Def);
2060 Rewrite (N, New_N);
2061 Analyze (N);
2062 Set_Is_Generic_Type (T);
2063 end Analyze_Formal_Interface_Type;
2065 ---------------------------------
2066 -- Analyze_Formal_Modular_Type --
2067 ---------------------------------
2069 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2070 begin
2071 -- Apart from their entity kind, generic modular types are treated like
2072 -- signed integer types, and have the same attributes.
2074 Analyze_Formal_Signed_Integer_Type (T, Def);
2075 Set_Ekind (T, E_Modular_Integer_Subtype);
2076 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2078 end Analyze_Formal_Modular_Type;
2080 ---------------------------------------
2081 -- Analyze_Formal_Object_Declaration --
2082 ---------------------------------------
2084 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2085 E : constant Node_Id := Default_Expression (N);
2086 Id : constant Node_Id := Defining_Identifier (N);
2087 K : Entity_Kind;
2088 T : Node_Id;
2090 begin
2091 Enter_Name (Id);
2093 -- Determine the mode of the formal object
2095 if Out_Present (N) then
2096 K := E_Generic_In_Out_Parameter;
2098 if not In_Present (N) then
2099 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2100 end if;
2102 else
2103 K := E_Generic_In_Parameter;
2104 end if;
2106 if Present (Subtype_Mark (N)) then
2107 Find_Type (Subtype_Mark (N));
2108 T := Entity (Subtype_Mark (N));
2110 -- Verify that there is no redundant null exclusion
2112 if Null_Exclusion_Present (N) then
2113 if not Is_Access_Type (T) then
2114 Error_Msg_N
2115 ("null exclusion can only apply to an access type", N);
2117 elsif Can_Never_Be_Null (T) then
2118 Error_Msg_NE
2119 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2120 end if;
2121 end if;
2123 -- Ada 2005 (AI-423): Formal object with an access definition
2125 else
2126 Check_Access_Definition (N);
2127 T := Access_Definition
2128 (Related_Nod => N,
2129 N => Access_Definition (N));
2130 end if;
2132 if Ekind (T) = E_Incomplete_Type then
2133 declare
2134 Error_Node : Node_Id;
2136 begin
2137 if Present (Subtype_Mark (N)) then
2138 Error_Node := Subtype_Mark (N);
2139 else
2140 Check_Access_Definition (N);
2141 Error_Node := Access_Definition (N);
2142 end if;
2144 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2145 end;
2146 end if;
2148 if K = E_Generic_In_Parameter then
2150 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2152 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2153 Error_Msg_N
2154 ("generic formal of mode IN must not be of limited type", N);
2155 Explain_Limited_Type (T, N);
2156 end if;
2158 if Is_Abstract_Type (T) then
2159 Error_Msg_N
2160 ("generic formal of mode IN must not be of abstract type", N);
2161 end if;
2163 if Present (E) then
2164 Preanalyze_Spec_Expression (E, T);
2166 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2167 Error_Msg_N
2168 ("initialization not allowed for limited types", E);
2169 Explain_Limited_Type (T, E);
2170 end if;
2171 end if;
2173 Set_Ekind (Id, K);
2174 Set_Etype (Id, T);
2176 -- Case of generic IN OUT parameter
2178 else
2179 -- If the formal has an unconstrained type, construct its actual
2180 -- subtype, as is done for subprogram formals. In this fashion, all
2181 -- its uses can refer to specific bounds.
2183 Set_Ekind (Id, K);
2184 Set_Etype (Id, T);
2186 if (Is_Array_Type (T) and then not Is_Constrained (T))
2187 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2188 then
2189 declare
2190 Non_Freezing_Ref : constant Node_Id :=
2191 New_Occurrence_Of (Id, Sloc (Id));
2192 Decl : Node_Id;
2194 begin
2195 -- Make sure the actual subtype doesn't generate bogus freezing
2197 Set_Must_Not_Freeze (Non_Freezing_Ref);
2198 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2199 Insert_Before_And_Analyze (N, Decl);
2200 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2201 end;
2202 else
2203 Set_Actual_Subtype (Id, T);
2204 end if;
2206 if Present (E) then
2207 Error_Msg_N
2208 ("initialization not allowed for `IN OUT` formals", N);
2209 end if;
2210 end if;
2212 if Has_Aspects (N) then
2213 Analyze_Aspect_Specifications (N, Id);
2214 end if;
2215 end Analyze_Formal_Object_Declaration;
2217 ----------------------------------------------
2218 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2219 ----------------------------------------------
2221 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2222 (T : Entity_Id;
2223 Def : Node_Id)
2225 Loc : constant Source_Ptr := Sloc (Def);
2226 Base : constant Entity_Id :=
2227 New_Internal_Entity
2228 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2229 Sloc (Defining_Identifier (Parent (Def))), 'G');
2231 begin
2232 -- The semantic attributes are set for completeness only, their values
2233 -- will never be used, since all properties of the type are non-static.
2235 Enter_Name (T);
2236 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2237 Set_Etype (T, Base);
2238 Set_Size_Info (T, Standard_Integer);
2239 Set_RM_Size (T, RM_Size (Standard_Integer));
2240 Set_Small_Value (T, Ureal_1);
2241 Set_Delta_Value (T, Ureal_1);
2242 Set_Scalar_Range (T,
2243 Make_Range (Loc,
2244 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2245 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2246 Set_Is_Constrained (T);
2248 Set_Is_Generic_Type (Base);
2249 Set_Etype (Base, Base);
2250 Set_Size_Info (Base, Standard_Integer);
2251 Set_RM_Size (Base, RM_Size (Standard_Integer));
2252 Set_Small_Value (Base, Ureal_1);
2253 Set_Delta_Value (Base, Ureal_1);
2254 Set_Scalar_Range (Base, Scalar_Range (T));
2255 Set_Parent (Base, Parent (Def));
2257 Check_Restriction (No_Fixed_Point, Def);
2258 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2260 ----------------------------------------
2261 -- Analyze_Formal_Package_Declaration --
2262 ----------------------------------------
2264 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2265 Loc : constant Source_Ptr := Sloc (N);
2266 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2267 Formal : Entity_Id;
2268 Gen_Id : constant Node_Id := Name (N);
2269 Gen_Decl : Node_Id;
2270 Gen_Unit : Entity_Id;
2271 New_N : Node_Id;
2272 Parent_Installed : Boolean := False;
2273 Renaming : Node_Id;
2274 Parent_Instance : Entity_Id;
2275 Renaming_In_Par : Entity_Id;
2276 Associations : Boolean := True;
2278 Vis_Prims_List : Elist_Id := No_Elist;
2279 -- List of primitives made temporarily visible in the instantiation
2280 -- to match the visibility of the formal type
2282 function Build_Local_Package return Node_Id;
2283 -- The formal package is rewritten so that its parameters are replaced
2284 -- with corresponding declarations. For parameters with bona fide
2285 -- associations these declarations are created by Analyze_Associations
2286 -- as for a regular instantiation. For boxed parameters, we preserve
2287 -- the formal declarations and analyze them, in order to introduce
2288 -- entities of the right kind in the environment of the formal.
2290 -------------------------
2291 -- Build_Local_Package --
2292 -------------------------
2294 function Build_Local_Package return Node_Id is
2295 Decls : List_Id;
2296 Pack_Decl : Node_Id;
2298 begin
2299 -- Within the formal, the name of the generic package is a renaming
2300 -- of the formal (as for a regular instantiation).
2302 Pack_Decl :=
2303 Make_Package_Declaration (Loc,
2304 Specification =>
2305 Copy_Generic_Node
2306 (Specification (Original_Node (Gen_Decl)),
2307 Empty, Instantiating => True));
2309 Renaming := Make_Package_Renaming_Declaration (Loc,
2310 Defining_Unit_Name =>
2311 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2312 Name => New_Occurrence_Of (Formal, Loc));
2314 if Nkind (Gen_Id) = N_Identifier
2315 and then Chars (Gen_Id) = Chars (Pack_Id)
2316 then
2317 Error_Msg_NE
2318 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2319 end if;
2321 -- If the formal is declared with a box, or with an others choice,
2322 -- create corresponding declarations for all entities in the formal
2323 -- part, so that names with the proper types are available in the
2324 -- specification of the formal package.
2326 -- On the other hand, if there are no associations, then all the
2327 -- formals must have defaults, and this will be checked by the
2328 -- call to Analyze_Associations.
2330 if Box_Present (N)
2331 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2332 then
2333 declare
2334 Formal_Decl : Node_Id;
2336 begin
2337 -- TBA : for a formal package, need to recurse ???
2339 Decls := New_List;
2340 Formal_Decl :=
2341 First
2342 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2343 while Present (Formal_Decl) loop
2344 Append_To
2345 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2346 Next (Formal_Decl);
2347 end loop;
2348 end;
2350 -- If generic associations are present, use Analyze_Associations to
2351 -- create the proper renaming declarations.
2353 else
2354 declare
2355 Act_Tree : constant Node_Id :=
2356 Copy_Generic_Node
2357 (Original_Node (Gen_Decl), Empty,
2358 Instantiating => True);
2360 begin
2361 Generic_Renamings.Set_Last (0);
2362 Generic_Renamings_HTable.Reset;
2363 Instantiation_Node := N;
2365 Decls :=
2366 Analyze_Associations
2367 (I_Node => Original_Node (N),
2368 Formals => Generic_Formal_Declarations (Act_Tree),
2369 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2371 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2372 end;
2373 end if;
2375 Append (Renaming, To => Decls);
2377 -- Add generated declarations ahead of local declarations in
2378 -- the package.
2380 if No (Visible_Declarations (Specification (Pack_Decl))) then
2381 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2382 else
2383 Insert_List_Before
2384 (First (Visible_Declarations (Specification (Pack_Decl))),
2385 Decls);
2386 end if;
2388 return Pack_Decl;
2389 end Build_Local_Package;
2391 -- Start of processing for Analyze_Formal_Package_Declaration
2393 begin
2394 Check_Text_IO_Special_Unit (Gen_Id);
2396 Init_Env;
2397 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2398 Gen_Unit := Entity (Gen_Id);
2400 -- Check for a formal package that is a package renaming
2402 if Present (Renamed_Object (Gen_Unit)) then
2404 -- Indicate that unit is used, before replacing it with renamed
2405 -- entity for use below.
2407 if In_Extended_Main_Source_Unit (N) then
2408 Set_Is_Instantiated (Gen_Unit);
2409 Generate_Reference (Gen_Unit, N);
2410 end if;
2412 Gen_Unit := Renamed_Object (Gen_Unit);
2413 end if;
2415 if Ekind (Gen_Unit) /= E_Generic_Package then
2416 Error_Msg_N ("expect generic package name", Gen_Id);
2417 Restore_Env;
2418 goto Leave;
2420 elsif Gen_Unit = Current_Scope then
2421 Error_Msg_N
2422 ("generic package cannot be used as a formal package of itself",
2423 Gen_Id);
2424 Restore_Env;
2425 goto Leave;
2427 elsif In_Open_Scopes (Gen_Unit) then
2428 if Is_Compilation_Unit (Gen_Unit)
2429 and then Is_Child_Unit (Current_Scope)
2430 then
2431 -- Special-case the error when the formal is a parent, and
2432 -- continue analysis to minimize cascaded errors.
2434 Error_Msg_N
2435 ("generic parent cannot be used as formal package "
2436 & "of a child unit", Gen_Id);
2438 else
2439 Error_Msg_N
2440 ("generic package cannot be used as a formal package "
2441 & "within itself", Gen_Id);
2442 Restore_Env;
2443 goto Leave;
2444 end if;
2445 end if;
2447 -- Check that name of formal package does not hide name of generic,
2448 -- or its leading prefix. This check must be done separately because
2449 -- the name of the generic has already been analyzed.
2451 declare
2452 Gen_Name : Entity_Id;
2454 begin
2455 Gen_Name := Gen_Id;
2456 while Nkind (Gen_Name) = N_Expanded_Name loop
2457 Gen_Name := Prefix (Gen_Name);
2458 end loop;
2460 if Chars (Gen_Name) = Chars (Pack_Id) then
2461 Error_Msg_NE
2462 ("& is hidden within declaration of formal package",
2463 Gen_Id, Gen_Name);
2464 end if;
2465 end;
2467 if Box_Present (N)
2468 or else No (Generic_Associations (N))
2469 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2470 then
2471 Associations := False;
2472 end if;
2474 -- If there are no generic associations, the generic parameters appear
2475 -- as local entities and are instantiated like them. We copy the generic
2476 -- package declaration as if it were an instantiation, and analyze it
2477 -- like a regular package, except that we treat the formals as
2478 -- additional visible components.
2480 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2482 if In_Extended_Main_Source_Unit (N) then
2483 Set_Is_Instantiated (Gen_Unit);
2484 Generate_Reference (Gen_Unit, N);
2485 end if;
2487 Formal := New_Copy (Pack_Id);
2488 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2490 begin
2491 -- Make local generic without formals. The formals will be replaced
2492 -- with internal declarations.
2494 New_N := Build_Local_Package;
2496 -- If there are errors in the parameter list, Analyze_Associations
2497 -- raises Instantiation_Error. Patch the declaration to prevent
2498 -- further exception propagation.
2500 exception
2501 when Instantiation_Error =>
2503 Enter_Name (Formal);
2504 Set_Ekind (Formal, E_Variable);
2505 Set_Etype (Formal, Any_Type);
2506 Restore_Hidden_Primitives (Vis_Prims_List);
2508 if Parent_Installed then
2509 Remove_Parent;
2510 end if;
2512 goto Leave;
2513 end;
2515 Rewrite (N, New_N);
2516 Set_Defining_Unit_Name (Specification (New_N), Formal);
2517 Set_Generic_Parent (Specification (N), Gen_Unit);
2518 Set_Instance_Env (Gen_Unit, Formal);
2519 Set_Is_Generic_Instance (Formal);
2521 Enter_Name (Formal);
2522 Set_Ekind (Formal, E_Package);
2523 Set_Etype (Formal, Standard_Void_Type);
2524 Set_Inner_Instances (Formal, New_Elmt_List);
2525 Push_Scope (Formal);
2527 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2529 -- Similarly, we have to make the name of the formal visible in the
2530 -- parent instance, to resolve properly fully qualified names that
2531 -- may appear in the generic unit. The parent instance has been
2532 -- placed on the scope stack ahead of the current scope.
2534 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2536 Renaming_In_Par :=
2537 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2538 Set_Ekind (Renaming_In_Par, E_Package);
2539 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2540 Set_Scope (Renaming_In_Par, Parent_Instance);
2541 Set_Parent (Renaming_In_Par, Parent (Formal));
2542 Set_Renamed_Object (Renaming_In_Par, Formal);
2543 Append_Entity (Renaming_In_Par, Parent_Instance);
2544 end if;
2546 Analyze (Specification (N));
2548 -- The formals for which associations are provided are not visible
2549 -- outside of the formal package. The others are still declared by a
2550 -- formal parameter declaration.
2552 -- If there are no associations, the only local entity to hide is the
2553 -- generated package renaming itself.
2555 declare
2556 E : Entity_Id;
2558 begin
2559 E := First_Entity (Formal);
2560 while Present (E) loop
2561 if Associations and then not Is_Generic_Formal (E) then
2562 Set_Is_Hidden (E);
2563 end if;
2565 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2566 Set_Is_Hidden (E);
2567 exit;
2568 end if;
2570 Next_Entity (E);
2571 end loop;
2572 end;
2574 End_Package_Scope (Formal);
2575 Restore_Hidden_Primitives (Vis_Prims_List);
2577 if Parent_Installed then
2578 Remove_Parent;
2579 end if;
2581 Restore_Env;
2583 -- Inside the generic unit, the formal package is a regular package, but
2584 -- no body is needed for it. Note that after instantiation, the defining
2585 -- unit name we need is in the new tree and not in the original (see
2586 -- Package_Instantiation). A generic formal package is an instance, and
2587 -- can be used as an actual for an inner instance.
2589 Set_Has_Completion (Formal, True);
2591 -- Add semantic information to the original defining identifier.
2592 -- for ASIS use.
2594 Set_Ekind (Pack_Id, E_Package);
2595 Set_Etype (Pack_Id, Standard_Void_Type);
2596 Set_Scope (Pack_Id, Scope (Formal));
2597 Set_Has_Completion (Pack_Id, True);
2599 <<Leave>>
2600 if Has_Aspects (N) then
2601 Analyze_Aspect_Specifications (N, Pack_Id);
2602 end if;
2603 end Analyze_Formal_Package_Declaration;
2605 ---------------------------------
2606 -- Analyze_Formal_Private_Type --
2607 ---------------------------------
2609 procedure Analyze_Formal_Private_Type
2610 (N : Node_Id;
2611 T : Entity_Id;
2612 Def : Node_Id)
2614 begin
2615 New_Private_Type (N, T, Def);
2617 -- Set the size to an arbitrary but legal value
2619 Set_Size_Info (T, Standard_Integer);
2620 Set_RM_Size (T, RM_Size (Standard_Integer));
2621 end Analyze_Formal_Private_Type;
2623 ------------------------------------
2624 -- Analyze_Formal_Incomplete_Type --
2625 ------------------------------------
2627 procedure Analyze_Formal_Incomplete_Type
2628 (T : Entity_Id;
2629 Def : Node_Id)
2631 begin
2632 Enter_Name (T);
2633 Set_Ekind (T, E_Incomplete_Type);
2634 Set_Etype (T, T);
2635 Set_Private_Dependents (T, New_Elmt_List);
2637 if Tagged_Present (Def) then
2638 Set_Is_Tagged_Type (T);
2639 Make_Class_Wide_Type (T);
2640 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2641 end if;
2642 end Analyze_Formal_Incomplete_Type;
2644 ----------------------------------------
2645 -- Analyze_Formal_Signed_Integer_Type --
2646 ----------------------------------------
2648 procedure Analyze_Formal_Signed_Integer_Type
2649 (T : Entity_Id;
2650 Def : Node_Id)
2652 Base : constant Entity_Id :=
2653 New_Internal_Entity
2654 (E_Signed_Integer_Type,
2655 Current_Scope,
2656 Sloc (Defining_Identifier (Parent (Def))), 'G');
2658 begin
2659 Enter_Name (T);
2661 Set_Ekind (T, E_Signed_Integer_Subtype);
2662 Set_Etype (T, Base);
2663 Set_Size_Info (T, Standard_Integer);
2664 Set_RM_Size (T, RM_Size (Standard_Integer));
2665 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2666 Set_Is_Constrained (T);
2668 Set_Is_Generic_Type (Base);
2669 Set_Size_Info (Base, Standard_Integer);
2670 Set_RM_Size (Base, RM_Size (Standard_Integer));
2671 Set_Etype (Base, Base);
2672 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2673 Set_Parent (Base, Parent (Def));
2674 end Analyze_Formal_Signed_Integer_Type;
2676 -------------------------------------------
2677 -- Analyze_Formal_Subprogram_Declaration --
2678 -------------------------------------------
2680 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2681 Spec : constant Node_Id := Specification (N);
2682 Def : constant Node_Id := Default_Name (N);
2683 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2684 Subp : Entity_Id;
2686 begin
2687 if Nam = Error then
2688 return;
2689 end if;
2691 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2692 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2693 goto Leave;
2694 end if;
2696 Analyze_Subprogram_Declaration (N);
2697 Set_Is_Formal_Subprogram (Nam);
2698 Set_Has_Completion (Nam);
2700 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2701 Set_Is_Abstract_Subprogram (Nam);
2702 Set_Is_Dispatching_Operation (Nam);
2704 declare
2705 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2706 begin
2707 if No (Ctrl_Type) then
2708 Error_Msg_N
2709 ("abstract formal subprogram must have a controlling type",
2712 elsif Ada_Version >= Ada_2012
2713 and then Is_Incomplete_Type (Ctrl_Type)
2714 then
2715 Error_Msg_NE
2716 ("controlling type of abstract formal subprogram cannot "
2717 & "be incomplete type", N, Ctrl_Type);
2719 else
2720 Check_Controlling_Formals (Ctrl_Type, Nam);
2721 end if;
2722 end;
2723 end if;
2725 -- Default name is resolved at the point of instantiation
2727 if Box_Present (N) then
2728 null;
2730 -- Else default is bound at the point of generic declaration
2732 elsif Present (Def) then
2733 if Nkind (Def) = N_Operator_Symbol then
2734 Find_Direct_Name (Def);
2736 elsif Nkind (Def) /= N_Attribute_Reference then
2737 Analyze (Def);
2739 else
2740 -- For an attribute reference, analyze the prefix and verify
2741 -- that it has the proper profile for the subprogram.
2743 Analyze (Prefix (Def));
2744 Valid_Default_Attribute (Nam, Def);
2745 goto Leave;
2746 end if;
2748 -- Default name may be overloaded, in which case the interpretation
2749 -- with the correct profile must be selected, as for a renaming.
2750 -- If the definition is an indexed component, it must denote a
2751 -- member of an entry family. If it is a selected component, it
2752 -- can be a protected operation.
2754 if Etype (Def) = Any_Type then
2755 goto Leave;
2757 elsif Nkind (Def) = N_Selected_Component then
2758 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2759 Error_Msg_N ("expect valid subprogram name as default", Def);
2760 end if;
2762 elsif Nkind (Def) = N_Indexed_Component then
2763 if Is_Entity_Name (Prefix (Def)) then
2764 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2765 Error_Msg_N ("expect valid subprogram name as default", Def);
2766 end if;
2768 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2769 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2770 E_Entry_Family
2771 then
2772 Error_Msg_N ("expect valid subprogram name as default", Def);
2773 end if;
2775 else
2776 Error_Msg_N ("expect valid subprogram name as default", Def);
2777 goto Leave;
2778 end if;
2780 elsif Nkind (Def) = N_Character_Literal then
2782 -- Needs some type checks: subprogram should be parameterless???
2784 Resolve (Def, (Etype (Nam)));
2786 elsif not Is_Entity_Name (Def)
2787 or else not Is_Overloadable (Entity (Def))
2788 then
2789 Error_Msg_N ("expect valid subprogram name as default", Def);
2790 goto Leave;
2792 elsif not Is_Overloaded (Def) then
2793 Subp := Entity (Def);
2795 if Subp = Nam then
2796 Error_Msg_N ("premature usage of formal subprogram", Def);
2798 elsif not Entity_Matches_Spec (Subp, Nam) then
2799 Error_Msg_N ("no visible entity matches specification", Def);
2800 end if;
2802 -- More than one interpretation, so disambiguate as for a renaming
2804 else
2805 declare
2806 I : Interp_Index;
2807 I1 : Interp_Index := 0;
2808 It : Interp;
2809 It1 : Interp;
2811 begin
2812 Subp := Any_Id;
2813 Get_First_Interp (Def, I, It);
2814 while Present (It.Nam) loop
2815 if Entity_Matches_Spec (It.Nam, Nam) then
2816 if Subp /= Any_Id then
2817 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2819 if It1 = No_Interp then
2820 Error_Msg_N ("ambiguous default subprogram", Def);
2821 else
2822 Subp := It1.Nam;
2823 end if;
2825 exit;
2827 else
2828 I1 := I;
2829 Subp := It.Nam;
2830 end if;
2831 end if;
2833 Get_Next_Interp (I, It);
2834 end loop;
2835 end;
2837 if Subp /= Any_Id then
2839 -- Subprogram found, generate reference to it
2841 Set_Entity (Def, Subp);
2842 Generate_Reference (Subp, Def);
2844 if Subp = Nam then
2845 Error_Msg_N ("premature usage of formal subprogram", Def);
2847 elsif Ekind (Subp) /= E_Operator then
2848 Check_Mode_Conformant (Subp, Nam);
2849 end if;
2851 else
2852 Error_Msg_N ("no visible subprogram matches specification", N);
2853 end if;
2854 end if;
2855 end if;
2857 <<Leave>>
2858 if Has_Aspects (N) then
2859 Analyze_Aspect_Specifications (N, Nam);
2860 end if;
2862 end Analyze_Formal_Subprogram_Declaration;
2864 -------------------------------------
2865 -- Analyze_Formal_Type_Declaration --
2866 -------------------------------------
2868 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2869 Def : constant Node_Id := Formal_Type_Definition (N);
2870 T : Entity_Id;
2872 begin
2873 T := Defining_Identifier (N);
2875 if Present (Discriminant_Specifications (N))
2876 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2877 then
2878 Error_Msg_N
2879 ("discriminants not allowed for this formal type", T);
2880 end if;
2882 -- Enter the new name, and branch to specific routine
2884 case Nkind (Def) is
2885 when N_Formal_Private_Type_Definition =>
2886 Analyze_Formal_Private_Type (N, T, Def);
2888 when N_Formal_Derived_Type_Definition =>
2889 Analyze_Formal_Derived_Type (N, T, Def);
2891 when N_Formal_Incomplete_Type_Definition =>
2892 Analyze_Formal_Incomplete_Type (T, Def);
2894 when N_Formal_Discrete_Type_Definition =>
2895 Analyze_Formal_Discrete_Type (T, Def);
2897 when N_Formal_Signed_Integer_Type_Definition =>
2898 Analyze_Formal_Signed_Integer_Type (T, Def);
2900 when N_Formal_Modular_Type_Definition =>
2901 Analyze_Formal_Modular_Type (T, Def);
2903 when N_Formal_Floating_Point_Definition =>
2904 Analyze_Formal_Floating_Type (T, Def);
2906 when N_Formal_Ordinary_Fixed_Point_Definition =>
2907 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2909 when N_Formal_Decimal_Fixed_Point_Definition =>
2910 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2912 when N_Array_Type_Definition =>
2913 Analyze_Formal_Array_Type (T, Def);
2915 when N_Access_To_Object_Definition |
2916 N_Access_Function_Definition |
2917 N_Access_Procedure_Definition =>
2918 Analyze_Generic_Access_Type (T, Def);
2920 -- Ada 2005: a interface declaration is encoded as an abstract
2921 -- record declaration or a abstract type derivation.
2923 when N_Record_Definition =>
2924 Analyze_Formal_Interface_Type (N, T, Def);
2926 when N_Derived_Type_Definition =>
2927 Analyze_Formal_Derived_Interface_Type (N, T, Def);
2929 when N_Error =>
2930 null;
2932 when others =>
2933 raise Program_Error;
2935 end case;
2937 Set_Is_Generic_Type (T);
2939 if Has_Aspects (N) then
2940 Analyze_Aspect_Specifications (N, T);
2941 end if;
2942 end Analyze_Formal_Type_Declaration;
2944 ------------------------------------
2945 -- Analyze_Function_Instantiation --
2946 ------------------------------------
2948 procedure Analyze_Function_Instantiation (N : Node_Id) is
2949 begin
2950 Analyze_Subprogram_Instantiation (N, E_Function);
2951 end Analyze_Function_Instantiation;
2953 ---------------------------------
2954 -- Analyze_Generic_Access_Type --
2955 ---------------------------------
2957 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2958 begin
2959 Enter_Name (T);
2961 if Nkind (Def) = N_Access_To_Object_Definition then
2962 Access_Type_Declaration (T, Def);
2964 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2965 and then No (Full_View (Designated_Type (T)))
2966 and then not Is_Generic_Type (Designated_Type (T))
2967 then
2968 Error_Msg_N ("premature usage of incomplete type", Def);
2970 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
2971 Error_Msg_N
2972 ("only a subtype mark is allowed in a formal", Def);
2973 end if;
2975 else
2976 Access_Subprogram_Declaration (T, Def);
2977 end if;
2978 end Analyze_Generic_Access_Type;
2980 ---------------------------------
2981 -- Analyze_Generic_Formal_Part --
2982 ---------------------------------
2984 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2985 Gen_Parm_Decl : Node_Id;
2987 begin
2988 -- The generic formals are processed in the scope of the generic unit,
2989 -- where they are immediately visible. The scope is installed by the
2990 -- caller.
2992 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2993 while Present (Gen_Parm_Decl) loop
2994 Analyze (Gen_Parm_Decl);
2995 Next (Gen_Parm_Decl);
2996 end loop;
2998 Generate_Reference_To_Generic_Formals (Current_Scope);
2999 end Analyze_Generic_Formal_Part;
3001 ------------------------------------------
3002 -- Analyze_Generic_Package_Declaration --
3003 ------------------------------------------
3005 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3006 Loc : constant Source_Ptr := Sloc (N);
3007 Id : Entity_Id;
3008 New_N : Node_Id;
3009 Save_Parent : Node_Id;
3010 Renaming : Node_Id;
3011 Decls : constant List_Id :=
3012 Visible_Declarations (Specification (N));
3013 Decl : Node_Id;
3015 begin
3016 -- The generic package declaration may be subject to pragma Ghost with
3017 -- policy Ignore. Set the mode now to ensure that any nodes generated
3018 -- during analysis and expansion are properly flagged as ignored Ghost.
3020 Set_Ghost_Mode (N);
3021 Check_SPARK_05_Restriction ("generic is not allowed", N);
3023 -- We introduce a renaming of the enclosing package, to have a usable
3024 -- entity as the prefix of an expanded name for a local entity of the
3025 -- form Par.P.Q, where P is the generic package. This is because a local
3026 -- entity named P may hide it, so that the usual visibility rules in
3027 -- the instance will not resolve properly.
3029 Renaming :=
3030 Make_Package_Renaming_Declaration (Loc,
3031 Defining_Unit_Name =>
3032 Make_Defining_Identifier (Loc,
3033 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3034 Name =>
3035 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3037 if Present (Decls) then
3038 Decl := First (Decls);
3039 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3040 Next (Decl);
3041 end loop;
3043 if Present (Decl) then
3044 Insert_Before (Decl, Renaming);
3045 else
3046 Append (Renaming, Visible_Declarations (Specification (N)));
3047 end if;
3049 else
3050 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3051 end if;
3053 -- Create copy of generic unit, and save for instantiation. If the unit
3054 -- is a child unit, do not copy the specifications for the parent, which
3055 -- are not part of the generic tree.
3057 Save_Parent := Parent_Spec (N);
3058 Set_Parent_Spec (N, Empty);
3060 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3061 Set_Parent_Spec (New_N, Save_Parent);
3062 Rewrite (N, New_N);
3064 -- Once the contents of the generic copy and the template are swapped,
3065 -- do the same for their respective aspect specifications.
3067 Exchange_Aspects (N, New_N);
3068 Id := Defining_Entity (N);
3069 Generate_Definition (Id);
3071 -- Expansion is not applied to generic units
3073 Start_Generic;
3075 Enter_Name (Id);
3076 Set_Ekind (Id, E_Generic_Package);
3077 Set_Etype (Id, Standard_Void_Type);
3078 Set_Contract (Id, Make_Contract (Sloc (Id)));
3080 -- A generic package declared within a Ghost region is rendered Ghost
3081 -- (SPARK RM 6.9(2)).
3083 if Ghost_Mode > None then
3084 Set_Is_Ghost_Entity (Id);
3085 end if;
3087 -- Analyze aspects now, so that generated pragmas appear in the
3088 -- declarations before building and analyzing the generic copy.
3090 if Has_Aspects (N) then
3091 Analyze_Aspect_Specifications (N, Id);
3092 end if;
3094 Push_Scope (Id);
3095 Enter_Generic_Scope (Id);
3096 Set_Inner_Instances (Id, New_Elmt_List);
3098 Set_Categorization_From_Pragmas (N);
3099 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3101 -- Link the declaration of the generic homonym in the generic copy to
3102 -- the package it renames, so that it is always resolved properly.
3104 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3105 Set_Entity (Associated_Node (Name (Renaming)), Id);
3107 -- For a library unit, we have reconstructed the entity for the unit,
3108 -- and must reset it in the library tables.
3110 if Nkind (Parent (N)) = N_Compilation_Unit then
3111 Set_Cunit_Entity (Current_Sem_Unit, Id);
3112 end if;
3114 Analyze_Generic_Formal_Part (N);
3116 -- After processing the generic formals, analysis proceeds as for a
3117 -- non-generic package.
3119 Analyze (Specification (N));
3121 Validate_Categorization_Dependency (N, Id);
3123 End_Generic;
3125 End_Package_Scope (Id);
3126 Exit_Generic_Scope (Id);
3128 if Nkind (Parent (N)) /= N_Compilation_Unit then
3129 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3130 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3131 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3133 else
3134 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3135 Validate_RT_RAT_Component (N);
3137 -- If this is a spec without a body, check that generic parameters
3138 -- are referenced.
3140 if not Body_Required (Parent (N)) then
3141 Check_References (Id);
3142 end if;
3143 end if;
3145 -- If there is a specified storage pool in the context, create an
3146 -- aspect on the package declaration, so that it is used in any
3147 -- instance that does not override it.
3149 if Present (Default_Pool) then
3150 declare
3151 ASN : Node_Id;
3153 begin
3154 ASN :=
3155 Make_Aspect_Specification (Loc,
3156 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3157 Expression => New_Copy (Default_Pool));
3159 if No (Aspect_Specifications (Specification (N))) then
3160 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3161 else
3162 Append (ASN, Aspect_Specifications (Specification (N)));
3163 end if;
3164 end;
3165 end if;
3166 end Analyze_Generic_Package_Declaration;
3168 --------------------------------------------
3169 -- Analyze_Generic_Subprogram_Declaration --
3170 --------------------------------------------
3172 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3173 Spec : Node_Id;
3174 Id : Entity_Id;
3175 Formals : List_Id;
3176 New_N : Node_Id;
3177 Result_Type : Entity_Id;
3178 Save_Parent : Node_Id;
3179 Typ : Entity_Id;
3181 begin
3182 -- The generic subprogram declaration may be subject to pragma Ghost
3183 -- with policy Ignore. Set the mode now to ensure that any nodes
3184 -- generated during analysis and expansion are properly flagged as
3185 -- ignored Ghost.
3187 Set_Ghost_Mode (N);
3188 Check_SPARK_05_Restriction ("generic is not allowed", N);
3190 -- Create copy of generic unit, and save for instantiation. If the unit
3191 -- is a child unit, do not copy the specifications for the parent, which
3192 -- are not part of the generic tree.
3194 Save_Parent := Parent_Spec (N);
3195 Set_Parent_Spec (N, Empty);
3197 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3198 Set_Parent_Spec (New_N, Save_Parent);
3199 Rewrite (N, New_N);
3201 -- Once the contents of the generic copy and the template are swapped,
3202 -- do the same for their respective aspect specifications.
3204 Exchange_Aspects (N, New_N);
3206 Spec := Specification (N);
3207 Id := Defining_Entity (Spec);
3208 Generate_Definition (Id);
3209 Set_Contract (Id, Make_Contract (Sloc (Id)));
3211 if Nkind (Id) = N_Defining_Operator_Symbol then
3212 Error_Msg_N
3213 ("operator symbol not allowed for generic subprogram", Id);
3214 end if;
3216 Start_Generic;
3218 Enter_Name (Id);
3219 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3221 -- Analyze the aspects of the generic copy to ensure that all generated
3222 -- pragmas (if any) perform their semantic effects.
3224 if Has_Aspects (N) then
3225 Analyze_Aspect_Specifications (N, Id);
3226 end if;
3228 Push_Scope (Id);
3229 Enter_Generic_Scope (Id);
3230 Set_Inner_Instances (Id, New_Elmt_List);
3231 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3233 Analyze_Generic_Formal_Part (N);
3235 Formals := Parameter_Specifications (Spec);
3237 if Present (Formals) then
3238 Process_Formals (Formals, Spec);
3239 end if;
3241 if Nkind (Spec) = N_Function_Specification then
3242 Set_Ekind (Id, E_Generic_Function);
3244 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3245 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3246 Set_Etype (Id, Result_Type);
3248 -- Check restriction imposed by AI05-073: a generic function
3249 -- cannot return an abstract type or an access to such.
3251 -- This is a binding interpretation should it apply to earlier
3252 -- versions of Ada as well as Ada 2012???
3254 if Is_Abstract_Type (Designated_Type (Result_Type))
3255 and then Ada_Version >= Ada_2012
3256 then
3257 Error_Msg_N
3258 ("generic function cannot have an access result "
3259 & "that designates an abstract type", Spec);
3260 end if;
3262 else
3263 Find_Type (Result_Definition (Spec));
3264 Typ := Entity (Result_Definition (Spec));
3266 if Is_Abstract_Type (Typ)
3267 and then Ada_Version >= Ada_2012
3268 then
3269 Error_Msg_N
3270 ("generic function cannot have abstract result type", Spec);
3271 end if;
3273 -- If a null exclusion is imposed on the result type, then create
3274 -- a null-excluding itype (an access subtype) and use it as the
3275 -- function's Etype.
3277 if Is_Access_Type (Typ)
3278 and then Null_Exclusion_Present (Spec)
3279 then
3280 Set_Etype (Id,
3281 Create_Null_Excluding_Itype
3282 (T => Typ,
3283 Related_Nod => Spec,
3284 Scope_Id => Defining_Unit_Name (Spec)));
3285 else
3286 Set_Etype (Id, Typ);
3287 end if;
3288 end if;
3290 else
3291 Set_Ekind (Id, E_Generic_Procedure);
3292 Set_Etype (Id, Standard_Void_Type);
3293 end if;
3295 -- A generic subprogram declared within a Ghost region is rendered Ghost
3296 -- (SPARK RM 6.9(2)).
3298 if Ghost_Mode > None then
3299 Set_Is_Ghost_Entity (Id);
3300 end if;
3302 -- For a library unit, we have reconstructed the entity for the unit,
3303 -- and must reset it in the library tables. We also make sure that
3304 -- Body_Required is set properly in the original compilation unit node.
3306 if Nkind (Parent (N)) = N_Compilation_Unit then
3307 Set_Cunit_Entity (Current_Sem_Unit, Id);
3308 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3309 end if;
3311 Set_Categorization_From_Pragmas (N);
3312 Validate_Categorization_Dependency (N, Id);
3314 Save_Global_References (Original_Node (N));
3316 -- For ASIS purposes, convert any postcondition, precondition pragmas
3317 -- into aspects, if N is not a compilation unit by itself, in order to
3318 -- enable the analysis of expressions inside the corresponding PPC
3319 -- pragmas.
3321 if ASIS_Mode and then Is_List_Member (N) then
3322 Make_Aspect_For_PPC_In_Gen_Sub_Decl (N);
3323 end if;
3325 End_Generic;
3326 End_Scope;
3327 Exit_Generic_Scope (Id);
3328 Generate_Reference_To_Formals (Id);
3330 List_Inherited_Pre_Post_Aspects (Id);
3331 end Analyze_Generic_Subprogram_Declaration;
3333 -----------------------------------
3334 -- Analyze_Package_Instantiation --
3335 -----------------------------------
3337 procedure Analyze_Package_Instantiation (N : Node_Id) is
3338 Loc : constant Source_Ptr := Sloc (N);
3339 Gen_Id : constant Node_Id := Name (N);
3341 Act_Decl : Node_Id;
3342 Act_Decl_Name : Node_Id;
3343 Act_Decl_Id : Entity_Id;
3344 Act_Spec : Node_Id;
3345 Act_Tree : Node_Id;
3347 Gen_Decl : Node_Id;
3348 Gen_Spec : Node_Id;
3349 Gen_Unit : Entity_Id;
3351 Is_Actual_Pack : constant Boolean :=
3352 Is_Internal (Defining_Entity (N));
3354 Env_Installed : Boolean := False;
3355 Parent_Installed : Boolean := False;
3356 Renaming_List : List_Id;
3357 Unit_Renaming : Node_Id;
3358 Needs_Body : Boolean;
3359 Inline_Now : Boolean := False;
3360 Has_Inline_Always : Boolean := False;
3362 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3363 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3365 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3366 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3367 -- Save the SPARK_Mode-related data for restore on exit
3369 Save_Style_Check : constant Boolean := Style_Check;
3370 -- Save style check mode for restore on exit
3372 procedure Delay_Descriptors (E : Entity_Id);
3373 -- Delay generation of subprogram descriptors for given entity
3375 function Might_Inline_Subp return Boolean;
3376 -- If inlining is active and the generic contains inlined subprograms,
3377 -- we instantiate the body. This may cause superfluous instantiations,
3378 -- but it is simpler than detecting the need for the body at the point
3379 -- of inlining, when the context of the instance is not available.
3381 -----------------------
3382 -- Delay_Descriptors --
3383 -----------------------
3385 procedure Delay_Descriptors (E : Entity_Id) is
3386 begin
3387 if not Delay_Subprogram_Descriptors (E) then
3388 Set_Delay_Subprogram_Descriptors (E);
3389 Pending_Descriptor.Append (E);
3390 end if;
3391 end Delay_Descriptors;
3393 -----------------------
3394 -- Might_Inline_Subp --
3395 -----------------------
3397 function Might_Inline_Subp return Boolean is
3398 E : Entity_Id;
3400 begin
3401 if not Inline_Processing_Required then
3402 return False;
3404 else
3405 E := First_Entity (Gen_Unit);
3406 while Present (E) loop
3407 if Is_Subprogram (E) and then Is_Inlined (E) then
3408 -- Remember if there are any subprograms with Inline_Always
3410 if Has_Pragma_Inline_Always (E) then
3411 Has_Inline_Always := True;
3412 end if;
3414 return True;
3415 end if;
3417 Next_Entity (E);
3418 end loop;
3419 end if;
3421 return False;
3422 end Might_Inline_Subp;
3424 -- Local declarations
3426 Vis_Prims_List : Elist_Id := No_Elist;
3427 -- List of primitives made temporarily visible in the instantiation
3428 -- to match the visibility of the formal type
3430 -- Start of processing for Analyze_Package_Instantiation
3432 begin
3433 Check_SPARK_05_Restriction ("generic is not allowed", N);
3435 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3436 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3438 Check_Text_IO_Special_Unit (Name (N));
3440 -- Make node global for error reporting
3442 Instantiation_Node := N;
3444 -- Turn off style checking in instances. If the check is enabled on the
3445 -- generic unit, a warning in an instance would just be noise. If not
3446 -- enabled on the generic, then a warning in an instance is just wrong.
3448 Style_Check := False;
3450 -- Case of instantiation of a generic package
3452 if Nkind (N) = N_Package_Instantiation then
3453 Act_Decl_Id := New_Copy (Defining_Entity (N));
3454 Set_Comes_From_Source (Act_Decl_Id, True);
3456 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3457 Act_Decl_Name :=
3458 Make_Defining_Program_Unit_Name (Loc,
3459 Name =>
3460 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3461 Defining_Identifier => Act_Decl_Id);
3462 else
3463 Act_Decl_Name := Act_Decl_Id;
3464 end if;
3466 -- Case of instantiation of a formal package
3468 else
3469 Act_Decl_Id := Defining_Identifier (N);
3470 Act_Decl_Name := Act_Decl_Id;
3471 end if;
3473 Generate_Definition (Act_Decl_Id);
3474 Preanalyze_Actuals (N);
3476 Init_Env;
3477 Env_Installed := True;
3479 -- Reset renaming map for formal types. The mapping is established
3480 -- when analyzing the generic associations, but some mappings are
3481 -- inherited from formal packages of parent units, and these are
3482 -- constructed when the parents are installed.
3484 Generic_Renamings.Set_Last (0);
3485 Generic_Renamings_HTable.Reset;
3487 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3488 Gen_Unit := Entity (Gen_Id);
3490 -- Verify that it is the name of a generic package
3492 -- A visibility glitch: if the instance is a child unit and the generic
3493 -- is the generic unit of a parent instance (i.e. both the parent and
3494 -- the child units are instances of the same package) the name now
3495 -- denotes the renaming within the parent, not the intended generic
3496 -- unit. See if there is a homonym that is the desired generic. The
3497 -- renaming declaration must be visible inside the instance of the
3498 -- child, but not when analyzing the name in the instantiation itself.
3500 if Ekind (Gen_Unit) = E_Package
3501 and then Present (Renamed_Entity (Gen_Unit))
3502 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3503 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3504 and then Present (Homonym (Gen_Unit))
3505 then
3506 Gen_Unit := Homonym (Gen_Unit);
3507 end if;
3509 if Etype (Gen_Unit) = Any_Type then
3510 Restore_Env;
3511 goto Leave;
3513 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3515 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3517 if From_Limited_With (Gen_Unit) then
3518 Error_Msg_N
3519 ("cannot instantiate a limited withed package", Gen_Id);
3520 else
3521 Error_Msg_NE
3522 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3523 end if;
3525 Restore_Env;
3526 goto Leave;
3527 end if;
3529 if In_Extended_Main_Source_Unit (N) then
3530 Set_Is_Instantiated (Gen_Unit);
3531 Generate_Reference (Gen_Unit, N);
3533 if Present (Renamed_Object (Gen_Unit)) then
3534 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3535 Generate_Reference (Renamed_Object (Gen_Unit), N);
3536 end if;
3537 end if;
3539 if Nkind (Gen_Id) = N_Identifier
3540 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3541 then
3542 Error_Msg_NE
3543 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3545 elsif Nkind (Gen_Id) = N_Expanded_Name
3546 and then Is_Child_Unit (Gen_Unit)
3547 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3548 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3549 then
3550 Error_Msg_N
3551 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3552 end if;
3554 Set_Entity (Gen_Id, Gen_Unit);
3556 -- If generic is a renaming, get original generic unit
3558 if Present (Renamed_Object (Gen_Unit))
3559 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3560 then
3561 Gen_Unit := Renamed_Object (Gen_Unit);
3562 end if;
3564 -- Verify that there are no circular instantiations
3566 if In_Open_Scopes (Gen_Unit) then
3567 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3568 Restore_Env;
3569 goto Leave;
3571 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3572 Error_Msg_Node_2 := Current_Scope;
3573 Error_Msg_NE
3574 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3575 Circularity_Detected := True;
3576 Restore_Env;
3577 goto Leave;
3579 else
3580 -- If the context of the instance is subject to SPARK_Mode "off",
3581 -- set the global flag which signals Analyze_Pragma to ignore all
3582 -- SPARK_Mode pragmas within the instance.
3584 if SPARK_Mode = Off then
3585 Ignore_Pragma_SPARK_Mode := True;
3586 end if;
3588 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3589 Gen_Spec := Specification (Gen_Decl);
3591 -- Initialize renamings map, for error checking, and the list that
3592 -- holds private entities whose views have changed between generic
3593 -- definition and instantiation. If this is the instance created to
3594 -- validate an actual package, the instantiation environment is that
3595 -- of the enclosing instance.
3597 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3599 -- Copy original generic tree, to produce text for instantiation
3601 Act_Tree :=
3602 Copy_Generic_Node
3603 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3605 Act_Spec := Specification (Act_Tree);
3607 -- If this is the instance created to validate an actual package,
3608 -- only the formals matter, do not examine the package spec itself.
3610 if Is_Actual_Pack then
3611 Set_Visible_Declarations (Act_Spec, New_List);
3612 Set_Private_Declarations (Act_Spec, New_List);
3613 end if;
3615 Renaming_List :=
3616 Analyze_Associations
3617 (I_Node => N,
3618 Formals => Generic_Formal_Declarations (Act_Tree),
3619 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3621 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3623 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3624 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3625 Set_Is_Generic_Instance (Act_Decl_Id);
3626 Set_Generic_Parent (Act_Spec, Gen_Unit);
3628 -- References to the generic in its own declaration or its body are
3629 -- references to the instance. Add a renaming declaration for the
3630 -- generic unit itself. This declaration, as well as the renaming
3631 -- declarations for the generic formals, must remain private to the
3632 -- unit: the formals, because this is the language semantics, and
3633 -- the unit because its use is an artifact of the implementation.
3635 Unit_Renaming :=
3636 Make_Package_Renaming_Declaration (Loc,
3637 Defining_Unit_Name =>
3638 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3639 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3641 Append (Unit_Renaming, Renaming_List);
3643 -- The renaming declarations are the first local declarations of the
3644 -- new unit.
3646 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3647 Insert_List_Before
3648 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3649 else
3650 Set_Visible_Declarations (Act_Spec, Renaming_List);
3651 end if;
3653 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3655 -- Propagate the aspect specifications from the package declaration
3656 -- template to the instantiated version of the package declaration.
3658 if Has_Aspects (Act_Tree) then
3659 Set_Aspect_Specifications (Act_Decl,
3660 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3661 end if;
3663 -- The generic may have a generated Default_Storage_Pool aspect,
3664 -- set at the point of generic declaration. If the instance has
3665 -- that aspect, it overrides the one inherited from the generic.
3667 if Has_Aspects (Gen_Spec) then
3668 if No (Aspect_Specifications (N)) then
3669 Set_Aspect_Specifications (N,
3670 (New_Copy_List_Tree
3671 (Aspect_Specifications (Gen_Spec))));
3673 else
3674 declare
3675 ASN1, ASN2 : Node_Id;
3677 begin
3678 ASN1 := First (Aspect_Specifications (N));
3679 while Present (ASN1) loop
3680 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3681 then
3682 -- If generic carries a default storage pool, remove
3683 -- it in favor of the instance one.
3685 ASN2 := First (Aspect_Specifications (Gen_Spec));
3686 while Present (ASN2) loop
3687 if Chars (Identifier (ASN2)) =
3688 Name_Default_Storage_Pool
3689 then
3690 Remove (ASN2);
3691 exit;
3692 end if;
3694 Next (ASN2);
3695 end loop;
3696 end if;
3698 Next (ASN1);
3699 end loop;
3701 Prepend_List_To (Aspect_Specifications (N),
3702 (New_Copy_List_Tree
3703 (Aspect_Specifications (Gen_Spec))));
3704 end;
3705 end if;
3706 end if;
3708 -- Save the instantiation node, for subsequent instantiation of the
3709 -- body, if there is one and we are generating code for the current
3710 -- unit. Mark unit as having a body (avoids premature error message).
3712 -- We instantiate the body if we are generating code, if we are
3713 -- generating cross-reference information, or if we are building
3714 -- trees for ASIS use or GNATprove use.
3716 declare
3717 Enclosing_Body_Present : Boolean := False;
3718 -- If the generic unit is not a compilation unit, then a body may
3719 -- be present in its parent even if none is required. We create a
3720 -- tentative pending instantiation for the body, which will be
3721 -- discarded if none is actually present.
3723 Scop : Entity_Id;
3725 begin
3726 if Scope (Gen_Unit) /= Standard_Standard
3727 and then not Is_Child_Unit (Gen_Unit)
3728 then
3729 Scop := Scope (Gen_Unit);
3730 while Present (Scop) and then Scop /= Standard_Standard loop
3731 if Unit_Requires_Body (Scop) then
3732 Enclosing_Body_Present := True;
3733 exit;
3735 elsif In_Open_Scopes (Scop)
3736 and then In_Package_Body (Scop)
3737 then
3738 Enclosing_Body_Present := True;
3739 exit;
3740 end if;
3742 exit when Is_Compilation_Unit (Scop);
3743 Scop := Scope (Scop);
3744 end loop;
3745 end if;
3747 -- If front-end inlining is enabled or there are any subprograms
3748 -- marked with Inline_Always, and this is a unit for which code
3749 -- will be generated, we instantiate the body at once.
3751 -- This is done if the instance is not the main unit, and if the
3752 -- generic is not a child unit of another generic, to avoid scope
3753 -- problems and the reinstallation of parent instances.
3755 if Expander_Active
3756 and then (not Is_Child_Unit (Gen_Unit)
3757 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3758 and then Might_Inline_Subp
3759 and then not Is_Actual_Pack
3760 then
3761 if not Back_End_Inlining
3762 and then (Front_End_Inlining or else Has_Inline_Always)
3763 and then (Is_In_Main_Unit (N)
3764 or else In_Main_Context (Current_Scope))
3765 and then Nkind (Parent (N)) /= N_Compilation_Unit
3766 then
3767 Inline_Now := True;
3769 -- In configurable_run_time mode we force the inlining of
3770 -- predefined subprograms marked Inline_Always, to minimize
3771 -- the use of the run-time library.
3773 elsif Is_Predefined_File_Name
3774 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3775 and then Configurable_Run_Time_Mode
3776 and then Nkind (Parent (N)) /= N_Compilation_Unit
3777 then
3778 Inline_Now := True;
3779 end if;
3781 -- If the current scope is itself an instance within a child
3782 -- unit, there will be duplications in the scope stack, and the
3783 -- unstacking mechanism in Inline_Instance_Body will fail.
3784 -- This loses some rare cases of optimization, and might be
3785 -- improved some day, if we can find a proper abstraction for
3786 -- "the complete compilation context" that can be saved and
3787 -- restored. ???
3789 if Is_Generic_Instance (Current_Scope) then
3790 declare
3791 Curr_Unit : constant Entity_Id :=
3792 Cunit_Entity (Current_Sem_Unit);
3793 begin
3794 if Curr_Unit /= Current_Scope
3795 and then Is_Child_Unit (Curr_Unit)
3796 then
3797 Inline_Now := False;
3798 end if;
3799 end;
3800 end if;
3801 end if;
3803 Needs_Body :=
3804 (Unit_Requires_Body (Gen_Unit)
3805 or else Enclosing_Body_Present
3806 or else Present (Corresponding_Body (Gen_Decl)))
3807 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3808 and then not Is_Actual_Pack
3809 and then not Inline_Now
3810 and then (Operating_Mode = Generate_Code
3812 -- Need comment for this check ???
3814 or else (Operating_Mode = Check_Semantics
3815 and then (ASIS_Mode or GNATprove_Mode)));
3817 -- If front-end inlining is enabled or there are any subprograms
3818 -- marked with Inline_Always, do not instantiate body when within
3819 -- a generic context.
3821 if ((Front_End_Inlining or else Has_Inline_Always)
3822 and then not Expander_Active)
3823 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3824 then
3825 Needs_Body := False;
3826 end if;
3828 -- If the current context is generic, and the package being
3829 -- instantiated is declared within a formal package, there is no
3830 -- body to instantiate until the enclosing generic is instantiated
3831 -- and there is an actual for the formal package. If the formal
3832 -- package has parameters, we build a regular package instance for
3833 -- it, that precedes the original formal package declaration.
3835 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3836 declare
3837 Decl : constant Node_Id :=
3838 Original_Node
3839 (Unit_Declaration_Node (Scope (Gen_Unit)));
3840 begin
3841 if Nkind (Decl) = N_Formal_Package_Declaration
3842 or else (Nkind (Decl) = N_Package_Declaration
3843 and then Is_List_Member (Decl)
3844 and then Present (Next (Decl))
3845 and then
3846 Nkind (Next (Decl)) =
3847 N_Formal_Package_Declaration)
3848 then
3849 Needs_Body := False;
3850 end if;
3851 end;
3852 end if;
3853 end;
3855 -- For RCI unit calling stubs, we omit the instance body if the
3856 -- instance is the RCI library unit itself.
3858 -- However there is a special case for nested instances: in this case
3859 -- we do generate the instance body, as it might be required, e.g.
3860 -- because it provides stream attributes for some type used in the
3861 -- profile of a remote subprogram. This is consistent with 12.3(12),
3862 -- which indicates that the instance body occurs at the place of the
3863 -- instantiation, and thus is part of the RCI declaration, which is
3864 -- present on all client partitions (this is E.2.3(18)).
3866 -- Note that AI12-0002 may make it illegal at some point to have
3867 -- stream attributes defined in an RCI unit, in which case this
3868 -- special case will become unnecessary. In the meantime, there
3869 -- is known application code in production that depends on this
3870 -- being possible, so we definitely cannot eliminate the body in
3871 -- the case of nested instances for the time being.
3873 -- When we generate a nested instance body, calling stubs for any
3874 -- relevant subprogram will be be inserted immediately after the
3875 -- subprogram declarations, and will take precedence over the
3876 -- subsequent (original) body. (The stub and original body will be
3877 -- complete homographs, but this is permitted in an instance).
3878 -- (Could we do better and remove the original body???)
3880 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
3881 and then Comes_From_Source (N)
3882 and then Nkind (Parent (N)) = N_Compilation_Unit
3883 then
3884 Needs_Body := False;
3885 end if;
3887 if Needs_Body then
3889 -- Here is a defence against a ludicrous number of instantiations
3890 -- caused by a circular set of instantiation attempts.
3892 if Pending_Instantiations.Last > Maximum_Instantiations then
3893 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
3894 Error_Msg_N ("too many instantiations, exceeds max of^", N);
3895 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
3896 raise Unrecoverable_Error;
3897 end if;
3899 -- Indicate that the enclosing scopes contain an instantiation,
3900 -- and that cleanup actions should be delayed until after the
3901 -- instance body is expanded.
3903 Check_Forward_Instantiation (Gen_Decl);
3904 if Nkind (N) = N_Package_Instantiation then
3905 declare
3906 Enclosing_Master : Entity_Id;
3908 begin
3909 -- Loop to search enclosing masters
3911 Enclosing_Master := Current_Scope;
3912 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
3913 if Ekind (Enclosing_Master) = E_Package then
3914 if Is_Compilation_Unit (Enclosing_Master) then
3915 if In_Package_Body (Enclosing_Master) then
3916 Delay_Descriptors
3917 (Body_Entity (Enclosing_Master));
3918 else
3919 Delay_Descriptors
3920 (Enclosing_Master);
3921 end if;
3923 exit Scope_Loop;
3925 else
3926 Enclosing_Master := Scope (Enclosing_Master);
3927 end if;
3929 elsif Is_Generic_Unit (Enclosing_Master)
3930 or else Ekind (Enclosing_Master) = E_Void
3931 then
3932 -- Cleanup actions will eventually be performed on the
3933 -- enclosing subprogram or package instance, if any.
3934 -- Enclosing scope is void in the formal part of a
3935 -- generic subprogram.
3937 exit Scope_Loop;
3939 else
3940 if Ekind (Enclosing_Master) = E_Entry
3941 and then
3942 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
3943 then
3944 if not Expander_Active then
3945 exit Scope_Loop;
3946 else
3947 Enclosing_Master :=
3948 Protected_Body_Subprogram (Enclosing_Master);
3949 end if;
3950 end if;
3952 Set_Delay_Cleanups (Enclosing_Master);
3954 while Ekind (Enclosing_Master) = E_Block loop
3955 Enclosing_Master := Scope (Enclosing_Master);
3956 end loop;
3958 if Is_Subprogram (Enclosing_Master) then
3959 Delay_Descriptors (Enclosing_Master);
3961 elsif Is_Task_Type (Enclosing_Master) then
3962 declare
3963 TBP : constant Node_Id :=
3964 Get_Task_Body_Procedure
3965 (Enclosing_Master);
3966 begin
3967 if Present (TBP) then
3968 Delay_Descriptors (TBP);
3969 Set_Delay_Cleanups (TBP);
3970 end if;
3971 end;
3972 end if;
3974 exit Scope_Loop;
3975 end if;
3976 end loop Scope_Loop;
3977 end;
3979 -- Make entry in table
3981 Pending_Instantiations.Append
3982 ((Inst_Node => N,
3983 Act_Decl => Act_Decl,
3984 Expander_Status => Expander_Active,
3985 Current_Sem_Unit => Current_Sem_Unit,
3986 Scope_Suppress => Scope_Suppress,
3987 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
3988 Version => Ada_Version,
3989 Version_Pragma => Ada_Version_Pragma,
3990 Warnings => Save_Warnings,
3991 SPARK_Mode => SPARK_Mode,
3992 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
3993 end if;
3994 end if;
3996 Set_Categorization_From_Pragmas (Act_Decl);
3998 if Parent_Installed then
3999 Hide_Current_Scope;
4000 end if;
4002 Set_Instance_Spec (N, Act_Decl);
4004 -- If not a compilation unit, insert the package declaration before
4005 -- the original instantiation node.
4007 if Nkind (Parent (N)) /= N_Compilation_Unit then
4008 Mark_Rewrite_Insertion (Act_Decl);
4009 Insert_Before (N, Act_Decl);
4011 if Has_Aspects (N) then
4012 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4014 -- The pragma created for a Default_Storage_Pool aspect must
4015 -- appear ahead of the declarations in the instance spec.
4016 -- Analysis has placed it after the instance node, so remove
4017 -- it and reinsert it properly now.
4019 declare
4020 ASN : constant Node_Id := First (Aspect_Specifications (N));
4021 A_Name : constant Name_Id := Chars (Identifier (ASN));
4022 Decl : Node_Id;
4024 begin
4025 if A_Name = Name_Default_Storage_Pool then
4026 if No (Visible_Declarations (Act_Spec)) then
4027 Set_Visible_Declarations (Act_Spec, New_List);
4028 end if;
4030 Decl := Next (N);
4031 while Present (Decl) loop
4032 if Nkind (Decl) = N_Pragma then
4033 Remove (Decl);
4034 Prepend (Decl, Visible_Declarations (Act_Spec));
4035 exit;
4036 end if;
4038 Next (Decl);
4039 end loop;
4040 end if;
4041 end;
4042 end if;
4044 Analyze (Act_Decl);
4046 -- For an instantiation that is a compilation unit, place
4047 -- declaration on current node so context is complete for analysis
4048 -- (including nested instantiations). If this is the main unit,
4049 -- the declaration eventually replaces the instantiation node.
4050 -- If the instance body is created later, it replaces the
4051 -- instance node, and the declaration is attached to it
4052 -- (see Build_Instance_Compilation_Unit_Nodes).
4054 else
4055 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4057 -- The entity for the current unit is the newly created one,
4058 -- and all semantic information is attached to it.
4060 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4062 -- If this is the main unit, replace the main entity as well
4064 if Current_Sem_Unit = Main_Unit then
4065 Main_Unit_Entity := Act_Decl_Id;
4066 end if;
4067 end if;
4069 Set_Unit (Parent (N), Act_Decl);
4070 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4071 Set_Package_Instantiation (Act_Decl_Id, N);
4073 -- Process aspect specifications of the instance node, if any, to
4074 -- take into account categorization pragmas before analyzing the
4075 -- instance.
4077 if Has_Aspects (N) then
4078 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4079 end if;
4081 Analyze (Act_Decl);
4082 Set_Unit (Parent (N), N);
4083 Set_Body_Required (Parent (N), False);
4085 -- We never need elaboration checks on instantiations, since by
4086 -- definition, the body instantiation is elaborated at the same
4087 -- time as the spec instantiation.
4089 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4090 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4091 end if;
4093 Check_Elab_Instantiation (N);
4095 if ABE_Is_Certain (N) and then Needs_Body then
4096 Pending_Instantiations.Decrement_Last;
4097 end if;
4099 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4101 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4102 First_Private_Entity (Act_Decl_Id));
4104 -- If the instantiation will receive a body, the unit will be
4105 -- transformed into a package body, and receive its own elaboration
4106 -- entity. Otherwise, the nature of the unit is now a package
4107 -- declaration.
4109 if Nkind (Parent (N)) = N_Compilation_Unit
4110 and then not Needs_Body
4111 then
4112 Rewrite (N, Act_Decl);
4113 end if;
4115 if Present (Corresponding_Body (Gen_Decl))
4116 or else Unit_Requires_Body (Gen_Unit)
4117 then
4118 Set_Has_Completion (Act_Decl_Id);
4119 end if;
4121 Check_Formal_Packages (Act_Decl_Id);
4123 Restore_Hidden_Primitives (Vis_Prims_List);
4124 Restore_Private_Views (Act_Decl_Id);
4126 Inherit_Context (Gen_Decl, N);
4128 if Parent_Installed then
4129 Remove_Parent;
4130 end if;
4132 Restore_Env;
4133 Env_Installed := False;
4134 end if;
4136 Validate_Categorization_Dependency (N, Act_Decl_Id);
4138 -- There used to be a check here to prevent instantiations in local
4139 -- contexts if the No_Local_Allocators restriction was active. This
4140 -- check was removed by a binding interpretation in AI-95-00130/07,
4141 -- but we retain the code for documentation purposes.
4143 -- if Ekind (Act_Decl_Id) /= E_Void
4144 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4145 -- then
4146 -- Check_Restriction (No_Local_Allocators, N);
4147 -- end if;
4149 if Inline_Now then
4150 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4151 end if;
4153 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4154 -- be used as defining identifiers for a formal package and for the
4155 -- corresponding expanded package.
4157 if Nkind (N) = N_Formal_Package_Declaration then
4158 Act_Decl_Id := New_Copy (Defining_Entity (N));
4159 Set_Comes_From_Source (Act_Decl_Id, True);
4160 Set_Is_Generic_Instance (Act_Decl_Id, False);
4161 Set_Defining_Identifier (N, Act_Decl_Id);
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;
4173 -- Check that if N is an instantiation of System.Dim_Float_IO or
4174 -- System.Dim_Integer_IO, the formal type has a dimension system.
4176 if Nkind (N) = N_Package_Instantiation
4177 and then Is_Dim_IO_Package_Instantiation (N)
4178 then
4179 declare
4180 Assoc : constant Node_Id := First (Generic_Associations (N));
4181 begin
4182 if not Has_Dimension_System
4183 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4184 then
4185 Error_Msg_N ("type with a dimension system expected", Assoc);
4186 end if;
4187 end;
4188 end if;
4190 <<Leave>>
4191 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4192 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4193 end if;
4195 exception
4196 when Instantiation_Error =>
4197 if Parent_Installed then
4198 Remove_Parent;
4199 end if;
4201 if Env_Installed then
4202 Restore_Env;
4203 end if;
4205 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4206 SPARK_Mode := Save_SM;
4207 SPARK_Mode_Pragma := Save_SMP;
4208 Style_Check := Save_Style_Check;
4210 if SPARK_Mode = On then
4211 Dynamic_Elaboration_Checks := False;
4212 end if;
4213 end Analyze_Package_Instantiation;
4215 --------------------------
4216 -- Inline_Instance_Body --
4217 --------------------------
4219 procedure Inline_Instance_Body
4220 (N : Node_Id;
4221 Gen_Unit : Entity_Id;
4222 Act_Decl : Node_Id)
4224 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4225 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4226 Gen_Comp : constant Entity_Id :=
4227 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4229 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4230 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4231 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4232 -- to provide a clean environment for analysis of the inlined body will
4233 -- eliminate any previously set SPARK_Mode.
4235 Scope_Stack_Depth : constant Int :=
4236 Scope_Stack.Last - Scope_Stack.First + 1;
4238 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4239 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4240 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4241 Curr_Scope : Entity_Id := Empty;
4242 List : Elist_Id;
4243 Num_Inner : Int := 0;
4244 Num_Scopes : Int := 0;
4245 N_Instances : Int := 0;
4246 Removed : Boolean := False;
4247 S : Entity_Id;
4248 Vis : Boolean;
4250 begin
4251 -- Case of generic unit defined in another unit. We must remove the
4252 -- complete context of the current unit to install that of the generic.
4254 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4256 -- Add some comments for the following two loops ???
4258 S := Current_Scope;
4259 while Present (S) and then S /= Standard_Standard loop
4260 loop
4261 Num_Scopes := Num_Scopes + 1;
4263 Use_Clauses (Num_Scopes) :=
4264 (Scope_Stack.Table
4265 (Scope_Stack.Last - Num_Scopes + 1).
4266 First_Use_Clause);
4267 End_Use_Clauses (Use_Clauses (Num_Scopes));
4269 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4270 or else Scope_Stack.Table
4271 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4272 end loop;
4274 exit when Is_Generic_Instance (S)
4275 and then (In_Package_Body (S)
4276 or else Ekind (S) = E_Procedure
4277 or else Ekind (S) = E_Function);
4278 S := Scope (S);
4279 end loop;
4281 Vis := Is_Immediately_Visible (Gen_Comp);
4283 -- Find and save all enclosing instances
4285 S := Current_Scope;
4287 while Present (S)
4288 and then S /= Standard_Standard
4289 loop
4290 if Is_Generic_Instance (S) then
4291 N_Instances := N_Instances + 1;
4292 Instances (N_Instances) := S;
4294 exit when In_Package_Body (S);
4295 end if;
4297 S := Scope (S);
4298 end loop;
4300 -- Remove context of current compilation unit, unless we are within a
4301 -- nested package instantiation, in which case the context has been
4302 -- removed previously.
4304 -- If current scope is the body of a child unit, remove context of
4305 -- spec as well. If an enclosing scope is an instance body, the
4306 -- context has already been removed, but the entities in the body
4307 -- must be made invisible as well.
4309 S := Current_Scope;
4310 while Present (S) and then S /= Standard_Standard loop
4311 if Is_Generic_Instance (S)
4312 and then (In_Package_Body (S)
4313 or else Ekind_In (S, E_Procedure, E_Function))
4314 then
4315 -- We still have to remove the entities of the enclosing
4316 -- instance from direct visibility.
4318 declare
4319 E : Entity_Id;
4320 begin
4321 E := First_Entity (S);
4322 while Present (E) loop
4323 Set_Is_Immediately_Visible (E, False);
4324 Next_Entity (E);
4325 end loop;
4326 end;
4328 exit;
4329 end if;
4331 if S = Curr_Unit
4332 or else (Ekind (Curr_Unit) = E_Package_Body
4333 and then S = Spec_Entity (Curr_Unit))
4334 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4335 and then S = Corresponding_Spec
4336 (Unit_Declaration_Node (Curr_Unit)))
4337 then
4338 Removed := True;
4340 -- Remove entities in current scopes from visibility, so that
4341 -- instance body is compiled in a clean environment.
4343 List := Save_Scope_Stack (Handle_Use => False);
4345 if Is_Child_Unit (S) then
4347 -- Remove child unit from stack, as well as inner scopes.
4348 -- Removing the context of a child unit removes parent units
4349 -- as well.
4351 while Current_Scope /= S loop
4352 Num_Inner := Num_Inner + 1;
4353 Inner_Scopes (Num_Inner) := Current_Scope;
4354 Pop_Scope;
4355 end loop;
4357 Pop_Scope;
4358 Remove_Context (Curr_Comp);
4359 Curr_Scope := S;
4361 else
4362 Remove_Context (Curr_Comp);
4363 end if;
4365 if Ekind (Curr_Unit) = E_Package_Body then
4366 Remove_Context (Library_Unit (Curr_Comp));
4367 end if;
4368 end if;
4370 S := Scope (S);
4371 end loop;
4373 pragma Assert (Num_Inner < Num_Scopes);
4375 -- The inlined package body must be analyzed with the SPARK_Mode of
4376 -- the enclosing context, otherwise the body may cause bogus errors
4377 -- if a configuration SPARK_Mode pragma in in effect.
4379 Push_Scope (Standard_Standard);
4380 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4381 Instantiate_Package_Body
4382 (Body_Info =>
4383 ((Inst_Node => N,
4384 Act_Decl => Act_Decl,
4385 Expander_Status => Expander_Active,
4386 Current_Sem_Unit => Current_Sem_Unit,
4387 Scope_Suppress => Scope_Suppress,
4388 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4389 Version => Ada_Version,
4390 Version_Pragma => Ada_Version_Pragma,
4391 Warnings => Save_Warnings,
4392 SPARK_Mode => Save_SM,
4393 SPARK_Mode_Pragma => Save_SMP)),
4394 Inlined_Body => True);
4396 Pop_Scope;
4398 -- Restore context
4400 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4402 -- Reset Generic_Instance flag so that use clauses can be installed
4403 -- in the proper order. (See Use_One_Package for effect of enclosing
4404 -- instances on processing of use clauses).
4406 for J in 1 .. N_Instances loop
4407 Set_Is_Generic_Instance (Instances (J), False);
4408 end loop;
4410 if Removed then
4411 Install_Context (Curr_Comp);
4413 if Present (Curr_Scope)
4414 and then Is_Child_Unit (Curr_Scope)
4415 then
4416 Push_Scope (Curr_Scope);
4417 Set_Is_Immediately_Visible (Curr_Scope);
4419 -- Finally, restore inner scopes as well
4421 for J in reverse 1 .. Num_Inner loop
4422 Push_Scope (Inner_Scopes (J));
4423 end loop;
4424 end if;
4426 Restore_Scope_Stack (List, Handle_Use => False);
4428 if Present (Curr_Scope)
4429 and then
4430 (In_Private_Part (Curr_Scope)
4431 or else In_Package_Body (Curr_Scope))
4432 then
4433 -- Install private declaration of ancestor units, which are
4434 -- currently available. Restore_Scope_Stack and Install_Context
4435 -- only install the visible part of parents.
4437 declare
4438 Par : Entity_Id;
4439 begin
4440 Par := Scope (Curr_Scope);
4441 while (Present (Par)) and then Par /= Standard_Standard loop
4442 Install_Private_Declarations (Par);
4443 Par := Scope (Par);
4444 end loop;
4445 end;
4446 end if;
4447 end if;
4449 -- Restore use clauses. For a child unit, use clauses in the parents
4450 -- are restored when installing the context, so only those in inner
4451 -- scopes (and those local to the child unit itself) need to be
4452 -- installed explicitly.
4454 if Is_Child_Unit (Curr_Unit) and then Removed then
4455 for J in reverse 1 .. Num_Inner + 1 loop
4456 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4457 Use_Clauses (J);
4458 Install_Use_Clauses (Use_Clauses (J));
4459 end loop;
4461 else
4462 for J in reverse 1 .. Num_Scopes loop
4463 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4464 Use_Clauses (J);
4465 Install_Use_Clauses (Use_Clauses (J));
4466 end loop;
4467 end if;
4469 -- Restore status of instances. If one of them is a body, make its
4470 -- local entities visible again.
4472 declare
4473 E : Entity_Id;
4474 Inst : Entity_Id;
4476 begin
4477 for J in 1 .. N_Instances loop
4478 Inst := Instances (J);
4479 Set_Is_Generic_Instance (Inst, True);
4481 if In_Package_Body (Inst)
4482 or else Ekind_In (S, E_Procedure, E_Function)
4483 then
4484 E := First_Entity (Instances (J));
4485 while Present (E) loop
4486 Set_Is_Immediately_Visible (E);
4487 Next_Entity (E);
4488 end loop;
4489 end if;
4490 end loop;
4491 end;
4493 -- If generic unit is in current unit, current context is correct. Note
4494 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4495 -- enclosing scopes were removed.
4497 else
4498 Instantiate_Package_Body
4499 (Body_Info =>
4500 ((Inst_Node => N,
4501 Act_Decl => Act_Decl,
4502 Expander_Status => Expander_Active,
4503 Current_Sem_Unit => Current_Sem_Unit,
4504 Scope_Suppress => Scope_Suppress,
4505 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4506 Version => Ada_Version,
4507 Version_Pragma => Ada_Version_Pragma,
4508 Warnings => Save_Warnings,
4509 SPARK_Mode => SPARK_Mode,
4510 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4511 Inlined_Body => True);
4512 end if;
4513 end Inline_Instance_Body;
4515 -------------------------------------
4516 -- Analyze_Procedure_Instantiation --
4517 -------------------------------------
4519 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4520 begin
4521 Analyze_Subprogram_Instantiation (N, E_Procedure);
4522 end Analyze_Procedure_Instantiation;
4524 -----------------------------------
4525 -- Need_Subprogram_Instance_Body --
4526 -----------------------------------
4528 function Need_Subprogram_Instance_Body
4529 (N : Node_Id;
4530 Subp : Entity_Id) return Boolean
4532 begin
4533 -- Must be inlined (or inlined renaming)
4535 if (Is_In_Main_Unit (N)
4536 or else Is_Inlined (Subp)
4537 or else Is_Inlined (Alias (Subp)))
4539 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4541 and then (Operating_Mode = Generate_Code
4542 or else (Operating_Mode = Check_Semantics
4543 and then (ASIS_Mode or GNATprove_Mode)))
4545 -- The body is needed when generating code (full expansion), in ASIS
4546 -- mode for other tools, and in GNATprove mode (special expansion) for
4547 -- formal verification of the body itself.
4549 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4551 -- No point in inlining if ABE is inevitable
4553 and then not ABE_Is_Certain (N)
4555 -- Or if subprogram is eliminated
4557 and then not Is_Eliminated (Subp)
4558 then
4559 Pending_Instantiations.Append
4560 ((Inst_Node => N,
4561 Act_Decl => Unit_Declaration_Node (Subp),
4562 Expander_Status => Expander_Active,
4563 Current_Sem_Unit => Current_Sem_Unit,
4564 Scope_Suppress => Scope_Suppress,
4565 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4566 Version => Ada_Version,
4567 Version_Pragma => Ada_Version_Pragma,
4568 Warnings => Save_Warnings,
4569 SPARK_Mode => SPARK_Mode,
4570 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4571 return True;
4573 -- Here if not inlined, or we ignore the inlining
4575 else
4576 return False;
4577 end if;
4578 end Need_Subprogram_Instance_Body;
4580 --------------------------------------
4581 -- Analyze_Subprogram_Instantiation --
4582 --------------------------------------
4584 procedure Analyze_Subprogram_Instantiation
4585 (N : Node_Id;
4586 K : Entity_Kind)
4588 Loc : constant Source_Ptr := Sloc (N);
4589 Gen_Id : constant Node_Id := Name (N);
4591 Anon_Id : constant Entity_Id :=
4592 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4593 Chars => New_External_Name
4594 (Chars (Defining_Entity (N)), 'R'));
4596 Act_Decl_Id : Entity_Id;
4597 Act_Decl : Node_Id;
4598 Act_Spec : Node_Id;
4599 Act_Tree : Node_Id;
4601 Env_Installed : Boolean := False;
4602 Gen_Unit : Entity_Id;
4603 Gen_Decl : Node_Id;
4604 Pack_Id : Entity_Id;
4605 Parent_Installed : Boolean := False;
4606 Renaming_List : List_Id;
4608 procedure Analyze_Instance_And_Renamings;
4609 -- The instance must be analyzed in a context that includes the mappings
4610 -- of generic parameters into actuals. We create a package declaration
4611 -- for this purpose, and a subprogram with an internal name within the
4612 -- package. The subprogram instance is simply an alias for the internal
4613 -- subprogram, declared in the current scope.
4615 ------------------------------------
4616 -- Analyze_Instance_And_Renamings --
4617 ------------------------------------
4619 procedure Analyze_Instance_And_Renamings is
4620 Def_Ent : constant Entity_Id := Defining_Entity (N);
4621 Pack_Decl : Node_Id;
4623 begin
4624 if Nkind (Parent (N)) = N_Compilation_Unit then
4626 -- For the case of a compilation unit, the container package has
4627 -- the same name as the instantiation, to insure that the binder
4628 -- calls the elaboration procedure with the right name. Copy the
4629 -- entity of the instance, which may have compilation level flags
4630 -- (e.g. Is_Child_Unit) set.
4632 Pack_Id := New_Copy (Def_Ent);
4634 else
4635 -- Otherwise we use the name of the instantiation concatenated
4636 -- with its source position to ensure uniqueness if there are
4637 -- several instantiations with the same name.
4639 Pack_Id :=
4640 Make_Defining_Identifier (Loc,
4641 Chars => New_External_Name
4642 (Related_Id => Chars (Def_Ent),
4643 Suffix => "GP",
4644 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4645 end if;
4647 Pack_Decl := Make_Package_Declaration (Loc,
4648 Specification => Make_Package_Specification (Loc,
4649 Defining_Unit_Name => Pack_Id,
4650 Visible_Declarations => Renaming_List,
4651 End_Label => Empty));
4653 Set_Instance_Spec (N, Pack_Decl);
4654 Set_Is_Generic_Instance (Pack_Id);
4655 Set_Debug_Info_Needed (Pack_Id);
4657 -- Case of not a compilation unit
4659 if Nkind (Parent (N)) /= N_Compilation_Unit then
4660 Mark_Rewrite_Insertion (Pack_Decl);
4661 Insert_Before (N, Pack_Decl);
4662 Set_Has_Completion (Pack_Id);
4664 -- Case of an instantiation that is a compilation unit
4666 -- Place declaration on current node so context is complete for
4667 -- analysis (including nested instantiations), and for use in a
4668 -- context_clause (see Analyze_With_Clause).
4670 else
4671 Set_Unit (Parent (N), Pack_Decl);
4672 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4673 end if;
4675 Analyze (Pack_Decl);
4676 Check_Formal_Packages (Pack_Id);
4677 Set_Is_Generic_Instance (Pack_Id, False);
4679 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4680 -- above???
4682 -- Body of the enclosing package is supplied when instantiating the
4683 -- subprogram body, after semantic analysis is completed.
4685 if Nkind (Parent (N)) = N_Compilation_Unit then
4687 -- Remove package itself from visibility, so it does not
4688 -- conflict with subprogram.
4690 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4692 -- Set name and scope of internal subprogram so that the proper
4693 -- external name will be generated. The proper scope is the scope
4694 -- of the wrapper package. We need to generate debugging info for
4695 -- the internal subprogram, so set flag accordingly.
4697 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4698 Set_Scope (Anon_Id, Scope (Pack_Id));
4700 -- Mark wrapper package as referenced, to avoid spurious warnings
4701 -- if the instantiation appears in various with_ clauses of
4702 -- subunits of the main unit.
4704 Set_Referenced (Pack_Id);
4705 end if;
4707 Set_Is_Generic_Instance (Anon_Id);
4708 Set_Debug_Info_Needed (Anon_Id);
4709 Act_Decl_Id := New_Copy (Anon_Id);
4711 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4712 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4713 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4715 -- Subprogram instance comes from source only if generic does
4717 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4719 -- The signature may involve types that are not frozen yet, but the
4720 -- subprogram will be frozen at the point the wrapper package is
4721 -- frozen, so it does not need its own freeze node. In fact, if one
4722 -- is created, it might conflict with the freezing actions from the
4723 -- wrapper package.
4725 Set_Has_Delayed_Freeze (Anon_Id, False);
4727 -- If the instance is a child unit, mark the Id accordingly. Mark
4728 -- the anonymous entity as well, which is the real subprogram and
4729 -- which is used when the instance appears in a context clause.
4730 -- Similarly, propagate the Is_Eliminated flag to handle properly
4731 -- nested eliminated subprograms.
4733 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4734 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4735 New_Overloaded_Entity (Act_Decl_Id);
4736 Check_Eliminated (Act_Decl_Id);
4737 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4739 -- In compilation unit case, kill elaboration checks on the
4740 -- instantiation, since they are never needed -- the body is
4741 -- instantiated at the same point as the spec.
4743 if Nkind (Parent (N)) = N_Compilation_Unit then
4744 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4745 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4746 Set_Is_Compilation_Unit (Anon_Id);
4748 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4749 end if;
4751 -- The instance is not a freezing point for the new subprogram
4753 Set_Is_Frozen (Act_Decl_Id, False);
4755 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4756 Valid_Operator_Definition (Act_Decl_Id);
4757 end if;
4759 Set_Alias (Act_Decl_Id, Anon_Id);
4760 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4761 Set_Has_Completion (Act_Decl_Id);
4762 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4764 if Nkind (Parent (N)) = N_Compilation_Unit then
4765 Set_Body_Required (Parent (N), False);
4766 end if;
4767 end Analyze_Instance_And_Renamings;
4769 -- Local variables
4771 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
4772 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
4774 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4775 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4776 -- Save the SPARK_Mode-related data for restore on exit
4778 Vis_Prims_List : Elist_Id := No_Elist;
4779 -- List of primitives made temporarily visible in the instantiation
4780 -- to match the visibility of the formal type
4782 -- Start of processing for Analyze_Subprogram_Instantiation
4784 begin
4785 Check_SPARK_05_Restriction ("generic is not allowed", N);
4787 -- Very first thing: check for special Text_IO unit in case we are
4788 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
4789 -- such an instantiation is bogus (these are packages, not subprograms),
4790 -- but we get a better error message if we do this.
4792 Check_Text_IO_Special_Unit (Gen_Id);
4794 -- Make node global for error reporting
4796 Instantiation_Node := N;
4798 -- For package instantiations we turn off style checks, because they
4799 -- will have been emitted in the generic. For subprogram instantiations
4800 -- we want to apply at least the check on overriding indicators so we
4801 -- do not modify the style check status.
4803 -- The renaming declarations for the actuals do not come from source and
4804 -- will not generate spurious warnings.
4806 Preanalyze_Actuals (N);
4808 Init_Env;
4809 Env_Installed := True;
4810 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4811 Gen_Unit := Entity (Gen_Id);
4813 Generate_Reference (Gen_Unit, Gen_Id);
4815 if Nkind (Gen_Id) = N_Identifier
4816 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4817 then
4818 Error_Msg_NE
4819 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4820 end if;
4822 if Etype (Gen_Unit) = Any_Type then
4823 Restore_Env;
4824 return;
4825 end if;
4827 -- Verify that it is a generic subprogram of the right kind, and that
4828 -- it does not lead to a circular instantiation.
4830 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
4831 Error_Msg_NE
4832 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
4834 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
4835 Error_Msg_NE
4836 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
4838 elsif In_Open_Scopes (Gen_Unit) then
4839 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4841 else
4842 -- If the context of the instance is subject to SPARK_Mode "off",
4843 -- set the global flag which signals Analyze_Pragma to ignore all
4844 -- SPARK_Mode pragmas within the instance.
4846 if SPARK_Mode = Off then
4847 Ignore_Pragma_SPARK_Mode := True;
4848 end if;
4850 Set_Entity (Gen_Id, Gen_Unit);
4851 Set_Is_Instantiated (Gen_Unit);
4853 if In_Extended_Main_Source_Unit (N) then
4854 Generate_Reference (Gen_Unit, N);
4855 end if;
4857 -- If renaming, get original unit
4859 if Present (Renamed_Object (Gen_Unit))
4860 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
4861 E_Generic_Function)
4862 then
4863 Gen_Unit := Renamed_Object (Gen_Unit);
4864 Set_Is_Instantiated (Gen_Unit);
4865 Generate_Reference (Gen_Unit, N);
4866 end if;
4868 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4869 Error_Msg_Node_2 := Current_Scope;
4870 Error_Msg_NE
4871 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4872 Circularity_Detected := True;
4873 Restore_Hidden_Primitives (Vis_Prims_List);
4874 goto Leave;
4875 end if;
4877 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4879 -- Initialize renamings map, for error checking
4881 Generic_Renamings.Set_Last (0);
4882 Generic_Renamings_HTable.Reset;
4884 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4886 -- Copy original generic tree, to produce text for instantiation
4888 Act_Tree :=
4889 Copy_Generic_Node
4890 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4892 -- Inherit overriding indicator from instance node
4894 Act_Spec := Specification (Act_Tree);
4895 Set_Must_Override (Act_Spec, Must_Override (N));
4896 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4898 Renaming_List :=
4899 Analyze_Associations
4900 (I_Node => N,
4901 Formals => Generic_Formal_Declarations (Act_Tree),
4902 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4904 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4906 -- The subprogram itself cannot contain a nested instance, so the
4907 -- current parent is left empty.
4909 Set_Instance_Env (Gen_Unit, Empty);
4911 -- Build the subprogram declaration, which does not appear in the
4912 -- generic template, and give it a sloc consistent with that of the
4913 -- template.
4915 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4916 Set_Generic_Parent (Act_Spec, Gen_Unit);
4917 Act_Decl :=
4918 Make_Subprogram_Declaration (Sloc (Act_Spec),
4919 Specification => Act_Spec);
4921 -- The aspects have been copied previously, but they have to be
4922 -- linked explicitly to the new subprogram declaration. Explicit
4923 -- pre/postconditions on the instance are analyzed below, in a
4924 -- separate step.
4926 Move_Aspects (Act_Tree, To => Act_Decl);
4927 Set_Categorization_From_Pragmas (Act_Decl);
4929 if Parent_Installed then
4930 Hide_Current_Scope;
4931 end if;
4933 Append (Act_Decl, Renaming_List);
4934 Analyze_Instance_And_Renamings;
4936 -- If the generic is marked Import (Intrinsic), then so is the
4937 -- instance. This indicates that there is no body to instantiate. If
4938 -- generic is marked inline, so it the instance, and the anonymous
4939 -- subprogram it renames. If inlined, or else if inlining is enabled
4940 -- for the compilation, we generate the instance body even if it is
4941 -- not within the main unit.
4943 if Is_Intrinsic_Subprogram (Gen_Unit) then
4944 Set_Is_Intrinsic_Subprogram (Anon_Id);
4945 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
4947 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
4948 Validate_Unchecked_Conversion (N, Act_Decl_Id);
4949 end if;
4950 end if;
4952 -- Inherit convention from generic unit. Intrinsic convention, as for
4953 -- an instance of unchecked conversion, is not inherited because an
4954 -- explicit Ada instance has been created.
4956 if Has_Convention_Pragma (Gen_Unit)
4957 and then Convention (Gen_Unit) /= Convention_Intrinsic
4958 then
4959 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
4960 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
4961 end if;
4963 Generate_Definition (Act_Decl_Id);
4964 -- Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id)));
4965 -- ??? needed?
4966 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
4968 -- Inherit all inlining-related flags which apply to the generic in
4969 -- the subprogram and its declaration.
4971 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
4972 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
4974 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
4975 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
4977 Set_Has_Pragma_Inline_Always
4978 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
4979 Set_Has_Pragma_Inline_Always
4980 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
4982 if not Is_Intrinsic_Subprogram (Gen_Unit) then
4983 Check_Elab_Instantiation (N);
4984 end if;
4986 if Is_Dispatching_Operation (Act_Decl_Id)
4987 and then Ada_Version >= Ada_2005
4988 then
4989 declare
4990 Formal : Entity_Id;
4992 begin
4993 Formal := First_Formal (Act_Decl_Id);
4994 while Present (Formal) loop
4995 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
4996 and then Is_Controlling_Formal (Formal)
4997 and then not Can_Never_Be_Null (Formal)
4998 then
4999 Error_Msg_NE
5000 ("access parameter& is controlling,", N, Formal);
5001 Error_Msg_NE
5002 ("\corresponding parameter of & must be "
5003 & "explicitly null-excluding", N, Gen_Id);
5004 end if;
5006 Next_Formal (Formal);
5007 end loop;
5008 end;
5009 end if;
5011 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5013 Validate_Categorization_Dependency (N, Act_Decl_Id);
5015 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5016 Inherit_Context (Gen_Decl, N);
5018 Restore_Private_Views (Pack_Id, False);
5020 -- If the context requires a full instantiation, mark node for
5021 -- subsequent construction of the body.
5023 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5024 Check_Forward_Instantiation (Gen_Decl);
5026 -- The wrapper package is always delayed, because it does not
5027 -- constitute a freeze point, but to insure that the freeze
5028 -- node is placed properly, it is created directly when
5029 -- instantiating the body (otherwise the freeze node might
5030 -- appear to early for nested instantiations).
5032 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5034 -- For ASIS purposes, indicate that the wrapper package has
5035 -- replaced the instantiation node.
5037 Rewrite (N, Unit (Parent (N)));
5038 Set_Unit (Parent (N), N);
5039 end if;
5041 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5043 -- Replace instance node for library-level instantiations of
5044 -- intrinsic subprograms, for ASIS use.
5046 Rewrite (N, Unit (Parent (N)));
5047 Set_Unit (Parent (N), N);
5048 end if;
5050 if Parent_Installed then
5051 Remove_Parent;
5052 end if;
5054 Restore_Hidden_Primitives (Vis_Prims_List);
5055 Restore_Env;
5056 Env_Installed := False;
5057 Generic_Renamings.Set_Last (0);
5058 Generic_Renamings_HTable.Reset;
5060 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5061 SPARK_Mode := Save_SM;
5062 SPARK_Mode_Pragma := Save_SMP;
5064 if SPARK_Mode = On then
5065 Dynamic_Elaboration_Checks := False;
5066 end if;
5068 end if;
5070 <<Leave>>
5071 if Has_Aspects (N) then
5072 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5073 end if;
5075 exception
5076 when Instantiation_Error =>
5077 if Parent_Installed then
5078 Remove_Parent;
5079 end if;
5081 if Env_Installed then
5082 Restore_Env;
5083 end if;
5085 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5086 SPARK_Mode := Save_SM;
5087 SPARK_Mode_Pragma := Save_SMP;
5089 if SPARK_Mode = On then
5090 Dynamic_Elaboration_Checks := False;
5091 end if;
5092 end Analyze_Subprogram_Instantiation;
5094 -------------------------
5095 -- Get_Associated_Node --
5096 -------------------------
5098 function Get_Associated_Node (N : Node_Id) return Node_Id is
5099 Assoc : Node_Id;
5101 begin
5102 Assoc := Associated_Node (N);
5104 if Nkind (Assoc) /= Nkind (N) then
5105 return Assoc;
5107 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5108 return Assoc;
5110 else
5111 -- If the node is part of an inner generic, it may itself have been
5112 -- remapped into a further generic copy. Associated_Node is otherwise
5113 -- used for the entity of the node, and will be of a different node
5114 -- kind, or else N has been rewritten as a literal or function call.
5116 while Present (Associated_Node (Assoc))
5117 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5118 loop
5119 Assoc := Associated_Node (Assoc);
5120 end loop;
5122 -- Follow and additional link in case the final node was rewritten.
5123 -- This can only happen with nested generic units.
5125 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5126 and then Present (Associated_Node (Assoc))
5127 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5128 N_Explicit_Dereference,
5129 N_Integer_Literal,
5130 N_Real_Literal,
5131 N_String_Literal))
5132 then
5133 Assoc := Associated_Node (Assoc);
5134 end if;
5136 -- An additional special case: an unconstrained type in an object
5137 -- declaration may have been rewritten as a local subtype constrained
5138 -- by the expression in the declaration. We need to recover the
5139 -- original entity which may be global.
5141 if Present (Original_Node (Assoc))
5142 and then Nkind (Parent (N)) = N_Object_Declaration
5143 then
5144 Assoc := Original_Node (Assoc);
5145 end if;
5147 return Assoc;
5148 end if;
5149 end Get_Associated_Node;
5151 ----------------------------
5152 -- Build_Function_Wrapper --
5153 ----------------------------
5155 function Build_Function_Wrapper
5156 (Formal_Subp : Entity_Id;
5157 Actual_Subp : Entity_Id) return Node_Id
5159 Loc : constant Source_Ptr := Sloc (Current_Scope);
5160 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5161 Actuals : List_Id;
5162 Decl : Node_Id;
5163 Func_Name : Node_Id;
5164 Func : Entity_Id;
5165 Parm_Type : Node_Id;
5166 Profile : List_Id := New_List;
5167 Spec : Node_Id;
5168 Act_F : Entity_Id;
5169 Form_F : Entity_Id;
5170 New_F : Entity_Id;
5172 begin
5173 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5175 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5176 Set_Ekind (Func, E_Function);
5177 Set_Is_Generic_Actual_Subprogram (Func);
5179 Actuals := New_List;
5180 Profile := New_List;
5182 Act_F := First_Formal (Actual_Subp);
5183 Form_F := First_Formal (Formal_Subp);
5184 while Present (Form_F) loop
5186 -- Create new formal for profile of wrapper, and add a reference
5187 -- to it in the list of actuals for the enclosing call. The name
5188 -- must be that of the formal in the formal subprogram, because
5189 -- calls to it in the generic body may use named associations.
5191 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5193 Parm_Type :=
5194 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5196 Append_To (Profile,
5197 Make_Parameter_Specification (Loc,
5198 Defining_Identifier => New_F,
5199 Parameter_Type => Parm_Type));
5201 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5202 Next_Formal (Form_F);
5204 if Present (Act_F) then
5205 Next_Formal (Act_F);
5206 end if;
5207 end loop;
5209 Spec :=
5210 Make_Function_Specification (Loc,
5211 Defining_Unit_Name => Func,
5212 Parameter_Specifications => Profile,
5213 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5215 Decl :=
5216 Make_Expression_Function (Loc,
5217 Specification => Spec,
5218 Expression =>
5219 Make_Function_Call (Loc,
5220 Name => Func_Name,
5221 Parameter_Associations => Actuals));
5223 return Decl;
5224 end Build_Function_Wrapper;
5226 ----------------------------
5227 -- Build_Operator_Wrapper --
5228 ----------------------------
5230 function Build_Operator_Wrapper
5231 (Formal_Subp : Entity_Id;
5232 Actual_Subp : Entity_Id) return Node_Id
5234 Loc : constant Source_Ptr := Sloc (Current_Scope);
5235 Ret_Type : constant Entity_Id :=
5236 Get_Instance_Of (Etype (Formal_Subp));
5237 Op_Type : constant Entity_Id :=
5238 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5239 Is_Binary : constant Boolean :=
5240 Present (Next_Formal (First_Formal (Formal_Subp)));
5242 Decl : Node_Id;
5243 Expr : Node_Id;
5244 F1, F2 : Entity_Id;
5245 Func : Entity_Id;
5246 Op_Name : Name_Id;
5247 Spec : Node_Id;
5248 L, R : Node_Id;
5250 begin
5251 Op_Name := Chars (Actual_Subp);
5253 -- Create entities for wrapper function and its formals
5255 F1 := Make_Temporary (Loc, 'A');
5256 F2 := Make_Temporary (Loc, 'B');
5257 L := New_Occurrence_Of (F1, Loc);
5258 R := New_Occurrence_Of (F2, Loc);
5260 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5261 Set_Ekind (Func, E_Function);
5262 Set_Is_Generic_Actual_Subprogram (Func);
5264 Spec :=
5265 Make_Function_Specification (Loc,
5266 Defining_Unit_Name => Func,
5267 Parameter_Specifications => New_List (
5268 Make_Parameter_Specification (Loc,
5269 Defining_Identifier => F1,
5270 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5271 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5273 if Is_Binary then
5274 Append_To (Parameter_Specifications (Spec),
5275 Make_Parameter_Specification (Loc,
5276 Defining_Identifier => F2,
5277 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5278 end if;
5280 -- Build expression as a function call, or as an operator node
5281 -- that corresponds to the name of the actual, starting with
5282 -- binary operators.
5284 if Op_Name not in Any_Operator_Name then
5285 Expr :=
5286 Make_Function_Call (Loc,
5287 Name =>
5288 New_Occurrence_Of (Actual_Subp, Loc),
5289 Parameter_Associations => New_List (L));
5291 if Is_Binary then
5292 Append_To (Parameter_Associations (Expr), R);
5293 end if;
5295 -- Binary operators
5297 elsif Is_Binary then
5298 if Op_Name = Name_Op_And then
5299 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5300 elsif Op_Name = Name_Op_Or then
5301 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5302 elsif Op_Name = Name_Op_Xor then
5303 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5304 elsif Op_Name = Name_Op_Eq then
5305 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5306 elsif Op_Name = Name_Op_Ne then
5307 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5308 elsif Op_Name = Name_Op_Le then
5309 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5310 elsif Op_Name = Name_Op_Gt then
5311 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5312 elsif Op_Name = Name_Op_Ge then
5313 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5314 elsif Op_Name = Name_Op_Lt then
5315 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5316 elsif Op_Name = Name_Op_Add then
5317 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5318 elsif Op_Name = Name_Op_Subtract then
5319 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5320 elsif Op_Name = Name_Op_Concat then
5321 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5322 elsif Op_Name = Name_Op_Multiply then
5323 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5324 elsif Op_Name = Name_Op_Divide then
5325 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5326 elsif Op_Name = Name_Op_Mod then
5327 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5328 elsif Op_Name = Name_Op_Rem then
5329 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5330 elsif Op_Name = Name_Op_Expon then
5331 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5332 end if;
5334 -- Unary operators
5336 else
5337 if Op_Name = Name_Op_Add then
5338 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5339 elsif Op_Name = Name_Op_Subtract then
5340 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5341 elsif Op_Name = Name_Op_Abs then
5342 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5343 elsif Op_Name = Name_Op_Not then
5344 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5345 end if;
5346 end if;
5348 Decl :=
5349 Make_Expression_Function (Loc,
5350 Specification => Spec,
5351 Expression => Expr);
5353 return Decl;
5354 end Build_Operator_Wrapper;
5356 -------------------------------------------
5357 -- Build_Instance_Compilation_Unit_Nodes --
5358 -------------------------------------------
5360 procedure Build_Instance_Compilation_Unit_Nodes
5361 (N : Node_Id;
5362 Act_Body : Node_Id;
5363 Act_Decl : Node_Id)
5365 Decl_Cunit : Node_Id;
5366 Body_Cunit : Node_Id;
5367 Citem : Node_Id;
5368 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5369 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5371 begin
5372 -- A new compilation unit node is built for the instance declaration
5374 Decl_Cunit :=
5375 Make_Compilation_Unit (Sloc (N),
5376 Context_Items => Empty_List,
5377 Unit => Act_Decl,
5378 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5380 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5382 -- The new compilation unit is linked to its body, but both share the
5383 -- same file, so we do not set Body_Required on the new unit so as not
5384 -- to create a spurious dependency on a non-existent body in the ali.
5385 -- This simplifies CodePeer unit traversal.
5387 -- We use the original instantiation compilation unit as the resulting
5388 -- compilation unit of the instance, since this is the main unit.
5390 Rewrite (N, Act_Body);
5392 -- Propagate the aspect specifications from the package body template to
5393 -- the instantiated version of the package body.
5395 if Has_Aspects (Act_Body) then
5396 Set_Aspect_Specifications
5397 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5398 end if;
5400 Body_Cunit := Parent (N);
5402 -- The two compilation unit nodes are linked by the Library_Unit field
5404 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5405 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5407 -- Preserve the private nature of the package if needed
5409 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5411 -- If the instance is not the main unit, its context, categorization
5412 -- and elaboration entity are not relevant to the compilation.
5414 if Body_Cunit /= Cunit (Main_Unit) then
5415 Make_Instance_Unit (Body_Cunit, In_Main => False);
5416 return;
5417 end if;
5419 -- The context clause items on the instantiation, which are now attached
5420 -- to the body compilation unit (since the body overwrote the original
5421 -- instantiation node), semantically belong on the spec, so copy them
5422 -- there. It's harmless to leave them on the body as well. In fact one
5423 -- could argue that they belong in both places.
5425 Citem := First (Context_Items (Body_Cunit));
5426 while Present (Citem) loop
5427 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5428 Next (Citem);
5429 end loop;
5431 -- Propagate categorization flags on packages, so that they appear in
5432 -- the ali file for the spec of the unit.
5434 if Ekind (New_Main) = E_Package then
5435 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5436 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5437 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5438 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5439 Set_Is_Remote_Call_Interface
5440 (Old_Main, Is_Remote_Call_Interface (New_Main));
5441 end if;
5443 -- Make entry in Units table, so that binder can generate call to
5444 -- elaboration procedure for body, if any.
5446 Make_Instance_Unit (Body_Cunit, In_Main => True);
5447 Main_Unit_Entity := New_Main;
5448 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5450 -- Build elaboration entity, since the instance may certainly generate
5451 -- elaboration code requiring a flag for protection.
5453 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5454 end Build_Instance_Compilation_Unit_Nodes;
5456 -----------------------------
5457 -- Check_Access_Definition --
5458 -----------------------------
5460 procedure Check_Access_Definition (N : Node_Id) is
5461 begin
5462 pragma Assert
5463 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5464 null;
5465 end Check_Access_Definition;
5467 -----------------------------------
5468 -- Check_Formal_Package_Instance --
5469 -----------------------------------
5471 -- If the formal has specific parameters, they must match those of the
5472 -- actual. Both of them are instances, and the renaming declarations for
5473 -- their formal parameters appear in the same order in both. The analyzed
5474 -- formal has been analyzed in the context of the current instance.
5476 procedure Check_Formal_Package_Instance
5477 (Formal_Pack : Entity_Id;
5478 Actual_Pack : Entity_Id)
5480 E1 : Entity_Id := First_Entity (Actual_Pack);
5481 E2 : Entity_Id := First_Entity (Formal_Pack);
5483 Expr1 : Node_Id;
5484 Expr2 : Node_Id;
5486 procedure Check_Mismatch (B : Boolean);
5487 -- Common error routine for mismatch between the parameters of the
5488 -- actual instance and those of the formal package.
5490 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5491 -- The formal may come from a nested formal package, and the actual may
5492 -- have been constant-folded. To determine whether the two denote the
5493 -- same entity we may have to traverse several definitions to recover
5494 -- the ultimate entity that they refer to.
5496 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5497 -- Similarly, if the formal comes from a nested formal package, the
5498 -- actual may designate the formal through multiple renamings, which
5499 -- have to be followed to determine the original variable in question.
5501 --------------------
5502 -- Check_Mismatch --
5503 --------------------
5505 procedure Check_Mismatch (B : Boolean) is
5506 Kind : constant Node_Kind := Nkind (Parent (E2));
5508 begin
5509 if Kind = N_Formal_Type_Declaration then
5510 return;
5512 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5513 N_Formal_Package_Declaration)
5514 or else Kind in N_Formal_Subprogram_Declaration
5515 then
5516 null;
5518 elsif B then
5519 Error_Msg_NE
5520 ("actual for & in actual instance does not match formal",
5521 Parent (Actual_Pack), E1);
5522 end if;
5523 end Check_Mismatch;
5525 --------------------------------
5526 -- Same_Instantiated_Constant --
5527 --------------------------------
5529 function Same_Instantiated_Constant
5530 (E1, E2 : Entity_Id) return Boolean
5532 Ent : Entity_Id;
5534 begin
5535 Ent := E2;
5536 while Present (Ent) loop
5537 if E1 = Ent then
5538 return True;
5540 elsif Ekind (Ent) /= E_Constant then
5541 return False;
5543 elsif Is_Entity_Name (Constant_Value (Ent)) then
5544 if Entity (Constant_Value (Ent)) = E1 then
5545 return True;
5546 else
5547 Ent := Entity (Constant_Value (Ent));
5548 end if;
5550 -- The actual may be a constant that has been folded. Recover
5551 -- original name.
5553 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5554 Ent := Entity (Original_Node (Constant_Value (Ent)));
5556 else
5557 return False;
5558 end if;
5559 end loop;
5561 return False;
5562 end Same_Instantiated_Constant;
5564 --------------------------------
5565 -- Same_Instantiated_Variable --
5566 --------------------------------
5568 function Same_Instantiated_Variable
5569 (E1, E2 : Entity_Id) return Boolean
5571 function Original_Entity (E : Entity_Id) return Entity_Id;
5572 -- Follow chain of renamings to the ultimate ancestor
5574 ---------------------
5575 -- Original_Entity --
5576 ---------------------
5578 function Original_Entity (E : Entity_Id) return Entity_Id is
5579 Orig : Entity_Id;
5581 begin
5582 Orig := E;
5583 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5584 and then Present (Renamed_Object (Orig))
5585 and then Is_Entity_Name (Renamed_Object (Orig))
5586 loop
5587 Orig := Entity (Renamed_Object (Orig));
5588 end loop;
5590 return Orig;
5591 end Original_Entity;
5593 -- Start of processing for Same_Instantiated_Variable
5595 begin
5596 return Ekind (E1) = Ekind (E2)
5597 and then Original_Entity (E1) = Original_Entity (E2);
5598 end Same_Instantiated_Variable;
5600 -- Start of processing for Check_Formal_Package_Instance
5602 begin
5603 while Present (E1) and then Present (E2) loop
5604 exit when Ekind (E1) = E_Package
5605 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5607 -- If the formal is the renaming of the formal package, this
5608 -- is the end of its formal part, which may occur before the
5609 -- end of the formal part in the actual in the presence of
5610 -- defaulted parameters in the formal package.
5612 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5613 and then Renamed_Entity (E2) = Scope (E2);
5615 -- The analysis of the actual may generate additional internal
5616 -- entities. If the formal is defaulted, there is no corresponding
5617 -- analysis and the internal entities must be skipped, until we
5618 -- find corresponding entities again.
5620 if Comes_From_Source (E2)
5621 and then not Comes_From_Source (E1)
5622 and then Chars (E1) /= Chars (E2)
5623 then
5624 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5625 Next_Entity (E1);
5626 end loop;
5627 end if;
5629 if No (E1) then
5630 return;
5632 -- If the formal entity comes from a formal declaration, it was
5633 -- defaulted in the formal package, and no check is needed on it.
5635 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5636 goto Next_E;
5638 -- Ditto for defaulted formal subprograms.
5640 elsif Is_Overloadable (E1)
5641 and then Nkind (Unit_Declaration_Node (E2)) in
5642 N_Formal_Subprogram_Declaration
5643 then
5644 goto Next_E;
5646 elsif Is_Type (E1) then
5648 -- Subtypes must statically match. E1, E2 are the local entities
5649 -- that are subtypes of the actuals. Itypes generated for other
5650 -- parameters need not be checked, the check will be performed
5651 -- on the parameters themselves.
5653 -- If E2 is a formal type declaration, it is a defaulted parameter
5654 -- and needs no checking.
5656 if not Is_Itype (E1) and then not Is_Itype (E2) then
5657 Check_Mismatch
5658 (not Is_Type (E2)
5659 or else Etype (E1) /= Etype (E2)
5660 or else not Subtypes_Statically_Match (E1, E2));
5661 end if;
5663 elsif Ekind (E1) = E_Constant then
5665 -- IN parameters must denote the same static value, or the same
5666 -- constant, or the literal null.
5668 Expr1 := Expression (Parent (E1));
5670 if Ekind (E2) /= E_Constant then
5671 Check_Mismatch (True);
5672 goto Next_E;
5673 else
5674 Expr2 := Expression (Parent (E2));
5675 end if;
5677 if Is_OK_Static_Expression (Expr1) then
5678 if not Is_OK_Static_Expression (Expr2) then
5679 Check_Mismatch (True);
5681 elsif Is_Discrete_Type (Etype (E1)) then
5682 declare
5683 V1 : constant Uint := Expr_Value (Expr1);
5684 V2 : constant Uint := Expr_Value (Expr2);
5685 begin
5686 Check_Mismatch (V1 /= V2);
5687 end;
5689 elsif Is_Real_Type (Etype (E1)) then
5690 declare
5691 V1 : constant Ureal := Expr_Value_R (Expr1);
5692 V2 : constant Ureal := Expr_Value_R (Expr2);
5693 begin
5694 Check_Mismatch (V1 /= V2);
5695 end;
5697 elsif Is_String_Type (Etype (E1))
5698 and then Nkind (Expr1) = N_String_Literal
5699 then
5700 if Nkind (Expr2) /= N_String_Literal then
5701 Check_Mismatch (True);
5702 else
5703 Check_Mismatch
5704 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5705 end if;
5706 end if;
5708 elsif Is_Entity_Name (Expr1) then
5709 if Is_Entity_Name (Expr2) then
5710 if Entity (Expr1) = Entity (Expr2) then
5711 null;
5712 else
5713 Check_Mismatch
5714 (not Same_Instantiated_Constant
5715 (Entity (Expr1), Entity (Expr2)));
5716 end if;
5718 else
5719 Check_Mismatch (True);
5720 end if;
5722 elsif Is_Entity_Name (Original_Node (Expr1))
5723 and then Is_Entity_Name (Expr2)
5724 and then Same_Instantiated_Constant
5725 (Entity (Original_Node (Expr1)), Entity (Expr2))
5726 then
5727 null;
5729 elsif Nkind (Expr1) = N_Null then
5730 Check_Mismatch (Nkind (Expr1) /= N_Null);
5732 else
5733 Check_Mismatch (True);
5734 end if;
5736 elsif Ekind (E1) = E_Variable then
5737 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5739 elsif Ekind (E1) = E_Package then
5740 Check_Mismatch
5741 (Ekind (E1) /= Ekind (E2)
5742 or else Renamed_Object (E1) /= Renamed_Object (E2));
5744 elsif Is_Overloadable (E1) then
5746 -- Verify that the actual subprograms match. Note that actuals
5747 -- that are attributes are rewritten as subprograms. If the
5748 -- subprogram in the formal package is defaulted, no check is
5749 -- needed. Note that this can only happen in Ada 2005 when the
5750 -- formal package can be partially parameterized.
5752 if Nkind (Unit_Declaration_Node (E1)) =
5753 N_Subprogram_Renaming_Declaration
5754 and then From_Default (Unit_Declaration_Node (E1))
5755 then
5756 null;
5758 -- If the formal package has an "others" box association that
5759 -- covers this formal, there is no need for a check either.
5761 elsif Nkind (Unit_Declaration_Node (E2)) in
5762 N_Formal_Subprogram_Declaration
5763 and then Box_Present (Unit_Declaration_Node (E2))
5764 then
5765 null;
5767 -- No check needed if subprogram is a defaulted null procedure
5769 elsif No (Alias (E2))
5770 and then Ekind (E2) = E_Procedure
5771 and then
5772 Null_Present (Specification (Unit_Declaration_Node (E2)))
5773 then
5774 null;
5776 -- Otherwise the actual in the formal and the actual in the
5777 -- instantiation of the formal must match, up to renamings.
5779 else
5780 Check_Mismatch
5781 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5782 end if;
5784 else
5785 raise Program_Error;
5786 end if;
5788 <<Next_E>>
5789 Next_Entity (E1);
5790 Next_Entity (E2);
5791 end loop;
5792 end Check_Formal_Package_Instance;
5794 ---------------------------
5795 -- Check_Formal_Packages --
5796 ---------------------------
5798 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5799 E : Entity_Id;
5800 Formal_P : Entity_Id;
5802 begin
5803 -- Iterate through the declarations in the instance, looking for package
5804 -- renaming declarations that denote instances of formal packages. Stop
5805 -- when we find the renaming of the current package itself. The
5806 -- declaration for a formal package without a box is followed by an
5807 -- internal entity that repeats the instantiation.
5809 E := First_Entity (P_Id);
5810 while Present (E) loop
5811 if Ekind (E) = E_Package then
5812 if Renamed_Object (E) = P_Id then
5813 exit;
5815 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5816 null;
5818 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5819 Formal_P := Next_Entity (E);
5820 Check_Formal_Package_Instance (Formal_P, E);
5822 -- After checking, remove the internal validating package. It
5823 -- is only needed for semantic checks, and as it may contain
5824 -- generic formal declarations it should not reach gigi.
5826 Remove (Unit_Declaration_Node (Formal_P));
5827 end if;
5828 end if;
5830 Next_Entity (E);
5831 end loop;
5832 end Check_Formal_Packages;
5834 ---------------------------------
5835 -- Check_Forward_Instantiation --
5836 ---------------------------------
5838 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5839 S : Entity_Id;
5840 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5842 begin
5843 -- The instantiation appears before the generic body if we are in the
5844 -- scope of the unit containing the generic, either in its spec or in
5845 -- the package body, and before the generic body.
5847 if Ekind (Gen_Comp) = E_Package_Body then
5848 Gen_Comp := Spec_Entity (Gen_Comp);
5849 end if;
5851 if In_Open_Scopes (Gen_Comp)
5852 and then No (Corresponding_Body (Decl))
5853 then
5854 S := Current_Scope;
5856 while Present (S)
5857 and then not Is_Compilation_Unit (S)
5858 and then not Is_Child_Unit (S)
5859 loop
5860 if Ekind (S) = E_Package then
5861 Set_Has_Forward_Instantiation (S);
5862 end if;
5864 S := Scope (S);
5865 end loop;
5866 end if;
5867 end Check_Forward_Instantiation;
5869 ---------------------------
5870 -- Check_Generic_Actuals --
5871 ---------------------------
5873 -- The visibility of the actuals may be different between the point of
5874 -- generic instantiation and the instantiation of the body.
5876 procedure Check_Generic_Actuals
5877 (Instance : Entity_Id;
5878 Is_Formal_Box : Boolean)
5880 E : Entity_Id;
5881 Astype : Entity_Id;
5883 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5884 -- For a formal that is an array type, the component type is often a
5885 -- previous formal in the same unit. The privacy status of the component
5886 -- type will have been examined earlier in the traversal of the
5887 -- corresponding actuals, and this status should not be modified for
5888 -- the array (sub)type itself. However, if the base type of the array
5889 -- (sub)type is private, its full view must be restored in the body to
5890 -- be consistent with subsequent index subtypes, etc.
5892 -- To detect this case we have to rescan the list of formals, which is
5893 -- usually short enough to ignore the resulting inefficiency.
5895 -----------------------------
5896 -- Denotes_Previous_Actual --
5897 -----------------------------
5899 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5900 Prev : Entity_Id;
5902 begin
5903 Prev := First_Entity (Instance);
5904 while Present (Prev) loop
5905 if Is_Type (Prev)
5906 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5907 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5908 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5909 then
5910 return True;
5912 elsif Prev = E then
5913 return False;
5915 else
5916 Next_Entity (Prev);
5917 end if;
5918 end loop;
5920 return False;
5921 end Denotes_Previous_Actual;
5923 -- Start of processing for Check_Generic_Actuals
5925 begin
5926 E := First_Entity (Instance);
5927 while Present (E) loop
5928 if Is_Type (E)
5929 and then Nkind (Parent (E)) = N_Subtype_Declaration
5930 and then Scope (Etype (E)) /= Instance
5931 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5932 then
5933 if Is_Array_Type (E)
5934 and then not Is_Private_Type (Etype (E))
5935 and then Denotes_Previous_Actual (Component_Type (E))
5936 then
5937 null;
5938 else
5939 Check_Private_View (Subtype_Indication (Parent (E)));
5940 end if;
5942 Set_Is_Generic_Actual_Type (E, True);
5943 Set_Is_Hidden (E, False);
5944 Set_Is_Potentially_Use_Visible (E,
5945 In_Use (Instance));
5947 -- We constructed the generic actual type as a subtype of the
5948 -- supplied type. This means that it normally would not inherit
5949 -- subtype specific attributes of the actual, which is wrong for
5950 -- the generic case.
5952 Astype := Ancestor_Subtype (E);
5954 if No (Astype) then
5956 -- This can happen when E is an itype that is the full view of
5957 -- a private type completed, e.g. with a constrained array. In
5958 -- that case, use the first subtype, which will carry size
5959 -- information. The base type itself is unconstrained and will
5960 -- not carry it.
5962 Astype := First_Subtype (E);
5963 end if;
5965 Set_Size_Info (E, (Astype));
5966 Set_RM_Size (E, RM_Size (Astype));
5967 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5969 if Is_Discrete_Or_Fixed_Point_Type (E) then
5970 Set_RM_Size (E, RM_Size (Astype));
5972 -- In nested instances, the base type of an access actual may
5973 -- itself be private, and need to be exchanged.
5975 elsif Is_Access_Type (E)
5976 and then Is_Private_Type (Etype (E))
5977 then
5978 Check_Private_View
5979 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5980 end if;
5982 elsif Ekind (E) = E_Package then
5984 -- If this is the renaming for the current instance, we're done.
5985 -- Otherwise it is a formal package. If the corresponding formal
5986 -- was declared with a box, the (instantiations of the) generic
5987 -- formal part are also visible. Otherwise, ignore the entity
5988 -- created to validate the actuals.
5990 if Renamed_Object (E) = Instance then
5991 exit;
5993 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5994 null;
5996 -- The visibility of a formal of an enclosing generic is already
5997 -- correct.
5999 elsif Denotes_Formal_Package (E) then
6000 null;
6002 elsif Present (Associated_Formal_Package (E))
6003 and then not Is_Generic_Formal (E)
6004 then
6005 if Box_Present (Parent (Associated_Formal_Package (E))) then
6006 Check_Generic_Actuals (Renamed_Object (E), True);
6008 else
6009 Check_Generic_Actuals (Renamed_Object (E), False);
6010 end if;
6012 Set_Is_Hidden (E, False);
6013 end if;
6015 -- If this is a subprogram instance (in a wrapper package) the
6016 -- actual is fully visible.
6018 elsif Is_Wrapper_Package (Instance) then
6019 Set_Is_Hidden (E, False);
6021 -- If the formal package is declared with a box, or if the formal
6022 -- parameter is defaulted, it is visible in the body.
6024 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6025 Set_Is_Hidden (E, False);
6026 end if;
6028 if Ekind (E) = E_Constant then
6030 -- If the type of the actual is a private type declared in the
6031 -- enclosing scope of the generic unit, the body of the generic
6032 -- sees the full view of the type (because it has to appear in
6033 -- the corresponding package body). If the type is private now,
6034 -- exchange views to restore the proper visiblity in the instance.
6036 declare
6037 Typ : constant Entity_Id := Base_Type (Etype (E));
6038 -- The type of the actual
6040 Gen_Id : Entity_Id;
6041 -- The generic unit
6043 Parent_Scope : Entity_Id;
6044 -- The enclosing scope of the generic unit
6046 begin
6047 if Is_Wrapper_Package (Instance) then
6048 Gen_Id :=
6049 Generic_Parent
6050 (Specification
6051 (Unit_Declaration_Node
6052 (Related_Instance (Instance))));
6053 else
6054 Gen_Id :=
6055 Generic_Parent (Package_Specification (Instance));
6056 end if;
6058 Parent_Scope := Scope (Gen_Id);
6060 -- The exchange is only needed if the generic is defined
6061 -- within a package which is not a common ancestor of the
6062 -- scope of the instance, and is not already in scope.
6064 if Is_Private_Type (Typ)
6065 and then Scope (Typ) = Parent_Scope
6066 and then Scope (Instance) /= Parent_Scope
6067 and then Ekind (Parent_Scope) = E_Package
6068 and then not Is_Child_Unit (Gen_Id)
6069 then
6070 Switch_View (Typ);
6072 -- If the type of the entity is a subtype, it may also have
6073 -- to be made visible, together with the base type of its
6074 -- full view, after exchange.
6076 if Is_Private_Type (Etype (E)) then
6077 Switch_View (Etype (E));
6078 Switch_View (Base_Type (Etype (E)));
6079 end if;
6080 end if;
6081 end;
6082 end if;
6084 Next_Entity (E);
6085 end loop;
6086 end Check_Generic_Actuals;
6088 ------------------------------
6089 -- Check_Generic_Child_Unit --
6090 ------------------------------
6092 procedure Check_Generic_Child_Unit
6093 (Gen_Id : Node_Id;
6094 Parent_Installed : in out Boolean)
6096 Loc : constant Source_Ptr := Sloc (Gen_Id);
6097 Gen_Par : Entity_Id := Empty;
6098 E : Entity_Id;
6099 Inst_Par : Entity_Id;
6100 S : Node_Id;
6102 function Find_Generic_Child
6103 (Scop : Entity_Id;
6104 Id : Node_Id) return Entity_Id;
6105 -- Search generic parent for possible child unit with the given name
6107 function In_Enclosing_Instance return Boolean;
6108 -- Within an instance of the parent, the child unit may be denoted by
6109 -- a simple name, or an abbreviated expanded name. Examine enclosing
6110 -- scopes to locate a possible parent instantiation.
6112 ------------------------
6113 -- Find_Generic_Child --
6114 ------------------------
6116 function Find_Generic_Child
6117 (Scop : Entity_Id;
6118 Id : Node_Id) return Entity_Id
6120 E : Entity_Id;
6122 begin
6123 -- If entity of name is already set, instance has already been
6124 -- resolved, e.g. in an enclosing instantiation.
6126 if Present (Entity (Id)) then
6127 if Scope (Entity (Id)) = Scop then
6128 return Entity (Id);
6129 else
6130 return Empty;
6131 end if;
6133 else
6134 E := First_Entity (Scop);
6135 while Present (E) loop
6136 if Chars (E) = Chars (Id)
6137 and then Is_Child_Unit (E)
6138 then
6139 if Is_Child_Unit (E)
6140 and then not Is_Visible_Lib_Unit (E)
6141 then
6142 Error_Msg_NE
6143 ("generic child unit& is not visible", Gen_Id, E);
6144 end if;
6146 Set_Entity (Id, E);
6147 return E;
6148 end if;
6150 Next_Entity (E);
6151 end loop;
6153 return Empty;
6154 end if;
6155 end Find_Generic_Child;
6157 ---------------------------
6158 -- In_Enclosing_Instance --
6159 ---------------------------
6161 function In_Enclosing_Instance return Boolean is
6162 Enclosing_Instance : Node_Id;
6163 Instance_Decl : Node_Id;
6165 begin
6166 -- We do not inline any call that contains instantiations, except
6167 -- for instantiations of Unchecked_Conversion, so if we are within
6168 -- an inlined body the current instance does not require parents.
6170 if In_Inlined_Body then
6171 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6172 return False;
6173 end if;
6175 -- Loop to check enclosing scopes
6177 Enclosing_Instance := Current_Scope;
6178 while Present (Enclosing_Instance) loop
6179 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6181 if Ekind (Enclosing_Instance) = E_Package
6182 and then Is_Generic_Instance (Enclosing_Instance)
6183 and then Present
6184 (Generic_Parent (Specification (Instance_Decl)))
6185 then
6186 -- Check whether the generic we are looking for is a child of
6187 -- this instance.
6189 E := Find_Generic_Child
6190 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6191 exit when Present (E);
6193 else
6194 E := Empty;
6195 end if;
6197 Enclosing_Instance := Scope (Enclosing_Instance);
6198 end loop;
6200 if No (E) then
6202 -- Not a child unit
6204 Analyze (Gen_Id);
6205 return False;
6207 else
6208 Rewrite (Gen_Id,
6209 Make_Expanded_Name (Loc,
6210 Chars => Chars (E),
6211 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6212 Selector_Name => New_Occurrence_Of (E, Loc)));
6214 Set_Entity (Gen_Id, E);
6215 Set_Etype (Gen_Id, Etype (E));
6216 Parent_Installed := False; -- Already in scope.
6217 return True;
6218 end if;
6219 end In_Enclosing_Instance;
6221 -- Start of processing for Check_Generic_Child_Unit
6223 begin
6224 -- If the name of the generic is given by a selected component, it may
6225 -- be the name of a generic child unit, and the prefix is the name of an
6226 -- instance of the parent, in which case the child unit must be visible.
6227 -- If this instance is not in scope, it must be placed there and removed
6228 -- after instantiation, because what is being instantiated is not the
6229 -- original child, but the corresponding child present in the instance
6230 -- of the parent.
6232 -- If the child is instantiated within the parent, it can be given by
6233 -- a simple name. In this case the instance is already in scope, but
6234 -- the child generic must be recovered from the generic parent as well.
6236 if Nkind (Gen_Id) = N_Selected_Component then
6237 S := Selector_Name (Gen_Id);
6238 Analyze (Prefix (Gen_Id));
6239 Inst_Par := Entity (Prefix (Gen_Id));
6241 if Ekind (Inst_Par) = E_Package
6242 and then Present (Renamed_Object (Inst_Par))
6243 then
6244 Inst_Par := Renamed_Object (Inst_Par);
6245 end if;
6247 if Ekind (Inst_Par) = E_Package then
6248 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6249 Gen_Par := Generic_Parent (Parent (Inst_Par));
6251 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6252 and then
6253 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6254 then
6255 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6256 end if;
6258 elsif Ekind (Inst_Par) = E_Generic_Package
6259 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6260 then
6261 -- A formal package may be a real child package, and not the
6262 -- implicit instance within a parent. In this case the child is
6263 -- not visible and has to be retrieved explicitly as well.
6265 Gen_Par := Inst_Par;
6266 end if;
6268 if Present (Gen_Par) then
6270 -- The prefix denotes an instantiation. The entity itself may be a
6271 -- nested generic, or a child unit.
6273 E := Find_Generic_Child (Gen_Par, S);
6275 if Present (E) then
6276 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6277 Set_Entity (Gen_Id, E);
6278 Set_Etype (Gen_Id, Etype (E));
6279 Set_Entity (S, E);
6280 Set_Etype (S, Etype (E));
6282 -- Indicate that this is a reference to the parent
6284 if In_Extended_Main_Source_Unit (Gen_Id) then
6285 Set_Is_Instantiated (Inst_Par);
6286 end if;
6288 -- A common mistake is to replicate the naming scheme of a
6289 -- hierarchy by instantiating a generic child directly, rather
6290 -- than the implicit child in a parent instance:
6292 -- generic .. package Gpar is ..
6293 -- generic .. package Gpar.Child is ..
6294 -- package Par is new Gpar ();
6296 -- with Gpar.Child;
6297 -- package Par.Child is new Gpar.Child ();
6298 -- rather than Par.Child
6300 -- In this case the instantiation is within Par, which is an
6301 -- instance, but Gpar does not denote Par because we are not IN
6302 -- the instance of Gpar, so this is illegal. The test below
6303 -- recognizes this particular case.
6305 if Is_Child_Unit (E)
6306 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6307 and then (not In_Instance
6308 or else Nkind (Parent (Parent (Gen_Id))) =
6309 N_Compilation_Unit)
6310 then
6311 Error_Msg_N
6312 ("prefix of generic child unit must be instance of parent",
6313 Gen_Id);
6314 end if;
6316 if not In_Open_Scopes (Inst_Par)
6317 and then Nkind (Parent (Gen_Id)) not in
6318 N_Generic_Renaming_Declaration
6319 then
6320 Install_Parent (Inst_Par);
6321 Parent_Installed := True;
6323 elsif In_Open_Scopes (Inst_Par) then
6325 -- If the parent is already installed, install the actuals
6326 -- for its formal packages. This is necessary when the child
6327 -- instance is a child of the parent instance: in this case,
6328 -- the parent is placed on the scope stack but the formal
6329 -- packages are not made visible.
6331 Install_Formal_Packages (Inst_Par);
6332 end if;
6334 else
6335 -- If the generic parent does not contain an entity that
6336 -- corresponds to the selector, the instance doesn't either.
6337 -- Analyzing the node will yield the appropriate error message.
6338 -- If the entity is not a child unit, then it is an inner
6339 -- generic in the parent.
6341 Analyze (Gen_Id);
6342 end if;
6344 else
6345 Analyze (Gen_Id);
6347 if Is_Child_Unit (Entity (Gen_Id))
6348 and then
6349 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6350 and then not In_Open_Scopes (Inst_Par)
6351 then
6352 Install_Parent (Inst_Par);
6353 Parent_Installed := True;
6355 -- The generic unit may be the renaming of the implicit child
6356 -- present in an instance. In that case the parent instance is
6357 -- obtained from the name of the renamed entity.
6359 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6360 and then Present (Renamed_Entity (Entity (Gen_Id)))
6361 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6362 then
6363 declare
6364 Renamed_Package : constant Node_Id :=
6365 Name (Parent (Entity (Gen_Id)));
6366 begin
6367 if Nkind (Renamed_Package) = N_Expanded_Name then
6368 Inst_Par := Entity (Prefix (Renamed_Package));
6369 Install_Parent (Inst_Par);
6370 Parent_Installed := True;
6371 end if;
6372 end;
6373 end if;
6374 end if;
6376 elsif Nkind (Gen_Id) = N_Expanded_Name then
6378 -- Entity already present, analyze prefix, whose meaning may be
6379 -- an instance in the current context. If it is an instance of
6380 -- a relative within another, the proper parent may still have
6381 -- to be installed, if they are not of the same generation.
6383 Analyze (Prefix (Gen_Id));
6385 -- In the unlikely case that a local declaration hides the name
6386 -- of the parent package, locate it on the homonym chain. If the
6387 -- context is an instance of the parent, the renaming entity is
6388 -- flagged as such.
6390 Inst_Par := Entity (Prefix (Gen_Id));
6391 while Present (Inst_Par)
6392 and then not Is_Package_Or_Generic_Package (Inst_Par)
6393 loop
6394 Inst_Par := Homonym (Inst_Par);
6395 end loop;
6397 pragma Assert (Present (Inst_Par));
6398 Set_Entity (Prefix (Gen_Id), Inst_Par);
6400 if In_Enclosing_Instance then
6401 null;
6403 elsif Present (Entity (Gen_Id))
6404 and then Is_Child_Unit (Entity (Gen_Id))
6405 and then not In_Open_Scopes (Inst_Par)
6406 then
6407 Install_Parent (Inst_Par);
6408 Parent_Installed := True;
6409 end if;
6411 elsif In_Enclosing_Instance then
6413 -- The child unit is found in some enclosing scope
6415 null;
6417 else
6418 Analyze (Gen_Id);
6420 -- If this is the renaming of the implicit child in a parent
6421 -- instance, recover the parent name and install it.
6423 if Is_Entity_Name (Gen_Id) then
6424 E := Entity (Gen_Id);
6426 if Is_Generic_Unit (E)
6427 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6428 and then Is_Child_Unit (Renamed_Object (E))
6429 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6430 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6431 then
6432 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6433 Inst_Par := Entity (Prefix (Gen_Id));
6435 if not In_Open_Scopes (Inst_Par) then
6436 Install_Parent (Inst_Par);
6437 Parent_Installed := True;
6438 end if;
6440 -- If it is a child unit of a non-generic parent, it may be
6441 -- use-visible and given by a direct name. Install parent as
6442 -- for other cases.
6444 elsif Is_Generic_Unit (E)
6445 and then Is_Child_Unit (E)
6446 and then
6447 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6448 and then not Is_Generic_Unit (Scope (E))
6449 then
6450 if not In_Open_Scopes (Scope (E)) then
6451 Install_Parent (Scope (E));
6452 Parent_Installed := True;
6453 end if;
6454 end if;
6455 end if;
6456 end if;
6457 end Check_Generic_Child_Unit;
6459 -----------------------------
6460 -- Check_Hidden_Child_Unit --
6461 -----------------------------
6463 procedure Check_Hidden_Child_Unit
6464 (N : Node_Id;
6465 Gen_Unit : Entity_Id;
6466 Act_Decl_Id : Entity_Id)
6468 Gen_Id : constant Node_Id := Name (N);
6470 begin
6471 if Is_Child_Unit (Gen_Unit)
6472 and then Is_Child_Unit (Act_Decl_Id)
6473 and then Nkind (Gen_Id) = N_Expanded_Name
6474 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6475 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6476 then
6477 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6478 Error_Msg_NE
6479 ("generic unit & is implicitly declared in &",
6480 Defining_Unit_Name (N), Gen_Unit);
6481 Error_Msg_N ("\instance must have different name",
6482 Defining_Unit_Name (N));
6483 end if;
6484 end Check_Hidden_Child_Unit;
6486 ------------------------
6487 -- Check_Private_View --
6488 ------------------------
6490 procedure Check_Private_View (N : Node_Id) is
6491 T : constant Entity_Id := Etype (N);
6492 BT : Entity_Id;
6494 begin
6495 -- Exchange views if the type was not private in the generic but is
6496 -- private at the point of instantiation. Do not exchange views if
6497 -- the scope of the type is in scope. This can happen if both generic
6498 -- and instance are sibling units, or if type is defined in a parent.
6499 -- In this case the visibility of the type will be correct for all
6500 -- semantic checks.
6502 if Present (T) then
6503 BT := Base_Type (T);
6505 if Is_Private_Type (T)
6506 and then not Has_Private_View (N)
6507 and then Present (Full_View (T))
6508 and then not In_Open_Scopes (Scope (T))
6509 then
6510 -- In the generic, the full type was visible. Save the private
6511 -- entity, for subsequent exchange.
6513 Switch_View (T);
6515 elsif Has_Private_View (N)
6516 and then not Is_Private_Type (T)
6517 and then not Has_Been_Exchanged (T)
6518 and then Etype (Get_Associated_Node (N)) /= T
6519 then
6520 -- Only the private declaration was visible in the generic. If
6521 -- the type appears in a subtype declaration, the subtype in the
6522 -- instance must have a view compatible with that of its parent,
6523 -- which must be exchanged (see corresponding code in Restore_
6524 -- Private_Views). Otherwise, if the type is defined in a parent
6525 -- unit, leave full visibility within instance, which is safe.
6527 if In_Open_Scopes (Scope (Base_Type (T)))
6528 and then not Is_Private_Type (Base_Type (T))
6529 and then Comes_From_Source (Base_Type (T))
6530 then
6531 null;
6533 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6534 or else not In_Private_Part (Scope (Base_Type (T)))
6535 then
6536 Prepend_Elmt (T, Exchanged_Views);
6537 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6538 end if;
6540 -- For composite types with inconsistent representation exchange
6541 -- component types accordingly.
6543 elsif Is_Access_Type (T)
6544 and then Is_Private_Type (Designated_Type (T))
6545 and then not Has_Private_View (N)
6546 and then Present (Full_View (Designated_Type (T)))
6547 then
6548 Switch_View (Designated_Type (T));
6550 elsif Is_Array_Type (T) then
6551 if Is_Private_Type (Component_Type (T))
6552 and then not Has_Private_View (N)
6553 and then Present (Full_View (Component_Type (T)))
6554 then
6555 Switch_View (Component_Type (T));
6556 end if;
6558 -- The normal exchange mechanism relies on the setting of a
6559 -- flag on the reference in the generic. However, an additional
6560 -- mechanism is needed for types that are not explicitly
6561 -- mentioned in the generic, but may be needed in expanded code
6562 -- in the instance. This includes component types of arrays and
6563 -- designated types of access types. This processing must also
6564 -- include the index types of arrays which we take care of here.
6566 declare
6567 Indx : Node_Id;
6568 Typ : Entity_Id;
6570 begin
6571 Indx := First_Index (T);
6572 while Present (Indx) loop
6573 Typ := Base_Type (Etype (Indx));
6575 if Is_Private_Type (Typ)
6576 and then Present (Full_View (Typ))
6577 then
6578 Switch_View (Typ);
6579 end if;
6581 Next_Index (Indx);
6582 end loop;
6583 end;
6585 elsif Is_Private_Type (T)
6586 and then Present (Full_View (T))
6587 and then Is_Array_Type (Full_View (T))
6588 and then Is_Private_Type (Component_Type (Full_View (T)))
6589 then
6590 Switch_View (T);
6592 -- Finally, a non-private subtype may have a private base type, which
6593 -- must be exchanged for consistency. This can happen when a package
6594 -- body is instantiated, when the scope stack is empty but in fact
6595 -- the subtype and the base type are declared in an enclosing scope.
6597 -- Note that in this case we introduce an inconsistency in the view
6598 -- set, because we switch the base type BT, but there could be some
6599 -- private dependent subtypes of BT which remain unswitched. Such
6600 -- subtypes might need to be switched at a later point (see specific
6601 -- provision for that case in Switch_View).
6603 elsif not Is_Private_Type (T)
6604 and then not Has_Private_View (N)
6605 and then Is_Private_Type (BT)
6606 and then Present (Full_View (BT))
6607 and then not Is_Generic_Type (BT)
6608 and then not In_Open_Scopes (BT)
6609 then
6610 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6611 Exchange_Declarations (BT);
6612 end if;
6613 end if;
6614 end Check_Private_View;
6616 -----------------------------
6617 -- Check_Hidden_Primitives --
6618 -----------------------------
6620 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6621 Actual : Node_Id;
6622 Gen_T : Entity_Id;
6623 Result : Elist_Id := No_Elist;
6625 begin
6626 if No (Assoc_List) then
6627 return No_Elist;
6628 end if;
6630 -- Traverse the list of associations between formals and actuals
6631 -- searching for renamings of tagged types
6633 Actual := First (Assoc_List);
6634 while Present (Actual) loop
6635 if Nkind (Actual) = N_Subtype_Declaration then
6636 Gen_T := Generic_Parent_Type (Actual);
6638 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6640 -- Traverse the list of primitives of the actual types
6641 -- searching for hidden primitives that are visible in the
6642 -- corresponding generic formal; leave them visible and
6643 -- append them to Result to restore their decoration later.
6645 Install_Hidden_Primitives
6646 (Prims_List => Result,
6647 Gen_T => Gen_T,
6648 Act_T => Entity (Subtype_Indication (Actual)));
6649 end if;
6650 end if;
6652 Next (Actual);
6653 end loop;
6655 return Result;
6656 end Check_Hidden_Primitives;
6658 --------------------------
6659 -- Contains_Instance_Of --
6660 --------------------------
6662 function Contains_Instance_Of
6663 (Inner : Entity_Id;
6664 Outer : Entity_Id;
6665 N : Node_Id) return Boolean
6667 Elmt : Elmt_Id;
6668 Scop : Entity_Id;
6670 begin
6671 Scop := Outer;
6673 -- Verify that there are no circular instantiations. We check whether
6674 -- the unit contains an instance of the current scope or some enclosing
6675 -- scope (in case one of the instances appears in a subunit). Longer
6676 -- circularities involving subunits might seem too pathological to
6677 -- consider, but they were not too pathological for the authors of
6678 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6679 -- enclosing generic scopes as containing an instance.
6681 loop
6682 -- Within a generic subprogram body, the scope is not generic, to
6683 -- allow for recursive subprograms. Use the declaration to determine
6684 -- whether this is a generic unit.
6686 if Ekind (Scop) = E_Generic_Package
6687 or else (Is_Subprogram (Scop)
6688 and then Nkind (Unit_Declaration_Node (Scop)) =
6689 N_Generic_Subprogram_Declaration)
6690 then
6691 Elmt := First_Elmt (Inner_Instances (Inner));
6693 while Present (Elmt) loop
6694 if Node (Elmt) = Scop then
6695 Error_Msg_Node_2 := Inner;
6696 Error_Msg_NE
6697 ("circular Instantiation: & instantiated within &!",
6698 N, Scop);
6699 return True;
6701 elsif Node (Elmt) = Inner then
6702 return True;
6704 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6705 Error_Msg_Node_2 := Inner;
6706 Error_Msg_NE
6707 ("circular Instantiation: & instantiated within &!",
6708 N, Node (Elmt));
6709 return True;
6710 end if;
6712 Next_Elmt (Elmt);
6713 end loop;
6715 -- Indicate that Inner is being instantiated within Scop
6717 Append_Elmt (Inner, Inner_Instances (Scop));
6718 end if;
6720 if Scop = Standard_Standard then
6721 exit;
6722 else
6723 Scop := Scope (Scop);
6724 end if;
6725 end loop;
6727 return False;
6728 end Contains_Instance_Of;
6730 -----------------------
6731 -- Copy_Generic_Node --
6732 -----------------------
6734 function Copy_Generic_Node
6735 (N : Node_Id;
6736 Parent_Id : Node_Id;
6737 Instantiating : Boolean) return Node_Id
6739 Ent : Entity_Id;
6740 New_N : Node_Id;
6742 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6743 -- Check the given value of one of the Fields referenced by the current
6744 -- node to determine whether to copy it recursively. The field may hold
6745 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6746 -- Char) in which case it need not be copied.
6748 procedure Copy_Descendants;
6749 -- Common utility for various nodes
6751 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6752 -- Make copy of element list
6754 function Copy_Generic_List
6755 (L : List_Id;
6756 Parent_Id : Node_Id) return List_Id;
6757 -- Apply Copy_Node recursively to the members of a node list
6759 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6760 -- True if an identifier is part of the defining program unit name of
6761 -- a child unit. The entity of such an identifier must be kept (for
6762 -- ASIS use) even though as the name of an enclosing generic it would
6763 -- otherwise not be preserved in the generic tree.
6765 ----------------------
6766 -- Copy_Descendants --
6767 ----------------------
6769 procedure Copy_Descendants is
6771 use Atree.Unchecked_Access;
6772 -- This code section is part of the implementation of an untyped
6773 -- tree traversal, so it needs direct access to node fields.
6775 begin
6776 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6777 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6778 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6779 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6780 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6781 end Copy_Descendants;
6783 -----------------------------
6784 -- Copy_Generic_Descendant --
6785 -----------------------------
6787 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6788 begin
6789 if D = Union_Id (Empty) then
6790 return D;
6792 elsif D in Node_Range then
6793 return Union_Id
6794 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6796 elsif D in List_Range then
6797 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6799 elsif D in Elist_Range then
6800 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6802 -- Nothing else is copyable (e.g. Uint values), return as is
6804 else
6805 return D;
6806 end if;
6807 end Copy_Generic_Descendant;
6809 ------------------------
6810 -- Copy_Generic_Elist --
6811 ------------------------
6813 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6814 M : Elmt_Id;
6815 L : Elist_Id;
6817 begin
6818 if Present (E) then
6819 L := New_Elmt_List;
6820 M := First_Elmt (E);
6821 while Present (M) loop
6822 Append_Elmt
6823 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6824 Next_Elmt (M);
6825 end loop;
6827 return L;
6829 else
6830 return No_Elist;
6831 end if;
6832 end Copy_Generic_Elist;
6834 -----------------------
6835 -- Copy_Generic_List --
6836 -----------------------
6838 function Copy_Generic_List
6839 (L : List_Id;
6840 Parent_Id : Node_Id) return List_Id
6842 N : Node_Id;
6843 New_L : List_Id;
6845 begin
6846 if Present (L) then
6847 New_L := New_List;
6848 Set_Parent (New_L, Parent_Id);
6850 N := First (L);
6851 while Present (N) loop
6852 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6853 Next (N);
6854 end loop;
6856 return New_L;
6858 else
6859 return No_List;
6860 end if;
6861 end Copy_Generic_List;
6863 ---------------------------
6864 -- In_Defining_Unit_Name --
6865 ---------------------------
6867 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6868 begin
6869 return Present (Parent (Nam))
6870 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6871 or else
6872 (Nkind (Parent (Nam)) = N_Expanded_Name
6873 and then In_Defining_Unit_Name (Parent (Nam))));
6874 end In_Defining_Unit_Name;
6876 -- Start of processing for Copy_Generic_Node
6878 begin
6879 if N = Empty then
6880 return N;
6881 end if;
6883 New_N := New_Copy (N);
6885 -- Copy aspects if present
6887 if Has_Aspects (N) then
6888 Set_Has_Aspects (New_N, False);
6889 Set_Aspect_Specifications
6890 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6891 end if;
6893 if Instantiating then
6894 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6895 end if;
6897 if not Is_List_Member (N) then
6898 Set_Parent (New_N, Parent_Id);
6899 end if;
6901 -- If defining identifier, then all fields have been copied already
6903 if Nkind (New_N) in N_Entity then
6904 null;
6906 -- Special casing for identifiers and other entity names and operators
6908 elsif Nkind_In (New_N, N_Identifier,
6909 N_Character_Literal,
6910 N_Expanded_Name,
6911 N_Operator_Symbol)
6912 or else Nkind (New_N) in N_Op
6913 then
6914 if not Instantiating then
6916 -- Link both nodes in order to assign subsequently the entity of
6917 -- the copy to the original node, in case this is a global
6918 -- reference.
6920 Set_Associated_Node (N, New_N);
6922 -- If we are within an instantiation, this is a nested generic
6923 -- that has already been analyzed at the point of definition.
6924 -- We must preserve references that were global to the enclosing
6925 -- parent at that point. Other occurrences, whether global or
6926 -- local to the current generic, must be resolved anew, so we
6927 -- reset the entity in the generic copy. A global reference has a
6928 -- smaller depth than the parent, or else the same depth in case
6929 -- both are distinct compilation units.
6931 -- A child unit is implicitly declared within the enclosing parent
6932 -- but is in fact global to it, and must be preserved.
6934 -- It is also possible for Current_Instantiated_Parent to be
6935 -- defined, and for this not to be a nested generic, namely if
6936 -- the unit is loaded through Rtsfind. In that case, the entity of
6937 -- New_N is only a link to the associated node, and not a defining
6938 -- occurrence.
6940 -- The entities for parent units in the defining_program_unit of a
6941 -- generic child unit are established when the context of the unit
6942 -- is first analyzed, before the generic copy is made. They are
6943 -- preserved in the copy for use in ASIS queries.
6945 Ent := Entity (New_N);
6947 if No (Current_Instantiated_Parent.Gen_Id) then
6948 if No (Ent)
6949 or else Nkind (Ent) /= N_Defining_Identifier
6950 or else not In_Defining_Unit_Name (N)
6951 then
6952 Set_Associated_Node (New_N, Empty);
6953 end if;
6955 elsif No (Ent)
6956 or else
6957 not Nkind_In (Ent, N_Defining_Identifier,
6958 N_Defining_Character_Literal,
6959 N_Defining_Operator_Symbol)
6960 or else No (Scope (Ent))
6961 or else
6962 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6963 and then not Is_Child_Unit (Ent))
6964 or else
6965 (Scope_Depth (Scope (Ent)) >
6966 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6967 and then
6968 Get_Source_Unit (Ent) =
6969 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6970 then
6971 Set_Associated_Node (New_N, Empty);
6972 end if;
6974 -- Case of instantiating identifier or some other name or operator
6976 else
6977 -- If the associated node is still defined, the entity in it
6978 -- is global, and must be copied to the instance. If this copy
6979 -- is being made for a body to inline, it is applied to an
6980 -- instantiated tree, and the entity is already present and
6981 -- must be also preserved.
6983 declare
6984 Assoc : constant Node_Id := Get_Associated_Node (N);
6986 begin
6987 if Present (Assoc) then
6988 if Nkind (Assoc) = Nkind (N) then
6989 Set_Entity (New_N, Entity (Assoc));
6990 Check_Private_View (N);
6992 -- The name in the call may be a selected component if the
6993 -- call has not been analyzed yet, as may be the case for
6994 -- pre/post conditions in a generic unit.
6996 elsif Nkind (Assoc) = N_Function_Call
6997 and then Is_Entity_Name (Name (Assoc))
6998 then
6999 Set_Entity (New_N, Entity (Name (Assoc)));
7001 elsif Nkind_In (Assoc, N_Defining_Identifier,
7002 N_Defining_Character_Literal,
7003 N_Defining_Operator_Symbol)
7004 and then Expander_Active
7005 then
7006 -- Inlining case: we are copying a tree that contains
7007 -- global entities, which are preserved in the copy to be
7008 -- used for subsequent inlining.
7010 null;
7012 else
7013 Set_Entity (New_N, Empty);
7014 end if;
7015 end if;
7016 end;
7017 end if;
7019 -- For expanded name, we must copy the Prefix and Selector_Name
7021 if Nkind (N) = N_Expanded_Name then
7022 Set_Prefix
7023 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7025 Set_Selector_Name (New_N,
7026 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7028 -- For operators, we must copy the right operand
7030 elsif Nkind (N) in N_Op then
7031 Set_Right_Opnd (New_N,
7032 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7034 -- And for binary operators, the left operand as well
7036 if Nkind (N) in N_Binary_Op then
7037 Set_Left_Opnd (New_N,
7038 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7039 end if;
7040 end if;
7042 -- Special casing for stubs
7044 elsif Nkind (N) in N_Body_Stub then
7046 -- In any case, we must copy the specification or defining
7047 -- identifier as appropriate.
7049 if Nkind (N) = N_Subprogram_Body_Stub then
7050 Set_Specification (New_N,
7051 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7053 else
7054 Set_Defining_Identifier (New_N,
7055 Copy_Generic_Node
7056 (Defining_Identifier (N), New_N, Instantiating));
7057 end if;
7059 -- If we are not instantiating, then this is where we load and
7060 -- analyze subunits, i.e. at the point where the stub occurs. A
7061 -- more permissive system might defer this analysis to the point
7062 -- of instantiation, but this seems too complicated for now.
7064 if not Instantiating then
7065 declare
7066 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7067 Subunit : Node_Id;
7068 Unum : Unit_Number_Type;
7069 New_Body : Node_Id;
7071 begin
7072 -- Make sure that, if it is a subunit of the main unit that is
7073 -- preprocessed and if -gnateG is specified, the preprocessed
7074 -- file will be written.
7076 Lib.Analysing_Subunit_Of_Main :=
7077 Lib.In_Extended_Main_Source_Unit (N);
7078 Unum :=
7079 Load_Unit
7080 (Load_Name => Subunit_Name,
7081 Required => False,
7082 Subunit => True,
7083 Error_Node => N);
7084 Lib.Analysing_Subunit_Of_Main := False;
7086 -- If the proper body is not found, a warning message will be
7087 -- emitted when analyzing the stub, or later at the point of
7088 -- instantiation. Here we just leave the stub as is.
7090 if Unum = No_Unit then
7091 Subunits_Missing := True;
7092 goto Subunit_Not_Found;
7093 end if;
7095 Subunit := Cunit (Unum);
7097 if Nkind (Unit (Subunit)) /= N_Subunit then
7098 Error_Msg_N
7099 ("found child unit instead of expected SEPARATE subunit",
7100 Subunit);
7101 Error_Msg_Sloc := Sloc (N);
7102 Error_Msg_N ("\to complete stub #", Subunit);
7103 goto Subunit_Not_Found;
7104 end if;
7106 -- We must create a generic copy of the subunit, in order to
7107 -- perform semantic analysis on it, and we must replace the
7108 -- stub in the original generic unit with the subunit, in order
7109 -- to preserve non-local references within.
7111 -- Only the proper body needs to be copied. Library_Unit and
7112 -- context clause are simply inherited by the generic copy.
7113 -- Note that the copy (which may be recursive if there are
7114 -- nested subunits) must be done first, before attaching it to
7115 -- the enclosing generic.
7117 New_Body :=
7118 Copy_Generic_Node
7119 (Proper_Body (Unit (Subunit)),
7120 Empty, Instantiating => False);
7122 -- Now place the original proper body in the original generic
7123 -- unit. This is a body, not a compilation unit.
7125 Rewrite (N, Proper_Body (Unit (Subunit)));
7126 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7127 Set_Was_Originally_Stub (N);
7129 -- Finally replace the body of the subunit with its copy, and
7130 -- make this new subunit into the library unit of the generic
7131 -- copy, which does not have stubs any longer.
7133 Set_Proper_Body (Unit (Subunit), New_Body);
7134 Set_Library_Unit (New_N, Subunit);
7135 Inherit_Context (Unit (Subunit), N);
7136 end;
7138 -- If we are instantiating, this must be an error case, since
7139 -- otherwise we would have replaced the stub node by the proper body
7140 -- that corresponds. So just ignore it in the copy (i.e. we have
7141 -- copied it, and that is good enough).
7143 else
7144 null;
7145 end if;
7147 <<Subunit_Not_Found>> null;
7149 -- If the node is a compilation unit, it is the subunit of a stub, which
7150 -- has been loaded already (see code below). In this case, the library
7151 -- unit field of N points to the parent unit (which is a compilation
7152 -- unit) and need not (and cannot) be copied.
7154 -- When the proper body of the stub is analyzed, the library_unit link
7155 -- is used to establish the proper context (see sem_ch10).
7157 -- The other fields of a compilation unit are copied as usual
7159 elsif Nkind (N) = N_Compilation_Unit then
7161 -- This code can only be executed when not instantiating, because in
7162 -- the copy made for an instantiation, the compilation unit node has
7163 -- disappeared at the point that a stub is replaced by its proper
7164 -- body.
7166 pragma Assert (not Instantiating);
7168 Set_Context_Items (New_N,
7169 Copy_Generic_List (Context_Items (N), New_N));
7171 Set_Unit (New_N,
7172 Copy_Generic_Node (Unit (N), New_N, False));
7174 Set_First_Inlined_Subprogram (New_N,
7175 Copy_Generic_Node
7176 (First_Inlined_Subprogram (N), New_N, False));
7178 Set_Aux_Decls_Node (New_N,
7179 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7181 -- For an assignment node, the assignment is known to be semantically
7182 -- legal if we are instantiating the template. This avoids incorrect
7183 -- diagnostics in generated code.
7185 elsif Nkind (N) = N_Assignment_Statement then
7187 -- Copy name and expression fields in usual manner
7189 Set_Name (New_N,
7190 Copy_Generic_Node (Name (N), New_N, Instantiating));
7192 Set_Expression (New_N,
7193 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7195 if Instantiating then
7196 Set_Assignment_OK (Name (New_N), True);
7197 end if;
7199 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7200 if not Instantiating then
7201 Set_Associated_Node (N, New_N);
7203 else
7204 if Present (Get_Associated_Node (N))
7205 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7206 then
7207 -- In the generic the aggregate has some composite type. If at
7208 -- the point of instantiation the type has a private view,
7209 -- install the full view (and that of its ancestors, if any).
7211 declare
7212 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7213 Rt : Entity_Id;
7215 begin
7216 if Present (T) and then Is_Private_Type (T) then
7217 Switch_View (T);
7218 end if;
7220 if Present (T)
7221 and then Is_Tagged_Type (T)
7222 and then Is_Derived_Type (T)
7223 then
7224 Rt := Root_Type (T);
7226 loop
7227 T := Etype (T);
7229 if Is_Private_Type (T) then
7230 Switch_View (T);
7231 end if;
7233 exit when T = Rt;
7234 end loop;
7235 end if;
7236 end;
7237 end if;
7238 end if;
7240 -- Do not copy the associated node, which points to the generic copy
7241 -- of the aggregate.
7243 declare
7244 use Atree.Unchecked_Access;
7245 -- This code section is part of the implementation of an untyped
7246 -- tree traversal, so it needs direct access to node fields.
7248 begin
7249 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7250 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7251 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7252 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7253 end;
7255 -- Allocators do not have an identifier denoting the access type, so we
7256 -- must locate it through the expression to check whether the views are
7257 -- consistent.
7259 elsif Nkind (N) = N_Allocator
7260 and then Nkind (Expression (N)) = N_Qualified_Expression
7261 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7262 and then Instantiating
7263 then
7264 declare
7265 T : constant Node_Id :=
7266 Get_Associated_Node (Subtype_Mark (Expression (N)));
7267 Acc_T : Entity_Id;
7269 begin
7270 if Present (T) then
7272 -- Retrieve the allocator node in the generic copy
7274 Acc_T := Etype (Parent (Parent (T)));
7276 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7277 Switch_View (Acc_T);
7278 end if;
7279 end if;
7281 Copy_Descendants;
7282 end;
7284 -- For a proper body, we must catch the case of a proper body that
7285 -- replaces a stub. This represents the point at which a separate
7286 -- compilation unit, and hence template file, may be referenced, so we
7287 -- must make a new source instantiation entry for the template of the
7288 -- subunit, and ensure that all nodes in the subunit are adjusted using
7289 -- this new source instantiation entry.
7291 elsif Nkind (N) in N_Proper_Body then
7292 declare
7293 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7295 begin
7296 if Instantiating and then Was_Originally_Stub (N) then
7297 Create_Instantiation_Source
7298 (Instantiation_Node,
7299 Defining_Entity (N),
7300 False,
7301 S_Adjustment);
7302 end if;
7304 -- Now copy the fields of the proper body, using the new
7305 -- adjustment factor if one was needed as per test above.
7307 Copy_Descendants;
7309 -- Restore the original adjustment factor in case changed
7311 S_Adjustment := Save_Adjustment;
7312 end;
7314 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
7315 -- generic unit, not to the instantiating unit.
7317 elsif Nkind (N) = N_Pragma and then Instantiating then
7318 declare
7319 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
7320 begin
7321 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
7322 New_N := Make_Null_Statement (Sloc (N));
7323 else
7324 Copy_Descendants;
7325 end if;
7326 end;
7328 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7330 -- No descendant fields need traversing
7332 null;
7334 elsif Nkind (N) = N_String_Literal
7335 and then Present (Etype (N))
7336 and then Instantiating
7337 then
7338 -- If the string is declared in an outer scope, the string_literal
7339 -- subtype created for it may have the wrong scope. Force reanalysis
7340 -- of the constant to generate a new itype in the proper context.
7342 Set_Etype (New_N, Empty);
7343 Set_Analyzed (New_N, False);
7345 -- For the remaining nodes, copy their descendants recursively
7347 else
7348 Copy_Descendants;
7350 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7351 Set_Generic_Parent (Specification (New_N), N);
7353 -- Should preserve Corresponding_Spec??? (12.3(14))
7354 end if;
7355 end if;
7357 return New_N;
7358 end Copy_Generic_Node;
7360 ----------------------------
7361 -- Denotes_Formal_Package --
7362 ----------------------------
7364 function Denotes_Formal_Package
7365 (Pack : Entity_Id;
7366 On_Exit : Boolean := False;
7367 Instance : Entity_Id := Empty) return Boolean
7369 Par : Entity_Id;
7370 Scop : constant Entity_Id := Scope (Pack);
7371 E : Entity_Id;
7373 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7374 -- The package in question may be an actual for a previous formal
7375 -- package P of the current instance, so examine its actuals as well.
7376 -- This must be recursive over other formal packages.
7378 ----------------------------------
7379 -- Is_Actual_Of_Previous_Formal --
7380 ----------------------------------
7382 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7383 E1 : Entity_Id;
7385 begin
7386 E1 := First_Entity (P);
7387 while Present (E1) and then E1 /= Instance loop
7388 if Ekind (E1) = E_Package
7389 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7390 then
7391 if Renamed_Object (E1) = Pack then
7392 return True;
7394 elsif E1 = P or else Renamed_Object (E1) = P then
7395 return False;
7397 elsif Is_Actual_Of_Previous_Formal (E1) then
7398 return True;
7399 end if;
7400 end if;
7402 Next_Entity (E1);
7403 end loop;
7405 return False;
7406 end Is_Actual_Of_Previous_Formal;
7408 -- Start of processing for Denotes_Formal_Package
7410 begin
7411 if On_Exit then
7412 Par :=
7413 Instance_Envs.Table
7414 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7415 else
7416 Par := Current_Instantiated_Parent.Act_Id;
7417 end if;
7419 if Ekind (Scop) = E_Generic_Package
7420 or else Nkind (Unit_Declaration_Node (Scop)) =
7421 N_Generic_Subprogram_Declaration
7422 then
7423 return True;
7425 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7426 N_Formal_Package_Declaration
7427 then
7428 return True;
7430 elsif No (Par) then
7431 return False;
7433 else
7434 -- Check whether this package is associated with a formal package of
7435 -- the enclosing instantiation. Iterate over the list of renamings.
7437 E := First_Entity (Par);
7438 while Present (E) loop
7439 if Ekind (E) /= E_Package
7440 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7441 then
7442 null;
7444 elsif Renamed_Object (E) = Par then
7445 return False;
7447 elsif Renamed_Object (E) = Pack then
7448 return True;
7450 elsif Is_Actual_Of_Previous_Formal (E) then
7451 return True;
7453 end if;
7455 Next_Entity (E);
7456 end loop;
7458 return False;
7459 end if;
7460 end Denotes_Formal_Package;
7462 -----------------
7463 -- End_Generic --
7464 -----------------
7466 procedure End_Generic is
7467 begin
7468 -- ??? More things could be factored out in this routine. Should
7469 -- probably be done at a later stage.
7471 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7472 Generic_Flags.Decrement_Last;
7474 Expander_Mode_Restore;
7475 end End_Generic;
7477 -------------
7478 -- Earlier --
7479 -------------
7481 function Earlier (N1, N2 : Node_Id) return Boolean is
7482 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7483 -- Find distance from given node to enclosing compilation unit
7485 ----------------
7486 -- Find_Depth --
7487 ----------------
7489 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7490 begin
7491 while Present (P)
7492 and then Nkind (P) /= N_Compilation_Unit
7493 loop
7494 P := True_Parent (P);
7495 D := D + 1;
7496 end loop;
7497 end Find_Depth;
7499 -- Local declarations
7501 D1 : Integer := 0;
7502 D2 : Integer := 0;
7503 P1 : Node_Id := N1;
7504 P2 : Node_Id := N2;
7505 T1 : Source_Ptr;
7506 T2 : Source_Ptr;
7508 -- Start of processing for Earlier
7510 begin
7511 Find_Depth (P1, D1);
7512 Find_Depth (P2, D2);
7514 if P1 /= P2 then
7515 return False;
7516 else
7517 P1 := N1;
7518 P2 := N2;
7519 end if;
7521 while D1 > D2 loop
7522 P1 := True_Parent (P1);
7523 D1 := D1 - 1;
7524 end loop;
7526 while D2 > D1 loop
7527 P2 := True_Parent (P2);
7528 D2 := D2 - 1;
7529 end loop;
7531 -- At this point P1 and P2 are at the same distance from the root.
7532 -- We examine their parents until we find a common declarative list.
7533 -- If we reach the root, N1 and N2 do not descend from the same
7534 -- declarative list (e.g. one is nested in the declarative part and
7535 -- the other is in a block in the statement part) and the earlier
7536 -- one is already frozen.
7538 while not Is_List_Member (P1)
7539 or else not Is_List_Member (P2)
7540 or else List_Containing (P1) /= List_Containing (P2)
7541 loop
7542 P1 := True_Parent (P1);
7543 P2 := True_Parent (P2);
7545 if Nkind (Parent (P1)) = N_Subunit then
7546 P1 := Corresponding_Stub (Parent (P1));
7547 end if;
7549 if Nkind (Parent (P2)) = N_Subunit then
7550 P2 := Corresponding_Stub (Parent (P2));
7551 end if;
7553 if P1 = P2 then
7554 return False;
7555 end if;
7556 end loop;
7558 -- Expanded code usually shares the source location of the original
7559 -- construct it was generated for. This however may not necessarely
7560 -- reflect the true location of the code within the tree.
7562 -- Before comparing the slocs of the two nodes, make sure that we are
7563 -- working with correct source locations. Assume that P1 is to the left
7564 -- of P2. If either one does not come from source, traverse the common
7565 -- list heading towards the other node and locate the first source
7566 -- statement.
7568 -- P1 P2
7569 -- ----+===+===+--------------+===+===+----
7570 -- expanded code expanded code
7572 if not Comes_From_Source (P1) then
7573 while Present (P1) loop
7575 -- Neither P2 nor a source statement were located during the
7576 -- search. If we reach the end of the list, then P1 does not
7577 -- occur earlier than P2.
7579 -- ---->
7580 -- start --- P2 ----- P1 --- end
7582 if No (Next (P1)) then
7583 return False;
7585 -- We encounter P2 while going to the right of the list. This
7586 -- means that P1 does indeed appear earlier.
7588 -- ---->
7589 -- start --- P1 ===== P2 --- end
7590 -- expanded code in between
7592 elsif P1 = P2 then
7593 return True;
7595 -- No need to look any further since we have located a source
7596 -- statement.
7598 elsif Comes_From_Source (P1) then
7599 exit;
7600 end if;
7602 -- Keep going right
7604 Next (P1);
7605 end loop;
7606 end if;
7608 if not Comes_From_Source (P2) then
7609 while Present (P2) loop
7611 -- Neither P1 nor a source statement were located during the
7612 -- search. If we reach the start of the list, then P1 does not
7613 -- occur earlier than P2.
7615 -- <----
7616 -- start --- P2 --- P1 --- end
7618 if No (Prev (P2)) then
7619 return False;
7621 -- We encounter P1 while going to the left of the list. This
7622 -- means that P1 does indeed appear earlier.
7624 -- <----
7625 -- start --- P1 ===== P2 --- end
7626 -- expanded code in between
7628 elsif P2 = P1 then
7629 return True;
7631 -- No need to look any further since we have located a source
7632 -- statement.
7634 elsif Comes_From_Source (P2) then
7635 exit;
7636 end if;
7638 -- Keep going left
7640 Prev (P2);
7641 end loop;
7642 end if;
7644 -- At this point either both nodes came from source or we approximated
7645 -- their source locations through neighbouring source statements.
7647 T1 := Top_Level_Location (Sloc (P1));
7648 T2 := Top_Level_Location (Sloc (P2));
7650 -- When two nodes come from the same instance, they have identical top
7651 -- level locations. To determine proper relation within the tree, check
7652 -- their locations within the template.
7654 if T1 = T2 then
7655 return Sloc (P1) < Sloc (P2);
7657 -- The two nodes either come from unrelated instances or do not come
7658 -- from instantiated code at all.
7660 else
7661 return T1 < T2;
7662 end if;
7663 end Earlier;
7665 ----------------------
7666 -- Find_Actual_Type --
7667 ----------------------
7669 function Find_Actual_Type
7670 (Typ : Entity_Id;
7671 Gen_Type : Entity_Id) return Entity_Id
7673 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7674 T : Entity_Id;
7676 begin
7677 -- Special processing only applies to child units
7679 if not Is_Child_Unit (Gen_Scope) then
7680 return Get_Instance_Of (Typ);
7682 -- If designated or component type is itself a formal of the child unit,
7683 -- its instance is available.
7685 elsif Scope (Typ) = Gen_Scope then
7686 return Get_Instance_Of (Typ);
7688 -- If the array or access type is not declared in the parent unit,
7689 -- no special processing needed.
7691 elsif not Is_Generic_Type (Typ)
7692 and then Scope (Gen_Scope) /= Scope (Typ)
7693 then
7694 return Get_Instance_Of (Typ);
7696 -- Otherwise, retrieve designated or component type by visibility
7698 else
7699 T := Current_Entity (Typ);
7700 while Present (T) loop
7701 if In_Open_Scopes (Scope (T)) then
7702 return T;
7703 elsif Is_Generic_Actual_Type (T) then
7704 return T;
7705 end if;
7707 T := Homonym (T);
7708 end loop;
7710 return Typ;
7711 end if;
7712 end Find_Actual_Type;
7714 ----------------------------
7715 -- Freeze_Subprogram_Body --
7716 ----------------------------
7718 procedure Freeze_Subprogram_Body
7719 (Inst_Node : Node_Id;
7720 Gen_Body : Node_Id;
7721 Pack_Id : Entity_Id)
7723 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7724 Par : constant Entity_Id := Scope (Gen_Unit);
7725 E_G_Id : Entity_Id;
7726 Enc_G : Entity_Id;
7727 Enc_I : Node_Id;
7728 F_Node : Node_Id;
7730 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7731 -- Find innermost package body that encloses the given node, and which
7732 -- is not a compilation unit. Freeze nodes for the instance, or for its
7733 -- enclosing body, may be inserted after the enclosing_body of the
7734 -- generic unit. Used to determine proper placement of freeze node for
7735 -- both package and subprogram instances.
7737 function Package_Freeze_Node (B : Node_Id) return Node_Id;
7738 -- Find entity for given package body, and locate or create a freeze
7739 -- node for it.
7741 ----------------------------
7742 -- Enclosing_Package_Body --
7743 ----------------------------
7745 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
7746 P : Node_Id;
7748 begin
7749 P := Parent (N);
7750 while Present (P)
7751 and then Nkind (Parent (P)) /= N_Compilation_Unit
7752 loop
7753 if Nkind (P) = N_Package_Body then
7754 if Nkind (Parent (P)) = N_Subunit then
7755 return Corresponding_Stub (Parent (P));
7756 else
7757 return P;
7758 end if;
7759 end if;
7761 P := True_Parent (P);
7762 end loop;
7764 return Empty;
7765 end Enclosing_Package_Body;
7767 -------------------------
7768 -- Package_Freeze_Node --
7769 -------------------------
7771 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7772 Id : Entity_Id;
7774 begin
7775 if Nkind (B) = N_Package_Body then
7776 Id := Corresponding_Spec (B);
7777 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7778 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7779 end if;
7781 Ensure_Freeze_Node (Id);
7782 return Freeze_Node (Id);
7783 end Package_Freeze_Node;
7785 -- Start of processing of Freeze_Subprogram_Body
7787 begin
7788 -- If the instance and the generic body appear within the same unit, and
7789 -- the instance precedes the generic, the freeze node for the instance
7790 -- must appear after that of the generic. If the generic is nested
7791 -- within another instance I2, then current instance must be frozen
7792 -- after I2. In both cases, the freeze nodes are those of enclosing
7793 -- packages. Otherwise, the freeze node is placed at the end of the
7794 -- current declarative part.
7796 Enc_G := Enclosing_Package_Body (Gen_Body);
7797 Enc_I := Enclosing_Package_Body (Inst_Node);
7798 Ensure_Freeze_Node (Pack_Id);
7799 F_Node := Freeze_Node (Pack_Id);
7801 if Is_Generic_Instance (Par)
7802 and then Present (Freeze_Node (Par))
7803 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7804 then
7805 -- The parent was a premature instantiation. Insert freeze node at
7806 -- the end the current declarative part.
7808 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7809 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7811 -- Handle the following case:
7813 -- package Parent_Inst is new ...
7814 -- Parent_Inst []
7816 -- procedure P ... -- this body freezes Parent_Inst
7818 -- package Inst is new ...
7820 -- In this particular scenario, the freeze node for Inst must be
7821 -- inserted in the same manner as that of Parent_Inst - before the
7822 -- next source body or at the end of the declarative list (body not
7823 -- available). If body P did not exist and Parent_Inst was frozen
7824 -- after Inst, either by a body following Inst or at the end of the
7825 -- declarative region, the freeze node for Inst must be inserted
7826 -- after that of Parent_Inst. This relation is established by
7827 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7829 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7830 List_Containing (Inst_Node)
7831 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7832 then
7833 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7835 else
7836 Insert_After (Freeze_Node (Par), F_Node);
7837 end if;
7839 -- The body enclosing the instance should be frozen after the body that
7840 -- includes the generic, because the body of the instance may make
7841 -- references to entities therein. If the two are not in the same
7842 -- declarative part, or if the one enclosing the instance is frozen
7843 -- already, freeze the instance at the end of the current declarative
7844 -- part.
7846 elsif Is_Generic_Instance (Par)
7847 and then Present (Freeze_Node (Par))
7848 and then Present (Enc_I)
7849 then
7850 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7851 or else
7852 (Nkind (Enc_I) = N_Package_Body
7853 and then
7854 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7855 then
7856 -- The enclosing package may contain several instances. Rather
7857 -- than computing the earliest point at which to insert its freeze
7858 -- node, we place it at the end of the declarative part of the
7859 -- parent of the generic.
7861 Insert_Freeze_Node_For_Instance
7862 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7863 end if;
7865 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7867 elsif Present (Enc_G)
7868 and then Present (Enc_I)
7869 and then Enc_G /= Enc_I
7870 and then Earlier (Inst_Node, Gen_Body)
7871 then
7872 if Nkind (Enc_G) = N_Package_Body then
7873 E_G_Id :=
7874 Corresponding_Spec (Enc_G);
7875 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7876 E_G_Id :=
7877 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7878 end if;
7880 -- Freeze package that encloses instance, and place node after the
7881 -- package that encloses generic. If enclosing package is already
7882 -- frozen we have to assume it is at the proper place. This may be a
7883 -- potential ABE that requires dynamic checking. Do not add a freeze
7884 -- node if the package that encloses the generic is inside the body
7885 -- that encloses the instance, because the freeze node would be in
7886 -- the wrong scope. Additional contortions needed if the bodies are
7887 -- within a subunit.
7889 declare
7890 Enclosing_Body : Node_Id;
7892 begin
7893 if Nkind (Enc_I) = N_Package_Body_Stub then
7894 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7895 else
7896 Enclosing_Body := Enc_I;
7897 end if;
7899 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7900 Insert_Freeze_Node_For_Instance
7901 (Enc_G, Package_Freeze_Node (Enc_I));
7902 end if;
7903 end;
7905 -- Freeze enclosing subunit before instance
7907 Ensure_Freeze_Node (E_G_Id);
7909 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7910 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7911 end if;
7913 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7915 else
7916 -- If none of the above, insert freeze node at the end of the current
7917 -- declarative part.
7919 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7920 end if;
7921 end Freeze_Subprogram_Body;
7923 ----------------
7924 -- Get_Gen_Id --
7925 ----------------
7927 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7928 begin
7929 return Generic_Renamings.Table (E).Gen_Id;
7930 end Get_Gen_Id;
7932 ---------------------
7933 -- Get_Instance_Of --
7934 ---------------------
7936 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7937 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7939 begin
7940 if Res /= Assoc_Null then
7941 return Generic_Renamings.Table (Res).Act_Id;
7943 else
7944 -- On exit, entity is not instantiated: not a generic parameter, or
7945 -- else parameter of an inner generic unit.
7947 return A;
7948 end if;
7949 end Get_Instance_Of;
7951 ------------------------------------
7952 -- Get_Package_Instantiation_Node --
7953 ------------------------------------
7955 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7956 Decl : Node_Id := Unit_Declaration_Node (A);
7957 Inst : Node_Id;
7959 begin
7960 -- If the Package_Instantiation attribute has been set on the package
7961 -- entity, then use it directly when it (or its Original_Node) refers
7962 -- to an N_Package_Instantiation node. In principle it should be
7963 -- possible to have this field set in all cases, which should be
7964 -- investigated, and would allow this function to be significantly
7965 -- simplified. ???
7967 Inst := Package_Instantiation (A);
7969 if Present (Inst) then
7970 if Nkind (Inst) = N_Package_Instantiation then
7971 return Inst;
7973 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7974 return Original_Node (Inst);
7975 end if;
7976 end if;
7978 -- If the instantiation is a compilation unit that does not need body
7979 -- then the instantiation node has been rewritten as a package
7980 -- declaration for the instance, and we return the original node.
7982 -- If it is a compilation unit and the instance node has not been
7983 -- rewritten, then it is still the unit of the compilation. Finally, if
7984 -- a body is present, this is a parent of the main unit whose body has
7985 -- been compiled for inlining purposes, and the instantiation node has
7986 -- been rewritten with the instance body.
7988 -- Otherwise the instantiation node appears after the declaration. If
7989 -- the entity is a formal package, the declaration may have been
7990 -- rewritten as a generic declaration (in the case of a formal with box)
7991 -- or left as a formal package declaration if it has actuals, and is
7992 -- found with a forward search.
7994 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7995 if Nkind (Decl) = N_Package_Declaration
7996 and then Present (Corresponding_Body (Decl))
7997 then
7998 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7999 end if;
8001 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8002 return Original_Node (Decl);
8003 else
8004 return Unit (Parent (Decl));
8005 end if;
8007 elsif Nkind (Decl) = N_Package_Declaration
8008 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8009 then
8010 return Original_Node (Decl);
8012 else
8013 Inst := Next (Decl);
8014 while not Nkind_In (Inst, N_Package_Instantiation,
8015 N_Formal_Package_Declaration)
8016 loop
8017 Next (Inst);
8018 end loop;
8020 return Inst;
8021 end if;
8022 end Get_Package_Instantiation_Node;
8024 ------------------------
8025 -- Has_Been_Exchanged --
8026 ------------------------
8028 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8029 Next : Elmt_Id;
8031 begin
8032 Next := First_Elmt (Exchanged_Views);
8033 while Present (Next) loop
8034 if Full_View (Node (Next)) = E then
8035 return True;
8036 end if;
8038 Next_Elmt (Next);
8039 end loop;
8041 return False;
8042 end Has_Been_Exchanged;
8044 ----------
8045 -- Hash --
8046 ----------
8048 function Hash (F : Entity_Id) return HTable_Range is
8049 begin
8050 return HTable_Range (F mod HTable_Size);
8051 end Hash;
8053 ------------------------
8054 -- Hide_Current_Scope --
8055 ------------------------
8057 procedure Hide_Current_Scope is
8058 C : constant Entity_Id := Current_Scope;
8059 E : Entity_Id;
8061 begin
8062 Set_Is_Hidden_Open_Scope (C);
8064 E := First_Entity (C);
8065 while Present (E) loop
8066 if Is_Immediately_Visible (E) then
8067 Set_Is_Immediately_Visible (E, False);
8068 Append_Elmt (E, Hidden_Entities);
8069 end if;
8071 Next_Entity (E);
8072 end loop;
8074 -- Make the scope name invisible as well. This is necessary, but might
8075 -- conflict with calls to Rtsfind later on, in case the scope is a
8076 -- predefined one. There is no clean solution to this problem, so for
8077 -- now we depend on the user not redefining Standard itself in one of
8078 -- the parent units.
8080 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8081 Set_Is_Immediately_Visible (C, False);
8082 Append_Elmt (C, Hidden_Entities);
8083 end if;
8085 end Hide_Current_Scope;
8087 --------------
8088 -- Init_Env --
8089 --------------
8091 procedure Init_Env is
8092 Saved : Instance_Env;
8094 begin
8095 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8096 Saved.Exchanged_Views := Exchanged_Views;
8097 Saved.Hidden_Entities := Hidden_Entities;
8098 Saved.Current_Sem_Unit := Current_Sem_Unit;
8099 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8100 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8102 -- Save configuration switches. These may be reset if the unit is a
8103 -- predefined unit, and the current mode is not Ada 2005.
8105 Save_Opt_Config_Switches (Saved.Switches);
8107 Instance_Envs.Append (Saved);
8109 Exchanged_Views := New_Elmt_List;
8110 Hidden_Entities := New_Elmt_List;
8112 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8113 -- this is set properly in Set_Instance_Env.
8115 Current_Instantiated_Parent :=
8116 (Current_Scope, Current_Scope, Assoc_Null);
8117 end Init_Env;
8119 ------------------------------
8120 -- In_Same_Declarative_Part --
8121 ------------------------------
8123 function In_Same_Declarative_Part
8124 (F_Node : Node_Id;
8125 Inst : Node_Id) return Boolean
8127 Decls : constant Node_Id := Parent (F_Node);
8128 Nod : Node_Id;
8130 begin
8131 Nod := Parent (Inst);
8132 while Present (Nod) loop
8133 if Nod = Decls then
8134 return True;
8136 elsif Nkind_In (Nod, N_Subprogram_Body,
8137 N_Package_Body,
8138 N_Package_Declaration,
8139 N_Task_Body,
8140 N_Protected_Body,
8141 N_Block_Statement)
8142 then
8143 return False;
8145 elsif Nkind (Nod) = N_Subunit then
8146 Nod := Corresponding_Stub (Nod);
8148 elsif Nkind (Nod) = N_Compilation_Unit then
8149 return False;
8151 else
8152 Nod := Parent (Nod);
8153 end if;
8154 end loop;
8156 return False;
8157 end In_Same_Declarative_Part;
8159 ---------------------
8160 -- In_Main_Context --
8161 ---------------------
8163 function In_Main_Context (E : Entity_Id) return Boolean is
8164 Context : List_Id;
8165 Clause : Node_Id;
8166 Nam : Node_Id;
8168 begin
8169 if not Is_Compilation_Unit (E)
8170 or else Ekind (E) /= E_Package
8171 or else In_Private_Part (E)
8172 then
8173 return False;
8174 end if;
8176 Context := Context_Items (Cunit (Main_Unit));
8178 Clause := First (Context);
8179 while Present (Clause) loop
8180 if Nkind (Clause) = N_With_Clause then
8181 Nam := Name (Clause);
8183 -- If the current scope is part of the context of the main unit,
8184 -- analysis of the corresponding with_clause is not complete, and
8185 -- the entity is not set. We use the Chars field directly, which
8186 -- might produce false positives in rare cases, but guarantees
8187 -- that we produce all the instance bodies we will need.
8189 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8190 or else (Nkind (Nam) = N_Selected_Component
8191 and then Chars (Selector_Name (Nam)) = Chars (E))
8192 then
8193 return True;
8194 end if;
8195 end if;
8197 Next (Clause);
8198 end loop;
8200 return False;
8201 end In_Main_Context;
8203 ---------------------
8204 -- Inherit_Context --
8205 ---------------------
8207 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8208 Current_Context : List_Id;
8209 Current_Unit : Node_Id;
8210 Item : Node_Id;
8211 New_I : Node_Id;
8213 Clause : Node_Id;
8214 OK : Boolean;
8215 Lib_Unit : Node_Id;
8217 begin
8218 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8220 -- The inherited context is attached to the enclosing compilation
8221 -- unit. This is either the main unit, or the declaration for the
8222 -- main unit (in case the instantiation appears within the package
8223 -- declaration and the main unit is its body).
8225 Current_Unit := Parent (Inst);
8226 while Present (Current_Unit)
8227 and then Nkind (Current_Unit) /= N_Compilation_Unit
8228 loop
8229 Current_Unit := Parent (Current_Unit);
8230 end loop;
8232 Current_Context := Context_Items (Current_Unit);
8234 Item := First (Context_Items (Parent (Gen_Decl)));
8235 while Present (Item) loop
8236 if Nkind (Item) = N_With_Clause then
8237 Lib_Unit := Library_Unit (Item);
8239 -- Take care to prevent direct cyclic with's
8241 if Lib_Unit /= Current_Unit then
8243 -- Do not add a unit if it is already in the context
8245 Clause := First (Current_Context);
8246 OK := True;
8247 while Present (Clause) loop
8248 if Nkind (Clause) = N_With_Clause and then
8249 Library_Unit (Clause) = Lib_Unit
8250 then
8251 OK := False;
8252 exit;
8253 end if;
8255 Next (Clause);
8256 end loop;
8258 if OK then
8259 New_I := New_Copy (Item);
8260 Set_Implicit_With (New_I, True);
8261 Set_Implicit_With_From_Instantiation (New_I, True);
8262 Append (New_I, Current_Context);
8263 end if;
8264 end if;
8265 end if;
8267 Next (Item);
8268 end loop;
8269 end if;
8270 end Inherit_Context;
8272 ----------------
8273 -- Initialize --
8274 ----------------
8276 procedure Initialize is
8277 begin
8278 Generic_Renamings.Init;
8279 Instance_Envs.Init;
8280 Generic_Flags.Init;
8281 Generic_Renamings_HTable.Reset;
8282 Circularity_Detected := False;
8283 Exchanged_Views := No_Elist;
8284 Hidden_Entities := No_Elist;
8285 end Initialize;
8287 -------------------------------------
8288 -- Insert_Freeze_Node_For_Instance --
8289 -------------------------------------
8291 procedure Insert_Freeze_Node_For_Instance
8292 (N : Node_Id;
8293 F_Node : Node_Id)
8295 Decl : Node_Id;
8296 Decls : List_Id;
8297 Inst : Entity_Id;
8298 Par_N : Node_Id;
8300 function Enclosing_Body (N : Node_Id) return Node_Id;
8301 -- Find enclosing package or subprogram body, if any. Freeze node may
8302 -- be placed at end of current declarative list if previous instance
8303 -- and current one have different enclosing bodies.
8305 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8306 -- Find the local instance, if any, that declares the generic that is
8307 -- being instantiated. If present, the freeze node for this instance
8308 -- must follow the freeze node for the previous instance.
8310 --------------------
8311 -- Enclosing_Body --
8312 --------------------
8314 function Enclosing_Body (N : Node_Id) return Node_Id is
8315 P : Node_Id;
8317 begin
8318 P := Parent (N);
8319 while Present (P)
8320 and then Nkind (Parent (P)) /= N_Compilation_Unit
8321 loop
8322 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8323 if Nkind (Parent (P)) = N_Subunit then
8324 return Corresponding_Stub (Parent (P));
8325 else
8326 return P;
8327 end if;
8328 end if;
8330 P := True_Parent (P);
8331 end loop;
8333 return Empty;
8334 end Enclosing_Body;
8336 -----------------------
8337 -- Previous_Instance --
8338 -----------------------
8340 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8341 S : Entity_Id;
8343 begin
8344 S := Scope (Gen);
8345 while Present (S) and then S /= Standard_Standard loop
8346 if Is_Generic_Instance (S)
8347 and then In_Same_Source_Unit (S, N)
8348 then
8349 return S;
8350 end if;
8352 S := Scope (S);
8353 end loop;
8355 return Empty;
8356 end Previous_Instance;
8358 -- Start of processing for Insert_Freeze_Node_For_Instance
8360 begin
8361 if not Is_List_Member (F_Node) then
8362 Decl := N;
8363 Decls := List_Containing (N);
8364 Inst := Entity (F_Node);
8365 Par_N := Parent (Decls);
8367 -- When processing a subprogram instantiation, utilize the actual
8368 -- subprogram instantiation rather than its package wrapper as it
8369 -- carries all the context information.
8371 if Is_Wrapper_Package (Inst) then
8372 Inst := Related_Instance (Inst);
8373 end if;
8375 -- If this is a package instance, check whether the generic is
8376 -- declared in a previous instance and the current instance is
8377 -- not within the previous one.
8379 if Present (Generic_Parent (Parent (Inst)))
8380 and then Is_In_Main_Unit (N)
8381 then
8382 declare
8383 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8384 Par_I : constant Entity_Id :=
8385 Previous_Instance
8386 (Generic_Parent (Parent (Inst)));
8387 Scop : Entity_Id;
8389 begin
8390 if Present (Par_I)
8391 and then Earlier (N, Freeze_Node (Par_I))
8392 then
8393 Scop := Scope (Inst);
8395 -- If the current instance is within the one that contains
8396 -- the generic, the freeze node for the current one must
8397 -- appear in the current declarative part. Ditto, if the
8398 -- current instance is within another package instance or
8399 -- within a body that does not enclose the current instance.
8400 -- In these three cases the freeze node of the previous
8401 -- instance is not relevant.
8403 while Present (Scop) and then Scop /= Standard_Standard loop
8404 exit when Scop = Par_I
8405 or else
8406 (Is_Generic_Instance (Scop)
8407 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8408 Scop := Scope (Scop);
8409 end loop;
8411 -- Previous instance encloses current instance
8413 if Scop = Par_I then
8414 null;
8416 -- If the next node is a source body we must freeze in
8417 -- the current scope as well.
8419 elsif Present (Next (N))
8420 and then Nkind_In (Next (N), N_Subprogram_Body,
8421 N_Package_Body)
8422 and then Comes_From_Source (Next (N))
8423 then
8424 null;
8426 -- Current instance is within an unrelated instance
8428 elsif Is_Generic_Instance (Scop) then
8429 null;
8431 -- Current instance is within an unrelated body
8433 elsif Present (Enclosing_N)
8434 and then Enclosing_N /= Enclosing_Body (Par_I)
8435 then
8436 null;
8438 else
8439 Insert_After (Freeze_Node (Par_I), F_Node);
8440 return;
8441 end if;
8442 end if;
8443 end;
8444 end if;
8446 -- When the instantiation occurs in a package declaration, append the
8447 -- freeze node to the private declarations (if any).
8449 if Nkind (Par_N) = N_Package_Specification
8450 and then Decls = Visible_Declarations (Par_N)
8451 and then Present (Private_Declarations (Par_N))
8452 and then not Is_Empty_List (Private_Declarations (Par_N))
8453 then
8454 Decls := Private_Declarations (Par_N);
8455 Decl := First (Decls);
8456 end if;
8458 -- Determine the proper freeze point of a package instantiation. We
8459 -- adhere to the general rule of a package or subprogram body causing
8460 -- freezing of anything before it in the same declarative region. In
8461 -- this case, the proper freeze point of a package instantiation is
8462 -- before the first source body which follows, or before a stub. This
8463 -- ensures that entities coming from the instance are already frozen
8464 -- and usable in source bodies.
8466 if Nkind (Par_N) /= N_Package_Declaration
8467 and then Ekind (Inst) = E_Package
8468 and then Is_Generic_Instance (Inst)
8469 and then
8470 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8471 then
8472 while Present (Decl) loop
8473 if (Nkind (Decl) in N_Unit_Body
8474 or else
8475 Nkind (Decl) in N_Body_Stub)
8476 and then Comes_From_Source (Decl)
8477 then
8478 Insert_Before (Decl, F_Node);
8479 return;
8480 end if;
8482 Next (Decl);
8483 end loop;
8484 end if;
8486 -- In a package declaration, or if no previous body, insert at end
8487 -- of list.
8489 Set_Sloc (F_Node, Sloc (Last (Decls)));
8490 Insert_After (Last (Decls), F_Node);
8491 end if;
8492 end Insert_Freeze_Node_For_Instance;
8494 ------------------
8495 -- Install_Body --
8496 ------------------
8498 procedure Install_Body
8499 (Act_Body : Node_Id;
8500 N : Node_Id;
8501 Gen_Body : Node_Id;
8502 Gen_Decl : Node_Id)
8504 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8505 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8506 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8507 Par : constant Entity_Id := Scope (Gen_Id);
8508 Gen_Unit : constant Node_Id :=
8509 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8510 Orig_Body : Node_Id := Gen_Body;
8511 F_Node : Node_Id;
8512 Body_Unit : Node_Id;
8514 Must_Delay : Boolean;
8516 function In_Same_Enclosing_Subp return Boolean;
8517 -- Check whether instance and generic body are within same subprogram.
8519 function True_Sloc (N : Node_Id) return Source_Ptr;
8520 -- If the instance is nested inside a generic unit, the Sloc of the
8521 -- instance indicates the place of the original definition, not the
8522 -- point of the current enclosing instance. Pending a better usage of
8523 -- Slocs to indicate instantiation places, we determine the place of
8524 -- origin of a node by finding the maximum sloc of any ancestor node.
8525 -- Why is this not equivalent to Top_Level_Location ???
8527 ----------------------------
8528 -- In_Same_Enclosing_Subp --
8529 ----------------------------
8531 function In_Same_Enclosing_Subp return Boolean is
8532 Scop : Entity_Id;
8533 Subp : Entity_Id;
8535 begin
8536 Scop := Scope (Act_Id);
8537 while Scop /= Standard_Standard
8538 and then not Is_Overloadable (Scop)
8539 loop
8540 Scop := Scope (Scop);
8541 end loop;
8543 if Scop = Standard_Standard then
8544 return False;
8545 else
8546 Subp := Scop;
8547 end if;
8549 Scop := Scope (Gen_Id);
8550 while Scop /= Standard_Standard loop
8551 if Scop = Subp then
8552 return True;
8553 else
8554 Scop := Scope (Scop);
8555 end if;
8556 end loop;
8558 return False;
8559 end In_Same_Enclosing_Subp;
8561 ---------------
8562 -- True_Sloc --
8563 ---------------
8565 function True_Sloc (N : Node_Id) return Source_Ptr is
8566 Res : Source_Ptr;
8567 N1 : Node_Id;
8569 begin
8570 Res := Sloc (N);
8571 N1 := N;
8572 while Present (N1) and then N1 /= Act_Unit loop
8573 if Sloc (N1) > Res then
8574 Res := Sloc (N1);
8575 end if;
8577 N1 := Parent (N1);
8578 end loop;
8580 return Res;
8581 end True_Sloc;
8583 -- Start of processing for Install_Body
8585 begin
8586 -- If the body is a subunit, the freeze point is the corresponding stub
8587 -- in the current compilation, not the subunit itself.
8589 if Nkind (Parent (Gen_Body)) = N_Subunit then
8590 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8591 else
8592 Orig_Body := Gen_Body;
8593 end if;
8595 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8597 -- If the instantiation and the generic definition appear in the same
8598 -- package declaration, this is an early instantiation. If they appear
8599 -- in the same declarative part, it is an early instantiation only if
8600 -- the generic body appears textually later, and the generic body is
8601 -- also in the main unit.
8603 -- If instance is nested within a subprogram, and the generic body
8604 -- is not, the instance is delayed because the enclosing body is. If
8605 -- instance and body are within the same scope, or the same subprogram
8606 -- body, indicate explicitly that the instance is delayed.
8608 Must_Delay :=
8609 (Gen_Unit = Act_Unit
8610 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8611 N_Generic_Package_Declaration)
8612 or else (Gen_Unit = Body_Unit
8613 and then True_Sloc (N) < Sloc (Orig_Body)))
8614 and then Is_In_Main_Unit (Gen_Unit)
8615 and then (Scope (Act_Id) = Scope (Gen_Id)
8616 or else In_Same_Enclosing_Subp));
8618 -- If this is an early instantiation, the freeze node is placed after
8619 -- the generic body. Otherwise, if the generic appears in an instance,
8620 -- we cannot freeze the current instance until the outer one is frozen.
8621 -- This is only relevant if the current instance is nested within some
8622 -- inner scope not itself within the outer instance. If this scope is
8623 -- a package body in the same declarative part as the outer instance,
8624 -- then that body needs to be frozen after the outer instance. Finally,
8625 -- if no delay is needed, we place the freeze node at the end of the
8626 -- current declarative part.
8628 if Expander_Active then
8629 Ensure_Freeze_Node (Act_Id);
8630 F_Node := Freeze_Node (Act_Id);
8632 if Must_Delay then
8633 Insert_After (Orig_Body, F_Node);
8635 elsif Is_Generic_Instance (Par)
8636 and then Present (Freeze_Node (Par))
8637 and then Scope (Act_Id) /= Par
8638 then
8639 -- Freeze instance of inner generic after instance of enclosing
8640 -- generic.
8642 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8644 -- Handle the following case:
8646 -- package Parent_Inst is new ...
8647 -- Parent_Inst []
8649 -- procedure P ... -- this body freezes Parent_Inst
8651 -- package Inst is new ...
8653 -- In this particular scenario, the freeze node for Inst must
8654 -- be inserted in the same manner as that of Parent_Inst,
8655 -- before the next source body or at the end of the declarative
8656 -- list (body not available). If body P did not exist and
8657 -- Parent_Inst was frozen after Inst, either by a body
8658 -- following Inst or at the end of the declarative region,
8659 -- the freeze node for Inst must be inserted after that of
8660 -- Parent_Inst. This relation is established by comparing
8661 -- the Slocs of Parent_Inst freeze node and Inst.
8663 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8664 List_Containing (N)
8665 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8666 then
8667 Insert_Freeze_Node_For_Instance (N, F_Node);
8668 else
8669 Insert_After (Freeze_Node (Par), F_Node);
8670 end if;
8672 -- Freeze package enclosing instance of inner generic after
8673 -- instance of enclosing generic.
8675 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8676 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8677 then
8678 declare
8679 Enclosing : Entity_Id;
8681 begin
8682 Enclosing := Corresponding_Spec (Parent (N));
8684 if No (Enclosing) then
8685 Enclosing := Defining_Entity (Parent (N));
8686 end if;
8688 Insert_Freeze_Node_For_Instance (N, F_Node);
8689 Ensure_Freeze_Node (Enclosing);
8691 if not Is_List_Member (Freeze_Node (Enclosing)) then
8693 -- The enclosing context is a subunit, insert the freeze
8694 -- node after the stub.
8696 if Nkind (Parent (Parent (N))) = N_Subunit then
8697 Insert_Freeze_Node_For_Instance
8698 (Corresponding_Stub (Parent (Parent (N))),
8699 Freeze_Node (Enclosing));
8701 -- The enclosing context is a package with a stub body
8702 -- which has already been replaced by the real body.
8703 -- Insert the freeze node after the actual body.
8705 elsif Ekind (Enclosing) = E_Package
8706 and then Present (Body_Entity (Enclosing))
8707 and then Was_Originally_Stub
8708 (Parent (Body_Entity (Enclosing)))
8709 then
8710 Insert_Freeze_Node_For_Instance
8711 (Parent (Body_Entity (Enclosing)),
8712 Freeze_Node (Enclosing));
8714 -- The parent instance has been frozen before the body of
8715 -- the enclosing package, insert the freeze node after
8716 -- the body.
8718 elsif List_Containing (Freeze_Node (Par)) =
8719 List_Containing (Parent (N))
8720 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8721 then
8722 Insert_Freeze_Node_For_Instance
8723 (Parent (N), Freeze_Node (Enclosing));
8725 else
8726 Insert_After
8727 (Freeze_Node (Par), Freeze_Node (Enclosing));
8728 end if;
8729 end if;
8730 end;
8732 else
8733 Insert_Freeze_Node_For_Instance (N, F_Node);
8734 end if;
8736 else
8737 Insert_Freeze_Node_For_Instance (N, F_Node);
8738 end if;
8739 end if;
8741 Set_Is_Frozen (Act_Id);
8742 Insert_Before (N, Act_Body);
8743 Mark_Rewrite_Insertion (Act_Body);
8744 end Install_Body;
8746 -----------------------------
8747 -- Install_Formal_Packages --
8748 -----------------------------
8750 procedure Install_Formal_Packages (Par : Entity_Id) is
8751 E : Entity_Id;
8752 Gen : Entity_Id;
8753 Gen_E : Entity_Id := Empty;
8755 begin
8756 E := First_Entity (Par);
8758 -- If we are installing an instance parent, locate the formal packages
8759 -- of its generic parent.
8761 if Is_Generic_Instance (Par) then
8762 Gen := Generic_Parent (Package_Specification (Par));
8763 Gen_E := First_Entity (Gen);
8764 end if;
8766 while Present (E) loop
8767 if Ekind (E) = E_Package
8768 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
8769 then
8770 -- If this is the renaming for the parent instance, done
8772 if Renamed_Object (E) = Par then
8773 exit;
8775 -- The visibility of a formal of an enclosing generic is already
8776 -- correct.
8778 elsif Denotes_Formal_Package (E) then
8779 null;
8781 elsif Present (Associated_Formal_Package (E)) then
8782 Check_Generic_Actuals (Renamed_Object (E), True);
8783 Set_Is_Hidden (E, False);
8785 -- Find formal package in generic unit that corresponds to
8786 -- (instance of) formal package in instance.
8788 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
8789 Next_Entity (Gen_E);
8790 end loop;
8792 if Present (Gen_E) then
8793 Map_Formal_Package_Entities (Gen_E, E);
8794 end if;
8795 end if;
8796 end if;
8798 Next_Entity (E);
8800 if Present (Gen_E) then
8801 Next_Entity (Gen_E);
8802 end if;
8803 end loop;
8804 end Install_Formal_Packages;
8806 --------------------
8807 -- Install_Parent --
8808 --------------------
8810 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
8811 Ancestors : constant Elist_Id := New_Elmt_List;
8812 S : constant Entity_Id := Current_Scope;
8813 Inst_Par : Entity_Id;
8814 First_Par : Entity_Id;
8815 Inst_Node : Node_Id;
8816 Gen_Par : Entity_Id;
8817 First_Gen : Entity_Id;
8818 Elmt : Elmt_Id;
8820 procedure Install_Noninstance_Specs (Par : Entity_Id);
8821 -- Install the scopes of noninstance parent units ending with Par
8823 procedure Install_Spec (Par : Entity_Id);
8824 -- The child unit is within the declarative part of the parent, so the
8825 -- declarations within the parent are immediately visible.
8827 -------------------------------
8828 -- Install_Noninstance_Specs --
8829 -------------------------------
8831 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8832 begin
8833 if Present (Par)
8834 and then Par /= Standard_Standard
8835 and then not In_Open_Scopes (Par)
8836 then
8837 Install_Noninstance_Specs (Scope (Par));
8838 Install_Spec (Par);
8839 end if;
8840 end Install_Noninstance_Specs;
8842 ------------------
8843 -- Install_Spec --
8844 ------------------
8846 procedure Install_Spec (Par : Entity_Id) is
8847 Spec : constant Node_Id := Package_Specification (Par);
8849 begin
8850 -- If this parent of the child instance is a top-level unit,
8851 -- then record the unit and its visibility for later resetting in
8852 -- Remove_Parent. We exclude units that are generic instances, as we
8853 -- only want to record this information for the ultimate top-level
8854 -- noninstance parent (is that always correct???).
8856 if Scope (Par) = Standard_Standard
8857 and then not Is_Generic_Instance (Par)
8858 then
8859 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8860 Instance_Parent_Unit := Par;
8861 end if;
8863 -- Open the parent scope and make it and its declarations visible.
8864 -- If this point is not within a body, then only the visible
8865 -- declarations should be made visible, and installation of the
8866 -- private declarations is deferred until the appropriate point
8867 -- within analysis of the spec being instantiated (see the handling
8868 -- of parent visibility in Analyze_Package_Specification). This is
8869 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8870 -- private view problems that occur when compiling instantiations of
8871 -- a generic child of that package (Generic_Dispatching_Constructor).
8872 -- If the instance freezes a tagged type, inlinings of operations
8873 -- from Ada.Tags may need the full view of type Tag. If inlining took
8874 -- proper account of establishing visibility of inlined subprograms'
8875 -- parents then it should be possible to remove this
8876 -- special check. ???
8878 Push_Scope (Par);
8879 Set_Is_Immediately_Visible (Par);
8880 Install_Visible_Declarations (Par);
8881 Set_Use (Visible_Declarations (Spec));
8883 if In_Body or else Is_RTU (Par, Ada_Tags) then
8884 Install_Private_Declarations (Par);
8885 Set_Use (Private_Declarations (Spec));
8886 end if;
8887 end Install_Spec;
8889 -- Start of processing for Install_Parent
8891 begin
8892 -- We need to install the parent instance to compile the instantiation
8893 -- of the child, but the child instance must appear in the current
8894 -- scope. Given that we cannot place the parent above the current scope
8895 -- in the scope stack, we duplicate the current scope and unstack both
8896 -- after the instantiation is complete.
8898 -- If the parent is itself the instantiation of a child unit, we must
8899 -- also stack the instantiation of its parent, and so on. Each such
8900 -- ancestor is the prefix of the name in a prior instantiation.
8902 -- If this is a nested instance, the parent unit itself resolves to
8903 -- a renaming of the parent instance, whose declaration we need.
8905 -- Finally, the parent may be a generic (not an instance) when the
8906 -- child unit appears as a formal package.
8908 Inst_Par := P;
8910 if Present (Renamed_Entity (Inst_Par)) then
8911 Inst_Par := Renamed_Entity (Inst_Par);
8912 end if;
8914 First_Par := Inst_Par;
8916 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8918 First_Gen := Gen_Par;
8920 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
8922 -- Load grandparent instance as well
8924 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8926 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8927 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8929 if Present (Renamed_Entity (Inst_Par)) then
8930 Inst_Par := Renamed_Entity (Inst_Par);
8931 end if;
8933 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
8935 if Present (Gen_Par) then
8936 Prepend_Elmt (Inst_Par, Ancestors);
8938 else
8939 -- Parent is not the name of an instantiation
8941 Install_Noninstance_Specs (Inst_Par);
8942 exit;
8943 end if;
8945 else
8946 -- Previous error
8948 exit;
8949 end if;
8950 end loop;
8952 if Present (First_Gen) then
8953 Append_Elmt (First_Par, Ancestors);
8954 else
8955 Install_Noninstance_Specs (First_Par);
8956 end if;
8958 if not Is_Empty_Elmt_List (Ancestors) then
8959 Elmt := First_Elmt (Ancestors);
8960 while Present (Elmt) loop
8961 Install_Spec (Node (Elmt));
8962 Install_Formal_Packages (Node (Elmt));
8963 Next_Elmt (Elmt);
8964 end loop;
8965 end if;
8967 if not In_Body then
8968 Push_Scope (S);
8969 end if;
8970 end Install_Parent;
8972 -------------------------------
8973 -- Install_Hidden_Primitives --
8974 -------------------------------
8976 procedure Install_Hidden_Primitives
8977 (Prims_List : in out Elist_Id;
8978 Gen_T : Entity_Id;
8979 Act_T : Entity_Id)
8981 Elmt : Elmt_Id;
8982 List : Elist_Id := No_Elist;
8983 Prim_G_Elmt : Elmt_Id;
8984 Prim_A_Elmt : Elmt_Id;
8985 Prim_G : Node_Id;
8986 Prim_A : Node_Id;
8988 begin
8989 -- No action needed in case of serious errors because we cannot trust
8990 -- in the order of primitives
8992 if Serious_Errors_Detected > 0 then
8993 return;
8995 -- No action possible if we don't have available the list of primitive
8996 -- operations
8998 elsif No (Gen_T)
8999 or else not Is_Record_Type (Gen_T)
9000 or else not Is_Tagged_Type (Gen_T)
9001 or else not Is_Record_Type (Act_T)
9002 or else not Is_Tagged_Type (Act_T)
9003 then
9004 return;
9006 -- There is no need to handle interface types since their primitives
9007 -- cannot be hidden
9009 elsif Is_Interface (Gen_T) then
9010 return;
9011 end if;
9013 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9015 if not Is_Class_Wide_Type (Act_T) then
9016 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9017 else
9018 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9019 end if;
9021 loop
9022 -- Skip predefined primitives in the generic formal
9024 while Present (Prim_G_Elmt)
9025 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9026 loop
9027 Next_Elmt (Prim_G_Elmt);
9028 end loop;
9030 -- Skip predefined primitives in the generic actual
9032 while Present (Prim_A_Elmt)
9033 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9034 loop
9035 Next_Elmt (Prim_A_Elmt);
9036 end loop;
9038 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9040 Prim_G := Node (Prim_G_Elmt);
9041 Prim_A := Node (Prim_A_Elmt);
9043 -- There is no need to handle interface primitives because their
9044 -- primitives are not hidden
9046 exit when Present (Interface_Alias (Prim_G));
9048 -- Here we install one hidden primitive
9050 if Chars (Prim_G) /= Chars (Prim_A)
9051 and then Has_Suffix (Prim_A, 'P')
9052 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9053 then
9054 Set_Chars (Prim_A, Chars (Prim_G));
9055 Append_New_Elmt (Prim_A, To => List);
9056 end if;
9058 Next_Elmt (Prim_A_Elmt);
9059 Next_Elmt (Prim_G_Elmt);
9060 end loop;
9062 -- Append the elements to the list of temporarily visible primitives
9063 -- avoiding duplicates.
9065 if Present (List) then
9066 if No (Prims_List) then
9067 Prims_List := New_Elmt_List;
9068 end if;
9070 Elmt := First_Elmt (List);
9071 while Present (Elmt) loop
9072 Append_Unique_Elmt (Node (Elmt), Prims_List);
9073 Next_Elmt (Elmt);
9074 end loop;
9075 end if;
9076 end Install_Hidden_Primitives;
9078 -------------------------------
9079 -- Restore_Hidden_Primitives --
9080 -------------------------------
9082 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9083 Prim_Elmt : Elmt_Id;
9084 Prim : Node_Id;
9086 begin
9087 if Prims_List /= No_Elist then
9088 Prim_Elmt := First_Elmt (Prims_List);
9089 while Present (Prim_Elmt) loop
9090 Prim := Node (Prim_Elmt);
9091 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9092 Next_Elmt (Prim_Elmt);
9093 end loop;
9095 Prims_List := No_Elist;
9096 end if;
9097 end Restore_Hidden_Primitives;
9099 --------------------------------
9100 -- Instantiate_Formal_Package --
9101 --------------------------------
9103 function Instantiate_Formal_Package
9104 (Formal : Node_Id;
9105 Actual : Node_Id;
9106 Analyzed_Formal : Node_Id) return List_Id
9108 Loc : constant Source_Ptr := Sloc (Actual);
9109 Actual_Pack : Entity_Id;
9110 Formal_Pack : Entity_Id;
9111 Gen_Parent : Entity_Id;
9112 Decls : List_Id;
9113 Nod : Node_Id;
9114 Parent_Spec : Node_Id;
9116 procedure Find_Matching_Actual
9117 (F : Node_Id;
9118 Act : in out Entity_Id);
9119 -- We need to associate each formal entity in the formal package with
9120 -- the corresponding entity in the actual package. The actual package
9121 -- has been analyzed and possibly expanded, and as a result there is
9122 -- no one-to-one correspondence between the two lists (for example,
9123 -- the actual may include subtypes, itypes, and inherited primitive
9124 -- operations, interspersed among the renaming declarations for the
9125 -- actuals) . We retrieve the corresponding actual by name because each
9126 -- actual has the same name as the formal, and they do appear in the
9127 -- same order.
9129 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9130 -- Retrieve entity of defining entity of generic formal parameter.
9131 -- Only the declarations of formals need to be considered when
9132 -- linking them to actuals, but the declarative list may include
9133 -- internal entities generated during analysis, and those are ignored.
9135 procedure Match_Formal_Entity
9136 (Formal_Node : Node_Id;
9137 Formal_Ent : Entity_Id;
9138 Actual_Ent : Entity_Id);
9139 -- Associates the formal entity with the actual. In the case where
9140 -- Formal_Ent is a formal package, this procedure iterates through all
9141 -- of its formals and enters associations between the actuals occurring
9142 -- in the formal package's corresponding actual package (given by
9143 -- Actual_Ent) and the formal package's formal parameters. This
9144 -- procedure recurses if any of the parameters is itself a package.
9146 function Is_Instance_Of
9147 (Act_Spec : Entity_Id;
9148 Gen_Anc : Entity_Id) return Boolean;
9149 -- The actual can be an instantiation of a generic within another
9150 -- instance, in which case there is no direct link from it to the
9151 -- original generic ancestor. In that case, we recognize that the
9152 -- ultimate ancestor is the same by examining names and scopes.
9154 procedure Process_Nested_Formal (Formal : Entity_Id);
9155 -- If the current formal is declared with a box, its own formals are
9156 -- visible in the instance, as they were in the generic, and their
9157 -- Hidden flag must be reset. If some of these formals are themselves
9158 -- packages declared with a box, the processing must be recursive.
9160 --------------------------
9161 -- Find_Matching_Actual --
9162 --------------------------
9164 procedure Find_Matching_Actual
9165 (F : Node_Id;
9166 Act : in out Entity_Id)
9168 Formal_Ent : Entity_Id;
9170 begin
9171 case Nkind (Original_Node (F)) is
9172 when N_Formal_Object_Declaration |
9173 N_Formal_Type_Declaration =>
9174 Formal_Ent := Defining_Identifier (F);
9176 while Chars (Act) /= Chars (Formal_Ent) loop
9177 Next_Entity (Act);
9178 end loop;
9180 when N_Formal_Subprogram_Declaration |
9181 N_Formal_Package_Declaration |
9182 N_Package_Declaration |
9183 N_Generic_Package_Declaration =>
9184 Formal_Ent := Defining_Entity (F);
9186 while Chars (Act) /= Chars (Formal_Ent) loop
9187 Next_Entity (Act);
9188 end loop;
9190 when others =>
9191 raise Program_Error;
9192 end case;
9193 end Find_Matching_Actual;
9195 -------------------------
9196 -- Match_Formal_Entity --
9197 -------------------------
9199 procedure Match_Formal_Entity
9200 (Formal_Node : Node_Id;
9201 Formal_Ent : Entity_Id;
9202 Actual_Ent : Entity_Id)
9204 Act_Pkg : Entity_Id;
9206 begin
9207 Set_Instance_Of (Formal_Ent, Actual_Ent);
9209 if Ekind (Actual_Ent) = E_Package then
9211 -- Record associations for each parameter
9213 Act_Pkg := Actual_Ent;
9215 declare
9216 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9217 F_Ent : Entity_Id;
9218 F_Node : Node_Id;
9220 Gen_Decl : Node_Id;
9221 Formals : List_Id;
9222 Actual : Entity_Id;
9224 begin
9225 -- Retrieve the actual given in the formal package declaration
9227 Actual := Entity (Name (Original_Node (Formal_Node)));
9229 -- The actual in the formal package declaration may be a
9230 -- renamed generic package, in which case we want to retrieve
9231 -- the original generic in order to traverse its formal part.
9233 if Present (Renamed_Entity (Actual)) then
9234 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9235 else
9236 Gen_Decl := Unit_Declaration_Node (Actual);
9237 end if;
9239 Formals := Generic_Formal_Declarations (Gen_Decl);
9241 if Present (Formals) then
9242 F_Node := First_Non_Pragma (Formals);
9243 else
9244 F_Node := Empty;
9245 end if;
9247 while Present (A_Ent)
9248 and then Present (F_Node)
9249 and then A_Ent /= First_Private_Entity (Act_Pkg)
9250 loop
9251 F_Ent := Get_Formal_Entity (F_Node);
9253 if Present (F_Ent) then
9255 -- This is a formal of the original package. Record
9256 -- association and recurse.
9258 Find_Matching_Actual (F_Node, A_Ent);
9259 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9260 Next_Entity (A_Ent);
9261 end if;
9263 Next_Non_Pragma (F_Node);
9264 end loop;
9265 end;
9266 end if;
9267 end Match_Formal_Entity;
9269 -----------------------
9270 -- Get_Formal_Entity --
9271 -----------------------
9273 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9274 Kind : constant Node_Kind := Nkind (Original_Node (N));
9275 begin
9276 case Kind is
9277 when N_Formal_Object_Declaration =>
9278 return Defining_Identifier (N);
9280 when N_Formal_Type_Declaration =>
9281 return Defining_Identifier (N);
9283 when N_Formal_Subprogram_Declaration =>
9284 return Defining_Unit_Name (Specification (N));
9286 when N_Formal_Package_Declaration =>
9287 return Defining_Identifier (Original_Node (N));
9289 when N_Generic_Package_Declaration =>
9290 return Defining_Identifier (Original_Node (N));
9292 -- All other declarations are introduced by semantic analysis and
9293 -- have no match in the actual.
9295 when others =>
9296 return Empty;
9297 end case;
9298 end Get_Formal_Entity;
9300 --------------------
9301 -- Is_Instance_Of --
9302 --------------------
9304 function Is_Instance_Of
9305 (Act_Spec : Entity_Id;
9306 Gen_Anc : Entity_Id) return Boolean
9308 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9310 begin
9311 if No (Gen_Par) then
9312 return False;
9314 -- Simplest case: the generic parent of the actual is the formal
9316 elsif Gen_Par = Gen_Anc then
9317 return True;
9319 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9320 return False;
9322 -- The actual may be obtained through several instantiations. Its
9323 -- scope must itself be an instance of a generic declared in the
9324 -- same scope as the formal. Any other case is detected above.
9326 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9327 return False;
9329 else
9330 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9331 end if;
9332 end Is_Instance_Of;
9334 ---------------------------
9335 -- Process_Nested_Formal --
9336 ---------------------------
9338 procedure Process_Nested_Formal (Formal : Entity_Id) is
9339 Ent : Entity_Id;
9341 begin
9342 if Present (Associated_Formal_Package (Formal))
9343 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9344 then
9345 Ent := First_Entity (Formal);
9346 while Present (Ent) loop
9347 Set_Is_Hidden (Ent, False);
9348 Set_Is_Visible_Formal (Ent);
9349 Set_Is_Potentially_Use_Visible
9350 (Ent, Is_Potentially_Use_Visible (Formal));
9352 if Ekind (Ent) = E_Package then
9353 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9354 Process_Nested_Formal (Ent);
9355 end if;
9357 Next_Entity (Ent);
9358 end loop;
9359 end if;
9360 end Process_Nested_Formal;
9362 -- Start of processing for Instantiate_Formal_Package
9364 begin
9365 Analyze (Actual);
9367 if not Is_Entity_Name (Actual)
9368 or else Ekind (Entity (Actual)) /= E_Package
9369 then
9370 Error_Msg_N
9371 ("expect package instance to instantiate formal", Actual);
9372 Abandon_Instantiation (Actual);
9373 raise Program_Error;
9375 else
9376 Actual_Pack := Entity (Actual);
9377 Set_Is_Instantiated (Actual_Pack);
9379 -- The actual may be a renamed package, or an outer generic formal
9380 -- package whose instantiation is converted into a renaming.
9382 if Present (Renamed_Object (Actual_Pack)) then
9383 Actual_Pack := Renamed_Object (Actual_Pack);
9384 end if;
9386 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9387 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9388 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9389 else
9390 Gen_Parent :=
9391 Generic_Parent (Specification (Analyzed_Formal));
9392 Formal_Pack :=
9393 Defining_Unit_Name (Specification (Analyzed_Formal));
9394 end if;
9396 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9397 Parent_Spec := Package_Specification (Actual_Pack);
9398 else
9399 Parent_Spec := Parent (Actual_Pack);
9400 end if;
9402 if Gen_Parent = Any_Id then
9403 Error_Msg_N
9404 ("previous error in declaration of formal package", Actual);
9405 Abandon_Instantiation (Actual);
9407 elsif
9408 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9409 then
9410 null;
9412 else
9413 Error_Msg_NE
9414 ("actual parameter must be instance of&", Actual, Gen_Parent);
9415 Abandon_Instantiation (Actual);
9416 end if;
9418 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9419 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9421 Nod :=
9422 Make_Package_Renaming_Declaration (Loc,
9423 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9424 Name => New_Occurrence_Of (Actual_Pack, Loc));
9426 Set_Associated_Formal_Package
9427 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9428 Decls := New_List (Nod);
9430 -- If the formal F has a box, then the generic declarations are
9431 -- visible in the generic G. In an instance of G, the corresponding
9432 -- entities in the actual for F (which are the actuals for the
9433 -- instantiation of the generic that F denotes) must also be made
9434 -- visible for analysis of the current instance. On exit from the
9435 -- current instance, those entities are made private again. If the
9436 -- actual is currently in use, these entities are also use-visible.
9438 -- The loop through the actual entities also steps through the formal
9439 -- entities and enters associations from formals to actuals into the
9440 -- renaming map. This is necessary to properly handle checking of
9441 -- actual parameter associations for later formals that depend on
9442 -- actuals declared in the formal package.
9444 -- In Ada 2005, partial parameterization requires that we make
9445 -- visible the actuals corresponding to formals that were defaulted
9446 -- in the formal package. There formals are identified because they
9447 -- remain formal generics within the formal package, rather than
9448 -- being renamings of the actuals supplied.
9450 declare
9451 Gen_Decl : constant Node_Id :=
9452 Unit_Declaration_Node (Gen_Parent);
9453 Formals : constant List_Id :=
9454 Generic_Formal_Declarations (Gen_Decl);
9456 Actual_Ent : Entity_Id;
9457 Actual_Of_Formal : Node_Id;
9458 Formal_Node : Node_Id;
9459 Formal_Ent : Entity_Id;
9461 begin
9462 if Present (Formals) then
9463 Formal_Node := First_Non_Pragma (Formals);
9464 else
9465 Formal_Node := Empty;
9466 end if;
9468 Actual_Ent := First_Entity (Actual_Pack);
9469 Actual_Of_Formal :=
9470 First (Visible_Declarations (Specification (Analyzed_Formal)));
9471 while Present (Actual_Ent)
9472 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9473 loop
9474 if Present (Formal_Node) then
9475 Formal_Ent := Get_Formal_Entity (Formal_Node);
9477 if Present (Formal_Ent) then
9478 Find_Matching_Actual (Formal_Node, Actual_Ent);
9479 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9481 -- We iterate at the same time over the actuals of the
9482 -- local package created for the formal, to determine
9483 -- which one of the formals of the original generic were
9484 -- defaulted in the formal. The corresponding actual
9485 -- entities are visible in the enclosing instance.
9487 if Box_Present (Formal)
9488 or else
9489 (Present (Actual_Of_Formal)
9490 and then
9491 Is_Generic_Formal
9492 (Get_Formal_Entity (Actual_Of_Formal)))
9493 then
9494 Set_Is_Hidden (Actual_Ent, False);
9495 Set_Is_Visible_Formal (Actual_Ent);
9496 Set_Is_Potentially_Use_Visible
9497 (Actual_Ent, In_Use (Actual_Pack));
9499 if Ekind (Actual_Ent) = E_Package then
9500 Process_Nested_Formal (Actual_Ent);
9501 end if;
9503 else
9504 Set_Is_Hidden (Actual_Ent);
9505 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9506 end if;
9507 end if;
9509 Next_Non_Pragma (Formal_Node);
9510 Next (Actual_Of_Formal);
9512 else
9513 -- No further formals to match, but the generic part may
9514 -- contain inherited operation that are not hidden in the
9515 -- enclosing instance.
9517 Next_Entity (Actual_Ent);
9518 end if;
9519 end loop;
9521 -- Inherited subprograms generated by formal derived types are
9522 -- also visible if the types are.
9524 Actual_Ent := First_Entity (Actual_Pack);
9525 while Present (Actual_Ent)
9526 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9527 loop
9528 if Is_Overloadable (Actual_Ent)
9529 and then
9530 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9531 and then
9532 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9533 then
9534 Set_Is_Hidden (Actual_Ent, False);
9535 Set_Is_Potentially_Use_Visible
9536 (Actual_Ent, In_Use (Actual_Pack));
9537 end if;
9539 Next_Entity (Actual_Ent);
9540 end loop;
9541 end;
9543 -- If the formal is not declared with a box, reanalyze it as an
9544 -- abbreviated instantiation, to verify the matching rules of 12.7.
9545 -- The actual checks are performed after the generic associations
9546 -- have been analyzed, to guarantee the same visibility for this
9547 -- instantiation and for the actuals.
9549 -- In Ada 2005, the generic associations for the formal can include
9550 -- defaulted parameters. These are ignored during check. This
9551 -- internal instantiation is removed from the tree after conformance
9552 -- checking, because it contains formal declarations for those
9553 -- defaulted parameters, and those should not reach the back-end.
9555 if not Box_Present (Formal) then
9556 declare
9557 I_Pack : constant Entity_Id :=
9558 Make_Temporary (Sloc (Actual), 'P');
9560 begin
9561 Set_Is_Internal (I_Pack);
9563 Append_To (Decls,
9564 Make_Package_Instantiation (Sloc (Actual),
9565 Defining_Unit_Name => I_Pack,
9566 Name =>
9567 New_Occurrence_Of
9568 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9569 Generic_Associations => Generic_Associations (Formal)));
9570 end;
9571 end if;
9573 return Decls;
9574 end if;
9575 end Instantiate_Formal_Package;
9577 -----------------------------------
9578 -- Instantiate_Formal_Subprogram --
9579 -----------------------------------
9581 function Instantiate_Formal_Subprogram
9582 (Formal : Node_Id;
9583 Actual : Node_Id;
9584 Analyzed_Formal : Node_Id) return Node_Id
9586 Analyzed_S : constant Entity_Id :=
9587 Defining_Unit_Name (Specification (Analyzed_Formal));
9588 Formal_Sub : constant Entity_Id :=
9589 Defining_Unit_Name (Specification (Formal));
9591 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9592 -- If the generic is a child unit, the parent has been installed on the
9593 -- scope stack, but a default subprogram cannot resolve to something
9594 -- on the parent because that parent is not really part of the visible
9595 -- context (it is there to resolve explicit local entities). If the
9596 -- default has resolved in this way, we remove the entity from immediate
9597 -- visibility and analyze the node again to emit an error message or
9598 -- find another visible candidate.
9600 procedure Valid_Actual_Subprogram (Act : Node_Id);
9601 -- Perform legality check and raise exception on failure
9603 -----------------------
9604 -- From_Parent_Scope --
9605 -----------------------
9607 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9608 Gen_Scope : Node_Id;
9610 begin
9611 Gen_Scope := Scope (Analyzed_S);
9612 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9613 if Scope (Subp) = Scope (Gen_Scope) then
9614 return True;
9615 end if;
9617 Gen_Scope := Scope (Gen_Scope);
9618 end loop;
9620 return False;
9621 end From_Parent_Scope;
9623 -----------------------------
9624 -- Valid_Actual_Subprogram --
9625 -----------------------------
9627 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9628 Act_E : Entity_Id;
9630 begin
9631 if Is_Entity_Name (Act) then
9632 Act_E := Entity (Act);
9634 elsif Nkind (Act) = N_Selected_Component
9635 and then Is_Entity_Name (Selector_Name (Act))
9636 then
9637 Act_E := Entity (Selector_Name (Act));
9639 else
9640 Act_E := Empty;
9641 end if;
9643 if (Present (Act_E) and then Is_Overloadable (Act_E))
9644 or else Nkind_In (Act, N_Attribute_Reference,
9645 N_Indexed_Component,
9646 N_Character_Literal,
9647 N_Explicit_Dereference)
9648 then
9649 return;
9650 end if;
9652 Error_Msg_NE
9653 ("expect subprogram or entry name in instantiation of &",
9654 Instantiation_Node, Formal_Sub);
9655 Abandon_Instantiation (Instantiation_Node);
9656 end Valid_Actual_Subprogram;
9658 -- Local variables
9660 Decl_Node : Node_Id;
9661 Loc : Source_Ptr;
9662 Nam : Node_Id;
9663 New_Spec : Node_Id;
9664 New_Subp : Entity_Id;
9666 -- Start of processing for Instantiate_Formal_Subprogram
9668 begin
9669 New_Spec := New_Copy_Tree (Specification (Formal));
9671 -- The tree copy has created the proper instantiation sloc for the
9672 -- new specification. Use this location for all other constructed
9673 -- declarations.
9675 Loc := Sloc (Defining_Unit_Name (New_Spec));
9677 -- Create new entity for the actual (New_Copy_Tree does not), and
9678 -- indicate that it is an actual.
9680 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
9681 Set_Ekind (New_Subp, Ekind (Analyzed_S));
9682 Set_Is_Generic_Actual_Subprogram (New_Subp);
9683 Set_Defining_Unit_Name (New_Spec, New_Subp);
9685 -- Create new entities for the each of the formals in the specification
9686 -- of the renaming declaration built for the actual.
9688 if Present (Parameter_Specifications (New_Spec)) then
9689 declare
9690 F : Node_Id;
9691 F_Id : Entity_Id;
9693 begin
9694 F := First (Parameter_Specifications (New_Spec));
9695 while Present (F) loop
9696 F_Id := Defining_Identifier (F);
9698 Set_Defining_Identifier (F,
9699 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
9700 Next (F);
9701 end loop;
9702 end;
9703 end if;
9705 -- Find entity of actual. If the actual is an attribute reference, it
9706 -- cannot be resolved here (its formal is missing) but is handled
9707 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9708 -- fully resolved subsequently, when the renaming declaration for the
9709 -- formal is analyzed. If it is an explicit dereference, resolve the
9710 -- prefix but not the actual itself, to prevent interpretation as call.
9712 if Present (Actual) then
9713 Loc := Sloc (Actual);
9714 Set_Sloc (New_Spec, Loc);
9716 if Nkind (Actual) = N_Operator_Symbol then
9717 Find_Direct_Name (Actual);
9719 elsif Nkind (Actual) = N_Explicit_Dereference then
9720 Analyze (Prefix (Actual));
9722 elsif Nkind (Actual) /= N_Attribute_Reference then
9723 Analyze (Actual);
9724 end if;
9726 Valid_Actual_Subprogram (Actual);
9727 Nam := Actual;
9729 elsif Present (Default_Name (Formal)) then
9730 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9731 N_Selected_Component,
9732 N_Indexed_Component,
9733 N_Character_Literal)
9734 and then Present (Entity (Default_Name (Formal)))
9735 then
9736 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9737 else
9738 Nam := New_Copy (Default_Name (Formal));
9739 Set_Sloc (Nam, Loc);
9740 end if;
9742 elsif Box_Present (Formal) then
9744 -- Actual is resolved at the point of instantiation. Create an
9745 -- identifier or operator with the same name as the formal.
9747 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
9748 Nam :=
9749 Make_Operator_Symbol (Loc,
9750 Chars => Chars (Formal_Sub),
9751 Strval => No_String);
9752 else
9753 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
9754 end if;
9756 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
9757 and then Null_Present (Specification (Formal))
9758 then
9759 -- Generate null body for procedure, for use in the instance
9761 Decl_Node :=
9762 Make_Subprogram_Body (Loc,
9763 Specification => New_Spec,
9764 Declarations => New_List,
9765 Handled_Statement_Sequence =>
9766 Make_Handled_Sequence_Of_Statements (Loc,
9767 Statements => New_List (Make_Null_Statement (Loc))));
9769 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
9770 return Decl_Node;
9772 else
9773 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
9774 Error_Msg_NE
9775 ("missing actual&", Instantiation_Node, Formal_Sub);
9776 Error_Msg_NE
9777 ("\in instantiation of & declared#",
9778 Instantiation_Node, Scope (Analyzed_S));
9779 Abandon_Instantiation (Instantiation_Node);
9780 end if;
9782 Decl_Node :=
9783 Make_Subprogram_Renaming_Declaration (Loc,
9784 Specification => New_Spec,
9785 Name => Nam);
9787 -- If we do not have an actual and the formal specified <> then set to
9788 -- get proper default.
9790 if No (Actual) and then Box_Present (Formal) then
9791 Set_From_Default (Decl_Node);
9792 end if;
9794 -- Gather possible interpretations for the actual before analyzing the
9795 -- instance. If overloaded, it will be resolved when analyzing the
9796 -- renaming declaration.
9798 if Box_Present (Formal) and then No (Actual) then
9799 Analyze (Nam);
9801 if Is_Child_Unit (Scope (Analyzed_S))
9802 and then Present (Entity (Nam))
9803 then
9804 if not Is_Overloaded (Nam) then
9805 if From_Parent_Scope (Entity (Nam)) then
9806 Set_Is_Immediately_Visible (Entity (Nam), False);
9807 Set_Entity (Nam, Empty);
9808 Set_Etype (Nam, Empty);
9810 Analyze (Nam);
9811 Set_Is_Immediately_Visible (Entity (Nam));
9812 end if;
9814 else
9815 declare
9816 I : Interp_Index;
9817 It : Interp;
9819 begin
9820 Get_First_Interp (Nam, I, It);
9821 while Present (It.Nam) loop
9822 if From_Parent_Scope (It.Nam) then
9823 Remove_Interp (I);
9824 end if;
9826 Get_Next_Interp (I, It);
9827 end loop;
9828 end;
9829 end if;
9830 end if;
9831 end if;
9833 -- The generic instantiation freezes the actual. This can only be done
9834 -- once the actual is resolved, in the analysis of the renaming
9835 -- declaration. To make the formal subprogram entity available, we set
9836 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9837 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9838 -- of formal abstract subprograms.
9840 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9842 -- We cannot analyze the renaming declaration, and thus find the actual,
9843 -- until all the actuals are assembled in the instance. For subsequent
9844 -- checks of other actuals, indicate the node that will hold the
9845 -- instance of this formal.
9847 Set_Instance_Of (Analyzed_S, Nam);
9849 if Nkind (Actual) = N_Selected_Component
9850 and then Is_Task_Type (Etype (Prefix (Actual)))
9851 and then not Is_Frozen (Etype (Prefix (Actual)))
9852 then
9853 -- The renaming declaration will create a body, which must appear
9854 -- outside of the instantiation, We move the renaming declaration
9855 -- out of the instance, and create an additional renaming inside,
9856 -- to prevent freezing anomalies.
9858 declare
9859 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9861 begin
9862 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9863 Insert_Before (Instantiation_Node, Decl_Node);
9864 Analyze (Decl_Node);
9866 -- Now create renaming within the instance
9868 Decl_Node :=
9869 Make_Subprogram_Renaming_Declaration (Loc,
9870 Specification => New_Copy_Tree (New_Spec),
9871 Name => New_Occurrence_Of (Anon_Id, Loc));
9873 Set_Defining_Unit_Name (Specification (Decl_Node),
9874 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9875 end;
9876 end if;
9878 return Decl_Node;
9879 end Instantiate_Formal_Subprogram;
9881 ------------------------
9882 -- Instantiate_Object --
9883 ------------------------
9885 function Instantiate_Object
9886 (Formal : Node_Id;
9887 Actual : Node_Id;
9888 Analyzed_Formal : Node_Id) return List_Id
9890 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9891 A_Gen_Obj : constant Entity_Id :=
9892 Defining_Identifier (Analyzed_Formal);
9893 Acc_Def : Node_Id := Empty;
9894 Act_Assoc : constant Node_Id := Parent (Actual);
9895 Actual_Decl : Node_Id := Empty;
9896 Decl_Node : Node_Id;
9897 Def : Node_Id;
9898 Ftyp : Entity_Id;
9899 List : constant List_Id := New_List;
9900 Loc : constant Source_Ptr := Sloc (Actual);
9901 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9902 Subt_Decl : Node_Id := Empty;
9903 Subt_Mark : Node_Id := Empty;
9905 function Copy_Access_Def return Node_Id;
9906 -- If formal is an anonymous access, copy access definition of formal
9907 -- for generated object declaration.
9909 ---------------------
9910 -- Copy_Access_Def --
9911 ---------------------
9913 function Copy_Access_Def return Node_Id is
9914 begin
9915 Def := New_Copy_Tree (Acc_Def);
9917 -- In addition, if formal is an access to subprogram we need to
9918 -- generate new formals for the signature of the default, so that
9919 -- the tree is properly formatted for ASIS use.
9921 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
9922 declare
9923 Par_Spec : Node_Id;
9924 begin
9925 Par_Spec :=
9926 First (Parameter_Specifications
9927 (Access_To_Subprogram_Definition (Def)));
9928 while Present (Par_Spec) loop
9929 Set_Defining_Identifier (Par_Spec,
9930 Make_Defining_Identifier (Sloc (Acc_Def),
9931 Chars => Chars (Defining_Identifier (Par_Spec))));
9932 Next (Par_Spec);
9933 end loop;
9934 end;
9935 end if;
9937 return Def;
9938 end Copy_Access_Def;
9940 -- Start of processing for Instantiate_Object
9942 begin
9943 -- Formal may be an anonymous access
9945 if Present (Subtype_Mark (Formal)) then
9946 Subt_Mark := Subtype_Mark (Formal);
9947 else
9948 Check_Access_Definition (Formal);
9949 Acc_Def := Access_Definition (Formal);
9950 end if;
9952 -- Sloc for error message on missing actual
9954 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9956 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9957 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9958 end if;
9960 Set_Parent (List, Parent (Actual));
9962 -- OUT present
9964 if Out_Present (Formal) then
9966 -- An IN OUT generic actual must be a name. The instantiation is a
9967 -- renaming declaration. The actual is the name being renamed. We
9968 -- use the actual directly, rather than a copy, because it is not
9969 -- used further in the list of actuals, and because a copy or a use
9970 -- of relocate_node is incorrect if the instance is nested within a
9971 -- generic. In order to simplify ASIS searches, the Generic_Parent
9972 -- field links the declaration to the generic association.
9974 if No (Actual) then
9975 Error_Msg_NE
9976 ("missing actual &",
9977 Instantiation_Node, Gen_Obj);
9978 Error_Msg_NE
9979 ("\in instantiation of & declared#",
9980 Instantiation_Node, Scope (A_Gen_Obj));
9981 Abandon_Instantiation (Instantiation_Node);
9982 end if;
9984 if Present (Subt_Mark) then
9985 Decl_Node :=
9986 Make_Object_Renaming_Declaration (Loc,
9987 Defining_Identifier => New_Copy (Gen_Obj),
9988 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9989 Name => Actual);
9991 else pragma Assert (Present (Acc_Def));
9992 Decl_Node :=
9993 Make_Object_Renaming_Declaration (Loc,
9994 Defining_Identifier => New_Copy (Gen_Obj),
9995 Access_Definition => New_Copy_Tree (Acc_Def),
9996 Name => Actual);
9997 end if;
9999 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10001 -- The analysis of the actual may produce Insert_Action nodes, so
10002 -- the declaration must have a context in which to attach them.
10004 Append (Decl_Node, List);
10005 Analyze (Actual);
10007 -- Return if the analysis of the actual reported some error
10009 if Etype (Actual) = Any_Type then
10010 return List;
10011 end if;
10013 -- This check is performed here because Analyze_Object_Renaming will
10014 -- not check it when Comes_From_Source is False. Note though that the
10015 -- check for the actual being the name of an object will be performed
10016 -- in Analyze_Object_Renaming.
10018 if Is_Object_Reference (Actual)
10019 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10020 then
10021 Error_Msg_N
10022 ("illegal discriminant-dependent component for in out parameter",
10023 Actual);
10024 end if;
10026 -- The actual has to be resolved in order to check that it is a
10027 -- variable (due to cases such as F (1), where F returns access to
10028 -- an array, and for overloaded prefixes).
10030 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10032 -- If the type of the formal is not itself a formal, and the current
10033 -- unit is a child unit, the formal type must be declared in a
10034 -- parent, and must be retrieved by visibility.
10036 if Ftyp = Orig_Ftyp
10037 and then Is_Generic_Unit (Scope (Ftyp))
10038 and then Is_Child_Unit (Scope (A_Gen_Obj))
10039 then
10040 declare
10041 Temp : constant Node_Id :=
10042 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10043 begin
10044 Set_Entity (Temp, Empty);
10045 Find_Type (Temp);
10046 Ftyp := Entity (Temp);
10047 end;
10048 end if;
10050 if Is_Private_Type (Ftyp)
10051 and then not Is_Private_Type (Etype (Actual))
10052 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10053 or else Base_Type (Etype (Actual)) = Ftyp)
10054 then
10055 -- If the actual has the type of the full view of the formal, or
10056 -- else a non-private subtype of the formal, then the visibility
10057 -- of the formal type has changed. Add to the actuals a subtype
10058 -- declaration that will force the exchange of views in the body
10059 -- of the instance as well.
10061 Subt_Decl :=
10062 Make_Subtype_Declaration (Loc,
10063 Defining_Identifier => Make_Temporary (Loc, 'P'),
10064 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10066 Prepend (Subt_Decl, List);
10068 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10069 Exchange_Declarations (Ftyp);
10070 end if;
10072 Resolve (Actual, Ftyp);
10074 if not Denotes_Variable (Actual) then
10075 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10077 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10079 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10080 -- the type of the actual shall resolve to a specific anonymous
10081 -- access type.
10083 if Ada_Version < Ada_2005
10084 or else Ekind (Base_Type (Ftyp)) /=
10085 E_Anonymous_Access_Type
10086 or else Ekind (Base_Type (Etype (Actual))) /=
10087 E_Anonymous_Access_Type
10088 then
10089 Error_Msg_NE
10090 ("type of actual does not match type of&", Actual, Gen_Obj);
10091 end if;
10092 end if;
10094 Note_Possible_Modification (Actual, Sure => True);
10096 -- Check for instantiation of atomic/volatile actual for
10097 -- non-atomic/volatile formal (RM C.6 (12)).
10099 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10100 Error_Msg_N
10101 ("cannot instantiate non-atomic formal object "
10102 & "with atomic actual", Actual);
10104 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10105 then
10106 Error_Msg_N
10107 ("cannot instantiate non-volatile formal object "
10108 & "with volatile actual", Actual);
10109 end if;
10111 -- Formal in-parameter
10113 else
10114 -- The instantiation of a generic formal in-parameter is constant
10115 -- declaration. The actual is the expression for that declaration.
10116 -- Its type is a full copy of the type of the formal. This may be
10117 -- an access to subprogram, for which we need to generate entities
10118 -- for the formals in the new signature.
10120 if Present (Actual) then
10121 if Present (Subt_Mark) then
10122 Def := New_Copy_Tree (Subt_Mark);
10123 else pragma Assert (Present (Acc_Def));
10124 Def := Copy_Access_Def;
10125 end if;
10127 Decl_Node :=
10128 Make_Object_Declaration (Loc,
10129 Defining_Identifier => New_Copy (Gen_Obj),
10130 Constant_Present => True,
10131 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10132 Object_Definition => Def,
10133 Expression => Actual);
10135 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10137 -- A generic formal object of a tagged type is defined to be
10138 -- aliased so the new constant must also be treated as aliased.
10140 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10141 Set_Aliased_Present (Decl_Node);
10142 end if;
10144 Append (Decl_Node, List);
10146 -- No need to repeat (pre-)analysis of some expression nodes
10147 -- already handled in Preanalyze_Actuals.
10149 if Nkind (Actual) /= N_Allocator then
10150 Analyze (Actual);
10152 -- Return if the analysis of the actual reported some error
10154 if Etype (Actual) = Any_Type then
10155 return List;
10156 end if;
10157 end if;
10159 declare
10160 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10161 Typ : Entity_Id;
10163 begin
10164 Typ := Get_Instance_Of (Formal_Type);
10166 -- If the actual appears in the current or an enclosing scope,
10167 -- use its type directly. This is relevant if it has an actual
10168 -- subtype that is distinct from its nominal one. This cannot
10169 -- be done in general because the type of the actual may
10170 -- depend on other actuals, and only be fully determined when
10171 -- the enclosing instance is analyzed.
10173 if Present (Etype (Actual))
10174 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10175 then
10176 Freeze_Before (Instantiation_Node, Etype (Actual));
10177 else
10178 Freeze_Before (Instantiation_Node, Typ);
10179 end if;
10181 -- If the actual is an aggregate, perform name resolution on
10182 -- its components (the analysis of an aggregate does not do it)
10183 -- to capture local names that may be hidden if the generic is
10184 -- a child unit.
10186 if Nkind (Actual) = N_Aggregate then
10187 Preanalyze_And_Resolve (Actual, Typ);
10188 end if;
10190 if Is_Limited_Type (Typ)
10191 and then not OK_For_Limited_Init (Typ, Actual)
10192 then
10193 Error_Msg_N
10194 ("initialization not allowed for limited types", Actual);
10195 Explain_Limited_Type (Typ, Actual);
10196 end if;
10197 end;
10199 elsif Present (Default_Expression (Formal)) then
10201 -- Use default to construct declaration
10203 if Present (Subt_Mark) then
10204 Def := New_Copy (Subt_Mark);
10205 else pragma Assert (Present (Acc_Def));
10206 Def := Copy_Access_Def;
10207 end if;
10209 Decl_Node :=
10210 Make_Object_Declaration (Sloc (Formal),
10211 Defining_Identifier => New_Copy (Gen_Obj),
10212 Constant_Present => True,
10213 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10214 Object_Definition => Def,
10215 Expression => New_Copy_Tree
10216 (Default_Expression (Formal)));
10218 Append (Decl_Node, List);
10219 Set_Analyzed (Expression (Decl_Node), False);
10221 else
10222 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10223 Error_Msg_NE ("\in instantiation of & declared#",
10224 Instantiation_Node, Scope (A_Gen_Obj));
10226 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10228 -- Create dummy constant declaration so that instance can be
10229 -- analyzed, to minimize cascaded visibility errors.
10231 if Present (Subt_Mark) then
10232 Def := Subt_Mark;
10233 else pragma Assert (Present (Acc_Def));
10234 Def := Acc_Def;
10235 end if;
10237 Decl_Node :=
10238 Make_Object_Declaration (Loc,
10239 Defining_Identifier => New_Copy (Gen_Obj),
10240 Constant_Present => True,
10241 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10242 Object_Definition => New_Copy (Def),
10243 Expression =>
10244 Make_Attribute_Reference (Sloc (Gen_Obj),
10245 Attribute_Name => Name_First,
10246 Prefix => New_Copy (Def)));
10248 Append (Decl_Node, List);
10250 else
10251 Abandon_Instantiation (Instantiation_Node);
10252 end if;
10253 end if;
10254 end if;
10256 if Nkind (Actual) in N_Has_Entity then
10257 Actual_Decl := Parent (Entity (Actual));
10258 end if;
10260 -- Ada 2005 (AI-423): For a formal object declaration with a null
10261 -- exclusion or an access definition that has a null exclusion: If the
10262 -- actual matching the formal object declaration denotes a generic
10263 -- formal object of another generic unit G, and the instantiation
10264 -- containing the actual occurs within the body of G or within the body
10265 -- of a generic unit declared within the declarative region of G, then
10266 -- the declaration of the formal object of G must have a null exclusion.
10267 -- Otherwise, the subtype of the actual matching the formal object
10268 -- declaration shall exclude null.
10270 if Ada_Version >= Ada_2005
10271 and then Present (Actual_Decl)
10272 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10273 N_Object_Declaration)
10274 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10275 and then not Has_Null_Exclusion (Actual_Decl)
10276 and then Has_Null_Exclusion (Analyzed_Formal)
10277 then
10278 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10279 Error_Msg_N
10280 ("actual must exclude null to match generic formal#", Actual);
10281 end if;
10283 -- An effectively volatile object cannot be used as an actual in
10284 -- a generic instance. The following check is only relevant when
10285 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10287 if SPARK_Mode = On
10288 and then Present (Actual)
10289 and then Is_Effectively_Volatile_Object (Actual)
10290 then
10291 Error_Msg_N
10292 ("volatile object cannot act as actual in generic instantiation "
10293 & "(SPARK RM 7.1.3(8))", Actual);
10294 end if;
10296 return List;
10297 end Instantiate_Object;
10299 ------------------------------
10300 -- Instantiate_Package_Body --
10301 ------------------------------
10303 procedure Instantiate_Package_Body
10304 (Body_Info : Pending_Body_Info;
10305 Inlined_Body : Boolean := False;
10306 Body_Optional : Boolean := False)
10308 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10309 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10310 Loc : constant Source_Ptr := Sloc (Inst_Node);
10312 Gen_Id : constant Node_Id := Name (Inst_Node);
10313 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10314 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10315 Act_Spec : constant Node_Id := Specification (Act_Decl);
10316 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10318 Act_Body_Name : Node_Id;
10319 Gen_Body : Node_Id;
10320 Gen_Body_Id : Node_Id;
10321 Act_Body : Node_Id;
10322 Act_Body_Id : Entity_Id;
10324 Parent_Installed : Boolean := False;
10325 Save_Style_Check : constant Boolean := Style_Check;
10327 Par_Ent : Entity_Id := Empty;
10328 Par_Vis : Boolean := False;
10330 Vis_Prims_List : Elist_Id := No_Elist;
10331 -- List of primitives made temporarily visible in the instantiation
10332 -- to match the visibility of the formal type
10334 procedure Check_Initialized_Types;
10335 -- In a generic package body, an entity of a generic private type may
10336 -- appear uninitialized. This is suspicious, unless the actual is a
10337 -- fully initialized type.
10339 -----------------------------
10340 -- Check_Initialized_Types --
10341 -----------------------------
10343 procedure Check_Initialized_Types is
10344 Decl : Node_Id;
10345 Formal : Entity_Id;
10346 Actual : Entity_Id;
10347 Uninit_Var : Entity_Id;
10349 begin
10350 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10351 while Present (Decl) loop
10352 Uninit_Var := Empty;
10354 if Nkind (Decl) = N_Private_Extension_Declaration then
10355 Uninit_Var := Uninitialized_Variable (Decl);
10357 elsif Nkind (Decl) = N_Formal_Type_Declaration
10358 and then Nkind (Formal_Type_Definition (Decl)) =
10359 N_Formal_Private_Type_Definition
10360 then
10361 Uninit_Var :=
10362 Uninitialized_Variable (Formal_Type_Definition (Decl));
10363 end if;
10365 if Present (Uninit_Var) then
10366 Formal := Defining_Identifier (Decl);
10367 Actual := First_Entity (Act_Decl_Id);
10369 -- For each formal there is a subtype declaration that renames
10370 -- the actual and has the same name as the formal. Locate the
10371 -- formal for warning message about uninitialized variables
10372 -- in the generic, for which the actual type should be a fully
10373 -- initialized type.
10375 while Present (Actual) loop
10376 exit when Ekind (Actual) = E_Package
10377 and then Present (Renamed_Object (Actual));
10379 if Chars (Actual) = Chars (Formal)
10380 and then not Is_Scalar_Type (Actual)
10381 and then not Is_Fully_Initialized_Type (Actual)
10382 and then Warn_On_No_Value_Assigned
10383 then
10384 Error_Msg_Node_2 := Formal;
10385 Error_Msg_NE
10386 ("generic unit has uninitialized variable& of "
10387 & "formal private type &?v?", Actual, Uninit_Var);
10388 Error_Msg_NE
10389 ("actual type for& should be fully initialized type?v?",
10390 Actual, Formal);
10391 exit;
10392 end if;
10394 Next_Entity (Actual);
10395 end loop;
10396 end if;
10398 Next (Decl);
10399 end loop;
10400 end Check_Initialized_Types;
10402 -- Start of processing for Instantiate_Package_Body
10404 begin
10405 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10407 -- The instance body may already have been processed, as the parent of
10408 -- another instance that is inlined (Load_Parent_Of_Generic).
10410 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10411 return;
10412 end if;
10414 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10416 -- Re-establish the state of information on which checks are suppressed.
10417 -- This information was set in Body_Info at the point of instantiation,
10418 -- and now we restore it so that the instance is compiled using the
10419 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10421 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10422 Scope_Suppress := Body_Info.Scope_Suppress;
10423 Opt.Ada_Version := Body_Info.Version;
10424 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10425 Restore_Warnings (Body_Info.Warnings);
10426 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10427 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10429 if No (Gen_Body_Id) then
10431 -- Do not look for parent of generic body if none is required.
10432 -- This may happen when the routine is called as part of the
10433 -- Pending_Instantiations processing, when nested instances
10434 -- may precede the one generated from the main unit.
10436 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10437 and then Body_Optional
10438 then
10439 return;
10440 else
10441 Load_Parent_Of_Generic
10442 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10443 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10444 end if;
10445 end if;
10447 -- Establish global variable for sloc adjustment and for error recovery
10449 Instantiation_Node := Inst_Node;
10451 if Present (Gen_Body_Id) then
10452 Save_Env (Gen_Unit, Act_Decl_Id);
10453 Style_Check := False;
10454 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10456 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10458 Create_Instantiation_Source
10459 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10461 Act_Body :=
10462 Copy_Generic_Node
10463 (Original_Node (Gen_Body), Empty, Instantiating => True);
10465 -- Build new name (possibly qualified) for body declaration
10467 Act_Body_Id := New_Copy (Act_Decl_Id);
10469 -- Some attributes of spec entity are not inherited by body entity
10471 Set_Handler_Records (Act_Body_Id, No_List);
10473 if Nkind (Defining_Unit_Name (Act_Spec)) =
10474 N_Defining_Program_Unit_Name
10475 then
10476 Act_Body_Name :=
10477 Make_Defining_Program_Unit_Name (Loc,
10478 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10479 Defining_Identifier => Act_Body_Id);
10480 else
10481 Act_Body_Name := Act_Body_Id;
10482 end if;
10484 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10486 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10487 Check_Generic_Actuals (Act_Decl_Id, False);
10488 Check_Initialized_Types;
10490 -- Install primitives hidden at the point of the instantiation but
10491 -- visible when processing the generic formals
10493 declare
10494 E : Entity_Id;
10496 begin
10497 E := First_Entity (Act_Decl_Id);
10498 while Present (E) loop
10499 if Is_Type (E)
10500 and then Is_Generic_Actual_Type (E)
10501 and then Is_Tagged_Type (E)
10502 then
10503 Install_Hidden_Primitives
10504 (Prims_List => Vis_Prims_List,
10505 Gen_T => Generic_Parent_Type (Parent (E)),
10506 Act_T => E);
10507 end if;
10509 Next_Entity (E);
10510 end loop;
10511 end;
10513 -- If it is a child unit, make the parent instance (which is an
10514 -- instance of the parent of the generic) visible. The parent
10515 -- instance is the prefix of the name of the generic unit.
10517 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10518 and then Nkind (Gen_Id) = N_Expanded_Name
10519 then
10520 Par_Ent := Entity (Prefix (Gen_Id));
10521 Par_Vis := Is_Immediately_Visible (Par_Ent);
10522 Install_Parent (Par_Ent, In_Body => True);
10523 Parent_Installed := True;
10525 elsif Is_Child_Unit (Gen_Unit) then
10526 Par_Ent := Scope (Gen_Unit);
10527 Par_Vis := Is_Immediately_Visible (Par_Ent);
10528 Install_Parent (Par_Ent, In_Body => True);
10529 Parent_Installed := True;
10530 end if;
10532 -- If the instantiation is a library unit, and this is the main unit,
10533 -- then build the resulting compilation unit nodes for the instance.
10534 -- If this is a compilation unit but it is not the main unit, then it
10535 -- is the body of a unit in the context, that is being compiled
10536 -- because it is encloses some inlined unit or another generic unit
10537 -- being instantiated. In that case, this body is not part of the
10538 -- current compilation, and is not attached to the tree, but its
10539 -- parent must be set for analysis.
10541 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10543 -- Replace instance node with body of instance, and create new
10544 -- node for corresponding instance declaration.
10546 Build_Instance_Compilation_Unit_Nodes
10547 (Inst_Node, Act_Body, Act_Decl);
10548 Analyze (Inst_Node);
10550 if Parent (Inst_Node) = Cunit (Main_Unit) then
10552 -- If the instance is a child unit itself, then set the scope
10553 -- of the expanded body to be the parent of the instantiation
10554 -- (ensuring that the fully qualified name will be generated
10555 -- for the elaboration subprogram).
10557 if Nkind (Defining_Unit_Name (Act_Spec)) =
10558 N_Defining_Program_Unit_Name
10559 then
10560 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10561 end if;
10562 end if;
10564 -- Case where instantiation is not a library unit
10566 else
10567 -- If this is an early instantiation, i.e. appears textually
10568 -- before the corresponding body and must be elaborated first,
10569 -- indicate that the body instance is to be delayed.
10571 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10573 -- Now analyze the body. We turn off all checks if this is an
10574 -- internal unit, since there is no reason to have checks on for
10575 -- any predefined run-time library code. All such code is designed
10576 -- to be compiled with checks off.
10578 -- Note that we do NOT apply this criterion to children of GNAT
10579 -- The latter units must suppress checks explicitly if needed.
10581 if Is_Predefined_File_Name
10582 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10583 then
10584 Analyze (Act_Body, Suppress => All_Checks);
10585 else
10586 Analyze (Act_Body);
10587 end if;
10588 end if;
10590 Inherit_Context (Gen_Body, Inst_Node);
10592 -- Remove the parent instances if they have been placed on the scope
10593 -- stack to compile the body.
10595 if Parent_Installed then
10596 Remove_Parent (In_Body => True);
10598 -- Restore the previous visibility of the parent
10600 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10601 end if;
10603 Restore_Hidden_Primitives (Vis_Prims_List);
10604 Restore_Private_Views (Act_Decl_Id);
10606 -- Remove the current unit from visibility if this is an instance
10607 -- that is not elaborated on the fly for inlining purposes.
10609 if not Inlined_Body then
10610 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10611 end if;
10613 Restore_Env;
10614 Style_Check := Save_Style_Check;
10616 -- If we have no body, and the unit requires a body, then complain. This
10617 -- complaint is suppressed if we have detected other errors (since a
10618 -- common reason for missing the body is that it had errors).
10619 -- In CodePeer mode, a warning has been emitted already, no need for
10620 -- further messages.
10622 elsif Unit_Requires_Body (Gen_Unit)
10623 and then not Body_Optional
10624 then
10625 if CodePeer_Mode then
10626 null;
10628 elsif Serious_Errors_Detected = 0 then
10629 Error_Msg_NE
10630 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10632 -- Don't attempt to perform any cleanup actions if some other error
10633 -- was already detected, since this can cause blowups.
10635 else
10636 return;
10637 end if;
10639 -- Case of package that does not need a body
10641 else
10642 -- If the instantiation of the declaration is a library unit, rewrite
10643 -- the original package instantiation as a package declaration in the
10644 -- compilation unit node.
10646 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10647 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10648 Rewrite (Inst_Node, Act_Decl);
10650 -- Generate elaboration entity, in case spec has elaboration code.
10651 -- This cannot be done when the instance is analyzed, because it
10652 -- is not known yet whether the body exists.
10654 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10655 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10657 -- If the instantiation is not a library unit, then append the
10658 -- declaration to the list of implicitly generated entities, unless
10659 -- it is already a list member which means that it was already
10660 -- processed
10662 elsif not Is_List_Member (Act_Decl) then
10663 Mark_Rewrite_Insertion (Act_Decl);
10664 Insert_Before (Inst_Node, Act_Decl);
10665 end if;
10666 end if;
10668 Expander_Mode_Restore;
10669 end Instantiate_Package_Body;
10671 ---------------------------------
10672 -- Instantiate_Subprogram_Body --
10673 ---------------------------------
10675 procedure Instantiate_Subprogram_Body
10676 (Body_Info : Pending_Body_Info;
10677 Body_Optional : Boolean := False)
10679 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10680 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10681 Loc : constant Source_Ptr := Sloc (Inst_Node);
10682 Gen_Id : constant Node_Id := Name (Inst_Node);
10683 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10684 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10685 Anon_Id : constant Entity_Id :=
10686 Defining_Unit_Name (Specification (Act_Decl));
10687 Pack_Id : constant Entity_Id :=
10688 Defining_Unit_Name (Parent (Act_Decl));
10689 Decls : List_Id;
10690 Gen_Body : Node_Id;
10691 Gen_Body_Id : Node_Id;
10692 Act_Body : Node_Id;
10693 Pack_Body : Node_Id;
10694 Prev_Formal : Entity_Id;
10695 Ret_Expr : Node_Id;
10696 Unit_Renaming : Node_Id;
10698 Parent_Installed : Boolean := False;
10700 Saved_Style_Check : constant Boolean := Style_Check;
10701 Saved_Warnings : constant Warning_Record := Save_Warnings;
10703 Par_Ent : Entity_Id := Empty;
10704 Par_Vis : Boolean := False;
10706 begin
10707 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10709 -- Subprogram body may have been created already because of an inline
10710 -- pragma, or because of multiple elaborations of the enclosing package
10711 -- when several instances of the subprogram appear in the main unit.
10713 if Present (Corresponding_Body (Act_Decl)) then
10714 return;
10715 end if;
10717 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10719 -- Re-establish the state of information on which checks are suppressed.
10720 -- This information was set in Body_Info at the point of instantiation,
10721 -- and now we restore it so that the instance is compiled using the
10722 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10724 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10725 Scope_Suppress := Body_Info.Scope_Suppress;
10726 Opt.Ada_Version := Body_Info.Version;
10727 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10728 Restore_Warnings (Body_Info.Warnings);
10729 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10730 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10732 if No (Gen_Body_Id) then
10734 -- For imported generic subprogram, no body to compile, complete
10735 -- the spec entity appropriately.
10737 if Is_Imported (Gen_Unit) then
10738 Set_Is_Imported (Anon_Id);
10739 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10740 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
10741 Set_Convention (Anon_Id, Convention (Gen_Unit));
10742 Set_Has_Completion (Anon_Id);
10743 return;
10745 -- For other cases, compile the body
10747 else
10748 Load_Parent_Of_Generic
10749 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10750 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10751 end if;
10752 end if;
10754 Instantiation_Node := Inst_Node;
10756 if Present (Gen_Body_Id) then
10757 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10759 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
10761 -- Either body is not present, or context is non-expanding, as
10762 -- when compiling a subunit. Mark the instance as completed, and
10763 -- diagnose a missing body when needed.
10765 if Expander_Active
10766 and then Operating_Mode = Generate_Code
10767 then
10768 Error_Msg_N
10769 ("missing proper body for instantiation", Gen_Body);
10770 end if;
10772 Set_Has_Completion (Anon_Id);
10773 return;
10774 end if;
10776 Save_Env (Gen_Unit, Anon_Id);
10777 Style_Check := False;
10778 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10779 Create_Instantiation_Source
10780 (Inst_Node,
10781 Gen_Body_Id,
10782 False,
10783 S_Adjustment);
10785 Act_Body :=
10786 Copy_Generic_Node
10787 (Original_Node (Gen_Body), Empty, Instantiating => True);
10789 -- Create proper defining name for the body, to correspond to
10790 -- the one in the spec.
10792 Set_Defining_Unit_Name (Specification (Act_Body),
10793 Make_Defining_Identifier
10794 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
10795 Set_Corresponding_Spec (Act_Body, Anon_Id);
10796 Set_Has_Completion (Anon_Id);
10797 Check_Generic_Actuals (Pack_Id, False);
10799 -- Generate a reference to link the visible subprogram instance to
10800 -- the generic body, which for navigation purposes is the only
10801 -- available source for the instance.
10803 Generate_Reference
10804 (Related_Instance (Pack_Id),
10805 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
10807 -- If it is a child unit, make the parent instance (which is an
10808 -- instance of the parent of the generic) visible. The parent
10809 -- instance is the prefix of the name of the generic unit.
10811 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10812 and then Nkind (Gen_Id) = N_Expanded_Name
10813 then
10814 Par_Ent := Entity (Prefix (Gen_Id));
10815 Par_Vis := Is_Immediately_Visible (Par_Ent);
10816 Install_Parent (Par_Ent, In_Body => True);
10817 Parent_Installed := True;
10819 elsif Is_Child_Unit (Gen_Unit) then
10820 Par_Ent := Scope (Gen_Unit);
10821 Par_Vis := Is_Immediately_Visible (Par_Ent);
10822 Install_Parent (Par_Ent, In_Body => True);
10823 Parent_Installed := True;
10824 end if;
10826 -- Inside its body, a reference to the generic unit is a reference
10827 -- to the instance. The corresponding renaming is the first
10828 -- declaration in the body.
10830 Unit_Renaming :=
10831 Make_Subprogram_Renaming_Declaration (Loc,
10832 Specification =>
10833 Copy_Generic_Node (
10834 Specification (Original_Node (Gen_Body)),
10835 Empty,
10836 Instantiating => True),
10837 Name => New_Occurrence_Of (Anon_Id, Loc));
10839 -- If there is a formal subprogram with the same name as the unit
10840 -- itself, do not add this renaming declaration. This is a temporary
10841 -- fix for one ACATS test. ???
10843 Prev_Formal := First_Entity (Pack_Id);
10844 while Present (Prev_Formal) loop
10845 if Chars (Prev_Formal) = Chars (Gen_Unit)
10846 and then Is_Overloadable (Prev_Formal)
10847 then
10848 exit;
10849 end if;
10851 Next_Entity (Prev_Formal);
10852 end loop;
10854 if Present (Prev_Formal) then
10855 Decls := New_List (Act_Body);
10856 else
10857 Decls := New_List (Unit_Renaming, Act_Body);
10858 end if;
10860 -- The subprogram body is placed in the body of a dummy package body,
10861 -- whose spec contains the subprogram declaration as well as the
10862 -- renaming declarations for the generic parameters.
10864 Pack_Body := Make_Package_Body (Loc,
10865 Defining_Unit_Name => New_Copy (Pack_Id),
10866 Declarations => Decls);
10868 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10870 -- If the instantiation is a library unit, then build resulting
10871 -- compilation unit nodes for the instance. The declaration of
10872 -- the enclosing package is the grandparent of the subprogram
10873 -- declaration. First replace the instantiation node as the unit
10874 -- of the corresponding compilation.
10876 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10877 if Parent (Inst_Node) = Cunit (Main_Unit) then
10878 Set_Unit (Parent (Inst_Node), Inst_Node);
10879 Build_Instance_Compilation_Unit_Nodes
10880 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
10881 Analyze (Inst_Node);
10882 else
10883 Set_Parent (Pack_Body, Parent (Inst_Node));
10884 Analyze (Pack_Body);
10885 end if;
10887 else
10888 Insert_Before (Inst_Node, Pack_Body);
10889 Mark_Rewrite_Insertion (Pack_Body);
10890 Analyze (Pack_Body);
10892 if Expander_Active then
10893 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
10894 end if;
10895 end if;
10897 Inherit_Context (Gen_Body, Inst_Node);
10899 Restore_Private_Views (Pack_Id, False);
10901 if Parent_Installed then
10902 Remove_Parent (In_Body => True);
10904 -- Restore the previous visibility of the parent
10906 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10907 end if;
10909 Restore_Env;
10910 Style_Check := Saved_Style_Check;
10911 Restore_Warnings (Saved_Warnings);
10913 -- Body not found. Error was emitted already. If there were no previous
10914 -- errors, this may be an instance whose scope is a premature instance.
10915 -- In that case we must insure that the (legal) program does raise
10916 -- program error if executed. We generate a subprogram body for this
10917 -- purpose. See DEC ac30vso.
10919 -- Should not reference proprietary DEC tests in comments ???
10921 elsif Serious_Errors_Detected = 0
10922 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
10923 then
10924 if Body_Optional then
10925 return;
10927 elsif Ekind (Anon_Id) = E_Procedure then
10928 Act_Body :=
10929 Make_Subprogram_Body (Loc,
10930 Specification =>
10931 Make_Procedure_Specification (Loc,
10932 Defining_Unit_Name =>
10933 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10934 Parameter_Specifications =>
10935 New_Copy_List
10936 (Parameter_Specifications (Parent (Anon_Id)))),
10938 Declarations => Empty_List,
10939 Handled_Statement_Sequence =>
10940 Make_Handled_Sequence_Of_Statements (Loc,
10941 Statements =>
10942 New_List (
10943 Make_Raise_Program_Error (Loc,
10944 Reason =>
10945 PE_Access_Before_Elaboration))));
10947 else
10948 Ret_Expr :=
10949 Make_Raise_Program_Error (Loc,
10950 Reason => PE_Access_Before_Elaboration);
10952 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
10953 Set_Analyzed (Ret_Expr);
10955 Act_Body :=
10956 Make_Subprogram_Body (Loc,
10957 Specification =>
10958 Make_Function_Specification (Loc,
10959 Defining_Unit_Name =>
10960 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
10961 Parameter_Specifications =>
10962 New_Copy_List
10963 (Parameter_Specifications (Parent (Anon_Id))),
10964 Result_Definition =>
10965 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10967 Declarations => Empty_List,
10968 Handled_Statement_Sequence =>
10969 Make_Handled_Sequence_Of_Statements (Loc,
10970 Statements =>
10971 New_List
10972 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10973 end if;
10975 Pack_Body := Make_Package_Body (Loc,
10976 Defining_Unit_Name => New_Copy (Pack_Id),
10977 Declarations => New_List (Act_Body));
10979 Insert_After (Inst_Node, Pack_Body);
10980 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10981 Analyze (Pack_Body);
10982 end if;
10984 Expander_Mode_Restore;
10985 end Instantiate_Subprogram_Body;
10987 ----------------------
10988 -- Instantiate_Type --
10989 ----------------------
10991 function Instantiate_Type
10992 (Formal : Node_Id;
10993 Actual : Node_Id;
10994 Analyzed_Formal : Node_Id;
10995 Actual_Decls : List_Id) return List_Id
10997 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10998 A_Gen_T : constant Entity_Id :=
10999 Defining_Identifier (Analyzed_Formal);
11000 Ancestor : Entity_Id := Empty;
11001 Def : constant Node_Id := Formal_Type_Definition (Formal);
11002 Act_T : Entity_Id;
11003 Decl_Node : Node_Id;
11004 Decl_Nodes : List_Id;
11005 Loc : Source_Ptr;
11006 Subt : Entity_Id;
11008 procedure Diagnose_Predicated_Actual;
11009 -- There are a number of constructs in which a discrete type with
11010 -- predicates is illegal, e.g. as an index in an array type declaration.
11011 -- If a generic type is used is such a construct in a generic package
11012 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11013 -- of the generic contract that the actual cannot have predicates.
11015 procedure Validate_Array_Type_Instance;
11016 procedure Validate_Access_Subprogram_Instance;
11017 procedure Validate_Access_Type_Instance;
11018 procedure Validate_Derived_Type_Instance;
11019 procedure Validate_Derived_Interface_Type_Instance;
11020 procedure Validate_Discriminated_Formal_Type;
11021 procedure Validate_Interface_Type_Instance;
11022 procedure Validate_Private_Type_Instance;
11023 procedure Validate_Incomplete_Type_Instance;
11024 -- These procedures perform validation tests for the named case.
11025 -- Validate_Discriminated_Formal_Type is shared by formal private
11026 -- types and Ada 2012 formal incomplete types.
11028 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11029 -- Check that base types are the same and that the subtypes match
11030 -- statically. Used in several of the above.
11032 ---------------------------------
11033 -- Diagnose_Predicated_Actual --
11034 ---------------------------------
11036 procedure Diagnose_Predicated_Actual is
11037 begin
11038 if No_Predicate_On_Actual (A_Gen_T)
11039 and then Has_Predicates (Act_T)
11040 then
11041 Error_Msg_NE
11042 ("actual for& cannot be a type with predicate",
11043 Instantiation_Node, A_Gen_T);
11045 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11046 and then Has_Predicates (Act_T)
11047 and then not Has_Static_Predicate_Aspect (Act_T)
11048 then
11049 Error_Msg_NE
11050 ("actual for& cannot be a type with a dynamic predicate",
11051 Instantiation_Node, A_Gen_T);
11052 end if;
11053 end Diagnose_Predicated_Actual;
11055 --------------------
11056 -- Subtypes_Match --
11057 --------------------
11059 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11060 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11062 begin
11063 -- Some detailed comments would be useful here ???
11065 return ((Base_Type (T) = Act_T
11066 or else Base_Type (T) = Base_Type (Act_T))
11067 and then Subtypes_Statically_Match (T, Act_T))
11069 or else (Is_Class_Wide_Type (Gen_T)
11070 and then Is_Class_Wide_Type (Act_T)
11071 and then Subtypes_Match
11072 (Get_Instance_Of (Root_Type (Gen_T)),
11073 Root_Type (Act_T)))
11075 or else
11076 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11077 E_Anonymous_Access_Type)
11078 and then Ekind (Act_T) = Ekind (Gen_T)
11079 and then Subtypes_Statically_Match
11080 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11081 end Subtypes_Match;
11083 -----------------------------------------
11084 -- Validate_Access_Subprogram_Instance --
11085 -----------------------------------------
11087 procedure Validate_Access_Subprogram_Instance is
11088 begin
11089 if not Is_Access_Type (Act_T)
11090 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11091 then
11092 Error_Msg_NE
11093 ("expect access type in instantiation of &", Actual, Gen_T);
11094 Abandon_Instantiation (Actual);
11095 end if;
11097 -- According to AI05-288, actuals for access_to_subprograms must be
11098 -- subtype conformant with the generic formal. Previous to AI05-288
11099 -- only mode conformance was required.
11101 -- This is a binding interpretation that applies to previous versions
11102 -- of the language, no need to maintain previous weaker checks.
11104 Check_Subtype_Conformant
11105 (Designated_Type (Act_T),
11106 Designated_Type (A_Gen_T),
11107 Actual,
11108 Get_Inst => True);
11110 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11111 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11112 Error_Msg_NE
11113 ("protected access type not allowed for formal &",
11114 Actual, Gen_T);
11115 end if;
11117 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11118 Error_Msg_NE
11119 ("expect protected access type for formal &",
11120 Actual, Gen_T);
11121 end if;
11122 end Validate_Access_Subprogram_Instance;
11124 -----------------------------------
11125 -- Validate_Access_Type_Instance --
11126 -----------------------------------
11128 procedure Validate_Access_Type_Instance is
11129 Desig_Type : constant Entity_Id :=
11130 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11131 Desig_Act : Entity_Id;
11133 begin
11134 if not Is_Access_Type (Act_T) then
11135 Error_Msg_NE
11136 ("expect access type in instantiation of &", Actual, Gen_T);
11137 Abandon_Instantiation (Actual);
11138 end if;
11140 if Is_Access_Constant (A_Gen_T) then
11141 if not Is_Access_Constant (Act_T) then
11142 Error_Msg_N
11143 ("actual type must be access-to-constant type", Actual);
11144 Abandon_Instantiation (Actual);
11145 end if;
11146 else
11147 if Is_Access_Constant (Act_T) then
11148 Error_Msg_N
11149 ("actual type must be access-to-variable type", Actual);
11150 Abandon_Instantiation (Actual);
11152 elsif Ekind (A_Gen_T) = E_General_Access_Type
11153 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11154 then
11155 Error_Msg_N -- CODEFIX
11156 ("actual must be general access type!", Actual);
11157 Error_Msg_NE -- CODEFIX
11158 ("add ALL to }!", Actual, Act_T);
11159 Abandon_Instantiation (Actual);
11160 end if;
11161 end if;
11163 -- The designated subtypes, that is to say the subtypes introduced
11164 -- by an access type declaration (and not by a subtype declaration)
11165 -- must match.
11167 Desig_Act := Designated_Type (Base_Type (Act_T));
11169 -- The designated type may have been introduced through a limited_
11170 -- with clause, in which case retrieve the non-limited view. This
11171 -- applies to incomplete types as well as to class-wide types.
11173 if From_Limited_With (Desig_Act) then
11174 Desig_Act := Available_View (Desig_Act);
11175 end if;
11177 if not Subtypes_Match (Desig_Type, Desig_Act) then
11178 Error_Msg_NE
11179 ("designated type of actual does not match that of formal &",
11180 Actual, Gen_T);
11182 if not Predicates_Match (Desig_Type, Desig_Act) then
11183 Error_Msg_N ("\predicates do not match", Actual);
11184 end if;
11186 Abandon_Instantiation (Actual);
11188 elsif Is_Access_Type (Designated_Type (Act_T))
11189 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11191 Is_Constrained (Designated_Type (Desig_Type))
11192 then
11193 Error_Msg_NE
11194 ("designated type of actual does not match that of formal &",
11195 Actual, Gen_T);
11197 if not Predicates_Match (Desig_Type, Desig_Act) then
11198 Error_Msg_N ("\predicates do not match", Actual);
11199 end if;
11201 Abandon_Instantiation (Actual);
11202 end if;
11204 -- Ada 2005: null-exclusion indicators of the two types must agree
11206 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11207 Error_Msg_NE
11208 ("non null exclusion of actual and formal & do not match",
11209 Actual, Gen_T);
11210 end if;
11211 end Validate_Access_Type_Instance;
11213 ----------------------------------
11214 -- Validate_Array_Type_Instance --
11215 ----------------------------------
11217 procedure Validate_Array_Type_Instance is
11218 I1 : Node_Id;
11219 I2 : Node_Id;
11220 T2 : Entity_Id;
11222 function Formal_Dimensions return Int;
11223 -- Count number of dimensions in array type formal
11225 -----------------------
11226 -- Formal_Dimensions --
11227 -----------------------
11229 function Formal_Dimensions return Int is
11230 Num : Int := 0;
11231 Index : Node_Id;
11233 begin
11234 if Nkind (Def) = N_Constrained_Array_Definition then
11235 Index := First (Discrete_Subtype_Definitions (Def));
11236 else
11237 Index := First (Subtype_Marks (Def));
11238 end if;
11240 while Present (Index) loop
11241 Num := Num + 1;
11242 Next_Index (Index);
11243 end loop;
11245 return Num;
11246 end Formal_Dimensions;
11248 -- Start of processing for Validate_Array_Type_Instance
11250 begin
11251 if not Is_Array_Type (Act_T) then
11252 Error_Msg_NE
11253 ("expect array type in instantiation of &", Actual, Gen_T);
11254 Abandon_Instantiation (Actual);
11256 elsif Nkind (Def) = N_Constrained_Array_Definition then
11257 if not (Is_Constrained (Act_T)) then
11258 Error_Msg_NE
11259 ("expect constrained array in instantiation of &",
11260 Actual, Gen_T);
11261 Abandon_Instantiation (Actual);
11262 end if;
11264 else
11265 if Is_Constrained (Act_T) then
11266 Error_Msg_NE
11267 ("expect unconstrained array in instantiation of &",
11268 Actual, Gen_T);
11269 Abandon_Instantiation (Actual);
11270 end if;
11271 end if;
11273 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11274 Error_Msg_NE
11275 ("dimensions of actual do not match formal &", Actual, Gen_T);
11276 Abandon_Instantiation (Actual);
11277 end if;
11279 I1 := First_Index (A_Gen_T);
11280 I2 := First_Index (Act_T);
11281 for J in 1 .. Formal_Dimensions loop
11283 -- If the indexes of the actual were given by a subtype_mark,
11284 -- the index was transformed into a range attribute. Retrieve
11285 -- the original type mark for checking.
11287 if Is_Entity_Name (Original_Node (I2)) then
11288 T2 := Entity (Original_Node (I2));
11289 else
11290 T2 := Etype (I2);
11291 end if;
11293 if not Subtypes_Match
11294 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11295 then
11296 Error_Msg_NE
11297 ("index types of actual do not match those of formal &",
11298 Actual, Gen_T);
11299 Abandon_Instantiation (Actual);
11300 end if;
11302 Next_Index (I1);
11303 Next_Index (I2);
11304 end loop;
11306 -- Check matching subtypes. Note that there are complex visibility
11307 -- issues when the generic is a child unit and some aspect of the
11308 -- generic type is declared in a parent unit of the generic. We do
11309 -- the test to handle this special case only after a direct check
11310 -- for static matching has failed. The case where both the component
11311 -- type and the array type are separate formals, and the component
11312 -- type is a private view may also require special checking in
11313 -- Subtypes_Match.
11315 if Subtypes_Match
11316 (Component_Type (A_Gen_T), Component_Type (Act_T))
11317 or else
11318 Subtypes_Match
11319 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11320 Component_Type (Act_T))
11321 then
11322 null;
11323 else
11324 Error_Msg_NE
11325 ("component subtype of actual does not match that of formal &",
11326 Actual, Gen_T);
11327 Abandon_Instantiation (Actual);
11328 end if;
11330 if Has_Aliased_Components (A_Gen_T)
11331 and then not Has_Aliased_Components (Act_T)
11332 then
11333 Error_Msg_NE
11334 ("actual must have aliased components to match formal type &",
11335 Actual, Gen_T);
11336 end if;
11337 end Validate_Array_Type_Instance;
11339 -----------------------------------------------
11340 -- Validate_Derived_Interface_Type_Instance --
11341 -----------------------------------------------
11343 procedure Validate_Derived_Interface_Type_Instance is
11344 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11345 Elmt : Elmt_Id;
11347 begin
11348 -- First apply interface instance checks
11350 Validate_Interface_Type_Instance;
11352 -- Verify that immediate parent interface is an ancestor of
11353 -- the actual.
11355 if Present (Par)
11356 and then not Interface_Present_In_Ancestor (Act_T, Par)
11357 then
11358 Error_Msg_NE
11359 ("interface actual must include progenitor&", Actual, Par);
11360 end if;
11362 -- Now verify that the actual includes all other ancestors of
11363 -- the formal.
11365 Elmt := First_Elmt (Interfaces (A_Gen_T));
11366 while Present (Elmt) loop
11367 if not Interface_Present_In_Ancestor
11368 (Act_T, Get_Instance_Of (Node (Elmt)))
11369 then
11370 Error_Msg_NE
11371 ("interface actual must include progenitor&",
11372 Actual, Node (Elmt));
11373 end if;
11375 Next_Elmt (Elmt);
11376 end loop;
11377 end Validate_Derived_Interface_Type_Instance;
11379 ------------------------------------
11380 -- Validate_Derived_Type_Instance --
11381 ------------------------------------
11383 procedure Validate_Derived_Type_Instance is
11384 Actual_Discr : Entity_Id;
11385 Ancestor_Discr : Entity_Id;
11387 begin
11388 -- If the parent type in the generic declaration is itself a previous
11389 -- formal type, then it is local to the generic and absent from the
11390 -- analyzed generic definition. In that case the ancestor is the
11391 -- instance of the formal (which must have been instantiated
11392 -- previously), unless the ancestor is itself a formal derived type.
11393 -- In this latter case (which is the subject of Corrigendum 8652/0038
11394 -- (AI-202) the ancestor of the formals is the ancestor of its
11395 -- parent. Otherwise, the analyzed generic carries the parent type.
11396 -- If the parent type is defined in a previous formal package, then
11397 -- the scope of that formal package is that of the generic type
11398 -- itself, and it has already been mapped into the corresponding type
11399 -- in the actual package.
11401 -- Common case: parent type defined outside of the generic
11403 if Is_Entity_Name (Subtype_Mark (Def))
11404 and then Present (Entity (Subtype_Mark (Def)))
11405 then
11406 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11408 -- Check whether parent is defined in a previous formal package
11410 elsif
11411 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11412 then
11413 Ancestor :=
11414 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11416 -- The type may be a local derivation, or a type extension of a
11417 -- previous formal, or of a formal of a parent package.
11419 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11420 or else
11421 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11422 then
11423 -- Check whether the parent is another derived formal type in the
11424 -- same generic unit.
11426 if Etype (A_Gen_T) /= A_Gen_T
11427 and then Is_Generic_Type (Etype (A_Gen_T))
11428 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11429 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11430 then
11431 -- Locate ancestor of parent from the subtype declaration
11432 -- created for the actual.
11434 declare
11435 Decl : Node_Id;
11437 begin
11438 Decl := First (Actual_Decls);
11439 while Present (Decl) loop
11440 if Nkind (Decl) = N_Subtype_Declaration
11441 and then Chars (Defining_Identifier (Decl)) =
11442 Chars (Etype (A_Gen_T))
11443 then
11444 Ancestor := Generic_Parent_Type (Decl);
11445 exit;
11446 else
11447 Next (Decl);
11448 end if;
11449 end loop;
11450 end;
11452 pragma Assert (Present (Ancestor));
11454 -- The ancestor itself may be a previous formal that has been
11455 -- instantiated.
11457 Ancestor := Get_Instance_Of (Ancestor);
11459 else
11460 Ancestor :=
11461 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11462 end if;
11464 -- An unusual case: the actual is a type declared in a parent unit,
11465 -- but is not a formal type so there is no instance_of for it.
11466 -- Retrieve it by analyzing the record extension.
11468 elsif Is_Child_Unit (Scope (A_Gen_T))
11469 and then In_Open_Scopes (Scope (Act_T))
11470 and then Is_Generic_Instance (Scope (Act_T))
11471 then
11472 Analyze (Subtype_Mark (Def));
11473 Ancestor := Entity (Subtype_Mark (Def));
11475 else
11476 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11477 end if;
11479 -- If the formal derived type has pragma Preelaborable_Initialization
11480 -- then the actual type must have preelaborable initialization.
11482 if Known_To_Have_Preelab_Init (A_Gen_T)
11483 and then not Has_Preelaborable_Initialization (Act_T)
11484 then
11485 Error_Msg_NE
11486 ("actual for & must have preelaborable initialization",
11487 Actual, Gen_T);
11488 end if;
11490 -- Ada 2005 (AI-251)
11492 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11493 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11494 Error_Msg_NE
11495 ("(Ada 2005) expected type implementing & in instantiation",
11496 Actual, Ancestor);
11497 end if;
11499 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11500 Error_Msg_NE
11501 ("expect type derived from & in instantiation",
11502 Actual, First_Subtype (Ancestor));
11503 Abandon_Instantiation (Actual);
11504 end if;
11506 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11507 -- that the formal type declaration has been rewritten as a private
11508 -- extension.
11510 if Ada_Version >= Ada_2005
11511 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11512 and then Synchronized_Present (Parent (A_Gen_T))
11513 then
11514 -- The actual must be a synchronized tagged type
11516 if not Is_Tagged_Type (Act_T) then
11517 Error_Msg_N
11518 ("actual of synchronized type must be tagged", Actual);
11519 Abandon_Instantiation (Actual);
11521 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11522 and then Nkind (Type_Definition (Parent (Act_T))) =
11523 N_Derived_Type_Definition
11524 and then not Synchronized_Present
11525 (Type_Definition (Parent (Act_T)))
11526 then
11527 Error_Msg_N
11528 ("actual of synchronized type must be synchronized", Actual);
11529 Abandon_Instantiation (Actual);
11530 end if;
11531 end if;
11533 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11534 -- removes the second instance of the phrase "or allow pass by copy".
11536 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11537 Error_Msg_N
11538 ("cannot have atomic actual type for non-atomic formal type",
11539 Actual);
11541 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11542 Error_Msg_N
11543 ("cannot have volatile actual type for non-volatile formal type",
11544 Actual);
11545 end if;
11547 -- It should not be necessary to check for unknown discriminants on
11548 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11549 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11550 -- needs fixing. ???
11552 if not Is_Indefinite_Subtype (A_Gen_T)
11553 and then not Unknown_Discriminants_Present (Formal)
11554 and then Is_Indefinite_Subtype (Act_T)
11555 then
11556 Error_Msg_N ("actual subtype must be constrained", Actual);
11557 Abandon_Instantiation (Actual);
11558 end if;
11560 if not Unknown_Discriminants_Present (Formal) then
11561 if Is_Constrained (Ancestor) then
11562 if not Is_Constrained (Act_T) then
11563 Error_Msg_N ("actual subtype must be constrained", Actual);
11564 Abandon_Instantiation (Actual);
11565 end if;
11567 -- Ancestor is unconstrained, Check if generic formal and actual
11568 -- agree on constrainedness. The check only applies to array types
11569 -- and discriminated types.
11571 elsif Is_Constrained (Act_T) then
11572 if Ekind (Ancestor) = E_Access_Type
11573 or else (not Is_Constrained (A_Gen_T)
11574 and then Is_Composite_Type (A_Gen_T))
11575 then
11576 Error_Msg_N ("actual subtype must be unconstrained", Actual);
11577 Abandon_Instantiation (Actual);
11578 end if;
11580 -- A class-wide type is only allowed if the formal has unknown
11581 -- discriminants.
11583 elsif Is_Class_Wide_Type (Act_T)
11584 and then not Has_Unknown_Discriminants (Ancestor)
11585 then
11586 Error_Msg_NE
11587 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11588 Abandon_Instantiation (Actual);
11590 -- Otherwise, the formal and actual must have the same number
11591 -- of discriminants and each discriminant of the actual must
11592 -- correspond to a discriminant of the formal.
11594 elsif Has_Discriminants (Act_T)
11595 and then not Has_Unknown_Discriminants (Act_T)
11596 and then Has_Discriminants (Ancestor)
11597 then
11598 Actual_Discr := First_Discriminant (Act_T);
11599 Ancestor_Discr := First_Discriminant (Ancestor);
11600 while Present (Actual_Discr)
11601 and then Present (Ancestor_Discr)
11602 loop
11603 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11604 No (Corresponding_Discriminant (Actual_Discr))
11605 then
11606 Error_Msg_NE
11607 ("discriminant & does not correspond "
11608 & "to ancestor discriminant", Actual, Actual_Discr);
11609 Abandon_Instantiation (Actual);
11610 end if;
11612 Next_Discriminant (Actual_Discr);
11613 Next_Discriminant (Ancestor_Discr);
11614 end loop;
11616 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11617 Error_Msg_NE
11618 ("actual for & must have same number of discriminants",
11619 Actual, Gen_T);
11620 Abandon_Instantiation (Actual);
11621 end if;
11623 -- This case should be caught by the earlier check for
11624 -- constrainedness, but the check here is added for completeness.
11626 elsif Has_Discriminants (Act_T)
11627 and then not Has_Unknown_Discriminants (Act_T)
11628 then
11629 Error_Msg_NE
11630 ("actual for & must not have discriminants", Actual, Gen_T);
11631 Abandon_Instantiation (Actual);
11633 elsif Has_Discriminants (Ancestor) then
11634 Error_Msg_NE
11635 ("actual for & must have known discriminants", Actual, Gen_T);
11636 Abandon_Instantiation (Actual);
11637 end if;
11639 if not Subtypes_Statically_Compatible
11640 (Act_T, Ancestor, Formal_Derived_Matching => True)
11641 then
11642 Error_Msg_N
11643 ("constraint on actual is incompatible with formal", Actual);
11644 Abandon_Instantiation (Actual);
11645 end if;
11646 end if;
11648 -- If the formal and actual types are abstract, check that there
11649 -- are no abstract primitives of the actual type that correspond to
11650 -- nonabstract primitives of the formal type (second sentence of
11651 -- RM95-3.9.3(9)).
11653 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11654 Check_Abstract_Primitives : declare
11655 Gen_Prims : constant Elist_Id :=
11656 Primitive_Operations (A_Gen_T);
11657 Gen_Elmt : Elmt_Id;
11658 Gen_Subp : Entity_Id;
11659 Anc_Subp : Entity_Id;
11660 Anc_Formal : Entity_Id;
11661 Anc_F_Type : Entity_Id;
11663 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11664 Act_Elmt : Elmt_Id;
11665 Act_Subp : Entity_Id;
11666 Act_Formal : Entity_Id;
11667 Act_F_Type : Entity_Id;
11669 Subprograms_Correspond : Boolean;
11671 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11672 -- Returns true if T2 is derived directly or indirectly from
11673 -- T1, including derivations from interfaces. T1 and T2 are
11674 -- required to be specific tagged base types.
11676 ------------------------
11677 -- Is_Tagged_Ancestor --
11678 ------------------------
11680 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11682 Intfc_Elmt : Elmt_Id;
11684 begin
11685 -- The predicate is satisfied if the types are the same
11687 if T1 = T2 then
11688 return True;
11690 -- If we've reached the top of the derivation chain then
11691 -- we know that T1 is not an ancestor of T2.
11693 elsif Etype (T2) = T2 then
11694 return False;
11696 -- Proceed to check T2's immediate parent
11698 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11699 return True;
11701 -- Finally, check to see if T1 is an ancestor of any of T2's
11702 -- progenitors.
11704 else
11705 Intfc_Elmt := First_Elmt (Interfaces (T2));
11706 while Present (Intfc_Elmt) loop
11707 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11708 return True;
11709 end if;
11711 Next_Elmt (Intfc_Elmt);
11712 end loop;
11713 end if;
11715 return False;
11716 end Is_Tagged_Ancestor;
11718 -- Start of processing for Check_Abstract_Primitives
11720 begin
11721 -- Loop over all of the formal derived type's primitives
11723 Gen_Elmt := First_Elmt (Gen_Prims);
11724 while Present (Gen_Elmt) loop
11725 Gen_Subp := Node (Gen_Elmt);
11727 -- If the primitive of the formal is not abstract, then
11728 -- determine whether there is a corresponding primitive of
11729 -- the actual type that's abstract.
11731 if not Is_Abstract_Subprogram (Gen_Subp) then
11732 Act_Elmt := First_Elmt (Act_Prims);
11733 while Present (Act_Elmt) loop
11734 Act_Subp := Node (Act_Elmt);
11736 -- If we find an abstract primitive of the actual,
11737 -- then we need to test whether it corresponds to the
11738 -- subprogram from which the generic formal primitive
11739 -- is inherited.
11741 if Is_Abstract_Subprogram (Act_Subp) then
11742 Anc_Subp := Alias (Gen_Subp);
11744 -- Test whether we have a corresponding primitive
11745 -- by comparing names, kinds, formal types, and
11746 -- result types.
11748 if Chars (Anc_Subp) = Chars (Act_Subp)
11749 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11750 then
11751 Anc_Formal := First_Formal (Anc_Subp);
11752 Act_Formal := First_Formal (Act_Subp);
11753 while Present (Anc_Formal)
11754 and then Present (Act_Formal)
11755 loop
11756 Anc_F_Type := Etype (Anc_Formal);
11757 Act_F_Type := Etype (Act_Formal);
11759 if Ekind (Anc_F_Type) =
11760 E_Anonymous_Access_Type
11761 then
11762 Anc_F_Type := Designated_Type (Anc_F_Type);
11764 if Ekind (Act_F_Type) =
11765 E_Anonymous_Access_Type
11766 then
11767 Act_F_Type :=
11768 Designated_Type (Act_F_Type);
11769 else
11770 exit;
11771 end if;
11773 elsif
11774 Ekind (Act_F_Type) = E_Anonymous_Access_Type
11775 then
11776 exit;
11777 end if;
11779 Anc_F_Type := Base_Type (Anc_F_Type);
11780 Act_F_Type := Base_Type (Act_F_Type);
11782 -- If the formal is controlling, then the
11783 -- the type of the actual primitive's formal
11784 -- must be derived directly or indirectly
11785 -- from the type of the ancestor primitive's
11786 -- formal.
11788 if Is_Controlling_Formal (Anc_Formal) then
11789 if not Is_Tagged_Ancestor
11790 (Anc_F_Type, Act_F_Type)
11791 then
11792 exit;
11793 end if;
11795 -- Otherwise the types of the formals must
11796 -- be the same.
11798 elsif Anc_F_Type /= Act_F_Type then
11799 exit;
11800 end if;
11802 Next_Entity (Anc_Formal);
11803 Next_Entity (Act_Formal);
11804 end loop;
11806 -- If we traversed through all of the formals
11807 -- then so far the subprograms correspond, so
11808 -- now check that any result types correspond.
11810 if No (Anc_Formal) and then No (Act_Formal) then
11811 Subprograms_Correspond := True;
11813 if Ekind (Act_Subp) = E_Function then
11814 Anc_F_Type := Etype (Anc_Subp);
11815 Act_F_Type := Etype (Act_Subp);
11817 if Ekind (Anc_F_Type) =
11818 E_Anonymous_Access_Type
11819 then
11820 Anc_F_Type :=
11821 Designated_Type (Anc_F_Type);
11823 if Ekind (Act_F_Type) =
11824 E_Anonymous_Access_Type
11825 then
11826 Act_F_Type :=
11827 Designated_Type (Act_F_Type);
11828 else
11829 Subprograms_Correspond := False;
11830 end if;
11832 elsif
11833 Ekind (Act_F_Type)
11834 = E_Anonymous_Access_Type
11835 then
11836 Subprograms_Correspond := False;
11837 end if;
11839 Anc_F_Type := Base_Type (Anc_F_Type);
11840 Act_F_Type := Base_Type (Act_F_Type);
11842 -- Now either the result types must be
11843 -- the same or, if the result type is
11844 -- controlling, the result type of the
11845 -- actual primitive must descend from the
11846 -- result type of the ancestor primitive.
11848 if Subprograms_Correspond
11849 and then Anc_F_Type /= Act_F_Type
11850 and then
11851 Has_Controlling_Result (Anc_Subp)
11852 and then not Is_Tagged_Ancestor
11853 (Anc_F_Type, Act_F_Type)
11854 then
11855 Subprograms_Correspond := False;
11856 end if;
11857 end if;
11859 -- Found a matching subprogram belonging to
11860 -- formal ancestor type, so actual subprogram
11861 -- corresponds and this violates 3.9.3(9).
11863 if Subprograms_Correspond then
11864 Error_Msg_NE
11865 ("abstract subprogram & overrides "
11866 & "nonabstract subprogram of ancestor",
11867 Actual, Act_Subp);
11868 end if;
11869 end if;
11870 end if;
11871 end if;
11873 Next_Elmt (Act_Elmt);
11874 end loop;
11875 end if;
11877 Next_Elmt (Gen_Elmt);
11878 end loop;
11879 end Check_Abstract_Primitives;
11880 end if;
11882 -- Verify that limitedness matches. If parent is a limited
11883 -- interface then the generic formal is not unless declared
11884 -- explicitly so. If not declared limited, the actual cannot be
11885 -- limited (see AI05-0087).
11887 -- Even though this AI is a binding interpretation, we enable the
11888 -- check only in Ada 2012 mode, because this improper construct
11889 -- shows up in user code and in existing B-tests.
11891 if Is_Limited_Type (Act_T)
11892 and then not Is_Limited_Type (A_Gen_T)
11893 and then Ada_Version >= Ada_2012
11894 then
11895 if In_Instance then
11896 null;
11897 else
11898 Error_Msg_NE
11899 ("actual for non-limited & cannot be a limited type",
11900 Actual, Gen_T);
11901 Explain_Limited_Type (Act_T, Actual);
11902 Abandon_Instantiation (Actual);
11903 end if;
11904 end if;
11905 end Validate_Derived_Type_Instance;
11907 ----------------------------------------
11908 -- Validate_Discriminated_Formal_Type --
11909 ----------------------------------------
11911 procedure Validate_Discriminated_Formal_Type is
11912 Formal_Discr : Entity_Id;
11913 Actual_Discr : Entity_Id;
11914 Formal_Subt : Entity_Id;
11916 begin
11917 if Has_Discriminants (A_Gen_T) then
11918 if not Has_Discriminants (Act_T) then
11919 Error_Msg_NE
11920 ("actual for & must have discriminants", Actual, Gen_T);
11921 Abandon_Instantiation (Actual);
11923 elsif Is_Constrained (Act_T) then
11924 Error_Msg_NE
11925 ("actual for & must be unconstrained", Actual, Gen_T);
11926 Abandon_Instantiation (Actual);
11928 else
11929 Formal_Discr := First_Discriminant (A_Gen_T);
11930 Actual_Discr := First_Discriminant (Act_T);
11931 while Formal_Discr /= Empty loop
11932 if Actual_Discr = Empty then
11933 Error_Msg_NE
11934 ("discriminants on actual do not match formal",
11935 Actual, Gen_T);
11936 Abandon_Instantiation (Actual);
11937 end if;
11939 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
11941 -- Access discriminants match if designated types do
11943 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
11944 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
11945 E_Anonymous_Access_Type
11946 and then
11947 Get_Instance_Of
11948 (Designated_Type (Base_Type (Formal_Subt))) =
11949 Designated_Type (Base_Type (Etype (Actual_Discr)))
11950 then
11951 null;
11953 elsif Base_Type (Formal_Subt) /=
11954 Base_Type (Etype (Actual_Discr))
11955 then
11956 Error_Msg_NE
11957 ("types of actual discriminants must match formal",
11958 Actual, Gen_T);
11959 Abandon_Instantiation (Actual);
11961 elsif not Subtypes_Statically_Match
11962 (Formal_Subt, Etype (Actual_Discr))
11963 and then Ada_Version >= Ada_95
11964 then
11965 Error_Msg_NE
11966 ("subtypes of actual discriminants must match formal",
11967 Actual, Gen_T);
11968 Abandon_Instantiation (Actual);
11969 end if;
11971 Next_Discriminant (Formal_Discr);
11972 Next_Discriminant (Actual_Discr);
11973 end loop;
11975 if Actual_Discr /= Empty then
11976 Error_Msg_NE
11977 ("discriminants on actual do not match formal",
11978 Actual, Gen_T);
11979 Abandon_Instantiation (Actual);
11980 end if;
11981 end if;
11982 end if;
11983 end Validate_Discriminated_Formal_Type;
11985 ---------------------------------------
11986 -- Validate_Incomplete_Type_Instance --
11987 ---------------------------------------
11989 procedure Validate_Incomplete_Type_Instance is
11990 begin
11991 if not Is_Tagged_Type (Act_T)
11992 and then Is_Tagged_Type (A_Gen_T)
11993 then
11994 Error_Msg_NE
11995 ("actual for & must be a tagged type", Actual, Gen_T);
11996 end if;
11998 Validate_Discriminated_Formal_Type;
11999 end Validate_Incomplete_Type_Instance;
12001 --------------------------------------
12002 -- Validate_Interface_Type_Instance --
12003 --------------------------------------
12005 procedure Validate_Interface_Type_Instance is
12006 begin
12007 if not Is_Interface (Act_T) then
12008 Error_Msg_NE
12009 ("actual for formal interface type must be an interface",
12010 Actual, Gen_T);
12012 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12013 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12014 or else Is_Protected_Interface (A_Gen_T) /=
12015 Is_Protected_Interface (Act_T)
12016 or else Is_Synchronized_Interface (A_Gen_T) /=
12017 Is_Synchronized_Interface (Act_T)
12018 then
12019 Error_Msg_NE
12020 ("actual for interface& does not match (RM 12.5.5(4))",
12021 Actual, Gen_T);
12022 end if;
12023 end Validate_Interface_Type_Instance;
12025 ------------------------------------
12026 -- Validate_Private_Type_Instance --
12027 ------------------------------------
12029 procedure Validate_Private_Type_Instance is
12030 begin
12031 if Is_Limited_Type (Act_T)
12032 and then not Is_Limited_Type (A_Gen_T)
12033 then
12034 if In_Instance then
12035 null;
12036 else
12037 Error_Msg_NE
12038 ("actual for non-limited & cannot be a limited type", Actual,
12039 Gen_T);
12040 Explain_Limited_Type (Act_T, Actual);
12041 Abandon_Instantiation (Actual);
12042 end if;
12044 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12045 and then not Has_Preelaborable_Initialization (Act_T)
12046 then
12047 Error_Msg_NE
12048 ("actual for & must have preelaborable initialization", Actual,
12049 Gen_T);
12051 elsif Is_Indefinite_Subtype (Act_T)
12052 and then not Is_Indefinite_Subtype (A_Gen_T)
12053 and then Ada_Version >= Ada_95
12054 then
12055 Error_Msg_NE
12056 ("actual for & must be a definite subtype", Actual, Gen_T);
12058 elsif not Is_Tagged_Type (Act_T)
12059 and then Is_Tagged_Type (A_Gen_T)
12060 then
12061 Error_Msg_NE
12062 ("actual for & must be a tagged type", Actual, Gen_T);
12063 end if;
12065 Validate_Discriminated_Formal_Type;
12066 Ancestor := Gen_T;
12067 end Validate_Private_Type_Instance;
12069 -- Start of processing for Instantiate_Type
12071 begin
12072 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12073 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12074 return New_List (Error);
12076 elsif not Is_Entity_Name (Actual)
12077 or else not Is_Type (Entity (Actual))
12078 then
12079 Error_Msg_NE
12080 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12081 Abandon_Instantiation (Actual);
12083 else
12084 Act_T := Entity (Actual);
12086 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12087 -- as a generic actual parameter if the corresponding formal type
12088 -- does not have a known_discriminant_part, or is a formal derived
12089 -- type that is an Unchecked_Union type.
12091 if Is_Unchecked_Union (Base_Type (Act_T)) then
12092 if not Has_Discriminants (A_Gen_T)
12093 or else (Is_Derived_Type (A_Gen_T)
12094 and then Is_Unchecked_Union (A_Gen_T))
12095 then
12096 null;
12097 else
12098 Error_Msg_N ("unchecked union cannot be the actual for a "
12099 & "discriminated formal type", Act_T);
12101 end if;
12102 end if;
12104 -- Deal with fixed/floating restrictions
12106 if Is_Floating_Point_Type (Act_T) then
12107 Check_Restriction (No_Floating_Point, Actual);
12108 elsif Is_Fixed_Point_Type (Act_T) then
12109 Check_Restriction (No_Fixed_Point, Actual);
12110 end if;
12112 -- Deal with error of using incomplete type as generic actual.
12113 -- This includes limited views of a type, even if the non-limited
12114 -- view may be available.
12116 if Ekind (Act_T) = E_Incomplete_Type
12117 or else (Is_Class_Wide_Type (Act_T)
12118 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12119 then
12120 -- If the formal is an incomplete type, the actual can be
12121 -- incomplete as well.
12123 if Ekind (A_Gen_T) = E_Incomplete_Type then
12124 null;
12126 elsif Is_Class_Wide_Type (Act_T)
12127 or else No (Full_View (Act_T))
12128 then
12129 Error_Msg_N ("premature use of incomplete type", Actual);
12130 Abandon_Instantiation (Actual);
12131 else
12132 Act_T := Full_View (Act_T);
12133 Set_Entity (Actual, Act_T);
12135 if Has_Private_Component (Act_T) then
12136 Error_Msg_N
12137 ("premature use of type with private component", Actual);
12138 end if;
12139 end if;
12141 -- Deal with error of premature use of private type as generic actual
12143 elsif Is_Private_Type (Act_T)
12144 and then Is_Private_Type (Base_Type (Act_T))
12145 and then not Is_Generic_Type (Act_T)
12146 and then not Is_Derived_Type (Act_T)
12147 and then No (Full_View (Root_Type (Act_T)))
12148 then
12149 -- If the formal is an incomplete type, the actual can be
12150 -- private or incomplete as well.
12152 if Ekind (A_Gen_T) = E_Incomplete_Type then
12153 null;
12154 else
12155 Error_Msg_N ("premature use of private type", Actual);
12156 end if;
12158 elsif Has_Private_Component (Act_T) then
12159 Error_Msg_N
12160 ("premature use of type with private component", Actual);
12161 end if;
12163 Set_Instance_Of (A_Gen_T, Act_T);
12165 -- If the type is generic, the class-wide type may also be used
12167 if Is_Tagged_Type (A_Gen_T)
12168 and then Is_Tagged_Type (Act_T)
12169 and then not Is_Class_Wide_Type (A_Gen_T)
12170 then
12171 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12172 Class_Wide_Type (Act_T));
12173 end if;
12175 if not Is_Abstract_Type (A_Gen_T)
12176 and then Is_Abstract_Type (Act_T)
12177 then
12178 Error_Msg_N
12179 ("actual of non-abstract formal cannot be abstract", Actual);
12180 end if;
12182 -- A generic scalar type is a first subtype for which we generate
12183 -- an anonymous base type. Indicate that the instance of this base
12184 -- is the base type of the actual.
12186 if Is_Scalar_Type (A_Gen_T) then
12187 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12188 end if;
12189 end if;
12191 if Error_Posted (Act_T) then
12192 null;
12193 else
12194 case Nkind (Def) is
12195 when N_Formal_Private_Type_Definition =>
12196 Validate_Private_Type_Instance;
12198 when N_Formal_Incomplete_Type_Definition =>
12199 Validate_Incomplete_Type_Instance;
12201 when N_Formal_Derived_Type_Definition =>
12202 Validate_Derived_Type_Instance;
12204 when N_Formal_Discrete_Type_Definition =>
12205 if not Is_Discrete_Type (Act_T) then
12206 Error_Msg_NE
12207 ("expect discrete type in instantiation of&",
12208 Actual, Gen_T);
12209 Abandon_Instantiation (Actual);
12210 end if;
12212 Diagnose_Predicated_Actual;
12214 when N_Formal_Signed_Integer_Type_Definition =>
12215 if not Is_Signed_Integer_Type (Act_T) then
12216 Error_Msg_NE
12217 ("expect signed integer type in instantiation of&",
12218 Actual, Gen_T);
12219 Abandon_Instantiation (Actual);
12220 end if;
12222 Diagnose_Predicated_Actual;
12224 when N_Formal_Modular_Type_Definition =>
12225 if not Is_Modular_Integer_Type (Act_T) then
12226 Error_Msg_NE
12227 ("expect modular type in instantiation of &",
12228 Actual, Gen_T);
12229 Abandon_Instantiation (Actual);
12230 end if;
12232 Diagnose_Predicated_Actual;
12234 when N_Formal_Floating_Point_Definition =>
12235 if not Is_Floating_Point_Type (Act_T) then
12236 Error_Msg_NE
12237 ("expect float type in instantiation of &", Actual, Gen_T);
12238 Abandon_Instantiation (Actual);
12239 end if;
12241 when N_Formal_Ordinary_Fixed_Point_Definition =>
12242 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12243 Error_Msg_NE
12244 ("expect ordinary fixed point type in instantiation of &",
12245 Actual, Gen_T);
12246 Abandon_Instantiation (Actual);
12247 end if;
12249 when N_Formal_Decimal_Fixed_Point_Definition =>
12250 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12251 Error_Msg_NE
12252 ("expect decimal type in instantiation of &",
12253 Actual, Gen_T);
12254 Abandon_Instantiation (Actual);
12255 end if;
12257 when N_Array_Type_Definition =>
12258 Validate_Array_Type_Instance;
12260 when N_Access_To_Object_Definition =>
12261 Validate_Access_Type_Instance;
12263 when N_Access_Function_Definition |
12264 N_Access_Procedure_Definition =>
12265 Validate_Access_Subprogram_Instance;
12267 when N_Record_Definition =>
12268 Validate_Interface_Type_Instance;
12270 when N_Derived_Type_Definition =>
12271 Validate_Derived_Interface_Type_Instance;
12273 when others =>
12274 raise Program_Error;
12276 end case;
12277 end if;
12279 Subt := New_Copy (Gen_T);
12281 -- Use adjusted sloc of subtype name as the location for other nodes in
12282 -- the subtype declaration.
12284 Loc := Sloc (Subt);
12286 Decl_Node :=
12287 Make_Subtype_Declaration (Loc,
12288 Defining_Identifier => Subt,
12289 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12291 if Is_Private_Type (Act_T) then
12292 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12294 elsif Is_Access_Type (Act_T)
12295 and then Is_Private_Type (Designated_Type (Act_T))
12296 then
12297 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12298 end if;
12300 Decl_Nodes := New_List (Decl_Node);
12302 -- Flag actual derived types so their elaboration produces the
12303 -- appropriate renamings for the primitive operations of the ancestor.
12304 -- Flag actual for formal private types as well, to determine whether
12305 -- operations in the private part may override inherited operations.
12306 -- If the formal has an interface list, the ancestor is not the
12307 -- parent, but the analyzed formal that includes the interface
12308 -- operations of all its progenitors.
12310 -- Same treatment for formal private types, so we can check whether the
12311 -- type is tagged limited when validating derivations in the private
12312 -- part. (See AI05-096).
12314 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12315 if Present (Interface_List (Def)) then
12316 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12317 else
12318 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12319 end if;
12321 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12322 N_Formal_Incomplete_Type_Definition)
12323 then
12324 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12325 end if;
12327 -- If the actual is a synchronized type that implements an interface,
12328 -- the primitive operations are attached to the corresponding record,
12329 -- and we have to treat it as an additional generic actual, so that its
12330 -- primitive operations become visible in the instance. The task or
12331 -- protected type itself does not carry primitive operations.
12333 if Is_Concurrent_Type (Act_T)
12334 and then Is_Tagged_Type (Act_T)
12335 and then Present (Corresponding_Record_Type (Act_T))
12336 and then Present (Ancestor)
12337 and then Is_Interface (Ancestor)
12338 then
12339 declare
12340 Corr_Rec : constant Entity_Id :=
12341 Corresponding_Record_Type (Act_T);
12342 New_Corr : Entity_Id;
12343 Corr_Decl : Node_Id;
12345 begin
12346 New_Corr := Make_Temporary (Loc, 'S');
12347 Corr_Decl :=
12348 Make_Subtype_Declaration (Loc,
12349 Defining_Identifier => New_Corr,
12350 Subtype_Indication =>
12351 New_Occurrence_Of (Corr_Rec, Loc));
12352 Append_To (Decl_Nodes, Corr_Decl);
12354 if Ekind (Act_T) = E_Task_Type then
12355 Set_Ekind (Subt, E_Task_Subtype);
12356 else
12357 Set_Ekind (Subt, E_Protected_Subtype);
12358 end if;
12360 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12361 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12362 Set_Generic_Parent_Type (Decl_Node, Empty);
12363 end;
12364 end if;
12366 return Decl_Nodes;
12367 end Instantiate_Type;
12369 ---------------------
12370 -- Is_In_Main_Unit --
12371 ---------------------
12373 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12374 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12375 Current_Unit : Node_Id;
12377 begin
12378 if Unum = Main_Unit then
12379 return True;
12381 -- If the current unit is a subunit then it is either the main unit or
12382 -- is being compiled as part of the main unit.
12384 elsif Nkind (N) = N_Compilation_Unit then
12385 return Nkind (Unit (N)) = N_Subunit;
12386 end if;
12388 Current_Unit := Parent (N);
12389 while Present (Current_Unit)
12390 and then Nkind (Current_Unit) /= N_Compilation_Unit
12391 loop
12392 Current_Unit := Parent (Current_Unit);
12393 end loop;
12395 -- The instantiation node is in the main unit, or else the current node
12396 -- (perhaps as the result of nested instantiations) is in the main unit,
12397 -- or in the declaration of the main unit, which in this last case must
12398 -- be a body.
12400 return Unum = Main_Unit
12401 or else Current_Unit = Cunit (Main_Unit)
12402 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12403 or else (Present (Library_Unit (Current_Unit))
12404 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12405 end Is_In_Main_Unit;
12407 ----------------------------
12408 -- Load_Parent_Of_Generic --
12409 ----------------------------
12411 procedure Load_Parent_Of_Generic
12412 (N : Node_Id;
12413 Spec : Node_Id;
12414 Body_Optional : Boolean := False)
12416 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12417 Saved_Style_Check : constant Boolean := Style_Check;
12418 Saved_Warnings : constant Warning_Record := Save_Warnings;
12419 True_Parent : Node_Id;
12420 Inst_Node : Node_Id;
12421 OK : Boolean;
12422 Previous_Instances : constant Elist_Id := New_Elmt_List;
12424 procedure Collect_Previous_Instances (Decls : List_Id);
12425 -- Collect all instantiations in the given list of declarations, that
12426 -- precede the generic that we need to load. If the bodies of these
12427 -- instantiations are available, we must analyze them, to ensure that
12428 -- the public symbols generated are the same when the unit is compiled
12429 -- to generate code, and when it is compiled in the context of a unit
12430 -- that needs a particular nested instance. This process is applied to
12431 -- both package and subprogram instances.
12433 --------------------------------
12434 -- Collect_Previous_Instances --
12435 --------------------------------
12437 procedure Collect_Previous_Instances (Decls : List_Id) is
12438 Decl : Node_Id;
12440 begin
12441 Decl := First (Decls);
12442 while Present (Decl) loop
12443 if Sloc (Decl) >= Sloc (Inst_Node) then
12444 return;
12446 -- If Decl is an instantiation, then record it as requiring
12447 -- instantiation of the corresponding body, except if it is an
12448 -- abbreviated instantiation generated internally for conformance
12449 -- checking purposes only for the case of a formal package
12450 -- declared without a box (see Instantiate_Formal_Package). Such
12451 -- an instantiation does not generate any code (the actual code
12452 -- comes from actual) and thus does not need to be analyzed here.
12453 -- If the instantiation appears with a generic package body it is
12454 -- not analyzed here either.
12456 elsif Nkind (Decl) = N_Package_Instantiation
12457 and then not Is_Internal (Defining_Entity (Decl))
12458 then
12459 Append_Elmt (Decl, Previous_Instances);
12461 -- For a subprogram instantiation, omit instantiations intrinsic
12462 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12464 elsif Nkind_In (Decl, N_Function_Instantiation,
12465 N_Procedure_Instantiation)
12466 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12467 then
12468 Append_Elmt (Decl, Previous_Instances);
12470 elsif Nkind (Decl) = N_Package_Declaration then
12471 Collect_Previous_Instances
12472 (Visible_Declarations (Specification (Decl)));
12473 Collect_Previous_Instances
12474 (Private_Declarations (Specification (Decl)));
12476 -- Previous non-generic bodies may contain instances as well
12478 elsif Nkind (Decl) = N_Package_Body
12479 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12480 then
12481 Collect_Previous_Instances (Declarations (Decl));
12483 elsif Nkind (Decl) = N_Subprogram_Body
12484 and then not Acts_As_Spec (Decl)
12485 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12486 then
12487 Collect_Previous_Instances (Declarations (Decl));
12488 end if;
12490 Next (Decl);
12491 end loop;
12492 end Collect_Previous_Instances;
12494 -- Start of processing for Load_Parent_Of_Generic
12496 begin
12497 if not In_Same_Source_Unit (N, Spec)
12498 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12499 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12500 and then not Is_In_Main_Unit (Spec))
12501 then
12502 -- Find body of parent of spec, and analyze it. A special case arises
12503 -- when the parent is an instantiation, that is to say when we are
12504 -- currently instantiating a nested generic. In that case, there is
12505 -- no separate file for the body of the enclosing instance. Instead,
12506 -- the enclosing body must be instantiated as if it were a pending
12507 -- instantiation, in order to produce the body for the nested generic
12508 -- we require now. Note that in that case the generic may be defined
12509 -- in a package body, the instance defined in the same package body,
12510 -- and the original enclosing body may not be in the main unit.
12512 Inst_Node := Empty;
12514 True_Parent := Parent (Spec);
12515 while Present (True_Parent)
12516 and then Nkind (True_Parent) /= N_Compilation_Unit
12517 loop
12518 if Nkind (True_Parent) = N_Package_Declaration
12519 and then
12520 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12521 then
12522 -- Parent is a compilation unit that is an instantiation.
12523 -- Instantiation node has been replaced with package decl.
12525 Inst_Node := Original_Node (True_Parent);
12526 exit;
12528 elsif Nkind (True_Parent) = N_Package_Declaration
12529 and then Present (Generic_Parent (Specification (True_Parent)))
12530 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12531 then
12532 -- Parent is an instantiation within another specification.
12533 -- Declaration for instance has been inserted before original
12534 -- instantiation node. A direct link would be preferable?
12536 Inst_Node := Next (True_Parent);
12537 while Present (Inst_Node)
12538 and then Nkind (Inst_Node) /= N_Package_Instantiation
12539 loop
12540 Next (Inst_Node);
12541 end loop;
12543 -- If the instance appears within a generic, and the generic
12544 -- unit is defined within a formal package of the enclosing
12545 -- generic, there is no generic body available, and none
12546 -- needed. A more precise test should be used ???
12548 if No (Inst_Node) then
12549 return;
12550 end if;
12552 exit;
12554 else
12555 True_Parent := Parent (True_Parent);
12556 end if;
12557 end loop;
12559 -- Case where we are currently instantiating a nested generic
12561 if Present (Inst_Node) then
12562 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12564 -- Instantiation node and declaration of instantiated package
12565 -- were exchanged when only the declaration was needed.
12566 -- Restore instantiation node before proceeding with body.
12568 Set_Unit (Parent (True_Parent), Inst_Node);
12569 end if;
12571 -- Now complete instantiation of enclosing body, if it appears in
12572 -- some other unit. If it appears in the current unit, the body
12573 -- will have been instantiated already.
12575 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12577 -- We need to determine the expander mode to instantiate the
12578 -- enclosing body. Because the generic body we need may use
12579 -- global entities declared in the enclosing package (including
12580 -- aggregates) it is in general necessary to compile this body
12581 -- with expansion enabled, except if we are within a generic
12582 -- package, in which case the usual generic rule applies.
12584 declare
12585 Exp_Status : Boolean := True;
12586 Scop : Entity_Id;
12588 begin
12589 -- Loop through scopes looking for generic package
12591 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12592 while Present (Scop)
12593 and then Scop /= Standard_Standard
12594 loop
12595 if Ekind (Scop) = E_Generic_Package then
12596 Exp_Status := False;
12597 exit;
12598 end if;
12600 Scop := Scope (Scop);
12601 end loop;
12603 -- Collect previous instantiations in the unit that contains
12604 -- the desired generic.
12606 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12607 and then not Body_Optional
12608 then
12609 declare
12610 Decl : Elmt_Id;
12611 Info : Pending_Body_Info;
12612 Par : Node_Id;
12614 begin
12615 Par := Parent (Inst_Node);
12616 while Present (Par) loop
12617 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12618 Par := Parent (Par);
12619 end loop;
12621 pragma Assert (Present (Par));
12623 if Nkind (Par) = N_Package_Body then
12624 Collect_Previous_Instances (Declarations (Par));
12626 elsif Nkind (Par) = N_Package_Declaration then
12627 Collect_Previous_Instances
12628 (Visible_Declarations (Specification (Par)));
12629 Collect_Previous_Instances
12630 (Private_Declarations (Specification (Par)));
12632 else
12633 -- Enclosing unit is a subprogram body. In this
12634 -- case all instance bodies are processed in order
12635 -- and there is no need to collect them separately.
12637 null;
12638 end if;
12640 Decl := First_Elmt (Previous_Instances);
12641 while Present (Decl) loop
12642 Info :=
12643 (Inst_Node => Node (Decl),
12644 Act_Decl =>
12645 Instance_Spec (Node (Decl)),
12646 Expander_Status => Exp_Status,
12647 Current_Sem_Unit =>
12648 Get_Code_Unit (Sloc (Node (Decl))),
12649 Scope_Suppress => Scope_Suppress,
12650 Local_Suppress_Stack_Top =>
12651 Local_Suppress_Stack_Top,
12652 Version => Ada_Version,
12653 Version_Pragma => Ada_Version_Pragma,
12654 Warnings => Save_Warnings,
12655 SPARK_Mode => SPARK_Mode,
12656 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12658 -- Package instance
12661 Nkind (Node (Decl)) = N_Package_Instantiation
12662 then
12663 Instantiate_Package_Body
12664 (Info, Body_Optional => True);
12666 -- Subprogram instance
12668 else
12669 -- The instance_spec is the wrapper package,
12670 -- and the subprogram declaration is the last
12671 -- declaration in the wrapper.
12673 Info.Act_Decl :=
12674 Last
12675 (Visible_Declarations
12676 (Specification (Info.Act_Decl)));
12678 Instantiate_Subprogram_Body
12679 (Info, Body_Optional => True);
12680 end if;
12682 Next_Elmt (Decl);
12683 end loop;
12684 end;
12685 end if;
12687 Instantiate_Package_Body
12688 (Body_Info =>
12689 ((Inst_Node => Inst_Node,
12690 Act_Decl => True_Parent,
12691 Expander_Status => Exp_Status,
12692 Current_Sem_Unit => Get_Code_Unit
12693 (Sloc (Inst_Node)),
12694 Scope_Suppress => Scope_Suppress,
12695 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12696 Version => Ada_Version,
12697 Version_Pragma => Ada_Version_Pragma,
12698 Warnings => Save_Warnings,
12699 SPARK_Mode => SPARK_Mode,
12700 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12701 Body_Optional => Body_Optional);
12702 end;
12703 end if;
12705 -- Case where we are not instantiating a nested generic
12707 else
12708 Opt.Style_Check := False;
12709 Expander_Mode_Save_And_Set (True);
12710 Load_Needed_Body (Comp_Unit, OK);
12711 Opt.Style_Check := Saved_Style_Check;
12712 Restore_Warnings (Saved_Warnings);
12713 Expander_Mode_Restore;
12715 if not OK
12716 and then Unit_Requires_Body (Defining_Entity (Spec))
12717 and then not Body_Optional
12718 then
12719 declare
12720 Bname : constant Unit_Name_Type :=
12721 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12723 begin
12724 -- In CodePeer mode, the missing body may make the analysis
12725 -- incomplete, but we do not treat it as fatal.
12727 if CodePeer_Mode then
12728 return;
12730 else
12731 Error_Msg_Unit_1 := Bname;
12732 Error_Msg_N ("this instantiation requires$!", N);
12733 Error_Msg_File_1 :=
12734 Get_File_Name (Bname, Subunit => False);
12735 Error_Msg_N ("\but file{ was not found!", N);
12736 raise Unrecoverable_Error;
12737 end if;
12738 end;
12739 end if;
12740 end if;
12741 end if;
12743 -- If loading parent of the generic caused an instantiation circularity,
12744 -- we abandon compilation at this point, because otherwise in some cases
12745 -- we get into trouble with infinite recursions after this point.
12747 if Circularity_Detected then
12748 raise Unrecoverable_Error;
12749 end if;
12750 end Load_Parent_Of_Generic;
12752 ---------------------------------
12753 -- Map_Formal_Package_Entities --
12754 ---------------------------------
12756 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
12757 E1 : Entity_Id;
12758 E2 : Entity_Id;
12760 begin
12761 Set_Instance_Of (Form, Act);
12763 -- Traverse formal and actual package to map the corresponding entities.
12764 -- We skip over internal entities that may be generated during semantic
12765 -- analysis, and find the matching entities by name, given that they
12766 -- must appear in the same order.
12768 E1 := First_Entity (Form);
12769 E2 := First_Entity (Act);
12770 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
12771 -- Could this test be a single condition??? Seems like it could, and
12772 -- isn't FPE (Form) a constant anyway???
12774 if not Is_Internal (E1)
12775 and then Present (Parent (E1))
12776 and then not Is_Class_Wide_Type (E1)
12777 and then not Is_Internal_Name (Chars (E1))
12778 then
12779 while Present (E2) and then Chars (E2) /= Chars (E1) loop
12780 Next_Entity (E2);
12781 end loop;
12783 if No (E2) then
12784 exit;
12785 else
12786 Set_Instance_Of (E1, E2);
12788 if Is_Type (E1) and then Is_Tagged_Type (E2) then
12789 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
12790 end if;
12792 if Is_Constrained (E1) then
12793 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
12794 end if;
12796 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
12797 Map_Formal_Package_Entities (E1, E2);
12798 end if;
12799 end if;
12800 end if;
12802 Next_Entity (E1);
12803 end loop;
12804 end Map_Formal_Package_Entities;
12806 -----------------------
12807 -- Move_Freeze_Nodes --
12808 -----------------------
12810 procedure Move_Freeze_Nodes
12811 (Out_Of : Entity_Id;
12812 After : Node_Id;
12813 L : List_Id)
12815 Decl : Node_Id;
12816 Next_Decl : Node_Id;
12817 Next_Node : Node_Id := After;
12818 Spec : Node_Id;
12820 function Is_Outer_Type (T : Entity_Id) return Boolean;
12821 -- Check whether entity is declared in a scope external to that of the
12822 -- generic unit.
12824 -------------------
12825 -- Is_Outer_Type --
12826 -------------------
12828 function Is_Outer_Type (T : Entity_Id) return Boolean is
12829 Scop : Entity_Id := Scope (T);
12831 begin
12832 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
12833 return True;
12835 else
12836 while Scop /= Standard_Standard loop
12837 if Scop = Out_Of then
12838 return False;
12839 else
12840 Scop := Scope (Scop);
12841 end if;
12842 end loop;
12844 return True;
12845 end if;
12846 end Is_Outer_Type;
12848 -- Start of processing for Move_Freeze_Nodes
12850 begin
12851 if No (L) then
12852 return;
12853 end if;
12855 -- First remove the freeze nodes that may appear before all other
12856 -- declarations.
12858 Decl := First (L);
12859 while Present (Decl)
12860 and then Nkind (Decl) = N_Freeze_Entity
12861 and then Is_Outer_Type (Entity (Decl))
12862 loop
12863 Decl := Remove_Head (L);
12864 Insert_After (Next_Node, Decl);
12865 Set_Analyzed (Decl, False);
12866 Next_Node := Decl;
12867 Decl := First (L);
12868 end loop;
12870 -- Next scan the list of declarations and remove each freeze node that
12871 -- appears ahead of the current node.
12873 while Present (Decl) loop
12874 while Present (Next (Decl))
12875 and then Nkind (Next (Decl)) = N_Freeze_Entity
12876 and then Is_Outer_Type (Entity (Next (Decl)))
12877 loop
12878 Next_Decl := Remove_Next (Decl);
12879 Insert_After (Next_Node, Next_Decl);
12880 Set_Analyzed (Next_Decl, False);
12881 Next_Node := Next_Decl;
12882 end loop;
12884 -- If the declaration is a nested package or concurrent type, then
12885 -- recurse. Nested generic packages will have been processed from the
12886 -- inside out.
12888 case Nkind (Decl) is
12889 when N_Package_Declaration =>
12890 Spec := Specification (Decl);
12892 when N_Task_Type_Declaration =>
12893 Spec := Task_Definition (Decl);
12895 when N_Protected_Type_Declaration =>
12896 Spec := Protected_Definition (Decl);
12898 when others =>
12899 Spec := Empty;
12900 end case;
12902 if Present (Spec) then
12903 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
12904 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
12905 end if;
12907 Next (Decl);
12908 end loop;
12909 end Move_Freeze_Nodes;
12911 ----------------
12912 -- Next_Assoc --
12913 ----------------
12915 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
12916 begin
12917 return Generic_Renamings.Table (E).Next_In_HTable;
12918 end Next_Assoc;
12920 ------------------------
12921 -- Preanalyze_Actuals --
12922 ------------------------
12924 procedure Preanalyze_Actuals (N : Node_Id) is
12925 Assoc : Node_Id;
12926 Act : Node_Id;
12927 Errs : constant Int := Serious_Errors_Detected;
12929 Cur : Entity_Id := Empty;
12930 -- Current homograph of the instance name
12932 Vis : Boolean;
12933 -- Saved visibility status of the current homograph
12935 begin
12936 Assoc := First (Generic_Associations (N));
12938 -- If the instance is a child unit, its name may hide an outer homonym,
12939 -- so make it invisible to perform name resolution on the actuals.
12941 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
12942 and then Present
12943 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
12944 then
12945 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
12947 if Is_Compilation_Unit (Cur) then
12948 Vis := Is_Immediately_Visible (Cur);
12949 Set_Is_Immediately_Visible (Cur, False);
12950 else
12951 Cur := Empty;
12952 end if;
12953 end if;
12955 while Present (Assoc) loop
12956 if Nkind (Assoc) /= N_Others_Choice then
12957 Act := Explicit_Generic_Actual_Parameter (Assoc);
12959 -- Within a nested instantiation, a defaulted actual is an empty
12960 -- association, so nothing to analyze. If the subprogram actual
12961 -- is an attribute, analyze prefix only, because actual is not a
12962 -- complete attribute reference.
12964 -- If actual is an allocator, analyze expression only. The full
12965 -- analysis can generate code, and if instance is a compilation
12966 -- unit we have to wait until the package instance is installed
12967 -- to have a proper place to insert this code.
12969 -- String literals may be operators, but at this point we do not
12970 -- know whether the actual is a formal subprogram or a string.
12972 if No (Act) then
12973 null;
12975 elsif Nkind (Act) = N_Attribute_Reference then
12976 Analyze (Prefix (Act));
12978 elsif Nkind (Act) = N_Explicit_Dereference then
12979 Analyze (Prefix (Act));
12981 elsif Nkind (Act) = N_Allocator then
12982 declare
12983 Expr : constant Node_Id := Expression (Act);
12985 begin
12986 if Nkind (Expr) = N_Subtype_Indication then
12987 Analyze (Subtype_Mark (Expr));
12989 -- Analyze separately each discriminant constraint, when
12990 -- given with a named association.
12992 declare
12993 Constr : Node_Id;
12995 begin
12996 Constr := First (Constraints (Constraint (Expr)));
12997 while Present (Constr) loop
12998 if Nkind (Constr) = N_Discriminant_Association then
12999 Analyze (Expression (Constr));
13000 else
13001 Analyze (Constr);
13002 end if;
13004 Next (Constr);
13005 end loop;
13006 end;
13008 else
13009 Analyze (Expr);
13010 end if;
13011 end;
13013 elsif Nkind (Act) /= N_Operator_Symbol then
13014 Analyze (Act);
13015 end if;
13017 if Errs /= Serious_Errors_Detected then
13019 -- Do a minimal analysis of the generic, to prevent spurious
13020 -- warnings complaining about the generic being unreferenced,
13021 -- before abandoning the instantiation.
13023 Analyze (Name (N));
13025 if Is_Entity_Name (Name (N))
13026 and then Etype (Name (N)) /= Any_Type
13027 then
13028 Generate_Reference (Entity (Name (N)), Name (N));
13029 Set_Is_Instantiated (Entity (Name (N)));
13030 end if;
13032 if Present (Cur) then
13034 -- For the case of a child instance hiding an outer homonym,
13035 -- provide additional warning which might explain the error.
13037 Set_Is_Immediately_Visible (Cur, Vis);
13038 Error_Msg_NE
13039 ("& hides outer unit with the same name??",
13040 N, Defining_Unit_Name (N));
13041 end if;
13043 Abandon_Instantiation (Act);
13044 end if;
13045 end if;
13047 Next (Assoc);
13048 end loop;
13050 if Present (Cur) then
13051 Set_Is_Immediately_Visible (Cur, Vis);
13052 end if;
13053 end Preanalyze_Actuals;
13055 -------------------
13056 -- Remove_Parent --
13057 -------------------
13059 procedure Remove_Parent (In_Body : Boolean := False) is
13060 S : Entity_Id := Current_Scope;
13061 -- S is the scope containing the instantiation just completed. The scope
13062 -- stack contains the parent instances of the instantiation, followed by
13063 -- the original S.
13065 Cur_P : Entity_Id;
13066 E : Entity_Id;
13067 P : Entity_Id;
13068 Hidden : Elmt_Id;
13070 begin
13071 -- After child instantiation is complete, remove from scope stack the
13072 -- extra copy of the current scope, and then remove parent instances.
13074 if not In_Body then
13075 Pop_Scope;
13077 while Current_Scope /= S loop
13078 P := Current_Scope;
13079 End_Package_Scope (Current_Scope);
13081 if In_Open_Scopes (P) then
13082 E := First_Entity (P);
13083 while Present (E) loop
13084 Set_Is_Immediately_Visible (E, True);
13085 Next_Entity (E);
13086 end loop;
13088 -- If instantiation is declared in a block, it is the enclosing
13089 -- scope that might be a parent instance. Note that only one
13090 -- block can be involved, because the parent instances have
13091 -- been installed within it.
13093 if Ekind (P) = E_Block then
13094 Cur_P := Scope (P);
13095 else
13096 Cur_P := P;
13097 end if;
13099 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13100 -- We are within an instance of some sibling. Retain
13101 -- visibility of parent, for proper subsequent cleanup, and
13102 -- reinstall private declarations as well.
13104 Set_In_Private_Part (P);
13105 Install_Private_Declarations (P);
13106 end if;
13108 -- If the ultimate parent is a top-level unit recorded in
13109 -- Instance_Parent_Unit, then reset its visibility to what it was
13110 -- before instantiation. (It's not clear what the purpose is of
13111 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13112 -- present before the ultimate parent test was added.???)
13114 elsif not In_Open_Scopes (Scope (P))
13115 or else (P = Instance_Parent_Unit
13116 and then not Parent_Unit_Visible)
13117 then
13118 Set_Is_Immediately_Visible (P, False);
13120 -- If the current scope is itself an instantiation of a generic
13121 -- nested within P, and we are in the private part of body of this
13122 -- instantiation, restore the full views of P, that were removed
13123 -- in End_Package_Scope above. This obscure case can occur when a
13124 -- subunit of a generic contains an instance of a child unit of
13125 -- its generic parent unit.
13127 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13128 declare
13129 Par : constant Entity_Id :=
13130 Generic_Parent (Package_Specification (S));
13131 begin
13132 if Present (Par)
13133 and then P = Scope (Par)
13134 and then (In_Package_Body (S) or else In_Private_Part (S))
13135 then
13136 Set_In_Private_Part (P);
13137 Install_Private_Declarations (P);
13138 end if;
13139 end;
13140 end if;
13141 end loop;
13143 -- Reset visibility of entities in the enclosing scope
13145 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13147 Hidden := First_Elmt (Hidden_Entities);
13148 while Present (Hidden) loop
13149 Set_Is_Immediately_Visible (Node (Hidden), True);
13150 Next_Elmt (Hidden);
13151 end loop;
13153 else
13154 -- Each body is analyzed separately, and there is no context that
13155 -- needs preserving from one body instance to the next, so remove all
13156 -- parent scopes that have been installed.
13158 while Present (S) loop
13159 End_Package_Scope (S);
13160 Set_Is_Immediately_Visible (S, False);
13161 S := Current_Scope;
13162 exit when S = Standard_Standard;
13163 end loop;
13164 end if;
13165 end Remove_Parent;
13167 -----------------
13168 -- Restore_Env --
13169 -----------------
13171 procedure Restore_Env is
13172 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13174 begin
13175 if No (Current_Instantiated_Parent.Act_Id) then
13176 -- Restore environment after subprogram inlining
13178 Restore_Private_Views (Empty);
13179 end if;
13181 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13182 Exchanged_Views := Saved.Exchanged_Views;
13183 Hidden_Entities := Saved.Hidden_Entities;
13184 Current_Sem_Unit := Saved.Current_Sem_Unit;
13185 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13186 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13188 Restore_Opt_Config_Switches (Saved.Switches);
13190 Instance_Envs.Decrement_Last;
13191 end Restore_Env;
13193 ---------------------------
13194 -- Restore_Private_Views --
13195 ---------------------------
13197 procedure Restore_Private_Views
13198 (Pack_Id : Entity_Id;
13199 Is_Package : Boolean := True)
13201 M : Elmt_Id;
13202 E : Entity_Id;
13203 Typ : Entity_Id;
13204 Dep_Elmt : Elmt_Id;
13205 Dep_Typ : Node_Id;
13207 procedure Restore_Nested_Formal (Formal : Entity_Id);
13208 -- Hide the generic formals of formal packages declared with box which
13209 -- were reachable in the current instantiation.
13211 ---------------------------
13212 -- Restore_Nested_Formal --
13213 ---------------------------
13215 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13216 Ent : Entity_Id;
13218 begin
13219 if Present (Renamed_Object (Formal))
13220 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13221 then
13222 return;
13224 elsif Present (Associated_Formal_Package (Formal)) then
13225 Ent := First_Entity (Formal);
13226 while Present (Ent) loop
13227 exit when Ekind (Ent) = E_Package
13228 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13230 Set_Is_Hidden (Ent);
13231 Set_Is_Potentially_Use_Visible (Ent, False);
13233 -- If package, then recurse
13235 if Ekind (Ent) = E_Package then
13236 Restore_Nested_Formal (Ent);
13237 end if;
13239 Next_Entity (Ent);
13240 end loop;
13241 end if;
13242 end Restore_Nested_Formal;
13244 -- Start of processing for Restore_Private_Views
13246 begin
13247 M := First_Elmt (Exchanged_Views);
13248 while Present (M) loop
13249 Typ := Node (M);
13251 -- Subtypes of types whose views have been exchanged, and that are
13252 -- defined within the instance, were not on the Private_Dependents
13253 -- list on entry to the instance, so they have to be exchanged
13254 -- explicitly now, in order to remain consistent with the view of the
13255 -- parent type.
13257 if Ekind_In (Typ, E_Private_Type,
13258 E_Limited_Private_Type,
13259 E_Record_Type_With_Private)
13260 then
13261 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13262 while Present (Dep_Elmt) loop
13263 Dep_Typ := Node (Dep_Elmt);
13265 if Scope (Dep_Typ) = Pack_Id
13266 and then Present (Full_View (Dep_Typ))
13267 then
13268 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13269 Exchange_Declarations (Dep_Typ);
13270 end if;
13272 Next_Elmt (Dep_Elmt);
13273 end loop;
13274 end if;
13276 Exchange_Declarations (Node (M));
13277 Next_Elmt (M);
13278 end loop;
13280 if No (Pack_Id) then
13281 return;
13282 end if;
13284 -- Make the generic formal parameters private, and make the formal types
13285 -- into subtypes of the actuals again.
13287 E := First_Entity (Pack_Id);
13288 while Present (E) loop
13289 Set_Is_Hidden (E, True);
13291 if Is_Type (E)
13292 and then Nkind (Parent (E)) = N_Subtype_Declaration
13293 then
13294 -- If the actual for E is itself a generic actual type from
13295 -- an enclosing instance, E is still a generic actual type
13296 -- outside of the current instance. This matter when resolving
13297 -- an overloaded call that may be ambiguous in the enclosing
13298 -- instance, when two of its actuals coincide.
13300 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13301 and then Is_Generic_Actual_Type
13302 (Entity (Subtype_Indication (Parent (E))))
13303 then
13304 null;
13305 else
13306 Set_Is_Generic_Actual_Type (E, False);
13307 end if;
13309 -- An unusual case of aliasing: the actual may also be directly
13310 -- visible in the generic, and be private there, while it is fully
13311 -- visible in the context of the instance. The internal subtype
13312 -- is private in the instance but has full visibility like its
13313 -- parent in the enclosing scope. This enforces the invariant that
13314 -- the privacy status of all private dependents of a type coincide
13315 -- with that of the parent type. This can only happen when a
13316 -- generic child unit is instantiated within a sibling.
13318 if Is_Private_Type (E)
13319 and then not Is_Private_Type (Etype (E))
13320 then
13321 Exchange_Declarations (E);
13322 end if;
13324 elsif Ekind (E) = E_Package then
13326 -- The end of the renaming list is the renaming of the generic
13327 -- package itself. If the instance is a subprogram, all entities
13328 -- in the corresponding package are renamings. If this entity is
13329 -- a formal package, make its own formals private as well. The
13330 -- actual in this case is itself the renaming of an instantiation.
13331 -- If the entity is not a package renaming, it is the entity
13332 -- created to validate formal package actuals: ignore it.
13334 -- If the actual is itself a formal package for the enclosing
13335 -- generic, or the actual for such a formal package, it remains
13336 -- visible on exit from the instance, and therefore nothing needs
13337 -- to be done either, except to keep it accessible.
13339 if Is_Package and then Renamed_Object (E) = Pack_Id then
13340 exit;
13342 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13343 null;
13345 elsif
13346 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13347 then
13348 Set_Is_Hidden (E, False);
13350 else
13351 declare
13352 Act_P : constant Entity_Id := Renamed_Object (E);
13353 Id : Entity_Id;
13355 begin
13356 Id := First_Entity (Act_P);
13357 while Present (Id)
13358 and then Id /= First_Private_Entity (Act_P)
13359 loop
13360 exit when Ekind (Id) = E_Package
13361 and then Renamed_Object (Id) = Act_P;
13363 Set_Is_Hidden (Id, True);
13364 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13366 if Ekind (Id) = E_Package then
13367 Restore_Nested_Formal (Id);
13368 end if;
13370 Next_Entity (Id);
13371 end loop;
13372 end;
13373 end if;
13374 end if;
13376 Next_Entity (E);
13377 end loop;
13378 end Restore_Private_Views;
13380 --------------
13381 -- Save_Env --
13382 --------------
13384 procedure Save_Env
13385 (Gen_Unit : Entity_Id;
13386 Act_Unit : Entity_Id)
13388 begin
13389 Init_Env;
13390 Set_Instance_Env (Gen_Unit, Act_Unit);
13391 end Save_Env;
13393 ----------------------------
13394 -- Save_Global_References --
13395 ----------------------------
13397 procedure Save_Global_References (N : Node_Id) is
13398 Gen_Scope : Entity_Id;
13399 E : Entity_Id;
13400 N2 : Node_Id;
13402 function Is_Global (E : Entity_Id) return Boolean;
13403 -- Check whether entity is defined outside of generic unit. Examine the
13404 -- scope of an entity, and the scope of the scope, etc, until we find
13405 -- either Standard, in which case the entity is global, or the generic
13406 -- unit itself, which indicates that the entity is local. If the entity
13407 -- is the generic unit itself, as in the case of a recursive call, or
13408 -- the enclosing generic unit, if different from the current scope, then
13409 -- it is local as well, because it will be replaced at the point of
13410 -- instantiation. On the other hand, if it is a reference to a child
13411 -- unit of a common ancestor, which appears in an instantiation, it is
13412 -- global because it is used to denote a specific compilation unit at
13413 -- the time the instantiations will be analyzed.
13415 procedure Reset_Entity (N : Node_Id);
13416 -- Save semantic information on global entity so that it is not resolved
13417 -- again at instantiation time.
13419 procedure Save_Entity_Descendants (N : Node_Id);
13420 -- Apply Save_Global_References to the two syntactic descendants of
13421 -- non-terminal nodes that carry an Associated_Node and are processed
13422 -- through Reset_Entity. Once the global entity (if any) has been
13423 -- captured together with its type, only two syntactic descendants need
13424 -- to be traversed to complete the processing of the tree rooted at N.
13425 -- This applies to Selected_Components, Expanded_Names, and to Operator
13426 -- nodes. N can also be a character literal, identifier, or operator
13427 -- symbol node, but the call has no effect in these cases.
13429 procedure Save_Global_Defaults (N1, N2 : Node_Id);
13430 -- Default actuals in nested instances must be handled specially
13431 -- because there is no link to them from the original tree. When an
13432 -- actual subprogram is given by a default, we add an explicit generic
13433 -- association for it in the instantiation node. When we save the
13434 -- global references on the name of the instance, we recover the list
13435 -- of generic associations, and add an explicit one to the original
13436 -- generic tree, through which a global actual can be preserved.
13437 -- Similarly, if a child unit is instantiated within a sibling, in the
13438 -- context of the parent, we must preserve the identifier of the parent
13439 -- so that it can be properly resolved in a subsequent instantiation.
13441 procedure Save_Global_Descendant (D : Union_Id);
13442 -- Apply Save_Global_References recursively to the descendents of the
13443 -- current node.
13445 procedure Save_References (N : Node_Id);
13446 -- This is the recursive procedure that does the work, once the
13447 -- enclosing generic scope has been established.
13449 ---------------
13450 -- Is_Global --
13451 ---------------
13453 function Is_Global (E : Entity_Id) return Boolean is
13454 Se : Entity_Id;
13456 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13457 -- Determine whether the parent node of a reference to a child unit
13458 -- denotes an instantiation or a formal package, in which case the
13459 -- reference to the child unit is global, even if it appears within
13460 -- the current scope (e.g. when the instance appears within the body
13461 -- of an ancestor).
13463 ----------------------
13464 -- Is_Instance_Node --
13465 ----------------------
13467 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13468 begin
13469 return Nkind (Decl) in N_Generic_Instantiation
13470 or else
13471 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13472 end Is_Instance_Node;
13474 -- Start of processing for Is_Global
13476 begin
13477 if E = Gen_Scope then
13478 return False;
13480 elsif E = Standard_Standard then
13481 return True;
13483 elsif Is_Child_Unit (E)
13484 and then (Is_Instance_Node (Parent (N2))
13485 or else (Nkind (Parent (N2)) = N_Expanded_Name
13486 and then N2 = Selector_Name (Parent (N2))
13487 and then
13488 Is_Instance_Node (Parent (Parent (N2)))))
13489 then
13490 return True;
13492 else
13493 Se := Scope (E);
13494 while Se /= Gen_Scope loop
13495 if Se = Standard_Standard then
13496 return True;
13497 else
13498 Se := Scope (Se);
13499 end if;
13500 end loop;
13502 return False;
13503 end if;
13504 end Is_Global;
13506 ------------------
13507 -- Reset_Entity --
13508 ------------------
13510 procedure Reset_Entity (N : Node_Id) is
13512 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13513 -- If the type of N2 is global to the generic unit, save the type in
13514 -- the generic node. Just as we perform name capture for explicit
13515 -- references within the generic, we must capture the global types
13516 -- of local entities because they may participate in resolution in
13517 -- the instance.
13519 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13520 -- Find the ultimate ancestor of the current unit. If it is not a
13521 -- generic unit, then the name of the current unit in the prefix of
13522 -- an expanded name must be replaced with its generic homonym to
13523 -- ensure that it will be properly resolved in an instance.
13525 ---------------------
13526 -- Set_Global_Type --
13527 ---------------------
13529 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13530 Typ : constant Entity_Id := Etype (N2);
13532 begin
13533 Set_Etype (N, Typ);
13535 if Entity (N) /= N2
13536 and then Has_Private_View (Entity (N))
13537 then
13538 -- If the entity of N is not the associated node, this is a
13539 -- nested generic and it has an associated node as well, whose
13540 -- type is already the full view (see below). Indicate that the
13541 -- original node has a private view.
13543 Set_Has_Private_View (N);
13544 end if;
13546 -- If not a private type, nothing else to do
13548 if not Is_Private_Type (Typ) then
13549 if Is_Array_Type (Typ)
13550 and then Is_Private_Type (Component_Type (Typ))
13551 then
13552 Set_Has_Private_View (N);
13553 end if;
13555 -- If it is a derivation of a private type in a context where no
13556 -- full view is needed, nothing to do either.
13558 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13559 null;
13561 -- Otherwise mark the type for flipping and use the full view when
13562 -- available.
13564 else
13565 Set_Has_Private_View (N);
13567 if Present (Full_View (Typ)) then
13568 Set_Etype (N2, Full_View (Typ));
13569 end if;
13570 end if;
13571 end Set_Global_Type;
13573 ------------------
13574 -- Top_Ancestor --
13575 ------------------
13577 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13578 Par : Entity_Id;
13580 begin
13581 Par := E;
13582 while Is_Child_Unit (Par) loop
13583 Par := Scope (Par);
13584 end loop;
13586 return Par;
13587 end Top_Ancestor;
13589 -- Start of processing for Reset_Entity
13591 begin
13592 N2 := Get_Associated_Node (N);
13593 E := Entity (N2);
13595 if Present (E) then
13597 -- If the node is an entry call to an entry in an enclosing task,
13598 -- it is rewritten as a selected component. No global entity to
13599 -- preserve in this case, since the expansion will be redone in
13600 -- the instance.
13602 if not Nkind_In (E, N_Defining_Identifier,
13603 N_Defining_Character_Literal,
13604 N_Defining_Operator_Symbol)
13605 then
13606 Set_Associated_Node (N, Empty);
13607 Set_Etype (N, Empty);
13608 return;
13609 end if;
13611 -- If the entity is an itype created as a subtype of an access
13612 -- type with a null exclusion restore source entity for proper
13613 -- visibility. The itype will be created anew in the instance.
13615 if Is_Itype (E)
13616 and then Ekind (E) = E_Access_Subtype
13617 and then Is_Entity_Name (N)
13618 and then Chars (Etype (E)) = Chars (N)
13619 then
13620 E := Etype (E);
13621 Set_Entity (N2, E);
13622 Set_Etype (N2, E);
13623 end if;
13625 if Is_Global (E) then
13627 -- If the entity is a package renaming that is the prefix of
13628 -- an expanded name, it has been rewritten as the renamed
13629 -- package, which is necessary semantically but complicates
13630 -- ASIS tree traversal, so we recover the original entity to
13631 -- expose the renaming. Take into account that the context may
13632 -- be a nested generic, that the original node may itself have
13633 -- an associated node that had better be an entity, and that
13634 -- the current node is still a selected component.
13636 if Ekind (E) = E_Package
13637 and then Nkind (N) = N_Selected_Component
13638 and then Nkind (Parent (N)) = N_Expanded_Name
13639 and then Present (Original_Node (N2))
13640 and then Is_Entity_Name (Original_Node (N2))
13641 and then Present (Entity (Original_Node (N2)))
13642 then
13643 if Is_Global (Entity (Original_Node (N2))) then
13644 N2 := Original_Node (N2);
13645 Set_Associated_Node (N, N2);
13646 Set_Global_Type (N, N2);
13648 else
13649 -- Renaming is local, and will be resolved in instance
13651 Set_Associated_Node (N, Empty);
13652 Set_Etype (N, Empty);
13653 end if;
13655 else
13656 Set_Global_Type (N, N2);
13657 end if;
13659 elsif Nkind (N) = N_Op_Concat
13660 and then Is_Generic_Type (Etype (N2))
13661 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13662 or else
13663 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13664 and then Is_Intrinsic_Subprogram (E)
13665 then
13666 null;
13668 else
13669 -- Entity is local. Mark generic node as unresolved.
13670 -- Note that now it does not have an entity.
13672 Set_Associated_Node (N, Empty);
13673 Set_Etype (N, Empty);
13674 end if;
13676 if Nkind (Parent (N)) in N_Generic_Instantiation
13677 and then N = Name (Parent (N))
13678 then
13679 Save_Global_Defaults (Parent (N), Parent (N2));
13680 end if;
13682 elsif Nkind (Parent (N)) = N_Selected_Component
13683 and then Nkind (Parent (N2)) = N_Expanded_Name
13684 then
13685 if Is_Global (Entity (Parent (N2))) then
13686 Change_Selected_Component_To_Expanded_Name (Parent (N));
13687 Set_Associated_Node (Parent (N), Parent (N2));
13688 Set_Global_Type (Parent (N), Parent (N2));
13689 Save_Entity_Descendants (N);
13691 -- If this is a reference to the current generic entity, replace
13692 -- by the name of the generic homonym of the current package. This
13693 -- is because in an instantiation Par.P.Q will not resolve to the
13694 -- name of the instance, whose enclosing scope is not necessarily
13695 -- Par. We use the generic homonym rather that the name of the
13696 -- generic itself because it may be hidden by a local declaration.
13698 elsif In_Open_Scopes (Entity (Parent (N2)))
13699 and then not
13700 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13701 then
13702 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13703 Rewrite (Parent (N),
13704 Make_Identifier (Sloc (N),
13705 Chars =>
13706 Chars (Generic_Homonym (Entity (Parent (N2))))));
13707 else
13708 Rewrite (Parent (N),
13709 Make_Identifier (Sloc (N),
13710 Chars => Chars (Selector_Name (Parent (N2)))));
13711 end if;
13712 end if;
13714 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13715 and then Parent (N) = Name (Parent (Parent (N)))
13716 then
13717 Save_Global_Defaults
13718 (Parent (Parent (N)), Parent (Parent ((N2))));
13719 end if;
13721 -- A selected component may denote a static constant that has been
13722 -- folded. If the static constant is global to the generic, capture
13723 -- its value. Otherwise the folding will happen in any instantiation.
13725 elsif Nkind (Parent (N)) = N_Selected_Component
13726 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13727 then
13728 if Present (Entity (Original_Node (Parent (N2))))
13729 and then Is_Global (Entity (Original_Node (Parent (N2))))
13730 then
13731 Rewrite (Parent (N), New_Copy (Parent (N2)));
13732 Set_Analyzed (Parent (N), False);
13734 else
13735 null;
13736 end if;
13738 -- A selected component may be transformed into a parameterless
13739 -- function call. If the called entity is global, rewrite the node
13740 -- appropriately, i.e. as an extended name for the global entity.
13742 elsif Nkind (Parent (N)) = N_Selected_Component
13743 and then Nkind (Parent (N2)) = N_Function_Call
13744 and then N = Selector_Name (Parent (N))
13745 then
13746 if No (Parameter_Associations (Parent (N2))) then
13747 if Is_Global (Entity (Name (Parent (N2)))) then
13748 Change_Selected_Component_To_Expanded_Name (Parent (N));
13749 Set_Associated_Node (Parent (N), Name (Parent (N2)));
13750 Set_Global_Type (Parent (N), Name (Parent (N2)));
13751 Save_Entity_Descendants (N);
13753 else
13754 Set_Is_Prefixed_Call (Parent (N));
13755 Set_Associated_Node (N, Empty);
13756 Set_Etype (N, Empty);
13757 end if;
13759 -- In Ada 2005, X.F may be a call to a primitive operation,
13760 -- rewritten as F (X). This rewriting will be done again in an
13761 -- instance, so keep the original node. Global entities will be
13762 -- captured as for other constructs. Indicate that this must
13763 -- resolve as a call, to prevent accidental overloading in the
13764 -- instance, if both a component and a primitive operation appear
13765 -- as candidates.
13767 else
13768 Set_Is_Prefixed_Call (Parent (N));
13769 end if;
13771 -- Entity is local. Reset in generic unit, so that node is resolved
13772 -- anew at the point of instantiation.
13774 else
13775 Set_Associated_Node (N, Empty);
13776 Set_Etype (N, Empty);
13777 end if;
13778 end Reset_Entity;
13780 -----------------------------
13781 -- Save_Entity_Descendants --
13782 -----------------------------
13784 procedure Save_Entity_Descendants (N : Node_Id) is
13785 begin
13786 case Nkind (N) is
13787 when N_Binary_Op =>
13788 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
13789 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13791 when N_Unary_Op =>
13792 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
13794 when N_Expanded_Name | N_Selected_Component =>
13795 Save_Global_Descendant (Union_Id (Prefix (N)));
13796 Save_Global_Descendant (Union_Id (Selector_Name (N)));
13798 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
13799 null;
13801 when others =>
13802 raise Program_Error;
13803 end case;
13804 end Save_Entity_Descendants;
13806 --------------------------
13807 -- Save_Global_Defaults --
13808 --------------------------
13810 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
13811 Loc : constant Source_Ptr := Sloc (N1);
13812 Assoc2 : constant List_Id := Generic_Associations (N2);
13813 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
13814 Assoc1 : List_Id;
13815 Act1 : Node_Id;
13816 Act2 : Node_Id;
13817 Def : Node_Id;
13818 Ndec : Node_Id;
13819 Subp : Entity_Id;
13820 Actual : Entity_Id;
13822 begin
13823 Assoc1 := Generic_Associations (N1);
13825 if Present (Assoc1) then
13826 Act1 := First (Assoc1);
13827 else
13828 Act1 := Empty;
13829 Set_Generic_Associations (N1, New_List);
13830 Assoc1 := Generic_Associations (N1);
13831 end if;
13833 if Present (Assoc2) then
13834 Act2 := First (Assoc2);
13835 else
13836 return;
13837 end if;
13839 while Present (Act1) and then Present (Act2) loop
13840 Next (Act1);
13841 Next (Act2);
13842 end loop;
13844 -- Find the associations added for default subprograms
13846 if Present (Act2) then
13847 while Nkind (Act2) /= N_Generic_Association
13848 or else No (Entity (Selector_Name (Act2)))
13849 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
13850 loop
13851 Next (Act2);
13852 end loop;
13854 -- Add a similar association if the default is global. The
13855 -- renaming declaration for the actual has been analyzed, and
13856 -- its alias is the program it renames. Link the actual in the
13857 -- original generic tree with the node in the analyzed tree.
13859 while Present (Act2) loop
13860 Subp := Entity (Selector_Name (Act2));
13861 Def := Explicit_Generic_Actual_Parameter (Act2);
13863 -- Following test is defence against rubbish errors
13865 if No (Alias (Subp)) then
13866 return;
13867 end if;
13869 -- Retrieve the resolved actual from the renaming declaration
13870 -- created for the instantiated formal.
13872 Actual := Entity (Name (Parent (Parent (Subp))));
13873 Set_Entity (Def, Actual);
13874 Set_Etype (Def, Etype (Actual));
13876 if Is_Global (Actual) then
13877 Ndec :=
13878 Make_Generic_Association (Loc,
13879 Selector_Name => New_Occurrence_Of (Subp, Loc),
13880 Explicit_Generic_Actual_Parameter =>
13881 New_Occurrence_Of (Actual, Loc));
13883 Set_Associated_Node
13884 (Explicit_Generic_Actual_Parameter (Ndec), Def);
13886 Append (Ndec, Assoc1);
13888 -- If there are other defaults, add a dummy association in case
13889 -- there are other defaulted formals with the same name.
13891 elsif Present (Next (Act2)) then
13892 Ndec :=
13893 Make_Generic_Association (Loc,
13894 Selector_Name => New_Occurrence_Of (Subp, Loc),
13895 Explicit_Generic_Actual_Parameter => Empty);
13897 Append (Ndec, Assoc1);
13898 end if;
13900 Next (Act2);
13901 end loop;
13902 end if;
13904 if Nkind (Name (N1)) = N_Identifier
13905 and then Is_Child_Unit (Gen_Id)
13906 and then Is_Global (Gen_Id)
13907 and then Is_Generic_Unit (Scope (Gen_Id))
13908 and then In_Open_Scopes (Scope (Gen_Id))
13909 then
13910 -- This is an instantiation of a child unit within a sibling, so
13911 -- that the generic parent is in scope. An eventual instance must
13912 -- occur within the scope of an instance of the parent. Make name
13913 -- in instance into an expanded name, to preserve the identifier
13914 -- of the parent, so it can be resolved subsequently.
13916 Rewrite (Name (N2),
13917 Make_Expanded_Name (Loc,
13918 Chars => Chars (Gen_Id),
13919 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13920 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13921 Set_Entity (Name (N2), Gen_Id);
13923 Rewrite (Name (N1),
13924 Make_Expanded_Name (Loc,
13925 Chars => Chars (Gen_Id),
13926 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
13927 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
13929 Set_Associated_Node (Name (N1), Name (N2));
13930 Set_Associated_Node (Prefix (Name (N1)), Empty);
13931 Set_Associated_Node
13932 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
13933 Set_Etype (Name (N1), Etype (Gen_Id));
13934 end if;
13936 end Save_Global_Defaults;
13938 ----------------------------
13939 -- Save_Global_Descendant --
13940 ----------------------------
13942 procedure Save_Global_Descendant (D : Union_Id) is
13943 N1 : Node_Id;
13945 begin
13946 if D in Node_Range then
13947 if D = Union_Id (Empty) then
13948 null;
13950 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
13951 Save_References (Node_Id (D));
13952 end if;
13954 elsif D in List_Range then
13955 if D = Union_Id (No_List) or else Is_Empty_List (List_Id (D)) then
13956 null;
13958 else
13959 N1 := First (List_Id (D));
13960 while Present (N1) loop
13961 Save_References (N1);
13962 Next (N1);
13963 end loop;
13964 end if;
13966 -- Element list or other non-node field, nothing to do
13968 else
13969 null;
13970 end if;
13971 end Save_Global_Descendant;
13973 ---------------------
13974 -- Save_References --
13975 ---------------------
13977 -- This is the recursive procedure that does the work once the enclosing
13978 -- generic scope has been established. We have to treat specially a
13979 -- number of node rewritings that are required by semantic processing
13980 -- and which change the kind of nodes in the generic copy: typically
13981 -- constant-folding, replacing an operator node by a string literal, or
13982 -- a selected component by an expanded name. In each of those cases, the
13983 -- transformation is propagated to the generic unit.
13985 procedure Save_References (N : Node_Id) is
13986 Loc : constant Source_Ptr := Sloc (N);
13988 begin
13989 if N = Empty then
13990 null;
13992 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
13993 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13994 Reset_Entity (N);
13996 elsif Nkind (N) = N_Operator_Symbol
13997 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
13998 then
13999 Change_Operator_Symbol_To_String_Literal (N);
14000 end if;
14002 elsif Nkind (N) in N_Op then
14003 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14004 if Nkind (N) = N_Op_Concat then
14005 Set_Is_Component_Left_Opnd (N,
14006 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14008 Set_Is_Component_Right_Opnd (N,
14009 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14010 end if;
14012 Reset_Entity (N);
14014 else
14015 -- Node may be transformed into call to a user-defined operator
14017 N2 := Get_Associated_Node (N);
14019 if Nkind (N2) = N_Function_Call then
14020 E := Entity (Name (N2));
14022 if Present (E)
14023 and then Is_Global (E)
14024 then
14025 Set_Etype (N, Etype (N2));
14026 else
14027 Set_Associated_Node (N, Empty);
14028 Set_Etype (N, Empty);
14029 end if;
14031 elsif Nkind_In (N2, N_Integer_Literal,
14032 N_Real_Literal,
14033 N_String_Literal)
14034 then
14035 if Present (Original_Node (N2))
14036 and then Nkind (Original_Node (N2)) = Nkind (N)
14037 then
14039 -- Operation was constant-folded. Whenever possible,
14040 -- recover semantic information from unfolded node,
14041 -- for ASIS use.
14043 Set_Associated_Node (N, Original_Node (N2));
14045 if Nkind (N) = N_Op_Concat then
14046 Set_Is_Component_Left_Opnd (N,
14047 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14048 Set_Is_Component_Right_Opnd (N,
14049 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14050 end if;
14052 Reset_Entity (N);
14054 else
14055 -- If original node is already modified, propagate
14056 -- constant-folding to template.
14058 Rewrite (N, New_Copy (N2));
14059 Set_Analyzed (N, False);
14060 end if;
14062 elsif Nkind (N2) = N_Identifier
14063 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14064 then
14065 -- Same if call was folded into a literal, but in this case
14066 -- retain the entity to avoid spurious ambiguities if it is
14067 -- overloaded at the point of instantiation or inlining.
14069 Rewrite (N, New_Copy (N2));
14070 Set_Analyzed (N, False);
14071 end if;
14072 end if;
14074 -- Complete operands check if node has not been constant-folded
14076 if Nkind (N) in N_Op then
14077 Save_Entity_Descendants (N);
14078 end if;
14080 elsif Nkind (N) = N_Identifier then
14081 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14083 -- If this is a discriminant reference, always save it. It is
14084 -- used in the instance to find the corresponding discriminant
14085 -- positionally rather than by name.
14087 Set_Original_Discriminant
14088 (N, Original_Discriminant (Get_Associated_Node (N)));
14089 Reset_Entity (N);
14091 else
14092 N2 := Get_Associated_Node (N);
14094 if Nkind (N2) = N_Function_Call then
14095 E := Entity (Name (N2));
14097 -- Name resolves to a call to parameterless function. If
14098 -- original entity is global, mark node as resolved.
14100 if Present (E)
14101 and then Is_Global (E)
14102 then
14103 Set_Etype (N, Etype (N2));
14104 else
14105 Set_Associated_Node (N, Empty);
14106 Set_Etype (N, Empty);
14107 end if;
14109 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14110 and then Is_Entity_Name (Original_Node (N2))
14111 then
14112 -- Name resolves to named number that is constant-folded,
14113 -- We must preserve the original name for ASIS use, and
14114 -- undo the constant-folding, which will be repeated in
14115 -- each instance.
14117 Set_Associated_Node (N, Original_Node (N2));
14118 Reset_Entity (N);
14120 elsif Nkind (N2) = N_String_Literal then
14122 -- Name resolves to string literal. Perform the same
14123 -- replacement in generic.
14125 Rewrite (N, New_Copy (N2));
14127 elsif Nkind (N2) = N_Explicit_Dereference then
14129 -- An identifier is rewritten as a dereference if it is the
14130 -- prefix in an implicit dereference (call or attribute).
14131 -- The analysis of an instantiation will expand the node
14132 -- again, so we preserve the original tree but link it to
14133 -- the resolved entity in case it is global.
14135 if Is_Entity_Name (Prefix (N2))
14136 and then Present (Entity (Prefix (N2)))
14137 and then Is_Global (Entity (Prefix (N2)))
14138 then
14139 Set_Associated_Node (N, Prefix (N2));
14141 elsif Nkind (Prefix (N2)) = N_Function_Call
14142 and then Is_Global (Entity (Name (Prefix (N2))))
14143 then
14144 Rewrite (N,
14145 Make_Explicit_Dereference (Loc,
14146 Prefix => Make_Function_Call (Loc,
14147 Name =>
14148 New_Occurrence_Of
14149 (Entity (Name (Prefix (N2))), Loc))));
14151 else
14152 Set_Associated_Node (N, Empty);
14153 Set_Etype (N, Empty);
14154 end if;
14156 -- The subtype mark of a nominally unconstrained object is
14157 -- rewritten as a subtype indication using the bounds of the
14158 -- expression. Recover the original subtype mark.
14160 elsif Nkind (N2) = N_Subtype_Indication
14161 and then Is_Entity_Name (Original_Node (N2))
14162 then
14163 Set_Associated_Node (N, Original_Node (N2));
14164 Reset_Entity (N);
14166 else
14167 null;
14168 end if;
14169 end if;
14171 elsif Nkind (N) in N_Entity then
14172 null;
14174 else
14175 declare
14176 Qual : Node_Id := Empty;
14177 Typ : Entity_Id := Empty;
14178 Nam : Node_Id;
14180 use Atree.Unchecked_Access;
14181 -- This code section is part of implementing an untyped tree
14182 -- traversal, so it needs direct access to node fields.
14184 begin
14185 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14186 N2 := Get_Associated_Node (N);
14188 if No (N2) then
14189 Typ := Empty;
14191 else
14192 Typ := Etype (N2);
14194 -- In an instance within a generic, use the name of the
14195 -- actual and not the original generic parameter. If the
14196 -- actual is global in the current generic it must be
14197 -- preserved for its instantiation.
14199 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14200 and then
14201 Present (Generic_Parent_Type (Parent (Typ)))
14202 then
14203 Typ := Base_Type (Typ);
14204 Set_Etype (N2, Typ);
14205 end if;
14206 end if;
14208 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14209 Set_Associated_Node (N, Empty);
14211 -- If the aggregate is an actual in a call, it has been
14212 -- resolved in the current context, to some local type.
14213 -- The enclosing call may have been disambiguated by the
14214 -- aggregate, and this disambiguation might fail at
14215 -- instantiation time because the type to which the
14216 -- aggregate did resolve is not preserved. In order to
14217 -- preserve some of this information, we wrap the
14218 -- aggregate in a qualified expression, using the id of
14219 -- its type. For further disambiguation we qualify the
14220 -- type name with its scope (if visible) because both
14221 -- id's will have corresponding entities in an instance.
14222 -- This resolves most of the problems with missing type
14223 -- information on aggregates in instances.
14225 if Nkind (N2) = Nkind (N)
14226 and then Nkind (Parent (N2)) in N_Subprogram_Call
14227 and then Comes_From_Source (Typ)
14228 then
14229 if Is_Immediately_Visible (Scope (Typ)) then
14230 Nam :=
14231 Make_Selected_Component (Loc,
14232 Prefix =>
14233 Make_Identifier (Loc, Chars (Scope (Typ))),
14234 Selector_Name =>
14235 Make_Identifier (Loc, Chars (Typ)));
14236 else
14237 Nam := Make_Identifier (Loc, Chars (Typ));
14238 end if;
14240 Qual :=
14241 Make_Qualified_Expression (Loc,
14242 Subtype_Mark => Nam,
14243 Expression => Relocate_Node (N));
14244 end if;
14245 end if;
14247 Save_Global_Descendant (Field1 (N));
14248 Save_Global_Descendant (Field2 (N));
14249 Save_Global_Descendant (Field3 (N));
14250 Save_Global_Descendant (Field5 (N));
14252 if Present (Qual) then
14253 Rewrite (N, Qual);
14254 end if;
14256 -- All other cases than aggregates
14258 else
14259 Save_Global_Descendant (Field1 (N));
14260 Save_Global_Descendant (Field2 (N));
14261 Save_Global_Descendant (Field3 (N));
14262 Save_Global_Descendant (Field4 (N));
14263 Save_Global_Descendant (Field5 (N));
14264 end if;
14265 end;
14266 end if;
14268 -- If a node has aspects, references within their expressions must
14269 -- be saved separately, given they are not directly in the tree.
14271 if Has_Aspects (N) then
14272 declare
14273 Aspect : Node_Id;
14275 begin
14276 Aspect := First (Aspect_Specifications (N));
14277 while Present (Aspect) loop
14278 if Present (Expression (Aspect)) then
14279 Save_Global_References (Expression (Aspect));
14280 end if;
14282 Next (Aspect);
14283 end loop;
14284 end;
14285 end if;
14286 end Save_References;
14288 -- Start of processing for Save_Global_References
14290 begin
14291 Gen_Scope := Current_Scope;
14293 -- If the generic unit is a child unit, references to entities in the
14294 -- parent are treated as local, because they will be resolved anew in
14295 -- the context of the instance of the parent.
14297 while Is_Child_Unit (Gen_Scope)
14298 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14299 loop
14300 Gen_Scope := Scope (Gen_Scope);
14301 end loop;
14303 Save_References (N);
14304 end Save_Global_References;
14306 --------------------------------------
14307 -- Set_Copied_Sloc_For_Inlined_Body --
14308 --------------------------------------
14310 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14311 begin
14312 Create_Instantiation_Source (N, E, True, S_Adjustment);
14313 end Set_Copied_Sloc_For_Inlined_Body;
14315 ---------------------
14316 -- Set_Instance_Of --
14317 ---------------------
14319 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14320 begin
14321 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14322 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14323 Generic_Renamings.Increment_Last;
14324 end Set_Instance_Of;
14326 --------------------
14327 -- Set_Next_Assoc --
14328 --------------------
14330 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14331 begin
14332 Generic_Renamings.Table (E).Next_In_HTable := Next;
14333 end Set_Next_Assoc;
14335 -------------------
14336 -- Start_Generic --
14337 -------------------
14339 procedure Start_Generic is
14340 begin
14341 -- ??? More things could be factored out in this routine.
14342 -- Should probably be done at a later stage.
14344 Generic_Flags.Append (Inside_A_Generic);
14345 Inside_A_Generic := True;
14347 Expander_Mode_Save_And_Set (False);
14348 end Start_Generic;
14350 ----------------------
14351 -- Set_Instance_Env --
14352 ----------------------
14354 procedure Set_Instance_Env
14355 (Gen_Unit : Entity_Id;
14356 Act_Unit : Entity_Id)
14358 Assertion_Status : constant Boolean := Assertions_Enabled;
14359 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14360 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14362 begin
14363 -- Regardless of the current mode, predefined units are analyzed in the
14364 -- most current Ada mode, and earlier version Ada checks do not apply
14365 -- to predefined units. Nothing needs to be done for non-internal units.
14366 -- These are always analyzed in the current mode.
14368 if Is_Internal_File_Name
14369 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14370 Renamings_Included => True)
14371 then
14372 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14374 -- In Ada2012 we may want to enable assertions in an instance of a
14375 -- predefined unit, in which case we need to preserve the current
14376 -- setting for the Assertions_Enabled flag. This will become more
14377 -- critical when pre/postconditions are added to predefined units,
14378 -- as is already the case for some numeric libraries.
14380 if Ada_Version >= Ada_2012 then
14381 Assertions_Enabled := Assertion_Status;
14382 end if;
14384 -- SPARK_Mode for an instance is the one applicable at the point of
14385 -- instantiation.
14387 SPARK_Mode := Save_SPARK_Mode;
14388 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14390 -- Make sure dynamic elaboration checks are off in SPARK Mode
14392 if SPARK_Mode = On then
14393 Dynamic_Elaboration_Checks := False;
14394 end if;
14395 end if;
14397 Current_Instantiated_Parent :=
14398 (Gen_Id => Gen_Unit,
14399 Act_Id => Act_Unit,
14400 Next_In_HTable => Assoc_Null);
14401 end Set_Instance_Env;
14403 -----------------
14404 -- Switch_View --
14405 -----------------
14407 procedure Switch_View (T : Entity_Id) is
14408 BT : constant Entity_Id := Base_Type (T);
14409 Priv_Elmt : Elmt_Id := No_Elmt;
14410 Priv_Sub : Entity_Id;
14412 begin
14413 -- T may be private but its base type may have been exchanged through
14414 -- some other occurrence, in which case there is nothing to switch
14415 -- besides T itself. Note that a private dependent subtype of a private
14416 -- type might not have been switched even if the base type has been,
14417 -- because of the last branch of Check_Private_View (see comment there).
14419 if not Is_Private_Type (BT) then
14420 Prepend_Elmt (Full_View (T), Exchanged_Views);
14421 Exchange_Declarations (T);
14422 return;
14423 end if;
14425 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14427 if Present (Full_View (BT)) then
14428 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14429 Exchange_Declarations (BT);
14430 end if;
14432 while Present (Priv_Elmt) loop
14433 Priv_Sub := (Node (Priv_Elmt));
14435 -- We avoid flipping the subtype if the Etype of its full view is
14436 -- private because this would result in a malformed subtype. This
14437 -- occurs when the Etype of the subtype full view is the full view of
14438 -- the base type (and since the base types were just switched, the
14439 -- subtype is pointing to the wrong view). This is currently the case
14440 -- for tagged record types, access types (maybe more?) and needs to
14441 -- be resolved. ???
14443 if Present (Full_View (Priv_Sub))
14444 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14445 then
14446 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14447 Exchange_Declarations (Priv_Sub);
14448 end if;
14450 Next_Elmt (Priv_Elmt);
14451 end loop;
14452 end Switch_View;
14454 -----------------
14455 -- True_Parent --
14456 -----------------
14458 function True_Parent (N : Node_Id) return Node_Id is
14459 begin
14460 if Nkind (Parent (N)) = N_Subunit then
14461 return Parent (Corresponding_Stub (Parent (N)));
14462 else
14463 return Parent (N);
14464 end if;
14465 end True_Parent;
14467 -----------------------------
14468 -- Valid_Default_Attribute --
14469 -----------------------------
14471 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
14472 Attr_Id : constant Attribute_Id :=
14473 Get_Attribute_Id (Attribute_Name (Def));
14474 T : constant Entity_Id := Entity (Prefix (Def));
14475 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
14476 F : Entity_Id;
14477 Num_F : Int;
14478 OK : Boolean;
14480 begin
14481 if No (T) or else T = Any_Id then
14482 return;
14483 end if;
14485 Num_F := 0;
14486 F := First_Formal (Nam);
14487 while Present (F) loop
14488 Num_F := Num_F + 1;
14489 Next_Formal (F);
14490 end loop;
14492 case Attr_Id is
14493 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
14494 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
14495 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
14496 Attribute_Unbiased_Rounding =>
14497 OK := Is_Fun
14498 and then Num_F = 1
14499 and then Is_Floating_Point_Type (T);
14501 when Attribute_Image | Attribute_Pred | Attribute_Succ |
14502 Attribute_Value | Attribute_Wide_Image |
14503 Attribute_Wide_Value =>
14504 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
14506 when Attribute_Max | Attribute_Min =>
14507 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
14509 when Attribute_Input =>
14510 OK := (Is_Fun and then Num_F = 1);
14512 when Attribute_Output | Attribute_Read | Attribute_Write =>
14513 OK := (not Is_Fun and then Num_F = 2);
14515 when others =>
14516 OK := False;
14517 end case;
14519 if not OK then
14520 Error_Msg_N
14521 ("attribute reference has wrong profile for subprogram", Def);
14522 end if;
14523 end Valid_Default_Attribute;
14525 end Sem_Ch12;